Passed
Pull Request — dev (#917)
by
unknown
01:58
created

data.datasets.gas_neighbours.gas_abroad   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 32
dl 0
loc 124
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A insert_gas_grid_capacities() 0 107 4
1
"""Module containing functions to insert gas abroad
2
3
In this module, functions useful to insert the gas components (H2 and
4
CH4) abroad for eGon2035 and eGon100RE are defined.
5
6
"""
7
8
import zipfile
9
10
from geoalchemy2.types import Geometry
11
import geopandas as gpd
12
import pandas as pd
13
14
from egon.data import config, db
15
16
17
def insert_gas_grid_capacities(Neighbouring_pipe_capacities_list, scn_name):
18
    """Insert crossbordering gas pipelines in the database
19
20
    This function insert a list of crossbordering gas pipelines after
21
    cleaning the database.
22
    For eGon2035, all the CH4 crossbordering pipelines are inserted
23
    there (no H2 grid in this scenario).
24
    For eGon100RE, only the the crossbordering pipelines with Germany
25
    are inserted there (the other ones are inerted in PypsaEurSec),
26
    but in this scenario there are H2 and CH4 pipelines.
27
28
    Parameters
29
    ----------
30
    Neighbouring_pipe_capacities_list : pandas.DataFrame
31
        List of the crossbordering gas pipelines
32
    scn_name : str
33
        Name of the scenario
34
35
    Returns
36
    -------
37
    None
38
39
    """
40
    sources = config.datasets()["gas_neighbours"]["sources"]
41
    targets = config.datasets()["gas_neighbours"]["targets"]
42
43
    # Delete existing data
44
    if scn_name == "eGon2035":
45
        carrier_link = "CH4"
46
        carrier_bus = "CH4"
47
48
        db.execute_sql(
49
            f"""
50
            DELETE FROM 
51
            {sources['links']['schema']}.{sources['links']['table']}
52
            WHERE "bus0" IN (
53
                SELECT bus_id FROM 
54
                {sources['buses']['schema']}.{sources['buses']['table']}
55
                    WHERE country != 'DE'
56
                    AND carrier = '{carrier_bus}'
57
                    AND scn_name = '{scn_name}')
58
            OR "bus1" IN (
59
                SELECT bus_id FROM 
60
                {sources['buses']['schema']}.{sources['buses']['table']}
61
                    WHERE country != 'DE'
62
                    AND carrier = '{carrier_bus}' 
63
                    AND scn_name = '{scn_name}')
64
            AND scn_name = '{scn_name}'
65
            AND carrier = '{carrier_link}'            
66
            ;
67
            """
68
        )
69
70
    carriers = {
71
        "CH4": {"bus_inDE": "CH4", "bus_abroad": "CH4"},
72
        "H2_retrofit": {"bus_inDE": "H2_grid", "bus_abroad": "H2"},
73
    }
74
75
    if scn_name == "eGon100RE":
76
        for c in carriers:
77
            db.execute_sql(
78
                f"""
79
                DELETE FROM
80
                {sources['links']['schema']}.{sources['links']['table']}
81
                WHERE ("bus0" IN (
82
                        SELECT bus_id FROM 
83
                        {sources['buses']['schema']}.{sources['buses']['table']}
84
                        WHERE country != 'DE'
85
                        AND carrier = '{carriers[c]["bus_abroad"]}'
86
                        AND scn_name = '{scn_name}')
87
                    AND "bus1" IN (SELECT bus_id FROM 
88
                        {sources['buses']['schema']}.{sources['buses']['table']}
89
                        WHERE country = 'DE'
90
                        AND carrier = '{carriers[c]["bus_inDE"]}' 
91
                        AND scn_name = '{scn_name}'))
92
                OR ("bus0" IN (
93
                        SELECT bus_id FROM 
94
                        {sources['buses']['schema']}.{sources['buses']['table']}
95
                        WHERE country = 'DE'
96
                        AND carrier = '{carriers[c]["bus_inDE"]}'
97
                        AND scn_name = '{scn_name}')
98
                AND "bus1" IN (
99
                        SELECT bus_id FROM 
100
                        {sources['buses']['schema']}.{sources['buses']['table']}
101
                        WHERE country != 'DE'
102
                        AND carrier = '{carriers[c]["bus_abroad"]}' 
103
                        AND scn_name = '{scn_name}'))
104
                AND scn_name = '{scn_name}'
105
                AND carrier = '{c.index}'            
106
                ;
107
                """
108
            )
109
110
    # Insert data to db
111
    Neighbouring_pipe_capacities_list.set_geometry(
112
        "geom", crs=4326
113
    ).to_postgis(
114
        "egon_etrago_gas_link",
115
        db.engine(),
116
        schema="grid",
117
        index=False,
118
        if_exists="replace",
119
        dtype={"geom": Geometry(), "topo": Geometry()},
120
    )
121
122
    db.execute_sql(
123
        f"""
124
    select UpdateGeometrySRID('grid', 'egon_etrago_gas_link', 'topo', 4326) ;
125
126
    INSERT INTO {targets['links']['schema']}.{targets['links']['table']} (
127
        scn_name, link_id, carrier,
128
        bus0, bus1, p_nom, length, geom, topo)
129
    
130
    SELECT scn_name, link_id, carrier, bus0, bus1, p_nom, length, geom, topo
131
132
    FROM grid.egon_etrago_gas_link;
133
134
    DROP TABLE grid.egon_etrago_gas_link;
135
        """
136
    )
137