Code Duplication    Length = 71-73 lines in 2 locations

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

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