|
6 | 6 | from message_ix import make_df
|
7 | 7 |
|
8 | 8 | import message_ix_models.util
|
| 9 | +from message_ix_models import ScenarioInfo |
| 10 | +from message_ix_models.model.material.data_util import ( |
| 11 | + gen_plastics_emission_factors, |
| 12 | + gen_chemicals_co2_ind_factors, |
| 13 | +) |
9 | 14 | from message_ix_models.model.material.material_demand import material_demand_calc
|
10 |
| -from message_ix_models.model.material.util import read_config |
11 |
| -from message_ix_models.util import broadcast, same_node |
| 15 | +from message_ix_models.model.material.util import combine_df_dictionaries, read_config |
| 16 | +from message_ix_models.util import broadcast, same_node, nodes_ex_world |
12 | 17 |
|
13 | 18 | if TYPE_CHECKING:
|
14 | 19 | from message_ix import Scenario
|
|
30 | 35 | }
|
31 | 36 |
|
32 | 37 |
|
33 |
| -def gen_data_methanol(scenario: "Scenario") -> Dict[str, pd.DataFrame]: |
| 38 | +def gen_data_methanol_new(scenario: "Scenario") -> Dict[str, pd.DataFrame]: |
34 | 39 | """
|
35 | 40 | Generates data for methanol industry model
|
36 | 41 |
|
@@ -88,6 +93,31 @@ def gen_data_methanol(scenario: "Scenario") -> Dict[str, pd.DataFrame]:
|
88 | 93 | )
|
89 | 94 | pars_dict["demand"] = df_final
|
90 | 95 |
|
| 96 | + s_info = ScenarioInfo(scenario) |
| 97 | + downstream_tec_pars = gen_meth_fs_downstream(s_info) |
| 98 | + meth_downstream_emi_top_down = gen_plastics_emission_factors(s_info, "methanol") |
| 99 | + meth_downstream_emi_bot_up = gen_chemicals_co2_ind_factors(s_info, "methanol") |
| 100 | + |
| 101 | + pars_dict = combine_df_dictionaries( |
| 102 | + pars_dict, |
| 103 | + downstream_tec_pars, |
| 104 | + meth_downstream_emi_top_down, |
| 105 | + meth_downstream_emi_bot_up, |
| 106 | + ) |
| 107 | + |
| 108 | + scen_rel_set = scenario.set("relation") |
| 109 | + for par in ["activity", "upper", "lower"]: |
| 110 | + df_rel = pars_dict[f"relation_{par}"] |
| 111 | + df_rel = df_rel[df_rel["relation"].isin(scen_rel_set.values)] |
| 112 | + exc_rels = [ |
| 113 | + i for i in df_rel["relation"].unique() if i not in scen_rel_set.values |
| 114 | + ] |
| 115 | + print( |
| 116 | + f"following relations are dropped from relation_{par} of methanol input " |
| 117 | + f"data because they are not compatible with the scenario: {exc_rels}" |
| 118 | + ) |
| 119 | + pars_dict[f"relation_{par}"] = df_rel |
| 120 | + |
91 | 121 | return pars_dict
|
92 | 122 |
|
93 | 123 |
|
@@ -197,7 +227,7 @@ def broadcast_years(
|
197 | 227 | return df_bc_node
|
198 | 228 |
|
199 | 229 |
|
200 |
| -def unpivot_input_data(df: pd.DataFrame, par_name: str) -> pd.DataFrame: |
| 230 | +def unpivot_input_data(df: pd.DataFrame, par_name: str): |
201 | 231 | """
|
202 | 232 | Unpivot data that is already contains columns for respective MESSAGEix parameter
|
203 | 233 | Parameters
|
@@ -257,3 +287,47 @@ def unpivot_input_data(df: pd.DataFrame, par_name: str) -> pd.DataFrame:
|
257 | 287 | ].index
|
258 | 288 | )
|
259 | 289 | return make_df(par_name, **df_final_full)
|
| 290 | + |
| 291 | + |
| 292 | +def gen_meth_fs_downstream(s_info: "ScenarioInfo") -> Dict[str, pd.DataFrame]: |
| 293 | + # input parameter |
| 294 | + yv_ya = s_info.yv_ya |
| 295 | + year_all = yv_ya["year_act"].unique() |
| 296 | + |
| 297 | + tec_name = "meth_ind_fs" |
| 298 | + cols = { |
| 299 | + "technology": tec_name, |
| 300 | + "commodity": "methanol", |
| 301 | + "mode": "M1", |
| 302 | + "level": "final_material", |
| 303 | + "time": "year", |
| 304 | + "time_origin": "year", |
| 305 | + "value": 1, |
| 306 | + "unit": "Mt", |
| 307 | + } |
| 308 | + df_in = ( |
| 309 | + make_df("input", **cols) |
| 310 | + .pipe(broadcast, node_loc=nodes_ex_world(s_info.N), year_act=year_all) |
| 311 | + .pipe(same_node) |
| 312 | + ) |
| 313 | + df_in["year_vtg"] = df_in["year_act"] |
| 314 | + |
| 315 | + # output parameter |
| 316 | + tec_name = "meth_ind_fs" |
| 317 | + cols = { |
| 318 | + "technology": tec_name, |
| 319 | + "commodity": "methanol", |
| 320 | + "mode": "M1", |
| 321 | + "level": "demand", |
| 322 | + "time": "year", |
| 323 | + "time_dest": "year", |
| 324 | + "value": 1, |
| 325 | + "unit": "Mt", |
| 326 | + } |
| 327 | + df_out = ( |
| 328 | + make_df("output", **cols) |
| 329 | + .pipe(broadcast, node_loc=nodes_ex_world(s_info.N), year_act=year_all) |
| 330 | + .pipe(same_node) |
| 331 | + ) |
| 332 | + df_out["year_vtg"] = df_out["year_act"] |
| 333 | + return dict(input=df_in, output=df_out) |
0 commit comments