-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbogue_allwaves_plot.py
374 lines (310 loc) · 13.1 KB
/
bogue_allwaves_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env /opt/env/haines/dataproc/bin/python
# Last modified: Time-stamp: <2010-03-29 17:28:14 haines>
"""bogue_allwaves_plot"""
import os, sys
import datetime, time, dateutil.tz
import pycdf
import numpy
sys.path.append('/opt/env/haines/dataproc/raw2proc')
del(sys)
os.environ["MPLCONFIGDIR"]="/home/haines/.matplotlib/"
from pylab import figure, twinx, savefig, setp, getp, cm, colorbar
from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date
import procutil
print 'bogue_allwaves_plot ...'
prev_month, this_month, next_month = procutil.find_months(procutil.this_month())
# ncFile1='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_2008_01.nc'
# ncFile2='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_2008_02.nc'
ncFile1='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_'+prev_month.strftime('%Y_%m')+'.nc'
ncFile2='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_'+this_month.strftime('%Y_%m')+'.nc'
have_ncFile1 = os.path.exists(ncFile1)
have_ncFile2 = os.path.exists(ncFile2)
print ' ... loading data for graph from ...'
print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
# load data
if have_ncFile1 and have_ncFile2:
nc = pycdf.CDFMF((ncFile1, ncFile2))
elif not have_ncFile1 and have_ncFile2:
nc = pycdf.CDFMF((ncFile2,))
elif have_ncFile1 and not have_ncFile2:
nc = pycdf.CDFMF((ncFile1,))
else:
print ' ... both files do not exist -- NO DATA LOADED'
exit()
ncvars = nc.variables()
# print ncvars
es = nc.var('time')[:]
units = nc.var('time').units
dt = [procutil.es2dt(e) for e in es]
# set timezone info to UTC (since data from level1 should be in UTC!!)
dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
# return new datetime based on computer local
dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
dn = date2num(dt)
Hs = nc.var('Hs')[:]
Tp = nc.var('Tp')[:]
Tm = nc.var('Tm')[:]
#Hmax = nc.var('Hmax')[:]
Dp = nc.var('Dp')[:]
Dm = nc.var('Dm')[:]
nc.close()
# ancillary data to plot
ncFile1='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+prev_month.strftime('%Y_%m')+'.nc'
ncFile2='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+this_month.strftime('%Y_%m')+'.nc'
print ' ... loading ancillary data for graph from ...'
print ' ... ... ' + ncFile1 + ' ... ' + str(have_ncFile1)
print ' ... ... ' + ncFile2 + ' ... ' + str(have_ncFile2)
# load data
if have_ncFile1 and have_ncFile2:
nc = pycdf.CDFMF((ncFile1, ncFile2))
elif not have_ncFile1 and have_ncFile2:
nc = pycdf.CDFMF((ncFile2,))
elif have_ncFile1 and not have_ncFile2:
nc = pycdf.CDFMF((ncFile1,))
else:
print ' ... both files do not exist -- NO ANCILLARY DATA LOADED'
exit()
ncvars = nc.variables()
# print ncvars
es = nc.var('time')[:]
units = nc.var('time').units
dt_anc = [procutil.es2dt(e) for e in es]
# set timezone info to UTC (since data from level1 should be in UTC!!)
dt_anc = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt_anc]
# return new datetime based on computer local
dt_anc_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt_anc]
dn_anc = date2num(dt)
wd_anc = nc.var('wd')[:]
nc.close
# range for pcolor plots
cmin, cmax = (-0.5, 0.5)
# last dt in data for labels
dt1 = dt[-1]
dt2 = dt_local[-1]
diff = abs(dt1 - dt2)
if diff.days>0:
last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
else:
last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
+ dt2.strftime(" on %b %d, %Y")
fig = figure(figsize=(10, 8))
fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
#######################################
# Last 30 days
#######################################
print ' ... Last 30 days'
ax = fig.add_subplot(4,1,1)
axs = [ax]
# ibad = y <= -6999.
# y[ibad] = numpy.nan
# # just in case they are all NaNs make one real so it will plot
# if numpy.isnan(y).all():
# y[-1]=0.
# use masked array to hide NaN's on plot
# wd_anc = numpy.ma.masked_where(numpy.isnan(wd_anc), wd_anc)
# ax.plot returns a list of lines, so unpack tuple
(x, y) = procutil.addnan(dt_anc, wd_anc, maxdelta=2./24)
l1, = ax.plot_date(x, y, fmt='b-')
l1.set_label('Water Level (HAB)')
# right-hand side scale
ax2 = twinx(ax)
ax2.yaxis.tick_right()
# convert (lhs) meters to (rhs) feet
feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
ax2.set_ylim(feet)
ax2.set_ylabel('HAB (feet)')
ax.set_ylabel('HEIGHT ABOVE\nBOTTOM (m)')
# ax.set_ylim(2.,10.)
# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what
ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
ax.set_xticklabels([])
# this only moves the label not the tick labels
ax.xaxis.set_label_position('top')
ax.set_xlabel('Bogue ALL WAVES -- Last 30 days from ' + last_dt_str)
# ax2.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
# ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
# ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
# ax2.set_xticklabels([])
# legend
ls1 = l1.get_label()
leg = ax.legend((l1,), (ls1,), loc='upper left')
ltext = leg.get_texts() # all the text.Text instance in the legend
llines = leg.get_lines() # all the lines.Line2D instance in the legend
frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend
frame.set_facecolor('0.80') # set the frame face color to light gray
frame.set_alpha(0.5) # set alpha low to see through
setp(ltext, fontsize='small') # the legend text fontsize
setp(llines, linewidth=1.5) # the legend linewidth
# leg.draw_frame(False) # don't draw the legend frame
#######################################
#
ax = fig.add_subplot(4,1,2)
axs.append(ax)
# use masked array to hide NaN's on plot
# Hs = numpy.ma.masked_where(numpy.isnan(Hs), Hs)
# Hmax = numpy.ma.masked_where(numpy.isnan(Hmax), Hmax)
# ax.plot returns a list of lines, so unpack tuple
(x, y) = procutil.addnan(dt, Hs, maxdelta=2./24)
l1, = ax.plot_date(x, y, fmt='b-')
l1.set_label('Significant Wave Height (Hs)')
# l2, = ax.plot_date(dt, Hmax, fmt='c-')
# l2.set_label('Max Wave Height (Hmax)')
ax.set_ylabel('WAVE\nHEIGHT (m)')
# ax.set_ylim(2.,10.)
# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what
ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
ax.set_xticklabels([])
# right-hand side scale
ax2 = twinx(ax)
ax2.yaxis.tick_right()
# convert (lhs) meters to (rhs) feet
feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
ax2.set_ylim(feet)
ax2.set_ylabel('(feet)')
ax2.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
ax2.set_xticklabels([])
# legend
ls1 = l1.get_label()
# ls2 = l2.get_label()
# leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
leg = ax.legend((l1,), (ls1,), loc='upper left')
ltext = leg.get_texts() # all the text.Text instance in the legend
llines = leg.get_lines() # all the lines.Line2D instance in the legend
frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend
frame.set_facecolor('0.80') # set the frame face color to light gray
frame.set_alpha(0.5) # set alpha low to see through
setp(ltext, fontsize='small') # the legend text fontsize
setp(llines, linewidth=1.5) # the legend linewidth
# leg.draw_frame(False) # don't draw the legend frame
#######################################
#
ax = fig.add_subplot(4,1,3)
axs.append(ax)
# use masked array to hide NaN's on plot
# Tp = numpy.ma.masked_where(numpy.isnan(Tp), Tp)
# Tm = numpy.ma.masked_where(numpy.isnan(Tm), Tm)
# ax.plot returns a list of lines, so unpack tuple
(x, y) = procutil.addnan(dt, Tp, maxdelta=2./24)
l1, = ax.plot_date(x, y, fmt='b-')
l1.set_label('Peak Wave Period (Tp)')
(x, y) = procutil.addnan(dt, Tm, maxdelta=2./24)
l2, = ax.plot_date(x, y, fmt='c-')
l2.set_label('Mean Wave Period (Tm)')
ax.set_ylabel('WAVE\nPERIOD (s)')
# ax.set_ylim(2.,10.)
# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what
ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) # last minus 30 days to last
ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
ax.set_xticklabels([])
# legend
ls1 = l1.get_label()
ls2 = l2.get_label()
leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
ltext = leg.get_texts() # all the text.Text instance in the legend
llines = leg.get_lines() # all the lines.Line2D instance in the legend
frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend
frame.set_facecolor('0.80') # set the frame face color to light gray
frame.set_alpha(0.5) # set alpha low to see through
setp(ltext, fontsize='small') # the legend text fontsize
setp(llines, linewidth=1.5) # the legend linewidth
# leg.draw_frame(False) # don't draw the legend frame
#######################################
#
ax = fig.add_subplot(4,1,4)
axs.append(ax)
# use masked array to hide NaN's on plot
# Dp = numpy.ma.masked_where(numpy.isnan(Dp), Dp)
# Dm = numpy.ma.masked_where(numpy.isnan(Dm), Dm)
# ax.plot returns a list of lines, so unpack tuple
(x, y) = procutil.addnan(dt, Dp, maxdelta=2./24)
l1, = ax.plot_date(x, y, fmt='b-')
l1.set_label('Peak Wave Direction (Dp)')
(x, y) = procutil.addnan(dt, Dm, maxdelta=2./24)
l2, = ax.plot_date(x, y, fmt='c-')
l2.set_label('Mean Wave Direction (Dp)')
ax.set_ylabel('WAVE\nDIR (deg N)')
ax.set_ylim(0.,360.)
# first to last regardless of what
# ax.set_xlim(dt[0], dt[-1])
# last minus 30 days,
ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1]))
ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
ax.set_xlabel('Bogue ALL WAVES -- Last 30 days from ' + last_dt_str)
# legend
ls1 = l1.get_label()
ls2 = l2.get_label()
leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left')
ltext = leg.get_texts() # all the text.Text instance in the legend
llines = leg.get_lines() # all the lines.Line2D instance in the legend
frame = leg.get_frame() # the patch.Rectangle instance surrounding the legend
frame.set_facecolor('0.80') # set the frame face color to light gray
frame.set_alpha(0.5) # set alpha low to see through
setp(ltext, fontsize='small') # the legend text fontsize
setp(llines, linewidth=1.5) # the legend linewidth
# leg.draw_frame(False) # don't draw the legend frame
# save figure
savefig('/home/haines/rayleigh/img/bogue_allwaves_last30days.png')
#######################################
# Last 7 days
#######################################
print ' ... Last 7 days'
ax = axs[0]
ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
ax.set_xticklabels([])
ax.set_xlabel('Bogue ALL WAVES -- Last 7 days from ' + last_dt_str)
ax = axs[1]
ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
ax.set_xticklabels([])
ax = axs[2]
ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
ax.set_xticklabels([])
ax = axs[3]
ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1]))
ax.xaxis.set_major_locator( DayLocator(range(0,32,1)) )
ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) )
ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
ax.set_xlabel('Bogue ALL WAVES -- Last 7 days from ' + last_dt_str)
savefig('/home/haines/rayleigh/img/bogue_allwaves_last07days.png')
#######################################
# Last 1 day (24hrs)
#######################################
print ' ... Last 1 days'
ax = axs[0]
ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
ax.set_xticklabels([])
ax.set_xlabel('Bogue ALL WAVES -- Last 24 hours from ' + last_dt_str)
ax = axs[1]
ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
ax.set_xticklabels([])
ax = axs[2]
ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
ax.set_xticklabels([])
ax = axs[3]
ax.set_xlim(date2num(dt[-1])-1, date2num(dt[-1]))
ax.xaxis.set_major_locator( HourLocator(range(0,25,1)) )
ax.xaxis.set_minor_locator( MinuteLocator(range(0,61,30)) )
ax.xaxis.set_major_formatter( DateFormatter('%H') )
ax.set_xlabel('Bogue ALL WAVES -- Last 24 hours from ' + last_dt_str)
savefig('/home/haines/rayleigh/img/bogue_allwaves_last01days.png')