-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSP_extrem_days.py
259 lines (201 loc) · 6.84 KB
/
LSP_extrem_days.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# %%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import os
from matplotlib.gridspec import GridSpec
params = {
"legend.fontsize": "x-large",
"axes.labelsize": 18,
"axes.titlesize": 18,
"xtick.labelsize": 18,
"ytick.labelsize": 18,
"legend.title_fontsize": "x-large",
"lines.linewidth": 4,
"lines.markersize": 10,
"hatch.linewidth": 0.1,
}
plt.rcParams.update(params)
# %% define functions
def count_frequency(arr):
frequency_dict = {}
for element in arr:
if element in frequency_dict:
frequency_dict[element] += 1
else:
frequency_dict[element] = 1
return frequency_dict
def count_frequency_given_keys(arr, keys):
frequency_dict = {key: 0 for key in keys} # Initialize all keys with 0
for element in arr:
if element in frequency_dict:
frequency_dict[element] += 1
return frequency_dict
def dic_percentage(dic, len):
percentage_dict = {}
for key in dic.keys():
percentage_dict[key] = np.round(dic[key] / len * 100, 1)
return percentage_dict
def get_season(month):
if month in [12, 1, 2]:
return "Winter DJF"
elif month in [3, 4, 5]:
return "Spring MAM"
elif month in [6, 7, 8]:
return "Summer JJA"
else:
return "Autumn SON"
def select_data_between_years(df, start_year, end_year):
return df[(df["date"].dt.year >= start_year) & (df["date"].dt.year <= end_year)]
params = {
"legend.fontsize": "x-large",
"axes.labelsize": 24,
"axes.titlesize": 24,
"xtick.labelsize": 24,
"ytick.labelsize": 24,
"lines.linewidth": 3,
"lines.markersize": 10,
"hatch.linewidth": 0.1,
}
plt.rcParams.update(params)
pal = sns.color_palette("colorblind")
pal_hex = pal.as_hex()
# %% read in data
station = "WEG_L"
home_path = os.getcwd()
# Read 20CRv3 data
df1 = pd.read_csv(f"/home/flo/LSP_analysis/Data/AT_20CRv3_{station}_daily.csv")
# Read SOM data
df2 = pd.read_csv(
home_path
+ "/Data/SOM_8_ssim_hgt_GRl_1900_2015/bmu_SOM_8_ssim_hgt_GRl_1900_2015.csv"
)
# Convert the 'date' column to datetime format
df1["date"] = pd.to_datetime(df1["time"])
df2["date"] = pd.to_datetime(df2["time"])
# Merge the two DataFrames on the 'date' column
merged_df = pd.merge(df1, df2, on="date", how="inner")
merged_df = merged_df[["date", "AT", "bmu"]]
# %%
merged_df["month"] = merged_df["date"].dt.month
# Map months to seasons using the function
merged_df["season"] = merged_df["month"].apply(get_season)
merged_df["DOY"] = merged_df["date"].dt.dayofyear
# Calculate the anomaly of 'AT' based on the average temperature in each season
seasonal_mean = merged_df.groupby("season")["AT"].mean()
mean_by_month = merged_df.groupby("month")["AT"].mean()
mean_by_day = merged_df.groupby("DOY")["AT"].mean()
merged_df["AT_rolling"] = merged_df["AT"].rolling(30, center=True).mean()
mean_by_day_rolling = merged_df.groupby("DOY")["AT_rolling"].mean()
merged_df["AT_anomaly"] = merged_df.apply(
lambda row: row["AT"] - seasonal_mean[row["season"]], axis=1
)
merged_df["AT_anomaly_month"] = merged_df.apply(
lambda row: row["AT"] - mean_by_month[row["month"]], axis=1
)
merged_df["AT_anomaly_doy"] = merged_df.apply(
lambda row: row["AT"] - mean_by_day[row["DOY"]], axis=1
)
merged_df["AT_anomaly_rolling"] = merged_df.apply(
lambda row: row["AT"] - mean_by_day_rolling[row["DOY"]], axis=1
)
merged_df["Period"] = "1900-2015"
merged_df.loc[
(merged_df["date"] >= "1922-01-01") & (merged_df["date"] <= "1932-12-31"), "Period"
] = "WP1 1922-1932"
merged_df.loc[
(merged_df["date"] >= "1993-01-01") & (merged_df["date"] <= "2007-12-31"), "Period"
] = "WP2 1993-2007"
df1 = select_data_between_years(merged_df, 1922, 1932)
df1["WP index"] = "WP1"
df1_freq = count_frequency(df1["bmu"])
df1_perc = dic_percentage(df1_freq, len(df1["bmu"]))
df2 = select_data_between_years(merged_df, 1993, 2007)
df2["WP index"] = "WP2"
df2_freq = count_frequency(df2["bmu"])
df2_perc = dic_percentage(df2_freq, len(df2["bmu"]))
df_freq = count_frequency(merged_df["bmu"])
df_perc = dic_percentage(df_freq, len(merged_df["bmu"]))
# %% LSP Manuscript Figure 6
percentage = 15
temp = merged_df
sort_value = "AT_anomaly_rolling"
fig = plt.figure(figsize=(15, 8), constrained_layout=True)
gs = GridSpec(2, 4, height_ratios=[7, 1], width_ratios=[1, 1, 1, 1], figure=fig)
# Subplot 1: Horizontal Bar Plot (on the left)
ax = fig.add_subplot(gs[0, 0])
bx = fig.add_subplot(gs[0, 1])
cx = fig.add_subplot(gs[0, 2])
dx = fig.add_subplot(gs[0, 3])
axes = [ax, bx, cx, dx]
for i, season in enumerate(pd.unique(temp["season"])):
sns.set_theme(style="whitegrid")
df_season = temp[temp["season"] == season]
df_season = df_season.sort_values(by=sort_value, ascending=False)
df_season = df_season.reset_index(drop=True)
df_season["rank"] = df_season.index + 1
df_season["rank_perc"] = np.round(
df_season["rank"] / len(df_season["rank"]) * 100, 1
)
warmest = df_season[
np.logical_and(
df_season["rank_perc"] >= 0, df_season["rank_perc"] <= percentage
)
]
df_freq_w = count_frequency_given_keys(warmest["bmu"], df_freq.keys())
df_perc_w = dic_percentage(df_freq_w, len(warmest["bmu"]))
coldest = df_season[
np.logical_and(
df_season["rank_perc"] >= 100 - percentage, df_season["rank_perc"] <= 100
)
]
df_freq_c = count_frequency_given_keys(coldest["bmu"], df_freq.keys())
df_perc_c = dic_percentage(df_freq_c, len(coldest["bmu"]))
keys = df_freq_c.keys()
bar_width = 0.3
r1 = np.arange(len(keys) + 1)[1:]
r2 = [x + bar_width / 2 for x in r1]
r0 = [x - bar_width / 2 for x in r1]
axx = axes[i]
axx.text(
0.1,
1.025,
"(" + "abcd"[i] + ")",
ha="center",
va="center",
transform=axx.transAxes,
fontsize=20,
fontweight="bold",
color="k",
)
for i, key in enumerate(keys):
axx.barh(
r0[key - 1],
df_perc_w[key],
color=pal_hex[3],
height=bar_width,
edgecolor="k",
label=str(percentage) + "% Warmest AT anomaly",
zorder=1,
)
axx.barh(
r2[key - 1],
df_perc_c[key],
color=pal_hex[-1],
height=bar_width,
edgecolor="k",
label=str(percentage) + "% Coldest AT anomaly",
zorder=1,
)
axx.set_title(season, fontsize=18, loc="center")
axx.invert_yaxis()
axx.set_xlim(0, 75)
axx.set_ylabel("LSP [#]")
axx.set_xlabel("Relative Occurence [%]")
legend_ax = fig.add_axes(
[0.38, 0.03, 0.3, 0.05], frameon=False
) # Adjust the position and size as needed
legend_ax.axis("off")
h, l = axx.get_legend_handles_labels()
legend_ax.legend(h[0:2], l[0:2], ncol=2, loc="center", fontsize=18, frameon=False)