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

root/proc2plot/trunk/proc2plot/billymitchell_sodar_plot.py

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

import first verison proc2plot

Line 
1 #!/usr/bin/env /opt/env/haines/dataproc/bin/python
2 # Last modified:  Time-stamp: <2010-04-12 13:51:06 haines>
3
4 """billymitchell_sodar_plot"""
5
6 #################################################
7 # [1a] search and replace XXXX with platform id
8 # [1b] search and replace YYYY with sensor id
9 # [1c] search and replace ZZZZ plot titles
10 #################################################
11
12 # plot each month
13
14 import os
15 # import sys, glob, re
16 import datetime, time, dateutil, dateutil.tz
17 import pycdf
18 import numpy
19
20 # sys.path.append('/opt/env/haines/dataproc/raw2proc')
21 # del(sys)
22
23 os.environ["MPLCONFIGDIR"]="/home/haines/.matplotlib/"
24
25 from pylab import figure, twinx, savefig, setp, getp, cm, colorbar
26 from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date
27 import procutil
28
29 def timeseries(pi, si, yyyy_mm, plot_type='latest'):
30     print 'billymitchell_sodar1_plot ...'
31
32     #
33
34     prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
35     yyyy_mm_str = this_month.strftime('%Y_%m')
36
37     ################################
38     # [2a] load primary data file
39     ################################
40
41     ncFile1='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+prev_month.strftime('%Y_%m')+'.nc'
42     ncFile2='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+this_month.strftime('%Y_%m')+'.nc'
43
44     have_ncFile1 = os.path.exists(ncFile1)
45     have_ncFile2 = os.path.exists(ncFile2)
46
47     print ' ... loading data for graph from ...'
48     print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
49     print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
50
51     # open netcdf data
52     if have_ncFile1 and have_ncFile2:
53         nc = pycdf.CDFMF((ncFile1, ncFile2))
54     elif not have_ncFile1 and have_ncFile2:
55         nc = pycdf.CDFMF((ncFile2,))
56     elif have_ncFile1 and not have_ncFile2:
57         nc = pycdf.CDFMF((ncFile1,))
58     else:
59         print ' ... both files do not exist -- NO DATA LOADED'
60         exit()
61
62     # ncvars = nc.variables()
63     # print ncvars
64     es = nc.var('time')[:]
65     units = nc.var('time').units
66     dt = [procutil.es2dt(e) for e in es]
67     # set timezone info to UTC (since data from level1 should be in UTC!!)
68     dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
69     # return new datetime based on computer local
70     dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
71     dn = date2num(dt)
72
73
74     ################################
75     # [2b] specify variables
76     ################################
77     z = nc.var('z')[:]
78     # convert cm/s to m/s
79     uu = nc.var('u')[:]
80     vv = nc.var('v')[:]
81     ww = nc.var('w')[:]
82     sigw = nc.var('sigw')[:]
83     echo = nc.var('bck')[:]
84
85     nc.close()
86
87     # retain original dt for addnan
88     dto = dt
89
90     # last dt in data for labels
91     dtu = dt[-1]
92     dtl = dt_local[-1]
93
94     diff = abs(dtu - dtl)
95     if diff.days>0:
96         last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')'
97     else:
98         last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \
99                       + dtl.strftime(" on %b %d, %Y")
100
101     #######################################
102     # Plot setup
103     #######################################
104
105     fig = figure(figsize=(10, 10))
106     fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
107
108     ax = fig.add_subplot(5,1,1)
109     axs = [ax]
110
111     # range for horizontal wind plots
112     cmin, cmax = (-20., 20.)
113     # print "%s : %g %g" % ('uv wind', cmin, cmax)
114     # use masked array to hide NaN's on plot
115     (dt, uu) = procutil.addnan(dto, uu)
116     dn = date2num(dt)
117     um = numpy.ma.masked_where(numpy.isnan(uu), uu)
118     pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax)
119     pc.set_label('True Eastward Wind (m s-1)')
120     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
121     ax.set_ylabel('Height (m)')
122
123     # setup colorbar axes instance.
124     l,b,w,h = ax.get_position().bounds
125     cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03])
126
127     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
128     cb.set_label('Wind Velocity (m s-1)')
129     cb.ax.xaxis.set_label_position('top')
130     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
131     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
132     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
133
134     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
135     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
136     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
137     ax.set_xticklabels([])
138
139     # this only moves the label not the tick labels
140     ax.xaxis.set_label_position('top')
141     ax.set_xlabel('BILLY MITCHELL SODAR -- ' + yyyy_mm_str)
142
143     # right-hand side scale
144     ax2 = twinx(ax)
145     ax2.yaxis.tick_right()
146     # convert (lhs) meters to (rhs) feet
147     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
148     ax2.set_ylim(feet)
149     ax2.set_ylabel('Height (ft)')
150
151     #######################################
152     #
153     ax = fig.add_subplot(5,1,2)
154     axs.append(ax)
155
156     # use masked array to hide NaN's on plot
157     (dt, vv) = procutil.addnan(dto, vv)
158     dn = date2num(dt)
159     vm = numpy.ma.masked_where(numpy.isnan(vv), vv)
160     pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax)
161     pc.set_label('True Northward Wind (m s-1)')
162     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
163     ax.set_ylabel('Height (m)')
164
165     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
166     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
167     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
168     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
169     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
170     ax.set_xticklabels([])
171
172     # right-hand side scale
173     ax2 = twinx(ax)
174     ax2.yaxis.tick_right()
175     # convert (lhs) meters to (rhs) feet
176     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
177     ax2.set_ylim(feet)
178     ax2.set_ylabel('Height (ft)')
179    
180     #######################################
181     #
182     ax = fig.add_subplot(5,1,3)
183     axs.append(ax)
184    
185     # range for horizontal wind plots
186     cmin, cmax = (-2., 2.)
187     # print "%s : %g %g" % ('w wind', cmin, cmax)
188
189     # use masked array to hide NaN's on plot
190     (dt, ww) = procutil.addnan(dto, ww)
191     dn = date2num(dt)
192     wm = numpy.ma.masked_where(numpy.isnan(ww), ww)
193     pc = ax.pcolor(dn, z, wm.T, vmin=cmin, vmax=cmax)
194     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
195     ax.set_ylabel('Height (m)')
196    
197     # setup colorbar axes instance.
198     l,b,w,h = ax.get_position().bounds
199     cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03])
200
201     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
202     cb.set_label('Upward Wind Velocity (m s-1)')
203     cb.ax.xaxis.set_label_position('top')
204     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
205     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
206     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
207
208     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
209     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
210     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
211     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
212     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
213     ax.set_xticklabels([])
214    
215     # right-hand side scale
216     ax2 = twinx(ax)
217     ax2.yaxis.tick_right()
218     # convert (lhs) meters to (rhs) feet
219     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
220     ax2.set_ylim(feet)
221     ax2.set_ylabel('Height (ft)')
222    
223     #######################################
224     #
225     ax = fig.add_subplot(5,1,4)
226     axs.append(ax)
227    
228     cmin, cmax = (0., 2.)
229     # print "%s : %g %g" % ('sigw', cmin, cmax)
230
231     # use masked array to hide NaN's on plot
232     (dt, sigw) = procutil.addnan(dto, sigw)
233     dn = date2num(dt)
234     sm = numpy.ma.masked_where(numpy.isnan(sigw), sigw)
235     pc = ax.pcolor(dn, z, sm.T, vmin=cmin, vmax=cmax)
236     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
237     ax.set_ylabel('Height (m)')
238
239     # setup colorbar axes instance.
240     l,b,w,h = ax.get_position().bounds
241     cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03])
242
243     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
244     cb.set_label('Std. Dev. of Vertical Wind (m s-1)')
245     cb.ax.xaxis.set_label_position('top')
246     cb.ax.set_xticks([0.0, 0.2, 0.4, 0.6, 0.8])
247     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
248     cb.ax.set_xticklabels([xtl[0], xtl[2], xtl[4], xtl[6], xtl[8]])
249    
250     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
251     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
252     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
253     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
254     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
255     ax.set_xticklabels([])
256    
257     # right-hand side scale
258     ax2 = twinx(ax)
259     ax2.yaxis.tick_right()
260     # convert (lhs) meters to (rhs) feet
261     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
262     ax2.set_ylim(feet)
263     ax2.set_ylabel('Height (ft)')
264
265     #######################################
266     #
267     ax = fig.add_subplot(5,1,5)
268     axs.append(ax)
269
270    
271     # cmin, cmax = (0., 1000.)
272     cmin, cmax = (2., 6.)
273     # print "%s : %g %g" % ('echo', cmin, cmax)
274
275     # use masked array to hide NaN's on plot
276     (dt, echo) = procutil.addnan(dto, numpy.log10(echo))
277     dn = date2num(dt)
278     em = numpy.ma.masked_where(numpy.isnan(echo), echo)
279     pc = ax.pcolor(dn, z, em.T, vmin=cmin, vmax=cmax)
280     # pc = ax.pcolor(dn, z, em.T)
281     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
282     ax.set_ylabel('Height (m)')
283
284     # setup colorbar axes instance.
285     l,b,w,h = ax.get_position().bounds
286     cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03])
287
288     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
289     cb.set_label('log10 Backscatter')
290     cb.ax.xaxis.set_label_position('top')
291     cb.ax.set_xticks([0.0, 1.0])
292     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
293     cb.ax.set_xticklabels([str(cmin), str(cmax)])
294    
295     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
296     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
297     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
298     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
299     ax.set_xticklabels([])
300    
301     # right-hand side scale
302     ax2 = twinx(ax)
303     ax2.yaxis.tick_right()
304     # convert (lhs) meters to (rhs) feet
305     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
306     ax2.set_ylim(feet)
307     ax2.set_ylabel('Height (ft)')
308
309     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
310     ax.set_xlabel('BILLY MITCHELL SODAR -- ' + yyyy_mm_str)
311     # save figure
312    
313     # save figure for this month
314     ofn = '/home/haines/rayleigh/img/billymitchell/billymitchell_sodar1_'+yyyy_mm_str+'.png'
315     print '... ... write: %s' % (ofn,)
316     savefig(ofn)
317
318
319     #######################################
320     # Last 30 days
321     #######################################
322     if plot_type=='latest':
323         print ' ... Last 30 days'
324         for idx, ax in enumerate(axs):
325             ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
326             ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
327             ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
328             ax.set_xticklabels([])
329             if idx==0:
330                 ax.set_xlabel('BILLY MITCHELL SODAR -- Last 30 days from ' + last_dt_str)
331             elif idx==len(axs)-1:
332                 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
333                 ax.set_xlabel('BILLY MITCHELL SODAR -- Last 30 days from ' + last_dt_str)
334
335         savefig('/home/haines/rayleigh/img/billymitchell_sodar1_last30days.png')
336
337
338     #######################################
339     # Last 7 days
340     #######################################
341     if plot_type=='latest':
342         print ' ... Last 7 days'
343         for idx, ax in enumerate(axs):
344             ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
345             ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
346             ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
347             ax.set_xticklabels([])
348             if idx==0:
349                 ax.set_xlabel('BILLY MITCHELL SODAR -- Last 7 days from ' + last_dt_str)
350             elif idx==len(axs)-1:
351                 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
352                 ax.set_xlabel('BILLY MITCHELL SODAR -- Last 7 days from ' + last_dt_str)
353                
354         savefig('/home/haines/rayleigh/img/billymitchell_sodar1_last07days.png')
355
356
357     #######################################
358     # Last 1 day (24hrs)
359     #######################################
360     if plot_type=='latest':
361         print ' ... Last 1 days'
362
363         for idx, ax in enumerate(axs):
364             ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
365             ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
366             ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
367             ax.set_xticklabels([])
368             if idx==0:
369                 ax.set_xlabel('BILLY MITCHELL SODAR -- Last 24 hours from ' + last_dt_str)
370             elif idx==len(axs)-1:
371                 ax.xaxis.set_major_formatter( DateFormatter('%H') )
372                 ax.set_xlabel('BILLY MITCHELL SODAR -- Last 24 hours from ' + last_dt_str)
373
374         savefig('/home/haines/rayleigh/img/billymitchell_sodar1_last01days.png')
375
376
377 def wind_vectors(pi, si, yyyy_mm, plot_type='latest'):
378     print 'billymitchell_arrows_plot ...'
379
380     #
381
382     prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
383     yyyy_mm_str = this_month.strftime('%Y_%m')
384
385     ################################
386     # [2a] load primary data file
387     ################################
388
389     ncFile1='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+prev_month.strftime('%Y_%m')+'.nc'
390     ncFile2='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+this_month.strftime('%Y_%m')+'.nc'
391
392     have_ncFile1 = os.path.exists(ncFile1)
393     have_ncFile2 = os.path.exists(ncFile2)
394
395     print ' ... loading data for graph from ...'
396     print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
397     print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
398
399     # open netcdf data
400     if have_ncFile1 and have_ncFile2:
401         nc = pycdf.CDFMF((ncFile1, ncFile2))
402     elif not have_ncFile1 and have_ncFile2:
403         nc = pycdf.CDFMF((ncFile2,))
404     elif have_ncFile1 and not have_ncFile2:
405         nc = pycdf.CDFMF((ncFile1,))
406     else:
407         print ' ... both files do not exist -- NO DATA LOADED'
408         exit()
409
410     # ncvars = nc.variables()
411     # print ncvars
412     es = nc.var('time')[:]
413     units = nc.var('time').units
414     dt = [procutil.es2dt(e) for e in es]
415     # set timezone info to UTC (since data from level1 should be in UTC!!)
416     dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
417     # return new datetime based on computer local
418     dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
419     dn = date2num(dt)
420
421
422     ################################
423     # [2b] specify variables
424     ################################
425     z = nc.var('z')[:]
426     # convert cm/s to m/s
427     uu = nc.var('u')[:]
428     vv = nc.var('v')[:]
429     ww = nc.var('w')[:]
430     sigw = nc.var('sigw')[:]
431     echo = nc.var('bck')[:]
432
433     nc.close()
434
435     # retain original dt for addnan
436     dto = dt
437
438     # last dt in data for labels
439     dtu = dt[-1]
440     dtl = dt_local[-1]
441
442     diff = abs(dtu - dtl)
443     if diff.days>0:
444         last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')'
445     else:
446         last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \
447                       + dtl.strftime(" on %b %d, %Y")
448
449     #######################################
450     # Plot setup
451     #######################################
452
453     fig = figure(figsize=(10, 10))
454     fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
455
456     ax = fig.add_subplot(1,1,1)
457     axs = [ax]
458
459     # range for horizontal wind plots
460     # cmin, cmax = (0., 20.)
461     # print "%s : %g %g" % ('uv wind', cmin, cmax)
462     # use masked array to hide NaN's on plot
463     (dt, uu) = procutil.addnan(dto, uu)
464     dn = date2num(dt)
465     um = numpy.ma.masked_where(numpy.isnan(uu), uu)
466
467     (dt, vv) = procutil.addnan(dto, vv)
468     vm = numpy.ma.masked_where(numpy.isnan(vv), vv)
469     wspd = numpy.sqrt(um*um + vm*vm)
470
471     X,Y = numpy.meshgrid(dn, z)
472
473     q1 = ax.quiver(X, Y, um.T, vm.T, wspd.T, units='inches', scale=40)
474     qk = ax.quiverkey(q1, 0.1, 0.8, 20, r'20 m s-1')
475    
476     ax.set_ylabel('Height (m)')
477
478     # setup colorbar axes instance.
479     l,b,w,h = ax.get_position().bounds
480     cax = fig.add_axes([l+0.02, b+h-0.06, 0.25*w, 0.03])
481
482     cb = colorbar(q1, cax=cax, orientation='horizontal') # draw colorbar
483     cb.set_label('Wind Velocity (m s-1)')
484
485     # lost control of colorbar ... ???
486     #
487     # cb.ax.xaxis.set_label_position('top')
488     # cb.ax.set_xticks([0.0, 0.5, 1.0])
489     # xtl = numpy.round(numpy.linspace(cmin, cmax, 11), decimals=0)
490     # cb.ax.set_xticklabels([xtl[0], xtl[5], xtl[10]])
491
492     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
493     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
494     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
495     ax.set_xticklabels([])
496
497     # this only moves the label not the tick labels
498     ax.xaxis.set_label_position('bottom')
499     ax.set_xlabel('BILLY MITCHELL Wind Profile -- ' + yyyy_mm_str)
500
501     # right-hand side scale
502     ax2 = twinx(ax)
503     ax2.yaxis.tick_right()
504     # convert (lhs) meters to (rhs) feet
505     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
506     ax2.set_ylim(feet)
507     ax2.set_ylabel('Height (ft)')
508
509    
510
511     #######################################
512     # Last 1 day (24hrs)
513     #######################################
514     if plot_type=='latest':
515         print ' ... Last 1 days'
516
517         for idx, ax in enumerate(axs):
518             ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
519             ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
520             ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
521             ax.set_xticklabels([])
522             if idx==0:
523                 ax.set_xlabel('BILLY MITCHELL Wind Profile -- Last 24 hours from ' + last_dt_str)
524             if idx==len(axs)-1:
525                 ax.xaxis.set_major_formatter( DateFormatter('%H') )
526                 ax.set_xlabel('BILLY MITCHELL Wind Profile -- Last 24 hours from ' + last_dt_str)
527
528         savefig('/home/haines/rayleigh/img/billymitchell_wind_last01days.png')
529
530
531 def wind_barbs(pi, si, yyyy_mm, plot_type='latest'):
532     print 'billymitchell_arrows_plot ...'
533
534     #
535
536     prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
537     yyyy_mm_str = this_month.strftime('%Y_%m')
538
539     ################################
540     # [2a] load primary data file
541     ################################
542
543     ncFile1='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+prev_month.strftime('%Y_%m')+'.nc'
544     ncFile2='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+this_month.strftime('%Y_%m')+'.nc'
545
546     have_ncFile1 = os.path.exists(ncFile1)
547     have_ncFile2 = os.path.exists(ncFile2)
548
549     print ' ... loading data for graph from ...'
550     print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
551     print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
552
553     # open netcdf data
554     if have_ncFile1 and have_ncFile2:
555         nc = pycdf.CDFMF((ncFile1, ncFile2))
556     elif not have_ncFile1 and have_ncFile2:
557         nc = pycdf.CDFMF((ncFile2,))
558     elif have_ncFile1 and not have_ncFile2:
559         nc = pycdf.CDFMF((ncFile1,))
560     else:
561         print ' ... both files do not exist -- NO DATA LOADED'
562         exit()
563
564     # ncvars = nc.variables()
565     # print ncvars
566     es = nc.var('time')[:]
567     units = nc.var('time').units
568     dt = [procutil.es2dt(e) for e in es]
569     # set timezone info to UTC (since data from level1 should be in UTC!!)
570     dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
571     # return new datetime based on computer local
572     dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
573     dn = date2num(dt)
574
575
576     ################################
577     # [2b] specify variables
578     ################################
579     z = nc.var('z')[:]
580     # convert cm/s to m/s
581     uu = nc.var('u')[:]
582     vv = nc.var('v')[:]
583     ww = nc.var('w')[:]
584     sigw = nc.var('sigw')[:]
585     echo = nc.var('bck')[:]
586
587     nc.close()
588
589     # retain original dt for addnan
590     dto = dt
591
592     # last dt in data for labels
593     dtu = dt[-1]
594     dtl = dt_local[-1]
595
596     diff = abs(dtu - dtl)
597     if diff.days>0:
598         last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')'
599     else:
600         last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \
601                       + dtl.strftime(" on %b %d, %Y")
602
603     #######################################
604     # Plot setup
605     #######################################
606
607     fig = figure(figsize=(10, 10))
608     fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
609
610     ax = fig.add_subplot(1,1,1)
611     axs = [ax]
612
613     # range for horizontal wind plots
614     # cmin, cmax = (0., 20.)
615     # print "%s : %g %g" % ('uv wind', cmin, cmax)
616     # use masked array to hide NaN's on plot
617     (dt, uu) = procutil.addnan(dto, uu)
618     dn = date2num(dt)
619     um = numpy.ma.masked_where(numpy.isnan(uu), uu)
620
621     (dt, vv) = procutil.addnan(dto, vv)
622     vm = numpy.ma.masked_where(numpy.isnan(vv), vv)
623     wspd = numpy.sqrt(um*um + vm*vm)
624
625     X,Y = numpy.meshgrid(dn, z)
626
627     q1 = ax.barbs(X, Y, um.T, vm.T, wspd.T)
628     # qk = ax.quiverkey(q1, 0.1, 0.8, 20, r'20 m s-1')
629    
630     ax.set_ylabel('Height (m)')
631
632     # setup colorbar axes instance.
633     l,b,w,h = ax.get_position().bounds
634     cax = fig.add_axes([l+0.02, b+h-0.06, 0.25*w, 0.03])
635
636     cb = colorbar(q1, cax=cax, orientation='horizontal') # draw colorbar
637     cb.set_label('Wind Velocity (m s-1)')
638
639     # lost control of colorbar ... ???
640     #
641     # cb.ax.xaxis.set_label_position('top')
642     # cb.ax.set_xticks([0.0, 0.5, 1.0])
643     # xtl = numpy.round(numpy.linspace(cmin, cmax, 11), decimals=0)
644     # cb.ax.set_xticklabels([xtl[0], xtl[5], xtl[10]])
645
646     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
647     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
648     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
649     ax.set_xticklabels([])
650
651     # this only moves the label not the tick labels
652     ax.xaxis.set_label_position('bottom')
653     ax.set_xlabel('BILLY MITCHELL Wind Profile -- ' + yyyy_mm_str)
654
655     # right-hand side scale
656     ax2 = twinx(ax)
657     ax2.yaxis.tick_right()
658     # convert (lhs) meters to (rhs) feet
659     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
660     ax2.set_ylim(feet)
661     ax2.set_ylabel('Height (ft)')
662
663    
664
665     #######################################
666     # Last 1 day (24hrs)
667     #######################################
668     if plot_type=='latest':
669         print ' ... Last 1 days'
670
671         for idx, ax in enumerate(axs):
672             ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
673             ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
674             ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
675             ax.set_xticklabels([])
676             if idx==0:
677                 ax.set_xlabel('BILLY MITCHELL Wind Profile -- Last 24 hours from ' + last_dt_str)
678             if idx==len(axs)-1:
679                 ax.xaxis.set_major_formatter( DateFormatter('%H') )
680                 ax.set_xlabel('BILLY MITCHELL Wind Profile -- Last 24 hours from ' + last_dt_str)
681
682         savefig('/home/haines/rayleigh/img/billymitchell_windbarbs_last01days.png')
683
Note: See TracBrowser for help on using the browser.