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])
>>> glbl.load(['4'])
>>> data = glbl['4']
>>> lons, lats = data.area.get_lonlats()
>>> target_area = get_area_def('euro4')

Initialize the resampler

>>> resampler = BucketResampler(adef, 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, mask_all_nan=False)

Calculate bin-averages using bucket resampling.

Parameters
  • data (Numpy or Dask array) – Data to be binned and averaged

  • fill_value (float) – Fill value to replace missing values. Default: np.nan

Returns

average – Binned and averaged data.

Return type

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

Return type

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, mask_all_nan=False)

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

Parameters
  • data (Numpy or Dask array) –

  • mask_all_nan (boolean (optional)) – Mask bins that have only NaN results, default: False

Returns

data – Bin-wise sums in the target grid

Return type

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

Returns

data – Source data rounded to the closest resolution unit

Return type

Numpy or Dask array