Code Duplication    Length = 70-72 lines in 2 locations

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

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