pyinterp.backends.xarray.RegularGridInterpolator#
- class pyinterp.backends.xarray.RegularGridInterpolator(array)[source]#
Bases:
objectInterpolate on a regular grid in arbitrary dimensions.
Perform interpolation on a regular grid with uneven spacing support. Automatically detects geodetic coordinates (lon/lat) using CF conventions and temporal axes (datetime64).
The data must be defined on a regular grid; the grid spacing however may be uneven. Linear, nearest neighbors, inverse distance weighting, and bicubic interpolation are supported.
- Parameters:
array (xr.DataArray) – The xarray DataArray defining the regular grid in
ndimensions. Must be 2D, 3D, or 4D.- Raises:
NotImplementedError – if the number of dimensions in the array is less than 2 or more than 4.
Notes
Automatic Detection:
The interpolator automatically detects:
Geodetic coordinates: If lon/lat are found via CF conventions (units attribute).
Temporal axes: If a coordinate has dtype=’datetime64’, it will be treated as a temporal axis with proper interpolation
Dimension count: Automatically selects Grid2D, Grid3D, or Grid4D
Geodetic Detection (CF Conventions):
Longitude axes are detected if the coordinate has units attribute matching:
degrees_east,degree_east,degree_E,degrees_E,degreeE, ordegreesELatitude axes are detected if the coordinate has units attribute matching:
degrees_north,degree_north,degree_N,degrees_N,degreeN, ordegreesNTemporal Detection:
Any coordinate with dtype containing ‘datetime64’ is automatically treated as a temporal axis.
Examples
2D sea surface temperature
>>> sst = xr.open_dataarray("sst.nc") # (lon, lat) >>> interp = RegularGridInterpolator(sst) >>> result = interp( ... {"lon": [10.5, 20.3], "lat": [45.2, -30.1]}, method="bilinear" ... )
3D ocean temperature with depth
>>> temp = xr.open_dataarray("temp.nc") # (lon, lat, depth) >>> interp = RegularGridInterpolator(temp) >>> result = interp( ... {"lon": [10.5], "lat": [45.2], "depth": [25.0]}, ... method="bilinear", ... )
3D SST time series (automatic temporal handling)
>>> sst_time = xr.open_dataarray("sst_time.nc") # (lon, lat, time) >>> interp = RegularGridInterpolator(sst_time) >>> result = interp( ... { ... "lon": [10.5], ... "lat": [45.2], ... "time": np.array(["2020-01-01"], dtype="datetime64"), ... }, ... method="bilinear", ... )
Initialize the interpolator from an Xarray data array.
- Parameters:
array (xr.DataArray) – The xarray DataArray to interpolate. Must be 2D, 3D, or 4D.
- Raises:
NotImplementedError – If array is not 2D, 3D, or 4D.
Attributes
Get the instance handling the regular grid for interpolations.
Get the number of array dimensions.
Special Methods
__call__(coords[, method])Interpolate at coordinates.