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