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