Index: proc2plot/trunk/proc2plot/billymitchell_sodar_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/billymitchell_sodar_plot.py (revision 326) @@ -1,0 +1,683 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-12 13:51:06 haines> + +"""billymitchell_sodar_plot""" + +################################################# +# [1a] search and replace XXXX with platform id +# [1b] search and replace YYYY with sensor id +# [1c] search and replace ZZZZ plot titles +################################################# + +# plot each month + +import os +# import sys, glob, re +import datetime, time, dateutil, 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 + +def timeseries(pi, si, yyyy_mm, plot_type='latest'): + print 'billymitchell_sodar1_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ################################ + # [2a] load primary data file + ################################ + + ncFile1='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+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) + + # open netcdf 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) + + + ################################ + # [2b] specify variables + ################################ + z = nc.var('z')[:] + # convert cm/s to m/s + uu = nc.var('u')[:] + vv = nc.var('v')[:] + ww = nc.var('w')[:] + sigw = nc.var('sigw')[:] + echo = nc.var('bck')[:] + + nc.close() + + # retain original dt for addnan + dto = dt + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot setup + ####################################### + + fig = figure(figsize=(10, 10)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + + ax = fig.add_subplot(5,1,1) + axs = [ax] + + # range for horizontal wind plots + cmin, cmax = (-20., 20.) + # print "%s : %g %g" % ('uv wind', cmin, cmax) + # use masked array to hide NaN's on plot + (dt, uu) = procutil.addnan(dto, uu) + dn = date2num(dt) + um = numpy.ma.masked_where(numpy.isnan(uu), uu) + pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) + pc.set_label('True Eastward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + + # setup colorbar axes instance. + l,b,w,h = ax.get_position().bounds + cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Wind Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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('BILLY MITCHELL SODAR -- ' + yyyy_mm_str) + + # 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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,2) + axs.append(ax) + + # use masked array to hide NaN's on plot + (dt, vv) = procutil.addnan(dto, vv) + dn = date2num(dt) + vm = numpy.ma.masked_where(numpy.isnan(vv), vv) + pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) + pc.set_label('True Northward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,3) + axs.append(ax) + + # range for horizontal wind plots + cmin, cmax = (-2., 2.) + # print "%s : %g %g" % ('w wind', cmin, cmax) + + # use masked array to hide NaN's on plot + (dt, ww) = procutil.addnan(dto, ww) + dn = date2num(dt) + wm = numpy.ma.masked_where(numpy.isnan(ww), ww) + pc = ax.pcolor(dn, z, wm.T, vmin=cmin, vmax=cmax) + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + + # setup colorbar axes instance. + l,b,w,h = ax.get_position().bounds + cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Upward Wind Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,4) + axs.append(ax) + + cmin, cmax = (0., 2.) + # print "%s : %g %g" % ('sigw', cmin, cmax) + + # use masked array to hide NaN's on plot + (dt, sigw) = procutil.addnan(dto, sigw) + dn = date2num(dt) + sm = numpy.ma.masked_where(numpy.isnan(sigw), sigw) + pc = ax.pcolor(dn, z, sm.T, vmin=cmin, vmax=cmax) + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + + # setup colorbar axes instance. + l,b,w,h = ax.get_position().bounds + cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Std. Dev. of Vertical Wind (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.0, 0.2, 0.4, 0.6, 0.8]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) + cb.ax.set_xticklabels([xtl[0], xtl[2], xtl[4], xtl[6], xtl[8]]) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,5) + axs.append(ax) + + + # cmin, cmax = (0., 1000.) + cmin, cmax = (2., 6.) + # print "%s : %g %g" % ('echo', cmin, cmax) + + # use masked array to hide NaN's on plot + (dt, echo) = procutil.addnan(dto, numpy.log10(echo)) + dn = date2num(dt) + em = numpy.ma.masked_where(numpy.isnan(echo), echo) + pc = ax.pcolor(dn, z, em.T, vmin=cmin, vmax=cmax) + # pc = ax.pcolor(dn, z, em.T) + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + + # setup colorbar axes instance. + l,b,w,h = ax.get_position().bounds + cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('log10 Backscatter') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.0, 1.0]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([str(cmin), str(cmax)]) + + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BILLY MITCHELL SODAR -- ' + yyyy_mm_str) + # save figure + + # save figure for this month + ofn = '/home/haines/rayleigh/img/billymitchell/billymitchell_sodar1_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('BILLY MITCHELL SODAR -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BILLY MITCHELL SODAR -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/billymitchell_sodar1_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BILLY MITCHELL SODAR -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BILLY MITCHELL SODAR -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/billymitchell_sodar1_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BILLY MITCHELL SODAR -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('BILLY MITCHELL SODAR -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/billymitchell_sodar1_last01days.png') + + +def wind_vectors(pi, si, yyyy_mm, plot_type='latest'): + print 'billymitchell_arrows_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ################################ + # [2a] load primary data file + ################################ + + ncFile1='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+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) + + # open netcdf 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) + + + ################################ + # [2b] specify variables + ################################ + z = nc.var('z')[:] + # convert cm/s to m/s + uu = nc.var('u')[:] + vv = nc.var('v')[:] + ww = nc.var('w')[:] + sigw = nc.var('sigw')[:] + echo = nc.var('bck')[:] + + nc.close() + + # retain original dt for addnan + dto = dt + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot setup + ####################################### + + fig = figure(figsize=(10, 10)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + + ax = fig.add_subplot(1,1,1) + axs = [ax] + + # range for horizontal wind plots + # cmin, cmax = (0., 20.) + # print "%s : %g %g" % ('uv wind', cmin, cmax) + # use masked array to hide NaN's on plot + (dt, uu) = procutil.addnan(dto, uu) + dn = date2num(dt) + um = numpy.ma.masked_where(numpy.isnan(uu), uu) + + (dt, vv) = procutil.addnan(dto, vv) + vm = numpy.ma.masked_where(numpy.isnan(vv), vv) + wspd = numpy.sqrt(um*um + vm*vm) + + X,Y = numpy.meshgrid(dn, z) + + q1 = ax.quiver(X, Y, um.T, vm.T, wspd.T, units='inches', scale=40) + qk = ax.quiverkey(q1, 0.1, 0.8, 20, r'20 m s-1') + + ax.set_ylabel('Height (m)') + + # setup colorbar axes instance. + l,b,w,h = ax.get_position().bounds + cax = fig.add_axes([l+0.02, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(q1, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Wind Velocity (m s-1)') + + # lost control of colorbar ... ??? + # + # cb.ax.xaxis.set_label_position('top') + # cb.ax.set_xticks([0.0, 0.5, 1.0]) + # xtl = numpy.round(numpy.linspace(cmin, cmax, 11), decimals=0) + # cb.ax.set_xticklabels([xtl[0], xtl[5], xtl[10]]) + + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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('bottom') + ax.set_xlabel('BILLY MITCHELL Wind Profile -- ' + yyyy_mm_str) + + # 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('Height (ft)') + + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BILLY MITCHELL Wind Profile -- Last 24 hours from ' + last_dt_str) + if idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('BILLY MITCHELL Wind Profile -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/billymitchell_wind_last01days.png') + + +def wind_barbs(pi, si, yyyy_mm, plot_type='latest'): + print 'billymitchell_arrows_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ################################ + # [2a] load primary data file + ################################ + + ncFile1='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/billymitchell/sodar1/billymitchell_sfas_'+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) + + # open netcdf 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) + + + ################################ + # [2b] specify variables + ################################ + z = nc.var('z')[:] + # convert cm/s to m/s + uu = nc.var('u')[:] + vv = nc.var('v')[:] + ww = nc.var('w')[:] + sigw = nc.var('sigw')[:] + echo = nc.var('bck')[:] + + nc.close() + + # retain original dt for addnan + dto = dt + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot setup + ####################################### + + fig = figure(figsize=(10, 10)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + + ax = fig.add_subplot(1,1,1) + axs = [ax] + + # range for horizontal wind plots + # cmin, cmax = (0., 20.) + # print "%s : %g %g" % ('uv wind', cmin, cmax) + # use masked array to hide NaN's on plot + (dt, uu) = procutil.addnan(dto, uu) + dn = date2num(dt) + um = numpy.ma.masked_where(numpy.isnan(uu), uu) + + (dt, vv) = procutil.addnan(dto, vv) + vm = numpy.ma.masked_where(numpy.isnan(vv), vv) + wspd = numpy.sqrt(um*um + vm*vm) + + X,Y = numpy.meshgrid(dn, z) + + q1 = ax.barbs(X, Y, um.T, vm.T, wspd.T) + # qk = ax.quiverkey(q1, 0.1, 0.8, 20, r'20 m s-1') + + ax.set_ylabel('Height (m)') + + # setup colorbar axes instance. + l,b,w,h = ax.get_position().bounds + cax = fig.add_axes([l+0.02, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(q1, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Wind Velocity (m s-1)') + + # lost control of colorbar ... ??? + # + # cb.ax.xaxis.set_label_position('top') + # cb.ax.set_xticks([0.0, 0.5, 1.0]) + # xtl = numpy.round(numpy.linspace(cmin, cmax, 11), decimals=0) + # cb.ax.set_xticklabels([xtl[0], xtl[5], xtl[10]]) + + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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('bottom') + ax.set_xlabel('BILLY MITCHELL Wind Profile -- ' + yyyy_mm_str) + + # 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('Height (ft)') + + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BILLY MITCHELL Wind Profile -- Last 24 hours from ' + last_dt_str) + if idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('BILLY MITCHELL Wind Profile -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/billymitchell_windbarbs_last01days.png') + Index: proc2plot/trunk/proc2plot/bogue_adcp_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/bogue_adcp_plot.py (revision 326) @@ -1,0 +1,289 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-12 13:37:36 haines> + +"""bogue_adcp_plot""" + +################################################# +# [1a] search and replace XXXX with platform id +# [1b] search and replace YYYY with sensor id +# [1c] search and replace ZZZZ plot titles +################################################# + +# plot each month + +import os +# import sys, glob, re +import datetime, time, dateutil, 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 + +def timeseries(pi, si, yyyy_mm, plot_type='latest'): + """ + """ + + + print 'bogue_adcp_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ################################ + # [2a] load primary data file + ################################ + + 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' + + 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) + + # open netcdf 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) + + ################################ + # [2b] specify variables + ################################ + z = nc.var('z')[:] + wd = nc.var('wd')[:] + wl = nc.var('wl')[:] + # en = nc.var('en')[:] + u = nc.var('u')[:] + v = nc.var('v')[:] + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot setup + ####################################### + # range for pcolor plots + cmin, cmax = (-0.5, 0.5) + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # replace gaps in time with NaN + (x, y) = procutil.addnan(dt, u, maxdelta=2./24) + dnx = date2num(x) + # use masked array to hide NaN's on plot + um = numpy.ma.masked_where(numpy.isnan(y), y) + pc = ax.pcolor(dnx, z, um.T, vmin=cmin, vmax=cmax) + pc.set_label('True Eastward Current (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + + # setup colorbar axes instance + l,b,w,h = ax.get_position().bounds + cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Current Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, wl, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='k-') + l1.set_label('Water Level') + + ax.set_ylabel('Depth (m)') + ax.set_ylim(-10., 2.) + # ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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 Current Profile -- ' + yyyy_mm_str) + + # 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('Depth (ft)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # replace gaps in time with NaN + (x, y) = procutil.addnan(dt, v, maxdelta=2./24) + dnx = date2num(x) + # use masked array to hide NaN's on plot + vm = numpy.ma.masked_where(numpy.isnan(y), y) + # (x, y) = procutil.addnan(dt, vm, maxdelta=2./24) + pc = ax.pcolor(dnx, z, vm.T, vmin=cmin, vmax=cmax) + pc.set_label('True Northward Current (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, wl, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='k-') + l1.set_label('Water Level') + + ax.set_ylabel('Depth (m)') + ax.set_ylim(-10.,2.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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 Current Profile -- ' + yyyy_mm_str) + + # 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('Depth (ft)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + + # 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 + + # save figure for this month + ofn = '/home/haines/rayleigh/img/bogue/bogue_adcp_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('BOGUE Current Profiles -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BOGUE Current Profiles -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_adcp_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BOGUE Current Profiles -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BOGUE Current Profiles -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_adcp_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BOGUE Current Profiles -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('BOGUE Current Profiles -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_adcp_last01days.png') + Index: proc2plot/trunk/proc2plot/bogue_allwaves_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/bogue_allwaves_plot.py (revision 326) @@ -1,0 +1,374 @@ +#!/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') + + Index: proc2plot/trunk/proc2plot/bogue_dspec_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/bogue_dspec_plot.py (revision 326) @@ -1,0 +1,367 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2009-10-29 13:47:50 haines> +"""bogue_dspec_plot""" + +import os, sys +import datetime, time, dateutil, 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, twiny, savefig, setp, getp, cm, colorbar +from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date +from matplotlib.ticker import MaxNLocator, FormatStrFormatter, ScalarFormatter +import procutil + +def which_to_plot(odir, ncfn): + """ + Finds which timestamp in netCDF data file (ncfn) does not have a + corresponding directional spectrum plot png in output dir (odir) + + :Parameters: + odir : string, path to location of png's + ncfn : string, filename and path to netCDF file + + :Returns: + j_seq : integer sequence, for indices of data to plot + + """ + + nc = pycdf.CDF(ncFile1) + # nc = pycdf.CDFMF((ncFile1, ncFile2)) + ncvars = nc.variables() + # print ncvars + es = nc.var('time')[:] + units = nc.var('time').units + dt = [procutil.es2dt(e) for e in es] + dts = [d.strftime('%Y_%m_%d_%H%M') for d in dt] + # list all pngs + import glob + gs = os.path.join(odir, '*.png') + all_pngs = glob.glob(gs) + ap = ''.join(all_pngs) + # find index of dts not in all_pngs + j_seq = [j for j in range(len(dts)) if ap.find(dts[j]) == -1] + # return index values to plot (j) + return j_seq + +print 'bogue_dspec_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/home/haines/test_data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_2008_03.nc' +# ncFile2='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_2008_03.nc' +# ncFile1='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile1='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_'+this_month.strftime('%Y_%m')+'.nc' +odir = os.path.join('/seacoos/data/nccoos/level3/bogue/adcpwaves/dspec', this_month.strftime('%Y_%m')) +if not os.path.exists(odir): + os.mkdir(odir) + +j_seq = which_to_plot(odir, ncFile1) +# print j_seq + +if not j_seq: + j_seq = [-1] + +# load data +print ' ... loading data for graph from ...' +print ' ... ... ' + ncFile1 +# print ' ... ... ' + ncFile2 +for j in j_seq: + nc = pycdf.CDF(ncFile1) + # nc = pycdf.CDFMF((ncFile1, ncFile2)) + 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) + f = nc.var('f')[:] + d = nc.var('d')[:] + Sxx = nc.var('Sxx')[j] + Sf = nc.var('Sf')[j] + Stheta = nc.var('Stheta')[j] + Stheta_wind = nc.var('Stheta_wind')[j] + Stheta_swell = nc.var('Stheta_swell')[j] + Tp = nc.var('Tp')[j] + Tpw = nc.var('Tp_wind')[j] + Tps = nc.var('Tp_swell')[j] + Dp = nc.var('Dp')[j] + Dpw = nc.var('Dp_wind')[j] + Dps = nc.var('Dp_swell')[j] + Hs = nc.var('Hs')[:] + Hss = nc.var('Hs_swell')[:] + Hsw = nc.var('Hs_wind')[:] + nc.close() + + print '... ... ' + dt[j].strftime('%Y_%m_%d_%H%M') + + # range for pcolor plots + cmin, cmax = (0.0, 0.05) + # last dt in data for labels + dt1 = dt[j] + dt2 = dt_local[j] + + 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") + + fn_dt_str = dt1.strftime("%Y_%m_%d_%H%M") + + fig = figure(figsize=(9, 7)) + + ####################################### + # full directional spectrum S(f,d) + ####################################### + # print ' ... Sxx' + ax = fig.add_axes((.1,.4,.4,.45)) + axs = [ax] + + # use masked array to hide NaN's on plot + Sxxm = numpy.ma.masked_where(Sxx==0.0, Sxx) + pc = ax.pcolor(f, d, Sxxm.T, vmin=cmin, vmax=cmax) + # pc = ax.pcolor(f, d, Sxxm.T) + ax.set_ylabel('Direction (deg N)') + ax.set_ylim(0., 360.) + l0 = ax.axvline(x=0.1, color='k', linestyle=':', linewidth=1.5) + ax.set_xlim(0., 0.635) + ax.set_xlabel('Frequency (Hz)') + + # setup colorbar axes instance. + l,b,w,h = ax.get_position().bounds + cax = fig.add_axes([l+w+0.025, b-0.06, 1.0*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Spectral Density (m2/Hz/deg)') + cb.ax.xaxis.set_label_position('top') + # cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + # cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) + + # top scale wave period + ax2 = twiny(ax) + ax2.set_xlim(0., 0.635) + ax2.xaxis.tick_top() + # convert (bottom) Hertz to (top scale) seconds (1/Hz) + Hertz = ax.get_xticks() + Hertz = [val for val in Hertz if val!=0 ] + ax2.set_xticks(Hertz) + s = [round(1./val,2) for val in Hertz] + ax2.set_xticklabels(s) + ax2.set_xlabel('Wave Period (sec)') + + ####################################### + # print ' ... all, swell, and wind labels' + ax = fig.add_axes((.1,.875,.4,.10)) + axs.append(ax) + + ax.set_axis_off() + ax.set_axis_bgcolor(None) + ax.axvline(x=0.1, color='k', linestyle=':', linewidth=1.5) + ax.plot([0., 0.635], [0.6, 0.6], 'k-') + ax.plot([0.005], [0.6],'k<') + ax.plot([0.63],[0.6],'k>') + ax.text(0.5, 0.65, 'ALL FREQs', + horizontalalignment='center', + # verticalalignment='center', + transform=ax.transAxes, + bbox=dict(facecolor=None, edgecolor='k', alpha=1)) + + ax.plot([0.0, 0.1], [0.3,0.3], 'g-') + ax.plot([0.005], [0.3],'g<') + ax.plot([0.095],[0.3],'g>') + ax.text(0.05, .35, 'SWELL', color='g', + horizontalalignment='center', + # verticalalignment='center', + transform=ax.transAxes, + bbox=dict(facecolor=None, edgecolor='g', alpha=1)) + + ax.plot([0.1, 0.635], [0.3,0.3], 'b-') + ax.plot([0.105], [0.3], 'b<') + ax.plot([0.63], [0.3], 'b>') + ax.text(0.7, 0.35, 'WIND', color='b', + horizontalalignment='center', + # verticalalignment='center', + transform=ax.transAxes, + bbox=dict(facecolor=None, edgecolor='b', alpha=1)) + ax.set_ylim(0.,1.) + ax.set_xlim(0.,0.635) + + ####################################### + # print ' ... Sf' + ax = fig.add_axes((.1,.25,.4,.15)) + axs.append(ax) + + l1, = ax.plot(f, Sf, 'k-') + l1.set_label('Non-directional Spectrum') + l0 = ax.axvline(x=0.1, color='k', linestyle=':', linewidth=1.5) + l2 = ax.axvline(1/Tp, color='k', linestyle='-', label='ALL Wave Frequencies') + l3 = ax.axvline(1/Tps, color='g', linestyle='-', label='SWELL Waves') + l4 = ax.axvline(1/Tpw, color='b', linestyle='-', label='WIND Waves') + ax.set_axis_bgcolor(None) + ax.set_xlim(0., 0.635) + ax.set_ylabel('Sf (m2/Hz)') + ax.set_ylim(0, 3.0) + # ax.set_title('Frequency Spectrum') + + # legend + ls2 = l2.get_label() + ls3 = l3.get_label() + ls4 = l4.get_label() + leg = fig.legend((l2,l3,l4), (ls2,ls3,ls4), loc=(.520,.225)) + 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 + + ####################################### + # print ' ... Stheta' + ax = fig.add_axes((.520,.4,.125,.45)) + axs.append(ax) + + xlim = (0.,0.003) + l1, = ax.plot(Stheta, d, 'k-') + l2 = ax.axhline(Dp, color='k', linestyle='-') + # label ALL FREQ + ax.text(0.5, 0.95, 'ALL FREQs', horizontalalignment='center', + transform=ax.transAxes, bbox=dict(facecolor=None, edgecolor='k', alpha=0.5)) + ax.set_yticklabels([]) + ax.set_ylim(0., 360.) + ax.xaxis.tick_top() + ax.xaxis.set_label_position('top') + ax.set_xlim(xlim) + # ax.xaxis.set_major_locator(MaxNLocator(3)) + # ax.xaxis.set_major_formatter(ScalarFormatter()) + ax.set_xticks([0.,0.001,0.002]) + ax.set_xticklabels(['0.0e-3','1.0','2.0']) + + ####################################### + # print ' ... Stheta_swell' + ax = fig.add_axes((.67,.4,.125,.45)) + axs.append(ax) + + l1, = ax.plot(Stheta_swell, d, 'g-') + l2 = ax.axhline(Dps, color='g', linestyle='-') + # label SWELL + ax.text(0.5, 0.95, 'SWELL', color='g', horizontalalignment='center', + transform=ax.transAxes, bbox=dict(facecolor=None, edgecolor='g', alpha=0.5)) + ax.set_yticklabels([]) + ax.set_ylim(0., 360.) + ax.xaxis.tick_top() + ax.xaxis.set_label_position('top') + ax.set_xlabel('Stheta (m2/deg)') + ax.set_xlim(xlim) + # ax.xaxis.set_major_locator(MaxNLocator(3)) + # ax.xaxis.set_major_formatter(ScalarFormatter()) + ax.set_xticks([0.,0.001,0.002]) + ax.set_xticklabels(['0.0','1.0','2.0']) + ax.set_title('Bogue Wave Data as of ' + last_dt_str, fontsize=14) + # ax.set_title('Directional Spectrum') + ax.title.set_position((-0.8, 1.25)) + + ####################################### + # print ' ... Stheta_wind' + ax = fig.add_axes((.82,.4,.125,.45)) + axs.append(ax) + + l1, = ax.plot(Stheta_wind, d, 'b-') + l2 = ax.axhline(Dpw, color='b', linestyle='-') + # label WIND + ax.text(0.5, 0.95, 'WIND', color='b', horizontalalignment='center', + transform=ax.transAxes, bbox=dict(facecolor=None, edgecolor='b', alpha=0.5)) + ax.xaxis.tick_top() + ax.xaxis.set_label_position('top') + ax.set_xlim(xlim) + # ax.xaxis.set_major_locator(MaxNLocator(3)) + # ax.xaxis.set_major_formatter(ScalarFormatter()) + ax.set_xticks([0.,0.001,0.002]) + ax.set_xticklabels(['0.0','1.0','2.0e-03']) + + ax.yaxis.tick_right() + ax.set_ylim(0., 360.) + ax.yaxis.set_label_position('right') + ax.set_ylabel('Direction (deg N)') + + ####################################### + # print ' ... Hs, Hss, Hsw' + ax = fig.add_axes((.1,.05,.8,.15)) + axs.append(ax) + + # use masked array to hide NaN's on plot + Hs = numpy.ma.masked_where(numpy.isnan(Hs), Hs) + Hss = numpy.ma.masked_where(numpy.isnan(Hss), Hss) + Hsw = numpy.ma.masked_where(numpy.isnan(Hsw), Hsw) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, Hs, fmt='k-') + l1.set_label('Significant Wave Height (Hs)') + + l2, = ax.plot_date(dt, Hss, fmt='g-') + l2.set_label('Sig. Swell Wave Height (Hss)') + + l3, = ax.plot_date(dt, Hsw, fmt='b-') + l3.set_label('Sig. Wind Wave Height (Hsw)') + + 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])-1, date2num(dt[-1])) # last minus 30 days to last + 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 Wave Height -- Last 24 hours from ' + last_dt_str) + + # 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)') + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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=8) # the legend text fontsize + setp(llines, linewidth=1.5) # the legend linewidth + leg.draw_frame(False) # don't draw the legend frame + + # save figure + ofn = os.path.join(odir, 'bogue_dspec_'+fn_dt_str+'.png') + savefig(ofn) + +# copy last latest +ofn2 = '/home/haines/rayleigh/img/bogue_dspec_last01days.png' +import shutil +shutil.copy(ofn, ofn2) + +# copy last 24 to loop directory +import glob +gs = os.path.join(odir, '*.png') +all_pngs = glob.glob(gs) +all_pngs.sort() +j=1 +for png in all_pngs[-24:]: + ofn = '/home/haines/rayleigh/loop/bogue_dspec_plot_%d.png' % (j,) + shutil.copy(png, ofn) + j=j+1 + +# Index: proc2plot/trunk/proc2plot/bogue_swellwaves_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/bogue_swellwaves_plot.py (revision 326) @@ -1,0 +1,353 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2009-10-29 16:15:21 haines> +"""bogue_swellwaves_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_swellwaves_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' + +# load data +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) +Hss = nc.var('Hs_swell')[:] +Tps = nc.var('Tp_swell')[:] +Tms = nc.var('Tm_swell')[:] +#Hmax = nc.var('Hmax')[:] +Dps = nc.var('Dp_swell')[:] +Dms = nc.var('Dm_swell')[:] +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] + +# 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)') + +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 ADCPWAVES -- Last 30 days from ' + last_dt_str) + +# 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)') + +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) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, Hss, maxdelta=2./24) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Significant Swell Wave Height (Hss)') + +# (x, y) = procutil.addnan(dt, Hmax, maxdelta=2./24) +# l2, = ax.plot_date(x, y, fmt='g-') +# 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) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, Tps, maxdelta=2./24) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Peak Swell Period (Tp)') + +(x, y) = procutil.addnan(dt, Tms, maxdelta=2./24) +l2, = ax.plot_date(x, y, fmt='c-') +l2.set_label('Mean Swell 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) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, Dps, maxdelta=2./24) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Peak Swell Direction (Dp)') + +(x, y) = procutil.addnan(dt, Dms, maxdelta=2./24) +l2, = ax.plot_date(x, y, fmt='c-') +l2.set_label('Mean Swell 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 SWELL 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_swellwaves_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 ADCPWAVES -- 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 ADCPWAVES -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/bogue_swellwaves_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 ADCPWAVES -- 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 ADCPWAVES -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/bogue_swellwaves_last01days.png') + + Index: proc2plot/trunk/proc2plot/bogue_waves_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/bogue_waves_plot.py (revision 326) @@ -1,0 +1,1078 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-09 16:54:06 haines> + +""" +bogue_adcpwaves_plot + +allwaves() +swellwaves() +windwaves() +""" + +################################################# +# [1a] search and replace XXXX with platform id +# [1b] search and replace YYYY with sensor id +# [1c] search and replace ZZZZ plot titles +################################################# + +# plot each month + +import os +# import sys, glob, re +import datetime, time, dateutil, 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 + +def swellwaves(pi, si, yyyy_mm, plot_type='latest'): + """ + """ + + print 'bogue_swellwaves_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ################################ + # [2a] load primary data file + ################################ + + 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) + + # open netcdf 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) + + ################################ + # [2b] specify variables + ################################ + Hss = nc.var('Hs_swell')[:] + Tps = nc.var('Tp_swell')[:] + Tms = nc.var('Tm_swell')[:] + # Hmax = nc.var('Hmax')[:] + Dps = nc.var('Dp_swell')[:] + Dms = nc.var('Dm_swell')[:] + + nc.close() + + + ################################ + # [3a] load ancillary files if needed + ################################ + + 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' + + 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) + + # open netcdf 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 + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + ################################ + # [3b] specify variables + ################################ + wd2 = nc.var('wd')[:] + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot setup + ####################################### + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, wd2, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Water Level (HAB)') + + 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(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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 Swell Waves -- ' + yyyy_mm_str) + + # 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)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, Hss, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Significant Swell Wave Height (Hss)') + + # (x, y) = procutil.addnan(dt, Hmax, maxdelta=2./24) + # l2, = ax.plot_date(x, y, fmt='g-') + # l2.set_label('Max Wave Height (Hmax)') + + ax.set_ylabel('WAVE\nHEIGHT (m)') + # ax.set_ylim(2.,10.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, Tps, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Peak Swell Period (Tp)') + + (x, y) = procutil.addnan(dt, Tms, maxdelta=2./24) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Mean Swell Period (Tm)') + + ax.set_ylabel('WAVE\nPERIOD (s)') + # ax.set_ylim(2.,10.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, Dps, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Peak Swell Direction (Dp)') + + (x, y) = procutil.addnan(dt, Dms, maxdelta=2./24) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Mean Swell 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(this_month), date2num(next_month-datetime.timedelta(seconds=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 Swell Waves -- ' + yyyy_mm_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 for this month + ofn = '/home/haines/rayleigh/img/bogue/bogue_swellwaves_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('BOGUE Swell Waves -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BOGUE Swell Waves -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_swellwaves_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BOGUE Swell Waves -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BOGUE Swell Waves -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_swellwaves_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BOGUE Swell Waves -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('BOGUE Swell Waves -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_swellwaves_last01days.png') + + +def windwaves(pi, si, yyyy_mm, plot_type='latest'): + """ + """ + + print 'bogue_windwaves_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ################################ + # [2a] load primary data file + ################################ + + 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) + + # open netcdf 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) + + ################################ + # [2b] specify variables + ################################ + Hsw = nc.var('Hs_wind')[:] + Tpw = nc.var('Tp_wind')[:] + Tmw = nc.var('Tm_wind')[:] + # Hmax = nc.var('Hmax')[:] + Dpw = nc.var('Dp_wind')[:] + Dmw = nc.var('Dm_wind')[:] + + nc.close() + + ################################ + # [3a] load ancillary files if needed + ################################ + + 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' + + 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) + + # open netcdf 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 + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + ################################ + # [3b] specify variables + ################################ + wd2 = nc.var('wd')[:] + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot setup + ####################################### + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, wd2, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Water Level (HAB)') + + 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(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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 Wind Waves -- ' + yyyy_mm_str) + + # 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)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, Hsw, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Significant Wind Wave Height (Hss)') + + # (x, y) = procutil.addnan(dt, Hmax, maxdelta=2./24) + # l2, = ax.plot_date(x, y, fmt='g-') + # l2.set_label('Max Wave Height (Hmax)') + + ax.set_ylabel('WAVE\nHEIGHT (m)') + # ax.set_ylim(2.,10.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, Tpw, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Peak Wind Period (Tp)') + + (x, y) = procutil.addnan(dt, Tmw, maxdelta=2./24) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Mean Wind Period (Tm)') + + ax.set_ylabel('WAVE\nPERIOD (s)') + # ax.set_ylim(2.,10.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, Dpw, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Peak Wind Direction (Dp)') + + (x, y) = procutil.addnan(dt, Dmw, maxdelta=2./24) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Mean Wind 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(this_month), date2num(next_month-datetime.timedelta(seconds=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 Wind Waves -- ' + yyyy_mm_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 for this month + ofn = '/home/haines/rayleigh/img/bogue/bogue_windwaves_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('BOGUE Wind Waves -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BOGUE Wind Waves -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_windwaves_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BOGUE Wind Waves -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BOGUE Wind Waves -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_windwaves_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BOGUE Wind Waves -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('BOGUE Wind Waves -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_windwaves_last01days.png') + + + + +def allwaves(pi, si, yyyy_mm, plot_type='latest'): + print 'bogue_allwaves_plot ...' + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ################################ + # [2a] load primary data file + ################################ + + 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) + + # open netcdf 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) + + ################################ + # [2b] specify variables + ################################ + 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() + + ################################ + # [3a] load ancillary files if needed + ################################ + + 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' + + 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) + + # open netcdf 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 + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + ################################ + # [3b] specify variables + ################################ + wd2 = nc.var('wd')[:] + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot setup + ####################################### + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, wd2, maxdelta=2./24) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Water Level (HAB)') + + 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(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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 Wind Waves -- ' + yyyy_mm_str) + + # 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)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # 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)') + + # (x, y) = procutil.addnan(dt, Hmax, maxdelta=2./24) + # l2, = ax.plot_date(x, y, fmt='g-') + # l2.set_label('Max Wave Height (Hmax)') + + ax.set_ylabel('WAVE\nHEIGHT (m)') + # ax.set_ylim(2.,10.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # 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 Period (Tp)') + + (x, y) = procutil.addnan(dt, Tm, maxdelta=2./24) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Mean Period (Tm)') + + ax.set_ylabel('WAVE\nPERIOD (s)') + # ax.set_ylim(2.,10.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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) + + # 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 Direction (Dp)') + + (x, y) = procutil.addnan(dt, Dm, maxdelta=2./24) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Mean 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(this_month), date2num(next_month-datetime.timedelta(seconds=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 Waves -- ' + yyyy_mm_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 for this month + ofn = '/home/haines/rayleigh/img/bogue/bogue_allwaves_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('BOGUE All Waves -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('BOGUE All Waves -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/bogue_allwaves_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BOGUE All Waves -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + 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) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('BOGUE All Waves -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + 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') + + + + Index: proc2plot/trunk/proc2plot/bogue_windwaves_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/bogue_windwaves_plot.py (revision 326) @@ -1,0 +1,357 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2009-10-29 16:29:21 haines> +"""bogue_windwaves_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_windwaves_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' + +# load data +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) +Hsw = nc.var('Hs_wind')[:] +Tpw = nc.var('Tp_wind')[:] +Tmw = nc.var('Tm_wind')[:] +#Hmax = nc.var('Hmax')[:] +Dpw = nc.var('Dp_wind')[:] +Dmw = nc.var('Dm_wind')[:] +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] + +# 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)') + +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 WIND WAVES -- Last 30 days from ' + last_dt_str) + +# 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)') + +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) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, Hsw, maxdelta=2./24) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Significant Wind Wave Height (Hsw)') + +# (x, y) = procutil.addnan(dt, Hmax, maxdelta=2./24) +# l2, = ax.plot_date(x, y, 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 +Tpw = numpy.ma.masked_where(numpy.isnan(Tpw), Tpw) +Tmw = numpy.ma.masked_where(numpy.isnan(Tmw), Tmw) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, Tpw, maxdelta=2./24) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Peak Wind Period (Tpw)') + +(x, y) = procutil.addnan(dt, Tmw, maxdelta=2./24) +l2, = ax.plot_date(x, y, fmt='c-') +l2.set_label('Mean Wind Period (Tmw)') + +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) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, Dpw, maxdelta=2./24) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Peak Wind Direction (Dpw)') + +(x, y) = procutil.addnan(dt, Dmw, maxdelta=2./24) +l2, = ax.plot_date(x, y, fmt='c-') +l2.set_label('Mean Wind Direction (Dmw)') + +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 WIND 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_windwaves_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 ADCPWAVES -- 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 WIND WAVES -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/bogue_windwaves_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 WIND 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 WIND WAVES -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/bogue_windwaves_last01days.png') + + Index: proc2plot/trunk/proc2plot/crow_flow_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/crow_flow_plot.py (revision 326) @@ -1,0 +1,382 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-09 16:52:14 haines> +"""crow_flow_plot""" + +# plot each month + +import os +# import sys, glob, re +import datetime, time, dateutil, 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 + +def timeseries(pi, si, yyyy_mm, plot_type='latest'): + """ + """ + + + print 'crow_flow_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ncFile1='/seacoos/data/nccoos/level1/crow/wq/crow_wq_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/crow/wq/crow_wq_'+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) + + # open netcdf 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) + + z = nc.var('z')[:] + wtemp = nc.var('wtemp')[:] + cond = nc.var('cond')[:] + turb = nc.var('turb')[:] + ph = nc.var('ph')[:] + do_mg = nc.var('do_mg')[:] + do_sat = nc.var('do_sat')[:] + batt = nc.var('battvolts')[:] + nc.close() + + + ncFile1='/seacoos/data/nccoos/level1/crow/flow/crow_flow_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/crow/flow/crow_flow_'+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) + + # open netcdf 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 + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + r2 = nc.var('rain')[:] # inches of rain in past 15 min + + ncvars = nc.variables() + have_press = 'press_wl' in ncvars.keys() or 'press_flow' in ncvars.keys() + have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys() + if have_press: + pwl2 = nc.var('press_wl')[:] # feet + pfl2 = nc.var('press_flow')[:] # cfs + if have_sontek: + swl2 = nc.var('sontek_wl')[:] # feet + sfl2 = nc.var('sontek_flow')[:] # cfs + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot month all flow (plus battery) the timeseries data + ####################################### + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, r2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Precipitation') + + ax.set_ylabel('Rain (in)') + # ax.set_ylim(0,1.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) inches to (rhs) mm + f = [procutil.inches2millimeters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Rain (mm)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('CROW Stream Flow -- ' + yyyy_mm_str) + + # 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) + + # ax.plot returns a list of lines, so unpack tuple + if have_press: + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + ls2 = l2.get_label() + + ax.plot_date((x[1],x[-1]), (0,0), fmt='k:') + # ax.plot_date((x[1],x[-1]), (2.5,2.5), fmt='k--') + + ax.set_ylabel('Depth (ft)') + # ax.set_ylim(-0.2,3) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) feet to (rhs) meters + f = [procutil.feet2meters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Depth (m)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # legend + if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') + if have_sontek: + leg = ax.legend((l2,), (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,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + if have_press: + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + ls2 = l2.get_label() + + ax.plot_date((x[1],x[-1]), (0,0), fmt='k:') + + ax.set_ylabel('Flow (cfs)') + # ax.set_ylim(0,350) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') + elif have_sontek: + leg = ax.legend((l2,), (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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, batt) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Station Battery') + + ax.set_ylabel('Battery\n (volts)') + # ax.set_ylim(12, 14.5) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('CROW Stream Flow -- ' + yyyy_mm_str) + + # 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 + + # save figure + ofn = '/home/haines/rayleigh/img/crow/crow_flow_' + yyyy_mm_str + '.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('CROW Stream Flow -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('CROW Stream Flow -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/crow_flow_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('CROW Stream Flow -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('CROW Stream Flow -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/crow_flow_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('CROW Stream Flow -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('CROW Stream Flow -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/crow_flow_last01days.png') + + + + Index: proc2plot/trunk/proc2plot/crow_wq_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/crow_wq_plot.py (revision 326) @@ -1,0 +1,447 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-09 16:52:29 haines> +"""meet_wq_plot""" + +# plot each month + +import os +# import sys, glob, re +import datetime, time, dateutil, 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 + +def timeseries(pi, si, yyyy_mm, plot_type='latest'): + """ + """ + + + print 'crow_wq_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ncFile1='/seacoos/data/nccoos/level1/crow/wq/crow_wq_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/crow/wq/crow_wq_'+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) + + # open netcdf 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) + + z = nc.var('z')[:] + wtemp = nc.var('wtemp')[:] + cond = nc.var('cond')[:] + turb = nc.var('turb')[:] + ph = nc.var('ph')[:] + do_mg = nc.var('do_mg')[:] + do_sat = nc.var('do_sat')[:] + batt = nc.var('battvolts')[:] + nc.close() + + + ncFile1='/seacoos/data/nccoos/level1/crow/flow/crow_flow_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/crow/flow/crow_flow_'+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) + + # open netcdf 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 + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + r2 = nc.var('rain')[:] # inches of rain in past 15 min + + ncvars = nc.variables() + have_press = 'press_wl' in ncvars.keys() or 'press_flow' in ncvars.keys() + have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys() + if have_press: + pwl2 = nc.var('press_wl')[:] # feet + pfl2 = nc.var('press_flow')[:] # cfs + if have_sontek: + swl2 = nc.var('sontek_wl')[:] # feet + sfl2 = nc.var('sontek_flow')[:] # cfs + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot month all wq (plus flow) the timeseries data + ####################################### + + fig = figure(figsize=(10, 9)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + + ax = fig.add_subplot(6,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, wtemp) + ibad = y <= -6999. + y[ibad] = numpy.nan + # if all are NaN's then pyplot throws an error for autoscale + # for some reason setting one value in the timeseries to a real value + # fixes this. + if numpy.isnan(y).all(): + y[-1]=0. + + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Water Temperature') + + ax.set_ylabel('TEMP\n (deg C)') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) deg C to (rhs) deg F + f = [procutil.celsius2fahrenheit(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('(deg F)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('CROW Water Quality -- ' + yyyy_mm_str) + + # 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(6,1,2) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, cond) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Specific Conductivity') + + ax.set_ylabel('Cond\n (mS/cm)') + # ax.set_ylim(0,10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, do_mg) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Dissolved Oxygen (DO)') + + ax.set_ylabel('DO \n(mg/l)') + # ax.set_ylim(20,120) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + (x, y) = procutil.addnan(dt, do_sat) + l2, = ax2.plot_date(x, y, fmt='c-') + l2.set_label('Percent Air Saturation') + # convert (lhs) deg C to (rhs) deg F + # ax2.set_ylim(0,200) + ax2.set_ylabel('DO (%)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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') + 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(6,1,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, ph) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('pH') + + ax.set_ylabel('pH') + # if not numpy.alltrue(y<14) and not numpy.alltrue(y>1): + ax.set_ylim(6, 12) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, turb) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Turbidity') + + ax.set_ylabel('Turb \n(NTU)') + # ax.set_ylim(0,100) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,6) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + if have_press and numpy.isnan(pfl2).all(): + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Flow from pressure') + ls1 = l1.get_label() + + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Flow from acoustic') + ls1 = l1.get_label() + + ax.set_ylim(0,350) + ax.set_ylabel('Flow\n (cfs)') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax.plot_date((x[1],x[-1]), (0,0), fmt='k:') + ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + (x, y) = procutil.addnan(dt2, r2) + l2, = ax2.plot_date(x, y, fmt='c-') + l2.set_label('Precipitation') + + # ax2.set_ylim(-0.5,0.5) + ax2.set_ylabel('Rain\n (in)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('CROW Water Quality -- ' + yyyy_mm_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 for this month + ofn = '/home/haines/rayleigh/img/crow/crow_wq_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('CROW Water Quality -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('CROW Water Quality -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/crow_wq_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('CROW Water Quality -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('CROW Water Quality -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/crow_wq_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('CROW Water Quality -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('CROW Water Quality -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/crow_wq_last01days.png') + + + + Index: proc2plot/trunk/proc2plot/jpier_adcp_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/jpier_adcp_plot.py (revision 326) @@ -1,0 +1,234 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2009-10-29 17:12:47 haines> +"""jpier_adcp_plot""" + +import os, sys +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'jpier_adcp_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_2008_02.nc' +ncFile1='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+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) + +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) +z = nc.var('z')[:] +wd = nc.var('wd')[:] +wl = nc.var('wl')[:] +u = nc.var('u')[:] +v = nc.var('v')[:] +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] + +# testing procutil.addnan for 2-d +# dto = dt +# (dt, u) = procutil.addnan(dt, u) +# dn = date2num(dt) + +# use masked array to hide NaN's on plot +um = numpy.ma.masked_where(numpy.isnan(u), u) +pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) +pc.set_label('True Eastward Current (m s-1)') +ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position().bounds +cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Current Velocity (m s-1)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, wl, maxdelta=2./24) +l1, = ax.plot_date(x, y, fmt='k-') +l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-14.,2.) +# 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('Jpier AWAC -- Last 30 days from ' + last_dt_str) + +# 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('Depth (ft)') + +# 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 +vm = numpy.ma.masked_where(numpy.isnan(v), v) +pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) +pc.set_label('True Northward Current (m s-1)') +ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, wl, maxdelta=2./24) +l1, = ax.plot_date(x, y, fmt='k-') +l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-14.,2.) +# 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('Jpier AWAC -- Last 30 days from ' + last_dt_str) + +# 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('Depth (ft)') + +# 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 + +# save figure +savefig('/home/haines/rayleigh/img/jpier_adcp_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(1,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.set_xticklabels([]) +ax.set_xlabel('Jpier AWAC -- 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(2,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('Jpier AWAC -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_adcp_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('Jpier AWAC -- 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.xaxis.set_major_formatter( DateFormatter('%H') ) +ax.set_xlabel('Jpier AWAC -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_adcp_last01days.png') + Index: proc2plot/trunk/proc2plot/jpier_allwaves_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/jpier_allwaves_plot.py (revision 326) @@ -1,0 +1,337 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2009-10-29 13:38:34 haines> +"""jpier_allwaves_plot""" + +import os, sys +import datetime, time, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'jpier_allwaves_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_2008_02.nc' + +ncFile1='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_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) + +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/jpier/adcp/jpier_adcp_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+this_month.strftime('%Y_%m')+'.nc' +nc = pycdf.CDFMF((ncFile1, ncFile2)) +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] + +# 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 +l1, = ax.plot_date(dt_anc, wd_anc, fmt='b-') +l1.set_label('Water Level (MSL)') + +ax.set_ylabel('DEPTH (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('Jpier ALL WAVES -- Last 30 days from ' + last_dt_str) + +# 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('Depth (feet)') + +# 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 +l1, = ax.plot_date(dt, Hs, 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)') + +# 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 +l1, = ax.plot_date(dt, Tp, fmt='b-') +l1.set_label('Peak Wave Period (Tp)') + +l2, = ax.plot_date(dt, Tm, 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 +l1, = ax.plot_date(dt, Dp, fmt='b-') +l1.set_label('Peak Wave Direction (Dp)') + +l2, = ax.plot_date(dt, Dm, 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('Jpier 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/jpier_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('Jpier 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('Jpier ALL WAVES -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_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('Jpier 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('Jpier ALL WAVES -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_allwaves_last01days.png') + + Index: proc2plot/trunk/proc2plot/jpier_dspec_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/jpier_dspec_plot.py (revision 326) @@ -1,0 +1,367 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-08-13 14:54:33 haines> +"""jpier_dspec_plot""" + +import os, sys +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/raw2proc') +del(sys) + +os.environ["MPLCONFIGDIR"]="/home/haines/.matplotlib/" + +from pylab import figure, twinx, twiny, savefig, setp, getp, cm, colorbar +from matplotlib.dates import DayLocator, HourLocator, MinuteLocator, DateFormatter, date2num, num2date +from matplotlib.ticker import MaxNLocator, FormatStrFormatter, ScalarFormatter +import procutil + +def which_to_plot(odir, ncfn): + """ + Finds which timestamp in netCDF data file (ncfn) does not have a + corresponding directional spectrum plot png in output dir (odir) + + :Parameters: + odir : string, path to location of png's + ncfn : string, filename and path to netCDF file + + :Returns: + j_seq : integer sequence, for indices of data to plot + + """ + + nc = pycdf.CDF(ncFile1) + # nc = pycdf.CDFMF((ncFile1, ncFile2)) + ncvars = nc.variables() + # print ncvars + es = nc.var('time')[:] + units = nc.var('time').units + dt = [procutil.es2dt(e) for e in es] + dts = [d.strftime('%Y_%m_%d_%H%M') for d in dt] + # list all pngs + import glob + gs = os.path.join(odir, '*.png') + all_pngs = glob.glob(gs) + ap = ''.join(all_pngs) + # find index of dts not in all_pngs + j_seq = [j for j in range(len(dts)) if ap.find(dts[j]) == -1] + # return index values to plot (j) + return j_seq + +print 'jpier_dspec_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/home/haines/test_data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_2008_03.nc' +# ncFile2='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_2008_03.nc' +# ncFile1='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile1='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_'+this_month.strftime('%Y_%m')+'.nc' +odir = os.path.join('/seacoos/data/nccoos/level3/jpier/adcpwaves/dspec', this_month.strftime('%Y_%m')) +if not os.path.exists(odir): + os.mkdir(odir) + +j_seq = which_to_plot(odir, ncFile1) +# print j_seq + +if not j_seq: + j_seq = [-1] + +# load data +print ' ... loading data for graph from ...' +print ' ... ... ' + ncFile1 +# print ' ... ... ' + ncFile2 +for j in j_seq: + nc = pycdf.CDF(ncFile1) + # nc = pycdf.CDFMF((ncFile1, ncFile2)) + 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) + f = nc.var('f')[:] + d = nc.var('d')[:] + Sxx = nc.var('Sxx')[j] + Sf = nc.var('Sf')[j] + Stheta = nc.var('Stheta')[j] + Stheta_wind = nc.var('Stheta_wind')[j] + Stheta_swell = nc.var('Stheta_swell')[j] + Tp = nc.var('Tp')[j] + Tpw = nc.var('Tp_wind')[j] + Tps = nc.var('Tp_swell')[j] + Dp = nc.var('Dp')[j] + Dpw = nc.var('Dp_wind')[j] + Dps = nc.var('Dp_swell')[j] + Hs = nc.var('Hs')[:] + Hss = nc.var('Hs_swell')[:] + Hsw = nc.var('Hs_wind')[:] + nc.close() + + print '... ... ' + dt[j].strftime('%Y_%m_%d_%H%M') + + # range for pcolor plots + cmin, cmax = (0.0, 0.05) + # last dt in data for labels + dt1 = dt[j] + dt2 = dt_local[j] + + 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") + + fn_dt_str = dt1.strftime("%Y_%m_%d_%H%M") + + fig = figure(figsize=(9, 7)) + + ####################################### + # full directional spectrum S(f,d) + ####################################### + # print ' ... Sxx' + ax = fig.add_axes((.1,.4,.4,.45)) + axs = [ax] + + # use masked array to hide NaN's on plot + Sxxm = numpy.ma.masked_where(Sxx==0.0, Sxx) + pc = ax.pcolor(f, d, Sxxm.T, vmin=cmin, vmax=cmax) + # pc = ax.pcolor(f, d, Sxxm.T) + ax.set_ylabel('Direction (deg N)') + ax.set_ylim(0., 360.) + l0 = ax.axvline(x=0.1, color='k', linestyle=':', linewidth=1.5) + ax.set_xlim(0., 0.635) + ax.set_xlabel('Frequency (Hz)') + + # setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l+w+0.025, b-0.06, 1.0*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Spectral Density (m2/Hz/deg)') + cb.ax.xaxis.set_label_position('top') + # cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + # cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) + + # top scale wave period + ax2 = twiny(ax) + ax2.set_xlim(0., 0.635) + ax2.xaxis.tick_top() + # convert (bottom) Hertz to (top scale) seconds (1/Hz) + Hertz = ax.get_xticks() + Hertz = [val for val in Hertz if val!=0 ] + ax2.set_xticks(Hertz) + s = [round(1./val,2) for val in Hertz] + ax2.set_xticklabels(s) + ax2.set_xlabel('Wave Period (sec)') + + ####################################### + # print ' ... all, swell, and wind labels' + ax = fig.add_axes((.1,.875,.4,.10)) + axs.append(ax) + + ax.set_axis_off() + ax.set_axis_bgcolor(None) + ax.axvline(x=0.1, color='k', linestyle=':', linewidth=1.5) + ax.plot([0., 0.635], [0.6, 0.6], 'k-') + ax.plot([0.005], [0.6],'k<') + ax.plot([0.63],[0.6],'k>') + ax.text(0.5, 0.65, 'ALL FREQs', + horizontalalignment='center', + # verticalalignment='center', + transform=ax.transAxes, + bbox=dict(facecolor=None, edgecolor='k', alpha=1)) + + ax.plot([0.0, 0.1], [0.3,0.3], 'g-') + ax.plot([0.005], [0.3],'g<') + ax.plot([0.095],[0.3],'g>') + ax.text(0.05, .35, 'SWELL', color='g', + horizontalalignment='center', + # verticalalignment='center', + transform=ax.transAxes, + bbox=dict(facecolor=None, edgecolor='g', alpha=1)) + + ax.plot([0.1, 0.635], [0.3,0.3], 'b-') + ax.plot([0.105], [0.3], 'b<') + ax.plot([0.63], [0.3], 'b>') + ax.text(0.7, 0.35, 'WIND', color='b', + horizontalalignment='center', + # verticalalignment='center', + transform=ax.transAxes, + bbox=dict(facecolor=None, edgecolor='b', alpha=1)) + ax.set_ylim(0.,1.) + ax.set_xlim(0.,0.635) + + ####################################### + # print ' ... Sf' + ax = fig.add_axes((.1,.25,.4,.15)) + axs.append(ax) + + l1, = ax.plot(f, Sf, 'k-') + l1.set_label('Non-directional Spectrum') + l0 = ax.axvline(x=0.1, color='k', linestyle=':', linewidth=1.5) + l2 = ax.axvline(1/Tp, color='k', linestyle='-', label='ALL Wave Frequencies') + l3 = ax.axvline(1/Tps, color='g', linestyle='-', label='SWELL Waves') + l4 = ax.axvline(1/Tpw, color='b', linestyle='-', label='WIND Waves') + ax.set_axis_bgcolor(None) + ax.set_xlim(0., 0.635) + ax.set_ylabel('Sf (m2/Hz)') + ax.set_ylim(0, 3.0) + # ax.set_title('Frequency Spectrum') + + # legend + ls2 = l2.get_label() + ls3 = l3.get_label() + ls4 = l4.get_label() + leg = fig.legend((l2,l3,l4), (ls2,ls3,ls4), loc=(.520,.225)) + 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 + + ####################################### + # print ' ... Stheta' + ax = fig.add_axes((.520,.4,.125,.45)) + axs.append(ax) + + xlim = (0.,0.003) + l1, = ax.plot(Stheta, d, 'k-') + l2 = ax.axhline(Dp, color='k', linestyle='-') + # label ALL FREQ + ax.text(0.5, 0.95, 'ALL FREQs', horizontalalignment='center', + transform=ax.transAxes, bbox=dict(facecolor=None, edgecolor='k', alpha=0.5)) + ax.set_yticklabels([]) + ax.set_ylim(0., 360.) + ax.xaxis.tick_top() + ax.xaxis.set_label_position('top') + ax.set_xlim(xlim) + # ax.xaxis.set_major_locator(MaxNLocator(3)) + # ax.xaxis.set_major_formatter(ScalarFormatter()) + ax.set_xticks([0.,0.001,0.002]) + ax.set_xticklabels(['0.0e-3','1.0','2.0']) + + ####################################### + # print ' ... Stheta_swell' + ax = fig.add_axes((.67,.4,.125,.45)) + axs.append(ax) + + l1, = ax.plot(Stheta_swell, d, 'g-') + l2 = ax.axhline(Dps, color='g', linestyle='-') + # label SWELL + ax.text(0.5, 0.95, 'SWELL', color='g', horizontalalignment='center', + transform=ax.transAxes, bbox=dict(facecolor=None, edgecolor='g', alpha=0.5)) + ax.set_yticklabels([]) + ax.set_ylim(0., 360.) + ax.xaxis.tick_top() + ax.xaxis.set_label_position('top') + ax.set_xlabel('Stheta (m2/deg)') + ax.set_xlim(xlim) + # ax.xaxis.set_major_locator(MaxNLocator(3)) + # ax.xaxis.set_major_formatter(ScalarFormatter()) + ax.set_xticks([0.,0.001,0.002]) + ax.set_xticklabels(['0.0','1.0','2.0']) + ax.set_title('Jpier Wave Data as of ' + last_dt_str, fontsize=14) + # ax.set_title('Directional Spectrum') + ax.title.set_position((-0.8, 1.25)) + + ####################################### + # print ' ... Stheta_wind' + ax = fig.add_axes((.82,.4,.125,.45)) + axs.append(ax) + + l1, = ax.plot(Stheta_wind, d, 'b-') + l2 = ax.axhline(Dpw, color='b', linestyle='-') + # label WIND + ax.text(0.5, 0.95, 'WIND', color='b', horizontalalignment='center', + transform=ax.transAxes, bbox=dict(facecolor=None, edgecolor='b', alpha=0.5)) + ax.xaxis.tick_top() + ax.xaxis.set_label_position('top') + ax.set_xlim(xlim) + # ax.xaxis.set_major_locator(MaxNLocator(3)) + # ax.xaxis.set_major_formatter(ScalarFormatter()) + ax.set_xticks([0.,0.001,0.002]) + ax.set_xticklabels(['0.0','1.0','2.0e-03']) + + ax.yaxis.tick_right() + ax.set_ylim(0., 360.) + ax.yaxis.set_label_position('right') + ax.set_ylabel('Direction (deg N)') + + ####################################### + # print ' ... Hs, Hss, Hsw' + ax = fig.add_axes((.1,.05,.8,.15)) + axs.append(ax) + + # use masked array to hide NaN's on plot + Hs = numpy.ma.masked_where(numpy.isnan(Hs), Hs) + Hss = numpy.ma.masked_where(numpy.isnan(Hss), Hss) + Hsw = numpy.ma.masked_where(numpy.isnan(Hsw), Hsw) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, Hs, fmt='k-') + l1.set_label('Significant Wave Height (Hs)') + + l2, = ax.plot_date(dt, Hss, fmt='g-') + l2.set_label('Sig. Swell Wave Height (Hss)') + + l3, = ax.plot_date(dt, Hsw, fmt='b-') + l3.set_label('Sig. Wind Wave Height (Hsw)') + + 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])-1, date2num(dt[-1])) # last minus 30 days to last + 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('Jpier Wave Height -- Last 24 hours from ' + last_dt_str) + + # 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)') + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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=8) # the legend text fontsize + setp(llines, linewidth=1.5) # the legend linewidth + leg.draw_frame(False) # don't draw the legend frame + + # save figure + ofn = os.path.join(odir, 'jpier_dspec_'+fn_dt_str+'.png') + savefig(ofn) + +# copy last latest +ofn2 = '/home/haines/rayleigh/img/jpier_dspec_last01days.png' +import shutil +shutil.copy(ofn, ofn2) + +# copy last 24 to loop directory +import glob +gs = os.path.join(odir, '*.png') +all_pngs = glob.glob(gs) +all_pngs.sort() +j=1 +for png in all_pngs[-24:]: + ofn = '/home/haines/rayleigh/loop/jpier_dspec_plot_%d.png' % (j,) + shutil.copy(png, ofn) + j=j+1 + +# Index: proc2plot/trunk/proc2plot/jpier_met1_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/jpier_met1_plot.py (revision 326) @@ -1,0 +1,327 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2009-09-14 11:17:11 haines> +"""jpier_met_plot1""" + +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 'jpier_met_plot1 ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +#ncFile1='/seacoos/data/nccoos/level1/jpier/met/jpier_met_2008_02.nc' +#ncFile2='/seacoos/data/nccoos/level1/jpier/met/jpier_met_2008_03.nc' + +ncFile1='/seacoos/data/nccoos/level1/jpier/met/jpier_met_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/met/jpier_met_'+this_month.strftime('%Y_%m')+'.nc' + +# load data + +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) + +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) +ap = nc.var('air_pressure')[:] +at = nc.var('air_temp')[:] +dp = nc.var('dew_temp')[:] +h = nc.var('humidity')[:] +p = nc.var('rainfall_day')[:] +nc.close() + +# 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] + +# use masked array to hide NaN's on plot +ap = numpy.ma.masked_where(numpy.isnan(ap), ap) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, ap, fmt='b-') +l1.set_label('Barometric Pressure') + +ax.set_ylabel('Pressure (mbar)') +ax.set_ylim(980.,1040.) +# 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('JPIER Met -- Last 30 days from ' + last_dt_str) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) mbar to (rhs) in Hg +inHG = [procutil.millibar2inches_Hg(val) for val in ax.get_ylim()] +ax2.set_ylim(inHG) +ax2.set_ylabel('Pressure (in Hg)') + +# 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 +at = numpy.ma.masked_where(numpy.isnan(at), at) + + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, at, fmt='b-') +l1.set_label('Air Temperature') + +l2, = ax.plot_date(dt, dp, fmt='g-') +l2.set_label('Dew Point') + +ax.set_ylabel('Temp (deg C)') +ax.set_ylim(-10.,40.) +# ax.set_xlim(dt[0], dt[-1]) +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_xticklabels([]) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) deg C to (rhs) deg F + +f = [procutil.celsius2fahrenheit(val) for val in ax.get_ylim()] +ax2.set_ylim(f) +ax2.set_ylabel('Temp (deg F)') + +# 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,3) +axs.append(ax) + +# use masked array to hide NaN's on plot +h = numpy.ma.masked_where(numpy.isnan(h), h) + + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, h, fmt='b-') +l1.set_label('Relative Humidity') + + +ax.set_ylabel('RHUM (%)') +ax.set_ylim(0.,100.) +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() +# no cenversion needed +ylim = [(val) for val in ax.get_ylim()] +ax2.set_ylim(ylim) +ax2.set_ylabel('RHUM (%)') + + + +# 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,4) +axs.append(ax) + +# use masked array to hide NaN's on plot +p = numpy.ma.masked_where(numpy.isnan(p), p) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, p, fmt='b-') +l1.set_label('Daily Precipitation') + +ax.set_ylabel('Rain (mm/day)') +ax.set_ylim(0.,40.) +# 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') ) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) mm/day to (rhs) in/day +ylim = [procutil.millimeters2inches(val) for val in ax.get_ylim()] +ax2.set_ylim(ylim) +ax2.set_ylabel('Rain (in/day)') + + +ax.set_xlabel('JPIER Met -- Last 30 days from ' + last_dt_str) + +# 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 + +# save figure +savefig('/home/haines/rayleigh/img/jpier_met_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('JPIER Met -- 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(2,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(2,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(2,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('JPIER Met -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_met_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('JPIER Met -- 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('JPIER Met -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_met_last01days.png') + + Index: proc2plot/trunk/proc2plot/jpier_met2_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/jpier_met2_plot.py (revision 326) @@ -1,0 +1,256 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-03 11:28:55 haines> +"""jpier_met_plot2""" + +import os, sys +import datetime, time, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'jpier_met_plot2 ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +#ncFile1='/seacoos/data/nccoos/level1/jpier/met/jpier_met_2008_01.nc' +#ncFile2='/seacoos/data/nccoos/level1/jpier/met/jpier_met_2008_02.nc' + +ncFile1='/seacoos/data/nccoos/level1/jpier/met/jpier_met_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/met/jpier_met_'+this_month.strftime('%Y_%m')+'.nc' + +# load data +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) + +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) +ws = nc.var('wspd')[:] +wd = nc.var('wdir')[:] +u = nc.var('wspd')[:] +v = nc.var('wspd')[:] + +i = 0 +for val in ws: + u[i] = -1.*procutil.wind_vector2u(ws[i],wd[i]) + v[i] = -1.*procutil.wind_vector2v(ws[i],wd[i]) + i += 1 +nc.close() + + +# 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] + +# use masked array to hide NaN's on plot +wd = numpy.ma.masked_where(numpy.isnan(wd), wd) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, wd, fmt='b-') +l1.set_label('Wind from Direction') + +ax.set_ylabel('Dir (true N)') +ax.set_ylim(0.,360.) +# 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('JPIER Met -- Last 30 days from ' + last_dt_str) + +# 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 +ws = numpy.ma.masked_where(numpy.isnan(ws), ws) + + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, ws, fmt='b-') +l1.set_label('Wind Speed') + + +ax.set_ylabel('Speed (m/s)') +ax.set_ylim(0.,20.) + +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) m/s to (rhs) knots +k = [procutil.meters_sec2knots(val) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +# 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,3) +axs.append(ax) + +# use masked array to hide NaN's on plot +# u = numpy.ma.masked_where(numpy.isnan(u), u) +# v = numpy.ma.masked_where(numpy.isnan(v), v) + +dt0 = numpy.zeros(len(dt)) + +ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) +ax.axhline(y=0, color='gray') +# make stick plot (set all head parameters to zero) +# scale to y-axis using units='height' of plot with scale = 40 (m/s range) per height with min=-20 and max=20 +q1 = ax.quiver(date2num(dt), dt0, u, v, units='height', scale=40, headwidth=0, headlength=0, headaxislength=0) +qk = ax.quiverkey(q1, 0.1, 0.8, 10, r'10 m s-1') + +ax.set_ylim(-20.,20.) +ax.set_ylabel('Speed (m/s)') + +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') ) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) m/s to (rhs) knots +k = [(val * 1.94384449) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +ax.set_xlabel('JPIER Met -- Last 30 days from ' + last_dt_str) + +# save figure +savefig('/home/haines/rayleigh/img/jpier_met2_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('JPIER Met -- 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.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('JPIER Met -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_met2_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('JPIER Met -- 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.xaxis.set_major_formatter( DateFormatter('%H') ) +ax.set_xlabel('JPIER Met -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_met2_last01days.png') + Index: proc2plot/trunk/proc2plot/jpier_swellwaves_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/jpier_swellwaves_plot.py (revision 326) @@ -1,0 +1,337 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-21 16:46:27 haines> +"""jpier_swellwaves_plot""" + +import os, sys +import datetime, time, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'jpier_swellwaves_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_2008_02.nc' + +ncFile1='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_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) + +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) +Hss = nc.var('Hs_swell')[:] +Tps = nc.var('Tp_swell')[:] +Tms = nc.var('Tm_swell')[:] +#Hmax = nc.var('Hmax')[:] +Dps = nc.var('Dp_swell')[:] +Dms = nc.var('Dm_swell')[:] +nc.close() + +# ancillary data to plot + +ncFile1='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+this_month.strftime('%Y_%m')+'.nc' +nc = pycdf.CDFMF((ncFile1, ncFile2)) +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] + +# 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 +l1, = ax.plot_date(dt_anc, wd_anc, fmt='b-') +l1.set_label('Water Depth (m)') + +ax.set_ylabel('DEPTH (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('Jpier ADCPWAVES -- Last 30 days from ' + last_dt_str) + +# 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('Depth (feet)') + +# 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 +Hss = numpy.ma.masked_where(numpy.isnan(Hss), Hss) +# Hmax = numpy.ma.masked_where(numpy.isnan(Hmax), Hmax) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, Hss, fmt='b-') +l1.set_label('Significant Swell Wave Height (Hss)') + +# l2, = ax.plot_date(dt, Hmax, fmt='g-') +# 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)') + +# 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 +Tps = numpy.ma.masked_where(numpy.isnan(Tps), Tps) +Tms = numpy.ma.masked_where(numpy.isnan(Tms), Tms) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, Tps, fmt='b-') +l1.set_label('Peak Swell Period (Tp)') + +l2, = ax.plot_date(dt, Tms, fmt='c-') +l2.set_label('Mean Swell 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 +Dps = numpy.ma.masked_where(numpy.isnan(Dps), Dps) +Dms = numpy.ma.masked_where(numpy.isnan(Dms), Dms) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, Dps, fmt='b-') +l1.set_label('Peak Swell Direction (Dp)') + +l2, = ax.plot_date(dt, Dms, fmt='c-') +l2.set_label('Mean Swell 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('Jpier SWELL 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/jpier_swellwaves_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('Jpier ADCPWAVES -- 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('Jpier ADCPWAVES -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_swellwaves_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('Jpier ADCPWAVES -- 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('Jpier ADCPWAVES -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_swellwaves_last01days.png') + + Index: proc2plot/trunk/proc2plot/jpier_windwaves_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/jpier_windwaves_plot.py (revision 326) @@ -1,0 +1,337 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-21 16:47:14 haines> +"""jpier_windwaves_plot""" + +import os, sys +import datetime, time, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'jpier_windwaves_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_2008_02.nc' + +ncFile1='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_adcpwaves_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/adcpwaves/jpier_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) + +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) +Hsw = nc.var('Hs_wind')[:] +Tpw = nc.var('Tp_wind')[:] +Tmw = nc.var('Tm_wind')[:] +#Hmax = nc.var('Hmax')[:] +Dpw = nc.var('Dp_wind')[:] +Dmw = nc.var('Dm_wind')[:] +nc.close() + +# ancillary data to plot + +ncFile1='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+this_month.strftime('%Y_%m')+'.nc' +nc = pycdf.CDFMF((ncFile1, ncFile2)) +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] + +# 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 +l1, = ax.plot_date(dt_anc, wd_anc, fmt='b-') +l1.set_label('Water Depth (m)') + +ax.set_ylabel('DEPTH (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('Jpier WIND WAVES -- Last 30 days from ' + last_dt_str) + +# 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('Depth (feet)') + +# 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 +Hsw = numpy.ma.masked_where(numpy.isnan(Hsw), Hsw) +# Hmax = numpy.ma.masked_where(numpy.isnan(Hmax), Hmax) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, Hsw, fmt='b-') +l1.set_label('Significant Wind Wave Height (Hsw)') + +# 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)') + +# 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 +Tpw = numpy.ma.masked_where(numpy.isnan(Tpw), Tpw) +Tmw = numpy.ma.masked_where(numpy.isnan(Tmw), Tmw) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, Tpw, fmt='b-') +l1.set_label('Peak Wind Period (Tp)') + +l2, = ax.plot_date(dt, Tmw, fmt='c-') +l2.set_label('Mean Wind 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 +Dpw = numpy.ma.masked_where(numpy.isnan(Dpw), Dpw) +Dmw = numpy.ma.masked_where(numpy.isnan(Dmw), Dmw) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, Dpw, fmt='b-') +l1.set_label('Peak Wind Direction (Dp)') + +l2, = ax.plot_date(dt, Dmw, fmt='c-') +l2.set_label('Mean Wind 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('Jpier WIND 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/jpier_windwaves_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('Jpier ADCPWAVES -- 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('Jpier WIND WAVES -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_windwaves_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('Jpier WIND 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('Jpier WIND WAVES -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/jpier_windwaves_last01days.png') + + Index: proc2plot/trunk/proc2plot/meet_flow_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/meet_flow_plot.py (revision 326) @@ -1,0 +1,372 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-09 16:49:47 haines> +"""meet_flow_plot""" + +# plot each month + +import os +# import sys, glob, re +import datetime, time, dateutil, 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 + +def timeseries(pi, si, yyyy_mm, plot_type='latest'): + """ + """ + + + print 'meet_flow_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ncFile1='/seacoos/data/nccoos/level1/meet/wq/meet_wq_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/meet/wq/meet_wq_'+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) + + # open netcdf 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) + + z = nc.var('z')[:] + wtemp = nc.var('wtemp')[:] + cond = nc.var('cond')[:] + turb = nc.var('turb')[:] + ph = nc.var('ph')[:] + do_mg = nc.var('do_mg')[:] + do_sat = nc.var('do_sat')[:] + batt = nc.var('battvolts')[:] + nc.close() + + + ncFile1='/seacoos/data/nccoos/level1/meet/flow/meet_flow_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/meet/flow/meet_flow_'+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) + + # open netcdf 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 + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + r2 = nc.var('rain')[:] # inches of rain in past 15 min + have_sontek = False # don't plot sontek + # have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys() + pwl2 = nc.var('press_wl')[:] # feet + pfl2 = nc.var('press_flow')[:] # cfs + + if have_sontek: + swl2 = nc.var('sontek_wl')[:] # feet + sfl2 = nc.var('sontek_flow')[:] # cfs + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot month all flow (plus battery) the timeseries data + ####################################### + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, r2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Precipitation') + + ax.set_ylabel('Rain (in)') + # ax.set_ylim(0,1.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) inches to (rhs) mm + f = [procutil.inches2millimeters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Rain (mm)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('MEET Stream Flow -- ' + yyyy_mm_str) + + # 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, pwl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + if have_sontek: + (x, y) = procutil.addnan(dt2, swl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('From Acoustic Sounding') + + ax.plot_date((x[1],x[-1]), (0,0), fmt='k:') + ax.plot_date((x[1],x[-1]), (2.5,2.5), fmt='k--') + + ax.set_ylabel('Depth (ft)') + ax.set_ylim(-0.2,3) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) feet to (rhs) meters + f = [procutil.feet2meters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Depth (m)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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() + if have_sontek: + ls2 = l2.get_label() + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + else: + 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + + ax.plot_date((x[1],x[-1]), (0,0), fmt='k:') + + ax.set_ylabel('Flow (cfs)') + ax.set_ylim(0,350) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + if have_sontek: + ls2 = l2.get_label() + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + else: + 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,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, batt) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Station Battery') + + ax.set_ylabel('Battery\n (volts)') + ax.set_ylim(12, 14.5) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('MEET Stream Flow -- ' + yyyy_mm_str) + + # 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 + + # save figure + ofn = '/home/haines/rayleigh/img/meet/meet_flow_' + yyyy_mm_str + '.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('MEET Stream Flow -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('MEET Stream Flow -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/meet_flow_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('MEET Stream Flow -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('MEET Stream Flow -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/meet_flow_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('MEET Stream Flow -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('MEET Stream Flow -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/meet_flow_last01days.png') + + + + Index: proc2plot/trunk/proc2plot/meet_wq_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/meet_wq_plot.py (revision 326) @@ -1,0 +1,430 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-14 11:37:45 haines> +"""meet_wq_plot""" + +# plot each month + +import os +# import sys, glob, re +import datetime, time, dateutil, 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 + +def timeseries(pi, si, yyyy_mm, plot_type='latest'): + """ + """ + + + print 'meet_wq_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ncFile1='/seacoos/data/nccoos/level1/meet/wq/meet_wq_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/meet/wq/meet_wq_'+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) + + # open netcdf 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) + + z = nc.var('z')[:] + wtemp = nc.var('wtemp')[:] + cond = nc.var('cond')[:] + turb = nc.var('turb')[:] + ph = nc.var('ph')[:] + do_mg = nc.var('do_mg')[:] + do_sat = nc.var('do_sat')[:] + batt = nc.var('battvolts')[:] + nc.close() + + ncFile1='/seacoos/data/nccoos/level1/meet/flow/meet_flow_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/meet/flow/meet_flow_'+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) + + # open netcdf 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 + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + r2 = nc.var('rain')[:] # inches of rain in past 15 min + have_sontek = False # don't plot sontek + # have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys() + pwl2 = nc.var('press_wl')[:] # feet + pfl2 = nc.var('press_flow')[:] # cfs + + if have_sontek: + swl2 = nc.var('sontek_wl')[:] # feet + sfl2 = nc.var('sontek_flow')[:] # cfs + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot month all wq (plus flow) the timeseries data + ####################################### + + fig = figure(figsize=(10, 9)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + + ax = fig.add_subplot(6,1,1) + axs = [ax] + + (x, y) = procutil.addnan(dt, wtemp) + ibad = y <= -6999. + y[ibad] = numpy.nan + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Water Temperature') + + ax.set_ylabel('TEMP\n (deg C)') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) deg C to (rhs) deg F + f = [procutil.celsius2fahrenheit(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('(deg F)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('MEET Water Quality -- ' + yyyy_mm_str) + + # 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(6,1,2) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, cond) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Specific Conductivity') + + ax.set_ylabel('Cond\n (mS/cm)') + # ax.set_ylim(0,10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, do_mg) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Dissolved Oxygen (DO)') + + ax.set_ylabel('DO \n(mg/l)') + # ax.set_ylim(20,120) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + (x, y) = procutil.addnan(dt, do_sat) + l2, = ax2.plot_date(x, y, fmt='c-') + l2.set_label('Percent Air Saturation') + # convert (lhs) deg C to (rhs) deg F + # ax2.set_ylim(0,200) + ax2.set_ylabel('DO (%)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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') + 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(6,1,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, ph) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('pH') + + ax.set_ylabel('pH') + # ax.set_ylim(6, 12) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, turb) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Turbidity') + + ax.set_ylabel('Turb \n(NTU)') + # ax.set_ylim(0,100) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,6) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + + ax.set_ylim(0,350) + ax.set_ylabel('Flow\n (cfs)') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax.plot_date((x[1],x[-1]), (0,0), fmt='k:') + ax.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + (x, y) = procutil.addnan(dt2, r2) + l2, = ax2.plot_date(x, y, fmt='c-') + l2.set_label('Precipitation') + + # ax2.set_ylim(-0.5,0.5) + ax2.set_ylabel('Rain\n (in)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('MEET Water Quality -- ' + yyyy_mm_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 for this month + ofn = '/home/haines/rayleigh/img/meet/meet_wq_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('MEET Water Quality -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('MEET Water Quality -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/meet_wq_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('MEET Water Quality -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('MEET Water Quality -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/meet_wq_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('MEET Water Quality -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%H') ) + ax.set_xlabel('MEET Water Quality -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/meet_wq_last01days.png') + + + + Index: proc2plot/trunk/proc2plot/morgan_avp_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/morgan_avp_plot.py (revision 326) @@ -1,0 +1,445 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-09-17 13:56:54 haines> +"""morgan_avp_plot""" + +import os, sys +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'morgan_avp_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_2008_02.nc' +ncFile1='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_'+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) + +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) +z = nc.var('z')[:] +wd = nc.var('wd')[:] +wtemp = nc.var('wtemp')[:] +salin = nc.var('salin')[:] +turb = nc.var('turb')[:] +ph = nc.var('ph')[:] +do = nc.var('do')[:] +chl = nc.var('chl')[:] +nc.close() + +# 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") +# add a couple of inches to length of page for 6 subplots (5 subs in 8 inches, 6 subs in 10) +fig = figure(figsize=(10, 10)) +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(6,1,1) +axs = [ax] + +# use masked array to hide NaN's on plot +wtm = numpy.ma.masked_where(numpy.isnan(wtemp), wtemp) +# range for pcolor plots +cmin = numpy.floor(wtm.min()) +cmax = cmin+8. +# print "%s : %g %g" % ('Temp', cmin, cmax) +# cmin, cmax = (15., 35.) +# plot pcolor +pc = ax.pcolor(dn, z, wtm.T, cmap=cm.get_cmap('jet'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Water Temperature (deg C)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +# make tick labels for 10 posns set and round to one decimal place +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) +# but only select one at set_xticks +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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('Morgan Bay AVP -- Last 30 days from ' + last_dt_str) + +# 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('Depth (ft)') + +# 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(6,1,2) +axs.append(ax) + +# use masked array to hide NaN's on plot +sm = numpy.ma.masked_where(numpy.isnan(salin), salin) +# range for pcolor plots +cmin = numpy.floor(sm.mean()-2*sm.std()) +cmax = cmin+10. +# print "%s : %g %g" % ('Salin', cmin, cmax) +# cmin, cmax = (0., 50.) +pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('Blues_r'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Salinity (PSU)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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.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('Depth (ft)') + +# 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(6,1,3) +axs.append(ax) + +# use masked array to hide NaN's on plot +sm = numpy.ma.masked_where(numpy.isnan(turb), turb) +# range for pcolor plots +# cmin, cmax = numpy.round([sm.min(), sm.max()], decimals=0) +# cmin = cmin-1 +# cmax = cmax+1 +cmin, cmax = (0., 10.) +# print "%s : %g %g" % ('Turb', cmin, cmax) +pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('YlOrBr'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Turbidity (NTU)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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.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('Depth (ft)') + +####################################### +# +ax = fig.add_subplot(6,1,4) +axs.append(ax) + +# use masked array to hide NaN's on plot +sm = numpy.ma.masked_where(numpy.isnan(chl), chl) +# range for pcolor plots +cmin = numpy.floor(sm.min()) +cmax = cmin+15. +# print "%s : %g %g" % ('Chloro', cmin, cmax) +# cmin, cmax = (0., 100.) +pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('BuGn'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Chlorophyll (ug l-1)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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.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('Depth (ft)') + +####################################### +# +ax = fig.add_subplot(6,1,5) +axs.append(ax) + +# use masked array to hide NaN's on plot +sm = numpy.ma.masked_where(numpy.isnan(do), do) +# range for pcolor plots +cmin, cmax = (0., 15.) +pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('PiYG'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Dissolved Oxygen (mg l-1)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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.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('Depth (ft)') + +####################################### +# +ax = fig.add_subplot(6,1,6) +axs.append(ax) + +# use masked array to hide NaN's on plot +sm = numpy.ma.masked_where(numpy.isnan(ph), ph) +# range for pcolor plots +# cmin, cmax = numpy.round([sm.min(), sm.max()], decimals=0) +# cmin = cmin-1 +# cmax = cmax+1 +cmin, cmax = (6., 8.) +print "%s : %g %g" % ('pH', cmin, cmax) +pc = ax.pcolor(dn, z, sm.T, cmap=cm.get_cmap('cool'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('pH') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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('Morgan Bay AVP -- Last 30 days from ' + last_dt_str) + +# 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('Depth (ft)') + +# save figure +savefig('/home/haines/rayleigh/img/morgan_avp_last30days.png') + +####################################### +# Last 7 days +####################################### + +print ' ... Last 7 days' +for i in [0, 1, 2, 3, 4]: + ax = axs[i] + ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) + ax.set_xticklabels([]) + if i==0: + ax.set_xlabel('Morgan Bay AVP -- Last 7 days from ' + last_dt_str) +# + +ax = axs[5] +ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1])) +ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('Morgan Bay AVP -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/morgan_avp_last07days.png') + +####################################### +# Last 1 day (24hrs) +####################################### + +print ' ... Last 1 days' + +for i in [0, 1, 2, 3, 4]: + ax = axs[i] + 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([]) + if i==0: + ax.set_xlabel('Morgan Bay AVP -- Last 24 hours from ' + last_dt_str) + +ax = axs[5] +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('Morgan Bay AVP -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/morgan_avp_last01days.png') + Index: proc2plot/trunk/proc2plot/morgan_met_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/morgan_met_plot.py (revision 326) @@ -1,0 +1,256 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2010-03-24 16:58:08 haines> +"""morgan_met_plot""" + +import os, sys +import datetime, time, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'morgan_met_plot2 ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +#ncFile1='/seacoos/data/nccoos/level1/morgan/met/morgan_met_2008_01.nc' +#ncFile2='/seacoos/data/nccoos/level1/morgan/met/morgan_met_2008_02.nc' + +ncFile1='/seacoos/data/nccoos/level1/morgan/met/morgan_met_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/morgan/met/morgan_met_'+this_month.strftime('%Y_%m')+'.nc' + +# load data +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) + +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) +ws = nc.var('wspd')[:] +wd = nc.var('wdir')[:] +u = nc.var('wspd')[:] +v = nc.var('wspd')[:] + +i = 0 +for val in ws: + u[i] = -1.*procutil.wind_vector2u(ws[i],wd[i]) + v[i] = -1.*procutil.wind_vector2v(ws[i],wd[i]) + i += 1 +nc.close() + + +# 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] + +# use masked array to hide NaN's on plot +wd = numpy.ma.masked_where(numpy.isnan(wd), wd) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, wd, fmt='b-') +l1.set_label('Wind from Direction') + +ax.set_ylabel('Dir (true N)') +ax.set_ylim(0.,360.) +# 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('MORGAN BAY WIND -- Last 30 days from ' + last_dt_str) + +# 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 +ws = numpy.ma.masked_where(numpy.isnan(ws), ws) + + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, ws, fmt='b-') +l1.set_label('Wind Speed') + + +ax.set_ylabel('Speed (m/s)') +ax.set_ylim(0.,20.) + +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) m/s to (rhs) knots +k = [procutil.meters_sec2knots(val) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +# 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,3) +axs.append(ax) + +# use masked array to hide NaN's on plot +# u = numpy.ma.masked_where(numpy.isnan(u), u) +# v = numpy.ma.masked_where(numpy.isnan(v), v) + +dt0 = numpy.zeros(len(dt)) + +ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) +ax.axhline(y=0, color='gray') +# make stick plot (set all head parameters to zero) +# scale to y-axis using units='height' of plot with scale = 40 (m/s range) per height with min=-20 and max=20 +q1 = ax.quiver(date2num(dt), dt0, u, v, units='height', scale=40, headwidth=0, headlength=0, headaxislength=0) +qk = ax.quiverkey(q1, 0.1, 0.8, 10, r'10 m s-1') + +ax.set_ylim(-20.,20.) +ax.set_ylabel('Speed (m/s)') + +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') ) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) m/s to (rhs) knots +k = [(val * 1.94384449) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +ax.set_xlabel('MORGAN BAY WIND -- Last 30 days from ' + last_dt_str) + +# save figure +savefig('/home/haines/rayleigh/img/morgan_met_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('MORGAN BAY WIND -- 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.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('MORGAN BAY WIND -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/morgan_met_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('MORGAN BAY WIND -- 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.xaxis.set_major_formatter( DateFormatter('%H') ) +ax.set_xlabel('MORGAN BAY WIND -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/morgan_met_last01days.png') + Index: proc2plot/trunk/proc2plot/proc2plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/proc2plot.py (revision 326) @@ -1,0 +1,305 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2010-03-31 17:12:14 haines> +"""Create plots from monthly netCDF data files + +This module plots different graphical products from different NCCOOS +sensors (ctd, adcp, waves-adcp, met) based on manual or automated +operation. If automated processing, latest and current month plots +are generated for all active sensors based on the current +configuration setting. If manual processing, generate graphics for +requested platform, sensor, and month. + +:Processing steps: + 0. proc2plot auto or manual for platform, sensor, month + 1. + +""" + +__version__ = "v0.1" +__author__ = "Sara Haines " + +import sys +import os +import re + +# for testing use: +# defconfigs='/home/haines/nccoos/test/r2p' + +# define config file location to run under cron +defconfigs='/opt/env/haines/dataproc/raw2proc' + +import numpy + +sys.path.append('/opt/env/haines/dataproc/raw2proc') +del(sys) + +from procutil import * +from ncutil import * + + +def get_config(name): + """Usage Example >>>sensor_info = get_config('bogue_config_20060918.sensor_info')""" + components = name.split('.') + mod = __import__(components[0]) + for comp in components[1:]: + attr = getattr(mod, comp) + return attr + +def find_configs(platform, yyyy_mm, config_dir=''): + """Find which configuration files for specified platform and month + + :Parameters: + platform : string + Platfrom id to process (e.g. 'bogue') + yyyy_mm : string + Year and month of data to process (e.g. '2007_07') + + :Returns: + cns : list of str + List of configurations that overlap with desired month + If empty [], no configs were found + """ + import glob + # list of config files based on platform + configs = glob.glob(os.path.join(config_dir, platform + '_config_*.py')) + configs.sort() + now_dt = datetime.utcnow() + now_dt.replace(microsecond=0) + # determine when month starts and ends + (prev_month, this_month, next_month) = find_months(yyyy_mm) + month_start_dt = this_month + month_end_dt = next_month - timedelta(seconds=1) + # print month_start_dt; print month_end_dt + # + cns = [] + for config in configs: + # datetime from filename + cn = os.path.splitext(os.path.basename(config))[0] + cndt = filt_datetime(os.path.basename(config))[0] + pi = get_config(cn+'.platform_info') + if pi['config_start_date']: + config_start_dt = filt_datetime(pi['config_start_date'])[0] + elif pi['config_start_date'] == None: + config_start_dt = now_dt + if pi['config_end_date']: + config_end_dt = filt_datetime(pi['config_end_date'])[0] + elif pi['config_end_date'] == None: + config_end_dt = now_dt + # + if (config_start_dt <= month_start_dt or config_start_dt <= month_end_dt) and \ + (config_end_dt >= month_start_dt or config_end_dt >= month_end_dt): + cns.append(cn) + return cns + + +def find_active_configs(config_dir=''): + """Find which configuration files are active + + :Returns: + cns : list of str + List of configurations that overlap with desired month + If empty [], no configs were found + """ + import glob + # list of all config files + configs = glob.glob(os.path.join(config_dir, '*_config_*.py')) + now_dt = datetime.utcnow() + now_dt.replace(microsecond=0) + # + cns = [] + for config in configs: + # datetime from filename + cn = os.path.splitext(os.path.basename(config))[0] + cndt = filt_datetime(os.path.basename(config))[0] + pi = get_config(cn+'.platform_info') + if pi['config_end_date'] == None: + cns.append(cn) + return cns + + + +def proc2plot(proctype, platform=None, package=None, yyyy_mm=None): + """ + Plot products either in auto-mode or manual-mode + + If auto-mode, process latest products for all platforms, all + sensors. Otherwise in manual-mode, process data for specified + platform, sensor package, and month. + + :Parameters: + proctype : string + 'auto' or 'manual' + + platform : string + Platfrom id to process (e.g. 'bogue') + package : string + Sensor package id to process (e.g. 'adcp') + yyyy_mm : string + Year and month of data to process (e.g. '2007_07') + + Examples + -------- + >>> proc2plot(proctype='manual', platform='bogue', package='adcp', yyyy_mm='2007_06') + >>> proc2plot('manual', 'bogue', 'adcp', '2007_06') + + """ + print '\nStart time for proc2plot: %s\n' % start_dt.strftime("%Y-%b-%d %H:%M:%S UTC") + + if proctype == 'auto': + print 'Processing in auto-mode, all platforms, all packages, latest data' + auto() + elif proctype == 'manual': + if platform and package and yyyy_mm: + print 'Processing in manually ...' + print ' ... platform id : %s' % platform + print ' ... package name : %s' % package + print ' ... month : %s' % yyyy_mm + print ' ... starting at : %s' % start_dt.strftime("%Y-%m-%d %H:%M:%S UTC") + manual(platform, package, yyyy_mm) + else: + print 'proc2plot: Manual operation requires platform, package, and month' + print " >>> proc2plot(proctype='manual', platform='bogue', package='adcp', yyyy_mm='2007_07')" + else: + print 'proc2plot: requires either auto or manual operation' + + +def auto(): + """Process all platforms, all packages, latest data + + Notes + ----- + + 1. determine which platforms (all platforms with currently active + config files i.e. config_end_date is None + 2. for each platform + get latest config + for each package + yyyy_mm is the current month + load this months netcdf + load and execute plot module(s) + + """ + yyyy_mm = this_month() + months = find_months(yyyy_mm) + month_start_dt = months[1] + month_end_dt = months[2] - timedelta(seconds=1) + + configs = find_active_configs(config_dir=defconfigs) + if configs: + # for each configuration + for cn in configs: + print ' ... config file : %s' % cn + pi = get_config(cn+'.platform_info') + asi = get_config(cn+'.sensor_info') + platform = pi['id'] + # for each sensor package + for package in asi.keys(): + print ' ... package name : %s' % package + si = asi[package] + si['proc_filename'] = '%s_%s_%s.nc' % (platform, package, yyyy_mm) + ofn = os.path.join(si['proc_dir'], si['proc_filename']) + si['proc_start_dt'] = month_start_dt + si['proc_end_dt'] = month_end_dt + if os.path.exists(ofn): + # get last dt from current month file + (es, units) = nc_get_time(ofn) + last_dt = es2dt(es[-1]) + # if older than month_start_dt use it instead to only process newest data + if last_dt>=month_start_dt: + si['proc_start_dt'] = last_dt + + if 'plot_module' in si.keys(): + make_plots(pi, si, yyyy_mm, plot_type='latest') + # determine if any nc_file at this point + # and what to do if no files found + else: + print ' ... ... NOTE: no plot module specified for %s %s for %s' % (package, platform, yyyy_mm) + + + # + else: + print ' ... ... NOTE: No active platforms' + +def manual(platform, package, yyyy_mm): + """Process data for specified platform, sensor package, and month + + Notes + ----- + + 1. determine which configs + 2. for each config for specific platform + if have package in config + which nc files + """ + # determine when month starts and ends + months = find_months(yyyy_mm) + month_start_dt = months[1] + month_end_dt = months[2] - timedelta(seconds=1) + + configs = find_configs(platform, yyyy_mm, config_dir=defconfigs) + + if configs: + # for each configuration + for index in range(len(configs)): + cn = configs[index] + print ' ... config file : %s' % cn + pi = get_config(cn+'.platform_info') + # month start and end dt to pi info + asi = get_config(cn+'.sensor_info') + if package in pi['packages']: + si = asi[package] + if si['utc_offset']: + print ' ... ... utc_offset : %g (hours)' % si['utc_offset'] + si['proc_start_dt'] = month_start_dt + si['proc_end_dt'] = month_end_dt + si['proc_filename'] = '%s_%s_%s.nc' % (platform, package, yyyy_mm) + ofn = os.path.join(si['proc_dir'], si['proc_filename']) + # this added just in case data repeated in data files + if os.path.exists(ofn): + # get last dt from current month file + (es, units) = nc_get_time(ofn) + last_dt = es2dt(es[-1]) + + if 'plot_module' in si.keys(): + make_plots(pi, si, yyyy_mm, plot_type='monthly') + # determine if any nc_file at this point + # and what to do if no files found + else: + print ' ... ... NOTE: no plot module specified for %s %s for %s' % (package, platform, yyyy_mm) + + else: + print ' ... ... NOTE: %s not operational on %s for %s' % (package, platform, yyyy_mm) + else: + print ' ... ... ... NOTE: %s not operational for %s' % (platform, yyyy_mm) + +def import_plotter(mod_name, plot_name): + mod = __import__(mod_name) + plotter = getattr(mod, plot_name) + return plotter + +def make_plots(pi, si, yyyy_mm, plot_type='latest'): + # tailored graphics for different packages and platforms and deployment configurations + mod_name = si['plot_module'] + plot_names = si['plot_names'] + + for pn in plot_names: + plot = import_plotter(mod_name, pn) + plot(pi, si, yyyy_mm, plot_type) + + # ISSUES: + # 1. how to handle other time spans, besides monthly ... hourly, daily, yearly, record ... + # 2. specifying data from other sources (besides current month, plat, package) for plots may be addressed by tailored plots + # 3. other types of plots (maps, wave energy spectrum) + + +# globals +start_dt = datetime.utcnow() +start_dt.replace(microsecond=0) + +if __name__ == "__main__": + import optparse + proc2plot('auto') + + # for testing + # proctype='manual'; platform='bogue'; package='adcp'; yyyy_mm='2007_07' + # proc2plot(proctype='manual', platform='bogue', package='adcp', yyyy_mm='2007_07') Index: proc2plot/trunk/proc2plot/scratch/legend_scatter.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/legend_scatter.py (revision 326) @@ -1,0 +1,38 @@ +#!/usr/bin/env python +from pylab import * +import os + +N=100 + +props = dict( alpha=0.5, faceted=False ) + +handles = [] +colours = ['red', 'green', 'blue', 'magenta', 'cyan', 'yellow'] +colours = ['red', 'green', 'blue'] +for colour in colours: + x, y = rand(2,N) + s = 400.0 * rand(N) + handles.append(scatter(x, y, c=colour, s=s, **props)) + +legend(handles, colours) +grid(True) + +leg = gca().get_legend() +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 + +# see text.Text, lines.Line2D, and patches.Rectangle for more info on +# the settable properties of lines, text, and rectangles +frame.set_facecolor('0.80') # set the frame face color to light gray +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 + +#show() +savefig('legend_scatter.png') +os.system('ls *.png') +os.system('scp -r *.png haines@pitot.marine.unc.edu:/afs/isis.unc.edu/depts/marine/workspace/haines/public_html/nccoos/test') + + + Index: proc2plot/trunk/proc2plot/scratch/scr_subplots.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/scr_subplots.py (revision 326) @@ -1,0 +1,103 @@ +#!/usr/bin/env python +""" +Mock-up timeseries for bogue +""" + +import os, sys +import datetime, time +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/raw2proc') +del(sys) + +from pylab import colorbar, figure, savefig, setp, getp +from matplotlib.dates import DayLocator, HourLocator, DateFormatter, date2num, num2date +from procutil import dt2es, es2dt + +# ---- on cromwell load data +#if load_data: +print ' ... loading data for graph' +ncFile='/home/haines/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_01.nc' +nc = pycdf.CDF(ncFile) +ncvars = nc.variables() +print ncvars +es = nc.var('time')[:] +units = nc.var('time').units +dt = [es2dt(e) for e in es] +dn = date2num(dt) +z = nc.var('z')[:] +wd = nc.var('water_depth')[:] +en = nc.var('en')[:] +u = nc.var('u')[:] +v = nc.var('v')[:] +# nc.close() + +# ---- open a new figure +fig = figure(figsize=(11, 8)) + +# ----- top plot +ax = fig.add_subplot(4,1,1) +ax.plot_date(dt, en, fmt='b-') + +# this is superfluous, since the autoscaler should get it right, but +# use date2num and num2date to to convert between dates and floats if +# you want; both date2num and num2date convert an instance or sequence +ax.set_xlim( dt[0], dt[-1] ) +setp(ax.xaxis, label_position='top') +ax.set_ylabel(nc.var('en').standard_name) +setp(ax, xlabel='Time') + +# The hour locator takes the hour or sequence of hours you want to +# tick, not the base multiple + +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('%d') ) +setp(ax, xticklabels=['']) + +# ----- middle plot +ax = fig.add_subplot(4,1,2) +ax.plot_date(dt, wd, fmt='b-') +ax.set_ylabel(nc.var('water_depth').standard_name) +ax.set_xlim( dt[0], 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('%d') ) +setp(ax, xticklabels=['']) +setp(ax, xlabel='') + +# ----- middle plot +ax = fig.add_subplot(4,1,3) +ax.pcolor(dn, z, u.transpose(), vmin=numpy.nanmin(u), vmax=numpy.nanmax(u)) +ax.plot_date(dt, wd, fmt='k-') +ax.set_ylabel(nc.var('u').standard_name) +ax.set_ylim(2,10) +ax.set_xlim(dt[0], 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('%d') ) +setp(ax, xticklabels=['']) +setp(ax, xlabel='') +# colorbar() + +# ----- bottom plot +ax = fig.add_subplot(4,1,4) +ax.pcolor(dn, z, v.transpose(), vmin=numpy.nanmin(v), vmax=numpy.nanmax(v)) +ax.plot_date(dt, wd, fmt='k-') +ax.set_ylabel(nc.var('v').standard_name) +ax.set_ylim(2,10) +ax.set_xlim(dt[0], 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('%d') ) +# setp(ax, xticklabels=['']) +setp(ax, xlabel='Time') +# colorbar() + +fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9) + +savefig('test_bogue_plot.png') +os.system('ls') +os.system('scp -r *.png haines@pitot.marine.unc.edu:/afs/isis.unc.edu/depts/marine/workspace/haines/public_html/nccoos/test') + Index: proc2plot/trunk/proc2plot/scratch/scr_top_part.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/scr_top_part.py (revision 326) @@ -1,0 +1,123 @@ +#!/usr/bin/env python +""" +Mock-up of adcp timeseries using pcolor and messing with plot stuff +1. pcolor adcp velocity components with depth +2. colormap +3. right-hand scale using second axis on top +4. legend +5. ylabel +""" + +import os, sys +import datetime, time +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/raw2proc') +del(sys) + +from pylab import figure, twinx, savefig, setp, getp, cm, colorbar +from matplotlib.ticker import FixedLocator, FormatStrFormatter +from matplotlib.dates import DayLocator, HourLocator, DateFormatter, date2num, num2date +from procutil import dt2es, es2dt + +print ' ... loading data for graph' +ncFile1='/home/haines/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_01.nc' +ncFile2='/home/haines/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_02.nc' +nc = pycdf.CDFMF((ncFile1, ncFile2)) +ncvars = nc.variables() +print ncvars +es = nc.var('time')[:] +units = nc.var('time').units +dt = [es2dt(e) for e in es] +dn = date2num(dt) +z = nc.var('z')[:] +wd = nc.var('water_depth')[:] +en = nc.var('en')[:] +u = nc.var('u')[:] +v = nc.var('v')[:] + +label_str = 'Current Velocity' + '(' + nc.var('u').units + ')' +# nc.close() + +# create a masked array for plotting +um = numpy.ma.masked_where(numpy.isnan(u), u) + +fig = figure(figsize=(10, 8)) +fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9) +ax = fig.add_subplot(4,1,1) +# pc = ax.pcolor(dn, z, u.transpose(), vmin=numpy.nanmin(u), vmax=numpy.nanmax(u)) + +# using masked array +# pc = ax.pcolor(dn, z, um.transpose(), vmin=numpy.nanmin(u), vmax=numpy.nanmax(u)) +# pc = ax.pcolor(dn, z, um.transpose(), vmin=um.min(), vmax=um.max()) +# pc = ax.pcolor(dn, z, um.T, vmin=um.min(), vmax=um.max()) +pc = ax.pcolor(dn, z, um.T, vmin=-0.5, vmax=0.5) +pc.set_label('True Eastward Current (m s-1)') +# in data coords +# ax.text(date2num(datetime.datetime(2008,1,2,0,0,0), 4, pc.get_label(), fontsize="small") +# in axes coords +ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Current Velocity (m s-1)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) +# cb.ax.xaxis.set_major_locator(FixedLocator([-0.4, -0.2, 0, 0.2, 0.4])) +# cb.ax.xaxis.set_major_formatter(FormatStrFormatter('%.1f')) + +# note that plot returns a list of lines. The "l1, = plot" usage +# extracts the first element of the list inot l1 using tuple +# unpacking. So l1 is a Line2D instance, not a sequence of lines +l1, = ax.plot_date(dt, wd, fmt='k-') +l1.set_label('Water Depth (m)') + +ax.set_ylabel('HAB (m)') +ax.set_ylim(2.,10.) +# 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') ) + +xlabel_str = 'Last 30 days' +ax.set_xlabel(xlabel_str) +# also can set properties +# setp(ax, xticklabels=['']) +# setp(ax, xlabel='') + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) meters to (rhs) feet +feet = [3.281*val for val in ax.get_ylim()] +ax2.set_ylim(feet) +ax2.set_ylabel('HAB (ft)') + +# legend (labels already specified above for pcolor and lines +# ax.legend(loc='upper left') +leg = ax.legend((l1,), (l1.get_label(),), loc='upper left') +# leg = gca().get_legend() +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 + +# see text.Text, lines.Line2D, and patches.Rectangle for more info on +# the settable properties of lines, text, and rectangles +frame.set_facecolor('0.80') # set the frame face color to light gray +frame.set_alpha(0.5) +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 + +savefig('test_colorbar.png') +os.system('ls *.png') +os.system('scp -r *.png haines@pitot.marine.unc.edu:/afs/isis.unc.edu/depts/marine/workspace/haines/public_html/nccoos/test') + Index: proc2plot/trunk/proc2plot/scratch/scr_which_to_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/scr_which_to_plot.py (revision 326) @@ -1,0 +1,49 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-05-12 14:59:00 haines> +"""bogue_dspec_plot""" + +import os, sys +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/raw2proc') +del(sys) + +import procutil + +def which_to_plot(odir, ncfn): + """which_to_plot finds which timestamp in netCDF data file + (ncfn) does not have a corresponding directional spectrum plot png + in output dir (odir) """ + + nc = pycdf.CDF(ncFile1) + # nc = pycdf.CDFMF((ncFile1, ncFile2)) + ncvars = nc.variables() + # print ncvars + es = nc.var('time')[:] + units = nc.var('time').units + dt = [procutil.es2dt(e) for e in es] + dts = [d.strftime('%Y_%m_%d_%H%M') for d in dt] + # list all pngs + import glob + gs = os.path.join(odir, '*.png') + all_pngs = glob.glob(gs) + ap = ''.join(all_pngs) + # find index of dts not in all_pngs + j_seq = [j for j in range(len(dts)) if ap.find(dts[j]) != -1] + # return index values to plot (j) + return j_seq + + +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/home/haines/test_data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_2008_03.nc' +# ncFile2='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_2008_03.nc' +# ncFile1='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile1='/seacoos/data/nccoos/level1/bogue/adcpwaves/bogue_adcpwaves_'+this_month.strftime('%Y_%m')+'.nc' +odir = os.path.join('/seacoos/data/nccoos/level3/bogue/adcpwaves/dspec', this_month.strftime('%Y_%m')) +if not os.path.exists(odir): + os.mkdir(odir) + +# j_seq = which_index_to_plot(odir, ncFile1) +# print j_seq Index: proc2plot/trunk/proc2plot/scratch/test_addnan.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_addnan.py (revision 326) @@ -1,0 +1,61 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2009-06-19 15:41:36 haines> +"""test add nan in time gap of data so plots lift pen at gap""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy +sys.path.append('/home/haines/nccoos/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 + +proc_dir = '/seacoos/data/nccoos/level1/crow/wq' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +fn = '/seacoos/data/nccoos/level1/crow/wq/crow_wq_2005_05.nc' +m=re.search('\d{4}_\d{2}', fn) +yyyy_mm = m.group() +prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + +print ' ... ... read: ' + fn +nc = pycdf.CDFMF((fn,)) +ncvars = nc.variables() +es = nc.var('time')[:] +units = nc.var('time').units +dt = [procutil.es2dt(e) for e in es] +dn = date2num(dt) +batt = nc.var('battvolts')[:] +nc.close() + +data = batt +delta_dn = numpy.diff(dn) +delta_dt = numpy.diff(dt) +sample_interval = numpy.median(delta_dn) +maxdelta = 2*sample_interval + +igap = (delta > maxdelta).nonzero()[0] +ngap = len(igap) +sample_interval = datetime.timedelta(sample_interval) + + +# do some error handling +# if ngap: +# return (new_dt, new_data) +# else: return (dt, data) + +# for each gap in data create NaN +data_insert = [numpy.nan for gap in igap] +# for each gap in time create datetime value +dt_insert = [dt[gap]+sample_interval for gap in igap] + +# insert new sample times at indices of the gaps +new_dt = numpy.insert(dt, igap+1, dt_insert) +# insert NaN data at the indices that match the above times +new_data = numpy.insert(data, igap+1, data_insert) Index: proc2plot/trunk/proc2plot/scratch/test_bogue_adcp_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_bogue_adcp_plot_month.py (revision 326) @@ -1,0 +1,176 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-16 12:00:20 haines> +"""bogue_adcp_plot_month""" + +# plot each month + +import os, sys +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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_adcp_plot ...' +yyyy_mm = '2008_05' +prev_month, this_month, next_month = procutil.find_months(yyyy_mm) +# ncFile1='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_2008_02.nc' +# 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' + +ncFile = '/seacoos/data/nccoos/level1/bogue/adcp/bogue_adcp_'+yyyy_mm+'.nc' + +# load data +print ' ... loading data for graph from ...' +print ' ... ... ' + ncFile +nc = pycdf.CDFMF((ncFile,)) +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) +z = nc.var('z')[:] +wd = nc.var('wd')[:] +wl = nc.var('wl')[:] +en = nc.var('en')[:] +u = nc.var('u')[:] +v = nc.var('v')[:] +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) + +####################################### +# Plot month +####################################### +print ' ... start month plot' +ax = fig.add_subplot(4,1,1) +axs = [ax] + +# use masked array to hide NaN's on plot +um = numpy.ma.masked_where(numpy.isnan(u), u) +pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) +pc.set_label('True Eastward Current (m s-1)') +ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Current Velocity (m s-1)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, wl, fmt='k-') +l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-8.,2.) +# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what +ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) +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 ADCP -- ' + yyyy_mm) + +# 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('Depth (ft)') + +# 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 +vm = numpy.ma.masked_where(numpy.isnan(v), v) +pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) +pc.set_label('True Northward Current (m s-1)') +ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, wl, fmt='k-') +l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-8.,2) + +# ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) +ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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 ADCP -- ' + yyyy_mm) + +# 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('Depth (ft)') + +# 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 + +# save figure +savefig('/home/haines/rayleigh/img/bogue/bogue_adcp_'+yyyy_mm+'.png') + Index: proc2plot/trunk/proc2plot/scratch/test_concat_platform_package_data.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_concat_platform_package_data.py (revision 326) @@ -1,0 +1,39 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-16 13:29:04 haines> +"""test_concat_data""" + +import os, sys, glob +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/raw2proc') +del(sys) + +import procutil + +# test with jpier adcp +print 'jpier_adcp ...' +proc_dir ='/seacoos/data/nccoos/level1/jpier/adcp/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() +nc = pycdf.CDFMF(fns) + +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) +z = nc.var('z')[:] +wd = nc.var('water_depth')[:] +u = nc.var('u')[:] +v = nc.var('v')[:] +nc.close() + +# averaged water depth over whole recorded deployment!! +print wd.mean() Index: proc2plot/trunk/proc2plot/scratch/test_crow_flow_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_crow_flow_plot.py (revision 326) @@ -1,0 +1,670 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2009-09-16 14:03:50 haines> +"""crow_all_plot""" + +import os, sys, glob, re +import datetime, time, dateutil, 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 'crow_all_plot ...' + +proc_dir = '/seacoos/data/nccoos/level1/crow/wq/' +# fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns = glob.glob((os.path.join(proc_dir, '*2009*.nc'))) +fns.sort() + +for fn in fns: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +#ncFile1='/seacoos/data/nccoos/level1/meet/wq/meet_wq_2009_08.nc' +#ncFile2='/seacoos/data/nccoos/level1/meet/wq/meet_wq_2008_09.nc' + +datadir = '/seacoos/data/nccoos/level1' +platform = 'meet' +package = 'wq' + +ncFile1 = os.path.join(datadir, platform, package, platform+'_'+package+'_'+prev_month.strftime('%Y_%m')+'.nc') +ncFile2 = os.path.join(datadir, platform, package, platform+'_'+package+'_'+this_month.strftime('%Y_%m')+'.nc') + +# load data + +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) + +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' + print ' ... NO PLOTS CREATED' + exit() + + + # get variable names + 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) + + z = nc.var('z')[:] + wtemp = nc.var('wtemp')[:] + cond = nc.var('cond')[:] + turb = nc.var('turb')[:] + ph = nc.var('ph')[:] + do_mg = nc.var('do_mg')[:] + do_sat = nc.var('do_sat')[:] + batt = nc.var('battvolts')[:] + nc.close() + +package = 'flow' +ncFile1 = os.path.join(datadir, platform, package, platform+'_'+package+'_'+prev_month.strftime('%Y_%m')+'.nc') +ncFile2 = os.path.join(datadir, platform, package, platform+'_'+package+'_'+this_month.strftime('%Y_%m')+'.nc') + +# load data + +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) + +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' + + # get variable names + ncvars = nc.variables() + # print ncvars + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + r2 = nc.var('rain')[:] # inches of rain in past 15 min + have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys() + have_press = 'press_wl' in ncvars.keys() or 'press_flow' in ncvars.keys() + + if have_press: + pwl2 = nc.var('press_wl')[:] # feet + pfl2 = nc.var('press_flow')[:] # cfs + + if have_sontek: + swl2 = nc.var('sontek_wl')[:] # feet + sfl2 = nc.var('sontek_flow')[:] # cfs + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot month all wq (plus flow) the timeseries data + ####################################### + + fig = figure(figsize=(10, 9)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + + ax = fig.add_subplot(6,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, wtemp) + ibad = y <= -6999. + y[ibad] = numpy.nan + # if all are NaN's then pyplot throws an error for autoscale + # for some reason setting one value in the timeseries to a real value + # fixes this. + if numpy.isnan(y).all(): + y[-1]=0. + + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Water Temperature') + + ax.set_ylabel('TEMP\n (deg C)') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) deg C to (rhs) deg F + f = [procutil.celsius2fahrenheit(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('(deg F)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('CROW Water Quality -- ' + yyyy_mm_str) + + # 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(6,1,2) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, cond) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Specific Conductivity') + + ax.set_ylabel('Cond\n (mS/cm)') + # ax.set_ylim(0,10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, do_mg) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Dissolved Oxygen (DO)') + + ax.set_ylabel('DO \n(mg/l)') + # ax.set_ylim(20,120) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + (x, y) = procutil.addnan(dt, do_sat) + l2, = ax2.plot_date(x, y, fmt='c-') + l2.set_label('Percent Air Saturation') + # convert (lhs) deg C to (rhs) deg F + # ax2.set_ylim(0,200) + ax2.set_ylabel('DO (%)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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') + 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(6,1,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, ph) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('pH') + + ax.set_ylabel('pH') + # ax.set_ylim(6, 12) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, turb) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Turbidity') + + ax.set_ylabel('Turb \n(NTU)') + # ax.set_ylim(0,100) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,6) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + if have_press: + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + ls2 = l2.get_label() + + ax.set_ylabel('Flow\n (cfs)') + # ax.set_ylim(0,15) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('CROW Water Quality -- ' + yyyy_mm_str) + + # legend + if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') + if have_sontek: + leg = ax.legend((l2,), (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 + ofn = '/home/haines/rayleigh/img/crow/crow_wq_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + del(fig) + + ####################################### + # Plot month all rain flow the timeseries data + ####################################### + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, r2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Precipitation') + + ax.set_ylabel('Rain (in)') + # ax.set_ylim(0,30) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) deg C to (rhs) deg F + f = [procutil.inches2millimeters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Rain (mm)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('CROW Stream Flow -- ' + yyyy_mm_str) + + # 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) + + # ax.plot returns a list of lines, so unpack tuple + if have_press: + (x, y) = procutil.addnan(dt2, pwl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() + if have_sontek: + (x, y) = procutil.addnan(dt2, swl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('From Acoustic Sounding') + ls2 = l2.get_label() + + ax.set_ylabel('Depth (ft)') + # ax.set_ylim(0,15) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) feet to (rhs) meters + f = [procutil.feet2meters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Depth (m)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # legend + if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') + elif have_sontek: + leg = ax.legend((l2,), (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,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + if have_press: + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + ls2 = l2.get_label() + + ax.set_ylabel('Flow (cfs)') + # ax.set_ylim(0,15) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') + elif have_sontek: + leg = ax.legend((l2,), (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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, batt) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Station Battery') + + ax.set_ylabel('Battery\n (volts)') + # ax.set_ylim(6, 12) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('CROW Stream Flow -- ' + yyyy_mm_str) + + # 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 + + # save figure + ofn = '/home/haines/rayleigh/img/crow/crow_flow_' + yyyy_mm_str + '.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + +####################################### +# Last 30 days +####################################### + +print ' ... Last 30 days' +ax = axs[0] +ax.set_xlim(date2num(dt[-1])-30, 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('CROW Stream Flow -- Last 30 days from ' + last_dt_str) + +ax = axs[1] +ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) +ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.set_xticklabels([]) + +ax = axs[2] +ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) +ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.set_xticklabels([]) + +ax = axs[3] +ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) +ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('CROW Stream Flow -- Last 7 days from ' + last_dt_str) + +# save figure +savefig('/home/haines/rayleigh/img/crow_flow_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('CROW Stream Flow -- 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(2,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(2,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(2,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('CROW Stream Flow -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/crow_flow_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('CROW Stream Flow -- 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('CROW Stream Flow -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/crow_flow_last01days.png') Index: proc2plot/trunk/proc2plot/scratch/test_crow_wq_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_crow_wq_plot.py (revision 326) @@ -1,0 +1,518 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2009-09-16 17:27:43 haines> +"""crow_wq_plot""" + +import os, sys, glob, re +import datetime, time, dateutil, 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 'crow_wq_plot ...' + +data_dir = '/seacoos/data/nccoos/level1' +platform = 'crow' +package = 'wq' +# yyyy_mm = None +# can be a past month +yyyy_mm = '2009_08' + +if not yyyy_mm: + yyyy_mm = procutil.this_month() + plot_latest = True +else: + plot_latest = False + +prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + +package1 = package +#ncFile1='/seacoos/data/nccoos/level1/meet/wq/meet_wq_2009_08.nc' +#ncFile2='/seacoos/data/nccoos/level1/meet/wq/meet_wq_2008_09.nc' +ncFile1 = os.path.join(data_dir, platform, package1, platform+'_'+package1+'_'+prev_month.strftime('%Y_%m')+'.nc') +ncFile2 = os.path.join(data_dir, platform, package1, platform+'_'+package1+'_'+this_month.strftime('%Y_%m')+'.nc') + +# load wq data + +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) + +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' + print ' ... NO PLOTS CREATED' + exit() + +# get variable names +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) + +z = nc.var('z')[:] +wtemp = nc.var('wtemp')[:] +cond = nc.var('cond')[:] +turb = nc.var('turb')[:] +ph = nc.var('ph')[:] +do_mg = nc.var('do_mg')[:] +do_sat = nc.var('do_sat')[:] +batt = nc.var('battvolts')[:] +nc.close() + +package2 = 'flow' +ncFile1 = os.path.join(data_dir, platform, package2, platform+'_'+package2+'_'+prev_month.strftime('%Y_%m')+'.nc') +ncFile2 = os.path.join(data_dir, platform, package2, platform+'_'+package2+'_'+this_month.strftime('%Y_%m')+'.nc') + +# load flow data + +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) + +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' + +# get variable names +ncvars = nc.variables() +# print ncvars +es2 = nc.var('time')[:] +units = nc.var('time').units +dt2 = [procutil.es2dt(e) for e in es2] +# set timezone info to UTC (since data from level1 should be in UTC!!) +dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] +dn2 = date2num(dt2) + +r2 = nc.var('rain')[:] # inches of rain in past 15 min +have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys() +have_press = 'press_wl' in ncvars.keys() or 'press_flow' in ncvars.keys() + +if have_press: + pwl2 = nc.var('press_wl')[:] # feet + pfl2 = nc.var('press_flow')[:] # cfs + +if have_sontek: + swl2 = nc.var('sontek_wl')[:] # feet + sfl2 = nc.var('sontek_flow')[:] # cfs + +nc.close() + +# last dt in data for labels +dtu = dt[-1] +dtl = dt_local[-1] + +diff = abs(dtu - dtl) +if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' +else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + +####################################### +# Plot month all wq (plus flow) the timeseries data +####################################### + +fig = figure(figsize=(10, 9)) +fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + +ax = fig.add_subplot(6,1,1) +axs = [ax] + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, wtemp) +ibad = y <= -6999. +y[ibad] = numpy.nan +# if all are NaN's then pyplot throws an error for autoscale +# for some reason setting one value in the timeseries to a real value +# fixes this. +if numpy.isnan(y).all(): + y[-1]=0. + +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Water Temperature') + +ax.set_ylabel('TEMP\n (deg C)') +ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) deg C to (rhs) deg F +f = [procutil.celsius2fahrenheit(val) for val in ax.get_ylim()] +ax2.set_ylim(f) +ax2.set_ylabel('(deg F)') + +ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) +ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) +ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) +ax2.set_xticklabels([]) + +# this only moves the label not the tick labels +ax.xaxis.set_label_position('top') +ax.set_xlabel('CROW Water Quality -- ' + yyyy_mm) + +# 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(6,1,2) +axs.append(ax) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, cond) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Specific Conductivity') + +ax.set_ylabel('Cond\n (mS/cm)') +# ax.set_ylim(0,10) +ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,3) +axs.append(ax) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, do_mg) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Dissolved Oxygen (DO)') + +ax.set_ylabel('DO \n(mg/l)') +# ax.set_ylim(20,120) +ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +(x, y) = procutil.addnan(dt, do_sat) +l2, = ax2.plot_date(x, y, fmt='c-') +l2.set_label('Percent Air Saturation') +# convert (lhs) deg C to (rhs) deg F +# ax2.set_ylim(0,200) +ax2.set_ylabel('DO (%)') +ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) +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') +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(6,1,4) +axs.append(ax) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, ph) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('pH') + +ax.set_ylabel('pH') +# ax.set_ylim(6, 12) +ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,5) +axs.append(ax) + +# ax.plot returns a list of lines, so unpack tuple +(x, y) = procutil.addnan(dt, turb) +l1, = ax.plot_date(x, y, fmt='b-') +l1.set_label('Turbidity') + +ax.set_ylabel('Turb \n(NTU)') +# ax.set_ylim(0,100) +ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,6) +axs.append(ax) + +# ax.plot returns a list of lines, so unpack tuple +if have_press: + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() +if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + ls2 = l2.get_label() + +ax.set_ylabel('Flow\n (cfs)') +# ax.set_ylim(0,15) +ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('CROW Water Quality -- ' + yyyy_mm) + +# legend +if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') +elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') +if have_sontek: + leg = ax.legend((l2,), (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 +ofn = '/home/haines/rayleigh/img/'+platform+'/'+platform+'_'+package+'_'+yyyy_mm+'.png' +print '... ... write: %s' % (ofn,) +savefig(ofn) + +if plot_latest: + ####################################### + # Last 30 days + ####################################### + + print ' ... Last 30 days' + ax = axs[0] + ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(0,32,2)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax.set_xticklabels([]) + ax.set_xlabel('CROW Water Quality -- Last 30 days from ' + last_dt_str) + + ax = axs[1] + ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(0,32,2)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax.set_xticklabels([]) + + ax = axs[2] + ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(0,32,2)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax.set_xticklabels([]) + + ax = axs[3] + ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(0,32,2)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax.set_xticklabels([]) + + ax = axs[4] + ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(0,32,2)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax.set_xticklabels([]) + + ax = axs[5] + ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(0,32,2)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('CROW Water Quality -- Last 7 days from ' + last_dt_str) + + # save figure + savefig('/home/haines/rayleigh/img/crow_wq_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('CROW Water Quality -- 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(2,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(2,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(2,32,1)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) + ax.set_xticklabels([]) + + ax = axs[4] + ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) + ax.set_xticklabels([]) + + ax = axs[5] + ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(2,32,1)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('CROW Water Quality -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/crow_wq_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('CROW Water Quality -- 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.set_xticklabels([]) + + ax = axs[4] + 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[5] + 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('CROW Water Quality -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/crow_wq_last01days.png') Index: proc2plot/trunk/proc2plot/scratch/test_jpier_adcp_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_jpier_adcp_plot_month.py (revision 326) @@ -1,0 +1,185 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-16 13:40:40 haines> +"""jpier_adcp_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'jpier_adcp_plot_month ...' +# yyyy_mm = '2008_05' +# yyyy_mm = '2008_06' +# ncFile1='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_2008_02.nc' +# ncFile1='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+prev_month.strftime('%Y_%m')+'.nc' +# ncFile2='/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+this_month.strftime('%Y_%m')+'.nc' +# ncFile = '/seacoos/data/nccoos/level1/jpier/adcp/jpier_adcp_'+yyyy_mm+'.nc' + +proc_dir = '/seacoos/data/nccoos/level1/jpier/adcp/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +for fn in fns: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + z = nc.var('z')[:] + wd = nc.var('wd')[:] + wl = nc.var('wl')[:] + u = nc.var('u')[:] + v = nc.var('v')[:] + 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) + +####################################### +# Plot month +####################################### + + ax = fig.add_subplot(4,1,1) + axs = [ax] + +# use masked array to hide NaN's on plot + um = numpy.ma.masked_where(numpy.isnan(u), u) + pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) + pc.set_label('True Eastward Current (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Current Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) + +# ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, wl, fmt='k-') + l1.set_label('Water Level') + + ax.set_ylabel('Depth (m)') + ax.set_ylim(-10.,2.) +# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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('Jpier ADCP -- ' + yyyy_mm) + +# 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('Depth (ft)') + +# 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 + vm = numpy.ma.masked_where(numpy.isnan(v), v) + pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) + pc.set_label('True Northward Current (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, wl, fmt='k-') + l1.set_label('Water Level') + + ax.set_ylabel('Depth (m)') + ax.set_ylim(-10.,2) + +# ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('Jpier ADCP -- ' + yyyy_mm) + +# 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('Depth (ft)') + +# 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 + +# save figure + + ofn = '/home/haines/rayleigh/img/jpier/jpier_adcp_'+yyyy_mm+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/scratch/test_jpier_quiver_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_jpier_quiver_plot.py (revision 326) @@ -1,0 +1,256 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-03 11:26:33 haines> +"""jpier_met_plot2""" + +import os, sys +import datetime, time, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'jpier_met_plot2 ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +#ncFile1='/seacoos/data/nccoos/level1/jpier/met/jpier_met_2008_01.nc' +#ncFile2='/seacoos/data/nccoos/level1/jpier/met/jpier_met_2008_02.nc' + +ncFile1='/seacoos/data/nccoos/level1/jpier/met/jpier_met_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/jpier/met/jpier_met_'+this_month.strftime('%Y_%m')+'.nc' + +# load data +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) + +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) +ws = nc.var('wspd')[:] +wd = nc.var('wdir')[:] +u = nc.var('wspd')[:] +v = nc.var('wspd')[:] + +i = 0 +for val in ws: + u[i] = -1.*procutil.wind_vector2u(ws[i],wd[i]) + v[i] = -1.*procutil.wind_vector2v(ws[i],wd[i]) + i += 1 +nc.close() + + +# 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] + +# use masked array to hide NaN's on plot +wd = numpy.ma.masked_where(numpy.isnan(wd), wd) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, wd, fmt='b-') +l1.set_label('Wind from Direction') + +ax.set_ylabel('Dir (true N)') +ax.set_ylim(0.,360.) +# 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('JPIER Met -- Last 30 days from ' + last_dt_str) + +# 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 +ws = numpy.ma.masked_where(numpy.isnan(ws), ws) + + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, ws, fmt='b-') +l1.set_label('Wind Speed') + + +ax.set_ylabel('Speed (m/s)') +ax.set_ylim(0.,20.) + +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) m/s to (rhs) knots +k = [procutil.meters_sec2knots(val) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +# 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,3) +axs.append(ax) + +# use masked array to hide NaN's on plot +# u = numpy.ma.masked_where(numpy.isnan(u), u) +# v = numpy.ma.masked_where(numpy.isnan(v), v) + +dt0 = numpy.zeros(len(dt)) + +ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) +ax.axhline(y=0, color='gray') +# make stick plot (set all head parameters to zero) +# scale to y-axis using units='height' of plot with scale = 40 (m/s range) per height with min=-20 and max=20 +q1 = ax.quiver(date2num(dt), dt0, u, v, units='height', scale=40, headwidth=0, headlength=0, headaxislength=0) +qk = ax.quiverkey(q1, 0.1, 0.8, 10, r'10 m s-1') + +ax.set_ylim(-20.,20.) +ax.set_ylabel('Speed (m/s)') + +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') ) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) m/s to (rhs) knots +k = [(val * 1.94384449) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +ax.set_xlabel('JPIER Met -- Last 30 days from ' + last_dt_str) + +# save figure +savefig('/home/haines/rayleigh/jpier_met2_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('JPIER Met -- 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.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('JPIER Met -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/jpier_met2_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('JPIER Met -- 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.xaxis.set_major_formatter( DateFormatter('%H') ) +ax.set_xlabel('JPIER Met -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/jpier_met2_last01days.png') + Index: proc2plot/trunk/proc2plot/scratch/test_load_ims_sodar.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_load_ims_sodar.py (revision 326) @@ -1,0 +1,51 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-11-21 15:19:36 haines> +"""ims_sodar_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'ims_sodar_plot_month ...' + +proc_dir = '/seacoos/data/nccoos/level1/ims/sodar/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +fn = fns[0] +m=re.search('\d{4}_\d{2}', fn) +yyyy_mm = m.group() +prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + +# load data +print ' ... ... read: ' + fn +nc = pycdf.CDFMF((fn,)) +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) +z = nc.var('z')[:]/100. # convert cm/s to m/s +u = nc.var('u')[:]/100. +v = nc.var('v')[:]/100. +w = nc.var('w')[:]/100. +e = nc.var('echo')[:] +nc.close() + Index: proc2plot/trunk/proc2plot/scratch/test_morgan_avp_data.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_morgan_avp_data.py (revision 326) @@ -1,0 +1,58 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2009-01-12 17:50:01 haines> +"""morgan_adcp_plot""" + +import os, sys +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 + +ncFile2='/seacoos/data/nccoos/level1/morgan/avp/morgan_avp_2008_09.nc' +nc = pycdf.CDFMF((ncFile2,)) +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) +wd = nc.var('wd')[:] + +ses = nc.var('stime')[:] +z = nc.var('z')[:] + +wtemp = nc.var('wtemp')[:] +salin = nc.var('salin')[:] +turb = nc.var('turb')[:] +ph = nc.var('ph')[:] +do = nc.var('do')[:] +chl = nc.var('chl')[:] +nc.close() + +# bin_size = sensor_info['bin_size'] # Bin Size (meters) +bin_size = 0.1 # 10cm or 0.1m +# nominal_depth = platform_info['water_depth'] # Mean sea level at station (meters) or nominal water depth +nominal_depth = 4.0 # m +yi = numpy.arange(0, -1*nominal_depth, -1*bin_size, dtype=float) +xi = es + +zi = numpy.array(numpy.ones((len(xi),len(yi)), dtype=float)*numpy.nan) + +i=0 +ibin = ((z[0,:])<=yi[0])*(z[0,:]yi[2]) +i,j = xxx.nonzero + Index: proc2plot/trunk/proc2plot/scratch/test_quiver_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/scratch/test_quiver_plot.py (revision 326) @@ -1,0 +1,259 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-09-17 15:52:54 haines> +"""morgan_met_plot""" + +import os, sys +import datetime, time, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'morgan_met_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +#ncFile1='/seacoos/data/nccoos/level1/morgan/met/morgan_met_2008_01.nc' +#ncFile2='/seacoos/data/nccoos/level1/morgan/met/morgan_met_2008_02.nc' + +ncFile1='/seacoos/data/nccoos/level1/morgan/met/morgan_met_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/morgan/met/morgan_met_'+this_month.strftime('%Y_%m')+'.nc' + +# load data +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) + +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) +ws = nc.var('wspd')[:] +wd = nc.var('wdir')[:] +cdir = nc.var('cdir')[:] +u = nc.var('u')[:] +v = nc.var('v')[:] + +# i = 0 +# for val in ws: +# u[i] = procutil.wind_vector2u(ws[i],wd[i]) +# v[i] = procutil.wind_vector2v(ws[i],wd[i]) +# i += 1 +# nc.close() + + +# 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] + +# use masked array to hide NaN's on plot +v = numpy.ma.masked_where(numpy.isnan(v), v) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, v, fmt='b-') +l1.set_label('Northward wind component') + +ax.set_ylabel('Speed N (m/s)') +ax.set_ylim(-20.,20.) + +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) m/s to (rhs) knots +k = [procutil.meters_sec2knots(val) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed N (knots)') + +# 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 +u = numpy.ma.masked_where(numpy.isnan(u), u) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, u, fmt='b-') +l1.set_label('Eastward wind component') + +ax.set_ylabel('Speed E (m/s)') +ax.set_ylim(-20.,20.) + +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.set_xticklabels([]) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) m/s to (rhs) knots +k = [(val * 1.94384449) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed E (knots)') + +# 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,3) +axs.append(ax) + +# use masked array to hide NaN's on plot +# u = numpy.ma.masked_where(numpy.isnan(u), u) +# v = numpy.ma.masked_where(numpy.isnan(v), v) + +dt0 = numpy.zeros(len(dt)) + +ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) +ax.axhline(y=0, color='gray') +# make stick plot (set all head parameters to zero) +# scale to y-axis using units='height' of plot with scale = 40 (m/s range) per height with min=-20 and max=20 +q1 = ax.quiver(date2num(dt), dt0, u, v, units='height', scale=40, headwidth=0, headlength=0, headaxislength=0) +qk = ax.quiverkey(q1, 0.1, 0.8, 10, r'10 m s-1') + +ax.set_ylim(-20.,20.) +ax.set_ylabel('Speed (m/s)') + +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') ) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) m/s to (rhs) knots +k = [(val * 1.94384449) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +ax.set_xlabel('Morgan Bay Wind -- Last 30 days from ' + last_dt_str) + + +# save figure +savefig('/home/haines/rayleigh/test_met_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('Morgan Bay Wind -- 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.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('Morgan Bay Wind -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/test_met_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('Morgan Bay Wind -- 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.xaxis.set_major_formatter( DateFormatter('%H') ) +ax.set_xlabel('Morgan Bay Wind -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/test_met_last01days.png') + Index: proc2plot/trunk/proc2plot/spin/spin_crow_all_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/spin/spin_crow_all_plot_month.py (revision 326) @@ -1,0 +1,518 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-05 11:59:42 haines> +"""crow_all_plot_month""" + +import os, sys, glob, re +import datetime, time, dateutil, 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 'crow_all_plot_month ...' + +proc_dir = '/seacoos/data/nccoos/level1/crow/wq/' +# fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns = glob.glob((os.path.join(proc_dir, '*2009_1?*.nc'))) +fns.sort() + +for fn in fns: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + + z = nc.var('z')[:] + wtemp = nc.var('wtemp')[:] + cond = nc.var('cond')[:] + turb = nc.var('turb')[:] + ph = nc.var('ph')[:] + do_mg = nc.var('do_mg')[:] + do_sat = nc.var('do_sat')[:] + batt = nc.var('battvolts')[:] + nc.close() + + fn_flow = '/seacoos/data/nccoos/level1/crow/flow/crow_flow_' + yyyy_mm_str + '.nc' + print ' ... ... read: ' + fn_flow + nc = pycdf.CDFMF((fn_flow,)) + ncvars = nc.variables() + # print ncvars + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + r2 = nc.var('rain')[:] # inches of rain in past 15 min + have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys() + have_press = 'press_wl' in ncvars.keys() or 'press_flow' in ncvars.keys() + + if have_press: + pwl2 = nc.var('press_wl')[:] # feet + pfl2 = nc.var('press_flow')[:] # cfs + + if have_sontek: + swl2 = nc.var('sontek_wl')[:] # feet + sfl2 = nc.var('sontek_flow')[:] # cfs + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot month all wq (plus flow) the timeseries data + ####################################### + + fig = figure(figsize=(10, 9)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + + ax = fig.add_subplot(6,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, wtemp) + ibad = y <= -6999. + y[ibad] = numpy.nan + # if all are NaN's then pyplot throws an error for autoscale + # for some reason setting one value in the timeseries to a real value + # fixes this. + if numpy.isnan(y).all(): + y[-1]=0. + + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Water Temperature') + + ax.set_ylabel('TEMP\n (deg C)') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) deg C to (rhs) deg F + f = [procutil.celsius2fahrenheit(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('(deg F)') + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('CROW Water Quality -- ' + yyyy_mm_str) + + # 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(6,1,2) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, cond) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Specific Conductivity') + + ax.set_ylabel('Cond\n (mS/cm)') + # ax.set_ylim(0,10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, do_mg) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Dissolved Oxygen (DO)') + + ax.set_ylabel('DO \n(mg/l)') + # ax.set_ylim(20,120) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + (x, y) = procutil.addnan(dt, do_sat) + l2, = ax2.plot_date(x, y, fmt='c-') + l2.set_label('Percent Air Saturation') + # convert (lhs) deg C to (rhs) deg F + # ax2.set_ylim(0,200) + ax2.set_ylabel('DO (%)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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') + 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(6,1,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, ph) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('pH') + + ax.set_ylabel('pH') + # ax.set_ylim(6, 12) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, turb) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Turbidity') + + ax.set_ylabel('Turb \n(NTU)') + # ax.set_ylim(0,100) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,6) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + if have_press: + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + ls2 = l2.get_label() + + ax.set_ylabel('Flow\n (cfs)') + # ax.set_ylim(0,15) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('CROW Water Quality -- ' + yyyy_mm_str) + + # legend + if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') + if have_sontek: + leg = ax.legend((l2,), (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 + ofn = '/home/haines/rayleigh/img/crow/crow_wq_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + del(fig) + + ####################################### + # Plot month all rain flow the timeseries data + ####################################### + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, r2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Precipitation') + + ax.set_ylabel('Rain (in)') + # ax.set_ylim(0,30) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) deg C to (rhs) deg F + f = [procutil.inches2millimeters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Rain (mm)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('CROW Stream Flow -- ' + yyyy_mm_str) + + # 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) + + # ax.plot returns a list of lines, so unpack tuple + if have_press: + (x, y) = procutil.addnan(dt2, pwl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() + if have_sontek: + (x, y) = procutil.addnan(dt2, swl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('From Acoustic Sounding') + ls2 = l2.get_label() + + ax.set_ylabel('Depth (ft)') + # ax.set_ylim(0,15) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) feet to (rhs) meters + f = [procutil.feet2meters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Depth (m)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # legend + if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') + elif have_sontek: + leg = ax.legend((l2,), (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,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + if have_press: + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + ls1 = l1.get_label() + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + ls2 = l2.get_label() + + ax.set_ylabel('Flow (cfs)') + # ax.set_ylim(0,15) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + if have_press and have_sontek: + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + elif have_press: + leg = ax.legend((l1,), (ls1,), loc='upper left') + elif have_sontek: + leg = ax.legend((l2,), (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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, batt) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Station Battery') + + ax.set_ylabel('Battery\n (volts)') + # ax.set_ylim(6, 12) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('CROW Stream Flow -- ' + yyyy_mm_str) + + # 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 + + # save figure + ofn = '/home/haines/rayleigh/img/crow/crow_flow_' + yyyy_mm_str + '.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/spin/spin_df_sodar1_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/spin/spin_df_sodar1_plot_month.py (revision 326) @@ -1,0 +1,274 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2009-02-18 15:45:58 haines> +"""spin_df_sodar_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'dukeforest_sodar_plot_month ...' + +proc_dir = '/seacoos/data/nccoos/level1/dukeforest/sodar/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +fns = fns[0:2] + +for fn in fns: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + z = nc.var('z')[:] + # convert cm/s to m/s + uu = nc.var('u')[:]/100. + vv = nc.var('v')[:]/100. + ww = nc.var('w')[:]/100. + echo = nc.var('echo')[:] + n1 = numpy.floor(nc.var('nois1')[:]/10.) + n2 = numpy.floor(nc.var('nois2')[:]/10.) + n3 = numpy.floor(nc.var('nois3')[:]/10.) + nc.close() + + # 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, 10)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + +####################################### +# Plot month +####################################### + + ax = fig.add_subplot(5,1,1) + axs = [ax] + + # range for horizontal wind plots + cmin, cmax = (-20., 20.) + # print "%s : %g %g" % ('uv wind', cmin, cmax) +# use masked array to hide NaN's on plot + um = numpy.ma.masked_where(numpy.isnan(uu), uu) + pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) + pc.set_label('True Eastward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2.) + +# setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Wind Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + #cb.ax.set_xticklabels([-16, -8, 0, 8, 16]) + +# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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('DUKE FOREST SODAR -- ' + yyyy_mm) + +# 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('Height (ft)') + +####################################### +# + ax = fig.add_subplot(5,1,2) + axs.append(ax) + +# use masked array to hide NaN's on plot + vm = numpy.ma.masked_where(numpy.isnan(vv), vv) + pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) + pc.set_label('True Northward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,3) + axs.append(ax) + + # range for horizontal wind plots + cmin, cmax = (-5., 5.) + # print "%s : %g %g" % ('w wind', cmin, cmax) + + # use masked array to hide NaN's on plot + wm = numpy.ma.masked_where(numpy.isnan(ww), ww) + pc = ax.pcolor(dn, z, wm.T, vmin=cmin, vmax=cmax) + # pc.set_label('Upward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Upward Wind Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,4) + axs.append(ax) + + cmin, cmax = (0., 1000.) + # print "%s : %g %g" % ('echo', cmin, cmax) + + # use masked array to hide NaN's on plot + em = numpy.ma.masked_where(numpy.isnan(echo), echo) + pc = ax.pcolor(dn, z, em.T, vmin=cmin, vmax=cmax) + # pc.set_label('Echo Strength ()') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Echo Strength') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.0, 1.0]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([str(cmin), str(cmax)]) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, n1, fmt='b-') + l1.set_label('n1 each beam') + l2, = ax.plot_date(dt, n2, fmt='c-') + l2.set_label('n2') + l3, = ax.plot_date(dt, n3, fmt='g-') + l3.set_label('n3') + + ax.set_ylabel('Ambient Noise') + ax.set_ylim(20,80) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('DUKE FOREST SODAR -- ' + yyyy_mm) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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 + + ofn = '/home/haines/rayleigh/img/dukeforest/dukeforest_sodar1_'+yyyy_mm+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/spin/spin_df_sodar2_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/spin/spin_df_sodar2_plot_month.py (revision 326) @@ -1,0 +1,312 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-12-05 12:39:39 haines> +"""dukeforest_sodar_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'dukeforest_sodar2_plot_month ...' + +proc_dir = '/seacoos/data/nccoos/level1/dukeforest/sodar/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +for fn in fns: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + + z = nc.var('z')[:] + + val1 = nc.var('val1')[:] + val2 = nc.var('val2')[:] + val3 = nc.var('val3')[:] + + spu1 = nc.var('spu1')[:] + spu2 = nc.var('spu2')[:] + spu3 = nc.var('spu3')[:] + + n1 = numpy.floor(nc.var('nois1')[:]/10.) + n2 = numpy.floor(nc.var('nois2')[:]/10.) + n3 = numpy.floor(nc.var('nois3')[:]/10.) + + s1 = numpy.floor(nc.var('snr1')[:]/10.) + s2 = numpy.floor(nc.var('snr2')[:]/10.) + s3 = numpy.floor(nc.var('snr3')[:]/10.) + + r1 = 10.*(nc.var('snr1')[:]-10.*s1) + r2 = 10.*(nc.var('snr2')[:]-10.*s2) + r3 = 10.*(nc.var('snr3')[:]-10.*s3) + + fe11 = nc.var('fe11')[:] + fe12 = nc.var('fe12')[:] + fe21 = nc.var('fe21')[:] + fe22 = nc.var('fe22')[:] + + nc.close() + + # 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) + + ####################################### + # Plot month + ####################################### + + ax = fig.add_subplot(6,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, val1, fmt='b-') + l1.set_label('val1 per beam') + l2, = ax.plot_date(dt, val2, fmt='c-') + l2.set_label('val2') + l3, = ax.plot_date(dt, val3, fmt='g-') + l3.set_label('val3') + + ax.set_ylabel('Validations') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('DUKEFOREST SODAR -- ' + yyyy_mm) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,2) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, spu1, fmt='b-') + l1.set_label('spu1 (<=3 OK)') + l2, = ax.plot_date(dt, spu2, fmt='c-') + l2.set_label('spu2') + l3, = ax.plot_date(dt, spu3, fmt='g-') + l3.set_label('spu3') + + ax.set_ylabel('False\n Signal') + ax.set_ylim(0,10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, n1, fmt='b-') + l1.set_label('n1') + l2, = ax.plot_date(dt, n2, fmt='c-') + l2.set_label('n2') + l3, = ax.plot_date(dt, n3, fmt='g-') + l3.set_label('n3') + + ax.set_ylabel('Ambient\n Noise') + ax.set_ylim(20,80) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, s1, fmt='b-') + l1.set_label('s1 SNR') + l2, = ax.plot_date(dt, s2, fmt='c-') + l2.set_label('s2') + l3, = ax.plot_date(dt, s3, fmt='g-') + l3.set_label('s3') + + ax.set_ylabel('Signal to \nNoise') + ax.set_ylim(6, 10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, r1, fmt='b-') + l1.set_label('BW1 (<50% bad)') + l2, = ax.plot_date(dt, r2, fmt='c-') + l2.set_label('BW2') + l3, = ax.plot_date(dt, r3, fmt='g-') + l3.set_label('BW3') + + ax.set_ylabel('Percent\n Total\n Bandwidth') + ax.set_ylim(0,100) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,6) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, fe11, fmt='b-') + l1.set_label('fe11 (9 max)') + l2, = ax.plot_date(dt, fe12, fmt='c-') + l2.set_label('fe12') + l3, = ax.plot_date(dt, fe21, fmt='g-') + l3.set_label('fe21') + l4, = ax.plot_date(dt, fe22, fmt='k-') + l4.set_label('fe22') + + ax.set_ylabel('Number of \nfreq emitted') + ax.set_ylim(0,10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('DUKEFOREST SODAR -- ' + yyyy_mm) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + ls4 = l4.get_label() + leg = ax.legend((l1,l2,l3,l4), (ls1,ls2,ls3,ls4), 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 + + ofn = '/home/haines/rayleigh/img/dukeforest/dukeforest_sodar2_'+yyyy_mm+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/spin/spin_ims_sodar1_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/spin/spin_ims_sodar1_plot_month.py (revision 326) @@ -1,0 +1,290 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-12-17 15:37:00 haines> +"""ims_sodar_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'ims_sodar_plot_month ...' + +proc_dir = '/seacoos/data/nccoos/level1/ims/sodar/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +print len(fns) + +for fn in fns[14:16]: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + z = nc.var('z')[:] + # convert cm/s to m/s + uu = nc.var('u')[:]/100. + vv = nc.var('v')[:]/100. + ww = nc.var('w')[:]/100. + echo = nc.var('echo')[:] + n1 = numpy.floor(nc.var('nois1')[:]/10.) + n2 = numpy.floor(nc.var('nois2')[:]/10.) + n3 = numpy.floor(nc.var('nois3')[:]/10.) + nc.close() + + # retain original dt for addnan + dto = dt + + # 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, 10)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + +####################################### +# Plot month +####################################### + + ax = fig.add_subplot(5,1,1) + axs = [ax] + + # range for horizontal wind plots + cmin, cmax = (-20., 20.) + # print "%s : %g %g" % ('uv wind', cmin, cmax) + # use masked array to hide NaN's on plot + (dt, uu) = procutil.addnan(dto, uu) + dn = date2num(dt) + um = numpy.ma.masked_where(numpy.isnan(uu), uu) + pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) + pc.set_label('True Eastward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2.) + +# setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Wind Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + #cb.ax.set_xticklabels([-16, -8, 0, 8, 16]) + +# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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('IMS SODAR -- ' + yyyy_mm) + +# 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('Height (ft)') + +####################################### +# + ax = fig.add_subplot(5,1,2) + axs.append(ax) + +# use masked array to hide NaN's on plot + (dt, vv) = procutil.addnan(dto, vv) + dn = date2num(dt) + vm = numpy.ma.masked_where(numpy.isnan(vv), vv) + pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) + pc.set_label('True Northward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,3) + axs.append(ax) + + # range for horizontal wind plots + cmin, cmax = (-5., 5.) + # print "%s : %g %g" % ('w wind', cmin, cmax) + + # use masked array to hide NaN's on plot + (dt, ww) = procutil.addnan(dto, ww) + dn = date2num(dt) + wm = numpy.ma.masked_where(numpy.isnan(ww), ww) + pc = ax.pcolor(dn, z, wm.T, vmin=cmin, vmax=cmax) + # pc.set_label('Upward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Upward Wind Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(5,1,4) + axs.append(ax) + + cmin, cmax = (0., 1000.) + # print "%s : %g %g" % ('echo', cmin, cmax) + + # use masked array to hide NaN's on plot + (dt, echo) = procutil.addnan(dto, echo) + dn = date2num(dt) + em = numpy.ma.masked_where(numpy.isnan(echo), echo) + pc = ax.pcolor(dn, z, em.T, vmin=cmin, vmax=cmax) + # pc.set_label('Echo Strength ()') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l+0.04, b+h-0.06, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Echo Strength') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.0, 1.0]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([str(cmin), str(cmax)]) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + + ####################################### + # + ax = fig.add_subplot(5,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (dt, n1) = procutil(dto, n1) + l1, = ax.plot_date(dt, n1, fmt='b-') + l1.set_label('n1 each beam') + (dt, n2) = procutil(dto, n2) + l2, = ax.plot_date(dt, n2, fmt='c-') + l2.set_label('n2') + (dt, n3) = procutil(dto, n3) + l3, = ax.plot_date(dt, n3, fmt='g-') + l3.set_label('n3') + + ax.set_ylabel('Ambient\n Noise') + ax.set_ylim(20,120) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('IMS SODAR -- ' + yyyy_mm) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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 + + ofn = '/home/haines/rayleigh/img/ims/ims_sodar1_'+yyyy_mm+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/spin/spin_ims_sodar2_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/spin/spin_ims_sodar2_plot_month.py (revision 326) @@ -1,0 +1,312 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-12-05 12:38:36 haines> +"""ims_sodar_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'ims_sodar2_plot_month ...' + +proc_dir = '/seacoos/data/nccoos/level1/ims/sodar/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +for fn in fns: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + + z = nc.var('z')[:] + + val1 = nc.var('val1')[:] + val2 = nc.var('val2')[:] + val3 = nc.var('val3')[:] + + spu1 = nc.var('spu1')[:] + spu2 = nc.var('spu2')[:] + spu3 = nc.var('spu3')[:] + + n1 = numpy.floor(nc.var('nois1')[:]/10.) + n2 = numpy.floor(nc.var('nois2')[:]/10.) + n3 = numpy.floor(nc.var('nois3')[:]/10.) + + s1 = numpy.floor(nc.var('snr1')[:]/10.) + s2 = numpy.floor(nc.var('snr2')[:]/10.) + s3 = numpy.floor(nc.var('snr3')[:]/10.) + + r1 = 10.*(nc.var('snr1')[:]-10.*s1) + r2 = 10.*(nc.var('snr2')[:]-10.*s2) + r3 = 10.*(nc.var('snr3')[:]-10.*s3) + + fe11 = nc.var('fe11')[:] + fe12 = nc.var('fe12')[:] + fe21 = nc.var('fe21')[:] + fe22 = nc.var('fe22')[:] + + nc.close() + + # 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) + + ####################################### + # Plot month + ####################################### + + ax = fig.add_subplot(6,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, val1, fmt='b-') + l1.set_label('val1 per beam') + l2, = ax.plot_date(dt, val2, fmt='c-') + l2.set_label('val2') + l3, = ax.plot_date(dt, val3, fmt='g-') + l3.set_label('val3') + + ax.set_ylabel('Validations') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('IMS SODAR -- ' + yyyy_mm) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,2) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, spu1, fmt='b-') + l1.set_label('spu1 (<=3 OK)') + l2, = ax.plot_date(dt, spu2, fmt='c-') + l2.set_label('spu2') + l3, = ax.plot_date(dt, spu3, fmt='g-') + l3.set_label('spu3') + + ax.set_ylabel('False\n Signal') + ax.set_ylim(0,10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, n1, fmt='b-') + l1.set_label('n1') + l2, = ax.plot_date(dt, n2, fmt='c-') + l2.set_label('n2') + l3, = ax.plot_date(dt, n3, fmt='g-') + l3.set_label('n3') + + ax.set_ylabel('Ambient\n Noise') + ax.set_ylim(20,120) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, s1, fmt='b-') + l1.set_label('s1 SNR') + l2, = ax.plot_date(dt, s2, fmt='c-') + l2.set_label('s2') + l3, = ax.plot_date(dt, s3, fmt='g-') + l3.set_label('s3') + + ax.set_ylabel('Signal to \nNoise') + ax.set_ylim(6, 12) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, r1, fmt='b-') + l1.set_label('BW1 (<50% bad)') + l2, = ax.plot_date(dt, r2, fmt='c-') + l2.set_label('BW2') + l3, = ax.plot_date(dt, r3, fmt='g-') + l3.set_label('BW3') + + ax.set_ylabel('Percent\n Total\n Bandwidth') + ax.set_ylim(0,100) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + leg = ax.legend((l1,l2,l3), (ls1,ls2,ls3), 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(6,1,6) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, fe11, fmt='b-') + l1.set_label('fe11 (9 max)') + l2, = ax.plot_date(dt, fe12, fmt='c-') + l2.set_label('fe12') + l3, = ax.plot_date(dt, fe21, fmt='g-') + l3.set_label('fe21') + l4, = ax.plot_date(dt, fe22, fmt='k-') + l4.set_label('fe22') + + ax.set_ylabel('Number of \nfreq emitted') + ax.set_ylim(0,15) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('IMS SODAR -- ' + yyyy_mm) + + # legend + ls1 = l1.get_label() + ls2 = l2.get_label() + ls3 = l3.get_label() + ls4 = l4.get_label() + leg = ax.legend((l1,l2,l3,l4), (ls1,ls2,ls3,ls4), 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 + + ofn = '/home/haines/rayleigh/img/ims/ims_sodar2_'+yyyy_mm+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/spin/spin_ims_sodar_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/spin/spin_ims_sodar_plot_month.py (revision 326) @@ -1,0 +1,234 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-12-09 16:47:29 haines> +"""ims_sodar_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'ims_sodar1_plot_month ...' + +proc_dir = '/seacoos/data/nccoos/level1/ims/sodar/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +for fn in fns[-1:-3]: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + z = nc.var('z')[:]/100. # convert cm/s to m/s + uu = nc.var('u')[:]/100. + vv = nc.var('v')[:]/100. + ww = nc.var('w')[:]/100. + echo = nc.var('echo')[:] + nc.close() + + # 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) + +####################################### +# Plot month +####################################### + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # range for horizontal wind plots + cmin, cmax = (-20., 20.) + # print "%s : %g %g" % ('uv wind', cmin, cmax) +# use masked array to hide NaN's on plot + um = numpy.ma.masked_where(numpy.isnan(uu), uu) + pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) + pc.set_label('True Eastward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2.) + +# setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Wind Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + #cb.ax.set_xticklabels([-16, -8, 0, 8, 16]) + +# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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('IMS SODAR -- ' + yyyy_mm) + +# 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('Height (ft)') + +####################################### +# + ax = fig.add_subplot(4,1,2) + axs.append(ax) + +# use masked array to hide NaN's on plot + vm = numpy.ma.masked_where(numpy.isnan(vv), vv) + pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) + pc.set_label('True Northward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(4,1,3) + axs.append(ax) + + # range for horizontal wind plots + cmin, cmax = (-5., 5.) + # print "%s : %g %g" % ('w wind', cmin, cmax) + + # use masked array to hide NaN's on plot + wm = numpy.ma.masked_where(numpy.isnan(ww), ww) + pc = ax.pcolor(dn, z, wm.T, vmin=cmin, vmax=cmax) + # pc.set_label('Upward Wind (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l+0.04, b+h-0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Upward Wind Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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('Height (ft)') + + ####################################### + # + ax = fig.add_subplot(4,1,4) + axs.append(ax) + + cmin, cmax = (0., 1000.) + # print "%s : %g %g" % ('echo', cmin, cmax) + + # use masked array to hide NaN's on plot + em = numpy.ma.masked_where(numpy.isnan(echo), echo) + pc = ax.pcolor(dn, z, em.T, vmin=cmin, vmax=cmax) + # pc.set_label('Echo Strength ()') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + ax.set_ylabel('Height (m)') + # ax.set_ylim(-26.,2) + + # setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l+0.04, b+h-0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Echo Strength') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) + cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + + # ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('IMS SODAR -- ' + yyyy_mm) + + # 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('Height (ft)') + + + # save figure + + ofn = '/home/haines/rayleigh/img/ims/ims_sodar1_'+yyyy_mm+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/spin/spin_lsrb_adcp_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/spin/spin_lsrb_adcp_plot_month.py (revision 326) @@ -1,0 +1,185 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-22 13:43:15 haines> +"""lsrb_adcp_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'lsrb_adcp_plot_month ...' +# yyyy_mm = '2008_05' +# yyyy_mm = '2008_06' +# ncFile1='/seacoos/data/nccoos/level1/lsrb/adcp/lsrb_adcp_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/lsrb/adcp/lsrb_adcp_2008_02.nc' +# ncFile1='/seacoos/data/nccoos/level1/lsrb/adcp/lsrb_adcp_'+prev_month.strftime('%Y_%m')+'.nc' +# ncFile2='/seacoos/data/nccoos/level1/lsrb/adcp/lsrb_adcp_'+this_month.strftime('%Y_%m')+'.nc' +# ncFile = '/seacoos/data/nccoos/level1/lsrb/adcp/lsrb_adcp_'+yyyy_mm+'.nc' + +proc_dir = '/seacoos/data/nccoos/level1/lsrb/adcp/' +fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns.sort() + +for fn in fns: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + z = nc.var('z')[:] + wd = nc.var('wd')[:] + wl = nc.var('wl')[:] + u = nc.var('u')[:] + v = nc.var('v')[:] + 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) + +####################################### +# Plot month +####################################### + + ax = fig.add_subplot(4,1,1) + axs = [ax] + +# use masked array to hide NaN's on plot + um = numpy.ma.masked_where(numpy.isnan(u), u) + pc = ax.pcolor(dn, z, um.T, vmin=cmin, vmax=cmax) + pc.set_label('True Eastward Current (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# setup colorbar axes instance. + l,b,w,h = ax.get_position() + cax = fig.add_axes([l, b+h+0.04, 0.25*w, 0.03]) + + cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar + cb.set_label('Current Velocity (m s-1)') + cb.ax.xaxis.set_label_position('top') + cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) + cb.ax.set_xticklabels([-0.4, -0.2, 0, 0.2, 0.4]) + +# ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, wl, fmt='k-') + l1.set_label('Water Level') + + ax.set_ylabel('Depth (m)') + ax.set_ylim(-26.,2.) +# ax.set_xlim(dt[0], dt[-1]) # first to last regardless of what + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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('LSRB ADCP -- ' + yyyy_mm) + +# 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('Depth (ft)') + +# 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 + vm = numpy.ma.masked_where(numpy.isnan(v), v) + pc = ax.pcolor(dn, z, vm.T, vmin=cmin, vmax=cmax) + pc.set_label('True Northward Current (m s-1)') + ax.text(0.025, 0.1, pc.get_label(), fontsize="small", transform=ax.transAxes) + +# ax.plot returns a list of lines, so unpack tuple + l1, = ax.plot_date(dt, wl, fmt='k-') + l1.set_label('Water Level') + + ax.set_ylabel('Depth (m)') + ax.set_ylim(-26.,2) + +# ax.set_xlim(date2num(dt[0]), date2num(dt[-1])) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('LSRB ADCP -- ' + yyyy_mm) + +# 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('Depth (ft)') + +# 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 + +# save figure + + ofn = '/home/haines/rayleigh/img/lsrb/lsrb_adcp_'+yyyy_mm+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/spin/spin_meet_all_plot_month.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/spin/spin_meet_all_plot_month.py (revision 326) @@ -1,0 +1,525 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-03-30 14:15:02 haines> +"""meet_all_plot_month""" + +# plot each month + +import os, sys, glob, re +import datetime, time, dateutil, 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 'meet_wq_plot_month ...' + +proc_dir = '/seacoos/data/nccoos/level1/meet/wq/' +# fns = glob.glob((os.path.join(proc_dir, '*.nc'))) +fns = glob.glob((os.path.join(proc_dir, '*2009*.nc'))) +fns.sort() + + + +for fn in fns: + m=re.search('\d{4}_\d{2}', fn) + yyyy_mm = m.group() + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + # load data + print ' ... ... read: ' + fn + nc = pycdf.CDFMF((fn,)) + 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) + + z = nc.var('z')[:] + wtemp = nc.var('wtemp')[:] + cond = nc.var('cond')[:] + turb = nc.var('turb')[:] + ph = nc.var('ph')[:] + do_mg = nc.var('do_mg')[:] + do_sat = nc.var('do_sat')[:] + batt = nc.var('battvolts')[:] + nc.close() + + fn_flow = '/seacoos/data/nccoos/level1/meet/flow/meet_flow_' + yyyy_mm_str + '.nc' + print ' ... ... read: ' + fn_flow + nc = pycdf.CDFMF((fn_flow,)) + ncvars = nc.variables() + # print ncvars + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + r2 = nc.var('rain')[:] # inches of rain in past 15 min + have_sontek = False # don't plot sontek + # have_sontek = 'sontek_wl' in ncvars.keys() or 'sontek_flow' in ncvars.keys() + pwl2 = nc.var('press_wl')[:] # feet + pfl2 = nc.var('press_flow')[:] # cfs + + if have_sontek: + swl2 = nc.var('sontek_wl')[:] # feet + sfl2 = nc.var('sontek_flow')[:] # cfs + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot month all wq (plus flow) the timeseries data + ####################################### + + fig = figure(figsize=(10, 9)) + fig.subplots_adjust(left=0.1, bottom=0.1, right=0.9, top=0.9, wspace=0.1, hspace=0.1) + + ax = fig.add_subplot(6,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, wtemp) + ibad = y <= -6999. + y[ibad] = numpy.nan + # if all are NaN's then pyplot throws an error for autoscale + # for some reason setting one value in the timeseries to a real value + # fixes this. + if numpy.isnan(y).all(): + y[-1]=0. + + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Water Temperature') + + ax.set_ylabel('TEMP\n (deg C)') + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) deg C to (rhs) deg F + f = [procutil.celsius2fahrenheit(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('(deg F)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('MEET Water Quality -- ' + yyyy_mm_str) + + # 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(6,1,2) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, cond) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Specific Conductivity') + + ax.set_ylabel('Cond\n (mS/cm)') + # ax.set_ylim(0,10) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,3) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, do_mg) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Dissolved Oxygen (DO)') + + ax.set_ylabel('DO \n(mg/l)') + # ax.set_ylim(20,120) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + (x, y) = procutil.addnan(dt, do_sat) + l2, = ax2.plot_date(x, y, fmt='c-') + l2.set_label('Percent Air Saturation') + # convert (lhs) deg C to (rhs) deg F + # ax2.set_ylim(0,200) + ax2.set_ylabel('DO (%)') + # ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + # ax2.set_xticklabels([]) + + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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') + 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(6,1,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, ph) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('pH') + + ax.set_ylabel('pH') + # ax.set_ylim(6, 12) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,5) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, turb) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Turbidity') + + ax.set_ylabel('Turb \n(NTU)') + # ax.set_ylim(0,100) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_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(6,1,6) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + + (x, y) = procutil.addnan(dt2, r2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Precipitation') + + ax2.set_ylim(0, 1.) + ax2.set_ylabel('Rain\n (in)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax2.set_xticklabels([]) + + ax.set_ylim(0,350) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + (axmin, axmax) = ax.get_xlim() + ax.plot_date((axmin, axmax), (0,0), fmt='k--') + 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('MEET Water Quality -- ' + yyyy_mm_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 + ofn = '/home/haines/rayleigh/img/meet/meet_wq_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + del(fig) + + ####################################### + # Plot month all rain flow the timeseries data + ####################################### + + 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) + + ax = fig.add_subplot(4,1,1) + axs = [ax] + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, r2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Precipitation') + + ax.set_ylabel('Rain (in)') + ax.set_ylim(0,1.) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) inches to (rhs) mm + f = [procutil.inches2millimeters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Rain (mm)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + ax2.xaxis.set_major_locator( DayLocator(range(2,32,2)) ) + ax2.xaxis.set_minor_locator( HourLocator(range(0,25,12)) ) + ax2.set_xticklabels([]) + + # this only moves the label not the tick labels + ax.xaxis.set_label_position('top') + ax.set_xlabel('MEET Stream Flow -- ' + yyyy_mm_str) + + # 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, pwl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + if have_sontek: + (x, y) = procutil.addnan(dt2, swl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('From Acoustic Sounding') + + ax.set_ylabel('Depth (ft)') + ax.set_ylim(-0.2,3) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + (axmin, axmax) = ax.get_xlim() + ax.plot_date((axmin, axmax), (0,0), fmt='k--') + ax.plot_date((axmin, axmax), (2.5,2.5), fmt='k--') + 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_xticklabels([]) + + # right-hand side scale + ax2 = twinx(ax) + ax2.yaxis.tick_right() + # convert (lhs) feet to (rhs) meters + f = [procutil.feet2meters(val) for val in ax.get_ylim()] + ax2.set_ylim(f) + ax2.set_ylabel('Depth (m)') + ax2.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + 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() + if have_sontek: + ls2 = l2.get_label() + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + else: + 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) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt2, pfl2) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Derived from pressure') + if have_sontek: + (x, y) = procutil.addnan(dt2, sfl2) + l2, = ax.plot_date(x, y, fmt='c-') + l2.set_label('Depth-avg Acoustic Profile') + + ax.set_ylabel('Flow (cfs)') + ax.set_ylim(0,350) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=1))) + (axmin, axmax) = ax.get_xlim() + ax.plot_date((axmin, axmax), (0,0), fmt='k--') + 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_xticklabels([]) + + # legend + ls1 = l1.get_label() + if have_sontek: + ls2 = l2.get_label() + leg = ax.legend((l1,l2), (ls1,ls2), loc='upper left') + else: + 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,4) + axs.append(ax) + + # ax.plot returns a list of lines, so unpack tuple + (x, y) = procutil.addnan(dt, batt) + l1, = ax.plot_date(x, y, fmt='b-') + l1.set_label('Station Battery') + + ax.set_ylabel('Battery\n (volts)') + ax.set_ylim(12, 14.5) + ax.set_xlim(date2num(this_month), date2num(next_month-datetime.timedelta(seconds=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('MEET Stream Flow -- ' + yyyy_mm_str) + + # 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 + + # save figure + ofn = '/home/haines/rayleigh/img/meet/meet_flow_' + yyyy_mm_str + '.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + Index: proc2plot/trunk/proc2plot/stones_avp_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/stones_avp_plot.py (revision 326) @@ -1,0 +1,494 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2009-01-20 19:40:57 haines> +"""stones_avp_plot""" + +import os, sys +import datetime, time, dateutil, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'stones_avp_plot ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +# ncFile1='/seacoos/data/nccoos/level1/stones/avp/stones_avp_2008_01.nc' +# ncFile2='/seacoos/data/nccoos/level1/stones/avp/stones_avp_2008_02.nc' +ncFile1='/seacoos/data/nccoos/level1/stones/avp/stones_avp_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/stones/avp/stones_avp_'+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) + +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) +wd = nc.var('wd')[:] + +data={} +data['ses'] = nc.var('stime')[:] +data['z'] = nc.var('z')[:] +data['wtemp'] = nc.var('wtemp')[:] +data['salin'] = nc.var('salin')[:] +data['turb'] = nc.var('turb')[:] +data['ph'] = nc.var('ph')[:] +data['do'] = nc.var('do')[:] +data['chl'] = nc.var('chl')[:] +nc.close() + +# bin_size = sensor_info['bin_size'] # Bin Size (meters) +# nominal_depth = platform_info['water_depth'] # Mean sea level at station (meters) or nominal water depth +bin_size = 0.1 # 10cm or 0.1m +nominal_depth = 4.0 # m +z = numpy.arange(0, -1*nominal_depth, -1*bin_size, dtype=float) + +nbins = len(z) +N = len(es) + +# +gd = {} +gd['wtemp'] = numpy.ma.array(numpy.ones((N,nbins), dtype=float)*numpy.nan) +gd['salin'] = numpy.ma.array(numpy.ones((N,nbins), dtype=float)*numpy.nan) +gd['turb'] = numpy.ma.array(numpy.ones((N,nbins), dtype=float)*numpy.nan) +gd['ph'] = numpy.ma.array(numpy.ones((N,nbins), dtype=float)*numpy.nan) +gd['chl'] = numpy.ma.array(numpy.ones((N,nbins), dtype=float)*numpy.nan) +gd['do'] = numpy.ma.array(numpy.ones((N,nbins), dtype=float)*numpy.nan) +Ns = numpy.array(numpy.ones((N,nbins), dtype=float)*numpy.nan) + +varlist = ['wtemp', 'salin', 'turb', 'ph', 'chl', 'do'] + + +print ' ... gridding data ...' + +for i in range(nbins): + ibin = ((z[i]<=data['z'])*(data['z']>z[i]-bin_size)) + # + nsamp = numpy.nansum(ibin, axis=1) + # can be used if want to have a minimum number of samples per bin + Ns[:,i] = nsamp + # + for v in varlist: + ta = data[v] # ta = data['wtemp'] + inan = numpy.isnan(ta) + # mask out either Nan or no in bin to get mean of samples within bin + ta = numpy.ma.masked_where(inan|~ibin, ta) + # ta = numpy.ma.masked_invalid(ta) # not this version of numpy.ma + # gd['wtemp'][:,i] = ta.mean(axis=1).data + gd[v][:,i] = ta.mean(axis=1) + gd[v].set_fill_value = numpy.nan + del(ta) + + # reset any exact zeros from averaging to NaN + # gd[v][gd[v]==0] = numpy.nan + +# + +wtm = gd['wtemp'] +sam = gd['salin'] +tum = gd['turb'] +phm = gd['ph'] +chm = gd['chl'] +dom = gd['do'] + +# 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") +# add a couple of inches to length of page for 6 subplots (5 subs in 8 inches, 6 subs in 10) +fig = figure(figsize=(10, 10)) +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(6,1,1) +axs = [ax] + +# use masked array to hide NaN's on plot +# wtm = numpy.ma.masked_where(numpy.isnan(wtemp), wtemp) +# range for pcolor plots +cmin = numpy.floor(wtm.min()) +cmax = cmin+8. +# print "%s : %g %g \n" % ('Temp', cmin, cmax) +# cmin, cmax = (15., 35.) +# plot pcolor +pc = ax.pcolor(dn, z, wtm.T, cmap=cm.get_cmap('jet'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Water Temperature (deg C)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +# make tick labels for 10 posns set and round to one decimal place +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) +# but only select one at set_xticks +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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('Stones Bay AVP -- Last 30 days from ' + last_dt_str) + +# 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('Depth (ft)') + +# 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(6,1,2) +axs.append(ax) + +# use masked array to hide NaN's on plot +# sam = numpy.ma.masked_where(numpy.isnan(salin), salin) +# range for pcolor plots +cmin = numpy.floor(sam.mean()-2*sam.std()) +cmax = cmin+10. +# print "%s : %g %g \n" % ('Salin', cmin, cmax) +# cmin, cmax = (0., 50.) +pc = ax.pcolor(dn, z, sam.T, cmap=cm.get_cmap('Blues_r'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Salinity (PSU)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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)) ) + +# 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('Depth (ft)') + +# 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(6,1,3) +axs.append(ax) + +# use masked array to hide NaN's on plot +# tum = numpy.ma.masked_where(numpy.isnan(turb), turb) +# range for pcolor plots +cmin, cmax = (0., 10.) +pc = ax.pcolor(dn, z, tum.T, cmap=cm.get_cmap('YlOrBr'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Turbidity (NTU)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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)) ) + +# 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('Depth (ft)') + +####################################### +# +ax = fig.add_subplot(6,1,4) +axs.append(ax) + +# use masked array to hide NaN's on plot +# cm = numpy.ma.masked_where(numpy.isnan(chl), chl) +# range for pcolor plots +cmin = numpy.floor(chm.min()) +cmax = cmin+15. +# cmin, cmax = (0., 100.) +pc = ax.pcolor(dn, z, chm.T, cmap=cm.get_cmap('BuGn'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Chlorophyll (ug l-1)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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)) ) + +# 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('Depth (ft)') + +####################################### +# +ax = fig.add_subplot(6,1,5) +axs.append(ax) + +# use masked array to hide NaN's on plot +# dom = numpy.ma.masked_where(numpy.isnan(do), do) +# range for pcolor plots +cmin, cmax = (0., 15.) +pc = ax.pcolor(dn, z, dom.T, cmap=cm.get_cmap('PiYG'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('Dissolved Oxygen (mg l-1)') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=0) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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)) ) + +# 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('Depth (ft)') + +####################################### +# +ax = fig.add_subplot(6,1,6) +axs.append(ax) + +# use masked array to hide NaN's on plot +# phm = numpy.ma.masked_where(numpy.isnan(ph), ph) +# range for pcolor plots +# cmin, cmax = numpy.round([sm.min(), sm.max()], decimals=0) +# cmin = cmin-1 +# cmax = cmax+1 +# print "%s : %g %g \n" % ('pH', cmin, cmax) +cmin, cmax = (6., 8.) +pc = ax.pcolor(dn, z, phm.T, cmap=cm.get_cmap('cool'), vmin=cmin, vmax=cmax) + +# setup colorbar axes instance. +l,b,w,h = ax.get_position() +cax = fig.add_axes([l+0.04, b+0.02, 0.25*w, 0.03]) + +cb = colorbar(pc, cax=cax, orientation='horizontal') # draw colorbar +cb.set_label('pH') +cb.ax.xaxis.set_label_position('top') +cb.ax.set_xticks([0.1, 0.3, 0.5, 0.7, 0.9]) +xtl = numpy.round(numpy.linspace(cmin, cmax, 10), decimals=1) +cb.ax.set_xticklabels([xtl[1], xtl[3], xtl[5], xtl[7], xtl[9]]) +# cb.ax.set_xticklabels([12., 16., 20., 24., 28.]) + +# ax.plot returns a list of lines, so unpack tuple +# l1, = ax.plot_date(dt, wd, fmt='k-') +# l1.set_label('Water Level') + +ax.set_ylabel('Depth (m)') +ax.set_ylim(-4.,0.) +# 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('Stones Bay AVP -- Last 30 days from ' + last_dt_str) + +# 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('Depth (ft)') + +# save figure +savefig('/home/haines/rayleigh/img/stones_avp_last30days.png') + +####################################### +# Last 7 days +####################################### + +print ' ... Last 7 days' +for i in [0, 1, 2, 3, 4]: + ax = axs[i] + ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1])) + ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) ) + ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) + ax.set_xticklabels([]) + if i==0: + ax.set_xlabel('Stones Bay AVP -- Last 7 days from ' + last_dt_str) +# + +ax = axs[5] +ax.set_xlim(date2num(dt[-1])-7, date2num(dt[-1])) +ax.xaxis.set_major_locator( DayLocator(range(1,32,1)) ) +ax.xaxis.set_minor_locator( HourLocator(range(0,25,6)) ) +ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('Stones Bay AVP -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/stones_avp_last07days.png') + +####################################### +# Last 1 day (24hrs) +####################################### + +print ' ... Last 1 days' + +for i in [0, 1, 2, 3, 4]: + ax = axs[i] + 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([]) + if i==0: + ax.set_xlabel('Stones Bay AVP -- Last 24 hours from ' + last_dt_str) + +ax = axs[5] +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('Stones Bay AVP -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/stones_avp_last01days.png') + Index: proc2plot/trunk/proc2plot/stones_met_plot.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/stones_met_plot.py (revision 326) @@ -1,0 +1,256 @@ +#!/usr/bin/env python +# Last modified: Time-stamp: <2008-10-03 11:35:27 haines> +"""stones_met_plot""" + +import os, sys +import datetime, time, dateutil.tz +import pycdf +import numpy + +sys.path.append('/home/haines/nccoos/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 'stones_met_plot2 ...' +prev_month, this_month, next_month = procutil.find_months(procutil.this_month()) +#ncFile1='/seacoos/data/nccoos/level1/stones/met/stones_met_2008_01.nc' +#ncFile2='/seacoos/data/nccoos/level1/stones/met/stones_met_2008_02.nc' + +ncFile1='/seacoos/data/nccoos/level1/stones/met/stones_met_'+prev_month.strftime('%Y_%m')+'.nc' +ncFile2='/seacoos/data/nccoos/level1/stones/met/stones_met_'+this_month.strftime('%Y_%m')+'.nc' + +# load data +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) + +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) +ws = nc.var('wspd')[:] +wd = nc.var('wdir')[:] +u = nc.var('wspd')[:] +v = nc.var('wspd')[:] + +i = 0 +for val in ws: + u[i] = -1.*procutil.wind_vector2u(ws[i],wd[i]) + v[i] = -1.*procutil.wind_vector2v(ws[i],wd[i]) + i += 1 +nc.close() + + +# 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] + +# use masked array to hide NaN's on plot +wd = numpy.ma.masked_where(numpy.isnan(wd), wd) + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, wd, fmt='b-') +l1.set_label('Wind from Direction') + +ax.set_ylabel('Dir (true N)') +ax.set_ylim(0.,360.) +# 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('STONES BAY WIND -- Last 30 days from ' + last_dt_str) + +# 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 +ws = numpy.ma.masked_where(numpy.isnan(ws), ws) + + +# ax.plot returns a list of lines, so unpack tuple +l1, = ax.plot_date(dt, ws, fmt='b-') +l1.set_label('Wind Speed') + + +ax.set_ylabel('Speed (m/s)') +ax.set_ylim(0.,20.) + +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) m/s to (rhs) knots +k = [procutil.meters_sec2knots(val) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +# 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,3) +axs.append(ax) + +# use masked array to hide NaN's on plot +# u = numpy.ma.masked_where(numpy.isnan(u), u) +# v = numpy.ma.masked_where(numpy.isnan(v), v) + +dt0 = numpy.zeros(len(dt)) + +ax.set_xlim(date2num(dt[-1])-30, date2num(dt[-1])) +ax.axhline(y=0, color='gray') +# make stick plot (set all head parameters to zero) +# scale to y-axis using units='height' of plot with scale = 40 (m/s range) per height with min=-20 and max=20 +q1 = ax.quiver(date2num(dt), dt0, u, v, units='height', scale=40, headwidth=0, headlength=0, headaxislength=0) +qk = ax.quiverkey(q1, 0.1, 0.8, 10, r'10 m s-1') + +ax.set_ylim(-20.,20.) +ax.set_ylabel('Speed (m/s)') + +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') ) + +# right-hand side scale +ax2 = twinx(ax) +ax2.yaxis.tick_right() +# convert (lhs) m/s to (rhs) knots +k = [(val * 1.94384449) for val in ax.get_ylim()] +ax2.set_ylim(k) +ax2.set_ylabel('Speed (knots)') + +ax.set_xlabel('STONES BAY WIND -- Last 30 days from ' + last_dt_str) + +# save figure +savefig('/home/haines/rayleigh/img/stones_met_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('STONES BAY WIND -- 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.xaxis.set_major_formatter( DateFormatter('%m/%d') ) +ax.set_xlabel('STONES BAY WIND -- Last 7 days from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/stones_met_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('STONES BAY WIND -- 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.xaxis.set_major_formatter( DateFormatter('%H') ) +ax.set_xlabel('STONES BAY WIND -- Last 24 hours from ' + last_dt_str) + +savefig('/home/haines/rayleigh/img/stones_met_last01days.png') + Index: proc2plot/trunk/proc2plot/template_p2p.py =================================================================== --- (revision ) +++ proc2plot/trunk/proc2plot/template_p2p.py (revision 326) @@ -1,0 +1,210 @@ +#!/usr/bin/env /opt/env/haines/dataproc/bin/python +# Last modified: Time-stamp: <2010-04-07 12:02:53 haines> + +"""XXXX_YYYY_plot""" + +################################################# +# [1a] search and replace XXXX with platform id +# [1b] search and replace YYYY with sensor id +# [1c] search and replace ZZZZ plot titles +################################################# + +# plot each month + +import os +# import sys, glob, re +import datetime, time, dateutil, 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 + +def timeseries(pi, si, yyyy_mm, plot_type='latest'): + """ + """ + + + print 'XXXX_YYYY_plot ...' + + # + + prev_month, this_month, next_month = procutil.find_months(yyyy_mm) + yyyy_mm_str = this_month.strftime('%Y_%m') + + ################################ + # [2a] load primary data file + ################################ + + ncFile1='/seacoos/data/nccoos/level1/XXXX/YYYY/XXXX_YYYY_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/XXXX/YYYY/XXXX_YYYY_'+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) + + # open netcdf 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) + + + ################################ + # [2b] specify variables + ################################ + + # e.g. + # z = nc.var('z')[:] + + nc.close() + + + ################################ + # [3a] load ancillary files if needed + ################################ + + ncFile1='/seacoos/data/nccoos/level1/XXXX/flow/XXXX_flow_'+prev_month.strftime('%Y_%m')+'.nc' + ncFile2='/seacoos/data/nccoos/level1/XXXX/flow/XXXX_flow_'+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) + + # open netcdf 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 + es2 = nc.var('time')[:] + units = nc.var('time').units + dt2 = [procutil.es2dt(e) for e in es2] + # set timezone info to UTC (since data from level1 should be in UTC!!) + dt2 = [e.replace(tzinfo=dateutil.tz.tzutc()) for e in dt2] + dn2 = date2num(dt2) + + ################################ + # [3b] specify variables + ################################ + + + nc.close() + + # last dt in data for labels + dtu = dt[-1] + dtl = dt_local[-1] + + diff = abs(dtu - dtl) + if diff.days>0: + last_dt_str = dtu.strftime("%H:%M %Z on %b %d, %Y") + ' (' + dtl.strftime("%H:%M %Z, %b %d") + ')' + else: + last_dt_str = dtu.strftime("%H:%M %Z") + ' (' + dtl.strftime("%H:%M %Z") + ')' \ + + dtl.strftime(" on %b %d, %Y") + + ####################################### + # Plot setup + ####################################### + + + # save figure for this month + ofn = '/home/haines/rayleigh/img/XXXX/XXXX_YYYY_'+yyyy_mm_str+'.png' + print '... ... write: %s' % (ofn,) + savefig(ofn) + + + ####################################### + # Last 30 days + ####################################### + if plot_type=='latest': + print ' ... Last 30 days' + for idx, ax in enumerate(axs): + 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.set_xticklabels([]) + if idx==0: + ax.set_xlabel('XXXX ZZZZ -- Last 30 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('XXXX ZZZZ -- Last 30 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/XXXX_YYYY_last30days.png') + + + ####################################### + # Last 7 days + ####################################### + if plot_type=='latest': + print ' ... Last 7 days' + for idx, ax in enumerate(axs): + 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([]) + if idx==0: + ax.set_xlabel('XXXX ZZZZ -- Last 7 days from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('XXXX ZZZZ -- Last 7 days from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/XXXX_YYYY_last07days.png') + + + ####################################### + # Last 1 day (24hrs) + ####################################### + if plot_type=='latest': + print ' ... Last 1 days' + + for idx, ax in enumerate(axs): + ax.set_xlim(date2num(dt[-1])-1, 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([]) + if idx==0: + ax.set_xlabel('XXXX ZZZZ -- Last 24 hours from ' + last_dt_str) + elif idx==len(axs)-1: + ax.xaxis.set_major_formatter( DateFormatter('%m/%d') ) + ax.set_xlabel('XXXX ZZZZ -- Last 24 hours from ' + last_dt_str) + + savefig('/home/haines/rayleigh/img/XXXX_YYYY_last01days.png') + + + +