climtas.regrid
climtas.regrid#
Dask-aware regridding
To apply a regridding you will need a set of weights mapping from the source grid to the target grid.
Regridding weights can be generated online using ESMF_RegridWeightGen
(esmf_generate_weights()
) or CDO (cdo_generate_weights()
), or
offline by calling these programs externally (this is recommended especially
for large grids, using ESMF_REgridWeightGen in MPI mode).
Once calculated regrid()
will apply these weights using a Dask sparse
matrix multiply, maintaining chunking in dimensions other than lat and lon.
Regrid
can create basic weights and store them to apply the weights to
multiple datasets.
- class climtas.regrid.Regridder(source_grid=None, target_grid=None, weights=None)[source]#
Set up the regridding operation
Supply either both
source_grid
anddest_grid
or justweights
.For large grids you may wish to pre-calculate the weights using ESMF_RegridWeightGen, if not supplied
weights
will be calculated fromsource_grid
anddest_grid
using CDO’s genbil function.Weights may be pre-computed by an external program, or created using
cdo_generate_weights()
oresmf_generate_weights()
- Parameters
source_grid (
coecms.grid.Grid
orxarray.DataArray
) – Source grid / sample datasettarget_grid (
coecms.grid.Grid
orxarray.DataArray
) – Target grid / sample datasetweights (
xarray.Dataset
) – Pre-computed interpolation weights
- regrid(source_data)[source]#
Regrid
source_data
to match the target grid- Parameters
source_data (
xarray.DataArray
or xarray.Dataset) – Sourcevariable –
- Returns
xarray.DataArray
or xarray.Dataset with a regridded version of the source variable
- climtas.regrid.apply_weights(source_data, weights, weights_matrix=None)[source]#
Apply the CDO weights
weights
tosource_data
, performing a regridding operation- Parameters
source_data (xarray.Dataset) – Source dataset
weights (xarray.Dataset) – CDO weights information
- Returns
Regridded version of the source dataset
- Return type
- climtas.regrid.cdo_generate_weights(source_grid, target_grid, method='bil', extrapolate=True, remap_norm='fracarea', remap_area_min=0.0)[source]#
Generate weights for regridding using CDO
Available weight generation methods are:
bic: SCRIP Bicubic
bil: SCRIP Bilinear
con: SCRIP First-order conservative
con2: SCRIP Second-order conservative
dis: SCRIP Distance-weighted average
laf: YAC Largest area fraction
ycon: YAC First-order conservative
nn: Nearest neighbour
Run
cdo gen${method} --help
for details of each method- Parameters
source_grid (xarray.DataArray) – Source grid
target_grid (xarray.DataArray) – Target grid description
method (str) – Regridding method
extrapolate (bool) – Extrapolate output field
remap_norm (str) – Normalisation method for conservative methods
remap_area_min (float) – Minimum destination area fraction
- Returns
xarray.Dataset
with regridding weights
- climtas.regrid.compute_weights_matrix(weights)[source]#
Convert the weights from CDO/ESMF to a numpy array
- climtas.regrid.esmf_generate_weights(source_grid, target_grid, method='bilinear', extrap_method='nearestidavg', norm_type='dstarea', line_type=None, pole=None, ignore_unmapped=False)[source]#
Generate regridding weights with ESMF
https://www.earthsystemcog.org/projects/esmf/regridding
- Parameters
source_grid (
xarray.Dataarray
) – Source grid. If masked the mask will be used in the regriddingtarget_grid (
xarray.Dataarray
) – Target grid. If masked the mask will be used in the regriddingmethod (str) – ESMF Regridding method, see
ESMF_RegridWeightGen --help
extrap_method (str) – ESMF Extrapolation method, see
ESMF_RegridWeightGen --help
- Returns
xarray.Dataset
with regridding information fromESMF_RegridWeightGen
- climtas.regrid.regrid(source_data, target_grid=None, weights=None)[source]#
A simple regrid. Inefficient if you are regridding more than one dataset to the target grid because it re-generates the weights each time you call the function.
To save the weights use
Regridder
.- Parameters
source_data (
xarray.DataArray
) – Source variabletarget_grid (
coecms.grid.Grid
orxarray.DataArray
) – Target grid / sample variable
- Returns
xarray.DataArray
with a regridded version of the source variable