-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.py
More file actions
429 lines (343 loc) · 16.3 KB
/
export.py
File metadata and controls
429 lines (343 loc) · 16.3 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
import json
import pandas as pd
import os
from loguru import logger
# Constants for validation
VALID_TEST_KEYS = ['int', 'binned', 'fixed']
def build_climatology_object(clim, testKey):
"""
Build climatology table with monthly ranges.
Args:
clim: Climatology data dictionary
testKey: Test type ('int', 'binned', or 'fixed')
Returns:
DataFrame with climatology ranges, or None if data is invalid
"""
# FIXED: Add defensive checks for None values
if clim is None:
logger.error(f"Climatology data is None for testKey={testKey}")
return None
if testKey not in VALID_TEST_KEYS:
logger.error(f"Invalid testKey: {testKey}. Must be one of {VALID_TEST_KEYS}")
return None
# FIXED: Check if the testKey exists in the clim dictionary
if testKey not in clim:
logger.error(f"TestKey '{testKey}' not found in climatology data. Available keys: {list(clim.keys())}")
return None
if 'binned' in testKey:
rows = []
# FIXED: Check if binned data exists and is valid
if not isinstance(clim['binned'], dict) or len(clim['binned']) == 0:
logger.error("No valid binned climatology data found")
return None
for depth_bin, values in clim['binned'].items():
# FIXED: Add None checks for values
if values is None:
logger.error(f"Climatology values are None for bin {depth_bin}")
continue
if not isinstance(values, dict):
logger.error(f"Climatology values are not a dictionary for bin {depth_bin}: {type(values)}")
continue
if 'lower' not in values or 'upper' not in values:
logger.error(f"Missing 'lower' or 'upper' keys for bin {depth_bin}")
continue
if values['lower'] is None or values['upper'] is None:
logger.error(f"Climatology lower/upper bounds are None for bin {depth_bin}")
continue
# Validate that we have 12 months of data
if len(values['lower']) != 12 or len(values['upper']) != 12:
logger.error(f"Expected 12 monthly values for bin {depth_bin}, "
f"got lower={len(values['lower'])}, upper={len(values['upper'])}")
continue
monthRanges = [f"[{values['lower'][i]:.2f}, {values['upper'][i]:.2f}]"
for i in range(12)]
# Prepend the depth bin
first = f"[{depth_bin[0]}, {depth_bin[1]}]"
row = [first] + monthRanges
rows.append(row)
# FIXED: Check if any valid rows were created
if len(rows) == 0:
logger.error("No valid binned climatology data could be processed")
return None
else:
# For integrated ('int') or fixed platform
# FIXED: Add None and type checks
if not isinstance(clim[testKey], dict):
logger.error(f"Climatology data for {testKey} is not a dictionary: {type(clim[testKey])}")
return None
if 'lower' not in clim[testKey] or 'upper' not in clim[testKey]:
logger.error(f"Missing 'lower' or 'upper' keys for {testKey}")
return None
if clim[testKey]['lower'] is None or clim[testKey]['upper'] is None:
logger.error(f"Climatology lower/upper bounds are None for {testKey}")
return None
# Validate that we have 12 months of data
if len(clim[testKey]['lower']) != 12 or len(clim[testKey]['upper']) != 12:
logger.error(f"Expected 12 monthly values for {testKey}, "
f"got lower={len(clim[testKey]['lower'])}, upper={len(clim[testKey]['upper'])}")
return None
first = "[0, 0]"
monthRanges = [f"[{clim[testKey]['lower'][i]:.2f}, {clim[testKey]['upper'][i]:.2f}]"
for i in range(12)]
rows = [[first] + monthRanges]
df_clim = pd.DataFrame(rows)
header = [""] + [f"[{i}, {i}]" for i in range(1, 13)]
df_out = pd.concat([pd.DataFrame([header]), df_clim], ignore_index=True)
return df_out
def build_gross_range_object(gr, testKey):
"""
Build gross range table.
Args:
gr: Gross range data dictionary
testKey: Test type ('int', 'binned', or 'fixed')
Returns:
DataFrame with gross range values, or None if data is invalid
"""
# FIXED: Add defensive checks
if gr is None:
logger.error(f"Gross range data is None for testKey={testKey}")
return None
if testKey not in VALID_TEST_KEYS:
logger.error(f"Invalid testKey: {testKey}. Must be one of {VALID_TEST_KEYS}")
return None
if testKey not in gr:
logger.error(f"TestKey '{testKey}' not found in gross range data. Available keys: {list(gr.keys())}")
return None
if 'binned' in testKey:
rows = []
if not isinstance(gr['binned'], dict) or len(gr['binned']) == 0:
logger.error("No valid binned gross range data found")
return None
for depth_bin, values in gr['binned'].items():
# Validate data exists
if values is None or not isinstance(values, dict):
logger.error(f"Invalid values for bin {depth_bin}")
continue
if 'lower' not in values or 'upper' not in values:
logger.error(f"Missing lower/upper values for bin {depth_bin}")
continue
lower_val = values['lower']
upper_val = values['upper']
# Handle both scalar and array cases
if isinstance(lower_val, (list, tuple)):
if len(lower_val) == 0:
logger.error(f"Empty lower value for bin {depth_bin}")
continue
lower_val = lower_val[0]
if isinstance(upper_val, (list, tuple)):
if len(upper_val) == 0:
logger.error(f"Empty upper value for bin {depth_bin}")
continue
upper_val = upper_val[0]
gr_range = [f"[{float(lower_val):.2f}, {float(upper_val):.2f}]"]
# Prepend the depth bin
first = f"[{depth_bin[0]}, {depth_bin[1]}]"
row = [first] + gr_range
rows.append(row)
if len(rows) == 0:
logger.error("No valid binned gross range data could be processed")
return None
else:
# For integrated ('int') or fixed platform
if not isinstance(gr[testKey], dict):
logger.error(f"Gross range data for {testKey} is not a dictionary: {type(gr[testKey])}")
return None
if 'lower' not in gr[testKey] or 'upper' not in gr[testKey]:
logger.error(f"Missing lower/upper values for {testKey}")
return None
lower_val = gr[testKey]['lower']
upper_val = gr[testKey]['upper']
# Handle both scalar and array cases
if isinstance(lower_val, (list, tuple)):
if len(lower_val) == 0:
logger.error(f"Empty lower value for {testKey}")
return None
lower_val = lower_val[0]
if isinstance(upper_val, (list, tuple)):
if len(upper_val) == 0:
logger.error(f"Empty upper value for {testKey}")
return None
upper_val = upper_val[0]
first = "[0, 0]"
gr_range = [f"[{float(lower_val):.2f}, {float(upper_val):.2f}]"]
rows = [[first] + gr_range]
df_gr = pd.DataFrame(rows)
df_out = pd.concat([pd.DataFrame([["", "gross_range"]]), df_gr], ignore_index=True)
return df_out
def write_lookup_csv(outfile, headers, row):
"""
Helper function to write CSV lookup files with minimal quoting.
Uses QUOTE_MINIMAL to avoid double-quoting JSON strings.
Args:
outfile: Path to output file
headers: List of column headers
row: List of row values
"""
try:
with open(outfile, "w", newline="") as f:
writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
writer.writerow(headers)
writer.writerow(row)
logger.info(f"Exported: {outfile}")
except IOError as e:
logger.error(f"Failed to write {outfile}: {e}")
raise
def exportTables(qartodDict, qartodTests_dict, site, node, sensor, stream, platform, pressParam):
"""
Cleaned and corrected QARTOD export function.
Produces:
- climatology tables + lookup CSV
- gross range tables + lookup CSV
"""
folderPath = os.path.join(os.path.expanduser('~'), 'qartod_staging')
os.makedirs(folderPath, exist_ok=True)
logger.info(f"Exporting QARTOD tables to {folderPath}")
for param, param_dict in qartodDict.items():
limits = qartodTests_dict[param]['limits']
# --------------------------------------------------
# CLIMATOLOGY
# --------------------------------------------------
if 'climatology' in param_dict:
for testKey in param_dict['climatology']:
if testKey not in VALID_TEST_KEYS:
logger.error(f"Unexpected key in climatology dict: {testKey}")
continue
try:
df_clim = build_climatology_object(param_dict['climatology'], testKey)
# FIXED: Check if build failed and skip this testKey
if df_clim is None:
logger.warning(f"Skipping climatology export for {param}/{testKey} due to invalid data")
continue
outfile = os.path.join(
folderPath,
f"{site}-{node}-{sensor}-{param}.climatology_table.csv.{testKey}"
)
df_clim.to_csv(outfile, index=False, header=False)
logger.info(f"Exported climatology table: {outfile}")
except Exception as e:
logger.error(f"Failed to export climatology table for {param}/{testKey}: {e}")
# FIXED: Continue instead of raising to allow other parameters to export
continue
# ------------- Climatology lookup file -------------
if 'profiler' in platform:
zinp = pressParam
notes = "Variance not reported for binned profiler climatology"
else:
zinp = None
# take notes from any valid key
val_keys = [k for k in param_dict['climatology'] if k in VALID_TEST_KEYS]
# FIXED: Handle case where no valid keys have notes
if val_keys:
clim_data = param_dict['climatology'][val_keys[0]]
if isinstance(clim_data, dict) and 'notes' in clim_data:
notes = clim_data['notes']
else:
notes = 'No notes available'
else:
notes = 'No valid climatology data available'
params_dict = {"inp": param, "tinp": "time", "zinp": zinp}
climatologyTable = f"climatology_tables/{site}-{node}-{sensor}-{param}.climatology_table.csv"
outfile = os.path.join(folderPath, f"{site}-{node}-{sensor}-{param}-climatology_test_values.csv")
lookup_row = [
site,
node,
sensor,
stream,
str(params_dict), # Python dict format (single quotes)
climatologyTable,
"",
notes
]
try:
with open(outfile, "w", newline="") as f:
writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
writer.writerow(["subsite","node","sensor","stream",
"parameters","climatologyTable","source","notes"])
writer.writerow(lookup_row)
logger.info(f"Exported: {outfile}")
except IOError as e:
logger.error(f"Failed to write {outfile}: {e}")
# FIXED: Continue instead of raising
continue
# --------------------------------------------------
# GROSS RANGE
# --------------------------------------------------
if 'gross_range' in param_dict:
for testKey in param_dict['gross_range']:
if testKey not in VALID_TEST_KEYS:
logger.error(f"Unexpected key in gross_range dict: {testKey}")
continue
try:
df_gr = build_gross_range_object(param_dict['gross_range'], testKey)
# FIXED: Check if build failed and skip this testKey
if df_gr is None:
logger.warning(f"Skipping gross range export for {param}/{testKey} due to invalid data")
continue
outfile = os.path.join(
folderPath,
f"{site}-{node}-{sensor}-{param}.gross_range_table.csv.{testKey}"
)
df_gr.to_csv(outfile, index=False, header=False)
logger.info(f"Exported gross range table: {outfile}")
except Exception as e:
logger.error(f"Failed gross range export {param}/{testKey}: {e}")
# FIXED: Continue instead of raising
continue
# ---- Gross Range Lookup File ----
val_keys = [k for k in param_dict['gross_range'] if k in VALID_TEST_KEYS]
# FIXED: Handle case where no valid keys exist
if not val_keys:
logger.error(f"No valid gross range keys found for {param}")
continue
testKey = val_keys[0]
lower_val = param_dict['gross_range'][testKey]['lower']
upper_val = param_dict['gross_range'][testKey]['upper']
# Normalize scalars
if isinstance(lower_val, (list, tuple)):
lower_val = float(lower_val[0]) if lower_val else 0.0
if isinstance(upper_val, (list, tuple)):
upper_val = float(upper_val[0]) if upper_val else 0.0
qc_dict = {
"subsite": site,
"node": node,
"sensor": sensor,
"stream": stream,
"parameters": {"inp": param},
"gross_range_suspect": [float(f"{lower_val:.2f}"), float(f"{upper_val:.2f}")],
"gross_range_fail": limits,
"notes": param_dict['gross_range'][testKey].get("notes", "No notes available")
}
qcConfig = {
"qartod": {
"gross_range_test": {
"suspect_span": qc_dict["gross_range_suspect"],
"fail_span": qc_dict["gross_range_fail"]
}
}
}
lookup_row = [
qc_dict['subsite'],
qc_dict['node'],
qc_dict['sensor'],
qc_dict['stream'],
str(qc_dict['parameters']), # Python dict format
str(qcConfig),
"",
qc_dict['notes']
]
outfile = os.path.join(folderPath, f"{site}-{node}-{sensor}-{param}-gross_range_test_values.csv")
try:
with open(outfile, "w", newline="") as f:
writer = csv.writer(f, quoting=csv.QUOTE_MINIMAL)
writer.writerow(["subsite","node","sensor","stream","parameters",
"qcConfig","source","notes"])
writer.writerow(lookup_row)
logger.info(f"Exported: {outfile}")
except IOError as e:
logger.error(f"Failed to write {outfile}: {e}")
# FIXED: Continue instead of raising
continue
logger.info("Export completed successfully")