def test_ddxi(): testvars = ["salt", "u", "v", "z_w"] for testvar in testvars: acc = ds[testvar].xroms.ddxi() assert np.allclose(acc, xroms.ddxi(ds[testvar], grid)) acc.name == acc.attrs["name"] acc.attrs["grid"] == ds.xroms.grid items = [ "T", "X", "Y", "Z", "longitude", "latitude", "vertical", "time" ] assert set(items).issubset(acc.cf.get_valid_keys()) acc = ds.xroms.ddxi(testvar) assert np.allclose(acc, xroms.ddxi(ds[testvar], grid)) acc.name == acc.attrs["name"] acc.attrs["grid"] == ds.xroms.grid items = [ "T", "X", "Y", "Z", "longitude", "latitude", "vertical", "time" ] assert set(items).issubset(acc.cf.get_valid_keys())
def test_ddxi(): # compare away from boundaries and for # correct dx value (eta=0) ddxi = (u[2] - u[0]) / (2 * dx) assert np.allclose(xroms.ddxi(ds.u, grid)[0, 1, 0, 1], ddxi)
def ddxi( self, hcoord=None, scoord=None, hboundary="extend", hfill_value=None, sboundary="extend", sfill_value=None, attrs=None, ): """Calculate d/dxi for variable. Inputs ------ hcoord: string, optional. Name of horizontal grid to interpolate output to. Options are 'rho', 'psi', 'u', 'v'. scoord: string, optional. Name of vertical grid to interpolate output to. Options are 's_rho', 's_w', 'rho', 'w'. hboundary: string, optional Passed to `grid` method calls; horizontal boundary selection for calculating horizontal derivative of var. This same value will be used for all horizontal grid changes too. 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 fill value. From xgcm documentation: The value to use in the boundary condition with `boundary='fill'`. sboundary: string, optional Passed to `grid` method calls; vertical boundary selection for calculating horizontal derivative of var. This same value will be used for all vertical grid changes too. 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. sfill_value: float, optional Passed to `grid` method calls; vertical boundary selection fill value. From xgcm documentation: The value to use in the boundary condition with `boundary='fill'`. attrs: dict, optional Dictionary of attributes to add to resultant arrays. Requires that q is DataArray. For example: `attrs={'name': 'varname', 'long_name': 'longvarname', 'units': 'units'}` Returns ------- DataArray of dqdxi, the gradient of q in the xi-direction with attributes altered to reflect calculation. Notes ----- dqdxi = dqdx*dzdz - dqdz*dzdx Derivatives are taken in the ROMS curvilinear grid native xi-direction. These derivatives properly account for the fact that ROMS vertical coordinates are s coordinates and therefore can vary in time and space. This will alter the number of points in the xi and s dimensions. Example usage ------------- >>> ds.salt.xroms.ddxi() """ return xroms.ddxi( self.da, self.da.attrs["grid"], attrs=attrs, hcoord=hcoord, scoord=scoord, hboundary=hboundary, hfill_value=hfill_value, sboundary=sboundary, sfill_value=sfill_value, )