1 |
""" |
---|
2 |
Parse data and assert what data creates and updates monthly NetCDF files. |
---|
3 |
|
---|
4 |
Spongenet mini_andi conductivity parameters sponge data. |
---|
5 |
""" |
---|
6 |
|
---|
7 |
import math |
---|
8 |
import numpy as n |
---|
9 |
import pycdf |
---|
10 |
import datetime |
---|
11 |
import procutil |
---|
12 |
from spongenet.parse import Data |
---|
13 |
|
---|
14 |
nowDt = datetime.datetime.utcnow().replace(microsecond=0) |
---|
15 |
|
---|
16 |
def parser(platform_info, sensor_info, lines): |
---|
17 |
""" |
---|
18 |
Parse and assign sponge data from XML file. |
---|
19 |
""" |
---|
20 |
|
---|
21 |
_data = Data(''.join(lines)) |
---|
22 |
|
---|
23 |
# Each Device tag represents a time sample. |
---|
24 |
num_samples = len(_data.devices) |
---|
25 |
|
---|
26 |
data = { |
---|
27 |
'dt' : n.array(n.ones((num_samples,)) * n.nan, dtype=object), |
---|
28 |
'time' : n.array(n.ones((num_samples,)) * n.nan, dtype=long), |
---|
29 |
'pdt' : n.array(n.ones((num_samples,)) * n.nan, dtype=object), |
---|
30 |
'ptime' : n.array(n.ones((num_samples,)) * n.nan, dtype=long), |
---|
31 |
'ds' : n.array(n.ones((num_samples,)) * n.nan, dtype=object), |
---|
32 |
'session' : n.array(n.ones((num_samples,)) * n.nan, dtype=long), |
---|
33 |
'pds' : n.array(n.ones((num_samples,)) * n.nan, dtype=object), |
---|
34 |
'psession' : n.array(n.ones((num_samples,)) * n.nan, dtype=long), |
---|
35 |
'record' : n.array(n.ones((num_samples,)) * n.nan, dtype=int), |
---|
36 |
'status' : n.array(n.ones((num_samples,)) * n.nan, dtype=int), |
---|
37 |
'pstatus' : n.array(n.ones((num_samples,)) * n.nan, dtype=int), |
---|
38 |
'conductivity' : n.array(n.ones((num_samples,)) * n.nan, dtype=float), |
---|
39 |
'temperature' : n.array(n.ones((num_samples,)) * n.nan, dtype=float), |
---|
40 |
} |
---|
41 |
|
---|
42 |
for (sample_index, sample) in enumerate(_data.devices): |
---|
43 |
# sample time at the platform |
---|
44 |
dt = {'month' : int(sample['time'][5:7]), |
---|
45 |
'day' : int(sample['time'][8:10]), |
---|
46 |
'year' : int(sample['time'][0:4]), |
---|
47 |
'hour' : int(sample['time'][11:13]), |
---|
48 |
'min' : int(sample['time'][14:16]), |
---|
49 |
'sec' : int(sample['time'][17:19]), |
---|
50 |
} |
---|
51 |
dt = '%(month)02d-%(day)02d-%(year)04d %(hour)02d:%(min)02d:%(sec)02d' \ |
---|
52 |
% dt |
---|
53 |
dt = procutil.scanf_datetime(dt, fmt='%m-%d-%Y %H:%M:%S') |
---|
54 |
if sensor_info['utc_offset']: |
---|
55 |
dt = dt + datetime.timedelta(hours=sensor_info['utc_offset']) |
---|
56 |
data['dt'][sample_index] = dt |
---|
57 |
data['time'][sample_index] = procutil.dt2es(dt) |
---|
58 |
|
---|
59 |
# sample time at the package |
---|
60 |
package_dt = {'month' : int(sample['data_time'][5:7]), |
---|
61 |
'day' : int(sample['data_time'][8:10]), |
---|
62 |
'year' : int(sample['data_time'][0:4]), |
---|
63 |
'hour' : int(sample['data_time'][11:13]), |
---|
64 |
'min' : int(sample['data_time'][14:16]), |
---|
65 |
'sec' : int(sample['data_time'][17:19]), |
---|
66 |
} |
---|
67 |
package_dt = ('%(month)02d-%(day)02d-%(year)04d ' + |
---|
68 |
'%(hour)02d:%(min)02d:%(sec)02d') \ |
---|
69 |
% package_dt |
---|
70 |
package_dt = procutil.scanf_datetime(package_dt, fmt='%m-%d-%Y %H:%M:%S') |
---|
71 |
if sensor_info['utc_offset']: |
---|
72 |
package_dt = package_dt + \ |
---|
73 |
datetime.timedelta(hours=sensor_info['utc_offset']) |
---|
74 |
data['pdt'][sample_index] = package_dt |
---|
75 |
data['ptime'][sample_index] = procutil.dt2es(package_dt) |
---|
76 |
|
---|
77 |
# platform session time |
---|
78 |
ds = {'month' : int(sample['sessionid'][14:16]), |
---|
79 |
'day' : int(sample['sessionid'][17:19]), |
---|
80 |
'year' : int(sample['sessionid'][9:13]), |
---|
81 |
'hour' : int(sample['sessionid'][20:22]), |
---|
82 |
'min' : int(sample['sessionid'][23:25]), |
---|
83 |
'sec' : int(sample['sessionid'][26:28]), |
---|
84 |
} |
---|
85 |
ds = '%(month)02d-%(day)02d-%(year)04d %(hour)02d:%(min)02d:%(sec)02d' \ |
---|
86 |
% ds |
---|
87 |
ds = procutil.scanf_datetime(ds, fmt='%m-%d-%Y %H:%M:%S') |
---|
88 |
if sensor_info['utc_offset']: |
---|
89 |
ds = ds + datetime.timedelta(hours=sensor_info['utc_offset']) |
---|
90 |
data['ds'][sample_index] = ds |
---|
91 |
data['session'][sample_index] = procutil.dt2es(ds) |
---|
92 |
|
---|
93 |
# package session time |
---|
94 |
package_ds = {'month' : int(sample['data_sessionid'][5:7]), |
---|
95 |
'day' : int(sample['data_sessionid'][8:10]), |
---|
96 |
'year' : int(sample['data_sessionid'][0:4]), |
---|
97 |
'hour' : int(sample['data_sessionid'][11:13]), |
---|
98 |
'min' : int(sample['data_sessionid'][14:16]), |
---|
99 |
'sec' : int(sample['data_sessionid'][17:19]), |
---|
100 |
} |
---|
101 |
package_ds = ('%(month)02d-%(day)02d-%(year)04d ' + |
---|
102 |
'%(hour)02d:%(min)02d:%(sec)02d') \ |
---|
103 |
% package_ds |
---|
104 |
package_ds = procutil.scanf_datetime(package_ds, fmt='%m-%d-%Y %H:%M:%S') |
---|
105 |
if sensor_info['utc_offset']: |
---|
106 |
package_ds = package_ds + \ |
---|
107 |
datetime.timedelta(hours=sensor_info['utc_offset']) |
---|
108 |
data['pds'][sample_index] = package_ds |
---|
109 |
data['psession'][sample_index] = procutil.dt2es(package_ds) |
---|
110 |
|
---|
111 |
# platform variables |
---|
112 |
try: |
---|
113 |
data['record'][sample_index] = int(sample["recordnumber"]) |
---|
114 |
except KeyError: |
---|
115 |
pass |
---|
116 |
|
---|
117 |
try: |
---|
118 |
data['status'][sample_index] = int(sample["status"]. |
---|
119 |
partition(":")[0]) |
---|
120 |
except (KeyError, AttributeError, ): |
---|
121 |
pass |
---|
122 |
|
---|
123 |
# package variables |
---|
124 |
try: |
---|
125 |
data['pstatus'][sample_index] = int(sample.sensors |
---|
126 |
[sensor_info["id_number"]] |
---|
127 |
["status"]. |
---|
128 |
partition(":")[0]) |
---|
129 |
except (KeyError, AttributeError, ): |
---|
130 |
pass |
---|
131 |
|
---|
132 |
try: |
---|
133 |
data['conductivity'][sample_index] = float(sample.sensors |
---|
134 |
[sensor_info["id_number"]]. |
---|
135 |
points[sensor_info |
---|
136 |
["conductivity_description"]] |
---|
137 |
["value"]) |
---|
138 |
except (KeyError, AttributeError, ): |
---|
139 |
pass |
---|
140 |
|
---|
141 |
try: |
---|
142 |
data['temperature'][sample_index] = float(sample.sensors |
---|
143 |
[sensor_info["id_number"]]. |
---|
144 |
points[sensor_info |
---|
145 |
["temperature_description"]] |
---|
146 |
["value"]) |
---|
147 |
except (KeyError, AttributeError, ): |
---|
148 |
pass |
---|
149 |
|
---|
150 |
return data |
---|
151 |
|
---|
152 |
def creator(platform_info, sensor_info, data): |
---|
153 |
# |
---|
154 |
# |
---|
155 |
title_str = sensor_info['description']+' at '+ sensor_info['location'] |
---|
156 |
global_atts = { |
---|
157 |
# Required |
---|
158 |
'title' : title_str, |
---|
159 |
'institution' : platform_info['institution'], |
---|
160 |
'institution_url' : platform_info['institution_url'], |
---|
161 |
'institution_dods_url' : platform_info['institution_dods_url'], |
---|
162 |
'contact' : platform_info['contact'], |
---|
163 |
'Conventions' : platform_info['conventions'], |
---|
164 |
# Required by Scout |
---|
165 |
'format_category_code' : platform_info['format_category_code'], |
---|
166 |
'institution_code' : platform_info['institution_code'], |
---|
167 |
'platform_code' : platform_info['id'], |
---|
168 |
'package_code' : sensor_info['id'], |
---|
169 |
# Required by Version tracking |
---|
170 |
'format' : platform_info['format'], |
---|
171 |
'seacoos_rt_version' : platform_info['seacoos_rt_version'], |
---|
172 |
# Recommended |
---|
173 |
'_FillValue' : n.nan, |
---|
174 |
'missing_value' : n.nan, |
---|
175 |
'source' : platform_info['source'], |
---|
176 |
'references' : platform_info['references'], |
---|
177 |
'metadata_url' : platform_info['metadata_url'], |
---|
178 |
'history' : 'raw2proc using ' + sensor_info['process_module'], |
---|
179 |
'comment' : 'File created using pycdf ' + \ |
---|
180 |
pycdf.pycdfVersion() + ' and ' + \ |
---|
181 |
pycdf.pycdfArrayPkg() + ' ' + \ |
---|
182 |
n.__version__, |
---|
183 |
'project' : platform_info['project'], |
---|
184 |
'project_url' : platform_info['project_url'], |
---|
185 |
# timeframe of data contained in file yyyy-mm-dd HH:MM:SS |
---|
186 |
# first date in monthly file |
---|
187 |
'start_date' : data['dt'][0].strftime("%Y-%m-%d %H:%M:%S"), |
---|
188 |
# last date in monthly file |
---|
189 |
'end_date' : data['dt'][-1].strftime("%Y-%m-%d %H:%M:%S"), |
---|
190 |
'release_date' : nowDt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
191 |
'creation_date' : nowDt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
192 |
'modification_date' : nowDt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
193 |
'process_level' : 'level1', |
---|
194 |
# Custom |
---|
195 |
'id_number' : platform_info['id_number'], |
---|
196 |
'description' : platform_info['description'], |
---|
197 |
'serial_number' : platform_info['serial_number'], |
---|
198 |
'product_number' : platform_info['product_number'], |
---|
199 |
'product_name' : platform_info['product_name'], |
---|
200 |
'type' : platform_info['type'], |
---|
201 |
'protocol_version' : platform_info['protocol_version'], |
---|
202 |
'xmlns' : platform_info['xmlns'], |
---|
203 |
'location' : platform_info['location'], |
---|
204 |
'vertical_position': platform_info['vertical_position'], |
---|
205 |
'owner' : platform_info['owner'], |
---|
206 |
'package_id_number' : sensor_info['id_number'], |
---|
207 |
'package_description' : sensor_info['description'], |
---|
208 |
'package_serial_number' : sensor_info['serial_number'], |
---|
209 |
'package_product_number' : sensor_info['product_number'], |
---|
210 |
'package_product_name' : sensor_info['product_name'], |
---|
211 |
'package_adr' : sensor_info['adr'], |
---|
212 |
'package_protocol_version' : sensor_info['protocol_version'], |
---|
213 |
'package_vertical_position' : sensor_info['vertical_position'], |
---|
214 |
} |
---|
215 |
|
---|
216 |
var_atts = { |
---|
217 |
# coordinate variables |
---|
218 |
'time' : {'short_name': 'time', |
---|
219 |
'long_name': 'Time', |
---|
220 |
'standard_name': 'time', |
---|
221 |
'units': 'seconds since 1970-1-1 00:00:00 -0', # UTC |
---|
222 |
'axis': 'T', |
---|
223 |
}, |
---|
224 |
'lat' : {'short_name': 'lat', |
---|
225 |
'long_name': 'Latitude', |
---|
226 |
'standard_name': 'latitude', |
---|
227 |
'reference':'geographic coordinates', |
---|
228 |
'units': 'degrees_north', |
---|
229 |
'valid_range':(-90.,90.), |
---|
230 |
'axis': 'Y', |
---|
231 |
}, |
---|
232 |
'lon' : {'short_name': 'lon', |
---|
233 |
'long_name': 'Longtitude', |
---|
234 |
'standard_name': 'longtitude', |
---|
235 |
'reference':'geographic coordinates', |
---|
236 |
'units': 'degrees_east', |
---|
237 |
'valid_range':(-180.,180.), |
---|
238 |
'axis': 'Y', |
---|
239 |
}, |
---|
240 |
'z' : {'short_name': 'z', |
---|
241 |
'long_name': 'Height', |
---|
242 |
'standard_name': 'height', |
---|
243 |
'reference':'zero at sea-surface', |
---|
244 |
'positive' : 'up', |
---|
245 |
'units': 'meters', |
---|
246 |
'axis': 'Z', |
---|
247 |
}, |
---|
248 |
# data variables |
---|
249 |
'ptime' : {'short_name': 'ptime', |
---|
250 |
'long_name': 'Package Time', |
---|
251 |
'standard_name': 'none', |
---|
252 |
'units': 'seconds since 1970-1-1 00:00:00 -0', # UTC |
---|
253 |
}, |
---|
254 |
'session' : {'short_name': 'session', |
---|
255 |
'long_name': 'Session ID', |
---|
256 |
'standard_name': 'none', |
---|
257 |
'units': 'seconds since 1970-1-1 00:00:00 -0', # UTC |
---|
258 |
}, |
---|
259 |
'psession' : {'short_name': 'ptime', |
---|
260 |
'long_name': 'Package Session ID', |
---|
261 |
'standard_name': 'none', |
---|
262 |
'units': 'seconds since 1970-1-1 00:00:00 -0', # UTC |
---|
263 |
}, |
---|
264 |
'record' : {'short_name': 'record', |
---|
265 |
'long_name': 'Record Number', |
---|
266 |
'standard_name': 'none', |
---|
267 |
'units' : 'none', |
---|
268 |
}, |
---|
269 |
'status': {'short_name' : 'status', |
---|
270 |
'long_name': 'Platform Status Code', |
---|
271 |
'standard_name': 'none', |
---|
272 |
'units' : 'none', |
---|
273 |
'value_map' : platform_info['status_map'], |
---|
274 |
}, |
---|
275 |
'pstatus': {'short_name' : 'pstatus', |
---|
276 |
'long_name': 'Package Status Code', |
---|
277 |
'standard_name': 'none', |
---|
278 |
'units' : 'none', |
---|
279 |
'value_map' : sensor_info['status_map'], |
---|
280 |
}, |
---|
281 |
'conductivity' : {'short_name' : 'conductivity', |
---|
282 |
'long_name': sensor_info['conductivity_description'], |
---|
283 |
'standard_name': 'conductivity', |
---|
284 |
'units': 'none', |
---|
285 |
'id_number' : sensor_info['conductivity_id'], |
---|
286 |
'type' : sensor_info['conductivity_type'], |
---|
287 |
'format' : sensor_info['conductivity_format'], |
---|
288 |
'non_standard_units' : sensor_info['conductivity_units'], |
---|
289 |
'range_min' : sensor_info['conductivity_range_min'], |
---|
290 |
'range_max' : sensor_info['conductivity_range_max'], |
---|
291 |
}, |
---|
292 |
'temperature' : {'short_name' : 'temperature', |
---|
293 |
'long_name': sensor_info['temperature_description'], |
---|
294 |
'standard_name': 'water_temperature', |
---|
295 |
'units': 'celsius', |
---|
296 |
'id_number' : sensor_info['temperature_id'], |
---|
297 |
'type' : sensor_info['temperature_type'], |
---|
298 |
'format' : sensor_info['temperature_format'], |
---|
299 |
'non_standard_units' : sensor_info['temperature_units'], |
---|
300 |
'range_min' : sensor_info['temperature_range_min'], |
---|
301 |
'range_max' : sensor_info['temperature_range_max'], |
---|
302 |
}, |
---|
303 |
} |
---|
304 |
|
---|
305 |
# dimension names use tuple so order of initialization is maintained |
---|
306 |
dim_inits = ( |
---|
307 |
('ntime', pycdf.NC.UNLIMITED), |
---|
308 |
('nlat', 1), |
---|
309 |
('nlon', 1), |
---|
310 |
('nz', 1), |
---|
311 |
) |
---|
312 |
|
---|
313 |
# using tuple of tuples so order of initialization is maintained |
---|
314 |
# using dict for attributes order of init not important |
---|
315 |
# use dimension names not values |
---|
316 |
# (varName, varType, (dimName1, [dimName2], ...)) |
---|
317 |
var_inits = ( |
---|
318 |
# coordinate variables |
---|
319 |
('time', pycdf.NC.INT, ('ntime',)), |
---|
320 |
('lat', pycdf.NC.FLOAT, ('nlat',)), |
---|
321 |
('lon', pycdf.NC.FLOAT, ('nlon',)), |
---|
322 |
('z', pycdf.NC.FLOAT, ('nz',)), |
---|
323 |
# data variables |
---|
324 |
('ptime', pycdf.NC.INT, ('ntime',)), |
---|
325 |
('session', pycdf.NC.INT, ('ntime',)), |
---|
326 |
('psession', pycdf.NC.INT, ('ntime',)), |
---|
327 |
('record', pycdf.NC.INT, ('ntime',)), |
---|
328 |
('status', pycdf.NC.INT, ('ntime',)), |
---|
329 |
('pstatus', pycdf.NC.INT, ('ntime',)), |
---|
330 |
('conductivity', pycdf.NC.FLOAT, ('ntime',)), |
---|
331 |
('temperature', pycdf.NC.FLOAT, ('ntime',)), |
---|
332 |
) |
---|
333 |
|
---|
334 |
# subset data only to month being processed (see raw2proc.process()) |
---|
335 |
i = data['in'] |
---|
336 |
|
---|
337 |
# var data |
---|
338 |
var_data = ( |
---|
339 |
('time', data['time'][i]), |
---|
340 |
('lat', sensor_info['lat']), |
---|
341 |
('lon', sensor_info['lat']), |
---|
342 |
('z', sensor_info['elevation']), |
---|
343 |
('ptime', data['ptime'][i]), |
---|
344 |
('session', data['session'][i]), |
---|
345 |
('psession', data['psession'][i]), |
---|
346 |
('record', data['record'][i]), |
---|
347 |
('status', data['status'][i]), |
---|
348 |
('pstatus', data['pstatus'][i]), |
---|
349 |
('conductivity', data['conductivity'][i]), |
---|
350 |
('temperature', data['temperature'][i]), |
---|
351 |
) |
---|
352 |
|
---|
353 |
return (global_atts, var_atts, dim_inits, var_inits, var_data) |
---|
354 |
|
---|
355 |
def updater(platform_info, sensor_info, data): |
---|
356 |
# |
---|
357 |
global_atts = { |
---|
358 |
# update times of data contained in file (yyyy-mm-dd HH:MM:SS) |
---|
359 |
# last date in monthly file |
---|
360 |
'end_date' : data['dt'][-1].strftime("%Y-%m-%d %H:%M:%S"), |
---|
361 |
'release_date' : nowDt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
362 |
# |
---|
363 |
'modification_date' : nowDt.strftime("%Y-%m-%d %H:%M:%S"), |
---|
364 |
} |
---|
365 |
|
---|
366 |
# data variables |
---|
367 |
# update any variable attributes like range, min, max |
---|
368 |
var_atts = {} |
---|
369 |
|
---|
370 |
# subset data only to month being processed (see raw2proc.process()) |
---|
371 |
i = data['in'] |
---|
372 |
|
---|
373 |
# data |
---|
374 |
var_data = ( |
---|
375 |
('time', data['time'][i]), |
---|
376 |
('ptime', data['ptime'][i]), |
---|
377 |
('session', data['session'][i]), |
---|
378 |
('psession', data['psession'][i]), |
---|
379 |
('record', data['record'][i]), |
---|
380 |
('status', data['status'][i]), |
---|
381 |
('pstatus', data['pstatus'][i]), |
---|
382 |
('conductivity', data['conductivity'][i]), |
---|
383 |
('temperature', data['temperature'][i]), |
---|
384 |
) |
---|
385 |
|
---|
386 |
return (global_atts, var_atts, var_data) |
---|