Skip to content

Commit fb69f8f

Browse files
committed
feat: generalise adding virtual nodes in build_tyndp_network.py
1 parent 64875c5 commit fb69f8f

2 files changed

Lines changed: 59 additions & 25 deletions

File tree

scripts/base_network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
get_snapshots,
4343
set_scenario_config,
4444
)
45-
from scripts.build_tyndp_network import AC_VIRTUAL_NODES
45+
from scripts.build_tyndp_network import AC_VIRTUAL_NODES_IT
4646

4747
PD_GE_2_2 = parse(pd.__version__) >= Version("2.2")
4848

@@ -576,7 +576,7 @@ def _restore_virtual_node_countries_tyndp(
576576
Buses as loaded in `_load_buses`, holding the countries declared in
577577
build_tyndp_network before the coordinate-based reassignment.
578578
"""
579-
nodes = n.buses.index.intersection(AC_VIRTUAL_NODES)
579+
nodes = n.buses.index.intersection(AC_VIRTUAL_NODES_IT)
580580
n.buses.loc[nodes, "country"] = buses.loc[nodes, "country"]
581581

582582

scripts/build_tyndp_network.py

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@
7878
"UK": "GB",
7979
}
8080

81-
AC_VIRTUAL_NODES = ["ITCO", "ITVI"]
81+
AC_VIRTUAL_NODES_IT = {
82+
"ITCO": "FR15",
83+
"ITVI": "ITSI",
84+
}
8285

8386
IBFI_COORD = (63.0, 25.0)
8487

@@ -225,6 +228,41 @@ def build_shapes(
225228
return bidding_shapes, country_shapes
226229

227230

231+
def _add_virtual_node(
232+
target_gdf: gpd.GeoDataFrame,
233+
new_bus: str,
234+
ref_bus: str,
235+
source_gdf: gpd.GeoDataFrame | None = None,
236+
**overrides,
237+
) -> None:
238+
"""
239+
Add a virtual node to target Dataframe as a copy of an existing reference bus.
240+
241+
The virtual node inherits every attribute of the reference bus and takes its
242+
own name as ``station_id`` and ``tags``. Any remaining attribute is set
243+
through ``overrides``.
244+
245+
Parameters
246+
----------
247+
target_gdf : gpd.GeoDataFrame
248+
Bus GeoDataFrame the virtual node is appended to, modified in place.
249+
new_bus : str
250+
Name of the virtual node.
251+
ref_bus : str
252+
Name of the reference bus whose attributes are copied.
253+
source_gdf : gpd.GeoDataFrame, optional
254+
Bus GeoDataFrame holding the reference bus. Defaults to ``target``.
255+
**overrides, optional
256+
Attribute values overriding those inherited from the reference bus.
257+
"""
258+
source_gdf = target_gdf if source_gdf is None else source_gdf
259+
target_gdf.loc[new_bus] = (
260+
source_gdf.loc[[ref_bus]]
261+
.assign(station_id=new_bus, tags=new_bus, **overrides)
262+
.loc[ref_bus]
263+
)
264+
265+
228266
def build_buses(
229267
buses_fn: str,
230268
countries: list[str],
@@ -283,14 +321,10 @@ def build_buses(
283321

284322
# Manually add Italian virtual nodes # TODO Refine assumptions
285323
if "IT" in countries:
286-
buses.loc["ITCO"] = (
287-
buses.loc[["FR15"]]
288-
.assign(station_id="ITCO", country="IT", tags="ITCO")
289-
.loc["FR15"]
290-
)
291-
buses.loc["ITVI"] = (
292-
buses.loc[["ITSI"]].assign(station_id="ITVI", tags="ITVI").loc["ITSI"]
293-
)
324+
for node, location in AC_VIRTUAL_NODES_IT.items():
325+
_add_virtual_node(
326+
target_gdf=buses, new_bus=node, ref_bus=location, country="IT"
327+
)
294328

295329
buses_h2 = (
296330
country_shapes[["node", "x", "y"]]
@@ -311,23 +345,23 @@ def build_buses(
311345

312346
# Manually add IBIT and IBFI nodes # TODO Refine assumptions
313347
if "IT" in countries:
314-
buses_h2.loc["IBIT H2"] = (
315-
buses.loc[["ITN1"]]
316-
.assign(station_id="IBIT H2", voltage=None, dc="f", tags="IBIT H2")
317-
.loc["ITN1"]
348+
_add_virtual_node(
349+
target_gdf=buses_h2,
350+
new_bus="IBIT H2",
351+
ref_bus="ITN1",
352+
source_gdf=buses,
353+
voltage=None,
354+
dc="f",
318355
)
319356
if "FI" in countries:
320357
ibfi_lat, ibfi_long = IBFI_COORD
321-
buses_h2.loc["IBFI H2"] = (
322-
buses_h2.loc[["FI H2"]]
323-
.assign(
324-
station_id="IBFI H2",
325-
tags="IBFI H2",
326-
x=ibfi_long,
327-
y=ibfi_lat,
328-
geometry=Point(ibfi_long, ibfi_lat),
329-
)
330-
.loc["FI H2"]
358+
_add_virtual_node(
359+
target_gdf=buses_h2,
360+
new_bus="IBFI H2",
361+
ref_bus="FI H2",
362+
x=ibfi_long,
363+
y=ibfi_lat,
364+
geometry=Point(ibfi_long, ibfi_lat),
331365
)
332366

333367
return buses, buses_h2

0 commit comments

Comments
 (0)