/
aux_plotting.ncl
1896 lines (1688 loc) · 49.3 KB
/
aux_plotting.ncl
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
; #############################################################################
; AUXILIARY PLOTTING ROUTINES
; #############################################################################
; Please consider using or extending existing routines before adding new ones.
; Check the header of each routine for documentation.
;
; Contents:
;
; function output_type
; procedure create_legend_lines
; procedure copy_VarAtt_sel
; function panelling
; function get_plot_dir
; function get_outfile_name
; function get_wks
; procedure add_markers
; procedure add_num_markers
; procedure add_errorbar
; procedure horizontal_whiskers
; procedure add_prediction_error
; function month_sel
; function lat_names
; procedure add_line
; procedure add_scatt
; procedure add_legend
; function calcRegCoeffs
; function genZonalMeans
; function calcMeanAnnCycleMonthly
; function calcMeanAnnCycleAnnual
; procedure rmMeanAnnCycle
; function apfiltersmooth
; procedure smoothAnomalies
; function clmMon2clmDayn
;
; #############################################################################
load "$diag_scripts/../interface_scripts/auxiliary.ncl"
load "$diag_scripts/../interface_scripts/logging.ncl"
; #############################################################################
undef("output_type")
function output_type()
;
; Arguments
;
; Return value
; A string with the output file type
;
; Description
; Provides a default, if file type is not explicitly specified
;
; Caveats
;
; Modification history
; 20131028-gottschaldt_klaus-dirk: written.
;
local funcname, scriptname, file_type
begin
funcname = "output_type"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
file_type = config_user_info@output_file_type
if (ismissing(file_type)) then
file_type = "ps"
end if
file_type = str_lower(file_type)
leave_msg(scriptname, funcname)
return(file_type)
end
; #############################################################################
undef("create_legend_lines")
procedure create_legend_lines(labels:string, \
styles, \
outfile:string,\
opt)
;
; Arguments
; labels: labels of the lines
; styles: style of the lines in the plot
; @colors
; @dashes -> optional (line dashes
; @thicks -> optional (line/marker thickness)
; @markers -> optional (marker index)
; @sizes -> optional (marker size)
; outfile: outfile directory
; opt: "lines" or "markers" legend
;
; Description
; Draws an extra plot with a legend, specified by labels and styles.
;
; Caveats
;
; Modification history
; 20150511-lauer_axel: added safe lower limits for panelling plot
; 20150508-righi_mattia: added lines/markers option.
; 20150120-gottschaldt_klaus-dirk: remove pre-existing file type suffix
; 20140305-righi_mattia: modified to plot always as epsi format.
; 20140219-winterstein_franziska: written.
;
local funcname, scriptname, region, temp, outfile, n_suff, wks_legend
begin
funcname = "create_legend_lines"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Open workstation
file_type = output_type()
wks_legend = gsn_open_wks(file_type, outfile)
; General resources
res = True
res@tmBorderThicknessF = 0.
res@tmBorderLineColor = "white"
res@tmXTOn = False
res@tmXBOn = False
res@tmYLOn = False
res@tmYROn = False
res@trXMinF = 0
res@trXMaxF = 1
res@trYMinF = 0
res@trYMaxF = 1
res@vpXF = 0.0
res@vpYF = 1.0
; Resources for polyline and text
resL = True
resT = True
resT@txFontHeightF = 0.013
resT@txJust = "CenterLeft"
; Set number of lines and labels
dim_LAB = dimsizes(labels)
; Rows and columns
deltax = 0.06
deltay = 0.03
nrowmax = 32
ncols = 3
res@vpWidthF = 0.8
if (dim_LAB .gt. nrowmax * ncols) then
ncols = 4
res@vpWidthF = 1.0
if (dim_LAB .gt. nrowmax * ncols) then
error_msg("f", "xy_line.ncl", funcname, "too many datasets")
end if
end if
nrows = toint(dim_LAB/tofloat(ncols))
line_xpos = new((/ncols, 2/), float)
do jj = 0, ncols - 1
line_xpos(jj, 0) = 0.01 + jj / tofloat(ncols)
end do
line_xpos(:, 1) = line_xpos(:, 0) + deltax
text_pos = line_xpos(:, 1) + 0.01
res@vpHeightF = 0.04 + deltay * (nrows - 1)
; Draw blank plot
blank = gsn_blank_plot(wks_legend, res)
; Loop over labels, draw lines and text
ypos = 0.98
ii = 0
do while (ii.lt.dim_LAB)
; Set color
resL@gsLineColor = styles@colors(ii)
resL@gsMarkerColor = styles@colors(ii)
; Set dash pattern
if (isatt(styles, "dashes")) then
resL@gsLineDashPattern = styles@dashes(ii)
end if
; Set thickness
if (isatt(styles, "thicks")) then
resL@gsLineThicknessF = styles@thicks(ii)
resL@gsMarkerThicknessF = styles@thicks(ii)
end if
; Set marker index
if (isatt(styles, "markers")) then
resL@gsMarkerIndex = styles@markers(ii)
end if
; Set marker size
if (isatt(styles, "sizes")) then
resL@gsMarkerSizeF = styles@sizes(ii)
end if
; Draw legend
if (opt.eq."lines") then
gsn_polyline_ndc(wks_legend, line_xpos(mod(ii, ncols), :), \
(/ypos, ypos/), resL)
gsn_text_ndc(wks_legend, labels(ii), text_pos(mod(ii, ncols)), \
ypos, resT)
else if (opt.eq."markers") then
gsn_polymarker_ndc(wks_legend, avg(line_xpos(mod(ii, ncols), :)), \
ypos, resL)
gsn_text_ndc(wks_legend, labels(ii), text_pos(mod(ii, ncols)) - 0.02, \
ypos, resT)
else
error_msg("f", "xy_line.ncl", funcname, "unrecognized option " + opt)
end if
end if
if (mod(ii, ncols).eq.ncols - 1) then
ypos = ypos - deltay ; go to the next row
end if
ii = ii + 1
end do
draw(blank)
frame(wks_legend)
leave_msg(scriptname, funcname)
end
; #############################################################################
undef("copy_VarAtt_sel")
procedure copy_VarAtt_sel(var1, var2, sel: string)
;
; Arguments
; var1: variable of any type and dimension with source attributes
; var2: variable of any type and dimension, shall receive selected
; attributes from var1
; sel: string (or list of strings) that specify the BEGINNING letters
; of the attributes to copy
;
; Description
; Intended to copy selected plot ressources for use in a different
; plot routine that may not allow all ressources from var1.
; Written for function legend_lines (legends.ncl).
;
; Caveats:
;
; Modification history
; 20141227-gottschaldt_klaus-dirk written.
;
local funcname, scriptname, atts, i1, i2, n
begin
funcname = "copy_VarAtt_sel"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Check if var1 has attributes at all
atts = getvaratts(var1)
if (all(atts.eq."missing")) then
error_msg("w", scriptname, funcname, "no attributes to copy")
return(var2) ; return var2 unchanged
end if
; Copy selected attributes from var1
; Existing attributes of the same name will be overwritten in var2.
do i2 = 0, dimsizes(sel) - 1
n = strlen(sel(i2))
do i1 = 0, dimsizes(atts) - 1
if (str_get_cols(atts(i1), 0, n - 1) .eq. sel(i2)) then
if (isatt(var2, atts(i1))) then
delete(var2@$atts(i1)$)
end if
var2@$atts(i1)$ = var1@$atts(i1)$
end if
end do
end do
leave_msg(scriptname, funcname)
end
; #############################################################################
undef("panelling")
function panelling(wks,
plots:graphic,
nvert[1]:integer,
nhori[1]:integer,
pres_in[1]:logical)
;
; Arguments
; wks: graphics object associated with a file.
; plots: graphics variable containing one or more plots.
; nvert: Maximum allowed number of plots per page (vertical).
; nhori: Maximum allowed number of plots per page (horizontal).
; pres_in: if it is a logical variable, attributes are used to override
; panelling defaults
;
; Return value
; A string with the output file name
;
; Description
; Writes a graphics file
;
; Caveats
; Might not be fit to be used with non-NCL routines.
;
; Modification history
; 20131112-gottschaldt_klaus-dirk: written.
;
local funcname, scriptname, plots, info, wks, outfile
begin
funcname = "panelling"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Check if valid wks has been provided
if (typeof(wks).ne."graphic") then
error_msg("f", scriptname, funcname, "panelling of " + \
"multiple plots needs valid wks")
end if
; Get outfile name
if (isatt(wks, "fullname")) then
outfile = wks@fullname
else
outfile = wks@name
error_msg("w", scriptname, funcname, "wks@fullname " + \
"missing, consider to use get_wks to open wks")
end if
; Panel parameters
npages = toint(ceil(tofloat(dimsizes(plots)) / tofloat(nvert * nhori)))
; Draw the panel
pres = True
pres@gsnMaximize = True
pres@gsnPanelYWhiteSpacePercent = 5
pres@gsnPanelXWhiteSpacePercent = 5
if (isatt(diag_script_info, "PanelTop")) then
top = tofloat(diag_script_info@PanelTop)
else
top = 0.99 ; default
end if
pres@gsnPanelTop = top
if (npages .gt. 1) then
pres@gsnPaperOrientation = "portrait"
if (typeof(pres_in).eq."logical") then ; Override defaults
copy_VarMeta(pres_in, pres)
end if
ia = 0
ie = nvert * nhori - 1
ie = min((/dimsizes(plots) - 1, ie/))
do ipage = 0, npages - 1
gsn_panel(wks, plots(ia:ie), (/nvert, nhori/), pres)
ia = ie + 1
ie = ia + (nvert * nhori) - 1
ie = min((/dimsizes(plots) - 1, ie/))
end do
else
pres@gsnPaperOrientation = "auto"
if (typeof(pres_in).eq."logical") then ; Override defaults
copy_VarMeta(pres_in, pres)
end if
nrow = toint(ceil(tofloat(dimsizes(plots)) / tofloat(nhori)))
ncol = min((/nhori, dimsizes(plots)/))
gsn_panel(wks, plots, (/nrow, ncol/), pres)
end if
leave_msg(scriptname, funcname)
return(outfile)
end
; #############################################################################
undef("get_plot_dir")
function get_plot_dir()
;
; Arguments
;
; Return value
; A string root path for plots
;
; Description
; Provides a default, if plot_dir is not explicitely specified
;
; Caveats
;
; Modification history
; 20131104-gottschaldt_klaus-dirk: written.
;
local funcname, scriptname, plot_dir
begin
funcname = "get_plot_dir"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
plot_dir = config_user_info@plot_dir
if (ismissing(plot_dir)) then
plot_dir = "./plots/"
end if
if (.not. fileexists(plot_dir)) then
system("mkdir -p " + plot_dir)
end if
leave_msg(scriptname, funcname)
return(plot_dir)
end
; #############################################################################
undef("get_outfile_name")
function get_outfile_name(add_specs[1]:string)
;
; Arguments
; add_specs: string containing specific elements to be added to the file
; name if not of type string, only variable name is used.
;
; Return value
; Complete outfile name incl. path, additional specifications and file type
; plot_dir + add_specs + file_type
;
; Description
; Fetches file_type, plot_dir
; Fetches string with additional elements specified within diag script
;
; Modification history
; 20131204-senftleben_daniel: generalized naming.
; 20131104-gottschaldt_klaus-dirk: written.
;
local funcname, scriptname, outfile, file_type, plot_dir, diag_script_base, \
add_specs, output_dir
begin
funcname = "get_outfile_name"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Collect parts of outfile name
file_type = output_type()
plot_dir = get_plot_dir()
; Assemble final name
if (typeof(add_specs).ne."string") then
outfile = plot_dir + var + "." + file_type
else
outfile = plot_dir + add_specs + "." + file_type
end if
leave_msg(scriptname, funcname)
return(outfile)
end
; #############################################################################
undef("get_wks")
function get_wks(wks_in,
diag_script[1]: string,
add_specs[1]: string)
;
; Arguments
; wks_in: dummy or graphic object
; diag_script: name of diagnostic script
; add_specs: own specificactions to be added to file name, i.e. variable
; name, etc. - needed for function get_outfile_name
;
; Return value
; wks: graphic object
;
; Description
; Provides a default wks, if wks_in is not of type "graphic".
; Attribute wks@fullname is used to transfer the output file name, since
; wks@name cuts off the path to the file name.
;
; Caveats
;
; Modification history
; 20131113-gottschaldt_klaus-dirk: written.
;
local funcname, scriptname, wks_in, diag_script, add_specs, file_type
begin
funcname = "get_wks"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Check if a valid wks has been provided, otherwise invoke default
if (typeof(wks_in).ne."graphic") then ; Use defaults
file_type = output_type()
copy_VarMeta(wks_in, file_type)
outfile = get_outfile_name(add_specs)
wks = gsn_open_wks(file_type, outfile)
wks@fullname = outfile
else
wks = wks_in
copy_VarMeta(wks_in, wks)
end if
leave_msg(scriptname, funcname)
return(wks)
end
; #############################################################################
undef("add_markers")
procedure add_markers(wks[1]:graphic,
plot[1]:graphic,
res_in[1]:logical,
xpos_in:numeric,
ypos_in:numeric)
;
; Arguments
; wks: valid workstation, e.g. created by get_wks
; plot: plot identifier, e.g. created by gsn_*
; res_in: plot ressources that may override local function defaults
; xpos_in: horizontal marker position(s)
; ypos_in: vertical marker position(s)
;
; Return value
; Attaches polyline IDs as attributes to plot.
;
; Description:
; Adds markers to an existing plot.
;
; Caveats:
;
; Modification history
; 20140224-gottschaldt_klaus-dirk: written for use with profile_plev.ncl
; in Emmons.ncl
;
local funcname, scriptname, res, str, atts, iatt, color
begin
funcname = "add_markers"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Set default plot ressources
res = True
res@gsMarkerSizeF = 0.006
res@gsMarkerColor = "black"
res@gsMarkerIndex = 16 ; solid circle
; Override defaults (after cleaning up for possible vector input)
if (res_in) then
atts = getvaratts(res_in)
if (.not.all(ismissing(atts))) then
do iatt = 0, dimsizes(atts) - 1
if (isatt(res, atts(iatt))) then
delete(res@$atts(iatt)$)
end if
end do
end if
copy_VarMeta(res_in, res)
end if
; Plot symbol rim (slightly bigger, white polymarker background)
color = res@gsMarkerColor ; remember original color
res@gsMarkerSizeF = res@gsMarkerSizeF + res@gsMarkerSizeF * 0.2
res@gsMarkerColor(:) = "white"
str = unique_string("polymarker")
plot@$str$ = gsn_add_polymarker(wks, plot, xpos_in, ypos_in, res)
; Plot symbols
res@gsMarkerColor(:) = color ; restore original color
res@gsMarkerSizeF = res@gsMarkerSizeF - res@gsMarkerSizeF * 0.2
str = unique_string("polymarker")
plot@$str$ = gsn_add_polymarker(wks, plot, xpos_in, ypos_in, res)
leave_msg(scriptname, funcname)
end
; #############################################################################
undef("add_num_markers")
procedure add_num_markers(wks[1]:graphic,
plot[1]:graphic,
res_in[1]:logical,
xpos_in:numeric,
ypos_in:numeric)
;
; Arguments
; wks: valid workstation, e.g. created by get_wks
; plot: plot identifier, e.g. created by gsn_*
; res_in: plot ressources that may override local function defaults
; xpos_in: horizontal marker position(s), needs year as a coordinate
; ypos_in: vertical marker position(s)
;
; Return value
; Attaches polytext IDs as attributes to plot.
;
; Description:
; Adds markers to an existing plot, being the last two intagers of a year.
; If a horizontal (vertical) coordinate
; has only one element, then this position is used for all markers.
;
; Caveats:
;
; Modification history
; 20150914-wenzel_sabrina: written.
;
local funcname, scriptname, xpos, ypos, res, str, atts, iatt, color
begin
funcname = "add_num_markers"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; set default plot ressources
res = True
res@txFontHeightF = 0.025
res@txFontColor = "black"
; override defaults (after cleaning up for possible vector input)
if (res_in) then
atts = getvaratts(res_in)
if (.not.all(ismissing(atts))) then
do iatt = 0, dimsizes(atts) - 1
if (isatt(res, atts(iatt))) then
delete(res@$atts(iatt)$)
end if
end do
end if
copy_VarMeta(res_in, res)
end if
atext = new(dimsizes(xpos_in), integer)
do y = 0, dimsizes(xpos_in) - 1
strs = str_split_by_length(tostring(xpos_in&year(y)), 1)
strs_y = toint(strs(0) + strs(1) + "00")
atext(y) = xpos_in&year(y) - strs_y
delete([/strs, strs_y/])
end do
; create vector, if horizontal or vertical position is given as scalar
if (dimsizes(xpos_in).eq.1) then
xpos = new(dimsizes(ypos_in), typeof(xpos_in))
xpos = (/xpos_in/)
else
xpos = (/xpos_in/)
end if
if (dimsizes(ypos_in).eq.1) then
ypos = new(dimsizes(xpos_in), typeof(ypos_in))
ypos = (/ypos_in/)
else
ypos = (/ypos_in/)
end if
; plot symbol
str = unique_string("polymarker")
plot@$str$ = \
gsn_add_text(wks, plot, sprinti("%-0.2hi", atext), xpos, ypos, res)
leave_msg(scriptname, funcname)
end
; #############################################################################
undef("add_errorbar")
procedure add_errorbar(wks[1]:graphic,
plot[1]:graphic,
res_in[1]:logical,
xpos_in:numeric,
ypos_in:numeric)
;
; Arguments
; wks: valid workstation, e.g. created by get_wks
; plot: plot identifier, e.g. created by gsn_*
; res_in: plot ressources that may override local function defaults
; xpos_in: 2D array with horizontal marker position(s) and stddev's,
; ypos_in: 2D array with vertical marker position(s) and stddev's
;
; Return value
; Attaches polytext IDs as attributes to plot.
;
; Description:
; Adds error bars to an existing plot. If a horizontal (vertical)
; coordinate has only one element, then this position is used for all
; markers.
;
; Caveats:
;
; Modification history
; 20150914-wenzel_sabrina: written.
;
local funcname, scriptname, xpos, ypos, res, str, atts, iatt, color
begin
funcname = "add_errorbar"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Set default plot ressources
res = True
res@gsLineThicknessF = 2
colors = new(dimsizes(xpos_in(0, :)), string)
colors = "black"
; Override defaults (after cleaning up for possible vector input)
if (res_in) then
atts = getvaratts(res_in)
if (.not.all(ismissing(atts))) then
do iatt = 0, dimsizes(atts) - 1
if (isatt(res, atts(iatt))) then
delete(res@$atts(iatt)$)
end if
end do
end if
copy_VarMeta(res_in, res)
if (isatt(res, "gsLineColor")) then
colors = res@gsLineColor
delete(res@gsLineColor)
end if
end if
; Create vector, if horizontal or vertical position is given as scalar
if (dimsizes(xpos_in(0, :)).eq.1) then
xpos = new(dimsizes(ypos_in), typeof(xpos_in))
xpos(:, :) = xpos_in
else
xpos = xpos_in
end if
if (dimsizes(ypos_in(0, :)).eq.1) then
ypos = new(dimsizes(xpos_in), typeof(ypos_in))
ypos(:, :) = ypos_in
else
ypos = ypos_in
end if
Xend = 0.01 * ((max(ypos(0, :)) + 0.2 * (abs(max(ypos(0, :))))) - \
(min(ypos(0, :)) - 0.2 * (abs(min(ypos(0, :))))))
Yend = 0.01 * ((max(xpos(0, :)) + 0.2 * (abs(avg(xpos(0, :))))) - \
(min(xpos(0, :)) - 0.2 * (abs(avg(xpos(0, :))))))
do i = 0, dimsizes(xpos(0, :)) - 1
res@gsLineColor = colors(i)
if (.not.any(ismissing((/xpos(0, i), ypos(0, i)/))))
Xbarstr = unique_string("Xbarerror")
Ybarstr = unique_string("Ybarerror")
Xendstr = unique_string("Xenderror")
Yendstr = unique_string("Yenderror")
plot@$Xbarstr$ = \
gsn_add_polyline(wks, plot, \
(/xpos(0, i) + xpos(1, i), \
xpos(0, i) - xpos(1, i)/), \
(/ypos(0, i), ypos(0, i)/), res)
plot@$Ybarstr$ = \
gsn_add_polyline(wks, plot, \
(/xpos(0, i), xpos(0, i)/), \
(/ypos(0, i) + ypos(1, i), \
ypos(0, i) - ypos(1, i)/), res)
plot@$Xendstr$ = \
gsn_add_polyline(wks, plot, \
(/xpos(0, i) + xpos(1, i), \
xpos(0, i) + xpos(1, i), \
xpos(0, i) + xpos(1, i), \
xpos(0, i) - xpos(1, i), \
xpos(0, i) - xpos(1, i), \
xpos(0, i) - xpos(1, i)/), \
(/ypos(0, i) - Xend, \
ypos(0, i) + Xend, \
ypos(0, i), ypos(0, i), \
ypos(0, i) - Xend, \
ypos(0, i) + Xend/), res)
plot@$Yendstr$ = \
gsn_add_polyline(wks, plot, \
(/xpos(0, i) - Yend, \
xpos(0, i) + Yend, \
xpos(0, i), \
xpos(0, i), \
xpos(0, i) - Yend, \
xpos(0, i) + Yend/), \
(/ypos(0, i) + ypos(1, i), \
ypos(0, i) + ypos(1, i), \
ypos(0, i) + ypos(1, i), \
ypos(0, i) - ypos(1, i), \
ypos(0, i) - ypos(1, i), \
ypos(0, i) - ypos(1, i) /), res)
end if
end do
leave_msg(scriptname, funcname)
end
; #############################################################################
undef("horizontal_whiskers")
procedure horizontal_whiskers(wks[1]:graphic,
plot[1]:graphic,
res_in[1]:logical,
xmin_in:numeric,
xmax_in:numeric,
ypos_in: numeric)
;
; Arguments
; wks: valid workstation, e.g. created by get_wks.
; plot: plot identifier, e.g. created by gsn_*.
; res_in: plot ressources that may override local function defaults.
; xmin_in: vector of whiskers' left ends (same size as xmax & y).
; xmax_in: vector of whiskers' right ends (same size as xmin & y).
; ypos_in: vector of whiskers' vertical positions (must have same size as
; xmax & xmin).
;
; Return value
; Attaches polyline IDs as attributes to plot.
;
; Description
; Creates vectors suitable as input for gsn_add_polyline:
; x = (/xmin1,xmax1,_FillValue,xmin2,xmax2,_FillValue, .../)
; y = (/ypos1,ypos1,_FillValue,ypos2,ypos2,_FillValue, .../)
; The separation by _FillValue results in individual whiskers.
; No whisker is created where xmin, xmax or ypos is missing.
;
; Caveats
;
; References
; www.ncl.ucar.edu/Document/Graphics/Interfaces/gsn_add_polyline.shtml
;
; Modification history
; 20140224-gottschaldt_klaus-dirk: written.
;
local funcname, scriptname, n, i, j, xmin, xmax, ypos, imiss, ivalid, result, \
x, y, res, str
begin
funcname = "horizontal_whiskers"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Check sizes of input vectors
n = dimsizes(ypos_in)
if (dimsizes(xmin_in).ne.n .or. dimsizes(xmax_in).ne.n) then
error_msg("f", scriptname, funcname, "all input vectors " + \
"must have same size")
end if
; Unify _FillValue and type
ypos = tofloat(ypos_in) ; #####################
copy_VarMeta(ypos_in, ypos)
ypos@_FillValue = default_fillvalue("float")
ypos = where(ismissing(ypos_in), ypos@_FillValue, ypos)
xmin = tofloat(xmin_in) ; #####################
copy_VarMeta(xmin_in, xmin)
xmin@_FillValue = default_fillvalue("float")
xmin = where(ismissing(xmin_in), xmin@_FillValue, xmin)
xmax = tofloat(xmax_in) ; #####################
copy_VarMeta(xmax_in, xmax)
xmax@_FillValue = default_fillvalue("float")
xmax = where(ismissing(xmax_in), xmax@_FillValue, xmax)
; Find positions where no input element is missing
imiss = ispan(0, n - 1, 1)
imiss@_FillValue = default_fillvalue("integer")
imiss = where(ismissing(ypos), imiss@_FillValue, imiss)
imiss = where(ismissing(xmin), imiss@_FillValue, imiss)
imiss = where(ismissing(xmax), imiss@_FillValue, imiss)
ivalid = ind(.not. ismissing(imiss))
if (all(ismissing(ivalid))) then
log_debug(" No valid data to plot whiskers: Skipping")
else
; Create vectors suited for gsn_add_polyline
x = new(3 * dimsizes(ivalid) - 1, typeof(xmin))
x@_FillValue = xmin@_FillValue
x(:) = x@_FillValue
y = new(3 * dimsizes(ivalid) - 1, typeof(ypos))
y@_FillValue = xmax@_FillValue
y(:) = y@_FillValue
do i = 0, dimsizes(ivalid) - 1
j = ivalid(i)
x(i * 3) = xmin(j)
x(i * 3 + 1) = xmax(j)
y(i * 3) = ypos(j)
y(i * 3 + 1) = ypos(j)
end do
; Resources
res = True
if (res_in) then
copy_VarMeta(res_in, res)
end if
; plot whiskers
; (see Example 2 of gsn_add_polyline help page):
; Attaching the return value of gsn_add_polyline
; to the plot variable ensures that this
; reference can live outside this script
str = unique_string("polyline")
plot@$str$ = gsn_add_polyline(wks, plot, x, y, res)
end if
leave_msg(scriptname, funcname)
end
; #############################################################################
undef("add_prediction_error")
procedure add_prediction_error(wks[1]:graphic,
plot[1]:graphic,
res_in[1]:logical,
xpos_in:numeric,
ypos_in:numeric)
;
; Arguments
; wks: valid workstation, e.g. created by get_wks
; plot: plot identifier, e.g. created by gsn_*
; res_in: plot ressources that may override local function defaults
; xpos_in: array defining the x-position of the four corners
; ypos_in: vertical marker position(s)
;
; Return value
; Attaches polytext IDs as attributes to plot.
;
; Description:
; Adds markers to an existing plot, being the last two intagers of a year.
; If a horizontal (vertical) coordinate
; has only one element, then this position is used for all markers.
;
; Caveats:
;
; Modification history
; 20150914-wenzel_sabrina: written.
;
local funcname, scriptname, xpos, ypos, res, str, atts, iatt, color
begin
funcname = "add_prediction_error"
scriptname = "diag_scripts/shared/plot/aux_plotting.ncl"
enter_msg(scriptname, funcname)
; Set default plot ressources
res = True
; Create a plot with filled area between both curves
res@xyLineColor = "grey85"
res@gsFillColor = "grey85"
res@gsLineDashPattern = 0
; Override defaults (after cleaning up for possible vector input)
if (res_in) then
atts = getvaratts(res_in)
if (.not.all(ismissing(atts))) then
do iatt = 0, dimsizes(atts) - 1
if (isatt(res, atts(iatt))) then
delete(res@$atts(iatt)$)
end if
end do
end if
copy_VarMeta(res_in, res)
end if
; Plot symbol
str = unique_string("polygon")
plot@$str$ = gsn_add_polygon(wks, plot, xpos_in, ypos_in, res)
leave_msg(scriptname, funcname)
end
; #############################################################################
undef("month_sel")
function month_sel(month_names:string)
;
; Arguments
;
;
; Return value
;
;
; Description
;
;
; Caveats
;
; Modification history
; 20190605-righi_mattia: ported from v1.
;
local month_names, month_n, i
begin
if (.not.any(ismissing(ind(month_names.eq."ANN")))) then
month_n = new(12, "integer")
month_n = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12/)
else
month_n = new(dimsizes(month_names), "integer")
do i = 0, dimsizes(month_names)-1
if (.not.ismissing(ind(month_names(i).eq."JAN"))) then
month_n(i) = 1
end if
if (.not.ismissing(ind(month_names(i).eq."FEB"))) then
month_n(i) = 2
end if
if (.not.ismissing(ind(month_names(i).eq."MAR"))) then
month_n(i) = 3
end if
if (.not.ismissing(ind(month_names(i).eq."APR"))) then
month_n(i) = 4
end if
if (.not.ismissing(ind(month_names(i).eq."MAY"))) then
month_n(i) = 5
end if
if (.not.ismissing(ind(month_names(i).eq."JUN"))) then
month_n(i) = 6
end if