lair.inventories.Inventory.convert_units#
- Inventory.convert_units(dst_units, inplace=False)[source]#
Convert the units of the data to the desired output units.
A flux per time step, per year on annual data or per month on monthly data (
time_step), is a flux over the calendar length of that step: 366 days in a leap year, 365 in any other, and the days of each month. pint alone, as in the plain-arrayconvert_units(), would use its year of 365.25 days and a twelfth of it for a month. So the totals,absolute_emissionsandintegrate(), are the same in any unit. Any other unit (per second, per day on annual data) converts as pint converts it. An amount (mol) and a mass (kg) convert through the molecular weight of the inventory’s pollutant.- Parameters:
dst_units (Any) – The destination units.
inplace (bool, optional) – Whether to modify the data in place, by default False.
- Returns:
Inventory – The inventory with converted units. If inplace=True returns self, otherwise returns a new Inventory copy.
- Return type:
See also
convert_unitsThe same for a plain array or dataset, with pint’s year.
Examples
A flux of 1 kg m-2 s-1 in 2019 and 2020, per year:
>>> import numpy as np >>> import pandas as pd >>> import xarray as xr >>> from lair.inventories import Inventory >>> ds = xr.Dataset( ... {"energy": (("time", "lat", "lon"), np.ones((2, 1, 1)))}, ... coords={ ... "time": pd.date_range("2019-01-01", periods=2, freq="YS"), ... "lat": [40.05], ... "lon": [-111.95], ... }, ... ) >>> inv = Inventory(ds, pollutant="CH4", src_units="kg m-2 s-1") >>> per_year = inv.convert_units("kg m-2 yr-1") >>> per_year.data["energy"].values.ravel() / 86400 # days in each year array([365., 366.])