Code Duplication    Length = 71-73 lines in 2 locations

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

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