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

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

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

Clean up plots, add polar coordinates, add standard deviations.

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     umean = mu.mean(axis=0).data
80     vmean = mv.mean(axis=0).data
81     wmean = mw.mean(axis=0).data
82  
83     ustd = mu.std(axis=0).data
84     vstd = mv.std(axis=0).data
85     wstd = mw.std(axis=0).data
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     mtheta = 360 * ((np.arctan(mv/mu))/np.pi)
93  
94     rhomean = mrho.mean(axis=0).data
95     thetamean = mtheta.mean(axis=0).data
96
97     rhostd = mrho.std(axis=0).data
98     thetastd = mtheta.std(axis=0).data
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 (cm/s)',fontsize=spt.get_fontsize()*0.6)
107     ylabel('Elevation (m)')
108     xlim(-8,8)
109     ylim(0,200)
110     xticks(fontsize=spt.get_fontsize()*0.6)
111     plot(umean,z,label=r'$\bar{u}$')
112     plot(vmean,z,label=r'$\bar{v}$')
113     plot(wmean,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,200)
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 (cm/s)',fontsize=spt.get_fontsize()*0.6)
132     xlim(0,10)
133     ylim(0,200)
134     xticks(fontsize=spt.get_fontsize()*0.6)
135     yticks([])
136     plot(rhomean,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 (Degrees from North)',fontsize=spt.get_fontsize()*0.6)
143     xlim(-180,180)
144     ylim(0,200)
145     xticks(range(-180,181,90),fontsize=spt.get_fontsize()*0.6)
146     yticks([])
147     plot(thetamean,z,label=r'$\bar{\theta}$')
148     legend(loc='best',prop={'size':spt.get_fontsize()*0.6})
149     grid(True)
150  
151     subplot(2,4,5)
152     title('Wind Speed Std Dev',fontsize=spt.get_fontsize()*0.5)
153     xlabel('Std Dev (cm/s)',fontsize=spt.get_fontsize()*0.6)
154     ylabel('Elevation (m)')
155     xlim(0,16)
156     ylim(0,200)
157     xticks(fontsize=spt.get_fontsize()*0.6)
158     plot(ustd,z,label=r'$\sigma_u$')
159     plot(vstd,z,label=r'$\sigma_v$')
160     plot(wstd,z,label=r'$\sigma_w$')
161     legend(loc='best',prop={'size':spt.get_fontsize()*0.6})
162     grid(True)
163  
164     subplot(2,4,6)
165     title('Number of Valid Samples',fontsize=spt.get_fontsize()*0.5)
166     xlabel('% of Samples per Month',fontsize=spt.get_fontsize()*0.6)
167     xlim(0,100)
168     ylim(0,200)
169     xticks(range(0,101,25),fontsize=spt.get_fontsize()*0.6)
170     yticks([])
171     plot((ucount * 100.0)/float(mu.shape[0]),z,label=r'$\%\ u|v$')
172     plot((wcount * 100.0)/float(mw.shape[0]),z,label=r'$\%\ w$')
173     legend(loc='best',prop={'size':spt.get_fontsize()*0.6})
174     grid(True)
175
176     subplot(2,4,7)
177     title('Wind Velocity Std Dev (Polar)',fontsize=spt.get_fontsize()*0.5)
178     xlabel('Std Dev (cm/s)',fontsize=spt.get_fontsize()*0.6)
179     xlim(0,10)
180     ylim(0,200)
181     xticks(fontsize=spt.get_fontsize()*0.6)
182     yticks([])
183     plot(rhostd,z,label=r'$\sigma_\rho$')
184     legend(loc='best',prop={'size':spt.get_fontsize()*0.6})
185     grid(True)
186
187     subplot(2,4,8)
188     title('Wind Direction Std Dev (Polar)',fontsize=spt.get_fontsize()*0.5)
189     xlabel('Std Dev (Degrees)',fontsize=spt.get_fontsize()*0.6)
190     xlim(0,360)
191     ylim(0,200)
192     xticks(range(0,361,90),fontsize=spt.get_fontsize()*0.6)
193     yticks([])
194     plot(thetastd,z,label=r'$\sigma_\theta$')
195     legend(loc='best',prop={'size':spt.get_fontsize()*0.6})
196     grid(True)
197  
198     outFile = os.path.join(pngDir,ncFileName + os.extsep + pngExt)
199     print 'Saving', outFile
200     fig.savefig(outFile)
201  
202     htmlFile = os.path.join(pngDir,ncFileName + os.extsep + htmlExt)
203     html = html1 + month + " " + year
204     html = html + html2 + previous + html3 + next + html4
205     html = html + os.path.basename(outFile) + html5
206     handle = open(htmlFile,'w')
207     handle.write(html)
208     handle.close()
209  
210     if first:
211         first = False
212         htmlFile = os.path.join(pngDir,"index" + os.extsep + htmlExt)
213         handle = open(htmlFile,'w')
214         handle.write(html)
215         handle.close()
216          
217  
Note: See TracBrowser for help on using the browser.