Code Duplication    Length = 72-74 lines in 2 locations

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

@@ 1438-1511 (lines=74) @@
1435
        log.info(f"Peak load for {scenario} exported to DB!")
1436
1437
1438
def get_cts_heat_peak_load():
1439
    """
1440
    Get heat peak load of all CTS buildings for both scenarios and store in DB.
1441
    """
1442
    log.info("Start logging!")
1443
1444
    BuildingHeatPeakLoads.__table__.create(bind=engine, checkfirst=True)
1445
1446
    # Delete rows with cts demand
1447
    with db.session_scope() as session:
1448
        session.query(BuildingHeatPeakLoads).filter(
1449
            BuildingHeatPeakLoads.sector == "cts"
1450
        ).delete()
1451
    log.info("Cts heat peak load removed from DB!")
1452
1453
    for scenario in ["eGon2035", "eGon100RE"]:
1454
1455
        with db.session_scope() as session:
1456
            cells_query = session.query(
1457
                EgonCtsElectricityDemandBuildingShare
1458
            ).filter(
1459
                EgonCtsElectricityDemandBuildingShare.scenario == scenario
1460
            )
1461
1462
        df_demand_share = pd.read_sql(
1463
            cells_query.statement, cells_query.session.bind, index_col=None
1464
        )
1465
        log.info(f"Retrieved demand share for scenario: {scenario}")
1466
1467
        with db.session_scope() as session:
1468
            cells_query = session.query(EgonEtragoHeatCts).filter(
1469
                EgonEtragoHeatCts.scn_name == scenario
1470
            )
1471
1472
        df_cts_profiles = pd.read_sql(
1473
            cells_query.statement,
1474
            cells_query.session.bind,
1475
        )
1476
        log.info(f"Retrieved substation profiles for scenario: {scenario}")
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 later
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
            BuildingHeatPeakLoads,
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
@@ 1364-1435 (lines=72) @@
1361
    log.info("Profile share exported to DB!")
1362
1363
1364
def get_cts_electricity_peak_load():
1365
    """
1366
    Get electricity peak load of all CTS buildings for both scenarios and
1367
    store in DB.
1368
    """
1369
    log.info("Start logging!")
1370
1371
    BuildingElectricityPeakLoads.__table__.create(bind=engine, checkfirst=True)
1372
1373
    # Delete rows with cts demand
1374
    with db.session_scope() as session:
1375
        session.query(BuildingElectricityPeakLoads).filter(
1376
            BuildingElectricityPeakLoads.sector == "cts"
1377
        ).delete()
1378
    log.info("Cts electricity peak load removed from DB!")
1379
1380
    for scenario in ["eGon2035", "eGon100RE"]:
1381
1382
        with db.session_scope() as session:
1383
            cells_query = session.query(
1384
                EgonCtsElectricityDemandBuildingShare
1385
            ).filter(
1386
                EgonCtsElectricityDemandBuildingShare.scenario == scenario
1387
            )
1388
1389
        df_demand_share = pd.read_sql(
1390
            cells_query.statement, cells_query.session.bind, index_col=None
1391
        )
1392
1393
        with db.session_scope() as session:
1394
            cells_query = session.query(EgonEtragoElectricityCts).filter(
1395
                EgonEtragoElectricityCts.scn_name == scenario
1396
            )
1397
1398
        df_cts_profiles = pd.read_sql(
1399
            cells_query.statement,
1400
            cells_query.session.bind,
1401
        )
1402
        df_cts_profiles = pd.DataFrame.from_dict(
1403
            df_cts_profiles.set_index("bus_id")["p_set"].to_dict(),
1404
            orient="columns",
1405
        )
1406
1407
        df_peak_load = pd.merge(
1408
            left=df_cts_profiles.max().astype(float).rename("max"),
1409
            right=df_demand_share,
1410
            left_index=True,
1411
            right_on="bus_id",
1412
        )
1413
1414
        # Convert unit from MWh to W
1415
        df_peak_load["max"] = df_peak_load["max"] * 1e6
1416
        df_peak_load["peak_load_in_w"] = (
1417
            df_peak_load["max"] * df_peak_load["profile_share"]
1418
        )
1419
        log.info(f"Peak load for {scenario} determined!")
1420
1421
        # TODO remove later
1422
        df_peak_load.rename(columns={"id": "building_id"}, inplace=True)
1423
        df_peak_load["sector"] = "cts"
1424
1425
        # # Write peak loads into db
1426
        write_table_to_postgres(
1427
            df_peak_load,
1428
            BuildingElectricityPeakLoads,
1429
            engine=engine,
1430
            drop=False,
1431
            index=False,
1432
            if_exists="append",
1433
        )
1434
1435
        log.info(f"Peak load for {scenario} exported to DB!")
1436
1437
1438
def get_cts_heat_peak_load():