|
17 | 17 | import sys |
18 | 18 | from datetime import datetime |
19 | 19 | import numpy as np |
| 20 | +import shutil |
20 | 21 |
|
21 | 22 | # Provide the function with a filename (don't include .nc), a nominal depth, |
22 | 23 | # and pairs of names and arrays containing the data to be included as variables. |
|
26 | 27 | # For example, netcdf_gen('test',30,'PRES',pres_data,'TEMP',temp_data) |
27 | 28 | # from the command line gen_test_data.py test 30 PRES 10,20,30 TEMP 11,12,NaN |
28 | 29 |
|
| 30 | + |
| 31 | + |
29 | 32 | def netcdf_gen(file_name, nominal_depth, *args): |
30 | 33 | # Convert the args tuple to a list |
31 | 34 | args = list(args) |
@@ -93,10 +96,49 @@ def netcdf_gen(file_name, nominal_depth, *args): |
93 | 96 | ds.createVariable(name_in, "f8", ("TIME")) |
94 | 97 | ds.variables[name_in][:] = data_in |
95 | 98 |
|
| 99 | + # read the variable names from the netCDF dataset |
| 100 | + vars = ds.variables |
| 101 | + |
| 102 | + # create a list of variables, don't include the 'TIME' variable |
| 103 | + # TODO: detect 'TIME' variable using the standard name 'time' |
| 104 | + to_add = [] |
| 105 | + |
| 106 | + for v in vars: |
| 107 | + #print (vars[v].dimensions) |
| 108 | + if v != 'TIME': |
| 109 | + to_add.append(v) |
| 110 | + |
| 111 | + # for each variable, add a new ancillary variable <VAR>_quality_control to each which has 'TIME' as a dimension |
| 112 | + for v in to_add: |
| 113 | + if "TIME" in vars[v].dimensions: |
| 114 | + # print("time dim ", v) |
| 115 | + |
| 116 | + if v+"_quality_control" not in ds.variables: |
| 117 | + ncVarOut = ds.createVariable(v+"_quality_control", "i1", vars[v].dimensions, fill_value=99, zlib=True) # fill_value=99 otherwise defaults to max, imos-toolbox uses 99 |
| 118 | + ncVarOut[:] = np.zeros(vars[v].shape) |
| 119 | + ncVarOut.long_name = "quality_code for " + v |
| 120 | + ncVarOut.flag_values = np.array([0, 1, 2, 3, 4, 6, 7, 9]) |
| 121 | + ncVarOut.flag_meanings = 'unknown good_data probably_good_data probably_bad_data bad_data not_deployed interpolated missing_value' |
| 122 | + |
| 123 | + |
| 124 | + vars[v].ancillary_variables = v + "_quality_control" |
| 125 | + |
| 126 | + # update the global attributes |
| 127 | + ds.file_version = "Level 1 - Quality Controlled Data" |
| 128 | + |
| 129 | + ds.history = datetime.utcnow().strftime("%Y%m%d:") + ' converted to FV01 file, quality_control variables added.' |
| 130 | + |
| 131 | + # ADD quality control attributes!! |
| 132 | + |
96 | 133 | ds.close() |
| 134 | + |
| 135 | + #add_qc(file_name) |
| 136 | + |
97 | 137 | print("generated ", file_name) |
98 | 138 |
|
99 | 139 | return (file_name) |
| 140 | + |
| 141 | + |
100 | 142 |
|
101 | 143 | else: |
102 | 144 | print('Data arrays not of equal length') |
|
0 commit comments