| Conditions | 18 |
| Total Lines | 341 |
| Code Lines | 238 |
| 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.gas_grid.define_gas_pipeline_list() 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 | # -*- coding: utf-8 -*- |
||
| 331 | def define_gas_pipeline_list( |
||
| 332 | gas_nodes_list, abroad_gas_nodes_list, scn_name="eGon2035" |
||
| 333 | ): |
||
| 334 | """Define gas pipelines in Germany from SciGRID_gas IGGIELGN data |
||
| 335 | |||
| 336 | Insert detailled description |
||
| 337 | |||
| 338 | Parameters |
||
| 339 | ---------- |
||
| 340 | gas_nodes_list : dataframe |
||
| 341 | description missing |
||
| 342 | abroad_gas_nodes_list: dataframe |
||
| 343 | description missing |
||
| 344 | scn_name : str |
||
| 345 | Name of the scenario |
||
| 346 | |||
| 347 | Returns |
||
| 348 | ------- |
||
| 349 | gas_pipelines_list : pandas.DataFrame |
||
| 350 | Dataframe containing the gas pipelines in Germany |
||
| 351 | |||
| 352 | """ |
||
| 353 | abroad_gas_nodes_list = abroad_gas_nodes_list.set_index("country")
|
||
| 354 | |||
| 355 | main_gas_carrier = get_sector_parameters("gas", scenario=scn_name)[
|
||
| 356 | "main_gas_carrier" |
||
| 357 | ] |
||
| 358 | |||
| 359 | # Select next id value |
||
| 360 | new_id = db.next_etrago_id("link")
|
||
| 361 | |||
| 362 | classifiaction_file = ( |
||
| 363 | Path(".")
|
||
| 364 | / "data_bundle_egon_data" |
||
| 365 | / "pipeline_classification_gas" |
||
| 366 | / "pipeline_classification.csv" |
||
| 367 | ) |
||
| 368 | |||
| 369 | classification = pd.read_csv( |
||
| 370 | classifiaction_file, |
||
| 371 | delimiter=",", |
||
| 372 | usecols=["classification", "max_transport_capacity_Gwh/d"], |
||
| 373 | ) |
||
| 374 | |||
| 375 | target_file = ( |
||
| 376 | Path(".")
|
||
| 377 | / "datasets" |
||
| 378 | / "gas_data" |
||
| 379 | / "data" |
||
| 380 | / "IGGIELGN_PipeSegments.csv" |
||
| 381 | ) |
||
| 382 | |||
| 383 | gas_pipelines_list = pd.read_csv( |
||
| 384 | target_file, |
||
| 385 | delimiter=";", |
||
| 386 | decimal=".", |
||
| 387 | usecols=["id", "node_id", "lat", "long", "country_code", "param"], |
||
| 388 | ) |
||
| 389 | |||
| 390 | # Select the links having at least one bus in Germany |
||
| 391 | gas_pipelines_list = gas_pipelines_list[ |
||
| 392 | gas_pipelines_list["country_code"].str.contains("DE")
|
||
| 393 | ] |
||
| 394 | |||
| 395 | # Remove links disconnected of the rest of the grid |
||
| 396 | # Remove manually for disconnected link EntsoG_Map__ST_195 |
||
| 397 | gas_pipelines_list = gas_pipelines_list[ |
||
| 398 | ~gas_pipelines_list["id"].str.match("EntsoG_Map__ST_195")
|
||
| 399 | ] |
||
| 400 | |||
| 401 | gas_pipelines_list["link_id"] = range( |
||
| 402 | new_id, new_id + len(gas_pipelines_list) |
||
| 403 | ) |
||
| 404 | gas_pipelines_list["link_id"] = gas_pipelines_list["link_id"].astype(int) |
||
| 405 | |||
| 406 | # Cut data to federal state if in testmode |
||
| 407 | NUTS1 = [] |
||
| 408 | for index, row in gas_pipelines_list.iterrows(): |
||
| 409 | param = ast.literal_eval(row["param"]) |
||
| 410 | NUTS1.append(param["nuts_id_1"]) |
||
| 411 | gas_pipelines_list["NUTS1"] = NUTS1 |
||
| 412 | |||
| 413 | map_states = {
|
||
| 414 | "Baden-Württemberg": "DE1", |
||
| 415 | "Nordrhein-Westfalen": "DEA", |
||
| 416 | "Hessen": "DE7", |
||
| 417 | "Brandenburg": "DE4", |
||
| 418 | "Bremen": "DE5", |
||
| 419 | "Rheinland-Pfalz": "DEB", |
||
| 420 | "Sachsen-Anhalt": "DEE", |
||
| 421 | "Schleswig-Holstein": "DEF", |
||
| 422 | "Mecklenburg-Vorpommern": "DE8", |
||
| 423 | "Thüringen": "DEG", |
||
| 424 | "Niedersachsen": "DE9", |
||
| 425 | "Sachsen": "DED", |
||
| 426 | "Hamburg": "DE6", |
||
| 427 | "Saarland": "DEC", |
||
| 428 | "Berlin": "DE3", |
||
| 429 | "Bayern": "DE2", |
||
| 430 | "Everything": "Nan", |
||
| 431 | } |
||
| 432 | gas_pipelines_list["NUTS1_0"] = [x[0] for x in gas_pipelines_list["NUTS1"]] |
||
| 433 | gas_pipelines_list["NUTS1_1"] = [x[1] for x in gas_pipelines_list["NUTS1"]] |
||
| 434 | |||
| 435 | boundary = settings()["egon-data"]["--dataset-boundary"] |
||
| 436 | |||
| 437 | if boundary != "Everything": |
||
| 438 | |||
| 439 | gas_pipelines_list = gas_pipelines_list[ |
||
| 440 | gas_pipelines_list["NUTS1_0"].str.contains(map_states[boundary]) |
||
| 441 | | gas_pipelines_list["NUTS1_1"].str.contains(map_states[boundary]) |
||
| 442 | ] |
||
| 443 | |||
| 444 | # Add missing columns |
||
| 445 | gas_pipelines_list["scn_name"] = scn_name |
||
| 446 | gas_pipelines_list["carrier"] = main_gas_carrier |
||
| 447 | gas_pipelines_list["p_nom_extendable"] = False |
||
| 448 | |||
| 449 | diameter = [] |
||
| 450 | geom = [] |
||
| 451 | topo = [] |
||
| 452 | length_km = [] |
||
| 453 | |||
| 454 | for index, row in gas_pipelines_list.iterrows(): |
||
| 455 | |||
| 456 | param = ast.literal_eval(row["param"]) |
||
| 457 | diameter.append(param["diameter_mm"]) |
||
| 458 | length_km.append(param["length_km"]) |
||
| 459 | |||
| 460 | long_e = json.loads(row["long"]) |
||
| 461 | lat_e = json.loads(row["lat"]) |
||
| 462 | crd_e = list(zip(long_e, lat_e)) |
||
| 463 | topo.append(geometry.LineString(crd_e)) |
||
| 464 | |||
| 465 | long_path = param["path_long"] |
||
| 466 | lat_path = param["path_lat"] |
||
| 467 | crd = list(zip(long_path, lat_path)) |
||
| 468 | crd.insert(0, crd_e[0]) |
||
| 469 | crd.append(crd_e[1]) |
||
| 470 | lines = [] |
||
| 471 | for i in range(len(crd) - 1): |
||
| 472 | lines.append(geometry.LineString([crd[i], crd[i + 1]])) |
||
| 473 | geom.append(geometry.MultiLineString(lines)) |
||
| 474 | |||
| 475 | gas_pipelines_list["diameter"] = diameter |
||
| 476 | gas_pipelines_list["geom"] = geom |
||
| 477 | gas_pipelines_list["topo"] = topo |
||
| 478 | gas_pipelines_list["length_km"] = length_km |
||
| 479 | gas_pipelines_list = gas_pipelines_list.set_geometry("geom", crs=4326)
|
||
| 480 | |||
| 481 | country_0 = [] |
||
| 482 | country_1 = [] |
||
| 483 | for index, row in gas_pipelines_list.iterrows(): |
||
| 484 | c = ast.literal_eval(row["country_code"]) |
||
| 485 | country_0.append(c[0]) |
||
| 486 | country_1.append(c[1]) |
||
| 487 | |||
| 488 | gas_pipelines_list["country_0"] = country_0 |
||
| 489 | gas_pipelines_list["country_1"] = country_1 |
||
| 490 | |||
| 491 | # Correct non valid neighbouring country nodes |
||
| 492 | gas_pipelines_list.loc[ |
||
| 493 | gas_pipelines_list["country_0"] == "XX", "country_0" |
||
| 494 | ] = "NO" |
||
| 495 | gas_pipelines_list.loc[ |
||
| 496 | gas_pipelines_list["country_1"] == "FI", "country_1" |
||
| 497 | ] = "RU" |
||
| 498 | gas_pipelines_list.loc[ |
||
| 499 | gas_pipelines_list["id"] == "ST_2612_Seg_0_Seg_0", "country_0" |
||
| 500 | ] = "AT" # bus "INET_N_1182" DE -> AT |
||
| 501 | gas_pipelines_list.loc[ |
||
| 502 | gas_pipelines_list["id"] == "INET_PL_385_EE_3_Seg_0_Seg_1", "country_1" |
||
| 503 | ] = "AT" # "INET_N_1182" DE -> AT |
||
| 504 | gas_pipelines_list.loc[ |
||
| 505 | gas_pipelines_list["id"] == "LKD_PS_0_Seg_0_Seg_3", "country_0" |
||
| 506 | ] = "NL" # bus "SEQ_10608_p" DE -> NL |
||
| 507 | |||
| 508 | # Remove uncorrect pipelines |
||
| 509 | gas_pipelines_list = gas_pipelines_list[ |
||
| 510 | (gas_pipelines_list["id"] != "PLNG_2637_Seg_0_Seg_0_Seg_0") |
||
| 511 | & (gas_pipelines_list["id"] != "NSG_6650_Seg_2_Seg_0") |
||
| 512 | & (gas_pipelines_list["id"] != "NSG_6734_Seg_2_Seg_0") |
||
| 513 | ] |
||
| 514 | |||
| 515 | # Remove link test if length = 0 |
||
| 516 | gas_pipelines_list = gas_pipelines_list[ |
||
| 517 | gas_pipelines_list["length_km"] != 0 |
||
| 518 | ] |
||
| 519 | |||
| 520 | # Adjust columns |
||
| 521 | bus0 = [] |
||
| 522 | bus1 = [] |
||
| 523 | geom_adjusted = [] |
||
| 524 | topo_adjusted = [] |
||
| 525 | length_adjusted = [] |
||
| 526 | pipe_class = [] |
||
| 527 | |||
| 528 | for index, row in gas_pipelines_list.iterrows(): |
||
| 529 | buses = row["node_id"].strip("][").split(", ")
|
||
| 530 | |||
| 531 | if ( |
||
| 532 | (boundary != "Everything") |
||
| 533 | & (row["NUTS1_0"] != map_states[boundary]) |
||
| 534 | & (row["country_0"] == "DE") |
||
| 535 | ): |
||
| 536 | bus0.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
| 537 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 538 | long_e = [ |
||
| 539 | abroad_gas_nodes_list.loc["DE", "x"], |
||
| 540 | json.loads(row["long"])[1], |
||
| 541 | ] |
||
| 542 | lat_e = [ |
||
| 543 | abroad_gas_nodes_list.loc["DE", "y"], |
||
| 544 | json.loads(row["lat"])[1], |
||
| 545 | ] |
||
| 546 | geom_pipe = geometry.MultiLineString( |
||
| 547 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 548 | ) |
||
| 549 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 550 | |||
| 551 | elif row["country_0"] != "DE": |
||
| 552 | country = str(row["country_0"]) |
||
| 553 | bus0.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
| 554 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 555 | long_e = [ |
||
| 556 | abroad_gas_nodes_list.loc[country, "x"], |
||
| 557 | json.loads(row["long"])[1], |
||
| 558 | ] |
||
| 559 | lat_e = [ |
||
| 560 | abroad_gas_nodes_list.loc[country, "y"], |
||
| 561 | json.loads(row["lat"])[1], |
||
| 562 | ] |
||
| 563 | geom_pipe = geometry.MultiLineString( |
||
| 564 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 565 | ) |
||
| 566 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 567 | |||
| 568 | elif ( |
||
| 569 | (boundary != "Everything") |
||
| 570 | & (row["NUTS1_1"] != map_states[boundary]) |
||
| 571 | & (row["country_1"] == "DE") |
||
| 572 | ): |
||
| 573 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 574 | bus1.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
| 575 | long_e = [ |
||
| 576 | json.loads(row["long"])[0], |
||
| 577 | abroad_gas_nodes_list.loc["DE", "x"], |
||
| 578 | ] |
||
| 579 | lat_e = [ |
||
| 580 | json.loads(row["lat"])[0], |
||
| 581 | abroad_gas_nodes_list.loc["DE", "y"], |
||
| 582 | ] |
||
| 583 | geom_pipe = geometry.MultiLineString( |
||
| 584 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 585 | ) |
||
| 586 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 587 | |||
| 588 | elif row["country_1"] != "DE": |
||
| 589 | country = str(row["country_1"]) |
||
| 590 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 591 | bus1.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
| 592 | long_e = [ |
||
| 593 | json.loads(row["long"])[0], |
||
| 594 | abroad_gas_nodes_list.loc[country, "x"], |
||
| 595 | ] |
||
| 596 | lat_e = [ |
||
| 597 | json.loads(row["lat"])[0], |
||
| 598 | abroad_gas_nodes_list.loc[country, "y"], |
||
| 599 | ] |
||
| 600 | geom_pipe = geometry.MultiLineString( |
||
| 601 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 602 | ) |
||
| 603 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 604 | |||
| 605 | else: |
||
| 606 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 607 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 608 | geom_pipe = row["geom"] |
||
| 609 | topo_adjusted.append(row["topo"]) |
||
| 610 | |||
| 611 | geom_adjusted.append(geom_pipe) |
||
| 612 | length_adjusted.append(geom_pipe.length) |
||
| 613 | |||
| 614 | if row["diameter"] >= 1000: |
||
| 615 | pipe_class = "A" |
||
| 616 | elif 700 <= row["diameter"] <= 1000: |
||
| 617 | pipe_class = "B" |
||
| 618 | elif 500 <= row["diameter"] <= 700: |
||
| 619 | pipe_class = "C" |
||
| 620 | elif 350 <= row["diameter"] <= 500: |
||
| 621 | pipe_class = "D" |
||
| 622 | elif 200 <= row["diameter"] <= 350: |
||
| 623 | pipe_class = "E" |
||
| 624 | elif 100 <= row["diameter"] <= 200: |
||
| 625 | pipe_class = "F" |
||
| 626 | elif row["diameter"] <= 100: |
||
| 627 | pipe_class = "G" |
||
| 628 | |||
| 629 | gas_pipelines_list["bus0"] = bus0 |
||
| 630 | gas_pipelines_list["bus1"] = bus1 |
||
| 631 | gas_pipelines_list["geom"] = geom_adjusted |
||
| 632 | gas_pipelines_list["topo"] = topo_adjusted |
||
| 633 | gas_pipelines_list["length"] = length_adjusted |
||
| 634 | gas_pipelines_list["pipe_class"] = pipe_class |
||
| 635 | |||
| 636 | # Remove pipes having the same node for start and end |
||
| 637 | gas_pipelines_list = gas_pipelines_list[ |
||
| 638 | gas_pipelines_list["bus0"] != gas_pipelines_list["bus1"] |
||
| 639 | ] |
||
| 640 | |||
| 641 | gas_pipelines_list = gas_pipelines_list.merge( |
||
| 642 | classification, |
||
| 643 | how="left", |
||
| 644 | left_on="pipe_class", |
||
| 645 | right_on="classification", |
||
| 646 | ) |
||
| 647 | gas_pipelines_list["p_nom"] = gas_pipelines_list[ |
||
| 648 | "max_transport_capacity_Gwh/d" |
||
| 649 | ] * (1000 / 24) |
||
| 650 | |||
| 651 | # Remove useless columns |
||
| 652 | gas_pipelines_list = gas_pipelines_list.drop( |
||
| 653 | columns=[ |
||
| 654 | "id", |
||
| 655 | "node_id", |
||
| 656 | "param", |
||
| 657 | "NUTS1", |
||
| 658 | "NUTS1_0", |
||
| 659 | "NUTS1_1", |
||
| 660 | "country_code", |
||
| 661 | "diameter", |
||
| 662 | "pipe_class", |
||
| 663 | "classification", |
||
| 664 | "max_transport_capacity_Gwh/d", |
||
| 665 | "lat", |
||
| 666 | "long", |
||
| 667 | "length_km", |
||
| 668 | ] |
||
| 669 | ) |
||
| 670 | |||
| 671 | return gas_pipelines_list |
||
| 672 | |||
| 851 |