コード例 #1
0
ファイル: spin_crow_csv.py プロジェクト: nccoos/raw2proc
sys.path.append('/opt/env/haines/dataproc/raw2proc')
import procutil
del(sys)

print 'spin_crow_csv ...'

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')
    
    # 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]
    
コード例 #2
0
ファイル: stones_avp_plot.py プロジェクト: nccoos/proc2plot
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:
コード例 #3
0
ファイル: plot_cr1000_met.py プロジェクト: nccoos/proc2plot
def timeseries(pi, si, yyyy_mm, plot_type='latest'):
    """ 
    """
    print 'plot_cr1000_met ...'
    img_dir = '/home/haines/rayleigh/img'

    prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
    yyyy_mm_str = this_month.strftime('%Y_%m')

    fn = '_'.join([pi['id'], si['id'], prev_month.strftime('%Y_%m')+'.nc'])
    ncFile1= os.path.join(si['proc_dir'], fn)
    fn = '_'.join([pi['id'], si['id'], this_month.strftime('%Y_%m')+'.nc'])
    ncFile2= os.path.join(si['proc_dir'], fn)

    # 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'
        return

    # 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)

    # 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")

    #######################################
    # Build Plot 
    #######################################

    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]

    vn = 'air_press'
    (x, y) = procutil.addnan(dt, nc.var(vn)[:])
    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('Barometric Pressure')

    ax.set_ylabel('Pressure\n (mbar)')

    # right-hand side scale
    ax2 = twinx(ax)
    ax2.yaxis.tick_right()
    f = [procutil.udconvert(val, 'mbar', 'inch_Hg')[0] for val in ax.get_ylim()]    
    ax2.set_ylim(f)
    ax2.set_ylabel('(in Hg)')

    # this only moves the label not the tick labels
    ax.xaxis.set_label_position('top')

    # 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)

    vn = 'air_temp'
    yo = nc.var(vn)[:]

    vn = 'air_temp_std'
    yy=nc.var(vn)[:]
    (x, y1) = procutil.addnan(dt, yo+yy)
    (x, y2) = procutil.addnan(dt, yo-yy)
    l2, = ax.plot_date(x, y1, fmt='c-', alpha=0.5)
    l2, = ax.plot_date(x, y2, fmt='c-', alpha=0.5)
    l2.set_label('Std Dev')

    # ax.plot returns a list of lines, so unpack tuple
    (x, y) = procutil.addnan(dt, yo)
    l1, = ax.plot_date(x, y, fmt='b-')
    l1.set_label('Air Temperature')

    ax.set_ylabel('Temp \n (deg C)')
    # ax.set_ylim(0,10)

    # right-hand side scale
    ax2 = twinx(ax)
    ax2.yaxis.tick_right()
    # convert (lhs) deg C to (rhs) deg F
    f = [procutil.udconvert(val, 'degreeC', 'degreeF')[0] for val in ax.get_ylim()]    
    ax2.set_ylim(f)
    ax2.set_ylabel('(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(6,1,3)
    axs.append(ax)
 
    vn = 'rh'
    yo = nc.var(vn)[:]

    vn = 'rh_std'
    yy=nc.var(vn)[:]
    (x, y1) = procutil.addnan(dt, yo+yy)
    (x, y2) = procutil.addnan(dt, yo-yy)
    l2, = ax.plot_date(x, y1, fmt='c-', alpha=0.5)
    l2, = ax.plot_date(x, y2, fmt='c-', alpha=0.5)
    l2.set_label('Std Dev')

    # ax.plot returns a list of lines, so unpack tuple
    (x, y) = procutil.addnan(dt, yo)
    l1, = ax.plot_date(x, y, fmt='b-')
    l1.set_label('Relative Humidity (%)')

    ax.set_ylabel('RHUM (%)')
    # ax.set_ylim(20,120)

    # 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,4)
    axs.append(ax)

    vn = 'rain'
    yo = nc.var(vn)[:]
    # ax.plot returns a list of lines, so unpack tuple
    (x, y) = procutil.addnan(dt, yo)
    l1, = ax.plot_date(x, y, fmt='b-')
    l1.set_label('Precipitation')

    ax.set_ylabel('Rain (mm)')
    # ax.set_ylim(6, 12)

    # right-hand side scale
    ax2 = twinx(ax)
    ax2.yaxis.tick_right()
    f = [procutil.udconvert(val, 'mm', 'inch')[0] for val in ax.get_ylim()]    
    ax2.set_ylim(f)
    ax2.set_ylabel('Rain (in)')

    # 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)

    vn = 'psp'
    yo = nc.var(vn)[:]

    vn = 'psp_std'
    yy=nc.var(vn)[:]
    (x, y1) = procutil.addnan(dt, yo+yy)
    (x, y2) = procutil.addnan(dt, yo-yy)
    l2, = ax.plot_date(x, y1, fmt='c-', alpha=0.5)
    l2, = ax.plot_date(x, y2, fmt='c-', alpha=0.5)
    l2.set_label('Std Dev')

    # ax.plot returns a list of lines, so unpack tuple
    (x, y) = procutil.addnan(dt, yo)
    l1, = ax.plot_date(x, y, fmt='b-')
    l1.set_label('Shortwave Radiation')

    ax.set_ylabel('PSP (W m-2)')
    # ax.set_ylim(0,100)

    # 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)

    vn = 'pir'
    yo = nc.var(vn)[:]

    vn = 'pir_std'
    yy=nc.var(vn)[:]
    (x, y1) = procutil.addnan(dt, yo+yy)
    (x, y2) = procutil.addnan(dt, yo-yy)
    l2, = ax.plot_date(x, y1, fmt='c-', alpha=0.5)
    l2, = ax.plot_date(x, y2, fmt='c-', alpha=0.5)
    l2.set_label('Std Dev')

    # ax.plot returns a list of lines, so unpack tuple
    (x, y) = procutil.addnan(dt, yo)
    l1, = ax.plot_date(x, y, fmt='b-')
    l1.set_label('Long-wave Radiation')

    # ax.set_ylim(0,350)
    ax.set_ylabel('PIR (W m-2)')
    #  ax.plot_date((x[1],x[-1]), (0,0), fmt='k:')

    # 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

    #######################################
    # YYYY_MM 
    #######################################
    title_str = ' '.join([pi['id'].upper(), si['description']])
    
    if plot_type=='latest' or plot_type=='monthly':
        print ' ... : %s' % (yyyy_mm_str,)
        # save figure for this month
        for idx, ax in enumerate(axs):
            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([])
            if idx==0:
                ax.set_xlabel(title_str+' -- ' + yyyy_mm_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- ' + yyyy_mm_str)

        fn = '_'.join([pi['id'], si['id'], yyyy_mm_str+'.png'])
        ofn = os.path.join(img_dir, pi['id'], fn) # /home/haines/rayleigh/img/meet
        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(title_str+' -- Last 30 days from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- Last 30 days from '+last_dt_str)

        fn = '_'.join([pi['id'], si['id'], 'last30days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img/
        savefig(ofn)

    #######################################
    # 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(title_str+' -- Last 7 days from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- Last 7 days from '+last_dt_str)
                
        fn = '_'.join([pi['id'], si['id'], 'last07days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)

    #######################################
    # 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(title_str+' -- Last 24 hours from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%H') )
                ax.set_xlabel(title_str+' -- Last 24 hours from '+last_dt_str)


        fn = '_'.join([pi['id'], si['id'], 'last01days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)
コード例 #4
0
ファイル: plot_cr1000_flow.py プロジェクト: nccoos/proc2plot
def timeseries(pi, si, yyyy_mm, plot_type='latest'):
    """ 
    """

    img_dir = '/home/haines/rayleigh/img'
    print 'plot_cr1000_flow ...'

    #

    prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
    yyyy_mm_str = this_month.strftime('%Y_%m')

    fn = '_'.join([pi['id'], 'wq', prev_month.strftime('%Y_%m')+'.nc'])
    ncFile1= os.path.join('/seacoos/data/nccoos/level1', pi['id'], 'wq', fn)
    fn = '_'.join([pi['id'], 'wq', this_month.strftime('%Y_%m')+'.nc'])
    ncFile2= os.path.join('/seacoos/data/nccoos/level1', pi['id'], 'wq', fn)

    # 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'
        return

    # 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 = '_'.join([pi['id'], si['id'], prev_month.strftime('%Y_%m')+'.nc'])
    ncFile1= os.path.join(si['proc_dir'], fn)
    fn = '_'.join([pi['id'], si['id'], this_month.strftime('%Y_%m')+'.nc'])
    ncFile2= os.path.join(si['proc_dir'], fn)

    # 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'
        return

    # 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.)

    # 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)')

    # this only moves the label not the tick labels
    ax.xaxis.set_label_position('top')
    
    # 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)

    # 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)')

    # 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)

    # 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)

    # 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
    
    #######################################
    # YYYY_MM 
    #######################################
    title_str = pi['id'].upper()+' '+si['description']
    
    if plot_type=='latest' or plot_type=='monthly':
        print ' ... : %s' % (yyyy_mm_str,)
        # save figure for this month
        for idx, ax in enumerate(axs):
            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([])
            if idx==0:
                ax.set_xlabel(title_str+' -- ' + yyyy_mm_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- ' + yyyy_mm_str)

        fn = '_'.join([pi['id'], si['id'], yyyy_mm_str+'.png'])
        ofn = os.path.join(img_dir, pi['id'], fn) # /home/haines/rayleigh/img/platform_id
        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(title_str+' -- Last 30 days from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- Last 30 days from '+last_dt_str)

        fn = '_'.join([pi['id'], si['id'], 'last30days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img/
        savefig(ofn)

    #######################################
    # 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(title_str+' -- Last 7 days from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- Last 7 days from '+last_dt_str)
                
        fn = '_'.join([pi['id'], si['id'], 'last07days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)

    #######################################
    # 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(title_str+' -- Last 24 hours from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%H') )
                ax.set_xlabel(title_str+' -- Last 24 hours from '+last_dt_str)


        fn = '_'.join([pi['id'], si['id'], 'last01days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)
コード例 #5
0
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/level2/billymitchell/sodar1/billymitchell_sfas_'+prev_month.strftime('%Y_%m')+'.nc'
    ncFile2='/seacoos/data/nccoos/level2/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:
        try:
            nc = pycdf.CDFMF((ncFile1, ncFile2))
        except: # files may have different dimensions
            nc = pycdf.CDFMF((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'
        return

    # 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 level2 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.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.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)
    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')
コード例 #6
0
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/level2/billymitchell/sodar1/billymitchell_sfas_'+prev_month.strftime('%Y_%m')+'.nc'
    ncFile2='/seacoos/data/nccoos/level2/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:
        try:
            nc = pycdf.CDFMF((ncFile1, ncFile2))
        except: # files may have different dimensions
            nc = pycdf.CDFMF((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'
        return

    # 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 level2 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')
コード例 #7
0
ファイル: bogue_adcp_plot.py プロジェクト: nccoos/proc2plot
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'
        return

    # 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')[:]
    e1 = nc.var('e1')[:]

    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)) )

    # 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)) )

    # 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)

    # replace gaps in time with NaN
    (x, y) = procutil.addnan(dt, e1, 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=0., vmax=150.)
    pc.set_label('Amplitude Beam 1 (count)')
    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')
コード例 #8
0
ファイル: plot_cr1000_comp.py プロジェクト: nccoos/proc2plot
def timeseries(pi, si, yyyy_mm, plot_type='latest'):
    """ 
    """
    print 'plot_cr1000_comp ...'
    img_dir = '/home/haines/rayleigh/img'

    prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
    yyyy_mm_str = this_month.strftime('%Y_%m')

    fn = '_'.join([pi['id'], si['id'], prev_month.strftime('%Y_%m')+'.nc'])
    ncFile1= os.path.join(si['proc_dir'], fn)
    fn = '_'.join([pi['id'], si['id'], this_month.strftime('%Y_%m')+'.nc'])
    ncFile2= os.path.join(si['proc_dir'], fn)

    # 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'
        return

    # 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]

    # 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")

    #######################################
    # Build Plot 
    #######################################

    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(4,1,1)
    axs = [ax]

    vn = 'hdg'
    yo = nc.var(vn)[:]
    # ibad = y <= -6999.
    # y[ibad] = numpy.nan

    # ax.plot returns a list of lines, so unpack tuple
    (x, y) = procutil.addnan(dt, yo)
    l1, = ax.plot_date(x, y, fmt='b-')
    l1.set_label(nc.var(vn).long_name)

    vn = 'hdg_std'
    yy=nc.var(vn)[:]
    (x, y1) = procutil.addnan(dt, yo+yy)
    (x, y2) = procutil.addnan(dt, yo-yy)
    # ibad = y <= -6999.
    # y[ibad] = numpy.nan

    # ax.plot returns a list of lines, so unpack tuple
    l2, = ax.plot_date(x, y1, fmt='c-', alpha=0.5)
    l2, = ax.plot_date(x, y2, fmt='c-', alpha=0.5)
    l2.set_label('Std Dev')

    ax.set_ylabel('HEADING\n ('+nc.var(vn).units+' Mag North)')
    # ax.set_ylim(0,360)

    # this only moves the label not the tick labels
    ax.xaxis.set_label_position('top')

    # 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)

    vn = 'pitch'
    yo = nc.var(vn)[:]
    (x, y) = procutil.addnan(dt, yo)
    # 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('Pitch')

    vn = 'pitch_std'
    (x, y1) = procutil.addnan(dt, yo+nc.var(vn)[:])
    (x, y2) = procutil.addnan(dt, yo-nc.var(vn)[:])
    # ibad = y <= -6999.
    # y[ibad] = numpy.nan

    # ax.plot returns a list of lines, so unpack tuple
    l2, = ax.plot_date(x, y1, fmt='c-', alpha=0.5)
    l2, = ax.plot_date(x, y2, fmt='c-', alpha=0.5)
    l2.set_label('Std Dev')
    
    vn = 'pitch_max'
    (x, y) = procutil.addnan(dt, nc.var(vn)[:])
    l3, = ax.plot_date(x, y, fmt='k-', alpha=0.5)
    l3.set_label('Maximum')
    
    ax.axhline(y=0, color='k')
    ax.set_ylabel('PITCH\n ('+nc.var(vn).units+')')
    # ax.set_ylim(-20,20)

    # 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(4,1,3)
    axs.append(ax)

    vn = 'roll'
    yo = nc.var(vn)[:]
    (x, y) = procutil.addnan(dt, yo)
    # 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(nc.var(vn).long_name)

    vn = 'roll_std'
    (x, y1) = procutil.addnan(dt, yo+nc.var(vn)[:])
    (x, y2) = procutil.addnan(dt, yo-nc.var(vn)[:])
    # ibad = y <= -6999.
    # y[ibad] = numpy.nan

    # ax.plot returns a list of lines, so unpack tuple
    l2, = ax.plot_date(x, y1, fmt='c-', alpha=0.5)
    l2, = ax.plot_date(x, y2, fmt='c-', alpha=0.5)
    l2.set_label('Std Dev')
    
    vn = 'roll_max'
    (x, y) = procutil.addnan(dt, nc.var(vn)[:])
    l3, = ax.plot_date(x, y, fmt='k-', alpha=0.5)
    l3.set_label('Maximum')
    
    ax.axhline(y=0, color='k')
    ax.set_ylabel('ROLL\n ('+nc.var(vn).units+')')
    # ax.set_ylim(-20,20)

    # legend
    ls1 = l1.get_label()
    ls2 = l2.get_label()
    ls3 = l3.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

    #######################################
    # YYYY_MM 
    #######################################
    title_str = ' '.join([pi['id'].upper(), si['description']])
    
    if plot_type=='latest' or plot_type=='monthly':
        print ' ... : %s' % (yyyy_mm_str,)
        # save figure for this month
        for idx, ax in enumerate(axs):
            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([])
            if idx==0:
                ax.set_xlabel(title_str+' -- ' + yyyy_mm_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- ' + yyyy_mm_str)

        fn = '_'.join([pi['id'], si['id'], yyyy_mm_str+'.png'])
        ofn = os.path.join(img_dir, pi['id'], fn) # /home/haines/rayleigh/img/meet
        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(title_str+' -- Last 30 days from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- Last 30 days from '+last_dt_str)

        fn = '_'.join([pi['id'], si['id'], 'last30days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img/
        savefig(ofn)

    #######################################
    # 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(title_str+' -- Last 7 days from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- Last 7 days from '+last_dt_str)
                
        fn = '_'.join([pi['id'], si['id'], 'last07days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)

    #######################################
    # 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(title_str+' -- Last 24 hours from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%H') )
                ax.set_xlabel(title_str+' -- Last 24 hours from '+last_dt_str)


        fn = '_'.join([pi['id'], si['id'], 'last01days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)
コード例 #9
0
ファイル: plot_cr1000_ctd.py プロジェクト: nccoos/proc2plot
def timeseries(pi, si, yyyy_mm, plot_type="latest"):
    """ 
    """
    print "plot_cr1000_ctd ..."
    img_dir = "/home/haines/rayleigh/img"

    prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
    yyyy_mm_str = this_month.strftime("%Y_%m")

    fn = "_".join([pi["id"], si["id"], prev_month.strftime("%Y_%m") + ".nc"])
    ncFile1 = os.path.join(si["proc_dir"], fn)
    fn = "_".join([pi["id"], si["id"], this_month.strftime("%Y_%m") + ".nc"])
    ncFile2 = os.path.join(si["proc_dir"], fn)

    # 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"
        return

    # 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)

    cond = nc.var("cond")[:]
    wtemp = nc.var("wtemp")[:]
    press = nc.var("press")[:]
    depth = nc.var("depth")[:]
    salin = nc.var("salin")[:]
    density = nc.var("density")[:]

    # 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")

    #######################################
    # Build Plot
    #######################################

    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, depth)
    # ax.plot returns a list of lines, so unpack tuple
    l1, = ax.plot_date(x, y, fmt="b-")
    l1.set_label("Depth from pressure")

    ax.set_ylabel("Depth\n (m)")

    # right-hand side scale
    ax2 = twinx(ax)
    ax2.yaxis.tick_right()
    f = [procutil.udconvert(val, "meter", "foot")[0] for val in ax.get_ylim()]
    ax2.set_ylim(f)
    ax2.set_ylabel("(ft)")

    # this only moves the label not the tick labels
    ax.xaxis.set_label_position("top")

    # 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, wtemp)
    l1, = ax.plot_date(x, y, fmt="b-")
    l1.set_label("Water Temperature")

    ax.set_ylabel("Temp \n (deg C)")
    # ax.set_ylim(0,10)

    # right-hand side scale
    ax2 = twinx(ax)
    ax2.yaxis.tick_right()
    # convert (lhs) deg C to (rhs) deg F
    f = [procutil.udconvert(val, "degreeC", "degreeF")[0] for val in ax.get_ylim()]
    ax2.set_ylim(f)
    ax2.set_ylabel("(deg F)")

    # 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, cond)
    #     l1, = ax.plot_date(x, y, fmt='b-')
    #     l1.set_label('Conductivity')

    #     ax.set_ylabel('Conductivity\n (S/m)')
    # ax.set_ylim(20,120)

    # right-hand side scale
    #     ax2 = twinx(ax)
    #     ax2.yaxis.tick_right()
    #     f = [procutil.udconvert(val, 'S m-1', 'mS cm-1')[0] for val in ax.get_ylim()]
    #     ax2.set_ylim(f)
    #     ax2.set_ylabel('(mS/cm)')

    # 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, salin)
    l1, = ax.plot_date(x, y, fmt="b-")
    l1.set_label("Salinity")

    ax.set_ylabel("Salinity\n (psu)")
    # ax.set_ylim(6, 12)

    # 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, 4)
    axs.append(ax)

    # ax.plot returns a list of lines, so unpack tuple
    (x, y) = procutil.addnan(dt, density)
    l1, = ax.plot_date(x, y, fmt="b-")
    l1.set_label("Density")

    ax.set_ylabel("Density (kg m-3)")
    from matplotlib.ticker import MultipleLocator, FormatStrFormatter

    ax.yaxis.set_major_locator(MultipleLocator(1))
    ax.yaxis.set_major_formatter(FormatStrFormatter("%d"))
    # ax.set_ylim(0,100)

    # right-hand side scale
    ax2 = twinx(ax)
    ax2.yaxis.tick_right()
    f = [procutil.udconvert(val, "kg m-3", "lb ft-3")[0] for val in ax.get_ylim()]
    ax2.set_ylim(f)
    ax2.set_ylabel("(lb ft-3)")

    # 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

    #######################################
    # YYYY_MM
    #######################################
    title_str = " ".join([pi["id"].upper(), si["description"]])

    if plot_type == "latest" or plot_type == "monthly":
        print " ... : %s" % (yyyy_mm_str,)
        # save figure for this month
        for idx, ax in enumerate(axs):
            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([])
            if idx == 0:
                ax.set_xlabel(title_str + " -- " + yyyy_mm_str)
            elif idx == len(axs) - 1:
                ax.xaxis.set_major_formatter(DateFormatter("%m/%d"))
                ax.set_xlabel(title_str + " -- " + yyyy_mm_str)

        fn = "_".join([pi["id"], si["id"], yyyy_mm_str + ".png"])
        ofn = os.path.join(img_dir, pi["id"], fn)  # /home/haines/rayleigh/img/meet
        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(title_str + " -- Last 30 days from " + last_dt_str)
            elif idx == len(axs) - 1:
                ax.xaxis.set_major_formatter(DateFormatter("%m/%d"))
                ax.set_xlabel(title_str + " -- Last 30 days from " + last_dt_str)

        fn = "_".join([pi["id"], si["id"], "last30days.png"])
        ofn = os.path.join(img_dir, fn)  # /home/haines/rayleigh/img/
        savefig(ofn)

    #######################################
    # 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(title_str + " -- Last 7 days from " + last_dt_str)
            elif idx == len(axs) - 1:
                ax.xaxis.set_major_formatter(DateFormatter("%m/%d"))
                ax.set_xlabel(title_str + " -- Last 7 days from " + last_dt_str)

        fn = "_".join([pi["id"], si["id"], "last07days.png"])
        ofn = os.path.join(img_dir, fn)  # /home/haines/rayleigh/img
        savefig(ofn)

    #######################################
    # 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(title_str + " -- Last 24 hours from " + last_dt_str)
            elif idx == len(axs) - 1:
                ax.xaxis.set_major_formatter(DateFormatter("%H"))
                ax.set_xlabel(title_str + " -- Last 24 hours from " + last_dt_str)

        fn = "_".join([pi["id"], si["id"], "last01days.png"])
        ofn = os.path.join(img_dir, fn)  # /home/haines/rayleigh/img
        savefig(ofn)
コード例 #10
0
ファイル: template_p2p.py プロジェクト: nccoos/proc2plot
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'
        return

    # 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'
        return

    # 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')
コード例 #11
0
ファイル: plot_cr1000_gps.py プロジェクト: nccoos/proc2plot
def watch_circle(pi, si, yyyy_mm, plot_type='latest'):
    """ 
    """
    print 'plot_cr1000_gps ...'
    img_dir = '/home/haines/rayleigh/img'

    prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
    yyyy_mm_str = this_month.strftime('%Y_%m')

    fn = '_'.join([pi['id'], si['id'], prev_month.strftime('%Y_%m')+'.nc'])
    ncFile1= os.path.join(si['proc_dir'], fn)
    fn = '_'.join([pi['id'], si['id'], this_month.strftime('%Y_%m')+'.nc'])
    ncFile2= os.path.join(si['proc_dir'], fn)

    # 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'
        return

    # 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)

    # 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")

    #######################################
    # Build Plot 
    #######################################

    fig = figure(figsize=(6, 5))
    fig.subplots_adjust(left=0.20, bottom=0.10, right=0.9, top=0.9, wspace=0.1, hspace=0.1)

    ax = fig.add_subplot(1,1,1)
    axs = [ax]

    # GPS longitude on x, latitude on y
    gps_lon = nc.var('gps_lon')[:]
    gps_lat = nc.var('gps_lat')[:]
    # gps_lon[-1:] returns last value as type numpy.array
    # gps_lon[-1] returns the value as type float
    (x, y) = (gps_lon[-1:], gps_lat[-1:])
    # print (x, y)
    # ax.plot returns a list of lines, so unpack tuple
    l1, = ax.plot(x, y, 'r*', ms=8)

    # anchor position platform lat and lon from config -- needs to be numpy.array([]) for ax.plot
    (x, y) = (numpy.array([pi['lon']]), numpy.array([pi['lat']]))
    # anchor posn platform lat and lon from netcdf file (should be the same as config)
    # (x, y) = (nc.var('lon')[:], nc.var('lat')[:])
    # print (x, y)
    l2, = ax.plot(x, y, 'ks', ms=8, mfc='none')

    # 1km watch circle, approx 111 km in 1 deg latitude 1 km is 1/111 of a deg
    if 0:
        wc = Circle((x,y), 1./111, alpha=0.2)
        wc.set_label('1 km Watch Circle')
        p = PatchCollection([wc], alpha=0.2)
        ax.add_collection(p)
        leg1 = ax.legend([wc], '1 km Watch Circle', loc='lower left')

    ax.set_xlabel('Longitude (deg)')
    ax.set_ylabel('Latitude (deg)')
    ax.axis('equal')

    # keep tick formatting from defaulting scienitific with offsets
    from matplotlib.ticker import FormatStrFormatter
    ax.xaxis.set_major_formatter(FormatStrFormatter('%g'))
    ax.yaxis.set_major_formatter(FormatStrFormatter('%g'))
    # limit number of ticks on x-axis to 5 to allow for the squeeze
    locator = ax.xaxis.get_major_locator()
    locator._nbins=5
    ax.xaxis.set_major_locator(locator)

    # legend 
    leg = ax.legend((l2,l1), ('Anchor Position', 'Current Buoy GPS') , 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


    #######################################
    # YYYY_MM 
    #######################################
    title_str = ' '.join([pi['id'].upper(), si['description']])
    
    if plot_type=='latest' or plot_type=='monthly':
        print ' ... : %s' % (yyyy_mm_str,)
        ax.set_title(title_str+' -- ' + yyyy_mm_str)
        dns = date2num(this_month)
        dne = date2num(next_month-datetime.timedelta(seconds=1))
        idx = (dn>=dns) & (dn<=dne)
        c = ax.scatter(gps_lon[idx],gps_lat[idx],c=dn[idx],
                       cmap=cm.jet,vmin=dns,vmax=dne)
        cbar = fig.colorbar(c, ticks=DayLocator(range(0,32,2)),format=DateFormatter('%m/%d'))
        
        fn = '_'.join([pi['id'], si['id'], yyyy_mm_str+'.png'])
        ofn = os.path.join(img_dir, pi['id'], fn) # /home/haines/rayleigh/img/meet
        savefig(ofn)
        # remove scatter instances and colorbar axis for next plot
        c.remove()
        fig.delaxes(fig.axes[1])
        fig.subplots_adjust(left=0.20, bottom=0.10, right=0.9, top=0.9, wspace=0.1, hspace=0.1)


    #######################################
    # Last 30 days
    #######################################
    if plot_type=='latest':
        print ' ... Last 30 days'
        ax.set_title(title_str+'\n Last 30 days from '+last_dt_str)
        dns = date2num(dt[-1])-30
        dne = date2num(dt[-1])
        idx = (dn>=dns) & (dn<=dne)
        c = ax.scatter(gps_lon[idx],gps_lat[idx],c=dn[idx],
                       cmap=cm.jet,vmin=dns,vmax=dne)
        cbar = fig.colorbar(c, ticks=DayLocator(range(0,32,2)),format=DateFormatter('%m/%d'))
        
        fn = '_'.join([pi['id'], si['id'], 'last30days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img/
        savefig(ofn)
        # remove scatter instances and colorbar axis for next plot
        c.remove()
        fig.delaxes(fig.axes[1])
        fig.subplots_adjust(left=0.20, bottom=0.10, right=0.9, top=0.9, wspace=0.1, hspace=0.1)

    #######################################
    # Last 7 days
    #######################################
    if plot_type=='latest':
        print ' ... Last 7 days'
        ax.set_title(title_str+'\n Last 7 days from '+last_dt_str)
        dns = date2num(dt[-1])-7
        dne = date2num(dt[-1])
        idx = (dn>=dns) & (dn<=dne)
        c = ax.scatter(gps_lon[idx],gps_lat[idx],c=dn[idx],
                       cmap=cm.jet,vmin=dns,vmax=dne)
        cbar = fig.colorbar(c, ticks=DayLocator(range(0,32,1)),format=DateFormatter('%m/%d'))
                
        fn = '_'.join([pi['id'], si['id'], 'last07days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)
        # remove scatter instances and colorbar axis for next plot
        c.remove()
        fig.delaxes(fig.axes[1])
        fig.subplots_adjust(left=0.20, bottom=0.10, right=0.9, top=0.9, wspace=0.1, hspace=0.1)

    #######################################
    # Last 1 day (24hrs)
    #######################################
    if plot_type=='latest':
        print ' ... Last 1 days'
        ax.set_title(title_str+'\n Last 24 hours from '+last_dt_str)
        dns = date2num(dt[-1])-1
        dne = date2num(dt[-1])
        idx = (dn>=dns) & (dn<=dne)
        c = ax.scatter(gps_lon[idx],gps_lat[idx],c=dn[idx],
                       cmap=cm.jet,vmin=dns,vmax=dne)
        cbar = fig.colorbar(c, ticks=HourLocator(range(0,25,1)),format=DateFormatter('%H'))

        fn = '_'.join([pi['id'], si['id'], 'last01days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)
        # remove scatter instances and colorbar axis for next plot
        c.remove()
        fig.delaxes(fig.axes[1])
        fig.subplots_adjust(left=0.20, bottom=0.10, right=0.9, top=0.9, wspace=0.1, hspace=0.1)
コード例 #12
0
ファイル: bogue_waves_plot.py プロジェクト: nccoos/proc2plot
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'
        return

    # 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'
        return

    # 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 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(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('Depth (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')
コード例 #13
0
ファイル: plot_cr1000_wind.py プロジェクト: nccoos/proc2plot
def timeseries(pi, si, yyyy_mm, plot_type='latest'):
    """ 
    """
    print 'plot_cr1000_wind ...'
    img_dir = '/home/haines/rayleigh/img'

    prev_month, this_month, next_month = procutil.find_months(yyyy_mm)
    yyyy_mm_str = this_month.strftime('%Y_%m')

    fn = '_'.join([pi['id'], si['id'], prev_month.strftime('%Y_%m')+'.nc'])
    ncFile1= os.path.join(si['proc_dir'], fn)
    fn = '_'.join([pi['id'], si['id'], this_month.strftime('%Y_%m')+'.nc'])
    ncFile2= os.path.join(si['proc_dir'], fn)

    # 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'
        return

    # 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)

    wspd1 = nc.var('wspd1')[:]
    wspd2 = nc.var('wspd2')[:]
    wgust1 = nc.var('wgust1')[:]
    wgust2 = nc.var('wgust2')[:]
    wdir1 = nc.var('wdir1')[:]
    wdir2 = nc.var('wdir2')[:]

    # 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")

    #######################################
    # Build Plot 
    #######################################

    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(4,1,1)
    axs = [ax]

    (x, y) = procutil.addnan(dt, wspd1)
    # ax.plot returns a list of lines, so unpack tuple
    l1, = ax.plot_date(x, y, fmt='b-')
    l1.set_label('Anemometer 1')

    (x, y) = procutil.addnan(dt, wspd2)
    l2, = ax.plot_date(x, y, fmt='g-')
    l2.set_label('Anemometer 2')

    ax.set_ylabel('Wind Speed\n (m/s)')

    # right-hand side scale
    ax2 = twinx(ax)
    ax2.yaxis.tick_right()
    f = [procutil.udconvert(val, 'm/sec', 'knot')[0] for val in ax.get_ylim()]    
    ax2.set_ylim(f)
    ax2.set_ylabel('(knot)')

    # this only moves the label not the tick labels
    ax.xaxis.set_label_position('top')

    # 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,2)
    axs.append(ax)

    # ax.plot returns a list of lines, so unpack tuple
    (x, y) = procutil.addnan(dt, wgust1)
    l1, = ax.plot_date(x, y, fmt='b-')

    (x, y) = procutil.addnan(dt, wgust2)
    l2, = ax.plot_date(x, y, fmt='g-')

    ax.set_ylabel('Wind Gust \n (m/s)')
    # ax.set_ylim(0,10)

    # right-hand side scale
    ax2 = twinx(ax)
    ax2.yaxis.tick_right()
    f = [procutil.udconvert(val, 'm/sec', 'knot')[0] for val in ax.get_ylim()]    
    ax2.set_ylim(f)
    ax2.set_ylabel('(knot)')


    #######################################
    #
    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, wdir1)
    l1, = ax.plot_date(x, y, fmt='b-')
    
    (x, y) = procutil.addnan(dt, wdir2)
    l2, = ax.plot_date(x, y, fmt='g-')

    ax.set_ylabel('Wind Direction \n(deg N)')
    # ax.set_ylim(0,360)

    #######################################
    # YYYY_MM 
    #######################################
    title_str = ' '.join([pi['id'].upper(), si['description']])
    
    if plot_type=='latest' or plot_type=='monthly':
        print ' ... : %s' % (yyyy_mm_str,)
        # save figure for this month
        for idx, ax in enumerate(axs):
            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([])
            if idx==0:
                ax.set_xlabel(title_str+' -- ' + yyyy_mm_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- ' + yyyy_mm_str)

        fn = '_'.join([pi['id'], si['id'], yyyy_mm_str+'.png'])
        ofn = os.path.join(img_dir, pi['id'], fn) # /home/haines/rayleigh/img/meet
        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(title_str+' -- Last 30 days from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- Last 30 days from '+last_dt_str)

        fn = '_'.join([pi['id'], si['id'], 'last30days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img/
        savefig(ofn)

    #######################################
    # 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(title_str+' -- Last 7 days from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%m/%d') )
                ax.set_xlabel(title_str+' -- Last 7 days from '+last_dt_str)
                
        fn = '_'.join([pi['id'], si['id'], 'last07days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)

    #######################################
    # 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(title_str+' -- Last 24 hours from '+last_dt_str)
            elif idx==len(axs)-1:
                ax.xaxis.set_major_formatter( DateFormatter('%H') )
                ax.set_xlabel(title_str+' -- Last 24 hours from '+last_dt_str)


        fn = '_'.join([pi['id'], si['id'], 'last01days.png'])
        ofn = os.path.join(img_dir, fn) # /home/haines/rayleigh/img
        savefig(ofn)