Skip to content

Commit

Permalink
custom nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorb1 committed Sep 17, 2024
1 parent d8390e7 commit 2539f73
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
10 changes: 9 additions & 1 deletion workflow/rules/preprocess.smk
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ rule variable_costs:
shell:
'python workflow/scripts/osemosys_global/variablecosts.py 2> {log}'

def demand_custom_csv() -> str:
if config["nodes_to_add"]:
return "resources/data/custom_nodes/specified_annual_demand.csv"
else:
return []

rule demand_projections:
message:
"Generating demand data..."
Expand All @@ -175,10 +181,12 @@ rule demand_projections:
iamc_urb = "resources/data/iamc_db_URB_Countries.xlsx",
iamc_missing = "resources/data/iamc_db_POP_GDPppp_URB_Countries_Missing.xlsx",
td_losses = "resources/data/T&D Losses.xlsx",
ember = "resources/data/ember_yearly_electricity_data.csv"
ember = "resources/data/ember_yearly_electricity_data.csv",
custom_nodes = demand_custom_csv()
params:
start_year = config['startYear'],
end_year = config['endYear'],
custom_nodes = config["nodes_to_add"]
output:
csv_files = 'results/data/SpecifiedAnnualDemand.csv',
log:
Expand Down
48 changes: 48 additions & 0 deletions workflow/scripts/osemosys_global/demand/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""Custom Nodes logic"""

import pandas as pd
import itertools


def _get_custom_demand_expected(
nodes: list[str], start_year: int, end_year: int
) -> pd.DataFrame:
"""Gets formatted expected custom data"""

years = range(start_year, end_year + 1)

df = pd.DataFrame(
list(itertools.product(nodes, years)), columns=["CUSTOM_NODE", "YEAR"]
)
df["REGION"] = "GLOBAL"
df["FUEL"] = "ELC" + df["CUSTOM_NODE"] + "02"

return df


def import_custom_demand_data(csv: str) -> pd.DataFrame:
"""Gets all custom demand data"""
return pd.read_csv(csv)


def get_custom_demand_data(
all_custom: pd.DataFrame, nodes: list[str], start_year: int, end_year: int
) -> pd.DataFrame:
"""Gets merged custom demand data"""

expected = _get_custom_demand_expected(nodes, start_year, end_year)

df = pd.merge(expected, all_custom, how="left", on=["CUSTOM_NODE", "YEAR"])
df = df[["REGION", "FUEL", "YEAR", "VALUE"]]

return df


def merge_default_custom_data(
default: pd.DataFrame, custom: pd.DataFrame
) -> pd.DataFrame:
assert default.columns.equals(custom.columns)
df = pd.concat([default, custom], ignore_index=True)
df["VALUE"] = df["VALUE"].round(2)
df = df.drop_duplicates(keep="first", subset=["REGION", "FUEL", "YEAR"])
return df
14 changes: 14 additions & 0 deletions workflow/scripts/osemosys_global/demand/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from data import get_historical_urban_pop_wb, get_iamc_data, format_for_writing
from regression import perform_regression
from projection import perform_node_projections
from custom import (
get_custom_demand_data,
import_custom_demand_data,
merge_default_custom_data,
)


def main(
Expand Down Expand Up @@ -58,6 +63,8 @@ def main(
csv = snakemake.output.csv_files
start_year = snakemake.params.start_year
end_year = snakemake.params.end_year
custom_nodes = snakemake.params.custom_nodes
custom_nodes_data = snakemake.input.custom_nodes
else:
file_plexos = "resources/data/PLEXOS_World_2015_Gold_V1.1.xlsx"
file_plexos_demand = "resources/data/All_Demand_UTC_2015.csv"
Expand All @@ -72,6 +79,8 @@ def main(
csv = "SpecifiedAnnualDemand.csv"
start_year = 2020
end_year = 2050
custom_nodes = ["INDWE", "INDEA", "INDNE", "INDNO", "INDSO"]
custom_nodes_data = "resources/data/custom_nodes/specified_annual_demand.csv"

# first bring together original and missing iamc data
plexos = import_plexos_2015(file_plexos)
Expand Down Expand Up @@ -102,4 +111,9 @@ def main(

df = df[df.YEAR.isin(range(start_year, end_year + 1))]

if custom_nodes:
all_custom = import_custom_demand_data(custom_nodes_data)
custom = get_custom_demand_data(all_custom, custom_nodes, start_year, end_year)
df = merge_default_custom_data(df, custom)

df.to_csv(csv, index=False)

0 comments on commit 2539f73

Please sign in to comment.