| Conditions | 47 | 
| Total Lines | 1081 | 
| Code Lines | 735 | 
| 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.pypsaeur.neighbor_reduction() 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 | """The central module containing all code dealing with importing data from  | 
            ||
| 578 | def neighbor_reduction():  | 
            ||
| 579 | network_solved = read_network()  | 
            ||
| 580 | network_prepared = prepared_network(planning_horizon="2045")  | 
            ||
| 581 | |||
| 582 |     # network.links.drop("pipe_retrofit", axis="columns", inplace=True) | 
            ||
| 583 | |||
| 584 | wanted_countries = [  | 
            ||
| 585 | "DE",  | 
            ||
| 586 | "AT",  | 
            ||
| 587 | "CH",  | 
            ||
| 588 | "CZ",  | 
            ||
| 589 | "PL",  | 
            ||
| 590 | "SE",  | 
            ||
| 591 | "NO",  | 
            ||
| 592 | "DK",  | 
            ||
| 593 | "GB",  | 
            ||
| 594 | "NL",  | 
            ||
| 595 | "BE",  | 
            ||
| 596 | "FR",  | 
            ||
| 597 | "LU",  | 
            ||
| 598 | ]  | 
            ||
| 599 | |||
| 600 | foreign_buses = network_solved.buses[  | 
            ||
| 601 |         (~network_solved.buses.index.str.contains("|".join(wanted_countries))) | 
            ||
| 602 |         | (network_solved.buses.index.str.contains("FR6")) | 
            ||
| 603 | ]  | 
            ||
| 604 | network_solved.buses = network_solved.buses.drop(  | 
            ||
| 605 | network_solved.buses.loc[foreign_buses.index].index  | 
            ||
| 606 | )  | 
            ||
| 607 | |||
| 608 | # Add H2 demand of Fischer-Tropsch process and methanolisation  | 
            ||
| 609 | # to industrial H2 demands  | 
            ||
| 610 | industrial_hydrogen = network_prepared.loads.loc[  | 
            ||
| 611 | network_prepared.loads.carrier == "H2 for industry"  | 
            ||
| 612 | ]  | 
            ||
| 613 | fischer_tropsch = (  | 
            ||
| 614 | network_solved.links_t.p0[  | 
            ||
| 615 | network_solved.links.loc[  | 
            ||
| 616 | network_solved.links.carrier == "Fischer-Tropsch"  | 
            ||
| 617 | ].index  | 
            ||
| 618 | ]  | 
            ||
| 619 | .mul(network_solved.snapshot_weightings.generators, axis=0)  | 
            ||
| 620 | .sum()  | 
            ||
| 621 | )  | 
            ||
| 622 | methanolisation = (  | 
            ||
| 623 | network_solved.links_t.p0[  | 
            ||
| 624 | network_solved.links.loc[  | 
            ||
| 625 | network_solved.links.carrier == "methanolisation"  | 
            ||
| 626 | ].index  | 
            ||
| 627 | ]  | 
            ||
| 628 | .mul(network_solved.snapshot_weightings.generators, axis=0)  | 
            ||
| 629 | .sum()  | 
            ||
| 630 | )  | 
            ||
| 631 | for i, row in industrial_hydrogen.iterrows():  | 
            ||
| 632 | network_prepared.loads.loc[i, "p_set"] += (  | 
            ||
| 633 | fischer_tropsch[  | 
            ||
| 634 | fischer_tropsch.index.str.startswith(row.bus[:5])  | 
            ||
| 635 | ].sum()  | 
            ||
| 636 | / 8760  | 
            ||
| 637 | )  | 
            ||
| 638 | network_prepared.loads.loc[i, "p_set"] += (  | 
            ||
| 639 | methanolisation[  | 
            ||
| 640 | methanolisation.index.str.startswith(row.bus[:5])  | 
            ||
| 641 | ].sum()  | 
            ||
| 642 | / 8760  | 
            ||
| 643 | )  | 
            ||
| 644 | # drop foreign lines and links from the 2nd row  | 
            ||
| 645 | |||
| 646 | network_solved.lines = network_solved.lines.drop(  | 
            ||
| 647 | network_solved.lines[  | 
            ||
| 648 | (  | 
            ||
| 649 | network_solved.lines["bus0"].isin(network_solved.buses.index)  | 
            ||
| 650 | == False  | 
            ||
| 651 | )  | 
            ||
| 652 | & (  | 
            ||
| 653 | network_solved.lines["bus1"].isin(network_solved.buses.index)  | 
            ||
| 654 | == False  | 
            ||
| 655 | )  | 
            ||
| 656 | ].index  | 
            ||
| 657 | )  | 
            ||
| 658 | |||
| 659 | # select all lines which have at bus1 the bus which is kept  | 
            ||
| 660 | lines_cb_1 = network_solved.lines[  | 
            ||
| 661 | (  | 
            ||
| 662 | network_solved.lines["bus0"].isin(network_solved.buses.index)  | 
            ||
| 663 | == False  | 
            ||
| 664 | )  | 
            ||
| 665 | ]  | 
            ||
| 666 | |||
| 667 | # create a load at bus1 with the line's hourly loading  | 
            ||
| 668 | for i, k in zip(lines_cb_1.bus1.values, lines_cb_1.index):  | 
            ||
| 669 | |||
| 670 | # Copy loading of lines into hourly resolution  | 
            ||
| 671 | pset = pd.Series(  | 
            ||
| 672 | index=network_prepared.snapshots,  | 
            ||
| 673 |             data=network_solved.lines_t.p1[k].resample("H").ffill(), | 
            ||
| 674 | )  | 
            ||
| 675 | pset["2011-12-31 22:00:00"] = pset["2011-12-31 21:00:00"]  | 
            ||
| 676 | pset["2011-12-31 23:00:00"] = pset["2011-12-31 21:00:00"]  | 
            ||
| 677 | |||
| 678 | # Loads are all imported from the prepared network in the end  | 
            ||
| 679 | network_prepared.add(  | 
            ||
| 680 | "Load",  | 
            ||
| 681 | "slack_fix " + i + " " + k,  | 
            ||
| 682 | bus=i,  | 
            ||
| 683 | p_set=pset,  | 
            ||
| 684 | carrier=lines_cb_1.loc[k, "carrier"],  | 
            ||
| 685 | )  | 
            ||
| 686 | |||
| 687 | # select all lines which have at bus0 the bus which is kept  | 
            ||
| 688 | lines_cb_0 = network_solved.lines[  | 
            ||
| 689 | (  | 
            ||
| 690 | network_solved.lines["bus1"].isin(network_solved.buses.index)  | 
            ||
| 691 | == False  | 
            ||
| 692 | )  | 
            ||
| 693 | ]  | 
            ||
| 694 | |||
| 695 | # create a load at bus0 with the line's hourly loading  | 
            ||
| 696 | for i, k in zip(lines_cb_0.bus0.values, lines_cb_0.index):  | 
            ||
| 697 | # Copy loading of lines into hourly resolution  | 
            ||
| 698 | pset = pd.Series(  | 
            ||
| 699 | index=network_prepared.snapshots,  | 
            ||
| 700 |             data=network_solved.lines_t.p0[k].resample("H").ffill(), | 
            ||
| 701 | )  | 
            ||
| 702 | pset["2011-12-31 22:00:00"] = pset["2011-12-31 21:00:00"]  | 
            ||
| 703 | pset["2011-12-31 23:00:00"] = pset["2011-12-31 21:00:00"]  | 
            ||
| 704 | |||
| 705 | network_prepared.add(  | 
            ||
| 706 | "Load",  | 
            ||
| 707 | "slack_fix " + i + " " + k,  | 
            ||
| 708 | bus=i,  | 
            ||
| 709 | p_set=pset,  | 
            ||
| 710 | carrier=lines_cb_0.loc[k, "carrier"],  | 
            ||
| 711 | )  | 
            ||
| 712 | |||
| 713 | # do the same for links  | 
            ||
| 714 | network_solved.mremove(  | 
            ||
| 715 | "Link",  | 
            ||
| 716 | network_solved.links[  | 
            ||
| 717 | (~network_solved.links.bus0.isin(network_solved.buses.index))  | 
            ||
| 718 | | (~network_solved.links.bus1.isin(network_solved.buses.index))  | 
            ||
| 719 | ].index,  | 
            ||
| 720 | )  | 
            ||
| 721 | |||
| 722 | # select all links which have at bus1 the bus which is kept  | 
            ||
| 723 | links_cb_1 = network_solved.links[  | 
            ||
| 724 | (  | 
            ||
| 725 | network_solved.links["bus0"].isin(network_solved.buses.index)  | 
            ||
| 726 | == False  | 
            ||
| 727 | )  | 
            ||
| 728 | ]  | 
            ||
| 729 | |||
| 730 | # create a load at bus1 with the link's hourly loading  | 
            ||
| 731 | for i, k in zip(links_cb_1.bus1.values, links_cb_1.index):  | 
            ||
| 732 | pset = pd.Series(  | 
            ||
| 733 | index=network_prepared.snapshots,  | 
            ||
| 734 |             data=network_solved.links_t.p1[k].resample("H").ffill(), | 
            ||
| 735 | )  | 
            ||
| 736 | pset["2011-12-31 22:00:00"] = pset["2011-12-31 21:00:00"]  | 
            ||
| 737 | pset["2011-12-31 23:00:00"] = pset["2011-12-31 21:00:00"]  | 
            ||
| 738 | |||
| 739 | network_prepared.add(  | 
            ||
| 740 | "Load",  | 
            ||
| 741 | "slack_fix_links " + i + " " + k,  | 
            ||
| 742 | bus=i,  | 
            ||
| 743 | p_set=pset,  | 
            ||
| 744 | carrier=links_cb_1.loc[k, "carrier"],  | 
            ||
| 745 | )  | 
            ||
| 746 | |||
| 747 | # select all links which have at bus0 the bus which is kept  | 
            ||
| 748 | links_cb_0 = network_solved.links[  | 
            ||
| 749 | (  | 
            ||
| 750 | network_solved.links["bus1"].isin(network_solved.buses.index)  | 
            ||
| 751 | == False  | 
            ||
| 752 | )  | 
            ||
| 753 | ]  | 
            ||
| 754 | |||
| 755 | # create a load at bus0 with the link's hourly loading  | 
            ||
| 756 | for i, k in zip(links_cb_0.bus0.values, links_cb_0.index):  | 
            ||
| 757 | pset = pd.Series(  | 
            ||
| 758 | index=network_prepared.snapshots,  | 
            ||
| 759 |             data=network_solved.links_t.p0[k].resample("H").ffill(), | 
            ||
| 760 | )  | 
            ||
| 761 | pset["2011-12-31 22:00:00"] = pset["2011-12-31 21:00:00"]  | 
            ||
| 762 | pset["2011-12-31 23:00:00"] = pset["2011-12-31 21:00:00"]  | 
            ||
| 763 | |||
| 764 | network_prepared.add(  | 
            ||
| 765 | "Load",  | 
            ||
| 766 | "slack_fix_links " + i + " " + k,  | 
            ||
| 767 | bus=i,  | 
            ||
| 768 | p_set=pset,  | 
            ||
| 769 | carrier=links_cb_0.carrier[k],  | 
            ||
| 770 | )  | 
            ||
| 771 | |||
| 772 | # drop remaining foreign components  | 
            ||
| 773 | for comp in network_solved.iterate_components():  | 
            ||
| 774 | if "bus0" in comp.df.columns:  | 
            ||
| 775 | network_solved.mremove(  | 
            ||
| 776 | comp.name,  | 
            ||
| 777 | comp.df[~comp.df.bus0.isin(network_solved.buses.index)].index,  | 
            ||
| 778 | )  | 
            ||
| 779 | network_solved.mremove(  | 
            ||
| 780 | comp.name,  | 
            ||
| 781 | comp.df[~comp.df.bus1.isin(network_solved.buses.index)].index,  | 
            ||
| 782 | )  | 
            ||
| 783 | elif "bus" in comp.df.columns:  | 
            ||
| 784 | network_solved.mremove(  | 
            ||
| 785 | comp.name,  | 
            ||
| 786 | comp.df[~comp.df.bus.isin(network_solved.buses.index)].index,  | 
            ||
| 787 | )  | 
            ||
| 788 | |||
| 789 | # Combine urban decentral and rural heat  | 
            ||
| 790 | network_prepared, network_solved = combine_decentral_and_rural_heat(  | 
            ||
| 791 | network_solved, network_prepared  | 
            ||
| 792 | )  | 
            ||
| 793 | |||
| 794 | # writing components of neighboring countries to etrago tables  | 
            ||
| 795 | |||
| 796 | # Set country tag for all buses  | 
            ||
| 797 | network_solved.buses.country = network_solved.buses.index.str[:2]  | 
            ||
| 798 | neighbors = network_solved.buses[network_solved.buses.country != "DE"]  | 
            ||
| 799 | |||
| 800 | neighbors["new_index"] = (  | 
            ||
| 801 |         db.next_etrago_id("bus") + neighbors.reset_index().index | 
            ||
| 802 | )  | 
            ||
| 803 | |||
| 804 | # Use index of AC buses created by electrical_neigbors  | 
            ||
| 805 | foreign_ac_buses = db.select_dataframe(  | 
            ||
| 806 | """  | 
            ||
| 807 | SELECT * FROM grid.egon_etrago_bus  | 
            ||
| 808 | WHERE carrier = 'AC' AND v_nom = 380  | 
            ||
| 809 | AND country!= 'DE' AND scn_name ='eGon100RE'  | 
            ||
| 810 | AND bus_id NOT IN (SELECT bus_i FROM osmtgmod_results.bus_data)  | 
            ||
| 811 | """  | 
            ||
| 812 | )  | 
            ||
| 813 | buses_with_defined_id = neighbors[  | 
            ||
| 814 | (neighbors.carrier == "AC")  | 
            ||
| 815 | & (neighbors.country.isin(foreign_ac_buses.country.values))  | 
            ||
| 816 | ].index  | 
            ||
| 817 | neighbors.loc[buses_with_defined_id, "new_index"] = (  | 
            ||
| 818 |         foreign_ac_buses.set_index("x") | 
            ||
| 819 | .loc[neighbors.loc[buses_with_defined_id, "x"]]  | 
            ||
| 820 | .bus_id.values  | 
            ||
| 821 | )  | 
            ||
| 822 | |||
| 823 | # lines, the foreign crossborder lines  | 
            ||
| 824 | # (without crossborder lines to Germany!)  | 
            ||
| 825 | |||
| 826 | neighbor_lines = network_solved.lines[  | 
            ||
| 827 | network_solved.lines.bus0.isin(neighbors.index)  | 
            ||
| 828 | & network_solved.lines.bus1.isin(neighbors.index)  | 
            ||
| 829 | ]  | 
            ||
| 830 | if not network_solved.lines_t["s_max_pu"].empty:  | 
            ||
| 831 | neighbor_lines_t = network_prepared.lines_t["s_max_pu"][  | 
            ||
| 832 | neighbor_lines.index  | 
            ||
| 833 | ]  | 
            ||
| 834 | |||
| 835 | neighbor_lines.reset_index(inplace=True)  | 
            ||
| 836 | neighbor_lines.bus0 = (  | 
            ||
| 837 | neighbors.loc[neighbor_lines.bus0, "new_index"].reset_index().new_index  | 
            ||
| 838 | )  | 
            ||
| 839 | neighbor_lines.bus1 = (  | 
            ||
| 840 | neighbors.loc[neighbor_lines.bus1, "new_index"].reset_index().new_index  | 
            ||
| 841 | )  | 
            ||
| 842 |     neighbor_lines.index += db.next_etrago_id("line") | 
            ||
| 843 | |||
| 844 | if not network_solved.lines_t["s_max_pu"].empty:  | 
            ||
| 845 | for i in neighbor_lines_t.columns:  | 
            ||
| 846 | new_index = neighbor_lines[neighbor_lines["name"] == i].index  | 
            ||
| 847 |             neighbor_lines_t.rename(columns={i: new_index[0]}, inplace=True) | 
            ||
| 848 | |||
| 849 | # links  | 
            ||
| 850 | neighbor_links = network_solved.links[  | 
            ||
| 851 | network_solved.links.bus0.isin(neighbors.index)  | 
            ||
| 852 | & network_solved.links.bus1.isin(neighbors.index)  | 
            ||
| 853 | ]  | 
            ||
| 854 | |||
| 855 | neighbor_links.reset_index(inplace=True)  | 
            ||
| 856 | neighbor_links.bus0 = (  | 
            ||
| 857 | neighbors.loc[neighbor_links.bus0, "new_index"].reset_index().new_index  | 
            ||
| 858 | )  | 
            ||
| 859 | neighbor_links.bus1 = (  | 
            ||
| 860 | neighbors.loc[neighbor_links.bus1, "new_index"].reset_index().new_index  | 
            ||
| 861 | )  | 
            ||
| 862 |     neighbor_links.index += db.next_etrago_id("link") | 
            ||
| 863 | |||
| 864 | # generators  | 
            ||
| 865 | neighbor_gens = network_solved.generators[  | 
            ||
| 866 | network_solved.generators.bus.isin(neighbors.index)  | 
            ||
| 867 | ]  | 
            ||
| 868 | neighbor_gens_t = network_prepared.generators_t["p_max_pu"][  | 
            ||
| 869 | neighbor_gens[  | 
            ||
| 870 | neighbor_gens.index.isin(  | 
            ||
| 871 | network_prepared.generators_t["p_max_pu"].columns  | 
            ||
| 872 | )  | 
            ||
| 873 | ].index  | 
            ||
| 874 | ]  | 
            ||
| 875 | |||
| 876 | gen_time = [  | 
            ||
| 877 | "solar",  | 
            ||
| 878 | "onwind",  | 
            ||
| 879 | "solar rooftop",  | 
            ||
| 880 | "offwind-ac",  | 
            ||
| 881 | "offwind-dc",  | 
            ||
| 882 | "solar-hsat",  | 
            ||
| 883 | "urban central solar thermal",  | 
            ||
| 884 | "rural solar thermal",  | 
            ||
| 885 | "offwind-float",  | 
            ||
| 886 | ]  | 
            ||
| 887 | |||
| 888 | missing_gent = neighbor_gens[  | 
            ||
| 889 | neighbor_gens["carrier"].isin(gen_time)  | 
            ||
| 890 | & ~neighbor_gens.index.isin(neighbor_gens_t.columns)  | 
            ||
| 891 | ].index  | 
            ||
| 892 | |||
| 893 | gen_timeseries = network_prepared.generators_t["p_max_pu"].copy()  | 
            ||
| 894 | for mgt in missing_gent: # mgt: missing generator timeseries  | 
            ||
| 895 | try:  | 
            ||
| 896 | neighbor_gens_t[mgt] = gen_timeseries.loc[:, mgt[0:-5]]  | 
            ||
| 897 | except:  | 
            ||
| 898 |             print(f"There are not timeseries for {mgt}") | 
            ||
| 899 | |||
| 900 | neighbor_gens.reset_index(inplace=True)  | 
            ||
| 901 | neighbor_gens.bus = (  | 
            ||
| 902 | neighbors.loc[neighbor_gens.bus, "new_index"].reset_index().new_index  | 
            ||
| 903 | )  | 
            ||
| 904 |     neighbor_gens.index += db.next_etrago_id("generator") | 
            ||
| 905 | |||
| 906 | for i in neighbor_gens_t.columns:  | 
            ||
| 907 | new_index = neighbor_gens[neighbor_gens["Generator"] == i].index  | 
            ||
| 908 |         neighbor_gens_t.rename(columns={i: new_index[0]}, inplace=True) | 
            ||
| 909 | |||
| 910 | # loads  | 
            ||
| 911 | # imported from prenetwork in 1h-resolution  | 
            ||
| 912 | neighbor_loads = network_prepared.loads[  | 
            ||
| 913 | network_prepared.loads.bus.isin(neighbors.index)  | 
            ||
| 914 | ]  | 
            ||
| 915 | neighbor_loads_t_index = neighbor_loads.index[  | 
            ||
| 916 | neighbor_loads.index.isin(network_prepared.loads_t.p_set.columns)  | 
            ||
| 917 | ]  | 
            ||
| 918 | neighbor_loads_t = network_prepared.loads_t["p_set"][  | 
            ||
| 919 | neighbor_loads_t_index  | 
            ||
| 920 | ]  | 
            ||
| 921 | |||
| 922 | neighbor_loads.reset_index(inplace=True)  | 
            ||
| 923 | neighbor_loads.bus = (  | 
            ||
| 924 | neighbors.loc[neighbor_loads.bus, "new_index"].reset_index().new_index  | 
            ||
| 925 | )  | 
            ||
| 926 |     neighbor_loads.index += db.next_etrago_id("load") | 
            ||
| 927 | |||
| 928 | for i in neighbor_loads_t.columns:  | 
            ||
| 929 | new_index = neighbor_loads[neighbor_loads["Load"] == i].index  | 
            ||
| 930 |         neighbor_loads_t.rename(columns={i: new_index[0]}, inplace=True) | 
            ||
| 931 | |||
| 932 | # stores  | 
            ||
| 933 | neighbor_stores = network_solved.stores[  | 
            ||
| 934 | network_solved.stores.bus.isin(neighbors.index)  | 
            ||
| 935 | ]  | 
            ||
| 936 | neighbor_stores_t_index = neighbor_stores.index[  | 
            ||
| 937 | neighbor_stores.index.isin(network_solved.stores_t.e_min_pu.columns)  | 
            ||
| 938 | ]  | 
            ||
| 939 | neighbor_stores_t = network_prepared.stores_t["e_min_pu"][  | 
            ||
| 940 | neighbor_stores_t_index  | 
            ||
| 941 | ]  | 
            ||
| 942 | |||
| 943 | neighbor_stores.reset_index(inplace=True)  | 
            ||
| 944 | neighbor_stores.bus = (  | 
            ||
| 945 | neighbors.loc[neighbor_stores.bus, "new_index"].reset_index().new_index  | 
            ||
| 946 | )  | 
            ||
| 947 |     neighbor_stores.index += db.next_etrago_id("store") | 
            ||
| 948 | |||
| 949 | for i in neighbor_stores_t.columns:  | 
            ||
| 950 | new_index = neighbor_stores[neighbor_stores["Store"] == i].index  | 
            ||
| 951 |         neighbor_stores_t.rename(columns={i: new_index[0]}, inplace=True) | 
            ||
| 952 | |||
| 953 | # storage_units  | 
            ||
| 954 | neighbor_storage = network_solved.storage_units[  | 
            ||
| 955 | network_solved.storage_units.bus.isin(neighbors.index)  | 
            ||
| 956 | ]  | 
            ||
| 957 | neighbor_storage_t_index = neighbor_storage.index[  | 
            ||
| 958 | neighbor_storage.index.isin(  | 
            ||
| 959 | network_solved.storage_units_t.inflow.columns  | 
            ||
| 960 | )  | 
            ||
| 961 | ]  | 
            ||
| 962 | neighbor_storage_t = network_prepared.storage_units_t["inflow"][  | 
            ||
| 963 | neighbor_storage_t_index  | 
            ||
| 964 | ]  | 
            ||
| 965 | |||
| 966 | neighbor_storage.reset_index(inplace=True)  | 
            ||
| 967 | neighbor_storage.bus = (  | 
            ||
| 968 | neighbors.loc[neighbor_storage.bus, "new_index"]  | 
            ||
| 969 | .reset_index()  | 
            ||
| 970 | .new_index  | 
            ||
| 971 | )  | 
            ||
| 972 |     neighbor_storage.index += db.next_etrago_id("storage") | 
            ||
| 973 | |||
| 974 | for i in neighbor_storage_t.columns:  | 
            ||
| 975 | new_index = neighbor_storage[  | 
            ||
| 976 | neighbor_storage["StorageUnit"] == i  | 
            ||
| 977 | ].index  | 
            ||
| 978 |         neighbor_storage_t.rename(columns={i: new_index[0]}, inplace=True) | 
            ||
| 979 | |||
| 980 | # Connect to local database  | 
            ||
| 981 | engine = db.engine()  | 
            ||
| 982 | |||
| 983 | neighbors["scn_name"] = "eGon100RE"  | 
            ||
| 984 | neighbors.index = neighbors["new_index"]  | 
            ||
| 985 | |||
| 986 | # Correct geometry for non AC buses  | 
            ||
| 987 | carriers = set(neighbors.carrier.to_list())  | 
            ||
| 988 |     carriers = [e for e in carriers if e not in ("AC")] | 
            ||
| 989 | non_AC_neighbors = pd.DataFrame()  | 
            ||
| 990 | for c in carriers:  | 
            ||
| 991 | c_neighbors = neighbors[neighbors.carrier == c].set_index(  | 
            ||
| 992 | "location", drop=False  | 
            ||
| 993 | )  | 
            ||
| 994 | for i in ["x", "y"]:  | 
            ||
| 995 | c_neighbors = c_neighbors.drop(i, axis=1)  | 
            ||
| 996 | coordinates = neighbors[neighbors.carrier == "AC"][  | 
            ||
| 997 | ["location", "x", "y"]  | 
            ||
| 998 |         ].set_index("location") | 
            ||
| 999 | c_neighbors = pd.concat([coordinates, c_neighbors], axis=1).set_index(  | 
            ||
| 1000 | "new_index", drop=False  | 
            ||
| 1001 | )  | 
            ||
| 1002 | non_AC_neighbors = pd.concat([non_AC_neighbors, c_neighbors])  | 
            ||
| 1003 | |||
| 1004 | neighbors = pd.concat(  | 
            ||
| 1005 | [neighbors[neighbors.carrier == "AC"], non_AC_neighbors]  | 
            ||
| 1006 | )  | 
            ||
| 1007 | |||
| 1008 | for i in [  | 
            ||
| 1009 | "new_index",  | 
            ||
| 1010 | "control",  | 
            ||
| 1011 | "generator",  | 
            ||
| 1012 | "location",  | 
            ||
| 1013 | "sub_network",  | 
            ||
| 1014 | "unit",  | 
            ||
| 1015 | "substation_lv",  | 
            ||
| 1016 | "substation_off",  | 
            ||
| 1017 | ]:  | 
            ||
| 1018 | neighbors = neighbors.drop(i, axis=1)  | 
            ||
| 1019 | |||
| 1020 | # Add geometry column  | 
            ||
| 1021 | neighbors = (  | 
            ||
| 1022 | gpd.GeoDataFrame(  | 
            ||
| 1023 | neighbors, geometry=gpd.points_from_xy(neighbors.x, neighbors.y)  | 
            ||
| 1024 | )  | 
            ||
| 1025 |         .rename_geometry("geom") | 
            ||
| 1026 | .set_crs(4326)  | 
            ||
| 1027 | )  | 
            ||
| 1028 | |||
| 1029 | # Unify carrier names  | 
            ||
| 1030 |     neighbors.carrier = neighbors.carrier.str.replace(" ", "_") | 
            ||
| 1031 | neighbors.carrier.replace(  | 
            ||
| 1032 |         { | 
            ||
| 1033 | "gas": "CH4",  | 
            ||
| 1034 | "gas_for_industry": "CH4_for_industry",  | 
            ||
| 1035 | "urban_central_heat": "central_heat",  | 
            ||
| 1036 | "EV_battery": "Li_ion",  | 
            ||
| 1037 | "urban_central_water_tanks": "central_heat_store",  | 
            ||
| 1038 | "rural_water_tanks": "rural_heat_store",  | 
            ||
| 1039 | },  | 
            ||
| 1040 | inplace=True,  | 
            ||
| 1041 | )  | 
            ||
| 1042 | |||
| 1043 | neighbors[~neighbors.carrier.isin(["AC"])].to_postgis(  | 
            ||
| 1044 | "egon_etrago_bus",  | 
            ||
| 1045 | engine,  | 
            ||
| 1046 | schema="grid",  | 
            ||
| 1047 | if_exists="append",  | 
            ||
| 1048 | index=True,  | 
            ||
| 1049 | index_label="bus_id",  | 
            ||
| 1050 | )  | 
            ||
| 1051 | |||
| 1052 | # prepare and write neighboring crossborder lines to etrago tables  | 
            ||
| 1053 | def lines_to_etrago(neighbor_lines=neighbor_lines, scn="eGon100RE"):  | 
            ||
| 1054 | neighbor_lines["scn_name"] = scn  | 
            ||
| 1055 | neighbor_lines["cables"] = 3 * neighbor_lines["num_parallel"].astype(  | 
            ||
| 1056 | int  | 
            ||
| 1057 | )  | 
            ||
| 1058 | neighbor_lines["s_nom"] = neighbor_lines["s_nom_min"]  | 
            ||
| 1059 | |||
| 1060 | for i in [  | 
            ||
| 1061 | "Line",  | 
            ||
| 1062 | "x_pu_eff",  | 
            ||
| 1063 | "r_pu_eff",  | 
            ||
| 1064 | "sub_network",  | 
            ||
| 1065 | "x_pu",  | 
            ||
| 1066 | "r_pu",  | 
            ||
| 1067 | "g_pu",  | 
            ||
| 1068 | "b_pu",  | 
            ||
| 1069 | "s_nom_opt",  | 
            ||
| 1070 | "i_nom",  | 
            ||
| 1071 | "dc",  | 
            ||
| 1072 | ]:  | 
            ||
| 1073 | neighbor_lines = neighbor_lines.drop(i, axis=1)  | 
            ||
| 1074 | |||
| 1075 | # Define geometry and add to lines dataframe as 'topo'  | 
            ||
| 1076 | gdf = gpd.GeoDataFrame(index=neighbor_lines.index)  | 
            ||
| 1077 | gdf["geom_bus0"] = neighbors.geom[neighbor_lines.bus0].values  | 
            ||
| 1078 | gdf["geom_bus1"] = neighbors.geom[neighbor_lines.bus1].values  | 
            ||
| 1079 | gdf["geometry"] = gdf.apply(  | 
            ||
| 1080 | lambda x: LineString([x["geom_bus0"], x["geom_bus1"]]), axis=1  | 
            ||
| 1081 | )  | 
            ||
| 1082 | |||
| 1083 | neighbor_lines = (  | 
            ||
| 1084 | gpd.GeoDataFrame(neighbor_lines, geometry=gdf["geometry"])  | 
            ||
| 1085 |             .rename_geometry("topo") | 
            ||
| 1086 | .set_crs(4326)  | 
            ||
| 1087 | )  | 
            ||
| 1088 | |||
| 1089 |         neighbor_lines["lifetime"] = get_sector_parameters("electricity", scn)[ | 
            ||
| 1090 | "lifetime"  | 
            ||
| 1091 | ]["ac_ehv_overhead_line"]  | 
            ||
| 1092 | |||
| 1093 | neighbor_lines.to_postgis(  | 
            ||
| 1094 | "egon_etrago_line",  | 
            ||
| 1095 | engine,  | 
            ||
| 1096 | schema="grid",  | 
            ||
| 1097 | if_exists="append",  | 
            ||
| 1098 | index=True,  | 
            ||
| 1099 | index_label="line_id",  | 
            ||
| 1100 | )  | 
            ||
| 1101 | |||
| 1102 | lines_to_etrago(neighbor_lines=neighbor_lines, scn="eGon100RE")  | 
            ||
| 1103 | |||
| 1104 | def links_to_etrago(neighbor_links, scn="eGon100RE", extendable=True):  | 
            ||
| 1105 | """Prepare and write neighboring crossborder links to eTraGo table  | 
            ||
| 1106 | |||
| 1107 | This function prepare the neighboring crossborder links  | 
            ||
| 1108 | generated the PyPSA-eur-sec (p-e-s) run by:  | 
            ||
| 1109 | * Delete the useless columns  | 
            ||
| 1110 | * If extendable is false only (non default case):  | 
            ||
| 1111 | * Replace p_nom = 0 with the p_nom_op values (arrising  | 
            ||
| 1112 | from the p-e-s optimisation)  | 
            ||
| 1113 | * Setting p_nom_extendable to false  | 
            ||
| 1114 | * Add geomtry to the links: 'geom' and 'topo' columns  | 
            ||
| 1115 | * Change the name of the carriers to have the consistent in  | 
            ||
| 1116 | eGon-data  | 
            ||
| 1117 | |||
| 1118 | The function insert then the link to the eTraGo table and has  | 
            ||
| 1119 | no return.  | 
            ||
| 1120 | |||
| 1121 | Parameters  | 
            ||
| 1122 | ----------  | 
            ||
| 1123 | neighbor_links : pandas.DataFrame  | 
            ||
| 1124 | Dataframe containing the neighboring crossborder links  | 
            ||
| 1125 | scn_name : str  | 
            ||
| 1126 | Name of the scenario  | 
            ||
| 1127 | extendable : bool  | 
            ||
| 1128 | Boolean expressing if the links should be extendable or not  | 
            ||
| 1129 | |||
| 1130 | Returns  | 
            ||
| 1131 | -------  | 
            ||
| 1132 | None  | 
            ||
| 1133 | |||
| 1134 | """  | 
            ||
| 1135 | neighbor_links["scn_name"] = scn  | 
            ||
| 1136 | |||
| 1137 | dropped_carriers = [  | 
            ||
| 1138 | "Link",  | 
            ||
| 1139 | "geometry",  | 
            ||
| 1140 | "tags",  | 
            ||
| 1141 | "under_construction",  | 
            ||
| 1142 | "underground",  | 
            ||
| 1143 | "underwater_fraction",  | 
            ||
| 1144 | "bus2",  | 
            ||
| 1145 | "bus3",  | 
            ||
| 1146 | "bus4",  | 
            ||
| 1147 | "efficiency2",  | 
            ||
| 1148 | "efficiency3",  | 
            ||
| 1149 | "efficiency4",  | 
            ||
| 1150 | "lifetime",  | 
            ||
| 1151 | "pipe_retrofit",  | 
            ||
| 1152 | "committable",  | 
            ||
| 1153 | "start_up_cost",  | 
            ||
| 1154 | "shut_down_cost",  | 
            ||
| 1155 | "min_up_time",  | 
            ||
| 1156 | "min_down_time",  | 
            ||
| 1157 | "up_time_before",  | 
            ||
| 1158 | "down_time_before",  | 
            ||
| 1159 | "ramp_limit_up",  | 
            ||
| 1160 | "ramp_limit_down",  | 
            ||
| 1161 | "ramp_limit_start_up",  | 
            ||
| 1162 | "ramp_limit_shut_down",  | 
            ||
| 1163 | "length_original",  | 
            ||
| 1164 | "reversed",  | 
            ||
| 1165 | "location",  | 
            ||
| 1166 | "project_status",  | 
            ||
| 1167 | "dc",  | 
            ||
| 1168 | "voltage",  | 
            ||
| 1169 | ]  | 
            ||
| 1170 | |||
| 1171 | if extendable:  | 
            ||
| 1172 |             dropped_carriers.append("p_nom_opt") | 
            ||
| 1173 | neighbor_links = neighbor_links.drop(  | 
            ||
| 1174 | columns=dropped_carriers,  | 
            ||
| 1175 | errors="ignore",  | 
            ||
| 1176 | )  | 
            ||
| 1177 | |||
| 1178 | else:  | 
            ||
| 1179 |             dropped_carriers.append("p_nom") | 
            ||
| 1180 |             dropped_carriers.append("p_nom_extendable") | 
            ||
| 1181 | neighbor_links = neighbor_links.drop(  | 
            ||
| 1182 | columns=dropped_carriers,  | 
            ||
| 1183 | errors="ignore",  | 
            ||
| 1184 | )  | 
            ||
| 1185 | neighbor_links = neighbor_links.rename(  | 
            ||
| 1186 |                 columns={"p_nom_opt": "p_nom"} | 
            ||
| 1187 | )  | 
            ||
| 1188 | neighbor_links["p_nom_extendable"] = False  | 
            ||
| 1189 | |||
| 1190 | if neighbor_links.empty:  | 
            ||
| 1191 |             print("No links selected") | 
            ||
| 1192 | return  | 
            ||
| 1193 | |||
| 1194 | # Define geometry and add to lines dataframe as 'topo'  | 
            ||
| 1195 | gdf = gpd.GeoDataFrame(  | 
            ||
| 1196 | index=neighbor_links.index,  | 
            ||
| 1197 |             data={ | 
            ||
| 1198 | "geom_bus0": neighbors.loc[neighbor_links.bus0, "geom"].values,  | 
            ||
| 1199 | "geom_bus1": neighbors.loc[neighbor_links.bus1, "geom"].values,  | 
            ||
| 1200 | },  | 
            ||
| 1201 | )  | 
            ||
| 1202 | |||
| 1203 | gdf["geometry"] = gdf.apply(  | 
            ||
| 1204 | lambda x: LineString([x["geom_bus0"], x["geom_bus1"]]), axis=1  | 
            ||
| 1205 | )  | 
            ||
| 1206 | |||
| 1207 | neighbor_links = (  | 
            ||
| 1208 | gpd.GeoDataFrame(neighbor_links, geometry=gdf["geometry"])  | 
            ||
| 1209 |             .rename_geometry("topo") | 
            ||
| 1210 | .set_crs(4326)  | 
            ||
| 1211 | )  | 
            ||
| 1212 | |||
| 1213 | # Unify carrier names  | 
            ||
| 1214 |         neighbor_links.carrier = neighbor_links.carrier.str.replace(" ", "_") | 
            ||
| 1215 | |||
| 1216 | neighbor_links.carrier.replace(  | 
            ||
| 1217 |             { | 
            ||
| 1218 | "H2_Electrolysis": "power_to_H2",  | 
            ||
| 1219 | "H2_Fuel_Cell": "H2_to_power",  | 
            ||
| 1220 | "H2_pipeline_retrofitted": "H2_retrofit",  | 
            ||
| 1221 | "SMR": "CH4_to_H2",  | 
            ||
| 1222 | "Sabatier": "H2_to_CH4",  | 
            ||
| 1223 | "gas_for_industry": "CH4_for_industry",  | 
            ||
| 1224 | "gas_pipeline": "CH4",  | 
            ||
| 1225 | "urban_central_gas_boiler": "central_gas_boiler",  | 
            ||
| 1226 | "urban_central_resistive_heater": "central_resistive_heater",  | 
            ||
| 1227 | "urban_central_water_tanks_charger": "central_heat_store_charger",  | 
            ||
| 1228 | "urban_central_water_tanks_discharger": "central_heat_store_discharger",  | 
            ||
| 1229 | "rural_water_tanks_charger": "rural_heat_store_charger",  | 
            ||
| 1230 | "rural_water_tanks_discharger": "rural_heat_store_discharger",  | 
            ||
| 1231 | "urban_central_gas_CHP": "central_gas_CHP",  | 
            ||
| 1232 | "urban_central_air_heat_pump": "central_heat_pump",  | 
            ||
| 1233 | "rural_ground_heat_pump": "rural_heat_pump",  | 
            ||
| 1234 | },  | 
            ||
| 1235 | inplace=True,  | 
            ||
| 1236 | )  | 
            ||
| 1237 | |||
| 1238 |         H2_links = { | 
            ||
| 1239 | "H2_to_CH4": "H2_to_CH4",  | 
            ||
| 1240 | "H2_to_power": "H2_to_power",  | 
            ||
| 1241 | "power_to_H2": "power_to_H2_system",  | 
            ||
| 1242 | "CH4_to_H2": "CH4_to_H2",  | 
            ||
| 1243 | }  | 
            ||
| 1244 | |||
| 1245 | for c in H2_links.keys():  | 
            ||
| 1246 | |||
| 1247 | neighbor_links.loc[  | 
            ||
| 1248 | (neighbor_links.carrier == c),  | 
            ||
| 1249 | "lifetime",  | 
            ||
| 1250 |             ] = get_sector_parameters("gas", "eGon100RE")["lifetime"][ | 
            ||
| 1251 | H2_links[c]  | 
            ||
| 1252 | ]  | 
            ||
| 1253 | |||
| 1254 | neighbor_links.to_postgis(  | 
            ||
| 1255 | "egon_etrago_link",  | 
            ||
| 1256 | engine,  | 
            ||
| 1257 | schema="grid",  | 
            ||
| 1258 | if_exists="append",  | 
            ||
| 1259 | index=True,  | 
            ||
| 1260 | index_label="link_id",  | 
            ||
| 1261 | )  | 
            ||
| 1262 | |||
| 1263 | extendable_links_carriers = [  | 
            ||
| 1264 | "battery charger",  | 
            ||
| 1265 | "battery discharger",  | 
            ||
| 1266 | "home battery charger",  | 
            ||
| 1267 | "home battery discharger",  | 
            ||
| 1268 | "rural water tanks charger",  | 
            ||
| 1269 | "rural water tanks discharger",  | 
            ||
| 1270 | "urban central water tanks charger",  | 
            ||
| 1271 | "urban central water tanks discharger",  | 
            ||
| 1272 | "urban decentral water tanks charger",  | 
            ||
| 1273 | "urban decentral water tanks discharger",  | 
            ||
| 1274 | "H2 Electrolysis",  | 
            ||
| 1275 | "H2 Fuel Cell",  | 
            ||
| 1276 | "SMR",  | 
            ||
| 1277 | "Sabatier",  | 
            ||
| 1278 | ]  | 
            ||
| 1279 | |||
| 1280 | # delete unwanted carriers for eTraGo  | 
            ||
| 1281 | excluded_carriers = [  | 
            ||
| 1282 | "gas for industry CC",  | 
            ||
| 1283 | "SMR CC",  | 
            ||
| 1284 | "DAC",  | 
            ||
| 1285 | ]  | 
            ||
| 1286 | neighbor_links = neighbor_links[  | 
            ||
| 1287 | ~neighbor_links.carrier.isin(excluded_carriers)  | 
            ||
| 1288 | ]  | 
            ||
| 1289 | |||
| 1290 | # Combine CHP_CC and CHP  | 
            ||
| 1291 | chp_cc = neighbor_links[  | 
            ||
| 1292 | neighbor_links.carrier == "urban central gas CHP CC"  | 
            ||
| 1293 | ]  | 
            ||
| 1294 | for index, row in chp_cc.iterrows():  | 
            ||
| 1295 | neighbor_links.loc[  | 
            ||
| 1296 |             neighbor_links.Link == row.Link.replace("CHP CC", "CHP"), | 
            ||
| 1297 | "p_nom_opt",  | 
            ||
| 1298 | ] += row.p_nom_opt  | 
            ||
| 1299 | neighbor_links.loc[  | 
            ||
| 1300 |             neighbor_links.Link == row.Link.replace("CHP CC", "CHP"), "p_nom" | 
            ||
| 1301 | ] += row.p_nom  | 
            ||
| 1302 | neighbor_links.drop(index, inplace=True)  | 
            ||
| 1303 | |||
| 1304 | # Combine heat pumps  | 
            ||
| 1305 | # Like in Germany, there are air heat pumps in central heat grids  | 
            ||
| 1306 | # and ground heat pumps in rural areas  | 
            ||
| 1307 | rural_air = neighbor_links[neighbor_links.carrier == "rural air heat pump"]  | 
            ||
| 1308 | for index, row in rural_air.iterrows():  | 
            ||
| 1309 | neighbor_links.loc[  | 
            ||
| 1310 |             neighbor_links.Link == row.Link.replace("air", "ground"), | 
            ||
| 1311 | "p_nom_opt",  | 
            ||
| 1312 | ] += row.p_nom_opt  | 
            ||
| 1313 | neighbor_links.loc[  | 
            ||
| 1314 |             neighbor_links.Link == row.Link.replace("air", "ground"), "p_nom" | 
            ||
| 1315 | ] += row.p_nom  | 
            ||
| 1316 | neighbor_links.drop(index, inplace=True)  | 
            ||
| 1317 | links_to_etrago(  | 
            ||
| 1318 | neighbor_links[neighbor_links.carrier.isin(extendable_links_carriers)],  | 
            ||
| 1319 | "eGon100RE",  | 
            ||
| 1320 | )  | 
            ||
| 1321 | links_to_etrago(  | 
            ||
| 1322 | neighbor_links[  | 
            ||
| 1323 | ~neighbor_links.carrier.isin(extendable_links_carriers)  | 
            ||
| 1324 | ],  | 
            ||
| 1325 | "eGon100RE",  | 
            ||
| 1326 | extendable=False,  | 
            ||
| 1327 | )  | 
            ||
| 1328 | # Include links time-series  | 
            ||
| 1329 | # For heat_pumps  | 
            ||
| 1330 |     hp = neighbor_links[neighbor_links["carrier"].str.contains("heat pump")] | 
            ||
| 1331 | |||
| 1332 | neighbor_eff_t = network_prepared.links_t["efficiency"][  | 
            ||
| 1333 | hp[hp.Link.isin(network_prepared.links_t["efficiency"].columns)].index  | 
            ||
| 1334 | ]  | 
            ||
| 1335 | |||
| 1336 | missing_hp = hp[~hp["Link"].isin(neighbor_eff_t.columns)].Link  | 
            ||
| 1337 | |||
| 1338 | eff_timeseries = network_prepared.links_t["efficiency"].copy()  | 
            ||
| 1339 | for met in missing_hp: # met: missing efficiency timeseries  | 
            ||
| 1340 | try:  | 
            ||
| 1341 | neighbor_eff_t[met] = eff_timeseries.loc[:, met[0:-5]]  | 
            ||
| 1342 | except:  | 
            ||
| 1343 |             print(f"There are not timeseries for heat_pump {met}") | 
            ||
| 1344 | |||
| 1345 | for i in neighbor_eff_t.columns:  | 
            ||
| 1346 | new_index = neighbor_links[neighbor_links["Link"] == i].index  | 
            ||
| 1347 |         neighbor_eff_t.rename(columns={i: new_index[0]}, inplace=True) | 
            ||
| 1348 | |||
| 1349 | # Include links time-series  | 
            ||
| 1350 | # For ev_chargers  | 
            ||
| 1351 |     ev = neighbor_links[neighbor_links["carrier"].str.contains("BEV charger")] | 
            ||
| 1352 | |||
| 1353 | ev_p_max_pu = network_prepared.links_t["p_max_pu"][  | 
            ||
| 1354 | ev[ev.Link.isin(network_prepared.links_t["p_max_pu"].columns)].index  | 
            ||
| 1355 | ]  | 
            ||
| 1356 | |||
| 1357 | missing_ev = ev[~ev["Link"].isin(ev_p_max_pu.columns)].Link  | 
            ||
| 1358 | |||
| 1359 | ev_p_max_pu_timeseries = network_prepared.links_t["p_max_pu"].copy()  | 
            ||
| 1360 | for mct in missing_ev: # evt: missing charger timeseries  | 
            ||
| 1361 | try:  | 
            ||
| 1362 | ev_p_max_pu[mct] = ev_p_max_pu_timeseries.loc[:, mct[0:-5]]  | 
            ||
| 1363 | except:  | 
            ||
| 1364 |             print(f"There are not timeseries for EV charger {mct}") | 
            ||
| 1365 | |||
| 1366 | for i in ev_p_max_pu.columns:  | 
            ||
| 1367 | new_index = neighbor_links[neighbor_links["Link"] == i].index  | 
            ||
| 1368 |         ev_p_max_pu.rename(columns={i: new_index[0]}, inplace=True) | 
            ||
| 1369 | |||
| 1370 | # prepare neighboring generators for etrago tables  | 
            ||
| 1371 | neighbor_gens["scn_name"] = "eGon100RE"  | 
            ||
| 1372 | neighbor_gens["p_nom"] = neighbor_gens["p_nom_opt"]  | 
            ||
| 1373 | neighbor_gens["p_nom_extendable"] = False  | 
            ||
| 1374 | |||
| 1375 | # Unify carrier names  | 
            ||
| 1376 |     neighbor_gens.carrier = neighbor_gens.carrier.str.replace(" ", "_") | 
            ||
| 1377 | |||
| 1378 | neighbor_gens.carrier.replace(  | 
            ||
| 1379 |         { | 
            ||
| 1380 | "onwind": "wind_onshore",  | 
            ||
| 1381 | "ror": "run_of_river",  | 
            ||
| 1382 | "offwind-ac": "wind_offshore",  | 
            ||
| 1383 | "offwind-dc": "wind_offshore",  | 
            ||
| 1384 | "offwind-float": "wind_offshore",  | 
            ||
| 1385 | "urban_central_solar_thermal": "urban_central_solar_thermal_collector",  | 
            ||
| 1386 | "residential_rural_solar_thermal": "residential_rural_solar_thermal_collector",  | 
            ||
| 1387 | "services_rural_solar_thermal": "services_rural_solar_thermal_collector",  | 
            ||
| 1388 | "solar-hsat": "solar",  | 
            ||
| 1389 | },  | 
            ||
| 1390 | inplace=True,  | 
            ||
| 1391 | )  | 
            ||
| 1392 | |||
| 1393 | for i in [  | 
            ||
| 1394 | "Generator",  | 
            ||
| 1395 | "weight",  | 
            ||
| 1396 | "lifetime",  | 
            ||
| 1397 | "p_set",  | 
            ||
| 1398 | "q_set",  | 
            ||
| 1399 | "p_nom_opt",  | 
            ||
| 1400 | "e_sum_min",  | 
            ||
| 1401 | "e_sum_max",  | 
            ||
| 1402 | ]:  | 
            ||
| 1403 | neighbor_gens = neighbor_gens.drop(i, axis=1)  | 
            ||
| 1404 | |||
| 1405 | neighbor_gens.to_sql(  | 
            ||
| 1406 | "egon_etrago_generator",  | 
            ||
| 1407 | engine,  | 
            ||
| 1408 | schema="grid",  | 
            ||
| 1409 | if_exists="append",  | 
            ||
| 1410 | index=True,  | 
            ||
| 1411 | index_label="generator_id",  | 
            ||
| 1412 | )  | 
            ||
| 1413 | |||
| 1414 | # prepare neighboring loads for etrago tables  | 
            ||
| 1415 | neighbor_loads["scn_name"] = "eGon100RE"  | 
            ||
| 1416 | |||
| 1417 | # Unify carrier names  | 
            ||
| 1418 |     neighbor_loads.carrier = neighbor_loads.carrier.str.replace(" ", "_") | 
            ||
| 1419 | |||
| 1420 | neighbor_loads.carrier.replace(  | 
            ||
| 1421 |         { | 
            ||
| 1422 | "electricity": "AC",  | 
            ||
| 1423 | "DC": "AC",  | 
            ||
| 1424 | "industry_electricity": "AC",  | 
            ||
| 1425 | "H2_pipeline_retrofitted": "H2_system_boundary",  | 
            ||
| 1426 | "gas_pipeline": "CH4_system_boundary",  | 
            ||
| 1427 | "gas_for_industry": "CH4_for_industry",  | 
            ||
| 1428 | "urban_central_heat": "central_heat",  | 
            ||
| 1429 | },  | 
            ||
| 1430 | inplace=True,  | 
            ||
| 1431 | )  | 
            ||
| 1432 | |||
| 1433 | neighbor_loads = neighbor_loads.drop(  | 
            ||
| 1434 | columns=["Load"],  | 
            ||
| 1435 | errors="ignore",  | 
            ||
| 1436 | )  | 
            ||
| 1437 | |||
| 1438 | neighbor_loads.to_sql(  | 
            ||
| 1439 | "egon_etrago_load",  | 
            ||
| 1440 | engine,  | 
            ||
| 1441 | schema="grid",  | 
            ||
| 1442 | if_exists="append",  | 
            ||
| 1443 | index=True,  | 
            ||
| 1444 | index_label="load_id",  | 
            ||
| 1445 | )  | 
            ||
| 1446 | |||
| 1447 | # prepare neighboring stores for etrago tables  | 
            ||
| 1448 | neighbor_stores["scn_name"] = "eGon100RE"  | 
            ||
| 1449 | |||
| 1450 | # Unify carrier names  | 
            ||
| 1451 |     neighbor_stores.carrier = neighbor_stores.carrier.str.replace(" ", "_") | 
            ||
| 1452 | |||
| 1453 | neighbor_stores.carrier.replace(  | 
            ||
| 1454 |         { | 
            ||
| 1455 | "Li_ion": "battery",  | 
            ||
| 1456 | "gas": "CH4",  | 
            ||
| 1457 | "urban_central_water_tanks": "central_heat_store",  | 
            ||
| 1458 | "rural_water_tanks": "rural_heat_store",  | 
            ||
| 1459 | "EV_battery": "battery_storage",  | 
            ||
| 1460 | },  | 
            ||
| 1461 | inplace=True,  | 
            ||
| 1462 | )  | 
            ||
| 1463 | neighbor_stores.loc[  | 
            ||
| 1464 | (  | 
            ||
| 1465 | (neighbor_stores.e_nom_max <= 1e9)  | 
            ||
| 1466 | & (neighbor_stores.carrier == "H2_Store")  | 
            ||
| 1467 | ),  | 
            ||
| 1468 | "carrier",  | 
            ||
| 1469 | ] = "H2_underground"  | 
            ||
| 1470 | neighbor_stores.loc[  | 
            ||
| 1471 | (  | 
            ||
| 1472 | (neighbor_stores.e_nom_max > 1e9)  | 
            ||
| 1473 | & (neighbor_stores.carrier == "H2_Store")  | 
            ||
| 1474 | ),  | 
            ||
| 1475 | "carrier",  | 
            ||
| 1476 | ] = "H2_overground"  | 
            ||
| 1477 | |||
| 1478 | for i in [  | 
            ||
| 1479 | "Store",  | 
            ||
| 1480 | "p_set",  | 
            ||
| 1481 | "q_set",  | 
            ||
| 1482 | "e_nom_opt",  | 
            ||
| 1483 | "lifetime",  | 
            ||
| 1484 | "e_initial_per_period",  | 
            ||
| 1485 | "e_cyclic_per_period",  | 
            ||
| 1486 | "location",  | 
            ||
| 1487 | ]:  | 
            ||
| 1488 | neighbor_stores = neighbor_stores.drop(i, axis=1, errors="ignore")  | 
            ||
| 1489 | |||
| 1490 | for c in ["H2_underground", "H2_overground"]:  | 
            ||
| 1491 | neighbor_stores.loc[  | 
            ||
| 1492 | (neighbor_stores.carrier == c),  | 
            ||
| 1493 | "lifetime",  | 
            ||
| 1494 |         ] = get_sector_parameters("gas", "eGon100RE")["lifetime"][c] | 
            ||
| 1495 | |||
| 1496 | neighbor_stores.to_sql(  | 
            ||
| 1497 | "egon_etrago_store",  | 
            ||
| 1498 | engine,  | 
            ||
| 1499 | schema="grid",  | 
            ||
| 1500 | if_exists="append",  | 
            ||
| 1501 | index=True,  | 
            ||
| 1502 | index_label="store_id",  | 
            ||
| 1503 | )  | 
            ||
| 1504 | |||
| 1505 | # prepare neighboring storage_units for etrago tables  | 
            ||
| 1506 | neighbor_storage["scn_name"] = "eGon100RE"  | 
            ||
| 1507 | |||
| 1508 | # Unify carrier names  | 
            ||
| 1509 |     neighbor_storage.carrier = neighbor_storage.carrier.str.replace(" ", "_") | 
            ||
| 1510 | |||
| 1511 | neighbor_storage.carrier.replace(  | 
            ||
| 1512 |         {"PHS": "pumped_hydro", "hydro": "reservoir"}, inplace=True | 
            ||
| 1513 | )  | 
            ||
| 1514 | |||
| 1515 | for i in [  | 
            ||
| 1516 | "StorageUnit",  | 
            ||
| 1517 | "p_nom_opt",  | 
            ||
| 1518 | "state_of_charge_initial_per_period",  | 
            ||
| 1519 | "cyclic_state_of_charge_per_period",  | 
            ||
| 1520 | ]:  | 
            ||
| 1521 | neighbor_storage = neighbor_storage.drop(i, axis=1, errors="ignore")  | 
            ||
| 1522 | |||
| 1523 | neighbor_storage.to_sql(  | 
            ||
| 1524 | "egon_etrago_storage",  | 
            ||
| 1525 | engine,  | 
            ||
| 1526 | schema="grid",  | 
            ||
| 1527 | if_exists="append",  | 
            ||
| 1528 | index=True,  | 
            ||
| 1529 | index_label="storage_id",  | 
            ||
| 1530 | )  | 
            ||
| 1531 | |||
| 1532 | # writing neighboring loads_t p_sets to etrago tables  | 
            ||
| 1533 | |||
| 1534 | neighbor_loads_t_etrago = pd.DataFrame(  | 
            ||
| 1535 | columns=["scn_name", "temp_id", "p_set"],  | 
            ||
| 1536 | index=neighbor_loads_t.columns,  | 
            ||
| 1537 | )  | 
            ||
| 1538 | neighbor_loads_t_etrago["scn_name"] = "eGon100RE"  | 
            ||
| 1539 | neighbor_loads_t_etrago["temp_id"] = 1  | 
            ||
| 1540 | for i in neighbor_loads_t.columns:  | 
            ||
| 1541 | neighbor_loads_t_etrago["p_set"][i] = neighbor_loads_t[  | 
            ||
| 1542 | i  | 
            ||
| 1543 | ].values.tolist()  | 
            ||
| 1544 | |||
| 1545 | neighbor_loads_t_etrago.to_sql(  | 
            ||
| 1546 | "egon_etrago_load_timeseries",  | 
            ||
| 1547 | engine,  | 
            ||
| 1548 | schema="grid",  | 
            ||
| 1549 | if_exists="append",  | 
            ||
| 1550 | index=True,  | 
            ||
| 1551 | index_label="load_id",  | 
            ||
| 1552 | )  | 
            ||
| 1553 | |||
| 1554 | # writing neighboring link_t efficiency and p_max_pu to etrago tables  | 
            ||
| 1555 | neighbor_link_t_etrago = pd.DataFrame(  | 
            ||
| 1556 | columns=["scn_name", "temp_id", "p_max_pu", "efficiency"],  | 
            ||
| 1557 | index=neighbor_eff_t.columns.to_list() + ev_p_max_pu.columns.to_list(),  | 
            ||
| 1558 | )  | 
            ||
| 1559 | neighbor_link_t_etrago["scn_name"] = "eGon100RE"  | 
            ||
| 1560 | neighbor_link_t_etrago["temp_id"] = 1  | 
            ||
| 1561 | for i in neighbor_eff_t.columns:  | 
            ||
| 1562 | neighbor_link_t_etrago["efficiency"][i] = neighbor_eff_t[  | 
            ||
| 1563 | i  | 
            ||
| 1564 | ].values.tolist()  | 
            ||
| 1565 | for i in ev_p_max_pu.columns:  | 
            ||
| 1566 | neighbor_link_t_etrago["p_max_pu"][i] = ev_p_max_pu[i].values.tolist()  | 
            ||
| 1567 | |||
| 1568 | neighbor_link_t_etrago.to_sql(  | 
            ||
| 1569 | "egon_etrago_link_timeseries",  | 
            ||
| 1570 | engine,  | 
            ||
| 1571 | schema="grid",  | 
            ||
| 1572 | if_exists="append",  | 
            ||
| 1573 | index=True,  | 
            ||
| 1574 | index_label="link_id",  | 
            ||
| 1575 | )  | 
            ||
| 1576 | |||
| 1577 | # writing neighboring generator_t p_max_pu to etrago tables  | 
            ||
| 1578 | neighbor_gens_t_etrago = pd.DataFrame(  | 
            ||
| 1579 | columns=["scn_name", "temp_id", "p_max_pu"],  | 
            ||
| 1580 | index=neighbor_gens_t.columns,  | 
            ||
| 1581 | )  | 
            ||
| 1582 | neighbor_gens_t_etrago["scn_name"] = "eGon100RE"  | 
            ||
| 1583 | neighbor_gens_t_etrago["temp_id"] = 1  | 
            ||
| 1584 | for i in neighbor_gens_t.columns:  | 
            ||
| 1585 | neighbor_gens_t_etrago["p_max_pu"][i] = neighbor_gens_t[  | 
            ||
| 1586 | i  | 
            ||
| 1587 | ].values.tolist()  | 
            ||
| 1588 | |||
| 1589 | neighbor_gens_t_etrago.to_sql(  | 
            ||
| 1590 | "egon_etrago_generator_timeseries",  | 
            ||
| 1591 | engine,  | 
            ||
| 1592 | schema="grid",  | 
            ||
| 1593 | if_exists="append",  | 
            ||
| 1594 | index=True,  | 
            ||
| 1595 | index_label="generator_id",  | 
            ||
| 1596 | )  | 
            ||
| 1597 | |||
| 1598 | # writing neighboring stores_t e_min_pu to etrago tables  | 
            ||
| 1599 | neighbor_stores_t_etrago = pd.DataFrame(  | 
            ||
| 1600 | columns=["scn_name", "temp_id", "e_min_pu"],  | 
            ||
| 1601 | index=neighbor_stores_t.columns,  | 
            ||
| 1602 | )  | 
            ||
| 1603 | neighbor_stores_t_etrago["scn_name"] = "eGon100RE"  | 
            ||
| 1604 | neighbor_stores_t_etrago["temp_id"] = 1  | 
            ||
| 1605 | for i in neighbor_stores_t.columns:  | 
            ||
| 1606 | neighbor_stores_t_etrago["e_min_pu"][i] = neighbor_stores_t[  | 
            ||
| 1607 | i  | 
            ||
| 1608 | ].values.tolist()  | 
            ||
| 1609 | |||
| 1610 | neighbor_stores_t_etrago.to_sql(  | 
            ||
| 1611 | "egon_etrago_store_timeseries",  | 
            ||
| 1612 | engine,  | 
            ||
| 1613 | schema="grid",  | 
            ||
| 1614 | if_exists="append",  | 
            ||
| 1615 | index=True,  | 
            ||
| 1616 | index_label="store_id",  | 
            ||
| 1617 | )  | 
            ||
| 1618 | |||
| 1619 | # writing neighboring storage_units inflow to etrago tables  | 
            ||
| 1620 | neighbor_storage_t_etrago = pd.DataFrame(  | 
            ||
| 1621 | columns=["scn_name", "temp_id", "inflow"],  | 
            ||
| 1622 | index=neighbor_storage_t.columns,  | 
            ||
| 1623 | )  | 
            ||
| 1624 | neighbor_storage_t_etrago["scn_name"] = "eGon100RE"  | 
            ||
| 1625 | neighbor_storage_t_etrago["temp_id"] = 1  | 
            ||
| 1626 | for i in neighbor_storage_t.columns:  | 
            ||
| 1627 | neighbor_storage_t_etrago["inflow"][i] = neighbor_storage_t[  | 
            ||
| 1628 | i  | 
            ||
| 1629 | ].values.tolist()  | 
            ||
| 1630 | |||
| 1631 | neighbor_storage_t_etrago.to_sql(  | 
            ||
| 1632 | "egon_etrago_storage_timeseries",  | 
            ||
| 1633 | engine,  | 
            ||
| 1634 | schema="grid",  | 
            ||
| 1635 | if_exists="append",  | 
            ||
| 1636 | index=True,  | 
            ||
| 1637 | index_label="storage_id",  | 
            ||
| 1638 | )  | 
            ||
| 1639 | |||
| 1640 | # writing neighboring lines_t s_max_pu to etrago tables  | 
            ||
| 1641 | if not network_solved.lines_t["s_max_pu"].empty:  | 
            ||
| 1642 | neighbor_lines_t_etrago = pd.DataFrame(  | 
            ||
| 1643 | columns=["scn_name", "s_max_pu"], index=neighbor_lines_t.columns  | 
            ||
| 1644 | )  | 
            ||
| 1645 | neighbor_lines_t_etrago["scn_name"] = "eGon100RE"  | 
            ||
| 1646 | |||
| 1647 | for i in neighbor_lines_t.columns:  | 
            ||
| 1648 | neighbor_lines_t_etrago["s_max_pu"][i] = neighbor_lines_t[  | 
            ||
| 1649 | i  | 
            ||
| 1650 | ].values.tolist()  | 
            ||
| 1651 | |||
| 1652 | neighbor_lines_t_etrago.to_sql(  | 
            ||
| 1653 | "egon_etrago_line_timeseries",  | 
            ||
| 1654 | engine,  | 
            ||
| 1655 | schema="grid",  | 
            ||
| 1656 | if_exists="append",  | 
            ||
| 1657 | index=True,  | 
            ||
| 1658 | index_label="line_id",  | 
            ||
| 1659 | )  | 
            ||
| 2308 |