Skip to content

Commit 9717cb0

Browse files
Consistent function scope (#1537)
* start removing snakemake from functions * move snakemake objects out of prepare_sector_functions * continue with add_existing_baseyear * continue with prepare_perfect_foresight * apply it to add_electricity * continue with solve network * add pylint check to reusable functions * iron out follow up issues * adjust pylint check
1 parent dfd75a2 commit 9717cb0

File tree

9 files changed

+1420
-364
lines changed

9 files changed

+1420
-364
lines changed

.github/workflows/test.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ jobs:
8585
conda env update -n pypsa-eur -f ${{ env.env_file }}
8686
echo "Run conda list" && conda list
8787
88+
- name: Run pylint check on scripts
89+
# check for undefined variables to reuse functions across scripts
90+
run: |
91+
pylint --disable=all --enable=E0601 --output-format=parseable scripts/add_* scripts/prepare_* scripts/solve_*
92+
8893
- name: Run snakemake test workflows
8994
run: |
9095
make test

envs/environment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ dependencies:
5959
- ipython
6060
- pre-commit
6161
- ruff
62+
- pylint
6263

6364
- pip:
6465
- gurobipy

scripts/add_brownfield.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,32 @@
2323
idx = pd.IndexSlice
2424

2525

26-
def add_brownfield(n, n_p, year):
26+
def add_brownfield(
27+
n,
28+
n_p,
29+
year,
30+
h2_retrofit=False,
31+
h2_retrofit_capacity_per_ch4=None,
32+
capacity_threshold=None,
33+
):
34+
"""
35+
Add brownfield capacity from previous network.
36+
37+
Parameters
38+
----------
39+
n : pypsa.Network
40+
Network to add brownfield to
41+
n_p : pypsa.Network
42+
Previous network to get brownfield from
43+
year : int
44+
Planning year
45+
h2_retrofit : bool
46+
Whether to allow hydrogen pipeline retrofitting
47+
h2_retrofit_capacity_per_ch4 : float
48+
Ratio of hydrogen to methane capacity for pipeline retrofitting
49+
capacity_threshold : float
50+
Threshold for removing assets with low capacity
51+
"""
2752
logger.info(f"Preparing brownfield for the year {year}")
2853

2954
# electric transmission grid set optimised capacities of previous as minimum
@@ -49,11 +74,9 @@ def add_brownfield(n, n_p, year):
4974
& c.df.index.str.contains("heat")
5075
]
5176

52-
threshold = snakemake.params.threshold_capacity
53-
5477
if not chp_heat.empty:
5578
threshold_chp_heat = (
56-
threshold
79+
capacity_threshold
5780
* c.df.efficiency[chp_heat.str.replace("heat", "electric")].values
5881
* c.df.p_nom_ratio[chp_heat.str.replace("heat", "electric")].values
5982
/ c.df.efficiency[chp_heat].values
@@ -67,7 +90,7 @@ def add_brownfield(n, n_p, year):
6790
c.name,
6891
c.df.index[
6992
(c.df[f"{attr}_nom_extendable"] & ~c.df.index.isin(chp_heat))
70-
& (c.df[f"{attr}_nom_opt"] < threshold)
93+
& (c.df[f"{attr}_nom_opt"] < capacity_threshold)
7194
],
7295
)
7396

@@ -85,7 +108,7 @@ def add_brownfield(n, n_p, year):
85108
n.import_series_from_dataframe(c.pnl[tattr], c.name, tattr)
86109

87110
# deal with gas network
88-
if snakemake.params.H2_retrofit:
111+
if h2_retrofit:
89112
# subtract the already retrofitted from the maximum capacity
90113
h2_retrofitted_fixed_i = n.links[
91114
(n.links.carrier == "H2 pipeline retrofitted")
@@ -118,7 +141,7 @@ def add_brownfield(n, n_p, year):
118141
pipe_capacity = n.links.loc[gas_pipes_i, "p_nom"]
119142
fr = "H2 pipeline retrofitted"
120143
to = "gas pipeline"
121-
CH4_per_H2 = 1 / snakemake.params.H2_retrofit_capacity_per_CH4
144+
CH4_per_H2 = 1 / h2_retrofit_capacity_per_ch4
122145
already_retrofitted.index = already_retrofitted.index.str.replace(fr, to)
123146
remaining_capacity = (
124147
pipe_capacity
@@ -301,7 +324,14 @@ def update_heat_pump_efficiency(n: pypsa.Network, n_p: pypsa.Network, year: int)
301324

302325
update_heat_pump_efficiency(n, n_p, year)
303326

304-
add_brownfield(n, n_p, year)
327+
add_brownfield(
328+
n,
329+
n_p,
330+
year,
331+
h2_retrofit=snakemake.params.H2_retrofit,
332+
h2_retrofit_capacity_per_ch4=snakemake.params.H2_retrofit_capacity_per_CH4,
333+
capacity_threshold=snakemake.params.threshold_capacity,
334+
)
305335

306336
disable_grid_expansion_if_limit_hit(n)
307337

0 commit comments

Comments
 (0)