impactlab_tools.gcp package

Submodules

impactlab_tools.gcp.reindex module

impactlab_tools.gcp.reindex.hierid_to_shapenum(data, dim='hierid', new_dim='SHAPENUM', inplace=False)[source]

Re-indexes a DataArray or Dataset from hierid to SHAPENUM using agglomerated-world-new region definitions

Parameters:
  • data (Dataset or DataArray) – xarray.Dataset or xarray.DataArray indexed by a hierid impact region name (str) index

  • dim (str, optional) – Dimension along which to reindex (default ‘hierid’)

  • new_dim (str, optional) – New name for reindexed dimension (default ‘SHAPENUM’)

  • inplace (bool, optional) – Modify the Dataset or DataArray in place rather than returning a copy (default False)

Returns:

reshaped – Copy of dataset reindexed by SHAPENUM along dimension dim

Return type:

Dataset or DataArray

Example

>>> import numpy as np, xarray as xr
>>> np.random.seed(1)
>>> ds = xr.Dataset({'var2': xr.DataArray(
...     np.random.random((10,)),
...     dims=('hierid',),
...     coords={'hierid':
...         ['ABW', 'AFG.1.12', 'AFG.1.R8abddb145b8788ee',
...          'AFG.1.R91a8634efe8e02a7', 'AFG.1.Ra6a2bba0a271cb4a',
...          'AFG.1.Rcf29900a4c191e96', 'AFG.1.Rd0d6327d56611fba',
...          'AFG.10.103', 'AFG.10.106', 'AFG.10.109']})})
...
...
>>> reshaped = hierid_to_shapenum(ds)
>>> reshaped
<xarray.Dataset>
Dimensions:   (SHAPENUM: 10)
Coordinates:
  * SHAPENUM  (SHAPENUM) float64 1.369e+03 5.747e+03 ...
Data variables:
    var2      (SHAPENUM) float64 0.417 0.7203 0.0001144 ...

>>> (reshaped.var2.values == ds.var2.values).all()
True
impactlab_tools.gcp.reindex.shapenum_to_hierid(data, dim='SHAPENUM', new_dim='hierid', inplace=False)[source]

Re-indexes a DataArray or Dataset from SHAPENUM to hierid using agglomerated-world-new region definitions

Parameters:
  • data (Dataset or DataArray) – xarray.Dataset or xarray.DataArray indexed by a SHAPENUM positional index

  • dim (str, optional) – Dimension along which to reindex (default ‘SHAPENUM’)

  • new_dim (str, optional) – New name for reindexed dimension (default ‘hierid’)

  • inplace (bool, optional) – Modify the Dataset or DataArray in place rather than returning a copy (default False)

Returns:

reshaped – Copy of dataset reindexed by hierid along dimension dim

Return type:

Dataset or DataArray

Example

>>> import numpy as np, xarray as xr
>>> np.random.seed(1)
>>> ds = xr.Dataset({'var1': xr.DataArray(
...     np.random.random((24378,)),
...     dims=('SHAPENUM',),
...     coords={'SHAPENUM': np.arange(1,24379)})})
...
>>> reshaped = shapenum_to_hierid(ds)
>>> reshaped 
<xarray.Dataset>
Dimensions:  (hierid: 24378)
Coordinates:
  * hierid   (hierid) ...
Data variables:
    var1     (hierid) float64 0.417 0.7203 0.0001144 ...

>>> (reshaped.var1.values == ds.var1.values).all()
True

impactlab_tools.gcp.dist module

impactlab_tools.gcp.dist.gcp_quantiles(data, rcp=None, quantiles=[0.05, 0.17, 0.5, 0.83, 0.95], values_sorted=False, dim='model', sample_weight=None)[source]

Compute quantiles of an xarray distribution using GCP weights

Note

This function does not control for the number of samples of each model. If they are not constant across models provide a correctly weighted weights array to utils.weighting.weighted_quantile_xr(). We would like to fix this. If you have a good fix we’d love a PR :)

Parameters:
  • data (DataArray or Dataset) – xarray.DataArray or xarray.Dataset with data indexed by GCP model along the dimension dim. If a Dataset is passed, gcp_quantiles computes the weighted quantile for each variable in the Dataset that is indexed by dim.

  • rcp (str, optional) – RCP weights/models to use (‘rcp45’, ‘rcp85’). Required if no sample_weight provided.

  • quantiles (array-like) – quantiles of distribution to return. quantiles should be in [0, 1].

  • values_sorted (bool) – if True, then will avoid sorting of initial array

  • dim (str, optional) –

    dimension along which to retrieve quantiles. The indices of this

    dimension should be valid (case insensitive) GCP climate models. Default: ‘model’.

    sample_weightDataArray, optional

    weights to use when producing the weighted quantiles. Required if no RCP provided. If not provided, uses the defualt weights for the RCP provided, based on Rasmussen et al. (2015).

Returns:

returns a new DataArray or Dataset with quantiles computed from weighted distribution along a new dimension quantile and dimension dim dropped.

Return type:

DataArray or Dataset

See also

Module contents