-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
371 lines (347 loc) · 15.9 KB
/
utils.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import datetime
import numpy as np
import xarray as xr
import yaml
import ioos_qc
from ioos_qc.config import Config
from ioos_qc.qartod import aggregate
from ioos_qc.streams import XarrayStream
from ioos_qc.results import collect_results, CollectedResult
import datetime
from pathlib import Path
import logging
_log = logging.getLogger(__name__)
def get_configs():
configs = {
"TEMP": {
"TEMP": {
"qartod": {
"gross_range_test": {"suspect_span": [0, 30], "fail_span": [-2.5, 40]},
"spike_test": {"suspect_threshold": 2.0, "fail_threshold": 6.0},
"location_test": {"bbox": [10, 50, 25, 70]},
}
}
},
"PSAL": {
"CNDC": {
"qartod": {
"gross_range_test": {"suspect_span": [6, 42], "fail_span": [3, 45]}
}
},
"PSAL": {
"qartod": {
"gross_range_test": {"suspect_span": [5, 38], "fail_span": [2, 41]},
"spike_test": {"suspect_threshold": 0.3, "fail_threshold": 0.9},
"location_test": {"bbox": [10, 50, 25, 70]},
}
}
},
"CNDC": {
"CNDC": {
"qartod": {
"gross_range_test": {"suspect_span": [6, 42], "fail_span": [3, 45]},
"location_test": {"bbox": [10, 50, 25, 70]},
}
},
},
"DOXY": {
"DOXY": {
"qartod": {
"gross_range_test": {"suspect_span": [0, 350], "fail_span": [0, 500]},
"spike_test": {"suspect_threshold": 10, "fail_threshold": 50},
"location_test": {"bbox": [10, 50, 25, 70]},
}
}
},
"CPWC": {
"CPWC": {
"qartod": {
"gross_range_test": {"suspect_span": [0, 15], "fail_span": [-1, 20]},
"spike_test": {"suspect_threshold": 1, "fail_threshold": 5},
"location_test": {"bbox": [10, 50, 25, 70]},
}
}
}
}
return configs
def apply_ioos_flags(ds, config):
if not set(config.keys()).issubset(set(list(ds))):
_log.warning(f"{ds.keys} not found in dataset. Skipping")
return None, None
c = Config(config)
qc = XarrayStream(ds, lon="LONX", lat="LATX")
runner = list(qc.run(c))
results = collect_results(runner, how='list')
agg = CollectedResult(
stream_id='',
package='qartod',
test='qc_rollup',
function=aggregate,
results=aggregate(results),
tinp=qc.time(),
data=ds
)
flag_vals = agg.results
call = c.calls
proc_record = str(call)
return flag_vals, proc_record
def flag_ioos(ds):
configs = get_configs()
for config_name, config in configs.items():
if config_name not in list(ds):
_log.warning(f"{config_name} not found in dataset")
continue
# extract ioos flags for these variables
flags, comment = apply_ioos_flags(ds, config)
flagged_prop = 100 * sum(np.logical_and(flags > 1, flags < 9)) / len(flags)
_log.info(f"Flagged {flagged_prop.round(3)} % of {config_name} as bad")
# Apply flags and add comment
ioos_comment = f"Quality control flags from IOOS QC QARTOD https://github.com/ioos/ioos_qc Version: " \
f"{ioos_qc.__version__}. Using config: {comment}."
if "temperature" in config.keys() or "salinity" in config.keys() or "conductivity" in config.keys():
ioos_comment = f"{ioos_comment} Threshold values from EuroGOOS DATA-MEQ Working Group (2010)" \
f" Recommendations for in-situ data Near Real Time Quality Control [Version 1.2]. EuroGOOS" \
f", 23pp. DOI http://dx.doi.org/10.25607/OBP-214."
flag = ds[config_name].copy()
flag.values = flags
parent_attrs = flag.attrs
flag.attrs = {
'ioos_qc_module': 'qartod',
"quality_control_conventions": "IOOS QARTOD standard flags",
"quality_control_set": 1,
"valid_min": 1,
"valid_max": 9,
"flag_values": [1, 2, 3, 4, 9],
'flag_meanings': 'GOOD, UNKNOWN, SUSPECT, FAIL, MISSING',
"long_name": f"quality control flags for {parent_attrs['long_name']}",
"standard_name": f"{parent_attrs['standard_name']}_flag",
"comment": ioos_comment}
ds[f"{config_name}_qc"] = flag
ds.attrs["processing_level"] = f"L1. Quality control flags from IOOS QC QARTOD https://github.com/ioos/ioos_qc " \
f"Version: {ioos_qc.__version__} "
ds.attrs["disclaimer"] = "Data, products and services from VOTO are provided 'as is' without any warranty as" \
" to fitness for a particular purpose."
return ds
def encode_times(ds):
if 'units' in ds.time.attrs.keys():
ds.time.attrs.pop('units')
if 'calendar' in ds.time.attrs.keys():
ds.time.attrs.pop('calendar')
ds["time"].encoding["units"] = 'seconds since 1970-01-01T00:00:00Z'
for var_name in list(ds):
if "time" in var_name.lower() and not var_name == "time":
for drop_attr in ['units', 'calendar', 'dtype']:
if drop_attr in ds[var_name].attrs.keys():
ds[var_name].attrs.pop(drop_attr)
ds[var_name].encoding = ds["time"].encoding
return ds
clean_names = {"Press [dbar]": "pressure",
"Temp [°C]": "temperature",
"Temp. [deg C]": "temperature",
"Salinity": "salinity",
"SALIN [PSU]": "salinity",
"Cond [mS/cm]": "conductivity",
'Density [kg/m3]': "density",
'Density [kg/m^3]': "density",
'DO [μmol/L]': 'oxygen_concentration',
'Chl_A [µg/l]': 'chlorophyll',
'Chl-a [ug/l]': 'chlorophyll',
'sonde_name': 'sonde_name',
'sonde_number': 'sonde_number',
'calibration_date': 'calibration_date',
'filename': 'filename',
'latitude': 'latitude',
'cast_number': 'cast_number',
'longitude': 'longitude',
"Legato3 Pressure (bar)": "pressure",
"Legato3 Temperature (deg C)": "temperature",
"Legato3 Salinity (PSU)": "salinity",
"Legato3 Conductivity (mS/cm)": "conductivity",
'Legato3 Oxygen Concentration (umol/L)': 'oxygen_concentration',
'INX Latitude (deg)': 'latitude',
'INX Longitude (deg)': 'longitude',
'time': 'time'
}
attrs_dict = {"sonde_name": {"comment": "model name of CTD"},
"sonde_number": {"comment": "serial number of CTD"},
"cast_number": {"comment": "cast number"},
"calibration_date": {"comment": "date of last calibration"},
"filename":
{"comment": "source filename"},
"latitude": {'coordinate_reference_frame': 'urn:ogc:crs:EPSG::4326',
'long_name': 'latitude',
'observation_type': 'measured',
'platform': 'platform',
'reference': 'WGS84',
'standard_name': 'latitude',
'units': 'degrees_north',
'valid_max': '90.0',
'valid_min': '-90.0',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/LATX/',
'SDN': 'SDN:P09::LATX'
},
"longitude":
{'coordinate_reference_frame': 'urn:ogc:crs:EPSG::4326',
'long_name': 'longitude',
'observation_type': 'measured',
'platform': 'platform',
'reference': 'WGS84',
'standard_name': 'longitude',
'units': 'degrees_east',
'valid_max': '180.0',
'valid_min': '-180.0',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/LONX/',
'SDN': 'SDN:P09::LONX' },
"oxygen_concentration":
{
'long_name': 'oxygen concentration',
'observation_type': 'calculated',
'standard_name': 'mole_concentration_of_dissolved_molecular_oxygen_in_sea_water',
'units': 'mmol m-3',
'valid_max': '425.',
'valid_min': '0.',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/DOXY/',
'SDN': 'SDN:P09::DOXY'
},
"chlorophyll":
{
'long_name': 'chlorophyll',
'observation_type': 'calculated',
'standard_name': 'concentration_of_chlorophyll_in_sea_water',
'units': 'mg m-3',
'valid_max': '50.',
'valid_min': '0.',
'URI': 'https://vocab.nerc.ac.uk/collection/P02/current/CPWC/',
},
"density": {'long_name': 'Density',
'standard_name': 'sea_water_density',
'units': 'kg m-3',
'comment': 'raw, uncorrected salinity',
'observation_type': 'calculated',
'sources': 'salinity temperature pressure',
'valid_min': '1000.0',
'valid_max': '1040.0',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/SIGT/',
'SDN': 'SDN:P09::SIGT'
},
"conductivity": {
'instrument': 'instrument_ctd',
'long_name': 'water conductivity',
'observation_type': 'measured',
'standard_name': 'sea_water_electrical_conductivity',
'units': 'mS cm-1',
'valid_max': '85.',
'valid_min': '0.',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/CNDC/',
'SDN': 'SDN:P09::CNDC'
},
"pressure": {
'comment': 'ctd pressure sensor',
'instrument': 'instrument_ctd',
'long_name': 'water pressure',
'observation_type': 'measured',
'positive': 'down',
'reference_datum': 'sea-surface',
'standard_name': 'sea_water_pressure',
'units': 'dbar',
'valid_max': '1000',
'valid_min': '0',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/PRES/',
'SDN': 'SDN:P09::PRES'
},
"salinity": {'long_name': 'water salinity',
'standard_name': 'sea_water_practical_salinity',
'units': '1e-3',
'comment': 'raw, uncorrected salinity',
'sources': 'conductivity temperature pressure',
'observation_type': 'calulated',
'instrument': 'instrument_ctd',
'valid_max': '40.0',
'valid_min': '0.0',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/PSAL/',
'SDN': 'SDN:P09::PSAL' },
"temperature":
{
'long_name': 'water temperature',
'observation_type': 'measured',
'standard_name': 'sea_water_temperature',
'units': 'Celsius',
'valid_max': '42',
'valid_min': '-5',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/TEMP/',
'SDN': 'SDN:P09::TEMP'
},
"time":
{
'long_name': 'datetime',
'observation_type': 'measured',
'standard_name': 'datetime',
'URI': 'https://vocab.nerc.ac.uk/collection/P09/current/TIME/',
'SDN': 'SDN:P09::TIME'
},
}
def get_attrs(ds):
date_created = str(datetime.datetime.now())
attrs = {
'acknowledgement': 'This study used data collected and made freely available by Voice of the Ocean Foundation ('
'https://voiceoftheocean.org)',
'area': 'Baltic Sea',
'cdm_data_type': 'TrajectoryProfile',
'conventions': 'CF-1.10',
'institution_country': 'SWE',
'creator_email': '[email protected]',
'creator_name': 'Callum Rollo',
'creator_type': 'Person',
'creator_url': 'https://observations.voiceoftheocean.org',
'date_created': date_created,
'date_issued': date_created,
'geospatial_lat_max': np.nanmax(ds.latitude),
'geospatial_lat_min': np.nanmin(ds.latitude),
'geospatial_lat_units': 'degrees_north',
'geospatial_lon_max': np.nanmax(ds.longitude),
'geospatial_lon_min': np.nanmin(ds.longitude),
'geospatial_lon_units': 'degrees_east',
'infoUrl': 'https://observations.voiceoftheocean.org',
'inspire': "ISO 19115",
'institution': 'Voice of the Ocean Foundation',
'institution_edmo_code': '5579',
'keywords': 'CTD, Oceans, Ocean Pressure, Water Pressure, Ocean Temperature, Water Temperature, Salinity/Density, '
'Conductivity, Density, Salinity',
'keywords_vocabulary': 'GCMD Science Keywords',
'licence': 'Creative Commons Attribution 4.0 (https://creativecommons.org/licenses/by/4.0/) This study used data collected and made freely available by Voice of the Ocean Foundation (https://voiceoftheocean.org) accessed from https://erddap.observations.voiceoftheocean.org/erddap/index.html',
"title": "CTD from glider deployment/recovery",
'disclaimer': "Data, products and services from VOTO are provided 'as is' without any warranty as to fitness for "
"a particular purpose.",
'QC_indicator': 'L1',
'qc_manual': 'https://cdn.ioos.noaa.gov/media/2020/03/QARTOD_TS_Manual_Update2_200324_final.pdf',
'qc_method':'https://github.com/ioos/ioos_qc',
'references': 'Voice of the Ocean Foundation',
'source': 'Voice of the Ocean Foundation',
'sourceUrl': 'https://observations.voiceoftheocean.org',
'standard_name_vocabulary':'CF Standard Name Table v70',
'time_coverage_end': str(np.nanmax(ds.time)).split(".")[0],
'time_coverage_start': str(np.nanmin(ds.time)).split(".")[0],
'variables': list(ds)
}
return attrs
def ds_from_df(df):
ds = xr.Dataset()
time_attr = attrs_dict['time']
ds['time'] = ('time', df["datetime"], time_attr)
for col_name in list(df):
if col_name in clean_names.keys():
name = clean_names[col_name]
ds[name] = ('time', df[col_name], attrs_dict[name])
ds.attrs = get_attrs(ds)
ds = encode_times(ds)
for var_name in list(ds.variables):
if not "URI" in ds[var_name].attrs.keys():
continue
new_name = ds[var_name].attrs["URI"].split("/")[-2]
if new_name in list(ds.variables):
continue
ds[new_name] = ds[var_name]
ds = ds.drop(var_name)
ds.attrs["variables"] = list(ds.variables)
ds = flag_ioos(ds)
return ds