interpolate

  • Interpolators for spatial data

Source code

pyTMD.interpolate.interp1d(x: float | ndarray, xp: ndarray, fp: ndarray, **kwargs)[source]

Vectorized one-dimensional linear interpolation

Parameters:
x: float | np.ndarray

x-coordinate(s) of the interpolated values

xp: np.ndarray

x-coordinates of the data points

fp: np.ndarray

y-coordinates of the data points

extrapolate: str, default = ‘linear’

Method of extrapolation

  • 'linear'

  • 'nearest'

Returns:
f: np.ndarray

Interpolated values at new coordinates

pyTMD.interpolate.inpaint(xs: ndarray, ys: ndarray, zs: ndarray, N: int = 0, s0: int = 3, power: int = 2, epsilon: float = 2.0, is_geographic: bool = False, **kwargs)[source]

Inpaint over missing data in a two-dimensional array using a penalized least-squares method based on discrete cosine transforms [24, 80]

Parameters:
xs: np.ndarray

x-coordinates

ys: np.ndarray

y-coordinates

zs: np.ndarray

Data with masked values

N: int, default 0

Number of iterations (0 for nearest neighbors)

s0: int, default 3

Smoothing factor

power: int, default 2

Power for lambda function

epsilon: float, default 2.0

Relaxation factor

is_geographic: bool, default False

Input grid is in geographic coordinates

workers: int, default 1

Number of parallel workers for KD-tree query

Returns:
z0: np.ndarray

Data with inpainted (filled) values

pyTMD.interpolate.extrapolate(xs: ndarray, ys: ndarray, zs: ndarray, X: ndarray, Y: ndarray, k: int = 1, fill_value: float = None, cutoff: int | float = inf, is_geographic: bool = True, **kwargs)[source]

Spatially extrapolate values beyond model domain using KD-trees and nearest-neighbor (NN) or inverse distance weighting (IDW)

Parameters:
xs: np.ndarray

x-coordinates of tidal model

ys: np.ndarray

y-coordinates of tidal model

zs: np.ndarray

Tide model data

X: np.ndarray

Output x-coordinates

Y: np.ndarray

Output y-coordinates

k: int, default 1

Number of nearest neighbors to use for extrapolation

fill_value: float, default np.nan

Invalid value

dtype: np.dtype, default np.float64

Output data type

cutoff: float, default np.inf

Return only neighbors within distance (kilometers)

Set to np.inf to extrapolate for all points

is_geographic: bool, default True

Input grid is in geographic coordinates

Returns:
data: xr.DataArray

Interpolated data

pyTMD.interpolate._to_cartesian(x: ndarray, y: ndarray, is_geographic: bool = True)[source]

Convert input coordinates to an array of points in a Cartesian coordinate system

Parameters:
x: np.ndarray

x-coordinates to be converted

y: np.ndarray

y-coordinates to be converted

is_geographic: bool, default True

Coordinates are geographic

Returns:
points: np.ndarray

Output points in Cartesian coordinates

pyTMD.interpolate._build_tree(points: ndarray, **kwargs)[source]

Build a KD-tree to search for neighboring points

Parameters:
points: np.ndarray

Input points in Cartesian coordinates

kwargs: dict

Additional keyword arguments for scipy.spatial.KDTree

Returns:
tree: scipy.spatial.KDTree

KD-tree from input points

pyTMD.interpolate._query_tree(tree: KDTree, points: ndarray, flattened: ndarray, k: int = 1, power: int = 2, cutoff: int | float = inf, fill_value: float = None, **kwargs)[source]

Extrapolation of valid model data using KD-trees using nearest-neighbor (NN) or inverse distance weighting (IDW)

Parameters:
tree: scipy.spatial.KDTree

KD-tree of valid points to query

points: np.ndarray

Output points in Cartesian coordinates

flattened: np.ndarray

Valid data array to be extrapolated

k: int, default 1

Number of nearest neighbors to use for extrapolation

power: int, default 2

Power for inverse distance weighting (IDW) extrapolation

cutoff: float, default np.inf

Return only neighbors within distance (kilometers)

fill_value: float, default None

Invalid value

dtype: np.dtype, default from input data

Output data type

workers: int, default 1

Number of parallel workers to use for KD-tree query

Set to -1 to use all available cores

Returns:
data: xr.DataArray

Extrapolated data

pyTMD.interpolate.barycentric(xv: ndarray, yv: ndarray, ze: ndarray, x: ndarray, y: ndarray, order: int = 1, **kwargs)[source]

Interpolation of unstructured model data using a barycentric method

Parameters:
xv: np.ndarray

x-coordinates of triangle vertices

yv: np.ndarray

y-coordinates of triangle vertices

ze: np.ndarray

Unstructured model data at elements

x: np.ndarray

Output x-coordinates

y: np.ndarray

Output y-coordinates

order: int, default 1

Polynomial order of the triangular elements

  • 1: linear

  • 2: quadratic

Returns:
data: xr.DataArray

Interpolated data

pyTMD.interpolate._to_barycentric(xv: ndarray, yv: ndarray, x: ndarray, y: ndarray)[source]

Convert coordinates to barycentric space

Parameters:
xv: np.ndarray

x-coordinates of triangle vertices

yv: np.ndarray

y-coordinates of triangle vertices

x: np.ndarray

Output x-coordinates

y: np.ndarray

Output y-coordinates

Returns:
xi: np.ndarray

Normalized barycentric (areal) xi-coordinates

eta: np.ndarray

Normalized barycentric (areal) eta-coordinates

pyTMD.interpolate._inside_triangle(xi: ndarray, eta: ndarray, atol: float = 1e-08)[source]

Check if point is within the triangular area

Parameters:
xi: np.ndarray

Normalized barycentric (areal) xi-coordinates

eta: np.ndarray

Normalized barycentric (areal) eta-coordinates

atol: float = 1e-8

Absolute tolerance parameter

Returns:
valid: np.ndarray

Mask for coordinates

pyTMD.interpolate._shape_functions(xi: ndarray, eta: ndarray, order: int)[source]

Get the interpolating shape functions for a polynomial order

Parameters:
xi: np.ndarray

Normalized barycentric (areal) xi-coordinates

eta: np.ndarray

Normalized barycentric (areal) eta-coordinates

order: int

Polynomial order of the triangular elements

  • 1: linear

  • 2: quadratic

Returns:
N: list

Shape functions in barycentric space

pyTMD.interpolate._winding_number(xv: ndarray, yv: ndarray)[source]

Calculate the winding number of a triangle by taking the cross-product of the vertex vectors

Parameters:
xv: np.ndarray

x-coordinates of triangle vertices

yv: np.ndarray

y-coordinates of triangle vertices

Returns:
wind: np.ndarray

Winding number of the triangle

pyTMD.interpolate.slerp(x: float | ndarray, y: float | ndarray, z: float | ndarray, u: float | ndarray, v: float | ndarray, w: float | ndarray, n: int = 10, eps: float = 1e-10)[source]

Geometric spherical linear interpolation between two points [67]

Parameters:
x: float or np.ndarray

x-coordinate of the first vector

y: float or np.ndarray

y-coordinate of the first vector

z: float or np.ndarray

z-coordinate of the first vector

u: float or np.ndarray

x-coordinate of the second vector

v: float or np.ndarray

y-coordinate of the second vector

w: float or np.ndarray

z-coordinate of the second vector

n: int, default 10

Number of interpolation points to return

eps: float, default 1e-10

Minimum angle between the two vectors to perform interpolation

Returns:
a: np.ndarray

x-coordinates of the interpolated points

b: np.ndarray

y-coordinates of the interpolated points

c: np.ndarray

z-coordinates of the interpolated points