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

root/sodarplot/trunk/sodarplot/scintec/winddist.py

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

Add html generation to winddist.

Line 
1 import os,datetime,glob
2 import numpy as np
3 import matplotlib as mpl
4 mpl.use("Agg")
5 from windrose import WindroseAxes
6 from matplotlib import pyplot as plt
7 import pycdf
8
9 ncDir = '/seacoos/data/nccoos/level1/billymitchell/sodar1'
10 ncFileGlob = 'billymitchell_sfas_*.nc'
11  
12 pngDir = '/seacoos/data/nccoos/level1/billymitchell/sodar1/plots/winddist'
13 if not os.path.exists(pngDir):
14     os.mkdir(pngDir)
15
16 pngExt = 'png'
17 htmlExt = 'html'
18  
19 ncFilePattern = os.path.join(ncDir,ncFileGlob)
20 files = glob.glob(ncFilePattern)
21 files = files[:-1] # sodar was broken last month
22 previous = [files[-1]] + files[:-1]
23 next = files[1:] + [files[0]]
24 files = zip(previous,files,next)
25
26 html1 = """<html>
27     <head>
28         <title>
29             Billy Mitchell Sodar :: """
30
31
32 html2 = """ :: Wind Distribution at """
33
34 html3 = """ Meters Above Sea Level
35         </title>
36     </head>
37     <body>
38         <form>
39             <a href=\""""
40
41 html4 = """\">&lt;Previous Month</a>&nbsp;&nbsp;&nbsp;
42             <a href=\""""
43
44 html5 = """\">Next Month&gt;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
45             <a href=\""""
46
47 html6 = """\">&lt;Previous Elevation</a>&nbsp;&nbsp;&nbsp;
48             <a href=\""""
49
50 html7 = """\">Next Elevation&gt;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
51             <a href=\"all.html\">&lt;All At Once&gt;</a>
52         </form>
53         <br/>
54         <img src=\""""
55
56 html8 = """\"/>
57     </body>
58 </html>"""
59  
60 firstMonth = True
61 for previous,ncFile,next in files:
62     print 'Processing',ncFile
63     ncFileName = os.path.splitext(os.path.basename(ncFile))[0]
64     year = ncFileName[19:23]
65     month = ncFileName[24:26]
66     month = datetime.datetime(int(year),int(month),1).strftime('%B')
67     if previous:
68         previous = os.path.splitext(os.path.basename(previous))[0]
69         previousYear = previous[19:23]
70         previousMonth = previous[24:26]
71         previousMonth = datetime.datetime(int(previousYear),int(previousMonth),1).strftime('%B')
72     if next:
73         next = os.path.splitext(os.path.basename(next))[0]
74         nextYear = next[19:23]
75         nextMonth = next[24:26]
76         nextMonth = datetime.datetime(int(nextYear),int(nextMonth),1).strftime('%B')
77  
78     nc = pycdf.CDF(ncFile)
79
80     t = nc.var('time')[:]
81     z = nc.var('z')[:]
82     u = nc.var('u')[:]
83     v = nc.var('v')[:]
84     w = nc.var('w')[:]
85  
86     mu = np.ma.masked_invalid(u)
87     mv = np.ma.masked_invalid(v)
88     mw = np.ma.masked_invalid(w)
89  
90     mumean = mu.mean(axis=0)
91     mvmean = mv.mean(axis=0)
92     mwmean = mw.mean(axis=0)
93  
94     ucount = mu.count(axis=0)
95     vcount = mv.count(axis=0)
96     wcount = mw.count(axis=0)
97
98     mrho = np.sqrt((mu**2) + (mv**2))
99     rho = mrho.T.data
100     mtheta = (180 * np.arctan2(mv, mu)/np.pi)
101     theta = np.piecewise(mtheta,
102                          (mtheta <= 90, mtheta > 90),
103                          (lambda x: 90 - x, lambda x: 450 - x))
104     theta = theta.T
105
106     firstElevation = True
107     indices = range(0,rho.shape[0])
108     previousX = [indices[-1]] + indices[:-1]
109     nextX = indices[1:] + [indices[0]]
110     indices = zip(previousX,indices,nextX)
111     for previousX,x,nextX in indices:
112         if rho[x].any():
113             fig = plt.figure(figsize=(8, 8), dpi=80, facecolor='w', edgecolor='w')
114             spt = fig.suptitle('Billy Mitchell Sodar :: ' + month + " " + year)
115             rect = [0.16, 0.16, 0.68, 0.68]
116             ax = WindroseAxes(fig, rect, axisbg='w')
117             ax.set_title('Wind Distribution at %d Meters Above Sea Level\nPercentage of Wind Magnitude Towards Direction\n\n' % z[x],fontsize=spt.get_fontsize()*0.8)
118             fig.add_axes(ax)
119             ax.bar(theta[x], rho[x], normed=True, opening=0.8, edgecolor='white')
120             l = ax.legend(axespad=-0.20,title="Magnitude (m/s)")
121             plt.setp(l.get_texts(), fontsize=8)
122             if not os.path.exists(os.path.join(pngDir,ncFileName)):
123                 os.mkdir(os.path.join(pngDir,ncFileName))
124             outFile = os.path.join(pngDir,ncFileName,('%dm' % z[x]) + os.extsep + pngExt)
125             print 'Saving', outFile
126             fig.savefig(outFile)
127
128             htmlFile = os.path.join(pngDir,ncFileName + "_" + ('%dm' % z[x]) + os.extsep + htmlExt)
129             html = html1 + month + " " + year
130             html = html + html2 + ('%dm' % z[x])
131             html = html + html3 + previous + "_" + ('%dm' % z[x]) + os.extsep + htmlExt
132             html = html + html4 + next + "_" + ('%dm' % z[x]) + os.extsep + htmlExt
133             html = html + html5 + ncFileName + "_" + ('%dm' % z[previousX]) + os.extsep + htmlExt
134             html = html + html6 + ncFileName + "_" + ('%dm' % z[nextX]) + os.extsep + htmlExt
135             html = html + html7 + os.path.join(ncFileName,os.path.basename(outFile)) + html8
136             handle = open(htmlFile,'w')
137             handle.write(html)
138             handle.close()
139
140             if firstMonth and firstElevation:
141                 firstMonth = False
142                 firstElevation = False
143                 htmlFile = os.path.join(pngDir,"index" + os.extsep + htmlExt)
144                 handle = open(htmlFile,'w')
145                 handle.write(html)
146                 handle.close()
147  
Note: See TracBrowser for help on using the browser.