Skip to content
Snippets Groups Projects
Commit 9749b866 authored by William Roberts's avatar William Roberts
Browse files

Simplify and improve logic for 5 minute intervals

parent 1a5f5e3e
No related branches found
No related tags found
No related merge requests found
...@@ -70,55 +70,24 @@ class Updater(object): ...@@ -70,55 +70,24 @@ class Updater(object):
def rolling_average(self, record): def rolling_average(self, record):
self.start_time = self.start_time if self.start_time else record['timestamp'] self.start_time = self.start_time if self.start_time else record['timestamp']
time_interval = (record['timestamp'] - self.start_time).total_seconds() time_interval = (record['timestamp'] - self.start_time).total_seconds()
# Add in 5 minutes for cutoff for key in record:
start = record['timestamp'] - timedelta(seconds=time_interval % 300) if self.data.get(key) is None:
end = self.start_time + timedelta(minutes=5) self.data[key] = np.array([])
if time_interval > 300: self.data[key] = np.append(self.data[key], record[key])
for key in record:
if key == 'timestamp':
self.data[key] = np.append(self.data[key], end)
else:
self.data[key] = np.append(self.data[key], np.nan)
else:
for key in record:
if self.data.get(key) is None:
self.data[key] = np.array([])
self.data[key] = np.append(self.data[key], record[key])
# If 5 minutes of data are ready, average current data in dict. Holds up to 15 minutes. # If 5 minutes of data are ready, average current data in dict. Holds up to 15 minutes.
# 60 * 5 seconds = 5 minutes. # 60 * 5 seconds = 5 minutes.
if time_interval >= 300: if time_interval >= 300 or (record['timestamp'].minute % 5 == 0 and record['timestamp'].second == 0):
# Appending to a DataFrame is slow. Instead, this adds to a dict in chunks and passes it to the DataFrame. # Appending to a DataFrame is slow. Instead, this adds to a dict in chunks and passes it to the DataFrame.
frame = self._calculate_averages() frame = self._calculate_averages()
frame.fillna(value=np.nan, inplace=True) frame.fillna(value=np.nan, inplace=True)
print(frame['rel_hum'])
print(frame.resample('5T', closed='right').mean()['rel_hum'])
print(frame.asfreq('5T')['rel_hum'])
# Keep data set within minimum window to improve speed. # Keep data set within minimum window to improve speed.
# Wind gusts looks at 10 minute intervals, including the first data point which needs 2 minutes of data # Wind gusts looks at 10 minute intervals, including the first data point which needs 2 minutes of data
# before it, totalling 12 minutes. # before it, totalling 12 minutes.
test = frame.asfreq('5T') time_mask = frame.index > frame.index[-1] - timedelta(minutes=12)
if len(test.index) > 3: self.data = {key: val[time_mask] for key, val in self.data.items()}
new_frame = frame.copy() self.start_time = self.data['timestamp'][-1]
new_frame['timestamp'] = list(new_frame.index) if record['timestamp'].minute % 5 == 0 and record['timestamp'].second == 0:
data = new_frame.drop(new_frame.index[:new_frame.index.get_loc(test.index[-3])]).to_dict('list') return frame
for key in self.data:
self.data[key] = data[key]
if time_interval > 300:
for key in record:
if key == 'timestamp':
if end != start:
self.data[key] = np.array([start])
else:
if end != start:
self.data[key] = np.array([np.nan])
self.start_time = self.data['timestamp'][-1]
for key in record:
if self.data.get(key) is None:
self.data[key] = np.array([])
self.data[key] = np.append(self.data[key], record[key])
else:
self.start_time = self.data['timestamp'][-1]
return test
def _calculate_averages(self): def _calculate_averages(self):
KNOTS_9 = calc.knots_to_mps(9.) KNOTS_9 = calc.knots_to_mps(9.)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment