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

root/proc2plot/trunk/proc2plot/morgan_avp_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 python
2 # Last modified:  Time-stamp: <2008-09-17 13:56:54 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/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 '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_2008_01.nc'
22 # ncFile2='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_2008_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     exit()
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 # Last 30 days
79 #######################################
80 print ' ... Last 30 days'
81 ax = fig.add_subplot(6,1,1)
82 axs = [ax]
83
84 # use masked array to hide NaN's on plot
85 wtm = numpy.ma.masked_where(numpy.isnan(wtemp), wtemp)
86 # range for pcolor plots
87 cmin = numpy.floor(wtm.min())
88 cmax = cmin+8.
89 # print "%s : %g %g" % ('Temp', cmin, cmax)
90 # cmin, cmax = (15., 35.)
91 # plot pcolor
92 pc = ax.pcolor(dn, z, wtm.T, cmap=cm.get_cmap('jet'), vmin=cmin, vmax=cmax)
93
94 # setup colorbar axes instance.
95 l,b,w,h = ax.get_position()
96 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
97
98 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
99 cb.set_label('Water Temperature (deg C)')
100 cb.ax.xaxis.set_label_position('top')
101 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
102 # make tick labels for 10 posns set and round to one decimal place
103 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
104 # but only select one at set_xticks
105 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
106
107 # ax.plot returns a list of lines, so unpack tuple
108 # l1, = ax.plot_date(dt, wd, fmt='k-')
109 # l1.set_label('Water Level')
110
111 ax.set_ylabel('Depth (m)')
112 ax.set_ylim(-4.,0.)
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('Morgan Bay AVP -- 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 (ft)')
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(6,1,2)
146 axs.append(ax)
147
148 # use masked array to hide NaN's on plot
149 sm = numpy.ma.masked_where(numpy.isnan(salin), salin)
150 # range for pcolor plots
151 cmin = numpy.floor(sm.mean()-2*sm.std())
152 cmax = cmin+10.
153 # print "%s : %g %g" % ('Salin', cmin, cmax)
154 # cmin, cmax = (0., 50.)
155 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('Blues_r'), vmin=cmin, vmax=cmax)
156
157 # setup colorbar axes instance.
158 l,b,w,h = ax.get_position()
159 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
160
161 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
162 cb.set_label('Salinity (PSU)')
163 cb.ax.xaxis.set_label_position('top')
164 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
165 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
166 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
167
168 # ax.plot returns a list of lines, so unpack tuple
169 # l1, = ax.plot_date(dt, wd, fmt='k-')
170 # l1.set_label('Water Level')
171
172 ax.set_ylabel('Depth (m)')
173 ax.set_ylim(-4.,0.)
174 # first to last regardless of what
175 # ax.set_xlim(dt[0], dt[-1])
176 # last minus 30 days,
177 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
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('Depth (ft)')
189
190 # legend
191 # ls1 = l1.get_label()
192 # leg = ax.legend((l1,), (ls1,), loc='upper left')
193 # ltext  = leg.get_texts()  # all the text.Text instance in the legend
194 # llines = leg.get_lines()  # all the lines.Line2D instance in the legend
195 # frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
196 # frame.set_facecolor('0.80')      # set the frame face color to light gray
197 # frame.set_alpha(0.5)             # set alpha low to see through
198 # setp(ltext, fontsize='small')    # the legend text fontsize
199 # setp(llines, linewidth=1.5)      # the legend linewidth
200 # leg.draw_frame(False)           # don't draw the legend frame
201
202 #######################################
203 #
204 ax = fig.add_subplot(6,1,3)
205 axs.append(ax)
206
207 # use masked array to hide NaN's on plot
208 sm = numpy.ma.masked_where(numpy.isnan(turb), turb)
209 # range for pcolor plots
210 # cmin, cmax = numpy.round([sm.min(), sm.max()], decimals=0)
211 # cmin = cmin-1
212 # cmax = cmax+1
213 cmin, cmax = (0., 10.)
214 # print "%s : %g %g" % ('Turb', cmin, cmax)
215 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('YlOrBr'), vmin=cmin, vmax=cmax)
216
217 # setup colorbar axes instance.
218 l,b,w,h = ax.get_position()
219 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
220
221 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
222 cb.set_label('Turbidity (NTU)')
223 cb.ax.xaxis.set_label_position('top')
224 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
225 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
226 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
227 # cb.ax.set_xticklabels([12., 16., 20., 24., 28.])
228
229 # ax.plot returns a list of lines, so unpack tuple
230 # l1, = ax.plot_date(dt, wd, fmt='k-')
231 # l1.set_label('Water Level')
232
233 ax.set_ylabel('Depth (m)')
234 ax.set_ylim(-4.,0.)
235 # first to last regardless of what
236 # ax.set_xlim(dt[0], dt[-1])
237 # last minus 30 days,
238 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
239 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
240 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
241 ax.set_xticklabels([])
242
243 # right-hand side scale
244 ax2 = twinx(ax)
245 ax2.yaxis.tick_right()
246 # convert (lhs) meters to (rhs) feet
247 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
248 ax2.set_ylim(feet)
249 ax2.set_ylabel('Depth (ft)')
250
251 #######################################
252 #
253 ax = fig.add_subplot(6,1,4)
254 axs.append(ax)
255
256 # use masked array to hide NaN's on plot
257 sm = numpy.ma.masked_where(numpy.isnan(chl), chl)
258 # range for pcolor plots
259 cmin = numpy.floor(sm.min())
260 cmax = cmin+15.
261 # print "%s : %g %g" % ('Chloro', cmin, cmax)
262 # cmin, cmax = (0., 100.)
263 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('BuGn'), vmin=cmin, vmax=cmax)
264
265 # setup colorbar axes instance.
266 l,b,w,h = ax.get_position()
267 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
268
269 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
270 cb.set_label('Chlorophyll (ug l-1)')
271 cb.ax.xaxis.set_label_position('top')
272 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
273 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
274 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
275 # cb.ax.set_xticklabels([12., 16., 20., 24., 28.])
276
277 # ax.plot returns a list of lines, so unpack tuple
278 # l1, = ax.plot_date(dt, wd, fmt='k-')
279 # l1.set_label('Water Level')
280
281 ax.set_ylabel('Depth (m)')
282 ax.set_ylim(-4.,0.)
283 # first to last regardless of what
284 # ax.set_xlim(dt[0], dt[-1])
285 # last minus 30 days,
286 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
287 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
288 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
289 ax.set_xticklabels([])
290
291 # right-hand side scale
292 ax2 = twinx(ax)
293 ax2.yaxis.tick_right()
294 # convert (lhs) meters to (rhs) feet
295 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
296 ax2.set_ylim(feet)
297 ax2.set_ylabel('Depth (ft)')
298
299 #######################################
300 #
301 ax = fig.add_subplot(6,1,5)
302 axs.append(ax)
303
304 # use masked array to hide NaN's on plot
305 sm = numpy.ma.masked_where(numpy.isnan(do), do)
306 # range for pcolor plots
307 cmin, cmax = (0., 15.)
308 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('PiYG'), vmin=cmin, vmax=cmax)
309
310 # setup colorbar axes instance.
311 l,b,w,h = ax.get_position()
312 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
313
314 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
315 cb.set_label('Dissolved Oxygen (mg l-1)')
316 cb.ax.xaxis.set_label_position('top')
317 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
318 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0)
319 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
320 # cb.ax.set_xticklabels([12., 16., 20., 24., 28.])
321
322 # ax.plot returns a list of lines, so unpack tuple
323 # l1, = ax.plot_date(dt, wd, fmt='k-')
324 # l1.set_label('Water Level')
325
326 ax.set_ylabel('Depth (m)')
327 ax.set_ylim(-4.,0.)
328 # first to last regardless of what
329 # ax.set_xlim(dt[0], dt[-1])
330 # last minus 30 days,
331 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
332 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
333 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
334 ax.set_xticklabels([])
335
336 # right-hand side scale
337 ax2 = twinx(ax)
338 ax2.yaxis.tick_right()
339 # convert (lhs) meters to (rhs) feet
340 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
341 ax2.set_ylim(feet)
342 ax2.set_ylabel('Depth (ft)')
343
344 #######################################
345 #
346 ax = fig.add_subplot(6,1,6)
347 axs.append(ax)
348
349 # use masked array to hide NaN's on plot
350 sm = numpy.ma.masked_where(numpy.isnan(ph), ph)
351 # range for pcolor plots
352 # cmin, cmax = numpy.round([sm.min(), sm.max()], decimals=0)
353 # cmin = cmin-1
354 # cmax = cmax+1
355 cmin, cmax = (6., 8.)
356 print "%s : %g %g" % ('pH', cmin, cmax)
357 pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('cool'), vmin=cmin, vmax=cmax)
358
359 # setup colorbar axes instance.
360 l,b,w,h = ax.get_position()
361 cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03])
362
363 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
364 cb.set_label('pH')
365 cb.ax.xaxis.set_label_position('top')
366 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
367 xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1)
368 cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]])
369 # cb.ax.set_xticklabels([12., 16., 20., 24., 28.])
370
371 # ax.plot returns a list of lines, so unpack tuple
372 # l1, = ax.plot_date(dt, wd, fmt='k-')
373 # l1.set_label('Water Level')
374
375 ax.set_ylabel('Depth (m)')
376 ax.set_ylim(-4.,0.)
377 # first to last regardless of what
378 # ax.set_xlim(dt[0], dt[-1])
379 # last minus 30 days,
380 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
381 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
382 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
383 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
384
385 ax.set_xlabel('Morgan Bay AVP -- Last 30 days from ' + last_dt_str)
386
387 # right-hand side scale
388 ax2 = twinx(ax)
389 ax2.yaxis.tick_right()
390 # convert (lhs) meters to (rhs) feet
391 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
392 ax2.set_ylim(feet)
393 ax2.set_ylabel('Depth (ft)')
394
395 # save figure
396 savefig('/home/haines/rayleigh/img/morgan_avp_last30days.png')
397
398 #######################################
399 # Last 7 days
400 #######################################
401
402 print ' ... Last 7 days'
403 for i in [0, 1, 2, 3, 4]:
404     ax = axs[i]
405     ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
406     ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) )
407     ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
408     ax.set_xticklabels([])
409     if i==0:
410         ax.set_xlabel('Morgan Bay AVP -- Last 7 days from ' + last_dt_str)
411 #
412
413 ax = axs[5]
414 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
415 ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) )
416 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
417 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
418 ax.set_xlabel('Morgan Bay AVP -- Last 7 days from ' + last_dt_str)
419
420 savefig('/home/haines/rayleigh/img/morgan_avp_last07days.png')
421
422 #######################################
423 # Last 1 day (24hrs)
424 #######################################
425
426 print ' ... Last 1 days'
427
428 for i in [0, 1, 2, 3, 4]:
429     ax = axs[i]
430     ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
431     ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
432     ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
433     ax.set_xticklabels([])
434     if i==0:
435         ax.set_xlabel('Morgan Bay AVP -- Last 24 hours from ' + last_dt_str)
436
437 ax = axs[5]
438 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
439 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
440 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
441 ax.xaxis.set_major_formatter( DateFormatter('%H') )
442 ax.set_xlabel('Morgan Bay AVP -- Last 24 hours from ' + last_dt_str)
443
444 savefig('/home/haines/rayleigh/img/morgan_avp_last01days.png')
445
Note: See TracBrowser for help on using the browser.