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

data.datasets.gas_neighbours.gas_abroad   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

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