From 5cece636eec3b2acfbe3be6ddb278ed1eff02f24 Mon Sep 17 00:00:00 2001 From: bobbyxng Date: Thu, 22 Aug 2024 22:02:52 +0200 Subject: [PATCH 1/5] Added functionality to set custom_busmaps for different base_networks. --- rules/build_electricity.smk | 17 ++++++++++++----- scripts/cluster_network.py | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/rules/build_electricity.smk b/rules/build_electricity.smk index 4446ef765..e3e38e5f5 100644 --- a/rules/build_electricity.smk +++ b/rules/build_electricity.smk @@ -530,6 +530,17 @@ rule simplify_network: "../scripts/simplify_network.py" +# Optional input when using custom busmaps - Needs to be tailored to selected base_network +def input_cluster_network(w): + if config_provider("enable", "custom_busmap", default=False)(w): + base_network = config_provider("electricity", "base_network")(w) + custom_busmap = f"data/custom_busmap_elec_s{w.simpl}_{w.clusters}_{base_network}.csv" + return { + "custom_busmap": custom_busmap + } + return {"custom_busmap": []} + + rule cluster_network: params: cluster_network=config_provider("clustering", "cluster_network"), @@ -546,15 +557,11 @@ rule cluster_network: length_factor=config_provider("lines", "length_factor"), costs=config_provider("costs"), input: + unpack(input_cluster_network), network=resources("networks/elec_s{simpl}.nc"), regions_onshore=resources("regions_onshore_elec_s{simpl}.geojson"), regions_offshore=resources("regions_offshore_elec_s{simpl}.geojson"), busmap=ancient(resources("busmap_elec_s{simpl}.csv")), - custom_busmap=lambda w: ( - "data/custom_busmap_elec_s{simpl}_{clusters}.csv" - if config_provider("enable", "custom_busmap", default=False)(w) - else [] - ), tech_costs=lambda w: resources( f"costs_{config_provider('costs', 'year')(w)}.csv" ), diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index 79184167e..66453d210 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -36,7 +36,7 @@ - ``resources/regions_offshore_elec_s{simpl}.geojson``: confer :ref:`simplify` - ``resources/busmap_elec_s{simpl}.csv``: confer :ref:`simplify` - ``networks/elec_s{simpl}.nc``: confer :ref:`simplify` -- ``data/custom_busmap_elec_s{simpl}_{clusters}.csv``: optional input +- ``data/custom_busmap_elec_s{simpl}_{clusters}_{base_network}.csv``: optional input Outputs ------- From 561db386acc972cdd93aa540e02a070447fd7dd9 Mon Sep 17 00:00:00 2001 From: bobbyxng Date: Fri, 23 Aug 2024 09:40:01 +0200 Subject: [PATCH 2/5] Updated configtables and location for busmaps. --- doc/configtables/enable.csv | 2 +- rules/build_electricity.smk | 2 +- scripts/cluster_network.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/configtables/enable.csv b/doc/configtables/enable.csv index 0268319eb..5359131de 100644 --- a/doc/configtables/enable.csv +++ b/doc/configtables/enable.csv @@ -4,5 +4,5 @@ retrieve_databundle,bool,"{true, false}","Switch to retrieve databundle from zen retrieve_cost_data,bool,"{true, false}","Switch to retrieve technology cost data from `technology-data repository `_." build_cutout,bool,"{true, false}","Switch to enable the building of cutouts via the rule :mod:`build_cutout`." retrieve_cutout,bool,"{true, false}","Switch to enable the retrieval of cutouts from zenodo with :mod:`retrieve_cutout`." -custom_busmap,bool,"{true, false}","Switch to enable the use of custom busmaps in rule :mod:`cluster_network`. If activated the rule looks for provided busmaps at ``data/custom_busmap_elec_s{simpl}_{clusters}.csv`` which should have the same format as ``resources/busmap_elec_s{simpl}_{clusters}.csv``, i.e. the index should contain the buses of ``networks/elec_s{simpl}.nc``." +custom_busmap,bool,"{true, false}","Switch to enable the use of custom busmaps in rule :mod:`cluster_network`. If activated the rule looks for provided busmaps at ``data/busmaps/elec_s{simpl}_{clusters}_{base_network}.csv`` which should have the same format as ``resources/busmap_elec_s{simpl}_{clusters}.csv``, i.e. the index should contain the buses of ``networks/elec_s{simpl}.nc``. {base_network} is the name of the selected base_network in electricity, e.g. ``gridkit``, ``osm-prebuilt``, or ``osm-raw``." drop_leap_day,bool,"{true, false}","Switch to drop February 29 from all time-dependent data in leap years" diff --git a/rules/build_electricity.smk b/rules/build_electricity.smk index e3e38e5f5..ae0dab1fa 100644 --- a/rules/build_electricity.smk +++ b/rules/build_electricity.smk @@ -534,7 +534,7 @@ rule simplify_network: def input_cluster_network(w): if config_provider("enable", "custom_busmap", default=False)(w): base_network = config_provider("electricity", "base_network")(w) - custom_busmap = f"data/custom_busmap_elec_s{w.simpl}_{w.clusters}_{base_network}.csv" + custom_busmap = f"data/busmaps/elec_s{w.simpl}_{w.clusters}_{base_network}.csv" return { "custom_busmap": custom_busmap } diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index 66453d210..ab6b23ada 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -462,7 +462,7 @@ def plot_busmap_for_n_clusters(n, n_clusters, solver_name="scip", fn=None): if "snakemake" not in globals(): from _helpers import mock_snakemake - snakemake = mock_snakemake("cluster_network", simpl="", clusters="40") + snakemake = mock_snakemake("cluster_network", simpl="", clusters="40", run = "osm-prebuilt_nuts2") configure_logging(snakemake) set_scenario_config(snakemake) From 411980174f115da990b8632296a8340cb82c0b7c Mon Sep 17 00:00:00 2001 From: bobbyxng Date: Fri, 23 Aug 2024 09:43:42 +0200 Subject: [PATCH 3/5] Added release nots. --- doc/release_notes.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/release_notes.rst b/doc/release_notes.rst index b1345e0c7..4df38b019 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -91,6 +91,8 @@ Upcoming Release * In :mod:`base_network`, replace own voronoi polygon calculation function with Geopandas `gdf.voronoi_polygons` method. +* Move custom busmaps to ```data/busmaps/elec_s{simpl}_{clusters}_{base_network}.csv``` (if enabled). This allows for different busmaps depending on the base network and scenario. + PyPSA-Eur 0.11.0 (25th May 2024) ===================================== From 6957eecdd7b694012a73cf9dff99416a5645b46a Mon Sep 17 00:00:00 2001 From: bobbyxng Date: Fri, 23 Aug 2024 09:45:58 +0200 Subject: [PATCH 4/5] removed run parameter in mock_snakemake (only used for debugging). --- scripts/cluster_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index ab6b23ada..66453d210 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -462,7 +462,7 @@ def plot_busmap_for_n_clusters(n, n_clusters, solver_name="scip", fn=None): if "snakemake" not in globals(): from _helpers import mock_snakemake - snakemake = mock_snakemake("cluster_network", simpl="", clusters="40", run = "osm-prebuilt_nuts2") + snakemake = mock_snakemake("cluster_network", simpl="", clusters="40") configure_logging(snakemake) set_scenario_config(snakemake) From cf63c11f1a21dfc87a201cb0fd60454f92e77a85 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 23 Aug 2024 15:35:35 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- rules/build_electricity.smk | 4 +--- scripts/cluster_network.py | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/rules/build_electricity.smk b/rules/build_electricity.smk index ae0dab1fa..1f568e99c 100644 --- a/rules/build_electricity.smk +++ b/rules/build_electricity.smk @@ -535,9 +535,7 @@ def input_cluster_network(w): if config_provider("enable", "custom_busmap", default=False)(w): base_network = config_provider("electricity", "base_network")(w) custom_busmap = f"data/busmaps/elec_s{w.simpl}_{w.clusters}_{base_network}.csv" - return { - "custom_busmap": custom_busmap - } + return {"custom_busmap": custom_busmap} return {"custom_busmap": []} diff --git a/scripts/cluster_network.py b/scripts/cluster_network.py index 9bc5550b2..b63747c24 100644 --- a/scripts/cluster_network.py +++ b/scripts/cluster_network.py @@ -511,9 +511,7 @@ def plot_busmap_for_n_clusters(n, n_clusters, solver_name="scip", fn=None): # Fast-path if no clustering is necessary busmap = n.buses.index.to_series() linemap = n.lines.index.to_series() - clustering = pypsa.clustering.spatial.Clustering( - n, busmap, linemap - ) + clustering = pypsa.clustering.spatial.Clustering(n, busmap, linemap) else: Nyears = n.snapshot_weightings.objective.sum() / 8760