1 |
#!/usr/bin/env python |
---|
2 |
# Last modified: Time-stamp: <2009-10-29 17:12:47 haines> |
---|
3 |
"""jpier_adcp_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 'jpier_adcp_plot ...' |
---|
20 |
prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) |
---|
21 |
# ncFile1='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_2008_01.nc' |
---|
22 |
# ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_2008_02.nc' |
---|
23 |
ncFile1='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+prev_month.strftime('%Y_%m')+'.nc' |
---|
24 |
ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+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 |
wl = nc.var('wl')[:] |
---|
56 |
u = nc.var('u')[:] |
---|
57 |
v = nc.var('v')[:] |
---|
58 |
nc.close() |
---|
59 |
|
---|
60 |
# range for pcolor plots |
---|
61 |
cmin, cmax = (-0.5, 0.5) |
---|
62 |
# last dt in data for labels |
---|
63 |
dt1 = dt[-1] |
---|
64 |
dt2 = dt_local[-1] |
---|
65 |
|
---|
66 |
diff = abs(dt1 - dt2) |
---|
67 |
if diff.days>0: |
---|
68 |
last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')' |
---|
69 |
else: |
---|
70 |
last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \ |
---|
71 |
+ dt2.strftime(" on %b %d, %Y") |
---|
72 |
|
---|
73 |
fig = figure(figsize=(10, 8)) |
---|
74 |
fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) |
---|
75 |
|
---|
76 |
####################################### |
---|
77 |
# Last 30 days |
---|
78 |
####################################### |
---|
79 |
print ' ... Last 30 days' |
---|
80 |
ax = fig.add_subplot(4,1,1) |
---|
81 |
axs = [ax] |
---|
82 |
|
---|
83 |
# testing procutil.addnan for 2-d |
---|
84 |
# dto = dt |
---|
85 |
# (dt, u) = procutil.addnan(dt, u) |
---|
86 |
# dn = date2num(dt) |
---|
87 |
|
---|
88 |
# use masked array to hide NaN's on plot |
---|
89 |
um = numpy.ma.masked_where(numpy.isnan(u), u) |
---|
90 |
pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) |
---|
91 |
pc.set_label('True Eastward Current (m s-1)') |
---|
92 |
ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) |
---|
93 |
|
---|
94 |
# setup colorbar axes instance. |
---|
95 |
l,b,w,h = ax.get_position().bounds |
---|
96 |
cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) |
---|
97 |
|
---|
98 |
cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar |
---|
99 |
cb.set_label('Current Velocity (m s-1)') |
---|
100 |
cb.ax.xaxis.set_label_position('top') |
---|
101 |
cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) |
---|
102 |
cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) |
---|
103 |
|
---|
104 |
# ax.plot returns a list of lines, so unpack tuple |
---|
105 |
(x, y) = procutil.addnan(dt, wl, maxdelta=2./24) |
---|
106 |
l1, = ax.plot_date(x, y, fmt='k-') |
---|
107 |
l1.set_label('Water Level') |
---|
108 |
|
---|
109 |
ax.set_ylabel('Depth (m)') |
---|
110 |
ax.set_ylim(-14.,2.) |
---|
111 |
# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what |
---|
112 |
ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last |
---|
113 |
ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) |
---|
114 |
ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) |
---|
115 |
ax.set_xticklabels([]) |
---|
116 |
|
---|
117 |
# this only moves the label not the tick labels |
---|
118 |
ax.xaxis.set_label_position('top') |
---|
119 |
ax.set_xlabel('Jpier AWAC -- Last 30 days from ' + last_dt_str) |
---|
120 |
|
---|
121 |
# right-hand side scale |
---|
122 |
ax2 = twinx(ax) |
---|
123 |
ax2.yaxis.tick_right() |
---|
124 |
# convert (lhs) meters to (rhs) feet |
---|
125 |
feet = [procutil.meters2feet(val) for val in ax.get_ylim()] |
---|
126 |
ax2.set_ylim(feet) |
---|
127 |
ax2.set_ylabel('Depth (ft)') |
---|
128 |
|
---|
129 |
# legend |
---|
130 |
ls1 = l1.get_label() |
---|
131 |
leg = ax.legend((l1,), (ls1,), loc='upper left') |
---|
132 |
ltext = leg.get_texts() # all the text.Text instance in the legend |
---|
133 |
llines = leg.get_lines() # all the lines.Line2D instance in the legend |
---|
134 |
frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend |
---|
135 |
frame.set_facecolor('0.80') # set the frame face color to light gray |
---|
136 |
frame.set_alpha(0.5) # set alpha low to see through |
---|
137 |
setp(ltext, fontsize='small') # the legend text fontsize |
---|
138 |
setp(llines, linewidth=1.5) # the legend linewidth |
---|
139 |
# leg.draw_frame(False) # don't draw the legend frame |
---|
140 |
|
---|
141 |
####################################### |
---|
142 |
# |
---|
143 |
ax = fig.add_subplot(4,1,2) |
---|
144 |
axs.append(ax) |
---|
145 |
|
---|
146 |
# use masked array to hide NaN's on plot |
---|
147 |
vm = numpy.ma.masked_where(numpy.isnan(v), v) |
---|
148 |
pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) |
---|
149 |
pc.set_label('True Northward Current (m s-1)') |
---|
150 |
ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) |
---|
151 |
|
---|
152 |
# ax.plot returns a list of lines, so unpack tuple |
---|
153 |
(x, y) = procutil.addnan(dt, wl, maxdelta=2./24) |
---|
154 |
l1, = ax.plot_date(x, y, fmt='k-') |
---|
155 |
l1.set_label('Water Level') |
---|
156 |
|
---|
157 |
ax.set_ylabel('Depth (m)') |
---|
158 |
ax.set_ylim(-14.,2.) |
---|
159 |
# first to last regardless of what |
---|
160 |
# ax.set_xlim(dt[0], dt[-1]) |
---|
161 |
# last minus 30 days, |
---|
162 |
ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) |
---|
163 |
ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) |
---|
164 |
ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) |
---|
165 |
ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) |
---|
166 |
|
---|
167 |
ax.set_xlabel('Jpier AWAC -- Last 30 days from ' + last_dt_str) |
---|
168 |
|
---|
169 |
# right-hand side scale |
---|
170 |
ax2 = twinx(ax) |
---|
171 |
ax2.yaxis.tick_right() |
---|
172 |
# convert (lhs) meters to (rhs) feet |
---|
173 |
feet = [procutil.meters2feet(val) for val in ax.get_ylim()] |
---|
174 |
ax2.set_ylim(feet) |
---|
175 |
ax2.set_ylabel('Depth (ft)') |
---|
176 |
|
---|
177 |
# legend |
---|
178 |
ls1 = l1.get_label() |
---|
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 |
# save figure |
---|
190 |
savefig('/home/haines/rayleigh/img/jpier_adcp_last30days.png') |
---|
191 |
|
---|
192 |
####################################### |
---|
193 |
# Last 7 days |
---|
194 |
####################################### |
---|
195 |
|
---|
196 |
print ' ... Last 7 days' |
---|
197 |
ax = axs[0] |
---|
198 |
ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1])) |
---|
199 |
ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) ) |
---|
200 |
ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) |
---|
201 |
ax.set_xticklabels([]) |
---|
202 |
ax.set_xlabel('Jpier AWAC -- Last 7 days from ' + last_dt_str) |
---|
203 |
|
---|
204 |
ax = axs[1] |
---|
205 |
ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1])) |
---|
206 |
ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) ) |
---|
207 |
ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) |
---|
208 |
ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) |
---|
209 |
ax.set_xlabel('Jpier AWAC -- Last 7 days from ' + last_dt_str) |
---|
210 |
|
---|
211 |
savefig('/home/haines/rayleigh/img/jpier_adcp_last07days.png') |
---|
212 |
|
---|
213 |
####################################### |
---|
214 |
# Last 1 day (24hrs) |
---|
215 |
####################################### |
---|
216 |
|
---|
217 |
print ' ... Last 1 days' |
---|
218 |
|
---|
219 |
ax = axs[0] |
---|
220 |
ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1])) |
---|
221 |
ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) ) |
---|
222 |
ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) ) |
---|
223 |
ax.set_xticklabels([]) |
---|
224 |
ax.set_xlabel('Jpier AWAC -- Last 24 hours from ' + last_dt_str) |
---|
225 |
|
---|
226 |
ax = axs[1] |
---|
227 |
ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1])) |
---|
228 |
ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) ) |
---|
229 |
ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) ) |
---|
230 |
ax.xaxis.set_major_formatter( DateFormatter('%H') ) |
---|
231 |
ax.set_xlabel('Jpier AWAC -- Last 24 hours from ' + last_dt_str) |
---|
232 |
|
---|
233 |
savefig('/home/haines/rayleigh/img/jpier_adcp_last01days.png') |
---|
234 |
|
---|