Code Duplication    Length = 72-74 lines in 2 locations

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

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