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