def main(cmdLineArgs,stream=False):
  numpy.seterr(divide='ignore', invalid='ignore', over='ignore') # To avoid warnings

  if not os.path.exists(cmdLineArgs.gridspec): raise ValueError('Specified gridspec directory/tar file does not exist.')
  if os.path.isdir(cmdLineArgs.gridspec):
    x = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['x'][::2,::2]
    xcenter = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['x'][1::2,1::2]
    y = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['y'][::2,::2]
    ycenter = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['y'][1::2,1::2]
    msk = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_mask.nc').variables['mask'][:]
    area = msk*netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['area'][:,:].reshape([msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
    depth = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_topog.nc').variables['depth'][:]
  elif os.path.isfile(cmdLineArgs.gridspec):
    x = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','x')[::2,::2]
    xcenter = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','x')[1::2,1::2]
    y = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','y')[::2,::2]
    ycenter = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','y')[1::2,1::2]
    msk = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_mask.nc','mask')[:]
    area = msk*m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','area')[:,:].reshape([msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
    depth = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_topog.nc','depth')[:]
  else:
    raise ValueError('Unable to extract grid information from gridspec directory/tar file.') 
  
  
  Sobs = netCDF4.Dataset( cmdLineArgs.woa ).variables['salt']
  if len(Sobs.shape)==3: Sobs = Sobs[0]
  else: Sobs = Sobs[:,0].mean(axis=0)

  rootGroup = netCDF4.MFDataset( cmdLineArgs.infile )
  if 'salt' in rootGroup.variables: varName = 'salt'
  elif 'so' in rootGroup.variables: varName = 'so'
  else: raise Exception('Could not find "salt" or "so" in file "%s"'%(cmdLineArgs.infile))
  if rootGroup.variables[varName].shape[0]>1: Smod = rootGroup.variables[varName][:,0].mean(axis=0)
  else: Smod = rootGroup.variables[varName][0,0]
  
  if cmdLineArgs.suptitle != '':  suptitle = cmdLineArgs.suptitle + ' ' + cmdLineArgs.label
  else: suptitle = rootGroup.title + ' ' + cmdLineArgs.label

  imgbufs = []
  ci=m6plot.pmCI(0.125,2.25,.25)
  if stream is True: img = io.BytesIO()
  else: img = cmdLineArgs.outdir+'/SSS_bias_WOA05.png'
  m6plot.xyplot( Smod - Sobs , x, y, area=area,
      suptitle=suptitle, title='SSS bias (w.r.t. WOA\'05) [ppt]',
      clim=ci, colormap='dunnePM', centerlabels=True, extend='both',
      save=img)
  if stream is True: imgbufs.append(img)
  
  m6plot.xycompare( Smod, Sobs , x, y, area=area,
      suptitle=suptitle,
      title1='SSS [ppt]',
      title2='WOA\'05 SSS [ppt]',
      clim=m6plot.linCI(20,30,10, 31,39,.5), colormap='dunneRainbow', extend='both',
      dlim=ci, dcolormap='dunnePM', dextend='both', centerdlabels=True,
      save=cmdLineArgs.outdir+'/SSS_bias_WOA05.3_panel.png')

  if stream is True:
    return imgbufs
Example #2
0
def main(cmdLineArgs,stream=None):
  numpy.seterr(divide='ignore', invalid='ignore', over='ignore') # To avoid warnings

  if not os.path.exists(cmdLineArgs.gridspec): raise ValueError('Specified gridspec directory/tar file does not exist.')
  if os.path.isdir(cmdLineArgs.gridspec):
    x = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['x'][::2,::2]
    xcenter = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['x'][1::2,1::2]
    y = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['y'][::2,::2]
    ycenter = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['y'][1::2,1::2]
    msk = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_mask.nc').variables['mask'][:]
    area = msk*netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_hgrid.nc').variables['area'][:,:].reshape([msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
    depth = netCDF4.Dataset(cmdLineArgs.gridspec+'/ocean_topog.nc').variables['depth'][:]
  elif os.path.isfile(cmdLineArgs.gridspec):
    x = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','x')[::2,::2]
    xcenter = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','x')[1::2,1::2]
    y = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','y')[::2,::2]
    ycenter = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','y')[1::2,1::2]
    msk = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_mask.nc','mask')[:]
    area = msk*m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_hgrid.nc','area')[:,:].reshape([msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
    depth = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,'ocean_topog.nc','depth')[:]
  else:
    raise ValueError('Unable to extract grid information from gridspec directory/tar file.') 
  
  
  Sobs = netCDF4.Dataset( cmdLineArgs.woa ).variables['salt']
  if len(Sobs.shape)==3: Sobs = Sobs[0]
  else: Sobs = Sobs[:,0].mean(axis=0)

  rootGroup = netCDF4.MFDataset( cmdLineArgs.annual_file )
  if 'salt' in rootGroup.variables: varName = 'salt'
  elif 'so' in rootGroup.variables: varName = 'so'
  else: raise Exception('Could not find "salt" or "so" in file "%s"'%(cmdLineArgs.annual_file))
  if rootGroup.variables[varName].shape[0]>1: Smod = rootGroup.variables[varName][:,0].mean(axis=0)
  else: Smod = rootGroup.variables[varName][0,0]
  
  if cmdLineArgs.suptitle != '':  suptitle = cmdLineArgs.suptitle + ' ' + cmdLineArgs.label
  else: suptitle = rootGroup.title + ' ' + cmdLineArgs.label

  ci=m6plot.pmCI(0.125,2.25,.25)
  if stream is None: stream = cmdLineArgs.outdir+'/SSS_bias_WOA05.png'
  m6plot.xyplot( Smod - Sobs , x, y, area=area,
      suptitle=suptitle, title='SSS bias (w.r.t. WOA\'05) [ppt]',
      clim=ci, colormap='dunnePM', centerlabels=True, extend='both',
      save=stream)
  
  m6plot.xycompare( Smod, Sobs , x, y, area=area,
      suptitle=suptitle,
      title1='SSS [ppt]',
      title2='WOA\'05 SSS [ppt]',
      clim=m6plot.linCI(20,30,10, 31,39,.5), colormap='dunneRainbow', extend='both',
      dlim=ci, dcolormap='dunnePM', dextend='both', centerdlabels=True,
      save=cmdLineArgs.outdir+'/SSS_bias_WOA05.3_panel.png')
msk = numpy.ma.array(msk, mask=(msk==0))

Tobs = netCDF4.Dataset( cmdLineArgs.woa )
if 'temp' in Tobs.variables: Tobs = Tobs.variables['temp']
elif 'ptemp' in Tobs.variables: Tobs = Tobs.variables['ptemp']
else: raise Exception('Could not find "temp" or "ptemp" in file "%s"'%(cmdLineArgs.woa))
if len(Tobs.shape)==3: Tobs = Tobs[0]
else: Tobs = Tobs[:,0].mean(axis=0)

rootGroup = netCDF4.Dataset( cmdLineArgs.annual_file )
if 'temp' in rootGroup.variables: varName = 'temp'
elif 'ptemp' in rootGroup.variables: varName = 'ptemp'
elif 'thetao' in rootGroup.variables: varName = 'thetao'
else: raise Exception('Could not find "temp", "ptemp" or "thetao" in file "%s"'%(cmdLineArgs.annual_file))
if rootGroup.variables[varName].shape[0]>1: salt = rootGroup.variables[varName][:,0].mean(axis=0)
else: Tmod = rootGroup.variables[varName][0,0]

ci=m6plot.pmCI(0.25,4.5,.5)
m6plot.xyplot( Tmod - Tobs , x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label, title='SST bias (w.r.t. WOA\'05) [$\degree$C]',
      clim=ci, colormap='dunnePM', centerlabels=True, extend='both',
      save=cmdLineArgs.outdir+'/SST_bias_WOA05.png')

m6plot.xycompare( Tmod, Tobs , x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label,
      title1='SST [$\degree$C]',
      title2='WOA\'05 SST [$\degree$C]',
      clim=m6plot.linCI(-2,29,.5), colormap='dunneRainbow', extend='max',
      dlim=ci, dcolormap='dunnePM', dextend='both', centerdlabels=True,
      save=cmdLineArgs.outdir+'/SST_bias_WOA05.3_panel.png')
Example #4
0
area = msk*netCDF4.Dataset(cmdLineArgs.gridspecdir+'/ocean_hgrid.nc').variables['area'][:,:].reshape([msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)

variable = rootGroup.variables['MLD_003']
shape = variable.shape
MLD = variable[:].reshape(shape[0]/12,12,shape[1],shape[2]).mean(axis=0)

MLD_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['MLD'][:]
x_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['LONGITUDE'][:]
y_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['LATITUDE'][:]

ciMin = m6plot.linCI(0,95,5)
ciMax = m6plot.linCI(0,680,20)

# Plot of shallowest model MLD (summer)
m6plot.xyplot( MLD.min(axis=0), x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label, title='Annual-minimum MLD$_{0.03}$ [m]',
      clim=ciMin, extend='max', colormap='dunneRainbow',
      save=cmdLineArgs.outdir+'/MLD_003_minimum.png')

# 2-panel plot of shallowest model MLD + obs (summer)
m6plot.setFigureSize(aspect=[3,3], verticalresolution=976, npanels=0)
ax1 = plt.subplot(2,1,1)
m6plot.xyplot( numpy.roll(MLD_obs.min(axis=0),300,axis=-1), x_obs-300, y_obs,
      suptitle=rootGroup.title+' '+cmdLineArgs.label, title='Hosoda et al., 2010, annual-minimum MLD$_{0.03}$ [m]',
      clim=ciMin, extend='max', colormap='dunneRainbow',
      axis=ax1)
ax2 = plt.subplot(2,1,2)
m6plot.xyplot( MLD.min(axis=0), x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label, title='Annual-minimum MLD$_{0.03}$ [m]',
      clim=ciMin, extend='max', colormap='dunneRainbow',
      axis=ax2,
      save=cmdLineArgs.outdir+'/MLD_003_minimum.2_panel.png')
Example #5
0
    Smod = rootGroup.variables[varName][:, 0].mean(axis=0)
else:
    Smod = rootGroup.variables[varName][0]

if len(cmdLineArgs.label1): title1 = cmdLineArgs.label1
else: title1 = rootGroup.title
if len(cmdLineArgs.label2): title2 = cmdLineArgs.label2
else: title2 = rootGroupRef.title

ci = m6plot.pmCI(0.025, 0.525, .05)
m6plot.xyplot(Smod - Sref,
              x,
              y,
              area=area,
              suptitle=title1 + ' - ' + title2,
              title='SSS difference [ppt] ' + cmdLineArgs.label,
              clim=ci,
              colormap='dunnePM',
              centerlabels=True,
              extend='both',
              save=cmdLineArgs.outdir + '/SSS_difference.png')

m6plot.xycompare(Smod,
                 Sref,
                 x,
                 y,
                 area=area,
                 suptitle='SSS difference [ppt] ' + cmdLineArgs.label,
                 title1=title1,
                 title2=title2,
                 clim=m6plot.linCI(20, 30, 10, 31, 39, .5),
Example #6
0
if len(cmdLineArgs.label1): title1 = cmdLineArgs.label1
else: title1 = rootGroup.title
if len(cmdLineArgs.label2): title2 = cmdLineArgs.label2
else: title2 = rootGroupRef.title

ci = m6plot.pmCI(0.05, 1., .1)

tPlot = depthAverageT(Tmod, Zmod, depth, lDepth, uDepth)
tRefPlot = depthAverageT(Tref, Zref, depth, lDepth, uDepth)
m6plot.xyplot(tPlot - tRefPlot,
              x,
              y,
              area=area,
              suptitle=title1 + ' - ' + title2,
              title=r'%i-%im depth-average $\theta$ difference [$\degree$C]' %
              (uDepth, lDepth) + cmdLineArgs.label,
              clim=ci,
              colormap='dunnePM',
              centerlabels=True,
              extend='both',
              save=cmdLineArgs.outdir + '/T_%i-%im_zave_difference.png' %
              (uDepth, lDepth))

m6plot.xycompare(
    tPlot,
    tRefPlot,
    x,
    y,
    area=area,
    suptitle=r'%i-%im depth-average $\theta$ difference [$\degree$C]' %
    (uDepth, lDepth) + cmdLineArgs.label,
Example #7
0
def main(cmdLineArgs, stream=False):
    numpy.seterr(divide='ignore', invalid='ignore',
                 over='ignore')  # To avoid warnings

    if not os.path.exists(cmdLineArgs.gridspec):
        raise ValueError(
            'Specified gridspec directory/tar file does not exist.')
    if os.path.isdir(cmdLineArgs.gridspec):
        x = netCDF4.Dataset(cmdLineArgs.gridspec +
                            '/ocean_hgrid.nc').variables['x'][::2, ::2]
        xcenter = netCDF4.Dataset(cmdLineArgs.gridspec +
                                  '/ocean_hgrid.nc').variables['x'][1::2, 1::2]
        y = netCDF4.Dataset(cmdLineArgs.gridspec +
                            '/ocean_hgrid.nc').variables['y'][::2, ::2]
        ycenter = netCDF4.Dataset(cmdLineArgs.gridspec +
                                  '/ocean_hgrid.nc').variables['y'][1::2, 1::2]
        msk = netCDF4.Dataset(cmdLineArgs.gridspec +
                              '/ocean_mask.nc').variables['mask'][:]
        area = msk * netCDF4.Dataset(
            cmdLineArgs.gridspec +
            '/ocean_hgrid.nc').variables['area'][:, :].reshape(
                [msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
        depth = netCDF4.Dataset(cmdLineArgs.gridspec +
                                '/ocean_topog.nc').variables['depth'][:]
    elif os.path.isfile(cmdLineArgs.gridspec):
        x = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_hgrid.nc',
                                    'x')[::2, ::2]
        xcenter = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,
                                          'ocean_hgrid.nc', 'x')[1::2, 1::2]
        y = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_hgrid.nc',
                                    'y')[::2, ::2]
        ycenter = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,
                                          'ocean_hgrid.nc', 'y')[1::2, 1::2]
        msk = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_mask.nc',
                                      'mask')[:]
        area = msk * m6toolbox.readNCFromTar(
            cmdLineArgs.gridspec, 'ocean_hgrid.nc', 'area')[:, :].reshape(
                [msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
        depth = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_topog.nc',
                                        'depth')[:]
    else:
        raise ValueError(
            'Unable to extract grid information from gridspec directory/tar file.'
        )

    # open dataset
    rootGroup = netCDF4.MFDataset(cmdLineArgs.infile)

    # gather months from input dataset
    tvar = rootGroup.variables['time']
    times = [
        netCDF4.num2date(i, tvar.units, calendar=tvar.calendar.lower())
        for i in tvar[:]
    ]
    idx = list(set([i.month - 1 for i in times]))
    month_label = [i.strftime('%b') for i in times]
    month_label = month_label[:len(idx)]
    month_label = str.join(', ', month_label)

    # read sst from model
    if 'sst' in rootGroup.variables: varName = 'sst'
    elif 'tos' in rootGroup.variables: varName = 'tos'
    else:
        raise Exception('Could not find "sst", "ptemp" or "tos" in file "%s"' %
                        (cmdLineArgs.infile))
    if rootGroup.variables[varName].shape[0] > 1:
        Tmod = rootGroup.variables[varName][:].mean(axis=0)
    else:
        Tmod = rootGroup.variables[varName][0]

    # read sst from obs
    Tobs = netCDF4.Dataset(cmdLineArgs.woa_monthly)
    if 'temp' in Tobs.variables: Tobs = Tobs.variables['temp']
    elif 'ptemp' in Tobs.variables: Tobs = Tobs.variables['ptemp']
    else:
        raise Exception('Could not find "temp" or "ptemp" in file "%s"' %
                        (cmdLineArgs.woa_monthly))
    if len(Tobs.shape) == 3: Tobs = Tobs[0]
    else: Tobs = Tobs[idx, 0].mean(axis=0)

    # create title for plot
    if cmdLineArgs.suptitle != '':
        suptitle = cmdLineArgs.suptitle + ' ' + cmdLineArgs.label
    else:
        suptitle = rootGroup.title + ' ' + cmdLineArgs.label

    # invoke m6plot
    imgbufs = []
    ci = m6plot.pmCI(0.25, 4.5, .5)
    if stream is True: objOut = io.BytesIO()
    else: objOut = cmdLineArgs.outdir + '/SST_bias_WOA05.png'
    m6plot.xyplot(Tmod - Tobs,
                  x,
                  y,
                  area=area,
                  suptitle=suptitle,
                  title=month_label +
                  ' SST bias (w.r.t. WOA\'05) [$\degree$C]',
                  clim=ci,
                  colormap='dunnePM',
                  centerlabels=True,
                  extend='both',
                  save=objOut)
    if stream is True: imgbufs.append(objOut)

    m6plot.xycompare(Tmod,
                     Tobs,
                     x,
                     y,
                     area=area,
                     suptitle=suptitle,
                     title1=month_label + ' SST [$\degree$C]',
                     title2='WOA\'05 ' + month_label + ' SST [$\degree$C]',
                     clim=m6plot.linCI(-2, 29, .5),
                     colormap='dunneRainbow',
                     extend='max',
                     dlim=ci,
                     dcolormap='dunnePM',
                     dextend='both',
                     centerdlabels=True,
                     save=cmdLineArgs.outdir + '/SST_bias_WOA05.3_panel.png')

    if stream is True:
        return imgbufs
ssv = rootGroup.variables['ssv']
ssv_mean = ssv[:].mean(axis=0)
eke_v = (0.5 * (ssv - ssv_mean)**2).mean(axis=0)
eke = eke + (eke_v + numpy.roll(eke_v, 1, axis=-2)) / 2

#factor of 10000 to convert to (cm/s)^2
eke = 10000 * eke

#clip the extreme small values that cause log to blow up
eke = eke.clip(min=1.0e-8)

#Plot with logscale=True
#ci=m6plot.pmCI(0.0,10.0,0.1)
#plot_title = 'Eddy Kinetic Energy annual mean [(cm/s)^2]'

#The logscale=True of matplotlib.pyplot does not work!
#So, pass the log to be plotted instead
eke = numpy.log(eke)
plot_title = 'Log of Eddy Kinetic Energy annual mean [(cm/s)^2]'

m6plot.xyplot(
    eke,
    x,
    y,
    area=area,
    suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
    title=plot_title,
    #      clim=ci, logscale=True,
    save=cmdLineArgs.outdir + '/EKE_mean.png')
Example #9
0
x = netCDF4.Dataset(cmdLineArgs.gridspecdir+'/ocean_hgrid.nc').variables['x'][::2,::2]
y = netCDF4.Dataset(cmdLineArgs.gridspecdir+'/ocean_hgrid.nc').variables['y'][::2,::2]
msk = netCDF4.Dataset(cmdLineArgs.gridspecdir+'/ocean_mask.nc').variables['mask'][:]
area = msk*netCDF4.Dataset(cmdLineArgs.gridspecdir+'/ocean_hgrid.nc').variables['area'][:,:].reshape([msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
msk = numpy.ma.array(msk, mask=(msk==0))

Sobs = netCDF4.Dataset( cmdLineArgs.woa ).variables['salt']
if len(Sobs.shape)==3: Sobs = Sobs[0]
else: Sobs = Sobs[:,0].mean(axis=0)

rootGroup = netCDF4.Dataset( cmdLineArgs.annual_file )
if 'salt' in rootGroup.variables: varName = 'salt'
elif 'so' in rootGroup.variables: varName = 'so'
else: raise Exception('Could not find "salt" or "so" in file "%s"'%(cmdLineArgs.annual_file))
if rootGroup.variables[varName].shape[0]>1: salt = rootGroup.variables[varName][:,0].mean(axis=0)
else: Smod = rootGroup.variables[varName][0,0]

ci=m6plot.pmCI(0.125,2.25,.25)
m6plot.xyplot( Smod - Sobs , x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label, title='SSS bias (w.r.t. WOA\'05) [ppt]',
      clim=ci, colormap='dunnePM', centerlabels=True, extend='both',
      save=cmdLineArgs.outdir+'/SSS_bias_WOA05.png')

m6plot.xycompare( Smod, Sobs , x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label,
      title1='SSS [ppt]',
      title2='WOA\'05 SSS [ppt]',
      clim=m6plot.linCI(20,30,10, 31,39,.5), colormap='dunneRainbow', extend='both',
      dlim=ci, dcolormap='dunnePM', dextend='both', centerdlabels=True,
      save=cmdLineArgs.outdir+'/SSS_bias_WOA05.3_panel.png')
Example #10
0
ssu_mean = ssu[:].mean(axis=0)
eke_u = (0.5*(ssu-ssu_mean)**2).mean(axis=0)
eke = (eke_u + numpy.roll(eke_u,1,axis=-1))/2 # U-point to T-point transform

ssv = rootGroup.variables['ssv']
ssv_mean = ssv[:].mean(axis=0)
eke_v = (0.5*(ssv-ssv_mean)**2).mean(axis=0)
eke = eke + (eke_v + numpy.roll(eke_v,1,axis=-2))/2 

#factor of 10000 to convert to (cm/s)^2
eke = 10000*eke

#clip the extreme small values that cause log to blow up
eke = eke.clip(min=1.0e-8)

#Plot with logscale=True
#ci=m6plot.pmCI(0.0,10.0,0.1)
#plot_title = 'Eddy Kinetic Energy annual mean [(cm/s)^2]'

#The logscale=True of matplotlib.pyplot does not work!
#So, pass the log to be plotted instead
eke = numpy.log(eke)
plot_title = 'Log of Eddy Kinetic Energy annual mean [(cm/s)^2]'

m6plot.xyplot( eke, x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label, title=plot_title,
#      clim=ci, logscale=True,
      save=cmdLineArgs.outfile )

Example #11
0
shape = variable.shape
MLD = variable[:].reshape(shape[0] / 12, 12, shape[1], shape[2]).mean(axis=0)

MLD_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['MLD'][:]
x_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['LONGITUDE'][:]
y_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['LATITUDE'][:]

ciMin = m6plot.linCI(0, 95, 5)
ciMax = m6plot.linCI(0, 680, 20)

# Plot of shallowest model MLD (summer)
m6plot.xyplot(MLD.min(axis=0),
              x,
              y,
              area=area,
              suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
              title='Annual-minimum MLD$_{0.03}$ [m]',
              clim=ciMin,
              extend='max',
              colormap='dunneRainbow',
              save=cmdLineArgs.outdir + '/MLD_003_minimum.png')

# 2-panel plot of shallowest model MLD + obs (summer)
m6plot.setFigureSize(aspect=[3, 3], verticalresolution=976, npanels=0)
ax1 = plt.subplot(2, 1, 1)
m6plot.xyplot(numpy.roll(MLD_obs.min(axis=0), 300, axis=-1),
              x_obs - 300,
              y_obs,
              suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
              title='Hosoda et al., 2010, annual-minimum MLD$_{0.03}$ [m]',
              clim=ciMin,
              extend='max',
Example #12
0
def main(cmdLineArgs, stream=None):
    numpy.seterr(divide='ignore', invalid='ignore',
                 over='ignore')  # To avoid warnings

    if not os.path.exists(cmdLineArgs.gridspec):
        raise ValueError(
            'Specified gridspec directory/tar file does not exist.')
    if os.path.isdir(cmdLineArgs.gridspec):
        x = netCDF4.Dataset(cmdLineArgs.gridspec +
                            '/ocean_hgrid.nc').variables['x'][::2, ::2]
        xcenter = netCDF4.Dataset(cmdLineArgs.gridspec +
                                  '/ocean_hgrid.nc').variables['x'][1::2, 1::2]
        y = netCDF4.Dataset(cmdLineArgs.gridspec +
                            '/ocean_hgrid.nc').variables['y'][::2, ::2]
        ycenter = netCDF4.Dataset(cmdLineArgs.gridspec +
                                  '/ocean_hgrid.nc').variables['y'][1::2, 1::2]
        msk = netCDF4.Dataset(cmdLineArgs.gridspec +
                              '/ocean_mask.nc').variables['mask'][:]
        area = msk * netCDF4.Dataset(
            cmdLineArgs.gridspec +
            '/ocean_hgrid.nc').variables['area'][:, :].reshape(
                [msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
        depth = netCDF4.Dataset(cmdLineArgs.gridspec +
                                '/ocean_topog.nc').variables['depth'][:]
    elif os.path.isfile(cmdLineArgs.gridspec):
        x = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_hgrid.nc',
                                    'x')[::2, ::2]
        xcenter = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,
                                          'ocean_hgrid.nc', 'x')[1::2, 1::2]
        y = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_hgrid.nc',
                                    'y')[::2, ::2]
        ycenter = m6toolbox.readNCFromTar(cmdLineArgs.gridspec,
                                          'ocean_hgrid.nc', 'y')[1::2, 1::2]
        msk = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_mask.nc',
                                      'mask')[:]
        area = msk * m6toolbox.readNCFromTar(
            cmdLineArgs.gridspec, 'ocean_hgrid.nc', 'area')[:, :].reshape(
                [msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
        depth = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_topog.nc',
                                        'depth')[:]
    else:
        raise ValueError(
            'Unable to extract grid information from gridspec directory/tar file.'
        )

    Tobs = netCDF4.Dataset(cmdLineArgs.woa)
    if 'temp' in Tobs.variables: Tobs = Tobs.variables['temp']
    elif 'ptemp' in Tobs.variables: Tobs = Tobs.variables['ptemp']
    else:
        raise Exception('Could not find "temp" or "ptemp" in file "%s"' %
                        (cmdLineArgs.woa))
    if len(Tobs.shape) == 3: Tobs = Tobs[0]
    else: Tobs = Tobs[:, 0].mean(axis=0)

    rootGroup = netCDF4.MFDataset(cmdLineArgs.annual_file)
    if 'temp' in rootGroup.variables: varName = 'temp'
    elif 'ptemp' in rootGroup.variables: varName = 'ptemp'
    elif 'thetao' in rootGroup.variables: varName = 'thetao'
    else:
        raise Exception(
            'Could not find "temp", "ptemp" or "thetao" in file "%s"' %
            (cmdLineArgs.annual_file))
    if rootGroup.variables[varName].shape[0] > 1:
        Tmod = rootGroup.variables[varName][:, 0].mean(axis=0)
    else:
        Tmod = rootGroup.variables[varName][0, 0]

    if cmdLineArgs.suptitle != '':
        suptitle = cmdLineArgs.suptitle + ' ' + cmdLineArgs.label
    else:
        suptitle = rootGroup.title + ' ' + cmdLineArgs.label

    ci = m6plot.pmCI(0.25, 4.5, .5)
    if stream is None: stream = cmdLineArgs.outdir + '/SST_bias_WOA05.png'
    m6plot.xyplot(Tmod - Tobs,
                  x,
                  y,
                  area=area,
                  suptitle=suptitle,
                  title='SST bias (w.r.t. WOA\'05) [$\degree$C]',
                  clim=ci,
                  colormap='dunnePM',
                  centerlabels=True,
                  extend='both',
                  save=stream)

    m6plot.xycompare(Tmod,
                     Tobs,
                     x,
                     y,
                     area=area,
                     suptitle=suptitle,
                     title1='SST [$\degree$C]',
                     title2='WOA\'05 SST [$\degree$C]',
                     clim=m6plot.linCI(-2, 29, .5),
                     colormap='dunneRainbow',
                     extend='max',
                     dlim=ci,
                     dcolormap='dunnePM',
                     dextend='both',
                     centerdlabels=True,
                     save=cmdLineArgs.outdir + '/SST_bias_WOA05.3_panel.png')
Example #13
0
def depthAverageT(T, z, d, lowerDepth, upperDepth):
  D = numpy.minimum(d, lowerDepth)
  H = numpy.zeros(T.shape[1:])
  HT = numpy.zeros(T.shape[1:])
  for k in range(T.shape[0]):
    zTop = numpy.minimum( z[k], -upperDepth )
    zBot = numpy.maximum( z[k+1], -D )
    dh = numpy.maximum( zTop -zBot, 0. )
    #dh = numpy.minimum( z[k]-z[k+1], D-H)
    H = H + dh
    HT = HT + dh*T[k]
  return HT/(H+1.e-20)

ci=m6plot.pmCI(0.25,4.5,.5)

tPlot = depthAverageT(Tmod, Zmod, depth, lDepth, uDepth)
tObsPlot = depthAverageT(Tobs, Zobs, depth, lDepth, uDepth)
m6plot.xyplot( tPlot - tObsPlot , x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label,
      title=r'''%i-%im depth-average $\theta$ bias (w.r.t. WOA'05) [$\degree$C]'''%(uDepth,lDepth),
      clim=ci, colormap='dunnePM', centerlabels=True, extend='both',
      save=cmdLineArgs.outdir+'/T_%i-%im_zave_bias_WOA05.png'%(uDepth,lDepth))

m6plot.xycompare( tPlot, tObsPlot , x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label,
      title1=r'%i-%im depth-average $\theta$ [$\degree$C]'%(uDepth,lDepth),
      title2=r'''WOA'05 $\theta$ [$\degree$C]''',
      clim=m6plot.linCI(-2,29,.5), colormap='dunneRainbow', extend='max',
      dlim=ci, dcolormap='dunnePM', dextend='both', centerdlabels=True,
      save=cmdLineArgs.outdir+'/T_%i-%im_zave_bias_WOA05.3_panel.png'%(uDepth,lDepth))
Example #14
0
MLDmod = variable[:].reshape(shape[0]/12,12,shape[1],shape[2]).mean(axis=0)

if len(cmdLineArgs.label1): title1 = cmdLineArgs.label1
else: title1 = rootGroup.title
if len(cmdLineArgs.label2): title2 = cmdLineArgs.label2
else: title2 = rootGroupRef.title

ciMin = m6plot.linCI(0,95,5)
ciMax = m6plot.linCI(0,680,20)
diMin = m6plot.pmCI(2.5,32.5,5)
diMax = m6plot.pmCI(0,20,5, 40,100,20)
diMax = numpy.array([-250,-200,-150,-100,-50,-20,-10,-3,3,10,20,50,100,150,200,250])

m6plot.xyplot( MLDmod.min(axis=0) - MLDref.min(axis=0), x, y, area=area,
      suptitle=title1+' - '+title2,
      title='MLD difference [m] '+cmdLineArgs.label,
      clim=diMin, colormap='dunnePM', centerlabels=True, extend='both',
      save=cmdLineArgs.outdir+'/MLD_minimum_difference.png')

m6plot.xycompare( MLDmod.min(axis=0), MLDref.min(axis=0), x, y, area=area,
      suptitle='MLD difference [m] '+cmdLineArgs.label,
      title1=title1, title2=title2,
      clim=ciMin, colormap='dunneRainbow', extend='max',
      dlim=diMin, dcolormap='dunnePM', dextend='both', centerdlabels=True,
      save=cmdLineArgs.outdir+'/MLD_minimum_difference.3_panel.png')

m6plot.xyplot( MLDmod.max(axis=0) - MLDref.max(axis=0), x, y, area=area,
      suptitle=title1+' - '+title2,
      title='MLD difference [m] '+cmdLineArgs.label,
      clim=diMax, colormap='dunnePM', centerlabels=False, extend='both',
      save=cmdLineArgs.outdir+'/MLD_maximum_difference.png')
Example #15
0
        H = H + dh
        HT = HT + dh * T[k]
    return HT / (H + 1.e-20)


ci = m6plot.pmCI(0.25, 4.5, .5)

tPlot = depthAverageT(Tmod, Zmod, depth, lDepth, uDepth)
tObsPlot = depthAverageT(Tobs, Zobs, depth, lDepth, uDepth)
m6plot.xyplot(
    tPlot - tObsPlot,
    x,
    y,
    area=area,
    suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
    title=r'''%i-%im depth-average $\theta$ bias (w.r.t. WOA'05) [$\degree$C]'''
    % (uDepth, lDepth),
    clim=ci,
    colormap='dunnePM',
    centerlabels=True,
    extend='both',
    save=cmdLineArgs.outdir + '/T_%i-%im_zave_bias_WOA05.png' %
    (uDepth, lDepth))

m6plot.xycompare(
    tPlot,
    tObsPlot,
    x,
    y,
    area=area,
    suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
    title1=r'%i-%im depth-average $\theta$ [$\degree$C]' % (uDepth, lDepth),
Example #16
0
elif 'so' in rootGroupRef.variables: varName='so'
else: raise Exception('Could not find "salt" or "so" in file "%s"'%(cmdLineArgs.ref))
if rootGroupRef.variables[varName].shape[0]>1: Sref = rootGroupRef.variables[varName][:,0].mean(axis=0)
else: Sref = rootGroupRef.variables[varName][0,0]

rootGroup = netCDF4.Dataset( cmdLineArgs.annual_file )
if 'salt' in rootGroup.variables: varName='salt'
elif 'so' in rootGroup.variables: varName='so'
else: raise Exception('Could not find "salt" or "so" in file "%s"'%(cmdLineArgs.annual_file))
if len(rootGroup.variables[varName].shape)==4: Smod = rootGroup.variables[varName][:,0].mean(axis=0)
else: Smod = rootGroup.variables[varName][0]

if len(cmdLineArgs.label1): title1 = cmdLineArgs.label1
else: title1 = rootGroup.title
if len(cmdLineArgs.label2): title2 = cmdLineArgs.label2
else: title2 = rootGroupRef.title

ci=m6plot.pmCI(0.025,0.525,.05)
m6plot.xyplot( Smod - Sref , x, y, area=area,
      suptitle=title1+' - '+title2,
      title='SSS difference [ppt] '+cmdLineArgs.label,
      clim=ci, colormap='dunnePM', centerlabels=True, extend='both',
      save=cmdLineArgs.outdir+'/SSS_difference.png')

m6plot.xycompare( Smod, Sref , x, y, area=area,
      suptitle='SSS difference [ppt] '+cmdLineArgs.label,
      title1=title1, title2=title2,
      clim=m6plot.linCI(20,30,10, 31,39,.5), colormap='dunneRainbow', extend='max',
      dlim=ci, dcolormap='dunnePM', dextend='both', centerdlabels=True,
      save=cmdLineArgs.outdir+'/SSS_difference.3_panel.png')
Example #17
0
def main(cmdLineArgs, stream=False):
    rootGroup = netCDF4.MFDataset(cmdLineArgs.infile)
    if 'MLD_003' not in rootGroup.variables:
        raise Exception('Could not find "MLD_003" in file "%s"' %
                        (cmdLineArgs.infile))

    if not os.path.exists(cmdLineArgs.gridspec):
        raise ValueError(
            'Specified gridspec directory/tar file does not exist.')
    if os.path.isdir(cmdLineArgs.gridspec):
        x = netCDF4.Dataset(cmdLineArgs.gridspec +
                            '/ocean_hgrid.nc').variables['x'][::2, ::2]
        y = netCDF4.Dataset(cmdLineArgs.gridspec +
                            '/ocean_hgrid.nc').variables['y'][::2, ::2]
        msk = netCDF4.Dataset(cmdLineArgs.gridspec +
                              '/ocean_mask.nc').variables['mask'][:]
        area = msk * netCDF4.Dataset(
            cmdLineArgs.gridspec +
            '/ocean_hgrid.nc').variables['area'][:, :].reshape(
                [msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
    elif os.path.isfile(cmdLineArgs.gridspec):
        x = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_hgrid.nc',
                                    'x')[::2, ::2]
        y = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_hgrid.nc',
                                    'y')[::2, ::2]
        msk = m6toolbox.readNCFromTar(cmdLineArgs.gridspec, 'ocean_mask.nc',
                                      'mask')[:]
        area = msk * m6toolbox.readNCFromTar(
            cmdLineArgs.gridspec, 'ocean_hgrid.nc', 'area')[:, :].reshape(
                [msk.shape[0], 2, msk.shape[1], 2]).sum(axis=-3).sum(axis=-1)
    else:
        raise ValueError(
            'Unable to extract grid information from gridspec directory/tar file.'
        )

    variable = rootGroup.variables['MLD_003']
    shape = variable.shape
    MLD = variable[:].reshape(shape[0] / 12, 12, shape[1],
                              shape[2]).mean(axis=0)

    if not hasattr(cmdLineArgs, 'obsdata') or cmdLineArgs.obsdata == '':
        cmdLineArgs.obsdata = '/archive/gold/datasets/obs/Hosada2010_MLD_climatology.v20140515.nc'

    MLD_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['MLD'][:]
    x_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['LONGITUDE'][:]
    y_obs = netCDF4.Dataset(cmdLineArgs.obsdata).variables['LATITUDE'][:]

    ciMin = m6plot.linCI(0, 95, 5)
    ciMax = m6plot.linCI(0, 680, 20)

    imgbufs = []

    # Plot of shallowest model MLD (summer)
    m6plot.xyplot(MLD.min(axis=0),
                  x,
                  y,
                  area=area,
                  suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
                  title='Annual-minimum MLD$_{0.03}$ [m]',
                  clim=ciMin,
                  extend='max',
                  colormap='dunneRainbow',
                  save=cmdLineArgs.outdir + '/MLD_003_minimum.png')

    # 2-panel plot of shallowest model MLD + obs (summer)
    if stream is True: img = io.BytesIO()
    else: img = cmdLineArgs.outdir + '/MLD_003_minimum.2_panel.png'
    m6plot.setFigureSize(aspect=[3, 3], verticalresolution=976, npanels=0)
    ax1 = plt.subplot(2, 1, 1)
    m6plot.xyplot(numpy.roll(MLD_obs.min(axis=0), 300, axis=-1),
                  x_obs - 300,
                  y_obs,
                  suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
                  title='Hosoda et al., 2010, annual-minimum MLD$_{0.03}$ [m]',
                  clim=ciMin,
                  extend='max',
                  colormap='dunneRainbow',
                  axis=ax1)
    ax2 = plt.subplot(2, 1, 2)
    m6plot.xyplot(MLD.min(axis=0),
                  x,
                  y,
                  area=area,
                  suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
                  title='Annual-minimum MLD$_{0.03}$ [m]',
                  clim=ciMin,
                  extend='max',
                  colormap='dunneRainbow',
                  axis=ax2,
                  save=img)
    if stream is True: imgbufs.append(img)

    # Plot of deepest model MLD (winter)
    m6plot.xyplot(MLD.max(axis=0),
                  x,
                  y,
                  area=area,
                  suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
                  title='Annual-maximum MLD$_{0.03}$ [m]',
                  clim=ciMax,
                  extend='max',
                  colormap='dunneRainbow',
                  save=cmdLineArgs.outdir + '/MLD_003_maximum.png')

    # 2-panel plot of deepest model MLD + obs (winter)
    if stream is True: img = io.BytesIO()
    else: img = cmdLineArgs.outdir + '/MLD_003_maximum.2_panel.png'
    m6plot.setFigureSize(aspect=[3, 3], verticalresolution=976, npanels=0)
    ax1 = plt.subplot(2, 1, 1)
    m6plot.xyplot(numpy.roll(MLD_obs.max(axis=0), 300, axis=-1),
                  x_obs - 300,
                  y_obs,
                  suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
                  title='Hosoda et al., 2010, annual-maximum MLD$_{0.03}$ [m]',
                  clim=ciMax,
                  extend='max',
                  colormap='dunneRainbow',
                  axis=ax1)
    ax2 = plt.subplot(2, 1, 2)
    m6plot.xyplot(MLD.max(axis=0),
                  x,
                  y,
                  area=area,
                  suptitle=rootGroup.title + ' ' + cmdLineArgs.label,
                  title='Annual-maximum MLD$_{0.03}$ [m]',
                  clim=ciMax,
                  extend='max',
                  colormap='dunneRainbow',
                  axis=ax2,
                  save=img)
    if stream is True: imgbufs.append(img)

    if stream is True:
        return imgbufs
  for k in range(T.shape[0]):
    zTop = numpy.minimum( z[k], -upperDepth )
    zBot = numpy.maximum( z[k+1], -D )
    dh = numpy.maximum( zTop -zBot, 0. )
    #dh = numpy.minimum( z[k]-z[k+1], D-H)
    H = H + dh
    HT = HT + dh*T[k]
  return HT/(H+1.e-20)

if len(cmdLineArgs.label1): title1 = cmdLineArgs.label1
else: title1 = rootGroup.title
if len(cmdLineArgs.label2): title2 = cmdLineArgs.label2
else: title2 = rootGroupRef.title

ci=m6plot.pmCI(0.05,1.,.1)

tPlot = depthAverageT(Tmod, Zmod, depth, lDepth, uDepth)
tRefPlot = depthAverageT(Tref, Zref, depth, lDepth, uDepth)
m6plot.xyplot( tPlot - tRefPlot , x, y, area=area,
      suptitle=rootGroup.title+' '+cmdLineArgs.label,
      title=r'%i-%im depth-average $\theta$ difference [$\degree$C]'%(uDepth,lDepth),
      clim=ci, colormap='dunnePM', centerlabels=True, extend='both',
      save=cmdLineArgs.outdir+'/T_%i-%im_zave_difference.png'%(uDepth,lDepth))

m6plot.xycompare( tPlot, tRefPlot , x, y, area=area,
      suptitle=r'%i-%im depth-average $\theta$ difference [$\degree$C]'%(uDepth,lDepth)+cmdLineArgs.label,
      title1=title1, title2=title2,
      clim=m6plot.linCI(-2,29,.5), colormap='dunneRainbow', extend='max',
      dlim=ci, dcolormap='dunnePM', dextend='both', centerdlabels=True,
      save=cmdLineArgs.outdir+'/T_%i-%im_zave_difference.3_panel.png'%(uDepth,lDepth))