diff --git a/message_ix_models/model/material/build.py b/message_ix_models/model/material/build.py index 46fd8b9abf..c925705894 100644 --- a/message_ix_models/model/material/build.py +++ b/message_ix_models/model/material/build.py @@ -13,32 +13,6 @@ log = logging.getLogger(__name__) -def get_spec() -> Mapping[str, ScenarioInfo]: - """Return the specification for materials accounting.""" - require = ScenarioInfo() - add = ScenarioInfo() - remove = ScenarioInfo() - - # Load configuration - # context = Context.get_instance(-1) - context = read_config() - - # Update the ScenarioInfo objects with required and new set elements - for type in SPEC_LIST: - for set_name, config in context["material"][type].items(): - # for cat_name, detail in config.items(): - # Required elements - require.set[set_name].extend(config.get("require", [])) - - # Elements to add - add.set[set_name].extend(config.get("add", [])) - - # Elements to remove - remove.set[set_name].extend(config.get("remove", [])) - - return dict(require=require, add=add, remove=remove) - - def ellipsize(elements: List) -> str: """Generate a short string representation of `elements`. diff --git a/message_ix_models/model/material/data_aluminum.py b/message_ix_models/model/material/data_aluminum.py index 3f59a33384..e72c493894 100644 --- a/message_ix_models/model/material/data_aluminum.py +++ b/message_ix_models/model/material/data_aluminum.py @@ -22,7 +22,7 @@ # Get endogenous material demand from buildings interface from .data_buildings import get_scen_mat_demand -from .build import get_spec +from . import get_spec def read_data_aluminum(scenario): diff --git a/message_ix_models/model/material/data_cement.py b/message_ix_models/model/material/data_cement.py index d791f008f0..1b91d11d31 100644 --- a/message_ix_models/model/material/data_cement.py +++ b/message_ix_models/model/material/data_cement.py @@ -21,7 +21,7 @@ # Get endogenous material demand from buildings interface from .data_buildings import get_scen_mat_demand -from .build import get_spec +from . import get_spec # gdp_growth = [0.121448215899944, 0.0733079014579874, 0.0348154093342843, \ diff --git a/message_ix_models/model/material/data_power_sector.py b/message_ix_models/model/material/data_power_sector.py index 25f8922ed8..9f8a5ac4f1 100644 --- a/message_ix_models/model/material/data_power_sector.py +++ b/message_ix_models/model/material/data_power_sector.py @@ -19,7 +19,7 @@ add_par_data, package_data_path, ) -from .build import get_spec +from . import get_spec def read_material_intensities(parameter, data_path, node, year, technology, commodity, level, inv_cost): @@ -210,7 +210,7 @@ def gen_data_power_sector(scenario, dry_run=False): # paths to lca data code_path = Path(__file__).parents[0] / "material_intensity" - data_path = context.package_data_path("material") + data_path = package_data_path("material") # Information about scenario, e.g. node, year s_info = ScenarioInfo(scenario) diff --git a/message_ix_models/model/material/data_steel.py b/message_ix_models/model/material/data_steel.py index 8a48acd090..509036d31a 100644 --- a/message_ix_models/model/material/data_steel.py +++ b/message_ix_models/model/material/data_steel.py @@ -23,7 +23,7 @@ add_par_data, package_data_path, ) -from .build import get_spec +from . import get_spec # Generate a fake steel demand diff --git a/message_ix_models/model/material/report/reporting.py b/message_ix_models/model/material/report/reporting.py index 09527c622c..13d66df0d5 100644 --- a/message_ix_models/model/material/report/reporting.py +++ b/message_ix_models/model/material/report/reporting.py @@ -46,6 +46,9 @@ from pyam.plotting import OUTSIDE_LEGEND from matplotlib.backends.backend_pdf import PdfPages +from message_ix_models.util import ( + package_data_path, +) def print_full(x): pd.set_option("display.max_rows", len(x)) @@ -192,7 +195,7 @@ def report(context,scenario): nodes.remove("R12_GLB*") # Path for materials reporting output - directory = context.get_local_path("report", "materials") + directory = package_data_path("material", "reporting_output") directory.mkdir(exist_ok=True) # Generate message_ix level reporting and dump to an excel file. diff --git a/message_ix_models/model/material/util.py b/message_ix_models/model/material/util.py index 78df1c7521..8863c0ee44 100644 --- a/message_ix_models/model/material/util.py +++ b/message_ix_models/model/material/util.py @@ -1,8 +1,11 @@ from message_ix_models import Context from pathlib import Path -from message_ix_models.util import load_private_data +from message_ix_models.util import package_data_path import pandas as pd +from pathlib import Path +import yaml + # Configuration files METADATA = [ # ("material", "config"), @@ -32,7 +35,7 @@ def read_config(): _parts = list(parts) _parts[-1] += ".yaml" - context[key] = load_private_data(*_parts) + context[key] = package_data_path(*_parts) # Read material.yaml # context.metadata_path=Path("C:/Users/unlu/Documents/GitHub/message_data/data") @@ -41,6 +44,20 @@ def read_config(): # Use a shorter name context["material"] = context["material set"] + # There was an error in context["material"][type].items() + # context["material"] is not the content of the yaml file but the path to + # the yaml file. Below section added to read the yaml file. + + try: + with open(context["material"], 'r') as yaml_file: + yaml_data = yaml.load(yaml_file, Loader=yaml.FullLoader) + except FileNotFoundError: + print(f"File not found: {file_path}") + except Exception as e: + print(f"Error reading YAML file: {e}") + + context["material"] = yaml_data + # Merge technology.yaml with set.yaml # context["material"]["steel"]["technology"]["add"] = ( # context.pop("transport technology") diff --git a/message_ix_models/tools/calibrate_UE_gr_to_demand.py b/message_ix_models/tools/calibrate_UE_gr_to_demand.py index c4e9b6a735..78c2d6bf5b 100644 --- a/message_ix_models/tools/calibrate_UE_gr_to_demand.py +++ b/message_ix_models/tools/calibrate_UE_gr_to_demand.py @@ -59,7 +59,7 @@ def main(scenario, data_path, ssp, region, first_mpa_year=None, intpol_lim=1, ve # Retrieve data for corresponding SSP data_filname = "SSP_UE_dyn_input.xlsx" - data_fil = data_path / "model" / "UE_dynamic_constraints" / data_filname + data_fil = data_path / "material" /"UE_dynamic_constraints" / data_filname mpa_data = pd.read_excel(data_fil, sheet_name="SSP_data") mpa_tec = mpa_data["technology"].tolist()