-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage.py
328 lines (277 loc) · 11.2 KB
/
storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""The central module containing all code dealing with heat sector in etrago
"""
from geoalchemy2 import Geometry
import geopandas as gpd
import pandas as pd
from egon.data import config, db
from egon.data.datasets import Dataset
def insert_H2_overground_storage():
"""Insert H2 steel tank storage for every H2 bus."""
# The targets of etrago_hydrogen also serve as source here ಠ_ಠ
sources = config.datasets()["etrago_hydrogen"]["sources"]
targets = config.datasets()["etrago_hydrogen"]["targets"]
# Place storage at every H2 bus
storages = db.select_geodataframe(
f"""
SELECT bus_id, scn_name, geom
FROM {sources['buses']['schema']}.
{sources['buses']['table']} WHERE carrier LIKE 'H2%%'""",
index_col="bus_id",
)
carrier = "H2_overground"
# Add missing column
storages["bus"] = storages.index
storages["carrier"] = carrier
# Does e_nom_extenable = True render e_nom useless?
storages["e_nom"] = 0
storages["e_nom_extendable"] = True
# "Synergies of sector coupling and transmission reinforcement in a cost-optimised, highly renewable European energy system", p.4
storages["capital_cost"] = 8.4 * 1e3
# Remove useless columns
storages.drop(columns=["geom"], inplace=True)
# Clean table
db.execute_sql(
f"""
DELETE FROM grid.egon_etrago_store WHERE "carrier" = '{carrier}';
"""
)
# Select next id value
new_id = db.next_etrago_id("store")
storages["store_id"] = range(new_id, new_id + len(storages))
storages = storages.reset_index(drop=True)
# Insert data to db
storages.to_sql(
targets["hydrogen_stores"]["table"],
db.engine(),
schema=targets["hydrogen_stores"]["schema"],
index=False,
if_exists="append",
)
def insert_H2_saltcavern_storage():
"""Insert H2 saltcavern storage for every H2_saltcavern bus in the table."""
# Datatables sources and targets
sources = config.datasets()["etrago_hydrogen"]["sources"]
targets = config.datasets()["etrago_hydrogen"]["targets"]
# Place storage at every H2 bus
storage_potentials = db.select_geodataframe(
f"""
SELECT *
FROM {sources['saltcavern_data']['schema']}.
{sources['saltcavern_data']['table']}""",
geom_col="geometry"
)
H2_AC_bus_map = db.select_dataframe(
f"""
SELECT *
FROM {sources['H2_AC_map']['schema']}.
{sources['H2_AC_map']['table']}""",
)
storage_potentials["storage_potential"] = (
storage_potentials["area_fraction"] * storage_potentials["potential"]
)
# print(storage_potentials["storage_potential"])
storage_potentials["summed_potential_per_bus"] = storage_potentials.groupby("bus_id")["storage_potential"].transform("sum")
storages = storage_potentials[["summed_potential_per_bus", "bus_id"]].copy()
storages.drop_duplicates('bus_id', keep='last', inplace=True)
# map AC buses in potetial data to respective H2 buses
storages = storages.merge(
H2_AC_bus_map, left_on='bus_id', right_on='bus_AC'
).reindex(columns=['bus_H2', 'summed_potential_per_bus', 'scn_name'])
# rename columns
storages.rename(
columns={'bus_H2': 'bus', 'summed_potential_per_bus': 'e_nom_max'},
inplace=True
)
# add missing columns
carrier = "H2_underground"
storages["carrier"] = carrier
storages["e_nom"] = 0
storages["e_nom_extendable"] = True
# capital cost needs update, also see respective redmine issue
storages["capital_cost"] = 8.4 * 1e3
# Clean table
db.execute_sql(
f"""
DELETE FROM grid.egon_etrago_store WHERE "carrier" = '{carrier}';
"""
)
# Select next id value
new_id = db.next_etrago_id("store")
storages["store_id"] = range(new_id, new_id + len(storages))
storages = storages.reset_index(drop=True)
# # Insert data to db
storages.to_sql(
targets['hydrogen_stores']['table'],
db.engine(),
schema=targets['hydrogen_stores']['schema'],
index=False,
if_exists="append",
)
def calculate_and_map_saltcavern_storage_potential():
"""Calculate site specific storage potential based on InSpEE-DS report."""
# select onshore vg250 data
sources = config.datasets()["bgr"]["sources"]
targets = config.datasets()["bgr"]["targets"]
vg250_data = db.select_geodataframe(
f"""SELECT * FROM
{sources['vg250_federal_states']['schema']}.
{sources['vg250_federal_states']['table']}
WHERE gf = '4'""",
index_col="id",
geom_col="geometry",
)
# get saltcavern shapes
saltcavern_data = db.select_geodataframe(
f"""SELECT * FROM
{sources['saltcaverns']['schema']}.
{sources['saltcaverns']['table']}
""",
geom_col="geometry",
)
# hydrogen storage potential data from InSpEE-DS report
hydrogen_storage_potential = pd.DataFrame(
columns=["INSPEEDS", "INSPEE"]
)
# values in MWh, modified to fit the saltstructure data
hydrogen_storage_potential.loc["Brandenburg"] = [353e6, 159e6]
hydrogen_storage_potential.loc["Mecklenburg-Vorpommern"] = [25e6, 193e6]
hydrogen_storage_potential.loc["Nordrhein-Westfalen"] = [168e6, 0]
hydrogen_storage_potential.loc["Sachsen-Anhalt"] = [318e6, 147e6]
hydrogen_storage_potential.loc["Thüringen"] = [595e6, 0]
# distribute SH/HH and NDS/HB potentials by area
# overlay saltstructures with federal state, calculate respective area
# map storage potential per federal state to area fraction of summed area
# potential_i = area_i / area_tot * potential_tot
pot_nds_hb = [253e6, 702e6]
pot_sh_hh = [0, 413e6]
potential_data_dict = {
0: {
"federal_states": ["Schleswig-Holstein", "Hamburg"],
"INSPEEDS": 0, "INSPEE": 413e6
},
1: {
"federal_states": ["Niedersachsen", "Bremen"],
"INSPEEDS": 253e6, "INSPEE": 702e6
}
}
# iterate over aggregated state data for SH/HH and NDS/HB
for data in potential_data_dict.values():
individual_areas = {}
# individual state areas
for federal_state in data["federal_states"]:
print(vg250_data[vg250_data["gen"] == federal_state])
print(saltcavern_data)
try:
individual_areas[federal_state] = saltcavern_data.overlay(
vg250_data[vg250_data["gen"] == federal_state],
how="intersection"
).to_crs(epsg=25832).area.sum()
except ValueError:
individual_areas[federal_state]=0
# derives weights from fraction of individual state area to total area
total_area = sum(individual_areas.values())
weights = {
f: individual_areas[f] / total_area if total_area > 0 else 0
for f in data["federal_states"]
}
# write data into potential dataframe
for federal_state in data["federal_states"]:
hydrogen_storage_potential.loc[federal_state] = (
[
data["INSPEEDS"] * weights[federal_state],
data["INSPEE"] * weights[federal_state]
]
)
# calculate total storage potential
hydrogen_storage_potential["total"] = (
# currently only InSpEE saltstructure shapefiles are available
# hydrogen_storage_potential["INSPEEDS"]
hydrogen_storage_potential["INSPEE"]
)
saltcaverns_in_fed_state = gpd.GeoDataFrame()
# intersection of saltstructures with federal state
for federal_state in hydrogen_storage_potential.index:
federal_state_data = vg250_data[vg250_data["gen"] == federal_state]
# skip if federal state not available (e.g. local testing)
if federal_state_data.size > 0:
saltcaverns_in_fed_state = saltcaverns_in_fed_state.append(
saltcavern_data.overlay(federal_state_data, how="intersection")
)
# write total potential in column, will be overwritten by actual
# value later
saltcaverns_in_fed_state.loc[
saltcaverns_in_fed_state["gen"] == federal_state,
"potential"
] = hydrogen_storage_potential.loc[federal_state, "total"]
# drop all federal state data columns except name of the state
saltcaverns_in_fed_state.drop(
columns=[
col
for col in federal_state_data.columns
if col not in ["gen", "geometry"]
],
inplace=True,
)
# this is required for the first loop as no geometry has been set
# prior to this, also set crs to match original saltcavern_data crs
saltcaverns_in_fed_state.set_geometry("geometry")
saltcaverns_in_fed_state.set_crs(saltcavern_data.crs, inplace=True)
saltcaverns_in_fed_state.to_crs(epsg=4326, inplace=True)
# recalculate area in case structures have been split at federal
# state borders in original data epsg
# mapping of potential to individual H2 storage is in
# hydrogen_etrago/storage.py
saltcaverns_in_fed_state["shape_star"] = saltcaverns_in_fed_state.to_crs(
epsg=25832
).area
# get substation voronois
substation_voronoi = db.select_geodataframe(
f"""
SELECT * FROM grid.egon_hvmv_substation_voronoi
""",
index_col="bus_id",
).to_crs(4326).sort_index()
# get substations
substations = db.select_geodataframe(
f"""
SELECT * FROM grid.egon_hvmv_substation""",
geom_col="point",
index_col="bus_id",
).to_crs(4326)
# create 500 m radius around substations as storage potential area
# epsg for buffer in line with original saltstructre data
substations_inflation = gpd.GeoDataFrame(
geometry=substations.to_crs(25832).buffer(500).to_crs(4326)
).sort_index()
# !!row wise!! intersection between the substations inflation and the
# respective voronoi (overlay only allows for intersection to all
# voronois)
voroni_buffer_intersect = substations_inflation["geometry"].intersection(
substation_voronoi["geom"]
)
# make intersection a dataframe to kepp bus_id column in potential area
# overlay
voroni_buffer_intersect = gpd.GeoDataFrame(
{
"bus_id": voroni_buffer_intersect.index.tolist(),
"geometry": voroni_buffer_intersect.geometry.tolist(),
}
).set_crs(epsg=4326)
# overlay saltstructures with substation buffer
potential_areas = saltcaverns_in_fed_state.overlay(
voroni_buffer_intersect, how="intersection"
).set_crs(epsg=4326)
# calculate area fraction of individual site over total area within
# the same federal state
potential_areas["area_fraction"] = potential_areas.to_crs(
epsg=25832
).area / potential_areas.groupby("gen")["shape_star"].transform("sum")
# write information to saltcavern data
potential_areas.to_crs(epsg=4326).to_postgis(
targets["storage_potential"]["table"],
db.engine(),
schema=targets["storage_potential"]["schema"],
index=True,
if_exists="replace",
dtype={"geometry": Geometry()},
)