|
23 | 23 | import numpy as np
|
24 | 24 | import pandas as pd
|
25 | 25 | import xarray as xr
|
26 |
| -from genno import Operator |
27 |
| -from genno.operator import apply_units, relabel, rename_dims |
| 26 | +from genno import Computer, KeySeq, Operator, quote |
| 27 | +from genno.operator import apply_units, rename_dims |
28 | 28 | from genno.testing import assert_qty_allclose, assert_units
|
29 |
| -from iam_units import registry |
30 | 29 | from message_ix_models import ScenarioInfo
|
31 | 30 | from message_ix_models.model.structure import get_codelist, get_codes
|
32 | 31 | from message_ix_models.report.operator import compound_growth
|
|
45 | 44 | from .config import Config
|
46 | 45 |
|
47 | 46 | if TYPE_CHECKING:
|
48 |
| - from genno import Computer |
49 | 47 | from genno.types import AnyQuantity
|
50 | 48 | from message_ix import Scenario
|
| 49 | + from message_ix_models import Context |
51 | 50 |
|
52 | 51 | import message_data.model.transport.factor
|
53 | 52 |
|
@@ -267,29 +266,32 @@ def distance_ldv(config: dict) -> "AnyQuantity":
|
267 | 266 | }
|
268 | 267 |
|
269 | 268 |
|
270 |
| -def distance_nonldv(config: dict) -> "AnyQuantity": |
| 269 | +def distance_nonldv(context: "Context") -> "AnyQuantity": |
271 | 270 | """Return annual travel distance per vehicle for non-LDV transport modes."""
|
272 |
| - # Load from IEA EEI |
273 |
| - from message_data.tools import iea_eei # type: ignore [attr-defined] |
| 271 | + import message_ix_models.tools.iea.eei # noqa: F401 |
| 272 | + from message_ix_models.tools import exo_data |
274 | 273 |
|
275 |
| - dfs = iea_eei.get_eei_data(config["regions"]) |
276 |
| - df = ( |
277 |
| - dfs["vehicle use"] |
278 |
| - .rename(columns={"region": "nl", "year": "y", "Mode/vehicle type": "t"}) |
279 |
| - .set_index(["nl", "t", "y"]) |
| 274 | + log.warning( |
| 275 | + "distance_nonldv() currently returns a sum, rather than weighted average. Use" |
| 276 | + "with caution." |
280 | 277 | )
|
281 | 278 |
|
282 |
| - # Check units |
283 |
| - assert "kilovkm / vehicle" == registry.parse_units( |
284 |
| - df["units"].unique()[0].replace("10^3 ", "k") |
285 |
| - ) |
286 |
| - units = "Mm / vehicle / year" |
| 279 | + c = Computer() |
| 280 | + source_kw = dict(measure="Vehicle use", aggregate=True) |
| 281 | + keys = exo_data.prepare_computer(context, c, "IEA EEI", source_kw) |
287 | 282 |
|
288 |
| - # Rename IEA EEI technology IDs to model-internal ones |
289 |
| - result = relabel( |
290 |
| - genno.Quantity(df["value"], name="non-ldv distance", units=units), |
291 |
| - dict(t=EEI_TECH_MAP), |
292 |
| - ) |
| 283 | + ks = KeySeq(keys[0]) |
| 284 | + |
| 285 | + c.add(ks[0], "select", ks.base, indexers={"SECTOR": "transport"}, drop=True) |
| 286 | + c.add(ks[1], "rename", ks[0], quote({"Mode/vehicle type": "t"})) |
| 287 | + # Replace IEA EEI technology codes with MESSAGEix-Transport ones |
| 288 | + c.add(ks[2], "relabel", ks[1], labels=dict(t=EEI_TECH_MAP)) |
| 289 | + # Ensure compatible dimensionality and convert units |
| 290 | + c.add(ks[3], "convert_units", ks[2], units="Mm / vehicle / year") |
| 291 | + c.add(ks[4], "rename_dims", ks[3], quote({"n": "nl"})) |
| 292 | + |
| 293 | + # Execute the calculation |
| 294 | + result = c.get(ks[4]) |
293 | 295 |
|
294 | 296 | # Select the latest year.
|
295 | 297 | # TODO check whether coverage varies by year; if so, then fill-forward or
|
|
0 commit comments