cf.Field.regridc¶
-
Field.
regridc
(dst, axes=None, method=None, use_src_mask=True, use_dst_mask=False, fracfield=False, axis_order=None, ignore_degenerate=True, inplace=False, i=False, _compute_field_mass=None, return_operator=False)[source]¶ Return the field with the specified Cartesian axes regridded onto a new grid.
Between 1 and 3 dimensions may be regridded.
Regridding, also called remapping or interpolation, is the process of changing the grid underneath field data values while preserving the qualities of the original data.
The regridding method must be specified. First-order conservative interpolation conserves the global spatial integral of the field, but may not give approximations to the values as good as (multi)linear interpolation. Second-order conservative interpolation also takes into account the gradient across the source cells, so in general gives a smoother, more accurate representation of the source field especially when going from a coarser to a finer grid. (Multi)linear interpolation is available. The latter method is particular useful for cases when the latitude and longitude coordinate cell boundaries are not known nor inferable. Higher order patch recovery is available as an alternative to (multi)linear interpolation. This typically results in better approximations to values and derivatives compared to the latter, but the weight matrix can be larger than the linear matrix, which can be an issue when regridding close to the memory limit on a machine. It is only available in 2-d. Nearest neighbour interpolation is also available. Nearest source to destination is particularly useful for regridding integer fields such as land use.
Metadata
The field construct’s domain must have axes matching those specified in src_axes. The same is true for the destination grid, if it provided as part of another field. Optionally the axes to use from the destination grid may be specified separately in dst_axes.
The output field construct’s coordinate objects which span the specified axes are replaced with those from the destination grid. Any fields contained in coordinate reference objects will also be regridded, if possible.
Mask
The data array mask of the field is automatically taken into account, such that the regridded data array will be masked in regions where the input data array is masked. By default the mask of the destination grid is not taken into account. If the destination field data has more dimensions than the number of axes specified then, if used, its mask is taken from the 1-3 dimensional section of the data where the indices of all axes other than X and Y are zero.
Implementation
The interpolation is carried out using the
ESMF
package, a Python interface to the Earth System Modeling Framework (ESMF) regridding utility.Logging
Whether ESMF logging is enabled or not is determined by
cf.regrid_logging
. If it is logging takes place after every call. By default logging is disabled.See also
- Parameters
- dst:
Field
ordict
orRegridOperator
The destination grid. Must be one of:
Field
. The grid is defined by the field construct’s domain.dict
. The grid is defined by a dictionary with keys'latitude'
and'longitude'
whose values are with either both 1-d dimension coordinates constructs, or both 2-d auxiliary coordinate constructs. In the 2-d case, both coordinate constructs must have their axes in the same order and this must be specified with the'axes'
key as either of the tuples('X', 'Y')
or('Y', 'X')
.RegridOperator
. The grid is defined by a regrid operator that has been returned by a previous call toregridc
withreturn_operator=True
.This option can give large performance increases, as greatest computational expense is often the creation of the regrid operator, rather than running the regrid operator to regrid the data.
The regrid operator defines the source grid and the regridding weights, so the method, axes, ignore_degenerate, use_src_mask, and use_dst_mask parameters are not required and are ignored if set.
An exception will be raised if the domain of the source field being regridded is inconsistent with source grid of the regrid operator.
The source field being regridded may, however, have a different data mask to that of the source grid in the regrid operator. In this case a new regrid operator is automatically created, with the associated loss in performance.
- dst:
Field
ordict
orRegridOperator
The field containing the new grid or a dictionary with the axes specifiers as keys referencing dimension coordinates.
- axes: optional
Select dimension coordinates from the source and destination fields for regridding. See
cf.Field.axes
TODO for options for selecting specific axes. However, the number of axes returned bycf.Field.axes
TODO must be the same as the number of specifiers passed in. TODO Can only be None if regridoperatorNote
When dst is a regrid operator then axes is ignored, and its value is set by the regrid operator’s parameters.
- method:
str
, optional Specify the regridding method. This parameter must be set unless the new grid is specified by a regridding operator, which stores its own method. See the dst parameter.
The method parameter may be one of the following:
Method
Description
'linear'
Bilinear interpolation.
'bilinear'
Deprecated alias for
'linear'
.'conservative_1st'
First order conservative interpolation.
Preserve the area integral of the data across the interpolation from source to destination. It uses the proportion of the area of the overlapping source and destination cells to determine appropriate weights.
In particular, the weight of a source cell is the ratio of the area of intersection of the source and destination cells to the area of the whole destination cell.
It does not account for the field gradient across the source cell, unlike the second-order conservative method (see below).
'conservative_2nd'
Second-order conservative interpolation.
As with first order (see above), preserves the area integral of the field between source and destination using a weighted sum, with weights based on the proportionate area of intersection.
Unlike first-order, the second-order method incorporates further terms to take into consideration the gradient of the field across the source cell, thereby typically producing a smoother result of higher accuracy.
'conservative'
Alias for
'conservative_1st'
'patch'
Higher-order patch recovery interpolation.
A second degree polynomial regridding method, which uses a least squares algorithm to calculate the polynomial.
This method gives better derivatives in the resulting destination data than the linear method.
'nearest_stod'
Nearest neighbour interpolation for which each destination point is mapped to the closest source point.
Useful for extrapolation of categorical data.
'nearest_dtos'
Nearest neighbour interpolation for which each source point is mapped to the destination point.
Useful for extrapolation of categorical data.
A given destination point may receive input from multiple source points, but no source point will map to more than one destination point.
This is the default and can only be used the new grid is specified by a regridding operator, which stores its own method.
Note
When dst is a regrid operator then the method may still be set, but must have the value
None
or else agree with the regridding operator’s method.- use_src_mask:
bool
, optional For all methods other than ‘nearest_stod’, this must be True as it does not make sense to set it to False. For the ‘nearest_stod’ method if it is True then points in the result that are nearest to a masked source point are masked. Otherwise, if it is False, then these points are interpolated to the nearest unmasked source points.
Note
When dst is a regrid operator then use_src_mask is ignored, and its value is set by the regrid operator’s parameters.
- use_dst_mask:
bool
, optional By default the mask of the data on the destination grid is not taken into account when performing regridding. If this option is set to True then it is.
Note
When dst is a regrid operator then use_dst_mask is ignored, and its value is set by the regrid operator’s parameters.
- fracfield:
bool
, optional If the method of regridding is conservative the fraction of each destination grid cell involved in the regridding is returned instead of the regridded data if this is True. Otherwise this is ignored.
- axis_order: sequence, optional
A sequence of items specifying dimension coordinates as retrieved by the
dim
method. These determine the order in which to iterate over the other axes of the field when regridding slices. The slowest moving axis will be the first one specified. Currently the regridding weights are recalculated every time the mask of a slice changes with respect to the previous one, so this option allows the user to minimise how frequently the mask changes. TODO.- ignore_degenerate:
bool
, optional True by default. Instructs ESMF to ignore degenerate cells when checking the grids for errors. Regridding will proceed and degenerate cells will be skipped, not producing a result, when set to True. Otherwise an error will be produced if degenerate cells are found. This will be present in the ESMF log files if cf.regrid_logging is set to True. As of ESMF 7.0.0 this only applies to conservative regridding. Other methods always skip degenerate cells.
Note
When dst is a regrid operator then ignore_degenerate is ignored, and its value is set by the regrid operator’s parameters.
- inplace:
bool
, optional If True then do the operation in-place and return
None
.- _compute_field_mass:
dict
, optional If this is a dictionary then the field masses of the source and destination fields are computed and returned within the dictionary. The keys of the dictionary indicates the lat/long slice of the field and the corresponding value is a tuple containing the source field construct’s mass and the destination field construct’s mass. The calculation is only done if conservative regridding is being performed. This is for debugging purposes.
- return_operator:
bool
, optional If True then do not perform the regridding, rather return a
RegridOperator
instance that defines the regridding operation, including the destination grid, and which can be used in subsequent calls toregridc
.New in version 3.10.0.
- i: deprecated at version 3.0.0
Use the inplace parameter instead.
- dst:
- Returns
Field
orNone
orRegridOperator
The regridded field construct, or
None
if the operation was in-place, or the regridding operator if return_operator is True.
Examples:
Regrid the time axes of field
f
conservatively onto a grid contained in fieldg
:>>> h = f.regridc(g, axes='T', method='conservative')
Regrid the T axis of field
f
conservatively onto the grid specified in the dimension coordinatet
:>>> h = f.regridc({'T': t}, axes=('T'), method='conservative_1st')
Regrid the T axis of field
f
using linear interpolation onto a grid contained in fieldg
:>>> h = f.regridc(g, axes=('T'), method='linear')
Regrid the X and Y axes of field
f
conservatively onto a grid contained in fieldg
:>>> h = f.regridc(g, axes=('X','Y'), method='conservative_1st')
Regrid the X and T axes of field
f
conservatively onto a grid contained in fieldg
using the destination mask:>>> h = f.regridc(g, axes=('X','Y'), method='linear', ... use_dst_mask=True)
Store the regrid operator and use it for a subsequent regrids:
>>> op = f.regridc(g, axes='T', method='conservative', ... return_operator=True) >>> h = f.regridc(op) >>> h2 = f2.regridc(op)