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

root/raw2proc/trunk/raw2proc/proc_jpier_ascii_met.py

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

Added use of epoch for UTC sample time

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