Code Duplication    Length = 72-74 lines in 2 locations

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

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