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

root/proc2plot/trunk/proc2plot/spin/spin_lsrb_adcp_plot_month.py

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