Conditions | 18 |
Total Lines | 370 |
Code Lines | 250 |
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 -*- |
||
445 | def define_gas_pipeline_list( |
||
446 | gas_nodes_list, abroad_gas_nodes_list, scn_name="eGon2035" |
||
447 | ): |
||
448 | """ |
||
449 | Define gas pipelines in Germany from SciGRID_gas IGGIELGN data |
||
450 | |||
451 | The gas pipelines, modelled as PyPSA links are read from the IGGIELGN_PipeSegments |
||
452 | csv file previously downloded in the function :py:func:`download_SciGRID_gas_data`. |
||
453 | |||
454 | The capacities of the pipelines are determined by the correspondance |
||
455 | table given by the parameters for the classification of gas pipelines |
||
456 | in `Electricity, heat, and gas sector data for modeling the German system |
||
457 | <https://www.econstor.eu/bitstream/10419/173388/1/1011162628.pdf>`_ |
||
458 | related to the pipeline diameter given in the SciGRID_gas dataset. |
||
459 | |||
460 | The manual corrections allow to: |
||
461 | * Delete gas pipelines disconnected of the rest of the gas grid |
||
462 | * Connect one pipeline (also connected to Norway) disconnected of |
||
463 | the rest of the gas grid |
||
464 | * Correct countries of some erroneous pipelines |
||
465 | |||
466 | Parameters |
||
467 | ---------- |
||
468 | gas_nodes_list : dataframe |
||
469 | Dataframe containing the gas nodes in Europe |
||
470 | abroad_gas_nodes_list: dataframe |
||
471 | Dataframe containing the gas buses in the neighbouring countries |
||
472 | and one in the center of Germany in test mode |
||
473 | scn_name : str |
||
474 | Name of the scenario |
||
475 | |||
476 | Returns |
||
477 | ------- |
||
478 | gas_pipelines_list : pandas.DataFrame |
||
479 | Dataframe containing the gas pipelines in Germany |
||
480 | |||
481 | """ |
||
482 | abroad_gas_nodes_list = abroad_gas_nodes_list.set_index("country") |
||
483 | |||
484 | main_gas_carrier = get_sector_parameters("gas", scenario=scn_name)[ |
||
485 | "main_gas_carrier" |
||
486 | ] |
||
487 | |||
488 | # Select next id value |
||
489 | new_id = db.next_etrago_id("link") |
||
490 | |||
491 | classifiaction_file = ( |
||
492 | Path(".") |
||
493 | / "data_bundle_egon_data" |
||
494 | / "pipeline_classification_gas" |
||
495 | / "pipeline_classification.csv" |
||
496 | ) |
||
497 | |||
498 | classification = pd.read_csv( |
||
499 | classifiaction_file, |
||
500 | delimiter=",", |
||
501 | usecols=["classification", "max_transport_capacity_Gwh/d"], |
||
502 | ) |
||
503 | |||
504 | target_file = ( |
||
505 | Path(".") |
||
506 | / "datasets" |
||
507 | / "gas_data" |
||
508 | / "data" |
||
509 | / "IGGIELGN_PipeSegments.csv" |
||
510 | ) |
||
511 | |||
512 | gas_pipelines_list = pd.read_csv( |
||
513 | target_file, |
||
514 | delimiter=";", |
||
515 | decimal=".", |
||
516 | usecols=["id", "node_id", "lat", "long", "country_code", "param"], |
||
517 | ) |
||
518 | |||
519 | # Select the links having at least one bus in Germany |
||
520 | gas_pipelines_list = gas_pipelines_list[ |
||
521 | gas_pipelines_list["country_code"].str.contains("DE") |
||
522 | ] |
||
523 | # Remove links disconnected of the rest of the grid |
||
524 | # Remove manually for disconnected link EntsoG_Map__ST_195 and EntsoG_Map__ST_108 |
||
525 | gas_pipelines_list = gas_pipelines_list[ |
||
526 | gas_pipelines_list["node_id"] != "['SEQ_11790_p', 'Stor_EU_107']" |
||
527 | ] |
||
528 | gas_pipelines_list = gas_pipelines_list[ |
||
529 | ~gas_pipelines_list["id"].str.match("EntsoG_Map__ST_108") |
||
530 | ] |
||
531 | |||
532 | # Manually add pipeline to artificially connect isolated pipeline |
||
533 | gas_pipelines_list.at["new_pipe", "param"] = gas_pipelines_list[ |
||
534 | gas_pipelines_list["id"] == "NO_PS_8_Seg_0_Seg_23" |
||
535 | ]["param"].values[0] |
||
536 | gas_pipelines_list.at[ |
||
537 | "new_pipe", "node_id" |
||
538 | ] = "['SEQ_12442_p', 'LKD_N_200']" |
||
539 | gas_pipelines_list.at["new_pipe", "lat"] = "[53.358536, 53.412719]" |
||
540 | gas_pipelines_list.at["new_pipe", "long"] = "[7.041677, 7.093251]" |
||
541 | gas_pipelines_list.at["new_pipe", "country_code"] = "['DE', 'DE']" |
||
542 | |||
543 | gas_pipelines_list["link_id"] = range( |
||
544 | new_id, new_id + len(gas_pipelines_list) |
||
545 | ) |
||
546 | gas_pipelines_list["link_id"] = gas_pipelines_list["link_id"].astype(int) |
||
547 | |||
548 | # Cut data to federal state if in testmode |
||
549 | NUTS1 = [] |
||
550 | for index, row in gas_pipelines_list.iterrows(): |
||
551 | param = ast.literal_eval(row["param"]) |
||
552 | NUTS1.append(param["nuts_id_1"]) |
||
553 | gas_pipelines_list["NUTS1"] = NUTS1 |
||
554 | |||
555 | map_states = { |
||
556 | "Baden-Württemberg": "DE1", |
||
557 | "Nordrhein-Westfalen": "DEA", |
||
558 | "Hessen": "DE7", |
||
559 | "Brandenburg": "DE4", |
||
560 | "Bremen": "DE5", |
||
561 | "Rheinland-Pfalz": "DEB", |
||
562 | "Sachsen-Anhalt": "DEE", |
||
563 | "Schleswig-Holstein": "DEF", |
||
564 | "Mecklenburg-Vorpommern": "DE8", |
||
565 | "Thüringen": "DEG", |
||
566 | "Niedersachsen": "DE9", |
||
567 | "Sachsen": "DED", |
||
568 | "Hamburg": "DE6", |
||
569 | "Saarland": "DEC", |
||
570 | "Berlin": "DE3", |
||
571 | "Bayern": "DE2", |
||
572 | "Everything": "Nan", |
||
573 | } |
||
574 | gas_pipelines_list["NUTS1_0"] = [x[0] for x in gas_pipelines_list["NUTS1"]] |
||
575 | gas_pipelines_list["NUTS1_1"] = [x[1] for x in gas_pipelines_list["NUTS1"]] |
||
576 | |||
577 | boundary = settings()["egon-data"]["--dataset-boundary"] |
||
578 | |||
579 | if boundary != "Everything": |
||
580 | |||
581 | gas_pipelines_list = gas_pipelines_list[ |
||
582 | gas_pipelines_list["NUTS1_0"].str.contains(map_states[boundary]) |
||
583 | | gas_pipelines_list["NUTS1_1"].str.contains(map_states[boundary]) |
||
584 | ] |
||
585 | |||
586 | # Add missing columns |
||
587 | gas_pipelines_list["scn_name"] = scn_name |
||
588 | gas_pipelines_list["carrier"] = main_gas_carrier |
||
589 | gas_pipelines_list["p_nom_extendable"] = False |
||
590 | gas_pipelines_list["p_min_pu"] = -1.0 |
||
591 | |||
592 | diameter = [] |
||
593 | geom = [] |
||
594 | topo = [] |
||
595 | length_km = [] |
||
596 | |||
597 | for index, row in gas_pipelines_list.iterrows(): |
||
598 | |||
599 | param = ast.literal_eval(row["param"]) |
||
600 | diameter.append(param["diameter_mm"]) |
||
601 | length_km.append(param["length_km"]) |
||
602 | |||
603 | long_e = json.loads(row["long"]) |
||
604 | lat_e = json.loads(row["lat"]) |
||
605 | crd_e = list(zip(long_e, lat_e)) |
||
606 | topo.append(geometry.LineString(crd_e)) |
||
607 | |||
608 | long_path = param["path_long"] |
||
609 | lat_path = param["path_lat"] |
||
610 | crd = list(zip(long_path, lat_path)) |
||
611 | crd.insert(0, crd_e[0]) |
||
612 | crd.append(crd_e[1]) |
||
613 | lines = [] |
||
614 | for i in range(len(crd) - 1): |
||
615 | lines.append(geometry.LineString([crd[i], crd[i + 1]])) |
||
616 | geom.append(geometry.MultiLineString(lines)) |
||
617 | |||
618 | gas_pipelines_list["diameter"] = diameter |
||
619 | gas_pipelines_list["geom"] = geom |
||
620 | gas_pipelines_list["topo"] = topo |
||
621 | gas_pipelines_list["length_km"] = length_km |
||
622 | gas_pipelines_list = gas_pipelines_list.set_geometry("geom", crs=4326) |
||
623 | |||
624 | country_0 = [] |
||
625 | country_1 = [] |
||
626 | for index, row in gas_pipelines_list.iterrows(): |
||
627 | c = ast.literal_eval(row["country_code"]) |
||
628 | country_0.append(c[0]) |
||
629 | country_1.append(c[1]) |
||
630 | |||
631 | gas_pipelines_list["country_0"] = country_0 |
||
632 | gas_pipelines_list["country_1"] = country_1 |
||
633 | |||
634 | # Correct non valid neighbouring country nodes |
||
635 | gas_pipelines_list.loc[ |
||
636 | gas_pipelines_list["country_0"] == "XX", "country_0" |
||
637 | ] = "NO" |
||
638 | gas_pipelines_list.loc[ |
||
639 | gas_pipelines_list["country_1"] == "FI", "country_1" |
||
640 | ] = "RU" |
||
641 | gas_pipelines_list.loc[ |
||
642 | gas_pipelines_list["id"] == "ST_2612_Seg_0_Seg_0", "country_0" |
||
643 | ] = "AT" # bus "INET_N_1182" DE -> AT |
||
644 | gas_pipelines_list.loc[ |
||
645 | gas_pipelines_list["id"] == "INET_PL_385_EE_3_Seg_0_Seg_1", "country_1" |
||
646 | ] = "AT" # "INET_N_1182" DE -> AT |
||
647 | gas_pipelines_list.loc[ |
||
648 | gas_pipelines_list["id"] == "LKD_PS_0_Seg_0_Seg_3", "country_0" |
||
649 | ] = "NL" # bus "SEQ_10608_p" DE -> NL |
||
650 | |||
651 | # Remove uncorrect pipelines |
||
652 | gas_pipelines_list = gas_pipelines_list[ |
||
653 | (gas_pipelines_list["id"] != "PLNG_2637_Seg_0_Seg_0_Seg_0") |
||
654 | & (gas_pipelines_list["id"] != "NSG_6650_Seg_2_Seg_0") |
||
655 | & (gas_pipelines_list["id"] != "NSG_6734_Seg_2_Seg_0") |
||
656 | ] |
||
657 | |||
658 | # Remove link test if length = 0 |
||
659 | gas_pipelines_list = gas_pipelines_list[ |
||
660 | gas_pipelines_list["length_km"] != 0 |
||
661 | ] |
||
662 | |||
663 | # Adjust columns |
||
664 | bus0 = [] |
||
665 | bus1 = [] |
||
666 | geom_adjusted = [] |
||
667 | topo_adjusted = [] |
||
668 | length_adjusted = [] |
||
669 | pipe_class = [] |
||
670 | |||
671 | for index, row in gas_pipelines_list.iterrows(): |
||
672 | buses = row["node_id"].strip("][").split(", ") |
||
673 | |||
674 | if ( |
||
675 | (boundary != "Everything") |
||
676 | & (row["NUTS1_0"] != map_states[boundary]) |
||
677 | & (row["country_0"] == "DE") |
||
678 | ): |
||
679 | bus0.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
680 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
681 | long_e = [ |
||
682 | abroad_gas_nodes_list.loc["DE", "x"], |
||
683 | json.loads(row["long"])[1], |
||
684 | ] |
||
685 | lat_e = [ |
||
686 | abroad_gas_nodes_list.loc["DE", "y"], |
||
687 | json.loads(row["lat"])[1], |
||
688 | ] |
||
689 | geom_pipe = geometry.MultiLineString( |
||
690 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
691 | ) |
||
692 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
693 | |||
694 | elif row["country_0"] != "DE": |
||
695 | country = str(row["country_0"]) |
||
696 | bus0.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
697 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
698 | long_e = [ |
||
699 | abroad_gas_nodes_list.loc[country, "x"], |
||
700 | json.loads(row["long"])[1], |
||
701 | ] |
||
702 | lat_e = [ |
||
703 | abroad_gas_nodes_list.loc[country, "y"], |
||
704 | json.loads(row["lat"])[1], |
||
705 | ] |
||
706 | geom_pipe = geometry.MultiLineString( |
||
707 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
708 | ) |
||
709 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
710 | |||
711 | elif ( |
||
712 | (boundary != "Everything") |
||
713 | & (row["NUTS1_1"] != map_states[boundary]) |
||
714 | & (row["country_1"] == "DE") |
||
715 | ): |
||
716 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
717 | bus1.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
718 | long_e = [ |
||
719 | json.loads(row["long"])[0], |
||
720 | abroad_gas_nodes_list.loc["DE", "x"], |
||
721 | ] |
||
722 | lat_e = [ |
||
723 | json.loads(row["lat"])[0], |
||
724 | abroad_gas_nodes_list.loc["DE", "y"], |
||
725 | ] |
||
726 | geom_pipe = geometry.MultiLineString( |
||
727 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
728 | ) |
||
729 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
730 | |||
731 | elif row["country_1"] != "DE": |
||
732 | country = str(row["country_1"]) |
||
733 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
734 | bus1.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
735 | long_e = [ |
||
736 | json.loads(row["long"])[0], |
||
737 | abroad_gas_nodes_list.loc[country, "x"], |
||
738 | ] |
||
739 | lat_e = [ |
||
740 | json.loads(row["lat"])[0], |
||
741 | abroad_gas_nodes_list.loc[country, "y"], |
||
742 | ] |
||
743 | geom_pipe = geometry.MultiLineString( |
||
744 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
745 | ) |
||
746 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
747 | |||
748 | else: |
||
749 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
750 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
751 | geom_pipe = row["geom"] |
||
752 | topo_adjusted.append(row["topo"]) |
||
753 | |||
754 | geom_adjusted.append(geom_pipe) |
||
755 | length_adjusted.append(geom_pipe.length) |
||
756 | |||
757 | if row["diameter"] >= 1000: |
||
758 | pipe_class = "A" |
||
759 | elif 700 <= row["diameter"] <= 1000: |
||
760 | pipe_class = "B" |
||
761 | elif 500 <= row["diameter"] <= 700: |
||
762 | pipe_class = "C" |
||
763 | elif 350 <= row["diameter"] <= 500: |
||
764 | pipe_class = "D" |
||
765 | elif 200 <= row["diameter"] <= 350: |
||
766 | pipe_class = "E" |
||
767 | elif 100 <= row["diameter"] <= 200: |
||
768 | pipe_class = "F" |
||
769 | elif row["diameter"] <= 100: |
||
770 | pipe_class = "G" |
||
771 | |||
772 | gas_pipelines_list["bus0"] = bus0 |
||
773 | gas_pipelines_list["bus1"] = bus1 |
||
774 | gas_pipelines_list["geom"] = geom_adjusted |
||
775 | gas_pipelines_list["topo"] = topo_adjusted |
||
776 | gas_pipelines_list["length"] = length_adjusted |
||
777 | gas_pipelines_list["pipe_class"] = pipe_class |
||
778 | |||
779 | # Remove pipes having the same node for start and end |
||
780 | gas_pipelines_list = gas_pipelines_list[ |
||
781 | gas_pipelines_list["bus0"] != gas_pipelines_list["bus1"] |
||
782 | ] |
||
783 | |||
784 | gas_pipelines_list = gas_pipelines_list.merge( |
||
785 | classification, |
||
786 | how="left", |
||
787 | left_on="pipe_class", |
||
788 | right_on="classification", |
||
789 | ) |
||
790 | gas_pipelines_list["p_nom"] = gas_pipelines_list[ |
||
791 | "max_transport_capacity_Gwh/d" |
||
792 | ] * (1000 / 24) |
||
793 | |||
794 | # Remove useless columns |
||
795 | gas_pipelines_list = gas_pipelines_list.drop( |
||
796 | columns=[ |
||
797 | "id", |
||
798 | "node_id", |
||
799 | "param", |
||
800 | "NUTS1", |
||
801 | "NUTS1_0", |
||
802 | "NUTS1_1", |
||
803 | "country_code", |
||
804 | "diameter", |
||
805 | "pipe_class", |
||
806 | "classification", |
||
807 | "max_transport_capacity_Gwh/d", |
||
808 | "lat", |
||
809 | "long", |
||
810 | "length_km", |
||
811 | ] |
||
812 | ) |
||
813 | |||
814 | return gas_pipelines_list |
||
815 | |||
1028 |