Skip to content

Commit bdbe796

Browse files
committed
Document IAMCConversion.add_tasks
1 parent 9ee9543 commit bdbe796

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

message_ix_models/report/util.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,63 @@ class IAMCConversion:
9898
#: Dimension(s) to sum over.
9999
sums: list[str] = field(default_factory=list)
100100

101-
#: If :any:`True`, ensure data is present for "R11_GLB".
101+
#: If :any:`True`, ensure data is present for ``R##_GLB``.
102102
GLB_zeros: bool = False
103103

104104
def __post_init__(self) -> None:
105105
# Ensure base is a Key
106106
self.base = Key(self.base)
107107

108108
def add_tasks(self, c: "Computer") -> None:
109+
"""Add tasks to convert :attr:`base` to IAMC structure.
110+
111+
The tasks include, in order:
112+
113+
1. If :attr:`GLB_zeroes` is :any:`True`:
114+
115+
- Create a quantity with the same shape as :attr:`base`, filled with all
116+
zeros (:func:`.zeros_like`) and a single coord like ``R##_GLB`` for the
117+
:math:`n` dimension (:func:`.node_glb`).
118+
- Add this to :attr:`base`.
119+
120+
These steps ensure that values for ``R##_GLB`` will appear in the
121+
IAMC-structured result.
122+
123+
2. Convert to the given :attr:`units` (:func:`~genno.operator.convert_units`).
124+
The :attr:`base` quantity **must** have dimensionally compatible units.
125+
126+
Steps (3) to (6) are repeated for (at least) an empty string (:py:`""`) and for
127+
any expressions like :py:`"x-y-z"` in :attr:`sums`.
128+
129+
3. Subtract the given dimension(s) (if any) from the dimensions of :attr:`base`.
130+
For example, if :attr:`base` is ``<foo:x-y-z>`` and :attr:`sums` includes
131+
:py:`"x-z"`, this gives a reference to ``<foo:y>``, which is the base
132+
quantity summed over the :math:`(x, z)` dimensions.
133+
134+
4. Reduce the :attr:`var_parts` in the same way. For example, if
135+
:attr:`var_parts` is :py:`["Variable prefix", "z", "x", "y", "Foo"]`, the
136+
above sum reduces this to :py:`["Variable prefix", "y", "Foo"]`.
137+
138+
5. Call :func:`genno.compat.pyam.iamc` to add further tasks to convert the
139+
quantity from (3) to IAMC structure. :func:`callback` in this module is used
140+
to help format the individual dimension labels and collapsed ‘variable’
141+
labels.
142+
143+
This step results in keys like ``base 0::iamc``, ``base 1::iamc``, etc. added
144+
to `rep`.
145+
146+
6. Append the key from (5) to the task at :data:`.report.key.all_iamc`. This
147+
ensures that the converted data is concatenated with all other
148+
IAMC-structured data.
149+
"""
109150
from genno.compat.pyam import iamc as handle_iamc
110151

111152
from .key import all_iamc, coords
112153

113154
k = Keys(base=self.base, glb=self.base + "glb")
114155

115156
if self.GLB_zeros:
116-
# Quantity of zeros like self.base
157+
# Quantity of zeros in the same shape as self.base, without an 'n' dimension
117158
c.add(k.glb[0], "zeros_like", self.base, drop=["n"])
118159

119160
# Add the 'n' dimension
@@ -147,7 +188,7 @@ def add_tasks(self, c: "Computer") -> None:
147188
# Exclude any summed dimensions from the expression.
148189
var_parts = [v for v in self.var_parts if v not in dims]
149190

150-
# Invoke genno's built-in handler
191+
# Invoke genno's built-in handler to add more tasks:
151192
# - Base key: the partial sum of k.base over any `dims`.
152193
# - "variable" argument is used only to construct keys; the resulting IAMC-
153194
# structured data is available at `{variable}::iamc`.

0 commit comments

Comments
 (0)