Conditions | 21 |
Total Lines | 279 |
Code Lines | 170 |
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:
Complex classes like data.datasets.power_plants.wind_farms.wind_power_states() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import re |
||
268 | def wind_power_states( |
||
269 | state_wf, |
||
270 | state_wf_ni, |
||
271 | state_mv_districts, |
||
272 | target_power, |
||
273 | scenario_year, |
||
274 | source, |
||
275 | fed_state, |
||
276 | ): |
||
277 | """Import OSM data from a Geofabrik `.pbf` file into a PostgreSQL |
||
278 | database. |
||
279 | |||
280 | Parameters |
||
281 | ---------- |
||
282 | state_wf: geodataframe, mandatory |
||
283 | gdf containing all the wf in the state created based on existing wf. |
||
284 | state_wf_ni: geodataframe, mandatory |
||
285 | potential areas in the the state wich don't intersect any existing wf |
||
286 | state_mv_districts: geodataframe, mandatory |
||
287 | gdf containing all the MV/HV substations in the state |
||
288 | target_power: int, mandatory |
||
289 | Objective power for a state given in MW |
||
290 | scenario_year: str, mandatory |
||
291 | name of the scenario |
||
292 | source: str, mandatory |
||
293 | Type of energy genetor. Always "Wind_onshore" for this script. |
||
294 | fed_state: str, mandatory |
||
295 | Name of the state where the wind farms will be allocated |
||
296 | |||
297 | """ |
||
298 | |||
299 | def match_district_se(x): |
||
300 | for sub in hvmv_substation.index: |
||
301 | if x["geom"].contains(hvmv_substation.at[sub, "point"]): |
||
302 | return hvmv_substation.at[sub, "point"] |
||
303 | |||
304 | con = db.engine() |
||
305 | sql = "SELECT point, voltage FROM grid.egon_hvmv_substation" |
||
306 | # hvmv_substation has the information about HV transmission lines in |
||
307 | # Germany |
||
308 | hvmv_substation = gpd.GeoDataFrame.from_postgis(sql, con, geom_col="point") |
||
309 | |||
310 | # Set wind potential depending on geographical location |
||
311 | power_north = 21.05 # MW/km² |
||
312 | power_south = 16.81 # MW/km² |
||
313 | # Set a maximum installed capacity to limit the power of big potential |
||
314 | # areas |
||
315 | max_power_hv = 120 # in MW |
||
316 | max_power_mv = 20 # in MW |
||
317 | # Max distance between WF (connected to MV) and nearest HV substation |
||
318 | # that allows its connection to HV. |
||
319 | max_dist_hv = 20000 # in meters |
||
320 | |||
321 | summary = pd.DataFrame( |
||
322 | columns=["state", "target", "from existin WF", "MV districts"] |
||
323 | ) |
||
324 | |||
325 | north = [ |
||
326 | "Schleswig-Holstein", |
||
327 | "Mecklenburg-Vorpommern", |
||
328 | "Niedersachsen", |
||
329 | "Bremen", |
||
330 | "Hamburg", |
||
331 | ] |
||
332 | |||
333 | if fed_state == "DE": |
||
334 | sql = f"""SELECT * FROM boundaries.vg250_lan |
||
335 | WHERE gen in {tuple(north)} |
||
336 | """ |
||
337 | north_states = gpd.GeoDataFrame.from_postgis( |
||
338 | sql, con, geom_col="geometry" |
||
339 | ) |
||
340 | north_states.to_crs(3035, inplace=True) |
||
341 | state_wf["nord"] = state_wf.within(north_states.unary_union) |
||
342 | state_wf["inst capacity [MW]"] = state_wf.apply( |
||
343 | lambda x: ( |
||
344 | power_north * x["area [km²]"] |
||
345 | if x["nord"] |
||
346 | else power_south * x["area [km²]"] |
||
347 | ), |
||
348 | axis=1, |
||
349 | ) |
||
350 | else: |
||
351 | if fed_state in north: |
||
352 | state_wf["inst capacity [MW]"] = ( |
||
353 | power_north * state_wf["area [km²]"] |
||
354 | ) |
||
355 | else: |
||
356 | state_wf["inst capacity [MW]"] = ( |
||
357 | power_south * state_wf["area [km²]"] |
||
358 | ) |
||
359 | |||
360 | # Divide selected areas based on voltage of connection points |
||
361 | wf_mv = state_wf[ |
||
362 | (state_wf["voltage"] != "Hochspannung") |
||
363 | & (state_wf["voltage"] != "Hoechstspannung") |
||
364 | & (state_wf["voltage"] != "UmspannungZurHochspannung") |
||
365 | ] |
||
366 | |||
367 | wf_hv = state_wf[ |
||
368 | (state_wf["voltage"] == "Hochspannung") |
||
369 | | (state_wf["voltage"] == "Hoechstspannung") |
||
370 | | (state_wf["voltage"] == "UmspannungZurHochspannung") |
||
371 | ] |
||
372 | |||
373 | # Wind farms connected to MV network will be connected to HV network if |
||
374 | # the distance to the closest HV substation is =< max_dist_hv, and the |
||
375 | # installed capacity is bigger than max_power_mv |
||
376 | hvmv_substation = hvmv_substation.to_crs(3035) |
||
377 | hvmv_substation["voltage"] = hvmv_substation["voltage"].apply( |
||
378 | lambda x: int(re.split(";|:", x)[0]) |
||
379 | ) |
||
380 | hv_substations = hvmv_substation[hvmv_substation["voltage"] >= 110000] |
||
381 | hv_substations = hv_substations.unary_union # join all the hv_substations |
||
382 | wf_mv["dist_to_HV"] = ( |
||
383 | state_wf["geom"].to_crs(3035).distance(hv_substations) |
||
384 | ) |
||
385 | wf_mv_to_hv = wf_mv[ |
||
386 | (wf_mv["dist_to_HV"] <= max_dist_hv) |
||
387 | & (wf_mv["inst capacity [MW]"] >= max_power_mv) |
||
388 | ] |
||
389 | wf_mv_to_hv = wf_mv_to_hv.drop(columns=["dist_to_HV"]) |
||
390 | wf_mv_to_hv["voltage"] = "Hochspannung" |
||
391 | |||
392 | wf_hv = pd.concat([wf_hv, wf_mv_to_hv]) |
||
393 | wf_mv = wf_mv[ |
||
394 | (wf_mv["dist_to_HV"] > max_dist_hv) |
||
395 | | (wf_mv["inst capacity [MW]"] < max_power_mv) |
||
396 | ] |
||
397 | wf_mv = wf_mv.drop(columns=["dist_to_HV"]) |
||
398 | |||
399 | wf_hv["inst capacity [MW]"] = wf_hv["inst capacity [MW]"].apply( |
||
400 | lambda x: x if x < max_power_hv else max_power_hv |
||
401 | ) |
||
402 | |||
403 | wf_mv["inst capacity [MW]"] = wf_mv["inst capacity [MW]"].apply( |
||
404 | lambda x: x if x < max_power_mv else max_power_mv |
||
405 | ) |
||
406 | |||
407 | wind_farms = pd.concat([wf_hv, wf_mv]) |
||
408 | |||
409 | # Adjust the total installed capacity to the scenario |
||
410 | total_wind_power = ( |
||
411 | wf_hv["inst capacity [MW]"].sum() + wf_mv["inst capacity [MW]"].sum() |
||
412 | ) |
||
413 | |||
414 | if total_wind_power > target_power: |
||
415 | scale_factor = target_power / total_wind_power |
||
416 | wf_mv["inst capacity [MW]"] = ( |
||
417 | wf_mv["inst capacity [MW]"] * scale_factor |
||
418 | ) |
||
419 | wf_hv["inst capacity [MW]"] = ( |
||
420 | wf_hv["inst capacity [MW]"] * scale_factor |
||
421 | ) |
||
422 | wind_farms = pd.concat([wf_hv, wf_mv]) |
||
423 | summary = pd.concat( |
||
424 | [ |
||
425 | summary, |
||
426 | pd.DataFrame( |
||
427 | index=[summary.index.max() + 1], |
||
428 | data={ |
||
429 | "state": fed_state, |
||
430 | "target": target_power, |
||
431 | "from existin WF": wind_farms[ |
||
432 | "inst capacity [MW]" |
||
433 | ].sum(), |
||
434 | "MV districts": 0, |
||
435 | }, |
||
436 | ), |
||
437 | ], |
||
438 | ignore_index=True, |
||
439 | ) |
||
440 | else: |
||
441 | extra_wf = state_mv_districts.copy() |
||
442 | extra_wf = extra_wf.set_geometry("geom") |
||
443 | extra_wf["area [km²]"] = 0.0 |
||
444 | for district in extra_wf.index: |
||
445 | try: |
||
446 | pot_area_district = gpd.clip( |
||
447 | state_wf_ni, extra_wf.at[district, "geom"] |
||
448 | ) |
||
449 | extra_wf.at[district, "area [km²]"] = pot_area_district[ |
||
450 | "area [km²]" |
||
451 | ].sum() |
||
452 | except: |
||
453 | print(district) |
||
454 | extra_wf = extra_wf[extra_wf["area [km²]"] != 0] |
||
455 | total_new_area = extra_wf["area [km²]"].sum() |
||
456 | scale_factor = (target_power - total_wind_power) / total_new_area |
||
457 | extra_wf["inst capacity [MW]"] = extra_wf["area [km²]"] * scale_factor |
||
458 | extra_wf["voltage"] = "Hochspannung" |
||
459 | summary = pd.concat( |
||
460 | [ |
||
461 | summary, |
||
462 | pd.DataFrame( |
||
463 | index=[summary.index.max() + 1], |
||
464 | data={ |
||
465 | "state": fed_state, |
||
466 | "target": target_power, |
||
467 | "from existin WF": wind_farms[ |
||
468 | "inst capacity [MW]" |
||
469 | ].sum(), |
||
470 | "MV districts": extra_wf["inst capacity [MW]"].sum(), |
||
471 | }, |
||
472 | ), |
||
473 | ], |
||
474 | ignore_index=True, |
||
475 | ) |
||
476 | extra_wf.to_crs(4326, inplace=True) |
||
477 | wind_farms = pd.concat([wind_farms, extra_wf], ignore_index=True) |
||
478 | |||
479 | # Use Definition of thresholds for voltage level assignment |
||
480 | wind_farms["voltage_level"] = 0 |
||
481 | for i in wind_farms.index: |
||
482 | try: |
||
483 | if wind_farms.at[i, "inst capacity [MW]"] < 5.5: |
||
484 | wind_farms.at[i, "voltage_level"] = 5 |
||
485 | continue |
||
486 | if wind_farms.at[i, "inst capacity [MW]"] < 20: |
||
487 | wind_farms.at[i, "voltage_level"] = 4 |
||
488 | continue |
||
489 | if wind_farms.at[i, "inst capacity [MW]"] >= 20: |
||
490 | wind_farms.at[i, "voltage_level"] = 3 |
||
491 | continue |
||
492 | except: |
||
493 | print(i) |
||
494 | |||
495 | # Look for the maximum id in the table egon_power_plants |
||
496 | sql = "SELECT MAX(id) FROM supply.egon_power_plants" |
||
497 | max_id = pd.read_sql(sql, con) |
||
498 | max_id = max_id["max"].iat[0] |
||
499 | if max_id is None: |
||
500 | wind_farm_id = 1 |
||
501 | else: |
||
502 | wind_farm_id = int(max_id + 1) |
||
503 | |||
504 | # write_table in egon-data database: |
||
505 | |||
506 | # Copy relevant columns from wind_farms |
||
507 | insert_wind_farms = wind_farms[ |
||
508 | ["inst capacity [MW]", "voltage_level", "centroid"] |
||
509 | ] |
||
510 | |||
511 | # Set static column values |
||
512 | insert_wind_farms["carrier"] = source |
||
513 | insert_wind_farms["scenario"] = scenario_year |
||
514 | |||
515 | # Change name and crs of geometry column |
||
516 | insert_wind_farms = ( |
||
517 | insert_wind_farms.rename( |
||
518 | {"centroid": "geom", "inst capacity [MW]": "el_capacity"}, axis=1 |
||
519 | ) |
||
520 | .set_geometry("geom") |
||
521 | .to_crs(4326) |
||
522 | ) |
||
523 | |||
524 | # Reset index |
||
525 | insert_wind_farms.index = pd.RangeIndex( |
||
526 | start=wind_farm_id, |
||
527 | stop=wind_farm_id + len(insert_wind_farms), |
||
528 | name="id", |
||
529 | ) |
||
530 | |||
531 | # Delete old wind_onshore generators |
||
532 | db.execute_sql( |
||
533 | f"""DELETE FROM supply.egon_power_plants |
||
534 | WHERE carrier = 'wind_onshore' |
||
535 | AND scenario = '{scenario_year}' |
||
536 | """ |
||
537 | ) |
||
538 | |||
539 | # Insert into database |
||
540 | insert_wind_farms.reset_index().to_postgis( |
||
541 | "egon_power_plants", |
||
542 | schema="supply", |
||
543 | con=db.engine(), |
||
544 | if_exists="append", |
||
545 | ) |
||
546 | return wind_farms, summary |
||
547 | |||
600 |