Skip to content

Commit 3c3d7ee

Browse files
committed
refactor: sort functions in clean_projects
1 parent 32ff08a commit 3c3d7ee

1 file changed

Lines changed: 59 additions & 59 deletions

File tree

scripts/cba/clean_projects.py

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -59,43 +59,32 @@
5959
}
6060

6161

62-
def extract_investment_attributes(excel_path: Path) -> pd.DataFrame:
62+
def read_tyndp_electricity_buses(buses_fn: str):
6363
"""
64-
Extract length, CAPEX, and underwater fraction from Trans.Investments sheet.
64+
Read node list for electricity from tyndp data input.
6565
66-
Aggregates investment-level data to the project level by summing route
67-
lengths and CAPEX, and computing the underwater fraction from offshore
68-
cable lengths.
69-
"""
70-
inv = pd.read_excel(
71-
excel_path,
72-
sheet_name="Trans.Investments",
73-
skiprows=1,
74-
usecols=[
75-
"This investment belongs to project number…",
76-
"Total route length (km)",
77-
"Estimated CAPEX (MEUR)",
78-
"Type of Element",
79-
],
80-
).rename(
81-
columns={
82-
"This investment belongs to project number…": "project_id",
83-
"Total route length (km)": "length_km",
84-
"Estimated CAPEX (MEUR)": "capex_meur",
85-
"Type of Element": "element_type",
86-
}
87-
)
66+
Parameters
67+
----------
68+
- buses_fn (str): Path to "LIST OF NODES.xlsx" from tyndp bundle
8869
89-
is_offshore = inv["element_type"].isin(OFFSHORE_ELEMENT_TYPES)
70+
Returns
71+
-------
72+
- buses: Index of electricity buses as used in Open-TYNDP
9073
91-
agg = inv.groupby("project_id").agg(
92-
length_km=("length_km", "sum"),
93-
capex_meur=("capex_meur", "sum"),
74+
See Also
75+
--------
76+
build_tyndp_network.py : build_buses
77+
"""
78+
buses = pd.Index(
79+
pd.read_excel(buses_fn)
80+
.replace("UK", "GB", regex=True)
81+
.rename({"NODE": "bus_id"}, axis=1)["bus_id"]
9482
)
95-
offshore_km = inv.loc[is_offshore].groupby("project_id")["length_km"].sum()
96-
agg["underwater_fraction"] = (offshore_km / agg["length_km"]).fillna(0).round(3)
9783

98-
return agg
84+
# Manually add Italian virtual nodes
85+
buses = buses.union(["ITCO", "ITVI"])
86+
87+
return buses
9988

10089

10190
def extract_transmission_projects(
@@ -183,6 +172,45 @@ def extract_transmission_projects(
183172
return projects
184173

185174

175+
def extract_investment_attributes(excel_path: Path) -> pd.DataFrame:
176+
"""
177+
Extract length, CAPEX, and underwater fraction from Trans.Investments sheet.
178+
179+
Aggregates investment-level data to the project level by summing route
180+
lengths and CAPEX, and computing the underwater fraction from offshore
181+
cable lengths.
182+
"""
183+
inv = pd.read_excel(
184+
excel_path,
185+
sheet_name="Trans.Investments",
186+
skiprows=1,
187+
usecols=[
188+
"This investment belongs to project number…",
189+
"Total route length (km)",
190+
"Estimated CAPEX (MEUR)",
191+
"Type of Element",
192+
],
193+
).rename(
194+
columns={
195+
"This investment belongs to project number…": "project_id",
196+
"Total route length (km)": "length_km",
197+
"Estimated CAPEX (MEUR)": "capex_meur",
198+
"Type of Element": "element_type",
199+
}
200+
)
201+
202+
is_offshore = inv["element_type"].isin(OFFSHORE_ELEMENT_TYPES)
203+
204+
agg = inv.groupby("project_id").agg(
205+
length_km=("length_km", "sum"),
206+
capex_meur=("capex_meur", "sum"),
207+
)
208+
offshore_km = inv.loc[is_offshore].groupby("project_id")["length_km"].sum()
209+
agg["underwater_fraction"] = (offshore_km / agg["length_km"]).fillna(0).round(3)
210+
211+
return agg
212+
213+
186214
def extract_storage_projects(
187215
excel_path: Path, existing_buses: pd.Index
188216
) -> pd.DataFrame:
@@ -262,34 +290,6 @@ def build_method_assignments(
262290
return projects.merge(assigned, on="project_id", how="left")
263291

264292

265-
def read_tyndp_electricity_buses(buses_fn: str):
266-
"""
267-
Read node list for electricity from tyndp data input.
268-
269-
Parameters
270-
----------
271-
- buses_fn (str): Path to "LIST OF NODES.xlsx" from tyndp bundle
272-
273-
Returns
274-
-------
275-
- buses: Index of electricity buses as used in Open-TYNDP
276-
277-
See Also
278-
--------
279-
build_tyndp_network.py : build_buses
280-
"""
281-
buses = pd.Index(
282-
pd.read_excel(buses_fn)
283-
.replace("UK", "GB", regex=True)
284-
.rename({"NODE": "bus_id"}, axis=1)["bus_id"]
285-
)
286-
287-
# Manually add Italian virtual nodes
288-
buses = buses.union(["ITCO", "ITVI"])
289-
290-
return buses
291-
292-
293293
if __name__ == "__main__":
294294
if "snakemake" not in globals():
295295
from scripts._helpers import mock_snakemake

0 commit comments

Comments
 (0)