-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfigures.py
478 lines (415 loc) · 13.8 KB
/
figures.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Autopep8: https://pypi.org/project/autopep8/
# Check with http://pep8online.com/
# Make different plot types
import proplot as plot
from variables import get_var_infos
from zones import get_zone
def plot_ref_new_obs(
var, ref, new, obs, label, units,
levels, cmap, extend,
levels_diff, cmap_diff, extend_diff,
levels_bias, cmap_bias, extend_bias,
save=False, dpi=300
):
"""
Plot 2 simulations versus observations. First row contains the 2
simulations and their differences (diff). Second raw contains their
bias regarding to observation and the observation itself.
Parameters
----------
var : str
Variable name. Options are:
- 'snc', 'frac_snow'
- 'tas'
- 'pr'
label : str
Name of the variable.
units : str
Units of the variables.
ref, new, obs : DataArray
Reference, new simulation and observation. The following attributes
are needed:
- 'title' for the name of the plots
- 'period' for the suptitle and saving the figure
- 'season' for the suptitle and saving the figure
- 'zone' for the suptitle and saving the figure
levels, levels_diff, levels_bias : numpy.ndarray, optional
Levels for the plots.
cmap, cmap_diff, cmap_bias : colormap spec
Colormaps for the plots.
extend, extend_diff, extend_bias : numpy.ndarray
Extends for the plots.
save : bool, optional
Save the figure or not. Default is None (does not save the figure).
Save figures to jpg, png and pdf.
dpi : int, optional
DPI of the figure. Default is 300.
Example
-------
>>> import xarray as xr
>>> import proplot as plot
>>> import sys
>>> sys.path.insert(1, '/home/mlalande/notebooks/utils')
>>> import utils as u
>>>
>>> period = slice('1979','2014')
>>> var = 'snc'
>>> label, units, cmap = u.get_var_infos('snc')
>>>
>>> ref = xr.open_dataarray(...); ref.attrs['title'] = ...
>>> new = xr.open_dataarray(...); new.attrs['title'] = ...
>>> obs = xr.open_dataarray(...); obs.attrs['title'] = ...
>>>
>>> clim_ref = u.clim(ref.sel(time=period))
>>> clim_new = u.clim(ref.sel(time=period))
>>> clim_obs = u.clim(ref.sel(time=period))
>>>
>>> levels = plot.arange(0,100,10)
>>> extend = 'neither'
>>>
>>> levels_diff = plot.arange(-30,30,5)
>>> cmap_diff = 'BuRd_r'
>>> extend_diff = 'both'
>>>
>>> levels_bias = plot.arange(-100,100,20)
>>> cmap_bias = 'BuRd_r'
>>> extend_bias = 'neither'
>>>
>>> u.plot_ref_new_obs(
var, clim_ref, clim_new, clim_obs, label, units,
levels, cmap, extend,
levels_diff, cmap_diff, extend_diff,
levels_bias, cmap_bias, extend_bias,
save=False, dpi=300
)
"""
# Change orientation for some zones
if ref.attrs['zone'] not in ['NH']:
axwidth = 2.5
ncols = 3; nrows = 2; cbar_loc = 'r'
i_ref = 0; i_new = 1; i_diff = 2
i_ref_bias = 3; i_new_bias = 4; i_obs = 5
else:
axwidth = 4.5
ncols = 2; nrows = 3; cbar_loc = 'b'
i_ref = 0; i_new = 2; i_diff = 4
i_ref_bias = 1; i_new_bias = 3; i_obs = 5
fig, axs = plot.subplots(proj='cyl', ncols=ncols, nrows=nrows,
axwidth=axwidth)
# Reference simulation
m0 = axs[i_ref].pcolormesh(ref, cmap=cmap, levels=levels, extend=extend)
axs[i_ref].format(title=ref.title)
# New simulation
axs[i_new].pcolormesh(new, cmap=cmap, levels=levels, extend=extend)
axs[i_new].format(title=new.title)
axs[i_new].colorbar(m0, label=label + ' [' + units + ']', loc=cbar_loc)
# Differences between the new and reference simulation
m2 = axs[i_diff].pcolormesh(new - ref, cmap=cmap_diff, levels=levels_diff,
extend=extend_diff)
axs[i_diff].format(title=ref.title + '\n - ' + new.title)
axs[i_diff].colorbar(m2,
label='Difference of\n' + label + ' [' + units + ']',
loc=cbar_loc)
# Bias of reference simulation regarding to observation
m3 = axs[i_ref_bias].pcolormesh(ref - obs, cmap=cmap_bias,
levels=levels_bias, extend=extend_bias)
axs[i_ref_bias].format(title=ref.title + '\n - ' + obs.obs_name)
# Bias of new simulation regarding to observation
axs[i_new_bias].pcolormesh(new - obs, cmap=cmap_bias, levels=levels_bias,
extend=extend_bias)
axs[i_new_bias].format(title=new.title + '\n - ' + obs.obs_name)
axs[i_new_bias].colorbar(m3,
label='Bias of\n' + label + ' [' + units + ']',
loc=cbar_loc)
# Observation
axs[i_obs].pcolormesh(obs, cmap=cmap, levels=levels, extend=extend)
axs[i_obs].format(title=obs.obs_name)
axs[i_obs].colorbar(m0, label=label + ' [' + units + ']', loc=cbar_loc)
axs.format(
labels=True,
coast=True,
borders=True,
latlim=(ref.lat.min(), ref.lat.max()),
lonlim=(ref.lon.min(), ref.lon.max()),
suptitle=label + " " + ref.attrs['season'] + " climatology: " + \
new.attrs['period'],
abc=True
)
if save:
for extension in ['jpg', 'png', 'pdf']:
fig.save(
'img/' +
new.attrs['title'] +
'/' +
var +
'_' +
ref.attrs['zone'] +
'_' +
ref.attrs['season'] +
'_clim_' +
new.attrs['period'] +
'_' +
new.attrs['title'] +
'_REF_' +
obs.attrs['obs_name'] +
'.' +
extension)
def plot_zonal_bias_HMA(
var, ref, new, obs, label, units,
levels_diff, cmap_diff, extend_diff,
levels_bias, cmap_bias, extend_bias,
save=False, dpi=300
):
"""
Plot zonal climatology bias of 2 simulations versus observations and
their differences. First (second) row contains the first (second)
simulation versus observations and the third row contains the
differences. The three columns correspond to global, HMA and global
without HMA domains.
Parameters
----------
var : str
Variable name. Options are: 'ta'.
label : str
Name of the variable.
units : str
Units of the variables.
ref, new, obs : DataArray
Reference, new simulation and observation. The following attributes
are needed:
- 'title' for the name of the plots
- 'period' for the suptitle and saving the figure
- 'season' for the suptitle and saving the figure
- 'zone' for the suptitle and saving the figure
levels_diff, levels_bias : numpy.ndarray, optional
Levels for the plots.
cmap_diff, cmap_bias : colormap spec
Colormaps for the plots.
extend_diff, extend_bias : numpy.ndarray
Extends for the plots.
save : bool, optional
Save the figure or not. Default is None (does not save the figure).
Save figures to jpg, png and pdf.
dpi : int, optional
DPI of the figure. Default is 300.
Example
-------
>>> import xarray as xr
>>> import proplot as plot
>>> import sys
>>> sys.path.insert(1, '/home/mlalande/notebooks/utils')
>>> import utils as u
>>>
>>> period = slice('1979','2014')
>>> var = 'ta'
>>> label, units, cmap = u.get_var_infos(var)
>>>
>>> ref = xr.open_dataarray(...); ref.attrs['title'] = ...
>>> new = xr.open_dataarray(...); new.attrs['title'] = ...
>>> obs = xr.open_dataarray(...); obs.attrs['title'] = ...
>>>
>>> clim_ref = u.clim(ref.sel(time=period))
>>> clim_new = u.clim(ref.sel(time=period))
>>> clim_obs = u.clim(ref.sel(time=period))
>>>
>>> label, units, \
levels, cmap, extend, \
levels_diff, cmap_diff, extend_diff, \
levels_bias, cmap_bias, extend_bias = u.get_var_infos(var)
>>>
>>> u.plot_ref_new_obs(
var, clim_ref, clim_new, clim_obs, label, units,
levels_diff, cmap_diff, extend_diff,
levels_bias, cmap_bias, extend_bias,
save=False, dpi=300
)
"""
fig, axs = plot.subplots(nrows=3, ncols=3)
k = 0 # Index for axes
color = 'black' # Line color for climatology contour
lw = 0.6 # Line width for climatology contour
alpha = 0.8 # Transparence for climatology contour
ylim = (1000,0)
latlim, lonlim = get_zone('HMA')
################################################
# Bias between simulations versus observations #
################################################
for simu in [ref, new]:
bias = simu - obs
# Global
m = axs[k].contourf(
bias.mean('lon', skipna=False),
cmap=cmap_bias,
levels=levels_bias,
extend=extend_bias
)
axs[k].contour(
simu.mean('lon', skipna=False),
color=color,
lw=lw,
alpha=alpha,
labels=True
)
axs[k].format(
ylim=ylim,
title=''
)
k += 1
# HMA
axs[k].contourf(
bias.sel(lon=lonlim).mean('lon', skipna=False),
cmap=cmap_bias,
levels=levels_bias,
extend=extend_bias
)
axs[k].contour(
simu.sel(lon=lonlim).mean('lon', skipna=False),
color=color,
lw=lw,
alpha=alpha,
labels=True
)
axs[k].format(
ylim=ylim,
title=simu.attrs['title'] + '\n - ' + obs.attrs['obs_name']
)
k += 1
# Global without HMA
axs[k].contourf(
bias.where(
(bias.lon < lonlim.start) | (bias.lon > lonlim.stop),
drop=True
).mean('lon', skipna=False),
cmap=cmap_bias,
levels=levels_bias,
extend=extend_bias
)
axs[k].contour(
simu.where(
(bias.lon < lonlim.start) | (bias.lon > lonlim.stop),
drop=True
).mean('lon', skipna=False),
color=color,
labels=True,
lw=lw,
alpha=alpha
)
axs[k].format(
ylim=ylim,
title=''
)
k += 1
fig.colorbar(
m,
label='Bias of ' + label + ' [' + units + ']',
loc='r',
rows=(1, 2)
)
###########################################
# Differences between the two simulations #
###########################################
diff = new - ref
# Global
m = axs[k].contourf(
diff.mean('lon', skipna=False),
cmap=cmap_diff,
levels=levels_diff,
extend=extend_diff
)
axs[k].contour(
new.mean('lon', skipna=False),
color=color,
lw=lw,
alpha=alpha,
labels=True
)
axs[k].format(
ylim=ylim,
title=''
)
k += 1
# HMA
axs[k].contourf(
diff.sel(lon=lonlim).mean('lon', skipna=False),
cmap=cmap_diff,
levels=levels_diff,
extend=extend_diff
)
axs[k].contour(
new.sel(lon=lonlim).mean('lon', skipna=False),
color=color,
lw=lw,
alpha=alpha,
labels=True
)
axs[k].format(
ylim=ylim,
title=new.attrs['title'] + '\n - ' + ref.attrs['title']
)
k += 1
# Global without HMA
axs[k].contourf(
diff.where(
(diff.lon < lonlim.start) | (diff.lon > lonlim.stop),
drop=True
).mean('lon', skipna=False),
cmap=cmap_diff,
levels=levels_diff,
extend=extend_diff
)
axs[k].contour(
new.where(
(bias.lon < lonlim.start) | (bias.lon > lonlim.stop),
drop=True
).mean('lon', skipna=False),
color=color,
labels=True,
lw=lw,
alpha=alpha
)
axs[k].format(
ylim=ylim,
title=''
)
k += 1
fig.colorbar(
m,
label='Difference of ' + label + ' [' + units + ']',
loc='r',
row=3
)
##########
# Format #
##########
axs.format(
suptitle=label + " " + ref.attrs['season'] + " zonal climatology bias: " + \
new.attrs['period'],
abc=True,
collabels=[
'Global',
'HMA ('+str(lonlim.start)+'°E-'+str(lonlim.stop)+'°E)',
'Global without HMA',
],
ylabel='Level (hPa)'
)
if save:
for extension in ['jpg', 'png', 'pdf']:
fig.save(
'img/' +
new.attrs['title'] +
'/' +
var +
'_' +
ref.attrs['zone'] +
'_' +
ref.attrs['season'] +
'_zonal_clim_bias_diff_' +
new.attrs['period'] +
'_' +
new.attrs['title'] +
'_REF_' +
obs.attrs['obs_name'] +
'.' +
extension)