s2spy.rgdr.utils

Commonly used utility functions for s2spy.

Module Contents

Functions

weighted_groupby(→ XrType)

Apply a weighted reduction after a groupby call.

geographical_cluster_center(→ xarray.DataArray)

Add the geographical centers to the clusters.

intervals_subtract(→ list[int])

Subtracts n from the interval indices, skipping 0.

Attributes

XrType

s2spy.rgdr.utils.XrType[source]
s2spy.rgdr.utils.weighted_groupby(ds: XrType, groupby: str, weight: str, method: str = 'mean') XrType[source]

Apply a weighted reduction after a groupby call.

xarray does not currently support combining weighted and groupby. An open PR adds supports for this functionality (https://github.com/pydata/xarray/pull/5480), but this branch was never merged.

Parameters:
  • ds – Data containing the coordinates or variables specified in the groupby and weight kwargs.

  • groupby – Coordinate which should be used to make the groups.

  • weight – Variable in the Dataset containing the weights that should be used.

  • method – Method that should be used to reduce the dataset, by default ‘mean’. Supports any of xarray’s builtin methods, e.g. ‘median’, ‘min’, ‘max’.

Returns:

Dataset reduced using the groupby coordinate, using weights =

based on ds[weight].

Return type:

Same as input

s2spy.rgdr.utils.geographical_cluster_center(masked_data: xarray.DataArray, reduced_data: xarray.DataArray) xarray.DataArray[source]

Add the geographical centers to the clusters.

Parameters:
  • masked_data (xr.DataArray) – Precursor data before being reduced to clusters, with the dimensions latitude and longitude, and cluster labels added.

  • reduced_data (xr.DataArray) – Data reduced to the clusters, to which the geographical centers will be added

Returns:

Reduced data with the latitude and longitude of the geographical

centers added as coordinates of the cluster labels.

Return type:

xr.DataArray

s2spy.rgdr.utils.intervals_subtract(intervals: list[int], n: int) list[int][source]

Subtracts n from the interval indices, skipping 0.