# pyresample.kd_tree module

Handle reprojection of geolocated data.

Several types of resampling are supported.

exception pyresample.kd_tree.EmptyResult

  Bases: `ValueError`

  No valid data is produced.

class pyresample.kd_tree.XArrayResamplerNN(source_geo_def, target_geo_def, radius_of_influence=None, neighbours=1, epsilon=0)

  Bases: `object`

  Resampler for Xarray DataArray objects with the nearest neighbor algorithm.

  Resampler for xarray DataArrays using a nearest neighbor algorithm.

  Parameters:

    - source_geo_def (object) – Geometry definition of source

    - target_geo_def (object) – Geometry definition of target

    - radius_of_influence (float, optional) – Cut off distance in geocentric meters. If not provided this will be estimated based on the source and target geometry definition.

    - neighbours (int, optional) – The number of neigbours to consider for each grid point. Default 1. Currently 1 is the only supported number.

    - epsilon (float, optional) – Allowed uncertainty in meters. Increasing uncertainty reduces execution time

  __init__(source_geo_def, target_geo_def, radius_of_influence=None, neighbours=1, epsilon=0)

    Resampler for xarray DataArrays using a nearest neighbor algorithm.

    Parameters:

      - source_geo_def (object) – Geometry definition of source

      - target_geo_def (object) – Geometry definition of target

      - radius_of_influence (float, optional) – Cut off distance in geocentric meters. If not provided this will be estimated based on the source and target geometry definition.

      - neighbours (int, optional) – The number of neigbours to consider for each grid point. Default 1. Currently 1 is the only supported number.

      - epsilon (float, optional) – Allowed uncertainty in meters. Increasing uncertainty reduces execution time

  get_neighbour_info(mask=None)

    Return neighbour info.

    Returns:

      - (valid_input_index, valid_output_index,

      - index_array, distance_array) (tuple of numpy arrays) – Neighbour resampling info

  get_sample_from_neighbour_info(data, fill_value=nan)

    Get the pixels matching the target area.

    This method should work for any dimensionality of the provided data array as long as the geolocation dimensions match in size and name in `data.dims`. Where source area definition are AreaDefinition objects the corresponding dimensions in the data should be `('y', 'x')`.

    This method also attempts to preserve chunk sizes of dask arrays, but does require loading/sharing the fully computed source data before it can actually compute the values to write to the destination array. This can result in large memory usage for large source data arrays, but is a necessary evil until fancier indexing is supported by dask and/or pykdtree.

    Parameters:

      - data (xarray.DataArray) – Source data pixels to sample

      - fill_value (float) – Output fill value when no source data is near the target pixel. When omitted, if the input data is an integer array then the maximum value for that integer type is used, but otherwise, NaN is used and can be detected in the result with `res.isnull()`.

    Returns:

      The resampled array. The dtype of the array will

        be the same as the input data. Pixels with no matching data from the input array will be filled (see the fill_value parameter description above).

    Return type:

      dask.array.Array

  query_resample_kdtree(resample_kdtree, tlons, tlats, valid_oi, mask)

    Query kd-tree on slice of target coordinates.

pyresample.kd_tree.get_neighbour_info(source_geo_def, target_geo_def, radius_of_influence, neighbours=8, epsilon=0, reduce_data=True, nprocs=1, segments=None)

  Return neighbour info.

  Parameters:

    - source_geo_def (object) – Geometry definition of source

    - target_geo_def (object) – Geometry definition of target

    - radius_of_influence (float) – Cut off distance in meters

    - neighbours (int, optional) – The number of neigbours to consider for each grid point

    - epsilon (float, optional) – Allowed uncertainty in meters. Increasing uncertainty reduces execution time

    - reduce_data (bool, optional) – Perform initial coarse reduction of source dataset in order to reduce execution time

    - nprocs (int, optional) – Number of processor cores to be used

    - segments (int or None) – Number of segments to use when resampling. If set to None an estimate will be calculated

  Returns:

    - (valid_input_index, valid_output_index,

    - index_array, distance_array) (tuple of numpy arrays) – Neighbour resampling info

pyresample.kd_tree.get_sample_from_neighbour_info(resample_type, output_shape, data, valid_input_index, valid_output_index, index_array, distance_array=None, weight_funcs=None, fill_value=0, with_uncert=False)

  Resamples swath based on neighbour info.

  Parameters:

    - resample_type ({'nn', 'custom'}) – ‘nn’: Use nearest neighbour resampling ‘custom’: Resample based on weight_funcs

    - output_shape ((int, int)) – Shape of output as (rows, cols)

    - data (numpy array) – Source data

    - valid_input_index (numpy array) – valid_input_index from get_neighbour_info

    - valid_output_index (numpy array) – valid_output_index from get_neighbour_info

    - index_array (numpy array) – index_array from get_neighbour_info

    - distance_array (numpy array, optional) – distance_array from get_neighbour_info Not needed for ‘nn’ resample type

    - weight_funcs (list of function objects or function object, optional) – List of weight functions f(dist) to use for the weighting of each channel 1 to k. If only one channel is resampled weight_funcs is a single function object. Must be supplied when using ‘custom’ resample type

    - fill_value (int, float, numpy floating, numpy integer or None, optional) – Set undetermined pixels to this value. If fill_value is None a masked array is returned with undetermined pixels masked

  Returns:

    result – Source data resampled to target geometry

  Return type:

    numpy array

pyresample.kd_tree.resample_custom(source_geo_def, data, target_geo_def, radius_of_influence, weight_funcs, neighbours=8, epsilon=0, fill_value=0, reduce_data=True, nprocs=1, segments=None, with_uncert=False)

  Resamples data using kd-tree custom radial weighting neighbour approach.

  Parameters:

    - source_geo_def (object) – Geometry definition of source

    - data (numpy array) – Array of single channel data points or (source_geo_def.shape, k) array of k channels of datapoints

    - target_geo_def (object) – Geometry definition of target

    - radius_of_influence (float) – Cut off distance in meters

    - weight_funcs (list of function objects or function object) – List of weight functions f(dist) to use for the weighting of each channel 1 to k. If only one channel is resampled weight_funcs is a single function object.

    - neighbours (int, optional) – The number of neigbours to consider for each grid point

    - epsilon (float, optional) – Allowed uncertainty in meters. Increasing uncertainty reduces execution time

    - fill_value ({int, None}, optional) – Set undetermined pixels to this value. If fill_value is None a masked array is returned with undetermined pixels masked

    - reduce_data (bool, optional) – Perform initial coarse reduction of source dataset in order to reduce execution time

    - nprocs (int, optional) – Number of processor cores to be used

    - segments ({int, None}) – Number of segments to use when resampling. If set to None an estimate will be calculated

  Returns:

    - data (numpy array (default)) – Source data resampled to target geometry

    - data, stddev, counts (numpy array, numpy array, numpy array (if with_uncert == True)) – Source data resampled to target geometry. Weighted standard devaition for all pixels having more than one source value Counts of number of source values used in weighting per pixel

pyresample.kd_tree.resample_gauss(source_geo_def, data, target_geo_def, radius_of_influence, sigmas, neighbours=8, epsilon=0, fill_value=0, reduce_data=True, nprocs=1, segments=None, with_uncert=False)

  Resamples data using kd-tree gaussian weighting neighbour approach.

  Parameters:

    - source_geo_def (object) – Geometry definition of source

    - data (numpy array) – Array of single channel data points or (source_geo_def.shape, k) array of k channels of datapoints

    - target_geo_def (object) – Geometry definition of target

    - radius_of_influence (float) – Cut off distance in meters

    - sigmas (list of floats or float) – List of sigmas to use for the gauss weighting of each channel 1 to k, w_k = exp(-dist^2/sigma_k^2). If only one channel is resampled sigmas is a single float value.

    - neighbours (int, optional) – The number of neigbours to consider for each grid point

    - epsilon (float, optional) – Allowed uncertainty in meters. Increasing uncertainty reduces execution time

    - fill_value ({int, None}, optional) – Set undetermined pixels to this value. If fill_value is None a masked array is returned with undetermined pixels masked

    - reduce_data (bool, optional) – Perform initial coarse reduction of source dataset in order to reduce execution time

    - nprocs (int, optional) – Number of processor cores to be used

    - segments (int or None) – Number of segments to use when resampling. If set to None an estimate will be calculated

    - with_uncert (bool, optional) – Calculate uncertainty estimates

  Returns:

    - data (numpy array (default)) – Source data resampled to target geometry

    - data, stddev, counts (numpy array, numpy array, numpy array (if with_uncert == True)) – Source data resampled to target geometry. Weighted standard devaition for all pixels having more than one source value Counts of number of source values used in weighting per pixel

pyresample.kd_tree.resample_nearest(source_geo_def, data, target_geo_def, radius_of_influence, epsilon=0, fill_value=0, reduce_data=True, nprocs=1, segments=None)

  Resamples data using kd-tree nearest neighbour approach.

  Parameters:

    - source_geo_def (object) – Geometry definition of source

    - data (numpy array) – 1d array of single channel data points or (source_size, k) array of k channels of datapoints

    - target_geo_def (object) – Geometry definition of target

    - radius_of_influence (float) – Cut off distance in meters

    - epsilon (float, optional) – Allowed uncertainty in meters. Increasing uncertainty reduces execution time

    - fill_value (int, float, numpy floating, numpy integer or None, optional) – Set undetermined pixels to this value. If fill_value is None a masked array is returned with undetermined pixels masked

    - reduce_data (bool, optional) – Perform initial coarse reduction of source dataset in order to reduce execution time

    - nprocs (int, optional) – Number of processor cores to be used

    - segments (int or None) – Number of segments to use when resampling. If set to None an estimate will be calculated

  Returns:

    data – Source data resampled to target geometry

  Return type:

    numpy array