Skip to content

Commit 2fc2588

Browse files
macflo8khaeru
authored andcommitted
Fix methanol final energy allocation bug
1 parent b838889 commit 2fc2588

File tree

3 files changed

+96
-38
lines changed

3 files changed

+96
-38
lines changed

message_ix_models/data/material/reporting/fe_reporting_aggregates.yaml

+21-12
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,39 @@ level_2:
9595
fe_pe_chem_other_h2 ]
9696

9797
level_1:
98+
99+
# some Chemicals|<Fuels>|<Sub-fuel> variables are not specified here
100+
# because they are generated later in the workflow, after the
101+
# Methanol/HVC/Ammonia subcomponents are generated.
102+
# The function run_reporting.add_chemicals_to_final_energy_variables
103+
# does this aggregation and updates all related aggregates.
104+
# Only the ones not generated there are mapped here
105+
106+
Chemicals|Liquids|Other:
107+
short: fe_pe_chem_oth
108+
components:
109+
[ fe_pe_hvc_oth ]
110+
Chemicals|Liquids|Biomass:
111+
short: fe_pe_chem_oth
112+
components:
113+
[ fe_pe_hvc_bio_eth ]
114+
98115
Chemicals|High-Value Chemicals|Electricity:
99116
short: fe_pe_hvc_el
100117
components:
101118
[fe_pe_hvc_el_ht, fe_pe_hvc_el_mto, fe_pe_hvc_el_sc]
102-
Chemicals|High-Value Chemicals|Liquids|Oil:
103-
short: fe_pe_hvc_oil
104-
components:
105-
[ fe_pe_hvc_oil_l, fe_pe_hvc_oil_h ]
106119
Chemicals|High-Value Chemicals|Liquids:
107120
short: fe_pe_hvc_liq
108121
components:
109122
[ fe_pe_hvc_oil_l, fe_pe_hvc_oil_h, fe_pe_hvc_bio_eth, fe_pe_hvc_oth]
123+
Chemicals|High-Value Chemicals|Liquids|Oil:
124+
short: fe_pe_hvc_oil
125+
components:
126+
[ fe_pe_hvc_oil_l, fe_pe_hvc_oil_h ]
110127
Chemicals|High-Value Chemicals|Solids:
111128
short: fe_pe_hvc_solids
112129
components:
113130
[ fe_pe_hvc_bio, fe_pe_hvc_coal, fe_pe_hvc_coke]
114-
Chemicals|Liquids|Other:
115-
short: fe_pe_chem_oth
116-
components:
117-
[ fe_pe_hvc_oth ]
118-
Chemicals|Liquids|Gases:
119-
short: fe_pe_hvc_gases
120-
components:
121-
[ fe_pe_hvc_gas ]
122131

123132
Chemicals|Other Sector|Electricity:
124133
short: fe_pe_chem_other_el

message_ix_models/model/material/report/reporter_utils.py

+44-22
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,33 @@ def add_methanol_share_calculations(rep: message_ix.Reporter, mode: str = "feeds
106106
if mode == "fuel":
107107
t_filter2.update({"l": ["primary"]})
108108

109-
rep.add("out::methanol", "select", "out:nl-t-ya-m-c-l", t_filter2)
110-
rep.add("out::methanol-by-tec", "group_sum", "out::methanol", group="t", sum="c")
109+
rep.add("out::methanol-prod", "select", "out:nl-t-ya-m-c-l", t_filter2)
111110
rep.add(
112-
"out::methanol-total", "group_sum", "out::methanol", group=["nl", "ya"], sum="t"
111+
"out::methanol-prod-by-tec",
112+
"group_sum",
113+
"out::methanol-prod",
114+
group="t",
115+
sum="c",
116+
)
117+
rep.add(
118+
"out::methanol-prod-total",
119+
"group_sum",
120+
"out::methanol-prod",
121+
group=["nl", "ya"],
122+
sum="t",
123+
)
124+
rep.add(
125+
"share::methanol-prod-by-tec",
126+
"div",
127+
"out::methanol-prod-by-tec",
128+
"out::methanol-prod-total",
113129
)
114-
rep.add("share::methanol", "div", "out::methanol-by-tec", "out::methanol-total")
115130
for comm, tecs in comm_tec_map.items():
116131
rep.add(
117-
f"share::{comm}methanol",
132+
f"share::{comm}-methanol-prod",
118133
"aggregate",
119-
"share::methanol",
120-
groups={"t": {f"{comm}methanol": tecs}},
134+
"share::methanol-prod-by-tec",
135+
groups={"t": {f"{comm}-methanol": tecs}},
121136
keep=False,
122137
)
123138

@@ -136,10 +151,8 @@ def add_meth_export_calculations(rep: message_ix.Reporter, mode: str = "feedstoc
136151
----------
137152
rep
138153
"""
154+
add_methanol_share_calculations(rep, mode=mode)
139155
for comm in comm_tec_map.keys():
140-
if f"share::{comm}methanol" not in rep.keys():
141-
add_methanol_share_calculations(rep, mode=mode)
142-
143156
t_filter2 = {"t": "meth_exp", "m": mode}
144157
rep.add("out::methanol-export", "select", "out:nl-t-ya-m", t_filter2)
145158
rep.add(
@@ -153,7 +166,7 @@ def add_meth_export_calculations(rep: message_ix.Reporter, mode: str = "feedstoc
153166
f"out::{comm}methanol-export",
154167
"mul",
155168
"out::methanol-export",
156-
f"share::{comm}methanol",
169+
f"share::{comm}-methanol-prod",
157170
)
158171

159172

@@ -174,10 +187,9 @@ def add_meth_import_calculations(rep: message_ix.Reporter, mode: str = "feedstoc
174187
----------
175188
rep
176189
"""
177-
for comm in comm_tec_map.keys():
178-
if "out::{comm}methanol-export" not in rep.keys():
179-
add_meth_export_calculations(rep, mode=mode)
180190

191+
add_meth_export_calculations(rep, mode=mode)
192+
for comm in comm_tec_map.keys():
181193
t_filter2 = {"t": "meth_imp", "m": mode}
182194
rep.add("out::methanol-import", "select", "out:nl-t-ya-m", t_filter2)
183195

@@ -205,7 +217,7 @@ def add_meth_import_calculations(rep: message_ix.Reporter, mode: str = "feedstoc
205217
f"share::{comm}methanol-import",
206218
"div",
207219
f"out::{comm}methanol-import",
208-
"out::methanol-total",
220+
"out::methanol-prod-total",
209221
)
210222

211223

@@ -220,17 +232,27 @@ def add_biometh_final_share(rep: message_ix.Reporter, mode: str = "feedstock"):
220232
----------
221233
rep
222234
"""
235+
add_meth_import_calculations(rep, mode=mode)
236+
if mode == "feedstock":
237+
t_filter2 = {
238+
"t": ["meth_t_d"],
239+
"m": [mode],
240+
}
241+
else:
242+
t_filter2 = {
243+
"t": ["meth_t_d", "furnace_methanol_refining"],
244+
"m": [mode, "high_temp"],
245+
}
223246
for comm in comm_tec_map.keys():
224-
if f"out::{comm}methanol-import" not in rep.keys():
225-
add_meth_import_calculations(rep, mode=mode)
226-
227-
t_filter2 = {"t": "meth_t_d", "m": mode}
228-
rep.add("in::methanol-final", "select", "in:nl-t-ya-m", t_filter2)
247+
rep.add("in::methanol-final0", "select", "in:nl-t-ya-m", t_filter2)
248+
rep.add(
249+
"in::methanol-final", "sum", "in::methanol-final0", dimensions=["t", "m"]
250+
)
229251
rep.add(
230252
f"out::{comm}methanol-prod",
231253
"mul",
232-
"out::methanol-total",
233-
f"share::{comm}methanol",
254+
"out::methanol-prod-total",
255+
f"share::{comm}-methanol-prod",
234256
)
235257
rep.add(
236258
f"out::{comm}methanol-final",

message_ix_models/model/material/report/run_reporting.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def run_fe_reporting(rep: message_ix.Reporter, model: str, scenario: str):
266266

267267
py_df_all = add_chemicals_to_final_energy_variables(dfs, rep, model, scenario)
268268

269-
py_df_all = update_liquids_subvariables(rep, py_df_all, model, scenario)
269+
py_df_all = split_fe_other(rep, py_df_all, model, scenario)
270270
df_final = (
271271
py_df_all.filter(unit="dimensionless", keep=False)
272272
.convert_unit("GWa", "EJ")
@@ -340,13 +340,13 @@ def add_chemicals_to_final_energy_variables(
340340
updated_fe_totals = []
341341
for comm in [
342342
"Electricity",
343+
"Gases",
344+
"Gases|Gas",
343345
"Liquids",
344346
"Liquids|Oil",
345347
"Solids",
346348
"Solids|Biomass",
347349
"Solids|Coal",
348-
"Gases",
349-
"Gases|Gas",
350350
"Hydrogen",
351351
]:
352352
updated_fe_totals.append(
@@ -363,10 +363,36 @@ def add_chemicals_to_final_energy_variables(
363363
return py_df_all
364364

365365

366-
def update_liquids_subvariables(
366+
def split_fe_other(
367367
rep: message_ix.Reporter, py_df_all: pyam.IamDataFrame, model: str, scenario: str
368368
):
369+
"""This function takes the Final Energy|Industry|*|Liquids|Other values
370+
and reallocates it to Liquids|Biomass/Coal/Oil/Gas based on the methanol
371+
feedstock shares.
372+
1) calculates the feedstock shares of methanol production with message_ix.Reporter
373+
2) append the shares as temporary iamc variables them to the existing reporting
374+
pyam object
375+
3) Uses pyam multiply feature to calculate shares with each "Liquids|Other"
376+
timeseries
377+
4) Uses pyam aggregate to sum existing Liquids|Biomass/Coal/Oil/Gas with new
378+
variables and store in separate pyam object
379+
5) Filters out existing (outdated) Liquids|Biomass/Coal/Oil/Gas from reporting
380+
pyam object
381+
6) Concats the updated variables with the full reporting
382+
383+
Parameters
384+
----------
385+
rep
386+
py_df_all
387+
model
388+
scenario
389+
390+
Returns
391+
-------
392+
393+
"""
369394
add_biometh_final_share(rep, mode="fuel")
395+
# set temporary filter on Reporter to speed up queries
370396
rep.set_filters(
371397
t=[
372398
"meth_bunker",
@@ -385,6 +411,7 @@ def update_liquids_subvariables(
385411
"meth_exp",
386412
"meth_imp",
387413
"meth_ind_fs",
414+
"furnace_methanol_refining"
388415
]
389416
)
390417
for c, full_name in zip(

0 commit comments

Comments
 (0)