@@ -148,39 +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 # UTC offset hours
155+
156+ # configure offset variable for UK BST or timezone offset from config file
157+ # and BST lookup function
158+ if config .uk_bst == True :
159+ if helpers .uk_bst ():
160+ offset = 1
161+ elif config .utc_offset != 0 :
162+ offset += config .utc_offset
163+
164+ # determine current day number and timestamp
154165 now = helpers .timestamp (helpers .datetime_string ())
155- now_day = helpers .timestamp_day (helpers .datetime_string (), config . utc_offset )
166+ now_day = helpers .timestamp_day (helpers .datetime_string (), offset )
156167 logging .info (f"> current day number is { now_day } " )
168+
169+ # process the rain file data
157170 if helpers .file_exists ("rain.txt" ):
158171 with open ("rain.txt" , "r" ) as rainfile :
159172 rain_entries = rainfile .read ().split ("\n " )
160173
161- # 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
162176 for entry in rain_entries :
163177 if entry :
164178 ts = helpers .timestamp (entry )
165179 tsday = helpers .timestamp_day (entry , config .utc_offset )
166180 logging .info (f"> rain reading day number is { tsday } " )
167- # count how many rain ticks since the last reading
181+ # populate amount with rain since the last reading
168182 if now - ts < seconds_since_last :
169183 amount += RAIN_MM_PER_TICK
170- # Pick up any untracked yesterday data if current reading is a new day
171- # Techincally this should be yesterday, but capturing in today is much
172- # 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
173190 if tsday != now_day :
174191 today += RAIN_MM_PER_TICK
175192 # count how many rain ticks in the last hour
176193 if now - ts < 3600 :
177194 per_hour += RAIN_MM_PER_TICK
178- # count how many rain ticks today and delete older entries
195+ # count how many rain ticks today and drop older entries for new file
179196 if tsday == now_day :
180197 today += RAIN_MM_PER_TICK
181198 new_rain_entries .append (entry )
182199
183- # write out adjusted rain log
200+ # write out new adjusted rain log
184201 with open ("rain.txt" , "w" ) as newrainfile :
185202 newrainfile .write ("\n " .join (new_rain_entries ))
186203
0 commit comments