|
1
|
|
|
from datetime import datetime |
|
2
|
|
|
import os |
|
3
|
|
|
|
|
4
|
|
|
from sqlalchemy import ARRAY, Column, Float, Integer, String |
|
5
|
|
|
from sqlalchemy.ext.declarative import declarative_base |
|
6
|
|
|
|
|
7
|
|
|
import numpy as np |
|
8
|
|
|
import pandas as pd |
|
9
|
|
|
|
|
10
|
|
|
from egon.data import db |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
from math import ceil |
|
14
|
|
|
|
|
15
|
|
|
import egon |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
Base = declarative_base() |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
class EgonHeatTimeseries(Base): |
|
22
|
|
|
__tablename__ = "egon_heat_timeseries_selected_profiles" |
|
23
|
|
|
__table_args__ = {"schema": "demand"} |
|
24
|
|
|
zensus_population_id = Column(Integer, primary_key=True) |
|
25
|
|
|
building_id = Column(Integer, primary_key=True) |
|
26
|
|
|
selected_idp_profiles = Column(ARRAY(Integer)) |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
View Code Duplication |
def temperature_classes(): |
|
|
|
|
|
|
30
|
|
|
return { |
|
31
|
|
|
-20: 1, |
|
32
|
|
|
-19: 1, |
|
33
|
|
|
-18: 1, |
|
34
|
|
|
-17: 1, |
|
35
|
|
|
-16: 1, |
|
36
|
|
|
-15: 1, |
|
37
|
|
|
-14: 2, |
|
38
|
|
|
-13: 2, |
|
39
|
|
|
-12: 2, |
|
40
|
|
|
-11: 2, |
|
41
|
|
|
-10: 2, |
|
42
|
|
|
-9: 3, |
|
43
|
|
|
-8: 3, |
|
44
|
|
|
-7: 3, |
|
45
|
|
|
-6: 3, |
|
46
|
|
|
-5: 3, |
|
47
|
|
|
-4: 4, |
|
48
|
|
|
-3: 4, |
|
49
|
|
|
-2: 4, |
|
50
|
|
|
-1: 4, |
|
51
|
|
|
0: 4, |
|
52
|
|
|
1: 5, |
|
53
|
|
|
2: 5, |
|
54
|
|
|
3: 5, |
|
55
|
|
|
4: 5, |
|
56
|
|
|
5: 5, |
|
57
|
|
|
6: 6, |
|
58
|
|
|
7: 6, |
|
59
|
|
|
8: 6, |
|
60
|
|
|
9: 6, |
|
61
|
|
|
10: 6, |
|
62
|
|
|
11: 7, |
|
63
|
|
|
12: 7, |
|
64
|
|
|
13: 7, |
|
65
|
|
|
14: 7, |
|
66
|
|
|
15: 7, |
|
67
|
|
|
16: 8, |
|
68
|
|
|
17: 8, |
|
69
|
|
|
18: 8, |
|
70
|
|
|
19: 8, |
|
71
|
|
|
20: 8, |
|
72
|
|
|
21: 9, |
|
73
|
|
|
22: 9, |
|
74
|
|
|
23: 9, |
|
75
|
|
|
24: 9, |
|
76
|
|
|
25: 9, |
|
77
|
|
|
26: 10, |
|
78
|
|
|
27: 10, |
|
79
|
|
|
28: 10, |
|
80
|
|
|
29: 10, |
|
81
|
|
|
30: 10, |
|
82
|
|
|
31: 10, |
|
83
|
|
|
32: 10, |
|
84
|
|
|
33: 10, |
|
85
|
|
|
34: 10, |
|
86
|
|
|
35: 10, |
|
87
|
|
|
36: 10, |
|
88
|
|
|
37: 10, |
|
89
|
|
|
38: 10, |
|
90
|
|
|
39: 10, |
|
91
|
|
|
40: 10, |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
def idp_pool_generator(): |
|
96
|
|
|
""" |
|
97
|
|
|
Description: Create List of Dataframes for each temperature class for each household stock |
|
98
|
|
|
|
|
99
|
|
|
Returns |
|
100
|
|
|
------- |
|
101
|
|
|
TYPE list |
|
102
|
|
|
List of dataframes with each element representing a dataframe |
|
103
|
|
|
for every combination of household stock and temperature class |
|
104
|
|
|
|
|
105
|
|
|
""" |
|
106
|
|
|
path = os.path.join( |
|
107
|
|
|
os.getcwd(), |
|
108
|
|
|
"data_bundle_egon_data", |
|
109
|
|
|
"household_heat_demand_profiles", |
|
110
|
|
|
"household_heat_demand_profiles.hdf5", |
|
111
|
|
|
) |
|
112
|
|
|
index = pd.date_range(datetime(2011, 1, 1, 0), periods=8760, freq="H") |
|
113
|
|
|
|
|
114
|
|
|
sfh = pd.read_hdf(path, key="SFH") |
|
115
|
|
|
mfh = pd.read_hdf(path, key="MFH") |
|
116
|
|
|
temp = pd.read_hdf(path, key="temperature") |
|
117
|
|
|
|
|
118
|
|
|
globals()["luebeck_sfh"] = sfh[sfh.filter(like="Luebeck").columns] |
|
|
|
|
|
|
119
|
|
|
globals()["luebeck_mfh"] = mfh[mfh.filter(like="Luebeck").columns] |
|
120
|
|
|
|
|
121
|
|
|
globals()["kassel_sfh"] = sfh[sfh.filter(like="Kassel").columns] |
|
122
|
|
|
globals()["kassel_mfh"] = mfh[mfh.filter(like="Kassel").columns] |
|
123
|
|
|
|
|
124
|
|
|
globals()["wuerzburg_sfh"] = sfh[sfh.filter(like="Wuerzburg").columns] |
|
125
|
|
|
globals()["wuerzburg_mfh"] = mfh[mfh.filter(like="Wuerzburg").columns] |
|
126
|
|
|
|
|
127
|
|
|
globals()["chemnitz_sfh"] = sfh[sfh.filter(like="Chemnitz").columns] |
|
128
|
|
|
globals()["chemnitz_mfh"] = mfh[mfh.filter(like="Chemnitz").columns] |
|
129
|
|
|
|
|
130
|
|
|
temp_daily = pd.DataFrame() |
|
131
|
|
|
for column in temp.columns: |
|
132
|
|
|
temp_current = ( |
|
133
|
|
|
temp[column] |
|
134
|
|
|
.resample("D") |
|
135
|
|
|
.mean() |
|
136
|
|
|
.reindex(temp.index) |
|
137
|
|
|
.fillna(method="ffill") |
|
138
|
|
|
.fillna(method="bfill") |
|
139
|
|
|
) |
|
140
|
|
|
temp_current_geom = temp_current |
|
141
|
|
|
temp_daily = pd.concat([temp_daily, temp_current_geom], axis=1) |
|
142
|
|
|
|
|
143
|
|
|
def round_temperature(station): |
|
144
|
|
|
""" |
|
145
|
|
|
Description: Create dataframe to assign temperature class to each day of TRY climate zones |
|
146
|
|
|
|
|
147
|
|
|
Parameters |
|
148
|
|
|
---------- |
|
149
|
|
|
station : str |
|
150
|
|
|
Name of the location |
|
151
|
|
|
|
|
152
|
|
|
Returns |
|
153
|
|
|
------- |
|
154
|
|
|
temp_class : pandas.DataFrame |
|
155
|
|
|
Each day assignd to their respective temperature class |
|
156
|
|
|
|
|
157
|
|
|
""" |
|
158
|
|
|
intervals = temperature_classes() |
|
159
|
|
|
temperature_rounded = [] |
|
160
|
|
|
for i in temp_daily.loc[:, station]: |
|
161
|
|
|
temperature_rounded.append(ceil(i)) |
|
162
|
|
|
temperature_interval = [] |
|
163
|
|
|
for i in temperature_rounded: |
|
164
|
|
|
temperature_interval.append(intervals[i]) |
|
165
|
|
|
temp_class_dic = {f"Class_{station}": temperature_interval} |
|
166
|
|
|
temp_class = pd.DataFrame.from_dict(temp_class_dic) |
|
167
|
|
|
return temp_class |
|
168
|
|
|
|
|
169
|
|
|
temp_class_luebeck = round_temperature("Luebeck") |
|
170
|
|
|
temp_class_kassel = round_temperature("Kassel") |
|
171
|
|
|
temp_class_wuerzburg = round_temperature("Wuerzburg") |
|
172
|
|
|
temp_class_chemnitz = round_temperature("Chemnitz") |
|
173
|
|
|
temp_class = pd.concat( |
|
174
|
|
|
[ |
|
175
|
|
|
temp_class_luebeck, |
|
176
|
|
|
temp_class_kassel, |
|
177
|
|
|
temp_class_wuerzburg, |
|
178
|
|
|
temp_class_chemnitz, |
|
179
|
|
|
], |
|
180
|
|
|
axis=1, |
|
181
|
|
|
) |
|
182
|
|
|
temp_class.set_index(index, inplace=True) |
|
183
|
|
|
|
|
184
|
|
|
def unique_classes(station): |
|
185
|
|
|
""" |
|
186
|
|
|
|
|
187
|
|
|
Returns |
|
188
|
|
|
------- |
|
189
|
|
|
classes : list |
|
190
|
|
|
Collection of temperature classes for each location |
|
191
|
|
|
|
|
192
|
|
|
""" |
|
193
|
|
|
classes = [] |
|
194
|
|
|
for x in temp_class[f"Class_{station}"]: |
|
195
|
|
|
if x not in classes: |
|
196
|
|
|
classes.append(x) |
|
197
|
|
|
classes.sort() |
|
198
|
|
|
return classes |
|
199
|
|
|
|
|
200
|
|
|
globals()["luebeck_classes"] = unique_classes("Luebeck") |
|
201
|
|
|
globals()["kassel_classes"] = unique_classes("Kassel") |
|
202
|
|
|
globals()["wuerzburg_classes"] = unique_classes("Wuerzburg") |
|
203
|
|
|
globals()["chemnitz_classes"] = unique_classes("Chemnitz") |
|
204
|
|
|
|
|
205
|
|
|
stock = ["MFH", "SFH"] |
|
206
|
|
|
class_list = [2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
207
|
|
|
for s in stock: |
|
208
|
|
|
for m in class_list: |
|
209
|
|
|
globals()[f"idp_collection_class_{m}_{s}"] = pd.DataFrame( |
|
210
|
|
|
index=range(24) |
|
211
|
|
|
) |
|
212
|
|
|
|
|
213
|
|
|
def splitter(station, household_stock): |
|
214
|
|
|
""" |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
Parameters |
|
218
|
|
|
---------- |
|
219
|
|
|
station : str |
|
220
|
|
|
Name of the location |
|
221
|
|
|
household_stock : str |
|
222
|
|
|
SFH or MFH |
|
223
|
|
|
|
|
224
|
|
|
Returns |
|
225
|
|
|
------- |
|
226
|
|
|
None. |
|
227
|
|
|
|
|
228
|
|
|
""" |
|
229
|
|
|
this_classes = globals()["{}_classes".format(station.lower())] |
|
|
|
|
|
|
230
|
|
|
for classes in this_classes: |
|
231
|
|
|
this_itteration = globals()[ |
|
232
|
|
|
"{}_{}".format(station.lower(), household_stock.lower()) |
|
233
|
|
|
].loc[temp_class["Class_{}".format(station)] == classes, :] |
|
234
|
|
|
days = list(range(int(len(this_itteration) / 24))) |
|
235
|
|
|
for day in days: |
|
236
|
|
|
this_day = this_itteration[day * 24 : (day + 1) * 24] |
|
237
|
|
|
this_day = this_day.reset_index(drop=True) |
|
238
|
|
|
globals()[ |
|
239
|
|
|
f"idp_collection_class_{classes}_{household_stock}" |
|
240
|
|
|
] = pd.concat( |
|
241
|
|
|
[ |
|
242
|
|
|
globals()[ |
|
243
|
|
|
f"idp_collection_class_{classes}_{household_stock}" |
|
244
|
|
|
], |
|
245
|
|
|
this_day, |
|
246
|
|
|
], |
|
247
|
|
|
axis=1, |
|
248
|
|
|
ignore_index=True, |
|
249
|
|
|
) |
|
250
|
|
|
|
|
251
|
|
|
splitter("Luebeck", "SFH") |
|
252
|
|
|
splitter("Kassel", "SFH") |
|
253
|
|
|
splitter("Wuerzburg", "SFH") |
|
254
|
|
|
splitter("Chemnitz", "SFH") |
|
255
|
|
|
splitter("Luebeck", "MFH") |
|
256
|
|
|
splitter("Kassel", "MFH") |
|
257
|
|
|
splitter("Chemnitz", "MFH") |
|
258
|
|
|
|
|
259
|
|
|
def pool_normalize(x): |
|
260
|
|
|
""" |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
Parameters |
|
264
|
|
|
---------- |
|
265
|
|
|
x : pandas.Series |
|
266
|
|
|
24-hour profiles of IDP pool |
|
267
|
|
|
|
|
268
|
|
|
Returns |
|
269
|
|
|
------- |
|
270
|
|
|
TYPE : pandas.Series |
|
271
|
|
|
Normalized to their daily total |
|
272
|
|
|
|
|
273
|
|
|
""" |
|
274
|
|
|
if x.sum() != 0: |
|
275
|
|
|
c = x.sum() |
|
276
|
|
|
return x / c |
|
277
|
|
|
else: |
|
278
|
|
|
return x |
|
279
|
|
|
|
|
280
|
|
|
stock = ["MFH", "SFH"] |
|
281
|
|
|
class_list = [2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
282
|
|
|
for s in stock: |
|
283
|
|
|
for m in class_list: |
|
284
|
|
|
df_name = globals()[f"idp_collection_class_{m}_{s}"] |
|
285
|
|
|
globals()[f"idp_collection_class_{m}_{s}_norm"] = df_name.apply( |
|
286
|
|
|
pool_normalize |
|
287
|
|
|
) |
|
288
|
|
|
|
|
289
|
|
|
return [ |
|
290
|
|
|
idp_collection_class_2_SFH_norm, |
|
|
|
|
|
|
291
|
|
|
idp_collection_class_3_SFH_norm, |
|
|
|
|
|
|
292
|
|
|
idp_collection_class_4_SFH_norm, |
|
|
|
|
|
|
293
|
|
|
idp_collection_class_5_SFH_norm, |
|
|
|
|
|
|
294
|
|
|
idp_collection_class_6_SFH_norm, |
|
|
|
|
|
|
295
|
|
|
idp_collection_class_7_SFH_norm, |
|
|
|
|
|
|
296
|
|
|
idp_collection_class_8_SFH_norm, |
|
|
|
|
|
|
297
|
|
|
idp_collection_class_9_SFH_norm, |
|
|
|
|
|
|
298
|
|
|
idp_collection_class_10_SFH_norm, |
|
|
|
|
|
|
299
|
|
|
idp_collection_class_2_MFH_norm, |
|
|
|
|
|
|
300
|
|
|
idp_collection_class_3_MFH_norm, |
|
|
|
|
|
|
301
|
|
|
idp_collection_class_4_MFH_norm, |
|
|
|
|
|
|
302
|
|
|
idp_collection_class_5_MFH_norm, |
|
|
|
|
|
|
303
|
|
|
idp_collection_class_6_MFH_norm, |
|
|
|
|
|
|
304
|
|
|
idp_collection_class_7_MFH_norm, |
|
|
|
|
|
|
305
|
|
|
idp_collection_class_8_MFH_norm, |
|
|
|
|
|
|
306
|
|
|
idp_collection_class_9_MFH_norm, |
|
|
|
|
|
|
307
|
|
|
idp_collection_class_10_MFH_norm, |
|
|
|
|
|
|
308
|
|
|
] |
|
309
|
|
|
|
|
310
|
|
|
|
|
311
|
|
|
def create(): |
|
312
|
|
|
""" |
|
313
|
|
|
Description: Create dataframe with all temprature classes, 24hr. profiles and household stock |
|
314
|
|
|
|
|
315
|
|
|
Returns |
|
316
|
|
|
------- |
|
317
|
|
|
idp_df : pandas.DataFrame |
|
318
|
|
|
All IDP pool as classified as per household stock and temperature class |
|
319
|
|
|
|
|
320
|
|
|
""" |
|
321
|
|
|
idp_list = idp_pool_generator() |
|
322
|
|
|
stock = ["MFH", "SFH"] |
|
323
|
|
|
class_list = [2, 3, 4, 5, 6, 7, 8, 9, 10] |
|
324
|
|
|
idp_df = pd.DataFrame(columns=["idp", "house", "temperature_class"]) |
|
325
|
|
|
for s in stock: |
|
326
|
|
|
for m in class_list: |
|
327
|
|
|
|
|
328
|
|
|
if s == "SFH": |
|
329
|
|
|
i = class_list.index(m) |
|
330
|
|
|
if s == "MFH": |
|
331
|
|
|
i = class_list.index(m) + 9 |
|
332
|
|
|
current_pool = idp_list[i] |
|
|
|
|
|
|
333
|
|
|
idp_df = idp_df.append( |
|
334
|
|
|
pd.DataFrame( |
|
335
|
|
|
data={ |
|
336
|
|
|
"idp": current_pool.transpose().values.tolist(), |
|
337
|
|
|
"house": s, |
|
338
|
|
|
"temperature_class": m, |
|
339
|
|
|
} |
|
340
|
|
|
) |
|
341
|
|
|
) |
|
342
|
|
|
idp_df = idp_df.reset_index(drop=True) |
|
343
|
|
|
|
|
344
|
|
|
idp_df.to_sql( |
|
345
|
|
|
"egon_heat_idp_pool", |
|
346
|
|
|
con=db.engine(), |
|
347
|
|
|
schema="demand", |
|
348
|
|
|
if_exists="replace", |
|
349
|
|
|
index=True, |
|
350
|
|
|
dtype={ |
|
351
|
|
|
"index": Integer(), |
|
352
|
|
|
"idp": ARRAY(Float()), |
|
353
|
|
|
"house": String(), |
|
354
|
|
|
"temperature_class": Integer(), |
|
355
|
|
|
}, |
|
356
|
|
|
) |
|
357
|
|
|
|
|
358
|
|
|
idp_df["idp"] = idp_df.idp.apply(lambda x: np.array(x)) |
|
359
|
|
|
|
|
360
|
|
|
idp_df.idp = idp_df.idp.apply(lambda x: x.astype(np.float32)) |
|
361
|
|
|
|
|
362
|
|
|
return idp_df |
|
363
|
|
|
|
|
364
|
|
|
|
|
365
|
|
|
def annual_demand_generator(): |
|
366
|
|
|
""" |
|
367
|
|
|
|
|
368
|
|
|
Description: Create dataframe with annual demand and household count for each zensus cell |
|
369
|
|
|
Returns |
|
370
|
|
|
------- |
|
371
|
|
|
demand_count: pandas.DataFrame |
|
372
|
|
|
Annual demand of all zensus cell with MFH and SFH count and |
|
373
|
|
|
respective associated Station |
|
374
|
|
|
|
|
375
|
|
|
""" |
|
376
|
|
|
|
|
377
|
|
|
scenario = "eGon2035" |
|
378
|
|
|
demand_zone = db.select_dataframe( |
|
379
|
|
|
f""" |
|
380
|
|
|
SELECT a.demand, a.zensus_population_id, a.scenario, c.climate_zone |
|
381
|
|
|
FROM demand.egon_peta_heat a |
|
382
|
|
|
JOIN boundaries.egon_map_zensus_climate_zones c |
|
383
|
|
|
ON a.zensus_population_id = c.zensus_population_id |
|
384
|
|
|
WHERE a.sector = 'residential' |
|
385
|
|
|
AND a.scenario = '{scenario}' |
|
386
|
|
|
""", |
|
387
|
|
|
index_col="zensus_population_id", |
|
388
|
|
|
) |
|
389
|
|
|
|
|
390
|
|
|
house_count_MFH = db.select_dataframe( |
|
391
|
|
|
""" |
|
392
|
|
|
|
|
393
|
|
|
SELECT cell_id as zensus_population_id, COUNT(*) as number FROM |
|
394
|
|
|
( |
|
395
|
|
|
SELECT cell_id, COUNT(*), building_id |
|
396
|
|
|
FROM demand.egon_household_electricity_profile_of_buildings |
|
397
|
|
|
GROUP BY (cell_id, building_id) |
|
398
|
|
|
) a |
|
399
|
|
|
|
|
400
|
|
|
WHERE a.count >1 |
|
401
|
|
|
GROUP BY cell_id |
|
402
|
|
|
""", |
|
403
|
|
|
index_col="zensus_population_id", |
|
404
|
|
|
) |
|
405
|
|
|
|
|
406
|
|
|
house_count_SFH = db.select_dataframe( |
|
407
|
|
|
""" |
|
408
|
|
|
|
|
409
|
|
|
SELECT cell_id as zensus_population_id, COUNT(*) as number FROM |
|
410
|
|
|
( |
|
411
|
|
|
SELECT cell_id, COUNT(*), building_id |
|
412
|
|
|
FROM demand.egon_household_electricity_profile_of_buildings |
|
413
|
|
|
GROUP BY (cell_id, building_id) |
|
414
|
|
|
) a |
|
415
|
|
|
WHERE a.count = 1 |
|
416
|
|
|
GROUP BY cell_id |
|
417
|
|
|
""", |
|
418
|
|
|
index_col="zensus_population_id", |
|
419
|
|
|
) |
|
420
|
|
|
|
|
421
|
|
|
demand_zone["SFH"] = house_count_SFH.number |
|
422
|
|
|
demand_zone["MFH"] = house_count_MFH.number |
|
423
|
|
|
|
|
424
|
|
|
demand_zone["SFH"].fillna(0, inplace=True) |
|
425
|
|
|
demand_zone["MFH"].fillna(0, inplace=True) |
|
426
|
|
|
|
|
427
|
|
|
return demand_zone |
|
428
|
|
|
|
|
429
|
|
|
|
|
430
|
|
|
def select(): |
|
431
|
|
|
""" |
|
432
|
|
|
|
|
433
|
|
|
Random assignment of intray-day profiles to each day based on their temeprature class |
|
434
|
|
|
and household stock count |
|
435
|
|
|
|
|
436
|
|
|
Returns |
|
437
|
|
|
------- |
|
438
|
|
|
None. |
|
439
|
|
|
|
|
440
|
|
|
""" |
|
441
|
|
|
start_profile_selector = datetime.now() |
|
442
|
|
|
|
|
443
|
|
|
# Drop old table and re-create it |
|
444
|
|
|
engine = db.engine() |
|
445
|
|
|
EgonHeatTimeseries.__table__.drop(bind=engine, checkfirst=True) |
|
446
|
|
|
EgonHeatTimeseries.__table__.create(bind=engine, checkfirst=True) |
|
447
|
|
|
|
|
448
|
|
|
# Select all intra-day-profiles |
|
449
|
|
|
idp_df = db.select_dataframe( |
|
450
|
|
|
""" |
|
451
|
|
|
SELECT index, house, temperature_class |
|
452
|
|
|
FROM demand.egon_heat_idp_pool |
|
453
|
|
|
""", |
|
454
|
|
|
index_col="index", |
|
455
|
|
|
) |
|
456
|
|
|
|
|
457
|
|
|
# Select daily heat demand shares per climate zone from table |
|
458
|
|
|
temperature_classes = db.select_dataframe( |
|
459
|
|
|
""" |
|
460
|
|
|
SELECT climate_zone, day_of_year, temperature_class |
|
461
|
|
|
FROM demand.egon_daily_heat_demand_per_climate_zone |
|
462
|
|
|
""" |
|
463
|
|
|
) |
|
464
|
|
|
|
|
465
|
|
|
# Calculate annual heat demand per census cell |
|
466
|
|
|
annual_demand = annual_demand_generator() |
|
467
|
|
|
|
|
468
|
|
|
# Count number of SFH and MFH per climate zone |
|
469
|
|
|
houses_per_climate_zone = ( |
|
470
|
|
|
annual_demand.groupby("climate_zone")[["SFH", "MFH"]].sum().astype(int) |
|
471
|
|
|
) |
|
472
|
|
|
|
|
473
|
|
|
# Set random seed to make code reproducable |
|
474
|
|
|
np.random.seed( |
|
475
|
|
|
seed=egon.data.config.settings()["egon-data"]["--random-seed"] |
|
476
|
|
|
) |
|
477
|
|
|
|
|
478
|
|
|
for station in houses_per_climate_zone.index: |
|
479
|
|
|
|
|
480
|
|
|
result_SFH = pd.DataFrame(columns=range(1, 366)) |
|
481
|
|
|
result_MFH = pd.DataFrame(columns=range(1, 366)) |
|
482
|
|
|
|
|
483
|
|
|
# Randomly select individual daily demand profile for selected climate zone |
|
484
|
|
|
for day in range(1, 366): |
|
485
|
|
|
t_class = temperature_classes.loc[ |
|
486
|
|
|
(temperature_classes.climate_zone == station) |
|
487
|
|
|
& (temperature_classes.day_of_year == day), |
|
488
|
|
|
"temperature_class", |
|
489
|
|
|
].values[0] |
|
490
|
|
|
|
|
491
|
|
|
result_SFH[day] = np.random.choice( |
|
492
|
|
|
np.array( |
|
493
|
|
|
idp_df[ |
|
494
|
|
|
(idp_df.temperature_class == t_class) |
|
495
|
|
|
& (idp_df.house == "SFH") |
|
496
|
|
|
].index.values |
|
497
|
|
|
), |
|
498
|
|
|
houses_per_climate_zone.loc[station, "SFH"], |
|
499
|
|
|
) |
|
500
|
|
|
|
|
501
|
|
|
result_MFH[day] = np.random.choice( |
|
502
|
|
|
np.array( |
|
503
|
|
|
idp_df[ |
|
504
|
|
|
(idp_df.temperature_class == t_class) |
|
505
|
|
|
& (idp_df.house == "MFH") |
|
506
|
|
|
].index.values |
|
507
|
|
|
), |
|
508
|
|
|
houses_per_climate_zone.loc[station, "MFH"], |
|
509
|
|
|
) |
|
510
|
|
|
|
|
511
|
|
|
result_SFH["zensus_population_id"] = ( |
|
512
|
|
|
annual_demand[annual_demand.climate_zone == station] |
|
513
|
|
|
.loc[ |
|
514
|
|
|
annual_demand[ |
|
515
|
|
|
annual_demand.climate_zone == station |
|
516
|
|
|
].index.repeat( |
|
517
|
|
|
annual_demand[ |
|
518
|
|
|
annual_demand.climate_zone == station |
|
519
|
|
|
].SFH.astype(int) |
|
520
|
|
|
) |
|
521
|
|
|
] |
|
522
|
|
|
.index.values |
|
523
|
|
|
) |
|
524
|
|
|
|
|
525
|
|
|
result_SFH["building_id"] = ( |
|
526
|
|
|
db.select_dataframe( |
|
527
|
|
|
""" |
|
528
|
|
|
|
|
529
|
|
|
SELECT cell_id as zensus_population_id, building_id FROM |
|
530
|
|
|
( |
|
531
|
|
|
SELECT cell_id, COUNT(*), building_id |
|
532
|
|
|
FROM demand.egon_household_electricity_profile_of_buildings |
|
533
|
|
|
GROUP BY (cell_id, building_id) |
|
534
|
|
|
) a |
|
535
|
|
|
WHERE a.count = 1 |
|
536
|
|
|
""", |
|
537
|
|
|
index_col="zensus_population_id", |
|
538
|
|
|
) |
|
539
|
|
|
.loc[result_SFH["zensus_population_id"].unique(), "building_id"] |
|
540
|
|
|
.values |
|
541
|
|
|
) |
|
542
|
|
|
|
|
543
|
|
|
result_MFH["zensus_population_id"] = ( |
|
544
|
|
|
annual_demand[annual_demand.climate_zone == station] |
|
545
|
|
|
.loc[ |
|
546
|
|
|
annual_demand[ |
|
547
|
|
|
annual_demand.climate_zone == station |
|
548
|
|
|
].index.repeat( |
|
549
|
|
|
annual_demand[ |
|
550
|
|
|
annual_demand.climate_zone == station |
|
551
|
|
|
].MFH.astype(int) |
|
552
|
|
|
) |
|
553
|
|
|
] |
|
554
|
|
|
.index.values |
|
555
|
|
|
) |
|
556
|
|
|
|
|
557
|
|
|
result_MFH["building_id"] = ( |
|
558
|
|
|
db.select_dataframe( |
|
559
|
|
|
""" |
|
560
|
|
|
|
|
561
|
|
|
SELECT cell_id as zensus_population_id, building_id FROM |
|
562
|
|
|
( |
|
563
|
|
|
SELECT cell_id, COUNT(*), building_id |
|
564
|
|
|
FROM demand.egon_household_electricity_profile_of_buildings |
|
565
|
|
|
GROUP BY (cell_id, building_id) |
|
566
|
|
|
) a |
|
567
|
|
|
WHERE a.count > 1 |
|
568
|
|
|
""", |
|
569
|
|
|
index_col="zensus_population_id", |
|
570
|
|
|
) |
|
571
|
|
|
.loc[result_MFH["zensus_population_id"].unique(), "building_id"] |
|
572
|
|
|
.values |
|
573
|
|
|
) |
|
574
|
|
|
|
|
575
|
|
|
df_sfh = pd.DataFrame( |
|
576
|
|
|
data={ |
|
577
|
|
|
"selected_idp_profiles": result_SFH[ |
|
578
|
|
|
range(1, 366) |
|
579
|
|
|
].values.tolist(), |
|
580
|
|
|
"zensus_population_id": ( |
|
581
|
|
|
annual_demand[annual_demand.climate_zone == station] |
|
582
|
|
|
.loc[ |
|
583
|
|
|
annual_demand[ |
|
584
|
|
|
annual_demand.climate_zone == station |
|
585
|
|
|
].index.repeat( |
|
586
|
|
|
annual_demand[ |
|
587
|
|
|
annual_demand.climate_zone == station |
|
588
|
|
|
].SFH.astype(int) |
|
589
|
|
|
) |
|
590
|
|
|
] |
|
591
|
|
|
.index.values |
|
592
|
|
|
), |
|
593
|
|
|
"building_id": ( |
|
594
|
|
|
db.select_dataframe( |
|
595
|
|
|
""" |
|
596
|
|
|
|
|
597
|
|
|
SELECT cell_id as zensus_population_id, building_id FROM |
|
598
|
|
|
( |
|
599
|
|
|
SELECT cell_id, COUNT(*), building_id |
|
600
|
|
|
FROM demand.egon_household_electricity_profile_of_buildings |
|
601
|
|
|
GROUP BY (cell_id, building_id) |
|
602
|
|
|
) a |
|
603
|
|
|
WHERE a.count = 1 |
|
604
|
|
|
""", |
|
605
|
|
|
index_col="zensus_population_id", |
|
606
|
|
|
) |
|
607
|
|
|
.loc[ |
|
608
|
|
|
result_SFH["zensus_population_id"].unique(), |
|
609
|
|
|
"building_id", |
|
610
|
|
|
] |
|
611
|
|
|
.values |
|
612
|
|
|
), |
|
613
|
|
|
} |
|
614
|
|
|
) |
|
615
|
|
|
start_sfh = datetime.now() |
|
616
|
|
|
df_sfh.set_index(["zensus_population_id", "building_id"]).to_sql( |
|
617
|
|
|
EgonHeatTimeseries.__table__.name, |
|
618
|
|
|
schema=EgonHeatTimeseries.__table__.schema, |
|
619
|
|
|
con=db.engine(), |
|
620
|
|
|
if_exists="append", |
|
621
|
|
|
chunksize=5000, |
|
622
|
|
|
method="multi", |
|
623
|
|
|
) |
|
624
|
|
|
print(f"SFH insertation for zone {station}:") |
|
625
|
|
|
print(datetime.now() - start_sfh) |
|
626
|
|
|
|
|
627
|
|
|
df_mfh = pd.DataFrame( |
|
628
|
|
|
data={ |
|
629
|
|
|
"selected_idp_profiles": result_MFH[ |
|
630
|
|
|
range(1, 366) |
|
631
|
|
|
].values.tolist(), |
|
632
|
|
|
"zensus_population_id": ( |
|
633
|
|
|
annual_demand[annual_demand.climate_zone == station] |
|
634
|
|
|
.loc[ |
|
635
|
|
|
annual_demand[ |
|
636
|
|
|
annual_demand.climate_zone == station |
|
637
|
|
|
].index.repeat( |
|
638
|
|
|
annual_demand[ |
|
639
|
|
|
annual_demand.climate_zone == station |
|
640
|
|
|
].MFH.astype(int) |
|
641
|
|
|
) |
|
642
|
|
|
] |
|
643
|
|
|
.index.values |
|
644
|
|
|
), |
|
645
|
|
|
"building_id": ( |
|
646
|
|
|
db.select_dataframe( |
|
647
|
|
|
""" |
|
648
|
|
|
|
|
649
|
|
|
SELECT cell_id as zensus_population_id, building_id FROM |
|
650
|
|
|
( |
|
651
|
|
|
SELECT cell_id, COUNT(*), building_id |
|
652
|
|
|
FROM demand.egon_household_electricity_profile_of_buildings |
|
653
|
|
|
GROUP BY (cell_id, building_id) |
|
654
|
|
|
) a |
|
655
|
|
|
WHERE a.count > 1 |
|
656
|
|
|
""", |
|
657
|
|
|
index_col="zensus_population_id", |
|
658
|
|
|
) |
|
659
|
|
|
.loc[ |
|
660
|
|
|
result_MFH["zensus_population_id"].unique(), |
|
661
|
|
|
"building_id", |
|
662
|
|
|
] |
|
663
|
|
|
.values |
|
664
|
|
|
), |
|
665
|
|
|
} |
|
666
|
|
|
) |
|
667
|
|
|
|
|
668
|
|
|
start_mfh = datetime.now() |
|
669
|
|
|
df_mfh.set_index(["zensus_population_id", "building_id"]).to_sql( |
|
670
|
|
|
EgonHeatTimeseries.__table__.name, |
|
671
|
|
|
schema=EgonHeatTimeseries.__table__.schema, |
|
672
|
|
|
con=db.engine(), |
|
673
|
|
|
if_exists="append", |
|
674
|
|
|
chunksize=5000, |
|
675
|
|
|
method="multi", |
|
676
|
|
|
) |
|
677
|
|
|
print(f"MFH insertation for zone {station}:") |
|
678
|
|
|
print(datetime.now() - start_mfh) |
|
679
|
|
|
|
|
680
|
|
|
print("Time for overall profile selection:") |
|
681
|
|
|
print(datetime.now() - start_profile_selector) |
|
682
|
|
|
|