xcdat.regridder.regrid2.Regrid2Regridder
xcdat.regridder.regrid2.Regrid2Regridder#
- class xcdat.regridder.regrid2.Regrid2Regridder(input_grid, output_grid, **options)[source]#
- __init__(input_grid, output_grid, **options)[source]#
Pure python implementation of the regrid2 horizontal regridder from CDMS2’s regrid2 module.
Regrid data from
input_gridtooutput_grid.Available options: None
- Parameters
input_grid (xr.Dataset) – Dataset containing the source grid.
output_grid (xr.Dataset) – Dataset containing the destination grid.
options (Dict[str, Any]) – Dictionary with extra parameters for the regridder.
Examples
Import xCDAT:
>>> import xcdat >>> from xcdat.regridder import regrid2
Open a dataset:
>>> ds = xcdat.open_dataset("ts.nc")
Create output grid:
>>> output_grid = xcdat.create_gaussian_grid(32)
Create regridder:
>>> regridder = regrid2.Regrid2Regridder(ds.grid, output_grid)
Regrid data:
>>> data_new_grid = regridder.horizontal("ts", ds)
Methods
__init__(input_grid, output_grid, **options)Pure python implementation of the regrid2 horizontal regridder from CDMS2's regrid2 module.
horizontal(data_var, ds)Regrid
data_varindsto output grid.- horizontal(data_var, ds)[source]#
Regrid
data_varindsto output grid.Mappings and weights between input and output grid are calculated on the first call, allowing a regridder to be applied to many input datasets.
- Parameters
data_var (str) – The name of the data variable inside the dataset to regrid.
ds (xr.Dataset) – The dataset containing
data_var.
- Returns
xr.Dataset – Dataset with variable on the destination grid.
- Raises
KeyError – If data variable does not exist in the Dataset.
Examples
Create output grid:
>>> output_grid = xcdat.create_gaussian_grid(32)
Create regridder:
>>> regridder = regrid2.Regrid2Regridder(ds, output_grid)
Regrid data:
>>> data_new_grid = regridder.horizontal("ts", ds)
- _output_axis_sizes(da)[source]#
Maps axes to output array sizes.
- Parameters
da (xr.DataArray) – Data array containing variable to be regridded.
- Returns
Dict – Mapping of axis name e.g. (“X”, “Y”, etc) to output sizes.
- _regrid(input_data, axis_sizes, ordered_axis_names)[source]#
Applies regridding to input data.
- Parameters
input_data (np.ndarray) – Input multi-dimensional array on source grid.
axis_sizes (Dict[str, int]) – Mapping of axis name e.g. (“X”, “Y”, etc) to output sizes.
ordered_axis_names (List[str]) – List of axis name in order of dimensions of
input_data.
- Returns
np.ndarray – Multi-dimensional array on destination grid.
- _base_put_indexes(axis_sizes)[source]#
Calculates the base indexes to place cell (0, 0).
Example: For a 3D array (time, lat, lon) with the shape (2, 2, 2) the offsets to place cell (0, 0) in each time step would be [0, 4].
For a 4D array (time, plev, lat, lon) with shape (2, 2, 2, 2) the offsets to place cell (0, 0) in each time step would be [0, 4, 8, 16].
- Parameters
axis_sizes (Dict[str, int]) – Mapping of axis name e.g. (“X”, “Y”, etc) to output sizes.
- Returns
np.ndarray – Array containing the base indexes to be used in np.put operations.
- _create_output_dataset(input_ds, data_var, output_data, axis_variable_name_map, ordered_axis_names)[source]#
Creates the output Dataset containing the new variable on the destination grid.
- Parameters
input_ds (xr.Dataset) – Input dataset containing coordinates and bounds for unmodified axes.
data_var (str) – The name of the regridded variable.
output_data (np.ndarray) – Output data array.
axis_variable_name_map (Dict[str, str]) – Map of axis name e.g. (“X”, “Y”, etc) to variable name e.g. (“lon”, “lat”, etc).
ordered_axis_names (List[str]) – List of axis names in the order observed for
output_data.
- Returns
xr.Dataset – Dataset containing the variable on the destination grid.
- _abc_impl = <_abc._abc_data object>#