-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_agg.py
304 lines (277 loc) · 8.7 KB
/
sensor_agg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/python
# Copyright (C) 2021 IST-SUPSI (www.supsi.ch/ist)
#
# Author: Daniele Strigaro
#
# This file is part of station_configurator.
#
# station_configurator is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# station_configurator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with station_configurator. If not, see <http://www.gnu.org/licenses/>.
import configparser
import os
from datetime import datetime, timezone, timedelta
import time
import sys
from io import StringIO
import csv
import pandas as pd
from statistics import stdev
import json
import yaml
# external lib
import requests
#
def time_consistency_check(x):
if len(x) >= 3:
sum_abs_val = abs(x[1] - x[0]) + abs(x[1] - x[2])
four_std = 4*stdev(x)
if sum_abs_val <= four_std:
return 103
else:
return False
else:
return False
def minimum_variability_check(x):
if sum(x)/len(x) == x[0]:
return False
else:
return 104
num = len(sys.argv)
if num > 1:
for i in range(1, num):
if sys.argv[i] == "-h" or sys.argv[i] == "?": # debug mode
print('-s = sensor/section name')
quit()
if sys.argv[i] == "-s": # debug mode
section_name = sys.argv[i+1]
if sys.argv[i] == "-c": # debug mode
config_file_path = sys.argv[i+1]
config = configparser.ConfigParser()
config.read(config_file_path)
section = config[section_name]
istsos_url = config['DEFAULT']['istsos']
service = config['DEFAULT']['service']
assigned_id = section['assignedagg_id']
### READING SENSOR METADATA
sensor_type = section['type']
separator = os.sep
with open(
os.path.join(
separator.join(config_file_path.split('/')[0:-1]),
'support',
section['driver'],
f'{sensor_type}.yaml'
)
) as f:
rs = yaml.safe_load(f)
outputs = rs['outputs'][1:]
print(outputs)
now = datetime.now(timezone.utc)
end_position = datetime(
now.year, now.month, now.day,
now.hour, now.minute, tzinfo=timezone.utc
)
begin_position = end_position - timedelta(
minutes=int(section['aggregation_time'])
)
event_time = f'{begin_position.isoformat()}/{end_position.isoformat()}'
url_get_data = (
f'{istsos_url}/{service}?request=GetObservation&'
f'offering=temporary&procedure={section_name}&'
f'eventTime={event_time}&observedProperty=:&'
'qualityIndex=True&responseFormat=text/plain'
'&service=SOS&version=1.0.0'
)
req = requests.get(
url_get_data,
auth=(
config['DEFAULT']['user'],
config['DEFAULT']['password']
)
)
if req.status_code == 200:
data = req.text
else:
raise Exception('ERROR in loading file')
expected_num_values = (
int(config[section_name]['aggregation_time']) /
int(config[section_name]['sampling_time'])
)
df = pd.read_csv(
StringIO(data),
index_col=0,
parse_dates=True
)
df_data = df.drop(
['urn:ogc:def:procedure'], axis=1
)
columns = df_data.columns
col_idx = 0
for col in columns:
if 'quality' not in col and 'procedure' not in col:
#############
# STEP TEST #
#############
freq_sec = (int(config[section_name]['sampling_time'])*3)*60
ts_tmp2 = df_data.loc[
df_data[columns[col_idx+1]] == 101
]
ts_tmp2 = ts_tmp2[col].rolling(
'{}s'.format(freq_sec)
).apply(
lambda x: time_consistency_check(x),
raw=True
)
# updating main dataframe
df_data[columns[col_idx+1]].update(
ts_tmp2.where(lambda x : x>0)
)
################################################
# TIME CONSISTENCY - MINIMUM VARIABILITY CHECK #
################################################
ts_tmp3 = df_data.loc[
df_data[columns[col_idx+1]] == 202
]
freq_sec = int(config[section_name]['aggregation_time'])*60
ts_tmp3 = ts_tmp3[col].rolling(
'{}s'.format(freq_sec)
).apply(
lambda x: minimum_variability_check(x),
raw=True
)
# updating main dataframe
df_data[columns[col_idx+1]].update(
ts_tmp2.where(lambda x : x>0)
)
col_idx+=1
df_data.insert(0, 'T', df_data.index.strftime('%Y-%m-%dT%H:%M:%S%z'))
data_with_qi = df_data.values.tolist()
#### UPDATE DATA istSOS ###
#### Using InsertObservation #####
go = requests.get(
(
"{}/wa/istsos/services/{}/operations/getobservation"
"/offerings/temporary/procedures/{}/observedproperties/:/eventtime/last"
).format(
config['DEFAULT']['istsos'],
config['DEFAULT']['service'],
section_name
),
auth=(
config['DEFAULT']['user'],
config['DEFAULT']['password']
)
)
go = go.json()
go = go['data'][0]
go["samplingTime"] = {
"beginPosition": begin_position.isoformat(),
"endPosition": end_position.isoformat()
}
go['result']['DataArray']['values'] = data_with_qi
res = requests.post(
"%s/wa/istsos/services/%s/operations/"
"insertobservation" % (
config['DEFAULT']['istsos'],
config['DEFAULT']['service'],
),
auth=(
config['DEFAULT']['user'], config['DEFAULT']['password']
),
data=json.dumps({
"ForceInsert": "true",
"AssignedSensorId": section['assigned_id'],
"Observation": go
})
)
res.raise_for_status()
print(" > Insert observation success: %s" % (
res.json()['success']))
aggregators = ['AVG', 'SUM', 'MIN', 'MAX']
{
'name': 'water-Chl-a',
'definition': 'urn:ogc:def:parameter:x-istsos:1.0:water:Chl-a',
'uom': 'μg/L',
'description': '',
'constraint': {'role': 'urn:ogc:def:classifiers:x-istsos:1.0:qualityIndex:check:reasonable', 'interval': ['0', '400']},
'aggregator': 'AVG'
}
if not df_data.empty:
columns = df_data.columns[1:]
data_post = None
idx = 0
for col in columns:
if col.find('quality') < 0:
if 'aggregator' in outputs[idx]:
aggregator = outputs[idx]['aggregator']
else:
aggregator = 'AVG'
if aggregator not in aggregators:
aggregator = 'AVG'
df_filtered = df_data.loc[
df_data[columns[idx+1]] >= 104
]
if df_filtered.empty:
if aggregator == 'AVG':
mean_val = df_data[col].mean()
elif aggregator == 'SUM':
mean_val = df_data[col].sum()
elif aggregator == 'MIN':
mean_val = df_data[col].min()
elif aggregator == 'MAX':
mean_val = df_data[col].max()
mean_val = round(mean_val, 2)
min_qi = 200
else:
if aggregator == 'AVG':
mean_val = df_filtered[col].mean()
elif aggregator == 'SUM':
mean_val = df_filtered[col].sum()
elif aggregator == 'MIN':
mean_val = df_filtered[col].min()
elif aggregator == 'MAX':
mean_val = df_filtered[col].max()
min_qi = df_filtered[columns[idx+1]].min()
cnt_val = df_filtered[col].count()
perc = cnt_val/expected_num_values
if perc == 0:
if cnt_val > 0:
min_qi = 0
else:
min_qi = -100
elif perc < 0.6:
mean_val = round(mean_val, 2)
min_qi = 200
else:
mean_val = round(mean_val, 2)
min_qi = 201
if data_post:
data_post = f'{data_post},{mean_val}:{min_qi}'
else:
data_post = (
f'{assigned_id};{end_position.isoformat()},{mean_val}:{min_qi}'
)
req = requests.post(
'{}/wa/istsos/services/{}agg/operations/fastinsert'.format(
config['DEFAULT']['istsos'],
config['DEFAULT']['service'],
),
data=data_post,
auth=(config['DEFAULT']['user'], config['DEFAULT']['password'])
)
if req.status_code == 200:
print(req.text)
else:
print(False)
else:
print("No data to aggregate")