Conditions | 4 |
Total Lines | 104 |
Code Lines | 24 |
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 |
||
163 | def insert_gas_grid_capacities(Neighbouring_pipe_capacities_list, scn_name): |
||
164 | """Insert crossbordering gas pipelines in the database |
||
165 | |||
166 | This function insert a list of crossbordering gas pipelines after |
||
167 | cleaning the database. |
||
168 | For eGon2035, all the CH4 crossbordering pipelines are inserted |
||
169 | there (no H2 grid in this scenario). |
||
170 | For eGon100RE, only the the crossbordering pipelines with Germany |
||
171 | are inserted there (the other ones are inerted in PypsaEurSec), |
||
172 | but in this scenario there are H2 and CH4 pipelines. |
||
173 | |||
174 | Parameters |
||
175 | ---------- |
||
176 | Neighbouring_pipe_capacities_list : pandas.DataFrame |
||
177 | List of the crossbordering gas pipelines |
||
178 | scn_name : str |
||
179 | Name of the scenario |
||
180 | |||
181 | Returns |
||
182 | ------- |
||
183 | None |
||
184 | |||
185 | """ |
||
186 | sources = config.datasets()["gas_neighbours"]["sources"] |
||
187 | targets = config.datasets()["gas_neighbours"]["targets"] |
||
188 | |||
189 | # Delete existing data |
||
190 | if scn_name == "eGon2035": |
||
191 | carrier_link = "CH4" |
||
192 | carrier_bus = "CH4" |
||
193 | |||
194 | db.execute_sql( |
||
195 | f""" |
||
196 | DELETE FROM |
||
197 | {sources['links']['schema']}.{sources['links']['table']} |
||
198 | WHERE "bus0" IN ( |
||
199 | SELECT bus_id FROM |
||
200 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
201 | WHERE country != 'DE' |
||
202 | AND carrier = '{carrier_bus}' |
||
203 | AND scn_name = '{scn_name}') |
||
204 | OR "bus1" IN ( |
||
205 | SELECT bus_id FROM |
||
206 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
207 | WHERE country != 'DE' |
||
208 | AND carrier = '{carrier_bus}' |
||
209 | AND scn_name = '{scn_name}') |
||
210 | AND scn_name = '{scn_name}' |
||
211 | AND carrier = '{carrier_link}' |
||
212 | ; |
||
213 | """ |
||
214 | ) |
||
215 | |||
216 | carriers = {"CH4": "CH4", "H2_retrofit": "H2_grid"} |
||
217 | |||
218 | if scn_name == "eGon100RE": |
||
219 | for c in carriers: |
||
220 | db.execute_sql( |
||
221 | f""" |
||
222 | DELETE FROM |
||
223 | {sources['links']['schema']}.{sources['links']['table']} |
||
224 | WHERE ("bus0" IN ( |
||
225 | SELECT bus_id FROM |
||
226 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
227 | WHERE country != 'DE' |
||
228 | AND carrier = '{carriers[c]}' |
||
229 | AND scn_name = '{scn_name}') |
||
230 | AND "bus1" IN (SELECT bus_id FROM |
||
231 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
232 | WHERE country = 'DE' |
||
233 | AND carrier = '{carriers[c]}' |
||
234 | AND scn_name = '{scn_name}')) |
||
235 | OR ("bus0" IN ( |
||
236 | SELECT bus_id FROM |
||
237 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
238 | WHERE country = 'DE' |
||
239 | AND carrier = '{carriers[c]}' |
||
240 | AND scn_name = '{scn_name}') |
||
241 | AND "bus1" IN ( |
||
242 | SELECT bus_id FROM |
||
243 | {sources['buses']['schema']}.{sources['buses']['table']} |
||
244 | WHERE country != 'DE' |
||
245 | AND carrier = '{carriers[c]}' |
||
246 | AND scn_name = '{scn_name}')) |
||
247 | AND scn_name = '{scn_name}' |
||
248 | AND carrier = '{c}' |
||
249 | ; |
||
250 | """ |
||
251 | ) |
||
252 | |||
253 | # Insert data to db |
||
254 | Neighbouring_pipe_capacities_list.set_geometry( |
||
255 | "geom", crs=4326 |
||
256 | ).to_postgis( |
||
257 | "egon_etrago_gas_link", |
||
258 | db.engine(), |
||
259 | schema="grid", |
||
260 | index=False, |
||
261 | if_exists="replace", |
||
262 | dtype={"geom": Geometry(), "topo": Geometry()}, |
||
263 | ) |
||
264 | |||
265 | db.execute_sql( |
||
266 | f""" |
||
267 | select UpdateGeometrySRID('grid', 'egon_etrago_gas_link', 'topo', 4326) ; |
||
337 |