-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathactive_break.py
More file actions
205 lines (178 loc) · 8.47 KB
/
active_break.py
File metadata and controls
205 lines (178 loc) · 8.47 KB
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
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import random
from scipy import stats
from datetime import datetime, timedelta
import cartopy.crs as ccrs
from matplotlib.axes import Axes
from cartopy.mpl.geoaxes import GeoAxes
import cartopy.io.shapereader as shapereader
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
GeoAxes._pcolormesh_patched = Axes.pcolormesh
# fname = '/home/cccr/msingh/phd_obj2_data/ensemble_data/Admin2.shp'
# adm1_shapes = list(shapereader.Reader(fname).geometries())
# %matplotlib inline
import warnings
warnings.filterwarnings("ignore")
import xesmf as xe
from sklearn.metrics import mean_squared_error
import os
import matplotlib.pyplot as plt
import numpy.ma as ma
import seaborn as sns
import pandas as pd
import glob
import matplotlib.pyplot as plt
lats, late, lons, lone = 14, 28, 74, 87
ds_anthrop_pr = []
ds_no_anthrop_pr = []
for i_y,year_ in enumerate(range(1982,2014)):
print(year_)
####################################################################################
data_dir = '/home/cccr/msingh/phd_data_cmip_account/anthrop/ATM/DAY/'+str(year_)
filenames_a = glob.glob(data_dir+'/atm_*_day.nc')
if len(filenames_a)==0:
continue
ens_list = []
for file in filenames_a:
#print(file)
data_ = xr.open_dataset(file).pr.sel(lat=slice(late,lats))\
.sel(lon=slice(lons,lone)).mean(dim='lat').mean(dim='lon')
datetimeindex = data_.indexes['time'].to_datetimeindex()
data_['time'] = datetimeindex
if data_.sel(time=slice(str(year_)+'-05-20', str(year_)+'-10-15')).time.shape[0]==148:
ens_list.append(data_.sel(time=slice(str(year_)+'-05-20', str(year_)+'-10-15')))
#print('test')
if len(filenames_a)>0:
ds_anthrop_pr_ = xr.concat(ens_list, dim='ens').mean(dim='ens')
ds_anthrop_pr.append(ds_anthrop_pr_)
######################################################################################
data_dir = '/home/cccr/msingh/phd_data_cmip_account/no_anthrop/ATM/DAY/'+str(year_)
filenames_a = glob.glob(data_dir+'/atm_*_day.nc')
if len(filenames_a)==0:
continue
ens_list = []
#print(filenames_a)
for file in filenames_a:
#ens_list = []
#print(file)
data_ = xr.open_dataset(file).pr.sel(lat=slice(late,lats))\
.sel(lon=slice(lons,lone)).mean(dim='lat').mean(dim='lon')
datetimeindex = data_.indexes['time'].to_datetimeindex()
data_['time'] = datetimeindex
if data_.sel(time=slice(str(year_)+'-05-20', str(year_)+'-10-15')).time.shape[0]==148:
ens_list.append(data_.sel(time=slice(str(year_)+'-05-20', str(year_)+'-10-15')))
if len(filenames_a)>0:
ds_no_anthrop_pr_ = xr.concat(ens_list, dim='ens').mean(dim='ens')
ds_no_anthrop_pr.append(ds_no_anthrop_pr_)
######################################################################################
ds_anthrop = xr.concat(ds_anthrop_pr, dim='time')
ds_no_anthrop = xr.concat(ds_no_anthrop_pr, dim='time')
data_dir = '/home/cccr/msingh/data/IMD/imd-dly-p25-1901-2019.nc'
ds_imd = xr.open_dataset(data_dir)
ds_imd['time'] = ds_imd.indexes['time'].normalize()
ds_anthrop['time'] = ds_anthrop.indexes['time'].normalize()
ds_no_anthrop['time'] = ds_no_anthrop.indexes['time'].normalize()
ds_imd_ = ds_imd.sel(time=ds_anthrop.time).sel(lat=slice(lats,late)).\
sel(lon=slice(lons,lone)).mean(dim='lat').mean(dim='lon')
ds_anthrop_ = ds_anthrop*86400
ds_no_anthrop_ = ds_no_anthrop*86400
imd_active = np.zeros((2014-1982))
anthrop_active = np.zeros((2014-1982))
no_anthrop_active = np.zeros((2014-1982))
imd_break = np.zeros((2014-1982))
anthrop_break = np.zeros((2014-1982))
no_anthrop_break= np.zeros((2014-1982))
years = np.arange(1982,2014)
months=[6,7,8,9]
print(years.shape)
ds_imd_active= ds_imd_.rf.where(ds_imd_.rf>ds_imd_.rf.mean(dim='time')+ds_imd_.rf.std(dim='time'), drop=True)
ds_imd_active_ja = ds_imd_active.time.sel(time=ds_imd_active.time.dt.month.isin(months))
#print(ds_imd_active_ja.time)
cnt=0
for year in range(1982,2014):
#print(ds_imd_active_ja.time.sel(time=ds_imd_active_ja.time.dt.year.isin([year])).values)
#print(cnt)
imd_active[cnt] = ds_imd_active_ja.time.sel(time=ds_imd_active_ja.time.dt.year.isin([year])).values.shape[0]
cnt=cnt+1
#np.savetxt('imd_active.txt',ds_imd_active.time.sel(time=ds_imd_active.time.dt.year.isin([1982])).values )
ds_anthrop_active= ds_anthrop_.where(ds_anthrop_>ds_anthrop_.mean(dim='time')+ds_anthrop_.std(dim='time'), drop=True)
ds_anthrop_active_ja = ds_anthrop_active.time.sel(time=ds_anthrop_active.time.dt.month.isin(months))
cnt=0
for year in range(1982,2014):
#print(ds_anthrop_active_ja.time.sel(time=ds_anthrop_active_ja.time.dt.year.isin([year])).values)
anthrop_active[cnt] = ds_anthrop_active_ja.time.sel(time=ds_anthrop_active_ja.time.dt.year.isin([year])).values.shape[0]
cnt=cnt+1
#ds_anthrop_.mean(dim='time')
ds_no_anthrop_active= ds_anthrop_.where(ds_no_anthrop_>ds_no_anthrop_.mean(dim='time')+ds_no_anthrop_.std(dim='time'), drop=True)
ds_no_anthrop_active_ja = ds_no_anthrop_active.time.sel(time=ds_no_anthrop_active.time.dt.month.isin(months))
cnt=0
for year in range(1982,2014):
#print(ds_no_anthrop_active_ja.time.sel(time=ds_no_anthrop_active_ja.time.dt.year.isin([year])).values)
no_anthrop_active[cnt] = ds_no_anthrop_active_ja.time.sel(time=ds_no_anthrop_active_ja.time.dt.year.isin([year])).values.shape[0]
cnt=cnt+1
#ds_anthrop_.mean(dim='time')
ds_imd_break= ds_imd_.rf.where(ds_imd_.rf<ds_imd_.rf.mean(dim='time')-ds_imd_.rf.std(dim='time'), drop=True)
ds_imd_break_ja = ds_imd_break.time.sel(time=ds_imd_break.time.dt.month.isin(months))
#print(ds_imd_active_ja.time)
cnt=0
for year in range(1982,2014):
#print(ds_imd_break_ja.time.sel(time=ds_imd_break_ja.time.dt.year.isin([year])).values)
imd_break[cnt] = ds_imd_break_ja.time.sel(time=ds_imd_break_ja.time.dt.year.isin([year])).values.shape[0]
cnt=cnt+1
#np.savetxt('imd_active.txt',ds_imd_active.time.sel(time=ds_imd_active.time.dt.year.isin([1982])).values )
ds_anthrop_break= ds_anthrop_.where(ds_anthrop_<ds_anthrop_.mean(dim='time')-ds_anthrop_.std(dim='time'), drop=True)
ds_anthrop_break_ja = ds_anthrop_break.time.sel(time=ds_anthrop_break.time.dt.month.isin(months))
cnt=0
for year in range(1982,2014):
#print(ds_anthrop_break_ja.time.sel(time=ds_anthrop_break_ja.time.dt.year.isin([year])).values)
anthrop_break[cnt] = ds_anthrop_break_ja.time.sel(time=ds_anthrop_break_ja.time.dt.year.isin([year])).values.shape[0]
cnt=cnt+1
#ds_anthrop_.mean(dim='time')
ds_no_anthrop_break= ds_no_anthrop_.where(ds_no_anthrop_<ds_no_anthrop_.mean(dim='time')-ds_no_anthrop_.std(dim='time'), drop=True)
ds_no_anthrop_break_ja = ds_no_anthrop_break.time.sel(time=ds_no_anthrop_break.time.dt.month.isin(months))
cnt=0
for year in range(1982,2014):
#print(ds_no_anthrop_break_ja.time.sel(time=ds_no_anthrop_break_ja.time.dt.year.isin([year])).values)
no_anthrop_break[cnt] = ds_no_anthrop_break_ja.time.sel(time=ds_no_anthrop_break_ja.time.dt.year.isin([year])).values.shape[0]
cnt=cnt+1
#ds_no_anthrop_.mean(dim='time')
data = {'years':years, 'data': imd_active}
df = pd.DataFrame(data=data)
df['kind'] = 'IMD'
data = {'years':years, 'data': anthrop_active}
df1 = pd.DataFrame(data=data)
df1['kind'] = 'Anthrop'
data = {'years':years, 'data': no_anthrop_active}
df2 = pd.DataFrame(data=data)
df2['kind'] = 'No-anthrop'
df_active = df.append(df1).append(df2)
fig = plt.figure(figsize=(20,10))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
g = sns.barplot(x="years", y="data", hue="kind", data=df_active, ax=ax)
g.legend_.set_title(None)
plt.grid()
plt.ylabel('Number of active days in JJAS', fontsize=18)
plt.xlabel('Years', fontsize=18)
ax.tick_params(axis='both', which='major', labelsize=14, rotation=45)
plt.legend(fontsize=18)
data = {'years':years, 'data': imd_break}
df = pd.DataFrame(data=data)
df['kind'] = 'IMD'
data = {'years':years, 'data': anthrop_break}
df1 = pd.DataFrame(data=data)
df1['kind'] = 'Anthrop'
data = {'years':years, 'data': no_anthrop_break}
df2 = pd.DataFrame(data=data)
df2['kind'] = 'No-anthrop'
df_break = df.append(df1).append(df2)
fig = plt.figure(figsize=(20,10))
ax = fig.add_axes([0.1,0.1,0.8,0.8])
g = sns.barplot(x="years", y="data", hue="kind", data=df_break, ax=ax)
g.legend_.set_title(None)
plt.grid()
plt.ylabel('Number of break days in JJAS', fontsize=18)
plt.xlabel('Years', fontsize=18)
ax.tick_params(axis='both', which='major', labelsize=14, rotation=45)
plt.legend(fontsize=18)