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 |
||
104 | def insert_gas_grid_capacities(Neighbouring_pipe_capacities_list, scn_name): |
||
105 | """Insert crossbordering gas pipelines in the database |
||
106 | |||
107 | This function insert a list of crossbordering gas pipelines after |
||
108 | cleaning the database. |
||
109 | For eGon2035, all the CH4 crossbordering pipelines are inserted |
||
110 | there (no H2 grid in this scenario). |
||
111 | For eGon100RE, only the the crossbordering pipelines with Germany |
||
112 | are inserted there (the other ones are inerted in PypsaEurSec), |
||
113 | but in this scenario there are H2 and CH4 pipelines. |
||
114 | |||
115 | Parameters |
||
116 | ---------- |
||
117 | Neighbouring_pipe_capacities_list : pandas.DataFrame |
||
118 | List of the crossbordering gas pipelines |
||
119 | scn_name : str |
||
120 | Name of the scenario |
||
121 | |||
122 | Returns |
||
123 | ------- |
||
124 | None |
||
125 | |||
126 | """ |
||
127 | sources = config.datasets()["gas_neighbours"]["sources"] |
||
128 | targets = config.datasets()["gas_neighbours"]["targets"] |
||
129 | |||
130 | # Delete existing data |
||
131 | if scn_name == "eGon2035": |
||
132 | carrier_link = "CH4" |
||
133 | carrier_bus = "CH4" |
||
134 | |||
135 | db.execute_sql( |
||
136 | f""" |
||
137 | DELETE FROM |
||
138 | {sources['links']['schema']}.{sources['links']['table']} |
||
139 | WHERE "bus0" IN ( |
||
140 | SELECT bus_id FROM |
||
141 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
142 | WHERE country != 'DE' |
||
143 | AND carrier = '{carrier_bus}' |
||
144 | AND scn_name = '{scn_name}') |
||
145 | OR "bus1" IN ( |
||
146 | SELECT bus_id FROM |
||
147 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
148 | WHERE country != 'DE' |
||
149 | AND carrier = '{carrier_bus}' |
||
150 | AND scn_name = '{scn_name}') |
||
151 | AND scn_name = '{scn_name}' |
||
152 | AND carrier = '{carrier_link}' |
||
153 | ; |
||
154 | """ |
||
155 | ) |
||
156 | |||
157 | carriers = { |
||
158 | "CH4": {"bus_inDE": "CH4", "bus_abroad": "CH4"}, |
||
159 | "H2_retrofit": {"bus_inDE": "H2_grid", "bus_abroad": "H2"}, |
||
160 | } |
||
161 | |||
162 | if scn_name == "eGon100RE": |
||
163 | for c in carriers: |
||
164 | db.execute_sql( |
||
165 | f""" |
||
166 | DELETE FROM |
||
167 | {sources['links']['schema']}.{sources['links']['table']} |
||
168 | WHERE ("bus0" IN ( |
||
169 | SELECT bus_id FROM |
||
170 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
171 | WHERE country != 'DE' |
||
172 | AND carrier = '{carriers[c]["bus_abroad"]}' |
||
173 | AND scn_name = '{scn_name}') |
||
174 | AND "bus1" IN (SELECT bus_id FROM |
||
175 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
176 | WHERE country = 'DE' |
||
177 | AND carrier = '{carriers[c]["bus_inDE"]}' |
||
178 | AND scn_name = '{scn_name}')) |
||
179 | OR ("bus0" IN ( |
||
180 | SELECT bus_id FROM |
||
181 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
182 | WHERE country = 'DE' |
||
183 | AND carrier = '{carriers[c]["bus_inDE"]}' |
||
184 | AND scn_name = '{scn_name}') |
||
185 | AND "bus1" IN ( |
||
186 | SELECT bus_id FROM |
||
187 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
188 | WHERE country != 'DE' |
||
189 | AND carrier = '{carriers[c]["bus_abroad"]}' |
||
190 | AND scn_name = '{scn_name}')) |
||
191 | AND scn_name = '{scn_name}' |
||
192 | AND carrier = '{c.index}' |
||
193 | ; |
||
194 | """ |
||
195 | ) |
||
196 | |||
197 | # Insert data to db |
||
198 | Neighbouring_pipe_capacities_list.set_geometry( |
||
199 | "geom", crs=4326 |
||
200 | ).to_postgis( |
||
201 | "egon_etrago_gas_link", |
||
202 | db.engine(), |
||
203 | schema="grid", |
||
204 | index=False, |
||
205 | if_exists="replace", |
||
206 | dtype={"geom": Geometry(), "topo": Geometry()}, |
||
207 | ) |
||
208 | |||
209 | db.execute_sql( |
||
210 | f""" |
||
211 | select UpdateGeometrySRID('grid', 'egon_etrago_gas_link', 'topo', 4326) ; |
||
281 |