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

root/sodar/trunk/sodar.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 process sodar data.
4 """
5
6 import optparse
7 from sodar.utils import openAnything, findMissing
8 from sodar import arrayData as a
9 import numpy as n
10 import pylab as p
11 import os
12
13 def plotSingle(filein, pathout):
14     """
15     Plot singe sodar raw data file.
16     
17     plotSingle(filein, pathout) -> rc
18     
19     filein - path to raw data file.
20     fileout - path to plot images directory.
21     rc - 0 if successful
22     """
23    
24     if not os.path.exists(pathout):
25         os.makedirs(pathout, mode=0775)
26        
27         try:
28             rawDataHandle = openAnything.openAnything(filein)
29             rawDataString = rawDataHandle.read()
30             rawDataHandle.close()
31         except:
32             raise IOError("Failure to read raw data.")
33        
34         rawDataObject = a.rawData.RawData(rawDataString)
35         formattedDataObject = a.formattedData.FormattedData(rawDataObject)
36         arrayDataObject = a.ArrayData(formattedDataObject)
37        
38         beginStamp = arrayDataObject.beginStamp
39         endStamp = arrayDataObject.endStamp
40         numIntervals = arrayDataObject.numIntervals
41         timeInterval = arrayDataObject.timeInterval
42         timeInterval = (((float(timeInterval.days) * 24.0) +
43                          (timeInterval.seconds / 3600.0)) * 60.0)
44         minAltitude = arrayDataObject.minAltitude
45         altInterval = arrayDataObject.altInterval
46        
47         def makePcolor(vector, title, fileout, cbLabel):
48             fig = p.figure(1)
49             axe = fig.add_subplot(1, 1, 1)
50             pc = axe.pcolor(vector)
51            
52             axe.set_xlabel('Time (min)')
53             axe.set_ylabel('Altitude (m)')
54             axe.set_xbound(upper=numIntervals)
55            
56             xticks = axe.get_xticks()
57             xticklabels = [str(x * timeInterval)
58                            for x in xticks]
59             axe.set_xticklabels(xticklabels)
60            
61             yticks = axe.get_yticks()
62             yticklabels = [str(y * altInterval + minAltitude)
63                            for y in yticks]
64             axe.set_yticklabels(yticklabels)
65            
66             axe.set_title(title)
67             cb = p.colorbar(pc)
68             cb.set_label(cbLabel)
69            
70             fig.savefig(os.path.join(pathout, fileout))
71             fig.clear()
72            
73             return 0
74        
75         uComponents = arrayDataObject.uComponents
76         maskedUComponents = n.ma.masked_where(n.isnan(uComponents),
77                                               uComponents)
78         makePcolor(maskedUComponents.T,
79                    'U Component of Wind Veloctiy\nFrom %s To %s' %
80                    (beginStamp, endStamp),
81                    'uComponents.png',
82                    'Speed (cm/sec)')
83        
84         vComponents = arrayDataObject.vComponents
85         maskedVComponents = n.ma.masked_where(n.isnan(vComponents),
86                                               vComponents)
87         makePcolor(maskedVComponents.T,
88                    'V Component of Wind Veloctiy\nFrom %s To %s' %
89                    (beginStamp, endStamp),
90                    'vComponents.png',
91                    'Speed (cm/sec)')
92        
93         wComponents = arrayDataObject.wComponents
94         maskedWComponents = n.ma.masked_where(n.isnan(wComponents),
95                                               wComponents)
96         makePcolor(maskedWComponents.T,
97                    'W Component of Wind Veloctiy\nFrom %s To %s' %
98                    (beginStamp, endStamp),
99                    'wComponents.png',
100                    'Speed (cm/sec)')
101        
102         echoStrengths = arrayDataObject.echoStrengths
103         maskedEchoStrenths = n.ma.masked_where(n.isnan(echoStrengths),
104                                                        echoStrengths)
105         makePcolor(maskedEchoStrenths.T,
106                    'Echo Strength\nFrom %s To %s' %
107                    (beginStamp, endStamp),
108                    'echoStrengths.png',
109                    'Strength (no units)')
110        
111         fig = p.figure(1)
112         axe = fig.add_subplot(1, 1, 1)
113         qv = axe.quiver(maskedUComponents,
114                         maskedVComponents,
115                         maskedWComponents)
116         axe.set_xlabel('Time (min)')
117         axe.set_ylabel('Altitude (m)')
118         axe.set_xbound(upper=numIntervals)
119         xticks = axe.get_xticks()
120         xticklabels = [str(x * timeInterval)
121                        for x in xticks]
122         axe.set_xticklabels(xticklabels)
123         yticks = axe.get_yticks()
124         yticklabels = [str(y * altInterval + minAltitude)
125                        for y in yticks]
126         axe.set_yticklabels(yticklabels)
127         axe.set_title('Wind Velocty\nFrom %s To %s' %
128                       (beginStamp, endStamp))
129         cb = p.colorbar(qv)
130         cb.set_label('W Component Speed (cm/sec)')
131         fig.savefig(os.path.join(pathout, 'quiver.png'))
132         fig.clear()
133        
134         return 0
135
136 def plotAll(source, destination):
137     """
138     Plot all sodar raw data files.
139     
140     plotAll(source, destination) -> rc
141     
142     source - path to raw data directory in NCCOOS format.
143     detination - path to plot images directory in NCCOOS format.
144     rc - 0 if successful
145     """
146    
147     walkList = findMissing.findMissing(source, destination)
148    
149     for filein, pathout in walkList:
150         plotSingle(filein, pathout)
151     return 0
152
153 def _main():
154     """bin/python %prog [options] /path/to/raw/files/ /path/to/plots/dir/"""
155    
156     __description__ = 'Plot all sodar raw data files.'
157    
158     parser = optparse.OptionParser(usage=_main.__doc__,
159                                    version='1.0',
160                                    description=__description__)
161     (values, args) = parser.parse_args()
162     (source, destination) = tuple(args)
163     plotAll(source, destination)
164     return 0
165
166 if __name__ == "__main__":
167     _main()
Note: See TracBrowser for help on using the browser.