def interpolate(coords, var, interp_coords, missing_value=None, fill=True, kind="linear"):
"""Interpolate globally defined data to a different (regular) grid.
Arguments:
coords: Tuple of coordinate arrays for each dimension.
var (:obj:`ndarray` of dim (nx1, ..., nxd)): Variable data to interpolate.
interp_coords: Tuple of coordinate arrays to interpolate to.
missing_value (optional): Value denoting cells of missing data in ``var``.
Is replaced by `NaN` before interpolating. Defaults to `None`, which means
no replacement is taking place.
fill (bool, optional): Whether `NaN` values should be replaced by the nearest
finite value after interpolating. Defaults to ``True``.
kind (str, optional): Order of interpolation. Supported are `nearest` and
`linear` (default).
Returns:
:obj:`ndarray` containing the interpolated values on the grid spanned by
``interp_coords``.
"""
if len(coords) != len(interp_coords) or len(coords) != var.ndim:
raise ValueError("Dimensions of coordinates and values do not match")
var = np.array(var)
if missing_value is not None:
invalid_mask = np.isclose(var, missing_value)
var[invalid_mask] = np.nan
if var.ndim > 1 and coords[0].ndim == 1:
interp_grid = np.rollaxis(np.array(np.meshgrid(
*interp_coords, indexing="ij", copy=False)), 0, len(interp_coords) + 1)
else:
interp_grid = coords
var = scipy.interpolate.interpn(coords, var, interp_grid,
bounds_error=False, fill_value=np.nan, method=kind)
if fill:
var = fill_holes(var)
return var
评论列表
文章目录