Example #1
0
def transport_divergence(ds,mask,varn='salt',kza=0,kzo=None,t=0):
    _warn_virtual_salt_flux_units()
    if varn == 'heat':
        uvar,vvar = 'UET','VNT'
    elif varn == 'salt':
        uvar,vvar = 'UES','VNS'
    dsvar = ds.variables
    dxu = dsvar['DXU'][:] * 1e-2
    dyu = dsvar['DYU'][:] * 1e-2
    tarea = dsvar['TAREA'][:] * 1e-4
    dz = dsvar['dz'][:] * 1e-2    
    if kzo is None: kzo = len(dz)
    transport_divergence = 0.
    for k in xrange(kza,kzo):
        uflux = _fill0(dsvar[uvar][t,k])
        uflux *= dyu
        uflux *= dz[k]
        uflux *= mask
        vflux = _fill0(dsvar[vvar][t,k])
        vflux *= dxu
        vflux *= dz[k]
        vflux *= mask
        divergence = central_differences(uflux,dxu,axis=1) + central_differences(vflux,dyu,axis=0)
        divergence *= mask
        transport_divergence += np.sum(divergence*tarea)
        if varn=='heat': warnings.warn('Units might be wrong for heat transport! Check!')
    return transport_divergence
Example #2
0
def transport_divergence(ds,mask,varn='salt',kza=0,kzo=None,t=0):
    _warn_virtual_salt_flux_units()
    if varn == 'heat':
        uvar,vvar = 'UET','VNT'
    elif varn == 'salt':
        uvar,vvar = 'UES','VNS'
    dsvar = ds.variables
    dxu = dsvar['DXU'][:] * 1e-2
    dyu = dsvar['DYU'][:] * 1e-2
    tarea = dsvar['TAREA'][:] * 1e-4
    dz = dsvar['dz'][:] * 1e-2    
    if kzo is None: kzo = len(dz)
    transport_divergence = 0.
    for k in range(kza,kzo):
        uflux = _fill0(dsvar[uvar][t,k])
        uflux *= dyu
        uflux *= dz[k]
        uflux *= mask
        vflux = _fill0(dsvar[vvar][t,k])
        vflux *= dxu
        vflux *= dz[k]
        vflux *= mask
        divergence = central_differences(uflux,dxu,axis=1) + central_differences(vflux,dyu,axis=0)
        divergence *= mask
        transport_divergence += np.sum(divergence*tarea)
        if varn=='heat': warnings.warn('Units might be wrong for heat transport! Check!')
    return transport_divergence
def diffusion_component(ds,varn,regmask=1,kza=0,kzo=None,S0=34.8):
    """Temperature/Salt diffusion"""
    dsvar = ds.variables
    dxt = dsvar['DXT'][:]/100.
    dyt = dsvar['DYT'][:]/100.
    dz = dsvar['dz'][:]/100.
    if kzo is None: kzo = len(dz)
    diffusion = np.zeros(regmask.shape[0])
    for k in xrange(kza,kzo):
        layer = _fill0(dsvar['KAPPA_ISOP'][0,k]/1e4) # m2 s-1
        if varn == 'heat':
            scalar = _fill0(dsvar['TEMP'][0,k])
        elif varn == 'salt':
            scalar = _fill0(dsvar['SALT'][0,k])
        elif varn == 'freshwater':
            scalar = (S0 - _fill0(dsvar['SALT'][0,k])) / S0
        gradient = central_differences(scalar,dyt,axis=0) # [scalar] m s-1
        #gradient = np.zeros(scalar.shape)
        #gradient[1:,:] = np.diff(scalar,axis=0)
        #gradient[0] = gradient[1]
        #gradient /= dyt
        layer *= gradient
        layer *= dz[k]
        layer *= dxt
        layer *= regmask
        diffusion += np.sum(layer,axis=-1)
        diffusion *= -1.
    if varn == 'heat':
        diffusion *= (1e3 * 4e3 * 1e-15) # PW
    elif varn == 'salt':
        diffusion *= 1e-6 # Sv PPT
    return diffusion
def diffusion_component(ds, varn, regmask=1, kza=0, kzo=None, S0=34.8):
    """Temperature/Salt diffusion"""
    dsvar = ds.variables
    dxt = dsvar['DXT'][:] / 100.
    dyt = dsvar['DYT'][:] / 100.
    dz = dsvar['dz'][:] / 100.
    if kzo is None: kzo = len(dz)
    diffusion = np.zeros(regmask.shape[0])
    for k in range(kza, kzo):
        layer = _fill0(dsvar['KAPPA_ISOP'][0, k] / 1e4)  # m2 s-1
        if varn == 'heat':
            scalar = _fill0(dsvar['TEMP'][0, k])
        elif varn == 'salt':
            scalar = _fill0(dsvar['SALT'][0, k])
        elif varn == 'freshwater':
            scalar = (S0 - _fill0(dsvar['SALT'][0, k])) / S0
        gradient = central_differences(scalar, dyt, axis=0)  # [scalar] m s-1
        #gradient = np.zeros(scalar.shape)
        #gradient[1:,:] = np.diff(scalar,axis=0)
        #gradient[0] = gradient[1]
        #gradient /= dyt
        layer *= gradient
        layer *= dz[k]
        layer *= dxt
        layer *= regmask
        diffusion += np.sum(layer, axis=-1)
        diffusion *= -1.
    if varn == 'heat':
        diffusion *= (1e3 * 4e3 * 1e-15)  # PW
    elif varn == 'salt':
        diffusion *= 1e-6  # Sv PPT
    return diffusion
Example #5
0
def fluxbudget_diffusion(ds,mask,varn,kza=0,kzo=None,S0=34.8,t=0):
    """Compute flux of `varn` into region `mask` due to diffusion"""
    _warn_virtual_salt_flux_units()
    dsvar = ds.variables
    dxt = dsvar['DXT'][:] * 1e-2
    dyt = dsvar['DYT'][:] * 1e-2
    dz = dsvar['dz'][:] * 1e-2
    if kzo is None: kzo = len(dz)
    fluxbudget = 0.
    for k in xrange(kza,kzo):
        # get scalar data
        if varn == 'heat':
            scalar = _fill0(dsvar['TEMP'][t,k])
        elif varn == 'salt':
            scalar = _fill0(dsvar['SALT'][t,k])
        elif varn == 'freshwater':
            scalar = (S0 - _fill0(dsvar['SALT'][t,k])) / S0
        # get gradient
        uflux = central_differences(scalar,dxt,axis=1) # [scalar] m-1
        vflux = central_differences(scalar,dyt,axis=0) # [scalar] m-1
        # multiply gradient by diffusion coefficient
        kappa = _fill0(dsvar['KAPPA_ISOP'][t,k] * 1e-4) # m2 s-1
        uflux *= kappa
        vflux *= kappa
        # multiply by horizontal grid spacing
        uflux *= dyt
        vflux *= dxt
        # multiply by vertical grid spacing
        uflux *= dz[k]
        vflux *= dz[k]
        # compute budget
        fluxbudget += budget_over_region_2D(uflux,vflux,scalar=None,mask=mask)
    # convert to right units
    if varn == 'heat':
        fluxbudget *= (1e3 * 4e3 * 1e-15) # PW
    return fluxbudget
Example #6
0
def fluxbudget_diffusion(ds,mask,varn,kza=0,kzo=None,S0=34.8,t=0):
    """Compute flux of `varn` into region `mask` due to diffusion"""
    _warn_virtual_salt_flux_units()
    dsvar = ds.variables
    dxt = dsvar['DXT'][:] * 1e-2
    dyt = dsvar['DYT'][:] * 1e-2
    dz = dsvar['dz'][:] * 1e-2
    if kzo is None: kzo = len(dz)
    fluxbudget = 0.
    for k in range(kza,kzo):
        # get scalar data
        if varn == 'heat':
            scalar = _fill0(dsvar['TEMP'][t,k])
        elif varn == 'salt':
            scalar = _fill0(dsvar['SALT'][t,k])
        elif varn == 'freshwater':
            scalar = (S0 - _fill0(dsvar['SALT'][t,k])) / S0
        # get gradient
        uflux = central_differences(scalar,dxt,axis=1) # [scalar] m-1
        vflux = central_differences(scalar,dyt,axis=0) # [scalar] m-1
        # multiply gradient by diffusion coefficient
        kappa = _fill0(dsvar['KAPPA_ISOP'][t,k] * 1e-4) # m2 s-1
        uflux *= kappa
        vflux *= kappa
        # multiply by horizontal grid spacing
        uflux *= dyt
        vflux *= dxt
        # multiply by vertical grid spacing
        uflux *= dz[k]
        vflux *= dz[k]
        # compute budget
        fluxbudget += budget_over_region_2D(uflux,vflux,scalar=None,mask=mask)
    # convert to right units
    if varn == 'heat':
        fluxbudget *= (1e3 * 4e3 * 1e-15) # PW
    return fluxbudget
Example #7
0
 def test_central_differences(self):
     a = np.arange(100, dtype=float)
     dx = np.ones(100)*2
     result = stats.central_differences(a, dx)
     expected = np.ones(100)*0.5
     self.assertTrue(np.allclose(result, expected))