@@ -148,47 +148,56 @@ def wind_direction():
148148
149149def rainfall (seconds_since_last ):
150150 new_rain_entries = []
151- amount = 0
151+ amount = 0 # rain since last reading
152152 per_hour = 0
153153 today = 0
154- offset = 0
154+ offset = 0 # UTC offset hours
155155
156+ # configure offset variable for UK BST or timezone offset from config file
157+ # and BST lookup function
156158 if config .uk_bst == True :
157159 if helpers .uk_bst ():
158160 offset = 1
159161 elif config .utc_offset != 0 :
160162 offset += config .utc_offset
161163
164+ # determine current day number and timestamp
162165 now = helpers .timestamp (helpers .datetime_string ())
163166 now_day = helpers .timestamp_day (helpers .datetime_string (), offset )
164167 logging .info (f"> current day number is { now_day } " )
168+
169+ # process the rain file data
165170 if helpers .file_exists ("rain.txt" ):
166171 with open ("rain.txt" , "r" ) as rainfile :
167172 rain_entries = rainfile .read ().split ("\n " )
168173
169- # process the rain file data
174+ # populate latest, per second, today and last hour readings from rain log
175+ # file, write new rain log file dropping any yesterday readings
170176 for entry in rain_entries :
171177 if entry :
172178 ts = helpers .timestamp (entry )
173179 tsday = helpers .timestamp_day (entry , config .utc_offset )
174180 logging .info (f"> rain reading day number is { tsday } " )
175- # count how many rain ticks since the last reading
181+ # populate amount with rain since the last reading
176182 if now - ts < seconds_since_last :
177183 amount += RAIN_MM_PER_TICK
178- # Pick up any untracked yesterday data if current reading is a new day
179- # Techincally this should be yesterday, but capturing in today is much
180- # less complex than backdating in the readings file from here
184+ # add any rain ticks from yesterday since the previous reading
185+ # this will misallocate day totals, but will ensure the hourly total
186+ # is correct without introducing complexity backdating yesterday and
187+ # the error will be minimised with frequent readings
188+ # TODO sum yesterday rain and generate a rain_today reading with
189+ # 23:59:59 timestamp of yesterday
181190 if tsday != now_day :
182191 today += RAIN_MM_PER_TICK
183192 # count how many rain ticks in the last hour
184193 if now - ts < 3600 :
185194 per_hour += RAIN_MM_PER_TICK
186- # count how many rain ticks today and delete older entries
195+ # count how many rain ticks today and drop older entries for new file
187196 if tsday == now_day :
188197 today += RAIN_MM_PER_TICK
189198 new_rain_entries .append (entry )
190199
191- # write out adjusted rain log
200+ # write out new adjusted rain log
192201 with open ("rain.txt" , "w" ) as newrainfile :
193202 newrainfile .write ("\n " .join (new_rain_entries ))
194203
0 commit comments