def EKE(ug, vg, grid, hboundary="extend", hfill_value=None): """Calculate EKE [m^2/s^2] Inputs ------ ug: DataArray Geostrophic or other xi component velocity [m/s] vg: DataArray Geostrophic or other eta component velocity [m/s] grid: xgcm.grid Grid object associated with ug, vg hboundary: string, optional Passed to `grid` method calls; horizontal boundary selection for moving to rho grid. From xgcm documentation: A flag indicating how to handle boundaries: * None: Do not apply any boundary conditions. Raise an error if boundary conditions are required for the operation. * 'fill': Set values outside the array boundary to fill_value (i.e. a Neumann boundary condition.) * 'extend': Set values outside the array to the nearest array value. (i.e. a limited form of Dirichlet boundary condition. hfill_value: float, optional Passed to `grid` method calls; horizontal boundary selection for moving to rho grid. From xgcm documentation: The value to use in the boundary condition with `boundary='fill'`. Returns ------- DataArray of eddy kinetic energy on rho grid. Output is `[T,Y,X]`. Notes ----- EKE = 0.5*(ug^2 + vg^2) Example usage ------------- >>> ug, vg = xroms.uv_geostrophic(ds.zeta, ds.f, grid) >>> xroms.EKE(ug, vg, grid) """ assert isinstance(ug, xr.DataArray), "ug must be DataArray" assert isinstance(vg, xr.DataArray), "vg must be DataArray" # make sure velocities are on rho grid ug = xroms.to_rho(ug, grid, hboundary=hboundary, hfill_value=hfill_value) vg = xroms.to_rho(vg, grid, hboundary=hboundary, hfill_value=hfill_value) var = 0.5 * (ug**2 + vg**2) var.attrs["name"] = "EKE" var.attrs["long_name"] = "eddy kinetic energy" var.attrs["units"] = "m^2/s^2" var.attrs["grid"] = grid var.name = var.attrs["name"] return var
def speed(u, v, grid, hboundary="extend", hfill_value=None): """Calculate horizontal speed [m/s] from u and v components Inputs ------ u: DataArray xi component of velocity [m/s] v: DataArray eta component of velocity [m/s] grid: xgcm.grid Grid object associated with u, v hboundary: string, optional Passed to `grid` method calls; horizontal boundary selection for moving to rho grid. From xgcm documentation: A flag indicating how to handle boundaries: * None: Do not apply any boundary conditions. Raise an error if boundary conditions are required for the operation. * 'fill': Set values outside the array boundary to fill_value (i.e. a Neumann boundary condition.) * 'extend': Set values outside the array to the nearest array value. (i.e. a limited form of Dirichlet boundary condition. hfill_value: float, optional Passed to `grid` method calls; horizontal boundary fill value selection for moving to rho grid. From xgcm documentation: The value to use in the boundary condition with `boundary='fill'`. Returns ------- DataArray of speed calculated on rho/rho grids. Output is `[T,Z,Y,X]`. Notes ----- speed = np.sqrt(u^2 + v^2) Example usage ------------- >>> xroms.speed(ds.u, ds.v, grid) """ assert isinstance(u, xr.DataArray), "var must be DataArray" assert isinstance(v, xr.DataArray), "var must be DataArray" u = xroms.to_rho(u, grid, hboundary=hboundary, hfill_value=hfill_value) v = xroms.to_rho(v, grid, hboundary=hboundary, hfill_value=hfill_value) var = np.sqrt(u**2 + v**2) var.attrs["name"] = "speed" var.attrs["long_name"] = "horizontal speed" var.attrs["units"] = "m/s" var.attrs["grid"] = grid var.name = var.attrs["name"] return var
def test_to_rho(): testvars = ["salt", "u", "v"] for testvar in testvars: var = xroms.to_rho(ds[testvar], grid) # check for correct dims assert "xi_rho" in var.dims assert "eta_rho" in var.dims
def vertical_shear(dudz, dvdz, grid, hboundary="extend", hfill_value=None): """Calculate the vertical shear [1/s] Inputs ------ dudz: DataArray xi component of vertical shear [1/s] dvdz: DataArray eta compoenent of vertical shear [1/s] grid: xgcm.grid Grid object associated with dudz, dvdz hboundary: string, optional Passed to `grid` method calls; horizontal boundary selection for moving dudz and dvdz to rho grid. From xgcm documentation: A flag indicating how to handle boundaries: * None: Do not apply any boundary conditions. Raise an error if boundary conditions are required for the operation. * 'fill': Set values outside the array boundary to fill_value (i.e. a Neumann boundary condition.) * 'extend': Set values outside the array to the nearest array value. (i.e. a limited form of Dirichlet boundary condition. hfill_value: float, optional Passed to `grid` method calls; horizontal boundary selection for moving to rho grid. From xgcm documentation: The value to use in the boundary condition with `boundary='fill'`. Returns ------- DataArray of vertical shear on rho/w grids. Notes ----- vertical_shear = np.sqrt(u_z^2 + v_z^2) Example usage ------------- >>> xroms.vertical_shear(dudz, dvdz, grid) """ assert isinstance(dudz, xr.DataArray), "dudz must be DataArray" assert isinstance(dvdz, xr.DataArray), "dvdz must be DataArray" # make sure velocities are on rho grid dudz = xroms.to_rho(dudz, grid, hboundary=hboundary, hfill_value=hfill_value) dvdz = xroms.to_rho(dvdz, grid, hboundary=hboundary, hfill_value=hfill_value) var = np.sqrt(dudz**2 + dvdz**2) var.attrs["name"] = "shear" var.attrs["long_name"] = "vertical shear" var.attrs["units"] = "1/s" var.attrs["grid"] = grid var.name = var.attrs["name"] return var