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

root/proc2plot/tags/proc2plot-1.0/bogue_allwaves_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 /opt/env/haines/dataproc/bin/python
2 # Last modified:  Time-stamp: <2010-03-29 17:28:14 haines>
3 """bogue_allwaves_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_allwaves_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 have_ncFile1 = os.path.exists(ncFile1)
28 have_ncFile2 = os.path.exists(ncFile2)
29
30 print ' ... loading data for graph from ...'
31 print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
32 print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
33
34 # load data
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 Hs = nc.var('Hs')[:]
56 Tp = nc.var('Tp')[:]
57 Tm = nc.var('Tm')[:]
58 #Hmax = nc.var('Hmax')[:]
59 Dp = nc.var('Dp')[:]
60 Dm = nc.var('Dm')[:]
61 nc.close()
62
63 # ancillary data to plot
64 ncFile1='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+prev_month.strftime('%Y_%m')+'.nc'
65 ncFile2='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+this_month.strftime('%Y_%m')+'.nc'
66
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 # ibad = y <= -6999.
119 # y[ibad] = numpy.nan
120 # # just in case they are all NaNs make one real so it will plot
121 # if numpy.isnan(y).all():
122 #    y[-1]=0.
123    
124 # use masked array to hide NaN's on plot
125 # wd_anc = numpy.ma.masked_where(numpy.isnan(wd_anc), wd_anc)
126
127 # ax.plot returns a list of lines, so unpack tuple
128 (x, y) = procutil.addnan(dt_anc, wd_anc, maxdelta=2./24)
129 l1, = ax.plot_date(x, y, fmt='b-')
130 l1.set_label('Water Level (HAB)')
131
132 # right-hand side scale
133 ax2 = twinx(ax)
134 ax2.yaxis.tick_right()
135 # convert (lhs) meters to (rhs) feet
136 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
137 ax2.set_ylim(feet)
138 ax2.set_ylabel('HAB (feet)')
139
140 ax.set_ylabel('HEIGHT ABOVE\nBOTTOM (m)')
141 # ax.set_ylim(2.,10.)
142 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
143 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
144 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
145 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
146 ax.set_xticklabels([])
147
148 # this only moves the label not the tick labels
149 ax.xaxis.set_label_position('top')
150 ax.set_xlabel('Bogue ALL WAVES -- Last 30 days from ' + last_dt_str)
151
152
153 # ax2.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
154 # ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
155 # ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
156 # ax2.set_xticklabels([])
157
158 # legend
159 ls1 = l1.get_label()
160 leg = ax.legend((l1,), (ls1,), loc='upper left')
161 ltext  = leg.get_texts()  # all the text.Text instance in the legend
162 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
163 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
164 frame.set_facecolor('0.80')      # set the frame face color to light gray
165 frame.set_alpha(0.5)             # set alpha low to see through
166 setp(ltext, fontsize='small')    # the legend text fontsize
167 setp(llines, linewidth=1.5)      # the legend linewidth
168 # leg.draw_frame(False)           # don't draw the legend frame
169
170 #######################################
171 #
172 ax = fig.add_subplot(4,1,2)
173 axs.append(ax)
174
175 # use masked array to hide NaN's on plot
176 # Hs = numpy.ma.masked_where(numpy.isnan(Hs), Hs)
177 # Hmax = numpy.ma.masked_where(numpy.isnan(Hmax), Hmax)
178
179 # ax.plot returns a list of lines, so unpack tuple
180 (x, y) = procutil.addnan(dt, Hs, maxdelta=2./24)
181 l1, = ax.plot_date(x, y, fmt='b-')
182 l1.set_label('Significant Wave Height (Hs)')
183
184 # l2, = ax.plot_date(dt, Hmax, fmt='c-')
185 # l2.set_label('Max Wave Height (Hmax)')
186
187 ax.set_ylabel('WAVE\nHEIGHT (m)')
188 # ax.set_ylim(2.,10.)
189 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
190 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
191 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
192 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
193 ax.set_xticklabels([])
194
195 # right-hand side scale
196 ax2 = twinx(ax)
197 ax2.yaxis.tick_right()
198 # convert (lhs) meters to (rhs) feet
199 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
200 ax2.set_ylim(feet)
201 ax2.set_ylabel('(feet)')
202
203 ax2.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
204 ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
205 ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
206 ax2.set_xticklabels([])
207
208 # legend
209 ls1 = l1.get_label()
210 # ls2 = l2.get_label()
211 # leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
212 leg = ax.legend((l1,), (ls1,), loc='upper left')
213 ltext  = leg.get_texts()  # all the text.Text instance in the legend
214 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
215 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
216 frame.set_facecolor('0.80')      # set the frame face color to light gray
217 frame.set_alpha(0.5)             # set alpha low to see through
218 setp(ltext, fontsize='small')    # the legend text fontsize
219 setp(llines, linewidth=1.5)      # the legend linewidth
220 # leg.draw_frame(False)           # don't draw the legend frame
221
222 #######################################
223 #
224 ax = fig.add_subplot(4,1,3)
225 axs.append(ax)
226
227 # use masked array to hide NaN's on plot
228 # Tp = numpy.ma.masked_where(numpy.isnan(Tp), Tp)
229 # Tm = numpy.ma.masked_where(numpy.isnan(Tm), Tm)
230
231 # ax.plot returns a list of lines, so unpack tuple
232 (x, y) = procutil.addnan(dt, Tp, maxdelta=2./24)
233 l1, = ax.plot_date(x, y, fmt='b-')
234 l1.set_label('Peak Wave Period (Tp)')
235
236 (x, y) = procutil.addnan(dt, Tm, maxdelta=2./24)
237 l2, = ax.plot_date(x, y, fmt='c-')
238 l2.set_label('Mean Wave Period (Tm)')
239
240 ax.set_ylabel('WAVE\nPERIOD (s)')
241 # ax.set_ylim(2.,10.)
242 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
243 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
244 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
245 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
246 ax.set_xticklabels([])
247
248 # legend
249 ls1 = l1.get_label()
250 ls2 = l2.get_label()
251 leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
252 ltext  = leg.get_texts()  # all the text.Text instance in the legend
253 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
254 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
255 frame.set_facecolor('0.80')      # set the frame face color to light gray
256 frame.set_alpha(0.5)             # set alpha low to see through
257 setp(ltext, fontsize='small')    # the legend text fontsize
258 setp(llines, linewidth=1.5)      # the legend linewidth
259 # leg.draw_frame(False)           # don't draw the legend frame
260
261 #######################################
262 #
263 ax = fig.add_subplot(4,1,4)
264 axs.append(ax)
265
266 # use masked array to hide NaN's on plot
267 # Dp = numpy.ma.masked_where(numpy.isnan(Dp), Dp)
268 # Dm = numpy.ma.masked_where(numpy.isnan(Dm), Dm)
269
270 # ax.plot returns a list of lines, so unpack tuple
271 (x, y) = procutil.addnan(dt, Dp, maxdelta=2./24)
272 l1, = ax.plot_date(x, y, fmt='b-')
273 l1.set_label('Peak Wave Direction (Dp)')
274
275 (x, y) = procutil.addnan(dt, Dm, maxdelta=2./24)
276 l2, = ax.plot_date(x, y, fmt='c-')
277 l2.set_label('Mean Wave Direction (Dp)')
278
279 ax.set_ylabel('WAVE\nDIR (deg N)')
280 ax.set_ylim(0.,360.)
281 # first to last regardless of what
282 # ax.set_xlim(dt[0], dt[-1])
283 # last minus 30 days,
284 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
285 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
286 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
287 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
288
289 ax.set_xlabel('Bogue ALL WAVES -- Last 30 days from ' + last_dt_str)
290
291 # legend
292 ls1 = l1.get_label()
293 ls2 = l2.get_label()
294 leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
295 ltext  = leg.get_texts()  # all the text.Text instance in the legend
296 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
297 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
298 frame.set_facecolor('0.80')      # set the frame face color to light gray
299 frame.set_alpha(0.5)             # set alpha low to see through
300 setp(ltext, fontsize='small')    # the legend text fontsize
301 setp(llines, linewidth=1.5)      # the legend linewidth
302 # leg.draw_frame(False)           # don't draw the legend frame
303
304 # save figure
305 savefig('/home/haines/rayleigh/img/bogue_allwaves_last30days.png')
306
307 #######################################
308 # Last 7 days
309 #######################################
310
311 print ' ... Last 7 days'
312 ax = axs[0]
313 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
314 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
315 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
316 ax.set_xticklabels([])
317 ax.set_xlabel('Bogue ALL WAVES -- Last 7 days from ' + last_dt_str)
318
319 ax = axs[1]
320 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
321 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
322 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
323 ax.set_xticklabels([])
324
325 ax = axs[2]
326 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
327 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
328 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
329 ax.set_xticklabels([])
330
331 ax = axs[3]
332 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
333 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
334 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
335 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
336 ax.set_xlabel('Bogue ALL WAVES -- Last 7 days from ' + last_dt_str)
337
338 savefig('/home/haines/rayleigh/img/bogue_allwaves_last07days.png')
339
340 #######################################
341 # Last 1 day (24hrs)
342 #######################################
343
344 print ' ... Last 1 days'
345
346 ax = axs[0]
347 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
348 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
349 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
350 ax.set_xticklabels([])
351 ax.set_xlabel('Bogue ALL WAVES -- Last 24 hours from ' + last_dt_str)
352
353 ax = axs[1]
354 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
355 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
356 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
357 ax.set_xticklabels([])
358
359 ax = axs[2]
360 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
361 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
362 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
363 ax.set_xticklabels([])
364
365 ax = axs[3]
366 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
367 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
368 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
369 ax.xaxis.set_major_formatter( DateFormatter('%H') )
370 ax.set_xlabel('Bogue ALL WAVES -- Last 24 hours from ' + last_dt_str)
371
372 savefig('/home/haines/rayleigh/img/bogue_allwaves_last01days.png')
373
374
Note: See TracBrowser for help on using the browser.