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

root/proc2plot/trunk/proc2plot/scratch/bogue_windwaves_plot.py

Revision 455 (checked in by haines, 13 years ago)

Fix SVN locks and update code.

  • Property svn:executable set to *
Line 
1 #!/usr/bin/env /opt/env/haines/dataproc/bin/python
2 # Last modified:  Time-stamp: <2010-08-12 15:18:36 haines>
3 """bogue_windwaves_plot"""
4
5 import os, sys
6 import datetime, time, dateutil.tz
7 import pycdf
8 import numpy
9
10 sys.path.append('/opt/env/haines/dataproc/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 'bogue_windwaves_plot ...'
20 prev_month, this_month, next_month = procutil.find_months(procutil.this_month())
21 # ncFile1='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_2008_01.nc'
22 # ncFile2='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_2008_02.nc'
23
24 ncFile1='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_'+prev_month.strftime('%Y_%m')+'.nc'
25 ncFile2='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_'+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 # load data
36 if have_ncFile1 and have_ncFile2:
37     nc = pycdf.CDFMF((ncFile1, ncFile2))
38 elif not have_ncFile1 and have_ncFile2:
39     nc = pycdf.CDFMF((ncFile2,))
40 elif have_ncFile1 and not have_ncFile2:
41     nc = pycdf.CDFMF((ncFile1,))
42 else:
43     print ' ... both files do not exist -- NO DATA LOADED'
44     exit()
45
46 ncvars = nc.variables()
47 # print ncvars
48 es = nc.var('time')[:]
49 units = nc.var('time').units
50 dt = [procutil.es2dt(e) for e in es]
51 # set timezone info to UTC (since data from level1 should be in UTC!!)
52 dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
53 # return new datetime based on computer local
54 dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
55 dn = date2num(dt)
56 Hsw = nc.var('Hs_wind')[:]
57 Tpw = nc.var('Tp_wind')[:]
58 Tmw = nc.var('Tm_wind')[:]
59 #Hmax = nc.var('Hmax')[:]
60 Dpw = nc.var('Dp_wind')[:]
61 Dmw = nc.var('Dm_wind')[:]
62 nc.close()
63
64 # ancillary data to plot
65 ncFile1='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+prev_month.strftime('%Y_%m')+'.nc'
66 ncFile2='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+this_month.strftime('%Y_%m')+'.nc'
67 print ' ... loading ancillary data for graph from ...'
68 print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
69 print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
70 # load data
71 if have_ncFile1 and have_ncFile2:
72     nc = pycdf.CDFMF((ncFile1, ncFile2))
73 elif not have_ncFile1 and have_ncFile2:
74     nc = pycdf.CDFMF((ncFile2,))
75 elif have_ncFile1 and not have_ncFile2:
76     nc = pycdf.CDFMF((ncFile1,))
77 else:
78     print ' ... both files do not exist -- NO ANCILLARY DATA LOADED'
79     exit()
80
81 ncvars = nc.variables()
82 # print ncvars
83 es = nc.var('time')[:]
84 units = nc.var('time').units
85 dt_anc = [procutil.es2dt(e) for e in es]
86 # set timezone info to UTC (since data from level1 should be in UTC!!)
87 dt_anc = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt_anc]
88 # return new datetime based on computer local
89 dt_anc_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt_anc]
90 dn_anc = date2num(dt)
91 wd_anc = nc.var('wd')[:]
92 nc.close
93
94 # range for pcolor plots
95 cmin, cmax = (-0.5, 0.5)
96 # last dt in data for labels
97 dt1 = dt[-1]
98 dt2 = dt_local[-1]
99
100 diff = abs(dt1 - dt2)
101 if diff.days>0:
102     last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
103 else:
104     last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
105               + dt2.strftime(" on %b %d, %Y")
106
107 fig = figure(figsize=(10, 8))
108 fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
109
110 #######################################
111 # Last 30 days
112 #######################################
113 print ' ... Last 30 days'
114
115 ax = fig.add_subplot(4,1,1)
116 axs = [ax]
117
118 # ax.plot returns a list of lines, so unpack tuple
119 (x, y) = procutil.addnan(dt_anc, wd_anc, maxdelta=2./24)
120 l1, = ax.plot_date(x, y, fmt='b-')
121 l1.set_label('Water Depth (m)')
122
123 ax.set_ylabel('Depth (m)')
124 # ax.set_ylim(2.,10.)
125 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
126 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
127 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
128 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
129 ax.set_xticklabels([])
130
131 # this only moves the label not the tick labels
132 ax.xaxis.set_label_position('top')
133 ax.set_xlabel('Bogue WIND WAVES -- Last 30 days from ' + last_dt_str)
134
135 # right-hand side scale
136 ax2 = twinx(ax)
137 ax2.yaxis.tick_right()
138 # convert (lhs) meters to (rhs) feet
139 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
140 ax2.set_ylim(feet)
141 ax2.set_ylabel('Depth (feet)')
142
143 ax2.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
144 ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
145 ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
146 ax2.set_xticklabels([])
147
148 # legend
149 ls1 = l1.get_label()
150 leg = ax.legend((l1,), (ls1,), loc='upper left')
151 ltext  = leg.get_texts()  # all the text.Text instance in the legend
152 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
153 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
154 frame.set_facecolor('0.80')      # set the frame face color to light gray
155 frame.set_alpha(0.5)             # set alpha low to see through
156 setp(ltext, fontsize='small')    # the legend text fontsize
157 setp(llines, linewidth=1.5)      # the legend linewidth
158 # leg.draw_frame(False)           # don't draw the legend frame
159
160 #######################################
161 #
162 ax = fig.add_subplot(4,1,2)
163 axs.append(ax)
164
165 # ax.plot returns a list of lines, so unpack tuple
166 (x, y) = procutil.addnan(dt, Hsw, maxdelta=2./24)
167 l1, = ax.plot_date(x, y, fmt='b-')
168 l1.set_label('Significant Wind Wave Height (Hsw)')
169
170 # (x, y) = procutil.addnan(dt, Hmax, maxdelta=2./24)
171 # l2, = ax.plot_date(x, y, fmt='c-')
172 # l2.set_label('Max Wave Height (Hmax)')
173
174 ax.set_ylabel('WAVE\nHEIGHT (m)')
175 # ax.set_ylim(2.,10.)
176 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
177 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
178 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
179 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
180 ax.set_xticklabels([])
181
182 # right-hand side scale
183 ax2 = twinx(ax)
184 ax2.yaxis.tick_right()
185 # convert (lhs) meters to (rhs) feet
186 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
187 ax2.set_ylim(feet)
188 ax2.set_ylabel('(feet)')
189
190 ax2.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
191 ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
192 ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
193 ax2.set_xticklabels([])
194
195 # legend
196 ls1 = l1.get_label()
197 # ls2 = l2.get_label()
198 # leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
199 leg = ax.legend((l1,), (ls1,), loc='upper left')
200 ltext  = leg.get_texts()  # all the text.Text instance in the legend
201 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
202 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
203 frame.set_facecolor('0.80')      # set the frame face color to light gray
204 frame.set_alpha(0.5)             # set alpha low to see through
205 setp(ltext, fontsize='small')    # the legend text fontsize
206 setp(llines, linewidth=1.5)      # the legend linewidth
207 # leg.draw_frame(False)           # don't draw the legend frame
208
209 #######################################
210 #
211 ax = fig.add_subplot(4,1,3)
212 axs.append(ax)
213
214 # use masked array to hide NaN's on plot
215 Tpw = numpy.ma.masked_where(numpy.isnan(Tpw), Tpw)
216 Tmw = numpy.ma.masked_where(numpy.isnan(Tmw), Tmw)
217
218 # ax.plot returns a list of lines, so unpack tuple
219 (x, y) = procutil.addnan(dt, Tpw, maxdelta=2./24)
220 l1, = ax.plot_date(x, y, fmt='b-')
221 l1.set_label('Peak Wind Period (Tpw)')
222
223 (x, y) = procutil.addnan(dt, Tmw, maxdelta=2./24)
224 l2, = ax.plot_date(x, y, fmt='c-')
225 l2.set_label('Mean Wind Period (Tmw)')
226
227 ax.set_ylabel('WAVE\nPERIOD (s)')
228 # ax.set_ylim(2.,10.)
229 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
230 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
231 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
232 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
233 ax.set_xticklabels([])
234
235 # legend
236 ls1 = l1.get_label()
237 ls2 = l2.get_label()
238 leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
239 ltext  = leg.get_texts()  # all the text.Text instance in the legend
240 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
241 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
242 frame.set_facecolor('0.80')      # set the frame face color to light gray
243 frame.set_alpha(0.5)             # set alpha low to see through
244 setp(ltext, fontsize='small')    # the legend text fontsize
245 setp(llines, linewidth=1.5)      # the legend linewidth
246 # leg.draw_frame(False)           # don't draw the legend frame
247
248 #######################################
249 #
250 ax = fig.add_subplot(4,1,4)
251 axs.append(ax)
252
253 # ax.plot returns a list of lines, so unpack tuple
254 (x, y) = procutil.addnan(dt, Dpw, maxdelta=2./24)
255 l1, = ax.plot_date(x, y, fmt='b-')
256 l1.set_label('Peak Wind Direction (Dpw)')
257
258 (x, y) = procutil.addnan(dt, Dmw, maxdelta=2./24)
259 l2, = ax.plot_date(x, y, fmt='c-')
260 l2.set_label('Mean Wind Direction (Dmw)')
261
262 ax.set_ylabel('WAVE\nDIR (deg N)')
263 ax.set_ylim(0.,360.)
264 # first to last regardless of what
265 # ax.set_xlim(dt[0], dt[-1])
266 # last minus 30 days,
267 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
268 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
269 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
270 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
271
272 ax.set_xlabel('Bogue WIND WAVES -- Last 30 days from ' + last_dt_str)
273
274 # legend
275 ls1 = l1.get_label()
276 ls2 = l2.get_label()
277 leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
278 ltext  = leg.get_texts()  # all the text.Text instance in the legend
279 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
280 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
281 frame.set_facecolor('0.80')      # set the frame face color to light gray
282 frame.set_alpha(0.5)             # set alpha low to see through
283 setp(ltext, fontsize='small')    # the legend text fontsize
284 setp(llines, linewidth=1.5)      # the legend linewidth
285 # leg.draw_frame(False)           # don't draw the legend frame
286
287 # save figure
288 savefig('/home/haines/rayleigh/img/bogue_windwaves_last30days.png')
289
290 #######################################
291 # Last 7 days
292 #######################################
293
294 print ' ... Last 7 days'
295 ax = axs[0]
296 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
297 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
298 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
299 ax.set_xticklabels([])
300 ax.set_xlabel('Bogue ADCPWAVES -- Last 7 days from ' + last_dt_str)
301
302 ax = axs[1]
303 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
304 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
305 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
306 ax.set_xticklabels([])
307
308 ax = axs[2]
309 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
310 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
311 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
312 ax.set_xticklabels([])
313
314 ax = axs[3]
315 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
316 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
317 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
318 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
319 ax.set_xlabel('Bogue WIND WAVES -- Last 7 days from ' + last_dt_str)
320
321 savefig('/home/haines/rayleigh/img/bogue_windwaves_last07days.png')
322
323 #######################################
324 # Last 1 day (24hrs)
325 #######################################
326
327 print ' ... Last 1 days'
328
329 ax = axs[0]
330 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
331 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
332 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
333 ax.set_xticklabels([])
334 ax.set_xlabel('Bogue WIND WAVES -- Last 24 hours from ' + last_dt_str)
335
336 ax = axs[1]
337 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
338 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
339 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
340 ax.set_xticklabels([])
341
342 ax = axs[2]
343 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
344 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
345 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
346 ax.set_xticklabels([])
347
348 ax = axs[3]
349 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
350 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
351 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
352 ax.xaxis.set_major_formatter( DateFormatter('%H') )
353 ax.set_xlabel('Bogue WIND WAVES -- Last 24 hours from ' + last_dt_str)
354
355 savefig('/home/haines/rayleigh/img/bogue_windwaves_last01days.png')
356
357
Note: See TracBrowser for help on using the browser.