Passed
Pull Request — dev (#894)
by
unknown
03:07 queued 01:37
created

data.datasets.chp_etrago.insert_egon100re()   B

Complexity

Conditions 1

Size

Total Lines 103
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 103
rs 8.824
c 0
b 0
f 0
cc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"""
2
The central module containing all code dealing with chp for eTraGo.
3
"""
4
5
import geopandas as gpd
6
import pandas as pd
7
8
from egon.data import config, db
9
from egon.data.datasets import Dataset
10
from egon.data.datasets.etrago_setup import link_geom_from_buses
11
from egon.data.datasets.scenario_parameters import get_sector_parameters
12
13
14
class ChpEtrago(Dataset):
15
    def __init__(self, dependencies):
16
        super().__init__(
17
            name="ChpEtrago",
18
            version="0.0.6",
19
            dependencies=dependencies,
20
            tasks=(insert),
21
        )
22
23
24
def insert_egon100re():
25
    sources = config.datasets()["chp_etrago"]["sources"]
26
27
    targets = config.datasets()["chp_etrago"]["targets"]
28
29
    db.execute_sql(
30
        f"""
31
        DELETE FROM {targets['link']['schema']}.{targets['link']['table']}
32
        WHERE carrier LIKE '%%CHP%%'
33
        AND scn_name = 'eGon100RE'
34
        AND bus0 IN
35
        (SELECT bus_id
36
         FROM {sources['etrago_buses']['schema']}.{sources['etrago_buses']['table']}
37
         WHERE scn_name = 'eGon100RE'
38
         AND country = 'DE')
39
        AND bus1 IN
40
        (SELECT bus_id
41
         FROM {sources['etrago_buses']['schema']}.{sources['etrago_buses']['table']}
42
         WHERE scn_name = 'eGon100RE'
43
         AND country = 'DE')
44
        """
45
    )
46
47
    # Select all CHP plants used in district heating
48
    chp_dh = db.select_dataframe(
49
        f"""
50
        SELECT electrical_bus_id, ch4_bus_id, a.carrier,
51
        SUM(el_capacity) AS el_capacity, SUM(th_capacity) AS th_capacity,
52
        c.bus_id as heat_bus_id
53
        FROM {sources['chp_table']['schema']}.
54
        {sources['chp_table']['table']} a
55
        JOIN {sources['district_heating_areas']['schema']}.
56
        {sources['district_heating_areas']['table']}  b
57
        ON a.district_heating_area_id = b.area_id
58
        JOIN grid.egon_etrago_bus c
59
        ON ST_Transform(ST_Centroid(b.geom_polygon), 4326) = c.geom
60
61
        WHERE a.scenario='eGon100RE'
62
        AND b.scenario = 'eGon100RE'
63
        AND c.scn_name = 'eGon100RE'
64
        AND c.carrier = 'central_heat'
65
        AND NOT district_heating_area_id IS NULL
66
        GROUP BY (
67
            electrical_bus_id, ch4_bus_id, a.carrier, c.bus_id)
68
        """
69
    )
70
71
    # Create geodataframes for gas CHP plants
72
    chp_el = link_geom_from_buses(
73
        gpd.GeoDataFrame(
74
            index=chp_dh.index,
75
            data={
76
                "scn_name": "eGon2035",
77
                "bus0": chp_dh.loc[:, "ch4_bus_id"].astype(int),
78
                "bus1": chp_dh.loc[:, "electrical_bus_id"].astype(int),
79
                "p_nom": chp_dh.loc[:, "el_capacity"],
80
                "carrier": "central_gas_CHP",
81
            },
82
        ),
83
        "eGon100RE",
84
    )
85
    # Set index
86
    chp_el["link_id"] = range(
87
        db.next_etrago_id("link"), len(chp_el) + db.next_etrago_id("link")
88
    )
89
90
    # Add marginal cost which is only VOM in case of gas chp
91
    chp_el["marginal_cost"] = get_sector_parameters("gas", "eGon100RE")[
92
        "marginal_cost"
93
    ]["chp_gas"]
94
95
    # Insert into database
96
    chp_el.to_postgis(
97
        targets["link"]["table"],
98
        schema=targets["link"]["schema"],
99
        con=db.engine(),
100
        if_exists="append",
101
    )
102
103
    #
104
    chp_heat = link_geom_from_buses(
105
        gpd.GeoDataFrame(
106
            index=chp_dh.index,
107
            data={
108
                "scn_name": "eGon100RE",
109
                "bus0": chp_dh.loc[:, "ch4_bus_id"].astype(int),
110
                "bus1": chp_dh.loc[:, "heat_bus_id"].astype(int),
111
                "p_nom": chp_dh.loc[:, "th_capacity"],
112
                "carrier": "central_gas_CHP_heat",
113
            },
114
        ),
115
        "eGon100RE",
116
    )
117
118
    chp_heat["link_id"] = range(
119
        db.next_etrago_id("link"), len(chp_heat) + db.next_etrago_id("link")
120
    )
121
122
    chp_heat.to_postgis(
123
        targets["link"]["table"],
124
        schema=targets["link"]["schema"],
125
        con=db.engine(),
126
        if_exists="append",
127
    )
128
129
130
def insert():
131
    """Insert combined heat and power plants into eTraGo tables.
132
133
    Gas CHP plants are modeled as links to the gas grid,
134
    biomass CHP plants (only in eGon2035) are modeled as generators
135
136
    Returns
137
    -------
138
    None.
139
140
    """
141
142
    sources = config.datasets()["chp_etrago"]["sources"]
143
144
    targets = config.datasets()["chp_etrago"]["targets"]
145
146
    db.execute_sql(
147
        f"""
148
        DELETE FROM {targets['link']['schema']}.{targets['link']['table']}
149
        WHERE carrier LIKE '%%CHP%%'
150
        AND scn_name = 'eGon2035'
151
        AND bus0 IN
152
        (SELECT bus_id
153
         FROM {sources['etrago_buses']['schema']}.{sources['etrago_buses']['table']}
154
         WHERE scn_name = 'eGon2035'
155
         AND country = 'DE')
156
        AND bus1 IN
157
        (SELECT bus_id
158
         FROM {sources['etrago_buses']['schema']}.{sources['etrago_buses']['table']}
159
         WHERE scn_name = 'eGon2035'
160
         AND country = 'DE')
161
        """
162
    )
163
    db.execute_sql(
164
        f"""
165
        DELETE FROM {targets['generator']['schema']}.{targets['generator']['table']}
166
        WHERE carrier LIKE '%%CHP%%'
167
        AND scn_name = 'eGon2035'
168
        """
169
    )
170
    # Select all CHP plants used in district heating
171
    chp_dh = db.select_dataframe(
172
        f"""
173
        SELECT electrical_bus_id, ch4_bus_id, a.carrier,
174
        SUM(el_capacity) AS el_capacity, SUM(th_capacity) AS th_capacity,
175
        c.bus_id as heat_bus_id
176
        FROM {sources['chp_table']['schema']}.
177
        {sources['chp_table']['table']} a
178
        JOIN {sources['district_heating_areas']['schema']}.
179
        {sources['district_heating_areas']['table']}  b
180
        ON a.district_heating_area_id = b.area_id
181
        JOIN grid.egon_etrago_bus c
182
        ON ST_Transform(ST_Centroid(b.geom_polygon), 4326) = c.geom
183
184
        WHERE a.scenario='eGon2035'
185
        AND b.scenario = 'eGon2035'
186
        AND c.scn_name = 'eGon2035'
187
        AND c.carrier = 'central_heat'
188
        AND NOT district_heating_area_id IS NULL
189
        GROUP BY (
190
            electrical_bus_id, ch4_bus_id, a.carrier, c.bus_id)
191
        """
192
    )
193
    # Divide into biomass and gas CHP which are modelled differently
194
    chp_link_dh = chp_dh[chp_dh.carrier != "biomass"].index
195
    chp_generator_dh = chp_dh[chp_dh.carrier == "biomass"].index
196
197
    # Create geodataframes for gas CHP plants
198
    chp_el = link_geom_from_buses(
199
        gpd.GeoDataFrame(
200
            index=chp_link_dh,
201
            data={
202
                "scn_name": "eGon2035",
203
                "bus0": chp_dh.loc[chp_link_dh, "ch4_bus_id"].astype(int),
204
                "bus1": chp_dh.loc[chp_link_dh, "electrical_bus_id"].astype(
205
                    int
206
                ),
207
                "p_nom": chp_dh.loc[chp_link_dh, "el_capacity"],
208
                "carrier": "central_gas_CHP",
209
            },
210
        ),
211
        "eGon2035",
212
    )
213
    # Set index
214
    chp_el["link_id"] = range(
215
        db.next_etrago_id("link"), len(chp_el) + db.next_etrago_id("link")
216
    )
217
218
    # Add marginal cost which is only VOM in case of gas chp
219
    chp_el["marginal_cost"] = get_sector_parameters("gas", "eGon2035")[
220
        "marginal_cost"
221
    ]["chp_gas"]
222
223
    # Insert into database
224
    chp_el.to_postgis(
225
        targets["link"]["table"],
226
        schema=targets["link"]["schema"],
227
        con=db.engine(),
228
        if_exists="append",
229
    )
230
231
    #
232
    chp_heat = link_geom_from_buses(
233
        gpd.GeoDataFrame(
234
            index=chp_link_dh,
235
            data={
236
                "scn_name": "eGon2035",
237
                "bus0": chp_dh.loc[chp_link_dh, "ch4_bus_id"].astype(int),
238
                "bus1": chp_dh.loc[chp_link_dh, "heat_bus_id"].astype(int),
239
                "p_nom": chp_dh.loc[chp_link_dh, "th_capacity"],
240
                "carrier": "central_gas_CHP_heat",
241
            },
242
        ),
243
        "eGon2035",
244
    )
245
246
    chp_heat["link_id"] = range(
247
        db.next_etrago_id("link"), len(chp_heat) + db.next_etrago_id("link")
248
    )
249
250
    chp_heat.to_postgis(
251
        targets["link"]["table"],
252
        schema=targets["link"]["schema"],
253
        con=db.engine(),
254
        if_exists="append",
255
    )
256
257
    # Insert biomass CHP as generators
258
    # Create geodataframes for CHP plants
259
    chp_el_gen = pd.DataFrame(
260
        index=chp_generator_dh,
261
        data={
262
            "scn_name": "eGon2035",
263
            "bus": chp_dh.loc[chp_generator_dh, "electrical_bus_id"].astype(
264
                int
265
            ),
266
            "p_nom": chp_dh.loc[chp_generator_dh, "el_capacity"],
267
            "carrier": "central_biomass_CHP",
268
        },
269
    )
270
271
    chp_el_gen["generator_id"] = range(
272
        db.next_etrago_id("generator"),
273
        len(chp_el_gen) + db.next_etrago_id("generator"),
274
    )
275
276
    # Add marginal cost
277
    chp_el_gen["marginal_cost"] = get_sector_parameters(
278
        "electricity", "eGon2035"
279
    )["marginal_cost"]["biomass"]
280
281
    chp_el_gen.to_sql(
282
        targets["generator"]["table"],
283
        schema=targets["generator"]["schema"],
284
        con=db.engine(),
285
        if_exists="append",
286
        index=False,
287
    )
288
289
    chp_heat_gen = pd.DataFrame(
290
        index=chp_generator_dh,
291
        data={
292
            "scn_name": "eGon2035",
293
            "bus": chp_dh.loc[chp_generator_dh, "heat_bus_id"].astype(int),
294
            "p_nom": chp_dh.loc[chp_generator_dh, "th_capacity"],
295
            "carrier": "central_biomass_CHP_heat",
296
        },
297
    )
298
299
    chp_heat_gen["generator_id"] = range(
300
        db.next_etrago_id("generator"),
301
        len(chp_heat_gen) + db.next_etrago_id("generator"),
302
    )
303
304
    chp_heat_gen.to_sql(
305
        targets["generator"]["table"],
306
        schema=targets["generator"]["schema"],
307
        con=db.engine(),
308
        if_exists="append",
309
        index=False,
310
    )
311
312
    chp_industry = db.select_dataframe(
313
        f"""
314
        SELECT electrical_bus_id, ch4_bus_id, carrier,
315
        SUM(el_capacity) AS el_capacity, SUM(th_capacity) AS th_capacity
316
        FROM {sources['chp_table']['schema']}.{sources['chp_table']['table']}
317
        WHERE scenario='eGon2035'
318
        AND district_heating_area_id IS NULL
319
        GROUP BY (electrical_bus_id, ch4_bus_id, carrier)
320
        """
321
    )
322
    chp_link_ind = chp_industry[chp_industry.carrier != "biomass"].index
323
324
    chp_generator_ind = chp_industry[chp_industry.carrier == "biomass"].index
325
326
    chp_el_ind = link_geom_from_buses(
327
        gpd.GeoDataFrame(
328
            index=chp_link_ind,
329
            data={
330
                "scn_name": "eGon2035",
331
                "bus0": chp_industry.loc[chp_link_ind, "ch4_bus_id"].astype(
332
                    int
333
                ),
334
                "bus1": chp_industry.loc[
335
                    chp_link_ind, "electrical_bus_id"
336
                ].astype(int),
337
                "p_nom": chp_industry.loc[chp_link_ind, "el_capacity"],
338
                "carrier": "industrial_gas_CHP",
339
            },
340
        ),
341
        "eGon2035",
342
    )
343
344
    chp_el_ind["link_id"] = range(
345
        db.next_etrago_id("link"), len(chp_el_ind) + db.next_etrago_id("link")
346
    )
347
348
    # Add marginal cost which is only VOM in case of gas chp
349
    chp_el_ind["marginal_cost"] = get_sector_parameters("gas", "eGon2035")[
350
        "marginal_cost"
351
    ]["chp_gas"]
352
353
    chp_el_ind.to_postgis(
354
        targets["link"]["table"],
355
        schema=targets["link"]["schema"],
356
        con=db.engine(),
357
        if_exists="append",
358
    )
359
360
    # Insert biomass CHP as generators
361
    chp_el_ind_gen = pd.DataFrame(
362
        index=chp_generator_ind,
363
        data={
364
            "scn_name": "eGon2035",
365
            "bus": chp_industry.loc[
366
                chp_generator_ind, "electrical_bus_id"
367
            ].astype(int),
368
            "p_nom": chp_industry.loc[chp_generator_ind, "el_capacity"],
369
            "carrier": "industrial_biomass_CHP",
370
        },
371
    )
372
373
    chp_el_ind_gen["generator_id"] = range(
374
        db.next_etrago_id("generator"),
375
        len(chp_el_ind_gen) + db.next_etrago_id("generator"),
376
    )
377
378
    # Add marginal cost
379
    chp_el_ind_gen["marginal_cost"] = get_sector_parameters(
380
        "electricity", "eGon2035"
381
    )["marginal_cost"]["biomass"]
382
383
    chp_el_ind_gen.to_sql(
384
        targets["generator"]["table"],
385
        schema=targets["generator"]["schema"],
386
        con=db.engine(),
387
        if_exists="append",
388
        index=False,
389
    )
390
391
    insert_egon100re()
392