Passed
Pull Request — dev (#930)
by
unknown
01:36
created

data.datasets.gas_neighbours.gas_abroad   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 27
dl 0
loc 117
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A insert_gas_grid_capacities() 0 104 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 = {"CH4": "CH4", "H2_retrofit": "H2_grid"}
67
68
    if scn_name == "eGon100RE":
69
        for c in carriers:
70
            db.execute_sql(
71
                f"""
72
                DELETE FROM
73
                {sources['links']['schema']}.{sources['links']['table']}
74
                WHERE ("bus0" IN (
75
                        SELECT bus_id FROM 
76
                        {sources['buses']['schema']}.{sources['buses']['table']}
77
                        WHERE country != 'DE'
78
                        AND carrier = '{carriers[c]}'
79
                        AND scn_name = '{scn_name}')
80
                    AND "bus1" IN (SELECT bus_id FROM 
81
                        {sources['buses']['schema']}.{sources['buses']['table']}
82
                        WHERE country = 'DE'
83
                        AND carrier = '{carriers[c]}'
84
                        AND scn_name = '{scn_name}'))
85
                OR ("bus0" IN (
86
                        SELECT bus_id FROM 
87
                        {sources['buses']['schema']}.{sources['buses']['table']}
88
                        WHERE country = 'DE'
89
                        AND carrier = '{carriers[c]}'
90
                        AND scn_name = '{scn_name}')
91
                    AND "bus1" IN (
92
                        SELECT bus_id FROM 
93
                        {sources['buses']['schema']}.{sources['buses']['table']}
94
                        WHERE country != 'DE'
95
                        AND carrier = '{carriers[c]}'
96
                        AND scn_name = '{scn_name}'))
97
                AND scn_name = '{scn_name}'
98
                AND carrier = '{c}'
99
                ;
100
                """
101
            )
102
103
    # Insert data to db
104
    Neighbouring_pipe_capacities_list.set_geometry(
105
        "geom", crs=4326
106
    ).to_postgis(
107
        "egon_etrago_gas_link",
108
        db.engine(),
109
        schema="grid",
110
        index=False,
111
        if_exists="replace",
112
        dtype={"geom": Geometry(), "topo": Geometry()},
113
    )
114
115
    db.execute_sql(
116
        f"""
117
    select UpdateGeometrySRID('grid', 'egon_etrago_gas_link', 'topo', 4326) ;
118
119
    INSERT INTO {targets['links']['schema']}.{targets['links']['table']} (
120
        scn_name, link_id, carrier,
121
        bus0, bus1, p_nom, length, geom, topo)
122
    
123
    SELECT scn_name, link_id, carrier, bus0, bus1, p_nom, length, geom, topo
124
125
    FROM grid.egon_etrago_gas_link;
126
127
    DROP TABLE grid.egon_etrago_gas_link;
128
        """
129
    )
130