[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2023-09-11 21:03:58 +00:00
parent 823df52309
commit 3eed341044
5 changed files with 88 additions and 56 deletions

View File

@ -6,8 +6,8 @@
import contextlib
import logging
import os
import urllib
import re
import urllib
from pathlib import Path
import pandas as pd
@ -25,6 +25,7 @@ REGION_COLS = ["geometry", "name", "x", "y", "country"]
def get_opt(opts, expr, flags=None):
"""
Return the first option matching the regular expression.
The regular expression is case-insensitive by default.
"""
if flags is None:
@ -35,6 +36,7 @@ def get_opt(opts, expr, flags=None):
return match.group(0)
return None
# Define a context manager to temporarily mute print statements
@contextlib.contextmanager
def mute_print():

View File

@ -71,6 +71,7 @@ idx = pd.IndexSlice
logger = logging.getLogger(__name__)
def find_opt(opts, expr):
"""
Return if available the float after the expression.
@ -84,6 +85,7 @@ def find_opt(opts, expr):
return True, None
return False, None
def add_co2limit(n, co2limit, Nyears=1.0):
n.add(
"GlobalConstraint",
@ -314,7 +316,9 @@ if __name__ == "__main__":
nhours_enable_config = nhours_opts_config.get("enable", None)
nhours_config = str(nhours_opts_config.get("hour", None)) + "h"
nhours_wildcard = get_opt(opts, r"^\d+h$")
if nhours_wildcard is not None or (nhours_enable_config and nhours_config is not None):
if nhours_wildcard is not None or (
nhours_enable_config and nhours_config is not None
):
nhours = nhours_wildcard or nhours_config
n = average_every_nhours(n, nhours)
@ -323,15 +327,21 @@ if __name__ == "__main__":
time_seg_enable_config = nhours_opts_config.get("enable", None)
time_seg_config = str(nhours_opts_config.get("hour", None)) + "seg"
time_seg_wildcard = get_opt(opts, r"^\d+seg$")
if time_seg_wildcard is not None or (time_seg_enable_config and time_seg_config is not None):
if time_seg_wildcard is not None or (
time_seg_enable_config and time_seg_config is not None
):
time_seg = time_seg_wildcard or time_seg_config
solver_name = snakemake.config["solving"]["solver"]["name"]
n = apply_time_segmentation(n, time_seg, solver_name)
Co2L_config = snakemake.params.co2limit_enable and isinstance(snakemake.params.co2limit,float)
Co2L_config = snakemake.params.co2limit_enable and isinstance(
snakemake.params.co2limit, float
)
Co2L_wildcard, co2limit_wildcard = find_opt(opts, "Co2L")
if Co2L_wildcard or Co2L_config:
if co2limit_wildcard is not None: # TODO: what if you wat to determine the factor through the wildcard?
if (
co2limit_wildcard is not None
): # TODO: what if you wat to determine the factor through the wildcard?
co2limit = co2limit_wildcard * snakemake.params.co2base
add_co2limit(n, co2limit, Nyears)
logger.info("Setting CO2 limit according to wildcard value.")
@ -339,10 +349,14 @@ if __name__ == "__main__":
add_co2limit(n, snakemake.params.co2limit, Nyears)
logger.info("Setting CO2 limit according to config value.")
CH4L_config = snakemake.params.gaslimit_enable and isinstance(snakemake.params.gaslimit,float)
CH4L_config = snakemake.params.gaslimit_enable and isinstance(
snakemake.params.gaslimit, float
)
CH4L_wildcard, gaslimit_wildcard = find_opt(opts, "CH4L")
if CH4L_wildcard or CH4L_config:
if gaslimit_wildcard is not None: # TODO: what if you wat to determine the factor through the wildcard?
if (
gaslimit_wildcard is not None
): # TODO: what if you wat to determine the factor through the wildcard?
gaslimit = gaslimit_wildcard * 1e6
add_gaslimit(n, gaslimit, Nyears)
logger.info("Setting gas usage limit according to wildcard value.")

View File

@ -17,7 +17,7 @@ import numpy as np
import pandas as pd
import pypsa
import xarray as xr
from _helpers import generate_periodic_profiles, update_config_with_sector_opts, get_opt
from _helpers import generate_periodic_profiles, get_opt, update_config_with_sector_opts
from add_electricity import calculate_annuity, sanitize_carriers
from build_energy_totals import build_co2_totals, build_eea_co2, build_eurostat_co2
from networkx.algorithms import complement
@ -175,9 +175,13 @@ def emission_sectors_from_opts(opts):
"international navigation",
]
heat_and_industry = opts_config.get("industry",False) and opts_config.get("heating",False)
heat_and_industry = opts_config.get("industry", False) and opts_config.get(
"heating", False
)
if ("I" in opts and "H" in opts and"A" in opts) or (heat_and_industry and opts_config.get("agriculture_machinery",False)):
if ("I" in opts and "H" in opts and "A" in opts) or (
heat_and_industry and opts_config.get("agriculture_machinery", False)
):
sectors += ["agriculture"]
return sectors
@ -3264,7 +3268,9 @@ def set_temporal_aggregation(n, opts, solver_name):
nhours_enable_config = nhours_opts_config.get("enable", None)
nhours_config = str(nhours_opts_config.get("hour", None)) + "H"
nhours_wildcard = get_opt(opts, r"^\d+h$")
if nhours_wildcard is not None or (nhours_enable_config and nhours_config is not None):
if nhours_wildcard is not None or (
nhours_enable_config and nhours_config is not None
):
nhours = nhours_wildcard or nhours_config
n = average_every_nhours(n, nhours)
return n
@ -3274,7 +3280,9 @@ def set_temporal_aggregation(n, opts, solver_name):
snapshots_enable_config = snapshots_opts_config.get("enable", None)
snapshots_config = snapshots_opts_config.get("hour", None)
snapshots_wildcard = get_opt(opts, r"(^\d+)sn$")
if snapshots_wildcard is not None or (snapshots_enable_config and snapshots_config is not None):
if snapshots_wildcard is not None or (
snapshots_enable_config and snapshots_config is not None
):
sn = int(snapshots_wildcard[:-2]) or snapshots_config
logger.info(f"Use every {sn} snapshot as representative")
n.set_snapshots(n.snapshots[::sn])
@ -3286,7 +3294,9 @@ def set_temporal_aggregation(n, opts, solver_name):
time_seg_enable_config = nhours_opts_config.get("enable", None)
time_seg_config = nhours_opts_config.get("hour", None)
time_seg_wildcard = get_opt(opts, r"^(\d+)seg$")
if time_seg_wildcard is not None or (time_seg_enable_config and time_seg_config is not None):
if time_seg_wildcard is not None or (
time_seg_enable_config and time_seg_config is not None
):
segments = int(time_seg_wildcard[:-3]) or time_seg_config
logger.info(f"Use temporal segmentation with {segments} segments")
n = apply_time_segmentation(n, segments, solver_name=solver_name)
@ -3320,7 +3330,9 @@ if __name__ == "__main__":
opts_config = snakemake.params.enable_sector
heat_and_industry = opts_config.get("industry",False) and opts_config.get("heating",False)
heat_and_industry = opts_config.get("industry", False) and opts_config.get(
"heating", False
)
investment_year = int(snakemake.wildcards.planning_horizons[-4:])
@ -3393,10 +3405,14 @@ if __name__ == "__main__":
if "I" in opts or opts_config.get("industry", False):
add_industry(n, costs)
if ("I" in opts and "H" in opts) or (heat_and_industry and opts_config.get("waste_heat",False)):
if ("I" in opts and "H" in opts) or (
heat_and_industry and opts_config.get("waste_heat", False)
):
add_waste_heat(n)
if ("I" in opts and "H" in opts and"A" in opts) or (heat_and_industry and opts_config.get("agriculture_machinery",False)): # requires H and I
if ("I" in opts and "H" in opts and "A" in opts) or (
heat_and_industry and opts_config.get("agriculture_machinery", False)
): # requires H and I
add_agriculture(n, costs)
if options["dac"]: