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

root/sodar/trunk/plotSodar.py

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

Fix typo lastest to latest in plotSodar.py.

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 import shutil
13
14 _uComponentsPlotName = 'uComponents.png'
15 _vComponentsPlotName = 'vComponents.png'
16 _wComponentsPlotName = 'wComponents.png'
17 _echoStrengthsPlotName = 'echoStrengths.png'
18 _quiverPlotName = 'quiver.png'
19 _latestPlotNamePrefix = 'ims_sodar_latest_'
20
21 def plotSingle(filein, pathout):
22     """
23     Plot single sodar raw data file.
24    
25     plotSingle(filein, pathout) -> rc
26    
27     filein - path to raw data file.
28     pathout - path to plot images directory.
29     rc - 0 if successful
30     """
31    
32     if not os.path.exists(pathout):
33         os.makedirs(pathout, mode=0775)
34    
35     rawDataHandle = openAnything.openAnything(filein)
36     rawDataString = rawDataHandle.read()
37     rawDataHandle.close()
38    
39     rawDataObject = a.rawData.RawData(rawDataString)
40     formattedDataObject = a.formattedData.FormattedData(rawDataObject)
41     arrayDataObject = a.ArrayData(formattedDataObject)
42    
43     beginStamp = arrayDataObject.beginStamp
44     endStamp = arrayDataObject.endStamp
45     numIntervals = arrayDataObject.numIntervals
46     timeInterval = arrayDataObject.timeInterval
47     minAltitude = arrayDataObject.minAltitude
48     numAltitudes = arrayDataObject.numAltitudes
49     altInterval = arrayDataObject.altInterval
50    
51     def makePcolor(vector, title, fileout, cbLabel):
52         fig = p.figure(1)
53         axe = fig.add_subplot(1, 1, 1)
54         pc = axe.pcolor(vector)
55        
56         axe.set_xlabel('Time (hh:mm UTC)')
57         axe.set_ylabel('Altitude (m)')
58         axe.set_xbound(upper=numIntervals)
59        
60         xticks = axe.get_xticks()
61         xticklabels = [(int(x) * timeInterval) + beginStamp
62                        for x in xticks]
63         xticklabels = [':'.join(('%02u' % x.hour, '%02u' % x.minute))
64                        for x in xticklabels]
65         axe.set_xticklabels(xticklabels)
66        
67         yticks = axe.get_yticks()
68         yticklabels = [str(y * altInterval + minAltitude)
69                        for y in yticks]
70         axe.set_yticklabels(yticklabels)
71        
72         axe.set_title(title)
73         cb = p.colorbar(pc)
74         cb.set_label(cbLabel)
75        
76         fig.savefig(os.path.join(pathout, fileout))
77         fig.clear()
78        
79         return 0
80    
81     uComponents = arrayDataObject.uComponents
82     maskedUComponents = n.ma.masked_where(n.isnan(uComponents),
83                                           uComponents)
84     makePcolor(maskedUComponents.T,
85                'U Component of Wind Velocity for %s' %
86                (str(beginStamp)[:10],),
87                _uComponentsPlotName,
88                'Speed (cm/sec)')
89    
90     vComponents = arrayDataObject.vComponents
91     maskedVComponents = n.ma.masked_where(n.isnan(vComponents),
92                                           vComponents)
93     makePcolor(maskedVComponents.T,
94                'V Component of Wind Velocity for %s' %
95                (str(beginStamp)[:10],),
96                _vComponentsPlotName,
97                'Speed (cm/sec)')
98    
99     wComponents = arrayDataObject.wComponents
100     maskedWComponents = n.ma.masked_where(n.isnan(wComponents),
101                                           wComponents)
102     makePcolor(maskedWComponents.T,
103                'W Component of Wind Velocity for %s' %
104                (str(beginStamp)[:10],),
105                _wComponentsPlotName,
106                'Speed (cm/sec)')
107    
108     echoStrengths = arrayDataObject.echoStrengths
109     maskedEchoStrenths = n.ma.masked_where(n.isnan(echoStrengths),
110                                                    echoStrengths)
111     makePcolor(maskedEchoStrenths.T,
112                'Echo Strength for %s' %
113                (str(beginStamp)[:10],),
114                _echoStrengthsPlotName,
115                'Strength (no units)')
116    
117     # timeComponent = n.array(range(numIntervals))
118     # altComponent = n.array(range(numAltitudes))
119     # fig = p.figure(1)
120     # axe = fig.add_subplot(1, 1, 1)
121     # qv = axe.quiver(altComponent,
122     #                 timeComponent,
123     #                 maskedUComponents.T,
124     #                 maskedVComponents.T)
125    
126     # axe.set_xlabel('Time (min)')
127     # axe.set_ylabel('Altitude (m)')
128     # axe.set_xbound(upper=numIntervals)
129    
130     # xticks = axe.get_xticks()
131     # xticklabels = [(int(x) * timeInterval) + beginStamp
132     #                for x in xticks]
133     # xticklabels = [':'.join(('%02u' % x.hour, '%02u' % x.minute))
134     #                for x in xticklabels]
135     # axe.set_xticklabels(xticklabels)
136    
137     # yticks = axe.get_yticks()
138     # yticklabels = [str(y * altInterval + minAltitude)
139     #                for y in yticks]
140     # axe.set_yticklabels(yticklabels)
141    
142     # axe.set_title('Wind Velocty for %s' %
143     #               (str(beginStamp)[:10],))
144     # cb = p.colorbar(qv)
145     # cb.set_label('W Component Speed (cm/sec)')
146    
147     # fig.savefig(os.path.join(pathout, _quiverPlotName))
148     # fig.clear()
149    
150     return 0
151
152 def copyLatest(pathout, latest, plotName):
153     """
154     Copy the latest plot to the official NCCOOS place.
155    
156     copyLatest(pathout, latest, plotName) -> rc
157    
158     pathout - path to directory where the latest plot is.
159     latest - path to directory where the latest plot is to be copied.
160     plotName - name of the plot file to copy.
161     rc - 0 if successful
162     """
163     shutil.copy(os.path.join(pathout, plotName),
164                 os.path.join(latest,
165                              _latestPlotNamePrefix + plotName))
166     return 0
167
168 def plotAll(source, destination, latest, force):
169     """
170     Plot all sodar raw data files.
171    
172     plotAll(source, destination) -> rc
173    
174     source - path to raw data directory in NCCOOS format.
175     detination - path to plot images directory in NCCOOS format.
176     latest - path to latest plots images directory in NCCOOS format.
177     force - update all destination plots for which raw data sources exist.
178     rc - 0 if successful
179     """
180    
181     if source.endswith(os.path.sep):
182         source = source.rstrip(os.path.sep)
183    
184     if destination.endswith(os.path.sep):
185         destination = destination.rstrip(os.path.sep)
186    
187     walkList = findMissing.findMissing(source, destination, force)
188    
189     for filein, pathout in walkList:
190         plotSingle(filein, pathout)
191    
192     if not os.path.exists(latest):
193         os.makedirs(latest, mode=0775)
194
195     filein, pathout = walkList[-1]
196     copyLatest(pathout, latest, _uComponentsPlotName)
197     copyLatest(pathout, latest, _vComponentsPlotName)
198     copyLatest(pathout, latest, _wComponentsPlotName)
199     copyLatest(pathout, latest, _echoStrengthsPlotName)
200     # copyLatest(pathout, latest, _quiverPlotName)
201    
202     return 0
203
204 def _main():
205     """bin/python %prog [options] /path/to/raw/files/ /path/to/plots/ /path/to/latest/plot/"""
206    
207     __description__ = 'Plot all sodar raw data files.'
208    
209     parser = optparse.OptionParser(usage=_main.__doc__,
210                                    version='%prog 1.0',
211                                    description=__description__)
212     parser.set_defaults(forceUpdate=False)
213     parser.add_option('-f', '--force-update',
214                       action='store_true',
215                       dest='force',
216                       help='update all plots for which raw data exists')
217     (values, args) = parser.parse_args()
218     (source, destination, latest) = tuple(args)
219     plotAll(source, destination, latest, values.force)
220    
221     return 0
222
223 if __name__ == "__main__":
224     _main()
Note: See TracBrowser for help on using the browser.