-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
1027 lines (890 loc) · 30.4 KB
/
Copy pathvalidation.py
File metadata and controls
1027 lines (890 loc) · 30.4 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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import calendar
import sys
import geopandas as gp
from clisops.core.subset import subset_shape
import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy
import xesmf as xe
from xskillscore import pearson_r
import cmdline_provenance as cmdprov
sys.path.append('/g/data/xv83/quantile-mapping/qqscale')
import utils
linestyles = {
'hist': 'solid',
'ref': 'solid',
'target': 'dotted',
'qq': 'dotted',
'qq-cmatch': 'dotted',
}
def calc_seasonal_change_diff(da_hist, da_ref, da_target, da_qq, scaling):
"""Calculate difference in seasonal change."""
hist_monthly_clim = da_hist.groupby('time.month').mean('time')
ref_monthly_clim = da_ref.groupby('time.month').mean('time')
target_monthly_clim = da_target.groupby('time.month').mean('time')
qq_monthly_clim = da_qq.groupby('time.month').mean('time')
if scaling == 'additive':
model_change = ref_monthly_clim - hist_monthly_clim
qq_change = qq_monthly_clim - target_monthly_clim
units = da_hist.attrs['units']
elif scaling == 'multiplicative':
model_change = ((ref_monthly_clim - hist_monthly_clim) / hist_monthly_clim) * 100
qq_change = ((qq_monthly_clim - target_monthly_clim) / target_monthly_clim) * 100
units = '%'
qq_change, model_change = match_grids(qq_change, model_change)
diffs = np.abs(qq_change - model_change)
mean_diff = diffs.mean(dim='month').compute()
mean_diff.attrs['units'] = units
return mean_diff
def plot_seasonal_change_diff(
seasonal_diff,
land_only=False,
city_lat_lon={},
outfile=None,
levels=None,
):
"""Plot difference in seasonal change"""
if land_only:
shape = gp.read_file('/g/data/ia39/aus-ref-clim-data-nci/shapefiles/data/australia/australia.shp')
seasonal_diff = subset_shape(seasonal_diff, shape=shape)
fig = plt.figure(figsize=[10, 5])
ax = fig.add_subplot(111, projection=ccrs.PlateCarree(central_longitude=180))
units = seasonal_diff.attrs['units']
seasonal_diff.plot(
ax=ax,
transform=ccrs.PlateCarree(),
cmap='Oranges',
cbar_kwargs={'label': f'average magnitude of monthly difference ({units})'},
levels=levels,
extend='max',
)
ax.set_title('difference in monthly change (model vs. qq)')
ax.coastlines()
ax.add_feature(cartopy.feature.STATES)
for lat, lon in city_lat_lon.values():
ax.plot(
lon,
lat,
marker='o',
markerfacecolor='lime',
markeredgecolor='none',
zorder=5,
transform=ccrs.PlateCarree()
)
xmin = 112.92
xmax = 153.63
ymin = -43.625
ymax = -10.07
ax.set_extent([xmin, xmax, ymin, ymax], crs=ccrs.PlateCarree())
if outfile:
plt.savefig(outfile, bbox_inches='tight', facecolor='white', dpi=300)
else:
plt.show()
def plot_monthly_change_sign_agreement(
da_before,
da_after,
land_only=False,
city_lat_lon={},
outfile=None,
):
"""Plot monthly change sign agreement.
Image shows number of months where the
climatological mean increases.
"""
before_monthly_clim = da_before.groupby('time.month').mean('time')
after_monthly_clim = da_after.groupby('time.month').mean('time')
change = after_monthly_clim - before_monthly_clim
increase = change > 0
count = increase.sum(dim='month')
if land_only:
shape = gp.read_file('/g/data/ia39/aus-ref-clim-data-nci/shapefiles/data/australia/australia.shp')
count = subset_shape(count, shape=shape)
fig = plt.figure(figsize=[10, 5])
ax = fig.add_subplot(111, projection=ccrs.PlateCarree(central_longitude=180))
cax = count.plot(
ax=ax,
transform=ccrs.PlateCarree(),
levels=[-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5],
add_colorbar=False,
)
fig.colorbar(
cax,
ticks=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
label='number of months',
)
ax.set_title('Number of months where the climatological mean increases')
ax.coastlines()
ax.add_feature(cartopy.feature.STATES)
for lat, lon in city_lat_lon.values():
ax.plot(
lon,
lat,
marker='o',
markerfacecolor='lime',
markeredgecolor='none',
zorder=5,
transform=ccrs.PlateCarree()
)
xmin = 112.92
xmax = 153.63
ymin = -43.625
ymax = -10.07
ax.set_extent([xmin, xmax, ymin, ymax], crs=ccrs.PlateCarree())
if outfile:
plt.savefig(outfile, bbox_inches='tight', facecolor='white', dpi=300)
else:
plt.show()
def monthly_annual_pct(ds, var):
"""Monthly mean precip expressed as a percentage of the annual mean."""
da_monthly_mean = ds[var].groupby('time.month').mean('time')
da_annual = ds[var].resample(time='Y').sum()
da_annual_mean = da_annual.mean('time')
da_monthly_annual_pct = (da_monthly_mean / da_annual_mean) * 100
da_monthly_annual_pct = da_monthly_annual_pct.compute()
return da_monthly_annual_pct
def calc_seasonal_correlation(ds_target, var_target, ds_qq, var_qq):
"""Calculate correlation between model and obs monthly climatology"""
if 'pr' in [var_target, var_qq]:
target_monthly_clim = monthly_annual_pct(ds_target, var_target)
qq_monthly_clim = monthly_annual_pct(ds_qq, var_qq)
else:
target_monthly_clim = ds_target[var_target].groupby('time.month').mean('time')
target_monthly_clim = target_monthly_clim.compute()
qq_monthly_clim = ds_qq[var_qq].groupby('time.month').mean('time')
qq_monthly_clim = qq_monthly_clim.compute()
seasonal_r = pearson_r(target_monthly_clim, qq_monthly_clim, 'month')
seasonal_r = seasonal_r.compute()
return seasonal_r
def plot_seasonal_correlation(
seasonal_r,
land_only=False,
city_lat_lon={},
outfile=None,
):
"""Plot the correlation between model and obs monthly climatology"""
if land_only:
shape = gp.read_file('/g/data/ia39/aus-ref-clim-data-nci/shapefiles/data/australia/australia.shp')
seasonal_r = subset_shape(seasonal_r, shape=shape)
fig = plt.figure(figsize=[10, 5])
ax = fig.add_subplot(111, projection=ccrs.PlateCarree(central_longitude=180))
seasonal_r.plot(
ax=ax,
transform=ccrs.PlateCarree(),
cmap='RdBu_r',
cbar_kwargs={'label': 'correlation'},
levels=[-1.0, -0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1.0],
)
ax.set_title('seasonal cycle correlation (target vs. qq)')
ax.coastlines()
ax.add_feature(cartopy.feature.STATES)
for lat, lon in city_lat_lon.values():
ax.plot(
lon,
lat,
marker='o',
markerfacecolor='lime',
markeredgecolor='none',
zorder=5,
transform=ccrs.PlateCarree()
)
xmin = 112.92
xmax = 153.63
ymin = -43.625
ymax = -10.07
ax.set_extent([xmin, xmax, ymin, ymax], crs=ccrs.PlateCarree())
if outfile:
plt.savefig(outfile, bbox_inches='tight', facecolor='white', dpi=300)
else:
plt.show()
def quantile_spatial_plot(
da, month, cmap, levels, lat_bounds=None, lon_bounds=None, city_lat_lon={},
):
"""Spatial plot of the 10th, 50th and 90th percentile"""
da_selection = da.sel({'quantiles': [.1, .5, .9], 'month': month}, method='nearest')
if lat_bounds:
lat_min, lat_max = lat_bounds
da_selection = da_selection.sel(lat=slice(lat_min, lat_max))
if lon_bounds:
lon_min, lon_max = lon_bounds
da_selection = da_selection.sel(lon=slice(lon_min, lon_max))
p = da_selection.plot(
col='quantiles',
transform=ccrs.PlateCarree(),
cmap=cmap,
figsize=[20, 5.5],
subplot_kws={'projection': ccrs.PlateCarree(),},
levels=levels,
)
for ax in p.axes.flat:
ax.coastlines()
for lat, lon in city_lat_lon.values():
ax.plot(
lon,
lat,
marker='o',
markerfacecolor='lime',
markeredgecolor='none',
zorder=5,
transform=ccrs.PlateCarree()
)
plt.suptitle(calendar.month_name[month])
plt.show()
def quantile_month_plot(quantiles, ax, cmap, levels=None, extend='both', point=None, title=None):
"""Create two dimensional month/quantile plot"""
kwargs = {}
if levels:
kwargs['levels'] = levels
else:
data_max = np.abs(quantiles).max()
kwargs['vmin'] = data_max * -1
kwargs['vmax'] = data_max
if quantiles.ndim == 1:
quantiles = quantiles.expand_dims(dim={'month': 1})
input_ndims = 1
else:
assert quantiles.ndim == 2
input_ndims = 2
quantiles.transpose('month', 'quantiles').plot.imshow(
ax=ax,
cmap=cmap,
extend=extend,
**kwargs
)
if input_ndims == 1:
ax.set_yticks([1], [])
ax.set_ylabel('')
ax.set_xlabel('')
else:
yticks = np.arange(1,13)
ytick_labels = [calendar.month_abbr[i] for i in yticks]
ax.set_yticks(yticks, ytick_labels)
ax.invert_yaxis()
if title:
ax.set_title(title)
def match_grids(primary_da, secondary_da):
"""Match grids (to the highest resolution)"""
da1 = primary_da.copy()
da2 = secondary_da.copy()
if len(da1['lat']) > len(da2['lat']):
regridder = xe.Regridder(da2, da1, "bilinear")
da2 = regridder(da2)
da2 = da2.compute()
elif len(da1['lat']) < len(da2['lat']):
regridder = xe.Regridder(da1, da2, "bilinear")
da1 = regridder(da1)
da1 = da1.compute()
return da1, da2
def spatial_comparison_data(da1, da2, scaling):
"""Compare two spatial fields."""
global_attrs = da1.attrs
primary_da, secondary_da = match_grids(da1, da2)
if scaling == 'additive':
comparison = primary_da - secondary_da
elif scaling == 'multiplicative':
comparison = ((primary_da - secondary_da) / secondary_da) * 100
comparison.attrs = global_attrs
if scaling == 'multiplicative':
comparison.attrs['units'] = '% difference'
return comparison
def spatial_comparison_plot(
da_clim1,
da_clim2,
da_comp,
clim1_title,
clim2_title,
clim_cmap,
comp_cmap,
clim_levels,
comp_levels,
scaling,
land_only=False,
city_lat_lon={},
clim_extend='max',
outfile=None,
print_mav=False,
):
"""Spatial plot of two climatologies and their difference."""
if land_only:
shape = gp.read_file('/g/data/ia39/aus-ref-clim-data-nci/shapefiles/data/australia/australia.shp')
da_clim1 = subset_shape(da_clim1, shape=shape)
da_clim2 = subset_shape(da_clim2, shape=shape)
da_comp = subset_shape(da_comp, shape=shape)
fig = plt.figure(figsize=[24, 6])
ax1 = fig.add_subplot(131, projection=ccrs.PlateCarree())
da_clim1.plot(
ax=ax1,
transform=ccrs.PlateCarree(),
cmap=clim_cmap,
levels=clim_levels,
extend=clim_extend,
)
ax1.set_title(clim1_title)
ax2 = fig.add_subplot(132, projection=ccrs.PlateCarree())
da_clim2.plot(
ax=ax2,
transform=ccrs.PlateCarree(),
cmap=clim_cmap,
levels=clim_levels,
extend=clim_extend,
)
ax2.set_title(clim2_title)
ax3 = fig.add_subplot(133, projection=ccrs.PlateCarree())
da_comp.plot(
ax=ax3,
transform=ccrs.PlateCarree(),
cmap=comp_cmap,
levels=comp_levels,
extend='both'
)
comp_text = 'Difference' if scaling == 'additive' else 'Ratio'
ax3.set_title(comp_text)
if print_mav:
mav = np.nanmean(np.abs(da_comp))
print(f'mean absolute value: {mav:.2f} ')
for ax in [ax1, ax2, ax3]:
ax.coastlines()
for lat, lon in city_lat_lon.values():
ax.plot(
lon,
lat,
marker='o',
markerfacecolor='lime',
markeredgecolor='none',
zorder=5,
transform=ccrs.PlateCarree()
)
xmin = 112.92
xmax = 153.63
ymin = -43.625
ymax = -10.07
ax1.set_extent([xmin, xmax, ymin, ymax], crs=ccrs.PlateCarree())
ax2.set_extent([xmin, xmax, ymin, ymax], crs=ccrs.PlateCarree())
ax3.set_extent([xmin, xmax, ymin, ymax], crs=ccrs.PlateCarree())
if outfile:
plt.savefig(outfile, bbox_inches='tight', facecolor='white', dpi=300)
else:
plt.show()
def plot_quantiles_2d_point(
da_hist_q_point,
da_ref_point,
da_target_point,
da_af_point,
variable,
quantiles,
general_cmap,
af_cmap,
general_levels,
af_levels,
):
"""Plot historical, reference and target quantiles for a single grid point."""
if da_hist_q_point.ndim == 1:
timescale = 'annual'
height = 14
else:
timescale = 'monthly'
height = 24
da_ref_q_point = utils.get_quantiles(da_ref_point, quantiles, timescale=timescale)
da_target_q_point = utils.get_quantiles(da_target_point, quantiles, timescale=timescale)
extend = 'max' if 'pr' in variable else 'both'
fig = plt.figure(figsize=[20, height])
ax1 = fig.add_subplot(411)
ax2 = fig.add_subplot(412)
ax3 = fig.add_subplot(413)
ax4 = fig.add_subplot(414)
da_ref_q_point.attrs = da_target_point.attrs
quantile_month_plot(
da_ref_q_point,
ax1,
general_cmap,
general_levels,
title='reference quantiles',
extend=extend,
)
da_hist_q_point.attrs = da_target_point.attrs
quantile_month_plot(
da_hist_q_point,
ax2,
general_cmap,
general_levels,
title='historical quantiles',
extend=extend,
)
da_target_q_point.attrs = da_target_point.attrs
quantile_month_plot(
da_target_q_point,
ax3,
general_cmap,
general_levels,
title='target quantiles',
extend=extend,
)
quantile_month_plot(
da_af_point,
ax4,
af_cmap,
af_levels,
title='adjustment factors'
)
plt.show()
def plot_pdfs_point(
da_hist_point, da_ref_point, da_target_point, da_qq_point, xbounds=None, ybounds=None, month=None
):
"""Plot PDFs for a single grid point"""
hist_point = da_hist_point.copy()
ref_point = da_ref_point.copy()
target_point = da_target_point.copy()
qq_point = da_qq_point.copy()
if month:
hist_point = hist_point[hist_point['time'].dt.month == month]
ref_point = ref_point[ref_point['time'].dt.month == month]
target_point = target_point[target_point['time'].dt.month == month]
qq_point = qq_point[qq_point['time'].dt.month == month]
fig = plt.figure(figsize=[15, 5])
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
bins = np.arange(-20, 150, 1)
ref_point.plot.hist(
ax=ax1,
bins=bins,
density=True,
label='reference',
facecolor='tab:green',
alpha=0.5,
rwidth=0.9,
)
hist_point.plot.hist(
ax=ax1,
bins=bins,
density=True,
label='histotical',
facecolor='tab:blue',
alpha=0.5,
rwidth=0.9,
)
ax1.set_ylabel('probability')
if xbounds:
ax1.set_xlim(xbounds[0], xbounds[1])
if ybounds:
ax1.set_ylim(ybounds[0], ybounds[1])
ax1.legend()
ax1.set_title('')
target_point.plot.hist(
ax=ax2,
bins=bins,
density=True,
label='target',
facecolor='tab:green',
alpha=0.5,
rwidth=0.9,
)
qq_point.plot.hist(
ax=ax2,
bins=bins,
density=True,
label='QQ-scaled data',
facecolor='tab:orange',
alpha=0.5,
rwidth=0.9,
)
ax2.set_ylabel('probability')
if xbounds:
ax2.set_xlim(xbounds[0], xbounds[1])
if ybounds:
ax2.set_ylim(ybounds[0], ybounds[1])
ax2.legend()
ax2.set_title('')
title = calendar.month_name[month] if month else 'all months'
plt.suptitle(title)
plt.show()
def plot_quantiles_1d_point(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
quantiles,
da_qq_cmatch_point=None,
xbounds=None,
month=None
):
"""Plot 1D quantiles comparisons"""
hist_point = da_hist_point.copy()
ref_point = da_ref_point.copy()
target_point = da_target_point.copy()
qq_point = da_qq_point.copy()
if da_qq_cmatch_point is not None:
qq_cmatch_point = da_qq_cmatch_point.copy()
if month:
hist_point = hist_point[hist_point['time'].dt.month == month]
ref_point = ref_point[ref_point['time'].dt.month == month]
target_point = target_point[target_point['time'].dt.month == month]
qq_point = qq_point[qq_point['time'].dt.month == month]
if da_qq_cmatch_point is not None:
qq_cmatch_point = qq_cmatch_point[qq_cmatch_point['time'].dt.month == month]
hist_q_point = utils.get_quantiles(hist_point, quantiles, timescale='annual')
ref_q_point = utils.get_quantiles(ref_point, quantiles, timescale='annual')
target_q_point = utils.get_quantiles(target_point, quantiles, timescale='annual')
qq_q_point = utils.get_quantiles(qq_point, quantiles, timescale='annual')
if da_qq_cmatch_point is not None:
qq_cmatch_q_point = utils.get_quantiles(qq_cmatch_point, quantiles, timescale='annual')
fig = plt.figure(figsize=[15, 5])
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
target_data = target_q_point.values
qq_data = qq_q_point.values
hist_data = hist_q_point.values
ref_data = ref_q_point.values
if da_qq_cmatch_point is not None:
qq_cmatch_data = qq_cmatch_q_point.values
width = 80 / len(hist_q_point)
ax1.bar(quantiles * 100, hist_data, alpha=0.5, width=width, label='historical')
ax1.bar(quantiles * 100, ref_data, alpha=0.5, width=width, label='reference')
ax2.bar(quantiles * 100, target_data, alpha=0.5, width=width, label='target')
ax2.bar(quantiles * 100, qq_data, alpha=0.5, width=width, label='qq-scaled data')
if da_qq_cmatch_point is not None:
ax2.bar(quantiles * 100, qq_cmatch_data, alpha=0.5, width=width, label='qq-scaled cmatch data')
ylabel = f"""{da_target_point.attrs['long_name']} ({da_target_point.attrs['units']})"""
data_min = np.min(np.concatenate([target_data, qq_data, hist_data, ref_data]))
ymin = data_min - 1 if (data_min < 0) else 0
ymax = np.max(np.concatenate([target_data, qq_data, hist_data, ref_data])) + 1
if xbounds:
ax1.set_xlim(xbounds[0], xbounds[1])
ax1.set_ylim(ymin, ymax)
ax1.grid()
ax1.legend()
ax1.set_ylabel(ylabel)
ax1.set_xlabel('quantile')
if xbounds:
ax2.set_xlim(xbounds[0], xbounds[1])
ax2.set_ylim(ymin, ymax)
ax2.grid()
ax2.legend()
ax2.set_xlabel('quantile')
title = calendar.month_name[month] if month else 'all months'
plt.suptitle(f'quantiles - {title}')
plt.show()
def plot_values_1d_point(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
scaling,
n_values=50,
extremes='max',
month=None
):
"""Plot 1D values comparisons."""
assert extremes in ['min', 'max']
hist_point = da_hist_point.copy()
ref_point = da_ref_point.copy()
target_point = da_target_point.copy()
qq_point = da_qq_point.copy()
assert len(target_point) == len(qq_point), "1D values plot only works if length of target and qq are the same"
qq_point['time'] = target_point['time']
if month:
hist_point = hist_point[hist_point['time'].dt.month == month]
ref_point = ref_point[ref_point['time'].dt.month == month]
target_point = target_point[target_point['time'].dt.month == month]
qq_point = qq_point[qq_point['time'].dt.month == month]
hist_point_sorted = np.sort(hist_point.values)
ref_point_sorted = np.sort(ref_point.values)
target_args = np.argsort(target_point.values)
target_point_sorted = target_point[target_args].values
qq_point_sorted = qq_point[target_args].values
if scaling == 'multiplicative':
adjustments = qq_point_sorted / target_point_sorted
else:
adjustments = qq_point_sorted - target_point_sorted
fig = plt.figure(figsize=[15, 5])
ax1a = fig.add_subplot(121)
ax2a = fig.add_subplot(122)
ylabel = f"""{da_target_point.attrs['long_name']} ({da_target_point.attrs['units']})"""
xvals1 = np.arange(len(target_point)) + 1
xvals2 = np.arange(len(target_point_sorted)) + 1
if extremes == 'max':
plot_xvals1 = xvals1[-n_values:]
plot_xvals2 = xvals2[-n_values:]
plot_hist = hist_point_sorted[-n_values:]
plot_ref = ref_point_sorted[-n_values:]
plot_adjustments = adjustments[-n_values:]
plot_target = target_point_sorted[-n_values:]
plot_qq = qq_point_sorted[-n_values:]
elif extremes == 'min':
plot_xvals1 = xvals1[0:n_values]
plot_xvals2 = xvals2[0:n_values]
plot_hist = hist_point_sorted[0:n_values]
plot_ref = ref_point_sorted[0:n_values]
plot_adjustments = adjustments[0:n_values]
plot_target = target_point_sorted[0:n_values]
plot_qq = qq_point_sorted[0:n_values]
all_data = np.concatenate([plot_hist, plot_ref, plot_target, plot_qq])
ymax = np.max(all_data) + 5
ax1a.stem(plot_xvals1, plot_hist, label='hist', markerfmt='bo', linefmt='--')
ax1a.stem(plot_xvals1, plot_ref, label='ref', markerfmt='go', linefmt='--')
ax1a.set_ylabel(ylabel)
ax1a.set_xlabel('rank')
ax1a.set_ylim([-5, ymax])
ax1a.legend(loc='center left')
ax1a.grid()
ax1b = ax1a.twinx()
ax1b.plot(plot_xvals1, plot_adjustments, color='tab:grey', linestyle=':', label='adjustment_factor')
ax1b.set_ylabel('adjustment factor')
ax1b.legend(loc='upper left')
ax2a.stem(plot_xvals2, plot_target, label='target', markerfmt='bo', linefmt='--')
ax2a.stem(plot_xvals2, plot_qq, label='qq', markerfmt='go', linefmt='--')
ax2a.set_xlabel('rank (target)')
ax2a.set_ylim([-5, ymax])
ax2a.legend(loc='center left')
ax2a.grid()
ax2b = ax2a.twinx()
ax2b.plot(plot_xvals2, plot_adjustments, color='tab:grey', linestyle=':', label='adjustment_factor')
ax2b.set_ylabel('adjustment factor')
ax2b.legend(loc='upper left')
title = calendar.month_name[month] if month else 'all months'
plt.suptitle(f'highest values - {title}')
plt.show()
def plot_seasonal_change(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
scaling,
):
"""Plot seasonal change"""
hist_monthly_clim = da_hist_point.groupby('time.month').mean('time')
ref_monthly_clim = da_ref_point.groupby('time.month').mean('time')
target_monthly_clim = da_target_point.groupby('time.month').mean('time')
qq_monthly_clim = da_qq_point.groupby('time.month').mean('time')
if scaling == 'additive':
model_change = ref_monthly_clim - hist_monthly_clim
qq_change = qq_monthly_clim - target_monthly_clim
units = da_hist_point.attrs['units']
elif scaling == 'multiplicative':
model_change = ((ref_monthly_clim - hist_monthly_clim) / hist_monthly_clim) * 100
qq_change = ((qq_monthly_clim - target_monthly_clim) / target_monthly_clim) * 100
units = '%'
xticks = np.arange(1, 13)
xtick_labels = [calendar.month_abbr[i] for i in xticks]
fig = plt.figure(figsize=[15, 10])
ax = fig.add_subplot(111)
ax.bar(xticks, qq_change, alpha=0.5, label='qq data')
ax.bar(xticks, model_change, alpha=0.5, label='model data')
ax.set_title('Monthly change')
ax.legend()
ax.set_ylabel(f'Change ({units})')
ax.set_xticks(xticks, xtick_labels)
ax.grid()
plt.show()
def plot_seasonal_cycle(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
):
"""Plot seasonal cycle"""
point_data = {}
point_data['hist'] = da_hist_point.copy()
point_data['ref'] = da_ref_point.copy()
point_data['target'] = da_target_point.copy()
point_data['qq'] = da_qq_point.copy()
xticks = np.arange(1, 13)
xtick_labels = [calendar.month_abbr[i] for i in xticks]
fig = plt.figure(figsize=[15, 10])
for label in ['hist', 'ref', 'target', 'qq']:
da_point = point_data[label]
monthly_means = da_point.groupby('time.month').mean('time')
plt.plot(xticks, monthly_means, label=label, linestyle=linestyles[label], marker='o')
plt.title('Monthly climatology')
plt.legend()
units = da_hist_point.attrs['units']
plt.ylabel(f'monthly mean ({units})')
plt.xticks(xticks, xtick_labels)
plt.grid()
plt.show()
def plot_seasonal_totals(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
da_qq_cmatch_point=None,
time_agg='mean',
):
"""Plot rainfall seasonal cycle"""
labels = ['hist', 'ref', 'target', 'qq']
point_data = {}
point_data['hist'] = da_hist_point.copy()
point_data['ref'] = da_ref_point.copy()
point_data['target'] = da_target_point.copy()
point_data['qq'] = da_qq_point.copy()
if da_qq_cmatch_point is not None:
point_data['qq-cmatch'] = da_qq_cmatch_point.copy()
labels.append('qq-cmatch')
xticks = np.arange(1, 13)
xtick_labels = [calendar.month_abbr[i] for i in xticks]
fig = plt.figure(figsize=[15, 10])
for label in labels:
da_point = point_data[label]
if time_agg == 'pct_total':
annual_total = da_point.data.sum()
monthly_totals = da_point.groupby('time.month').sum('time')
monthly_values = (monthly_totals.values / annual_total) * 100
ylabel = '% of annual total'
elif time_agg == 'mean':
monthly_values = da_point.groupby('time.month').mean('time')
ylabel = da_hist_point.attrs['units']
plt.plot(xticks, monthly_values, label=label, linestyle=linestyles[label], marker='o')
plt.title('Climatology')
plt.legend()
plt.ylabel(ylabel)
plt.xticks(xticks, xtick_labels)
plt.grid()
plt.show()
def plot_clipped(da_qq_point, da_qq_clipped_point):
"""Plot first year of clipped data"""
fig = plt.figure(figsize=[15, 10])
da_qq_point[0:365].plot(color='tab:orange', label='QQ data')
da_qq_clipped_point[0:365].plot(color='tab:blue', label='QQ clipped data')
plt.title('Clipping check - first year of daily data')
plt.legend()
plt.grid()
plt.show()
def single_point_analysis(
da_hist,
da_ref,
da_target,
da_qq,
ds_adjust,
variable,
scaling,
city,
lat,
lon,
general_cmap,
af_cmap,
general_levels,
af_levels,
da_qq_clipped=None,
da_qq_cmatch=None,
pdf_xbounds=None,
pdf_ybounds=None,
q_xbounds=None,
n_values=50,
months=[],
seasonal_agg='mean',
extreme_for_values='max',
plot_1d_quantiles=True,
plot_1d_values=True,
plot_pdfs=True,
):
"""Plots for a single grid point"""
point_selection = {'lat': lat, 'lon': lon}
da_hist_point = da_hist.sel(point_selection, method='nearest').compute()
da_ref_point = da_ref.sel(point_selection, method='nearest').compute()
da_target_point = da_target.sel(point_selection, method='nearest').compute()
da_qq_point = da_qq.sel(point_selection, method='nearest').compute()
if da_qq_clipped is not None:
da_qq_clipped_point = da_qq_clipped.sel(point_selection, method='nearest').compute()
if da_qq_cmatch is not None:
da_qq_cmatch_point = da_qq_cmatch.sel(point_selection, method='nearest').compute()
else:
da_qq_cmatch_point = None
ds_adjust_point = ds_adjust.sel(point_selection, method='nearest').compute()
quantiles = ds_adjust['quantiles'].data
print(city.upper())
plot_quantiles_2d_point(
ds_adjust_point['hist_q'],
da_ref_point,
da_target_point,
ds_adjust_point['af'],
variable,
quantiles,
general_cmap,
af_cmap,
general_levels,
af_levels,
)
if 'pr' in variable:
plot_seasonal_totals(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
da_qq_cmatch_point=da_qq_cmatch_point,
)
else:
plot_seasonal_cycle(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
)
plot_seasonal_change(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
scaling,
)
if plot_1d_quantiles:
plot_quantiles_1d_point(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
quantiles,
da_qq_cmatch_point=da_qq_cmatch_point,
xbounds=q_xbounds
)
for month in months:
plot_quantiles_1d_point(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
quantiles,
da_qq_cmatch_point=da_qq_cmatch_point,
month=month,
xbounds=q_xbounds,
)
if plot_1d_values:
plot_values_1d_point(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
scaling,
extremes=extreme_for_values,
n_values=n_values
)
for month in months:
plot_values_1d_point(
da_hist_point,
da_ref_point,
da_target_point,
da_qq_point,
scaling,
month=month,