Code Duplication    Length = 72-74 lines in 2 locations

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

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