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.Datasetorxarray.DataArrayindexed by a hierid impact region name (str) indexdim (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> Size: 160B Dimensions: (SHAPENUM: 10) Coordinates: * SHAPENUM (SHAPENUM) float64 80B 1.369e+03 5.747e+03 ... 5.812e+03 5.832e+03 Data variables: var2 (SHAPENUM) float64 80B 0.417 0.7203 0.0001144 ... 0.3968 0.5388 >>> (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.Datasetorxarray.DataArrayindexed by a SHAPENUM positional indexdim (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> Size: 4MB Dimensions: (hierid: 24378) Coordinates: * hierid (hierid) <U35 3MB 'CAN.1.2.28' 'CAN.1.17.403' ... 'BWA.4.13' Data variables: var1 (hierid) float64 195kB 0.417 0.7203 0.0001144 ... 0.1359 0.9259 >>> (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.DataArrayorxarray.Datasetwith data indexed by GCP model along the dimensiondim. If a Dataset is passed,gcp_quantilescomputes the weighted quantile for each variable in theDatasetthat is indexed bydim.rcp (str, optional) – RCP weights/models to use (‘rcp45’, ‘rcp85’). Required if no
sample_weightprovided.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
DataArrayorDatasetwith quantiles computed from weighted distribution along a new dimensionquantileand dimensiondimdropped.- Return type:
DataArray or Dataset
See also
numpy.percentile()xarray.Dataset.quantile()xarray.DataArray.quantile()pandas.DataFrame.quantile()pandas.Series.quantile()