pyresample.resampler module
Base resampler class made for subclassing.
- class pyresample.resampler.BaseResampler(source_geo_def, target_geo_def)
Bases:
objectBase abstract resampler class.
Initialize resampler with geolocation information.
- Parameters:
source_geo_def (
Union[SwathDefinition,AreaDefinition]) – Geolocation definition for the data to be resampledtarget_geo_def (
Union[CoordinateDefinition,AreaDefinition]) – Geolocation definition for the area to resample data to.
- __init__(source_geo_def, target_geo_def)
Initialize resampler with geolocation information.
- Parameters:
source_geo_def (
Union[SwathDefinition,AreaDefinition]) – Geolocation definition for the data to be resampledtarget_geo_def (
Union[CoordinateDefinition,AreaDefinition]) – Geolocation definition for the area to resample data to.
- compute(data, **kwargs)
Do the actual resampling.
This must be implemented by subclasses.
- get_hash(source_geo_def=None, target_geo_def=None, **kwargs)
Get hash for the current resample with the given kwargs.
- precompute(**kwargs)
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(data, cache_dir=None, mask_area=None, **kwargs)
Resample data by calling precompute and compute methods.
Only certain resampling classes may use cache_dir and the mask provided when mask_area is True. The return value of calling the precompute method is passed as the cache_id keyword argument of the compute method, but may not be used directly for caching. It is up to the individual resampler subclasses to determine how this is used.
- Parameters:
data (xarray.DataArray) – Data to be resampled
cache_dir (str) – directory to cache precomputed results (default False, optional)
mask_area (bool) – Mask geolocation data where data values are invalid. This should be used when data values may affect what neighbors are considered valid.
kwargs – Keyword arguments to pass to both the
precomputeandcomputestages of the resampler.
Returns (xarray.DataArray): Data resampled to the target area
- pyresample.resampler.crop_data_around_area(source_geo_def, src_arrays, target_geo_def)
Crop the data around the provided area.
- pyresample.resampler.crop_source_area(source_geo_def, target_geo_def)
Crop a source area around the provided target area.
- pyresample.resampler.resample_blocks(func, src_area, src_arrays, dst_area, dst_arrays=(), chunk_size=None, dtype=None, name=None, fill_value=None, **kwargs)
Resample dask arrays blockwise.
Resample_blocks applies a function blockwise to transform data from a source area domain to a destination area domain.
- Parameters:
func – A callable to apply on the input data. This function is passed a block of src_arrays, dst_arrays in that order, followed by the kwargs, which include the fill_value. If the callable accepts a block_info keyword argument, block information is passed to it. Block information provides the source area, destination area, position of source and destination blocks relative to respectively src_area and dst_area.
src_area – a source geo definition.
dst_area – a destination geo definition. If the same as the source definition, a ValueError is raised.
src_arrays – data to use. When split into smaller bit to pass to func, they are split across the x and y dimensions, but not across the other dimensions, so all the dimensions of the smaller arrays will be using only one chunk!
dst_arrays – arrays to use that are already in dst_area space. If the array has more than 2 dimensions, the last two are expected to be y, x.
chunk_size – the chunks size(s) to use in the dst_area space. This has to be provided since it is not guaranteed that we can get this information from the other arguments. Moreover, this needs to be an iterable of k elements if the resulting array of func is to have a different number of dimensions (k) than the input array.
dtype – the dtype the resulting array is going to have. Has to be provided.
name – Name prefix of the dask tasks to be generated
fill_value – Desired value for any invalid values in the output array
kwargs – any other keyword arguments that will be passed on to func.
- Returns:
A dask array, chunked as dst_area, containing the resampled data.
- Principle of operations:
Resample_blocks works by iterating over chunks on the dst_area domain. For each chunk, the corresponding slice of the src_area domain is computed and the input src_arrays are cut accordingly to pass to func. To know more about how the slicing is performed, refer to the :class:Slicer class and subclasses.
Examples
To generate indices from the gradient resampler, you can apply the corresponding function with no input. Note how we provide the chunk sizes knowing that the result array with have 2 elements along a third dimension.
>>> indices_xy = resample_blocks(gradient_resampler_indices, source_geo_def, [], target_geo_def, ... chunk_size=(2, "auto", "auto"), dtype=float)
From these indices, to resample an array using bilinear interpolation:
>>> resampled = resample_blocks(block_bilinear_interpolator, source_geo_def, [src_array], target_geo_def, ... dst_arrays=[indices_xy], ... chunk_size=("auto", "auto"), dtype=src_array.dtype)