NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/raw2proc/trunk/raw2proc/proc_rdi_logdata_dw.py

Revision 101 (checked in by haines, 16 years ago)

first commit of code

Line 
1 #!/usr/bin/env python
2 # Last modified:  Time-stamp: <2008-01-14 12:14:32 haines>
3 """
4 how to parse data, and assert what data and info goes into
5 creating and updating monthly netcdf files
6
7 RDI/Wavesmon processed adcp current profile data
8
9 parser : sample date and time, ensemble number, wave summary output from WavesMon software
10 creator : lat, lon, z, time, sig_wave_ht, peak_wave_period, peak_wave_dir,
11           max_wave_ht, max_wave_period, water_depth
12 updater : time, sig_wave_ht, peak_wave_period, peak_wave_dir,
13           max_wave_ht, max_wave_period, water_depth
14
15 Examples
16 --------
17
18 >> (parse, create, update) = load_processors('proc_rdi_logdata_adcp')
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 from raw2proc import *
31 from procutil import *
32 from ncutil import *
33
34 now_dt = datetime.utcnow()
35 now_dt.replace(microsecond=0)
36
37 def parser(platform_info, sensor_info, lines):
38     """
39     parse and assign currents data from RDI ADCP Log Data
40
41     """
42  
43     i = 0
44    
45     for line in lines:
46         # split line and parse float and integers
47         rdi = []
48         sw = re.split(',', line)
49         for s in sw:
50             m = re.search(REAL_RE_STR, s)
51             if m:
52                 rdi.append(float(m.groups()[0]))
53
54         # assign specific fields
55         n = len(rdi)
56         burst_num = int(rdi[0]) # Ensemble Number
57
58         # get sample datetime from data
59         sample_str = '%02d-%02d-%02d %02d:%02d:%02d' % tuple(rdi[1:7])
60         sample_dt = scanf_datetime(sample_str, fmt='%y-%m-%d %H:%M:%S')
61          #   datetime(*strptime(sample_str, "%y-%m-%d %H:%M:%S")[0:6])
62
63         # get sample datetime from filename
64         # compare with datetime from filename
65
66         sig_wave_ht = rdi[8]         # Significant Wave Height (Hs, meters)
67         peak_wave_period = rdi[9]    # Peak Wave Period (Tp, sec)
68         peak_wave_dir = rdi[10]      # Peak Wave Direction (deg N)
69         max_wave_ht = rdi[12]        # Maximum Wave Height (Hmax, meters)
70         mean_wave_period = rdi[13]    # Maximum Wave Period (Tmean, sec)
71
72         water_depth = rdi[11]/1000   # Water Depth (meters) (based on ADCP backscatter or input config??)
73         nbins = int(rdi[14])         # Number of bins
74
75         # set up dict of data if first line
76         if i==0:
77             data = {
78                 'en' : numpy.array(numpy.ones((len(lines),), dtype=int)*numpy.nan),
79                 'dt' : numpy.array(numpy.ones((len(lines),), dtype=object)*numpy.nan),
80                 'time' : numpy.array(numpy.ones((len(lines),), dtype=long)*numpy.nan),
81                 'sig_wave_ht' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
82                 'peak_wave_period' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
83                 'peak_wave_dir' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
84                 'max_wave_ht' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
85                 'mean_wave_period' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
86                 'water_depth' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
87                 }
88        
89         data['en'][i] = burst_num
90         data['dt'][i] = sample_dt # sample datetime
91         data['time'][i] = dt2es(sample_dt) # sample time in epoch seconds
92         data['sig_wave_ht'][i] = sig_wave_ht
93         data['peak_wave_period'][i] = peak_wave_period
94         data['peak_wave_dir'][i] = peak_wave_dir
95         data['max_wave_ht'][i] =  max_wave_ht
96         data['mean_wave_period'][i] =  mean_wave_period
97         data['water_depth'][i] = water_depth
98         i = i+1
99
100     return data
101
102 def creator(platform_info, sensor_info, data):
103     #
104     #
105     title_str = sensor_info['description']+' at '+ platform_info['location']
106     global_atts = {
107         'title' : title_str,
108         'institution' : 'Unversity of North Carolina at Chapel Hill (UNC-CH)',
109         'institution_url' : 'http://nccoos.unc.edu',
110         'institution_dods_url' : 'http://nccoos.unc.edu',
111         'metadata_url' : 'http://nccoos.unc.edu',
112         'references' : 'http://nccoos.unc.edu',
113         'contact' : 'Sara Haines (haines@email.unc.edu)',
114         #
115         'source' : 'directional wave (acoustic doppler) observation',
116         'history' : 'raw2proc using ' + sensor_info['process_module'],
117         'comment' : 'File created using pycdf'+pycdfVersion()+' and numpy '+pycdfArrayPkg(),
118         # conventions
119         'Conventions' : 'CF-1.0; SEACOOS-CDL-v2.0',
120         # SEACOOS CDL codes
121         'format_category_code' : 'directional waves',
122         'institution_code' : platform_info['institution'],
123         'platform_code' : platform_info['id'],
124         'package_code' : sensor_info['id'],
125         # institution specific
126         'project' : 'North Carolina Coastal Ocean Observing System (NCCOOS)',
127         'project_url' : 'http://nccoos.unc.edu',
128         # timeframe of data contained in file yyyy-mm-dd HH:MM:SS
129         'start_date' : data['dt'][0].strftime("%Y-%m-%d %H:%M:%S"),
130         'end_date' : data['dt'][-1].strftime("%Y-%m-%d %H:%M:%S"),
131         'release_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
132         #
133         'creation_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
134         'modification_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
135         'process_level' : 'level1',
136         #
137         # must type match to data (e.g. fillvalue is real if data is real)
138         '_FillValue' : -99999.,
139         }
140
141     var_atts = {
142         # coordinate variables
143         'time' : {'short_name': 'time',
144                   'long_name': 'Time',
145                   'standard_name': 'time',
146                   'units': 'seconds since 1970-1-1 00:00:00 -0', # UTC
147                   'axis': 'T',
148                   },
149         'lat' : {'short_name': 'lat',
150              'long_name': 'Latitude',
151              'standard_name': 'latitude',
152              'reference':'geographic coordinates',
153              'units': 'degrees_north',
154              'valid_range':(-90.,90.),
155              'axis': 'Y',
156              },
157         'lon' : {'short_name': 'lon',
158                  'long_name': 'Longtitude',
159                  'standard_name': 'longtitude',
160                  'reference':'geographic coordinates',
161                  'units': 'degrees_east',
162                  'valid_range':(-180.,180.),
163                  'axis': 'Y',
164                  },
165         'z' : {'short_name': 'z',
166                'long_name': 'Height',
167                'standard_name': 'height',
168                'reference':'zero at sea-surface',
169                'units': 'm',
170                'axis': 'Z',
171                },
172         # data variables
173         'en' : {'short_name': 'en',
174                 'long_name': 'Ensemble Number',
175                 'standard_name': 'ensemble_number',                         
176                 'units': 'None',
177                  },
178         'sig_wave_ht' : {'short_name': 'Hs',
179                          'long_name': 'Significant Wave Height',
180                          'definition': 'Four times the square root of the first moment of the wave spectrum (4*sqrt(m0))',
181                          'standard_name': 'significant_wave_height',
182                          'units': 'm',
183                         },
184         'peak_wave_period' : {'short_name': 'Tp',
185                              'long_name': 'Peak Wave Period',
186                              'definition': 'Period of strongest wave (wave energy maximum)',
187                              'standard_name': 'peak_wave_period',                         
188                              'units': 'sec',
189                              },
190         'peak_wave_dir' : {'short_name': 'Dp',
191                            'long_name': 'Peak Wave Direction',
192                            'definition': 'Direction from which strongest waves (wave energy max) are coming',
193                            'standard_name': 'peak_wave_from_direction',                         
194                            'units': 'deg from N',
195                            'reference': 'clockwise from True North',
196                            },
197         'max_wave_ht' : {'short_name': 'Hmax',
198                          'long_name': 'Maximum Wave Height',
199                          'standard_name': 'max_wave_height',                         
200                          'units': 'm',
201                          },
202         'mean_wave_period' : {'short_name': 'Tmean',
203                               'long_name': 'Mean Wave Period',
204                               'definition': 'Zero-moment of the non-directional spectrum divided by the first-moment (m0/m1)',
205                               'standard_name': 'mean_wave_period',                         
206                               'units': 'sec',
207                               },
208         'water_depth': {'short_name': '',
209                         'long_name': 'Water Depth',
210                         'standard_name': 'water_depth',                         
211                         'units': 'm',
212                         },
213
214         }
215
216
217     # integer values
218     ntime=NC.UNLIMITED
219     nlat=1
220     nlon=1
221     nz=1
222    
223     # dimension names use tuple so order of initialization is maintained
224     dim_inits = (
225         ('ntime', NC.UNLIMITED),
226         ('nlat', 1),
227         ('nlon', 1),
228         ('nz', 1)
229         )
230    
231     # using tuple of tuples so order of initialization is maintained
232     # using dict for attributes order of init not important
233     # use dimension names not values
234     # (varName, varType, (dimName1, [dimName2], ...))
235     var_inits = (
236         # coordinate variables
237         ('time', NC.INT, ('ntime',)),
238         ('lat', NC.FLOAT, ('nlat',)),
239         ('lon', NC.FLOAT, ('nlon',)),
240         ('z',  NC.FLOAT, ('nz',)),
241         # data variables
242         ('en', NC.INT, ('ntime', )),
243         ('sig_wave_ht', NC.FLOAT, ('ntime',)),
244         ('peak_wave_period', NC.FLOAT, ('ntime',)),
245         ('peak_wave_dir', NC.FLOAT, ('ntime',)),
246         ('max_wave_ht', NC.FLOAT, ('ntime',)),
247         ('mean_wave_period', NC.FLOAT, ('ntime',)),
248         ('water_depth', NC.FLOAT, ('ntime',)),
249         )
250    
251     # subset data only to month being processed (see raw2proc.process())
252     i = data['in']
253
254     # var data
255     var_data = (
256         ('lat',  platform_info['lat']),
257         ('lon', platform_info['lon']),
258         ('z', 0),
259         #
260         ('time', data['time'][i]),
261         ('en', data['en'][i]),
262         ('sig_wave_ht', data['sig_wave_ht'][i]),
263         ('peak_wave_period', data['peak_wave_period'][i]),
264         ('peak_wave_dir', data['peak_wave_dir'][i]),
265         ('max_wave_ht', data['max_wave_ht'][i]),
266         ('mean_wave_period', data['mean_wave_period'][i]),
267         ('water_depth', data['water_depth'][i]),
268         )
269
270     return (global_atts, var_atts, dim_inits, var_inits, var_data)
271
272 def updater(platform_info, sensor_info, data):
273     #
274     global_atts = {
275         # update times of data contained in file (yyyy-mm-dd HH:MM:SS)
276         # last date in monthly file
277         'end_date' : data['dt'][-1].strftime("%Y-%m-%d %H:%M:%S"),
278         'release_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
279         #
280         'modification_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
281         }
282
283     # data variables
284     # update any variable attributes like range, min, max
285     var_atts = {}
286     # var_atts = {
287     #    'u': {'max': max(data.u),
288     #          'min': min(data.v),
289     #          },
290     #    'v': {'max': max(data.u),
291     #          'min': min(data.v),
292     #          },
293     #    }
294     
295     # subset data only to month being processed (see raw2proc.process())
296     i = data['in']
297
298     # data
299     var_data = (
300         ('time', data['time'][i]),
301         ('en', data['en'][i]),
302         ('sig_wave_ht', data['sig_wave_ht'][i]),
303         ('peak_wave_period', data['peak_wave_period'][i]),
304         ('peak_wave_dir', data['peak_wave_dir'][i]),
305         ('max_wave_ht', data['max_wave_ht'][i]),
306         ('mean_wave_period', data['mean_wave_period'][i]),
307         ('water_depth', data['water_depth'][i]),
308         )
309
310     return (global_atts, var_atts, var_data)
311
312 #
313
Note: See TracBrowser for help on using the browser.