Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent unwanted subprocess termination #1079

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
11 changes: 11 additions & 0 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ def terminate_if_cutout_exists(config=config):
Check if any of the requested cutout files exist.
If that's the case, terminate execution to avoid data loss.
"""
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--mode", type=int, default=snakemake.Mode.default)
parser.add_argument("--target-jobs", type=str, nargs="+", default=[])
args, _ = parser.parse_known_args(sys.argv)
if args.mode == snakemake.Mode.subprocess and "build_cutout" not in " ".join(
args.target_jobs
):
return

config_cutouts = [
d_value["cutout"] for tc, d_value in config["renewable"].items()
] + list(config["atlite"]["cutouts"].keys())
Expand Down
1 change: 1 addition & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ PyPSA-Earth 0.4.1
* Fixed problematic float parsing (`_parse_float`) in `clean_osm_data.py` to make sure all OSM lines are correctly accounted for `PR #1089 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1089>`__
* Fix minor bug for advanced csp implementation `PR #1076 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1076>`__
* Fix minor bug in `build_powerplants.py` where the gas technology assignment incorrectly introduced NaN values for all powerplant technologies. `PR #1102 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1102>`__
* Fix unwanted termination due to existing cutout during snakemake subprocess calls `PR #1079 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1079>`__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind moving this up? :)



PyPSA-Earth 0.4.0
Expand Down
50 changes: 50 additions & 0 deletions test/test_cutout_termination.smk
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# SPDX-FileCopyrightText: PyPSA-Earth and PyPSA-Eur Authors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for the test example! :)
Do you think it shall be included or can we remove it? [less material to maintain]

#
# SPDX-License-Identifier: AGPL-3.0-or-later

import os
import sys

CUTOUT = "cutout.nc"


def terminate_if_cutout_exists(subprocess_check=False):
"""
Check if any of the requested cutout files exist.
If that's the case, terminate execution to avoid data loss.
"""
if subprocess_check:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--mode", type=int, default=snakemake.Mode.default)
parser.add_argument("--target-jobs", type=str, nargs="+", default=[])
args, _ = parser.parse_known_args(sys.argv)
if args.mode == snakemake.Mode.subprocess and "build_cutout" not in " ".join(
args.target_jobs
):
return

if os.path.exists(CUTOUT):
os.remove(CUTOUT)
raise Exception(
f"A cutout file {CUTOUT} exists and risks to be overwritten. "
"If this is an intended behavior, please move or delete this file and re-run the rule."
)


terminate_if_cutout_exists(subprocess_check=config.get("subprocess_check", False))


rule remove_cutout:
input:
cutout=CUTOUT,
run:
os.remove(input.cutout)


rule build_cutout:
output:
cutout=CUTOUT,
shell:
"echo build_cutout > {output.cutout}"
Loading