Example #1
0
def pv(dgl):
    """pv(dgl) requires u,v and b to be loaded already"""
    ylen, xlen, zlen, tlen = mit.dgl_dims(dgl)
    fld_exists = mit.check_field(dgl,'pvz')
    if not fld_exists:
        dgl = mit.add_field(dgl,'pvz')
        zlen = len(dgl[0]['z'])
        dgl[0]['pvz'] = np.array(0.5*(dgl[0]['z'][0:zlen-1] + dgl[0]['z'][1:zlen]))

    pv_exists = mit.check_field(dgl,'pv')
    if not pv_exists:
        dgl = mit.add_field(dgl,'pv')
    for ti in np.arange(0,tlen):
        """Loop in time to reduce memory requirement"""
        if len(dgl[0]['x']) > 1:
            p1 =  pv1_calc(dgl[0]['v'][:,:,:,ti],
            dgl[0]['b'][:,:,:,ti],dgl[0]['x'],dgl[0]['z'])
        else:
            p1 = np.zeros(np.shape(dgl[0]['pv'][:,:,:,0]))

        if len(dgl[0]['y']) > 1:
            p2 =  pv2_calc(dgl[0]['u'][:,:,:,ti],
            dgl[0]['b'][:,:,:,ti],dgl[0]['y'],dgl[0]['z'])
        else:
            p2 = np.zeros(np.shape(ylen,xlen,zlen-1))
        if len(dgl[0]['z']) > 1:
            p3 =  pv3_calc(dgl[0]['f'],dgl[0]['u'][:,:,:,ti],
            dgl[0]['v'][:,:,:,ti],dgl[0]['b'][:,:,:,ti],dgl[0]['x'],dgl[0]['y'],dgl[0]['z'])
        else:
            p3 = np.zeros(np.shape(ylen,xlen,zlen-1))
        dgl[0]['pv'][:,:,:,ti] = p1 + p2 + p3

    return dgl
Example #2
0
def pv2(dgl):
    """Calculate pv2 and add it to dgl"""
    fld_exists = mit.check_field(dgl,'pvz')
    if not fld_exists:
        dgl = mit.add_field(dgl,'pvz')
        zlen = len(dgl[0]['z'])
        dgl[0]['pvz'] = 0.5*(dgl[0]['z'][0:zlen-1] + dgl[0]['z'][1:zlen])

    pv2_exists = mit.check_field(dgl,'pv2')
    w_exists = mit.check_field(dgl,'w')

    if not pv2_exists:

        dgl = mit.add_field(dgl,'pv2')
    ylen, xlen, zlen, tlen = mit.dgl_dims(dgl)
    for ti in np.arange(0,tlen):
        """Loop in time to reduce memory requirement"""
        if ylen > 1:
            if not w_exists:
                p2 =  pv2_calc(dgl[0]['u'][:,:,:,ti],
                dgl[0]['b'][:,:,:,ti],dgl[0]['y'],dgl[0]['z'])
            else:
                p2 =  pv2_calc(dgl[0]['u'][:,:,:,ti],
                dgl[0]['b'][:,:,:,ti],dgl[0]['y'],dgl[0]['z'],dgl[0]['w'][:,:,:,ti])
        else:
            p2 = np.zeros(np.shape(dgl[0]['pv2'][:,:,:,0]))

        dgl[0]['pv2'][:,:,:,ti] = p2
    return dgl
Example #3
0
def buoy_dgl(dgl,
             eos,
             alpha=None,
             sbeta=None,
             g=None,
             lat=None,
             Tref=None,
             Sref=None):
    """Calculate the buoyancy field for a structured array dgl"""
    import mit
    b_exists = mit.check_field(dgl, 'b')
    if not b_exists:
        dgl = mit.add_field(dgl, 'b')
    S_exists = mit.check_field(dgl, 's')
    T_exists = mit.check_field(dgl, 't')
    if eos == 'lin':
        alpha, sbeta, g, Tref, Sref = lin_eos_params(alpha, sbeta, g, lat,
                                                     Tref, Sref)
        for rw in np.arange(0, len(dgl[0]['y'])):
            if T_exists and not S_exists:
                """Thermal contribution only"""
                dgl[0]['b'][rw, :, :, :] = g * alpha * (
                    dgl[0]['t'][rw, :, :, :] - Tref)
            if S_exists and not T_exists:
                """Haline contribution only"""
                dgl[0]['b'][rw, :, :, :] = -g * sbeta * (
                    dgl[0]['s'][rw, :, :, :] - Sref)
            if T_exists and S_exists:
                dgl[0]['b'][rw, :, :, :] = g * alpha * (
                    dgl[0]['t'][rw, :, :, :] - Tref)
                -g * sbeta * (dgl[0]['s'][rw, :, :, :] - Sref)

    elif eos == 'jmd':
        g = 9.81
        import jmd95
        import seawater
        import mit
        if not T_exists:
            T = np.zeros(np.shape(dgl[0]['s']))
        if not S_exists:
            S = np.zeros(np.shape(dgl[0]['t']))
        rhoConst = 1000
        ylen, xlen, zlen, tlen = mit.dgl_dims(dgl)
        #Pressure needed in decibars
        press = seawater.pres(-dgl[0]['z'], lat=45)
        buoy_fac = g / rhoConst
        #Loop this routine along t and y to avoid running out of memory
        for ti in np.arange(0, tlen):
            for rw in np.arange(0, ylen):
                dgl[0]['b'][rw, :, :, ti] = -buoy_fac * (jmd95.densjmd95(
                    np.squeeze(dgl[0]['s'][rw, :, :, ti]),
                    np.squeeze(dgl[0]['t'][rw, :, :, ti]), press) - 1030)
    return dgl
Example #4
0
def n2(dgl):
    """Calculate n2 for stand-alone calculations rather than as part of PV.
    N2 is at w-points. This function adds n2 (and pvz if needed) to the dgl output array"""
    ylen, xlen, zlen, tlen = mit.dgl_dims(dgl)
    fld_exists = mit.check_field(dgl,'pvz')
    if not fld_exists:
        dgl = mit.add_field(dgl,'pvz')
        zlen = len(dgl[0]['z'])
        dgl[0]['pvz'] = 0.5*(dgl[0]['z'][0:zlen-1] + dgl[0]['z'][1:zlen])

    fld_exists = mit.check_field(dgl,'n2')
    if not fld_exists:
        dgl = mit.add_field(dgl,'n2')
    for ti in np.arange(0,tlen):
        dgl[0]['n2'][:,:,:,ti]= n2_calc(dgl[0]['b'][:,:,:,ti],dgl[0]['z'])
    return dgl
Example #5
0
def rv(dgl, normf = False):
    """Calculate the vertical component of relative vorticity for
    stand-alone calculations rather than as part of PV.
    RV is at the lower-left of the first cell at the same depth as u and v points
    This function adds rv to the dgl output array"""
    #Check if rv is already a field of dgl
    fld_exists = mit.check_field(dgl,'rv')
    #Add rv as a field if it isn't already
    if not fld_exists:
        dgl = mit.add_field(dgl,'rv')

    ylen, xlen, zlen, tlen = mit.dgl_dims(dgl)
    if xlen>1:
        dx = 1e3*(dgl[0]['x'][1] - dgl[0]['x'][0])
    else:
        dx = []
    if ylen>1:
        dy = 1e3*(dgl[0]['y'][1] - dgl[0]['y'][0])
    else:
        dy = []
    for ti in np.arange(0,tlen):
        dgl[0]['rv'][:,:,:,ti] = rv_calc(dgl[0]['u'][:,:,:,ti],
        dgl[0]['v'][:,:,:,ti],dx,dy)
    if normf is True:
        dgl[0]['rv'] = dgl[0]['rv']/dgl[0]['f']
    return dgl
Example #6
0
def rib(dgl):
    """Caclulate the balanced Richardson number and add to dgl"""
    ylen, xlen, zlen, tlen = mit.dgl_dims(dgl)
    fld_exists = mit.check_field(dgl,'pvz')
    if not fld_exists:
        dgl = mit.add_field(dgl,'pvz')
        zlen = len(dgl[0]['z'])
        dgl[0]['pvz'] = 0.5*(dgl[0]['z'][0:zlen-1] + dgl[0]['z'][1:zlen])
    fld_exists = mit.check_field(dgl,'rib')
    if not fld_exists:
        dgl = mit.add_field(dgl,'rib')
    print dgl.dtype
    print tlen
    for ti in np.arange(tlen):
        dgl[0]['rib'][:,:,:,ti] = rib_calc(dgl[0]['b'][:,:,:,ti],
        dgl[0]['f'],dgl[0]['x'],dgl[0]['y'],dgl[0]['z'])
    return dgl
Example #7
0
def vbyz(dgl):
    """Calculate the effect on stratification of differential meridional
    buoyancy fluxes"""
    #First ensure that the correct vertical field is present
    fld_exists = mit.check_field(dgl,'pvz')
    if not fld_exists:
        dgl = mit.add_field(dgl,'pvz')
        zlen = len(dgl[0]['z'])
        dgl[0]['pvz'] = 0.5*(dgl[0]['z'][0:zlen-1] + dgl[0]['z'][1:zlen])
    import eosmit   #This routine checks if buoyancy is already loaded before running
    eos = 'jmd'
    dgl = eosmit.buoy_dgl(dgl,eos=eos)
    fld_exists = mit.check_field(dgl,'vbxz')
    if not fld_exists:
        dgl = mit.add_field(dgl,'vbyz')
    dgl[0]['vbyz'] = dgl[0]['v']*mit.ddy(dgl[0]['b'],dgl[0]['y'])

    return dgl
Example #8
0
def buoy_dgl(dgl,eos,alpha = None,sbeta = None, g = None, lat = None,
        Tref = None, Sref = None):
    """Calculate the buoyancy field for a structured array dgl"""
    import mit
    b_exists = mit.check_field(dgl,'b')
    if not b_exists:
        dgl = mit.add_field(dgl,'b')
    S_exists = mit.check_field(dgl,'s')
    T_exists = mit.check_field(dgl,'t')
    if eos == 'lin':
        alpha, sbeta, g, Tref, Sref = lin_eos_params(alpha,sbeta,g,lat,Tref,Sref)
        for rw in np.arange(0,len(dgl[0]['y'])):
            if T_exists and not S_exists:
                """Thermal contribution only"""
                dgl[0]['b'][rw,:,:,:] = g*alpha*(dgl[0]['t'][rw,:,:,:] - Tref)
            if S_exists and not T_exists:
                """Haline contribution only"""
                dgl[0]['b'][rw,:,:,:] = -g*sbeta*(dgl[0]['s'][rw,:,:,:] - Sref)
            if T_exists and S_exists:
                dgl[0]['b'][rw,:,:,:] = g*alpha*(dgl[0]['t'][rw,:,:,:] - Tref)
                -g*sbeta*(dgl[0]['s'][rw,:,:,:] - Sref)

    elif eos == 'jmd':
        g = 9.81
        import jmd95
        import seawater
        import mit
        if not T_exists:
            T = np.zeros(np.shape(dgl[0]['s']))
        if not S_exists:
            S = np.zeros(np.shape(dgl[0]['t']))
        rhoConst = 1000
        ylen, xlen, zlen, tlen = mit.dgl_dims(dgl)
        #Pressure needed in decibars
        press = seawater.pres(-dgl[0]['z'],lat = 45)
        buoy_fac = g/rhoConst
        #Loop this routine along t and y to avoid running out of memory
        for ti in np.arange(0,tlen):
            for rw in np.arange(0,ylen):
                dgl[0]['b'][rw,:,:,ti] =-buoy_fac*(jmd95.densjmd95(
                np.squeeze(dgl[0]['s'][rw,:,:,ti]),
                np.squeeze(dgl[0]['t'][rw,:,:,ti]),press) - 1030)
    return dgl