| Conditions | 25 |
| Total Lines | 416 |
| Code Lines | 284 |
| 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 -*- |
||
| 492 | def define_gas_pipeline_list( |
||
| 493 | gas_nodes_list, abroad_gas_nodes_list, scn_name="eGon2035" |
||
| 494 | ): |
||
| 495 | """ |
||
| 496 | Define gas pipelines in Germany from SciGRID_gas IGGIELGN data |
||
| 497 | |||
| 498 | The gas pipelines, modelled as PyPSA links are read from the IGGIELGN_PipeSegments |
||
| 499 | csv file previously downloded in the function :py:func:`download_SciGRID_gas_data`. |
||
| 500 | |||
| 501 | The capacities of the pipelines are determined by the correspondance |
||
| 502 | table given by the parameters for the classification of gas pipelines |
||
| 503 | in `Electricity, heat, and gas sector data for modeling the German system |
||
| 504 | <https://www.econstor.eu/bitstream/10419/173388/1/1011162628.pdf>`_ |
||
| 505 | related to the pipeline diameter given in the SciGRID_gas dataset. |
||
| 506 | |||
| 507 | The manual corrections allow to: |
||
| 508 | |||
| 509 | * Delete gas pipelines disconnected of the rest of the gas grid |
||
| 510 | * Connect one pipeline (also connected to Norway) disconnected of |
||
| 511 | the rest of the gas grid |
||
| 512 | * Correct countries of some erroneous pipelines |
||
| 513 | |||
| 514 | Parameters |
||
| 515 | ---------- |
||
| 516 | gas_nodes_list : dataframe |
||
| 517 | Dataframe containing the gas nodes in Europe |
||
| 518 | abroad_gas_nodes_list: dataframe |
||
| 519 | Dataframe containing the gas buses in the neighbouring countries |
||
| 520 | and one in the center of Germany in test mode |
||
| 521 | scn_name : str |
||
| 522 | Name of the scenario |
||
| 523 | |||
| 524 | Returns |
||
| 525 | ------- |
||
| 526 | gas_pipelines_list : pandas.DataFrame |
||
| 527 | Dataframe containing the gas pipelines in Germany |
||
| 528 | |||
| 529 | """ |
||
| 530 | scn_params = get_sector_parameters("gas", scn_name)
|
||
| 531 | |||
| 532 | abroad_gas_nodes_list = abroad_gas_nodes_list.set_index("country")
|
||
| 533 | |||
| 534 | gas_carrier = "CH4" |
||
| 535 | |||
| 536 | classifiaction_file = ( |
||
| 537 | Path(".")
|
||
| 538 | / "data_bundle_egon_data" |
||
| 539 | / "pipeline_classification_gas" |
||
| 540 | / "pipeline_classification.csv" |
||
| 541 | ) |
||
| 542 | |||
| 543 | classification = pd.read_csv( |
||
| 544 | classifiaction_file, |
||
| 545 | delimiter=",", |
||
| 546 | usecols=["classification", "max_transport_capacity_Gwh/d"], |
||
| 547 | ) |
||
| 548 | |||
| 549 | target_file = ( |
||
| 550 | Path(".")
|
||
| 551 | / "datasets" |
||
| 552 | / "gas_data" |
||
| 553 | / "data" |
||
| 554 | / "IGGIELGN_PipeSegments.csv" |
||
| 555 | ) |
||
| 556 | |||
| 557 | gas_pipelines_list = pd.read_csv( |
||
| 558 | target_file, |
||
| 559 | delimiter=";", |
||
| 560 | decimal=".", |
||
| 561 | usecols=["id", "node_id", "lat", "long", "country_code", "param"], |
||
| 562 | ) |
||
| 563 | |||
| 564 | # Correct some country codes (also changed in define_gas_nodes_list()) |
||
| 565 | gas_pipelines_list["bus0"] = gas_pipelines_list["node_id"].apply( |
||
| 566 | lambda x: x.split(",")[0]
|
||
| 567 | ) |
||
| 568 | gas_pipelines_list["bus1"] = gas_pipelines_list["node_id"].apply( |
||
| 569 | lambda x: x.split(",")[1]
|
||
| 570 | ) |
||
| 571 | gas_pipelines_list["country0"] = gas_pipelines_list["country_code"].apply( |
||
| 572 | lambda x: x.split(",")[0]
|
||
| 573 | ) |
||
| 574 | gas_pipelines_list["country1"] = gas_pipelines_list["country_code"].apply( |
||
| 575 | lambda x: x.split(",")[1]
|
||
| 576 | ) |
||
| 577 | |||
| 578 | gas_pipelines_list.loc[ |
||
| 579 | gas_pipelines_list["bus0"].str.contains("INET_N_1182"), "country0"
|
||
| 580 | ] = "['AT'" |
||
| 581 | gas_pipelines_list.loc[ |
||
| 582 | gas_pipelines_list["bus1"].str.contains("INET_N_1182"), "country1"
|
||
| 583 | ] = "'AT']" |
||
| 584 | gas_pipelines_list.loc[ |
||
| 585 | gas_pipelines_list["bus0"].str.contains("SEQ_10608_p"), "country0"
|
||
| 586 | ] = "['NL'" |
||
| 587 | gas_pipelines_list.loc[ |
||
| 588 | gas_pipelines_list["bus1"].str.contains("SEQ_10608_p"), "country1"
|
||
| 589 | ] = "'NL']" |
||
| 590 | gas_pipelines_list.loc[ |
||
| 591 | gas_pipelines_list["bus0"].str.contains("N_88_NS_LMGN"), "country0"
|
||
| 592 | ] = "['XX'" |
||
| 593 | gas_pipelines_list.loc[ |
||
| 594 | gas_pipelines_list["bus1"].str.contains("N_88_NS_LMGN"), "country1"
|
||
| 595 | ] = "'XX']" |
||
| 596 | |||
| 597 | gas_pipelines_list["country_code"] = gas_pipelines_list.apply( |
||
| 598 | lambda x: x["country0"] + "," + x["country1"], axis=1 |
||
| 599 | ) |
||
| 600 | gas_pipelines_list.drop( |
||
| 601 | columns=["bus0", "bus1", "country0", "country1"], inplace=True |
||
| 602 | ) |
||
| 603 | |||
| 604 | # Select the links having at least one bus in Germany |
||
| 605 | gas_pipelines_list = gas_pipelines_list[ |
||
| 606 | gas_pipelines_list["country_code"].str.contains("DE")
|
||
| 607 | ] |
||
| 608 | # Remove links disconnected of the rest of the grid |
||
| 609 | # Remove manually for disconnected link EntsoG_Map__ST_195 and EntsoG_Map__ST_108 |
||
| 610 | gas_pipelines_list = gas_pipelines_list[ |
||
| 611 | gas_pipelines_list["node_id"] != "['SEQ_11790_p', 'Stor_EU_107']" |
||
| 612 | ] |
||
| 613 | gas_pipelines_list = gas_pipelines_list[ |
||
| 614 | ~gas_pipelines_list["id"].str.match("EntsoG_Map__ST_108")
|
||
| 615 | ] |
||
| 616 | |||
| 617 | # Manually add pipeline to artificially connect isolated pipeline |
||
| 618 | gas_pipelines_list.at["new_pipe", "param"] = gas_pipelines_list[ |
||
| 619 | gas_pipelines_list["id"] == "NO_PS_8_Seg_0_Seg_23" |
||
| 620 | ]["param"].values[0] |
||
| 621 | gas_pipelines_list.at["new_pipe", "node_id"] = ( |
||
| 622 | "['SEQ_12442_p', 'LKD_N_200']" |
||
| 623 | ) |
||
| 624 | gas_pipelines_list.at["new_pipe", "lat"] = "[53.358536, 53.412719]" |
||
| 625 | gas_pipelines_list.at["new_pipe", "long"] = "[7.041677, 7.093251]" |
||
| 626 | gas_pipelines_list.at["new_pipe", "country_code"] = "['DE', 'DE']" |
||
| 627 | |||
| 628 | gas_pipelines_list["link_id"] = db.next_etrago_id("link", len(gas_pipelines_list))
|
||
| 629 | |||
| 630 | # Cut data to federal state if in testmode |
||
| 631 | NUTS1 = [] |
||
| 632 | for index, row in gas_pipelines_list.iterrows(): |
||
| 633 | param = ast.literal_eval(row["param"]) |
||
| 634 | NUTS1.append(param["nuts_id_1"]) |
||
| 635 | gas_pipelines_list["NUTS1"] = NUTS1 |
||
| 636 | |||
| 637 | map_states = {
|
||
| 638 | "Baden-Württemberg": "DE1", |
||
| 639 | "Nordrhein-Westfalen": "DEA", |
||
| 640 | "Hessen": "DE7", |
||
| 641 | "Brandenburg": "DE4", |
||
| 642 | "Bremen": "DE5", |
||
| 643 | "Rheinland-Pfalz": "DEB", |
||
| 644 | "Sachsen-Anhalt": "DEE", |
||
| 645 | "Schleswig-Holstein": "DEF", |
||
| 646 | "Mecklenburg-Vorpommern": "DE8", |
||
| 647 | "Thüringen": "DEG", |
||
| 648 | "Niedersachsen": "DE9", |
||
| 649 | "Sachsen": "DED", |
||
| 650 | "Hamburg": "DE6", |
||
| 651 | "Saarland": "DEC", |
||
| 652 | "Berlin": "DE3", |
||
| 653 | "Bayern": "DE2", |
||
| 654 | "Everything": "Nan", |
||
| 655 | } |
||
| 656 | gas_pipelines_list["NUTS1_0"] = [x[0] for x in gas_pipelines_list["NUTS1"]] |
||
| 657 | gas_pipelines_list["NUTS1_1"] = [x[1] for x in gas_pipelines_list["NUTS1"]] |
||
| 658 | |||
| 659 | boundary = settings()["egon-data"]["--dataset-boundary"] |
||
| 660 | |||
| 661 | if boundary != "Everything": |
||
| 662 | gas_pipelines_list = gas_pipelines_list[ |
||
| 663 | gas_pipelines_list["NUTS1_0"].str.contains(map_states[boundary]) |
||
| 664 | | gas_pipelines_list["NUTS1_1"].str.contains(map_states[boundary]) |
||
| 665 | ] |
||
| 666 | |||
| 667 | # Add missing columns |
||
| 668 | gas_pipelines_list["scn_name"] = scn_name |
||
| 669 | gas_pipelines_list["carrier"] = gas_carrier |
||
| 670 | gas_pipelines_list["p_nom_extendable"] = False |
||
| 671 | gas_pipelines_list["p_min_pu"] = -1.0 |
||
| 672 | |||
| 673 | diameter = [] |
||
| 674 | geom = [] |
||
| 675 | topo = [] |
||
| 676 | length_km = [] |
||
| 677 | |||
| 678 | for index, row in gas_pipelines_list.iterrows(): |
||
| 679 | param = ast.literal_eval(row["param"]) |
||
| 680 | diameter.append(param["diameter_mm"]) |
||
| 681 | length_km.append(param["length_km"]) |
||
| 682 | |||
| 683 | long_e = json.loads(row["long"]) |
||
| 684 | lat_e = json.loads(row["lat"]) |
||
| 685 | crd_e = list(zip(long_e, lat_e)) |
||
| 686 | topo.append(geometry.LineString(crd_e)) |
||
| 687 | |||
| 688 | long_path = param["path_long"] |
||
| 689 | lat_path = param["path_lat"] |
||
| 690 | crd = list(zip(long_path, lat_path)) |
||
| 691 | crd.insert(0, crd_e[0]) |
||
| 692 | crd.append(crd_e[1]) |
||
| 693 | lines = [] |
||
| 694 | for i in range(len(crd) - 1): |
||
| 695 | lines.append(geometry.LineString([crd[i], crd[i + 1]])) |
||
| 696 | geom.append(geometry.MultiLineString(lines)) |
||
| 697 | |||
| 698 | gas_pipelines_list["diameter"] = diameter |
||
| 699 | gas_pipelines_list["geom"] = geom |
||
| 700 | gas_pipelines_list["topo"] = topo |
||
| 701 | gas_pipelines_list["length_km"] = length_km |
||
| 702 | gas_pipelines_list = gas_pipelines_list.set_geometry("geom", crs=4326)
|
||
| 703 | |||
| 704 | country_0 = [] |
||
| 705 | country_1 = [] |
||
| 706 | for index, row in gas_pipelines_list.iterrows(): |
||
| 707 | c = ast.literal_eval(row["country_code"]) |
||
| 708 | country_0.append(c[0]) |
||
| 709 | country_1.append(c[1]) |
||
| 710 | |||
| 711 | gas_pipelines_list["country_0"] = country_0 |
||
| 712 | gas_pipelines_list["country_1"] = country_1 |
||
| 713 | |||
| 714 | # Correct non valid neighbouring country nodes |
||
| 715 | gas_pipelines_list.loc[ |
||
| 716 | gas_pipelines_list["country_0"] == "XX", "country_0" |
||
| 717 | ] = "NO" |
||
| 718 | gas_pipelines_list.loc[ |
||
| 719 | gas_pipelines_list["country_1"] == "FI", "country_1" |
||
| 720 | ] = "RU" |
||
| 721 | gas_pipelines_list.loc[ |
||
| 722 | gas_pipelines_list["id"] == "ST_2612_Seg_0_Seg_0", "country_0" |
||
| 723 | ] = "AT" # bus "INET_N_1182" DE -> AT |
||
| 724 | gas_pipelines_list.loc[ |
||
| 725 | gas_pipelines_list["id"] == "INET_PL_385_EE_3_Seg_0_Seg_1", "country_1" |
||
| 726 | ] = "AT" # "INET_N_1182" DE -> AT |
||
| 727 | gas_pipelines_list.loc[ |
||
| 728 | gas_pipelines_list["id"] == "LKD_PS_0_Seg_0_Seg_3", "country_0" |
||
| 729 | ] = "NL" # bus "SEQ_10608_p" DE -> NL |
||
| 730 | |||
| 731 | if scn_name == "eGon100RE": |
||
| 732 | gas_pipelines_list = gas_pipelines_list[ |
||
| 733 | gas_pipelines_list["country_1"] != "RU" |
||
| 734 | ] |
||
| 735 | |||
| 736 | # Remove uncorrect pipelines |
||
| 737 | gas_pipelines_list = gas_pipelines_list[ |
||
| 738 | (gas_pipelines_list["id"] != "PLNG_2637_Seg_0_Seg_0_Seg_0") |
||
| 739 | & (gas_pipelines_list["id"] != "NSG_6650_Seg_2_Seg_0") |
||
| 740 | & (gas_pipelines_list["id"] != "NSG_6734_Seg_2_Seg_0") |
||
| 741 | ] |
||
| 742 | |||
| 743 | # Remove link test if length = 0 |
||
| 744 | gas_pipelines_list = gas_pipelines_list[ |
||
| 745 | gas_pipelines_list["length_km"] != 0 |
||
| 746 | ] |
||
| 747 | |||
| 748 | # Adjust columns |
||
| 749 | bus0 = [] |
||
| 750 | bus1 = [] |
||
| 751 | geom_adjusted = [] |
||
| 752 | topo_adjusted = [] |
||
| 753 | length_adjusted = [] |
||
| 754 | pipe_class = [] |
||
| 755 | |||
| 756 | for index, row in gas_pipelines_list.iterrows(): |
||
| 757 | buses = row["node_id"].strip("][").split(", ")
|
||
| 758 | |||
| 759 | if ( |
||
| 760 | (boundary != "Everything") |
||
| 761 | & (row["NUTS1_0"] != map_states[boundary]) |
||
| 762 | & (row["country_0"] == "DE") |
||
| 763 | ): |
||
| 764 | bus0.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
| 765 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 766 | long_e = [ |
||
| 767 | abroad_gas_nodes_list.loc["DE", "x"], |
||
| 768 | json.loads(row["long"])[1], |
||
| 769 | ] |
||
| 770 | lat_e = [ |
||
| 771 | abroad_gas_nodes_list.loc["DE", "y"], |
||
| 772 | json.loads(row["lat"])[1], |
||
| 773 | ] |
||
| 774 | geom_pipe = geometry.MultiLineString( |
||
| 775 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 776 | ) |
||
| 777 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 778 | |||
| 779 | elif row["country_0"] != "DE": |
||
| 780 | country = str(row["country_0"]) |
||
| 781 | bus0.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
| 782 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 783 | long_e = [ |
||
| 784 | abroad_gas_nodes_list.loc[country, "x"], |
||
| 785 | json.loads(row["long"])[1], |
||
| 786 | ] |
||
| 787 | lat_e = [ |
||
| 788 | abroad_gas_nodes_list.loc[country, "y"], |
||
| 789 | json.loads(row["lat"])[1], |
||
| 790 | ] |
||
| 791 | geom_pipe = geometry.MultiLineString( |
||
| 792 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 793 | ) |
||
| 794 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 795 | |||
| 796 | elif ( |
||
| 797 | (boundary != "Everything") |
||
| 798 | & (row["NUTS1_1"] != map_states[boundary]) |
||
| 799 | & (row["country_1"] == "DE") |
||
| 800 | ): |
||
| 801 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 802 | bus1.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
| 803 | long_e = [ |
||
| 804 | json.loads(row["long"])[0], |
||
| 805 | abroad_gas_nodes_list.loc["DE", "x"], |
||
| 806 | ] |
||
| 807 | lat_e = [ |
||
| 808 | json.loads(row["lat"])[0], |
||
| 809 | abroad_gas_nodes_list.loc["DE", "y"], |
||
| 810 | ] |
||
| 811 | geom_pipe = geometry.MultiLineString( |
||
| 812 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 813 | ) |
||
| 814 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 815 | |||
| 816 | elif row["country_1"] != "DE": |
||
| 817 | country = str(row["country_1"]) |
||
| 818 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 819 | bus1.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
| 820 | long_e = [ |
||
| 821 | json.loads(row["long"])[0], |
||
| 822 | abroad_gas_nodes_list.loc[country, "x"], |
||
| 823 | ] |
||
| 824 | lat_e = [ |
||
| 825 | json.loads(row["lat"])[0], |
||
| 826 | abroad_gas_nodes_list.loc[country, "y"], |
||
| 827 | ] |
||
| 828 | geom_pipe = geometry.MultiLineString( |
||
| 829 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 830 | ) |
||
| 831 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 832 | |||
| 833 | else: |
||
| 834 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 835 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 836 | geom_pipe = row["geom"] |
||
| 837 | topo_adjusted.append(row["topo"]) |
||
| 838 | |||
| 839 | geom_adjusted.append(geom_pipe) |
||
| 840 | length_adjusted.append(geom_pipe.length) |
||
| 841 | |||
| 842 | if row["diameter"] >= 1000: |
||
| 843 | pipe_class = "A" |
||
| 844 | elif 700 <= row["diameter"] <= 1000: |
||
| 845 | pipe_class = "B" |
||
| 846 | elif 500 <= row["diameter"] <= 700: |
||
| 847 | pipe_class = "C" |
||
| 848 | elif 350 <= row["diameter"] <= 500: |
||
| 849 | pipe_class = "D" |
||
| 850 | elif 200 <= row["diameter"] <= 350: |
||
| 851 | pipe_class = "E" |
||
| 852 | elif 100 <= row["diameter"] <= 200: |
||
| 853 | pipe_class = "F" |
||
| 854 | elif row["diameter"] <= 100: |
||
| 855 | pipe_class = "G" |
||
| 856 | |||
| 857 | gas_pipelines_list["bus0"] = bus0 |
||
| 858 | gas_pipelines_list["bus1"] = bus1 |
||
| 859 | gas_pipelines_list["geom"] = geom_adjusted |
||
| 860 | gas_pipelines_list["topo"] = topo_adjusted |
||
| 861 | gas_pipelines_list["length"] = length_adjusted |
||
| 862 | gas_pipelines_list["pipe_class"] = pipe_class |
||
| 863 | |||
| 864 | # Remove pipes having the same node for start and end |
||
| 865 | gas_pipelines_list = gas_pipelines_list[ |
||
| 866 | gas_pipelines_list["bus0"] != gas_pipelines_list["bus1"] |
||
| 867 | ] |
||
| 868 | |||
| 869 | gas_pipelines_list = gas_pipelines_list.merge( |
||
| 870 | classification, |
||
| 871 | how="left", |
||
| 872 | left_on="pipe_class", |
||
| 873 | right_on="classification", |
||
| 874 | ) |
||
| 875 | gas_pipelines_list["p_nom"] = gas_pipelines_list[ |
||
| 876 | "max_transport_capacity_Gwh/d" |
||
| 877 | ] * (1000 / 24) |
||
| 878 | |||
| 879 | if scn_name == "eGon100RE": |
||
| 880 | # remaining CH4 share is 1 - retroffited pipeline share |
||
| 881 | gas_pipelines_list["p_nom"] *= ( |
||
| 882 | 1 - scn_params["retrofitted_CH4pipeline-to-H2pipeline_share"] |
||
| 883 | ) |
||
| 884 | |||
| 885 | # Remove useless columns |
||
| 886 | gas_pipelines_list = gas_pipelines_list.drop( |
||
| 887 | columns=[ |
||
| 888 | "id", |
||
| 889 | "node_id", |
||
| 890 | "param", |
||
| 891 | "NUTS1", |
||
| 892 | "NUTS1_0", |
||
| 893 | "NUTS1_1", |
||
| 894 | "country_code", |
||
| 895 | "country_0", |
||
| 896 | "country_1", |
||
| 897 | "diameter", |
||
| 898 | "pipe_class", |
||
| 899 | "classification", |
||
| 900 | "max_transport_capacity_Gwh/d", |
||
| 901 | "lat", |
||
| 902 | "long", |
||
| 903 | "length_km", |
||
| 904 | ] |
||
| 905 | ) |
||
| 906 | |||
| 907 | return gas_pipelines_list |
||
| 908 | |||
| 1166 |