1
|
|
|
"""The central module containing all code dealing with importing and |
2
|
|
|
adjusting data from demandRegio |
3
|
|
|
|
4
|
|
|
""" |
5
|
|
|
import pandas as pd |
6
|
|
|
import numpy as np |
7
|
|
|
import egon.data.config |
8
|
|
|
import egon.data.datasets.scenario_parameters.parameters as scenario_parameters |
9
|
|
|
from egon.data import db |
10
|
|
|
from egon.data.datasets.scenario_parameters import ( |
11
|
|
|
get_sector_parameters, |
12
|
|
|
EgonScenario, |
13
|
|
|
) |
14
|
|
|
from sqlalchemy import Column, String, Float, Integer, ForeignKey, ARRAY |
15
|
|
|
from sqlalchemy.ext.declarative import declarative_base |
16
|
|
|
from egon.data.datasets.demandregio.install_disaggregator import ( |
17
|
|
|
clone_and_install, |
18
|
|
|
) |
19
|
|
|
from egon.data.datasets import Dataset |
20
|
|
|
from pathlib import Path |
21
|
|
|
|
22
|
|
|
try: |
23
|
|
|
from disaggregator import data, spatial, config |
24
|
|
|
|
25
|
|
|
except ImportError as e: |
26
|
|
|
pass |
27
|
|
|
|
28
|
|
|
# will be later imported from another file ### |
29
|
|
|
Base = declarative_base() |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class DemandRegio(Dataset): |
33
|
|
|
def __init__(self, dependencies): |
34
|
|
|
super().__init__( |
35
|
|
|
name="DemandRegio", |
36
|
|
|
version="0.0.4", |
37
|
|
|
dependencies=dependencies, |
38
|
|
|
tasks=( |
39
|
|
|
clone_and_install, |
40
|
|
|
create_tables, |
41
|
|
|
{ |
42
|
|
|
insert_household_demand, |
43
|
|
|
insert_society_data, |
44
|
|
|
insert_cts_ind_demands, |
45
|
|
|
}, |
46
|
|
|
), |
47
|
|
|
) |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
class EgonDemandRegioHH(Base): |
51
|
|
|
__tablename__ = "egon_demandregio_hh" |
52
|
|
|
__table_args__ = {"schema": "demand"} |
53
|
|
|
nuts3 = Column(String(5), primary_key=True) |
54
|
|
|
hh_size = Column(Integer, primary_key=True) |
55
|
|
|
scenario = Column(String, ForeignKey(EgonScenario.name), primary_key=True) |
56
|
|
|
year = Column(Integer) |
57
|
|
|
demand = Column(Float) |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
class EgonDemandRegioCtsInd(Base): |
61
|
|
|
__tablename__ = "egon_demandregio_cts_ind" |
62
|
|
|
__table_args__ = {"schema": "demand"} |
63
|
|
|
nuts3 = Column(String(5), primary_key=True) |
64
|
|
|
wz = Column(Integer, primary_key=True) |
65
|
|
|
scenario = Column(String, ForeignKey(EgonScenario.name), primary_key=True) |
66
|
|
|
year = Column(Integer) |
67
|
|
|
demand = Column(Float) |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
class EgonDemandRegioPopulation(Base): |
71
|
|
|
__tablename__ = "egon_demandregio_population" |
72
|
|
|
__table_args__ = {"schema": "society"} |
73
|
|
|
nuts3 = Column(String(5), primary_key=True) |
74
|
|
|
year = Column(Integer, primary_key=True) |
75
|
|
|
population = Column(Float) |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
class EgonDemandRegioHouseholds(Base): |
79
|
|
|
__tablename__ = "egon_demandregio_household" |
80
|
|
|
__table_args__ = {"schema": "society"} |
81
|
|
|
nuts3 = Column(String(5), primary_key=True) |
82
|
|
|
hh_size = Column(Integer, primary_key=True) |
83
|
|
|
year = Column(Integer, primary_key=True) |
84
|
|
|
households = Column(Integer) |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
class EgonDemandRegioWz(Base): |
88
|
|
|
__tablename__ = "egon_demandregio_wz" |
89
|
|
|
__table_args__ = {"schema": "demand"} |
90
|
|
|
wz = Column(Integer, primary_key=True) |
91
|
|
|
sector = Column(String(50)) |
92
|
|
|
definition = Column(String(150)) |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
class EgonDemandRegioTimeseriesCtsInd(Base): |
96
|
|
|
__tablename__ = "egon_demandregio_timeseries_cts_ind" |
97
|
|
|
__table_args__ = {"schema": "demand"} |
98
|
|
|
wz = Column(Integer, primary_key=True) |
99
|
|
|
year = Column(Integer, primary_key=True) |
100
|
|
|
slp = Column(String(50)) |
101
|
|
|
load_curve = Column(ARRAY(Float())) |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def create_tables(): |
105
|
|
|
"""Create tables for demandregio data |
106
|
|
|
Returns |
107
|
|
|
------- |
108
|
|
|
None. |
109
|
|
|
""" |
110
|
|
|
db.execute_sql("CREATE SCHEMA IF NOT EXISTS demand;") |
111
|
|
|
db.execute_sql("CREATE SCHEMA IF NOT EXISTS society;") |
112
|
|
|
engine = db.engine() |
113
|
|
|
EgonDemandRegioHH.__table__.create(bind=engine, checkfirst=True) |
114
|
|
|
EgonDemandRegioCtsInd.__table__.create(bind=engine, checkfirst=True) |
115
|
|
|
EgonDemandRegioPopulation.__table__.create(bind=engine, checkfirst=True) |
116
|
|
|
EgonDemandRegioHouseholds.__table__.create(bind=engine, checkfirst=True) |
117
|
|
|
EgonDemandRegioWz.__table__.create(bind=engine, checkfirst=True) |
118
|
|
|
EgonDemandRegioTimeseriesCtsInd.__table__.drop( |
119
|
|
|
bind=engine, checkfirst=True |
120
|
|
|
) |
121
|
|
|
EgonDemandRegioTimeseriesCtsInd.__table__.create( |
122
|
|
|
bind=engine, checkfirst=True |
123
|
|
|
) |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
def data_in_boundaries(df): |
127
|
|
|
"""Select rows with nuts3 code within boundaries, used for testmode |
128
|
|
|
|
129
|
|
|
Parameters |
130
|
|
|
---------- |
131
|
|
|
df : pandas.DataFrame |
132
|
|
|
Data for all nuts3 regions |
133
|
|
|
|
134
|
|
|
Returns |
135
|
|
|
------- |
136
|
|
|
pandas.DataFrame |
137
|
|
|
Data for nuts3 regions within boundaries |
138
|
|
|
|
139
|
|
|
""" |
140
|
|
|
engine = db.engine() |
141
|
|
|
|
142
|
|
|
df = df.reset_index() |
143
|
|
|
|
144
|
|
|
# Change nuts3 region names to 2016 version |
145
|
|
|
nuts_names = {"DEB16": "DEB1C", "DEB19": "DEB1D"} |
146
|
|
|
df.loc[df.nuts3.isin(nuts_names), "nuts3"] = df.loc[ |
147
|
|
|
df.nuts3.isin(nuts_names), "nuts3" |
148
|
|
|
].map(nuts_names) |
149
|
|
|
|
150
|
|
|
df = df.set_index("nuts3") |
151
|
|
|
|
152
|
|
|
return df[ |
153
|
|
|
df.index.isin( |
154
|
|
|
pd.read_sql( |
155
|
|
|
"SELECT DISTINCT ON (nuts) nuts FROM boundaries.vg250_krs", |
156
|
|
|
engine, |
157
|
|
|
).nuts |
158
|
|
|
) |
159
|
|
|
] |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
def insert_cts_ind_wz_definitions(): |
163
|
|
|
"""Insert demandregio's definitions of CTS and industrial branches |
164
|
|
|
|
165
|
|
|
Returns |
166
|
|
|
------- |
167
|
|
|
None. |
168
|
|
|
|
169
|
|
|
""" |
170
|
|
|
|
171
|
|
|
source = egon.data.config.datasets()["demandregio_cts_ind_demand"][ |
172
|
|
|
"sources" |
173
|
|
|
] |
174
|
|
|
|
175
|
|
|
target = egon.data.config.datasets()["demandregio_cts_ind_demand"][ |
176
|
|
|
"targets" |
177
|
|
|
]["wz_definitions"] |
178
|
|
|
|
179
|
|
|
engine = db.engine() |
180
|
|
|
|
181
|
|
|
for sector in source["wz_definitions"]: |
182
|
|
|
|
183
|
|
|
file_path = ( |
184
|
|
|
Path(".") |
185
|
|
|
/ "data_bundle_egon_data" |
186
|
|
|
/ "WZ_definition" |
187
|
|
|
/ source["wz_definitions"][sector] |
188
|
|
|
) |
189
|
|
|
|
190
|
|
|
if sector == "CTS": |
191
|
|
|
delimiter = ";" |
192
|
|
|
else: |
193
|
|
|
delimiter = "," |
194
|
|
|
df = ( |
195
|
|
|
pd.read_csv(file_path, delimiter=delimiter, header=None) |
196
|
|
|
.rename({0: "wz", 1: "definition"}, axis="columns") |
197
|
|
|
.set_index("wz") |
198
|
|
|
) |
199
|
|
|
df["sector"] = sector |
200
|
|
|
df.to_sql( |
201
|
|
|
target["table"], |
202
|
|
|
engine, |
203
|
|
|
schema=target["schema"], |
204
|
|
|
if_exists="append", |
205
|
|
|
) |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
def match_nuts3_bl(): |
209
|
|
|
"""Function that maps the federal state to each nuts3 region |
210
|
|
|
|
211
|
|
|
Returns |
212
|
|
|
------- |
213
|
|
|
df : pandas.DataFrame |
214
|
|
|
List of nuts3 regions and the federal state of Germany. |
215
|
|
|
|
216
|
|
|
""" |
217
|
|
|
|
218
|
|
|
engine = db.engine() |
219
|
|
|
|
220
|
|
|
df = pd.read_sql( |
221
|
|
|
"SELECT DISTINCT ON (boundaries.vg250_krs.nuts) " |
222
|
|
|
"boundaries.vg250_krs.nuts, boundaries.vg250_lan.gen " |
223
|
|
|
"FROM boundaries.vg250_lan, boundaries.vg250_krs " |
224
|
|
|
" WHERE ST_CONTAINS(" |
225
|
|
|
"boundaries.vg250_lan.geometry, " |
226
|
|
|
"boundaries.vg250_krs.geometry)", |
227
|
|
|
con=engine, |
228
|
|
|
) |
229
|
|
|
|
230
|
|
|
df.gen[df.gen == "Baden-Württemberg (Bodensee)"] = "Baden-Württemberg" |
231
|
|
|
df.gen[df.gen == "Bayern (Bodensee)"] = "Bayern" |
232
|
|
|
|
233
|
|
|
return df.set_index("nuts") |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
def adjust_ind_pes(ec_cts_ind): |
237
|
|
|
""" |
238
|
|
|
Adjust electricity demand of industrial consumers due to electrification |
239
|
|
|
of process heat based on assumptions of pypsa-eur-sec. |
240
|
|
|
|
241
|
|
|
Parameters |
242
|
|
|
---------- |
243
|
|
|
ec_cts_ind : pandas.DataFrame |
244
|
|
|
Industrial demand without additional electrification |
245
|
|
|
|
246
|
|
|
Returns |
247
|
|
|
------- |
248
|
|
|
ec_cts_ind : pandas.DataFrame |
249
|
|
|
Industrial demand with additional electrification |
250
|
|
|
|
251
|
|
|
""" |
252
|
|
|
|
253
|
|
|
pes_path = ( |
254
|
|
|
Path(".") / "data_bundle_egon_data" / "pypsa_eur_sec" / "resources" |
255
|
|
|
) |
256
|
|
|
|
257
|
|
|
sources = egon.data.config.datasets()["demandregio_cts_ind_demand"][ |
258
|
|
|
"sources" |
259
|
|
|
]["new_consumers_2050"] |
260
|
|
|
|
261
|
|
|
# Extract today's industrial demand from pypsa-eur-sec |
262
|
|
|
demand_today = pd.read_csv( |
263
|
|
|
pes_path / sources["pes-demand-today"], |
264
|
|
|
header=None, |
265
|
|
|
).transpose() |
266
|
|
|
|
267
|
|
|
# Filter data |
268
|
|
|
demand_today[1].fillna("carrier", inplace=True) |
269
|
|
|
demand_today = demand_today[ |
270
|
|
|
(demand_today[0] == "DE") | (demand_today[1] == "carrier") |
271
|
|
|
].drop([0, 2], axis="columns") |
272
|
|
|
|
273
|
|
|
demand_today = ( |
274
|
|
|
demand_today.transpose() |
275
|
|
|
.set_index(0) |
276
|
|
|
.transpose() |
277
|
|
|
.set_index("carrier") |
278
|
|
|
.transpose() |
279
|
|
|
.loc["electricity"] |
280
|
|
|
.astype(float) |
281
|
|
|
) |
282
|
|
|
|
283
|
|
|
# Calculate future industrial demand from pypsa-eur-sec |
284
|
|
|
# based on production and energy demands per carrier ('sector ratios') |
285
|
|
|
prod_tomorrow = pd.read_csv(pes_path / sources["pes-production-tomorrow"]) |
286
|
|
|
|
287
|
|
|
prod_tomorrow = prod_tomorrow[prod_tomorrow["kton/a"] == "DE"].set_index( |
288
|
|
|
"kton/a" |
289
|
|
|
) |
290
|
|
|
|
291
|
|
|
sector_ratio = ( |
292
|
|
|
pd.read_csv(pes_path / sources["pes-sector-ratios"]) |
293
|
|
|
.set_index("MWh/tMaterial") |
294
|
|
|
.loc["elec"] |
295
|
|
|
) |
296
|
|
|
|
297
|
|
|
demand_tomorrow = prod_tomorrow.multiply( |
298
|
|
|
sector_ratio.div(1000) |
299
|
|
|
).transpose()["DE"] |
300
|
|
|
|
301
|
|
|
# Calculate changes of electrical demand per sector in pypsa-eur-sec |
302
|
|
|
change = pd.DataFrame( |
303
|
|
|
(demand_tomorrow / demand_today) |
304
|
|
|
/ (demand_tomorrow / demand_today).sum() |
305
|
|
|
) |
306
|
|
|
|
307
|
|
|
# Drop rows without changes |
308
|
|
|
change = change[~change[0].isnull()] |
309
|
|
|
|
310
|
|
|
# Map industrial branches of pypsa-eur-sec to WZ2008 used in demandregio |
311
|
|
|
change["wz"] = change.index.map( |
312
|
|
|
{ |
313
|
|
|
"Alumina production": 24, |
314
|
|
|
"Aluminium - primary production": 24, |
315
|
|
|
"Aluminium - secondary production": 24, |
316
|
|
|
"Ammonia": 20, |
317
|
|
|
"Basic chemicals (without ammonia)": 20, |
318
|
|
|
"Cement": 23, |
319
|
|
|
"Ceramics & other NMM": 23, |
320
|
|
|
"Electric arc": 24, |
321
|
|
|
"Food, beverages and tobacco": 10, |
322
|
|
|
"Glass production": 23, |
323
|
|
|
"Integrated steelworks": 24, |
324
|
|
|
"Machinery Equipment": 28, |
325
|
|
|
"Other Industrial Sectors": 32, |
326
|
|
|
"Other chemicals": 20, |
327
|
|
|
"Other non-ferrous metals": 24, |
328
|
|
|
"Paper production": 17, |
329
|
|
|
"Pharmaceutical products etc.": 21, |
330
|
|
|
"Printing and media reproduction": 18, |
331
|
|
|
"Pulp production": 17, |
332
|
|
|
"Textiles and leather": 13, |
333
|
|
|
"Transport Equipment": 29, |
334
|
|
|
"Wood and wood products": 16, |
335
|
|
|
} |
336
|
|
|
) |
337
|
|
|
|
338
|
|
|
# Group by WZ2008 |
339
|
|
|
shares_per_wz = change.groupby("wz")[0].sum() |
340
|
|
|
|
341
|
|
|
# Calculate addtional demands needed to meet future demand of pypsa-eur-sec |
342
|
|
|
addtional_mwh = shares_per_wz.multiply( |
343
|
|
|
demand_tomorrow.sum() * 1000000 - ec_cts_ind.sum().sum() |
344
|
|
|
) |
345
|
|
|
|
346
|
|
|
# Calulate overall industrial demand for eGon100RE |
347
|
|
|
final_mwh = addtional_mwh + ec_cts_ind[addtional_mwh.index].sum() |
348
|
|
|
|
349
|
|
|
# Linear scale the industrial demands per nuts3 and wz to meet final demand |
350
|
|
|
ec_cts_ind[addtional_mwh.index] *= ( |
351
|
|
|
final_mwh / ec_cts_ind[addtional_mwh.index].sum() |
352
|
|
|
) |
353
|
|
|
|
354
|
|
|
return ec_cts_ind |
355
|
|
|
|
356
|
|
|
|
357
|
|
|
def adjust_cts_ind_nep(ec_cts_ind, sector): |
358
|
|
|
"""Add electrical demand of new largescale CTS und industrial consumers |
359
|
|
|
according to NEP 2021, scneario C 2035. Values per federal state are |
360
|
|
|
linear distributed over all CTS branches and nuts3 regions. |
361
|
|
|
|
362
|
|
|
Parameters |
363
|
|
|
---------- |
364
|
|
|
ec_cts_ind : pandas.DataFrame |
365
|
|
|
CTS or industry demand without new largescale consumers. |
366
|
|
|
|
367
|
|
|
Returns |
368
|
|
|
------- |
369
|
|
|
ec_cts_ind : pandas.DataFrame |
370
|
|
|
CTS or industry demand including new largescale consumers. |
371
|
|
|
|
372
|
|
|
""" |
373
|
|
|
sources = egon.data.config.datasets()["demandregio_cts_ind_demand"][ |
374
|
|
|
"sources" |
375
|
|
|
] |
376
|
|
|
|
377
|
|
|
file_path = ( |
378
|
|
|
Path(".") |
379
|
|
|
/ "data_bundle_egon_data" |
380
|
|
|
/ "nep2035_version2021" |
381
|
|
|
/ sources["new_consumers_2035"] |
382
|
|
|
) |
383
|
|
|
|
384
|
|
|
# get data from NEP per federal state |
385
|
|
|
new_con = pd.read_csv(file_path, delimiter=";", decimal=",", index_col=0) |
386
|
|
|
|
387
|
|
|
# match nuts3 regions to federal states |
388
|
|
|
groups = ec_cts_ind.groupby(match_nuts3_bl().gen) |
389
|
|
|
|
390
|
|
|
# update demands per federal state |
391
|
|
|
for group in groups.indices.keys(): |
392
|
|
|
g = groups.get_group(group) |
393
|
|
|
data_new = g.mul(1 + new_con[sector][group] * 1e6 / g.sum().sum()) |
394
|
|
|
ec_cts_ind[ec_cts_ind.index.isin(g.index)] = data_new |
395
|
|
|
|
396
|
|
|
return ec_cts_ind |
397
|
|
|
|
398
|
|
|
|
399
|
|
|
def disagg_households_power( |
400
|
|
|
scenario, year, weight_by_income=False, original=False, **kwargs |
401
|
|
|
): |
402
|
|
|
""" |
403
|
|
|
Perform spatial disaggregation of electric power in [GWh/a] by key and |
404
|
|
|
possibly weight by income. |
405
|
|
|
Similar to disaggregator.spatial.disagg_households_power |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
Parameters |
409
|
|
|
---------- |
410
|
|
|
by : str |
411
|
|
|
must be one of ['households', 'population'] |
412
|
|
|
weight_by_income : bool, optional |
413
|
|
|
Flag if to weight the results by the regional income (default False) |
414
|
|
|
orignal : bool, optional |
415
|
|
|
Throughput to function households_per_size, |
416
|
|
|
A flag if the results should be left untouched and returned in |
417
|
|
|
original form for the year 2011 (True) or if they should be scaled to |
418
|
|
|
the given `year` by the population in that year (False). |
419
|
|
|
|
420
|
|
|
Returns |
421
|
|
|
------- |
422
|
|
|
pd.DataFrame or pd.Series |
423
|
|
|
""" |
424
|
|
|
# source: survey of energieAgenturNRW |
425
|
|
|
demand_per_hh_size = pd.DataFrame( |
426
|
|
|
index=range(1, 7), |
427
|
|
|
data={ |
428
|
|
|
"weighted DWH": [2290, 3202, 4193, 4955, 5928, 5928], |
429
|
|
|
"without DHW": [1714, 2812, 3704, 4432, 5317, 5317], |
430
|
|
|
}, |
431
|
|
|
) |
432
|
|
|
|
433
|
|
|
# Bottom-Up: Power demand by household sizes in [MWh/a] for each scenario |
434
|
|
|
if scenario == "eGon2035": |
435
|
|
|
# chose demand per household size from survey including weighted DHW |
436
|
|
|
power_per_HH = demand_per_hh_size["weighted DWH"] / 1e3 |
437
|
|
|
|
438
|
|
|
# calculate demand per nuts3 |
439
|
|
|
df = ( |
440
|
|
|
data.households_per_size(original=original, year=year) |
441
|
|
|
* power_per_HH |
442
|
|
|
) |
443
|
|
|
|
444
|
|
|
# scale to fit demand of NEP 2021 scebario C 2035 (119TWh) |
445
|
|
|
df *= 119000000 / df.sum().sum() |
446
|
|
|
|
447
|
|
|
elif scenario == "eGon100RE": |
448
|
|
|
|
449
|
|
|
# chose demand per household size from survey without DHW |
450
|
|
|
power_per_HH = demand_per_hh_size["without DHW"] / 1e3 |
451
|
|
|
|
452
|
|
|
# calculate demand per nuts3 in 2011 |
453
|
|
|
df_2011 = data.households_per_size(year=2011) * power_per_HH |
454
|
|
|
|
455
|
|
|
# scale demand per hh-size to meet demand without heat |
456
|
|
|
# according to JRC in 2011 (136.6-(20.14+9.41) TWh) |
457
|
|
|
power_per_HH *= (136.6 - (20.14 + 9.41)) * 1e6 / df_2011.sum().sum() |
458
|
|
|
|
459
|
|
|
# calculate demand per nuts3 in 2050 |
460
|
|
|
df = data.households_per_size(year=year) * power_per_HH |
461
|
|
|
|
462
|
|
|
else: |
463
|
|
|
print( |
464
|
|
|
f"Electric demand per household size for scenario {scenario} " |
465
|
|
|
"is not specified." |
466
|
|
|
) |
467
|
|
|
|
468
|
|
|
if weight_by_income: |
469
|
|
|
df = spatial.adjust_by_income(df=df) |
|
|
|
|
470
|
|
|
|
471
|
|
|
return df |
472
|
|
|
|
473
|
|
|
|
474
|
|
|
def insert_hh_demand(scenario, year, engine): |
475
|
|
|
"""Calculates electrical demands of private households using demandregio's |
476
|
|
|
disaggregator and insert results into the database. |
477
|
|
|
|
478
|
|
|
Parameters |
479
|
|
|
---------- |
480
|
|
|
scenario : str |
481
|
|
|
Name of the corresponing scenario. |
482
|
|
|
year : int |
483
|
|
|
The number of households per region is taken from this year. |
484
|
|
|
|
485
|
|
|
Returns |
486
|
|
|
------- |
487
|
|
|
None. |
488
|
|
|
|
489
|
|
|
""" |
490
|
|
|
targets = egon.data.config.datasets()["demandregio_household_demand"][ |
491
|
|
|
"targets" |
492
|
|
|
]["household_demand"] |
493
|
|
|
# get demands of private households per nuts and size from demandregio |
494
|
|
|
ec_hh = disagg_households_power(scenario, year) |
495
|
|
|
|
496
|
|
|
# Select demands for nuts3-regions in boundaries (needed for testmode) |
497
|
|
|
ec_hh = data_in_boundaries(ec_hh) |
498
|
|
|
|
499
|
|
|
# insert into database |
500
|
|
|
for hh_size in ec_hh.columns: |
501
|
|
|
df = pd.DataFrame(ec_hh[hh_size]) |
502
|
|
|
df["year"] = year |
503
|
|
|
df["scenario"] = scenario |
504
|
|
|
df["hh_size"] = hh_size |
505
|
|
|
df = df.rename({hh_size: "demand"}, axis="columns") |
506
|
|
|
df.to_sql( |
507
|
|
|
targets["table"], |
508
|
|
|
engine, |
509
|
|
|
schema=targets["schema"], |
510
|
|
|
if_exists="append", |
511
|
|
|
) |
512
|
|
|
|
513
|
|
|
|
514
|
|
|
def insert_cts_ind(scenario, year, engine, target_values): |
515
|
|
|
"""Calculates electrical demands of CTS and industry using demandregio's |
516
|
|
|
disaggregator, adjusts them according to resulting values of NEP 2021 or |
517
|
|
|
JRC IDEES and insert results into the database. |
518
|
|
|
|
519
|
|
|
Parameters |
520
|
|
|
---------- |
521
|
|
|
scenario : str |
522
|
|
|
Name of the corresponing scenario. |
523
|
|
|
year : int |
524
|
|
|
The number of households per region is taken from this year. |
525
|
|
|
target_values : dict |
526
|
|
|
List of target values for each scenario and sector. |
527
|
|
|
|
528
|
|
|
Returns |
529
|
|
|
------- |
530
|
|
|
None. |
531
|
|
|
|
532
|
|
|
""" |
533
|
|
|
|
534
|
|
|
targets = egon.data.config.datasets()["demandregio_cts_ind_demand"][ |
535
|
|
|
"targets" |
536
|
|
|
] |
537
|
|
|
|
538
|
|
|
for sector in ["CTS", "industry"]: |
539
|
|
|
# get demands per nuts3 and wz of demandregio |
540
|
|
|
ec_cts_ind = spatial.disagg_CTS_industry( |
541
|
|
|
use_nuts3code=True, source="power", sector=sector, year=year |
542
|
|
|
).transpose() |
543
|
|
|
|
544
|
|
|
ec_cts_ind.index = ec_cts_ind.index.rename("nuts3") |
545
|
|
|
|
546
|
|
|
# exclude mobility sector from GHD |
547
|
|
|
ec_cts_ind = ec_cts_ind.drop(columns=49, errors="ignore") |
548
|
|
|
|
549
|
|
|
# scale values according to target_values |
550
|
|
|
if sector in target_values[scenario].keys(): |
551
|
|
|
ec_cts_ind *= ( |
552
|
|
|
target_values[scenario][sector] * 1e3 / ec_cts_ind.sum().sum() |
553
|
|
|
) |
554
|
|
|
|
555
|
|
|
# include new largescale consumers according to NEP 2021 |
556
|
|
|
if scenario == "eGon2035": |
557
|
|
|
ec_cts_ind = adjust_cts_ind_nep(ec_cts_ind, sector) |
558
|
|
|
# include new industrial demands due to sector coupling |
559
|
|
|
if (scenario == "eGon100RE") & (sector == "industry"): |
560
|
|
|
ec_cts_ind = adjust_ind_pes(ec_cts_ind) |
561
|
|
|
|
562
|
|
|
# Select demands for nuts3-regions in boundaries (needed for testmode) |
563
|
|
|
ec_cts_ind = data_in_boundaries(ec_cts_ind) |
564
|
|
|
|
565
|
|
|
# insert into database |
566
|
|
|
for wz in ec_cts_ind.columns: |
567
|
|
|
df = pd.DataFrame(ec_cts_ind[wz]) |
568
|
|
|
df["year"] = year |
569
|
|
|
df["wz"] = wz |
570
|
|
|
df["scenario"] = scenario |
571
|
|
|
df = df.rename({wz: "demand"}, axis="columns") |
572
|
|
|
df.index = df.index.rename("nuts3") |
573
|
|
|
df.to_sql( |
574
|
|
|
targets["cts_ind_demand"]["table"], |
575
|
|
|
engine, |
576
|
|
|
targets["cts_ind_demand"]["schema"], |
577
|
|
|
if_exists="append", |
578
|
|
|
) |
579
|
|
|
|
580
|
|
|
|
581
|
|
|
def insert_household_demand(): |
582
|
|
|
"""Insert electrical demands for households according to |
583
|
|
|
demandregio using its disaggregator-tool in MWh |
584
|
|
|
|
585
|
|
|
Returns |
586
|
|
|
------- |
587
|
|
|
None. |
588
|
|
|
|
589
|
|
|
""" |
590
|
|
|
targets = egon.data.config.datasets()["demandregio_household_demand"][ |
591
|
|
|
"targets" |
592
|
|
|
] |
593
|
|
|
engine = db.engine() |
594
|
|
|
|
595
|
|
|
for t in targets: |
596
|
|
|
db.execute_sql( |
597
|
|
|
f"DELETE FROM {targets[t]['schema']}.{targets[t]['table']};" |
598
|
|
|
) |
599
|
|
|
|
600
|
|
|
for scn in ["eGon2035", "eGon100RE"]: |
601
|
|
|
|
602
|
|
|
year = scenario_parameters.global_settings(scn)["population_year"] |
603
|
|
|
|
604
|
|
|
# Insert demands of private households |
605
|
|
|
insert_hh_demand(scn, year, engine) |
606
|
|
|
|
607
|
|
|
|
608
|
|
|
def insert_cts_ind_demands(): |
609
|
|
|
"""Insert electricity demands per nuts3-region in Germany according to |
610
|
|
|
demandregio using its disaggregator-tool in MWh |
611
|
|
|
|
612
|
|
|
Returns |
613
|
|
|
------- |
614
|
|
|
None. |
615
|
|
|
|
616
|
|
|
""" |
617
|
|
|
targets = egon.data.config.datasets()["demandregio_cts_ind_demand"][ |
618
|
|
|
"targets" |
619
|
|
|
] |
620
|
|
|
engine = db.engine() |
621
|
|
|
|
622
|
|
|
for t in targets: |
623
|
|
|
db.execute_sql( |
624
|
|
|
f"DELETE FROM {targets[t]['schema']}.{targets[t]['table']};" |
625
|
|
|
) |
626
|
|
|
|
627
|
|
|
insert_cts_ind_wz_definitions() |
628
|
|
|
|
629
|
|
|
for scn in ["eGon2035", "eGon100RE"]: |
630
|
|
|
|
631
|
|
|
year = scenario_parameters.global_settings(scn)["population_year"] |
632
|
|
|
|
633
|
|
|
if year > 2035: |
634
|
|
|
year = 2035 |
635
|
|
|
|
636
|
|
|
# target values per scenario in MWh |
637
|
|
|
target_values = { |
638
|
|
|
# according to NEP 2021 |
639
|
|
|
# new consumers will be added seperatly |
640
|
|
|
"eGon2035": {"CTS": 135300, "industry": 225400}, |
641
|
|
|
# CTS: reduce overall demand from demandregio (without traffic) |
642
|
|
|
# by share of heat according to JRC IDEES, data from 2011 |
643
|
|
|
# industry: no specific heat demand, use data from demandregio |
644
|
|
|
"eGon100RE": {"CTS": (1 - (5.96 + 6.13) / 154.64) * 125183.403}, |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
insert_cts_ind(scn, year, engine, target_values) |
648
|
|
|
|
649
|
|
|
# Insert load curves per wz |
650
|
|
|
timeseries_per_wz() |
651
|
|
|
|
652
|
|
|
|
653
|
|
|
def insert_society_data(): |
654
|
|
|
"""Insert population and number of households per nuts3-region in Germany |
655
|
|
|
according to demandregio using its disaggregator-tool |
656
|
|
|
|
657
|
|
|
Returns |
658
|
|
|
------- |
659
|
|
|
None. |
660
|
|
|
|
661
|
|
|
""" |
662
|
|
|
targets = egon.data.config.datasets()["demandregio_society"]["targets"] |
663
|
|
|
engine = db.engine() |
664
|
|
|
|
665
|
|
|
for t in targets: |
666
|
|
|
db.execute_sql( |
667
|
|
|
f"DELETE FROM {targets[t]['schema']}.{targets[t]['table']};" |
668
|
|
|
) |
669
|
|
|
|
670
|
|
|
target_years = np.append( |
671
|
|
|
get_sector_parameters("global").population_year.values, 2018 |
672
|
|
|
) |
673
|
|
|
|
674
|
|
|
for year in target_years: |
675
|
|
|
df_pop = pd.DataFrame(data.population(year=year)) |
676
|
|
|
df_pop["year"] = year |
677
|
|
|
df_pop = df_pop.rename({"value": "population"}, axis="columns") |
678
|
|
|
# Select data for nuts3-regions in boundaries (needed for testmode) |
679
|
|
|
df_pop = data_in_boundaries(df_pop) |
680
|
|
|
df_pop.to_sql( |
681
|
|
|
targets["population"]["table"], |
682
|
|
|
engine, |
683
|
|
|
schema=targets["population"]["schema"], |
684
|
|
|
if_exists="append", |
685
|
|
|
) |
686
|
|
|
|
687
|
|
|
for year in target_years: |
688
|
|
|
df_hh = pd.DataFrame(data.households_per_size(year=year)) |
689
|
|
|
# Select data for nuts3-regions in boundaries (needed for testmode) |
690
|
|
|
df_hh = data_in_boundaries(df_hh) |
691
|
|
|
for hh_size in df_hh.columns: |
692
|
|
|
df = pd.DataFrame(df_hh[hh_size]) |
693
|
|
|
df["year"] = year |
694
|
|
|
df["hh_size"] = hh_size |
695
|
|
|
df = df.rename({hh_size: "households"}, axis="columns") |
696
|
|
|
df.to_sql( |
697
|
|
|
targets["household"]["table"], |
698
|
|
|
engine, |
699
|
|
|
schema=targets["household"]["schema"], |
700
|
|
|
if_exists="append", |
701
|
|
|
) |
702
|
|
|
|
703
|
|
|
|
704
|
|
|
def insert_timeseries_per_wz(sector, year): |
705
|
|
|
"""Insert normalized electrical load time series for the selected sector |
706
|
|
|
|
707
|
|
|
Parameters |
708
|
|
|
---------- |
709
|
|
|
sector : str |
710
|
|
|
Name of the sector. ['CTS', 'industry'] |
711
|
|
|
year : int |
712
|
|
|
Selected weather year |
713
|
|
|
|
714
|
|
|
Returns |
715
|
|
|
------- |
716
|
|
|
None. |
717
|
|
|
|
718
|
|
|
""" |
719
|
|
|
targets = egon.data.config.datasets()["demandregio_cts_ind_demand"][ |
720
|
|
|
"targets" |
721
|
|
|
] |
722
|
|
|
|
723
|
|
|
if sector == "CTS": |
724
|
|
|
profiles = ( |
725
|
|
|
data.CTS_power_slp_generator("SH", year=year).resample("H").sum() |
726
|
|
|
) |
727
|
|
|
wz_slp = config.slp_branch_cts_power() |
728
|
|
|
elif sector == "industry": |
729
|
|
|
profiles = ( |
730
|
|
|
data.shift_load_profile_generator(state="SH", year=year) |
731
|
|
|
.resample("H") |
732
|
|
|
.sum() |
733
|
|
|
) |
734
|
|
|
wz_slp = config.shift_profile_industry() |
735
|
|
|
|
736
|
|
|
else: |
737
|
|
|
print(f"Sector {sector} is not valid.") |
738
|
|
|
|
739
|
|
|
df = pd.DataFrame( |
740
|
|
|
index=wz_slp.keys(), columns=["slp", "load_curve", "year"] |
|
|
|
|
741
|
|
|
) |
742
|
|
|
|
743
|
|
|
df.index.rename("wz", inplace=True) |
744
|
|
|
|
745
|
|
|
df.slp = wz_slp.values() |
746
|
|
|
|
747
|
|
|
df.year = year |
748
|
|
|
|
749
|
|
|
df.load_curve = profiles[df.slp].transpose().values.tolist() |
|
|
|
|
750
|
|
|
|
751
|
|
|
db.execute_sql( |
752
|
|
|
f""" |
753
|
|
|
DELETE FROM {targets['timeseries_cts_ind']['schema']}. |
754
|
|
|
{targets['timeseries_cts_ind']['table']} |
755
|
|
|
WHERE wz IN ( |
756
|
|
|
SELECT wz FROM {targets['wz_definitions']['schema']}. |
757
|
|
|
{targets['wz_definitions']['table']} |
758
|
|
|
WHERE sector = '{sector}') |
759
|
|
|
""" |
760
|
|
|
) |
761
|
|
|
|
762
|
|
|
df.to_sql( |
763
|
|
|
targets["timeseries_cts_ind"]["table"], |
764
|
|
|
schema=targets["timeseries_cts_ind"]["schema"], |
765
|
|
|
con=db.engine(), |
766
|
|
|
if_exists="append", |
767
|
|
|
) |
768
|
|
|
|
769
|
|
|
|
770
|
|
|
def timeseries_per_wz(): |
771
|
|
|
"""Calcultae and insert normalized timeseries per wz for cts and industry |
772
|
|
|
|
773
|
|
|
Returns |
774
|
|
|
------- |
775
|
|
|
None. |
776
|
|
|
|
777
|
|
|
""" |
778
|
|
|
|
779
|
|
|
years = get_sector_parameters("global").weather_year.unique() |
780
|
|
|
|
781
|
|
|
for year in years: |
782
|
|
|
|
783
|
|
|
for sector in ["CTS", "industry"]: |
784
|
|
|
|
785
|
|
|
insert_timeseries_per_wz(sector, int(year)) |
786
|
|
|
|