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

root/sodar/trunk/plotSodar.py

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

Some adjustments to axes and labels.

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     numAltitudes = arrayDataObject.numAltitudes
46     altInterval = arrayDataObject.altInterval
47    
48     def makePcolor(vector, title, fileout, cbLabel):
49         fig = p.figure(1)
50         axe = fig.add_subplot(1, 1, 1)
51         pc = axe.pcolor(vector)
52        
53         axe.set_xlabel('Time (hh:mm UTC)')
54         axe.set_ylabel('Altitude (m)')
55         axe.set_xbound(upper=numIntervals)
56        
57         xticks = axe.get_xticks()
58         xticklabels = [(int(x) * timeInterval) + beginStamp
59                        for x in xticks]
60         xticklabels = [':'.join(('%02u' % x.hour, '%02u' % x.minute))
61                        for x in xticklabels]
62         axe.set_xticklabels(xticklabels)
63        
64         yticks = axe.get_yticks()
65         yticklabels = [str(y * altInterval + minAltitude)
66                        for y in yticks]
67         axe.set_yticklabels(yticklabels)
68        
69         axe.set_title(title)
70         cb = p.colorbar(pc)
71         cb.set_label(cbLabel)
72        
73         fig.savefig(os.path.join(pathout, fileout))
74         fig.clear()
75        
76         return 0
77    
78     uComponents = arrayDataObject.uComponents
79     maskedUComponents = n.ma.masked_where(n.isnan(uComponents),
80                                           uComponents)
81     makePcolor(maskedUComponents.T,
82                'U Component of Wind Veloctiy for %s' %
83                 (str(beginStamp)[:10],),
84                'uComponents.png',
85                'Speed (cm/sec)')
86    
87     vComponents = arrayDataObject.vComponents
88     maskedVComponents = n.ma.masked_where(n.isnan(vComponents),
89                                           vComponents)
90     makePcolor(maskedVComponents.T,
91                'V Component of Wind Veloctiy for %s' %
92                 (str(beginStamp)[:10],),
93                'vComponents.png',
94                'Speed (cm/sec)')
95    
96     wComponents = arrayDataObject.wComponents
97     maskedWComponents = n.ma.masked_where(n.isnan(wComponents),
98                                           wComponents)
99     makePcolor(maskedWComponents.T,
100                'W Component of Wind Veloctiy for %s' %
101                 (str(beginStamp)[:10],),
102                'wComponents.png',
103                'Speed (cm/sec)')
104    
105     echoStrengths = arrayDataObject.echoStrengths
106     maskedEchoStrenths = n.ma.masked_where(n.isnan(echoStrengths),
107                                                    echoStrengths)
108     makePcolor(maskedEchoStrenths.T,
109                'Echo Strength for %s' %
110                 (str(beginStamp)[:10],),
111                'echoStrengths.png',
112                'Strength (no units)')
113    
114     # timeComponent = n.array(range(numIntervals))
115     # altComponent = n.array(range(numAltitudes))
116     # fig = p.figure(1)
117     # axe = fig.add_subplot(1, 1, 1)
118     # qv = axe.quiver(altComponent,
119     #                 timeComponent,
120     #                 maskedUComponents.T,
121     #                 maskedVComponents.T)
122     # axe.set_xlabel('Time (min)')
123     # axe.set_ylabel('Altitude (m)')
124     # axe.set_xbound(upper=numIntervals)
125     # xticks = axe.get_xticks()
126     # xticklabels = [str(x * timeInterval)
127     #                for x in xticks]
128     # axe.set_xticklabels(xticklabels)
129     # yticks = axe.get_yticks()
130     # yticklabels = [str(y * altInterval + minAltitude)
131     #                for y in yticks]
132     # axe.set_yticklabels(yticklabels)
133     # axe.set_title('Wind Velocty for %s' %
134     #               (str(beginStamp)[:10],))
135     # cb = p.colorbar(qv)
136     # cb.set_label('W Component Speed (cm/sec)')
137     # fig.savefig(os.path.join(pathout, 'quiver.png'))
138     # fig.clear()
139     
140     return 0
141
142 def plotAll(source, destination):
143     """
144     Plot all sodar raw data files.
145     
146     plotAll(source, destination) -> rc
147     
148     source - path to raw data directory in NCCOOS format.
149     detination - path to plot images directory in NCCOOS format.
150     rc - 0 if successful
151     """
152    
153     if source.endswith(os.path.sep):
154         source = source.rstrip(os.path.sep)
155    
156     if destination.endswith(os.path.sep):
157         destination = destination.rstrip(os.path.sep)
158    
159     walkList = findMissing.findMissing(source, destination)
160    
161     for filein, pathout in walkList:
162         plotSingle(filein, pathout)
163    
164     return 0
165
166 def _main():
167     """bin/python %prog [options] /path/to/raw/files/ /path/to/plots/dir/"""
168    
169     __description__ = 'Plot all sodar raw data files.'
170    
171     parser = optparse.OptionParser(usage=_main.__doc__,
172                                    version='%prog 1.0',
173                                    description=__description__)
174     parser.set_defaults(forceUpdate=False)
175     parser.add_option('-f', '--force-update',
176                       action='store_true',
177                       dest='forceUpdate')
178     (values, args) = parser.parse_args()
179     (source, destination) = tuple(args)
180     plotAll(source, destination)
181    
182     return 0
183
184 if __name__ == "__main__":
185     _main()
Note: See TracBrowser for help on using the browser.