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

root/sodar/trunk/plotSodar.py

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

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