Code Duplication    Length = 50-55 lines in 2 locations

src/egon/data/datasets/gas_neighbours/gas_abroad.py 1 location

@@ 226-280 (lines=55) @@
223
    )
224
225
226
def get_foreign_gas_bus_id(scn_name="eGon2035", carrier="CH4"):
227
    """Calculate the etrago bus id based on the geometry for eGon2035
228
229
    Map node_ids from TYNDP and etragos bus_id
230
231
    Parameters
232
    ----------
233
    scn_name : str
234
        Name of the scenario
235
    carrier : str
236
        Name of the carrier
237
238
    Returns
239
    -------
240
    pandas.Series
241
        List of mapped node_ids from TYNDP and etragos bus_id
242
243
    """
244
    sources = config.datasets()["gas_neighbours"]["sources"]
245
246
    bus_id = db.select_geodataframe(
247
        f"""
248
        SELECT bus_id, ST_Buffer(geom, 1) as geom, country
249
        FROM grid.egon_etrago_bus
250
        WHERE scn_name = '{scn_name}'
251
        AND carrier = '{carrier}'
252
        AND country != 'DE'
253
        """,
254
        epsg=3035,
255
    )
256
257
    # insert installed capacities
258
    file = zipfile.ZipFile(f"tyndp/{sources['tyndp_capacities']}")
259
260
    # Select buses in neighbouring countries as geodataframe
261
    buses = pd.read_excel(
262
        file.open("TYNDP-2020-Scenario-Datafile.xlsx").read(),
263
        sheet_name="Nodes - Dict",
264
    ).query("longitude==longitude")
265
    buses = gpd.GeoDataFrame(
266
        buses,
267
        crs=4326,
268
        geometry=gpd.points_from_xy(buses.longitude, buses.latitude),
269
    ).to_crs(3035)
270
271
    buses["bus_id"] = 0
272
273
    # Select bus_id from etrago with shortest distance to TYNDP node
274
    for i, row in buses.iterrows():
275
        distance = bus_id.set_index("bus_id").geom.distance(row.geometry)
276
        buses.loc[i, "bus_id"] = distance[
277
            distance == distance.min()
278
        ].index.values[0]
279
280
    return buses.set_index("node_id").bus_id
281

src/egon/data/datasets/electrical_neighbours.py 1 location

@@ 741-790 (lines=50) @@
738
    }
739
740
741
def get_foreign_bus_id():
742
    """Calculte the etrago bus id from Nodes of TYNDP based on the geometry
743
744
    Returns
745
    -------
746
    pandas.Series
747
        List of mapped node_ids from TYNDP and etragos bus_id
748
749
    """
750
751
    sources = config.datasets()["electrical_neighbours"]["sources"]
752
753
    bus_id = db.select_geodataframe(
754
        """SELECT bus_id, ST_Buffer(geom, 1) as geom, country
755
        FROM grid.egon_etrago_bus
756
        WHERE scn_name = 'eGon2035'
757
        AND carrier = 'AC'
758
        AND v_nom = 380.
759
        AND country != 'DE'
760
        AND bus_id NOT IN (
761
            SELECT bus_i
762
            FROM osmtgmod_results.bus_data)
763
        """,
764
        epsg=3035,
765
    )
766
767
    # insert installed capacities
768
    file = zipfile.ZipFile(f"tyndp/{sources['tyndp_capacities']}")
769
770
    # Select buses in neighbouring countries as geodataframe
771
    buses = pd.read_excel(
772
        file.open("TYNDP-2020-Scenario-Datafile.xlsx").read(),
773
        sheet_name="Nodes - Dict",
774
    ).query("longitude==longitude")
775
    buses = gpd.GeoDataFrame(
776
        buses,
777
        crs=4326,
778
        geometry=gpd.points_from_xy(buses.longitude, buses.latitude),
779
    ).to_crs(3035)
780
781
    buses["bus_id"] = 0
782
783
    # Select bus_id from etrago with shortest distance to TYNDP node
784
    for i, row in buses.iterrows():
785
        distance = bus_id.set_index("bus_id").geom.distance(row.geometry)
786
        buses.loc[i, "bus_id"] = distance[
787
            distance == distance.min()
788
        ].index.values[0]
789
790
    return buses.set_index("node_id").bus_id
791
792
793
def calc_capacities():