|
15 | 15 |
|
16 | 16 | from .data_util import read_rel, read_timeseries
|
17 | 17 | from .material_demand import material_demand_calc
|
18 |
| -from .util import combine_df_dictionaries, get_ssp_from_context, read_config |
| 18 | +from .util import ( |
| 19 | + add_R12_column, |
| 20 | + combine_df_dictionaries, |
| 21 | + get_pycountry_iso, |
| 22 | + get_ssp_from_context, |
| 23 | + read_config, |
| 24 | +) |
19 | 25 |
|
20 | 26 |
|
21 | 27 | def read_data_aluminum(
|
@@ -733,3 +739,94 @@ def gen_data_alu_trade(scenario: message_ix.Scenario) -> dict[str, pd.DataFrame]
|
733 | 739 | results["bound_activity_lo"].append(bound_act_net_export_chn_2025)
|
734 | 740 |
|
735 | 741 | return {par_name: pd.concat(dfs) for par_name, dfs in results.items()}
|
| 742 | + |
| 743 | + |
| 744 | +def gen_hist_new_cap(): |
| 745 | + df_cap = pd.read_excel( |
| 746 | + package_data_path("material", "aluminum", "smelters-with 2022 projection.xls"), |
| 747 | + sheet_name="Sheet1", |
| 748 | + skipfooter=23, |
| 749 | + ).rename(columns={"Unnamed: 0": "Country", "Unnamed: 1": "Region"}) |
| 750 | + df_cap.Technology = df_cap.Technology.fillna("unknown") |
| 751 | + df_cap = df_cap[~df_cap[1995].isna()] |
| 752 | + df_cap.Country = df_cap["Country"].ffill() |
| 753 | + df_cap["ISO"] = df_cap.Country.apply( |
| 754 | + lambda c: get_pycountry_iso( |
| 755 | + c, |
| 756 | + { |
| 757 | + "Surinam": "SUR", |
| 758 | + "Trinidad": "TTO", |
| 759 | + "Quatar": "QAT", |
| 760 | + "Turkey": "TUR", |
| 761 | + "UAE": "ARE", |
| 762 | + "Gernamy": "DEU", |
| 763 | + "Azerbaydzhan": "AZE", |
| 764 | + "Russia": "RUS", |
| 765 | + "Tadzhikistan": "TJK", |
| 766 | + "UK": "GBR", |
| 767 | + "Total": "World", |
| 768 | + "Bosnia": "BIH", |
| 769 | + }, |
| 770 | + ) |
| 771 | + ) |
| 772 | + df_cap = add_R12_column( |
| 773 | + df_cap, file_path=package_data_path("node", "R12.yaml"), iso_column="ISO" |
| 774 | + ) |
| 775 | + |
| 776 | + # generate historical_new_capacity for soderberg |
| 777 | + df_cap_ss = df_cap[ |
| 778 | + (df_cap.Technology.str.contains("SS") & ~(df_cap.Technology.str.contains("PB"))) |
| 779 | + & ~(df_cap.Technology.str.contains("HAL")) |
| 780 | + & ~(df_cap.Technology.str.contains("P")) |
| 781 | + ] |
| 782 | + df_cap_ss_r12 = df_cap_ss.groupby("R12").sum(numeric_only=True) |
| 783 | + sample = df_cap_ss_r12[df_cap_ss_r12[df_cap_ss_r12.columns[-1]] != 0][ |
| 784 | + [i for i in range(1995, 2020, 5)] + [2019] |
| 785 | + ] |
| 786 | + hist_new_cap_ss = compute_differences(sample, 1995) / 10**6 |
| 787 | + hist_new_cap_ss = hist_new_cap_ss.rename(columns={2019: 2020}) |
| 788 | + hist_new_cap_ss = ( |
| 789 | + hist_new_cap_ss.reset_index() |
| 790 | + .melt(id_vars="R12", var_name="year_vtg") |
| 791 | + .assign(unit="Mt", technology="soderberg_aluminum") |
| 792 | + .rename(columns={"R12": "node_loc"}) |
| 793 | + ) |
| 794 | + |
| 795 | + # generate historical_new_capacity for prebake |
| 796 | + df_cap_pb = df_cap.loc[df_cap.index.difference(df_cap_ss.index)] |
| 797 | + df_cap_pb_r12 = df_cap_pb.groupby("R12").sum(numeric_only=True) |
| 798 | + sample = df_cap_pb_r12[[i for i in range(1995, 2020, 5)] + [2019]] |
| 799 | + hist_new_cap_pb = compute_differences(sample, 1995) / 10**6 |
| 800 | + hist_new_cap_pb = hist_new_cap_pb.rename(columns={2019: 2020}) |
| 801 | + hist_new_cap_pb = ( |
| 802 | + hist_new_cap_pb.reset_index() |
| 803 | + .melt(id_vars="R12", var_name="year_vtg") |
| 804 | + .assign(unit="Mt", technology="prebake_aluminum") |
| 805 | + .rename(columns={"R12": "node_loc"}) |
| 806 | + ) |
| 807 | + |
| 808 | + return {"historical_new_capacity": pd.concat([hist_new_cap_ss, hist_new_cap_pb])} |
| 809 | + |
| 810 | + |
| 811 | +def compute_differences(df, ref_col): |
| 812 | + # Initialize a DataFrame to store differences |
| 813 | + differences = df[ref_col].to_frame() |
| 814 | + |
| 815 | + # Start with the reference column |
| 816 | + ref_values = df[ref_col].copy() |
| 817 | + |
| 818 | + for col in df.columns: |
| 819 | + if col == ref_col: |
| 820 | + continue # Skip the reference column |
| 821 | + |
| 822 | + # Compute differences |
| 823 | + diff = df[col] - ref_values |
| 824 | + diff[diff <= 0] = 0 # Keep only positive differences |
| 825 | + |
| 826 | + # Store differences |
| 827 | + differences[col] = diff |
| 828 | + |
| 829 | + # Update the reference column where the current column is greater |
| 830 | + ref_values = ref_values.where(df[col] <= ref_values, df[col]) |
| 831 | + |
| 832 | + return differences |
0 commit comments