Code Duplication    Length = 63-66 lines in 2 locations

src/egon/data/datasets/power_plants/assign_weather_data.py 1 location

@@ 7-69 (lines=63) @@
4
from egon.data.datasets.power_plants.pv_rooftop_buildings import timer_func
5
import egon.data.config
6
7
def assign_bus_id(power_plants, cfg):
8
    """Assigns bus_ids to power plants according to location and voltage level
9
10
    Parameters
11
    ----------
12
    power_plants : pandas.DataFrame
13
        Power plants including voltage level
14
15
    Returns
16
    -------
17
    power_plants : pandas.DataFrame
18
        Power plants including voltage level and bus_id
19
20
    """
21
22
    mv_grid_districts = db.select_geodataframe(
23
        f"""
24
        SELECT * FROM {cfg['sources']['egon_mv_grid_district']}
25
        """,
26
        epsg=4326,
27
    )
28
29
    ehv_grid_districts = db.select_geodataframe(
30
        f"""
31
        SELECT * FROM {cfg['sources']['ehv_voronoi']}
32
        """,
33
        epsg=4326,
34
    )
35
36
    # Assign power plants in hv and below to hvmv bus
37
    power_plants_hv = power_plants[power_plants.voltage_level >= 3].index
38
    if len(power_plants_hv) > 0:
39
        power_plants.loc[power_plants_hv, "bus_id"] = gpd.sjoin(
40
            power_plants[power_plants.index.isin(power_plants_hv)],
41
            mv_grid_districts,
42
        ).bus_id
43
44
    # Assign power plants in ehv to ehv bus
45
    power_plants_ehv = power_plants[power_plants.voltage_level < 3].index
46
47
    if len(power_plants_ehv) > 0:
48
        ehv_join = gpd.sjoin(
49
            power_plants[power_plants.index.isin(power_plants_ehv)],
50
            ehv_grid_districts,
51
        )
52
53
        if "bus_id_right" in ehv_join.columns:
54
            power_plants.loc[power_plants_ehv, "bus_id"] = gpd.sjoin(
55
                power_plants[power_plants.index.isin(power_plants_ehv)],
56
                ehv_grid_districts,
57
            ).bus_id_right
58
59
        else:
60
            power_plants.loc[power_plants_ehv, "bus_id"] = gpd.sjoin(
61
                power_plants[power_plants.index.isin(power_plants_ehv)],
62
                ehv_grid_districts,
63
            ).bus_id
64
65
    # Assert that all power plants have a bus_id
66
    assert power_plants.bus_id.notnull().all(), f"""Some power plants are
67
    not attached to a bus: {power_plants[power_plants.bus_id.isnull()]}"""
68
69
    return power_plants
70
71
72
@timer_func

src/egon/data/datasets/power_plants/__init__.py 1 location

@@ 521-586 (lines=66) @@
518
    return mastr_loc.voltage_level
519
520
521
def assign_bus_id(power_plants, cfg, drop_missing=False):
522
    """Assigns bus_ids to power plants according to location and voltage level
523
524
    Parameters
525
    ----------
526
    power_plants : pandas.DataFrame
527
        Power plants including voltage level
528
529
    Returns
530
    -------
531
    power_plants : pandas.DataFrame
532
        Power plants including voltage level and bus_id
533
534
    """
535
536
    mv_grid_districts = db.select_geodataframe(
537
        f"""
538
        SELECT * FROM {cfg['sources']['egon_mv_grid_district']}
539
        """,
540
        epsg=4326,
541
    )
542
543
    ehv_grid_districts = db.select_geodataframe(
544
        f"""
545
        SELECT * FROM {cfg['sources']['ehv_voronoi']}
546
        """,
547
        epsg=4326,
548
    )
549
550
    # Assign power plants in hv and below to hvmv bus
551
    power_plants_hv = power_plants[power_plants.voltage_level >= 3].index
552
    if len(power_plants_hv) > 0:
553
        power_plants.loc[power_plants_hv, "bus_id"] = gpd.sjoin(
554
            power_plants[power_plants.index.isin(power_plants_hv)],
555
            mv_grid_districts,
556
        ).bus_id
557
558
    # Assign power plants in ehv to ehv bus
559
    power_plants_ehv = power_plants[power_plants.voltage_level < 3].index
560
561
    if len(power_plants_ehv) > 0:
562
        ehv_join = gpd.sjoin(
563
            power_plants[power_plants.index.isin(power_plants_ehv)],
564
            ehv_grid_districts,
565
        )
566
567
        if "bus_id_right" in ehv_join.columns:
568
            power_plants.loc[power_plants_ehv, "bus_id"] = gpd.sjoin(
569
                power_plants[power_plants.index.isin(power_plants_ehv)],
570
                ehv_grid_districts,
571
            ).bus_id_right
572
573
        else:
574
            power_plants.loc[power_plants_ehv, "bus_id"] = gpd.sjoin(
575
                power_plants[power_plants.index.isin(power_plants_ehv)],
576
                ehv_grid_districts,
577
            ).bus_id
578
579
    if drop_missing:
580
        power_plants = power_plants[~power_plants.bus_id.isnull()]
581
582
    # Assert that all power plants have a bus_id
583
    assert power_plants.bus_id.notnull().all(), f"""Some power plants are
584
    not attached to a bus: {power_plants[power_plants.bus_id.isnull()]}"""
585
586
    return power_plants
587
588
589
def insert_hydro_biomass():