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

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