xcdat.regridder.xgcm.XGCMRegridder#

class xcdat.regridder.xgcm.XGCMRegridder(input_grid, output_grid, method='linear', target_data=None, grid_positions=None, periodic=False, extra_init_options=None, **options)[source]#
__init__(input_grid, output_grid, method='linear', target_data=None, grid_positions=None, periodic=False, extra_init_options=None, **options)[source]#

Extension of xgcm regridder.

The XGCMRegridder extends xgcm by automatically constructing the Grid object, transposing the output data to match the dimensional order of the input data, and ensuring bounds and metadata are preserved in the output dataset.

Linear and log methods require a single dimension position, which can usually be automatically derived. A custom position can be specified using the grid_positions argument.

Conservative regridding requires multiple dimension positions, e.g., {“center”: “xc”, “left”: “xg”} which can be passed using the grid_positions argument.

xgcm.Grid can be passed additional arguments using extra_init_options. These arguments can be found on XGCM’s Grid documentation.

xgcm.Grid.transform can be passed additional arguments using options. These arguments can be found on XGCM’s Grid.transform documentation.

Parameters
  • input_grid (xr.Dataset) – Contains source grid coordinates.

  • output_grid (xr.Dataset) – Contains destination grid coordinates.

  • method (XGCMVerticalMethods) –

    Regridding method, by default “linear”. Options are
    • linear (default)

    • log

    • conservative

  • target_data (Optional[Union[str, xr.DataArray]]) – Data to transform target data onto, either the key of a variable in the input dataset or an xr.DataArray, by default None.

  • grid_positions (Optional[Dict[str, str]]) – Mapping of dimension positions, by default None. If None then an attempt is made to derive this argument.

  • periodic (Optional[bool]) – Whether the grid is periodic, by default False.

  • extra_init_options (Optional[Dict[str, Any]]) – Extra options passed to the xgcm.Grid constructor, by default None.

  • options (Optional[Dict[str, Any]]) – Extra options passed to the xgcm.Grid.transform method.

Raises
  • KeyError – If data variable does not exist in the Dataset.

  • ValueError – If method is not valid.

Examples

Import xCDAT:

>>> import xcdat
>>> from xcdat.regridder import xgcm

Open a dataset:

>>> ds = xcdat.open_dataset("so.nc")

Create output grid:

>>> output_grid = xcdat.create_grid(lev=np.linspace(1000, 1, 5))

Create theta:

>>> ds["pressure"] = (ds["hyam"] * ds["P0"] + ds["hybm"] * ds["PS"]).transpose(**ds["T"].dims)

Create regridder:

>>> regridder = xgcm.XGCMRegridder(ds, output_grid, method="linear", target_data="pressure")

Regrid data:

>>> data_new_grid = regridder.vertical("so", ds)

Passing additional arguments to xgcm.Grid and xgcm.Grid.transform:

>>> regridder = xgcm.XGCMRegridder(ds, output_grid, method="linear", extra_init_options={"boundary": "fill", "fill_value": 1e27}, mask_edges=True)

Methods

__init__(input_grid, output_grid[, method, ...])

Extension of xgcm regridder.

horizontal(data_var, ds)

Placeholder for base class.

vertical(data_var, ds)

Regrid data_var in ds to output grid.

horizontal(data_var, ds)[source]#

Placeholder for base class.

vertical(data_var, ds)[source]#

Regrid data_var in ds to output grid.

To pass additional arguments to xgcm.Grid or xgcm.Grid.transform see the documentation in the constructor xcdat.regridder.xgcm.XGCMRegridder.__init__().

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.

  • RuntimeError – If “Z” coordinate is not detected.

Examples

Create output grid:

>>> output_grid = xcdat.create_grid(np.linspace(1000, 1, 20))

Create regridder:

>>> regridder = xgcm.XGCMRegridder(ds, output_grid, method="linear", target_data="pressure")

Regrid data:

>>> data_new_grid = regridder.vertical("T", ds)
_get_grid_positions()[source]#
_abc_impl = <_abc._abc_data object>#