2024-02-18 08:45:37 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors
|
2020-05-29 07:50:55 +00:00
|
|
|
#
|
2021-09-14 14:37:41 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
2020-05-29 07:50:55 +00:00
|
|
|
|
2019-11-28 15:33:33 +00:00
|
|
|
from os.path import normpath, exists
|
2023-03-08 17:43:52 +00:00
|
|
|
from shutil import copyfile, move, rmtree
|
2023-08-15 13:02:41 +00:00
|
|
|
from pathlib import Path
|
|
|
|
import yaml
|
2019-11-28 15:33:33 +00:00
|
|
|
|
Atlite availability (#224)
* adjust buil_cutout.py and Snakefile
* try adjusting build_renewable_profiles, currently crashing due to weird pyproj error
* build_renewable_profiles: -remove printing gid
* build_renewable_profiles: use dask for paralellization, use dense functions
* build_renewable_profiles:
- revise imports
- add logging for long calculation
- revise explaining comment
- revise distance calculation
* build profiles: adjust to cutout.grid
* * fix area to square km
* rename potmatrix -> capacity_potential
* rename available to availibility
* config.default update cutout params
build_renewable_potentials: major refactoring and simplification
hydro_profiles: update code
* build profiles: fix weight output dimensions
* build profiles: fix typo, fix selection of buses
* build profiles: reinsert paths variable
* follow up
* build profiles: move to dask calculation only
* CI: set build cutout to true (add CDSAPI)
* build profiles: use pyproj, test with gleas and geokit upstream
* environment.yaml fix atlite version
* build profiles: use dask 'processes' for more than 25 regions
* build profiles: specify dask scheduler according to number of regions
* backpedal a bit, only allow scheduler='processes'
* follow up, code style and fixup
* build profiles: add logger info for underwater fraction calc
* config adjust cutout parameters
Snakefile fixup
* config.default.yaml: adjust resolution
* config: use one cutout in total
build_cutout: automatic detetection of geographical boundaries
* env: add python>=3.8 requirement
build_cutout: fixup for region bound
* config: allow base cutout
* folllow up, fix up
* follow up II
* clean up
* clean up II
* build profiles: move back to multiprocessing due to performance issues
* small code style corrections
* move in pool context
* swqitch to ratsterio
* switch to rasterio for availibility calculation
* tiny fixup
* * build continental raster for offshore distance calculation
* adjust Snakefile to new script build_raster
* rename continental raster to onshore raster
add projected_mask function (not yet tested)
add docstrings, modularize
* Snakefile: remove build_onhore_raster rule, build mask directly from geometry instead
build_natura_raster: adjust code, add function for exporting
build_profiles:
* add buffer to shore distance to init_globals function
* update docstrings
* improve handling of nodata grid codes
* add geometry mask if natura raster not activated
(the 255 value is an 'eligible' value for the corine data base,
do this for excluding data outside the shape)
* build_profiles: adjust docstrings
* update environment
* build profiles: fixup reproject woth padding
* follow up, small fixups
* fix resampling method
checkpoint: reproduces solar profile in tut data
* reintegrate plot map
code style
* config: rename cutout into "base"
* build profiles: adjust to new atlite code
* natura raster: small fixup
* build natura raster: compress tiff file
* config: adjust cutout names
* build profiles: cover case if no or partial overlap between natura raster and cutout
* config-tutorial: adjust cutout params
* buid-profifiles: fixup in gebco filter
* follow up
* update config files
* build profiles: select layoutmatrix != 0
* build profiles: speed up average_distance and underwaterfraction
* build profiles: fix typo
* update release notes
build_cutout: only build needed features
* update envs
* config: add temperature to sarah features
* temporary fix for atlite v0.2.1 and new xarray version release
* env: remove xarray specification
* * remove rule build_country_flh
* build profiles: remove sneaked in line
* doc: update configuration.rst (section atlite) and corresponding csv table
* release notes: fix quotes
* build profiles: use 3035 for area calculation
* Update envs/environment.docs.yaml
* Update scripts/build_cutout.py
* Update doc/release_notes.rst
Co-authored-by: euronion <42553970+euronion@users.noreply.github.com>
* Update doc/configuration.rst
Co-authored-by: euronion <42553970+euronion@users.noreply.github.com>
* Update scripts/build_cutout.py
Co-authored-by: euronion <42553970+euronion@users.noreply.github.com>
* update release notes
* release notes: add deprecation of 'keep_all_available_areas'
build profiles: remove warning for 'keep_all_available_areas'
* build cutout: rearrage code, set buffer correctly
* Rename tutorial cutout to remove name clash with real cutout.
* Update release_notes.rst: Rename tutorial cutout.
* retrieve: update cutouts and downloads (alternative) (#237)
* retrieve: update cutouts and downloads
* retrieve: remove unnecessary import
* use snakemake remote file functionality
* Snakefile: update zenodo link
* update natura remote link (closes #234)
* env: update atlite version to 0.2.2
* env: fix dask version due to memory issues
* test: retrieve cutout instead of build
* test: use tutorial cutout for CI
Co-authored-by: euronion <42553970+euronion@users.noreply.github.com>
Co-authored-by: Fabian Neumann <fabian.neumann@outlook.de>
2021-04-27 15:58:31 +00:00
|
|
|
from snakemake.remote.HTTP import RemoteProvider as HTTPRemoteProvider
|
2023-03-08 16:44:25 +00:00
|
|
|
from snakemake.utils import min_version
|
2020-12-03 18:50:53 +00:00
|
|
|
|
2023-08-23 15:14:57 +00:00
|
|
|
from scripts._helpers import path_provider
|
2017-12-18 19:34:15 +00:00
|
|
|
|
2023-03-08 18:28:00 +00:00
|
|
|
min_version("7.7")
|
2023-08-15 13:02:41 +00:00
|
|
|
HTTP = HTTPRemoteProvider()
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2024-02-18 08:55:45 +00:00
|
|
|
default_files = {
|
|
|
|
"config/config.default.yaml": "config/config.yaml",
|
|
|
|
"config/scenarios.template.yaml": "config/scenarios.yaml",
|
|
|
|
}
|
|
|
|
for template, target in default_files.items():
|
|
|
|
target = os.path.join(workflow.current_basedir, target)
|
|
|
|
template = os.path.join(workflow.current_basedir, template)
|
|
|
|
if not exists(target) and exists(template):
|
|
|
|
copyfile(template, target)
|
2017-12-18 19:34:15 +00:00
|
|
|
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2024-02-12 11:10:05 +00:00
|
|
|
configfile: "config/config.default.yaml"
|
2023-04-21 09:05:18 +00:00
|
|
|
configfile: "config/config.yaml"
|
2018-08-03 09:49:23 +00:00
|
|
|
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2023-08-15 13:02:41 +00:00
|
|
|
run = config["run"]
|
2023-08-24 11:17:44 +00:00
|
|
|
scenarios = run.get("scenarios", {})
|
2024-02-18 09:24:26 +00:00
|
|
|
if run["name"] and scenarios.get("enable"):
|
|
|
|
fn = Path(scenarios["file"])
|
|
|
|
scenarios = yaml.safe_load(fn.read_text())
|
|
|
|
RDIR = "{run}/"
|
|
|
|
if run["name"] == "all":
|
|
|
|
config["run"]["name"] = list(scenarios.keys())
|
|
|
|
elif run["name"]:
|
|
|
|
RDIR = run["name"] + "/"
|
2023-08-15 13:02:41 +00:00
|
|
|
else:
|
|
|
|
RDIR = ""
|
2022-09-08 07:52:00 +00:00
|
|
|
|
2023-08-23 15:14:57 +00:00
|
|
|
logs = path_provider("logs/", RDIR, run["shared_resources"])
|
|
|
|
benchmarks = path_provider("benchmarks/", RDIR, run["shared_resources"])
|
|
|
|
resources = path_provider("resources/", RDIR, run["shared_resources"])
|
|
|
|
|
|
|
|
CDIR = "" if run["shared_cutouts"] else RDIR
|
2023-03-07 17:11:59 +00:00
|
|
|
RESULTS = "results/" + RDIR
|
2022-06-15 11:51:08 +00:00
|
|
|
|
2020-03-16 14:51:47 +00:00
|
|
|
|
2023-03-08 18:22:29 +00:00
|
|
|
localrules:
|
|
|
|
purge,
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2020-03-16 14:51:47 +00:00
|
|
|
|
2017-12-18 19:34:15 +00:00
|
|
|
wildcard_constraints:
|
2023-03-06 16:41:09 +00:00
|
|
|
simpl="[a-zA-Z0-9]*",
|
2023-04-27 08:10:30 +00:00
|
|
|
clusters="[0-9]+(m|c)?|all",
|
2023-03-06 16:41:09 +00:00
|
|
|
ll="(v|c)([0-9\.]+|opt)",
|
2019-06-18 09:50:54 +00:00
|
|
|
opts="[-+a-zA-Z0-9\.]*",
|
2023-03-06 18:44:57 +00:00
|
|
|
sector_opts="[-+a-zA-Z0-9\.\s]*",
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2017-12-18 19:34:15 +00:00
|
|
|
|
2023-03-08 18:22:00 +00:00
|
|
|
include: "rules/common.smk"
|
2023-03-08 16:29:01 +00:00
|
|
|
include: "rules/collect.smk"
|
|
|
|
include: "rules/retrieve.smk"
|
|
|
|
include: "rules/build_electricity.smk"
|
|
|
|
include: "rules/build_sector.smk"
|
|
|
|
include: "rules/solve_electricity.smk"
|
|
|
|
include: "rules/postprocess.smk"
|
2023-07-03 15:14:50 +00:00
|
|
|
include: "rules/validate.smk"
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2020-10-20 11:53:43 +00:00
|
|
|
|
2020-07-08 14:34:15 +00:00
|
|
|
if config["foresight"] == "overnight":
|
2017-12-18 19:34:15 +00:00
|
|
|
|
2023-03-08 16:29:01 +00:00
|
|
|
include: "rules/solve_overnight.smk"
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2017-12-18 19:34:15 +00:00
|
|
|
|
2020-07-30 06:27:33 +00:00
|
|
|
if config["foresight"] == "myopic":
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2023-03-08 16:29:01 +00:00
|
|
|
include: "rules/solve_myopic.smk"
|
2017-12-18 19:34:15 +00:00
|
|
|
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2023-04-03 14:09:50 +00:00
|
|
|
if config["foresight"] == "perfect":
|
|
|
|
|
|
|
|
include: "rules/solve_perfect.smk"
|
|
|
|
|
2023-09-29 06:40:23 +00:00
|
|
|
|
2023-09-22 07:01:12 +00:00
|
|
|
rule all:
|
|
|
|
input:
|
2024-02-10 17:09:23 +00:00
|
|
|
expand(RESULTS + "graphs/costs.pdf", run=config["run"]["name"]),
|
2023-09-22 07:01:12 +00:00
|
|
|
default_target: True
|
|
|
|
|
|
|
|
|
2024-02-18 09:40:44 +00:00
|
|
|
rule create_scenarios:
|
|
|
|
output:
|
|
|
|
config["run"]["scenarios"]["file"],
|
2024-02-18 10:03:37 +00:00
|
|
|
conda:
|
|
|
|
"envs/retrieve.yaml"
|
2024-02-18 09:40:44 +00:00
|
|
|
script:
|
|
|
|
"config/create_scenarios.py"
|
|
|
|
|
|
|
|
|
2023-03-08 17:15:49 +00:00
|
|
|
rule purge:
|
2023-03-08 17:43:52 +00:00
|
|
|
run:
|
2023-09-28 15:19:13 +00:00
|
|
|
import builtins
|
|
|
|
|
|
|
|
do_purge = builtins.input(
|
|
|
|
"Do you really want to delete all generated resources, \nresults and docs (downloads are kept)? [y/N] "
|
|
|
|
)
|
|
|
|
if do_purge == "y":
|
|
|
|
rmtree("resources/", ignore_errors=True)
|
|
|
|
rmtree("results/", ignore_errors=True)
|
|
|
|
rmtree("doc/_build", ignore_errors=True)
|
|
|
|
print("Purging generated resources, results and docs. Downloads are kept.")
|
|
|
|
else:
|
|
|
|
raise Exception(f"Input {do_purge}. Aborting purge.")
|
2018-01-30 22:11:16 +00:00
|
|
|
|
2020-09-11 10:40:53 +00:00
|
|
|
|
2023-03-08 17:43:52 +00:00
|
|
|
rule dag:
|
2023-03-08 18:22:29 +00:00
|
|
|
message:
|
|
|
|
"Creating DAG of workflow."
|
2018-01-30 22:11:16 +00:00
|
|
|
output:
|
2023-08-23 15:14:57 +00:00
|
|
|
dot=resources("dag.dot"),
|
|
|
|
pdf=resources("dag.pdf"),
|
|
|
|
png=resources("dag.png"),
|
2023-03-08 18:22:29 +00:00
|
|
|
conda:
|
|
|
|
"envs/environment.yaml"
|
2023-03-08 17:45:11 +00:00
|
|
|
shell:
|
2023-03-08 17:45:32 +00:00
|
|
|
"""
|
|
|
|
snakemake --rulegraph all | sed -n "/digraph/,\$p" > {output.dot}
|
|
|
|
dot -Tpdf -o {output.pdf} {output.dot}
|
|
|
|
dot -Tpng -o {output.png} {output.dot}
|
2023-03-08 18:22:29 +00:00
|
|
|
"""
|
2023-03-09 12:28:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
rule doc:
|
|
|
|
message:
|
|
|
|
"Build documentation."
|
2017-12-18 19:34:15 +00:00
|
|
|
output:
|
2023-03-09 12:29:13 +00:00
|
|
|
directory("doc/_build"),
|
2023-03-09 12:28:42 +00:00
|
|
|
shell:
|
|
|
|
"make -C doc html"
|
2023-07-03 15:14:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
rule sync:
|
|
|
|
params:
|
|
|
|
cluster=f"{config['remote']['ssh']}:{config['remote']['path']}",
|
|
|
|
shell:
|
|
|
|
"""
|
2023-07-06 17:14:01 +00:00
|
|
|
rsync -uvarh --ignore-missing-args --files-from=.sync-send . {params.cluster}
|
2023-09-19 10:22:16 +00:00
|
|
|
rsync -uvarh --no-g {params.cluster}/resources . || echo "No resources directory, skipping rsync"
|
2023-07-26 09:41:24 +00:00
|
|
|
rsync -uvarh --no-g {params.cluster}/results . || echo "No results directory, skipping rsync"
|
|
|
|
rsync -uvarh --no-g {params.cluster}/logs . || echo "No logs directory, skipping rsync"
|
2023-07-03 15:14:50 +00:00
|
|
|
"""
|