Skip to content

Commit

Permalink
Change imports and data paths
Browse files Browse the repository at this point in the history
  • Loading branch information
GamzeUnlu95 committed Oct 4, 2023
1 parent 32f257f commit 236fade
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 35 deletions.
26 changes: 0 additions & 26 deletions message_ix_models/model/material/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion message_ix_models/model/material/data_aluminum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion message_ix_models/model/material/data_cement.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
4 changes: 2 additions & 2 deletions message_ix_models/model/material/data_power_sector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion message_ix_models/model/material/data_steel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
add_par_data,
package_data_path,
)
from .build import get_spec
from . import get_spec


# Generate a fake steel demand
Expand Down
5 changes: 4 additions & 1 deletion message_ix_models/model/material/report/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 19 additions & 2 deletions message_ix_models/model/material/util.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion message_ix_models/tools/calibrate_UE_gr_to_demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 236fade

Please sign in to comment.