Skip to content

Commit

Permalink
feat: replace dict access to config in snakemake
Browse files Browse the repository at this point in the history
  • Loading branch information
amos-schledorn committed Nov 15, 2024
1 parent a7d4b06 commit 6d064bf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 94 deletions.
31 changes: 5 additions & 26 deletions rules/build_sector.smk
Original file line number Diff line number Diff line change
Expand Up @@ -284,35 +284,23 @@ rule build_central_heating_temperature_profiles:
"../scripts/build_central_heating_temperature_profiles/run.py"


def input_heat_source_potentials(w):

return {
heat_source_name: f"data/fraunhofer_heat_utilisation_potentials/{heat_source_name}.gpkg"
for heat_source_name in config_provider(
"sector", "district_heating", "fraunhofer_heat_utilisation_potentials"
)(w).keys()
if heat_source_name
in config_provider("sector", "heat_pump_sources", "urban central")(w)
}


rule build_heat_source_potentials:
params:
fraunhofer_heat_sources=config_provider(
"sector", "district_heating", "fraunhofer_heat_utilisation_potentials"
),
heat_source="{heat_source}",
input:
unpack(input_heat_source_potentials),
utilisation_potential="data/fraunhofer_heat_source_utilisation_potentials/{heat_source}.gpkg",
regions_onshore=resources("regions_onshore_base_s_{clusters}.geojson"),
output:
# TODO: this is a workaround since unpacked functions don't work in output
geothermal=resources("heat_source_potential_geothermal_base_s_{clusters}.csv"),
resources("heat_source_potential_{heat_source}_base_s_{clusters}.csv"),
resources:
mem_mb=2000,
log:
logs("build_heat_source_potentials_s_{clusters}.log"),
logs("build_heat_source_potentials_{heat_source}_s_{clusters}.log"),
benchmark:
benchmarks("build_heat_source_potentials/s_{clusters}")
benchmarks("build_heat_source_potentials/{heat_source}_s_{clusters}")
conda:
"../envs/environment.yaml"
script:
Expand Down Expand Up @@ -1124,15 +1112,6 @@ rule prepare_sector_network:
unpack(input_heat_source_potentials),
**rules.cluster_gas_network.output,
**rules.build_gas_input_locations.output,
# **{
# heat_source: resources(
# "heat_source_potential_" + heat_source + "_base_s_{clusters}.csv"
# )
# for heat_source in config["sector"]["district_heating"][
# "fraunhofer_heat_utilisation_potentials"
# ].keys()
# if heat_source in config["sector"]["heat_pump_sources"]["urban central"]
# },
snapshot_weightings=resources(
"snapshot_weightings_base_s_{clusters}_elec_l{ll}_{opts}_{sector_opts}.csv"
),
Expand Down
43 changes: 9 additions & 34 deletions rules/retrieve.smk
Original file line number Diff line number Diff line change
Expand Up @@ -634,42 +634,17 @@ if config["enable"]["retrieve"] and (

if config["enable"]["retrieve"]:

def input_heat_source_potentials(w):

return {
heat_source_name: storage(
f"https://fordatis.fraunhofer.de/bitstream/fordatis/341.3/10/{heat_source_features["key"]}.gpkg",
keep_local=True,
)
for heat_source_name, heat_source_features in config_provider(
"sector", "district_heating", "fraunhofer_heat_utilisation_potentials"
)(w).items()
if heat_source_name
in config_provider("sector", "heat_pump_sources", "urban central")(w)
}

def output_heat_source_potentials(w):

return {
heat_source_name: f"data/fraunhofer_heat_utilisation_potentials/{heat_source_name}.gpkg"
for heat_source_name in config_provider(
"sector", "district_heating", "fraunhofer_heat_utilisation_potentials"
)(w).keys()
if heat_source_name
in config_provider("sector", "heat_pump_sources", "urban central")(w)
}

rule retrieve_fraunhofer_heat_source_utilisation_potentials:
input:
unpack(input_heat_source_potentials),
params:
heat_source="{heat_source}",
fraunhofer_heat_utilisation_potentials=config_provider(
"sector", "district_heating", "fraunhofer_heat_utilisation_potentials"
),
log:
"logs/retrieve_heat_source_utilisation_potentials.log",
"logs/retrieve_fraunhofer_heat_source_potentials_{heat_source}.log",
resources:
mem_mb=500,
output:
geothermal="data/fraunhofer_heat_utilisation_potentials/geothermal.gpkg",
run:
for key in input.keys():
output_dir = Path(output[key]).parent
output_dir.mkdir(parents=True, exist_ok=True)
move(input[key], output[key])
"data/fraunhofer_heat_source_utilisation_potentials/{heat_source}.gpkg",
script:
"../scripts/retrieve_fraunhofer_heat_source_utilisation_potentials.py"
40 changes: 20 additions & 20 deletions scripts/build_heat_source_potentials/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ def get_unit_conversion_factor(
set_scenario_config(snakemake)

regions_onshore = gpd.read_file(snakemake.input.regions_onshore)
heat_source_utilisation_potential = gpd.read_file(
snakemake.input.utilisation_potential
)

heat_source_technical_potential = {}
for (
heat_source,
heat_source_features,
) in snakemake.params.fraunhofer_heat_sources.items():

heat_source_utilisation_potential = gpd.read_file(snakemake.input[heat_source])

heat_source_technical_potential[heat_source] = OnshoreRegionData(
onshore_regions=regions_onshore,
data=heat_source_utilisation_potential,
column_name=heat_source_features["column_name"],
scaling_factor=get_unit_conversion_factor(
input_unit=heat_source_features["unit"], output_unit="MWh"
)
/ heat_source_features["full_load_hours"],
).data_in_regions_scaled

heat_source_technical_potential[heat_source].to_csv(
snakemake.output[heat_source]
heat_source_technical_potential = OnshoreRegionData(
onshore_regions=regions_onshore,
data=heat_source_utilisation_potential,
column_name=snakemake.params.fraunhofer_heat_sources[
snakemake.params.heat_source
]["column_name"],
scaling_factor=get_unit_conversion_factor(
input_unit=snakemake.params.fraunhofer_heat_sources[
snakemake.params.heat_source
]["unit"],
output_unit="MWh",
)
/ snakemake.params.fraunhofer_heat_sources[snakemake.params.heat_source][
"full_load_hours"
],
).data_in_regions_scaled

heat_source_technical_potential.to_csv(snakemake.output[0])
29 changes: 15 additions & 14 deletions scripts/retrieve_fraunhofer_heat_source_utilisation_potentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,20 @@
rootpath = "."
configure_logging(snakemake)
set_scenario_config(snakemake)
breakpoint()

# license: https://creativecommons.org/licenses/by/4.0/
for heat_source_name, url in snakemake.input.items():
# download the data in url
filepath = Path(
f"data/fraunhofer_heat_utilisation_potentials/{heat_source_name}.gpkg"
)

logger.info(
f"Downloading heat source utilisation potential data for {heat_source_name} from '{url}'."
)
disable_progress = snakemake.config["run"].get("disable_progressbar", False)
progress_retrieve(url, filepath, disable=disable_progress)

logger.info(f"Data available at at {filepath}")
# download the data in url
heat_source = snakemake.params["heat_source"]
filepath = Path(snakemake.output[0])
if not filepath.parent.exists():
filepath.parent.mkdir(parents=True)

url = f"https://fordatis.fraunhofer.de/bitstream/fordatis/341.3/10/{snakemake.params.fraunhofer_heat_utilisation_potentials[heat_source]['key']}.gpkg"

logger.info(
f"Downloading heat source utilisation potential data for {heat_source} from '{url}'."
)
disable_progress = snakemake.config["run"].get("disable_progressbar", False)
progress_retrieve(url, filepath, disable=disable_progress)

logger.info(f"Data available at at {filepath}")

0 comments on commit 6d064bf

Please sign in to comment.