Code Duplication    Length = 70-72 lines in 2 locations

src/egon/data/datasets/electricity_demand_timeseries/cts_buildings.py 2 locations

@@ 1539-1610 (lines=72) @@
1536
        log.info(f"Peak load for {scenario} exported to DB!")
1537
1538
1539
def get_cts_heat_peak_load():
1540
    """
1541
    Get heat peak load of all CTS buildings for both scenarios and store in DB.
1542
    """
1543
    log.info("Start logging!")
1544
1545
    BuildingHeatPeakLoads.__table__.create(bind=engine, checkfirst=True)
1546
1547
    # Delete rows with cts demand
1548
    with db.session_scope() as session:
1549
        session.query(BuildingHeatPeakLoads).filter(
1550
            BuildingHeatPeakLoads.sector == "cts"
1551
        ).delete()
1552
    log.info("Cts heat peak load removed from DB!")
1553
1554
    for scenario in config.settings()["egon-data"]["--scenarios"]:
1555
        with db.session_scope() as session:
1556
            cells_query = session.query(
1557
                EgonCtsElectricityDemandBuildingShare
1558
            ).filter(
1559
                EgonCtsElectricityDemandBuildingShare.scenario == scenario
1560
            )
1561
1562
        df_demand_share = pd.read_sql(
1563
            cells_query.statement, cells_query.session.bind, index_col=None
1564
        )
1565
        log.info(f"Retrieved demand share for scenario: {scenario}")
1566
1567
        with db.session_scope() as session:
1568
            cells_query = session.query(EgonEtragoHeatCts).filter(
1569
                EgonEtragoHeatCts.scn_name == scenario
1570
            )
1571
1572
        df_cts_profiles = pd.read_sql(
1573
            cells_query.statement,
1574
            cells_query.session.bind,
1575
        )
1576
        log.info(f"Retrieved substation profiles for scenario: {scenario}")
1577
1578
        df_cts_profiles = pd.DataFrame.from_dict(
1579
            df_cts_profiles.set_index("bus_id")["p_set"].to_dict(),
1580
            orient="columns",
1581
        )
1582
1583
        df_peak_load = pd.merge(
1584
            left=df_cts_profiles.max().astype(float).rename("max"),
1585
            right=df_demand_share,
1586
            left_index=True,
1587
            right_on="bus_id",
1588
        )
1589
1590
        # Convert unit from MWh to W
1591
        df_peak_load["max"] = df_peak_load["max"] * 1e6
1592
        df_peak_load["peak_load_in_w"] = (
1593
            df_peak_load["max"] * df_peak_load["profile_share"]
1594
        )
1595
        log.info(f"Peak load for {scenario} determined!")
1596
1597
        # TODO remove after #772
1598
        df_peak_load.rename(columns={"id": "building_id"}, inplace=True)
1599
        df_peak_load["sector"] = "cts"
1600
1601
        # # Write peak loads into db
1602
        write_table_to_postgres(
1603
            df_peak_load,
1604
            BuildingHeatPeakLoads,
1605
            drop=False,
1606
            index=False,
1607
            if_exists="append",
1608
        )
1609
1610
        log.info(f"Peak load for {scenario} exported to DB!")
1611
1612
1613
def assign_voltage_level_to_buildings():
@@ 1467-1536 (lines=70) @@
1464
    log.info("Profile share exported to DB!")
1465
1466
1467
def get_cts_electricity_peak_load():
1468
    """
1469
    Get electricity peak load of all CTS buildings for both scenarios and
1470
    store in DB.
1471
    """
1472
    log.info("Start logging!")
1473
1474
    BuildingElectricityPeakLoads.__table__.create(bind=engine, checkfirst=True)
1475
1476
    # Delete rows with cts demand
1477
    with db.session_scope() as session:
1478
        session.query(BuildingElectricityPeakLoads).filter(
1479
            BuildingElectricityPeakLoads.sector == "cts"
1480
        ).delete()
1481
    log.info("Cts electricity peak load removed from DB!")
1482
1483
    for scenario in config.settings()["egon-data"]["--scenarios"]:
1484
        with db.session_scope() as session:
1485
            cells_query = session.query(
1486
                EgonCtsElectricityDemandBuildingShare
1487
            ).filter(
1488
                EgonCtsElectricityDemandBuildingShare.scenario == scenario
1489
            )
1490
1491
        df_demand_share = pd.read_sql(
1492
            cells_query.statement, cells_query.session.bind, index_col=None
1493
        )
1494
1495
        with db.session_scope() as session:
1496
            cells_query = session.query(EgonEtragoElectricityCts).filter(
1497
                EgonEtragoElectricityCts.scn_name == scenario
1498
            )
1499
1500
        df_cts_profiles = pd.read_sql(
1501
            cells_query.statement,
1502
            cells_query.session.bind,
1503
        )
1504
        df_cts_profiles = pd.DataFrame.from_dict(
1505
            df_cts_profiles.set_index("bus_id")["p_set"].to_dict(),
1506
            orient="columns",
1507
        )
1508
1509
        df_peak_load = pd.merge(
1510
            left=df_cts_profiles.max().astype(float).rename("max"),
1511
            right=df_demand_share,
1512
            left_index=True,
1513
            right_on="bus_id",
1514
        )
1515
1516
        # Convert unit from MWh to W
1517
        df_peak_load["max"] = df_peak_load["max"] * 1e6
1518
        df_peak_load["peak_load_in_w"] = (
1519
            df_peak_load["max"] * df_peak_load["profile_share"]
1520
        )
1521
        log.info(f"Peak load for {scenario} determined!")
1522
1523
        # TODO remove after #772
1524
        df_peak_load.rename(columns={"id": "building_id"}, inplace=True)
1525
        df_peak_load["sector"] = "cts"
1526
1527
        # # Write peak loads into db
1528
        write_table_to_postgres(
1529
            df_peak_load,
1530
            BuildingElectricityPeakLoads,
1531
            drop=False,
1532
            index=False,
1533
            if_exists="append",
1534
        )
1535
1536
        log.info(f"Peak load for {scenario} exported to DB!")
1537
1538
1539
def get_cts_heat_peak_load():