def y_grad_rho(RomsFile, RomsGrd, varname): """ Compute y-gradient assuming rectangular grid cells """ if type(varname) == str: #load roms file RomsNC = nc4(RomsFile, 'r') #load variable _var = RomsNC.variables[varname][:] else: _var = varname #get mask Mask = ma.getmask(_var) #compute difference dvar_v = np.diff(_var, n=1, axis=2) #shift from v points to rho points dvar = GridShift.Vpt_to_Rho(dvar_v) #lon positions [meters] y_dist = rt.rho_dist_grd(RomsGrd)[1] #repeat over depth and time space and apply mask dy = ma.array(rt.AddDepthTime(RomsFile, y_dist), mask=Mask) #compute gradient dvar_dy = dvar / dy return dvar_dy
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
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
def TermOne(RomsFile, Mask, Variance) : """ Compute the change of variance within a control volume """ #ocean time RomsNC = nc4(RomsFile, 'r') time = RomsNC.variables['ocean_time'] #differential volume dv = df.dV(RomsFile) dv = ma.array(dv, mask = Mask) #multiply to get integrad S_m = Variance*dv #integrate volume at each time step S_int = np.empty(S_m.shape[0]) S_int.fill(np.nan) for n in range(S_int.shape[0]) : S_int[n] = np.sum(S_m[n, :, :, :]) #time derivative dVar_dt = np.diff(S_int)/np.diff(time) return dVar_dt
def RhoUV_Mask(RomsFile, latbounds, lonbounds) : """ defines masks on rho, u, and v points """ RomsNC = nc4(RomsFile, 'r') Rholats = (RomsNC.variables['lat_rho'][:] >= latbounds[0])*(RomsNC.variables['lat_rho'][:] <= latbounds[1]) Rholons = (RomsNC.variables['lon_rho'][:] >= lonbounds[0])*(RomsNC.variables['lon_rho'][:] <= lonbounds[1]) #define rho mask Rholats = (RomsNC.variables['lat_rho'][:] >= latbounds[0])*(RomsNC.variables['lat_rho'][:] <= latbounds[1]) Rholons = (RomsNC.variables['lon_rho'][:] >= lonbounds[0])*(RomsNC.variables['lon_rho'][:] <= lonbounds[1]) Rho_Mask = np.ma.asarray(AddDepthTime(RomsFile, np.invert(Rholats*Rholons))) #offsets for c - grid c_offset_v = np.round(RomsNC.variables['lat_v'][0, 0] - RomsNC.variables['lat_rho'][0,0], decimals = 2) c_offset_u = np.round(RomsNC.variables['lon_u'][0,0] - RomsNC.variables['lon_rho'][0,0], decimals = 2) #define u mask Ulats = (RomsNC.variables['lat_u'][:] >= latbounds[0])*(RomsNC.variables['lat_u'][:] <= latbounds[1]) Ulons = (RomsNC.variables['lon_u'][:] >= lonbounds[0]-c_offset_u)*(RomsNC.variables['lon_u'][:] <= lonbounds[1]-c_offset_u) U_Mask = np.ma.asarray(AddDepthTime(RomsFile, np.invert(Ulats*Ulons))) #define v mask Vlats = (RomsNC.variables['lat_v'][:] >= latbounds[0]+c_offset_v)*(RomsNC.variables['lat_v'][:] <= latbounds[1]+c_offset_v) Vlons = Rholons = (RomsNC.variables['lon_v'][:] >= lonbounds[0])*(RomsNC.variables['lon_v'][:] <= lonbounds[1]) V_Mask = np.ma.asarray(AddDepthTime(RomsFile, np.invert(Vlats*Vlons))) return Rho_Mask, U_Mask, V_Mask
def RhoPsiIndex_Mask(RomsFile, latbounds, lonbounds): """Locates indices of lat and lon within ROMS Output File with logical mask""" #load roms file RomsNC = nc4(RomsFile, 'r') var = RomsNC.variables['salt'][:] Rholats = (RomsNC.variables['lat_rho'][:] >= latbounds[0]) * (RomsNC.variables['lat_rho'][:] <= latbounds[1]) Rholons = (RomsNC.variables['lon_rho'][:] >= lonbounds[0]) * (RomsNC.variables['lon_rho'][:] <= lonbounds[1]) RHOMASK = np.invert(Rholats * Rholons) Psilats = (RomsNC.variables['lat_psi'][:] >= latbounds[0]) * (RomsNC.variables['lat_psi'][:] <= latbounds[1]) Psilons = (RomsNC.variables['lon_psi'][:] >= lonbounds[0]) * (RomsNC.variables['lon_psi'][:] <= lonbounds[1]) PSIMASK = np.invert(Psilats * Psilons) #repeat masks over depth and time dimensions _RM = np.repeat(np.array(RHOMASK)[np.newaxis, :, :], var.shape[1], axis=0) RhoMask = np.repeat(np.array(_RM)[np.newaxis, :, :, :], var.shape[0], axis=0) _PM = np.repeat(np.array(PSIMASK)[np.newaxis, :, :], var.shape[1], axis=0) PsiMask = np.repeat(np.array(_PM)[np.newaxis, :, :, :], var.shape[0], axis=0) return RhoMask, PsiMask
def x_grad_rho(RomsFile, RomsGrd, varname): """ Compute x-gradient assuming rectangular grid cells """ if type(varname) == str: #load roms file RomsNC = nc4(RomsFile, 'r') #load variable _var = RomsNC.variables[varname][:] else: _var = varname #get mask Mask = ma.getmask(_var) #gradient in x direction on u points dvar_u = np.diff(_var, n=1, axis=3) #shift u points to rho points dvar = GridShift.Upt_to_Rho(dvar_u) #lon positions [meters] x_dist = rt.rho_dist_grd(RomsGrd)[0] #repeat over depth and time space and apply mask dx = ma.array(rt.AddDepthTime(RomsFile, x_dist), mask=Mask) #compute gradient dvar_dx = dvar / dx return dvar_dx
def rho_dist_grd(RomsGrd): """ Compute distance between rho points using roms grid file Output on """ RomsNC = nc4(RomsGrd, 'r') #distance from grid edge Xx = RomsNC.variables['x_rho'][:] Yy = RomsNC.variables['y_rho'][:] #difference between points centered on cell edge dx_cell = np.diff(Xx, n=1, axis=1) dy_cell = np.diff(Yy, n=1, axis=0) #pad ends dx_pad = np.concatenate((dx_cell[:, 0:1], dx_cell, dx_cell[:, -2:-1]), axis=1) dy_pad = np.concatenate((dy_cell[0:1, :], dy_cell, dy_cell[-2:-1, :]), axis=0) #average to cell center dx = 0.5 * (dx_pad[:, 0:dx_pad.shape[1] - 1] + dx_pad[:, 1:dx_pad.shape[1]]) dy = 0.5 * (dy_pad[0:dy_pad.shape[0] - 1, :] + dy_pad[1:dy_pad.shape[0]]) return dx, dy
def ROMS_CV(varname, RomsFile, Mask): """ Load variable and apply mask """ RomsNC = nc4(RomsFile, 'r') Var = np.ma.array(RomsNC.variables[varname][:], mask = Mask) return Var
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
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
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
def AddDepthTime(RomsFile, var2D) : """ Add depth and time dimensions for lat x lon variables """ RomsNC = nc4(RomsFile, 'r') var = RomsNC.variables['salt'][:] #repeat over depth space _DepthSpace = np.repeat(np.array(var2D)[np.newaxis, :, :], \ var.shape[1], axis = 0) #repeat over time space TimeDepth = np.repeat(np.array(_DepthSpace)[np.newaxis, :, :, :],\ var.shape[0], axis = 0) return TimeDepth
def FaceMask(RomsFile, latbounds, lonbounds): """ Define masks along each face of control volume Returns dictionary for each face with masks on rho and u or v points """ RomsNC = nc4(RomsFile, 'r') #offsets for c - grid c_offset_lat = np.round(RomsNC.variables['lat_v'][0, 0] - RomsNC.variables['lat_rho'][0, 0], decimals=2) c_offset_lon = np.round(RomsNC.variables['lon_u'][0, 0] - RomsNC.variables['lon_rho'][0, 0], decimals=2) #north face v points with c-grid offset Vlats = (RomsNC.variables['lat_v'][:] == latbounds[0] + c_offset_lat) * ( RomsNC.variables['lat_v'][:] <= latbounds[1] + c_offset_lat) Vlons = (RomsNC.variables['lon_v'][:] >= lonbounds[0]) * (RomsNC.variables['lon_v'][:] <= lonbounds[1]) _NFv = np.invert(Vlats * Vlons) NorthFace = np.ma.asarray(AddDepthTime(RomsFile, _NFv)) #south face v points Vlats = (RomsNC.variables['lat_v'][:] >= latbounds[0] + c_offset_lat) * ( RomsNC.variables['lat_v'][:] == latbounds[1] + c_offset_lat) Vlons = (RomsNC.variables['lon_v'][:] >= lonbounds[0]) * (RomsNC.variables['lon_v'][:] <= lonbounds[1]) _SFv = np.invert(Vlats * Vlons) SouthFace = np.ma.asarray(AddDepthTime(RomsFile, _SFv)) #West face u points Ulats = (RomsNC.variables['lat_u'][:] >= latbounds[0]) * (RomsNC.variables['lat_u'][:] <= latbounds[1]) Ulons = (RomsNC.variables['lon_u'][:] == lonbounds[0] - c_offset_lon) * ( RomsNC.variables['lon_u'][:] <= lonbounds[1] - c_offset_lon) _WFu = np.invert(Ulats * Ulons) WestFace = np.ma.asarray(AddDepthTime(RomsFile, _WFu)) #East face u points Ulats = (RomsNC.variables['lat_u'][:] >= latbounds[0]) * (RomsNC.variables['lat_u'][:] <= latbounds[1]) Ulons = (RomsNC.variables['lon_u'][:] >= lonbounds[0] - c_offset_lon) * ( RomsNC.variables['lon_u'][:] == lonbounds[1] - c_offset_lon) _EFu = np.invert(Ulats * Ulons) EastFace = np.ma.asarray(AddDepthTime(RomsFile, _EFu)) return NorthFace, WestFace, SouthFace, EastFace
def x_grad_u(RomsFile, RomsGrd, varname): """ compute x-gradient on u points """ if type(varname) == str: #load roms file RomsNC = nc4(RomsFile, 'r') #load variable _var = RomsNC.variables[varname][:] else: _var = varname #[u points] #get mask Mask = ma.getmask(_var) #gradient in x direction [rho points] dvar_rho = ma.diff(_var, n=1, axis=3) #pad mask_pad = ma.notmasked_edges(dvar_rho, axis=3) dvar_ = dvar_rho dvar_[:, :, :, mask_pad[0][3][0] - 1] = dvar_rho[:, :, :, mask_pad[0][3][0]] dvar_[:, :, :, mask_pad[1][3][0] + 1] = dvar_rho[:, :, :, mask_pad[1][3][0]] dvar_pad = ma.concatenate((dvar_rho[:,:,:,0:1], dvar_rho, \ dvar_rho[:,:,:,-2:-1]), axis = 3) #shift to u points dvar_u = GridShift.Rho_to_Upt(dvar_pad) #dx [rho points] x_dist = rt.rho_dist_grd(RomsGrd)[0] #repeat over depth and time and apply mask _dx = rt.AddDepthTime(RomsFile, x_dist) dx = ma.array(GridShift.Rho_to_Upt(_dx), mask=Mask) #gradient dvar_dx = dvar_u / dx return dvar_dx
def TermFour(RomsFile, RomsGrd, variance) : """ Internal mixing """ RomsNC = nc4(RomsFile, 'r') #vertical viscosity coefficient K _Kv_w = RomsNC.variables['AKs'][:] #average to rho points Kv_rho = 0.5*(_Kv_w[:,0:_Kv_w.shape[1]-1, :, :] + _Kv_w[:, 1:_Kv_w.shape[1], :, :]) #apply mask Kv = ma.array(Kv_rho, mask = ma.getmask(variance)) #horizontal viscosity coefficient (constant) Kh = RomsNC.variables['nl_tnu2'][0] Mask = ma.getmask(variance) #gradients squared xgrad = ma.array(gr.x_grad_rho(RomsFile, RomsGrd, variance)**2, \ mask = Mask) ygrad = ma.array(gr.y_grad_rho(RomsFile, RomsGrd, variance)**2, \ mask = Mask) zgrad = ma.array(gr.z_grad(RomsFile, variance)**2, \ mask = Mask) # #differentail volume dV = ma.array(df.dV(RomsFile), mask = ma.getmask(variance)) #integrad x_m = 2*Kh*xgrad*dV y_m = 2*Kh*ygrad*dV z_m = 2*Kv*zgrad*dV #integrate mixing = np.empty(z_m.shape[0]) mixing.fill(np.nan) for n in range(mixing.shape[0]) : mixing[n] = ma.sum(z_m[n,:,:,:]) + ma.sum(x_m[n,:,:,:]) + ma.sum(y_m[n,:,:,:]) return mixing
def y_grad_v(RomsFile, RomsGrd, varname): """ Compute y-gradient on v points """ if type(varname) == str: #load roms file RomsNC = nc4(RomsFile, 'r') #load variable _var = RomsNC.variables[varname][:] else: _var = varname #[v points] #get mask Mask = ma.getmask(_var) #compute difference [rho points] dvar_rho = ma.diff(_var, n=1, axis=2) #pad mask_pad = ma.notmasked_edges(dvar_rho, axis=2) dvar_ = dvar_rho dvar_[:, :, mask_pad[0][2][0] - 1, :] = dvar_rho[:, :, mask_pad[0][2][0], :] dvar_[:, :, mask_pad[1][2][0] + 1, :] = dvar_rho[:, :, mask_pad[1][2][0], :] dvar_pad = ma.concatenate((dvar_[:,:,0:1, :],\ dvar_, \ dvar_[:,:,-2:-1, :]), axis = 2) #shift to V points dvar_v = GridShift.Rho_to_Vpt(dvar_pad) #dy y_dist = rt.rho_dist_grd(RomsGrd)[1] dy = rt.AddDepthTime(RomsFile, y_dist) dy_v = ma.array(GridShift.Rho_to_Vpt(dy), mask=Mask) #compute gradient dvar_dy = dvar_v / dy_v return dvar_dy
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
def dAz_psi(RomsFile): """Compute differential area of each cell using a trapazoid at psi points""" #load roms file RomsNC = nc4(RomsFile, 'r') #compute z romsvars = {'h' : RomsNC.variables['h'], \ 'zeta' : RomsNC.variables['zeta']} #compute depth at w points depth_domain = dep._set_depth(RomsFile, None, 'psi', romsvars['h'], romsvars['zeta']) dz = np.diff(depth_domain, n=1, axis=0) #compute lengths of horizontal cell directions repeat over depth axis dx = np.repeat(1 / np.array(RomsNC.variables['pm'])[np.newaxis, :, :], dz.shape[0], axis=0) dy = np.repeat(1 / np.array(RomsNC.variables['pn'])[np.newaxis, :, :], dz.shape[0], axis=0) #compute differential area assuming trapizoidal shape A_xz = np.empty(dz.shape) A_xz.fill(np.nan) A_yz = np.empty(dz.shape) A_yz.fill(np.nan) Lm = dz.shape[1] Mm = dz.shape[2] #x area and y area for k in range(1, dz.shape[1] - 1): A_xz[:, k - 1, :] = 0.5 * (dz[:, k, 0:Mm] + dz[:, k - 1, 0:Mm]) * dx[:, k - 1, 0:Mm] for k in range(1, dz.shape[2] - 1): A_yz[:, :, k - 1] = 0.5 * (dz[:, 0:Lm, k] + dz[:, 0:Lm, k - 1]) * dy[:, 0:Lm, k - 1] return A_xz, A_yz
def TermThree(RomsFile, RomsGrd, varname, latbounds, lonbounds): """ Diffusion of variance across open boundaries """ RomsNC = nc4(RomsFile, 'r') var = RomsNC.variables[varname][:] #shift variable from rho points to u and v points _var_u = 0.5 * (var[:, :, :, 0:var.shape[3] - 1] + var[:, :, :, 1:var.shape[3]]) _var_v = 0.5 * (var[:, :, 0:var.shape[2] - 1, :] + var[:, :, 1:var.shape[2], :]) #define masks for u and v points _, U_Mask, V_Mask = rt.RhoUV_Mask(RomsFile, latbounds, lonbounds) #compute variance var_u = ma.array(_var_u, mask=U_Mask) var_v = ma.array(_var_v, mask=V_Mask) #variance squared prime2_u = (var_u - ma.mean(var_u))**2 prime2_v = (var_v - ma.mean(var_v))**2 #define face masks NorthFace, WestFace, SouthFace, EastFace = rt.FaceMask(RomsFile,\ latbounds, lonbounds) #apply face masks to variance squared North_var = ma.array(prime2_v, mask=NorthFace) South_var = ma.array(prime2_v, mask=SouthFace) West_var = ma.array(prime2_u, mask=WestFace) East_var = ma.array(prime2_u, mask=EastFace) #compute differential areas Ax_norm, Ay_norm = df.dA(RomsFile, RomsGrd) #subset areas North_Ay = ma.array(Ay_norm, mask=NorthFace) West_Ax = ma.array(Ax_norm, mask=WestFace) South_Ay = ma.array(Ay_norm, mask=SouthFace) East_Ax = ma.array(Ax_norm, mask=EastFace) #load diffusion coef _Ks_w = RomsNC.variables['AKs'][:] #shift to u points Ks_u = GridShift.Wpt_to_Upt(_Ks_w) #shift to v points Ks_v = GridShift.Wpt_to_Vpt(_Ks_w) #apply face mask to east and west faces East_Ks = ma.array(Ks_u, mask=EastFace) West_Ks = ma.array(Ks_u, mask=WestFace) #apply face mask to north and south faces North_Ks = ma.array(Ks_v, mask=NorthFace) South_Ks = ma.array(Ks_v, mask=SouthFace) #integrad North = -1 * North_Ks * North_var * North_Ay South = South_Ks * South_var * South_Ay East = -1 * East_Ks * East_var * East_Ax West = West_Ks * West_var * West_Ax #integrate Diff = np.empty(var.shape[0]) Diff.fill(np.nan) for n in range(Diff.shape[0]): Diff[n] = ma.sum(North[n, :, :, :]) + ma.sum( South[n, :, :, :]) + ma.sum(West[n, :, :, :]) + ma.sum( East[n, :, :, :]) return Diff
@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)
def Term2(RomsFile, RomsGrd, varname, latbounds, lonbounds): """ Flux of variance across open boundaries """ RomsNC = nc4(RomsFile, 'r') var = RomsNC.variables[varname][:] #shift variable from rho points to u and v points _var_u = 0.5 * (var[:, :, :, 0:var.shape[3] - 1] + var[:, :, :, 1:var.shape[3]]) _var_v = 0.5 * (var[:, :, 0:var.shape[2] - 1, :] + var[:, :, 1:var.shape[2], :]) #define masks for u and v points _, U_Mask, V_Mask = rt.RhoUV_Mask(RomsFile, latbounds, lonbounds) #compute variance var_u = ma.array(_var_u, mask=U_Mask) var_v = ma.array(_var_v, mask=V_Mask) #variance squared prime2_u = (var_u - ma.mean(var_u))**2 prime2_v = (var_v - ma.mean(var_v))**2 #define face masks NorthFace, WestFace, SouthFace, EastFace = rt.FaceMask(RomsFile,\ latbounds, lonbounds) #apply face masks to variance squared North_var = ma.array(prime2_v, mask=NorthFace) South_var = ma.array(prime2_v, mask=SouthFace) West_var = ma.array(prime2_u, mask=WestFace) East_var = ma.array(prime2_u, mask=EastFace) #compute differential areas Ax_norm, Ay_norm = df.dA(RomsFile, RomsGrd) #subset areas North_Ay = ma.array(Ay_norm, mask=NorthFace) West_Ax = ma.array(Ax_norm, mask=WestFace) South_Ay = ma.array(Ay_norm, mask=SouthFace) East_Ax = ma.array(Ax_norm, mask=EastFace) #velocities North_V = ma.array(RomsNC.variables['v'][:], mask=NorthFace) West_U = ma.array(RomsNC.variables['u'][:], mask=WestFace) South_V = ma.array(RomsNC.variables['v'][:], mask=SouthFace) East_U = ma.array(RomsNC.variables['u'][:], mask=EastFace) #multiply to get integrad North = North_V * North_Ay * North_var West = West_U * West_Ax * West_var South = South_V * South_Ay * South_var East = East_U * East_Ax * East_var #sum/integrate each time step Flux = np.empty(North.shape[0]) Flux.fill(np.nan) for n in range(North.shape[0]): Flux[n] = ma.sum(North[n,:,:,:]) + ma.sum(South[n,:,:,:]) + \ ma.sum(East[n,:,:,:]) + ma.sum(West[n,:,:,:]) return Flux
import GridShift_3D as GridShift import PolyMask as mt import Differential_Tstep as dff import Gradients_Tstep as gr #import numpy.ma as ma #import Exact_Budget_Terms #bounds of control volume Vertices = np.array([[37.0, -123.0], [37.0, -122.5], [37.2, -122.5], [37.2, -123.0]]) #load files FilePath = '/home/cae/runs/jasen/wc15.a01.b03.hourlywindWT.windmcurrent.diags/out/' AvgFile = FilePath + 'ocean_avg_2014_0005.nc' RomsNC = nc4(AvgFile, 'r') HistFile = FilePath + 'ocean_his_2014_0005.nc' Hist = nc4(HistFile, 'r') Diag = nc4(FilePath + 'ocean_dia_2014_0005.nc', 'r') GridFile = '/home/ablowe/runs/ncfiles/grids/wc15.a01.b03_grd.nc' #variable lat = Avg.variables['lat_rho'][:] lon = Avg.variables['lon_rho'][:] salt = Avg.variables['salt'][0, 0, :, :] m = np.ma.getmask(salt) lats = np.ma.array(lat, mask=m) lons = np.ma.array(lon, mask=m)
import obs_depth_JJ as dep import Mask_Tools as mt import numpy.ma as ma import numpy as np from netCDF4 import Dataset as nc4 import Differential_Tstep as dff #bounds of control volume latbounds = [37.193, 37.378] lonbounds = [-122.7, -122.456] #load files FilePath = '/home/cae/runs/jasen/wc15.a01.b03.hourlywindWT.windmcurrent.diags/out/' HistFile = FilePath + 'ocean_his_2014_0005.nc' Hist = nc4(HistFile, 'r') AvgFile = FilePath + 'ocean_avg_2014_0005.nc' Avg = nc4(AvgFile, 'r') Diag = nc4(FilePath + 'ocean_dia_2014_0005.nc', 'r') Grd = nc4('/home/ablowe/runs/ncfiles/grids/wc15.a01.b03_grd.nc', 'r') #create CV mask RhoMask2D = mt.RhoMask(Avg, latbounds, lonbounds) RhoMask = np.repeat(np.array(RhoMask2D)[np.newaxis, :, :], Avg.variables['salt'].shape[1], axis=0) #load time for steping
def Prime(var_4dim): """ Compute anomaly of variable for each time step var_4dim = var_4dim[time, depth, lat, lon] """ var_prime = np.empty(var_4dim.shape) var_prime.fill(np.nan) for n in range(var_4dim.shape[0]): 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'][:], \
def TermThree(RomsFile, RomsGrd, varname, latbounds, lonbounds) : """ Diffusive Flux of variance across open boundaries """ RomsNC = nc4(RomsFile, 'r') var = RomsNC.variables[varname][:] #shift variable from rho points to u and v points _var_u = 0.5*(var[:,:, :, 0:var.shape[3]-1] + var[:,:, :, 1:var.shape[3]]) _var_v = 0.5*(var[:,:, 0:var.shape[2]-1, :] + var[:,:, 1:var.shape[2], :]) #define masks for u and v points _, U_Mask, V_Mask = rt.RhoUV_Mask(RomsFile, latbounds, lonbounds) #mask variable var_u = ma.array(_var_u, mask = U_Mask) var_v = ma.array(_var_v, mask = V_Mask) #variance squared prime2_u = (var_u - ma.mean(var_u))**2 prime2_v = (var_v - ma.mean(var_v))**2 #gradients of variance squared dprime_u_dx = gr.x_grad_u(RomsFile, RomsGrd, prime2_u) dprime_v_dy = gr.y_grad_v(RomsFile, RomsGrd, prime2_v) #define face masks NorthFace, WestFace, SouthFace, EastFace = rt.FaceMask(RomsFile,\ latbounds, lonbounds) #apply face masks to gradient of variance squared North_var = ma.array(dprime_v_dy, mask = NorthFace) South_var = ma.array(dprime_v_dy, mask = SouthFace) West_var = ma.array(dprime_u_dx, mask = WestFace) East_var = ma.array(dprime_u_dx, mask = EastFace) #compute differential areas Ax_norm, Ay_norm = df.dA(RomsFile, RomsGrd) #subset areas North_Ay = ma.array(Ay_norm, mask = NorthFace) West_Ax = ma.array(Ax_norm, mask = WestFace) South_Ay = ma.array(Ay_norm, mask = SouthFace) East_Ax = ma.array(Ax_norm, mask = EastFace) #load horizontal diffusion coef Ks = RomsNC.variables['nl_tnu2'][0] #integrad North = -1*Ks*North_var*North_Ay South = Ks*South_var*South_Ay East = -1*Ks*East_var*East_Ax West = Ks*West_var*West_Ax #integrate Diff = np.empty(var.shape[0]) Diff.fill(np.nan) for n in range(Diff.shape[0]): Diff[n] = ma.sum(North[n,:,:,:]) + ma.sum(South[n,:,:,:]) + ma.sum(West[n,:,:,:]) + ma.sum(East[n,:,:,:]) return Diff
import numpy as np import numpy.ma as ma import Gradients as gr import matplotlib.pyplot as plt from netCDF4 import Dataset as nc4 import ROMS_Tools_Mask as rt import obs_depth_JJ as dep #files RomsFile = '/Users/Jasen/Documents/Data/ROMS_TestRuns/wc12_his_5day.nc' RomsGrd = '/Users/Jasen/Documents/Data/ROMS_TestRuns/wc12_grd.nc.0' #load bathymetry RomsNC = nc4(RomsFile, 'r') bathy = RomsNC.variables['h'][:] #select variable Salt = RomsNC.variables['salt'][:] Prime2 = (Salt - ma.mean(Salt))**2 varname = Prime2 #horizontal gradients ds_dx = gr.x_grad_rho(RomsFile, RomsGrd, varname) ds_dy = gr.y_grad_rho(RomsFile, RomsGrd, varname) #grid corrections xCor = gr.x_grad_GridCor_Rho(RomsFile, RomsGrd, varname) yCor = gr.y_grad_GridCor_Rho(RomsFile, RomsGrd, varname)
import os os.chdir( '/home/jjacob2/python/Salt_Budget/SalinityVarianceBudget/Exact_Volume_Budget/' ) from netCDF4 import Dataset as nc4 import numpy as np import Exact_Budget_Salt_Terms as ebt import obs_depth_JJ as dep import matplotlib.pyplot as plt #load files (hourly output) FilePath = '/home/cae/runs/jasen/wc15.a01.b03.hourlywindWT.windmcurrent.diags/out.jasen3/' HistFile = FilePath + 'ocean_his_2014.nc' Hist = nc4(HistFile, 'r') AvgFile = FilePath + 'ocean_avg_2014.nc' Avg = nc4(AvgFile, 'r') Diag = nc4(FilePath + 'ocean_dia_2014.nc', 'r') #bounds of control volume (points in counter clockwise order) #north west corner of domain for mask testing #Vertices = np.array([[35.7, -124.215], [35.7, -124.0], [35.8, -124.0], [35.8, -124.215]]) #shelf & slope offshore of Pt. Ano Nuevo #Vertices = np.array([[37.0, -123.0], [37.0, -122.5], [37.2, -122.5], [37.2, -123.0]]) #Carmel Bay #Vertices = np.array([[36.522, -121.948], [36.522, -121.926], [36.567, -121.926], [36.567, -121.948]]) #Monterey Bay