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

root/proc2plot/trunk/proc2plot/jpier_met1_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-09-14 11:17:11 haines>
3 """jpier_met_plot1"""
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 'jpier_met_plot1 ...'
20 prev_month, this_month, next_month = procutil.find_months(procutil.this_month())
21 #ncFile1='/seacoos/data/nccoos/level1/jpier/met/jpier_met_2008_02.nc'
22 #ncFile2='/seacoos/data/nccoos/level1/jpier/met/jpier_met_2008_03.nc'
23
24 ncFile1='/seacoos/data/nccoos/level1/jpier/met/jpier_met_'+prev_month.strftime('%Y_%m')+'.nc'
25 ncFile2='/seacoos/data/nccoos/level1/jpier/met/jpier_met_'+this_month.strftime('%Y_%m')+'.nc'
26
27 # load data
28
29 have_ncFile1 = os.path.exists(ncFile1)
30 have_ncFile2 = os.path.exists(ncFile2)
31
32 print ' ... loading data for graph from ...'
33 print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
34 print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
35
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 ap = nc.var('air_pressure')[:]
57 at = nc.var('air_temp')[:]
58 dp = nc.var('dew_temp')[:]
59 h = nc.var('humidity')[:]
60 p = nc.var('rainfall_day')[:]
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
74 fig = figure(figsize=(10, 8))
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
82 ax = fig.add_subplot(4,1,1)
83 axs = [ax]
84
85 # use masked array to hide NaN's on plot
86 ap = numpy.ma.masked_where(numpy.isnan(ap), ap)
87
88 # ax.plot returns a list of lines, so unpack tuple
89 l1, = ax.plot_date(dt, ap, fmt='b-')
90 l1.set_label('Barometric Pressure')
91
92 ax.set_ylabel('Pressure (mbar)')
93 ax.set_ylim(980.,1040.)
94 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
95 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
96 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
97 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
98 ax.set_xticklabels([])
99
100 # this only moves the label not the tick labels
101 ax.xaxis.set_label_position('top')
102 ax.set_xlabel('JPIER Met -- Last 30 days from ' + last_dt_str)
103
104 # right-hand side scale
105 ax2 = twinx(ax)
106 ax2.yaxis.tick_right()
107 # convert (lhs) mbar to (rhs) in Hg
108 inHG = [procutil.millibar2inches_Hg(val) for val in ax.get_ylim()]
109 ax2.set_ylim(inHG)
110 ax2.set_ylabel('Pressure (in Hg)')
111
112 # legend
113 ls1 = l1.get_label()
114 leg = ax.legend((l1,), (ls1,), loc='upper left')
115 ltext  = leg.get_texts()  # all the text.Text instance in the legend
116 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
117 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
118 frame.set_facecolor('0.80')      # set the frame face color to light gray
119 frame.set_alpha(0.5)             # set alpha low to see through
120 setp(ltext, fontsize='small')    # the legend text fontsize
121 setp(llines, linewidth=1.5)      # the legend linewidth
122 # leg.draw_frame(False)           # don't draw the legend frame
123
124 #######################################
125 #
126 ax = fig.add_subplot(4,1,2)
127 axs.append(ax)
128
129 # use masked array to hide NaN's on plot
130 at = numpy.ma.masked_where(numpy.isnan(at), at)
131
132
133 # ax.plot returns a list of lines, so unpack tuple
134 l1, = ax.plot_date(dt, at, fmt='b-')
135 l1.set_label('Air Temperature')
136
137 l2, = ax.plot_date(dt, dp, fmt='g-')
138 l2.set_label('Dew Point')
139
140 ax.set_ylabel('Temp (deg C)')
141 ax.set_ylim(-10.,40.)
142 # ax.set_xlim(dt[0], dt[-1])
143 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
144 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
145 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
146 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
147 ax.set_xticklabels([])
148
149 # right-hand side scale
150 ax2 = twinx(ax)
151 ax2.yaxis.tick_right()
152 # convert (lhs) deg C to (rhs) deg F
153
154 f = [procutil.celsius2fahrenheit(val) for val in ax.get_ylim()]
155 ax2.set_ylim(f)
156 ax2.set_ylabel('Temp (deg F)')
157
158 # legend
159 ls1 = l1.get_label()
160 ls2 = l2.get_label()
161 leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
162 ltext  = leg.get_texts()  # all the text.Text instance in the legend
163 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
164 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
165 frame.set_facecolor('0.80')      # set the frame face color to light gray
166 frame.set_alpha(0.5)             # set alpha low to see through
167 setp(ltext, fontsize='small')    # the legend text fontsize
168 setp(llines, linewidth=1.5)      # the legend linewidth
169 # leg.draw_frame(False)           # don't draw the legend frame
170
171 #######################################
172 #
173 ax = fig.add_subplot(4,1,3)
174 axs.append(ax)
175
176 # use masked array to hide NaN's on plot
177 h = numpy.ma.masked_where(numpy.isnan(h), h)
178
179
180 # ax.plot returns a list of lines, so unpack tuple
181 l1, = ax.plot_date(dt, h, fmt='b-')
182 l1.set_label('Relative Humidity')
183
184
185 ax.set_ylabel('RHUM (%)')
186 ax.set_ylim(0.,100.)
187 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
188 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
189 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
190 ax.set_xticklabels([])
191
192 # right-hand side scale
193 ax2 = twinx(ax)
194 ax2.yaxis.tick_right()
195 # no cenversion needed
196 ylim = [(val) for val in ax.get_ylim()]
197 ax2.set_ylim(ylim)
198 ax2.set_ylabel('RHUM (%)')
199
200
201
202 # legend
203 ls1 = l1.get_label()
204 leg = ax.legend((l1,), (ls1,), loc='upper left')
205 ltext  = leg.get_texts()  # all the text.Text instance in the legend
206 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
207 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
208 frame.set_facecolor('0.80')      # set the frame face color to light gray
209 frame.set_alpha(0.5)             # set alpha low to see through
210 setp(ltext, fontsize='small')    # the legend text fontsize
211 setp(llines, linewidth=1.5)      # the legend linewidth
212 # leg.draw_frame(False)           # don't draw the legend frame
213
214 #######################################
215 #
216 ax = fig.add_subplot(4,1,4)
217 axs.append(ax)
218
219 # use masked array to hide NaN's on plot
220 p = numpy.ma.masked_where(numpy.isnan(p), p)
221
222 # ax.plot returns a list of lines, so unpack tuple
223 l1, = ax.plot_date(dt, p, fmt='b-')
224 l1.set_label('Daily Precipitation')
225
226 ax.set_ylabel('Rain (mm/day)')
227 ax.set_ylim(0.,40.)
228 # last minus 30 days,
229 ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
230 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
231 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
232 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
233
234 # right-hand side scale
235 ax2 = twinx(ax)
236 ax2.yaxis.tick_right()
237 # convert (lhs) mm/day to (rhs) in/day
238 ylim = [procutil.millimeters2inches(val) for val in ax.get_ylim()]
239 ax2.set_ylim(ylim)
240 ax2.set_ylabel('Rain (in/day)')
241
242
243 ax.set_xlabel('JPIER Met -- Last 30 days from ' + last_dt_str)
244
245 # legend
246 ls1 = l1.get_label()
247 leg = ax.legend((l1,), (ls1,), loc='upper left')
248 ltext  = leg.get_texts()  # all the text.Text instance in the legend
249 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
250 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
251 frame.set_facecolor('0.80')      # set the frame face color to light gray
252 frame.set_alpha(0.5)             # set alpha low to see through
253 setp(ltext, fontsize='small')    # the legend text fontsize
254 setp(llines, linewidth=1.5)      # the legend linewidth
255 # leg.draw_frame(False)           # don't draw the legend frame
256
257 # save figure
258 savefig('/home/haines/rayleigh/img/jpier_met_last30days.png')
259
260 #######################################
261 # Last 7 days
262 #######################################
263
264 print ' ... Last 7 days'
265 ax = axs[0]
266 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
267 ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
268 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
269 ax.set_xticklabels([])
270 ax.set_xlabel('JPIER Met -- Last 7 days from ' + last_dt_str)
271
272 ax = axs[1]
273 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
274 ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) )
275 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
276 ax.set_xticklabels([])
277
278 ax = axs[2]
279 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
280 ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) )
281 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
282 ax.set_xticklabels([])
283
284 ax = axs[3]
285 ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
286 ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) )
287 ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
288 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
289 ax.set_xlabel('JPIER Met -- Last 7 days from ' + last_dt_str)
290
291 savefig('/home/haines/rayleigh/img/jpier_met_last07days.png')
292
293 #######################################
294 # Last 1 day (24hrs)
295 #######################################
296
297 print ' ... Last 1 days'
298
299 ax = axs[0]
300 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
301 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
302 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
303 ax.set_xticklabels([])
304 ax.set_xlabel('JPIER Met -- Last 24 hours from ' + last_dt_str)
305
306 ax = axs[1]
307 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
308 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
309 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
310 ax.set_xticklabels([])
311
312 ax = axs[2]
313 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
314 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
315 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
316 ax.set_xticklabels([])
317
318 ax = axs[3]
319 ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
320 ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
321 ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
322 ax.xaxis.set_major_formatter( DateFormatter('%H') )
323 ax.set_xlabel('JPIER Met -- Last 24 hours from ' + last_dt_str)
324
325 savefig('/home/haines/rayleigh/img/jpier_met_last01days.png')
326
327
Note: See TracBrowser for help on using the browser.