Code Duplication    Length = 72-74 lines in 2 locations

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

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