Skip to content

Commit 9ee9543

Browse files
committed
Move unit conversion into IAMCConversion.add_tasks
1 parent 5017276 commit 9ee9543

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

message_ix_models/report/extraction.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#: Describe the conversion to IAMC structure.
1717
CONV = IAMCConversion(
18-
base=K.all[2],
18+
base=K.all[1],
1919
var_parts=["Resource|Extraction", "c"],
2020
sums=["c"],
2121
unit="EJ/yr",
@@ -25,8 +25,8 @@
2525

2626
def callback(r: "Reporter", context: "Context") -> None:
2727
"""Prepare reporting of resource extraction."""
28-
# TODO Generalize the following 3 blocks. Similar operations will be used in other
29-
# cases.
28+
# TODO Generalize the following 3 calls to Reporter.add(), e.g. by a utility
29+
# function. Similar operations will be used in other cases.
3030

3131
# Apply missing units to model contents
3232
r.add(K.hist[0], "apply_units", K.hist, units="GWa/year")
@@ -35,11 +35,8 @@ def callback(r: "Reporter", context: "Context") -> None:
3535
# Add historical and current values together
3636
r.add(K.all[0], "add", K.hist[0], K.model[0])
3737

38-
# Convert to target units
39-
r.add(K.all[1], "convert_units", K.all[0], units=CONV.unit)
40-
4138
# Aggregate on 'c' dimension using groups from commodity_groups()
42-
r.add(K.all[2], "aggregate", K.all[1], groups.c, keep=False)
39+
r.add(K.all[1], "aggregate", K.all[0], groups.c, keep=False)
4340

4441
# Add tasks to (a) transform to IAMC-structured data, (b) concatenate to all::iamc
4542
CONV.add_tasks(r)

message_ix_models/report/util.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,13 @@ def add_tasks(self, c: "Computer") -> None:
120120
c.add(k.glb[1], "expand_dims", k.glb[0], coords.n_glb)
121121

122122
# Add zeros to base data & update the base key for next steps
123-
k.base += "glb"
124-
c.add(k.base, "add", self.base, k.glb[1])
123+
c.add(k.base[0], "add", self.base, k.glb[1])
124+
else:
125+
# Simple alias
126+
c.add(k.base[0], k.base)
127+
128+
# Convert to target units
129+
c.add(k.base[1], "convert_units", k.base[0], units=self.unit, sums=True)
125130

126131
# Common keyword arguments for genno.compat.pyam.iamc
127132
args: dict = dict(rename=_RENAME, unit=self.unit)
@@ -151,7 +156,7 @@ def add_tasks(self, c: "Computer") -> None:
151156
c,
152157
args
153158
| dict(
154-
base=k.base.drop(*dims),
159+
base=k.base[1].drop(*dims),
155160
variable=f"{label} {i}",
156161
collapse=dict(callback=collapse, var=var_parts),
157162
),

0 commit comments

Comments
 (0)