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