pcmdi_metrics.io.region_subset

Contents

pcmdi_metrics.io.region_subset#

pcmdi_metrics.io.region_subset(ds, region, data_var=None, regions_specs=None, debug=False)[source]#

Subset a dataset or data array based on a specified region.

This function subsets an xarray Dataset or DataArray based on the latitude and longitude coordinates defined for a given region. It handles different longitude conventions and can optionally provide debug information.

Parameters:
  • ds (Union[xr.Dataset, xr.DataArray]) – The input dataset or data array to be subsetted.

  • region (str) – The name of the region to subset the data to. This should correspond to a key in the regions_specs dictionary.

  • data_var (str, optional) – The name of the data variable if ds is a DataArray, by default None, which names DataArray as “variable” in the Dataset if DataArray has no name.

  • regions_specs (dict, optional) – A dictionary containing the specifications for different regions. If None, it will be loaded from a default source, by default None.

  • debug (bool, optional) – If True, print debug information during the subsetting process, by default False.

Returns:

Union[xr.Dataset, xr.DataArray] – The subsetted dataset or data array, matching the input type.

Notes

This function first converts DataArrays to Datasets for processing, then converts back if necessary. It handles both latitude and longitude subsetting based on the region specifications, and can deal with different longitude conventions (0 to 360 vs -180 to 180).