Skip to content

Commit

Permalink
Assign units in .report.compat
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Oct 19, 2023
1 parent 134d6d9 commit 7a7ac76
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions message_ix_models/report/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from itertools import chain, count
from typing import TYPE_CHECKING, List, Mapping, Optional, Sequence, cast

from genno import Computer, Key, quote
from genno import Computer, Key, Quantity, quote
from genno.core.key import single_key

if TYPE_CHECKING:
Expand Down Expand Up @@ -61,7 +61,9 @@ def get_techs(prefix: str, kinds: str) -> List[str]:
)


def make_shorthand_function(base_name, to_drop):
def make_shorthand_function(
base_name: str, to_drop: str, default_unit_key: Optional[str] = None
):
"""Create a shorthand function for adding tasks to a :class:`.Reporter`."""
_to_drop = to_drop.split()

Expand All @@ -71,24 +73,35 @@ def func(
*,
name: Optional[str] = None,
filters: Optional[dict] = None,
unit_key: Optional[str] = default_unit_key,
) -> Key:
base = single_key(c.full_key(base_name))
key = anon(name, dims=base)

indexers = dict(t=technologies)
indexers.update(filters or {})

c.add(key, "select", base, indexers=indexers, sums=True)
if unit_key:
c.add(key + "sel", "select", base, indexers=indexers)
c.add(
key,
"assign_units",
key + "sel",
units=c.graph["config"]["model"].units[unit_key],
sums=True,
)
else:
c.add(key, "select", base, indexers=indexers, sums=True)

# Return the partial sum over some dimensions
return key.drop(*_to_drop)

return func


inp = make_shorthand_function("in", "c h ho l no t")
inp = make_shorthand_function("in", "c h ho l no t", "energy")
emi = make_shorthand_function("rel", "nr r t yr")
out = make_shorthand_function("out", "c h hd l nd t")
out = make_shorthand_function("out", "c h hd l nd t", "energy")


def eff(
Expand Down Expand Up @@ -116,10 +129,7 @@ def pe_wCSSretro(
k_share: Optional[Key],
filters: Optional[dict] = None,
) -> Key:
"""Equivalent to the function of the same name.
:func:`message_data.tools.post_processing.default_tables._pe_wCCS_retro` at L129.
"""
"""Equivalent to :func:`default_tables._pe_wCCS_retro` at L129."""
ACT: Key = single_key(c.full_key("ACT"))

k0 = out(c, [t_scrub])
Expand All @@ -141,9 +151,7 @@ def pe_wCSSretro(


def callback(rep: "Reporter", context: "Context") -> None:
"""Partially duplicate the behaviour of :func:`_retr_CO2emi`."""
# TODO Handle `units_ene_mdl`
# TODO Handle emissions_units=units_emi
"""Partially duplicate the behaviour of :func:`.default_tables.retr_CO2emi`."""
from . import iamc

N = len(rep.graph)
Expand All @@ -154,8 +162,9 @@ def callback(rep: "Reporter", context: "Context") -> None:
rep.add(f"t::{k}", quote(v))

# Constants from report/default_units.yaml
rep.add("conv_c2co2:", 44.0 / 12.0)
rep.add("crbcnt_gas:", 0.482) # “Carbon content of natural gas”
rep.add("conv_c2co2:", 44.0 / 12.0) # dimensionless
# “Carbon content of natural gas”
rep.add("crbcnt_gas:", Quantity(0.482, units="Mt / GWa / a"))

# L3059 from message_data/tools/post_processing/default_tables.py
k0 = out(rep, ["gas_cc", "gas_ppl"])
Expand Down Expand Up @@ -208,7 +217,9 @@ def callback(rep: "Reporter", context: "Context") -> None:
rep.add(inp_all_gas_tecs, "add", inp_nonccs_gas_tecs, key)

# L3165
Hydrogen_tot = emi(rep, ["h2_mix"], filters=dict(r="CO2_cc"))
Hydrogen_tot = emi(
rep, ["h2_mix"], filters=dict(r="CO2_cc"), unit_key="CO2 emissions"
)

# L3063
filters = dict(c=["gas"], l=["secondary"])
Expand Down Expand Up @@ -249,6 +260,7 @@ def callback(rep: "Reporter", context: "Context") -> None:
get_techs("trp", "coal foil gas loil meth"),
name="FE_Transport",
filters=dict(r=["CO2_trp"]),
unit_key="CO2 emissions",
)

# L3886
Expand All @@ -258,9 +270,6 @@ def callback(rep: "Reporter", context: "Context") -> None:
k1 = Key.product("Transport", k0, Hydrogen_trp)
rep.add(k1, "add", k0, Hydrogen_trp, sums=True)

# FIXME Set units properly upstream so these units emerge from the calculation
k2 = rep.add(k1 + "units", "assign_units", k1, units="Mt/a")

# TODO Identify where to sum on "h", "m", "yv" dimensions

# Convert to IAMC format
Expand Down

0 comments on commit 7a7ac76

Please sign in to comment.