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

root/raw2proc/trunk/raw2proc/proc_jpier_ascii_met.py

Revision 113 (checked in by jcleary, 16 years ago)

Initial commit of JPIER processor code. See the raw2proc wiki page

Line 
1 #!/usr/bin/env python
2 # Last modified:  Time-stamp: <2008-02-15 10:51:00 jcleary>
3 """
4 how to parse data, and assert what data and info goes into
5 creating and updating monthly netcdf files
6
7 Texas Weather Instruments - Weather Processing System (WPS) met data
8         delimited ASCII file like:
9         year mon day hhmm       epoch  tmean hmean wsmean wdmean barom   dewPt wchill rrmean rday rmonth rterm
10
11 parser : output delimited ASCII file from onsite perl script
12 creator : lat, lon, time, air_temp, humidity, wspd, wdir, air_pressure,
13                   dew_temp, wchill, rainfall_rate, rainfall_day, rainfall_month, rainfall_term
14 updater : time, air_temp, humidity, wspd, wdir, air_pressure,
15                   dew_temp, wchill, rainfall_rate, rainfall_day, rainfall_month, rainfall_term
16
17 Examples
18 --------
19
20 >> (parse, create, update) = load_processors('proc_jpier_ascii_met')
21 or
22 >> si = get_config(cn+'.sensor_info')
23 >> (parse, create, update) = load_processors(si['met']['proc_module'])
24
25 >> lines = load_data(filename)
26 >> data = parse(platform_info, sensor_info, lines)
27 >> create(platform_info, sensor_info, data) or
28 >> update(platform_info, sensor_info, data)
29
30 """
31
32 from raw2proc import *
33 from procutil import *
34 from ncutil import *
35
36 now_dt = datetime.utcnow()
37 now_dt.replace(microsecond=0)
38
39 def parser(platform_info, sensor_info, lines):
40         """
41         parse and assign met data from JPIER TWS WPS text file
42
43         """
44  
45         i = 0
46
47         # drop header row from incoming lines list
48         if lines[0].startswith('year',0,5):
49                 print "... Header row present, skipping ..."
50                 del lines[0]
51    
52         for line in lines:
53                 # split line and parse float and integers
54                 tws = []
55                 # data row looks like:
56                 # 2008  1  1 0000 1199163600  17.2  84.8   0.00 0  1015.10  14.4  17.2 1729.1   0.0     0.0 1031.5
57        
58                 sw = re.split('\s+', line)
59                 for s in sw:
60                         m = re.search(REAL_RE_STR, s)
61                         if m:
62                                 tws.append(float(m.groups()[0]))
63                 # assign specific fields
64                 n = len(tws)
65                 # get sample datetime from data
66                 sample_str = '%04d-%02d-%02d %04d:00' % tuple(tws[0:4])
67                 sample_dt = scanf_datetime(sample_str, fmt='%Y-%m-%d %H%M:%S')
68                 #   datetime(*strptime(sample_str, "%y-%m-%d %H:%M:%S")[0:6])
69
70                 air_temp = tws[5]          # Air Temperature (deg F)
71                 humidity = tws[6]               # Humidity (%)
72                 dew_temp = tws[10]       # Dew Point (deg F)   
73                 air_pressure = tws[9]   # Air Pressure (Tmean, sec)
74                 wspd = tws[7]    # Mean Wind Speed (knots)
75                 wdir = tws[8]      # Mean Wind Direction (deg from N)
76                 wchill = tws[11]        # Wind Chill (deg F)
77                 rainfall_rate = tws[12]  # Rainfall Rate ()
78                 rainfall_day = tws[13]   # Rainfall amount last 24 hours (in)
79                 rainfall_month = tws[14] # Rainfall amount last month (in)
80                 rainfall_term = tws[15]  # Rainfall amount since installation (in)
81
82                 # set up dict of data if first line
83                 if i==0:
84                         data = {
85                                 'dt' : numpy.array(numpy.ones((len(lines),), dtype=object)*numpy.nan),
86                                 'time' : numpy.array(numpy.ones((len(lines),), dtype=long)*numpy.nan),
87                                 'air_temp' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
88                                 'humidity' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
89                                 'dew_temp' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
90                                 'air_pressure' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
91                                 'wspd' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
92                                 'wdir' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
93                                 'wchill' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
94                                 'rainfall_rate' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
95                                 'rainfall_day' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
96                                 'rainfall_month' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
97                                 'rainfall_term' : numpy.array(numpy.ones((len(lines)), dtype=float)*numpy.nan),
98                                 }
99                
100                 data['dt'][i] = sample_dt # sample datetime
101                 data['time'][i] = dt2es(sample_dt) # sample time in epoch seconds
102                 data['air_temp'][i] = air_temp
103                 data['humidity'][i] = humidity
104                 data['dew_temp'][i] = dew_temp
105                 data['air_pressure'][i] =  air_pressure
106                 data['wspd'][i] =  wspd
107                 data['wdir'][i] = wdir
108                 data['wchill'][i] = wchill
109                 data['rainfall_rate'][i] = rainfall_rate
110                 data['rainfall_day'][i] =  rainfall_day
111                 data['rainfall_month'][i] =  rainfall_month
112                 data['rainfall_term'][i] = rainfall_term
113                 i = i+1
114
115         return data
116
117 def creator(platform_info, sensor_info, data):
118         #
119         #
120         title_str = sensor_info['description']+' at '+ platform_info['location']
121         global_atts = {
122                 'title' : title_str,
123                 'institution' : 'Unversity of North Carolina at Chapel Hill (UNC-CH)',
124                 'institution_url' : 'http://nccoos.org',
125                 'institution_dods_url' : 'http://nccoos.org',
126                 'metadata_url' : 'http://nccoos.org',
127                 'references' : 'http://nccoos.org',
128                 'contact' : 'Jesse Cleary (jcleary@email.unc.edu)',
129                 #
130                 'source' : 'TWS Met station observation',
131                 'history' : 'raw2proc using ' + sensor_info['process_module'],
132                 'comment' : 'File created using pycdf'+pycdfVersion()+' and numpy '+pycdfArrayPkg(),
133                 # conventions
134                 'Conventions' : 'CF-1.0; SEACOOS-CDL-v2.0',
135                 # SEACOOS CDL codes
136                 'format_category_code' : 'fixed-point',
137                 'institution_code' : platform_info['institution'],
138                 'platform_code' : platform_info['id'],
139                 'package_code' : sensor_info['id'],
140                 # institution specific
141                 'project' : 'North Carolina Coastal Ocean Observing System (NCCOOS)',
142                 'project_url' : 'http://nccoos.org',
143                 # timeframe of data contained in file yyyy-mm-dd HH:MM:SS
144                 'start_date' : data['dt'][0].strftime("%Y-%m-%d %H:%M:%S"),
145                 'end_date' : data['dt'][-1].strftime("%Y-%m-%d %H:%M:%S"),
146                 'release_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
147                 #
148                 'creation_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
149                 'modification_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
150                 'process_level' : 'level1',
151                 #
152                 # must type match to data (e.g. fillvalue is real if data is real)
153                 '_FillValue' : -99999.,
154                 }
155
156         var_atts = {
157                 # coordinate variables
158                 'time' : {'short_name': 'time',
159                                   'long_name': 'Sample Time',
160                                   'standard_name': 'time',
161                                   'units': 'seconds since 1970-1-1 00:00:00 -0', # UTC
162                                   'axis': 'T',
163                                   },
164                 'lat' : {'short_name': 'lat',
165                                  'long_name': 'Latitude in Decimal Degrees',
166                                  'standard_name': 'latitude',
167                                  'reference':'geographic coordinates',
168                                  'units': 'degrees_north',
169                                  'valid_range':(-90.,90.),
170                                  'axis': 'Y',
171                                  },
172                 'lon' : {'short_name': 'lon',
173                                  'long_name': 'Longtitude in Decimal Degrees',
174                                  'standard_name': 'longtitude',
175                                  'reference':'geographic coordinates',
176                                  'units': 'degrees_east',
177                                  'valid_range':(-180.,180.),
178                                  'axis': 'Y',
179                                  },
180                 'z' : {'short_name': 'z',
181                                 'long_name': 'Height',
182                                 'standard_name': 'height',
183                                 'reference':'zero at sea-surface',
184                                 'positive': 'up',
185                                 'units': 'm',
186                                 'axis': 'Z',
187                                 },
188                 # data variables
189                
190                 'air_temp' :    {'short_name': 'air_temp',
191                                                 'long_name': 'Air Temperature',
192                                                 'standard_name': 'air_temperature',
193                                                 'units': 'degrees Celsius',
194                                                 },
195                 'humidity' :    {'short_name': 'humidity',
196                                                 'long_name': 'Humidity',
197                                                 'standard_name': 'humidity',
198                                                 'units': '%',
199                                                 },
200                 'dew_temp' :    {'short_name': 'dew_temp',
201                                                 'long_name': 'Dew Temperature',
202                                                 'standard_name': 'dew_temp',                                             
203                                                 'units': 'degrees Celsius',
204                                                 },
205                 'air_pressure' : {'short_name': 'air_pressure',
206                                                 'long_name': 'Air Pressure at Barometer Height',
207                                                 'standard_name': 'air_pressure',                                                 
208                                                 'units': 'hPa',
209                                                 },
210                 'wspd' :        {'short_name': 'wspd',
211                                                 'long_name': 'Wind Speed',
212                                                 'standard_name': 'wind_speed',
213                                                 'units': 'm s-1',
214                                                 'can_be_normalized': '?',
215                                                 },
216                 'wdir' :        {'short_name': 'wdir',
217                                                 'long_name': 'Wind Direction from',
218                                                 'standard_name': 'wind_from_direction',
219                                                 'reference': 'clockwise from True North',
220                                                 'valid_range': '0., 360',
221                                                 'units': 'degrees',
222                                                 },
223                 'wchill' :      {'short_name': 'wchill',
224                                                 'long_name': 'Wind Chill',
225                                                 'standard_name': 'wind_chill',
226                                                 'units': 'degrees Celsius',
227                                                 },
228                 'rainfall_rate' :       {'short_name': 'rR',
229                                                 'long_name': 'Rainfall Rate',
230                                                 'standard_name': 'rainfall_rate',
231                                                 'units': 'mm hr-1',
232                                                 },
233                 'rainfall_day' : {'short_name': 'rD',
234                                                 'long_name': 'Rainfall Day',
235                                                 'standard_name': 'rainfall_day',
236                                                 'units': 'mm',
237                                                 },
238                 'rainfall_month' : {'short_name': 'rM',
239                                                 'long_name': 'Rainfall Month',
240                                                 'standard_name': 'rainfall_month',
241                                                 'units': 'mm',
242                                                 },
243                 'rainfall_term' : {'short_name': 'rT',
244                                                 'long_name': 'Rainfall Term',
245                                                 'standard_name': 'rainfall_term',
246                                                 'units': 'mm',
247                                                 },
248         }
249         # integer values
250         ntime=NC.UNLIMITED
251         nlat=1
252         nlon=1
253         nz=1
254        
255         # dimension names use tuple so order of initialization is maintained
256         dim_inits = (
257                 ('ntime', NC.UNLIMITED),
258                 ('nlat', 1),
259                 ('nlon', 1),
260                 ('nz', 1)
261                 )
262
263         # using tuple of tuples so order of initialization is maintained
264         # using dict for attributes order of init not important
265         # use dimension names not values
266         # (varName, varType, (dimName1, [dimName2], ...))
267         var_inits = (
268                 # coordinate variables
269                 ('time', NC.INT, ('ntime',)),
270                 ('lat', NC.FLOAT, ('nlat',)),
271                 ('lon', NC.FLOAT, ('nlon',)),
272                 ('z',  NC.FLOAT, ('nz',)),
273                 # data variables
274                 ('air_temp', NC.FLOAT, ('ntime',)),
275                 ('humidity', NC.FLOAT, ('ntime',)),
276                 ('dew_temp', NC.FLOAT, ('ntime',)),
277                 ('air_pressure', NC.FLOAT, ('ntime',)),
278                 ('wspd', NC.FLOAT, ('ntime',)),
279                 ('wdir', NC.FLOAT, ('ntime',)),
280                 ('wchill', NC.FLOAT, ('ntime',)),
281                 ('rainfall_rate', NC.FLOAT, ('ntime',)),
282                 ('rainfall_day', NC.FLOAT, ('ntime',)),
283                 ('rainfall_month', NC.FLOAT, ('ntime',)),
284                 ('rainfall_term', NC.FLOAT, ('ntime',)),
285                 )
286
287         # subset data only to month being processed (see raw2proc.process())
288         i = data['in']
289
290         # var data
291         var_data = (
292                 ('lat',  platform_info['lat']),
293                 ('lon', platform_info['lon']),
294                 ('z', 6),
295                 #
296                 ('time', data['time'][i]),
297                 ('air_temp', data['air_temp'][i]),
298                 ('humidity', data['humidity'][i]),
299                 ('dew_temp', data['dew_temp'][i]),
300                 ('air_pressure', data['air_pressure'][i]),
301                 ('wspd', data['wspd'][i]),
302                 ('wdir', data['wdir'][i]),
303                 ('wchill', data['wchill'][i]),
304                 ('rainfall_rate', data['rainfall_rate'][i]),
305                 ('rainfall_day', data['rainfall_day'][i]),
306                 ('rainfall_month', data['rainfall_month'][i]),
307                 ('rainfall_term', data['rainfall_term'][i]),
308                 )
309                
310         return (global_atts, var_atts, dim_inits, var_inits, var_data)
311
312 def updater(platform_info, sensor_info, data):
313         #
314         global_atts = {
315                 # update times of data contained in file (yyyy-mm-dd HH:MM:SS)
316                 # last date in monthly file
317                 'end_date' : data['dt'][-1].strftime("%Y-%m-%d %H:%M:%S"),
318                 'release_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
319                 #
320                 'modification_date' : now_dt.strftime("%Y-%m-%d %H:%M:%S"),
321                 }
322
323         # data variables
324         # update any variable attributes like range, min, max
325         var_atts = {}
326         # var_atts = {
327         #       'u': {'max': max(data.u),
328         #                 'min': min(data.v),
329         #                 },
330         #       'v': {'max': max(data.u),
331         #                 'min': min(data.v),
332         #                 },
333         #       }
334        
335         # subset data only to month being processed (see raw2proc.process())
336         i = data['in']
337
338         # data
339         var_data = (
340                 ('time', data['time'][i]),
341                 ('air_temp', data['air_temp'][i]),
342                 ('humidity', data['humidity'][i]),
343                 ('dew_temp', data['dew_temp'][i]),
344                 ('air_pressure', data['air_pressure'][i]),
345                 ('wspd', data['wspd'][i]),
346                 ('wdir', data['wdir'][i]),
347                 ('wchill', data['wchill'][i]),
348                 ('rainfall_rate', data['rainfall_rate'][i]),
349                 ('rainfall_day', data['rainfall_day'][i]),
350                 ('rainfall_month', data['rainfall_month'][i]),
351                 ('rainfall_term', data['rainfall_term'][i]),
352                 )
353
354         return (global_atts, var_atts, var_data)
355
356 #
Note: See TracBrowser for help on using the browser.