Code Duplication    Length = 72-74 lines in 2 locations

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

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