| Conditions | 1 |
| Total Lines | 265 |
| Code Lines | 144 |
| 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 | """ |
||
| 153 | def insert_scenario(scenario): |
||
| 154 | sources = config.datasets()["chp_etrago"]["sources"] |
||
| 155 | |||
| 156 | targets = config.datasets()["chp_etrago"]["targets"] |
||
| 157 | |||
| 158 | db.execute_sql( |
||
| 159 | f""" |
||
| 160 | DELETE FROM {targets['link']['schema']}.{targets['link']['table']} |
||
| 161 | WHERE carrier LIKE '%%CHP%%' |
||
| 162 | AND scn_name = '{scenario}' |
||
| 163 | AND bus0 IN |
||
| 164 | (SELECT bus_id |
||
| 165 | FROM {sources['etrago_buses']['schema']}.{sources['etrago_buses']['table']} |
||
| 166 | WHERE scn_name = '{scenario}' |
||
| 167 | AND country = 'DE') |
||
| 168 | AND bus1 IN |
||
| 169 | (SELECT bus_id |
||
| 170 | FROM {sources['etrago_buses']['schema']}.{sources['etrago_buses']['table']} |
||
| 171 | WHERE scn_name = '{scenario}' |
||
| 172 | AND country = 'DE') |
||
| 173 | """ |
||
| 174 | ) |
||
| 175 | db.execute_sql( |
||
| 176 | f""" |
||
| 177 | DELETE FROM {targets['generator']['schema']}.{targets['generator']['table']} |
||
| 178 | WHERE carrier LIKE '%%CHP%%' |
||
| 179 | AND scn_name = '{scenario}' |
||
| 180 | """ |
||
| 181 | ) |
||
| 182 | # Select all CHP plants used in district heating |
||
| 183 | chp_dh = db.select_dataframe( |
||
| 184 | f""" |
||
| 185 | SELECT electrical_bus_id, ch4_bus_id, a.carrier, |
||
| 186 | SUM(el_capacity) AS el_capacity, SUM(th_capacity) AS th_capacity, |
||
| 187 | c.bus_id as heat_bus_id |
||
| 188 | FROM {sources['chp_table']['schema']}. |
||
| 189 | {sources['chp_table']['table']} a |
||
| 190 | JOIN {sources['district_heating_areas']['schema']}. |
||
| 191 | {sources['district_heating_areas']['table']} b |
||
| 192 | ON a.district_heating_area_id = b.area_id |
||
| 193 | JOIN grid.egon_etrago_bus c |
||
| 194 | ON ST_Transform(ST_Centroid(b.geom_polygon), 4326) = c.geom |
||
| 195 | |||
| 196 | WHERE a.scenario='{scenario}' |
||
| 197 | AND b.scenario = '{scenario}' |
||
| 198 | AND c.scn_name = '{scenario}' |
||
| 199 | AND c.carrier = 'central_heat' |
||
| 200 | AND NOT district_heating_area_id IS NULL |
||
| 201 | GROUP BY ( |
||
| 202 | electrical_bus_id, ch4_bus_id, a.carrier, c.bus_id) |
||
| 203 | """ |
||
| 204 | ) |
||
| 205 | # Divide into biomass and gas CHP which are modelled differently |
||
| 206 | chp_link_dh = chp_dh[chp_dh.carrier == "gas"].index |
||
| 207 | chp_generator_dh = chp_dh[chp_dh.carrier != "gas"].index |
||
| 208 | |||
| 209 | # Create geodataframes for gas CHP plants |
||
| 210 | chp_el = link_geom_from_buses( |
||
| 211 | gpd.GeoDataFrame( |
||
| 212 | index=chp_link_dh, |
||
| 213 | data={ |
||
| 214 | "scn_name": scenario, |
||
| 215 | "bus0": chp_dh.loc[chp_link_dh, "ch4_bus_id"].astype(int), |
||
| 216 | "bus1": chp_dh.loc[chp_link_dh, "electrical_bus_id"].astype( |
||
| 217 | int |
||
| 218 | ), |
||
| 219 | "p_nom": chp_dh.loc[chp_link_dh, "el_capacity"], |
||
| 220 | "carrier": "central_gas_CHP", |
||
| 221 | }, |
||
| 222 | ), |
||
| 223 | scenario, |
||
| 224 | ) |
||
| 225 | # Set index |
||
| 226 | chp_el["link_id"] = range( |
||
| 227 | db.next_etrago_id("link"), len(chp_el) + db.next_etrago_id("link") |
||
| 228 | ) |
||
| 229 | |||
| 230 | # Add marginal cost which is only VOM in case of gas chp |
||
| 231 | chp_el["marginal_cost"] = get_sector_parameters("gas", scenario)[ |
||
| 232 | "marginal_cost" |
||
| 233 | ]["chp_gas"] |
||
| 234 | |||
| 235 | # Insert into database |
||
| 236 | chp_el.to_postgis( |
||
| 237 | targets["link"]["table"], |
||
| 238 | schema=targets["link"]["schema"], |
||
| 239 | con=db.engine(), |
||
| 240 | if_exists="append", |
||
| 241 | ) |
||
| 242 | |||
| 243 | # |
||
| 244 | chp_heat = link_geom_from_buses( |
||
| 245 | gpd.GeoDataFrame( |
||
| 246 | index=chp_link_dh, |
||
| 247 | data={ |
||
| 248 | "scn_name": scenario, |
||
| 249 | "bus0": chp_dh.loc[chp_link_dh, "ch4_bus_id"].astype(int), |
||
| 250 | "bus1": chp_dh.loc[chp_link_dh, "heat_bus_id"].astype(int), |
||
| 251 | "p_nom": chp_dh.loc[chp_link_dh, "th_capacity"], |
||
| 252 | "carrier": "central_gas_CHP_heat", |
||
| 253 | }, |
||
| 254 | ), |
||
| 255 | scenario, |
||
| 256 | ) |
||
| 257 | |||
| 258 | chp_heat["link_id"] = range( |
||
| 259 | db.next_etrago_id("link"), len(chp_heat) + db.next_etrago_id("link") |
||
| 260 | ) |
||
| 261 | |||
| 262 | chp_heat.to_postgis( |
||
| 263 | targets["link"]["table"], |
||
| 264 | schema=targets["link"]["schema"], |
||
| 265 | con=db.engine(), |
||
| 266 | if_exists="append", |
||
| 267 | ) |
||
| 268 | |||
| 269 | # Insert biomass, coal, oil and other CHP as generators |
||
| 270 | # Create geodataframes for CHP plants |
||
| 271 | chp_el_gen = pd.DataFrame( |
||
| 272 | index=chp_generator_dh, |
||
| 273 | data={ |
||
| 274 | "scn_name": scenario, |
||
| 275 | "bus": chp_dh.loc[chp_generator_dh, "electrical_bus_id"].astype( |
||
| 276 | int |
||
| 277 | ), |
||
| 278 | "p_nom": chp_dh.loc[chp_generator_dh, "el_capacity"], |
||
| 279 | "carrier": chp_dh.loc[chp_generator_dh, "carrier"], |
||
| 280 | }, |
||
| 281 | ) |
||
| 282 | |||
| 283 | chp_el_gen["generator_id"] = range( |
||
| 284 | db.next_etrago_id("generator"), |
||
| 285 | len(chp_el_gen) + db.next_etrago_id("generator"), |
||
| 286 | ) |
||
| 287 | |||
| 288 | # Add marginal cost |
||
| 289 | chp_el_gen["marginal_cost"] = ( |
||
| 290 | pd.Series( |
||
| 291 | get_sector_parameters("electricity", scenario)["marginal_cost"] |
||
| 292 | ) |
||
| 293 | .rename({"other_non_renewable": "others"}) |
||
| 294 | .loc[chp_el_gen["carrier"]] |
||
| 295 | ).values |
||
| 296 | |||
| 297 | chp_el_gen["carrier"] = ( |
||
| 298 | "central_" + chp_dh.loc[chp_generator_dh, "carrier"] + "_CHP" |
||
| 299 | ) |
||
| 300 | |||
| 301 | chp_el_gen.to_sql( |
||
| 302 | targets["generator"]["table"], |
||
| 303 | schema=targets["generator"]["schema"], |
||
| 304 | con=db.engine(), |
||
| 305 | if_exists="append", |
||
| 306 | index=False, |
||
| 307 | ) |
||
| 308 | |||
| 309 | chp_heat_gen = pd.DataFrame( |
||
| 310 | index=chp_generator_dh, |
||
| 311 | data={ |
||
| 312 | "scn_name": scenario, |
||
| 313 | "bus": chp_dh.loc[chp_generator_dh, "heat_bus_id"].astype(int), |
||
| 314 | "p_nom": chp_dh.loc[chp_generator_dh, "th_capacity"], |
||
| 315 | "carrier": "central_" |
||
| 316 | + chp_dh.loc[chp_generator_dh, "carrier"] |
||
| 317 | + "_CHP_heat", |
||
| 318 | }, |
||
| 319 | ) |
||
| 320 | |||
| 321 | chp_heat_gen["generator_id"] = range( |
||
| 322 | db.next_etrago_id("generator"), |
||
| 323 | len(chp_heat_gen) + db.next_etrago_id("generator"), |
||
| 324 | ) |
||
| 325 | |||
| 326 | chp_heat_gen.to_sql( |
||
| 327 | targets["generator"]["table"], |
||
| 328 | schema=targets["generator"]["schema"], |
||
| 329 | con=db.engine(), |
||
| 330 | if_exists="append", |
||
| 331 | index=False, |
||
| 332 | ) |
||
| 333 | |||
| 334 | chp_industry = db.select_dataframe( |
||
| 335 | f""" |
||
| 336 | SELECT electrical_bus_id, ch4_bus_id, carrier, |
||
| 337 | SUM(el_capacity) AS el_capacity, SUM(th_capacity) AS th_capacity |
||
| 338 | FROM {sources['chp_table']['schema']}.{sources['chp_table']['table']} |
||
| 339 | WHERE scenario='{scenario}' |
||
| 340 | AND district_heating_area_id IS NULL |
||
| 341 | GROUP BY (electrical_bus_id, ch4_bus_id, carrier) |
||
| 342 | """ |
||
| 343 | ) |
||
| 344 | chp_link_ind = chp_industry[chp_industry.carrier == "gas"].index |
||
| 345 | |||
| 346 | chp_generator_ind = chp_industry[chp_industry.carrier != "gas"].index |
||
| 347 | |||
| 348 | chp_el_ind = link_geom_from_buses( |
||
| 349 | gpd.GeoDataFrame( |
||
| 350 | index=chp_link_ind, |
||
| 351 | data={ |
||
| 352 | "scn_name": scenario, |
||
| 353 | "bus0": chp_industry.loc[chp_link_ind, "ch4_bus_id"].astype( |
||
| 354 | int |
||
| 355 | ), |
||
| 356 | "bus1": chp_industry.loc[ |
||
| 357 | chp_link_ind, "electrical_bus_id" |
||
| 358 | ].astype(int), |
||
| 359 | "p_nom": chp_industry.loc[chp_link_ind, "el_capacity"], |
||
| 360 | "carrier": "industrial_gas_CHP", |
||
| 361 | }, |
||
| 362 | ), |
||
| 363 | scenario, |
||
| 364 | ) |
||
| 365 | |||
| 366 | chp_el_ind["link_id"] = range( |
||
| 367 | db.next_etrago_id("link"), len(chp_el_ind) + db.next_etrago_id("link") |
||
| 368 | ) |
||
| 369 | |||
| 370 | # Add marginal cost which is only VOM in case of gas chp |
||
| 371 | chp_el_ind["marginal_cost"] = get_sector_parameters("gas", scenario)[ |
||
| 372 | "marginal_cost" |
||
| 373 | ]["chp_gas"] |
||
| 374 | |||
| 375 | chp_el_ind.to_postgis( |
||
| 376 | targets["link"]["table"], |
||
| 377 | schema=targets["link"]["schema"], |
||
| 378 | con=db.engine(), |
||
| 379 | if_exists="append", |
||
| 380 | ) |
||
| 381 | |||
| 382 | # Insert biomass CHP as generators |
||
| 383 | chp_el_ind_gen = pd.DataFrame( |
||
| 384 | index=chp_generator_ind, |
||
| 385 | data={ |
||
| 386 | "scn_name": scenario, |
||
| 387 | "bus": chp_industry.loc[ |
||
| 388 | chp_generator_ind, "electrical_bus_id" |
||
| 389 | ].astype(int), |
||
| 390 | "p_nom": chp_industry.loc[chp_generator_ind, "el_capacity"], |
||
| 391 | "carrier": chp_industry.loc[chp_generator_ind, "carrier"], |
||
| 392 | }, |
||
| 393 | ) |
||
| 394 | |||
| 395 | chp_el_ind_gen["generator_id"] = range( |
||
| 396 | db.next_etrago_id("generator"), |
||
| 397 | len(chp_el_ind_gen) + db.next_etrago_id("generator"), |
||
| 398 | ) |
||
| 399 | |||
| 400 | # Add marginal cost |
||
| 401 | chp_el_ind_gen["marginal_cost"] = ( |
||
| 402 | pd.Series( |
||
| 403 | get_sector_parameters("electricity", scenario)["marginal_cost"] |
||
| 404 | ) |
||
| 405 | .rename({"other_non_renewable": "others"}) |
||
| 406 | .loc[chp_el_ind_gen["carrier"]] |
||
| 407 | ).values |
||
| 408 | |||
| 409 | # Update carrier |
||
| 410 | chp_el_ind_gen["carrier"] = "industrial_" + chp_el_ind_gen.carrier + "_CHP" |
||
| 411 | |||
| 412 | chp_el_ind_gen.to_sql( |
||
| 413 | targets["generator"]["table"], |
||
| 414 | schema=targets["generator"]["schema"], |
||
| 415 | con=db.engine(), |
||
| 416 | if_exists="append", |
||
| 417 | index=False, |
||
| 418 | ) |
||
| 439 |