-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_CMI_current_drought.py
executable file
·187 lines (148 loc) · 6.02 KB
/
plot_CMI_current_drought.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
#!/usr/bin/env python
"""
Plot DJF for each year of the Millennium drought
"""
__author__ = "Martin De Kauwe"
__version__ = "1.0 (25.07.2019)"
__email__ = "mdekauwe@gmail.com"
import os
import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors
import cartopy.crs as ccrs
import cartopy
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import sys
import matplotlib.ticker as mticker
from cartopy.mpl.geoaxes import GeoAxes
from mpl_toolkits.axes_grid1 import AxesGrid
from calendar import monthrange
def main(fname, plot_dir, start_year, end_year):
ds = xr.open_dataset(fname)
lat = ds.y.values
lon = ds.x.values
bottom, top = lat[0], lat[-1]
left, right = lon[0], lon[-1]
nmonths, nrows, ncols = ds.Rainf.shape
nyears = (end_year - start_year) + 1
yr_count = 0
aet = np.zeros((nyears,nrows,ncols))
ppt = np.zeros((nyears,nrows,ncols))
sec_2_day = 86400.0
count = 0
yr_count = 0
mth_count = 1
if start_year == 2017:
# shift the start point onwards...as data starts in 2016
for year in np.arange(2016, 2017):
for month in np.arange(1, 13):
#print(year, month)
count += 1
for year in np.arange(start_year, end_year+1):
for month in np.arange(1, 13):
days_in_month = monthrange(year, month)[1]
conv = sec_2_day * days_in_month
yr_val = str(ds.time[count].values).split("-")[0]
print(yr_val)
aet[yr_count,:,:] += ds.Evap[count,:,:] * conv
ppt[yr_count,:,:] += ds.Rainf[count,:,:] * conv
mth_count += 1
if mth_count == 13:
mth_count = 1
yr_count += 1
count += 1
ppt = np.nanmean(ppt, axis=0)
aet = np.nanmean(aet, axis=0)
cmi = np.where(~np.isnan(aet), ppt-aet, np.nan)
fig = plt.figure(figsize=(9, 6))
plt.rcParams['font.family'] = "sans-serif"
plt.rcParams['font.size'] = "14"
plt.rcParams['font.sans-serif'] = "Helvetica"
cmap = plt.cm.get_cmap('BrBG', 10) # discrete colour map
projection = ccrs.PlateCarree()
axes_class = (GeoAxes, dict(map_projection=projection))
rows = 1
cols = 1
axgr = AxesGrid(fig, 111, axes_class=axes_class,
nrows_ncols=(rows, cols),
axes_pad=0.2,
cbar_location='right',
cbar_mode='single',
cbar_pad=0.5,
cbar_size='5%',
label_mode='') # note the empty label_mode
for i, ax in enumerate(axgr):
# add a subplot into the array of plots
plims = plot_map(ax, cmi, cmap, i, top, bottom, left, right)
"""
import cartopy.feature as cfeature
states = cfeature.NaturalEarthFeature(category='cultural',
name='.in_1_states_provinces_lines',
scale='10m',facecolor='none')
# plot state border
SOURCE = 'Natural Earth'
LICENSE = 'public domain'
ax.add_feature(states, edgecolor='black', lw=0.5)
"""
from cartopy.feature import ShapelyFeature
from cartopy.io.shapereader import Reader
#fname = '/Users/mdekauwe/research/Drought_linkage/Bios2_SWC_1979_2013/AUS_shape/STE11aAust.shp'
fname = "/Users/mdekauwe/Dropbox/ne_10m_admin_1_states_provinces_lines/ne_10m_admin_1_states_provinces_lines.shp"
shape_feature = ShapelyFeature(Reader(fname).geometries(),
ccrs.PlateCarree(), edgecolor='black')
ax.add_feature(shape_feature, facecolor='none', edgecolor='black',
lw=0.5)
cbar = axgr.cbar_axes[0].colorbar(plims)
cbar.ax.set_title("P-AET\n(mm yr$^{-1}$)", fontsize=16, pad=10)
#cbar.ax.set_yticklabels([' ', '$\minus$40', '$\minus$20', '0', '20', '40-530'])
props = dict(boxstyle='round', facecolor='white', alpha=0.0, ec="white")
ax.text(0.95, 0.05, "(d)", transform=ax.transAxes, fontsize=12,
verticalalignment='top', bbox=props)
props = dict(boxstyle='round', facecolor='white', alpha=0.0, ec="white")
ax.text(0.95, 0.05, "(d)", transform=ax.transAxes, fontsize=12,
verticalalignment='top', bbox=props)
ofname = os.path.join(plot_dir, "cmi_current_drought.png")
fig.savefig(ofname, dpi=300, bbox_inches='tight',
pad_inches=0.1)
def plot_map(ax, var, cmap, i, top, bottom, left, right):
print(np.nanmin(var), np.nanmax(var))
vmin, vmax = -200, 200
#top, bottom = 90, -90
#left, right = -180, 180
img = ax.imshow(var, origin='lower',
transform=ccrs.PlateCarree(),
interpolation='nearest', cmap=cmap,
extent=(left, right, bottom, top),
vmin=vmin, vmax=vmax)
ax.coastlines(resolution='10m', linewidth=1.0, color='black')
#ax.add_feature(cartopy.feature.OCEAN)
ax.set_xlim(140.7, 154)
ax.set_ylim(-39.2, -28.1)
if i == 0 or i >= 5:
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=0.5, color='black', alpha=0.5,
linestyle='--')
else:
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False,
linewidth=0.5, color='black', alpha=0.5,
linestyle='--')
#if i < 5:
#s gl.xlabels_bottom = False
if i > 5:
gl.ylabels_left = False
gl.xlabels_top = False
gl.ylabels_right = False
gl.xlines = False
gl.ylines = False
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlocator = mticker.FixedLocator([141, 145, 149, 153])
gl.ylocator = mticker.FixedLocator([-29, -32, -35, -38])
return img
if __name__ == "__main__":
plot_dir = "plots"
if not os.path.exists(plot_dir):
os.makedirs(plot_dir)
fname = "outputs/all_yrs_CMI.nc"
main(fname, plot_dir, 2017, 2019)