Skip to content

Commit 55ed71b

Browse files
author
JMGilbert
committed
Add stacked damage function diagnostic plot
1 parent bf5fda0 commit 55ed71b

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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()

dscim/diagnostics/var_timeseries.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,13 @@ def rff_timeseries(
236236
gmst_pulse_minus_control = gmst_pulse_minus_control.rename(
237237
"gmst_pulse_minus_control"
238238
)
239-
239+
240240
# gdp
241241
gdp = (
242242
xr.open_dataset(
243243
"/shares/gcp/integration_replication/inputs/econ/rff_global_socioeconomics.nc4"
244244
)
245-
.drop('region')
245+
.drop("region")
246246
.rename({"runid": "rff_sp"})
247247
.gdp.sel(rff_sp=cw.rff_sp)
248248
)
@@ -409,7 +409,6 @@ def ssp_timeseries(
409409
if "coastal" not in sector:
410410
anomaly = anomaly.sel(gas=gas, drop=True)
411411

412-
413412
data = xr.combine_by_coords(
414413
[
415414
i.to_dataset()

0 commit comments

Comments
 (0)