-
Notifications
You must be signed in to change notification settings - Fork 5
/
custom-conversions.py
68 lines (59 loc) · 1.78 KB
/
custom-conversions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.15.2
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %% [markdown]
# # Custom conversions
#
# Here we show how custom conversions can be passed to OpenSCM-Units' `ScmUnitRegistry`.
# %%
import pandas as pd
from openscm_units import ScmUnitRegistry
# %% [markdown]
# ## Custom conversions DataFrame
#
# On initialisation, a `pd.DataFrame` can be provided which contains the custom
# conversions. This `pd.DataFrame` should be formatted as shown below, with an
# index that contains the different species and columns which contain the
# conversion for different metrics.
# %%
metric_conversions_custom = pd.DataFrame(
[
{
"Species": "CH4",
"Custom1": 20,
"Custom2": 25,
},
{
"Species": "N2O",
"Custom1": 341,
"Custom2": 300,
},
]
).set_index("Species")
metric_conversions_custom
# %% [markdown]
# With such a `pd.DataFrame`, we can use custom conversions in our unit registry as shown.
# %%
# initialise the unit registry with custom conversions
unit_registry = ScmUnitRegistry(metric_conversions=metric_conversions_custom)
# add standard conversions before moving on
unit_registry.add_standards()
# start with e.g. N2O
nitrous_oxide = unit_registry("tN2O / yr")
# our unit registry allows us to make conversions using the
# conversion factors we previously defined
for context in ["Custom1", "Custom2"]:
with unit_registry.context(context):
print(
f"{nitrous_oxide} in CO2-equivalent in context {context} is {nitrous_oxide.to('tCO2 / yr')}"
)