1 |
#!/usr/bin/env python |
---|
2 |
# Last modified: Time-stamp: <2012-05-15 15:44:51 haines> |
---|
3 |
""" |
---|
4 |
how to parse data, and assert what data and info goes into |
---|
5 |
creating and updating monthly netcdf files |
---|
6 |
|
---|
7 |
parse data met data collected on Campbell Scientific DataLogger (loggernet) (csi) |
---|
8 |
|
---|
9 |
parser : sample date and time, |
---|
10 |
|
---|
11 |
creator : lat, lon, z, time, |
---|
12 |
updator : time, |
---|
13 |
|
---|
14 |
|
---|
15 |
Examples |
---|
16 |
-------- |
---|
17 |
|
---|
18 |
>> (parse, create, update) = load_processors('proc_csi_adcp_v2') |
---|
19 |
or |
---|
20 |
>> si = get_config(cn+'.sensor_info') |
---|
21 |
>> (parse, create, update) = load_processors(si['adcp']['proc_module']) |
---|
22 |
|
---|
23 |
>> lines = load_data(filename) |
---|
24 |
>> data = parse(platform_info, sensor_info, lines) |
---|
25 |
>> create(platform_info, sensor_info, data) or |
---|
26 |
>> update(platform_info, sensor_info, data) |
---|
27 |
|
---|
28 |
""" |
---|
29 |
|
---|
30 |
|
---|
31 |
from raw2proc import * |
---|
32 |
from procutil import * |
---|
33 |
from ncutil import * |
---|
34 |
|
---|
35 |
now_dt = datetime.utcnow() |
---|
36 |
now_dt.replace(microsecond=0) |
---|
37 |
|
---|
38 |
def parser(platform_info, sensor_info, lines): |
---|
39 |
""" |
---|
40 |
Example met data |
---|
41 |
|
---|
42 |
"TOA5","CR1000_B1","CR1000","37541","CR1000.Std.21","CPU:NCWIND_12_Buoy_All.CR1","58723","AMet_6Min" |
---|
43 |
"TIMESTAMP","RECORD","Baro_mbar_Avg","RHumidity_Avg","RHumidity_Std","AirTempC_Avg","AirTempC_Std","Rain","Psp_Avg","Psp_Std","Pir_Wm2_Avg","Pir_Wm2_Std" |
---|
44 |
"TS","RN","","","","","","","","","","" |
---|
45 |
"","","Avg","Avg","Std","Avg","Std","Smp","Avg","Std","Avg","Std" |
---|
46 |
"2011-11-01 00:00:59",4590,14.3792,75.59,0.579,15.67,0.05,-22.35,1197.037,45.58967,371.5126,0.9030571 |
---|
47 |
"2011-11-01 00:06:59",4591,14.37995,74.96,0.912,16.61,0.048,-21,-1071.813,129.5147,381.2539,0.2076943 |
---|
48 |
"2011-11-01 00:12:59",4592,14.3792,72.71,2.677,17.29,0.032,-15.58,-2056.658,0,381.1828,0.1402813 |
---|
49 |
"2011-11-01 00:18:59",4593,14.3791,72.63,0.928,17.67,0.041,-19.64,-1895.86,9.866026,381.0333,0.2442325 |
---|
50 |
|
---|
51 |
""" |
---|
52 |
|
---|
53 |
import numpy |
---|
54 |
from datetime import datetime |
---|
55 |
from time import strptime |
---|
56 |
|
---|
57 |
# get sample datetime from filename |
---|
58 |
fn = sensor_info['fn'] |
---|
59 |
sample_dt_start = filt_datetime(fn) |
---|
60 |
|
---|
61 |
# how many samples (don't count header 4 lines) |
---|
62 |
nsamp = len(lines[4:]) |
---|
63 |
|
---|
64 |
N = nsamp |
---|
65 |
data = { |
---|
66 |
'dt' : numpy.array(numpy.ones((N,), dtype=object)*numpy.nan), |
---|
67 |
'time' : numpy.array(numpy.ones((N,), dtype=long)*numpy.nan), |
---|
68 |
'air_press' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
69 |
'rh' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
70 |
'rh_std' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
71 |
'air_temp' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
72 |
'air_temp_std' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
73 |
'rain' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
74 |
'psp' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
75 |
'psp_std' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
76 |
'pir' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
77 |
'pir_std' : numpy.array(numpy.ones((N,), dtype=float)*numpy.nan), |
---|
78 |
} |
---|
79 |
|
---|
80 |
# sample count |
---|
81 |
i = 0 |
---|
82 |
|
---|
83 |
for line in lines[4:]: |
---|
84 |
csi = [] |
---|
85 |
# split line |
---|
86 |
sw = re.split(',', line) |
---|
87 |
if len(sw)<=0: |
---|
88 |
print ' ... skipping line %d ' % (i,) |
---|
89 |
continue |
---|
90 |
|
---|
91 |
# replace any "NAN" text with a number |
---|
92 |
for index, s in enumerate(sw): |
---|
93 |
m = re.search(NAN_RE_STR, s) |
---|
94 |
if m: |
---|
95 |
sw[index] = '-99999' |
---|
96 |
|
---|
97 |
# parse date-time, and all other float and integers |
---|
98 |
for s in sw[1:]: |
---|
99 |
m = re.search(REAL_RE_STR, s) |
---|
100 |
if m: |
---|
101 |
csi.append(float(m.groups()[0])) |
---|
102 |
|
---|
103 |
if sensor_info['utc_offset']: |
---|
104 |
sample_dt = scanf_datetime(sw[0], fmt='"%Y-%m-%d %H:%M:%S"') + \ |
---|
105 |
timedelta(hours=sensor_info['utc_offset']) |
---|
106 |
else: |
---|
107 |
sample_dt = scanf_datetime(sw[0], fmt='"%Y-%m-%d %H:%M:%S"') |
---|
108 |
|
---|
109 |
data['dt'][i] = sample_dt # sample datetime |
---|
110 |
data['time'][i] = dt2es(sample_dt) # sample time in epoch seconds |
---|
111 |
|
---|
112 |
if len(csi)==11: |
---|
113 |
# |
---|
114 |
# data['samplenum'][i] = csi[0] # sample number assigned by datalogger in table |
---|
115 |
data['air_press'][i] = csi[1] # Heise Barometer (psi) to mbar |
---|
116 |
data['rh'][i] = csi[2] # relative humidity avg (60 samples for 1 min) |
---|
117 |
data['rh_std'][i] = csi[3] # relative humidity std |
---|
118 |
data['air_temp'][i] = csi[4] # air temperature avg (deg C) |
---|
119 |
data['air_temp_std'][i] = csi[5] # air temperature std (deg C) |
---|
120 |
data['rain'][i] = csi[6]/100. # precip gauge cummulative (mm) |
---|
121 |
data['psp'][i] = csi[7] # PSP avg |
---|
122 |
data['psp_std'][i] = csi[8] # PSP std |
---|
123 |
data['pir'][i] = csi[9] # PIR avg (W m-2) |
---|
124 |
data['pir_std'][i] = csi[10] # PIR std (W m-2) |
---|
125 |
i=i+1 |
---|
126 |
else: |
---|
127 |
print ' ... skipping line %d -- %s ' % (i,line) |
---|
128 |
continue |
---|
129 |
|
---|
130 |
# if re.search |
---|
131 |
# for line |
---|
132 |
|
---|
133 |
data['air_press'] = udconvert(data['air_press'], 'psi', 'mbar')[0] |
---|
134 |
# cannot figure out how to combine the two operations |
---|
135 |
# for some reason, this one liner does not work |
---|
136 |
# good = -40<at & at<60 |
---|
137 |
above_tol=-40<data['air_temp'] |
---|
138 |
below_tol=data['air_temp']<60 |
---|
139 |
good = above_tol & below_tol |
---|
140 |
bad = ~good |
---|
141 |
data['air_temp'][bad] = numpy.nan |
---|
142 |
|
---|
143 |
# check that no data[dt] is set to Nan or anything but datetime |
---|
144 |
# keep only data that has a resolved datetime |
---|
145 |
keep = numpy.array([type(datetime(1970,1,1)) == type(dt) for dt in data['dt'][:]]) |
---|
146 |
if keep.any(): |
---|
147 |
for param in data.keys(): |
---|
148 |
data[param] = data[param][keep] |
---|
149 |
|
---|
150 |
return data |
---|
151 |
|
---|
152 |
|
---|
153 |
def creator(platform_info, sensor_info, data): |
---|
154 |
# |
---|
155 |
# |
---|
156 |
# subset data only to month being processed (see raw2proc.process()) |
---|
157 |
i = data['in'] |
---|
158 |
|
---|
159 |
title_str = sensor_info['description']+' at '+ platform_info['location'] |
---|
160 |
global_atts = { |
---|
161 |
'title' : title_str, |
---|
162 |
'institution' : platform_info['institution'], |
---|
163 |
'institution_url' : platform_info['institution_url'], |
---|
164 |
'institution_dods_url' : platform_info['institution_dods_url'], |
---|
165 |
'metadata_url' : platform_info['metadata_url'], |
---|
166 |
'references' : platform_info['references'], |
---|
167 |
'contact' : platform_info['contact'], |
---|
168 |
# |
---|
169 |
'source' : platform_info['source']+' '+sensor_info['source'], |
---|
170 |
'history' : 'raw2proc using ' + sensor_info['process_module'], |
---|
171 |
'comment' : 'File created using pycdf'+pycdfVersion()+' and numpy '+pycdfArrayPkg(), |
---|
172 |
# conventions |
---|
173 |
'Conventions' : platform_info['conventions'], |
---|
174 |
# SEACOOS CDL codes |
---|
175 |
'format_category_code' : platform_info['format_category_code'], |
---|
176 |
'institution_code' : platform_info['institution_code'], |
---|
177 |
'platform_code' : platform_info['id'], |
---|
178 |
'package_code' : sensor_info['id'], |
---|
179 |
# institution specific |
---|
180 |
'project' : platform_info['project'], |
---|
181 |
'project_url' : platform_info['project_url'], |
---|
182 |
# timeframe of data contained in file yyyy-mm-dd HH:MM:SS |
---|
183 |
# first date in monthly file |
---|
184 |
'start_date' : data['dt'][i][0].strftime("%Y-%m-%d %H:%M:%S"), |
---|
185 |
# last date in monthly file |
---|
186 |
'end_date' : data['dt'][i][-1].strftime("%Y-%m-%d %H:%M:%S"), |
---|
187 |
'release_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
188 |
# |
---|
189 |
'creation_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
190 |
'modification_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
191 |
'process_level' : 'level1', |
---|
192 |
# |
---|
193 |
# must type match to data (e.g. fillvalue is real if data is real) |
---|
194 |
'_FillValue' : -99999., |
---|
195 |
} |
---|
196 |
|
---|
197 |
var_atts = { |
---|
198 |
# coordinate variables |
---|
199 |
'time' : {'short_name': 'time', |
---|
200 |
'long_name': 'Time', |
---|
201 |
'standard_name': 'time', |
---|
202 |
'units': 'seconds since 1970-1-1 00:00:00 -0', # UTC |
---|
203 |
'axis': 'T', |
---|
204 |
}, |
---|
205 |
'lat' : {'short_name': 'lat', |
---|
206 |
'long_name': 'Latitude', |
---|
207 |
'standard_name': 'latitude', |
---|
208 |
'reference':'geographic coordinates', |
---|
209 |
'units': 'degrees_north', |
---|
210 |
'valid_range':(-90.,90.), |
---|
211 |
'axis': 'Y', |
---|
212 |
}, |
---|
213 |
'lon' : {'short_name': 'lon', |
---|
214 |
'long_name': 'Longitude', |
---|
215 |
'standard_name': 'longitude', |
---|
216 |
'reference':'geographic coordinates', |
---|
217 |
'units': 'degrees_east', |
---|
218 |
'valid_range':(-180.,180.), |
---|
219 |
'axis': 'Y', |
---|
220 |
}, |
---|
221 |
'z' : {'short_name': 'z', |
---|
222 |
'long_name': 'Altitude', |
---|
223 |
'standard_name': 'altitude', |
---|
224 |
'reference':'zero at mean sea level', |
---|
225 |
'positive' : 'up', |
---|
226 |
'units': 'm', |
---|
227 |
'axis': 'Z', |
---|
228 |
}, |
---|
229 |
# data variables |
---|
230 |
'air_press': {'short_name': 'air_press', |
---|
231 |
'long_name': 'Air Pressure', |
---|
232 |
'standard_name': 'air_pressure', |
---|
233 |
'units': 'mbar', |
---|
234 |
'z': sensor_info['barometer_height'], |
---|
235 |
'z_units' : 'meter', |
---|
236 |
}, |
---|
237 |
'air_temp': {'short_name': 'air_temp', |
---|
238 |
'long_name': 'Air Temperature', |
---|
239 |
'standard_name': 'air_temperature', |
---|
240 |
'units': 'degC', |
---|
241 |
'z': sensor_info['temperature_height'], |
---|
242 |
'z_units' : 'meter', |
---|
243 |
}, |
---|
244 |
'air_temp_std': {'short_name': 'air_temp_std', |
---|
245 |
'long_name': 'Standard Deviation of Air Temperature', |
---|
246 |
'standard_name': 'air_temperature', |
---|
247 |
'units': 'degC', |
---|
248 |
}, |
---|
249 |
'rh': {'short_name': 'rh', |
---|
250 |
'long_name': 'Relative Humidity', |
---|
251 |
'standard_name': 'relative_humidity', |
---|
252 |
'units': '%', |
---|
253 |
'z': sensor_info['temperature_height'], |
---|
254 |
'z_units' : 'meter', |
---|
255 |
}, |
---|
256 |
'rh_std': {'short_name': 'rh_std', |
---|
257 |
'long_name': 'Standard Deviation of Relative Humidity', |
---|
258 |
'standard_name': 'relative_humidity', |
---|
259 |
'units': '%', |
---|
260 |
}, |
---|
261 |
'rain': {'short_name': 'rain', |
---|
262 |
'long_name': '6-Minute Rain', |
---|
263 |
'standard_name': 'rain', |
---|
264 |
'units': 'inches', |
---|
265 |
}, |
---|
266 |
'psp': {'short_name': 'psp', |
---|
267 |
'long_name': 'Short-wave Radiation', |
---|
268 |
'standard_name': 'downwelling_shortwave_irradiance', |
---|
269 |
'units': 'W m-2', |
---|
270 |
}, |
---|
271 |
'psp_std': {'short_name': 'psp_std', |
---|
272 |
'long_name': 'Standard Deviation of Short-wave Radiation', |
---|
273 |
'standard_name': 'shortwave_radiation', |
---|
274 |
'units': 'W m-2', |
---|
275 |
}, |
---|
276 |
'pir': {'short_name': 'pir', |
---|
277 |
'long_name': 'Long-wave Radiation', |
---|
278 |
'standard_name': 'longwave_radiation', |
---|
279 |
'units': 'W m-2', |
---|
280 |
}, |
---|
281 |
'pir_std': {'short_name': 'pir_std', |
---|
282 |
'long_name': 'Standard Deviation of Long-wave Radiation', |
---|
283 |
'standard_name': 'longwave_radiation', |
---|
284 |
'units': 'W m-2', |
---|
285 |
}, |
---|
286 |
} |
---|
287 |
|
---|
288 |
# dimension names use tuple so order of initialization is maintained |
---|
289 |
dim_inits = ( |
---|
290 |
('ntime', NC.UNLIMITED), |
---|
291 |
('nlat', 1), |
---|
292 |
('nlon', 1), |
---|
293 |
('nz', 1), |
---|
294 |
) |
---|
295 |
|
---|
296 |
# using tuple of tuples so order of initialization is maintained |
---|
297 |
# using dict for attributes order of init not important |
---|
298 |
# use dimension names not values |
---|
299 |
# (varName, varType, (dimName1, [dimName2], ...)) |
---|
300 |
var_inits = ( |
---|
301 |
# coordinate variables |
---|
302 |
('time', NC.INT, ('ntime',)), |
---|
303 |
('lat', NC.FLOAT, ('nlat',)), |
---|
304 |
('lon', NC.FLOAT, ('nlon',)), |
---|
305 |
('z', NC.FLOAT, ('nz',)), |
---|
306 |
# data variables |
---|
307 |
('air_press', NC.FLOAT, ('ntime',)), |
---|
308 |
('rh', NC.FLOAT, ('ntime',)), |
---|
309 |
('rh_std', NC.FLOAT, ('ntime',)), |
---|
310 |
('air_temp', NC.FLOAT, ('ntime',)), |
---|
311 |
('air_temp_std', NC.FLOAT, ('ntime',)), |
---|
312 |
('rain', NC.FLOAT, ('ntime',)), |
---|
313 |
('psp', NC.FLOAT, ('ntime',)), |
---|
314 |
('psp_std', NC.FLOAT, ('ntime',)), |
---|
315 |
('pir', NC.FLOAT, ('ntime',)), |
---|
316 |
('pir_std', NC.FLOAT, ('ntime',)), |
---|
317 |
) |
---|
318 |
|
---|
319 |
# subset data only to month being processed (see raw2proc.process()) |
---|
320 |
i = data['in'] |
---|
321 |
|
---|
322 |
# var data |
---|
323 |
var_data = ( |
---|
324 |
('lat', platform_info['lat']), |
---|
325 |
('lon', platform_info['lon']), |
---|
326 |
('z', platform_info['altitude']), |
---|
327 |
# |
---|
328 |
('time', data['time'][i]), |
---|
329 |
# |
---|
330 |
('air_press', data['air_press'][i]), |
---|
331 |
('rh', data['rh'][i]), |
---|
332 |
('rh_std', data['rh_std'][i]), |
---|
333 |
('air_temp', data['air_temp'][i]), |
---|
334 |
('air_temp_std', data['air_temp_std'][i]), |
---|
335 |
('rain', data['rain'][i]), |
---|
336 |
('psp', data['psp'][i]), |
---|
337 |
('psp_std', data['psp_std'][i]), |
---|
338 |
('pir', data['pir'][i]), |
---|
339 |
('pir_std', data['pir_std'][i]), |
---|
340 |
) |
---|
341 |
|
---|
342 |
return (global_atts, var_atts, dim_inits, var_inits, var_data) |
---|
343 |
|
---|
344 |
def updater(platform_info, sensor_info, data): |
---|
345 |
# |
---|
346 |
# subset data only to month being processed (see raw2proc.process()) |
---|
347 |
i = data['in'] |
---|
348 |
|
---|
349 |
global_atts = { |
---|
350 |
# update times of data contained in file (yyyy-mm-dd HH:MM:SS) |
---|
351 |
# last date in monthly file |
---|
352 |
'end_date' : data['dt'][i][-1].strftime("%Y-%m-%d %H:%M:%S"), |
---|
353 |
'release_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
354 |
# |
---|
355 |
'modification_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
356 |
} |
---|
357 |
|
---|
358 |
# data variables |
---|
359 |
# update any variable attributes like range, min, max |
---|
360 |
var_atts = {} |
---|
361 |
# var_atts = { |
---|
362 |
# 'wtemp': {'max': max(data.u), |
---|
363 |
# 'min': min(data.v), |
---|
364 |
# }, |
---|
365 |
# 'cond': {'max': max(data.u), |
---|
366 |
# 'min': min(data.v), |
---|
367 |
# }, |
---|
368 |
# } |
---|
369 |
|
---|
370 |
# data |
---|
371 |
var_data = ( |
---|
372 |
('time', data['time'][i]), |
---|
373 |
# |
---|
374 |
('air_press', data['air_press'][i]), |
---|
375 |
('rh', data['rh'][i]), |
---|
376 |
('rh_std', data['rh_std'][i]), |
---|
377 |
('air_temp', data['air_temp'][i]), |
---|
378 |
('air_temp_std', data['air_temp_std'][i]), |
---|
379 |
('rain', data['rain'][i]), |
---|
380 |
('psp', data['psp'][i]), |
---|
381 |
('psp_std', data['psp_std'][i]), |
---|
382 |
('pir', data['pir'][i]), |
---|
383 |
('pir_std', data['pir_std'][i]), |
---|
384 |
) |
---|
385 |
|
---|
386 |
return (global_atts, var_atts, var_data) |
---|
387 |
# |
---|