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

root/proc2plot/trunk/proc2plot/spin/spin_ims_sodar1_plot_month.py

Revision 326 (checked in by haines, 14 years ago)

import first verison proc2plot

  • Property svn:executable set to
Line 
1 #!/usr/bin/env python
2 # Last modified:  Time-stamp: <2008-12-17 15:37:00 haines>
3 """ims_sodar_plot_month"""
4
5 # plot each month
6
7 import os, sys, glob, re
8 import datetime, time, dateutil, dateutil.tz
9 import pycdf
10 import numpy
11
12 sys.path.append('/home/haines/nccoos/raw2proc')
13 del(sys)
14
15 os.environ["MPLCONFIGDIR"]="/home/haines/.matplotlib/"
16
17 from pylab import figure, twinx, savefig, setp, getp, cm, colorbar
18 from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date
19 import procutil
20
21 print 'ims_sodar_plot_month ...'
22
23 proc_dir = '/seacoos/data/nccoos/level1/ims/sodar/'
24 fns = glob.glob((os.path.join(proc_dir, '*.nc')))
25 fns.sort()
26
27 print len(fns)
28
29 for fn in fns[14:16]:
30     m=re.search('\d{4}_\d{2}', fn)
31     yyyy_mm = m.group()
32     prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
33    
34     # load data
35     print ' ... ... read: ' + fn
36     nc = pycdf.CDFMF((fn,))
37     ncvars = nc.variables()
38     # print ncvars
39     es = nc.var('time')[:]
40     units = nc.var('time').units
41     dt = [procutil.es2dt(e) for e in es]
42     # set timezone info to UTC (since data from level1 should be in UTC!!)
43     dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
44     # return new datetime based on computer local
45     dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
46     dn = date2num(dt)
47     z = nc.var('z')[:]
48     # convert cm/s to m/s
49     uu = nc.var('u')[:]/100.
50     vv = nc.var('v')[:]/100.
51     ww = nc.var('w')[:]/100.
52     echo = nc.var('echo')[:]
53     n1 = numpy.floor(nc.var('nois1')[:]/10.)
54     n2 = numpy.floor(nc.var('nois2')[:]/10.)
55     n3 = numpy.floor(nc.var('nois3')[:]/10.)
56     nc.close()
57
58     # retain original dt for addnan
59     dto = dt
60
61     # last dt in data for labels
62     dt1 = dt[-1]
63     dt2 = dt_local[-1]
64    
65     diff = abs(dt1 - dt2)
66     if diff.days>0:
67         last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
68     else:
69         last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
70                       + dt2.strftime(" on %b %d, %Y")
71
72     fig = figure(figsize=(10, 10))
73     fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
74
75 #######################################
76 # Plot month
77 #######################################
78
79     ax = fig.add_subplot(5,1,1)
80     axs = [ax]
81
82     # range for horizontal wind plots
83     cmin, cmax = (-20., 20.)
84     # print "%s : %g %g" % ('uv wind', cmin, cmax)
85     # use masked array to hide NaN's on plot
86     (dt, uu) = procutil.addnan(dto, uu)
87     dn = date2num(dt)
88     um = numpy.ma.masked_where(numpy.isnan(uu), uu)
89     pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax)
90     pc.set_label('True Eastward Wind (m s-1)')
91     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
92     ax.set_ylabel('Height (m)')
93     # ax.set_ylim(-26.,2.)
94
95 # setup colorbar axes instance.
96     l,b,w,h = ax.get_position()
97     cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03])
98
99     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
100     cb.set_label('Wind Velocity (m s-1)')
101     cb.ax.xaxis.set_label_position('top')
102     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
103     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
104     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
105     #cb.ax.set_xticklabels([-16, -8, 0, 8, 16])
106
107 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
108     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
109     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
110     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
111     ax.set_xticklabels([])
112
113 # this only moves the label not the tick labels
114     ax.xaxis.set_label_position('top')
115     ax.set_xlabel('IMS SODAR -- ' + yyyy_mm)
116
117 # right-hand side scale
118     ax2 = twinx(ax)
119     ax2.yaxis.tick_right()
120 # convert (lhs) meters to (rhs) feet
121     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
122     ax2.set_ylim(feet)
123     ax2.set_ylabel('Height (ft)')
124
125 #######################################
126 #
127     ax = fig.add_subplot(5,1,2)
128     axs.append(ax)
129
130 # use masked array to hide NaN's on plot
131     (dt, vv) = procutil.addnan(dto, vv)
132     dn = date2num(dt)
133     vm = numpy.ma.masked_where(numpy.isnan(vv), vv)
134     pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax)
135     pc.set_label('True Northward Wind (m s-1)')
136     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
137     ax.set_ylabel('Height (m)')
138     # ax.set_ylim(-26.,2)
139
140     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
141     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
142     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
143     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
144     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
145     ax.set_xticklabels([])
146
147     # right-hand side scale
148     ax2 = twinx(ax)
149     ax2.yaxis.tick_right()
150     # convert (lhs) meters to (rhs) feet
151     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
152     ax2.set_ylim(feet)
153     ax2.set_ylabel('Height (ft)')
154    
155     #######################################
156     #
157     ax = fig.add_subplot(5,1,3)
158     axs.append(ax)
159    
160     # range for horizontal wind plots
161     cmin, cmax = (-5., 5.)
162     # print "%s : %g %g" % ('w wind', cmin, cmax)
163
164     # use masked array to hide NaN's on plot
165     (dt, ww) = procutil.addnan(dto, ww)
166     dn = date2num(dt)
167     wm = numpy.ma.masked_where(numpy.isnan(ww), ww)
168     pc = ax.pcolor(dn, z, wm.T, vmin=cmin, vmax=cmax)
169     # pc.set_label('Upward Wind (m s-1)')
170     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
171     ax.set_ylabel('Height (m)')
172     # ax.set_ylim(-26.,2)
173    
174     # setup colorbar axes instance.
175     l,b,w,h = ax.get_position()
176     cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03])
177
178     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
179     cb.set_label('Upward Wind Velocity (m s-1)')
180     cb.ax.xaxis.set_label_position('top')
181     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
182     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
183     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
184
185     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
186     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
187     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
188     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
189     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
190     ax.set_xticklabels([])
191    
192     # right-hand side scale
193     ax2 = twinx(ax)
194     ax2.yaxis.tick_right()
195     # convert (lhs) meters to (rhs) feet
196     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
197     ax2.set_ylim(feet)
198     ax2.set_ylabel('Height (ft)')
199    
200     #######################################
201     #
202     ax = fig.add_subplot(5,1,4)
203     axs.append(ax)
204    
205     cmin, cmax = (0., 1000.)
206     # print "%s : %g %g" % ('echo', cmin, cmax)
207
208     # use masked array to hide NaN's on plot
209     (dt, echo) = procutil.addnan(dto, echo)
210     dn = date2num(dt)
211     em = numpy.ma.masked_where(numpy.isnan(echo), echo)
212     pc = ax.pcolor(dn, z, em.T, vmin=cmin, vmax=cmax)
213     # pc.set_label('Echo Strength ()')
214     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
215     ax.set_ylabel('Height (m)')
216     # ax.set_ylim(-26.,2)
217
218     # setup colorbar axes instance.
219     l,b,w,h = ax.get_position()
220     cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03])
221
222     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
223     cb.set_label('Echo Strength')
224     cb.ax.xaxis.set_label_position('top')
225     cb.ax.set_xticks([0.0, 1.0])
226     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
227     cb.ax.set_xticklabels([str(cmin), str(cmax)])
228    
229     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
230     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
231     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
232     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
233     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
234     ax.set_xticklabels([])
235    
236    
237     # right-hand side scale
238     ax2 = twinx(ax)
239     ax2.yaxis.tick_right()
240     # convert (lhs) meters to (rhs) feet
241     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
242     ax2.set_ylim(feet)
243     ax2.set_ylabel('Height (ft)')
244    
245    
246     #######################################
247     #
248     ax = fig.add_subplot(5,1,5)
249     axs.append(ax)
250        
251     # ax.plot returns a list of lines, so unpack tuple
252     (dt, n1) = procutil(dto, n1)
253     l1, = ax.plot_date(dt, n1, fmt='b-')
254     l1.set_label('n1 each beam')
255     (dt, n2) = procutil(dto, n2)
256     l2, = ax.plot_date(dt, n2, fmt='c-')
257     l2.set_label('n2')   
258     (dt, n3) = procutil(dto, n3)
259     l3, = ax.plot_date(dt, n3, fmt='g-')
260     l3.set_label('n3')
261    
262     ax.set_ylabel('Ambient\n Noise')
263     ax.set_ylim(20,120)
264     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
265     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
266     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
267     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
268
269     ax.set_xlabel('IMS SODAR -- ' + yyyy_mm)
270    
271     # legend
272     ls1 = l1.get_label()
273     ls2 = l2.get_label()
274     ls3 = l3.get_label()
275     leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), loc='upper left')
276     ltext  = leg.get_texts()  # all the text.Text instance in the legend
277     llines = leg.get_lines()  # all the lines.Line2D instance in the legend
278     frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
279     frame.set_facecolor('0.80')      # set the frame face color to light gray
280     frame.set_alpha(0.5)             # set alpha low to see through
281     setp(ltext, fontsize='small')    # the legend text fontsize
282     setp(llines, linewidth=1.5)      # the legend linewidth
283     # leg.draw_frame(False)           # don't draw the legend frame
284
285     # save figure
286    
287     ofn = '/home/haines/rayleigh/img/ims/ims_sodar1_'+yyyy_mm+'.png'
288     print '... ... write: %s' % (ofn,)
289     savefig(ofn)
290
Note: See TracBrowser for help on using the browser.