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