Skip to content

Commit ec3b89f

Browse files
committed
Add .transport.operator.indexer_scenario()
- Allow use of scenario="LED" in some cases and scenario="SSP1" in others. - Select LDV load factor data using "LED" label where necessary.
1 parent 683bead commit ec3b89f

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

message_ix_models/model/transport/build.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from message_ix_models.util.graphviz import HAS_GRAPHVIZ
2121

2222
from . import Config
23+
from .operator import indexer_scenario
2324
from .structure import get_technology_groups
2425

2526
if TYPE_CHECKING:
@@ -291,6 +292,8 @@ def add_exogenous_data(c: Computer, info: ScenarioInfo) -> None:
291292
("groups::iea to transport", itemgetter(0), "groups::iea eweb"),
292293
("groups::transport to iea", itemgetter(1), "groups::iea eweb"),
293294
("indexers::iea to transport", itemgetter(2), "groups::iea eweb"),
295+
("indexers:scenario", partial(indexer_scenario, with_LED=False), "config"),
296+
("indexers:scenario:LED", partial(indexer_scenario, with_LED=True), "config"),
294297
("n::ex world", "nodes_ex_world", "n"),
295298
(
296299
"n:n:ex world",
@@ -395,14 +398,12 @@ def add_structure(c: Computer) -> None:
395398
# - `Static` tasks
396399
# - Single 'dynamic' tasks based on config, info, spec, and/or t_groups
397400
# - Multiple static and dynamic tasks generated in loops etc.
398-
tasks = list(STRUCTURE_STATIC) + [
401+
tasks: list[tuple] = list(STRUCTURE_STATIC) + [
399402
("c::transport", quote(spec.add.set["commodity"])),
400403
("c::transport+base", quote(spec.add.set["commodity"] + info.set["commodity"])),
401404
("cg", quote(spec.add.set["consumer_group"])),
402405
("indexers:cg", spec.add.set["consumer_group indexers"]),
403406
("nodes", quote(info.set["node"])),
404-
# TODO Use "LED" where appropriate
405-
("indexers:scenario", quote(dict(scenario=repr(config.ssp).split(":")[1]))),
406407
("t::transport", quote(spec.add.set["technology"])),
407408
("t::transport agg", quote(dict(t=t_groups))),
408409
("t::transport all", quote(dict(t=spec.add.set["technology"]))),

message_ix_models/model/transport/ldv.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def prepare_computer(c: Computer):
162162

163163
# Select load factor
164164
k.lf_ny = k.lf_nsy / "scenario"
165-
c.add(k.lf_ny[0], "select", k.lf_nsy[0], "indexers:scenario")
165+
c.add(k.lf_ny[0], "select", k.lf_nsy[0], "indexers:scenario:LED")
166166

167167
# Insert a scaling factor that varies according to SSP
168168
c.apply(factor.insert, k.lf_ny[0], name="ldv load factor", target=k.lf_ny)

message_ix_models/model/transport/operator.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from functools import partial, reduce
77
from itertools import product
88
from operator import gt, le, lt
9-
from typing import TYPE_CHECKING, Any, Hashable, Optional, Union, cast
9+
from typing import TYPE_CHECKING, Any, Hashable, Literal, Optional, Union, cast
1010

1111
import genno
1212
import numpy as np
@@ -64,6 +64,7 @@
6464
"groups_iea_eweb",
6565
"groups_y_annual",
6666
"iea_eei_fv",
67+
"indexer_scenario",
6768
"indexers_n_cd",
6869
"indexers_usage",
6970
"logit",
@@ -788,6 +789,30 @@ def iea_eei_fv(name: str, config: dict) -> "AnyQuantity":
788789
return result.sel(y=ym1, t="Total freight transport", drop=True)
789790

790791

792+
def indexer_scenario(config: dict, *, with_LED: bool) -> dict[Literal["scenario"], str]:
793+
"""Indexer for the ``scenario`` dimension.
794+
795+
If `with_LED` **and** :py:`config.project["LDV"] = True`, then the single label is
796+
"LED". Otherwise it is the short form of the :attr:`.transport.config.Config.ssp`
797+
code, e.g. "SSP1". In other words, this treats "LDV" as mutually exclusive with an
798+
SSP scenario identifier (instead of orthogonal).
799+
800+
Parameters
801+
----------
802+
config :
803+
The genno.Computer "config" dictionary, with a key "transport" mapped to an
804+
instance of :class:`.transport.Config`.
805+
"""
806+
# Retrieve the .transport.Config object from the genno.Computer "config" dict
807+
c: "Config" = config["transport"]
808+
809+
return dict(
810+
scenario="LED"
811+
if (with_LED and c.project.get("LED", False))
812+
else repr(c.ssp).split(":")[1]
813+
)
814+
815+
791816
def indexers_n_cd(config: dict) -> dict[str, xr.DataArray]:
792817
"""Indexers for selecting (`n`, `census_division`) → `n`.
793818

0 commit comments

Comments
 (0)