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