-
Notifications
You must be signed in to change notification settings - Fork 4
/
gas_grid.py
executable file
·492 lines (410 loc) · 14.1 KB
/
gas_grid.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# -*- coding: utf-8 -*-
"""
The central module containing all code dealing with importing data from SciGRID_gas IGGIELGN data
"""
import ast
import json
import os
from pathlib import Path
from urllib.request import urlretrieve
from zipfile import ZipFile
import numpy as np
import geopandas
import pandas as pd
from egon.data import db
from egon.data.config import settings
from egon.data.datasets import Dataset
from geoalchemy2.types import Geometry
from shapely import geometry
class GasNodesandPipes(Dataset):
def __init__(self, dependencies):
super().__init__(
name="GasNodesandPipes",
version="0.0.0",
dependencies=dependencies,
tasks=(insert_gas_data),
)
def download_SciGRID_gas_data():
"""
Download SciGRID_gas IGGIELGN data from Zenodo
"""
path = Path(".") / "datasets" / "gas_data"
os.makedirs(path, exist_ok=True)
basename = "IGGIELGN"
zip_file = Path(".") / "datasets" / "gas_data" / "IGGIELGN.zip"
zenodo_zip_file_url = (
"https://zenodo.org/record/4767098/files/" + basename + ".zip"
)
if not os.path.isfile(zip_file):
urlretrieve(zenodo_zip_file_url, zip_file)
components = [
"Nodes",
"PipeSegments",
"Productions",
"Storages",
] #'Compressors'
files = []
for i in components:
files.append("data/" + basename + "_" + i + ".csv")
with ZipFile(zip_file, "r") as zipObj:
listOfFileNames = zipObj.namelist()
for fileName in listOfFileNames:
if fileName in files:
zipObj.extract(fileName, path)
def define_gas_nodes_list():
"""Define list of gas nodes from SciGRID_gas IGGIELGN data
Returns
-------
gas_nodes_list : dataframe
Dataframe containing the gas nodes (Europe)
"""
# Select next id value
new_id = db.next_etrago_id("bus")
target_file = (
Path(".") / "datasets" / "gas_data" / "data" / "IGGIELGN_Nodes.csv"
)
gas_nodes_list = pd.read_csv(
target_file,
delimiter=";",
decimal=".",
usecols=["lat", "long", "id", "country_code", "param"],
)
# Ajouter tri pour ne conserver que les pays ayant des pipelines en commun.
gas_nodes_list = gas_nodes_list.rename(columns={"lat": "y", "long": "x"})
# Remove buses disconnected of the rest of the grid, until the SciGRID_gas data has been corrected.
gas_nodes_list = gas_nodes_list[
~gas_nodes_list["id"].str.match("SEQ_11790_p")
]
gas_nodes_list = gas_nodes_list[
~gas_nodes_list["id"].str.match("Stor_EU_107")
]
gas_nodes_list["bus_id"] = range(new_id, new_id + len(gas_nodes_list))
gas_nodes_list = gas_nodes_list.set_index("id")
return gas_nodes_list
def ch4_nodes_number_G(gas_nodes_list):
"""Insert list of CH4 nodes from SciGRID_gas IGGIELGN data
Parameters
----------
gas_nodes_list : dataframe
Dataframe containing the gas nodes (Europe)
Returns
-------
N_ch4_nodes_G : int
Number of CH4 buses in Germany (independantly from the mode used)
"""
ch4_nodes_list = gas_nodes_list[
gas_nodes_list["country_code"].str.match("DE")
] # A remplacer evtmt par un test sur le NUTS0 ?
N_ch4_nodes_G = len(ch4_nodes_list)
return N_ch4_nodes_G
def insert_CH4_nodes_list(gas_nodes_list):
"""Insert list of CH4 nodes from SciGRID_gas IGGIELGN data
Parameters
----------
gas_nodes_list : dataframe
Dataframe containing the gas nodes (Europe)
Returns
-------
None
"""
# Connect to local database
engine = db.engine()
gas_nodes_list = gas_nodes_list[
gas_nodes_list["country_code"].str.match("DE")
] # A remplacer evtmt par un test sur le NUTS0 ?
# Cut data to federal state if in testmode
NUTS1 = []
for index, row in gas_nodes_list.iterrows():
param = ast.literal_eval(row["param"])
NUTS1.append(param["nuts_id_1"])
gas_nodes_list = gas_nodes_list.assign(NUTS1=NUTS1)
boundary = settings()["egon-data"]["--dataset-boundary"]
if boundary != "Everything":
map_states = {
"Baden-Württemberg": "DE1",
"Nordrhein-Westfalen": "DEA",
"Hessen": "DE7",
"Brandenburg": "DE4",
"Bremen": "DE5",
"Rheinland-Pfalz": "DEB",
"Sachsen-Anhalt": "DEE",
"Schleswig-Holstein": "DEF",
"Mecklenburg-Vorpommern": "DE8",
"Thüringen": "DEG",
"Niedersachsen": "DE9",
"Sachsen": "DED",
"Hamburg": "DE6",
"Saarland": "DEC",
"Berlin": "DE3",
"Bayern": "DE2",
}
gas_nodes_list = gas_nodes_list[
gas_nodes_list["NUTS1"].isin([map_states[boundary], np.nan])
]
# A completer avec nodes related to pipelines which have an end in the selected area et evt deplacer ds define_gas_nodes_list
# Add missing columns
c = {"scn_name": "eGon2035", "carrier": "CH4"}
gas_nodes_list = gas_nodes_list.assign(**c)
gas_nodes_list = geopandas.GeoDataFrame(
gas_nodes_list,
geometry=geopandas.points_from_xy(
gas_nodes_list["x"], gas_nodes_list["y"]
),
)
gas_nodes_list = gas_nodes_list.rename(
columns={"geometry": "geom"}
).set_geometry("geom", crs=4326)
gas_nodes_list = gas_nodes_list.reset_index(drop=True)
gas_nodes_list = gas_nodes_list.drop(
columns=["NUTS1", "param", "country_code"]
)
# Insert data to db
db.execute_sql(
"""
DELETE FROM grid.egon_etrago_bus WHERE "carrier" = 'CH4';
DELETE FROM grid.egon_etrago_bus WHERE "carrier" = 'H2';
"""
)
# Insert CH4 data to db
print(gas_nodes_list)
gas_nodes_list.to_postgis(
"egon_etrago_bus",
engine,
schema="grid",
index=False,
if_exists="append",
dtype={"geom": Geometry()},
)
def insert_H2_nodes_list():
"""Insert the H2 buses to db, same buses than the CH4 buses
Returns
-------
None.
"""
# Connect to local database
engine = db.engine()
# Select the CH4 buses
sql_CH4 = """SELECT bus_id, scn_name, geom
FROM grid.egon_etrago_bus
WHERE carrier = 'CH4';"""
gdf_H2 = db.select_geodataframe(sql_CH4, epsg=4326)
# Select next id value
new_id = db.next_etrago_id("bus")
gdf_H2["carrier"] = "H2"
gdf_H2["bus_id"] = range(new_id, new_id + len(gdf_H2))
# Insert H2 data to db
print(gdf_H2)
gdf_H2.to_postgis(
"egon_etrago_bus",
engine,
schema="grid",
index=False,
if_exists="append",
dtype={"geom": Geometry()},
)
def insert_gas_pipeline_list(gas_nodes_list):
"""Insert list of gas pipelines from SciGRID_gas IGGIELGN data
Parameters
----------
gas_nodes_list : dataframe
Dataframe containing the gas nodes (Europe)
Returns
-------
None.
"""
# Connect to local database
engine = db.engine()
# Select next id value
new_id = db.next_etrago_id("link")
classifiaction_file = (
Path(".")
/ "data_bundle_egon_data"
/ "pipeline_classification_gas"
/ "pipeline_classification.csv"
)
classification = pd.read_csv(
classifiaction_file,
delimiter=",",
usecols=["classification", "max_transport_capacity_Gwh/d"],
)
target_file = (
Path(".")
/ "datasets"
/ "gas_data"
/ "data"
/ "IGGIELGN_PipeSegments.csv"
)
gas_pipelines_list = pd.read_csv(
target_file,
delimiter=";",
decimal=".",
usecols=["id", "node_id", "lat", "long", "country_code", "param"],
)
# Select the links having at least one bus in Germany
gas_pipelines_list = gas_pipelines_list[
gas_pipelines_list["country_code"].str.contains("DE")
] # A remplacer evtmt par un test sur le NUTS0 ?
# Remove links disconnected of the rest of the grid, until the SciGRID_gas data has been corrected.
gas_pipelines_list = gas_pipelines_list[
~gas_pipelines_list["id"].str.match("EntsoG_Map__ST_195")
]
gas_pipelines_list = gas_pipelines_list[
~gas_pipelines_list["id"].str.match("EntsoG_Map__ST_5")
]
gas_pipelines_list["link_id"] = range(
new_id, new_id + len(gas_pipelines_list)
)
gas_pipelines_list["link_id"] = gas_pipelines_list["link_id"].astype(int)
# Cut data to federal state if in testmode
NUTS1 = []
for index, row in gas_pipelines_list.iterrows():
param = ast.literal_eval(row["param"])
NUTS1.append(param["nuts_id_1"])
gas_pipelines_list["NUTS1"] = NUTS1
boundary = settings()["egon-data"]["--dataset-boundary"]
if boundary != "Everything":
map_states = {
"Baden-Württemberg": "DE1",
"Nordrhein-Westfalen": "DEA",
"Hessen": "DE7",
"Brandenburg": "DE4",
"Bremen": "DE5",
"Rheinland-Pfalz": "DEB",
"Sachsen-Anhalt": "DEE",
"Schleswig-Holstein": "DEF",
"Mecklenburg-Vorpommern": "DE8",
"Thüringen": "DEG",
"Niedersachsen": "DE9",
"Sachsen": "DED",
"Hamburg": "DE6",
"Saarland": "DEC",
"Berlin": "DE3",
"Bayern": "DE2",
}
gas_pipelines_list["NUTS1"] = [
x[0] for x in gas_pipelines_list["NUTS1"]
]
gas_pipelines_list = gas_pipelines_list[
gas_pipelines_list["NUTS1"].str.contains(map_states[boundary])
]
# A completer avec nodes related to pipelines which have an end in the selected area
# Add missing columns
gas_pipelines_list["scn_name"] = "eGon2035"
gas_pipelines_list["carrier"] = "CH4"
diameter = []
length = []
geom = []
topo = []
for index, row in gas_pipelines_list.iterrows():
param = ast.literal_eval(row["param"])
diameter.append(param["diameter_mm"])
length.append(param["length_km"])
long_e = json.loads(row["long"])
lat_e = json.loads(row["lat"])
crd_e = list(zip(long_e, lat_e))
topo.append(geometry.LineString(crd_e))
long_path = param["path_long"]
lat_path = param["path_lat"]
crd = list(zip(long_path, lat_path))
crd.insert(0, crd_e[0])
crd.append(crd_e[1])
lines = []
for i in range(len(crd) - 1):
lines.append(geometry.LineString([crd[i], crd[i + 1]]))
geom.append(geometry.MultiLineString(lines))
print(topo)
gas_pipelines_list["diameter"] = diameter
gas_pipelines_list["length"] = length
gas_pipelines_list["geom"] = geom
gas_pipelines_list["topo"] = topo
gas_pipelines_list = gas_pipelines_list.set_geometry("geom", crs=4326)
# Adjust columns
bus0 = []
bus1 = []
pipe_class = []
for index, row in gas_pipelines_list.iterrows():
buses = row["node_id"].strip("][").split(", ")
bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"])
bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"])
if row["diameter"] >= 1000:
pipe_class = "A"
elif 700 <= row["diameter"] <= 1000:
pipe_class = "B"
elif 500 <= row["diameter"] <= 700:
pipe_class = "C"
elif 350 <= row["diameter"] <= 500:
pipe_class = "D"
elif 200 <= row["diameter"] <= 350:
pipe_class = "E"
elif 100 <= row["diameter"] <= 200:
pipe_class = "F"
elif row["diameter"] <= 100:
pipe_class = "G"
gas_pipelines_list["bus0"] = bus0
gas_pipelines_list["bus1"] = bus1
gas_pipelines_list["pipe_class"] = pipe_class
gas_pipelines_list = gas_pipelines_list.merge(
classification,
how="left",
left_on="pipe_class",
right_on="classification",
)
gas_pipelines_list["p_nom"] = gas_pipelines_list[
"max_transport_capacity_Gwh/d"
] * (1000 / 24)
gas_pipelines_list = gas_pipelines_list.drop(
columns=[
"id",
"node_id",
"param",
"NUTS1",
"country_code",
"diameter",
"pipe_class",
"classification",
"max_transport_capacity_Gwh/d",
"lat",
"long",
]
)
# Insert data to db
db.execute_sql(
"""DELETE FROM grid.egon_etrago_link WHERE "carrier" = 'CH4';
"""
)
gas_pipelines_list.to_postgis(
"egon_etrago_gas_link",
engine,
schema="grid",
index=False,
if_exists="replace",
dtype={"geom": Geometry(), "topo": Geometry()},
)
db.execute_sql(
"""
select UpdateGeometrySRID('grid', 'egon_etrago_gas_link', 'topo', 4326) ;
INSERT INTO grid.egon_etrago_link (scn_name,
link_id, carrier,
bus0, bus1,
p_nom, length,
geom, topo)
SELECT scn_name,
link_id, carrier,
bus0, bus1,
p_nom, length,
geom, topo
FROM grid.egon_etrago_gas_link;
DROP TABLE grid.egon_etrago_gas_link;
"""
)
def insert_gas_data():
"""Overall function for importing gas data from SciGRID_gas
Returns
-------
None.
"""
download_SciGRID_gas_data()
gas_nodes_list = define_gas_nodes_list()
insert_CH4_nodes_list(gas_nodes_list)
insert_H2_nodes_list()
insert_gas_pipeline_list(gas_nodes_list)