def setUp(self):
        kw = dict(comments='#', skiprows=6, delimiter=',')
        station_61 = 'Endeavor_Cruise-88_Station-61.csv'
        station_64 = 'Endeavor_Cruise-88_Station-64.csv'
        st61 = np.loadtxt(os.path.join(rootpath, 'data', station_61), **kw)
        st64 = np.loadtxt(os.path.join(rootpath, 'data', station_64), **kw)

        latst = 36. + 40.03 / 60., 37. + 39.93 / 60.
        lonst = -(70. + 59.59 / 60.), -71.
        Sal = np.c_[st61[:, 2], st64[:, 2]]
        Temp = np.c_[st61[:, 1], st64[:, 1]]
        Pres = np.c_[st61[:, 0], st61[:, 0]]
        Gpan = sw.gpan(Sal, Temp, Pres)

        self.values = dict(r=np.array([56.4125, 56.3161, 50.6703, 38.1345,
                                       35.0565, 32.9865]) / c3515,
                           s=np.array([34.5487, 34.7275, 34.8605, 34.6810,
                                       34.5680, 34.5600]),
                           t=np.array([28.7856, 28.4329, 22.8103, 10.2600,
                                       6.8863, 4.4036]),
                           p=np.array([10., 50., 125., 250., 600., 1000.]),
                           pref=np.array([0.0]),
                           pt=np.array([28.8099, 28.4392, 22.7862, 10.2262,
                                        6.8272, 4.3236]),
                           rtx=np.array([0.99353194]),
                           delt=np.array([13.7856]),
                           rt=np.array([1.32968079, 1.32094651, 1.18368907,
                                        0.89332541, 0.81977076, 0.76703445]),
                           units='km',
                           lon=np.array([-30.] * 6),
                           lat=np.linspace(-22., -21., 6.),
                           length=np.array([100.] * 6),
                           latst=latst, lonst=lonst, Sal=Sal, Temp=Temp,
                           Pres=Pres, Gpan=Gpan
                           )
Example #2
0
def calcs(self, S, T, P):
    self['gpan'] = sw.gpan(S, T, P)
    self['pt'] = sw.ptmp(S, T, P)
    self['psigma0'] = sw.pden(S, T, P, 0) - 1000
    self['psigma1'] = sw.pden(S, T, P, 1000) - 1000
    self['psigma2'] = sw.pden(S, T, P, 2000) - 1000

    return self
Example #3
0
    def setUp(self):
        # TODO: More tests with station data.
        kw = dict(comments='#', skiprows=6, delimiter=',')
        station_61 = 'Endeavor_Cruise-88_Station-61.csv'
        station_64 = 'Endeavor_Cruise-88_Station-64.csv'
        st61 = np.loadtxt(os.path.join(rootpath, station_61), **kw)
        st64 = np.loadtxt(os.path.join(rootpath, station_64), **kw)

        latst = 36. + 40.03 / 60., 37. + 39.93 / 60.
        lonst = -(70. + 59.59 / 60.), -71.
        Sal = np.c_[st61[:, 2], st64[:, 2]]
        Temp = np.c_[st61[:, 1], st64[:, 1]]
        Pres = np.c_[st61[:, 0], st61[:, 0]]
        Gpan = sw.gpan(Sal, Temp, Pres)

        self.values = dict(
            r=np.array([56.4125, 56.3161, 50.6703, 38.1345, 35.0565, 32.9865
                        ]) / c3515,
            s=np.array([34.5487, 34.7275, 34.8605, 34.6810, 34.5680, 34.5600]),
            t=np.array([28.7856, 28.4329, 22.8103, 10.2600, 6.8863, 4.4036]),
            p=np.array([10., 50., 125., 250., 600., 1000.]),
            pref=np.array([0.0]),
            pt=np.array([28.8099, 28.4392, 22.7862, 10.2262, 6.8272, 4.3236]),
            rtx=np.array([0.99353194]),
            delt=np.array([13.7856]),
            rt=np.array([
                1.32968079, 1.32094651, 1.18368907, 0.89332541, 0.81977076,
                0.76703445
            ]),
            units='km',
            lon=np.array([-30.] * 6),
            lat=np.linspace(-22., -21., 6.),
            length=np.array([100.] * 6),
            latst=latst,
            lonst=lonst,
            Sal=Sal,
            Temp=Temp,
            Pres=Pres,
            Gpan=Gpan)
 def test_3D(self):
     steric_height_new = sw.gpan(self.s_mean, self.t_mean,
                                 self.depth[..., None, None])
     np.testing.assert_array_almost_equal(self.steric_height,
                                          steric_height_new)
 def test_1D(self):
     steric_height_new = sw.gpan(self.s_mean[:, 0, 0], self.t_mean[:, 0, 0],
                                 self.depth)
     np.testing.assert_array_almost_equal(self.steric_height[:, 0, 0],
                                          steric_height_new)
# Using first dimension from the data-set.
s_mean, pt_mean = s_mean_comp[0], pt_mean_comp[0]


def test_array(arr1, arr2):
    try:
        np.testing.assert_equal(arr1, arr2)
    except AssertionError:
        return False
    return True


if __name__ == '__main__':
    # 1D.
    steric_height_new = sw.gpan(s_mean[:, 0, 0], t_mean[:, 0, 0], depth)
    if test_array(steric_height[:, 0, 0], steric_height_new):
        print("1D sw.gpan test passed.")
    else:
        print("1D sw.gpan test failed.")

    # 2D.
    steric_height_new = sw.gpan(s_mean[..., 1], t_mean[..., 1], depth[...,
                                                                      None])
    if test_array(steric_height[..., 1], steric_height_new):
        print("2D sw.gpan test passed.")
    else:
        print("2D sw.gpan test failed.")

    # 3D.
    steric_height_new = sw.gpan(s_mean, t_mean, depth[..., None, None])
depth = mat.get('depths').astype(float).squeeze()

# Using first dimension from the data-set.
s_mean, pt_mean = s_mean_comp[0], pt_mean_comp[0]


def test_array(arr1, arr2):
    try:
        np.testing.assert_equal(arr1, arr2)
    except AssertionError:
        return False
    return True

if __name__ == '__main__':
    # 1D.
    steric_height_new = sw.gpan(s_mean[:, 0, 0], t_mean[:, 0, 0], depth)
    if test_array(steric_height[:, 0, 0], steric_height_new):
        print("1D sw.gpan test passed.")
    else:
        print("1D sw.gpan test failed.")

    # 2D.
    steric_height_new = sw.gpan(s_mean[..., 1], t_mean[..., 1],
                                depth[..., None])
    if test_array(steric_height[..., 1], steric_height_new):
        print("2D sw.gpan test passed.")
    else:
        print("2D sw.gpan test failed.")

    # 3D.
    steric_height_new = sw.gpan(s_mean, t_mean, depth[..., None, None])
Example #8
0
def ctdproc(lista,
            temp_name='t068C',
            lathint='Latitude =',
            lonhint='Longitude =',
            cond_name='c0S/m',
            press_name='prDM',
            down_cast=True,
            looped=True,
            hann_f=False,
            hann_block=20,
            hann_times=2,
            latline=[],
            lonline=[]):
    '''
    This function do the basic proccess to all .cnv CTD data from
    given list.
    '''
    for fname in lista:

        lon, lat, data = ctdread(fname,
                                 press_name=press_name,
                                 down_cast=down_cast,
                                 lathint=lathint,
                                 lonhint=lonhint,
                                 lonline=lonline,
                                 latline=latline)

        if looped:
            data = loopedit(data)

        dataname = basename(fname)[1]

        if (data.shape[0] < 101) & (
                data.shape[0] >
                10):  # se o tamanho do perfil for com menos de 101 medidas

            if (data.shape[0] /
                    2) % 2 == 0:  # caso a metade dos dados seja par
                blk = (data.shape[0] / 2) + 1  # bloco = a metade +1
            else:
                blk = data.shape[0] / 2  # se for impar o bloco e a metade

            # remove spikes dos perfis de temperatura e condutividade
            data = despike(data, propname=temp_name, block=blk, wgth=2)
            data = despike(data, propname=cond_name, block=blk, wgth=2)
        elif data.shape[0] >= 101:
            # para perfis com mais de 101 medidas, utiliza-se blocos de 101
            data = despike(data, propname=temp_name, block=101, wgth=2)
            data = despike(data, propname=cond_name, block=101, wgth=2)
        else:
            print('radial muito rasa')

        # realiza média em caixa de 1 metro
        data = binning(data, delta=1.)
        if temp_name == 't068C':
            data['t090C'] = gsw.t90_from_t68(data['t068C'])

        data['sp'] = gsw.SP_from_C(data[cond_name] * 10, data['t090C'],
                                   data.index.values)

        if hann_f:
            times = 0
            while times < hann_times:
                data = hann_filter(data, 't090C', hann_block)
                data = hann_filter(data, 'sp', hann_block)
                times += 1

        data['pt'] = sw.ptmp(data['sp'], data['t090C'], data.index.values)
        #data['ct'] = gsw.CT_from_pt(data['sa'],data['pt'])
        data['psigma0'] = sw.pden(
            data['sp'], data['t090C'], data.index.values, pr=0) - 1000
        data['psigma1'] = sw.pden(
            data['sp'], data['t090C'], data.index.values, pr=1000) - 1000
        data['psigma2'] = sw.pden(
            data['sp'], data['t090C'], data.index.values, pr=2000) - 1000
        data['gpan'] = sw.gpan(data['sp'], data['t090C'], data.index.values)
        data['lat'] = lat
        data['lon'] = lon

        data.to_pickle(
            os.path.split(fname)[0] + '/' +
            os.path.splitext(os.path.split(fname)[1])[0])

        print(dataname)
Example #9
0
def makeSteric(salinity,salinityChg,temp,tempChg,outFileName,thetao,pressure):
    """
    The makeSteric() function takes 3D (not temporal) arguments and creates
    heat content and steric fields which are written to a specified outfile

    Author: Paul J. Durack : [email protected] : @durack1.
    Created on Thu Jul 18 13:03:37 2013.

    Inputs:
    ------
    - salinity(lev,lat,lon) - 3D array for the climatological period.
    - salinityChg(lev,lat,lon) - 3D array for the temporal change period.
    - temp(lev,lat,lon) - 3D array for the climatological period either in-situ or potential temperature.
    - tempChg(lev,lat,lon) - 3D array for the temporal change period as with temp, either in-situ or potential temperature.
    - outFileName(str) - output filename with full path specified.
    - thetao(bool) - boolean value specifying either in-situ or potential temperature arrays provided.
    - pressure(bool) - boolean value specifying whether lev-coordinate is pressure (dbar) or depth (m).

    Usage:
    ------
        >>> from makeStericLib import makeSteric
        >>> makeSteric(salinity,salinityChg,thetao,thetaoChg,'outfile.nc',True,False)

    Notes:
    -----
    - PJD 18 Jul 2013 - Validated Ishii v6.13 data against WOA94 - checks out ok. Units: dyn decimeter compared to http://www.nodc.noaa.gov/OC5/WOA94/dyn.html uses cm (not decimeter; x 10)
    - PJD 18 Jul 2013 - Added attribute scrub to incoming variables (so,so_chg,temp,temp_chg) to maintain output consistency
    - PJD 22 Jul 2013 - Added name attributes to so and temp variables, added units to so_chg
    - PJD 22 Jul 2013 - removed duplicated code by converting repetition to function scrubNaNAndMask
    - PJD 23 Jul 2013 - Further cleaned up so,so_chg,temp,temp_chg outputs specifying id/name attributes
    - PJD  5 Aug 2013 - Updated python-seawater library to version 3.3.1 from github repo, git clone http://github.com/ocefpaf/python-seawater, python setup.py install --user
    - PJD  7 Aug 2013 - FIXED: thetao rather than in-situ temperature propagating throughout calculations
    - PJD  7 Aug 2013 - Replaced looping with 3D gpan
    - PJD  7 Aug 2013 - Further code duplication cleanup
    - PJD  8 Aug 2013 - FIXED: scrubNanAndMask function type/mask/grid issue - encase sw arguments in np.array() (attempt to strip cdms fluff)
    - PJD  8 Aug 2013 - FIXED: removed depth variable unit edits - not all inputs are depth (m)
    - PJD 15 Aug 2013 - Increased interpolated field resolution [200,300,500,700,1000,1500,1800,2000] - [5,10,20,30,40,50,75,100,125,150,200, ...]
    - PJD 18 Aug 2013 - AR5 hard coded rho=1020,cp=4187 == 4.3e6 vs Ishii 1970 rho.mean=1024,cp.mean=3922 == 4.1e6 ~5% too high
    - PJD 13 Jan 2014 - Corrected steric_height_anom and steric_height_thermo_anom to true anomaly fields, needed to remove climatology
    - PJD  3 May 2014 - Turned off thetao conversion, although convert to numpy array rather than cdms2 transient variable
    - PJD 13 Oct 2014 - Added seawater_library_version as a global attribute
    - PJD 13 Oct 2014 - FIXED: bug with calculation of rho_halo variable was calculating gpan
    - PJD 13 Oct 2014 - Added alternate calculation of halosteric anomaly (direct salinity anomaly calculation, rather than total-thermosteric)
    - PJD 13 Oct 2014 - Added makeSteric_version as a global attribute
    - TODO: Better deal with insitu vs thetao variables
    - TODO: Query Charles on why *.name attributes are propagating
    - TODO: validate outputs and compare to matlab versions - 10e-7 errors.
    """

    # Remap all variables to short names
    so          = salinity
    so_chg      = salinityChg
    temp        = temp
    temp_chg    = tempChg
    del(salinity,salinityChg,tempChg) ; gc.collect()

    # Strip attributes to maintain consistency between datasets
    for count,x in enumerate(so.attributes.keys()):
        delattr(so,x)
    #print so.listattributes() ; # Print remaining attributes
    for count,x in enumerate(so_chg.attributes.keys()):
        delattr(so_chg,x)
    for count,x in enumerate(temp.attributes.keys()):
        delattr(temp,x)
    for count,x in enumerate(temp_chg.attributes.keys()):
        delattr(temp_chg,x)
    del(count,x)

    # Create z-coordinate from salinity input
    if not pressure:
        z_coord                         = so.getAxis(0)
        y_coord                         = so.getAxis(1)
        y_coord                         = tile(y_coord,(so.shape[2],1)).transpose()
        depth_levels                    = tile(z_coord.getValue(),(so.shape[2],so.shape[1],1)).transpose()
        pressure_levels                 = sw.pres(np.array(depth_levels),np.array(y_coord))
        del(z_coord,y_coord,depth_levels) ; gc.collect()
    else:
        pressure_levels                 = so.getAxis(0)
        pressure_levels                 = transpose(tile(pressure_levels,(so.shape[2],so.shape[1],1)))

    pressure_levels                 = cdm.createVariable(pressure_levels,id='pressure_levels')
    pressure_levels.setAxis(0,so.getAxis(0))
    pressure_levels.setAxis(1,so.getAxis(1))
    pressure_levels.setAxis(2,so.getAxis(2))
    pressure_levels.id              = 'pressure_levels'
    pressure_levels.units_long      = 'decibar (pressure)'
    pressure_levels.positive        = 'down'
    pressure_levels.long_name       = 'sea_water_pressure'
    pressure_levels.standard_name   = 'sea_water_pressure'
    pressure_levels.units           = 'decibar'
    pressure_levels.axis            = 'Z'

    # Cleanup depth axis attributes
    depth               = so.getAxis(0)
    depth.id            = 'depth'
    depth.name          = 'depth'
    depth.long_name     = 'depth'
    depth.standard_name = 'depth'
    depth.axis          = 'Z'
    so.setAxis(0,depth)
    so_chg.setAxis(0,depth)
    temp.setAxis(0,depth)
    temp_chg.setAxis(0,depth)
    del(depth)

    # Convert using python-seawater library (v3.3.1 - 130807)
    if thetao:
        # Process potential temperature to in-situ - default conversion sets reference pressure to 0 (surface)
        #temp_chg                = sw.temp(np.array(so),np.array(temp_chg),np.array(pressure_levels)); # units degrees C
        #temp                    = sw.temp(np.array(so),np.array(temp),np.array(pressure_levels)); # units degrees C
        #temp_chg                = sw.ptmp(np.array(so),np.array(temp_chg),np.array(pressure_levels),np.array(pressure_levels)); # units degrees C
        #temp                    = sw.ptmp(np.array(so),np.array(temp),np.array(pressure_levels),np.array(pressure_levels)); # units degrees C
        temp_chg                = np.array(temp_chg); # units degrees C
        temp                    = np.array(temp); # units degrees C

    # Climatologies - rho,cp,steric_height
    rho                         = sw.dens(np.array(so),np.array(temp),np.array(pressure_levels)) ; # units kg m-3
    cp                          = sw.cp(np.array(so),np.array(temp),np.array(pressure_levels)) ; # units J kg-1 C-1
    steric_height               = sw.gpan(np.array(so),np.array(temp),np.array(pressure_levels)) ; # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)

    # Halosteric - rho,cp
    ss                          = map(array,(so+so_chg))
    rho_halo                    = sw.dens(np.array(ss),np.array(temp),np.array(pressure_levels)) ; # units kg m-3
    cp_halo                     = sw.cp(np.array(ss),np.array(temp),np.array(pressure_levels)) ; # units J kg-1 C-1
    tmp                         = sw.gpan(np.array(ss),np.array(temp),np.array(pressure_levels)) ; # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    steric_height_halo_anom2    = tmp-steric_height ; # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)

    # Full steric - steric_height
    tt                          = map(array,(temp+temp_chg))
    tmp                         = sw.gpan(np.array(ss),np.array(tt),np.array(pressure_levels)) ; # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    steric_height_anom          = tmp-steric_height ; # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    del(ss,tmp) ; gc.collect()

    # Thermosteric - rho,cp,steric_height
    rho_thermo                  = sw.dens(np.array(so),np.array(tt),np.array(pressure_levels)) ; # units kg m-3
    cp_thermo                   = sw.cp(np.array(so),np.array(tt),np.array(pressure_levels)) ; # units J kg-1 C-1
    tmp                         = sw.gpan(np.array(so),np.array(tt),np.array(pressure_levels)) ; # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    steric_height_thermo_anom   = tmp-steric_height ; # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    del(tt,tmp) ; gc.collect()

    # Halosteric - steric_height
    steric_height_halo_anom     = steric_height_anom-steric_height_thermo_anom ; # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)

    # Create heat content
    heat_content                = np.array(temp)*np.array(rho)*np.array(cp) ; # units J
    heat_content_sanom          = np.array(temp)*np.array(rho_halo)*np.array(cp_halo) ; # units J
    heat_content_tanom          = np.array(temp_chg)*np.array(rho)*np.array(cp) ; # units J
    #heat_content_tanom          = np.array(temp_chg)*np.array(1020)*np.array(4187) ; # units J - try hard-coded - AR5 numbers
    heat_content_tsanom         = np.array(temp_chg)*np.array(rho_halo)*np.array(cp_halo) ; # units J

    # Correct all instances of NaN values and fix masks - applied before cdms variables are created otherwise names/ids/attributes are reset
    temp                        = scrubNaNAndMask(temp,so)
    temp_chg                    = scrubNaNAndMask(temp_chg,so)
    rho                         = scrubNaNAndMask(rho,so)
    cp                          = scrubNaNAndMask(cp,so)
    rho_halo                    = scrubNaNAndMask(rho_halo,so)
    cp_halo                     = scrubNaNAndMask(cp_halo,so)
    rho_thermo                  = scrubNaNAndMask(rho_thermo,so)
    cp_thermo                   = scrubNaNAndMask(cp_thermo,so)
    steric_height               = scrubNaNAndMask(steric_height,so)
    steric_height_anom          = scrubNaNAndMask(steric_height_anom,so)
    steric_height_thermo_anom   = scrubNaNAndMask(steric_height_thermo_anom,so)
    steric_height_halo_anom     = scrubNaNAndMask(steric_height_halo_anom,so)
    steric_height_halo_anom2    = scrubNaNAndMask(steric_height_halo_anom2,so)
    heat_content                = scrubNaNAndMask(heat_content,so)
    heat_content_sanom          = scrubNaNAndMask(heat_content_sanom,so)
    heat_content_tanom          = scrubNaNAndMask(heat_content_tanom,so)
    heat_content_tsanom         = scrubNaNAndMask(heat_content_tsanom,so)

    # Recreate and redress variables
    so.id                           = 'so_mean'
    so.units                        = '1e-3'
    so_chg.id                       = 'so_chg'
    so_chg.units                    = '1e-3'
    temp                            = cdm.createVariable(temp,id='temp_mean')
    temp.setAxis(0,so.getAxis(0))
    temp.setAxis(1,so.getAxis(1))
    temp.setAxis(2,so.getAxis(2))
    temp.units                      = 'degrees_C'
    temp_chg                        = cdm.createVariable(temp_chg,id='temp_chg')
    temp_chg.setAxis(0,so.getAxis(0))
    temp_chg.setAxis(1,so.getAxis(1))
    temp_chg.setAxis(2,so.getAxis(2))
    temp_chg.units                  = 'degrees_C'
    rho                             = cdm.createVariable(rho,id='rho')
    rho.setAxis(0,so.getAxis(0))
    rho.setAxis(1,so.getAxis(1))
    rho.setAxis(2,so.getAxis(2))
    rho.name                        = 'density_mean'
    rho.units                       = 'kg m^-3'
    cp                              = cdm.createVariable(cp,id='cp')
    cp.setAxis(0,so.getAxis(0))
    cp.setAxis(1,so.getAxis(1))
    cp.setAxis(2,so.getAxis(2))
    cp.name                         = 'heat_capacity_mean'
    cp.units                        = 'J kg^-1 C^-1'
    rho_halo                        = cdm.createVariable(rho_halo,id='rho_halo')
    rho_halo.setAxis(0,so.getAxis(0))
    rho_halo.setAxis(1,so.getAxis(1))
    rho_halo.setAxis(2,so.getAxis(2))
    rho_halo.name                   = 'density_mean_halo'
    rho_halo.units                  = 'kg m^-3'
    cp_halo                         = cdm.createVariable(cp_halo,id='cp_halo')
    cp_halo.setAxis(0,so.getAxis(0))
    cp_halo.setAxis(1,so.getAxis(1))
    cp_halo.setAxis(2,so.getAxis(2))
    cp_halo.name                    = 'heat_capacity_mean_halo'
    cp_halo.units                   = 'J kg^-1 C^-1'
    rho_thermo                      = cdm.createVariable(rho_thermo,id='rho_thermo')
    rho_thermo.setAxis(0,so.getAxis(0))
    rho_thermo.setAxis(1,so.getAxis(1))
    rho_thermo.setAxis(2,so.getAxis(2))
    rho_thermo.name                 = 'density_mean_thermo'
    rho_thermo.units                = 'kg m^-3'
    cp_thermo                       = cdm.createVariable(cp_thermo,id='cp_thermo')
    cp_thermo.setAxis(0,so.getAxis(0))
    cp_thermo.setAxis(1,so.getAxis(1))
    cp_thermo.setAxis(2,so.getAxis(2))
    cp_thermo.name                  = 'heat_capacity_mean_thermo'
    cp_thermo.units                 = 'J kg^-1 C^-1'
    steric_height                   = cdm.createVariable(steric_height,id='steric_height')
    steric_height.setAxis(0,so.getAxis(0))
    steric_height.setAxis(1,so.getAxis(1))
    steric_height.setAxis(2,so.getAxis(2))
    steric_height.units             = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_anom              = cdm.createVariable(steric_height_anom,id='steric_height_anom')
    steric_height_anom.setAxis(0,so.getAxis(0))
    steric_height_anom.setAxis(1,so.getAxis(1))
    steric_height_anom.setAxis(2,so.getAxis(2))
    steric_height_anom.units        = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_thermo_anom       = cdm.createVariable(steric_height_thermo_anom,id='steric_height_thermo_anom')
    steric_height_thermo_anom.setAxis(0,so.getAxis(0))
    steric_height_thermo_anom.setAxis(1,so.getAxis(1))
    steric_height_thermo_anom.setAxis(2,so.getAxis(2))
    steric_height_thermo_anom.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_halo_anom         = cdm.createVariable(steric_height_halo_anom,id='steric_height_halo_anom')
    steric_height_halo_anom.setAxis(0,so.getAxis(0))
    steric_height_halo_anom.setAxis(1,so.getAxis(1))
    steric_height_halo_anom.setAxis(2,so.getAxis(2))
    steric_height_halo_anom.units   = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_halo_anom2         = cdm.createVariable(steric_height_halo_anom2,id='steric_height_halo_anom2')
    steric_height_halo_anom2.setAxis(0,so.getAxis(0))
    steric_height_halo_anom2.setAxis(1,so.getAxis(1))
    steric_height_halo_anom2.setAxis(2,so.getAxis(2))
    steric_height_halo_anom2.units   = 'm^3 kg^-1 Pa (dynamic decimeter)'
    heat_content                    = cdm.createVariable(heat_content,id='heat_content')
    heat_content.setAxis(0,so.getAxis(0))
    heat_content.setAxis(1,so.getAxis(1))
    heat_content.setAxis(2,so.getAxis(2))
    heat_content.units         = 'J'
    heat_content_sanom              = cdm.createVariable(heat_content_sanom,id='heat_content_sanom')
    heat_content_sanom.setAxis(0,so.getAxis(0))
    heat_content_sanom.setAxis(1,so.getAxis(1))
    heat_content_sanom.setAxis(2,so.getAxis(2))
    heat_content_sanom.units        = 'J'
    heat_content_tanom              = cdm.createVariable(heat_content_tanom,id='heat_content_tanom')
    heat_content_tanom.setAxis(0,so.getAxis(0))
    heat_content_tanom.setAxis(1,so.getAxis(1))
    heat_content_tanom.setAxis(2,so.getAxis(2))
    heat_content_tanom.units        = 'J'
    heat_content_tsanom             = cdm.createVariable(heat_content_tsanom,id='heat_content_tsanom')
    heat_content_tsanom.setAxis(0,so.getAxis(0))
    heat_content_tsanom.setAxis(1,so.getAxis(1))
    heat_content_tsanom.setAxis(2,so.getAxis(2))
    heat_content_tsanom.units       = 'J'

    # Create model-based depth index for subset target levels
    newdepth = np.array([5,10,20,30,40,50,75,100,125,150,200,300,500,700,1000,1500,1800,2000]).astype('f');
    newdepth_bounds = np.array([[0,5],[5,10],[10,20],[20,30],[30,40],[40,50],[50,75],[75,100],[100,125],[125,150],
    [150,200],[200,300],[300,500],[500,700],[700,1000],[1000,1500],[1500,1800],[1800,2000]]).astype('f')
    #newdepth = np.array([200,300,500,700,1000,1500,1800,2000]).astype('f');
    #newdepth_bounds = np.array([[0,200],[200,300],[300,500],[500,700],[700,1000],[1000,1500],[1500,1800],[1800,2000]]).astype('f')

    # Interpolate to depths
    so_depthInterp                          = cdu.linearInterpolation(so,pressure_levels,levels=newdepth)
    temp_depthInterp                        = cdu.linearInterpolation(temp,pressure_levels,levels=newdepth)
    steric_height_depthInterp               = cdu.linearInterpolation(steric_height,pressure_levels,levels=newdepth)
    steric_height_anom_depthInterp          = cdu.linearInterpolation(steric_height_anom,pressure_levels,levels=newdepth)
    steric_height_thermo_anom_depthInterp   = cdu.linearInterpolation(steric_height_thermo_anom,pressure_levels,levels=newdepth)
    steric_height_halo_anom_depthInterp     = cdu.linearInterpolation(steric_height_halo_anom,pressure_levels,levels=newdepth)
    steric_height_halo_anom2_depthInterp    = cdu.linearInterpolation(steric_height_halo_anom2,pressure_levels,levels=newdepth)
    heat_content_sanom_depthInterp          = cdu.linearInterpolation(heat_content_sanom,pressure_levels,levels=newdepth)
    heat_content_tanom_depthInterp          = cdu.linearInterpolation(heat_content_tanom,pressure_levels,levels=newdepth)
    heat_content_tsanom_depthInterp         = cdu.linearInterpolation(heat_content_tanom,pressure_levels,levels=newdepth)

    # Fix masks - applied before cdms variables are created otherwise names/ids/attributes are reset
    temp_depthInterp                        = scrubNaNAndMask(temp_depthInterp,so_depthInterp)
    steric_height_depthInterp               = scrubNaNAndMask(steric_height_depthInterp,so_depthInterp)
    steric_height_anom_depthInterp          = scrubNaNAndMask(steric_height_anom_depthInterp,so_depthInterp)
    steric_height_thermo_anom_depthInterp   = scrubNaNAndMask(steric_height_thermo_anom_depthInterp,so_depthInterp)
    steric_height_halo_anom_depthInterp     = scrubNaNAndMask(steric_height_halo_anom_depthInterp,so_depthInterp)
    steric_height_halo_anom2_depthInterp    = scrubNaNAndMask(steric_height_halo_anom2_depthInterp,so_depthInterp)
    heat_content_sanom_depthInterp          = scrubNaNAndMask(heat_content_sanom_depthInterp,so_depthInterp)
    heat_content_tanom_depthInterp          = scrubNaNAndMask(heat_content_tanom_depthInterp,so_depthInterp)
    heat_content_tsanom_depthInterp         = scrubNaNAndMask(heat_content_tsanom_depthInterp,so_depthInterp)

    # Fix bounds
    newdepth = so_depthInterp.getAxis(0)
    newdepth.setBounds(newdepth_bounds)
    del(newdepth_bounds)
    newdepth.id             = 'depth2'
    newdepth.units_long     = 'decibar (pressure)'
    newdepth.positive       = 'down'
    newdepth.long_name      = 'sea_water_pressure'
    newdepth.standard_name  = 'sea_water_pressure'
    newdepth.units          = 'decibar'
    newdepth.axis           = 'Z'

    # Assign corrected bounds
    so_depthInterp.setAxis(0,newdepth)
    temp_depthInterp.setAxis(0,newdepth)
    steric_height_depthInterp.setAxis(0,newdepth)
    steric_height_anom_depthInterp.setAxis(0,newdepth)
    steric_height_thermo_anom_depthInterp.setAxis(0,newdepth)
    steric_height_halo_anom_depthInterp.setAxis(0,newdepth)
    steric_height_halo_anom2_depthInterp.setAxis(0,newdepth)
    heat_content_sanom_depthInterp.setAxis(0,newdepth)
    heat_content_tanom_depthInterp.setAxis(0,newdepth)
    heat_content_tsanom_depthInterp.setAxis(0,newdepth)

    # Average/integrate to surface - configure bounds
    # Preallocate arrays
    so_depthAve                     = np.ma.zeros([len(newdepth),shape(so)[1],shape(so)[2]])
    temp_depthAve                   = so_depthAve.copy()
    heat_content_sanom_depthInteg   = so_depthAve.copy()
    heat_content_tanom_depthInteg   = so_depthAve.copy()
    heat_content_tsanom_depthInteg  = so_depthAve.copy()
    for count,depth in enumerate(newdepth):
        tmp = cdu.averager(so_depthInterp[0:(count+1),...],axis=0,weights='weighted',action='average')
        so_depthAve[count,]                     = tmp;
        tmp = cdu.averager(temp_depthInterp[0:(count+1),...],axis=0,weights='weighted',action='average')
        temp_depthAve[count,]                   = tmp;
        tmp = cdu.averager(heat_content_sanom_depthInterp[0:(count+1),...],axis=0,weights='weighted',action='sum')
        heat_content_sanom_depthInteg[count,]   = tmp
        tmp = cdu.averager(heat_content_tanom_depthInterp[0:(count+1),...],axis=0,weights='weighted',action='sum')
        heat_content_tanom_depthInteg[count,]   = tmp
        tmp = cdu.averager(heat_content_tsanom_depthInterp[0:(count+1),...],axis=0,weights='weighted',action='sum')
        heat_content_tsanom_depthInteg[count,]  = tmp
    del(heat_content_tanom_depthInterp,heat_content_tsanom_depthInterp); gc.collect()

    # Fix masks - applied before cdms variables are created otherwise names/ids/attributes are reset
    so_depthAve = scrubNaNAndMask(so_depthAve,so_depthInterp)
    temp_depthAve = scrubNaNAndMask(temp_depthAve,so_depthInterp)
    heat_content_sanom_depthInteg = scrubNaNAndMask(heat_content_sanom_depthInteg,so_depthInterp)
    heat_content_tanom_depthInteg = scrubNaNAndMask(heat_content_tanom_depthInteg,so_depthInterp)
    heat_content_tsanom_depthInteg = scrubNaNAndMask(heat_content_tsanom_depthInteg,so_depthInterp)
    del(so_depthInterp)

    # Convert numpy arrays to cdms objects
    heat_content_sanom_depthInteg               = cdm.createVariable(heat_content_sanom_depthInteg,id='heat_content_sanom_depthInteg')
    heat_content_sanom_depthInteg.id            = 'heat_content_sanom_depthInteg'
    heat_content_sanom_depthInteg.setAxis(0,newdepth)
    heat_content_sanom_depthInteg.setAxis(1,so.getAxis(1))
    heat_content_sanom_depthInteg.setAxis(2,so.getAxis(2))
    heat_content_sanom_depthInteg.units         = 'J'
    heat_content_tanom_depthInteg               = cdm.createVariable(heat_content_tanom_depthInteg,id='heat_content_tanom_depthInteg')
    heat_content_tanom_depthInteg.id            = 'heat_content_tanom_depthInteg'
    heat_content_tanom_depthInteg.setAxis(0,newdepth)
    heat_content_tanom_depthInteg.setAxis(1,so.getAxis(1))
    heat_content_tanom_depthInteg.setAxis(2,so.getAxis(2))
    heat_content_tanom_depthInteg.units         = 'J'
    heat_content_tsanom_depthInteg              = cdm.createVariable(heat_content_tsanom_depthInteg,id='heat_content_tsanom_depthInteg')
    heat_content_tsanom_depthInteg.id           = 'heat_content_tsanom_depthInteg'
    heat_content_tsanom_depthInteg.setAxis(0,newdepth)
    heat_content_tsanom_depthInteg.setAxis(1,so.getAxis(1))
    heat_content_tsanom_depthInteg.setAxis(2,so.getAxis(2))
    heat_content_tsanom_depthInteg.units        = 'J'
    so_depthAve                                 = cdm.createVariable(so_depthAve,id='so_depthAve')
    so_depthAve.id                              = 'so_depthAve'
    so_depthAve.setAxis(0,newdepth)
    so_depthAve.setAxis(1,so.getAxis(1))
    so_depthAve.setAxis(2,so.getAxis(2))
    so_depthAve.units                           = '1e-3'
    temp_depthAve                               = cdm.createVariable(temp_depthAve,id='temp_depthAve')
    temp_depthAve.id                            = 'temp_depthAve'
    temp_depthAve.setAxis(0,newdepth)
    temp_depthAve.setAxis(1,so.getAxis(1))
    temp_depthAve.setAxis(2,so.getAxis(2))
    temp_depthAve.units                         = 'degrees_C'
    steric_height_depthInterp                   = cdm.createVariable(steric_height_depthInterp,id='steric_height_depthInterp')
    steric_height_depthInterp.setAxis(0,newdepth)
    steric_height_depthInterp.setAxis(1,so.getAxis(1))
    steric_height_depthInterp.setAxis(2,so.getAxis(2))
    steric_height_depthInterp.units             = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_anom_depthInterp              = cdm.createVariable(steric_height_anom_depthInterp,id='steric_height_anom_depthInterp')
    steric_height_anom_depthInterp.setAxis(0,newdepth)
    steric_height_anom_depthInterp.setAxis(1,so.getAxis(1))
    steric_height_anom_depthInterp.setAxis(2,rho.getAxis(2))
    steric_height_anom_depthInterp.units        = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_thermo_anom_depthInterp       = cdm.createVariable(steric_height_thermo_anom_depthInterp,id='steric_height_thermo_anom_depthInterp')
    steric_height_thermo_anom_depthInterp.setAxis(0,newdepth)
    steric_height_thermo_anom_depthInterp.setAxis(1,so.getAxis(1))
    steric_height_thermo_anom_depthInterp.setAxis(2,so.getAxis(2))
    steric_height_thermo_anom_depthInterp.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_halo_anom_depthInterp         = cdm.createVariable(steric_height_halo_anom_depthInterp,id='steric_height_halo_anom_depthInterp')
    steric_height_halo_anom_depthInterp.setAxis(0,newdepth)
    steric_height_halo_anom_depthInterp.setAxis(1,so.getAxis(1))
    steric_height_halo_anom_depthInterp.setAxis(2,so.getAxis(2))
    steric_height_halo_anom_depthInterp.units   = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_halo_anom2_depthInterp         = cdm.createVariable(steric_height_halo_anom2_depthInterp,id='steric_height_halo_anom2_depthInterp')
    steric_height_halo_anom2_depthInterp.setAxis(0,newdepth)
    steric_height_halo_anom2_depthInterp.setAxis(1,so.getAxis(1))
    steric_height_halo_anom2_depthInterp.setAxis(2,so.getAxis(2))
    steric_height_halo_anom2_depthInterp.units   = 'm^3 kg^-1 Pa (dynamic decimeter)'
    # Cleanup workspace
    del(newdepth) ; gc.collect()


    # Write variables to file
    if os.path.isfile(outFileName):
        os.remove(outFileName)
    filehandle = cdm.open(outFileName,'w')
    # Global attributes
    globalAttWrite(filehandle,options=None) ; # Use function to write standard global atts
    # Write seawater version
    filehandle.seawater_library_version = sw.__version__
    # Write makeSteric version
    makeStericPath = str(makeSteric.__code__).split(' ')[6]
    makeStericPath = replace(replace(makeStericPath,'"',''),',','') ; # Clean scraped path
    filehandle.makeSteric_version = ' '.join(getGitInfo(makeStericPath)[0:3])
    # Master variables
    filehandle.write(so.astype('float32'))
    filehandle.write(so_chg.astype('float32'))
    filehandle.write(so_depthAve.astype('float32'))
    filehandle.write(temp.astype('float32'))
    filehandle.write(temp_chg.astype('float32'))
    filehandle.write(temp_depthAve.astype('float32'))
    # Derived variables
    filehandle.write(cp.astype('float32'))
    filehandle.write(cp_halo.astype('float32'))
    filehandle.write(cp_thermo.astype('float32'))
    filehandle.write(rho.astype('float32'))
    filehandle.write(rho_halo.astype('float32'))
    filehandle.write(rho_thermo.astype('float32'))
    filehandle.write(heat_content.astype('float32'))
    filehandle.write(heat_content_sanom.astype('float32'))
    filehandle.write(heat_content_sanom_depthInteg.astype('float32'))
    filehandle.write(heat_content_tanom.astype('float32'))
    filehandle.write(heat_content_tanom_depthInteg.astype('float32'))
    filehandle.write(heat_content_tsanom.astype('float32'))
    filehandle.write(heat_content_tsanom_depthInteg.astype('float32'))
    filehandle.write(steric_height.astype('float32'))
    filehandle.write(steric_height_depthInterp.astype('float32'))
    filehandle.write(steric_height_anom.astype('float32'))
    filehandle.write(steric_height_anom_depthInterp.astype('float32'))
    filehandle.write(steric_height_halo_anom.astype('float32'))
    filehandle.write(steric_height_halo_anom2.astype('float32'))
    filehandle.write(steric_height_halo_anom_depthInterp.astype('float32'))
    filehandle.write(steric_height_halo_anom2_depthInterp.astype('float32'))
    filehandle.write(steric_height_thermo_anom.astype('float32'))
    filehandle.write(steric_height_thermo_anom_depthInterp.astype('float32'))
    filehandle.close()
    # Cleanup workspace
    del(outFileName) ; gc.collect()
Example #10
0
def makeSteric(salinity, salinityChg, temp, tempChg, outFileName, thetao,
               pressure):
    """
    The makeSteric() function takes 3D (not temporal) arguments and creates
    heat content and steric fields which are written to a specified outfile

    Author: Paul J. Durack : [email protected] : @durack1.
    Created on Thu Jul 18 13:03:37 2013.

    Inputs:
    ------
    - salinity(lev,lat,lon) - 3D array for the climatological period.
    - salinityChg(lev,lat,lon) - 3D array for the temporal change period.
    - temp(lev,lat,lon) - 3D array for the climatological period either in-situ or potential temperature.
    - tempChg(lev,lat,lon) - 3D array for the temporal change period as with temp, either in-situ or potential temperature.
    - outFileName(str) - output filename with full path specified.
    - thetao(bool) - boolean value specifying either in-situ or potential temperature arrays provided.
    - pressure(bool) - boolean value specifying whether lev-coordinate is pressure (dbar) or depth (m).

    Usage:
    ------
        >>> from makeStericLib import makeSteric
        >>> makeSteric(salinity,salinityChg,thetao,thetaoChg,'outfile.nc',True,False)

    Notes:
    -----
    - PJD 18 Jul 2013 - Validated Ishii v6.13 data against WOA94 - checks out ok. Units: dyn decimeter compared to http://www.nodc.noaa.gov/OC5/WOA94/dyn.html uses cm (not decimeter; x 10)
    - PJD 18 Jul 2013 - Added attribute scrub to incoming variables (so,so_chg,temp,temp_chg) to maintain output consistency
    - PJD 22 Jul 2013 - Added name attributes to so and temp variables, added units to so_chg
    - PJD 22 Jul 2013 - removed duplicated code by converting repetition to function scrubNaNAndMask
    - PJD 23 Jul 2013 - Further cleaned up so,so_chg,temp,temp_chg outputs specifying id/name attributes
    - PJD  5 Aug 2013 - Updated python-seawater library to version 3.3.1 from github repo, git clone http://github.com/ocefpaf/python-seawater, python setup.py install --user
    - PJD  7 Aug 2013 - FIXED: thetao rather than in-situ temperature propagating throughout calculations
    - PJD  7 Aug 2013 - Replaced looping with 3D gpan
    - PJD  7 Aug 2013 - Further code duplication cleanup
    - PJD  8 Aug 2013 - FIXED: scrubNanAndMask function type/mask/grid issue - encase sw arguments in np.array() (attempt to strip cdms fluff)
    - PJD  8 Aug 2013 - FIXED: removed depth variable unit edits - not all inputs are depth (m)
    - PJD 15 Aug 2013 - Increased interpolated field resolution [200,300,500,700,1000,1500,1800,2000] - [5,10,20,30,40,50,75,100,125,150,200, ...]
    - PJD 18 Aug 2013 - AR5 hard coded rho=1020,cp=4187 == 4.3e6 vs Ishii 1970 rho.mean=1024,cp.mean=3922 == 4.1e6 ~5% too high
    - PJD 13 Jan 2014 - Corrected steric_height_anom and steric_height_thermo_anom to true anomaly fields, needed to remove climatology
    - PJD  3 May 2014 - Turned off thetao conversion, although convert to numpy array rather than cdms2 transient variable
    - PJD 13 Oct 2014 - Added seawater_library_version as a global attribute
    - PJD 13 Oct 2014 - FIXED: bug with calculation of rho_halo variable was calculating gpan
    - PJD 13 Oct 2014 - Added alternate calculation of halosteric anomaly (direct salinity anomaly calculation, rather than total-thermosteric)
    - PJD 13 Oct 2014 - Added makeSteric_version as a global attribute
    - TODO: Better deal with insitu vs thetao variables
    - TODO: Query Charles on why *.name attributes are propagating
    - TODO: validate outputs and compare to matlab versions - 10e-7 errors.
    """

    # Remap all variables to short names
    so = salinity
    so_chg = salinityChg
    temp = temp
    temp_chg = tempChg
    del (salinity, salinityChg, tempChg)
    gc.collect()

    # Strip attributes to maintain consistency between datasets
    for count, x in enumerate(so.attributes.keys()):
        delattr(so, x)
    #print so.listattributes() ; # Print remaining attributes
    for count, x in enumerate(so_chg.attributes.keys()):
        delattr(so_chg, x)
    for count, x in enumerate(temp.attributes.keys()):
        delattr(temp, x)
    for count, x in enumerate(temp_chg.attributes.keys()):
        delattr(temp_chg, x)
    del (count, x)

    # Create z-coordinate from salinity input
    if not pressure:
        z_coord = so.getAxis(0)
        y_coord = so.getAxis(1)
        y_coord = tile(y_coord, (so.shape[2], 1)).transpose()
        depth_levels = tile(z_coord.getValue(),
                            (so.shape[2], so.shape[1], 1)).transpose()
        pressure_levels = sw.pres(np.array(depth_levels), np.array(y_coord))
        del (z_coord, y_coord, depth_levels)
        gc.collect()
    else:
        pressure_levels = so.getAxis(0)
        pressure_levels = transpose(
            tile(pressure_levels, (so.shape[2], so.shape[1], 1)))

    pressure_levels = cdm.createVariable(pressure_levels, id='pressure_levels')
    pressure_levels.setAxis(0, so.getAxis(0))
    pressure_levels.setAxis(1, so.getAxis(1))
    pressure_levels.setAxis(2, so.getAxis(2))
    pressure_levels.id = 'pressure_levels'
    pressure_levels.units_long = 'decibar (pressure)'
    pressure_levels.positive = 'down'
    pressure_levels.long_name = 'sea_water_pressure'
    pressure_levels.standard_name = 'sea_water_pressure'
    pressure_levels.units = 'decibar'
    pressure_levels.axis = 'Z'

    # Cleanup depth axis attributes
    depth = so.getAxis(0)
    depth.id = 'depth'
    depth.name = 'depth'
    depth.long_name = 'depth'
    depth.standard_name = 'depth'
    depth.axis = 'Z'
    so.setAxis(0, depth)
    so_chg.setAxis(0, depth)
    temp.setAxis(0, depth)
    temp_chg.setAxis(0, depth)
    del (depth)

    # Convert using python-seawater library (v3.3.1 - 130807)
    if thetao:
        # Process potential temperature to in-situ - default conversion sets reference pressure to 0 (surface)
        #temp_chg                = sw.temp(np.array(so),np.array(temp_chg),np.array(pressure_levels)); # units degrees C
        #temp                    = sw.temp(np.array(so),np.array(temp),np.array(pressure_levels)); # units degrees C
        #temp_chg                = sw.ptmp(np.array(so),np.array(temp_chg),np.array(pressure_levels),np.array(pressure_levels)); # units degrees C
        #temp                    = sw.ptmp(np.array(so),np.array(temp),np.array(pressure_levels),np.array(pressure_levels)); # units degrees C
        temp_chg = np.array(temp_chg)
        # units degrees C
        temp = np.array(temp)
        # units degrees C

    # Climatologies - rho,cp,steric_height
    rho = sw.dens(np.array(so), np.array(temp), np.array(pressure_levels))
    # units kg m-3
    cp = sw.cp(np.array(so), np.array(temp), np.array(pressure_levels))
    # units J kg-1 C-1
    steric_height = sw.gpan(np.array(so), np.array(temp),
                            np.array(pressure_levels))
    # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)

    # Halosteric - rho,cp
    ss = map(array, (so + so_chg))
    rho_halo = sw.dens(np.array(ss), np.array(temp), np.array(pressure_levels))
    # units kg m-3
    cp_halo = sw.cp(np.array(ss), np.array(temp), np.array(pressure_levels))
    # units J kg-1 C-1
    tmp = sw.gpan(np.array(ss), np.array(temp), np.array(pressure_levels))
    # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    steric_height_halo_anom2 = tmp - steric_height
    # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)

    # Full steric - steric_height
    tt = map(array, (temp + temp_chg))
    tmp = sw.gpan(np.array(ss), np.array(tt), np.array(pressure_levels))
    # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    steric_height_anom = tmp - steric_height
    # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    del (ss, tmp)
    gc.collect()

    # Thermosteric - rho,cp,steric_height
    rho_thermo = sw.dens(np.array(so), np.array(tt), np.array(pressure_levels))
    # units kg m-3
    cp_thermo = sw.cp(np.array(so), np.array(tt), np.array(pressure_levels))
    # units J kg-1 C-1
    tmp = sw.gpan(np.array(so), np.array(tt), np.array(pressure_levels))
    # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    steric_height_thermo_anom = tmp - steric_height
    # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)
    del (tt, tmp)
    gc.collect()

    # Halosteric - steric_height
    steric_height_halo_anom = steric_height_anom - steric_height_thermo_anom
    # units m3 kg-1 Pa == m2 s-2 == J kg-1 (dynamic decimeter)

    # Create heat content
    heat_content = np.array(temp) * np.array(rho) * np.array(cp)
    # units J
    heat_content_sanom = np.array(temp) * np.array(rho_halo) * np.array(
        cp_halo)
    # units J
    heat_content_tanom = np.array(temp_chg) * np.array(rho) * np.array(cp)
    # units J
    #heat_content_tanom          = np.array(temp_chg)*np.array(1020)*np.array(4187) ; # units J - try hard-coded - AR5 numbers
    heat_content_tsanom = np.array(temp_chg) * np.array(rho_halo) * np.array(
        cp_halo)
    # units J

    # Correct all instances of NaN values and fix masks - applied before cdms variables are created otherwise names/ids/attributes are reset
    temp = scrubNaNAndMask(temp, so)
    temp_chg = scrubNaNAndMask(temp_chg, so)
    rho = scrubNaNAndMask(rho, so)
    cp = scrubNaNAndMask(cp, so)
    rho_halo = scrubNaNAndMask(rho_halo, so)
    cp_halo = scrubNaNAndMask(cp_halo, so)
    rho_thermo = scrubNaNAndMask(rho_thermo, so)
    cp_thermo = scrubNaNAndMask(cp_thermo, so)
    steric_height = scrubNaNAndMask(steric_height, so)
    steric_height_anom = scrubNaNAndMask(steric_height_anom, so)
    steric_height_thermo_anom = scrubNaNAndMask(steric_height_thermo_anom, so)
    steric_height_halo_anom = scrubNaNAndMask(steric_height_halo_anom, so)
    steric_height_halo_anom2 = scrubNaNAndMask(steric_height_halo_anom2, so)
    heat_content = scrubNaNAndMask(heat_content, so)
    heat_content_sanom = scrubNaNAndMask(heat_content_sanom, so)
    heat_content_tanom = scrubNaNAndMask(heat_content_tanom, so)
    heat_content_tsanom = scrubNaNAndMask(heat_content_tsanom, so)

    # Recreate and redress variables
    so.id = 'so_mean'
    so.units = '1e-3'
    so_chg.id = 'so_chg'
    so_chg.units = '1e-3'
    temp = cdm.createVariable(temp, id='temp_mean')
    temp.setAxis(0, so.getAxis(0))
    temp.setAxis(1, so.getAxis(1))
    temp.setAxis(2, so.getAxis(2))
    temp.units = 'degrees_C'
    temp_chg = cdm.createVariable(temp_chg, id='temp_chg')
    temp_chg.setAxis(0, so.getAxis(0))
    temp_chg.setAxis(1, so.getAxis(1))
    temp_chg.setAxis(2, so.getAxis(2))
    temp_chg.units = 'degrees_C'
    rho = cdm.createVariable(rho, id='rho')
    rho.setAxis(0, so.getAxis(0))
    rho.setAxis(1, so.getAxis(1))
    rho.setAxis(2, so.getAxis(2))
    rho.name = 'density_mean'
    rho.units = 'kg m^-3'
    cp = cdm.createVariable(cp, id='cp')
    cp.setAxis(0, so.getAxis(0))
    cp.setAxis(1, so.getAxis(1))
    cp.setAxis(2, so.getAxis(2))
    cp.name = 'heat_capacity_mean'
    cp.units = 'J kg^-1 C^-1'
    rho_halo = cdm.createVariable(rho_halo, id='rho_halo')
    rho_halo.setAxis(0, so.getAxis(0))
    rho_halo.setAxis(1, so.getAxis(1))
    rho_halo.setAxis(2, so.getAxis(2))
    rho_halo.name = 'density_mean_halo'
    rho_halo.units = 'kg m^-3'
    cp_halo = cdm.createVariable(cp_halo, id='cp_halo')
    cp_halo.setAxis(0, so.getAxis(0))
    cp_halo.setAxis(1, so.getAxis(1))
    cp_halo.setAxis(2, so.getAxis(2))
    cp_halo.name = 'heat_capacity_mean_halo'
    cp_halo.units = 'J kg^-1 C^-1'
    rho_thermo = cdm.createVariable(rho_thermo, id='rho_thermo')
    rho_thermo.setAxis(0, so.getAxis(0))
    rho_thermo.setAxis(1, so.getAxis(1))
    rho_thermo.setAxis(2, so.getAxis(2))
    rho_thermo.name = 'density_mean_thermo'
    rho_thermo.units = 'kg m^-3'
    cp_thermo = cdm.createVariable(cp_thermo, id='cp_thermo')
    cp_thermo.setAxis(0, so.getAxis(0))
    cp_thermo.setAxis(1, so.getAxis(1))
    cp_thermo.setAxis(2, so.getAxis(2))
    cp_thermo.name = 'heat_capacity_mean_thermo'
    cp_thermo.units = 'J kg^-1 C^-1'
    steric_height = cdm.createVariable(steric_height, id='steric_height')
    steric_height.setAxis(0, so.getAxis(0))
    steric_height.setAxis(1, so.getAxis(1))
    steric_height.setAxis(2, so.getAxis(2))
    steric_height.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_anom = cdm.createVariable(steric_height_anom,
                                            id='steric_height_anom')
    steric_height_anom.setAxis(0, so.getAxis(0))
    steric_height_anom.setAxis(1, so.getAxis(1))
    steric_height_anom.setAxis(2, so.getAxis(2))
    steric_height_anom.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_thermo_anom = cdm.createVariable(
        steric_height_thermo_anom, id='steric_height_thermo_anom')
    steric_height_thermo_anom.setAxis(0, so.getAxis(0))
    steric_height_thermo_anom.setAxis(1, so.getAxis(1))
    steric_height_thermo_anom.setAxis(2, so.getAxis(2))
    steric_height_thermo_anom.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_halo_anom = cdm.createVariable(steric_height_halo_anom,
                                                 id='steric_height_halo_anom')
    steric_height_halo_anom.setAxis(0, so.getAxis(0))
    steric_height_halo_anom.setAxis(1, so.getAxis(1))
    steric_height_halo_anom.setAxis(2, so.getAxis(2))
    steric_height_halo_anom.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_halo_anom2 = cdm.createVariable(
        steric_height_halo_anom2, id='steric_height_halo_anom2')
    steric_height_halo_anom2.setAxis(0, so.getAxis(0))
    steric_height_halo_anom2.setAxis(1, so.getAxis(1))
    steric_height_halo_anom2.setAxis(2, so.getAxis(2))
    steric_height_halo_anom2.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    heat_content = cdm.createVariable(heat_content, id='heat_content')
    heat_content.setAxis(0, so.getAxis(0))
    heat_content.setAxis(1, so.getAxis(1))
    heat_content.setAxis(2, so.getAxis(2))
    heat_content.units = 'J'
    heat_content_sanom = cdm.createVariable(heat_content_sanom,
                                            id='heat_content_sanom')
    heat_content_sanom.setAxis(0, so.getAxis(0))
    heat_content_sanom.setAxis(1, so.getAxis(1))
    heat_content_sanom.setAxis(2, so.getAxis(2))
    heat_content_sanom.units = 'J'
    heat_content_tanom = cdm.createVariable(heat_content_tanom,
                                            id='heat_content_tanom')
    heat_content_tanom.setAxis(0, so.getAxis(0))
    heat_content_tanom.setAxis(1, so.getAxis(1))
    heat_content_tanom.setAxis(2, so.getAxis(2))
    heat_content_tanom.units = 'J'
    heat_content_tsanom = cdm.createVariable(heat_content_tsanom,
                                             id='heat_content_tsanom')
    heat_content_tsanom.setAxis(0, so.getAxis(0))
    heat_content_tsanom.setAxis(1, so.getAxis(1))
    heat_content_tsanom.setAxis(2, so.getAxis(2))
    heat_content_tsanom.units = 'J'

    # Create model-based depth index for subset target levels
    newdepth = np.array([
        5, 10, 20, 30, 40, 50, 75, 100, 125, 150, 200, 300, 500, 700, 1000,
        1500, 1800, 2000
    ]).astype('f')
    newdepth_bounds = np.array([[0, 5], [5, 10], [10, 20], [20, 30], [30, 40],
                                [40, 50], [50, 75], [75, 100], [100, 125],
                                [125, 150], [150, 200], [200, 300], [300, 500],
                                [500, 700], [700, 1000], [1000, 1500],
                                [1500, 1800], [1800, 2000]]).astype('f')
    #newdepth = np.array([200,300,500,700,1000,1500,1800,2000]).astype('f');
    #newdepth_bounds = np.array([[0,200],[200,300],[300,500],[500,700],[700,1000],[1000,1500],[1500,1800],[1800,2000]]).astype('f')

    # Interpolate to depths
    so_depthInterp = cdu.linearInterpolation(so,
                                             pressure_levels,
                                             levels=newdepth)
    temp_depthInterp = cdu.linearInterpolation(temp,
                                               pressure_levels,
                                               levels=newdepth)
    steric_height_depthInterp = cdu.linearInterpolation(steric_height,
                                                        pressure_levels,
                                                        levels=newdepth)
    steric_height_anom_depthInterp = cdu.linearInterpolation(
        steric_height_anom, pressure_levels, levels=newdepth)
    steric_height_thermo_anom_depthInterp = cdu.linearInterpolation(
        steric_height_thermo_anom, pressure_levels, levels=newdepth)
    steric_height_halo_anom_depthInterp = cdu.linearInterpolation(
        steric_height_halo_anom, pressure_levels, levels=newdepth)
    steric_height_halo_anom2_depthInterp = cdu.linearInterpolation(
        steric_height_halo_anom2, pressure_levels, levels=newdepth)
    heat_content_sanom_depthInterp = cdu.linearInterpolation(
        heat_content_sanom, pressure_levels, levels=newdepth)
    heat_content_tanom_depthInterp = cdu.linearInterpolation(
        heat_content_tanom, pressure_levels, levels=newdepth)
    heat_content_tsanom_depthInterp = cdu.linearInterpolation(
        heat_content_tanom, pressure_levels, levels=newdepth)

    # Fix masks - applied before cdms variables are created otherwise names/ids/attributes are reset
    temp_depthInterp = scrubNaNAndMask(temp_depthInterp, so_depthInterp)
    steric_height_depthInterp = scrubNaNAndMask(steric_height_depthInterp,
                                                so_depthInterp)
    steric_height_anom_depthInterp = scrubNaNAndMask(
        steric_height_anom_depthInterp, so_depthInterp)
    steric_height_thermo_anom_depthInterp = scrubNaNAndMask(
        steric_height_thermo_anom_depthInterp, so_depthInterp)
    steric_height_halo_anom_depthInterp = scrubNaNAndMask(
        steric_height_halo_anom_depthInterp, so_depthInterp)
    steric_height_halo_anom2_depthInterp = scrubNaNAndMask(
        steric_height_halo_anom2_depthInterp, so_depthInterp)
    heat_content_sanom_depthInterp = scrubNaNAndMask(
        heat_content_sanom_depthInterp, so_depthInterp)
    heat_content_tanom_depthInterp = scrubNaNAndMask(
        heat_content_tanom_depthInterp, so_depthInterp)
    heat_content_tsanom_depthInterp = scrubNaNAndMask(
        heat_content_tsanom_depthInterp, so_depthInterp)

    # Fix bounds
    newdepth = so_depthInterp.getAxis(0)
    newdepth.setBounds(newdepth_bounds)
    del (newdepth_bounds)
    newdepth.id = 'depth2'
    newdepth.units_long = 'decibar (pressure)'
    newdepth.positive = 'down'
    newdepth.long_name = 'sea_water_pressure'
    newdepth.standard_name = 'sea_water_pressure'
    newdepth.units = 'decibar'
    newdepth.axis = 'Z'

    # Assign corrected bounds
    so_depthInterp.setAxis(0, newdepth)
    temp_depthInterp.setAxis(0, newdepth)
    steric_height_depthInterp.setAxis(0, newdepth)
    steric_height_anom_depthInterp.setAxis(0, newdepth)
    steric_height_thermo_anom_depthInterp.setAxis(0, newdepth)
    steric_height_halo_anom_depthInterp.setAxis(0, newdepth)
    steric_height_halo_anom2_depthInterp.setAxis(0, newdepth)
    heat_content_sanom_depthInterp.setAxis(0, newdepth)
    heat_content_tanom_depthInterp.setAxis(0, newdepth)
    heat_content_tsanom_depthInterp.setAxis(0, newdepth)

    # Average/integrate to surface - configure bounds
    # Preallocate arrays
    so_depthAve = np.ma.zeros([len(newdepth), shape(so)[1], shape(so)[2]])
    temp_depthAve = so_depthAve.copy()
    heat_content_sanom_depthInteg = so_depthAve.copy()
    heat_content_tanom_depthInteg = so_depthAve.copy()
    heat_content_tsanom_depthInteg = so_depthAve.copy()
    for count, depth in enumerate(newdepth):
        tmp = cdu.averager(so_depthInterp[0:(count + 1), ...],
                           axis=0,
                           weights='weighted',
                           action='average')
        so_depthAve[count, ] = tmp
        tmp = cdu.averager(temp_depthInterp[0:(count + 1), ...],
                           axis=0,
                           weights='weighted',
                           action='average')
        temp_depthAve[count, ] = tmp
        tmp = cdu.averager(heat_content_sanom_depthInterp[0:(count + 1), ...],
                           axis=0,
                           weights='weighted',
                           action='sum')
        heat_content_sanom_depthInteg[count, ] = tmp
        tmp = cdu.averager(heat_content_tanom_depthInterp[0:(count + 1), ...],
                           axis=0,
                           weights='weighted',
                           action='sum')
        heat_content_tanom_depthInteg[count, ] = tmp
        tmp = cdu.averager(heat_content_tsanom_depthInterp[0:(count + 1), ...],
                           axis=0,
                           weights='weighted',
                           action='sum')
        heat_content_tsanom_depthInteg[count, ] = tmp
    del (heat_content_tanom_depthInterp, heat_content_tsanom_depthInterp)
    gc.collect()

    # Fix masks - applied before cdms variables are created otherwise names/ids/attributes are reset
    so_depthAve = scrubNaNAndMask(so_depthAve, so_depthInterp)
    temp_depthAve = scrubNaNAndMask(temp_depthAve, so_depthInterp)
    heat_content_sanom_depthInteg = scrubNaNAndMask(
        heat_content_sanom_depthInteg, so_depthInterp)
    heat_content_tanom_depthInteg = scrubNaNAndMask(
        heat_content_tanom_depthInteg, so_depthInterp)
    heat_content_tsanom_depthInteg = scrubNaNAndMask(
        heat_content_tsanom_depthInteg, so_depthInterp)
    del (so_depthInterp)

    # Convert numpy arrays to cdms objects
    heat_content_sanom_depthInteg = cdm.createVariable(
        heat_content_sanom_depthInteg, id='heat_content_sanom_depthInteg')
    heat_content_sanom_depthInteg.id = 'heat_content_sanom_depthInteg'
    heat_content_sanom_depthInteg.setAxis(0, newdepth)
    heat_content_sanom_depthInteg.setAxis(1, so.getAxis(1))
    heat_content_sanom_depthInteg.setAxis(2, so.getAxis(2))
    heat_content_sanom_depthInteg.units = 'J'
    heat_content_tanom_depthInteg = cdm.createVariable(
        heat_content_tanom_depthInteg, id='heat_content_tanom_depthInteg')
    heat_content_tanom_depthInteg.id = 'heat_content_tanom_depthInteg'
    heat_content_tanom_depthInteg.setAxis(0, newdepth)
    heat_content_tanom_depthInteg.setAxis(1, so.getAxis(1))
    heat_content_tanom_depthInteg.setAxis(2, so.getAxis(2))
    heat_content_tanom_depthInteg.units = 'J'
    heat_content_tsanom_depthInteg = cdm.createVariable(
        heat_content_tsanom_depthInteg, id='heat_content_tsanom_depthInteg')
    heat_content_tsanom_depthInteg.id = 'heat_content_tsanom_depthInteg'
    heat_content_tsanom_depthInteg.setAxis(0, newdepth)
    heat_content_tsanom_depthInteg.setAxis(1, so.getAxis(1))
    heat_content_tsanom_depthInteg.setAxis(2, so.getAxis(2))
    heat_content_tsanom_depthInteg.units = 'J'
    so_depthAve = cdm.createVariable(so_depthAve, id='so_depthAve')
    so_depthAve.id = 'so_depthAve'
    so_depthAve.setAxis(0, newdepth)
    so_depthAve.setAxis(1, so.getAxis(1))
    so_depthAve.setAxis(2, so.getAxis(2))
    so_depthAve.units = '1e-3'
    temp_depthAve = cdm.createVariable(temp_depthAve, id='temp_depthAve')
    temp_depthAve.id = 'temp_depthAve'
    temp_depthAve.setAxis(0, newdepth)
    temp_depthAve.setAxis(1, so.getAxis(1))
    temp_depthAve.setAxis(2, so.getAxis(2))
    temp_depthAve.units = 'degrees_C'
    steric_height_depthInterp = cdm.createVariable(
        steric_height_depthInterp, id='steric_height_depthInterp')
    steric_height_depthInterp.setAxis(0, newdepth)
    steric_height_depthInterp.setAxis(1, so.getAxis(1))
    steric_height_depthInterp.setAxis(2, so.getAxis(2))
    steric_height_depthInterp.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_anom_depthInterp = cdm.createVariable(
        steric_height_anom_depthInterp, id='steric_height_anom_depthInterp')
    steric_height_anom_depthInterp.setAxis(0, newdepth)
    steric_height_anom_depthInterp.setAxis(1, so.getAxis(1))
    steric_height_anom_depthInterp.setAxis(2, rho.getAxis(2))
    steric_height_anom_depthInterp.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_thermo_anom_depthInterp = cdm.createVariable(
        steric_height_thermo_anom_depthInterp,
        id='steric_height_thermo_anom_depthInterp')
    steric_height_thermo_anom_depthInterp.setAxis(0, newdepth)
    steric_height_thermo_anom_depthInterp.setAxis(1, so.getAxis(1))
    steric_height_thermo_anom_depthInterp.setAxis(2, so.getAxis(2))
    steric_height_thermo_anom_depthInterp.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_halo_anom_depthInterp = cdm.createVariable(
        steric_height_halo_anom_depthInterp,
        id='steric_height_halo_anom_depthInterp')
    steric_height_halo_anom_depthInterp.setAxis(0, newdepth)
    steric_height_halo_anom_depthInterp.setAxis(1, so.getAxis(1))
    steric_height_halo_anom_depthInterp.setAxis(2, so.getAxis(2))
    steric_height_halo_anom_depthInterp.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    steric_height_halo_anom2_depthInterp = cdm.createVariable(
        steric_height_halo_anom2_depthInterp,
        id='steric_height_halo_anom2_depthInterp')
    steric_height_halo_anom2_depthInterp.setAxis(0, newdepth)
    steric_height_halo_anom2_depthInterp.setAxis(1, so.getAxis(1))
    steric_height_halo_anom2_depthInterp.setAxis(2, so.getAxis(2))
    steric_height_halo_anom2_depthInterp.units = 'm^3 kg^-1 Pa (dynamic decimeter)'
    # Cleanup workspace
    del (newdepth)
    gc.collect()

    # Write variables to file
    if os.path.isfile(outFileName):
        os.remove(outFileName)
    filehandle = cdm.open(outFileName, 'w')
    # Global attributes
    globalAttWrite(filehandle, options=None)
    # Use function to write standard global atts
    # Write seawater version
    filehandle.seawater_library_version = sw.__version__
    # Write makeSteric version
    makeStericPath = str(makeSteric.__code__).split(' ')[6]
    makeStericPath = replace(replace(makeStericPath, '"', ''), ',', '')
    # Clean scraped path
    filehandle.makeSteric_version = ' '.join(getGitInfo(makeStericPath)[0:3])
    # Master variables
    filehandle.write(so.astype('float32'))
    filehandle.write(so_chg.astype('float32'))
    filehandle.write(so_depthAve.astype('float32'))
    filehandle.write(temp.astype('float32'))
    filehandle.write(temp_chg.astype('float32'))
    filehandle.write(temp_depthAve.astype('float32'))
    # Derived variables
    filehandle.write(cp.astype('float32'))
    filehandle.write(cp_halo.astype('float32'))
    filehandle.write(cp_thermo.astype('float32'))
    filehandle.write(rho.astype('float32'))
    filehandle.write(rho_halo.astype('float32'))
    filehandle.write(rho_thermo.astype('float32'))
    filehandle.write(heat_content.astype('float32'))
    filehandle.write(heat_content_sanom.astype('float32'))
    filehandle.write(heat_content_sanom_depthInteg.astype('float32'))
    filehandle.write(heat_content_tanom.astype('float32'))
    filehandle.write(heat_content_tanom_depthInteg.astype('float32'))
    filehandle.write(heat_content_tsanom.astype('float32'))
    filehandle.write(heat_content_tsanom_depthInteg.astype('float32'))
    filehandle.write(steric_height.astype('float32'))
    filehandle.write(steric_height_depthInterp.astype('float32'))
    filehandle.write(steric_height_anom.astype('float32'))
    filehandle.write(steric_height_anom_depthInterp.astype('float32'))
    filehandle.write(steric_height_halo_anom.astype('float32'))
    filehandle.write(steric_height_halo_anom2.astype('float32'))
    filehandle.write(steric_height_halo_anom_depthInterp.astype('float32'))
    filehandle.write(steric_height_halo_anom2_depthInterp.astype('float32'))
    filehandle.write(steric_height_thermo_anom.astype('float32'))
    filehandle.write(steric_height_thermo_anom_depthInterp.astype('float32'))
    filehandle.close()
    # Cleanup workspace
    del (outFileName)
    gc.collect()
Example #11
0
if not os.path.exists(path):
    raise ValueError("matlab seawater path %s not found" % path)

_ = octave.addpath(octave.genpath(path))

kw = dict(comment='#', header=5, index_col=0)
st61 = read_csv('Endeavor_Cruise-88_Station-61.csv', **kw)
st64 = read_csv('Endeavor_Cruise-88_Station-64.csv', **kw)
latst = 36. + 40.03 / 60., 37. + 39.93 / 60.
lonst = -(70. + 59.59 / 60.), -71.

Sal=np.c_[st61['S'].values, st64['S'].values]
Temp=np.c_[st61['t'].values, st64['t'].values]
Pres=np.c_[st61.index.values.astype(float), st64.index.values.astype(float)]
Gpan = sw.gpan(Sal, Temp, Pres)

def compare_results(name, function, args):
    args = [values.get(arg) for arg in args]

    try:  # Python.
        res = function(*args)
    except:
        print('%s: python runtime error' % name)
        raise
        return 'no_python'

    # FIXME: Testing only the first output when multiple outputs are present.
    nout = 1
    if isinstance(res, tuple):
        nout = len(res)
Example #12
0
    lasty = Y[i,-1]; yaux = np.linspace(lasty, lasty, rpt)
    YAUX[i,:] = yaux

# coordinates:
X  = np.hstack((X, XAUX))
Y  = np.hstack((Y, YAUX))

# velocity:

# computing geostrophic velocity to EASTERN boundary, to check SEC structure
temp = np.squeeze(TEMP[...,-1])
salt = np.squeeze(SALT[...,-1])
y    = Y[:,-1] 
y, z = np.meshgrid(y,Z); z = -z

gp   = sw.gpan(salt, temp, z)
gp   = (gp - gp[-1,:]) * -1 # to reference in the bottom
 
dgp  = np.array(np.gradient(gp))
dgp  = np.squeeze(dgp[1,:,:])

dy   = np.array(np.gradient(y))
dy   = np.squeeze(dy[1,:,:]) * 111000

dgpdy = dgp / dy 

usec  = -dgpdy / f0

# getting the right transport
usec = usec*0.4
f = np.where(usec > 0); usec[f] = 0