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 |
catalogData = [(catalogDate[:4], catalogDate[4:6], catalogDate[6:]) |
---|
184 |
for catalogDate in catalogData] |
---|
185 |
catalogHandle = open(os.path.join(catalog, _catalogName), 'wb') |
---|
186 |
writer = csv.writer(catalogHandle) |
---|
187 |
writer.writerows(catalogData) |
---|
188 |
catalogHandle.close() |
---|
189 |
|
---|
190 |
return 0 |
---|
191 |
|
---|
192 |
def plotAll(source, destination, latest, catalog, force): |
---|
193 |
""" |
---|
194 |
Plot all sodar raw data files. |
---|
195 |
|
---|
196 |
plotAll(source, destination, latest, catalog, force) -> rc |
---|
197 |
|
---|
198 |
source - path to raw data directory in NCCOOS format. |
---|
199 |
destination - path to plot images directory in NCCOOS format. |
---|
200 |
latest - path to latest plots images directory in NCCOOS format. |
---|
201 |
catalog - path to plot catalog. |
---|
202 |
force - update all destination plots for which raw data sources exist. |
---|
203 |
rc - 0 if successful |
---|
204 |
""" |
---|
205 |
|
---|
206 |
if source.endswith(os.path.sep): |
---|
207 |
source = source.rstrip(os.path.sep) |
---|
208 |
|
---|
209 |
if destination.endswith(os.path.sep): |
---|
210 |
destination = destination.rstrip(os.path.sep) |
---|
211 |
|
---|
212 |
walkList = findMissing.findMissing(source, destination, force) |
---|
213 |
|
---|
214 |
for filein, pathout in walkList: |
---|
215 |
plotSingle(filein, pathout) |
---|
216 |
|
---|
217 |
if not os.path.exists(latest): |
---|
218 |
os.makedirs(latest, mode=0775) |
---|
219 |
|
---|
220 |
filein, pathout = walkList[-1] |
---|
221 |
copyLatest(pathout, latest, _uComponentsPlotName) |
---|
222 |
copyLatest(pathout, latest, _vComponentsPlotName) |
---|
223 |
copyLatest(pathout, latest, _wComponentsPlotName) |
---|
224 |
copyLatest(pathout, latest, _echoStrengthsPlotName) |
---|
225 |
# copyLatest(pathout, latest, _quiverPlotName) |
---|
226 |
|
---|
227 |
createCatalog(destination, catalog) |
---|
228 |
|
---|
229 |
return 0 |
---|
230 |
|
---|
231 |
def _main(): |
---|
232 |
"""bin/python %prog [options] /path/to/raw/files/ /path/to/plots/ /path/to/latest/plot/ /path/to/catalog/""" |
---|
233 |
|
---|
234 |
__description__ = 'Plot all sodar raw data files.' |
---|
235 |
|
---|
236 |
parser = optparse.OptionParser(usage=_main.__doc__, |
---|
237 |
version='%prog 1.0', |
---|
238 |
description=__description__) |
---|
239 |
parser.set_defaults(force=False) |
---|
240 |
parser.add_option('-f', '--force-update', |
---|
241 |
action='store_true', |
---|
242 |
dest='force', |
---|
243 |
help='update all plots for which raw data exists') |
---|
244 |
(values, args) = parser.parse_args() |
---|
245 |
(source, destination, latest, catalog) = tuple(args) |
---|
246 |
plotAll(source, destination, latest, catalog, values.force) |
---|
247 |
|
---|
248 |
return 0 |
---|
249 |
|
---|
250 |
if __name__ == "__main__": |
---|
251 |
_main() |
---|