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

root/proc2plot/trunk/proc2plot/stones_met_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-10-03 11:35:27 haines>
3 """stones_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 'stones_met_plot2 ...'
20 prev_month, this_month, next_month = procutil.find_months(procutil.this_month())
21 #ncFile1='/seacoos/data/nccoos/level1/stones/met/stones_met_2008_01.nc'
22 #ncFile2='/seacoos/data/nccoos/level1/stones/met/stones_met_2008_02.nc'
23
24 ncFile1='/seacoos/data/nccoos/level1/stones/met/stones_met_'+prev_month.strftime('%Y_%m')+'.nc'
25 ncFile2='/seacoos/data/nccoos/level1/stones/met/stones_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 u = nc.var('wspd')[:]
58 v = nc.var('wspd')[:]
59
60 i = 0
61 for val in ws: 
62         u[i] = -1.*procutil.wind_vector2u(ws[i],wd[i])
63         v[i] = -1.*procutil.wind_vector2v(ws[i],wd[i])
64         i += 1
65 nc.close()
66
67
68 # last dt in data for labels
69 dt1 = dt[-1]
70 dt2 = dt_local[-1]
71
72 diff = abs(dt1 - dt2)
73 if diff.days>0:
74     last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
75 else:
76     last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
77               + dt2.strftime(" on %b %d, %Y")
78
79 fig = figure(figsize=(10, 8))
80 fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
81
82 #######################################
83 # Last 30 days
84 #######################################
85 print ' ... Last 30 days'
86
87 ax = fig.add_subplot(4,1,1)
88 axs = [ax]
89
90 # use masked array to hide NaN's on plot
91 wd = numpy.ma.masked_where(numpy.isnan(wd), wd)
92
93 # ax.plot returns a list of lines, so unpack tuple
94 l1, = ax.plot_date(dt, wd, fmt='b-')
95 l1.set_label('Wind from Direction')
96
97 ax.set_ylabel('Dir (true N)')
98 ax.set_ylim(0.,360.)
99 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
100 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
101 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
102 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
103 ax.set_xticklabels([])
104
105 # this only moves the label not the tick labels
106 ax.xaxis.set_label_position('top')
107 ax.set_xlabel('STONES BAY WIND -- Last 30 days from ' + last_dt_str)
108
109 # legend
110 ls1 = l1.get_label()
111 leg = ax.legend((l1,), (ls1,), loc='upper left')
112 ltext  = leg.get_texts()  # all the text.Text instance in the legend
113 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
114 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
115 frame.set_facecolor('0.80')      # set the frame face color to light gray
116 frame.set_alpha(0.5)             # set alpha low to see through
117 setp(ltext, fontsize='small')    # the legend text fontsize
118 setp(llines, linewidth=1.5)      # the legend linewidth
119 # leg.draw_frame(False)           # don't draw the legend frame
120
121 #######################################
122 #
123 ax = fig.add_subplot(4,1,2)
124 axs.append(ax)
125
126 # use masked array to hide NaN's on plot
127 ws = numpy.ma.masked_where(numpy.isnan(ws), ws)
128
129
130 # ax.plot returns a list of lines, so unpack tuple
131 l1, = ax.plot_date(dt, ws, fmt='b-')
132 l1.set_label('Wind Speed')
133
134
135 ax.set_ylabel('Speed (m/s)')
136 ax.set_ylim(0.,20.)
137
138 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
139 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
140 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
141 ax.set_xticklabels([])
142
143 # right-hand side scale
144 ax2 = twinx(ax)
145 ax2.yaxis.tick_right()
146 # convert (lhs) m/s to (rhs) knots
147 k = [procutil.meters_sec2knots(val) for val in ax.get_ylim()]
148 ax2.set_ylim(k)
149 ax2.set_ylabel('Speed (knots)')
150
151 # legend
152 ls1 = l1.get_label()
153 leg = ax.legend((l1,), (ls1,), loc='upper left')
154 ltext  = leg.get_texts()  # all the text.Text instance in the legend
155 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
156 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
157 frame.set_facecolor('0.80')      # set the frame face color to light gray
158 frame.set_alpha(0.5)             # set alpha low to see through
159 setp(ltext, fontsize='small')    # the legend text fontsize
160 setp(llines, linewidth=1.5)      # the legend linewidth
161 # leg.draw_frame(False)           # don't draw the legend frame
162
163 #######################################
164 #
165 ax = fig.add_subplot(4,1,3)
166 axs.append(ax)
167
168 # use masked array to hide NaN's on plot
169 # u = numpy.ma.masked_where(numpy.isnan(u), u)
170 # v = numpy.ma.masked_where(numpy.isnan(v), v)
171
172 dt0 = numpy.zeros(len(dt))
173
174 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
175 ax.axhline(y=0, color='gray')
176 # make stick plot (set all head parameters to zero)
177 # scale to y-axis using units='height' of plot with scale = 40 (m/s range) per height with min=-20 and max=20
178 q1 = ax.quiver(date2num(dt), dt0, u, v, units='height', scale=40, headwidth=0, headlength=0, headaxislength=0)
179 qk = ax.quiverkey(q1, 0.1, 0.8, 10, r'10 m s-1')
180
181 ax.set_ylim(-20.,20.)
182 ax.set_ylabel('Speed (m/s)')
183
184 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
185 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
186 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
187 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
188
189 # right-hand side scale
190 ax2 = twinx(ax)
191 ax2.yaxis.tick_right()
192 # convert (lhs) m/s to (rhs) knots
193 k = [(val * 1.94384449) for val in ax.get_ylim()]
194 ax2.set_ylim(k)
195 ax2.set_ylabel('Speed (knots)')
196
197 ax.set_xlabel('STONES BAY WIND -- Last 30 days from ' + last_dt_str)
198
199 # save figure
200 savefig('/home/haines/rayleigh/img/stones_met_last30days.png')
201
202 #######################################
203 # Last 7 days
204 #######################################
205
206 print ' ... Last 7 days'
207 ax = axs[0]
208 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
209 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
210 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
211 ax.set_xticklabels([])
212 ax.set_xlabel('STONES BAY WIND -- Last 7 days from ' + last_dt_str)
213
214 ax = axs[1]
215 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
216 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
217 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
218 ax.set_xticklabels([])
219
220 ax = axs[2]
221 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
222 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
223 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
224 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
225 ax.set_xlabel('STONES BAY WIND  -- Last 7 days from ' + last_dt_str)
226
227 savefig('/home/haines/rayleigh/img/stones_met_last07days.png')
228
229 #######################################
230 # Last 1 day (24hrs)
231 #######################################
232
233 print ' ... Last 1 days'
234
235 ax = axs[0]
236 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
237 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
238 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
239 ax.set_xticklabels([])
240 ax.set_xlabel('STONES BAY WIND -- Last 24 hours from ' + last_dt_str)
241
242 ax = axs[1]
243 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
244 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
245 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
246 ax.set_xticklabels([])
247
248 ax = axs[2]
249 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
250 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
251 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
252 ax.xaxis.set_major_formatter( DateFormatter('%H') )
253 ax.set_xlabel('STONES BAY WIND -- Last 24 hours from ' + last_dt_str)
254
255 savefig('/home/haines/rayleigh/img/stones_met_last01days.png')
256
Note: See TracBrowser for help on using the browser.