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

root/proc2plot/trunk/proc2plot/bogue_swellwaves_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: <2009-10-29 16:15:21 haines>
3 """bogue_swellwaves_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_swellwaves_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 Hss = nc.var('Hs_swell')[:]
57 Tps = nc.var('Tp_swell')[:]
58 Tms = nc.var('Tm_swell')[:]
59 #Hmax = nc.var('Hmax')[:]
60 Dps = nc.var('Dp_swell')[:]
61 Dms = nc.var('Dm_swell')[:]
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 Level (HAB)')
122
123 ax.set_ylabel('HEIGHT ABOVE\nBOTTOM (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 ADCPWAVES -- 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('HAB (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, Hss, maxdelta=2./24)
167 l1, = ax.plot_date(x, y, fmt='b-')
168 l1.set_label('Significant Swell Wave Height (Hss)')
169
170 # (x, y) = procutil.addnan(dt, Hmax, maxdelta=2./24)
171 # l2, = ax.plot_date(x, y, fmt='g-')
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 # ax.plot returns a list of lines, so unpack tuple
215 (x, y) = procutil.addnan(dt, Tps, maxdelta=2./24)
216 l1, = ax.plot_date(x, y, fmt='b-')
217 l1.set_label('Peak Swell Period (Tp)')
218
219 (x, y) = procutil.addnan(dt, Tms, maxdelta=2./24)
220 l2, = ax.plot_date(x, y, fmt='c-')
221 l2.set_label('Mean Swell Period (Tm)')
222
223 ax.set_ylabel('WAVE\nPERIOD (s)')
224 # ax.set_ylim(2.,10.)
225 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
226 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
227 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
228 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
229 ax.set_xticklabels([])
230
231 # legend
232 ls1 = l1.get_label()
233 ls2 = l2.get_label()
234 leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
235 ltext  = leg.get_texts()  # all the text.Text instance in the legend
236 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
237 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
238 frame.set_facecolor('0.80')      # set the frame face color to light gray
239 frame.set_alpha(0.5)             # set alpha low to see through
240 setp(ltext, fontsize='small')    # the legend text fontsize
241 setp(llines, linewidth=1.5)      # the legend linewidth
242 # leg.draw_frame(False)           # don't draw the legend frame
243
244 #######################################
245 #
246 ax = fig.add_subplot(4,1,4)
247 axs.append(ax)
248
249 # ax.plot returns a list of lines, so unpack tuple
250 (x, y) = procutil.addnan(dt, Dps, maxdelta=2./24)
251 l1, = ax.plot_date(x, y, fmt='b-')
252 l1.set_label('Peak Swell Direction (Dp)')
253
254 (x, y) = procutil.addnan(dt, Dms, maxdelta=2./24)
255 l2, = ax.plot_date(x, y, fmt='c-')
256 l2.set_label('Mean Swell Direction (Dp)')
257
258 ax.set_ylabel('WAVE\nDIR (deg N)')
259 ax.set_ylim(0.,360.)
260 # first to last regardless of what
261 # ax.set_xlim(dt[0], dt[-1])
262 # last minus 30 days,
263 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
264 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
265 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
266 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
267
268 ax.set_xlabel('Bogue SWELL WAVES -- Last 30 days from ' + last_dt_str)
269
270 # legend
271 ls1 = l1.get_label()
272 ls2 = l2.get_label()
273 leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
274 ltext  = leg.get_texts()  # all the text.Text instance in the legend
275 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
276 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
277 frame.set_facecolor('0.80')      # set the frame face color to light gray
278 frame.set_alpha(0.5)             # set alpha low to see through
279 setp(ltext, fontsize='small')    # the legend text fontsize
280 setp(llines, linewidth=1.5)      # the legend linewidth
281 # leg.draw_frame(False)           # don't draw the legend frame
282
283 # save figure
284 savefig('/home/haines/rayleigh/img/bogue_swellwaves_last30days.png')
285
286 #######################################
287 # Last 7 days
288 #######################################
289
290 print ' ... Last 7 days'
291 ax = axs[0]
292 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
293 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
294 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
295 ax.set_xticklabels([])
296 ax.set_xlabel('Bogue ADCPWAVES -- Last 7 days from ' + last_dt_str)
297
298 ax = axs[1]
299 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
300 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
301 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
302 ax.set_xticklabels([])
303
304 ax = axs[2]
305 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
306 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
307 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
308 ax.set_xticklabels([])
309
310 ax = axs[3]
311 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
312 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
313 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
314 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
315 ax.set_xlabel('Bogue ADCPWAVES -- Last 7 days from ' + last_dt_str)
316
317 savefig('/home/haines/rayleigh/img/bogue_swellwaves_last07days.png')
318
319 #######################################
320 # Last 1 day (24hrs)
321 #######################################
322
323 print ' ... Last 1 days'
324
325 ax = axs[0]
326 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
327 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
328 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
329 ax.set_xticklabels([])
330 ax.set_xlabel('Bogue ADCPWAVES -- Last 24 hours from ' + last_dt_str)
331
332 ax = axs[1]
333 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
334 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
335 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
336 ax.set_xticklabels([])
337
338 ax = axs[2]
339 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
340 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
341 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
342 ax.set_xticklabels([])
343
344 ax = axs[3]
345 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
346 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
347 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
348 ax.xaxis.set_major_formatter( DateFormatter('%H') )
349 ax.set_xlabel('Bogue ADCPWAVES -- Last 24 hours from ' + last_dt_str)
350
351 savefig('/home/haines/rayleigh/img/bogue_swellwaves_last01days.png')
352
353
Note: See TracBrowser for help on using the browser.