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

root/proc2plot/trunk/proc2plot/scratch/test_bogue_adcp_plot_month.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-10-16 12:00:20 haines>
3 """bogue_adcp_plot_month"""
4
5 # plot each month
6
7 import os, sys
8 import datetime, time, dateutil, dateutil.tz
9 import pycdf
10 import numpy
11
12 sys.path.append('/home/haines/nccoos/raw2proc')
13 del(sys)
14
15 os.environ["MPLCONFIGDIR"]="/home/haines/.matplotlib/"
16
17 from pylab import figure, twinx, savefig, setp, getp, cm, colorbar
18 from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date
19 import procutil
20
21 print 'bogue_adcp_plot ...'
22 yyyy_mm = '2008_05'
23 prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
24 # ncFile1='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_01.nc'
25 # ncFile2='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_02.nc'
26 # ncFile1='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+prev_month.strftime('%Y_%m')+'.nc'
27 # ncFile2='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+this_month.strftime('%Y_%m')+'.nc'
28
29 ncFile = '/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+yyyy_mm+'.nc'
30
31 # load data
32 print ' ... loading data for graph from ...'
33 print ' ... ... ' + ncFile
34 nc = pycdf.CDFMF((ncFile,))
35 ncvars = nc.variables()
36 # print ncvars
37 es = nc.var('time')[:]
38 units = nc.var('time').units
39 dt = [procutil.es2dt(e) for e in es]
40 # set timezone info to UTC (since data from level1 should be in UTC!!)
41 dt = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt]
42 # return new datetime based on computer local
43 dt_local = [e.astimezone(dateutil.tz.tzlocal()) for e in dt]
44 dn = date2num(dt)
45 z = nc.var('z')[:]
46 wd = nc.var('wd')[:]
47 wl = nc.var('wl')[:]
48 en = nc.var('en')[:]
49 u = nc.var('u')[:]
50 v = nc.var('v')[:]
51 nc.close()
52
53 # range for pcolor plots
54 cmin, cmax = (-0.5, 0.5)
55 # last dt in data for labels
56 dt1 = dt[-1]
57 dt2 = dt_local[-1]
58
59 diff = abs(dt1 - dt2)
60 if diff.days>0:
61     last_dt_str = dt1.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dt2.strftime("%H:%M %Z, %b %d") + ')'
62 else:
63     last_dt_str = dt1.strftime("%H:%M %Z") + ' (' + dt2.strftime("%H:%M %Z") + ')' \
64               + dt2.strftime(" on %b %d, %Y")
65
66 fig = figure(figsize=(10, 8))
67 fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
68
69 #######################################
70 # Plot month
71 #######################################
72 print ' ... start month plot'
73 ax = fig.add_subplot(4,1,1)
74 axs = [ax]
75
76 # use masked array to hide NaN's on plot
77 um = numpy.ma.masked_where(numpy.isnan(u), u)
78 pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax)
79 pc.set_label('True Eastward Current (m s-1)')
80 ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
81
82 # setup colorbar axes instance.
83 l,b,w,h = ax.get_position()
84 cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03])
85
86 cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar
87 cb.set_label('Current Velocity (m s-1)')
88 cb.ax.xaxis.set_label_position('top')
89 cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9])
90 cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4])
91
92 # ax.plot returns a list of lines, so unpack tuple
93 l1, = ax.plot_date(dt, wl, fmt='k-')
94 l1.set_label('Water Level')
95
96 ax.set_ylabel('Depth (m)')
97 ax.set_ylim(-8.,2.)
98 # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what 
99 ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
100 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
101 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
102 ax.set_xticklabels([])
103
104 # this only moves the label not the tick labels
105 ax.xaxis.set_label_position('top')
106 ax.set_xlabel('Bogue ADCP -- ' + yyyy_mm)
107
108 # right-hand side scale
109 ax2 = twinx(ax)
110 ax2.yaxis.tick_right()
111 # convert (lhs) meters to (rhs) feet
112 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
113 ax2.set_ylim(feet)
114 ax2.set_ylabel('Depth (ft)')
115
116 # legend
117 ls1 = l1.get_label()
118 leg = ax.legend((l1,), (ls1,), loc='upper left')
119 ltext  = leg.get_texts()  # all the text.Text instance in the legend
120 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
121 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
122 frame.set_facecolor('0.80')      # set the frame face color to light gray
123 frame.set_alpha(0.5)             # set alpha low to see through
124 setp(ltext, fontsize='small')    # the legend text fontsize
125 setp(llines, linewidth=1.5)      # the legend linewidth
126 # leg.draw_frame(False)           # don't draw the legend frame
127
128 #######################################
129 #
130 ax = fig.add_subplot(4,1,2)
131 axs.append(ax)
132
133 # use masked array to hide NaN's on plot
134 vm = numpy.ma.masked_where(numpy.isnan(v), v)
135 pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax)
136 pc.set_label('True Northward Current (m s-1)')
137 ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes)
138
139 # ax.plot returns a list of lines, so unpack tuple
140 l1, = ax.plot_date(dt, wl, fmt='k-')
141 l1.set_label('Water Level')
142
143 ax.set_ylabel('Depth (m)')
144 ax.set_ylim(-8.,2)
145
146 # ax.set_xlim(date2num(dt[0]), date2num(dt[-1]))
147 ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1)))
148 ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) )
149 ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) )
150 ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
151
152 ax.set_xlabel('Bogue ADCP -- ' + yyyy_mm)
153
154 # right-hand side scale
155 ax2 = twinx(ax)
156 ax2.yaxis.tick_right()
157 # convert (lhs) meters to (rhs) feet
158 feet = [procutil.meters2feet(val) for val in ax.get_ylim()]
159 ax2.set_ylim(feet)
160 ax2.set_ylabel('Depth (ft)')
161
162 # legend
163 ls1 = l1.get_label()
164 leg = ax.legend((l1,), (ls1,), loc='upper left')
165 ltext  = leg.get_texts()  # all the text.Text instance in the legend
166 llines = leg.get_lines()  # all the lines.Line2D instance in the legend
167 frame  = leg.get_frame()  # the patch.Rectangle instance surrounding the legend
168 frame.set_facecolor('0.80')      # set the frame face color to light gray
169 frame.set_alpha(0.5)             # set alpha low to see through
170 setp(ltext, fontsize='small')    # the legend text fontsize
171 setp(llines, linewidth=1.5)      # the legend linewidth
172 # leg.draw_frame(False)           # don't draw the legend frame
173
174 # save figure
175 savefig('/home/haines/rayleigh/img/bogue/bogue_adcp_'+yyyy_mm+'.png')
176
Note: See TracBrowser for help on using the browser.