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