-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_accprec_time_evo.py
272 lines (256 loc) · 9.63 KB
/
stats_accprec_time_evo.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# Souza & Ramos da Silva,
# Ocean-Land Atmosphere Model (OLAM) performance for major extreme
# meteorological events near the coastal region of southern Brazil,
# Climate Research, in revision 2020
"""
Created on Mon Feb 1 21:32:34 2021
Script for analysis of the temporal evolution of the precipitation
@author: Danilo
"""
import statistics_Danilo as st
from scipy.stats import mannwhitneyu
from prepare_data import (regrid, GetPrecData, unacc_olam_prec)
import numpy as np
import pandas as pd
# plotting packages
import pylab as pl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
def mean_corrl(event):
'''
Calculate temporal evolution of the spatial correlation of mean acc. prec.
Additionally, returns the value of mean, max and min
accumulated precipitation for the domain
'''
olam,obs = GetPrecData(event)[0],GetPrecData(event)[1]
if event < 3:
obs = obs.resample(time='3H').sum(dim='time')
obs = obs.cumsum('time')
olam_r = regrid(obs.lon,obs.lat,olam,event)
meanobs = []
meanolam = []
up_obs = []
down_obs = []
up_olam = []
down_olam = []
corr = []
for t in range(len(olam.time)):
meanobs.append(np.mean(obs[t]))
meanolam.append(np.mean(olam[t]))
up_obs.append(np.amax(obs[t]))
down_obs.append(np.amin(obs[t]))
up_olam.append(np.amax(olam[t]))
down_olam.append(np.amin(olam[t]))
corr.append(st.Scorr(obs[t], olam_r[t])[0])
return (meanobs, meanolam, corr, olam.time,
up_obs, down_obs, up_olam, down_olam)
def ptot_corrl(event):
'''
Calculate spatial correlation index for the total accumulated precipitation
'''
olam,obs = GetPrecData(event)[0],GetPrecData(event)[1]
if event < 3:
obs = obs.resample(time='3H').sum(dim='time')
obs = obs.cumsum('time')
olam_r = regrid(obs.lon,obs.lat,olam,event)
obspt = []
olampt = []
corr = []
for t in range(len(olam.time)):
s_obs = obs[t].sum('lon').sum('lat')
s_olam = olam[t].sum('lon').sum('lat')
obspt.append(s_obs)
olampt.append(s_olam)
corr.append(st.Scorr(obs[t], olam_r[t])[0])
return obspt, olampt, corr, olam.time
def plot_panel(which):
'''
Plot temporal evolution of the accumulated precipitation
Parameters
----------
which: string
Which data to plot.
If 'pt': plots the total accumulated (sum over entire domain) and
the spatial correlation index
If 'mean': plot mean acccmulated precipitation (line) and
maximum and minimum values (shaded)
'''
# figure params
fig = plt.figure(figsize=(15,15))
gs1 = gridspec.GridSpec(4, 3, hspace=0.1, wspace=0.3)
axs = []
# box for plotting texts
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
for i in range(1,13):
axs.append(fig.add_subplot(gs1[i - 1]))
ax1 = axs[-1]
# get data
if which == 'pt':
tmp = ptot_corrl(i)
ax1.set_yscale('log')
elif which == 'mean':
tmp = mean_corrl(i)
obst, obsb, olabt, olamb = tmp[4],tmp[5],tmp[6],tmp[7]
obs, olam, corr, time = tmp[0], tmp[1], tmp[2], tmp[3]
# plots
ax1.plot(time,obs, linewidth=4,
c='#0077b6', label='Reanalysis')
ax1.plot(time,olam, linewidth=4,
c='#69140E', label='OLAM')
ax2 = ax1.twinx()
ax2.scatter(time, corr, alpha=0.7,
c='#41521F', label='Correlation')
# shading for max and min values
if which == 'mean':
ax1.fill_between(time,obst,obsb, color='#0077b6',alpha=0.2)
ax1.fill_between(time,olabt,olamb, color='#69140E',alpha=0.2)
plt.xticks([])
# legend
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
if which == 'mean':
loc = 'upper right'
elif which == 'pt':
loc = 'lower right'
if i == 1:
ax2.legend(lines + lines2, labels + labels2, loc=loc)
# plot cosmedics
ax1.tick_params(labelsize=14)
ax2.tick_params(labelsize=14)
ax1.text(0.85,0.85, str(i), fontsize = 18, transform=ax1.transAxes, bbox=props)
ax1.set_xlim(time[0],time[-1])
ax2.set_ylim(0,1)
pl.savefig('./figures/accprec_time_evo/accprec_'+str(which)+'_time_evo.jpg', format='jpg')
pl.savefig('./figures/accprec_time_evo/accprec_'+str(which)+'_time_evo.eps', format='eps', dpi=300)
def GetDailyAccPrec(event):
'''
Transform prec. to daily accumulates
'''
tmp = GetPrecData(event)
olam,obs = tmp[0],tmp[1]
if event > 2:
dtr = pd.date_range(start=olam.time[0].values, periods=len(obs.time), freq='3h')
obs = obs.assign_coords(time=dtr)
olam = unacc_olam_prec(olam)
olam = olam.resample(time='1D').sum('time')
obs = obs.resample(time='1D').sum('time')
if event > 2 and event !=9:
obs = obs[:-1]
return obs, olam
def DataToDataFrame(data):
'''
Transforms the DataArray data to a DataFrame
(easiest way I've found to create the boxplots)
'''
list_ = []
for t in data.time:
l = list(data.sel(time=t).values.ravel())
list_.append(np.reshape(l,len(l)))
df = pd.DataFrame(list_).transpose()
# dates = data['time'].dt.strftime("%D")
# df.columns = dates
df.columns = range(1,len(data.time)+1)
return df
def TestMannWithneyU_DailyAccPrec():
'''
Perform Mann Whitnney U test for Daily accumulated precipitation
'''
MWUT = {}
for i in range(1,13):
MWUT['Event '+str(i)] = []
tmp = GetDailyAccPrec(i)
obs,olam = tmp[0],tmp[1]
#daily corrl
olam_r = regrid(obs.lon,obs.lat,olam,i)
olam_r, obs_acc = olam_r.cumsum('time'), obs.cumsum('time')
for d in olam.time:
stat, p = mannwhitneyu(obs_acc.sel(time=d).values.ravel(), olam_r.sel(time=d).values.ravel())
alpha = 0.05
if p > alpha:
MWUT['Event '+str(i)].append('Same distribution (fail to reject H0)')
else:
MWUT['Event '+str(i)].append('Different distribution (reject H0)')
return MWUT
def PlotBoxPLot():
'''
Create Boxplots for daily accumulated precipitation
'''
# Figure params
fig = plt.figure(figsize=(15,15))
gs1 = gridspec.GridSpec(4, 3, hspace=0.25, wspace=0.25)
axs = []
# colors for plots
c1 ='#0077b6'
c2 = '#ff6961'
# box for plotting texts
props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)
for i in range(1,13):
# get data
tmp = GetDailyAccPrec(i)
obs,olam = tmp[0],tmp[1]
olam_df = DataToDataFrame(olam)
obs_df = DataToDataFrame(obs)
# daily spatial correlation
olam_r = regrid(obs.lon,obs.lat,olam,i)
olam_r, obs_acc = olam_r.cumsum('time'), obs.cumsum('time')
corrl = []
for d in olam.time:
corrl.append(st.Scorr(obs_acc.sel(time=d), olam_r.sel(time=d))[0])
for ct in range(3):
corrl.append(np.nan)
xs = range(len(corrl))
s1 = pd.Series(corrl, index=xs)
# figure
axs.append(fig.add_subplot(gs1[i - 1]))
ax1 = axs[-1]
axs.append(ax1)
ax2 = axs[-1].twinx()
# ticks for the x-axis
ticks = np.arange(0,len(obs_df.columns))
# plot reanalysis
ax1.boxplot(obs_df, labels=obs_df.columns,
positions=np.array(ticks)*4-1, widths=1.6,
showfliers=False,
notch=True, patch_artist=True,
boxprops=dict(facecolor=c1),
medianprops=dict(color='k', linewidth=2),
flierprops=dict(color='k'))
# plot olam
ax1.boxplot(olam_df, labels=olam_df.columns,
positions=np.array(ticks)*4+1,widths=1.6,
showfliers=False,
notch=True, patch_artist=True,
boxprops=dict(facecolor=c2),
medianprops=dict(color='k', linewidth=2),
flierprops=dict(color='k'))
# plot correlation values
ax2.plot(s1.dropna(),c='#028e2c',marker='v',
alpha=0.85, label='Corrl.', linewidth=2)
ax2.set_ylim(0,1)
ax2.grid(linewidth=0.25,c='gray')
# create fake labels
if i == 1:
plt.plot([], c=c1, label='Reanalysis')
plt.plot([], c=c2, label='OLAM')
plt.legend(fontsize=14,loc='upper right')
# ticks
plt.xticks(np.arange(0, (len(ticks)) * 4, 4), range(1,len(obs_df.columns)+1))
# cosmedics
ax1.text(.05,0.8, str(i), fontsize = 14,
transform=ax1.transAxes, bbox=props)
ax1.tick_params(labelsize=12)
ax2.tick_params(labelsize=12)
plt.xlim(-3, (len(ticks)*4)+1)
if i > 9:
ax1.set_xlabel('Time (days)', fontsize = 16)
if i == 1 or i == 4 or i == 7 or i == 10:
ax1.set_ylabel('Daily Acc. Prec. (mm)',fontsize = 16)
if i == 3 or i == 6 or i == 9 or i == 12:
ax2.set_ylabel('Corrl I.',fontsize = 16)
pl.savefig('./figures/accprec_time_evo/daily_boxplot.jpg', format='jpg')
pl.savefig('./figures/accprec_time_evo/daily_boxplot.eps', format='eps', dpi=300)
if __name__ == "__main__":
# plot_panel('mean')
# plot_panel('pt')
PlotBoxPLot()
test = TestMannWithneyU_DailyAccPrec()