Interpolation
The interpolation module contains functionalities for interpolating geospatial data in spherical and planar geometry in one, two, or three dimensions (latitude, longitude, elevation).
In the two-dimensional case, the interpolation can be made on both regular and irregular grids.
In the three-dimensional case, only interpolation on regular grids has been implemented, although an extension to irregular grids (following the same methodology as in the two-dimensional case) should be straightforward.
- Contents:
GridData2D class: GridData3D class: Interpolator2D class: Interpolator3D class: Uniform2D class: Uniform3D class: DepthInterpolator3D class
- class kadlu.geospatial.interpolation.DepthInterpolator3D(values, depths, method='quadratic')[source]
Bases:
object
Class for interpolating 3D (lat,lon,depth) geospatial data that only varies with depth.
- Attributes:
- values: 1d or 3d numpy array
Values to be interpolated
- depths: 1d numpy array
Depth values
- method{‘linear’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, ‘next’}, optional
Interpolation method. Default is quadratic.
- interp(lat, lon, z, grid=False, squeeze=True)[source]
Interpolate using spherical coordinate system (lat-lon).
lat, lon, z can be floats or arrays.
If grid is set to False, the interpolation will be evaluated at the coordinates (lat_i, lon_i, z_i), where lat=(lat_1,…,lat_N), lon=(lon_1,…,lon_N) and z=(z_,…,z_K). Note that in this case, lat and lon must have the same length.
If grid is set to True, the interpolation will be evaluated at all combinations (lat_i, lon_j, z_k), where lat=(lat_1,…,lat_N), lon=(lon_1,…,lon_M) and z=(z_1,…,z_K). Note that in this case, the lengths of lat and lon do not have to be the same.
- Args:
- lat: float or array
Latitude of the positions(s) where the bathymetry is to be evaluated
- lon: float or array
Longitude of the positions(s) where the bathymetry is to be evaluated
- z: float or array
Depth of the positions(s) where the interpolation is to be evaluated
- grid: bool
Specify how to combine elements of lat,lon,z.
- Returns:
zi: Interpolated bathymetry values (or derivates)
- interp_xy(x, y, z, grid=False)[source]
Interpolate using planar coordinate system (xy).
x,y,z can be floats or arrays.
If grid is set to False, the interpolation will be evaluated at the positions (x_i, y_i, z_i), where x=(x_1,…,x_N), y=(y_1,…,y_N), and z=(z_1,…,z_N). Note that in this case, x,y,z must have the same length.
If grid is set to True, the interpolation will be evaluated at all combinations (x_i, y_j, z_k), where x=(x_1,…,x_N), y=(y_1,…,y_M), and z=(z_1,…,z_K). Note that in this case, the lengths of x,y,z do not have to be the same.
- Args:
- x: float or array
x-coordinate of the positions(s) where the interpolation is to be evaluated
- y: float or array
y-coordinate of the positions(s) where the interpolation is to be evaluated
- z: float or array
Depth of the positions(s) where the interpolation is to be evaluated
- grid: bool
Specify how to combine elements of x,y,z.
- Returns:
vi: Interpolated values
- class kadlu.geospatial.interpolation.GridData2D(u, v, r, method='linear')[source]
Bases:
object
Interpolation of data on a two-dimensional irregular grid.
Essentially, a wrapper function around scipy’s interpolate.griddata.
https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.interpolate.griddata.html
An alternative to griddata could be Rbf, as discussed here:
- Attributes:
- u: 1d numpy array
data points 1st coordinate
- v: 1d numpy array
data points 2nd coordinate
- r: 1d numpy array
data values
method : {‘linear’, ‘nearest’, ‘cubic’}, optional
- class kadlu.geospatial.interpolation.GridData3D(u, v, w, r, method='linear')[source]
Bases:
object
Interpolation of data on a three-dimensional irregular grid.
Essentially, a wrapper function around scipy’s interpolate.griddata.
https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.interpolate.griddata.html
An alternative to griddata could be Rbf, as discussed here:
- Attributes:
- u: 1d numpy array
data points 1st coordinate
- v: 1d numpy array
data points 2nd coordinate
- w: 1d numpy array
data points 3rd coordinate
- r: 1d numpy array
data values
method : {‘linear’, ‘nearest’}, optional
- class kadlu.geospatial.interpolation.Interpolator2D(values, lats, lons, origin=None, method_irreg='regularize', bins_irreg_max=2000)[source]
Bases:
object
Class for interpolating 2D (lat,lon) geospatial data.
For irregular grids, the data values must be passed as a 1d array and all three arrays (values, lats, lons) must have the same length.
For regular grids, the data values must be passed as a 2d array with shape (M,N) where M and N are the lengths of the latitude and longitude array, respectively.
- Attributes:
- values: 1d or 2d numpy array
Values to be interpolated
- lats: 1d numpy array
Latitude values
- lons: 1d numpy array
Longitude values
- origin: tuple(float,float)
Reference location (origo of XY coordinate system).
- method_irreg{‘linear’, ‘nearest’, ‘cubic’, ‘regularize’}, optional
Interpolation method used for irregular grids. Note that ‘nearest’ is usually significantly faster than the ‘linear’ and ‘cubic’. If the ‘regularize’ is selected, the data is first mapped onto a regular grid by means of a linear interpolation (for points outside the area covered by the data, a nearest-point interpolation is used). The bin size of the regular grid is specified via the reg_bin argument.
- bins_irreg_max: int
Maximum number of bins along either axis of the regular grid onto which the irregular data is mapped. Only relevant if method_irreg is set to ‘regularize’. Default is 2000.
- interp(lat, lon, grid=False, squeeze=True, lat_deriv_order=0, lon_deriv_order=0)[source]
Interpolate using spherical coordinate system (latitude-longitude).
lat and lot can be floats or arrays.
If grid is set to False, the interpolation will be evaluated at the coordinates (lat_i, lon_i), where lat=(lat_1,…,lat_N) and lon=(lon_1,…,lon_N). Note that in this case, lat and lon must have the same length.
If grid is set to True, the interpolation will be evaluated at all combinations (lat_i, lon_j), where lat=(lat_1,…,lat_N) and lon=(lon_1,…,lon_M). Note that in this case, the lengths of lat and lon do not have to be the same.
Derivates are given per radians^n, where n is the overall derivative order.
- Args:
- lat: float or array
latitude of the positions(s) where the interpolation is to be evaluated
- lon: float or array
longitude of the positions(s) where the interpolation is to be evaluated
- grid: bool
Specify how to combine elements of lat and lon. If lat and lon have different lengths, specifying grid has no effect as it is automatically set to True.
- lat_deriv_order: int
Order of latitude-derivative
- lon_deriv_order: int
Order of longitude-derivative
- Returns:
zi: Interpolated values (or derivates)
- interp_xy(x, y, grid=False, x_deriv_order=0, y_deriv_order=0)[source]
Interpolate using planar coordinate system (xy).
x and y can be floats or arrays.
If grid is set to False, the interpolation will be evaluated at the positions (x_i, y_i), where x=(x_1,…,x_N) and y=(y_1,…,y_N). Note that in this case, x and y must have the same length.
If grid is set to True, the interpolation will be evaluated at all combinations (x_i, y_j), where x=(x_1,…,x_N) and y=(y_1,…,y_M). Note that in this case, the lengths of x and y do not have to be the same.
- Args:
- x: float or array
x-coordinate of the positions(s) where the interpolation is to be evaluated
- y: float or array
y-coordinate of the positions(s) where the interpolation is to be evaluated
- grid: bool
Specify how to combine elements of x and y.
- x_deriv_order: int
Order of x-derivative
- y_deriv_order: int
Order of y-derivative
- Returns:
zi: Interpolated interpolation values
- class kadlu.geospatial.interpolation.Interpolator3D(values, lats, lons, depths, origin=None, method='linear', method_irreg='regularize', bins_irreg_max=200)[source]
Bases:
object
Class for interpolating 3D (lat,lon,depth) geospatial data.
For irregular grids, the data values must be passed as a 1d array and all three arrays (values, lats, lons, depths) must have the same length.
For regular grids, the data values must be passed as a 3d array with shape (M,N,K) where M,N,K are the lengths of the latitude, longitude, and depth arrays, respectively.
- Attributes:
- values: 3d numpy array
Values to be interpolated
- lats: 1d numpy array
Latitude values
- lons: 1d numpy array
Longitude values
- depths: 1d numpy array
Depth values
- origin: tuple(float,float)
Reference location (origo of XY coordinate system).
- method{‘linear’, ‘nearest’}, optional
Interpolation method. Default is linear
- method_irreg{‘linear’, ‘nearest’, ‘regularize’}, optional
Interpolation method used for irregular grids. Note ‘nearest’ is usually significantly faster than ‘linear’’. If the ‘regularize’ is selected, the data is first mapped onto a regular grid by means of a linear interpolation (for points outside the area covered by the data, a nearest-point interpolation is used). The bin size of the regular grid is specified via the reg_bin argument.
- bins_irreg_max: int
Maximum number of bins along either axis of the regular grid onto which the irregular data is mapped. Only relevant if method_irreg is set to ‘regularize’. Default is 200.
- interp(lat, lon, z, grid=False, squeeze=True)[source]
Interpolate using spherical coordinate system (lat-lon).
lat,lot,z can be floats or arrays.
If grid is set to False, the interpolation will be evaluated at the coordinates (lat_i, lon_i, z_i), where lat=(lat_1,…,lat_N), lon=(lon_1,…,lon_N) and z=(z_,…,z_K). Note that in this case, lat and lon must have the same length.
If grid is set to True, the interpolation will be evaluated at all combinations (lat_i, lon_j, z_k), where lat=(lat_1,…,lat_N), lon=(lon_1,…,lon_M) and z=(z_1,…,z_K). Note that in this case, the lengths of lat and lon do not have to be the same.
- Args:
- lat: float or array
Latitude of the positions(s) where the bathymetry is to be evaluated
- lon: float or array
Longitude of the positions(s) where the bathymetry is to be evaluated
- z: float or array
Depth of the positions(s) where the interpolation is to be evaluated
- grid: bool
Specify how to combine elements of lat,lon,z.
- Returns:
zi: Interpolated bathymetry values (or derivates)
- interp_xy(x, y, z, grid=False)[source]
Interpolate using planar coordinate system (xy).
x,y,z can be floats or arrays.
If grid is set to False, the interpolation will be evaluated at the positions (x_i, y_i, z_i), where x=(x_1,…,x_N), y=(y_1,…,y_N), and z=(z_1,…,z_N). Note that in this case, x,y,z must have the same length.
If grid is set to True, the interpolation will be evaluated at all combinations (x_i, y_j, z_k), where x=(x_1,…,x_N), y=(y_1,…,y_M), and z=(z_1,…,z_K). Note that in this case, the lengths of x,y,z do not have to be the same.
- Args:
- x: float or array
x-coordinate of the positions(s) where the interpolation is to be evaluated
- y: float or array
y-coordinate of the positions(s) where the interpolation is to be evaluated
- z: float or array
Depth of the positions(s) where the interpolation is to be evaluated
- grid: bool
Specify how to combine elements of x,y,z.
- Returns:
vi: Interpolated values