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

root/raw2proc/trunk/raw2proc/test_proc_codar_totals.py

Revision 451 (checked in by cbc, 13 years ago)

Add various proc and config files not already under SVN.

Line 
1 #!/usr/bin/env python
2 # Last modified:  Time-stamp: <2010-12-09 16:16:12 haines>
3 """
4 parse datestr from cr1000 files to create monthly files
5
6 input file
7 /seacoos/data/nccoos/level0/crow/crow_csi_loggernet_yyyymmdd-yyyymmdd.dat
8
9 Output form
10 /seacoos/data/nccoos/level0/crow/yyyy_mm/wq/csi_wq_yyyy_mm.dat
11 /seacoos/data/nccoos/level0/crow/yyyy_mm/flow/csi_flow_yyyy_mm.dat
12
13 load data file
14 parse lines for time YYYY, jjj, HHMM
15 what year and month?
16
17 create YYYY_MM directory and output file if does not exist.
18 write line to YYYY_MM/csi_loggernet_yyyy_mm.dat output file
19
20 """
21
22 REAL_RE_STR = '\\s*(-?\\d(\\.\\d+|)[Ee][+\\-]\\d\\d?|-?(\\d+\\.\\d*|\\d*\\.\\d+)|-?\\d+)\\s*'
23
24 import sys
25 import os
26 import re
27 from procutil import *
28
29 def parser_codar(fn, lines):
30     """
31     parse and assign data to variables from CODAR Totals LLUV format
32
33     Notes
34     -----
35     1. Requires grid definition 
36     
37     """
38
39     import numpy
40     from datetime import datetime
41     from time import strptime
42     from StringIO import StringIO
43     from matplotlib.mlab import griddata
44
45     # get sample datetime from filename
46     # fn = sensor_info['fn']
47     sample_dt_start = filt_datetime(fn)
48
49     # read header that match '%(k): (v)\n' pairs on each line
50     m = re.findall(r'^(%.*):\s*(.*)$', ''.join(lines), re.MULTILINE)
51     for k,v in m:       
52         if k == '%TimeStamp':
53             sample_dt = scanf_datetime(v, fmt='%Y %m %d %H %M %S')
54            
55         elif k == '%TableType':
56             ftype = v
57         elif k == '%TableColumns':
58             ncol = int(v)
59         elif k == '%TableRows':
60             nrow = int(v)
61
62     # read data from string of lines but make it behave like a file object with StringIO
63     s = StringIO(''.join(lines))
64     s.seek(0) # ensures start posn of file
65     d = numpy.loadtxt(s, comments='%')
66     # lat, lon, u, v = numpy.loadtxt(s, usecols=(0,1,2,3), comments='%', unpack=True)
67     if 'TOT4' in ftype:
68         lon = d[:,0]
69         lat = d[:,1]
70         wu = d[:,2]
71         wv = d[:,3]
72         gridflag = d[:,4]
73         wu_std_qual = d[:,5]
74         wv_std_qual = d[:,6]
75         cov_qual = d[:,7]
76         x_dist = d[:,8]
77         y_dist = d[:,9]
78         rang = d[:,10]
79         bearing = d[:,11]
80         vel_mag = d[:,12]
81         vel_dir = d[:,13]
82         s1 = d[:,14]
83         s2 = d[:,15]
84         s3 = d[:,16]
85         s4 = d[:,17]
86         s5 = d[:,18]
87         s6 = d[:,19]
88
89     # define the lat/lon grid based on 6km resolution
90     # For Outer Banks north and east of Cape Hatteras, bounding box is defined by:
91     minlat, maxlat = (34.5, 38)
92     minlon, maxlon = (-76, -73.)
93     midlat = minlat + 0.5*(maxlat-minlat)
94     # ~111 km = 1 deg latitude
95     nlat = numpy.round((maxlat-minlat) *111/6)
96     nlon = numpy.round((maxlon-minlon) * math.cos(midlat*math.pi/180)*111/6)
97     yi = numpy.linspace(minlat, maxlat, nlat)
98     xi = numpy.linspace(minlon, maxlon, nlon)
99     uim = griddata(lon, lat, wu, xi, yi)
100     vim = griddata(lon, lat, wv, xi, yi)
101     # returned masked array as an ndarray with masked values filled with fill_value
102     ui = uim.filled(fill_value=numpy.nan)
103     vi = vim.filled(fill_value=numpy.nan)
104     # ---------------------------------------------------------------
105     data = {
106         'dt' : numpy.array(numpy.ones((1,), dtype=object)*numpy.nan),
107         'time' : numpy.array(numpy.ones((1,), dtype=long)*numpy.nan),
108         'lon' : numpy.array(numpy.ones((1,nlon), dtype=float)*numpy.nan),
109         'lat' : numpy.array(numpy.ones((1,nlat), dtype=float)*numpy.nan),
110         'u' : numpy.array(numpy.ones((1,nlon,nlat), dtype=float)*numpy.nan),
111         'v' : numpy.array(numpy.ones((1,nlon,nlat), dtype=float)*numpy.nan),
112         }
113     # ---------------------------------------------------------------
114     i = 0
115     data['dt'][i] =  sample_dt #
116     data['time'][i] =  dt2es(sample_dt) #
117     data['lon'][i] = xi # new longitude grid
118     data['lat'][i] = yi # new latitude grid
119     data['u'][i] = ui.T # u-component of water velocity (cm/s)
120     data['v'][i] = vi.T # v-component of water velocity
121
122     return data
123
124
125 def load_data(inFile):
126     lines=None
127     if os.path.exists(inFile):
128         f = open(inFile, 'r')
129         lines = f.readlines()
130         f.close()
131         if len(lines)<=0:
132             print 'Empty file: '+ inFile           
133     else:
134         print 'File does not exist: '+ inFile
135     return lines
136
137 from raw2proc import *
138
139 def test1(fn):
140     lines = load_data(fn)
141     return parser_codar(fn, lines)
142
143
144 if __name__ == '__main__':
145     fn = '/seacoos/data/nc-coos/level0/ouba/hfr_totals/2010_07/TOTL_OUBA_2010_07_14_0000.tuv'
146
147     #
148     # fn = sys.argv[1]
149     try:
150         test1(fn)
151     except:
152         pass
153    
Note: See TracBrowser for help on using the browser.