Gas baskets#
Gas baskets like KyotoGHG
are essentially the sum of individual emissions. Usually,
gas baskets are specified in units of warming equivalent CO2, so they necessarily
always have to specify a global warming potential metric as well.
We offer a few specialized functions for handling gas baskets.
Summation#
To sum the contents of gas baskets , the function
xarray.Dataset.pr.gas_basket_contents_sum()
is available.
Let’s first create an example dataset.
import primap2
import xarray as xr
import numpy as np
# select example dataset
ds = primap2.open_dataset("../minimal_ds.nc").loc[{"time": slice("2000", "2003")}][
["CH4", "CO2", "SF6"]
]
ds
<xarray.Dataset> Size: 496B Dimensions: (time: 4, area (ISO3): 4, source: 1) Coordinates: * area (ISO3) (area (ISO3)) <U3 48B 'COL' 'ARG' 'MEX' 'BOL' * source (source) <U8 32B 'RAND2020' * time (time) datetime64[ns] 32B 2000-01-01 2001-01-01 ... 2003-01-01 Data variables: CH4 (time, area (ISO3), source) float64 128B [CH4·Gg/a] 0.7543 .... CO2 (time, area (ISO3), source) float64 128B [CO2·Gg/a] 0.6635 .... SF6 (time, area (ISO3), source) float64 128B [SF6·Gg/a] 0.0018 .... Attributes: area: area (ISO3)
# add (empty) gas basket with corresponding metadata
ds["KyotoGHG (AR4GWP100)"] = xr.full_like(ds["CO2"], np.nan).pr.quantify(units="Gg CO2 / year")
ds["KyotoGHG (AR4GWP100)"].attrs = {"entity": "KyotoGHG", "gwp_context": "AR4GWP100"}
ds
<xarray.Dataset> Size: 624B Dimensions: (time: 4, area (ISO3): 4, source: 1) Coordinates: * area (ISO3) (area (ISO3)) <U3 48B 'COL' 'ARG' 'MEX' 'BOL' * source (source) <U8 32B 'RAND2020' * time (time) datetime64[ns] 32B 2000-01-01 ... 2003-01-01 Data variables: CH4 (time, area (ISO3), source) float64 128B [CH4·Gg/a] ... CO2 (time, area (ISO3), source) float64 128B [CO2·Gg/a] ... SF6 (time, area (ISO3), source) float64 128B [SF6·Gg/a] ... KyotoGHG (AR4GWP100) (time, area (ISO3), source) float64 128B [CO2·Gg/a] ... Attributes: area: area (ISO3)
Now, we can compute KyotoGHG
from its contents (assuming for the moment that this
only includes CO\(_2\), SF\(_6\) and CH\(_4\))
# compute gas basket from its contents, which have to be given explicitly
ds.pr.gas_basket_contents_sum(
basket="KyotoGHG (AR4GWP100)",
basket_contents=["CO2", "SF6", "CH4"],
)
<xarray.DataArray 'KyotoGHG (AR4GWP100)' (time: 4, area (ISO3): 4, source: 1)> Size: 128B <Quantity([[[ 60.55826022] [11537.15564864] [15441.09112246] [ 2832.13708223]] [[10913.89923578] [12017.91242534] [18293.41941003] [18461.14522004]] [[12164.44190136] [14106.2809531 ] [ 5472.05066779] [ 7054.12526007]] [[12163.56107579] [11353.23339466] [ 9916.31088755] [15594.44908077]]], 'gigagram * CO2 / year')> Coordinates: * area (ISO3) (area (ISO3)) <U3 48B 'COL' 'ARG' 'MEX' 'BOL' * source (source) <U8 32B 'RAND2020' * time (time) datetime64[ns] 32B 2000-01-01 2001-01-01 ... 2003-01-01 Attributes: gwp_context: AR4GWP100 entity: KyotoGHG
Note that like all PRIMAP2 functions,
xarray.Dataset.pr.gas_basket_contents_sum()
returns the result without overwriting anything in the original dataset,
so you have to explicitly overwrite existing data if you want that:
ds["KyotoGHG (AR4GWP100)"] = ds.pr.gas_basket_contents_sum(
basket="KyotoGHG (AR4GWP100)",
basket_contents=["CO2", "SF6", "CH4"],
)
Filling in missing information#
To fill in missing data in a gas basket, use
xarray.Dataset.pr.fill_na_gas_basket_from_contents()
# delete all data about the years 2002-2003 (inclusive) from the
# KyotoGHG data
ds["KyotoGHG (AR4GWP100)"].loc[{"time": slice("2002", "2003")}].pint.magnitude[:] = np.nan
ds["KyotoGHG (AR4GWP100)"]
<xarray.DataArray 'KyotoGHG (AR4GWP100)' (time: 4, area (ISO3): 4, source: 1)> Size: 128B <Quantity([[[ 60.55826022] [11537.15564864] [15441.09112246] [ 2832.13708223]] [[10913.89923578] [12017.91242534] [18293.41941003] [18461.14522004]] [[ nan] [ nan] [ nan] [ nan]] [[ nan] [ nan] [ nan] [ nan]]], 'gigagram * CO2 / year')> Coordinates: * area (ISO3) (area (ISO3)) <U3 48B 'COL' 'ARG' 'MEX' 'BOL' * source (source) <U8 32B 'RAND2020' * time (time) datetime64[ns] 32B 2000-01-01 2001-01-01 ... 2003-01-01 Attributes: gwp_context: AR4GWP100 entity: KyotoGHG
ds.pr.fill_na_gas_basket_from_contents(
basket="KyotoGHG (AR4GWP100)", basket_contents=["CO2", "SF6", "CH4"]
)
<xarray.DataArray 'KyotoGHG (AR4GWP100)' (time: 4, area (ISO3): 4, source: 1)> Size: 128B <Quantity([[[ 60.55826022] [11537.15564864] [15441.09112246] [ 2832.13708223]] [[10913.89923578] [12017.91242534] [18293.41941003] [18461.14522004]] [[12164.44190136] [14106.2809531 ] [ 5472.05066779] [ 7054.12526007]] [[12163.56107579] [11353.23339466] [ 9916.31088755] [15594.44908077]]], 'gigagram * CO2 / year')> Coordinates: * area (ISO3) (area (ISO3)) <U3 48B 'COL' 'ARG' 'MEX' 'BOL' * source (source) <U8 32B 'RAND2020' * time (time) datetime64[ns] 32B 2000-01-01 2001-01-01 ... 2003-01-01 Attributes: gwp_context: AR4GWP100 entity: KyotoGHG
The reverse case is that you are missing some data in the timeseries of
individual gases and want to fill those in using downscaled data from
a gas basket.
Here, use
xarray.Dataset.pr.downscale_gas_timeseries()
# delete all data about the years 2005-2009 from the individual gas data
sel = {"time": slice("2002", "2003")}
ds["CO2"].loc[sel].pint.magnitude[:] = np.nan
ds["SF6"].loc[sel].pint.magnitude[:] = np.nan
ds["CH4"].loc[sel].pint.magnitude[:] = np.nan
ds
<xarray.Dataset> Size: 624B Dimensions: (time: 4, area (ISO3): 4, source: 1) Coordinates: * area (ISO3) (area (ISO3)) <U3 48B 'COL' 'ARG' 'MEX' 'BOL' * source (source) <U8 32B 'RAND2020' * time (time) datetime64[ns] 32B 2000-01-01 ... 2003-01-01 Data variables: CH4 (time, area (ISO3), source) float64 128B [CH4·Gg/a] ... CO2 (time, area (ISO3), source) float64 128B [CO2·Gg/a] ... SF6 (time, area (ISO3), source) float64 128B [SF6·Gg/a] ... KyotoGHG (AR4GWP100) (time, area (ISO3), source) float64 128B [CO2·Gg/a] ... Attributes: area: area (ISO3)
# This determines gas shares at the points in time where individual gas
# data is available, interpolates the shares where data is missing, and
# then downscales the gas basket data using the interpolated shares
ds.pr.downscale_gas_timeseries(basket="KyotoGHG (AR4GWP100)", basket_contents=["CO2", "SF6", "CH4"])
<xarray.Dataset> Size: 624B Dimensions: (time: 4, area (ISO3): 4, source: 1) Coordinates: * area (ISO3) (area (ISO3)) <U3 48B 'COL' 'ARG' 'MEX' 'BOL' * source (source) <U8 32B 'RAND2020' * time (time) datetime64[ns] 32B 2000-01-01 ... 2003-01-01 Data variables: CH4 (time, area (ISO3), source) float64 128B [CH4·Gg/a] ... CO2 (time, area (ISO3), source) float64 128B [CO2·Gg/a] ... SF6 (time, area (ISO3), source) float64 128B [SF6·Gg/a] ... KyotoGHG (AR4GWP100) (time, area (ISO3), source) float64 128B [CO2·Gg/a] ... Attributes: area: area (ISO3)