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

root/raw2proc/tags/raw2proc-1.0/proc_rdi_logdata_dw.py

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

corrected lon to negative for (longitude W) in config files; corrected misspelled field in all proc_*.py

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