Skip to content

Commit 14bd30e

Browse files
committed
Create and adjust interim tables for time series
1 parent 9829a02 commit 14bd30e

File tree

4 files changed

+100
-15
lines changed

4 files changed

+100
-15
lines changed

src/egon/data/datasets.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,9 @@ electrical_load_curves_industry:
738738
sites_load:
739739
schema: 'demand'
740740
table: 'egon_sites_ind_load_curves'
741+
sites_load_individual:
742+
schema: 'demand'
743+
table: 'egon_sites_ind_load_curves_individual'
741744

742745
etrago_electricity:
743746
sources:

src/egon/data/datasets/DSM_cts_ind.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def calc_ind_site_timeseries(scenario):
212212
data=curves_bus["bus_id"], index=curves_bus["id"].astype(int)
213213
)
214214
ts["scenario_name"] = scenario
215-
curves_bus.drop({"id", "bus_id"}, axis=1, inplace=True)
215+
curves_bus.drop({"id", "bus_id", "geom"}, axis=1, inplace=True)
216216
ts["p_set"] = curves_bus.values.tolist()
217217

218218
# add subsector to relate to Schmidt's tables afterwards

src/egon/data/datasets/industry/__init__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class DemandCurvesOsmIndustryIndividual(Base):
6060
bus_id = Column(Integer)
6161
scn_name = Column(String, primary_key= True)
6262
p_set = Column(ARRAY(Float))
63+
peak_load = Column(Float)
64+
demand = Column(Float)
65+
voltage_level = Column(Integer)
6366

6467

6568

@@ -78,11 +81,12 @@ class DemandCurvesSitesIndustryIndividual(Base):
7881
__table_args__ = {"schema": "demand"}
7982

8083
site_id = Column(Integer, primary_key = True)
81-
bus = Column(Integer)
84+
bus_id = Column(Integer)
8285
scn_name = Column(String, primary_key=True)
83-
wz = Column(Integer)
8486
p_set = Column(ARRAY(Float))
85-
geom= Column(Geometry("POINT", 4326))
87+
peak_load = Column(Float)
88+
demand = Column(Float)
89+
voltage_level = Column(Integer)
8690

8791

8892
def create_tables():
@@ -135,6 +139,12 @@ def create_tables():
135139
{targets_temporal['sites_load']['table']} CASCADE;"""
136140
)
137141

142+
db.execute_sql(
143+
f"""DROP TABLE IF EXISTS
144+
{targets_temporal['sites_load_individual']['schema']}.
145+
{targets_temporal['sites_load_individual']['table']} CASCADE;"""
146+
)
147+
138148
engine = db.engine()
139149

140150
EgonDemandRegioSitesIndElectricity.__table__.create(
@@ -153,6 +163,10 @@ def create_tables():
153163

154164
DemandCurvesSitesIndustry.__table__.create(bind=engine, checkfirst=True)
155165

166+
DemandCurvesSitesIndustryIndividual.__table__.create(
167+
bind=engine, checkfirst=True
168+
)
169+
156170

157171
def industrial_demand_distr():
158172
""" Distribute electrical demands for industry to osm landuse polygons
@@ -386,7 +400,7 @@ class IndustrialDemandCurves(Dataset):
386400
def __init__(self, dependencies):
387401
super().__init__(
388402
name="Industrial_demand_curves",
389-
version="0.0.4",
403+
version="0.0.5",
390404
dependencies=dependencies,
391405
tasks=(
392406
create_tables,

src/egon/data/datasets/industry/temporal.py

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import egon.data.config
77
import geopandas as gpd
88
import pandas as pd
9+
import numpy as np
910
from egon.data import db
1011
from egon.data.datasets.electricity_demand.temporal import calc_load_curve
1112
from sqlalchemy import ARRAY, Column, Float, Integer, String
@@ -14,6 +15,38 @@
1415
Base = declarative_base()
1516

1617

18+
def identify_voltage_level(df):
19+
20+
"""Identify the voltage_level of a grid component based on its peak load and
21+
defined thresholds.
22+
23+
24+
Parameters
25+
----------
26+
df : pandas.DataFrame
27+
Data frame containing information about peak loads
28+
29+
30+
Returns
31+
-------
32+
pandas.DataFrame
33+
Data frame with an additional column with voltage level
34+
35+
"""
36+
37+
df['voltage_level']= np.nan
38+
39+
# Identify voltage_level for every demand area taking thresholds into account which were defined in the eGon project
40+
df.loc[df["peak_load"] < 0.1, "voltage_level"] = 7
41+
df.loc[df["peak_load"] > 0.1, "voltage_level"] = 6
42+
df.loc[df["peak_load"] > 0.2, "voltage_level"] = 5
43+
df.loc[df["peak_load"] > 5.5, "voltage_level"] = 4
44+
df.loc[df["peak_load"] > 20, "voltage_level"] = 3
45+
df.loc[df["peak_load"] > 120, "voltage_level"] = 1
46+
47+
return df
48+
49+
1750
def identify_bus(load_curves, demand_area):
1851
"""Identify the grid connection point for a consumer by determining its grid level
1952
based on the time series' peak load and the spatial intersection to mv
@@ -50,17 +83,11 @@ def identify_bus(load_curves, demand_area):
5083
)
5184

5285
# Initialize dataframe to identify peak load per demand area (e.g. osm landuse area or industrial site)
53-
peak = pd.DataFrame(columns=["id", "peak_load", "voltage_level"])
86+
peak = pd.DataFrame(columns=["id", "peak_load"])
5487
peak["id"] = load_curves.max(axis=0).index
5588
peak["peak_load"] = load_curves.max(axis=0).values
5689

57-
# Identify voltage_level for every demand area taking thresholds into account which were defined in the eGon project
58-
peak.loc[peak["peak_load"] < 0.1, "voltage_level"] = 7
59-
peak.loc[peak["peak_load"] > 0.1, "voltage_level"] = 6
60-
peak.loc[peak["peak_load"] > 0.2, "voltage_level"] = 5
61-
peak.loc[peak["peak_load"] > 5.5, "voltage_level"] = 4
62-
peak.loc[peak["peak_load"] > 20, "voltage_level"] = 3
63-
peak.loc[peak["peak_load"] > 120, "voltage_level"] = 1
90+
peak = identify_voltage_level(peak)
6491

6592
# Assign bus_id to demand area by merging landuse and peak df
6693
peak = pd.merge(demand_area, peak, right_on="id", left_index=True)
@@ -210,6 +237,14 @@ def insert_osm_ind_load():
210237
"""
211238
)
212239

240+
db.execute_sql(
241+
f"""
242+
DELETE FROM
243+
{targets['osm_load_individual']['schema']}.{targets['osm_load_individual']['table']}
244+
WHERE scn_name = '{scenario}'
245+
"""
246+
)
247+
213248
# Calculate cts load curves per mv substation (hvmv bus)
214249
data, curves_individual = calc_load_curves_ind_osm(scenario)
215250
data.index = data.index.rename("bus")
@@ -225,6 +260,9 @@ def insert_osm_ind_load():
225260
if_exists="append",
226261
)
227262

263+
curves_individual['peak_load'] = np.array(curves_individual['p_set'].values.tolist()).max(axis=1)
264+
curves_individual['demand'] = np.array(curves_individual['p_set'].values.tolist()).sum(axis=1)
265+
curves_individual = identify_voltage_level(curves_individual)
228266

229267
curves_individual.to_sql(
230268
targets["osm_load_individual"]["table"],
@@ -321,7 +359,16 @@ def calc_load_curves_ind_sites(scenario):
321359
# Insert data for pf load timeseries table
322360
load_ts_df.p_set = curves_bus.values.tolist()
323361

324-
return load_ts_df
362+
# Create Dataframe to store time series individually
363+
curves_individual_interim = (
364+
curves_da.drop(["bus_id", "geom", "wz"], axis=1).fillna(0)
365+
).set_index("id")
366+
curves_individual = curves_da[["id", "bus_id"]]
367+
curves_individual["p_set"] = curves_individual_interim.values.tolist()
368+
curves_individual["scn_name"]= scenario
369+
curves_individual = curves_individual.rename(columns={"id": "site_id"}).set_index(['site_id', 'scn_name'])
370+
371+
return load_ts_df, curves_individual
325372

326373

327374
def insert_sites_ind_load():
@@ -348,8 +395,18 @@ def insert_sites_ind_load():
348395
"""
349396
)
350397

398+
# Delete existing data from database
399+
db.execute_sql(
400+
f"""
401+
DELETE FROM
402+
{targets['sites_load_individual']['schema']}.
403+
{targets['sites_load_individual']['table']}
404+
WHERE scn_name = '{scenario}'
405+
"""
406+
)
407+
351408
# Calculate industrial load curves per bus
352-
data = calc_load_curves_ind_sites(scenario)
409+
data, curves_individual = calc_load_curves_ind_sites(scenario)
353410
data.index = data.index.rename(["bus", "wz"])
354411
data["scn_name"] = scenario
355412

@@ -362,3 +419,14 @@ def insert_sites_ind_load():
362419
con=db.engine(),
363420
if_exists="append",
364421
)
422+
423+
curves_individual['peak_load'] = np.array(curves_individual['p_set'].values.tolist()).max(axis=1)
424+
curves_individual['demand'] = np.array(curves_individual['p_set'].values.tolist()).sum(axis=1)
425+
curves_individual = identify_voltage_level(curves_individual)
426+
427+
curves_individual.to_sql(
428+
targets["sites_load_individual"]["table"],
429+
schema=targets["sites_load_individual"]["schema"],
430+
con=db.engine(),
431+
if_exists="append",
432+
)

0 commit comments

Comments
 (0)