Skip to content

Commit

Permalink
change script structure; modify dh shares in csv before prepare_secto…
Browse files Browse the repository at this point in the history
…r_network
  • Loading branch information
cpschau committed Aug 8, 2024
1 parent 0887025 commit c9ac4fb
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 213 deletions.
58 changes: 38 additions & 20 deletions workflow/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,25 @@ rule retrieve_ariadne_database:
"scripts/retrieve_ariadne_database.py"


# def input_profile_offwind(w):
# return {
# f"profile_{tech}": resources(f"profile_{tech}.nc")
# for tech in ["offwind-ac", "offwind-dc"]
# if (tech in config["electricity"]["renewable_carriers"])
# }
def input_profile_offwind(w):
return {
f"profile_{tech}": resources(f"profile_{tech}.nc")
for tech in ["offwind-ac", "offwind-dc", "offwind-float"]
if (tech in config["electricity"]["renewable_carriers"])
}

# use rule prepare_sector_network from pypsaeur with:
# input:
# unpack(input_profile_offwind),
# # **{k: v for k, v in rules.prepare_sector_network.input.items() if k != "costs"},
# # costs=resources("modified-costs_{planning_horizons}.csv"),

use rule prepare_sector_network from pypsaeur with:
input:
unpack(input_profile_offwind),
**{
k: v
for k, v in rules.prepare_sector_network.input.items()
if k != "district_heat_share"
},
district_heat_share=resources(
"district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}-modified.csv"
),


rule modify_cost_data:
Expand Down Expand Up @@ -172,29 +179,40 @@ rule build_mobility_demand:
"scripts/build_mobility_demand.py"


rule modify_dh_systems:
params:
district_heating=config_provider("sector", "district_heating"),
rule build_egon_data:
input:
network=RESULTS
+ "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc",
fn="data/egon/demandregio_spatial_2018.json",
fn_map=storage(
"https://ffeopendatastorage.blob.core.windows.net/opendata/mapping_from_4_to_38.json",
keep_local=True,
),
nuts3=resources("nuts3_shapes.geojson"),
output:
heating_technologies_nuts3=resources("heating_technologies_nuts3.geojson"),
script:
"scripts/build_egon_data.py"


ruleorder: modify_district_heat_share > build_district_heat_share


rule modify_district_heat_share:
params:
district_heating=config_provider("sector", "district_heating"),
input:
heating_technologies_nuts3=resources("heating_technologies_nuts3.geojson"),
regions_onshore=resources("regions_onshore_elec_s{simpl}_{clusters}.geojson"),
district_heat_share=resources(
"district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv"
),
output:
network=RESULTS
+ "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_dh.nc",
district_heat_share=resources(
"district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}-modified.csv"
),
resources:
mem_mb=1000,
script:
"scripts/modify_dh_systems.py"
"scripts/modify_district_heat_share.py"


rule modify_prenetwork:
Expand Down Expand Up @@ -223,7 +241,7 @@ rule modify_prenetwork:
),
input:
network=RESULTS
+ "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_dh.nc",
+ "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc",
wkn=(
resources("wasserstoff_kernnetz_elec_s{simpl}_{clusters}.csv")
if config_provider("wasserstoff_kernnetz", "enable")
Expand Down
71 changes: 71 additions & 0 deletions workflow/scripts/build_egon_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
import logging

logger = logging.getLogger(__name__)
import json

import geopandas as gpd
import pandas as pd

"""
Load and prepares the egon data about district heating in Germany on NUTS3
level.
Output:
GeoDataFrame: A GeoDataFrame containing the processed egon data.
"""


if __name__ == "__main__":
if "snakemake" not in globals():
import os
import sys

os.chdir(os.path.dirname(os.path.abspath(__file__)))

path = "../submodules/pypsa-eur/scripts"
sys.path.insert(0, os.path.abspath(path))
from _helpers import mock_snakemake

snakemake = mock_snakemake(
"build_egon_data",
run="KN2045_Bal_v4",
)

logger.info("Retrieving and cleaning egon data")

nuts3 = gpd.read_file(snakemake.input.nuts3)[
["index", "pop", "geometry"]
] # Keep only necessary columns

internal_id = {
9: "Hard coal",
10: "Brown coal",
11: "Natural gas",
34: "Heating oil",
35: "Biomass (solid)",
68: "Ambient heating",
69: "Solar heat",
71: "District heating",
72: "Electrical energy",
218: "Biomass (excluding wood, biogas)",
}

with open(snakemake.input.fn) as datafile:
data = json.load(datafile)["data"]
df = pd.DataFrame(data)

id_region = pd.read_json(snakemake.input.fn_map)

df["internal_id"] = df["internal_id"].apply(lambda x: x[0])

df["nuts3"] = df.id_region.map(id_region.set_index(id_region.id_region_from).kuerzel_to)

heat_tech_per_region = df.groupby([df.nuts3, df.internal_id]).sum().value.unstack()
heat_tech_per_region.rename(columns=internal_id, inplace=True)

egon_df = heat_tech_per_region.merge(nuts3, left_on="nuts3", right_on="index")
egon_gdf = gpd.GeoDataFrame(egon_df) # Convert merged DataFrame to GeoDataFrame
egon_gdf = egon_gdf.to_crs("EPSG:4326")

egon_gdf.to_file(snakemake.output.heating_technologies_nuts3)
193 changes: 0 additions & 193 deletions workflow/scripts/modify_dh_systems.py

This file was deleted.

Loading

0 comments on commit c9ac4fb

Please sign in to comment.