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

root/proc2plot/trunk/proc2plot/spin/spin_df_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: <2009-02-18 15:45:58 haines>
3 """spin_df_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 'dukeforest_sodar_plot_month ...'
22
23 proc_dir = '/seacoos/data/nccoos/level1/dukeforest/sodar/'
24 fns = glob.glob((os.path.join(proc_dir, '*.nc')))
25 fns.sort()
26
27 fns = fns[0:2]
28
29 for fn in fns:
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     # last dt in data for labels
59     dt1 = dt[-1]
60     dt2 = dt_local[-1]
61    
62     diff = abs(dt1 - dt2)
63     if diff.days>0:
64         last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
65     else:
66         last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
67                       + dt2.strftime(" on %b %d, %Y")
68
69     fig = figure(figsize=(10, 10))
70     fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
71
72 #######################################
73 # Plot month
74 #######################################
75
76     ax = fig.add_subplot(5,1,1)
77     axs = [ax]
78
79     # range for horizontal wind plots
80     cmin, cmax = (-20., 20.)
81     # print "%s : %g %g" % ('uv wind', cmin, cmax)
82 # use masked array to hide NaN's on plot
83     um = numpy.ma.masked_where(numpy.isnan(uu), uu)
84     pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax)
85     pc.set_label('True Eastward Wind (m s-1)')
86     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
87     ax.set_ylabel('Height (m)')
88     # ax.set_ylim(-26.,2.)
89
90 # setup colorbar axes instance.
91     l,b,w,h = ax.get_position()
92     cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03])
93
94     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
95     cb.set_label('Wind Velocity (m s-1)')
96     cb.ax.xaxis.set_label_position('top')
97     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
98     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
99     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
100     #cb.ax.set_xticklabels([-16, -8, 0, 8, 16])
101
102 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
103     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
104     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
105     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
106     ax.set_xticklabels([])
107
108 # this only moves the label not the tick labels
109     ax.xaxis.set_label_position('top')
110     ax.set_xlabel('DUKE FOREST SODAR -- ' + yyyy_mm)
111
112 # right-hand side scale
113     ax2 = twinx(ax)
114     ax2.yaxis.tick_right()
115 # convert (lhs) meters to (rhs) feet
116     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
117     ax2.set_ylim(feet)
118     ax2.set_ylabel('Height (ft)')
119
120 #######################################
121 #
122     ax = fig.add_subplot(5,1,2)
123     axs.append(ax)
124
125 # use masked array to hide NaN's on plot
126     vm = numpy.ma.masked_where(numpy.isnan(vv), vv)
127     pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax)
128     pc.set_label('True Northward Wind (m s-1)')
129     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
130     ax.set_ylabel('Height (m)')
131     # ax.set_ylim(-26.,2)
132
133     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
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.xaxis.set_major_formatter( DateFormatter('%m/%d') )
138     ax.set_xticklabels([])
139
140     # right-hand side scale
141     ax2 = twinx(ax)
142     ax2.yaxis.tick_right()
143     # convert (lhs) meters to (rhs) feet
144     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
145     ax2.set_ylim(feet)
146     ax2.set_ylabel('Height (ft)')
147    
148     #######################################
149     #
150     ax = fig.add_subplot(5,1,3)
151     axs.append(ax)
152    
153     # range for horizontal wind plots
154     cmin, cmax = (-5., 5.)
155     # print "%s : %g %g" % ('w wind', cmin, cmax)
156
157     # use masked array to hide NaN's on plot
158     wm = numpy.ma.masked_where(numpy.isnan(ww), ww)
159     pc = ax.pcolor(dn, z, wm.T, vmin=cmin, vmax=cmax)
160     # pc.set_label('Upward Wind (m s-1)')
161     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
162     ax.set_ylabel('Height (m)')
163     # ax.set_ylim(-26.,2)
164    
165     # setup colorbar axes instance.
166     l,b,w,h = ax.get_position()
167     cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03])
168
169     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
170     cb.set_label('Upward Wind Velocity (m s-1)')
171     cb.ax.xaxis.set_label_position('top')
172     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
173     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
174     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
175
176     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
177     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
178     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
179     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
180     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
181     ax.set_xticklabels([])
182    
183     # right-hand side scale
184     ax2 = twinx(ax)
185     ax2.yaxis.tick_right()
186     # convert (lhs) meters to (rhs) feet
187     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
188     ax2.set_ylim(feet)
189     ax2.set_ylabel('Height (ft)')
190    
191     #######################################
192     #
193     ax = fig.add_subplot(5,1,4)
194     axs.append(ax)
195    
196     cmin, cmax = (0., 1000.)
197     # print "%s : %g %g" % ('echo', cmin, cmax)
198
199     # use masked array to hide NaN's on plot
200     em = numpy.ma.masked_where(numpy.isnan(echo), echo)
201     pc = ax.pcolor(dn, z, em.T, vmin=cmin, vmax=cmax)
202     # pc.set_label('Echo Strength ()')
203     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
204     ax.set_ylabel('Height (m)')
205     # ax.set_ylim(-26.,2)
206
207     # setup colorbar axes instance.
208     l,b,w,h = ax.get_position()
209     cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03])
210
211     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
212     cb.set_label('Echo Strength')
213     cb.ax.xaxis.set_label_position('top')
214     cb.ax.set_xticks([0.0, 1.0])
215     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
216     cb.ax.set_xticklabels([str(cmin), str(cmax)])
217    
218     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
219     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
220     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
221     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
222     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
223     ax.set_xticklabels([])
224    
225     # right-hand side scale
226     ax2 = twinx(ax)
227     ax2.yaxis.tick_right()
228     # convert (lhs) meters to (rhs) feet
229     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
230     ax2.set_ylim(feet)
231     ax2.set_ylabel('Height (ft)')
232    
233     #######################################
234     #
235     ax = fig.add_subplot(5,1,5)
236     axs.append(ax)
237        
238     # ax.plot returns a list of lines, so unpack tuple
239     l1, = ax.plot_date(dt, n1, fmt='b-')
240     l1.set_label('n1 each beam')
241     l2, = ax.plot_date(dt, n2, fmt='c-')
242     l2.set_label('n2')   
243     l3, = ax.plot_date(dt, n3, fmt='g-')
244     l3.set_label('n3')
245    
246     ax.set_ylabel('Ambient Noise')
247     ax.set_ylim(20,80)
248     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
249     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
250     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
251     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
252    
253     ax.set_xlabel('DUKE FOREST SODAR -- ' + yyyy_mm)
254
255     # legend
256     ls1 = l1.get_label()
257     ls2 = l2.get_label()
258     ls3 = l3.get_label()
259     leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), loc='upper left')
260     ltext  = leg.get_texts()  # all the text.Text instance in the legend
261     llines = leg.get_lines()  # all the lines.Line2D instance in the legend
262     frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
263     frame.set_facecolor('0.80')      # set the frame face color to light gray
264     frame.set_alpha(0.5)             # set alpha low to see through
265     setp(ltext, fontsize='small')    # the legend text fontsize
266     setp(llines, linewidth=1.5)      # the legend linewidth
267     # leg.draw_frame(False)           # don't draw the legend frame
268    
269     # save figure
270    
271     ofn = '/home/haines/rayleigh/img/dukeforest/dukeforest_sodar1_'+yyyy_mm+'.png'
272     print '... ... write: %s' % (ofn,)
273     savefig(ofn)
274
Note: See TracBrowser for help on using the browser.