pyresample.future.resamplers package


pyresample.future.resamplers.nearest module

Nearest neighbor resampler.

class pyresample.future.resamplers.nearest.KDTreeNearestXarrayResampler(source_geo_def: SwathDefinition | CoordinateDefinition | AreaDefinition, target_geo_def: SwathDefinition | CoordinateDefinition | AreaDefinition, cache=None)

Bases: Resampler

Resampler using the basic nearest neighbor algorithm.


Generate a mask array where data is invalid.

This is used by resample() to determine what mask to pass to precompute(). It may be useful for users to use this manually for special cases of wanting to call precompute manually.

get_sample_from_neighbor_info(data, valid_input_index, index_array, neighbors=1, 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.

  • 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().


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:


precompute(mask=None, radius_of_influence=None, epsilon=0)

Generate neighbor indexes using geolocation information and optional data mask.

  • mask (ArrayLike, optional) – Boolean array where True represents invalid pixels in the data array to be resampled in the future. This allows the indexes computed by this method and used during resampling to filter out invalid values and produce a result with more overall valid pixels. If provided then pre-computed results will not be cached as it is assumed that the mask will likely change for every input array.

  • 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.

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

resample(data, mask_area=None, fill_value=nan, radius_of_influence=None, epsilon=0)

Resample input data from the source geometry to the target geometry.

  • data (ArrayLike) – Data to be resampled

  • mask_area (bool or ArrayLike) – Mask geolocation data where data values are invalid. This should be used when data values may affect what neighbors are considered valid. For complex masking behavior this can also be the mask array to use instead of the resampler computing it on the fly. This is useful for non-xarray DataArrays that don’t have related metadata like “_FillValue”. By default this is None which means a mask will be created automatically for SwathDefinition input. Set to False to disable any masking and True to ensure a mask is created. See precompute() for more information.

  • fill_value (int or 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() for DataArray output and np.isnan(res) for dask and numpy arrays.

  • radius_of_influence (float, optional) – Passed directly to precompute().

  • epsilon (float, optional) – Passed directly to precompute().


Array-like object of the same type as data, resampled to the target geographic geometry.

property version: str

Get the current version of this class used for hashing and caching.

pyresample.future.resamplers.nearest.query_no_distance(target_lons, target_lats, valid_output_index, mask=None, valid_input_index=None, neighbours=None, epsilon=None, radius=None, kdtree=None)

Query the kdtree. No distances are returned.

NOTE: Dask array arguments must always come before other keyword arguments

for da.blockwise arguments to work.

pyresample.future.resamplers.registry module

Registry of resampler classes.

pyresample.future.resamplers.registry.create_resampler(src_geom, dst_geom, resampler: str = None, cache=None, **kwargs) Resampler

Create instance of a Resampler with the provided arguments.

  • src_geom – Geometry object defining the source geographic region that input data lies on and will be resampled from.

  • dst_geom – Geometry object defining the destination geographic region that input data will be resampled to.

  • resampler – The name of a resampler class that has been previously registered with resampler_registry() and will be instantiated. If not provided then a registered Resampler class will be chosen based on the geometry types provided. This is currently always the ‘nearest’ (nearest neighbor) resampler.

  • cache – ResampleCache instance used by the resampler to cache intermediate results for improved resampling performance on multiple executions or future use of the resampler.

  • kwargs – Additional keyword arguments to pass to the Resampler. Note that most resamplers do not have additional keyword arguments on creation, but instead have extra arguments passed when their resample methods are called.

pyresample.future.resamplers.registry.list_resamplers() list[str, ...]

Get sorted list of registered resamplers.

pyresample.future.resamplers.registry.register_resampler(resampler_name: str, resampler_cls: Type[Resampler]) None

Register Resampler subclass for future use.

  • resampler_name – Name of the resampler in the registry. This name can then be used in functions like create_resampler().

  • resampler_cls – Subclass of Resampler that will be added to the registry.


Register a custom class:

register_resampler("my_resampler", MyResamplerClass)

Register as a plugin from third-party package (in your

entry_points = {
    "pyresample.resamplers": [
        "my_resampler = mypkg.mymodule:MyResamplerClass",
pyresample.future.resamplers.registry.unregister_resampler(resampler_name: str) None

Remove previously registered Resampler so it can’t be used anymore.

pyresample.future.resamplers.registry.with_loaded_registry(callable: Callable) Callable

Load and verify registry plugins before calling the decorated object.

Note: This decorator is structured in a way that this plugin loading only happens on the usage of the provided callable instead of on import time.

pyresample.future.resamplers.resampler module

Base resampler class made for subclassing.

class pyresample.future.resamplers.resampler.Resampler(source_geo_def: CoordinateDefinition | AreaDefinition, target_geo_def: CoordinateDefinition | AreaDefinition, cache=None)

Bases: object

Base abstract resampler class.


Do the precomputation.

This is an optional step if the subclass wants to implement more complex features like caching or can share some calculations between multiple datasets to be processed.


Resample input data from the source geometry to the target geometry.


data (ArrayLike) – Data to be resampled

Returns (ArrayLike): Data resampled to the target area

abstract property version: str

Get version number string of the current resampler for hashing and caching.

In cases where a resampler has changed enough that past cached files are not usable then this version number will be incremented to distinguish between the cache entries.

pyresample.future.resamplers.resampler.add_crs_xy_coords(data_arr, area)

Add and x/y or lons/lats to coordinates.

For SwathDefinition or GridDefinition areas this will add a crs coordinate and coordinates for the 2D arrays of lons and lats.

For AreaDefinition areas this will add a crs coordinate and the 1-dimensional x and y coordinate variables.

pyresample.future.resamplers.resampler.add_xy_coords(data_arr, area, crs=None)

Assign x/y coordinates to DataArray from provided area.

If ‘x’ and ‘y’ coordinates already exist then they will not be added.


Returns (xarray.DataArray): Updated DataArray object

pyresample.future.resamplers.resampler.hash_dict(the_dict: dict, existing_hash: HASH | None = None) HASH

Calculate a hash for a dictionary and optionally update an existing hash.

pyresample.future.resamplers.resampler.hash_resampler_geometries(source_geo_def, target_geo_def, **kwargs) str

Get hash for the geometries used by a resampler with extra kwargs.

pyresample.future.resamplers.resampler.update_resampled_coords(old_data, new_data, new_area)

Add coordinate information to newly resampled DataArray.


Module contents

Collection of resampler subclasses.