forked from jcabaker/land_atm_coupling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzengs_gamma.py
251 lines (206 loc) · 7.99 KB
/
zengs_gamma.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
;;#############################################################################
;;
;; zengs_gamma.py
;; Author: Jess Baker ([email protected])
;; LAPSE project, part of CSSP Brazil
;; University of Leeds, UK
;;
;;#############################################################################
;;
;; Description
;; This script calculates Zeng's Gamma for all grid cells in input array(s).
;;
;; Zeng's gamma is the correlation between evaporation and precipitation
;; scaled by the standard deviation of the evaporation and normalized by the
;; standard deviation of precipitation to keep the index dimensionless.
;;
;; Γ = r(P',E')* σ(E')
;; _____
;; σ(P')
;;
;; Requirements
;; Takes precipitation and evapotranspiration as input variables. Data
;; should be formatted as Iris cubes, constrained to the same time period.
;;
;; Reference
;; Zeng et al (2010) Comparison of Land-Precipitation Coupling Strength
;; Using Observations and Models
;;
;;#############################################################################
"""
# Required Python packages
import numpy as np
import iris.coord_categorisation
import iris
import matplotlib.pyplot as plt
import copy
import os
from mpl_toolkits import basemap
from scipy.stats import pearsonr
from scipy.stats import spearmanr
from mpl_toolkits.basemap import maskoceans
from datetime import datetime
def main(pre_cube, et_cube, plotting=False,
plotting_args={'lat_lims': [-50, 20],
'lon_lims': [-100, -30]},
anom=True, corr_method='pearson'):
"""
This function uses precipitation and evapotranspiration data to calculate
Zeng's gamma.
Takes Iris cubes as input.
Arguments
pre_cube = Iris cube of precipitation (model or observations).
et_cube = Iris cube of evapotranspiration (model or observations).
plotting = Boolean. Plot output of metric. If False returns output of
metric as array only.
plotting_args = limits for output map.
anom = Boolean. Calculate metric using anomalies from climatological
seasonal cycle (True) or interannual monthly data
(False).
corr_method = correlation method. Can be 'pearson' (assumes data are
normally distributed) or 'spearman' (no assumption
about the distribution).
"""
# Calculate anomalies versus climatological seasonal cycle
if anom is True:
pre_anom = monthly_anom_cube(pre_cube)
et_anom = monthly_anom_cube(et_cube)
else:
# only if anom calculated before calling metric
pre_anom = pre_cube
et_anom = et_cube
# Check if lats are ascending, if not then reverse
pre_anom = flip_lats(pre_anom)
et_anom = flip_lats(et_anom)
# Reorder data from -180 to +180 degrees
pre_lon = pre_anom.coord('longitude').points
if pre_lon.max() > 180:
pre_anom = minus180_to_plus180(pre_anom)
et_lon = et_anom.coord('longitude').points
if et_lon.max() > 180:
et_anom = minus180_to_plus180(et_anom)
# Compute correlation
rvals = np.nan * np.zeros((pre_anom.shape[-2], pre_anom.shape[-1]))
pvals = np.nan * np.zeros((pre_anom.shape[-2], pre_anom.shape[-1]))
for nlat in range(pre_anom.shape[-2]):
for nlon in range(pre_anom.shape[-1]):
x = pre_anom.data[:, nlat, nlon]
y = et_anom.data[:, nlat, nlon]
mask = ~np.isnan(x) & ~np.isnan(y)
try:
if corr_method == 'pearson':
r, p = pearsonr(x[mask], y[mask])
if corr_method == 'spearman':
r, p = spearmanr(x[mask], y[mask])
rvals[nlat, nlon] = r
pvals[nlat, nlon] = p
except ValueError:
rvals[nlat, nlon] = np.nan
pvals[nlat, nlon] = np.nan
continue
# Get standard deviation of evapotranspiration
sd_et = et_anom.collapsed('time', iris.analysis.STD_DEV).data
# Get standard deviation of precipitation
sd_pre = pre_anom.collapsed('time', iris.analysis.STD_DEV).data
# Calculate gamma
gamma = rvals *(sd_et/sd_pre)
lat = pre_anom.coord('latitude').points
lon = pre_anom.coord('longitude').points
if plotting is True:
lat_lims = plotting_args['lat_lims']
lon_lims = plotting_args['lon_lims']
plot_array(gamma, lat, lon, lat_lims, lon_lims)
return(gamma, pvals, lat, lon)
def monthly_anom_cube(cube, fill=None):
# Extract data array and identify nans
ds = np.array(cube.data)
if fill is not None:
ds[np.where(ds == fill)] = np.nan
# Find where original dataset is masked
mask = np.where(ds >= 1e20)
# Group data by month and calculate anomaly from seaonal climatology
if len(ds.shape) == 3:
# Check if analysis on seasonal cube
try:
nmonth = len(cube.coord('season').points[0])
except:
nmonth = 12
ds = ds.reshape(-1, nmonth, cube.shape[-2], cube.shape[-1])
anomalies = np.nan * np.zeros((ds.shape))
for mn in range(nmonth):
anomalies[:, mn, :, :] = ds[:, mn, :, :] - \
np.nanmean(ds[:, mn, :, :], axis=0)
cube2 = cube.copy()
cube2.data = anomalies.reshape((-1, cube.shape[-2], cube.shape[-1]))
cube2.data[mask] = np.nan
# Remove null values
cube2.data[cube2.data >= 1e20] = np.nan
cube2.data[cube2.data <= -1e20] = np.nan
return(cube2)
def minus180_to_plus180(var_cube):
"""
Function to reorder cube data from -180 to +180.
"""
# Reorganise data
var = var_cube.data
lat = var_cube.coord('latitude').points
if len(lat.shape) > 1:
lat = lat[:, 0]
lon = var_cube.coord('longitude').points
if len(lon.shape) > 1:
lon = lon[0, :]
half = int(var.shape[-1]/2)
temp1 = var[:, :, 0:half]
temp2 = var[:, :, half:]
new_var = np.concatenate((temp2, temp1), axis=2)
new_lon = np.arange(-180, 180, (abs(lon[1]-lon[0])))
# Save re-ordered data as new cube
new_cube = var_cube.copy()
new_cube.data = new_var
new_cube.coord('longitude').points = new_lon
return(new_cube)
def plot_array(array, lat, lon, lat_lims, lon_lims):
# Corners of subset map
lat1 = lat_lims[0]
lat2 = lat_lims[1]
lon1 = lon_lims[0]
lon2 = lon_lims[1]
cmap = copy.copy(plt.cm.RdBu_r)
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
m = basemap.Basemap(projection='mill',
llcrnrlat=lat1, urcrnrlat=lat2,
llcrnrlon=lon1, urcrnrlon=lon2,
lat_ts=20, resolution='c')
lons1, lats1 = np.meshgrid(lon, lat)
x, y = m(lons1, lats1)
m.drawcoastlines()
ds_new = maskoceans(lons1, lats1, array)
levels = np.linspace(-0.5, 0.5, 11)
cs = m.contourf(x, y, ds_new, levels=levels, cmap=cmap, extend='both')
plt.colorbar(cs, orientation='vertical', pad=0.05)
title = ("Zeng's gamma")
ax.set_title(title)
path = str(os.getcwd()) + '/'
print(path)
today = datetime.today()
date = today.strftime("_%d.%m.%Y")
fname = 'zengs_gamma' + date + '.png'
plt.savefig(path+fname, dpi=300, bbox_inches='tight')
def flip_lats(data_cube):
lats = data_cube.coord('latitude').points
# Check if lats need flipping
if lats[0] < lats[-1]:
print('Lats already ascending')
return(data_cube)
else:
new_cube = data_cube.copy()
new_lats = lats[::-1]
new_data = data_cube.data[:, ::-1, :]
new_cube.data = new_data
new_cube.coord('latitude').points = new_lats
print('Lats flipped')
return(new_cube)