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

Tighten constraint on usage of global vars #1549

Merged
merged 5 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
- name: Run pylint check on scripts
# check for undefined variables to reuse functions across scripts
run: |
pylint --disable=all --enable=E0601 --output-format=parseable scripts/add_* scripts/prepare_* scripts/solve_*
pylint --disable=all --enable=E0601,E0606 --output-format=parseable scripts/add_* scripts/prepare_* scripts/solve_*

- name: Run snakemake test workflows
run: |
Expand Down
2 changes: 2 additions & 0 deletions doc/configtables/sector.csv
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ methanol,--,--,Add methanol as carrrier and add enabled methnol technologies
-- -- ccgt_cc,--,"{true, false}"," Add combined cycle gas turbine (CCGT) with carbon capture using methanol"
-- -- ocgt,--,"{true, false}"," Add open cycle gas turbine (OCGT) using methanol"
-- -- allam,--,"{true, false}"," Add Allam cycle gas power plants using methanol"
-- -- biomass_to_methanol,--,"{true, false}"," Add biomass to methanol"
-- -- biomass_to_methanol_cc,--,"{true, false}"," Add biomass to methanol with carbon capture"
ammonia,--,"{true, false, regional}","Add ammonia as a carrrier. It can be either true (copperplated NH3), false (no NH3 carrier) or ""regional"" (regionalised NH3 without network)"
min_part_load_fischer _tropsch,per unit of p_nom ,float,The minimum unit dispatch (``p_min_pu``) for the Fischer-Tropsch process
min_part_load _methanolisation,per unit of p_nom ,float,The minimum unit dispatch (``p_min_pu``) for the methanolisation process
Expand Down
2 changes: 1 addition & 1 deletion scripts/add_brownfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def update_heat_pump_efficiency(n: pypsa.Network, n_p: pypsa.Network, year: int)
planning_horizons=2050,
)

configure_logging(snakemake)
configure_logging(snakemake) # pylint: disable=E0606
set_scenario_config(snakemake)

update_config_from_wildcards(snakemake.config, snakemake.wildcards)
Expand Down
6 changes: 4 additions & 2 deletions scripts/add_electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ def attach_hydro(
capital_cost=costs.at["ror", "capital_cost"],
weight=ror["p_nom"],
p_max_pu=(
inflow_t[ror.index]
inflow_t[ror.index] # pylint: disable=E0606
.divide(ror["p_nom"], axis=1)
.where(lambda df: df <= 1.0, other=1.0)
),
Expand Down Expand Up @@ -787,6 +787,8 @@ def attach_hydro(
max_hours_country = (
hydro_stats["E_store[TWh]"] * 1e3 / hydro_stats["p_nom_discharge[GW]"]
)
else:
raise ValueError(f"Unknown hydro_max_hours method: {hydro_max_hours}")

max_hours_country.clip(0, inplace=True)

Expand Down Expand Up @@ -1089,7 +1091,7 @@ def attach_stores(
from _helpers import mock_snakemake

snakemake = mock_snakemake("add_electricity", clusters=100)
configure_logging(snakemake)
configure_logging(snakemake) # pylint: disable=E0606
set_scenario_config(snakemake)

params = snakemake.params
Expand Down
7 changes: 5 additions & 2 deletions scripts/add_existing_baseyear.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def get_efficiency(
key = f"{carrier} services space efficiency"
efficiency = nodes.str[:2].map(efficiencies[key])
else:
logger.warning(f"{heat_system} not defined.")
raise ValueError(f"Heat system {heat_system} not defined.")

return efficiency

Expand Down Expand Up @@ -518,6 +518,9 @@ def add_heating_capacities_installed_before_baseyear(
energy_totals_year
]

ratios = []
valid_grouping_years = []

for heat_system in existing_capacities.columns.get_level_values(0).unique():
heat_system = HeatSystem(heat_system)

Expand Down Expand Up @@ -710,7 +713,7 @@ def add_heating_capacities_installed_before_baseyear(
planning_horizons=2030,
)

configure_logging(snakemake)
configure_logging(snakemake) # pylint: disable=E0606
set_scenario_config(snakemake)

update_config_from_wildcards(snakemake.config, snakemake.wildcards)
Expand Down
2 changes: 1 addition & 1 deletion scripts/add_transmission_projects_and_dlr.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def attach_line_rating(
from _helpers import mock_snakemake

snakemake = mock_snakemake("add_transmission_projects_and_dlr")
configure_logging(snakemake)
configure_logging(snakemake) # pylint: disable=E0606
set_scenario_config(snakemake)

params = snakemake.params
Expand Down
2 changes: 1 addition & 1 deletion scripts/prepare_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def set_line_nom_max(
ll="v1.0",
opts="Co2L-4H",
)
configure_logging(snakemake)
configure_logging(snakemake) # pylint: disable=E0606
set_scenario_config(snakemake)
update_config_from_wildcards(snakemake.config, snakemake.wildcards)

Expand Down
7 changes: 4 additions & 3 deletions scripts/prepare_osm_network_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ def export_clean_csv(df, columns, output_file):
return None


def create_geometries(network, crs=GEO_CRS):
def create_geometries(network, is_converter, crs=GEO_CRS):
"""
Create GeoDataFrames for different network components with specified coordinate reference system (CRS).

Parameters
----------
network (PyPSA Network): The network object containing buses, lines, links, converters, and transformers data.
is_converter (bool): Boolean that specifies if link element is a converter.
crs (str, optional): Coordinate reference system to be used for the GeoDataFrames. Defaults to GEO_CRS.

Returns
Expand Down Expand Up @@ -225,7 +226,7 @@ def create_geometries(network, crs=GEO_CRS):

snakemake = mock_snakemake("prepare_osm_network_release")

configure_logging(snakemake)
configure_logging(snakemake) # pylint: disable=E0606
set_scenario_config(snakemake)

# Params
Expand Down Expand Up @@ -312,7 +313,7 @@ def create_geometries(network, crs=GEO_CRS):

### Create interactive map
buses, lines, links, converters, transformers = create_geometries(
network, crs=GEO_CRS
network, is_converter=is_converter, crs=GEO_CRS
)
stations_polygon = gpd.read_file(snakemake.input.stations_polygon)
buses_polygon = gpd.read_file(snakemake.input.buses_polygon)
Expand Down
12 changes: 7 additions & 5 deletions scripts/prepare_perfect_foresight.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ def adjust_electricity_grid(n: pypsa.Network, year: int, years: list[int]) -> No


# --------------------------------------------------------------------
def concat_networks(years: list[int], network_paths: list[str]) -> pypsa.Network:
def concat_networks(
years: list[int], network_paths: list[str], social_discountrate: float
) -> pypsa.Network:
"""
Concat given pypsa networks and add build years.

Expand Down Expand Up @@ -341,7 +343,7 @@ def set_phase_out(
n.links.loc[assets_i, "lifetime"] = (phase_out_year - build_year).astype(float)


def set_all_phase_outs(n: pypsa.Network) -> None:
def set_all_phase_outs(n: pypsa.Network, years: list[int]) -> None:
# TODO move this to a csv or to the config
planned = [
(["nuclear"], "DE", 2022),
Expand Down Expand Up @@ -605,7 +607,7 @@ def update_heat_pump_efficiency(n: pypsa.Network, years: list[int]) -> None:
ll="v1.5",
sector_opts="1p7-4380H-T-H-B-I-A-dist1",
)
configure_logging(snakemake)
configure_logging(snakemake) # pylint: disable=E0606
set_scenario_config(snakemake)

update_config_from_wildcards(snakemake.config, snakemake.wildcards)
Expand All @@ -621,7 +623,7 @@ def update_heat_pump_efficiency(n: pypsa.Network, years: list[int]) -> None:
network_paths = [snakemake.input.brownfield_network] + [
snakemake.input[f"network_{year}"] for year in years[1:]
]
n = concat_networks(years, network_paths)
n = concat_networks(years, network_paths, social_discountrate)

# temporal aggregate
solver_name = snakemake.config["solving"]["solver"]["name"]
Expand All @@ -637,7 +639,7 @@ def update_heat_pump_efficiency(n: pypsa.Network, years: list[int]) -> None:
adjust_stores(n)

# set phase outs
set_all_phase_outs(n)
set_all_phase_outs(n, years)

# add H2 boiler
add_H2_boilers(n)
Expand Down
Loading
Loading