# pyresample.bucket package¶

## Module contents¶

Code for resampling using bucket resampling.

class pyresample.bucket.BucketResampler(target_area, source_lons, source_lats)

Bases: object

Class for bucket resampling.

Bucket resampling is useful for calculating averages and hit-counts when aggregating data to coarser scale grids.

Below are examples how to use the resampler.

Read data using Satpy. The resampling can also be done (apart from fractions) directly from Satpy, but this demonstrates the direct low-level usage.

>>> from pyresample.bucket import BucketResampler
>>> from satpy import Scene
>>> from satpy.resample import get_area_def
>>> fname = "hrpt_noaa19_20170519_1214_42635.l1b"
>>> glbl = Scene(filenames=[fname])
>>> data = glbl['4']
>>> lons, lats = data.area.get_lonlats()
>>> target_area = get_area_def('euro4')


Initialize the resampler

>>> resampler = BucketResampler(target_area, lons, lats)


Calculate the sum of all the data in each grid location:

>>> sums = resampler.get_sum(data)


Calculate how many values were collected at each grid location:

>>> counts = resampler.get_count()


The average can be calculated from the above two results, or directly using the helper method:

>>> average = resampler.get_average(data)


Calculate fractions of occurrences of different values in each grid location. The data needs to be categorical (in integers), so we’ll create some categorical data from the brightness temperature data that were read earlier. The data are returned in a dictionary with the categories as keys.

>>> data = da.where(data > 250, 1, 0)
>>> fractions = resampler.get_fractions(data, categories=[0, 1])
>>> import matplotlib.pyplot as plt
>>> plt.imshow(fractions[0]); plt.show()

get_average(data, fill_value=nan, skipna=True)

Calculate bin-averages using bucket resampling.

Parameters: data (Numpy or Dask array) – Data to be binned and averaged. fill_value (float) – Fill value to mark missing/invalid values in the input data, as well as in the binned and averaged output data. Default: np.nan skipna (bool) – If True, skips missing values (as marked by NaN or fill_value) for the average calculation (similarly to Numpy’s nanmean). Buckets containing only missing values are set to fill_value. If False, sets the bucket to fill_value if one or more missing values are present in the bucket (similarly to Numpy’s mean). In both cases, empty buckets are set to NaN. Default: True average – Binned and averaged data. Dask array
get_count()

Count the number of occurrences for each bin using drop-in-a-bucket resampling.

Returns: data – Bin-wise count of hits for each target grid location Dask array
get_fractions(data, categories=None, fill_value=nan)

Get fraction of occurrences for each given categorical value.

Parameters: data (Numpy or Dask array) – Categorical data to be processed categories (iterable or None) – One dimensional list of categories in the data, or None. If None, categories are determined from the data by fully processing the data and finding the unique category values. fill_value (float) – Fill value to replace missing values. Default: np.nan
get_sum(data, skipna=True)

Calculate sums for each bin with drop-in-a-bucket resampling.

Parameters: data (Numpy or Dask array) – Data to be binned and averaged. skipna (boolean (optional)) – If True, skips NaN values for the sum calculation (similarly to Numpy’s nansum). Buckets containing only NaN are set to zero. If False, sets the bucket to NaN if one or more NaN values are present in the bucket (similarly to Numpy’s sum). In both cases, empty buckets are set to 0. Default: True data – Bin-wise sums in the target grid Numpy or Dask array
pyresample.bucket.round_to_resolution(arr, resolution)

Round the values in arr to closest resolution element.

Parameters: arr (list, tuple, Numpy or Dask array) – Array to be rounded resolution (float) – Resolution unit to which data are rounded data – Source data rounded to the closest resolution unit Numpy or Dask array