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

root/sodarplot/trunk/sodarplot/scintec/meanspeed.py

Revision 78 (checked in by cbc, 13 years ago)

Heavily refactor winddist to prevent memory leaks.

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