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