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

root/proc2plot/trunk/proc2plot/scratch/test_quiver_plot.py

Revision 329 (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-09-17 15:52:54 haines>
3 """morgan_met_plot"""
4
5 import os, sys
6 import datetime, time, dateutil.tz
7 import pycdf
8 import numpy
9
10 sys.path.append('/home/haines/nccoos/raw2proc')
11 del(sys)
12
13 os.environ["MPLCONFIGDIR"]="/home/haines/.matplotlib/"
14
15 from pylab import figure, twinx, savefig, setp, getp, cm, colorbar
16 from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date
17 import procutil
18
19 print 'morgan_met_plot ...'
20 prev_month, this_month, next_month = procutil.find_months(procutil.this_month())
21 #ncFile1='/seacoos/data/nccoos/level1/morgan/met/morgan_met_2008_01.nc'
22 #ncFile2='/seacoos/data/nccoos/level1/morgan/met/morgan_met_2008_02.nc'
23
24 ncFile1='/seacoos/data/nccoos/level1/morgan/met/morgan_met_'+prev_month.strftime('%Y_%m')+'.nc'
25 ncFile2='/seacoos/data/nccoos/level1/morgan/met/morgan_met_'+this_month.strftime('%Y_%m')+'.nc'
26
27 # load data
28 have_ncFile1 = os.path.exists(ncFile1)
29 have_ncFile2 = os.path.exists(ncFile2)
30
31 print ' ... loading data for graph from ...'
32 print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
33 print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
34
35 if have_ncFile1 and have_ncFile2:
36         nc = pycdf.CDFMF((ncFile1, ncFile2))
37 elif not have_ncFile1 and have_ncFile2:
38         nc = pycdf.CDFMF((ncFile2,))
39 elif have_ncFile1 and not have_ncFile2:
40         nc = pycdf.CDFMF((ncFile1,))
41 else:
42         print ' ... both files do not exist -- NO DATA LOADED'
43         exit()
44                                                    
45 ncvars = nc.variables()
46 #print ncvars
47 es = nc.var('time')[:]
48 units = nc.var('time').units
49 dt = [procutil.es2dt(e) for e in es]
50 # set timezone info to UTC (since data from level1 should be in UTC!!)
51 dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
52 # return new datetime based on computer local
53 dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
54 dn = date2num(dt)
55 ws = nc.var('wspd')[:]
56 wd = nc.var('wdir')[:]
57 cdir = nc.var('cdir')[:]
58 u = nc.var('u')[:]
59 v = nc.var('v')[:]
60
61 # i = 0
62 # for val in ws:       
63 #       u[i] = procutil.wind_vector2u(ws[i],wd[i])
64 #       v[i] = procutil.wind_vector2v(ws[i],wd[i])
65 #        i += 1
66 # nc.close()
67
68
69 # last dt in data for labels
70 dt1 = dt[-1]
71 dt2 = dt_local[-1]
72
73 diff = abs(dt1 - dt2)
74 if diff.days>0:
75     last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
76 else:
77     last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
78               + dt2.strftime(" on %b %d, %Y")
79
80 fig = figure(figsize=(10, 8))
81 fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
82
83 #######################################
84 # Last 30 days
85 #######################################
86 print ' ... Last 30 days'
87
88 ax = fig.add_subplot(4,1,1)
89 axs = [ax]
90
91 # use masked array to hide NaN's on plot
92 v = numpy.ma.masked_where(numpy.isnan(v), v)
93
94 # ax.plot returns a list of lines, so unpack tuple
95 l1, = ax.plot_date(dt, v, fmt='b-')
96 l1.set_label('Northward wind component')
97
98 ax.set_ylabel('Speed N (m/s)')
99 ax.set_ylim(-20.,20.)
100
101 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
102 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
103 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
104 ax.set_xticklabels([])
105
106 # right-hand side scale
107 ax2 = twinx(ax)
108 ax2.yaxis.tick_right()
109 # convert (lhs) m/s to (rhs) knots
110 k = [procutil.meters_sec2knots(val) for val in ax.get_ylim()]
111 ax2.set_ylim(k)
112 ax2.set_ylabel('Speed N (knots)')
113
114 # legend
115 ls1 = l1.get_label()
116 leg = ax.legend((l1,), (ls1,), loc='upper left')
117 ltext  = leg.get_texts()  # all the text.Text instance in the legend
118 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
119 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
120 frame.set_facecolor('0.80')      # set the frame face color to light gray
121 frame.set_alpha(0.5)             # set alpha low to see through
122 setp(ltext, fontsize='small')    # the legend text fontsize
123 setp(llines, linewidth=1.5)      # the legend linewidth
124 # leg.draw_frame(False)           # don't draw the legend frame
125
126 #######################################
127 #
128 ax = fig.add_subplot(4,1,2)
129 axs.append(ax)
130
131 # use masked array to hide NaN's on plot
132 u = numpy.ma.masked_where(numpy.isnan(u), u)
133
134 # ax.plot returns a list of lines, so unpack tuple
135 l1, = ax.plot_date(dt, u, fmt='b-')
136 l1.set_label('Eastward wind component')
137
138 ax.set_ylabel('Speed E (m/s)')
139 ax.set_ylim(-20.,20.)
140
141 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-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.set_xticklabels([])
145
146 # right-hand side scale
147 ax2 = twinx(ax)
148 ax2.yaxis.tick_right()
149 # convert (lhs) m/s to (rhs) knots
150 k = [(val * 1.94384449) for val in ax.get_ylim()]
151 ax2.set_ylim(k)
152 ax2.set_ylabel('Speed E (knots)')
153
154 # legend
155 ls1 = l1.get_label()
156 leg = ax.legend((l1,), (ls1,), loc='upper left')
157 ltext  = leg.get_texts()  # all the text.Text instance in the legend
158 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
159 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
160 frame.set_facecolor('0.80')      # set the frame face color to light gray
161 frame.set_alpha(0.5)             # set alpha low to see through
162 setp(ltext, fontsize='small')    # the legend text fontsize
163 setp(llines, linewidth=1.5)      # the legend linewidth
164 # leg.draw_frame(False)           # don't draw the legend frame
165 #######################################
166 #
167 ax = fig.add_subplot(4,1,3)
168 axs.append(ax)
169
170 # use masked array to hide NaN's on plot
171 # u = numpy.ma.masked_where(numpy.isnan(u), u)
172 # v = numpy.ma.masked_where(numpy.isnan(v), v)
173
174 dt0 = numpy.zeros(len(dt))
175
176 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
177 ax.axhline(y=0, color='gray')
178 # make stick plot (set all head parameters to zero)
179 # scale to y-axis using units='height' of plot with scale = 40 (m/s range) per height with min=-20 and max=20
180 q1 = ax.quiver(date2num(dt), dt0, u, v, units='height', scale=40, headwidth=0, headlength=0, headaxislength=0)
181 qk = ax.quiverkey(q1, 0.1, 0.8, 10, r'10 m s-1')
182
183 ax.set_ylim(-20.,20.)
184 ax.set_ylabel('Speed (m/s)')
185
186 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-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
191 # right-hand side scale
192 ax2 = twinx(ax)
193 ax2.yaxis.tick_right()
194 # convert (lhs) m/s to (rhs) knots
195 k = [(val * 1.94384449) for val in ax.get_ylim()]
196 ax2.set_ylim(k)
197 ax2.set_ylabel('Speed (knots)')
198
199 ax.set_xlabel('Morgan Bay Wind -- Last 30 days from ' + last_dt_str)
200
201
202 # save figure
203 savefig('/home/haines/rayleigh/test_met_last30days.png')
204
205 #######################################
206 # Last 7 days
207 #######################################
208
209 print ' ... Last 7 days'
210 ax = axs[0]
211 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
212 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
213 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
214 ax.set_xticklabels([])
215 ax.set_xlabel('Morgan Bay Wind -- Last 7 days from ' + last_dt_str)
216
217 ax = axs[1]
218 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
219 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
220 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
221 ax.set_xticklabels([])
222
223 ax = axs[2]
224 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
225 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
226 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
227 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
228 ax.set_xlabel('Morgan Bay Wind -- Last 7 days from ' + last_dt_str)
229
230 savefig('/home/haines/rayleigh/test_met_last07days.png')
231
232 #######################################
233 # Last 1 day (24hrs)
234 #######################################
235
236 print ' ... Last 1 days'
237
238 ax = axs[0]
239 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
240 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
241 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
242 ax.set_xticklabels([])
243 ax.set_xlabel('Morgan Bay Wind -- Last 24 hours from ' + last_dt_str)
244
245 ax = axs[1]
246 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
247 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
248 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
249 ax.set_xticklabels([])
250
251 ax = axs[2]
252 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
253 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
254 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
255 ax.xaxis.set_major_formatter( DateFormatter('%H') )
256 ax.set_xlabel('Morgan Bay Wind -- Last 24 hours from ' + last_dt_str)
257
258 savefig('/home/haines/rayleigh/test_met_last01days.png')
259
Note: See TracBrowser for help on using the browser.