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