Skip to content

Commit

Permalink
Merge branch 'feature/extend_steel_example' into feature/amount-muss-…
Browse files Browse the repository at this point in the history
…als-number-formatiertnormiert-werden
  • Loading branch information
SabineHaas committed Oct 22, 2024
2 parents 06fa64d + c065665 commit 02fd1ef
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ __pycache__/
/tests/_files/tabular_datapackage_hack_a_thon/data/
/tests/_files/tabular_datapackage_hack_a_thon/datapackage.json
/tests/_files/tabular_datapackage_mininmal_example/
/tests/_files/tsam/*
/examples/industry/collections/*
/examples/industry/datapackage/*
92 changes: 92 additions & 0 deletions data_adapter_oemof/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,57 @@ def get_default_parameters(
if self.get("carrier") == "carrier":
defaults["carrier"] = self.get_busses()["bus"]

if self.get("amount") == None:
amount = 9999999999999
logger.warning(
f"Adding parameter 'amount' with value {amount} to commodity "
f"{self.process_name}. \n "
f"This is beneficial if your commodity is functioning as "
f"shortage or unlimited import/source. "
f"Otherwise please add 'amount' to your commodity!"
)
defaults["amount"] = amount

return defaults


class CommodityGHGAdapter(CommodityAdapter):
"""
CommodityGHGAdapter
"""

type = "commodity_ghg"
facade = facades.CommodityGHG

def get_busses(self) -> dict:
bus_list = self.structure["outputs"]
bus_dict = {}
counter = 0
for bus in bus_list:
if not bus.startswith("emi"):
bus_dict["bus"] = bus
elif bus.startswith("emi"):
bus_dict[f"emission_bus_{counter}"] = bus
counter += 1

# check that bus is defined
if bus_dict.get("bus") is None:
raise KeyError(f"{self.process_name} is missing 'bus', the input.")
return bus_dict

def get_default_parameters(self) -> dict:
defaults = super().get_default_parameters()
for key, value in self.data.items():
if key.startswith("ef"):
# adapt to the naming convention in oemof.tabular commodityGHG facade: emission_factor_<emission_bus_label>
target_label = None
emission_bus_labels = [key for item, key in defaults.items() if item.startswith("emission_bus")]
for label in emission_bus_labels:
if label in key:
target_label = label
if target_label == None:
raise ValueError(f"Emission factor of {self.process_name} is named {key} but None of the emission buses matches: {emission_bus_labels}.")
defaults[f"emission_factor_{target_label}"] = value
return defaults


Expand All @@ -448,6 +499,44 @@ class ConversionAdapter(Adapter):
facade = facades.Conversion


class ConversionGHGAdapter(Adapter):
"""
ConversionGHGAdapter
"""

type = "conversion_ghg"
facade = facades.ConversionGHG

def get_busses(self) -> dict:
def get_bus_from_struct(bus_list: list, bus_key: str) -> dict:
bus_dict = {}
counter = 0
for bus in bus_list:
if not bus.startswith("emi"):
bus_dict[f"{bus_key}"] = bus
elif bus.startswith("emi"):
bus_dict[f"emission_bus_{counter}"] = bus
counter += 1
return bus_dict

return_bus_dict = get_bus_from_struct(
self.structure["inputs"], bus_key="from_bus"
) | get_bus_from_struct(self.structure["outputs"], bus_key="to_bus")

# check that from_bus and to_bus is defined
for key in ["from_bus", "to_bus"]:
if return_bus_dict.get(key) is None:
raise KeyError(f"{self.process_name} is missing {key}.")
return return_bus_dict

def get_default_parameters(self) -> dict:
defaults = super().get_default_parameters()
for key, value in self.data.items():
if key.startswith("ef"):
defaults[key.replace("ef", "emission_factor")] = value
return defaults


class LoadAdapter(Adapter):
"""
LoadAdapter
Expand Down Expand Up @@ -528,10 +617,13 @@ def get_default_parameters(self) -> dict:
"emissions_factor_",
"conversion_factor_",
"flow_share_",
"ef_",
)
for key, value in self.data.items():
for keyword in keywords:
if key.startswith(keyword):
if key.startswith("ef"):
key = key.replace("ef", "emission_factor")
defaults[key] = value
return defaults

Expand Down
17 changes: 17 additions & 0 deletions data_adapter_oemof/build_datapackage.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ def build_datapackage(
parameter_map: Optional[dict] = PARAMETER_MAP,
bus_map: Optional[dict] = BUS_MAP,
location_to_save_to: str = None,
debug=False,
):
"""
Creating a Datapackage from the oemof_data_adapter that fits oemof.tabular Datapackages.
Expand Down Expand Up @@ -545,6 +546,22 @@ def _reduce_lists(x):
)
periods = cls.get_periods_from_parametrized_sequences(parametrized_sequences)

def reduce_data_frame(data_frame, steps=4):
"""reduces `df` to 5 time steps per period"""
df = data_frame.copy()
df["ind"] = df.index
df["ind"] = df["ind"].apply(
lambda x: True if x.month == 1 and x.day == 1 and x.hour <= steps else False
)
df_reduced = df.loc[df["ind"] == 1].drop(columns=["ind"])
return df_reduced

if debug:
periods = reduce_data_frame(data_frame=periods)
for key, value in parametrized_sequences.items():
df_short = reduce_data_frame(data_frame=value)
parametrized_sequences.update({key: df_short})

return cls(
parametrized_elements=parametrized_elements,
parametrized_sequences=parametrized_sequences,
Expand Down
20 changes: 19 additions & 1 deletion examples/industry/data_adapter_industry.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@
deserialize_constraints,
deserialize_energy_system,
)
from oemof.tabular.facades import Commodity, Conversion, Excess, Load, Storage, Volatile
from oemof.tabular.facades import (
Commodity,
Conversion,
Excess,
Load,
Volatile,
Storage,
ConversionGHG,
CommodityGHG,
)
from oemof_industry.mimo_converter import MIMO

from data_adapter_oemof.build_datapackage import DataPackage # noqa: E402
Expand All @@ -23,6 +32,9 @@
EnergySystem.from_datapackage = classmethod(deserialize_energy_system)

Model.add_constraints_from_datapackage = deserialize_constraints

DEBUG = True # set to False for full run. DEBUG reduces to 5 time steps per period

"""
Download Collection
Expand Down Expand Up @@ -242,6 +254,7 @@
},
"x2x_storage_hydrogen_lohc_1": {
"efficiency": "efficiency_sto_in",
"fixed_costs": "cost_fix_w",
"loss_rate": "sto_self_discharge",
"storage_capacity_cost": "cost_inv_e",
"fixed_costs": "cost_fix_p",
Expand All @@ -251,6 +264,7 @@
"efficiency": "efficiency_sto_in",
"loss_rate": "sto_self_discharge",
"storage_capacity_cost": "cost_inv_e",
"fixed_costs": "cost_fix_w",
"fixed_costs": "cost_fix_p",
"marginal_cost": "cost_var_e",
"capacity_capacity_potential": "capacity_e_max",
Expand All @@ -260,6 +274,7 @@
"fixed_costs": "cost_fix_p",
"loss_rate": "sto_self_discharge",
"storage_capacity_cost": "cost_inv_e",
"fixed_costs": "cost_fix_p",
"marginal_cost": "cost_var_e",
"capacity_capacity_potential": "capacity_e_max",
"storage_capacity": "capacity_e_inst",
Expand All @@ -284,6 +299,7 @@
adapter=adapter,
process_adapter_map=process_adapter_map,
parameter_map=parameter_map,
debug=DEBUG, # set DEBUG to False for full run. DEBUG reduces to 5 time steps per period
)
datapackage_path = pathlib.Path(__file__).parent / "datapackage"
dp.save_datapackage_to_csv(str(datapackage_path))
Expand All @@ -301,6 +317,8 @@
"volatile": Volatile,
"mimo": MIMO,
"storage": Storage,
"conversion_ghg": ConversionGHG,
"commodity_ghg": CommodityGHG,
},
)

Expand Down
Binary file not shown.

0 comments on commit 02fd1ef

Please sign in to comment.