1 |
|
---|
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 |
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 Veloctiy 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 Veloctiy 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 Veloctiy 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 |
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|
118 |
|
---|
119 |
|
---|
120 |
|
---|
121 |
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 |
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 |
|
---|
143 |
|
---|
144 |
|
---|
145 |
return 0 |
---|
146 |
|
---|
147 |
def plotAll(source, destination, force): |
---|
148 |
""" |
---|
149 |
Plot all sodar raw data files. |
---|
150 |
|
---|
151 |
plotAll(source, destination) -> rc |
---|
152 |
|
---|
153 |
source - path to raw data directory in NCCOOS format. |
---|
154 |
detination - path to plot images directory in NCCOOS format. |
---|
155 |
force - update all destination plots for which raw data sources exist. |
---|
156 |
rc - 0 if successful |
---|
157 |
""" |
---|
158 |
|
---|
159 |
if source.endswith(os.path.sep): |
---|
160 |
source = source.rstrip(os.path.sep) |
---|
161 |
|
---|
162 |
if destination.endswith(os.path.sep): |
---|
163 |
destination = destination.rstrip(os.path.sep) |
---|
164 |
|
---|
165 |
walkList = findMissing.findMissing(source, destination, force) |
---|
166 |
|
---|
167 |
for filein, pathout in walkList: |
---|
168 |
plotSingle(filein, pathout) |
---|
169 |
|
---|
170 |
return 0 |
---|
171 |
|
---|
172 |
def _main(): |
---|
173 |
"""bin/python %prog [options] /path/to/raw/files/ /path/to/plots/dir/""" |
---|
174 |
|
---|
175 |
__description__ = 'Plot all sodar raw data files.' |
---|
176 |
|
---|
177 |
parser = optparse.OptionParser(usage=_main.__doc__, |
---|
178 |
version='%prog 1.0', |
---|
179 |
description=__description__) |
---|
180 |
parser.set_defaults(forceUpdate=False) |
---|
181 |
parser.add_option('-f', '--force-update', |
---|
182 |
action='store_true', |
---|
183 |
dest='force', |
---|
184 |
help='update all plots for which raw data exists') |
---|
185 |
(values, args) = parser.parse_args() |
---|
186 |
(source, destination) = tuple(args) |
---|
187 |
plotAll(source, destination, values.force) |
---|
188 |
|
---|
189 |
return 0 |
---|
190 |
|
---|
191 |
if __name__ == "__main__": |
---|
192 |
_main() |
---|