From 2b43f43006a026bd6ee70233a9844b5c5013533f Mon Sep 17 00:00:00 2001 From: Fabian Neumann Date: Wed, 15 Jun 2022 13:51:08 +0200 Subject: [PATCH] add more versatile cutout building with weather years --- Snakefile | 13 ++++++++++++- scripts/build_cutout.py | 16 +++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Snakefile b/Snakefile index 94ad7926..9a65d5df 100644 --- a/Snakefile +++ b/Snakefile @@ -18,7 +18,7 @@ ATLITE_NPROCESSES = config['atlite'].get('nprocesses', 4) wildcard_constraints: - weather_year="[0-9]*", + weather_year="[0-9]{4}|$", simpl="[a-zA-Z0-9]*|all", clusters="[0-9]+m?|all", ll="(v|c)([0-9\.]+|opt|all)|all", @@ -148,6 +148,9 @@ rule build_bus_regions: script: "scripts/build_bus_regions.py" if config['enable'].get('build_cutout', False): + + ruleorder: build_cutout_year > build_cutout + rule build_cutout: input: regions_onshore="resources/regions_onshore.geojson", @@ -159,6 +162,14 @@ if config['enable'].get('build_cutout', False): resources: mem_mb=ATLITE_NPROCESSES * 1000 script: "scripts/build_cutout.py" + rule build_cutout_year: + input: rules.build_cutout.input + output: "cutouts/{cutout}-{weather_year}.nc" + log: "logs/build_cutout/{cutout}-{weather_year}.log" + benchmark: "benchmarks/build_cutout_{cutout}-{weather_year}" + threads: ATLITE_NPROCESSES + resources: mem_mb=ATLITE_NPROCESSES * 1000 + script: "scripts/build_cutout.py" if config['enable'].get('retrieve_cutout', True): rule retrieve_cutout: diff --git a/scripts/build_cutout.py b/scripts/build_cutout.py index 2722a62b..a5681b00 100644 --- a/scripts/build_cutout.py +++ b/scripts/build_cutout.py @@ -103,15 +103,21 @@ logger = logging.getLogger(__name__) if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake - snakemake = mock_snakemake('build_cutout', cutout='europe-2013-era5') + # snakemake = mock_snakemake('build_cutout_year', cutout='europe-era5', weather_year=2011) + snakemake = mock_snakemake('build_cutout', cutout='europe-era5') configure_logging(snakemake) - # TODO make it accept year independent config cutout_params = snakemake.config['atlite']['cutouts'][snakemake.wildcards.cutout] - snapshots = pd.date_range(freq='h', **snakemake.config['snapshots']) - time = [snapshots[0], snapshots[-1]] - cutout_params['time'] = slice(*cutout_params.get('time', time)) + if hasattr(snakemake.wildcards, 'weather_year'): + time = snakemake.wildcards.weather_year + cutout_params["time"] = [time, time] + + if "time" not in cutout_params: + snapshots = pd.date_range(freq='h', **snakemake.config['snapshots']) + cutout_params["time"] = [snapshots[0], snapshots[-1]] + + cutout_params['time'] = slice(*cutout_params['time']) if {'x', 'y', 'bounds'}.isdisjoint(cutout_params): # Determine the bounds from bus regions with a buffer of two grid cells