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

root/sodar/trunk/sodar/formattedData.py

Revision 136 (checked in by cbc, 16 years ago)

Fixed up axes, labels, and titles.

Line 
1 #!/usr/bin/python
2 """
3 Module to handle formatting sodar data samples.
4
5 A list of sample data.
6
7 A sample: formattedDataObject[0] -> dictionary
8
9 A sample header: formattedDataObject[0]['header'] -> dictionary
10
11 A sample body: formattedDataObject[0]['body'] -> list of dictionaries
12
13 Altitude data from a sample: formattedDataObject[0]['body'][0] -> dictionary
14 """
15
16 __author__ = 'Chris Calloway'
17 __email__ = 'cbc@unc.edu'
18 __copyright__ = 'Copyright 2007 UNC-CH Department of Marine Science'
19 __license__ = 'GPL2'
20
21 import rawData
22 import numpy as n
23 import datetime
24
25 class FormattedData(list):
26    
27     """Class to handle formatted daily sodar file data.
28    
29        A list of samples.
30     """
31    
32     def __init__(self, data):
33         """Create formatted sodar daily data."""
34         super(FormattedData, self).__init__()
35         self.extend([sample.data() for sample in data])
36         self._convert()
37         self._stamp()
38         self._beginStamp()
39         self._endStamp()
40         self._numIntervals()
41         self._timeInterval()
42         self._minAltitude()
43         self._maxAltitude()
44         self._numAltitudes()
45         self._altInterval()
46    
47     def _convert(self):
48         """Convert data to numbers and correct for invalid values."""
49         INVALID = "-9999"
50         for sample in self:
51             # convert header data to integers
52             for key,value in sample['header'].items():
53                 try:
54                     if value == INVALID:
55                         raise ValueError
56                     sample['header'][key] = int(value)
57                 except (ValueError, TypeError):
58                     sample['header'][key] = n.NaN
59             # convert body data to floats
60             for altitude in sample['body']:
61                 for key,value in altitude.items():
62                     try:
63                         if value == INVALID:
64                             raise ValueError
65                         altitude[key] = float(value)
66                     except (ValueError, TypeError):
67                         altitude[key] = n.NaN
68    
69     def _stamp(self):
70         """Add a datetime stamp item to each sample."""
71         for sample in self:
72             header = sample['header']
73             sample['stamp'] = datetime.datetime(header['YEAR'],
74                                                 header['MONTH'],
75                                                 header['DAY'],
76                                                 header['HOUR'],
77                                                 header['MIN'])
78    
79     def _beginStamp(self):
80         self.beginStamp = self[0]['stamp']
81    
82     def _endStamp(self):
83         self.endStamp = self[-1]['stamp']
84    
85     def _numIntervals(self):
86         self.numIntervals = len(self)
87    
88     def _timeInterval(self):
89         """Add a time interval attribute."""
90         intervals = zip([sample['stamp'] for sample in self[:-1]],
91                         [sample['stamp'] for sample in self[1:]])
92         intervals = [interval[1] - interval[0] for interval in intervals]
93         self.timeInterval = min(intervals)
94    
95     def _minAltitude(self):
96         """Add an overall minimum altitude attribute."""
97         altitudes = [sample['body'][0]['ALT'] for sample in self]
98         self.minAltitude = min(altitudes)
99    
100     def _maxAltitude(self):
101         """Add an overall maximum altitude attribute."""
102         altitudes = [sample['body'][-1]['ALT'] for sample in self]
103         self.maxAltitude = max(altitudes)
104    
105     def _numAltitudes(self):
106         """Add an overall maximum number of altitudes attribute"""
107         self.numAltitudes = max([len(sample['body']) for sample in self])
108    
109     def _altInterval(self):
110         """Add an overall altitude interval attribute."""
111         self.altInterval = (self.maxAltitude - self.minAltitude) / \
112                            (self.numAltitudes - 1)
113    
114     def thetas(self):
115         """Create a list of lists of horizontal directional data."""
116         return [[altitude['DIR']
117                  for altitude
118                  in sample['body']]
119                 for sample in self]
120    
121     def radials(self):
122         """Create a list of lists of horizontal radial velocity data."""
123         return [[altitude['SPEED']
124                  for altitude
125                  in sample['body']]
126                 for sample in self]
127    
128     def wComponents(self):
129         """Create a list of lists of vertical velocity data."""
130         return [[altitude['W']
131                  for altitude
132                  in sample['body']]
133                 for sample in self]
134    
135     def echoStrengths(self):
136         """Create a list of lists of echo strength data."""
137         return [[altitude['CT']
138                  for altitude
139                  in sample['body']]
140                 for sample in self]
141
142
143 def _main():
144     """Process as script from command line."""
145     import urllib2
146     try:
147         rawDataHandle = urllib2.urlopen('http://nemo.isis.unc.edu/'\
148                                         'data/nccoos/level0/ims/sodar/'\
149                                         '2008_01/20080101.dat')
150         rawDataString = rawDataHandle.read()
151         rawDataHandle.close()
152     except:
153         raise IOError("Failure to read raw test data")
154     rawDataObject = rawData.RawData(rawDataString)
155     formattedDataObject = FormattedData(rawDataObject)
156     print "Time Interval =", formattedDataObject.timeInterval
157     print "Minumum Altitude =", formattedDataObject.minAltitude
158     print "Maximum Altitude =", formattedDataObject.maxAltitude
159     print "Number of Altitudes =", formattedDataObject.numAltitudes
160     print "Altitude Interval =", formattedDataObject.altInterval
161
162 if __name__ == "__main__":
163     _main()
Note: See TracBrowser for help on using the browser.