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

root/proc2plot/trunk/proc2plot/morgan_avp_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: <2011-02-25 17:02:13 haines>
3 """morgan_avp_plot"""
4
5 import os, sys
6 import datetime, time, dateutil, dateutil.tz
7 import pycdf
8 import numpy
9
10 sys.path.append('/home/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 'morgan_avp_plot ...'
20 prev_month, this_month, next_month = procutil.find_months(procutil.this_month())
21 ncFile1='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_2012_01.nc'
22 ncFile2='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_2012_02.nc'
23 # ncFile1='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_'+prev_month.strftime('%Y_%m')+'.nc'
24 # ncFile2='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_'+this_month.strftime('%Y_%m')+'.nc'
25
26 have_ncFile1 = os.path.exists(ncFile1)
27 have_ncFile2 = os.path.exists(ncFile2)
28
29 print ' ... loading data for graph from ...'
30 print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
31 print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
32
33 if have_ncFile1 and have_ncFile2:
34     nc = pycdf.CDFMF((ncFile1, ncFile2))
35 elif not have_ncFile1 and have_ncFile2:
36     nc = pycdf.CDFMF((ncFile2,))
37 elif have_ncFile1 and not have_ncFile2:
38     nc = pycdf.CDFMF((ncFile1,))
39 else:
40     print ' ... both files do not exist -- NO DATA LOADED'
41     return
42
43 ncvars = nc.variables()
44 # print ncvars
45 es = nc.var('time')[:]
46 units = nc.var('time').units
47 dt = [procutil.es2dt(e) for e in es]
48 # set timezone info to UTC (since data from level1 should be in UTC!!)
49 dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
50 # return new datetime based on computer local
51 dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
52 dn = date2num(dt)
53 z = nc.var('z')[:]
54 wd = nc.var('wd')[:]
55 wtemp = nc.var('wtemp')[:]
56 salin = nc.var('salin')[:]
57 turb = nc.var('turb')[:]
58 ph = nc.var('ph')[:]
59 do = nc.var('do')[:]
60 chl = nc.var('chl')[:]
61 nc.close()
62
63 # last dt in data for labels
64 dt1 = dt[-1]
65 dt2 = dt_local[-1]
66
67 diff = abs(dt1 - dt2)
68 if diff.days>0:
69     last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
70 else:
71     last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
72               + dt2.strftime(" on %b %d, %Y")
73 # add a couple of inches to length of page for 6 subplots (5 subs in 8 inches, 6 subs in 10)
74 fig = figure(figsize=(10, 10))
75 fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
76
77
78
79 #######################################
80 # Last 30 days
81 #######################################
82 print ' ... Last 30 days'
83 ax = fig.add_subplot(6,1,1)
84 axs = [ax]
85
86 # use masked array to hide NaN's on plot
87 wtm = numpy.ma.masked_where(numpy.isnan(wtemp), wtemp)
88 # range for pcolor plots
89 cmin = numpy.floor(wtm.min())
90 cmax = cmin+8.
91 # print "%s : %g %g" % ('Temp', cmin, cmax)
92 # cmin, cmax = (15., 35.)
93 # plot pcolor
94 pc = ax.pcolor(dn, z, wtm.T, cmap=cm.get_cmap('jet'), vmin=cmin, vmax=cmax)
95
96 # setup colorbar axes instance.
97 l,b,w,h = ax.get_position()
98 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
99
100 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
101 cb.set_label('Water Temperature (deg C)')
102 cb.ax.xaxis.set_label_position('top')
103 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
104 # make tick labels for 10 posns set and round to one decimal place
105 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
106 # but only select one at set_xticks
107 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
108
109 # ax.plot returns a list of lines, so unpack tuple
110 # l1, = ax.plot_date(dt, wd, fmt='k-')
111 # l1.set_label('Water Level')
112
113 ax.set_ylabel('Depth (m)')
114 ax.set_ylim(-4.,0.)
115 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
116 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
117 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
118 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
119 ax.set_xticklabels([])
120
121 # this only moves the label not the tick labels
122 ax.xaxis.set_label_position('top')
123 ax.set_xlabel('Morgan Bay AVP -- Last 30 days from ' + last_dt_str)
124
125 # right-hand side scale
126 ax2 = twinx(ax)
127 ax2.yaxis.tick_right()
128 # convert (lhs) meters to (rhs) feet
129 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
130 ax2.set_ylim(feet)
131 ax2.set_ylabel('Depth (ft)')
132
133 # legend
134 # ls1 = l1.get_label()
135 # leg = ax.legend((l1,), (ls1,), loc='upper left')
136 # ltext  = leg.get_texts()  # all the text.Text instance in the legend
137 # llines = leg.get_lines()  # all the lines.Line2D instance in the legend
138 # frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
139 # frame.set_facecolor('0.80')      # set the frame face color to light gray
140 # frame.set_alpha(0.5)             # set alpha low to see through
141 # setp(ltext, fontsize='small')    # the legend text fontsize
142 # setp(llines, linewidth=1.5)      # the legend linewidth
143 # leg.draw_frame(False)           # don't draw the legend frame
144
145 #######################################
146 #
147 ax = fig.add_subplot(6,1,2)
148 axs.append(ax)
149
150 # use masked array to hide NaN's on plot
151 sm = numpy.ma.masked_where(numpy.isnan(salin), salin)
152 # range for pcolor plots
153 cmin = numpy.floor(sm.mean()-2*sm.std())
154 cmax = cmin+10.
155 # print "%s : %g %g" % ('Salin', cmin, cmax)
156 # cmin, cmax = (0., 50.)
157 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('Blues_r'), vmin=cmin, vmax=cmax)
158
159 # setup colorbar axes instance.
160 l,b,w,h = ax.get_position()
161 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
162
163 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
164 cb.set_label('Salinity (PSU)')
165 cb.ax.xaxis.set_label_position('top')
166 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
167 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
168 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
169
170 # ax.plot returns a list of lines, so unpack tuple
171 # l1, = ax.plot_date(dt, wd, fmt='k-')
172 # l1.set_label('Water Level')
173
174 ax.set_ylabel('Depth (m)')
175 ax.set_ylim(-4.,0.)
176 # first to last regardless of what
177 # ax.set_xlim(dt[0], dt[-1])
178 # last minus 30 days,
179 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
180 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
181 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
182 ax.set_xticklabels([])
183
184 # right-hand side scale
185 ax2 = twinx(ax)
186 ax2.yaxis.tick_right()
187 # convert (lhs) meters to (rhs) feet
188 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
189 ax2.set_ylim(feet)
190 ax2.set_ylabel('Depth (ft)')
191
192 # legend
193 # ls1 = l1.get_label()
194 # leg = ax.legend((l1,), (ls1,), loc='upper left')
195 # ltext  = leg.get_texts()  # all the text.Text instance in the legend
196 # llines = leg.get_lines()  # all the lines.Line2D instance in the legend
197 # frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
198 # frame.set_facecolor('0.80')      # set the frame face color to light gray
199 # frame.set_alpha(0.5)             # set alpha low to see through
200 # setp(ltext, fontsize='small')    # the legend text fontsize
201 # setp(llines, linewidth=1.5)      # the legend linewidth
202 # leg.draw_frame(False)           # don't draw the legend frame
203
204 #######################################
205 #
206 ax = fig.add_subplot(6,1,3)
207 axs.append(ax)
208
209 # use masked array to hide NaN's on plot
210 sm = numpy.ma.masked_where(numpy.isnan(turb), turb)
211 # range for pcolor plots
212 # cmin, cmax = numpy.round([sm.min(), sm.max()], decimals=0)
213 # cmin = cmin-1
214 # cmax = cmax+1
215 cmin, cmax = (0., 10.)
216 # print "%s : %g %g" % ('Turb', cmin, cmax)
217 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('YlOrBr'), vmin=cmin, vmax=cmax)
218
219 # setup colorbar axes instance.
220 l,b,w,h = ax.get_position()
221 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
222
223 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
224 cb.set_label('Turbidity (NTU)')
225 cb.ax.xaxis.set_label_position('top')
226 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
227 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
228 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
229 # cb.ax.set_xticklabels([12., 16., 20., 24., 28.])
230
231 # ax.plot returns a list of lines, so unpack tuple
232 # l1, = ax.plot_date(dt, wd, fmt='k-')
233 # l1.set_label('Water Level')
234
235 ax.set_ylabel('Depth (m)')
236 ax.set_ylim(-4.,0.)
237 # first to last regardless of what
238 # ax.set_xlim(dt[0], dt[-1])
239 # last minus 30 days,
240 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
241 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
242 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
243 ax.set_xticklabels([])
244
245 # right-hand side scale
246 ax2 = twinx(ax)
247 ax2.yaxis.tick_right()
248 # convert (lhs) meters to (rhs) feet
249 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
250 ax2.set_ylim(feet)
251 ax2.set_ylabel('Depth (ft)')
252
253 #######################################
254 #
255 ax = fig.add_subplot(6,1,4)
256 axs.append(ax)
257
258 # use masked array to hide NaN's on plot
259 sm = numpy.ma.masked_where(numpy.isnan(chl), chl)
260 # range for pcolor plots
261 cmin = numpy.floor(sm.min())
262 cmax = cmin+15.
263 # print "%s : %g %g" % ('Chloro', cmin, cmax)
264 # cmin, cmax = (0., 100.)
265 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('BuGn'), vmin=cmin, vmax=cmax)
266
267 # setup colorbar axes instance.
268 l,b,w,h = ax.get_position()
269 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
270
271 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
272 cb.set_label('Chlorophyll (ug l-1)')
273 cb.ax.xaxis.set_label_position('top')
274 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
275 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
276 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
277 # cb.ax.set_xticklabels([12., 16., 20., 24., 28.])
278
279 # ax.plot returns a list of lines, so unpack tuple
280 # l1, = ax.plot_date(dt, wd, fmt='k-')
281 # l1.set_label('Water Level')
282
283 ax.set_ylabel('Depth (m)')
284 ax.set_ylim(-4.,0.)
285 # first to last regardless of what
286 # ax.set_xlim(dt[0], dt[-1])
287 # last minus 30 days,
288 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
289 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
290 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
291 ax.set_xticklabels([])
292
293 # right-hand side scale
294 ax2 = twinx(ax)
295 ax2.yaxis.tick_right()
296 # convert (lhs) meters to (rhs) feet
297 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
298 ax2.set_ylim(feet)
299 ax2.set_ylabel('Depth (ft)')
300
301 #######################################
302 #
303 ax = fig.add_subplot(6,1,5)
304 axs.append(ax)
305
306 # use masked array to hide NaN's on plot
307 sm = numpy.ma.masked_where(numpy.isnan(do), do)
308 # range for pcolor plots
309 cmin, cmax = (0., 15.)
310 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('PiYG'), vmin=cmin, vmax=cmax)
311
312 # setup colorbar axes instance.
313 l,b,w,h = ax.get_position()
314 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
315
316 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
317 cb.set_label('Dissolved Oxygen (mg l-1)')
318 cb.ax.xaxis.set_label_position('top')
319 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
320 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
321 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
322 # cb.ax.set_xticklabels([12., 16., 20., 24., 28.])
323
324 # ax.plot returns a list of lines, so unpack tuple
325 # l1, = ax.plot_date(dt, wd, fmt='k-')
326 # l1.set_label('Water Level')
327
328 ax.set_ylabel('Depth (m)')
329 ax.set_ylim(-4.,0.)
330 # first to last regardless of what
331 # ax.set_xlim(dt[0], dt[-1])
332 # last minus 30 days,
333 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
334 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
335 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
336 ax.set_xticklabels([])
337
338 # right-hand side scale
339 ax2 = twinx(ax)
340 ax2.yaxis.tick_right()
341 # convert (lhs) meters to (rhs) feet
342 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
343 ax2.set_ylim(feet)
344 ax2.set_ylabel('Depth (ft)')
345
346 #######################################
347 #
348 ax = fig.add_subplot(6,1,6)
349 axs.append(ax)
350
351 # use masked array to hide NaN's on plot
352 sm = numpy.ma.masked_where(numpy.isnan(ph), ph)
353 # range for pcolor plots
354 # cmin, cmax = numpy.round([sm.min(), sm.max()], decimals=0)
355 # cmin = cmin-1
356 # cmax = cmax+1
357 cmin, cmax = (6., 8.)
358 print "%s : %g %g" % ('pH', cmin, cmax)
359 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('cool'), vmin=cmin, vmax=cmax)
360
361 # setup colorbar axes instance.
362 l,b,w,h = ax.get_position()
363 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
364
365 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
366 cb.set_label('pH')
367 cb.ax.xaxis.set_label_position('top')
368 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
369 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
370 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
371 # cb.ax.set_xticklabels([12., 16., 20., 24., 28.])
372
373 # ax.plot returns a list of lines, so unpack tuple
374 # l1, = ax.plot_date(dt, wd, fmt='k-')
375 # l1.set_label('Water Level')
376
377 ax.set_ylabel('Depth (m)')
378 ax.set_ylim(-4.,0.)
379 # first to last regardless of what
380 # ax.set_xlim(dt[0], dt[-1])
381 # last minus 30 days,
382 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
383 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
384 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
385 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
386
387 ax.set_xlabel('Morgan Bay AVP -- Last 30 days from ' + last_dt_str)
388
389 # right-hand side scale
390 ax2 = twinx(ax)
391 ax2.yaxis.tick_right()
392 # convert (lhs) meters to (rhs) feet
393 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
394 ax2.set_ylim(feet)
395 ax2.set_ylabel('Depth (ft)')
396
397 # save figure
398 savefig('/home/haines/rayleigh/img/morgan_avp_last30days.png')
399
400 #######################################
401 # Last 7 days
402 #######################################
403
404 print ' ... Last 7 days'
405 for i in [0, 1, 2, 3, 4]:
406     ax = axs[i]
407     ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
408     ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) )
409     ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
410     ax.set_xticklabels([])
411     if i==0:
412         ax.set_xlabel('Morgan Bay AVP -- Last 7 days from ' + last_dt_str)
413 #
414
415 ax = axs[5]
416 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
417 ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) )
418 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
419 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
420 ax.set_xlabel('Morgan Bay AVP -- Last 7 days from ' + last_dt_str)
421
422 savefig('/home/haines/rayleigh/img/morgan_avp_last07days.png')
423
424 #######################################
425 # Last 1 day (24hrs)
426 #######################################
427
428 print ' ... Last 1 days'
429
430 for i in [0, 1, 2, 3, 4]:
431     ax = axs[i]
432     ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
433     ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
434     ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
435     ax.set_xticklabels([])
436     if i==0:
437         ax.set_xlabel('Morgan Bay AVP -- Last 24 hours from ' + last_dt_str)
438
439 ax = axs[5]
440 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
441 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
442 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
443 ax.xaxis.set_major_formatter( DateFormatter('%H') )
444 ax.set_xlabel('Morgan Bay AVP -- Last 24 hours from ' + last_dt_str)
445
446 savefig('/home/haines/rayleigh/img/morgan_avp_last01days.png')
447
Note: See TracBrowser for help on using the browser.