Skip to content

Commit

Permalink
gracefully handle absent extra_functionality file; add file to path
Browse files Browse the repository at this point in the history
  • Loading branch information
fneum committed Jan 4, 2024
1 parent b4fb395 commit d145758
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
7 changes: 7 additions & 0 deletions rules/common.smk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def memory(w):
return int(factor * (10000 + 195 * int(w.clusters)))


def input_custom_extra_functionality(w):
path = config["solving"]["options"].get("custom_extra_functionality", False)
if path:
return workflow.source_path(path)
return []


# Check if the workflow has access to the internet by trying to access the HEAD of specified url
def has_internet_access(url="www.zenodo.org") -> bool:
import http.client as http_client
Expand Down
4 changes: 1 addition & 3 deletions rules/solve_electricity.smk
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ rule solve_network:
co2_sequestration_potential=config["sector"].get(
"co2_sequestration_potential", 200
),
custom_extra_functionality=workflow.source_path(
config["solving"]["options"].get("custom_extra_functionality", "")
),
custom_extra_functionality=input_custom_extra_functionality,
input:
network=RESOURCES + "networks/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}.nc",
config=RESULTS + "config.yaml",
Expand Down
4 changes: 1 addition & 3 deletions rules/solve_myopic.smk
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ rule solve_sector_network_myopic:
co2_sequestration_potential=config["sector"].get(
"co2_sequestration_potential", 200
),
custom_extra_functionality=workflow.source_path(
config["solving"]["options"].get("custom_extra_functionality", "")
),
custom_extra_functionality=input_custom_extra_functionality,
input:
network=RESULTS
+ "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc",
Expand Down
5 changes: 1 addition & 4 deletions rules/solve_overnight.smk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#
# SPDX-License-Identifier: MIT


rule solve_sector_network:
params:
solving=config["solving"],
Expand All @@ -11,9 +10,7 @@ rule solve_sector_network:
co2_sequestration_potential=config["sector"].get(
"co2_sequestration_potential", 200
),
custom_extra_functionality=workflow.source_path(
config["solving"]["options"].get("custom_extra_functionality", "")
),
custom_extra_functionality=input_custom_extra_functionality,
input:
network=RESULTS
+ "prenetworks/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}.nc",
Expand Down
4 changes: 1 addition & 3 deletions rules/solve_perfect.smk
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ rule solve_sector_network_perfect:
co2_sequestration_potential=config["sector"].get(
"co2_sequestration_potential", 200
),
custom_extra_functionality=workflow.source_path(
config["solving"]["options"].get("custom_extra_functionality", "")
),
custom_extra_functionality=input_custom_extra_functionality,
input:
network=RESULTS
+ "prenetworks-brownfield/elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_brownfield_all_years.nc",
Expand Down
2 changes: 2 additions & 0 deletions scripts/solve_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import logging
import os
import re
import sys

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -831,6 +832,7 @@ def extra_functionality(n, snapshots):
if snakemake.params.custom_extra_functionality:
source_path = snakemake.params.custom_extra_functionality
assert os.path.exists(source_path), f"{source_path} does not exist"
sys.path.append(os.path.dirname(source_path))
module_name = os.path.splitext(os.path.basename(source_path))[0]
module = importlib.import_module(module_name)
module.custom_extra_functionality(n, snapshots)
Expand Down

0 comments on commit d145758

Please sign in to comment.