The difference_ops module.¶
This module implements averaging and differencing functions for data on a C-grid.
Detailed Module Contents¶
The entire module is documented below.
difference_ops.py.
Created on Wed Apr 17 21:03:43 2019
Difference operators for C-grid data.
Note: written for MONC grid
v[i ,j ,k] – + – v[i+1,j ,k] – + | | | | | | | | p[i ,j ,k] – u[i ,j ,k] – p[i+1,j ,k] – u[i+1,j,k] | | | | | | | |
v[i ,j-1,k] – + – v[i+1,j-1,k] – +
The 0th point is a p point. We have decided this is at dx/2, dy/2
roll(f, +1) shifts data right, so is equivalent to f[i-1] (or j-1).
For optional UM grid: + – v[i ,j+1,k] – + – v[i+1 ,j ,k] –+ | | | | | | | | u[i ,j ,k]– p[i ,j ,k] – u[i+1,j ,k] – p[i+1,j ,k] – u[i+2,j,k] | | | | | | | | + – v[i ,j ,k] – + – v[i+1 ,j ,k] –+
The 0th point is a p point. We have decided this is at dx/2, dy/2
roll(f, +1) shifts data right, so is equivalent to f[i-1] (or j-1).
@author: Peter Clark
- monc_utils.data_utils.difference_ops.set_difference_ops_options(opts)¶
- monc_utils.data_utils.difference_ops.exec_fn(fn: Callable, field: DataArray, axis: int) DataArray¶
Execute function using map_overlap with overlap on selected axis.
- Parameters:
fn (function) – DESCRIPTION.
field (xarray.DataArray) – DESCRIPTION.
axis (int) – DESCRIPTION.
- Return type:
new xarray.DataArray
- monc_utils.data_utils.difference_ops.last_dim(z)¶
Remove all but last dimension of z. (Deprecated)
- Parameters:
z (n-dimensional array.) –
- Return type:
last dimension of z.
@author: Peter Clark
- monc_utils.data_utils.difference_ops.interpolate_z(field, znew)¶
Interpolate field znew.
- Parameters:
field (xarray nD field) –
znew (xarray coordinate new z.) –
- Return type:
field on znew levels
@author: Peter Clark
- monc_utils.data_utils.difference_ops.interp_aux_coords(field, dim, newfield, target_dim)¶
Intorpolat non-dimensional coords to new dimension.
- Parameters:
field (xarray.DataArray) – nD field.
dim (char) – dimension in field. e.g. ‘x_p’
newfield (xarray.DataArray) – field with new aux_coords associated with dim.
target_dim (char) – new dimension in field. e.g. ‘x_v’
- Returns:
newfield – copy of field with target_dim coords.
- Return type:
xarray.DataArray
- monc_utils.data_utils.difference_ops.grid_conform_xy(field, target_dim)¶
Force field to target grid by averaging if necessary. This works on x or y grid. This replaces grid_conform_x and grid_conform_y
- Parameters:
field (xarray) – Any multi-dimensional xarray with x dimension ‘x_u’ or ‘x_p’. Any other x dimensionm treated as ‘x_p’. OR Any multi-dimensional xarray with y dimension ‘y_v’ or ‘y_p’. Any other y dimensionm treated as ‘y_p’.
target_xdim (str) – Dimension name ‘x_u’ or ‘x_p’ OR ‘y_v’ or ‘y_p’
- Returns:
field on target x or y grid.
- Return type:
xarray
- monc_utils.data_utils.difference_ops.grid_conform_x(field, target_xdim)¶
Force field to target x grid by averaging if necessary.
- Parameters:
field (xarray) – Any multi-dimensional xarray with x dimension ‘x_u’ or ‘x_p’. Any other x dimensionm treated as ‘x_p’.
target_xdim (str) – Dimension name ‘x_u’ or ‘x_p’
- Returns:
field on target x grid.
- Return type:
xarray
- monc_utils.data_utils.difference_ops.grid_conform_y(field, target_ydim)¶
Force field to target y grid by averaging if necessary.
- Parameters:
field (xarray) – Any multi-dimensional xarray with y dimension ‘y_v’ or ‘y_p’. Any other y dimensionm treated as ‘y_p’.
target_xdim (str) – Dimension name ‘y_v’ or ‘y_p’
- Returns:
field on target y grid.
- Return type:
xarray
- monc_utils.data_utils.difference_ops.grid_conform_z(field, z_w, z_p, target_zdim)¶
Force field to target x grid by interpolation if necessary.
- Parameters:
field (xarray) – Any multi-dimensional xarray with z dimension ‘z_w’ or ‘z_p’.
z_w (xarray coord.) –
z_p (xarray coord.) –
target_xdim (str) – Dimension name ‘z_w’ or ‘z_p’
- Returns:
field on target x grid.
- Return type:
xarray
- monc_utils.data_utils.difference_ops.grid_conform(field, z_w, z_p, grid: str = 'p')¶
Force field to target grid by interpolation if necessary.
- Parameters:
field (xarray) – Any multi-dimensional xarray with z dimension ‘z_w’ or ‘z_p’.
z_w (xarray coord.) –
z_p (xarray coord.) –
grid (str | tuple(str)) – grid identifier ‘p’| ‘u’| ‘v’| ‘w’ or tuple (xdim, ydim, zdim).
- Returns:
field on target grid.
- Return type:
xarray
- monc_utils.data_utils.difference_ops.d_by_dxy_field_native(field, dim_dir)¶
Differentiate field in x direction on native grid.
- Parameters:
field (xarray nD field) –
- Return type:
field on native grid
- monc_utils.data_utils.difference_ops.d_by_dx_field_native(field)¶
Differentiate field in x direction on native grid.
- Parameters:
field (xarray nD field) –
- Return type:
field on native grid
- monc_utils.data_utils.difference_ops.d_by_dx_field(field, z_w, z_p, grid: str = 'p')¶
Differentiate field in x direction.
- Parameters:
field (xarray nD field) –
z_w (xarray coordinate) – zcoord on w levels - needed if changing vertical grid.
z_p (xarray coordinate) – zcoord on p levels - needed if changing vertical grid.
grid (str | tuple of 2 strings) – destination grid (Default = ‘p’)
- Return type:
field on required grid
@author: Peter Clark
- monc_utils.data_utils.difference_ops.d_by_dy_field_native(field)¶
Differentiate field in y direction on native grid.
- Parameters:
field (xarray nD field) –
- Return type:
field on native grid
- monc_utils.data_utils.difference_ops.d_by_dy_field(field, z_w, z_p, grid: str = 'p')¶
Differentiate field in y direction.
- Parameters:
field (xarray nD field) –
z_w (xarray coordinate) – zcoord on w levels - needed if changing vertical grid.
z_p (xarray coordinate) – zcoord on p levels - needed if changing vertical grid.
grid (str | tuple of 2 strings) – destination grid (Default = ‘p’)
- Return type:
field on required grid
@author: Peter Clark
- monc_utils.data_utils.difference_ops.d_by_dxy_field(field, z_w, z_p, dim_dir, grid: str = 'p')¶
Differentiate field in x direction.
- Parameters:
field (xarray nD field) –
z_w (xarray coordinate) – zcoord on w levels - needed if changing vertical grid.
z_p (xarray coordinate) – zcoord on p levels - needed if changing vertical grid.
dim_dir (char) – Direction to differentiate: ‘x’ or ‘y’.
grid (str | tuple of 2 strings) – destination grid (Default = ‘p’)
- Return type:
field on required grid
@author: Peter Clark
- monc_utils.data_utils.difference_ops.d_by_dz_field_native(field)¶
Differentiate field in z direction on native grid.
- Parameters:
field (xarray nD field) –
- Return type:
field on native grid
- monc_utils.data_utils.difference_ops.d_by_dz_field(field, z_w, z_p, grid: str = 'p')¶
Differentiate field in z direction.
- Parameters:
field (xarray nD field) –
z_w (xarray coordinate) – zcoord on w levels - needed if changing vertical grid.
z_p (xarray coordinate) – zcoord on p levels - needed if changing vertical grid.
grid (str | tuple of 2 strings) – destination grid (Default = ‘p’)
- Return type:
field on required grid
@author: Peter Clark
- monc_utils.data_utils.difference_ops.padleft(f, zt, axis=0)¶
Add dummy field at bottom of nD array.
- Parameters:
f (nD field) –
zt (1D zcoordinates) –
axis=0 (Specify axis to extend) –
- Return type:
extended field, extended coord
@author: Peter Clark
- monc_utils.data_utils.difference_ops.padright(f, zt, axis=0)¶
Add dummy field at top of nD array.
- Parameters:
f (nD field) –
zt (1D zcoordinates) –
axis=0 (Specify axis to extend) –
- Return type:
extended field, extended coord
@author: Peter Clark