From c6a4de74693bee6a407fd9ded15b8f839ff2d66d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20H=C3=B6rsch?= Date: Fri, 3 Aug 2018 11:54:28 +0200 Subject: [PATCH] Generate cutouts on-demand --- Snakefile | 11 ++++++++++- config.yaml | 22 +++++++++++++++++++--- scripts/build_cutout.py | 12 ++++++++++++ scripts/build_hydro_profile.py | 4 +++- scripts/build_renewable_potentials.py | 4 +++- scripts/build_renewable_profiles.py | 5 ++++- 6 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 scripts/build_cutout.py diff --git a/Snakefile b/Snakefile index 1e9be6ab..b4efe832 100644 --- a/Snakefile +++ b/Snakefile @@ -77,8 +77,16 @@ rule build_bus_regions: resources: mem_mb=1000 script: "scripts/build_bus_regions.py" +rule build_cutout: + output: "cutouts/{cutout}" + resources: mem_mb=5000 + threads: config['atlite'].get('nprocesses', 4) + benchmark: "benchmarks/build_cutout_{cutout}" + script: "scripts/build_cutout.py" + rule build_renewable_potentials: input: + cutout=lambda wildcards: "cutouts/" + config["renewable"][wildcards.technology]['cutout'], corine="data/bundle/corine/g250_clc06_V18_5.tif", natura="data/bundle/natura/Natura2000_end2015.shp" output: "resources/potentials_{technology}.nc" @@ -92,7 +100,8 @@ rule build_renewable_profiles: potentials="resources/potentials_{technology}.nc", regions=lambda wildcards: ("resources/regions_onshore.geojson" if wildcards.technology in ('onwind', 'solar') - else "resources/regions_offshore.geojson") + else "resources/regions_offshore.geojson"), + cutout=lambda wildcards: "cutouts/" + config["renewable"][wildcards.technology]['cutout'], output: profile="resources/profile_{technology}.nc", resources: mem_mb=5000 diff --git a/config.yaml b/config.yaml index f4254dbc..938e0040 100644 --- a/config.yaml +++ b/config.yaml @@ -30,9 +30,25 @@ electricity: conventional_carriers: [nuclear, oil, OCGT, CCGT, coal, lignite, geothermal, biomass] +atlite: + cutout_directory: 'cutouts' + nprocesses: 1 + cutouts: + europe-2013-era5: + module: era5 + xs: [-12., 35.] + ys: [72., 33.] + years: [2013, 2013] + europe-2013-sarah: + module: sarah + resolution: 0.2 + xs: [-12., 42.] + ys: [65., 33.] + years: [2013, 2013] + renewable: onwind: - cutout: europe-2012-2016-era5 + cutout: europe-2013-era5 resource: method: wind turbine: Vestas_V112_3MW @@ -51,7 +67,7 @@ renewable: distance_grid_codes: [1, 2, 3, 4, 5, 6] natura: true offwind: - cutout: europe-2012-2016-era5 + cutout: europe-2013-era5 resource: method: wind turbine: NREL_ReferenceTurbine_5MW_offshore @@ -63,7 +79,7 @@ renewable: grid_codes: [44, 255] natura: true solar: - cutout: europe-2013-2015-sarah + cutout: europe-2013-sarah resource: method: pv panel: CSi diff --git a/scripts/build_cutout.py b/scripts/build_cutout.py new file mode 100644 index 00000000..49ff3357 --- /dev/null +++ b/scripts/build_cutout.py @@ -0,0 +1,12 @@ +import os +import atlite +import logging +logger = logging.getLogger(__name__) + +logging.basicConfig(level=snakemake.config['logging_level']) + +cutout = atlite.Cutout(snakemake.wildcards.cutout, + cutout_dir=os.path.dirname(snakemake.output.cutout), + **snakemake.config['atlite']['cutouts'][snakemake.wildcards.cutout]) + +cutout.prepare(nprocesses=snakemake.config['atlite'].get('nprocesses', 4)) diff --git a/scripts/build_hydro_profile.py b/scripts/build_hydro_profile.py index daef35e6..74cd93d5 100644 --- a/scripts/build_hydro_profile.py +++ b/scripts/build_hydro_profile.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import os import atlite import pandas as pd import geopandas as gpd @@ -8,7 +9,8 @@ import logging logger = logging.getLogger(__name__) logger.setLevel(level=snakemake.config['logging_level']) -cutout = atlite.Cutout(snakemake.config['renewable']['hydro']['cutout']) +cutout = atlite.Cutout(snakemake.config['renewable']['hydro']['cutout'], + cutout_dir=os.path.dirname(snakemake.input.cutout)) countries = snakemake.config['countries'] country_shapes = gpd.read_file(snakemake.input.country_shapes).set_index('id')['geometry'].reindex(countries) diff --git a/scripts/build_renewable_potentials.py b/scripts/build_renewable_potentials.py index 750d38a2..08e1e17d 100644 --- a/scripts/build_renewable_potentials.py +++ b/scripts/build_renewable_potentials.py @@ -1,3 +1,4 @@ +import os import atlite import xarray as xr import pandas as pd @@ -6,7 +7,8 @@ from vresutils import landuse as vlanduse config = snakemake.config['renewable'][snakemake.wildcards.technology] -cutout = atlite.Cutout(config['cutout']) +cutout = atlite.Cutout(config['cutout'], + cutout_dir=os.path.dirname(snakemake.input.cutout)) total_capacity = config['capacity_per_sqm'] * vlanduse._cutout_cell_areas(cutout) potentials = xr.DataArray(total_capacity * diff --git a/scripts/build_renewable_profiles.py b/scripts/build_renewable_profiles.py index 38a3e7b6..1fe6391e 100644 --- a/scripts/build_renewable_profiles.py +++ b/scripts/build_renewable_profiles.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import os import atlite import numpy as np import xarray as xr @@ -18,7 +19,9 @@ params = dict(years=slice(*time.year[[0, -1]]), months=slice(*time.month[[0, -1] regions = gpd.read_file(snakemake.input.regions).set_index('name') regions.index.name = 'bus' -cutout = atlite.Cutout(config['cutout'], **params) +cutout = atlite.Cutout(config['cutout'], + cutout_dir=os.path.dirname(snakemake.input.cutout), + **params) # Potentials potentials = xr.open_dataarray(snakemake.input.potentials)