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 single sodar raw data file. |
---|
16 |
|
---|
17 |
plotSingle(filein, pathout) -> rc |
---|
18 |
|
---|
19 |
filein - path to raw data file. |
---|
20 |
pathout - 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 |
minAltitude = arrayDataObject.minAltitude |
---|
43 |
numAltitudes = arrayDataObject.numAltitudes |
---|
44 |
altInterval = arrayDataObject.altInterval |
---|
45 |
|
---|
46 |
def makePcolor(vector, title, fileout, cbLabel): |
---|
47 |
fig = p.figure(1) |
---|
48 |
axe = fig.add_subplot(1, 1, 1) |
---|
49 |
pc = axe.pcolor(vector) |
---|
50 |
|
---|
51 |
axe.set_xlabel('Time (hh:mm UTC)') |
---|
52 |
axe.set_ylabel('Altitude (m)') |
---|
53 |
axe.set_xbound(upper=numIntervals) |
---|
54 |
|
---|
55 |
xticks = axe.get_xticks() |
---|
56 |
xticklabels = [(int(x) * timeInterval) + beginStamp |
---|
57 |
for x in xticks] |
---|
58 |
xticklabels = [':'.join(('%02u' % x.hour, '%02u' % x.minute)) |
---|
59 |
for x in xticklabels] |
---|
60 |
axe.set_xticklabels(xticklabels) |
---|
61 |
|
---|
62 |
yticks = axe.get_yticks() |
---|
63 |
yticklabels = [str(y * altInterval + minAltitude) |
---|
64 |
for y in yticks] |
---|
65 |
axe.set_yticklabels(yticklabels) |
---|
66 |
|
---|
67 |
axe.set_title(title) |
---|
68 |
cb = p.colorbar(pc) |
---|
69 |
cb.set_label(cbLabel) |
---|
70 |
|
---|
71 |
fig.savefig(os.path.join(pathout, fileout)) |
---|
72 |
fig.clear() |
---|
73 |
|
---|
74 |
return 0 |
---|
75 |
|
---|
76 |
uComponents = arrayDataObject.uComponents |
---|
77 |
maskedUComponents = n.ma.masked_where(n.isnan(uComponents), |
---|
78 |
uComponents) |
---|
79 |
makePcolor(maskedUComponents.T, |
---|
80 |
'U Component of Wind Velocity for %s' % |
---|
81 |
(str(beginStamp)[:10],), |
---|
82 |
'uComponents.png', |
---|
83 |
'Speed (cm/sec)') |
---|
84 |
|
---|
85 |
vComponents = arrayDataObject.vComponents |
---|
86 |
maskedVComponents = n.ma.masked_where(n.isnan(vComponents), |
---|
87 |
vComponents) |
---|
88 |
makePcolor(maskedVComponents.T, |
---|
89 |
'V Component of Wind Velocity for %s' % |
---|
90 |
(str(beginStamp)[:10],), |
---|
91 |
'vComponents.png', |
---|
92 |
'Speed (cm/sec)') |
---|
93 |
|
---|
94 |
wComponents = arrayDataObject.wComponents |
---|
95 |
maskedWComponents = n.ma.masked_where(n.isnan(wComponents), |
---|
96 |
wComponents) |
---|
97 |
makePcolor(maskedWComponents.T, |
---|
98 |
'W Component of Wind Velocity for %s' % |
---|
99 |
(str(beginStamp)[:10],), |
---|
100 |
'wComponents.png', |
---|
101 |
'Speed (cm/sec)') |
---|
102 |
|
---|
103 |
echoStrengths = arrayDataObject.echoStrengths |
---|
104 |
maskedEchoStrenths = n.ma.masked_where(n.isnan(echoStrengths), |
---|
105 |
echoStrengths) |
---|
106 |
makePcolor(maskedEchoStrenths.T, |
---|
107 |
'Echo Strength for %s' % |
---|
108 |
(str(beginStamp)[:10],), |
---|
109 |
'echoStrengths.png', |
---|
110 |
'Strength (no units)') |
---|
111 |
|
---|
112 |
# timeComponent = n.array(range(numIntervals)) |
---|
113 |
# altComponent = n.array(range(numAltitudes)) |
---|
114 |
# fig = p.figure(1) |
---|
115 |
# axe = fig.add_subplot(1, 1, 1) |
---|
116 |
# qv = axe.quiver(altComponent, |
---|
117 |
# timeComponent, |
---|
118 |
# maskedUComponents.T, |
---|
119 |
# maskedVComponents.T) |
---|
120 |
|
---|
121 |
# axe.set_xlabel('Time (min)') |
---|
122 |
# axe.set_ylabel('Altitude (m)') |
---|
123 |
# axe.set_xbound(upper=numIntervals) |
---|
124 |
|
---|
125 |
# xticks = axe.get_xticks() |
---|
126 |
# xticklabels = [(int(x) * timeInterval) + beginStamp |
---|
127 |
# for x in xticks] |
---|
128 |
# xticklabels = [':'.join(('%02u' % x.hour, '%02u' % x.minute)) |
---|
129 |
# for x in xticklabels] |
---|
130 |
# axe.set_xticklabels(xticklabels) |
---|
131 |
|
---|
132 |
# yticks = axe.get_yticks() |
---|
133 |
# yticklabels = [str(y * altInterval + minAltitude) |
---|
134 |
# for y in yticks] |
---|
135 |
# axe.set_yticklabels(yticklabels) |
---|
136 |
|
---|
137 |
# axe.set_title('Wind Velocty for %s' % |
---|
138 |
# (str(beginStamp)[:10],)) |
---|
139 |
# cb = p.colorbar(qv) |
---|
140 |
# cb.set_label('W Component Speed (cm/sec)') |
---|
141 |
|
---|
142 |
# fig.savefig(os.path.join(pathout, 'quiver.png')) |
---|
143 |
# fig.clear() |
---|
144 |
|
---|
145 |
return 0 |
---|
146 |
|
---|
147 |
|
---|
148 |
def plotLatest(filein, pathout): |
---|
149 |
""" |
---|
150 |
Plot latest sodar raw data file. |
---|
151 |
|
---|
152 |
plotLatest(filein, pathout) -> rc |
---|
153 |
|
---|
154 |
filein - path to raw data file. |
---|
155 |
pathout - path to plot images directory. |
---|
156 |
rc - 0 if successful |
---|
157 |
""" |
---|
158 |
|
---|
159 |
if not os.path.exists(pathout): |
---|
160 |
os.makedirs(pathout, mode=0775) |
---|
161 |
|
---|
162 |
try: |
---|
163 |
rawDataHandle = openAnything.openAnything(filein) |
---|
164 |
rawDataString = rawDataHandle.read() |
---|
165 |
rawDataHandle.close() |
---|
166 |
except: |
---|
167 |
raise IOError("Failure to read raw data.") |
---|
168 |
|
---|
169 |
rawDataObject = a.rawData.RawData(rawDataString) |
---|
170 |
formattedDataObject = a.formattedData.FormattedData(rawDataObject) |
---|
171 |
arrayDataObject = a.ArrayData(formattedDataObject) |
---|
172 |
|
---|
173 |
beginStamp = arrayDataObject.beginStamp |
---|
174 |
endStamp = arrayDataObject.endStamp |
---|
175 |
numIntervals = arrayDataObject.numIntervals |
---|
176 |
timeInterval = arrayDataObject.timeInterval |
---|
177 |
minAltitude = arrayDataObject.minAltitude |
---|
178 |
numAltitudes = arrayDataObject.numAltitudes |
---|
179 |
altInterval = arrayDataObject.altInterval |
---|
180 |
|
---|
181 |
def makePcolor(vector, title, fileout, cbLabel): |
---|
182 |
fig = p.figure(1) |
---|
183 |
axe = fig.add_subplot(1, 1, 1) |
---|
184 |
pc = axe.pcolor(vector) |
---|
185 |
|
---|
186 |
axe.set_xlabel('Time (hh:mm UTC)') |
---|
187 |
axe.set_ylabel('Altitude (m)') |
---|
188 |
axe.set_xbound(upper=numIntervals) |
---|
189 |
|
---|
190 |
xticks = axe.get_xticks() |
---|
191 |
xticklabels = [(int(x) * timeInterval) + beginStamp |
---|
192 |
for x in xticks] |
---|
193 |
xticklabels = [':'.join(('%02u' % x.hour, '%02u' % x.minute)) |
---|
194 |
for x in xticklabels] |
---|
195 |
axe.set_xticklabels(xticklabels) |
---|
196 |
|
---|
197 |
yticks = axe.get_yticks() |
---|
198 |
yticklabels = [str(y * altInterval + minAltitude) |
---|
199 |
for y in yticks] |
---|
200 |
axe.set_yticklabels(yticklabels) |
---|
201 |
|
---|
202 |
axe.set_title(title) |
---|
203 |
cb = p.colorbar(pc) |
---|
204 |
cb.set_label(cbLabel) |
---|
205 |
|
---|
206 |
fig.savefig(os.path.join(pathout, fileout)) |
---|
207 |
fig.clear() |
---|
208 |
|
---|
209 |
return 0 |
---|
210 |
|
---|
211 |
uComponents = arrayDataObject.uComponents |
---|
212 |
maskedUComponents = n.ma.masked_where(n.isnan(uComponents), |
---|
213 |
uComponents) |
---|
214 |
makePcolor(maskedUComponents.T, |
---|
215 |
'U Component of Wind Velocity for %s' % |
---|
216 |
(str(beginStamp)[:10],), |
---|
217 |
'ims_sodar_latest_uComponents.png', |
---|
218 |
'Speed (cm/sec)') |
---|
219 |
|
---|
220 |
vComponents = arrayDataObject.vComponents |
---|
221 |
maskedVComponents = n.ma.masked_where(n.isnan(vComponents), |
---|
222 |
vComponents) |
---|
223 |
makePcolor(maskedVComponents.T, |
---|
224 |
'V Component of Wind Velocity for %s' % |
---|
225 |
(str(beginStamp)[:10],), |
---|
226 |
'ims_sodar_latest_vComponents.png', |
---|
227 |
'Speed (cm/sec)') |
---|
228 |
|
---|
229 |
wComponents = arrayDataObject.wComponents |
---|
230 |
maskedWComponents = n.ma.masked_where(n.isnan(wComponents), |
---|
231 |
wComponents) |
---|
232 |
makePcolor(maskedWComponents.T, |
---|
233 |
'W Component of Wind Velocity for %s' % |
---|
234 |
(str(beginStamp)[:10],), |
---|
235 |
'ims_sodar_latest_wComponents.png', |
---|
236 |
'Speed (cm/sec)') |
---|
237 |
|
---|
238 |
echoStrengths = arrayDataObject.echoStrengths |
---|
239 |
maskedEchoStrenths = n.ma.masked_where(n.isnan(echoStrengths), |
---|
240 |
echoStrengths) |
---|
241 |
makePcolor(maskedEchoStrenths.T, |
---|
242 |
'Echo Strength for %s' % |
---|
243 |
(str(beginStamp)[:10],), |
---|
244 |
'ims_sodar_latest_echoStrengths.png', |
---|
245 |
'Strength (no units)') |
---|
246 |
|
---|
247 |
# timeComponent = n.array(range(numIntervals)) |
---|
248 |
# altComponent = n.array(range(numAltitudes)) |
---|
249 |
# fig = p.figure(1) |
---|
250 |
# axe = fig.add_subplot(1, 1, 1) |
---|
251 |
# qv = axe.quiver(altComponent, |
---|
252 |
# timeComponent, |
---|
253 |
# maskedUComponents.T, |
---|
254 |
# maskedVComponents.T) |
---|
255 |
|
---|
256 |
# axe.set_xlabel('Time (min)') |
---|
257 |
# axe.set_ylabel('Altitude (m)') |
---|
258 |
# axe.set_xbound(upper=numIntervals) |
---|
259 |
|
---|
260 |
# xticks = axe.get_xticks() |
---|
261 |
# xticklabels = [(int(x) * timeInterval) + beginStamp |
---|
262 |
# for x in xticks] |
---|
263 |
# xticklabels = [':'.join(('%02u' % x.hour, '%02u' % x.minute)) |
---|
264 |
# for x in xticklabels] |
---|
265 |
# axe.set_xticklabels(xticklabels) |
---|
266 |
|
---|
267 |
# yticks = axe.get_yticks() |
---|
268 |
# yticklabels = [str(y * altInterval + minAltitude) |
---|
269 |
# for y in yticks] |
---|
270 |
# axe.set_yticklabels(yticklabels) |
---|
271 |
|
---|
272 |
# axe.set_title('Wind Velocty for %s' % |
---|
273 |
# (str(beginStamp)[:10],)) |
---|
274 |
# cb = p.colorbar(qv) |
---|
275 |
# cb.set_label('W Component Speed (cm/sec)') |
---|
276 |
|
---|
277 |
# fig.savefig(os.path.join(pathout, 'ims_sodar_latest_quiver.png')) |
---|
278 |
# fig.clear() |
---|
279 |
|
---|
280 |
return 0 |
---|
281 |
|
---|
282 |
def plotAll(source, destination, latest, force): |
---|
283 |
""" |
---|
284 |
Plot all sodar raw data files. |
---|
285 |
|
---|
286 |
plotAll(source, destination) -> rc |
---|
287 |
|
---|
288 |
source - path to raw data directory in NCCOOS format. |
---|
289 |
detination - path to plot images directory in NCCOOS format. |
---|
290 |
latest - path to latest plots images directory in NCCOOS format. |
---|
291 |
force - update all destination plots for which raw data sources exist. |
---|
292 |
rc - 0 if successful |
---|
293 |
""" |
---|
294 |
|
---|
295 |
if source.endswith(os.path.sep): |
---|
296 |
source = source.rstrip(os.path.sep) |
---|
297 |
|
---|
298 |
if destination.endswith(os.path.sep): |
---|
299 |
destination = destination.rstrip(os.path.sep) |
---|
300 |
|
---|
301 |
walkList = findMissing.findMissing(source, destination, force) |
---|
302 |
|
---|
303 |
for filein, pathout in walkList: |
---|
304 |
plotSingle(filein, pathout) |
---|
305 |
|
---|
306 |
filein, pathout = walkList[-1] |
---|
307 |
plotLatest(filein, latest) |
---|
308 |
|
---|
309 |
return 0 |
---|
310 |
|
---|
311 |
def _main(): |
---|
312 |
"""bin/python %prog [options] /path/to/raw/files/ /path/to/plots/ /path/to/latest/plot/""" |
---|
313 |
|
---|
314 |
__description__ = 'Plot all sodar raw data files.' |
---|
315 |
|
---|
316 |
parser = optparse.OptionParser(usage=_main.__doc__, |
---|
317 |
version='%prog 1.0', |
---|
318 |
description=__description__) |
---|
319 |
parser.set_defaults(forceUpdate=False) |
---|
320 |
parser.add_option('-f', '--force-update', |
---|
321 |
action='store_true', |
---|
322 |
dest='force', |
---|
323 |
help='update all plots for which raw data exists') |
---|
324 |
(values, args) = parser.parse_args() |
---|
325 |
(source, destination, latest) = tuple(args) |
---|
326 |
plotAll(source, destination, latest, values.force) |
---|
327 |
|
---|
328 |
return 0 |
---|
329 |
|
---|
330 |
if __name__ == "__main__": |
---|
331 |
_main() |
---|