|
| 1 | +import xarray as xr |
| 2 | +import pandas as pd |
| 3 | +import numpy as np |
| 4 | +import seaborn as sns |
| 5 | +import os, sys |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +from dscim.diagnostics.damage_function import damage_function |
| 8 | + |
| 9 | + |
| 10 | +def plot_stacked( |
| 11 | + sector, |
| 12 | + recipe="risk_aversion", |
| 13 | + disc="constant", |
| 14 | + eta=2.0, |
| 15 | + rho=0.0, |
| 16 | + xlim=(-1, 8), |
| 17 | + years=[2020, 2050, 2090, 2100, 2200, 2300], |
| 18 | + sharey=False, |
| 19 | + rff=True, |
| 20 | +): |
| 21 | + |
| 22 | + root_rff = f"/shares/gcp/integration_replication/results/rff/{sector}/2020/" |
| 23 | + root_ssp = f"/shares/gcp/integration_replication/results/AR6_ssp/{sector}/2020/" |
| 24 | + coefs_rff = root_rff |
| 25 | + coefs_ssp = root_ssp |
| 26 | + output = f"/mnt/CIL_integration/plots/rff_diagnostics/rff_ssp_stacked_damage_functions_replication/{sector}" |
| 27 | + |
| 28 | + # overlap ssp and rff |
| 29 | + quantiles = [0.01, 0.05, 0.25, 0.5, 0.75, 0.95, 0.99] |
| 30 | + ds_rff_mean = ( |
| 31 | + xr.open_dataset( |
| 32 | + f"{coefs_rff}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_coefficients.nc4" |
| 33 | + ) |
| 34 | + .sel(year=years) |
| 35 | + .mean("runid") |
| 36 | + ) |
| 37 | + ds_rff = ( |
| 38 | + xr.open_dataset( |
| 39 | + f"{coefs_rff}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_coefficients.nc4" |
| 40 | + ) |
| 41 | + .sel(year=years) |
| 42 | + .quantile(quantiles, "runid") |
| 43 | + ) |
| 44 | + ds_ssp = xr.open_dataset( |
| 45 | + f"{coefs_ssp}/{recipe}_{disc}_eta{eta}_rho{rho}_damage_function_coefficients.nc4" |
| 46 | + ).sel(year=years) |
| 47 | + |
| 48 | + temps = xr.DataArray( |
| 49 | + np.arange(xlim[0], xlim[1], 0.1), |
| 50 | + coords={"anomaly": np.arange(xlim[0], xlim[1], 0.1)}, |
| 51 | + ) |
| 52 | + |
| 53 | + fit_rff_mean = ( |
| 54 | + ds_rff_mean["anomaly"] * temps |
| 55 | + + ds_rff_mean["np.power(anomaly, 2)"] * temps**2 |
| 56 | + ) |
| 57 | + fit_rff_mean = fit_rff_mean.to_dataframe("fit").reset_index() |
| 58 | + fit_rff_mean["model"] = "RFF mean" |
| 59 | + fit_rff_mean = fit_rff_mean[["fit", "year", "model", "anomaly"]] |
| 60 | + |
| 61 | + fit_rff = ds_rff["anomaly"] * temps + ds_rff["np.power(anomaly, 2)"] * temps**2 |
| 62 | + fit_rff = fit_rff.to_dataframe("fit").reset_index() |
| 63 | + fit_rff["model"] = "quantile: " + fit_rff["quantile"].astype(str) |
| 64 | + fit_rff = fit_rff[["fit", "year", "model", "anomaly"]] |
| 65 | + |
| 66 | + fit_ssp = ds_ssp["anomaly"] * temps + ds_ssp["np.power(anomaly, 2)"] * temps**2 |
| 67 | + fit_ssp = fit_ssp.to_dataframe("fit").reset_index() |
| 68 | + fit_ssp["model"] = fit_ssp.ssp + "-" + fit_ssp.model |
| 69 | + fit_ssp = fit_ssp[["fit", "year", "model", "anomaly"]] |
| 70 | + |
| 71 | + if rff: |
| 72 | + fit = pd.concat([fit_ssp, fit_rff, fit_rff_mean]).set_index( |
| 73 | + ["year", "model", "anomaly"] |
| 74 | + ) |
| 75 | + pal = sns.color_palette("Paired", 6) + sns.color_palette( |
| 76 | + "Greys", len(quantiles) + 1 |
| 77 | + ) |
| 78 | + else: |
| 79 | + fit = fit_ssp.set_index(["year", "model", "anomaly"]) |
| 80 | + pal = sns.color_palette("Paired", 6) |
| 81 | + |
| 82 | + g = sns.relplot( |
| 83 | + data=fit, |
| 84 | + x="anomaly", |
| 85 | + y="fit", |
| 86 | + hue="model", |
| 87 | + col="year", |
| 88 | + col_wrap=3, |
| 89 | + kind="line", |
| 90 | + palette=pal, |
| 91 | + facet_kws={"sharey": sharey, "sharex": True}, |
| 92 | + legend="full", |
| 93 | + ) |
| 94 | + |
| 95 | + g.fig.suptitle(f"{sector} {recipe} {disc} eta={eta} rho={rho}") |
| 96 | + |
| 97 | + plt.subplots_adjust(top=0.85) |
| 98 | + |
| 99 | + os.makedirs(output, exist_ok=True) |
| 100 | + plt.savefig( |
| 101 | + f"{output}/stacked_{sector}_{recipe}_{disc}_{eta}_{rho}_xlim{xlim}_rff-{rff}_years{years}.png", |
| 102 | + bbox_inches="tight", |
| 103 | + dpi=300, |
| 104 | + ) |
| 105 | + |
| 106 | + # plt.close() |
0 commit comments