-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmarine_qc.py
executable file
·357 lines (270 loc) · 14.8 KB
/
marine_qc.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
#!/usr/local/sci/bin/python2.7
"""
marine_qc.py invoked by typing::
python2.7 marine_qc.py -config configuration.txt -year1 1850 -year2 1855 -month1 1 -month2 1 [-tracking]
This quality controls data for the chosen years. The location of the data and the locations of the climatology files are
all to be specified in the configuration files:
Inputs
-year1
year of the first month to QC
-month1
month of the first month to QC
-year2
year of the last month to QC
-month2
month of the last month to QC
-config
specifies the configuration file to use
-tracking
switches on the tracking QC output, which produces one file per month per drifter ID in addition to other output and
performs matches with OSTIA background fields.
Inputs are specified in the configuration file and the parameters file (whose location is specified in the configuration
file.
Output from the QC is written to the out_dir specified in the configuration file. Each output file line start with
an ICOADS UID so that they can be stitched together at a later date.
"""
import gzip
import qc
from IMMA1 import IMMA
import Extended_IMMA as ex
import Climatology as clim
import BackgroundField as bf
import argparse
import ConfigParser
import json
import sys
def main(argv):
"""
This program reads in data from ICOADS.3.0.0/ICOADS.3.0.1 and applies quality control processes to it, flagging data
as good or bad according to a set of different criteria.
The first step of the process is to read in various SST and MAT climatologies from file. These are 1degree latitude
by 1 degree longitude by 73 pentad fields in NetCDF format.
The program then loops over all specified years and months reads in the data needed to QC that month and then
does the QC. There are three stages in the QC
basic QC - this proceeds one :class:`.MarineReport` at a time. Checks are relatively simple and detect gross errors
track check - this works on :class:`.Voyage` objects consisting of all the observations from a single ship (or
at least a single ID) and identifies observations which make for an implausible ship track
buddy check - this works on :class:`.Deck` objects which are large collections of observations and compares
observations to their neighbours. Buddy checks are performed on a range of different variables.
"""
print('########################')
print('Running make_and_full_qc')
print('########################')
parser = argparse.ArgumentParser(description='Marine QC system, main program')
parser.add_argument('-config', type=str, default='configuration.txt', help='name of config file')
parser.add_argument('-year1', type=int, default=1850, help='First year for processing')
parser.add_argument('-year2', type=int, default=1850, help='Final year for processing')
parser.add_argument('-month1', type=int, default=1, help='First month for processing')
parser.add_argument('-month2', type=int, default=1, help='Final month for processing')
parser.add_argument('-tracking', action='store_true', help='perform tracking QC')
args = parser.parse_args()
inputfile = args.config
year1 = args.year1
year2 = args.year2
month1 = args.month1
month2 = args.month2
tracking = args.tracking
print("running on ICOADS, this is not a test!")
print('Input file is {}'.format(inputfile))
print('Running from {} {} to {} {}'.format(month1, year1, month2, year2))
print('')
config = ConfigParser.ConfigParser()
config.read(inputfile)
icoads_dir = config.get('Directories', 'ICOADS_dir')
out_dir = config.get('Directories', 'out_dir')
bad_id_file = config.get('Files', 'IDs_to_exclude')
version = config.get('Icoads', 'icoads_version')
print('ICOADS directory = {}'.format(icoads_dir))
print('ICOADS version = {}'.format(version))
print('Output to {}'.format(out_dir))
print('List of bad IDs = {}'.format(bad_id_file))
print('Parameter file = {}'.format(config.get('Files', 'parameter_file')))
print('')
ids_to_exclude = bf.process_bad_id_file(bad_id_file)
# read in climatology files
sst_pentad_stdev = clim.Climatology.from_filename(config.get('Climatologies', 'Old_SST_stdev_climatology'), 'sst')
sst_stdev_1 = clim.Climatology.from_filename(config.get('Climatologies', 'SST_buddy_one_box_to_buddy_avg'), 'sst')
sst_stdev_2 = clim.Climatology.from_filename(config.get('Climatologies', 'SST_buddy_one_ob_to_box_avg'), 'sst')
sst_stdev_3 = clim.Climatology.from_filename(config.get('Climatologies', 'SST_buddy_avg_sampling'), 'sst')
with open(config.get('Files', 'parameter_file'), 'r') as f:
parameters = json.load(f)
print("Reading climatologies from parameter file")
climlib = ex.ClimatologyLibrary()
for entry in parameters['climatologies']:
print("{} {}".format(entry[0], entry[1]))
climlib.add_field(entry[0], entry[1], clim.Climatology.from_filename(entry[2], entry[3]))
for year, month in qc.year_month_gen(year1, month1, year2, month2):
print("{} {}".format(year, month))
last_year, last_month = qc.last_month_was(year, month)
next_year, next_month = qc.next_month_is(year, month)
reps = ex.Deck()
count = 0
lastday = -99
for readyear, readmonth in qc.year_month_gen(last_year, last_month, next_year, next_month):
print("{} {}".format(readyear, readmonth))
ostia_bg_var = None
if tracking:
ostia_bg_var = clim.Climatology.from_filename(
config.get('Climatologies', qc.season(readmonth) + '_ostia_background'), 'bg_var')
filename = bf.icoads_filename_from_stub(parameters['icoads_dir'],
parameters['icoads_filenames'],
readyear, readmonth)
try:
icoads_file = gzip.open(filename, "r")
except IOError:
print("no ICOADS file for {} {}".format(readyear, readmonth))
continue
rec = IMMA()
for line in icoads_file:
try:
rec.readstr(line)
readob = True
except:
readob = False
print("Rejected ob {}".format(line))
if (not (rec.data['ID'] in ids_to_exclude) and
readob and
rec.data['YR'] == readyear and
rec.data['MO'] == readmonth):
rep = ex.MarineReportQC(rec)
del rec
rep.setvar('AT2', rep.getvar('AT'))
# if day has changed then read in OSTIA field if available and append SST and sea-ice fraction
# to the observation metadata
if tracking and readyear >= 1985 and rep.getvar('DY') is not None:
if rep.getvar('DY') != lastday:
lastday = rep.getvar('DY')
y_year, y_month, y_day = qc.yesterday(readyear, readmonth, lastday)
# ofname = ostia_filename(ostia_dir, y_year, y_month, y_day)
ofname = bf.get_background_filename(parameters['background_dir'],
parameters['background_filenames'],
y_year, y_month, y_day)
climlib.add_field('OSTIA', 'background',
clim.Climatology.from_filename(ofname, 'analysed_sst'))
climlib.add_field('OSTIA', 'ice',
clim.Climatology.from_filename(ofname, 'sea_ice_fraction'))
rep_clim = climlib.get_field('OSTIA', 'background').get_value_ostia(rep.lat(), rep.lon())
if rep_clim is not None:
rep_clim -= 273.15
rep.setext('OSTIA', rep_clim)
rep.setext('ICE', climlib.get_field('OSTIA', 'ice').get_value_ostia(rep.lat(), rep.lon()))
rep.setext('BGVAR', ostia_bg_var.get_value_mds_style(rep.lat(), rep.lon(), rep.getvar('MO'),
rep.getvar('DY')))
for varname in ['SST', 'AT']:
rep_clim = climlib.get_field(varname, 'mean').get_value_mds_style(rep.lat(), rep.lon(),
rep.getvar('MO'),
rep.getvar('DY'))
rep.add_climate_variable(varname, rep_clim)
for varname in ['SLP2', 'SHU', 'CRH', 'CWB', 'DPD']:
rep_clim = climlib.get_field(varname, 'mean').get_value(rep.lat(), rep.lon(), rep.getvar('MO'),
rep.getvar('DY'))
rep.add_climate_variable(varname, rep_clim)
for varname in ['DPT', 'AT2', 'SLP']:
rep_clim = climlib.get_field(varname, 'mean').get_value(rep.lat(), rep.lon(), rep.getvar('MO'),
rep.getvar('DY'))
rep_stdev = climlib.get_field(varname, 'stdev').get_value(rep.lat(), rep.lon(),
rep.getvar('MO'), rep.getvar('DY'))
rep.add_climate_variable(varname, rep_clim, rep_stdev)
rep.calculate_humidity_variables(['SHU', 'VAP', 'CRH', 'CWB', 'DPD'])
rep.perform_base_qc(parameters)
rep.set_qc('POS', 'month_match', qc.month_match(year, month, rep.getvar('YR'), rep.getvar('MO')))
reps.append(rep)
count += 1
rec = IMMA()
icoads_file.close()
print("Read {} ICOADS records".format(count))
# filter the obs into passes and fails of basic positional QC
filt = ex.QC_filter()
filt.add_qc_filter('POS', 'date', 0)
filt.add_qc_filter('POS', 'time', 0)
filt.add_qc_filter('POS', 'pos', 0)
filt.add_qc_filter('POS', 'blklst', 0)
reps.add_filter(filt)
# track check the passes one ship at a time
count_ships = 0
for one_ship in reps.get_one_platform_at_a_time():
one_ship.track_check(parameters['track_check'])
one_ship.iquam_track_check(parameters['IQUAM_track_check'])
one_ship.spike_check(parameters['IQUAM_spike_check'])
one_ship.find_saturated_runs(parameters['saturated_runs'])
one_ship.find_multiple_rounded_values(parameters['multiple_rounded_values'])
for varname in ['SST', 'AT', 'AT2', 'DPT', 'SLP']:
one_ship.find_repeated_values(parameters['find_repeated_values'],
intype=varname)
count_ships += 1
print("Track checked {} ships".format(count_ships))
# SST buddy check
filt = ex.QC_filter()
filt.add_qc_filter('POS', 'is780', 0)
filt.add_qc_filter('POS', 'date', 0)
filt.add_qc_filter('POS', 'time', 0)
filt.add_qc_filter('POS', 'pos', 0)
filt.add_qc_filter('POS', 'blklst', 0)
filt.add_qc_filter('POS', 'trk', 0)
filt.add_qc_filter('SST', 'noval', 0)
filt.add_qc_filter('SST', 'freez', 0)
filt.add_qc_filter('SST', 'clim', 0)
filt.add_qc_filter('SST', 'nonorm', 0)
reps.add_filter(filt)
reps.bayesian_buddy_check('SST', sst_stdev_1, sst_stdev_2, sst_stdev_3, parameters)
reps.mds_buddy_check('SST', sst_pentad_stdev, parameters['mds_buddy_check'])
# NMAT buddy check
filt = ex.QC_filter()
filt.add_qc_filter('POS', 'isship', 1) # only do ships mat_blacklist
filt.add_qc_filter('AT', 'mat_blacklist', 0)
filt.add_qc_filter('POS', 'date', 0)
filt.add_qc_filter('POS', 'time', 0)
filt.add_qc_filter('POS', 'pos', 0)
filt.add_qc_filter('POS', 'blklst', 0)
filt.add_qc_filter('POS', 'trk', 0)
filt.add_qc_filter('POS', 'day', 0)
filt.add_qc_filter('AT', 'noval', 0)
filt.add_qc_filter('AT', 'clim', 0)
filt.add_qc_filter('AT', 'nonorm', 0)
reps.add_filter(filt)
reps.bayesian_buddy_check('AT', sst_stdev_1, sst_stdev_2, sst_stdev_3, parameters)
reps.mds_buddy_check('AT', sst_pentad_stdev, parameters['mds_buddy_check'])
# DPT buddy check #NB no day check for this one
filt = ex.QC_filter()
filt.add_qc_filter('DPT', 'hum_blacklist', 0)
filt.add_qc_filter('POS', 'date', 0)
filt.add_qc_filter('POS', 'time', 0)
filt.add_qc_filter('POS', 'pos', 0)
filt.add_qc_filter('POS', 'blklst', 0)
filt.add_qc_filter('POS', 'trk', 0)
filt.add_qc_filter('DPT', 'noval', 0)
filt.add_qc_filter('DPT', 'clim', 0)
filt.add_qc_filter('DPT', 'nonorm', 0)
reps.add_filter(filt)
reps.mds_buddy_check('DPT', climlib.get_field('DPT', 'stdev'), parameters['mds_buddy_check'])
# SLP buddy check
filt = ex.QC_filter()
filt.add_qc_filter('POS', 'date', 0)
filt.add_qc_filter('POS', 'time', 0)
filt.add_qc_filter('POS', 'pos', 0)
filt.add_qc_filter('POS', 'blklst', 0)
filt.add_qc_filter('POS', 'trk', 0)
filt.add_qc_filter('SLP', 'noval', 0)
filt.add_qc_filter('SLP', 'clim', 0)
filt.add_qc_filter('SLP', 'nonorm', 0)
reps.add_filter(filt)
reps.mds_buddy_check('SLP', climlib.get_field('SLP', 'stdev'), parameters['slp_buddy_check'])
extdir = bf.safe_make_dir(out_dir, year, month)
reps.write_output(parameters['runid'], extdir, year, month)
if tracking:
# set QC for output by ID - buoys only and passes base SST QC
filt = ex.QC_filter()
filt.add_qc_filter('POS', 'month_match', 1)
filt.add_qc_filter('POS', 'isdrifter', 1)
reps.add_filter(filt)
idfile = open(extdir + '/ID_file.txt', 'w')
for one_ship in reps.get_one_platform_at_a_time():
if len(one_ship) > 0:
thisid = one_ship.getrep(0).getvar('ID')
if thisid is not None:
idfile.write(thisid + ',' + ex.safe_filename(thisid) + '\n')
one_ship.write_output(parameters['runid'], extdir, year, month)
idfile.close()
del reps
if __name__ == '__main__':
main(sys.argv[1:])