Passed
Pull Request — dev (#568)
by
unknown
02:18
created

create_H2_CH4_table()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nop 0
1
"""The central module containing all code dealing with heat sector in etrago
2
"""
3
import datetime
4
import json
5
6
from geoalchemy2 import Geometry
7
from sqlalchemy import BigInteger, Column, Text
8
from sqlalchemy.ext.declarative import declarative_base
9
10
from egon.data import config, db
11
from egon.data.datasets.etrago_helpers import (
12
    copy_and_modify_buses,
13
    finalize_bus_insertion,
14
    initialise_bus_insertion,
15
)
16
from egon.data.metadata import (
17
    context,
18
    contributors,
19
    license_egon_data_odbl,
20
    meta_metadata,
21
    sources,
22
)
23
24
25
def insert_hydrogen_buses(scenario="eGon2035"):
26
    """Insert hydrogen buses to etrago table
27
28
    Hydrogen buses are divided into cavern and methane grid attached buses
29
30
    Parameters
31
    ----------
32
    scenario : str, optional
33
        Name of the scenario The default is 'eGon2035'.
34
35
    """
36
    sources = config.datasets()["etrago_hydrogen"]["sources"]
37
    target = config.datasets()["etrago_hydrogen"]["targets"]["hydrogen_buses"]
38
    # initalize dataframe for hydrogen buses
39
    carrier = "H2_saltcavern"
40
    hydrogen_buses = initialise_bus_insertion(
41
        carrier, target, scenario=scenario
42
    )
43
    insert_H2_buses_from_saltcavern(
44
        hydrogen_buses, carrier, sources, target, scenario
45
    )
46
47
    carrier = "H2_grid"
48
    hydrogen_buses = initialise_bus_insertion(
49
        carrier, target, scenario=scenario
50
    )
51
    insert_H2_buses_from_CH4_grid(hydrogen_buses, carrier, target, scenario)
52
53
54
Base = declarative_base()
55
56
57 View Code Duplication
class EgonMapACH2(Base):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
58
    source_list = [
59
        sources()["openstreetmap"],
60
        sources()["SciGRID_gas"],
61
        sources()["bgr_inspeeds_data_bundle"],
62
    ]
63
    meta_ac_h2 = {
64
        "name": "grid.egon_etrago_ac_h2",
65
        "title": "Mapping table of AC-H2 buses",
66
        "id": "WILL_BE_SET_AT_PUBLICATION",
67
        "description": "Table mapping AC and H2 buses in Germany",
68
        "language": ["en-EN"],
69
        "publicationDate": datetime.date.today().isoformat(),
70
        "context": context(),
71
        "spatial": {
72
            "location": None,
73
            "extent": "Germany",
74
            "resolution": None,
75
        },
76
        "sources": source_list,
77
        "licenses": [license_egon_data_odbl()],
78
        "contributors": contributors(["fw"]),
79
        "resources": [
80
            {
81
                "profile": "tabular-data-resource",
82
                "name": "grid.egon_etrago_ac_h2",
83
                "path": None,
84
                "format": "PostgreSQL",
85
                "encoding": "UTF-8",
86
                "schema": {
87
                    "fields": [
88
                        {
89
                            "name": "scn_name",
90
                            "description": "Name of the scenario",
91
                            "type": "str",
92
                            "unit": None,
93
                        },
94
                        {
95
                            "name": "bus_H2",
96
                            "description": "H2 bus_id",
97
                            "type": "integer",
98
                            "unit": None,
99
                        },
100
                        {
101
                            "name": "bus_AC",
102
                            "description": "AC bus_id",
103
                            "type": "integer",
104
                            "unit": None,
105
                        },
106
                    ],
107
                    "primaryKey": ["scn_name", "bus_H2"],
108
                    "foreignKeys": [],
109
                },
110
                "dialect": {"delimiter": None, "decimalSeparator": "."},
111
            }
112
        ],
113
        "metaMetadata": meta_metadata(),
114
    }
115
    # Create json dump
116
    meta_json_ac_h2 = (
117
        "'" + json.dumps(meta_ac_h2, indent=4, ensure_ascii=False) + "'"
118
    )
119
120
    __tablename__ = "egon_etrago_ac_h2"
121
    __table_args__ = {
122
        "schema": "grid",
123
        "comment": meta_json_ac_h2,
124
    }
125
126
    scn_name = Column(Text, primary_key=True, nullable=False)
127
    bus_H2 = Column(BigInteger, primary_key=True, nullable=False)
128
    bus_AC = Column(BigInteger, primary_key=False, nullable=False)
129
130
131
def create_AC_H2_table():
132
    engine = db.engine()
133
    EgonMapACH2.__table__.drop(bind=engine, checkfirst=True)
134
    EgonMapACH2.__table__.create(bind=engine, checkfirst=True)
135
136
137
def insert_H2_buses_from_saltcavern(gdf, carrier, sources, target, scn_name):
138
    """Insert the H2 buses based saltcavern locations to db.
139
140
    Parameters
141
    ----------
142
    gdf : geopandas.GeoDataFrame
143
        GeoDataFrame containing the empty bus data.
144
    carrier : str
145
        Name of the carrier.
146
    sources : dict
147
        Sources schema and table information.
148
    target : dict
149
        Target schema and table information.
150
    scn_name : str
151
        Name of the scenario.
152
    """
153
    # electrical buses related to saltcavern storage
154
    el_buses = db.select_dataframe(
155
        f"""
156
        SELECT bus_id
157
        FROM  {sources['saltcavern_data']['schema']}.
158
        {sources['saltcavern_data']['table']}"""
159
    )["bus_id"]
160
161
    # locations of electrical buses (filtering not necessarily required)
162
    locations = db.select_geodataframe(
163
        f"""
164
        SELECT bus_id, geom
165
        FROM  {sources['buses']['schema']}.
166
        {sources['buses']['table']} WHERE scn_name = '{scn_name}'
167
        AND country = 'DE'""",
168
        index_col="bus_id",
169
    ).to_crs(epsg=4326)
170
171
    # filter by related electrical buses and drop duplicates
172
    locations = locations.loc[el_buses]
173
    locations = locations[~locations.index.duplicated(keep="first")]
174
175
    # AC bus ids and respective hydrogen bus ids are written to db for
176
    # later use (hydrogen storage mapping)
177
    AC_bus_ids = locations.index.copy()
178
179
    # create H2 bus data
180
    hydrogen_bus_ids = finalize_bus_insertion(
181
        locations, carrier, target, scenario=scn_name
182
    )
183
184
    gdf_H2_cavern = hydrogen_bus_ids[["bus_id"]].rename(
185
        columns={"bus_id": "bus_H2"}
186
    )
187
    gdf_H2_cavern["bus_AC"] = AC_bus_ids
188
    gdf_H2_cavern["scn_name"] = hydrogen_bus_ids["scn_name"]
189
190
    create_AC_H2_table()
191
192
    # Insert data to db
193
    gdf_H2_cavern.to_sql(
194
        "egon_etrago_ac_h2",
195
        db.engine(),
196
        schema="grid",
197
        index=False,
198
        if_exists="append",
199
    )
200
201
202 View Code Duplication
class EgonMapH2CH4(Base):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
203
    source_list = [
204
        sources()["openstreetmap"],
205
        sources()["SciGRID_gas"],
206
        sources()["bgr_inspeeds_data_bundle"],
207
    ]
208
    meta_H2_CH4 = {
209
        "name": "grid.egon_etrago_ch4_h2",
210
        "title": "Mapping table of CH4-H2 buses",
211
        "id": "WILL_BE_SET_AT_PUBLICATION",
212
        "description": "Table mapping CH4 and H2 buses in Germany",
213
        "language": ["en-EN"],
214
        "publicationDate": datetime.date.today().isoformat(),
215
        "context": context(),
216
        "spatial": {
217
            "location": None,
218
            "extent": "Germany",
219
            "resolution": None,
220
        },
221
        "sources": source_list,
222
        "licenses": [license_egon_data_odbl()],
223
        "contributors": contributors(["fw"]),
224
        "resources": [
225
            {
226
                "profile": "tabular-data-resource",
227
                "name": "grid.egon_etrago_ch4_h2",
228
                "path": None,
229
                "format": "PostgreSQL",
230
                "encoding": "UTF-8",
231
                "schema": {
232
                    "fields": [
233
                        {
234
                            "name": "scn_name",
235
                            "description": "Name of the scenario",
236
                            "type": "str",
237
                            "unit": None,
238
                        },
239
                        {
240
                            "name": "bus_H2",
241
                            "description": "H2 bus_id",
242
                            "type": "integer",
243
                            "unit": None,
244
                        },
245
                        {
246
                            "name": "bus_CH4",
247
                            "description": "CH4 bus_id",
248
                            "type": "integer",
249
                            "unit": None,
250
                        },
251
                    ],
252
                    "primaryKey": ["scn_name", "bus_H2"],
253
                    "foreignKeys": [],
254
                },
255
                "dialect": {"delimiter": None, "decimalSeparator": "."},
256
            }
257
        ],
258
        "metaMetadata": meta_metadata(),
259
    }
260
261
    # Create json dump
262
    meta_json_H2_CH4 = (
263
        "'" + json.dumps(meta_H2_CH4, indent=4, ensure_ascii=False) + "'"
264
    )
265
266
    __tablename__ = "egon_etrago_ch4_h2"
267
    __table_args__ = {
268
        "schema": "grid",
269
        "comment": meta_json_H2_CH4,
270
    }
271
272
    scn_name = Column(Text, primary_key=True, nullable=False)
273
    bus_H2 = Column(BigInteger, primary_key=True, nullable=False)
274
    bus_CH4 = Column(BigInteger, primary_key=False, nullable=False)
275
276
277
def create_H2_CH4_table():
278
    engine = db.engine()
279
    EgonMapH2CH4.__table__.drop(bind=engine, checkfirst=True)
280
    EgonMapH2CH4.__table__.create(bind=engine, checkfirst=True)
281
282
283
def insert_H2_buses_from_CH4_grid(gdf, carrier, target, scn_name):
284
    """Insert the H2 buses based on CH4 grid to db.
285
286
    Parameters
287
    ----------
288
    gdf : geopandas.GeoDataFrame
289
        GeoDataFrame containing the empty bus data.
290
    carrier : str
291
        Name of the carrier.
292
    target : dict
293
        Target schema and table information.
294
    scn_name : str
295
        Name of the scenario.
296
297
    """
298
    # Connect to local database
299
    engine = db.engine()
300
301
    # Select the CH4 buses
302
    sql_CH4 = f"""SELECT bus_id, scn_name, geom
303
                 FROM grid.egon_etrago_bus
304
                 WHERE carrier = 'CH4' AND scn_name = '{scn_name}'
305
                 AND country = 'DE';"""
306
307
    gdf_H2 = db.select_geodataframe(sql_CH4, epsg=4326)
308
    # CH4 bus ids and respective hydrogen bus ids are written to db for
309
    # later use (CH4 grid to H2 links)
310
    CH4_bus_ids = gdf_H2[["bus_id", "scn_name"]].copy()
311
312
    H2_bus_ids = finalize_bus_insertion(
313
        gdf_H2, carrier, target, scenario=scn_name
314
    )
315
316
    gdf_H2_CH4 = H2_bus_ids[["bus_id"]].rename(columns={"bus_id": "bus_H2"})
317
    gdf_H2_CH4["bus_CH4"] = CH4_bus_ids["bus_id"]
318
    gdf_H2_CH4["scn_name"] = CH4_bus_ids["scn_name"]
319
320
    create_H2_CH4_table()
321
322
    # Insert data to db
323
    gdf_H2_CH4.to_sql(
324
        "egon_etrago_ch4_h2",
325
        engine,
326
        schema="grid",
327
        index=False,
328
        if_exists="append",
329
    )
330
331
332
def insert_hydrogen_buses_eGon100RE():
333
    """Copy H2 buses from the eGon2035 to the eGon100RE scenario."""
334
    copy_and_modify_buses(
335
        "eGon2035",
336
        "eGon100RE",
337
        {"carrier": ["H2_grid", "H2_saltcavern"]},
338
    )
339