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

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