pylawr.remap#

pylawr.remap.base.BaseRemap()

The BaseRemap is a base class for all remap subclasses.

pylawr.remap.NearestNeighbor([n_neighbors, ...])

NearestNeighbur implements the nearest neighbor remapping.

pylawr.remap.OrdinaryKriging([kernel, ...])

Ordinary kriging is the default kriging class for interpolation purpose.

pylawr.remap.SimpleKriging([kernel, ...])

Simple kriging is the base class for all kriging instances.

pylawr.remap.tree.TreeRemap([n_neighbors, ...])

The TreeRemap is a base class for all remapping subclasses, where the n nearest neighbors are used.

pylawr.remap.base.BaseRemap#

class pylawr.remap.base.BaseRemap[source]#

Bases: object

The BaseRemap is a base class for all remap subclasses.

_array_postprocess(remapped_data, original_data)[source]#

Replace the grid of the stacked data with the output grid. Unstack the stacked data and set the output grid as grid of the returned data.

Parameters
  • remapped_data (numpy.ndarray) – The remapped data which should be converted into a xarray with grid_out’s coordinates.

  • original_data (xarray.DataArray) – This original data array is used as template for the new data array.

Returns

gridded_data – The unstacked data with replaced grid. The output grid is accessible with :py:attr:gridded_data.lawr.grid.

Return type

xarray.DataArray

_prepare_grid(grid)[source]#
abstract _remap_method(data)[source]#
_stack_grid_coords(data)[source]#

The data is stacked along matching grid coordinates. It is further checked whether the array can use this grid.

Parameters

data (xarray.DataArray) – This data is stacked.

Returns

stacked_data – The stacked data based on the coordinates names of the input grid. The grid coordinates are stacked to a new grid coordinate.

Return type

xarray.DataArray

abstract fit(grid_in, grid_out)[source]#

Fit the remapping for given grids. If the remapping is refitted and only the output grid changed the old point neighbours are recovered when possible. If the input grid in a refitting the old tree is reused.

Parameters
  • grid_in (child of pylawr.grid.BaseGrid) – The data is remapped from this grid to another grid. This grid needs to have get_altitude() and get_lat_lon().

  • grid_out (child of pylawr.grid.BaseGrid) – The data is remapped from another grid to this grid. This grid needs to have get_altitude() and get_lat_lon().

abstract property fitted#

Check if the remapping is fitted.

Returns

fitted – If this remap object is fitted.

Return type

bool

optimize(data, warm_start=False)[source]#

Optimize the parameter of the remapping based on given data.

Parameters
  • data (xarray.DataArray) – The data is used to optimize the parameters of this remapping.

  • warm_start (bool, optional) – If the last optimized values should used as starting values for this optimization loop. Default is False.

Returns

self – This optimized interpolation instance.

Return type

Interpolation

remap(data)[source]#

Remap the given data with the optimized parameters and the given grids.

Parameters

data (xarray.DataArray) – This data is remapped from grid_in to grid_out. The data should have the same shape as the input grid shape.

Returns

remapped_data – The array with the remapped data based on grid_out. The grid_out is accessible via remapped_data.lawr.Grid.

Return type

xarray.DataArray

Raises

NotFittedError – A NotFittedError is raised if the fit method was not called yet.

Warning

Make sure that there are no NaNs are within the grid because the mapping from input grid to output grid is static and therefore not “NaN-safe”. To fill up NaN values you can use pylawr.filter.interpolation.

pylawr.remap.NearestNeighbor#

class pylawr.remap.NearestNeighbor(n_neighbors=1, max_dist=None)[source]#

Bases: TreeRemap

NearestNeighbur implements the nearest neighbor remapping. To find the nearest neighbor a kd-tree is used. The n-nearest neighbors are averaged to obtain the field value.

Parameters
  • n_neighbors (int, optional) – This number of nearest neighbour values is averaged to obtain the remapped value. Default is 1.

  • max_dist (float or None, optional) – Use for remapping only neighbors within this distance to the interpolated point. If this maximum distance is None (default), no restriction is used (this number is internally converted to infinity), while an float number indicates a constrain in meters.

_remap_method(data)[source]#

This remap method uses the average of the n-nearest neighbors as field value.

pylawr.remap.OrdinaryKriging#

class pylawr.remap.OrdinaryKriging(kernel=None, n_neighbors=10, max_dist=None, alpha=None)[source]#

Bases: SimpleKriging

Ordinary kriging is the default kriging class for interpolation purpose. Here, we assume that the interpolated / remapped field is stationary and the expectation of this field can be estimated by the mean. This kriging is based on localized kriging, where the number of localized neighbors can be specified. In this ordinary kriging, the weights are normalized to unity. The resulting weights are physically plausible. This constrain is introduced as lagrange multiplier.

Parameters
  • kernel (child of BaseKernel) –

  • None (or) – This kernel is used to transform distances into a new feature space, where the features can be used as non-linear predictors. In kriging this is also called covariance function. This covariance function should resemble a typical precipitation covariance field. If this kernel is None, a default radial basis function kernel with white noise is used. Default is None.

  • optional – This kernel is used to transform distances into a new feature space, where the features can be used as non-linear predictors. In kriging this is also called covariance function. This covariance function should resemble a typical precipitation covariance field. If this kernel is None, a default radial basis function kernel with white noise is used. Default is None.

  • n_neighbors (int, optional) – This is the number of neighbors, used to interpolate / remap a specific point. Default is 10.

  • max_dist (float or None, optional) – Use for remapping only neighbors within this distance to the interpolated point. If this maximum distance is None (default), no restriction is used (this number is internally converted to infinity), while an float number indicates a constrain in meters.

  • alpha (float or None, optional) – This alpha value is used as regularization parameter to stabilize the kriging solution. This alpha parameter is added to the diagonal of the covariance matrix. This regularization is a kind of Tikhonov regularization and adds a small white noise kernel to the data.

_const_rkhs_matrix(src_points)[source]#
_get_k_matrix(dists)[source]#
_remap_method(data)[source]#

pylawr.remap.SimpleKriging#

class pylawr.remap.SimpleKriging(kernel=None, n_neighbors=10, max_dist=None, alpha=None)[source]#

Bases: TreeRemap

Simple kriging is the base class for all kriging instances. Simple kriging is also known as Gaussian Process in machine learning with distances as specified predictor. Here, we assume that the interpolated / remapped field is stationary and the expectation of this field is zero everywhere. This kriging is based on localized kriging, where the number of localized neighbors can be specified. In this simple kriging, the weights are not normalized to 1 such that resulting weights are not physical plausible. For physical plausible kriging, please use OrdinaryKriging.

Parameters
  • kernel (child of BaseKernel) –

  • None (or) – This kernel is used to transform distances into a new feature space, where the features can be used as non-linear predictors. In kriging this is also called covariance function. This covariance function should resemble a typical precipitation covariance field. If this kernel is None, a default radial basis function kernel with white noise is used. Default is None.

  • optional – This kernel is used to transform distances into a new feature space, where the features can be used as non-linear predictors. In kriging this is also called covariance function. This covariance function should resemble a typical precipitation covariance field. If this kernel is None, a default radial basis function kernel with white noise is used. Default is None.

  • n_neighbors (int, optional) – This is the number of neighbors, used to interpolate / remap a specific point. Default is 10.

  • max_dist (float or None, optional) – Use for remapping only neighbors within this distance to the interpolated point. If this maximum distance is None (default), no restriction is used (this number is internally converted to infinity), while an float number indicates a constrain in meters.

  • alpha (float or None, optional) – This alpha value is used as regularization parameter to stabilize the kriging solution. This alpha parameter is added to the diagonal of the covariance matrix. This regularization is a kind of Tikhonov regularization and adds a small white noise kernel to the data.

_const_rkhs_matrix(src_points)[source]#
static _estimate_weights(rkhs, target)[source]#
static _get_distance_matrix(points)[source]#
_get_k_matrix(dists)[source]#
_remap_method(data)[source]#
property alpha#
property covariance#

Get the point specific interpolation variance of this kriging instance. Every point variance is independently estimated. This variance reflects the interpolation uncertainty, if the field is stationary and has no bias compared to the kriging assumptions.

Returns

post_cov – This is the estimated posterior variance, based on specified kernel and estimated weights.

Return type

numpy.ndarray

Raises

NotFittedError – A NotFittedError is raised if fit was not called yet.

fit(grid_in, grid_out)[source]#

Fit the remapping for given grids. This fitting method searches the nearest neighbor points via kd-tree and sets corresponding distances and locations. This fitting method also estimates the kriging weights based on these distances and locations.

Parameters
  • grid_in (child of pylawr.grid.BaseGrid) – The data is remapped from this grid to another grid. This grid needs to have get_altitude() and get_lat_lon().

  • grid_out (child of pylawr.grid.BaseGrid) – The data is remapped from another grid to this grid. This grid needs to have get_altitude() and get_lat_lon().

property kernel#

pylawr.remap.tree.TreeRemap#

class pylawr.remap.tree.TreeRemap(n_neighbors=20, max_dist=None)[source]#

Bases: BaseRemap

The TreeRemap is a base class for all remapping subclasses, where the n nearest neighbors are used. The remapping is based on scipy.spatial.cKDTree to get the nearest neighbor points.

Parameters
  • n_neighbors (int, optional) – For every output grid point this number of neighbors is searched within the input grid points. Default is 20.

  • max_dist (float or None, optional) – Use for remapping only neighbors within this distance to the interpolated point. If this maximum distance is None (default), no restriction is used (this number is internally converted to infinity), while an float number indicates a constrain in meters.

static _all_neighbors_nan(neighbor_vals)[source]#
_get_tree(prepared_src, prepared_trg)[source]#
_neighbors_not_available(neighbor_vals)[source]#
property _out_of_bound_locs#
fit(grid_in, grid_out)[source]#

Fit the remapping for given grids. This fitting method searches the nearest neighbor points via kd-tree and sets corresponding distances and locations.

Parameters
  • grid_in (child of pylawr.grid.BaseGrid) – The data is remapped from this grid to another grid. This grid needs to have get_altitude() and get_lat_lon().

  • grid_out (child of pylawr.grid.BaseGrid) – The data is remapped from another grid to this grid. This grid needs to have get_altitude() and get_lat_lon().

property fitted#

Check if the remapping is fitted.

Returns

fitted – If this tree based remap object is fitted.

Return type

bool

property max_dist#