| 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 |
||
| 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) ;
|
||
| 130 |