pycontrails.core.interpolation¶
Interpolation utilities.
Functions
|
Interpolate over a grid with |
Classes
|
Support for interpolating a profile on a linear or logarithmic scale. |
|
Support for performant interpolation over a regular grid. |
|
An interface to intermediate RGI interpolation artifacts. |
- class pycontrails.core.interpolation.EmissionsProfileInterpolator(xp, fp, drop_duplicates=True)¶
Bases:
objectSupport for interpolating a profile on a linear or logarithmic scale.
This class simply wraps
numpy.interp()with fixed values for thexpandfparguments. Unlikexarray.DataArrayinterpolation, thenumpy.interp()automatically clips values outside the range of thexparray.- Parameters:
xp (
npt.NDArray[np.floating]) – Array of x-values. These must be strictly increasing and free from any nan values. Passed tonumpy.interp().fp (
npt.NDArray[np.floating]) – Array of y-values. Passed tonumpy.interp().drop_duplicates (
bool, optional) – Whether to drop duplicate values inxp. Defaults toTrue.
Examples
>>> xp = np.array([3, 7, 10, 30], dtype=float) >>> fp = np.array([0.1, 0.2, 0.3, 0.4], dtype=float) >>> epi = EmissionsProfileInterpolator(xp, fp) >>> # Interpolate a single value >>> epi.interp(5) np.float64(0.150000...)
>>> # Interpolate a single value on a logarithmic scale >>> epi.log_interp(5) np.float64(1.105171...)
>>> # Demonstrate speed up compared with xarray.DataArray interpolation >>> import time, xarray as xr >>> da = xr.DataArray(fp, dims=["x"], coords={"x": xp})
>>> inputs = [np.random.uniform(0, 31, size=200) for _ in range(1000)] >>> t0 = time.perf_counter() >>> xr_out = [da.interp(x=x.clip(3, 30)).values for x in inputs] >>> t1 = time.perf_counter() >>> np_out = [epi.interp(x) for x in inputs] >>> t2 = time.perf_counter() >>> assert np.allclose(xr_out, np_out)
>>> # We see a 100 fold speed up (more like 500x faster, but we don't >>> # want the test to fail!) >>> assert t2 - t1 < (t1 - t0) / 100
- interp(x)¶
Interpolate x against xp and fp.
- Parameters:
x (
npt.NDArray[np.floating]) – Array of x-values to interpolate.- Returns:
npt.NDArray[np.floating]– Array of interpolated y-values arising from the x-values. Thedtypeof the output array is the same as thedtypeofx.
- log_interp(x)¶
Interpolate x against xp and fp on a logarithmic scale.
- This method composes the following three functions.
numpy.log()numpy.exp()
- Parameters:
x (
npt.NDArray[np.floating]) – Array of x-values to interpolate.- Returns:
npt.NDArray[np.floating]– Array of interpolated y-values arising from the x-values.
- class pycontrails.core.interpolation.PycontrailsRegularGridInterpolator(points, values, *, method, bounds_error, fill_value)¶
Bases:
RegularGridInterpolatorSupport for performant interpolation over a regular grid.
This class is a thin wrapper around the
scipy.interpolate.RegularGridInterpolatorin order to make typical pycontrails linear interpolation use-cases more performant:Avoid
RegularGridInterpolatorconstructor validation whenmethod="linear". Ininterp(), parameters are carefully crafted to fit into the intended form, thereby making validation unnecessary.Override the
_evaluate_linear()method with a faster implementation. See the_evaluate_linear()docstring for more information.
This class should not be used directly. Instead, use the
interp()function.Changed in version 0.40.0: The
_evaluate_linear()method now uses a Cython implementation. The dtype of the output is now consistent with the dtype of the underlyingvaluesChanged in version 0.58.0: Any
methodother than"linear"now uses thescipy.interpolate.RegularGridInterpolatorimplementation. This allows for greater flexibility in themethodparameter.- Parameters:
points (
tuple[npt.NDArray[np.floating],]) – Coordinates of the grid points.values (
npt.NDArray[np.floating]) – Grid values. The shape of this array must be compatible with the coordinates.method (
str) – Passed intoscipy.interpolate.RegularGridInterpolatorbounds_error (
bool) – Passed intoscipy.interpolate.RegularGridInterpolatorfill_value (
float | np.float64 | None) – Passed intoscipy.interpolate.RegularGridInterpolator
- class pycontrails.core.interpolation.RGIArtifacts(xi_indices, norm_distances, out_of_bounds)¶
Bases:
objectAn interface to intermediate RGI interpolation artifacts.
- norm_distances¶
- out_of_bounds¶
- xi_indices¶
- pycontrails.core.interpolation.interp(longitude, latitude, level, time, da, method, bounds_error, fill_value, localize, *, indices=None, return_indices=False)¶
Interpolate over a grid with
localizeoption.Changed in version 0.25.6: Utilize scipy 1.9 upgrades to remove singleton dimensions.
Changed in version 0.26.0: Include
indicesandreturn_indicesexperimental parameters. Currently, nan values inlongitude,latitude,level, ortimeare always propagated through to the output, regardless ofbounds_error. In other words, aValueErrorfor an out of bounds coordinate is only raised if a non-nan value is out of bounds.Changed in version 0.40.0: When
return_indicesis True, an instance ofRGIArtifactsis used to store the indices artifacts.- Parameters:
longitude, latitude, level, time (
numpy.ndarray) – Coordinates of points to be interpolated. These parameters have the same meaning asxin analogy withnumpy.interp(). All four of these arrays must be 1 dimensional of the same size.da (
xarray.DataArray) – Gridded data interpolated over. Must adhere toMetDataArrayconventions. In particular, the dimensions ofdamust belongitude,latitude,level, andtime. The three spatial dimensions must be monotonically increasing withfloat64dtype. Thetimedimension must be monotonically increasing withnumpy.datetime64dtype. Assumed to be cheap to load into memory (xarray.DataArray.valuesis used without hesitation).method (
str) – Passed intoscipy.interpolate.RegularGridInterpolator.bounds_error (
bool) – Passed intoscipy.interpolate.RegularGridInterpolator.fill_value (
float | np.float64 | None) – Passed intoscipy.interpolate.RegularGridInterpolator.localize (
bool) – If True, clipdato the smallest bounding box that contains all ofcoords.indices (
tuple | None, optional) – Experimental. Provide intermediate artifacts computed byscipy.interpolate.RegularGridInterpolator._find_indices()to avoid redundant computation. If known and provided, this can speed up interpolation by avoiding an unnecessary call to_find_indices. By default, None. Must be used precisely.return_indices (
bool, optional) – If True, return output ofscipy.interpolate.RegularGridInterpolator._find_indices()in addition to interpolated values.
- Returns:
npt.NDArray[np.floating] | tuple[npt.NDArray[np.floating],RGIArtifacts]– Interpolated values with same size aslongitude. Ifreturn_indicesis True, return intermediate indices artifacts as well.