Skip to content

GridInterp

cfdb.interp.GridInterp

Wrapper around geointerp.GridInterpolator that auto-detects CRS and spatial/temporal coordinate names from dataset axis metadata, handles dimension reordering, and iterates over iter_dim slices when present.

All interpolation methods are generators that yield (dim_value, result) tuples. When there is no iter_dim, a single tuple is yielded with dim_value=None.

Parameters:

Name Type Description Default
data_var DataVariableView or DataVariable

The data variable to interpolate.

required
x str or None

Name of the x coordinate. Auto-detected from axis metadata if None.

None
y str or None

Name of the y coordinate. Auto-detected from axis metadata if None.

None
z str or None

Name of the z coordinate. Auto-detected from axis metadata if None.

None
iter_dim str or None

Name of the dimension to iterate over. Auto-detected from axis='t' metadata if None.

None

Raises:

Type Description
ValueError

If the dataset has no CRS defined or x/y coordinates cannot be determined.

__init__(data_var, x=None, y=None, z=None, iter_dim=None)

to_grid(grid_res=None, to_crs=None, bbox=None, order=3, extrapolation='constant', fill_val=np.nan, min_val=None)

Regrid the data variable onto a new regular grid.

The first argument can be a DataVariableView from a grid dataset, in which case grid_res, bbox, and to_crs are derived from it.

Parameters:

Name Type Description Default
grid_res float, tuple of float, or DataVariableView

Output grid resolution, or a destination DataVariable to derive target grid parameters from.

None
to_crs int, str, or None

Target CRS for the output grid (e.g. 4326). Defaults to the dataset's CRS. Ignored if grid_res is a DataVariable.

None
bbox tuple of float or None

Bounding box in to_crs coordinates. Ignored if grid_res is a DataVariable.

None
order int

Spline interpolation order (0-5). 0=nearest, 1=linear, 3=cubic.

3
extrapolation str

Mode for values outside grid: 'constant', 'nearest', 'reflect', 'mirror', or 'wrap'.

'constant'
fill_val float

Fill value for 'constant' extrapolation.

nan
min_val float or None

Floor value; results below this are clamped.

None

Yields:

Type Description
tuple of (dim_value, np.ndarray)

dim_value is None when there is no iter_dim. The array is the regridded 2D or 3D spatial data.

to_points(target_points, to_crs=None, order=3, min_val=None)

Sample the data variable at specific target point locations.

Parameters:

Name Type Description Default
target_points np.ndarray, list of shapely Points, or DataVariableView

Array of shape (M, 2) or (M, 3) with target point locations, a list/array of shapely Point geometries, or a DataVariableView from a ts_ortho dataset to derive target points from.

required
to_crs int, str, or None

CRS of target_points if different from the dataset's CRS. Ignored if target_points is a DataVariable.

None
order int

Spline interpolation order (0-5). 0=nearest, 1=linear, 3=cubic.

3
min_val float or None

Floor value; results below this are clamped.

None

Yields:

Type Description
tuple of (dim_value, np.ndarray)

dim_value is None when there is no iter_dim. The array is a 1D array of shape (M,) with interpolated values at each target point. When target_points is 2D (M, 2) but the source grid has a z dimension, each z-level is interpolated independently and the result has shape (n_z, M).

interp_na(method='linear', min_val=None)

Fill NaN values in the data variable via spatial interpolation.

Parameters:

Name Type Description Default
method str

Interpolation method: 'nearest', 'linear', or 'cubic'.

'linear'
min_val float or None

Floor value; results below this are clamped.

None

Yields:

Type Description
tuple of (dim_value, np.ndarray)

dim_value is None when there is no iter_dim. The array has the same shape as the source spatial data with NaN values filled.

regrid_levels(target_levels, source_levels, axis=0, method='linear')

Regrid data from variable vertical levels onto fixed target levels.

This is useful for data on terrain-following or sigma coordinates where the actual level heights vary at each grid point.

Parameters:

Name Type Description Default
target_levels array-like of float

Target level values to interpolate onto (must be monotonically increasing).

required
source_levels str

Name of a data variable in the dataset that contains the source level values. Must have the same shape as this data variable.

required
axis int

The axis in the data that corresponds to the vertical/level dimension.

0
method str

Interpolation method. Currently only 'linear' is supported.

'linear'

Yields:

Type Description
tuple of (dim_value, np.ndarray)

dim_value is None when there is no iter_dim. The array has the vertical axis replaced by the target levels.

Raises:

Type Description
TypeError

If source_levels is not a string.

ValueError

If source_levels does not exist as a data variable in the dataset.

PointInterp

cfdb.interp.PointInterp

Wrapper around geointerp.PointInterpolator that auto-detects CRS and spatial/temporal coordinate names from dataset axis metadata for ts_ortho (scattered point) datasets.

All interpolation methods are generators that yield (dim_value, result) tuples. When there is no iter_dim, a single tuple is yielded with dim_value=None.

Parameters:

Name Type Description Default
data_var DataVariableView or DataVariable

The data variable to interpolate.

required
xy str or None

Name of the xy geometry coordinate. Auto-detected from axis metadata if None.

None
z str or None

Name of the z coordinate. Auto-detected from axis metadata if None.

None
iter_dim str or None

Name of the dimension to iterate over. Auto-detected from axis='t' metadata if None.

None

Raises:

Type Description
ValueError

If the dataset has no CRS defined or xy coordinate cannot be determined.

__init__(data_var, xy=None, z=None, iter_dim=None)

to_grid(grid_res=None, to_crs=None, bbox=None, method='linear', extrapolation='constant', fill_val=np.nan, min_val=None)

Interpolate scattered point data onto a regular grid.

The first argument can be a DataVariableView from a grid dataset, in which case grid_res, bbox, and to_crs are derived from it.

Parameters:

Name Type Description Default
grid_res float, or DataVariableView

Output grid resolution, or a destination DataVariable.

None
to_crs int, str, or None

Target CRS for the output grid.

None
bbox tuple of float or None

Bounding box in to_crs coordinates.

None
method str

'nearest', 'linear', or 'cubic'.

'linear'
extrapolation str

'constant' or 'nearest'.

'constant'
fill_val float

Fill value for 'constant' extrapolation.

nan
min_val float or None

Floor value.

None

Yields:

Type Description
tuple of (dim_value, np.ndarray)

dim_value is None when there is no iter_dim.

to_points(target_points, to_crs=None, method='linear', min_val=None)

Interpolate scattered point data at target point locations.

Parameters:

Name Type Description Default
target_points np.ndarray, list of shapely Points, or DataVariableView

Target point locations, shapely Points, or a DataVariableView from a ts_ortho dataset.

required
to_crs int, str, or None

CRS of target_points.

None
method str

'nearest', 'linear', or 'cubic'.

'linear'
min_val float or None

Floor value.

None

Yields:

Type Description
tuple of (dim_value, np.ndarray)

dim_value is None when there is no iter_dim.