Ejemplo n.º 1
0
def roms2z(var, grd, grdz, Cpos='rho', irange=None, jrange=None, \
           spval=1e37, mode='linear'):
    """
    varz = roms2z(var, grd, grdz)

    optional switch:
      - Cpos='rho', 'u' 'v' or 'w'   specify the C-grid position where
                                     the variable rely
      - irange                       specify grid sub-sample for i direction
      - jrange                       specify grid sub-sample for j direction
      - spval=1e37                   define spval value
      - mode='linear' or 'spline'    specify the type of interpolation

    Interpolate the variable from ROMS grid grd to z vertical grid grdz
    """

    var = var.copy()

    assert len(var.shape) == 3, 'var must be 3D'

    if mode == 'linear':
        imode = 0
    elif mode == 'spline':
        imode = 1
    else:
        imode = 0
        raise Warning('%s not supported, defaulting to linear' % mode)

    if Cpos is 'rho':
        z = grd.vgrid.z_r[0, :]
        depth = grdz.vgrid.z
        mask = grd.hgrid.mask_rho
    elif Cpos is 'u':
        z = 0.5 * (grd.vgrid.z_r[0, :, :, :-1] + grd.vgrid.z_r[0, :, :, 1:])
        depth = 0.5 * (grdz.vgrid.z[:, :, :-1] + grdz.vgrid.z[:, :, 1:])
        mask = grd.hgrid.mask_u
    elif Cpos is 'v':
        z = 0.5 * (grd.vgrid.z_r[0, :, :-1, :] + grd.vgrid.z_r[0, :, 1:, :])
        depth = 0.5 * (grdz.vgrid.z[:, :-1, :] + grdz.vgrid.z[:, 1:, :])
        mask = grd.hgrid.mask_v
    elif Cpos is 'w':
        z = grd.vgrid.z_w[0, :]
        depth = grdz.vgrid.z
        mask = grd.hgrid.mask_rho
    else:
        raise Warning('%s unknown position. Cpos must be rho, u, v or w.' %
                      Cpos)

    Nm, Mm, Lm = var.shape
    nlev = grdz.vgrid.N

    var = np.concatenate((var, var[-2:-1, :, :]), 0)
    z = np.concatenate((z, 100 * np.ones((1, z.shape[1], z.shape[2]))), 0)

    if irange is None:
        irange = (0, Lm)
    else:
        assert var.shape[2] == irange[1]-irange[0], \
               'var shape and irange must agree'

    if jrange is None:
        jrange = (0, Mm)
    else:
        assert var.shape[1] == jrange[1]-jrange[0], \
               'var shape and jrange must agree'

    varz = np.zeros((nlev, jrange[1] - jrange[0], irange[1] - irange[0]))

    for k in range(nlev):
        varz[k,:,:] = _interp.xhslice(var, \
                        z[:,jrange[0]:jrange[1], irange[0]:irange[1]], \
                        depth[k,jrange[0]:jrange[1], irange[0]:irange[1]], \
                        mask[jrange[0]:jrange[1], irange[0]:irange[1]], \
                        imode, spval)

    #mask
    idx = np.where(abs((varz - spval) / spval) <= 1e-5)
    varz[idx] = spval
    #varz = np.ma.masked_values(varz, spval, rtol=1e-5)

    return varz
Ejemplo n.º 2
0
def z2roms(varz, grdz, grd, Cpos='rho', irange=None, jrange=None, \
           spval=1e37, flood=True, dmax=0, cdepth=0, kk=0, \
           mode='linear'):
    """
    var = z2roms(var, grdz, grd)

    optional switch:
      - Cpos='rho', 'u' or 'v'	     specify the C-grid position where 
				     the variable rely
      - irange                       specify grid sub-sample for i direction
      - jrange                       specify grid sub-sample for j direction
      - spval=1e37                   define spval value
      - dmax=0                       if dmax>0, maximum horizontal 
                                     flooding distance
      - cdepth=0                     critical depth for flooding
                                     if depth<cdepth => no flooding
      - kk
      - mode='linear' or 'spline'    specify the type of interpolation

    Interpolate the variable from z vertical grid grdz to ROMS grid grd
    """

    varz = varz.copy()

    assert len(varz.shape) == 3, 'var must be 3D'

    if mode=='linear':
        imode=0
    elif mode=='spline':
        imode=1
    else:
        raise Warning, '%s not supported, defaulting to linear' % mode

    if Cpos is 'rho':
        z = grdz.vgrid.z[:]
        depth = grd.vgrid.z_r[0,:]
        mask = grd.hgrid.mask_rho
    elif Cpos is 'u':
        z = 0.5 * (grdz.vgrid.z[:,:,:-1] + grdz.vgrid.z[:,:,1:])
        depth = 0.5 * (grd.vgrid.z_r[0,:,:,:-1] + grd.vgrid.z_r[0,:,:,1:])
        mask = grd.hgrid.mask_u
    elif Cpos is 'v':
        z = 0.5 * (grdz.vgrid.z[:,:-1,:] + grdz.vgrid.z[:,1:,:])
        depth = 0.5 * (grd.vgrid.z_r[0,:,:-1,:] + grd.vgrid.z_r[0,:,1:,:])
        mask = grd.hgrid.mask_v
    elif Cpos is 'w':
        z = grdz.vgrid.z[:]
        depth = grd.vgrid.z_w[0,:]
        mask = grd.hgrid.mask_rho
    else:
        raise Warning, '%s bad position. Use depth at Arakawa-C \
                             rho points instead.' % Cpos

    nlev, Mm, Lm = varz.shape
    Nm = depth.shape[0]

    if irange is None:
        irange = (0,Lm)
    else:
        assert varz.shape[2] == irange[1]-irange[0], \
               'var shape and irange must agreed'

    if jrange is None:
        jrange = (0,Mm)
    else:
        assert varz.shape[1] == jrange[1]-jrange[0], \
               'var shape and jrange must agreed'

    # flood varz if requested
    if flood is True:
        varz = pyroms.remapping.flood(varz, grdz, Cpos=Cpos, \
                 irange=irange, jrange=jrange, spval=spval, \
                 dmax=dmax, cdepth=cdepth, kk=kk)

    varz = np.concatenate((varz[0:1,:,:], varz, varz[-1:,:,:]), 0)
    z = np.concatenate((-9999*np.ones((1,z.shape[1], z.shape[2])), \
           z, \
           100*np.ones((1,z.shape[1], z.shape[2]))), 0)
    
    var = np.ma.zeros((Nm, Mm, Lm))

    for k in range(Nm):
        var[k,:,:] = _interp.xhslice(varz, \
                      z[:,jrange[0]:jrange[1], irange[0]:irange[1]], \
                      depth[k,jrange[0]:jrange[1], irange[0]:irange[1]], \
                      mask[jrange[0]:jrange[1], irange[0]:irange[1]], \
                      imode, spval)
        #mask
        var = np.ma.masked_values(var, spval, rtol=1e-5)
        #var[k,:,:] = np.ma.masked_where(mask == 0, var[k,:,:])
    
    return var
Ejemplo n.º 3
0
def roms2z(var, grd, grdz, Cpos='rho', irange=None, jrange=None, \
           spval=1e37, mode='linear'):
    """
    varz = roms2z(var, grd, grdz)

    optional switch:
      - Cpos='rho', 'u' 'v' or 'w'   specify the C-grid position where 
				     the variable rely
      - irange                       specify grid sub-sample for i direction
      - jrange                       specify grid sub-sample for j direction
      - spval=1e37                   define spval value
      - mode='linear' or 'spline'    specify the type of interpolation

    Interpolate the variable from ROMS grid grd to z vertical grid grdz
    """

    var = var.copy()

    assert len(var.shape) == 3, 'var must be 3D'

    if mode=='linear':
        imode=0
    elif mode=='spline':
        imode=1
    else:
        imode=0
        raise Warning, '%s not supported, defaulting to linear' % mode


    if Cpos is 'rho':
        z = grd.vgrid.z_r[0,:]
        depth = grdz.vgrid.z
        mask = grd.hgrid.mask_rho
    elif Cpos is 'u':
        z = 0.5 * (grd.vgrid.z_r[0,:,:,:-1] + grd.vgrid.z_r[0,:,:,1:])
        depth = 0.5 * (grdz.vgrid.z[:,:,:-1] + grdz.vgrid.z[:,:,1:])
        mask = grd.hgrid.mask_u
    elif Cpos is 'v':
        z = 0.5 * (grd.vgrid.z_r[0,:,:-1,:] + grd.vgrid.z_r[0,:,1:,:])
        depth = 0.5 * (grdz.vgrid.z[:,:-1,:] + grdz.vgrid.z[:,1:,:])
        mask = grd.hgrid.mask_v
    elif Cpos is 'w':
        z = grd.vgrid.z_w[0,:]
        depth = grdz.vgrid.z
        mask = grd.hgrid.mask_rho
    else:
        raise Warning, '%s unknown position. Cpos must be rho, u, v or w.' % Cpos

    Nm, Mm, Lm = var.shape
    nlev = grdz.vgrid.N

    var = np.concatenate((var, var[-2:-1,:,:]), 0)
    z = np.concatenate((z, 100*np.ones((1,z.shape[1], z.shape[2]))), 0)

    if irange is None:
        irange = (0,Lm)
    else:
        assert var.shape[2] == irange[1]-irange[0], \
               'var shape and irange must agreed'

    if jrange is None:
        jrange = (0,Mm)
    else:
        assert var.shape[1] == jrange[1]-jrange[0], \
               'var shape and jrange must agreed'

    varz = np.zeros((nlev, jrange[1]-jrange[0], irange[1]-irange[0]))

    for k in range(nlev):
        varz[k,:,:] = _interp.xhslice(var, \
                        z[:,jrange[0]:jrange[1], irange[0]:irange[1]], \
                        depth[k,jrange[0]:jrange[1], irange[0]:irange[1]], \
                        mask[jrange[0]:jrange[1], irange[0]:irange[1]], \
                        imode, spval)

    #mask
    idx = np.where(abs((varz-spval)/spval)<=1e-5)
    varz[idx] = spval
    #varz = np.ma.masked_values(varz, spval, rtol=1e-5)
    
    return varz
Ejemplo n.º 4
0
def sta2z(var, grd, grdz, Cpos='rho', srange=None, \
           spval=1e37, mode='linear'):
    """
    varz = roms2z(var, grd, grdz)

    optional switch:
      - Cpos                         specify vertical grid type
      - srange                       specify grid sub-sample of stations
      - spval=1e37                   define spval value
      - mode='linear' or 'spline'    specify the type of interpolation

    Interpolate the variable from stations grid grd to z vertical grid grdz
    """

    var = var.copy()

    assert len(var.shape) == 2, 'var must be 2D'

    if mode == 'linear':
        imode = 0
    elif mode == 'spline':
        imode = 1
    else:
        imode = 0
        raise Warning, '%s not supported, defaulting to linear' % mode

    if Cpos is 'rho':
        z = grd.vgrid.z_r[0, :]
        depth = grdz.vgrid.z
    elif Cpos is 'w':
        z = grd.vgrid.z_w[0, :]
        depth = grdz.vgrid.z
    else:
        raise Warning, '%s unknown position. Cpos must be rho or w.' % Cpos

    var = var.T
    Nm, Sm = var.shape
    nlev = grdz.vgrid.N

    # putting in a fake i dimension
    var = np.dstack(var).T
    z = np.dstack(z).T
    depth = np.dstack(depth).T
    mask = np.ones((Sm, 1))

    # copy surface value to high in the sky
    var = np.concatenate((var, var[-2:-1, :, :]), 0)
    z = np.concatenate((z, 100 * np.ones((1, z.shape[1], z.shape[2]))), 0)
    #    print 'nlev', nlev, 'var.shape', var.shape, srange
    #    print 'z.shape', z.shape
    #    print 'mask.shape', mask.shape
    #    print 'depth.shape', depth.shape

    if srange is None:
        srange = (0, Sm)
    else:
        assert var.shape[1] == srange[1]-srange[0], \
               'var shape and srange must agree'

    varz = np.zeros((nlev, srange[1] - srange[0], 1))

    for k in range(nlev):
        varz[k,:] = _interp.xhslice(var, \
                        z[:, srange[0]:srange[1], :], \
                        depth[k, srange[0]:srange[1], :], \
                        mask[srange[0]:srange[1], :], \
                        imode, spval)

#    pdb.set_trace()
#mask
    idx = np.where(abs((varz - spval) / spval) <= 1e-5)
    varz[idx] = spval
    #varz = np.ma.masked_values(varz, spval, rtol=1e-5)

    return varz
Ejemplo n.º 5
0
Archivo: sta2z.py Proyecto: ESMG/pyroms
def sta2z(var, grd, grdz, Cpos='rho', srange=None, \
           spval=1e37, mode='linear'):
    """
    varz = roms2z(var, grd, grdz)

    optional switch:
      - Cpos                         specify vertical grid type
      - srange                       specify grid sub-sample of stations
      - spval=1e37                   define spval value
      - mode='linear' or 'spline'    specify the type of interpolation

    Interpolate the variable from stations grid grd to z vertical grid grdz
    """

    var = var.copy()

    assert len(var.shape) == 2, 'var must be 2D'

    if mode=='linear':
        imode=0
    elif mode=='spline':
        imode=1
    else:
        imode=0
        raise Warning, '%s not supported, defaulting to linear' % mode

    if Cpos is 'rho':
        z = grd.vgrid.z_r[0,:]
        depth = grdz.vgrid.z
    elif Cpos is 'w':
        z = grd.vgrid.z_w[0,:]
        depth = grdz.vgrid.z
    else:
        raise Warning, '%s unknown position. Cpos must be rho or w.' % Cpos

    var = var.T
    Nm, Sm = var.shape
    nlev = grdz.vgrid.N

    # putting in a fake i dimension
    var = np.dstack(var).T
    z = np.dstack(z).T
    depth = np.dstack(depth).T
    mask = np.ones((Sm, 1))

    # copy surface value to high in the sky
    var = np.concatenate((var, var[-2:-1,:,:]), 0)
    z = np.concatenate((z, 100*np.ones((1,z.shape[1], z.shape[2]))), 0)
#    print 'nlev', nlev, 'var.shape', var.shape, srange
#    print 'z.shape', z.shape
#    print 'mask.shape', mask.shape
#    print 'depth.shape', depth.shape

    if srange is None:
        srange = (0,Sm)
    else:
        assert var.shape[1] == srange[1]-srange[0], \
               'var shape and srange must agree'

    varz = np.zeros((nlev, srange[1]-srange[0], 1))

    for k in range(nlev):
        varz[k,:] = _interp.xhslice(var, \
                        z[:, srange[0]:srange[1], :], \
                        depth[k, srange[0]:srange[1], :], \
                        mask[srange[0]:srange[1], :], \
                        imode, spval)

#    pdb.set_trace()
    #mask
    idx = np.where(abs((varz-spval)/spval)<=1e-5)
    varz[idx] = spval
    #varz = np.ma.masked_values(varz, spval, rtol=1e-5)
    
    return varz
Ejemplo n.º 6
0
def z2roms(varz, grdz, grd, Cpos='rho', irange=None, jrange=None, \
           spval=1e37, flood=True, dmax=0, cdepth=0, kk=0, \
           mode='linear'):
    """
    var = z2roms(var, grdz, grd)

    optional switch:
      - Cpos='rho', 'u' or 'v'	     specify the C-grid position where 
				     the variable rely
      - irange                       specify grid sub-sample for i direction
      - jrange                       specify grid sub-sample for j direction
      - spval=1e37                   define spval value
      - dmax=0                       if dmax>0, maximum horizontal 
                                     flooding distance
      - cdepth=0                     critical depth for flooding
                                     if depth<cdepth => no flooding
      - kk
      - mode='linear' or 'spline'    specify the type of interpolation

    Interpolate the variable from z vertical grid grdz to ROMS grid grd
    """

    varz = varz.copy()

    assert len(varz.shape) == 3, 'var must be 3D'

    if mode=='linear':
        imode=0
    elif mode=='spline':
        imode=1
    else:
        raise Warning, '%s not supported, defaulting to linear' % mode

    if Cpos is 'rho':
        z = grdz.vgrid.z[:]
        depth = grd.vgrid.z_r[0,:]
        mask = grd.hgrid.mask_rho
    elif Cpos is 'u':
        z = 0.5 * (grdz.vgrid.z[:,:,:-1] + grdz.vgrid.z[:,:,1:])
        depth = 0.5 * (grd.vgrid.z_r[0,:,:,:-1] + grd.vgrid.z_r[0,:,:,1:])
        mask = grd.hgrid.mask_u
    elif Cpos is 'v':
        z = 0.5 * (grdz.vgrid.z[:,:-1,:] + grdz.vgrid.z[:,1:,:])
        depth = 0.5 * (grd.vgrid.z_r[0,:,:-1,:] + grd.vgrid.z_r[0,:,1:,:])
        mask = grd.hgrid.mask_v
    elif Cpos is 'w':
        z = grdz.vgrid.z[:]
        depth = grd.vgrid.z_w[0,:]
        mask = grd.hgrid.mask_rho
    else:
        raise Warning, '%s bad position. Use depth at Arakawa-C \
                             rho points instead.' % Cpos

    nlev, Mm, Lm = varz.shape
    Nm = depth.shape[0]

    if irange is None:
        irange = (0,Lm)
    else:
        assert varz.shape[2] == irange[1]-irange[0], \
               'var shape and irange must agree'

    if jrange is None:
        jrange = (0,Mm)
    else:
        assert varz.shape[1] == jrange[1]-jrange[0], \
               'var shape and jrange must agree'

    # flood varz if requested
    if flood is True:
        varz = pyroms.remapping.flood(varz, grdz, Cpos=Cpos, \
                 irange=irange, jrange=jrange, spval=spval, \
                 dmax=dmax, cdepth=cdepth, kk=kk)

    varz = np.concatenate((varz[0:1,:,:], varz, varz[-1:,:,:]), 0)
    z = np.concatenate((-9999*np.ones((1,z.shape[1], z.shape[2])), \
           z, \
           100*np.ones((1,z.shape[1], z.shape[2]))), 0)
    
    var = np.ma.zeros((Nm, Mm, Lm))

    for k in range(Nm):
        var[k,:,:] = _interp.xhslice(varz, \
                      z[:,jrange[0]:jrange[1], irange[0]:irange[1]], \
                      depth[k,jrange[0]:jrange[1], irange[0]:irange[1]], \
                      mask[jrange[0]:jrange[1], irange[0]:irange[1]], \
                      imode, spval)
        #mask
        var = np.ma.masked_values(var, spval, rtol=1e-5)
        #var[k,:,:] = np.ma.masked_where(mask == 0, var[k,:,:])
    
    return var