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

root/proc2plot/tags/proc2plot-1.0/spin/spin_ims_sodar_plot_month.py

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

import first verison proc2plot

Line 
1 #!/usr/bin/env python
2 # Last modified:  Time-stamp: <2008-12-09 16:47:29 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_sodar1_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 for fn in fns[-1:-3]:
28     m=re.search('\d{4}_\d{2}', fn)
29     yyyy_mm = m.group()
30     prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
31    
32     # load data
33     print ' ... ... read: ' + fn
34     nc = pycdf.CDFMF((fn,))
35     ncvars = nc.variables()
36     # print ncvars
37     es = nc.var('time')[:]
38     units = nc.var('time').units
39     dt = [procutil.es2dt(e) for e in es]
40     # set timezone info to UTC (since data from level1 should be in UTC!!)
41     dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
42     # return new datetime based on computer local
43     dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
44     dn = date2num(dt)
45     z = nc.var('z')[:]/100. # convert cm/s to m/s
46     uu = nc.var('u')[:]/100.
47     vv = nc.var('v')[:]/100.
48     ww = nc.var('w')[:]/100.
49     echo = nc.var('echo')[:]
50     nc.close()
51
52     # last dt in data for labels
53     dt1 = dt[-1]
54     dt2 = dt_local[-1]
55    
56     diff = abs(dt1 - dt2)
57     if diff.days>0:
58         last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
59     else:
60         last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
61                       + dt2.strftime(" on %b %d, %Y")
62
63     fig = figure(figsize=(10, 8))
64     fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
65
66 #######################################
67 # Plot month
68 #######################################
69
70     ax = fig.add_subplot(4,1,1)
71     axs = [ax]
72
73     # range for horizontal wind plots
74     cmin, cmax = (-20., 20.)
75     # print "%s : %g %g" % ('uv wind', cmin, cmax)
76 # use masked array to hide NaN's on plot
77     um = numpy.ma.masked_where(numpy.isnan(uu), uu)
78     pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax)
79     pc.set_label('True Eastward Wind (m s-1)')
80     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
81     ax.set_ylabel('Height (m)')
82     # ax.set_ylim(-26.,2.)
83
84 # setup colorbar axes instance.
85     l,b,w,h = ax.get_position()
86     cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03])
87
88     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
89     cb.set_label('Wind Velocity (m s-1)')
90     cb.ax.xaxis.set_label_position('top')
91     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
92     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
93     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
94     #cb.ax.set_xticklabels([-16, -8, 0, 8, 16])
95
96 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
97     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
98     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
99     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
100     ax.set_xticklabels([])
101
102 # this only moves the label not the tick labels
103     ax.xaxis.set_label_position('top')
104     ax.set_xlabel('IMS SODAR -- ' + yyyy_mm)
105
106 # right-hand side scale
107     ax2 = twinx(ax)
108     ax2.yaxis.tick_right()
109 # convert (lhs) meters to (rhs) feet
110     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
111     ax2.set_ylim(feet)
112     ax2.set_ylabel('Height (ft)')
113
114 #######################################
115 #
116     ax = fig.add_subplot(4,1,2)
117     axs.append(ax)
118
119 # use masked array to hide NaN's on plot
120     vm = numpy.ma.masked_where(numpy.isnan(vv), vv)
121     pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax)
122     pc.set_label('True Northward Wind (m s-1)')
123     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
124     ax.set_ylabel('Height (m)')
125     # ax.set_ylim(-26.,2)
126
127     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
128     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
129     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
130     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
131     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
132     ax.set_xticklabels([])
133
134     # right-hand side scale
135     ax2 = twinx(ax)
136     ax2.yaxis.tick_right()
137     # convert (lhs) meters to (rhs) feet
138     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
139     ax2.set_ylim(feet)
140     ax2.set_ylabel('Height (ft)')
141    
142     #######################################
143     #
144     ax = fig.add_subplot(4,1,3)
145     axs.append(ax)
146    
147     # range for horizontal wind plots
148     cmin, cmax = (-5., 5.)
149     # print "%s : %g %g" % ('w wind', cmin, cmax)
150
151     # use masked array to hide NaN's on plot
152     wm = numpy.ma.masked_where(numpy.isnan(ww), ww)
153     pc = ax.pcolor(dn, z, wm.T, vmin=cmin, vmax=cmax)
154     # pc.set_label('Upward Wind (m s-1)')
155     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
156     ax.set_ylabel('Height (m)')
157     # ax.set_ylim(-26.,2)
158    
159     # setup colorbar axes instance.
160     l,b,w,h = ax.get_position()
161     cax = fig.add_axes([l+0.04, b+h-0.04, 0.25*w, 0.03])
162
163     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
164     cb.set_label('Upward Wind Velocity (m s-1)')
165     cb.ax.xaxis.set_label_position('top')
166     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
167     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
168     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
169
170     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
171     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
172     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
173     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
174     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
175     ax.set_xticklabels([])
176    
177     # right-hand side scale
178     ax2 = twinx(ax)
179     ax2.yaxis.tick_right()
180     # convert (lhs) meters to (rhs) feet
181     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
182     ax2.set_ylim(feet)
183     ax2.set_ylabel('Height (ft)')
184    
185     #######################################
186     #
187     ax = fig.add_subplot(4,1,4)
188     axs.append(ax)
189    
190     cmin, cmax = (0., 1000.)
191     # print "%s : %g %g" % ('echo', cmin, cmax)
192
193     # use masked array to hide NaN's on plot
194     em = numpy.ma.masked_where(numpy.isnan(echo), echo)
195     pc = ax.pcolor(dn, z, em.T, vmin=cmin, vmax=cmax)
196     # pc.set_label('Echo Strength ()')
197     ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
198     ax.set_ylabel('Height (m)')
199     # ax.set_ylim(-26.,2)
200
201     # setup colorbar axes instance.
202     l,b,w,h = ax.get_position()
203     cax = fig.add_axes([l+0.04, b+h-0.04, 0.25*w, 0.03])
204
205     cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
206     cb.set_label('Echo Strength')
207     cb.ax.xaxis.set_label_position('top')
208     cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
209     xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
210     cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
211    
212     # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
213     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
214     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
215     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
216     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
217    
218     ax.set_xlabel('IMS SODAR -- ' + yyyy_mm)
219    
220     # right-hand side scale
221     ax2 = twinx(ax)
222     ax2.yaxis.tick_right()
223     # convert (lhs) meters to (rhs) feet
224     feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
225     ax2.set_ylim(feet)
226     ax2.set_ylabel('Height (ft)')
227    
228    
229     # save figure
230    
231     ofn = '/home/haines/rayleigh/img/ims/ims_sodar1_'+yyyy_mm+'.png'
232     print '... ... write: %s' % (ofn,)
233     savefig(ofn)
234
Note: See TracBrowser for help on using the browser.