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

root/proc2plot/trunk/proc2plot/crow_flow_plot.py

Revision 326 (checked in by haines, 14 years ago)

import first verison proc2plot

Line 
1 #!/usr/bin/env /opt/env/haines/dataproc/bin/python
2 # Last modified:  Time-stamp: <2010-04-09 16:52:14 haines>
3 """crow_flow_plot"""
4
5 # plot each month
6
7 import os
8 # import sys, glob, re
9 import datetime, time, dateutil, dateutil.tz
10 import pycdf
11 import numpy
12
13 # sys.path.append('/opt/env/haines/dataproc/raw2proc')
14 # del(sys)
15
16 os.environ["MPLCONFIGDIR"]="/home/haines/.matplotlib/"
17
18 from pylab import figure, twinx, savefig, setp, getp, cm, colorbar
19 from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date
20 import procutil
21
22 def timeseries(pi, si, yyyy_mm, plot_type='latest'):
23     """
24     """
25
26
27     print 'crow_flow_plot ...'
28
29     #
30
31     prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
32     yyyy_mm_str = this_month.strftime('%Y_%m')
33
34     ncFile1='/seacoos/data/nccoos/level1/crow/wq/crow_wq_'+prev_month.strftime('%Y_%m')+'.nc'
35     ncFile2='/seacoos/data/nccoos/level1/crow/wq/crow_wq_'+this_month.strftime('%Y_%m')+'.nc'
36
37     have_ncFile1 = os.path.exists(ncFile1)
38     have_ncFile2 = os.path.exists(ncFile2)
39
40     print ' ... loading data for graph from ...'
41     print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
42     print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
43
44     # open netcdf data
45     if have_ncFile1 and have_ncFile2:
46         nc = pycdf.CDFMF((ncFile1, ncFile2))
47     elif not have_ncFile1 and have_ncFile2:
48         nc = pycdf.CDFMF((ncFile2,))
49     elif have_ncFile1 and not have_ncFile2:
50         nc = pycdf.CDFMF((ncFile1,))
51     else:
52         print ' ... both files do not exist -- NO DATA LOADED'
53         exit()
54
55     # ncvars = nc.variables()
56     # print ncvars
57     es = nc.var('time')[:]
58     units = nc.var('time').units
59     dt = [procutil.es2dt(e) for e in es]
60     # set timezone info to UTC (since data from level1 should be in UTC!!)
61     dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
62     # return new datetime based on computer local
63     dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
64     dn = date2num(dt)
65
66     z = nc.var('z')[:]
67     wtemp = nc.var('wtemp')[:]
68     cond = nc.var('cond')[:]
69     turb = nc.var('turb')[:]
70     ph = nc.var('ph')[:]
71     do_mg = nc.var('do_mg')[:]
72     do_sat = nc.var('do_sat')[:]
73     batt = nc.var('battvolts')[:]
74     nc.close()
75
76
77     ncFile1='/seacoos/data/nccoos/level1/crow/flow/crow_flow_'+prev_month.strftime('%Y_%m')+'.nc'
78     ncFile2='/seacoos/data/nccoos/level1/crow/flow/crow_flow_'+this_month.strftime('%Y_%m')+'.nc'
79
80     have_ncFile1 = os.path.exists(ncFile1)
81     have_ncFile2 = os.path.exists(ncFile2)
82
83     print ' ... loading data for graph from ...'
84     print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
85     print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
86
87     # open netcdf data
88     if have_ncFile1 and have_ncFile2:
89         nc = pycdf.CDFMF((ncFile1, ncFile2))
90     elif not have_ncFile1 and have_ncFile2:
91         nc = pycdf.CDFMF((ncFile2,))
92     elif have_ncFile1 and not have_ncFile2:
93         nc = pycdf.CDFMF((ncFile1,))
94     else:
95         print ' ... both files do not exist -- NO DATA LOADED'
96         exit()
97
98     # ncvars = nc.variables()
99     # print ncvars
100     es2 = nc.var('time')[:]
101     units = nc.var('time').units
102     dt2 = [procutil.es2dt(e) for e in es2]
103     # set timezone info to UTC (since data from level1 should be in UTC!!)
104     dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2]
105     dn2 = date2num(dt2)
106
107     r2 = nc.var('rain')[:] # inches of rain in past 15 min
108
109     ncvars = nc.variables()
110     have_press = 'press_wl' in ncvars.keys() or 'press_flow' in ncvars.keys()
111     have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys()
112     if have_press:
113         pwl2 = nc.var('press_wl')[:] # feet
114         pfl2 = nc.var('press_flow')[:] # cfs
115     if have_sontek:
116         swl2 = nc.var('sontek_wl')[:] # feet
117         sfl2 = nc.var('sontek_flow')[:] # cfs
118
119     nc.close()
120
121     # last dt in data for labels
122     dtu = dt[-1]
123     dtl = dt_local[-1]
124
125     diff = abs(dtu - dtl)
126     if diff.days>0:
127         last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')'
128     else:
129         last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \
130                       + dtl.strftime(" on %b %d, %Y")
131
132     #######################################
133     # Plot month all flow (plus battery) the timeseries data
134     #######################################
135
136     fig = figure(figsize=(10, 8))
137     fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
138    
139     ax = fig.add_subplot(4,1,1)
140     axs = [ax]
141    
142     # ax.plot returns a list of lines, so unpack tuple
143     (x, y) = procutil.addnan(dt2, r2)
144     l1, = ax.plot_date(x, y, fmt='b-')
145     l1.set_label('Precipitation')
146    
147     ax.set_ylabel('Rain (in)')
148     # ax.set_ylim(0,1.)
149     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
150     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
151     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
152     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
153     ax.set_xticklabels([])
154
155     # right-hand side scale
156     ax2 = twinx(ax)
157     ax2.yaxis.tick_right()
158     # convert (lhs) inches to (rhs) mm
159     f = [procutil.inches2millimeters(val) for val in ax.get_ylim()]
160     ax2.set_ylim(f)
161     ax2.set_ylabel('Rain (mm)')
162     ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
163     ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
164     ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
165     ax2.set_xticklabels([])
166
167     # this only moves the label not the tick labels
168     ax.xaxis.set_label_position('top')
169     ax.set_xlabel('CROW Stream Flow -- ' + yyyy_mm_str)
170    
171     # legend
172     ls1 = l1.get_label()
173     leg = ax.legend((l1,), (ls1,), loc='upper left')
174     ltext  = leg.get_texts()  # all the text.Text instance in the legend
175     llines = leg.get_lines()  # all the lines.Line2D instance in the legend
176     frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
177     frame.set_facecolor('0.80')      # set the frame face color to light gray
178     frame.set_alpha(0.5)             # set alpha low to see through
179     setp(ltext, fontsize='small')    # the legend text fontsize
180     setp(llines, linewidth=1.5)      # the legend linewidth
181     # leg.draw_frame(False)           # don't draw the legend frame
182    
183     #######################################
184     #
185     ax = fig.add_subplot(4,1,2)
186     axs.append(ax)
187
188     # ax.plot returns a list of lines, so unpack tuple
189     if have_press:
190         (x, y) = procutil.addnan(dt2, pfl2)
191         l1, = ax.plot_date(x, y, fmt='b-')
192         l1.set_label('Derived from pressure')
193         ls1 = l1.get_label()
194     if have_sontek:
195         (x, y) = procutil.addnan(dt2, sfl2)
196         l2, = ax.plot_date(x, y, fmt='c-')
197         l2.set_label('Depth-avg Acoustic Profile')   
198         ls2 = l2.get_label()
199    
200     ax.plot_date((x[1],x[-1]), (0,0), fmt='k:')
201     # ax.plot_date((x[1],x[-1]), (2.5,2.5), fmt='k--')
202
203     ax.set_ylabel('Depth (ft)')
204     # ax.set_ylim(-0.2,3)
205     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
206     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
207     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
208     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
209     ax.set_xticklabels([])
210
211     # right-hand side scale
212     ax2 = twinx(ax)
213     ax2.yaxis.tick_right()
214     # convert (lhs) feet to (rhs) meters
215     f = [procutil.feet2meters(val) for val in ax.get_ylim()]
216     ax2.set_ylim(f)
217     ax2.set_ylabel('Depth (m)')
218     ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
219     ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
220     ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
221     ax2.set_xticklabels([])
222
223     # legend
224     if have_press and have_sontek:
225         leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
226     elif have_press:
227         leg = ax.legend((l1,), (ls1,), loc='upper left')       
228     if have_sontek:
229         leg = ax.legend((l2,), (ls2,), loc='upper left')       
230
231     ltext  = leg.get_texts()  # all the text.Text instance in the legend
232     llines = leg.get_lines()  # all the lines.Line2D instance in the legend
233     frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
234     frame.set_facecolor('0.80')      # set the frame face color to light gray
235     frame.set_alpha(0.5)             # set alpha low to see through
236     setp(ltext, fontsize='small')    # the legend text fontsize
237     setp(llines, linewidth=1.5)      # the legend linewidth
238     # leg.draw_frame(False)           # don't draw the legend frame
239    
240     #######################################
241     #
242     ax = fig.add_subplot(4,1,3)
243     axs.append(ax)
244    
245     # ax.plot returns a list of lines, so unpack tuple
246     if have_press:
247         (x, y) = procutil.addnan(dt2, pfl2)
248         l1, = ax.plot_date(x, y, fmt='b-')
249         l1.set_label('Derived from pressure')
250         ls1 = l1.get_label()
251     if have_sontek:
252         (x, y) = procutil.addnan(dt2, sfl2)
253         l2, = ax.plot_date(x, y, fmt='c-')
254         l2.set_label('Depth-avg Acoustic Profile')   
255         ls2 = l2.get_label()
256    
257     ax.plot_date((x[1],x[-1]), (0,0), fmt='k:')
258
259     ax.set_ylabel('Flow (cfs)')
260     # ax.set_ylim(0,350)
261     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
262     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
263     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
264     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
265     ax.set_xticklabels([])
266
267     # legend
268     if have_press and have_sontek:
269         leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
270     elif have_press:
271         leg = ax.legend((l1,), (ls1,), loc='upper left')
272     elif have_sontek:
273         leg = ax.legend((l2,), (ls2,), loc='upper left')
274        
275     ltext  = leg.get_texts()  # all the text.Text instance in the legend
276     llines = leg.get_lines()  # all the lines.Line2D instance in the legend
277     frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
278     frame.set_facecolor('0.80')      # set the frame face color to light gray
279     frame.set_alpha(0.5)             # set alpha low to see through
280     setp(ltext, fontsize='small')    # the legend text fontsize
281     setp(llines, linewidth=1.5)      # the legend linewidth
282     # leg.draw_frame(False)           # don't draw the legend frame
283
284     #######################################
285     #
286     #######################################
287     #
288     ax = fig.add_subplot(4,1,4)
289     axs.append(ax)
290
291     # ax.plot returns a list of lines, so unpack tuple
292     (x, y) = procutil.addnan(dt, batt)
293     l1, = ax.plot_date(x, y, fmt='b-')
294     l1.set_label('Station Battery')
295    
296     ax.set_ylabel('Battery\n (volts)')
297     # ax.set_ylim(12, 14.5)
298     ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
299     ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
300     ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
301     ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
302
303     ax.set_xlabel('CROW Stream Flow -- ' + yyyy_mm_str)
304
305     # legend
306     ls1 = l1.get_label()
307     leg = ax.legend((l1,), (ls1,), loc='upper left')
308     ltext  = leg.get_texts()  # all the text.Text instance in the legend
309     llines = leg.get_lines()  # all the lines.Line2D instance in the legend
310     frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
311     frame.set_facecolor('0.80')      # set the frame face color to light gray
312     frame.set_alpha(0.5)             # set alpha low to see through
313     setp(ltext, fontsize='small')    # the legend text fontsize
314     setp(llines, linewidth=1.5)      # the legend linewidth
315     # leg.draw_frame(False)           # don't draw the legend frame
316    
317     # save figure   
318     ofn = '/home/haines/rayleigh/img/crow/crow_flow_' + yyyy_mm_str + '.png'
319     print '... ... write: %s' % (ofn,)
320     savefig(ofn)
321
322
323     #######################################
324     # Last 30 days
325     #######################################
326     if plot_type=='latest':
327         print ' ... Last 30 days'
328         for idx, ax in enumerate(axs):
329             ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
330             ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
331             ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
332             ax.set_xticklabels([])
333             if idx==0:
334                 ax.set_xlabel('CROW Stream Flow -- Last 30 days from ' + last_dt_str)
335             elif idx==len(axs)-1:
336                 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
337                 ax.set_xlabel('CROW Stream Flow -- Last 30 days from ' + last_dt_str)
338
339         savefig('/home/haines/rayleigh/img/crow_flow_last30days.png')
340
341
342     #######################################
343     # Last 7 days
344     #######################################
345     if plot_type=='latest':
346         print ' ... Last 7 days'
347         for idx, ax in enumerate(axs):
348             ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
349             ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
350             ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
351             ax.set_xticklabels([])
352             if idx==0:
353                 ax.set_xlabel('CROW Stream Flow -- Last 7 days from ' + last_dt_str)
354             elif idx==len(axs)-1:
355                 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
356                 ax.set_xlabel('CROW Stream Flow -- Last 7 days from ' + last_dt_str)
357                
358         savefig('/home/haines/rayleigh/img/crow_flow_last07days.png')
359
360
361     #######################################
362     # Last 1 day (24hrs)
363     #######################################
364     if plot_type=='latest':
365         print ' ... Last 1 days'
366
367         for idx, ax in enumerate(axs):
368             ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
369             ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
370             ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
371             ax.set_xticklabels([])
372             if idx==0:
373                 ax.set_xlabel('CROW  Stream Flow -- Last 24 hours from ' + last_dt_str)
374             elif idx==len(axs)-1:
375                 ax.xaxis.set_major_formatter( DateFormatter('%H') )
376                 ax.set_xlabel('CROW  Stream Flow -- Last 24 hours from ' + last_dt_str)
377
378         savefig('/home/haines/rayleigh/img/crow_flow_last01days.png')
379
380
381
382
Note: See TracBrowser for help on using the browser.