Conditions | 4 |
Total Lines | 107 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | """Module containing functions to insert gas abroad |
||
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) ; |
||
133 |