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