From b135a0cc01a76dbe81ffcdc3ca5cdc63891ab846 Mon Sep 17 00:00:00 2001 From: Fabian Neumann Date: Thu, 4 Jan 2024 19:42:22 +0100 Subject: [PATCH 1/3] build_ship_raster: performance improvement --- scripts/build_ship_raster.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/scripts/build_ship_raster.py b/scripts/build_ship_raster.py index 02f4d5d5..7025e85d 100644 --- a/scripts/build_ship_raster.py +++ b/scripts/build_ship_raster.py @@ -62,11 +62,13 @@ if __name__ == "__main__": xs, Xs, ys, Ys = zip(*(determine_cutout_xXyY(cutout) for cutout in cutouts)) with zipfile.ZipFile(snakemake.input.ship_density) as zip_f: - zip_f.extract("shipdensity_global.tif") - with rioxarray.open_rasterio("shipdensity_global.tif") as ship_density: - ship_density = ship_density.drop_vars(["band"]).sel( - x=slice(min(xs), max(Xs)), y=slice(max(Ys), min(ys)) - ) - ship_density.rio.to_raster(snakemake.output[0]) + resources = Path(snakemake.output[0]).parent + fn = "shipdensity_global.tif" + zip_f.extract(fn, resources) + with rioxarray.open_rasterio(resources / fn) as ship_density: + ship_density = ship_density.drop_vars(["band"]).sel( + x=slice(min(xs), max(Xs)), y=slice(max(Ys), min(ys)) + ) + ship_density.rio.to_raster(snakemake.output[0]) - os.remove("shipdensity_global.tif") + (resources / fn).unlink() From c1f527b663b5e9f11f283f2052ab23eaba3c9788 Mon Sep 17 00:00:00 2001 From: Fabian Neumann Date: Thu, 4 Jan 2024 19:44:29 +0100 Subject: [PATCH 2/3] build_ship_raster: adjust imports --- scripts/build_ship_raster.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build_ship_raster.py b/scripts/build_ship_raster.py index 7025e85d..da8c8b28 100644 --- a/scripts/build_ship_raster.py +++ b/scripts/build_ship_raster.py @@ -42,8 +42,8 @@ Description """ import logging -import os import zipfile +from pathlib import Path import rioxarray from _helpers import configure_logging From 0534f574e9ef15b59d475991080fd2c47c8cd2c0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 4 Jan 2024 19:18:07 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- rules/common.smk | 2 +- scripts/add_brownfield.py | 4 +--- scripts/prepare_sector_network.py | 6 ++++-- scripts/solve_network.py | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/rules/common.smk b/rules/common.smk index 0e85b620..2298ff91 100644 --- a/rules/common.smk +++ b/rules/common.smk @@ -4,7 +4,7 @@ import os, sys, glob -helper_source_path = [match for match in glob.glob('**/_helpers.py', recursive=True)] +helper_source_path = [match for match in glob.glob("**/_helpers.py", recursive=True)] for path in helper_source_path: path = os.path.dirname(os.path.abspath(path)) diff --git a/scripts/add_brownfield.py b/scripts/add_brownfield.py index e151c441..cb1f51c8 100644 --- a/scripts/add_brownfield.py +++ b/scripts/add_brownfield.py @@ -133,9 +133,7 @@ def disable_grid_expansion_if_LV_limit_hit(n): # allow small numerical differences if lv_limit - total_expansion < 1: - logger.info( - f"LV is already reached, disabling expansion and LV limit" - ) + logger.info(f"LV is already reached, disabling expansion and LV limit") extendable_acs = n.lines.query("s_nom_extendable").index n.lines.loc[extendable_acs, "s_nom_extendable"] = False n.lines.loc[extendable_acs, "s_nom"] = n.lines.loc[extendable_acs, "s_nom_min"] diff --git a/scripts/prepare_sector_network.py b/scripts/prepare_sector_network.py index 79bc67e9..8a45da2e 100644 --- a/scripts/prepare_sector_network.py +++ b/scripts/prepare_sector_network.py @@ -3000,8 +3000,9 @@ def add_industry(n, costs): if options["co2_spatial"] or options["co2network"]: p_set = ( - -industrial_demand.loc[nodes, "process emission"] - .rename(index=lambda x: x + " process emissions") + -industrial_demand.loc[nodes, "process emission"].rename( + index=lambda x: x + " process emissions" + ) / nhours ) else: @@ -3414,6 +3415,7 @@ def cluster_heat_buses(n): pnl = c.pnl agg = define_clustering(pd.Index(pnl.keys()), aggregate_dict) for k in pnl.keys(): + def renamer(s): return s.replace("residential ", "").replace("services ", "") diff --git a/scripts/solve_network.py b/scripts/solve_network.py index 203d8b0f..aa802ea8 100644 --- a/scripts/solve_network.py +++ b/scripts/solve_network.py @@ -856,7 +856,7 @@ def solve_network(n, config, solving, opts="", **kwargs): kwargs["assign_all_duals"] = cf_solving.get("assign_all_duals", False) if kwargs["solver_name"] == "gurobi": - logging.getLogger('gurobipy').setLevel(logging.CRITICAL) + logging.getLogger("gurobipy").setLevel(logging.CRITICAL) rolling_horizon = cf_solving.pop("rolling_horizon", False) skip_iterations = cf_solving.pop("skip_iterations", False)