1 |
import os,datetime,glob |
---|
2 |
import multiprocessing |
---|
3 |
import numpy as np |
---|
4 |
import matplotlib as mpl |
---|
5 |
mpl.use("Agg") |
---|
6 |
from pylab import figure, suptitle, subplot, plot, \ |
---|
7 |
xlim, ylim, xticks, yticks, legend, grid, \ |
---|
8 |
xlabel, ylabel, title |
---|
9 |
import pycdf |
---|
10 |
|
---|
11 |
ncDir = '/seacoos/data/nccoos/level1/billymitchell/sodar1' |
---|
12 |
ncFileGlob = 'billymitchell_sfas_*.nc' |
---|
13 |
|
---|
14 |
pngDir = '/seacoos/data/nccoos/level1/billymitchell/sodar1/plots/meanspeed' |
---|
15 |
pngExt = 'png' |
---|
16 |
htmlExt = 'html' |
---|
17 |
|
---|
18 |
ncFilePattern = os.path.join(ncDir,ncFileGlob) |
---|
19 |
files = glob.glob(ncFilePattern) |
---|
20 |
previous = [files[-1]] + files[:-1] |
---|
21 |
next = files[1:] + [files[0]] |
---|
22 |
files = zip(previous,files,next) |
---|
23 |
|
---|
24 |
html1 = """<html> |
---|
25 |
<head> |
---|
26 |
<title> |
---|
27 |
Billy Mitchell Sodar :: """ |
---|
28 |
|
---|
29 |
html2 = """ :: Mean Speed |
---|
30 |
</title> |
---|
31 |
</head> |
---|
32 |
<body> |
---|
33 |
<form> |
---|
34 |
<a href=\"""" |
---|
35 |
|
---|
36 |
html3 = """\"><Previous</a> |
---|
37 |
<a href=\"""" |
---|
38 |
|
---|
39 |
html4 = """\">Next></a> |
---|
40 |
</form> |
---|
41 |
<br/> |
---|
42 |
<img src=\"""" |
---|
43 |
|
---|
44 |
html5 = """\"> |
---|
45 |
</body> |
---|
46 |
</html>""" |
---|
47 |
|
---|
48 |
def _components(fileSpec): |
---|
49 |
"Figure out year and month from file specification." |
---|
50 |
|
---|
51 |
fileName = os.path.splitext(os.path.basename(fileSpec))[0] |
---|
52 |
year = fileName[-7:-3] |
---|
53 |
month = fileName[-2:] |
---|
54 |
monthName = datetime.datetime(int(year),int(month),1).strftime('%B') |
---|
55 |
return (fileName, year, month, monthName) |
---|
56 |
|
---|
57 |
def meanspeed((previous, ncFile, next), pngDir, genHtml=False, lastMonth=False): |
---|
58 |
print 'Processing',ncFile |
---|
59 |
ncFileName, year, month, monthName = _components(ncFile) |
---|
60 |
previous, previousYear, previousMonth, previousMonthName = _components(previous) |
---|
61 |
next, nextYear, nextMonth, nextMonthName = _components(next) |
---|
62 |
|
---|
63 |
nc = pycdf.CDF(ncFile) |
---|
64 |
|
---|
65 |
t = nc.var('time')[:] |
---|
66 |
z = nc.var('z')[:] |
---|
67 |
u = nc.var('u')[:] |
---|
68 |
v = nc.var('v')[:] |
---|
69 |
w = nc.var('w')[:] |
---|
70 |
|
---|
71 |
mu = np.ma.masked_invalid(u) |
---|
72 |
mv = np.ma.masked_invalid(v) |
---|
73 |
mw = np.ma.masked_invalid(w) |
---|
74 |
|
---|
75 |
mumean = mu.mean(axis=0) |
---|
76 |
mvmean = mv.mean(axis=0) |
---|
77 |
mwmean = mw.mean(axis=0) |
---|
78 |
|
---|
79 |
mustd = mu.std(axis=0) |
---|
80 |
mvstd = mv.std(axis=0) |
---|
81 |
mwstd = mw.std(axis=0) |
---|
82 |
|
---|
83 |
ucount = mu.count(axis=0) |
---|
84 |
vcount = mv.count(axis=0) |
---|
85 |
wcount = mw.count(axis=0) |
---|
86 |
|
---|
87 |
mrho = np.sqrt((mu**2) + (mv**2)) |
---|
88 |
mrhomean = mrho.mean(axis=0) |
---|
89 |
mrhostd = mrho.std(axis=0) |
---|
90 |
|
---|
91 |
mthetamean = (180 * np.arctan2(mv.mean(axis=0), mu.mean(axis=0))/np.pi) |
---|
92 |
thetamean = np.piecewise(mthetamean, |
---|
93 |
(mthetamean <= 90, mthetamean > 90), |
---|
94 |
(lambda x: 90 - x, lambda x: 450 - x)) |
---|
95 |
|
---|
96 |
fig = figure() |
---|
97 |
fig.subplots_adjust(hspace=0.3) |
---|
98 |
spt = suptitle('Billy Mitchell Sodar :: ' + monthName + " " + year) |
---|
99 |
|
---|
100 |
subplot(2,4,1) |
---|
101 |
title('Mean Wind Speeds (Cartesian)',fontsize=spt.get_fontsize()*0.5) |
---|
102 |
xlabel('Speed (m/s)',fontsize=spt.get_fontsize()*0.6) |
---|
103 |
ylabel('Elevation (m)') |
---|
104 |
xlim(-8,8) |
---|
105 |
ylim(0,int(max(z))) |
---|
106 |
xticks(fontsize=spt.get_fontsize()*0.6) |
---|
107 |
plot(mumean.data,z,label=r'$\bar{u}$') |
---|
108 |
plot(mvmean.data,z,label=r'$\bar{v}$') |
---|
109 |
plot(mwmean.data,z,label=r'$\bar{w}$') |
---|
110 |
legend(loc='best',prop={'size':spt.get_fontsize()*0.6}) |
---|
111 |
grid(True) |
---|
112 |
|
---|
113 |
subplot(2,4,2) |
---|
114 |
title('Number of Valid Samples',fontsize=spt.get_fontsize()*0.5) |
---|
115 |
xlabel('% of Samples per Month',fontsize=spt.get_fontsize()*0.6) |
---|
116 |
xlim(0,100) |
---|
117 |
ylim(0,int(max(z))) |
---|
118 |
xticks(range(0,101,25),fontsize=spt.get_fontsize()*0.6) |
---|
119 |
yticks([]) |
---|
120 |
plot((ucount * 100.0)/float(mu.shape[0]),z,label=r'$\%\ u|v$') |
---|
121 |
plot((wcount * 100.0)/float(mw.shape[0]),z,label=r'$\%\ w$') |
---|
122 |
legend(loc='best',prop={'size':spt.get_fontsize()*0.6}) |
---|
123 |
grid(True) |
---|
124 |
|
---|
125 |
subplot(2,4,3) |
---|
126 |
title('Mean Wind Velocity (Polar)',fontsize=spt.get_fontsize()*0.5) |
---|
127 |
xlabel('Velocity (m/s)',fontsize=spt.get_fontsize()*0.6) |
---|
128 |
xlim(0,10) |
---|
129 |
ylim(0,int(max(z))) |
---|
130 |
xticks(fontsize=spt.get_fontsize()*0.6) |
---|
131 |
yticks([]) |
---|
132 |
plot(mrhomean.data,z,label=r'$\bar{\rho}$') |
---|
133 |
legend(loc='best',prop={'size':spt.get_fontsize()*0.6}) |
---|
134 |
grid(True) |
---|
135 |
|
---|
136 |
subplot(2,4,4) |
---|
137 |
title('Mean Wind Direction (Polar)',fontsize=spt.get_fontsize()*0.5) |
---|
138 |
xlabel('Direction (Wind From)',fontsize=spt.get_fontsize()*0.6) |
---|
139 |
xlim(0,360) |
---|
140 |
ylim(0,int(max(z))) |
---|
141 |
xticks(range(0,361,90),['N','E','S','W','N',], |
---|
142 |
fontsize=spt.get_fontsize()*0.6) |
---|
143 |
yticks([]) |
---|
144 |
plot(thetamean,z,label=r'$\bar{\theta}$') |
---|
145 |
legend(loc='best',prop={'size':spt.get_fontsize()*0.6}) |
---|
146 |
grid(True) |
---|
147 |
|
---|
148 |
subplot(2,4,5) |
---|
149 |
title('Wind Speed Std Dev',fontsize=spt.get_fontsize()*0.5) |
---|
150 |
xlabel('Std Dev (m/s)',fontsize=spt.get_fontsize()*0.6) |
---|
151 |
ylabel('Elevation (m)') |
---|
152 |
xlim(0,16) |
---|
153 |
ylim(0,int(max(z))) |
---|
154 |
xticks(fontsize=spt.get_fontsize()*0.6) |
---|
155 |
plot(mustd.data,z,label=r'$\sigma_u$') |
---|
156 |
plot(mvstd.data,z,label=r'$\sigma_v$') |
---|
157 |
plot(mwstd.data,z,label=r'$\sigma_w$') |
---|
158 |
legend(loc='best',prop={'size':spt.get_fontsize()*0.6}) |
---|
159 |
grid(True) |
---|
160 |
|
---|
161 |
subplot(2,4,6) |
---|
162 |
title('Number of Valid Samples',fontsize=spt.get_fontsize()*0.5) |
---|
163 |
xlabel('% of Samples per Month',fontsize=spt.get_fontsize()*0.6) |
---|
164 |
xlim(0,100) |
---|
165 |
ylim(0,int(max(z))) |
---|
166 |
xticks(range(0,101,25),fontsize=spt.get_fontsize()*0.6) |
---|
167 |
yticks([]) |
---|
168 |
plot((ucount * 100.0)/float(mu.shape[0]),z,label=r'$\%\ u|v$') |
---|
169 |
plot((wcount * 100.0)/float(mw.shape[0]),z,label=r'$\%\ w$') |
---|
170 |
legend(loc='best',prop={'size':spt.get_fontsize()*0.6}) |
---|
171 |
grid(True) |
---|
172 |
|
---|
173 |
subplot(2,4,7) |
---|
174 |
title('Wind Velocity Std Dev (Polar)',fontsize=spt.get_fontsize()*0.5) |
---|
175 |
xlabel('Std Dev (m/s)',fontsize=spt.get_fontsize()*0.6) |
---|
176 |
xlim(0,10) |
---|
177 |
ylim(0,int(max(z))) |
---|
178 |
xticks(fontsize=spt.get_fontsize()*0.6) |
---|
179 |
yticks([]) |
---|
180 |
plot(mrhostd.data,z,label=r'$\sigma_\rho$') |
---|
181 |
legend(loc='best',prop={'size':spt.get_fontsize()*0.6}) |
---|
182 |
grid(True) |
---|
183 |
|
---|
184 |
outFile = os.path.join(pngDir,ncFileName + os.extsep + pngExt) |
---|
185 |
print 'Saving', outFile |
---|
186 |
fig.savefig(outFile) |
---|
187 |
fig.clear() |
---|
188 |
|
---|
189 |
if genHtml: |
---|
190 |
htmlFile = os.path.join(pngDir,ncFileName + os.extsep + htmlExt) |
---|
191 |
html = html1 + month + " " + year |
---|
192 |
html = html + html2 + previous + os.extsep + htmlExt |
---|
193 |
html = html + html3 + next + os.extsep + htmlExt + html4 |
---|
194 |
html = html + os.path.basename(outFile) + html5 |
---|
195 |
handle = open(htmlFile,'w') |
---|
196 |
handle.write(html) |
---|
197 |
handle.close() |
---|
198 |
|
---|
199 |
if lastMonth: |
---|
200 |
htmlFile = os.path.join(pngDir,"index" + os.extsep + htmlExt) |
---|
201 |
handle = open(htmlFile,'w') |
---|
202 |
handle.write(html) |
---|
203 |
handle.close() |
---|
204 |
|
---|
205 |
if __name__ == "__main__": |
---|
206 |
lastMonth = False |
---|
207 |
for previous,ncFile,next in files: |
---|
208 |
if next <= ncFile: |
---|
209 |
lastMonth = True |
---|
210 |
p = multiprocessing.Process(target=meanspeed, args=((previous, ncFile, next), pngDir, True, lastMonth)) |
---|
211 |
p.start() |
---|
212 |
p.join() |
---|
213 |
if p.exitcode: |
---|
214 |
print "Exitcode %s from processing %s" % (p.exitcode, ncFile) |
---|