예제 #1
0
def y_grad_GridCor_Rho(RomsFile, RomsGrd, varname):
    """
    Compute Grid Correction for gradients in y direction
    
    """
    #load roms file
    RomsNC = nc4(RomsFile, 'r')

    #check if variable supplied or needs to load
    if type(varname) == str:
        _var = RomsNC.variables[varname][:]
    else:
        _var = varname

    #get land mask
    Mask_rho = ma.getmask(_var)
    Mask_w = ma.concatenate((Mask_rho, Mask_rho[:, -2:-1, :, :]), axis=1)

    #compute depth at rho points
    _rhodepth = ma.array(dep._set_depth_T(RomsFile, None, 'rho', \
                                          RomsNC.variables['h'][:], \
                                          RomsNC.variables['zeta'][:]), mask = Mask_rho)

    #compute depth at w points
    _wdepth = ma.array(dep._set_depth_T(RomsFile, None, 'w', \
                                        RomsNC.variables['h'][:], \
                                        RomsNC.variables['zeta'][:]), mask = Mask_w)

    #depth difference in vertical [rho points]
    dz_z = np.diff(_wdepth, n=1, axis=1)

    #depth difference between adjacent rho points [v points]
    _dz_y = np.diff(_rhodepth, n=1, axis=2)

    #shift to rho points
    dz_y = GridShift.Vpt_to_Rho(_dz_y)

    #compute difference [w points]
    _dvar_z = ma.diff(_var, n=1, axis=1)

    #shift to rho points
    dvar_z = GridShift.Wpt_to_Rho(_dvar_z)

    #distance between rho points in x and y directions
    _y_dist = rt.rho_dist_grd(RomsGrd)[1]

    #repeat over depth and time space and add mask
    dy = ma.array(rt.AddDepthTime(RomsFile, _y_dist), mask=Mask_rho)

    #vertical gradient [rho points]
    dvar_dz = dvar_z / dz_z

    #correction for roms grid
    dv_dyCor = dvar_dz * (dz_y / dy)

    return dv_dyCor
예제 #2
0
def x_grad_GridCor_Rho(RomsFile, RomsGrd, varname):
    """
    Compute Grid Correction for gradients in x direction on rho points
    
    """
    #load roms file
    RomsNC = nc4(RomsFile, 'r')

    #check if variable is suppled or need to be loaded
    if type(varname) == str:
        _var = RomsNC.variables[varname][:]
    else:
        _var = varname

    #get land mask
    Mask_rho = ma.getmask(_var)
    Mask_w = ma.concatenate((Mask_rho, Mask_rho[:, -2:-1, :, :]), axis=1)

    #compute depth at rho and w points
    _rhodepth = ma.array(dep._set_depth_T(RomsFile, None, 'rho', \
                                          RomsNC.variables['h'][:], \
                                          RomsNC.variables['zeta'][:]), mask = Mask_rho)

    _wdepth = ma.array(dep._set_depth_T(RomsFile, None, 'w', \
                                        RomsNC.variables['h'][:], \
                                        RomsNC.variables['zeta'][:]), mask = Mask_w)

    #depth difference in vertical [rho points]
    dz_z = np.diff(_wdepth, n=1, axis=1)

    #depth difference of adjacent rho points [u points]
    _dz_x = np.diff(_rhodepth, n=1, axis=3)

    #shift to rho points
    dz_x = GridShift.Upt_to_Rho(_dz_x)

    # compute vertical differential [on w points]
    _dvar_z = np.diff(_var, n=1, axis=1)

    # shift to rho points
    dvar_z = GridShift.Wpt_to_Rho(_dvar_z)

    #distance between rho points in x direction
    _x_dist = rt.rho_dist_grd(RomsGrd)[0]

    #repeat over depth and time space & apply mask
    dx = ma.array(rt.AddDepthTime(RomsFile, _x_dist), mask=Mask_rho)

    #vertical gradient on rho points
    dvar_dz = dvar_z / dz_z

    #correction for roms grid
    dv_dxCor = dvar_dz * (dz_x / dx)

    return dv_dxCor
예제 #3
0
def dA(RomsFile, RomsGrd):
    """
    Compute area of vertical grid cell faces 
    (Ax -> lat X depth, Ay -> lon X depth)
    """
    RomsNC = nc4(RomsFile, 'r')

    #depth at w points
    depth_w = dep._set_depth_T(RomsFile, None, 'w', RomsNC.variables['h'][:],\
                               RomsNC.variables['zeta'][:])
    dz_w = np.diff(depth_w, n=1, axis=1)

    #average depth at w points to u points
    dz_u = 0.5 * (dz_w[:, :, :, 0:dz_w.shape[3] - 1] +
                  dz_w[:, :, :, 1:dz_w.shape[3]])

    #average depth at w points to v points
    dz_v = 0.5 * (dz_w[:, :, 0:dz_w.shape[2] - 1, :] +
                  dz_w[:, :, 1:dz_w.shape[2], :])

    #cell widths
    dx0, dy0 = rt.cell_width(RomsGrd)

    #expand over depth and time dimensions
    dx = rt.AddDepthTime(RomsFile, dx0)
    dy = rt.AddDepthTime(RomsFile, dy0)

    #Area of face with x-normal
    Ax_norm = dz_u * dy

    #Area of face with y-normal
    Ay_norm = dz_v * dx

    return Ax_norm, Ay_norm
예제 #4
0
def z_grad(RomsFile, varname):
    """
    Compute z-gradient assuming rectangular grid cells
    """
    #load roms file
    RomsNC = nc4(RomsFile, 'r')

    if type(varname) == str:
        #load variable
        var = RomsNC.variables[varname][:]

    else:
        var = varname

    #vertical difference
    dvar_w = np.diff(var, n=1, axis=1)

    #shift w points to rho points
    dvar = GridShift.Wpt_to_Rho(dvar_w)

    #depth on w points
    depth = dep._set_depth_T(RomsFile, None, 'w', \
                             RomsNC.variables['h'], RomsNC.variables['zeta'])

    #difference in depth on rho points
    d_dep = np.diff(depth, n=1, axis=1)

    #compute gradient
    dvar_dz = dvar / d_dep

    return dvar_dz
예제 #5
0
def dA_int_w(RomsFile):
    """ Compute differential area of each cell interpolating depth at w points to u, v points """
    RomsNC = nc4(RomsFile, 'r')

    #depth at w points
    depth_w = dep._set_depth_T(RomsFile, None, 'w', RomsNC.variables['h'][:],\
                               RomsNC.variables['zeta'][:])
    dz_w = np.diff(depth_w, n=1, axis=1)

    # lon at rho points
    lon_rho0 = rt.AddDepthTime(RomsFile, \
                               RomsNC.variables['lon_rho'][:, 0:dz_w.shape[3]-1])
    lon_rho1 = rt.AddDepthTime(RomsFile, \
                               RomsNC.variables['lon_rho'][:, 1:dz_w.shape[3]])

    # lon at u point
    lon_u = rt.AddDepthTime(RomsFile, RomsNC.variables['lon_u'][:])

    #dz at rho points
    z_rho0 = dz_w[:, :, 0:dz_w.shape[2] - 1]
    z_rho1 = dz_w[:, :, 1:dz_w.shape[2]]

    #interpolation to u points
    dz_u = z_rho0 + (z_rho1 - z_rho0) / (lon_rho1 - lon_rho0) * (lon_u -
                                                                 lon_rho0)

    # depth at v points
    # lats
    lat_rho0 = RomsNC.variables['lat_rho'][0:dz_w.shape[1] - 1, :]
    lat_rho1 = RomsNC.variables['lat_rho'][1:dz_w.shape[1], :]
    lat_v = RomsNC.variables['lat_v'][:]

    #depths at w points
    z_Vrho0 = dz_w[:, 0:dz_w.shape[1] - 1, :]
    z_Vrho1 = dz_w[:, 1:dz_w.shape[1], :]

    #interpolate between rho points, u
    dz_v = z_Vrho0 - (z_Vrho1 - z_Vrho0) / (lat_rho1 - lat_rho0) * (lat_v -
                                                                    lat_rho0)

    #sides of cells
    dx = np.repeat(1 / np.array(RomsNC.variables['pm'])[np.newaxis, :, :],
                   dz_v.shape[0],
                   axis=0)
    dx = dx[:, 0:dz_v.shape[1], 0:dz_v.shape[2]]
    dy = np.repeat(1 / np.array(RomsNC.variables['pn'])[np.newaxis, :, :],
                   dz_u.shape[0],
                   axis=0)
    dy = dy[:, 0:dz_u.shape[1], 0:dz_u.shape[2]]

    #compute area
    A_xz = dx * dz_v
    A_yz = dy * dz_u

    return A_xz, A_yz
예제 #6
0
def x_grad_GridCor00(RomsFile, RomsGrd, varname) :
    """
    compute gradient in x (lon) direction
    """
    #load roms file
    RomsNC = dt(RomsFile, 'r')
    
    #load variable and compute differential
    var = RomsNC.variables[varname][:]
    dvar_x = np.diff(var, n = 1, axis = 3)
    dvar_z = np.diff(var, n = 1, axis = 1)
    
    #compute depth at rho points
    depth = dep._set_depth_T(RomsFile, None, 'rho', RomsNC.variables['h'],RomsNC.variables['zeta'])
       
    #distance between rho points in x and y directions
    x_dist = dist(RomsGrd)[0]
    
    #repeat over depth and time space
    _DX = np.repeat(np.array(x_dist)[np.newaxis, :, :], depth.shape[1], axis = 0)
    dx = np.repeat(np.array(_DX)[np.newaxis, :, :, :], depth.shape[0], axis = 0)
    
    #depth difference between adjacent rho points
    dz_x = np.diff(depth, n = 1, axis = 3)
    
    #vertical derivative
    dz_z = np.diff(depth, n = 1, axis = 1)
    dp_dz0 = dvar_z/dz_z
    dp_dz = 0.5*(dp_dz0[:,:,:,0:_dp_dz.shape[3]-1] + dp_dz0[:,:,:, 1:dp_dz0.shape[3]])
    
    #distance between adjacent rho points
    dl = np.sqrt(dx*dx + dz*dz)
    
    #correction for roms grid
    dp_dl = dvar/dl*dl/dx
    
    #gradient
    dp_dx = dvar/dx
    
    rat = np.abs(dp_dl[0,:,:,:])/np.abs(dp_dx[0,:,:,:])
    
    grat = dl[0,:,:,:]/dx[0,:,:,:]
    
    stat = np.array(rat).flatten()
예제 #7
0
def dV(RomsFile):
    """ Load full roms grid and compute differential volume of each cell"""
    RomsNC = nc4(RomsFile, 'r')

    #compute depth at w points
    depth_domain = dep._set_depth_T(RomsFile, None, 'w',\
                                    RomsNC.variables['h'][:],\
                                    RomsNC.variables['zeta'][:])

    dz = np.diff(depth_domain, n=1, axis=1)

    #compute lengths of horizontal cell directions & repeat over depth space
    dx = rt.AddDepthTime(RomsFile, 1 / np.array(RomsNC.variables['pm'][:]))
    dy = rt.AddDepthTime(RomsFile, 1 / np.array(RomsNC.variables['pn'][:]))

    #compute differential volume of each cell
    DV = dx * dy * dz

    return DV
        var_prime[n, :, :, :] = var_4dim[n, :, :, :] - ma.mean(
            var_4dim[n, :, :, :])

    return var_prime


#load salt_rate from diagnostic file
Diag = nc4(DiagFile, 'r')
salt_rate = Diag.variables['salt_rate'][:]

#from average file, load salt and vertical thickness
Avg = nc4(AvgFile, 'r')
salt = Avg.variables['salt'][:]

#Compute vertical thickness at w points
depth_domain = dep._set_depth_T(AvgFile, None, 'w', Avg.variables['h'][:],\
                                Avg.variables['zeta'][:])

delta = ma.diff(depth_domain, n=1, axis=1)

#from history files compute time derivative of thickness of cell
Hist = nc4(HistFile, 'r')
depth_hist = dep._set_depth_T(HistFile, None, 'w', Hist.variables['h'][:], \
                              Hist.variables['zeta'][:])

delta_hist = ma.diff(depth_hist, n=1, axis=1)

ddelta_dt = ma.diff(delta_hist, \
                     n = 1, axis = 0)/ma.diff(Hist.variables['ocean_time'][:],\
                                    n = 1, axis = 0)

#time deriv of salt, less change in cell volume
예제 #9
0
#remove mask
Xratio = Xratio[~Xratio.mask]
Yratio = Yratio[~Yratio.mask]

X_good = Xratio !=0
Y_good = Yratio !=0

xlog = ma.log10(Xratio[X_good])
ylog = ma.log10(Yratio[Y_good])

#depth
romsvars = {'h' : RomsNC.variables['h'][:], \
            'zeta' : RomsNC.variables['zeta'][:]}
    
#compute depth at rho points
depth = dep._set_depth_T(RomsFile, None, 'rho', romsvars['h'], romsvars['zeta'])
depth = ma.array(depth, mask = Land).flatten()

_dep = depth[X_good]
depthLR = _dep[xlog > 1]
depthLR = depthLR[~depthLR.mask]


#log median ration, skipping zeros
for n in range(DM_ratx.shape[0]) :
    ind = DM_ratx[n,:] != 0
    DM_ratx[n,ind] = ma.log10(DM_ratx[n,ind])

for n in range(DM_raty.shape[0]) :
    ind = DM_raty[n,:] != 0
    DM_raty[n,ind] = np.log10(DM_raty[n, ind])
예제 #10
0
@author: Jasen
"""
import os

os.chdir('/Users/Jasen/Documents/GitHub/ROMS_Budget/')
from netCDF4 import Dataset as nc4
import numpy.ma as ma

import obs_depth_JJ as dep

DiaFile = '/Users/Jasen/Documents/Data/ROMS_WCOF/DiagnosticFiles/ocean_dia_jasen.nc'
AvgFile = '/Users/Jasen/Documents/Data/ROMS_WCOF/DiagnosticFiles/ocean_avg_jasen.nc'
GrdFile = '/Users/Jasen/Documents/Data/ROMS_WCOF/DiagnosticFiles/grd_wcofs4_visc200_50-50-50_diff200_spherical.nc'

DiaNC = nc4(DiaFile, 'r')
AvgNC = nc4(AvgFile, 'r')
GrdNC = nc4(GrdFile, 'r')

#variable from average file
salt = AvgNC.variables['salt'][:]

#depth at w points
romsvars = {'h' : AvgNC.variables['h'][:], \
                'zeta' : AvgNC.variables['zeta'][:]}

# update to include Vstretching = 4 (spherical?)
depth_w = dep._set_depth_T(AvgFile, None, 'w', romsvars['h'], romsvars['zeta'])

delta = ma.diff(depth_w, n=1, axis=1)

#variables from