-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon_plot_lib.ncl
2055 lines (1929 loc) · 69.8 KB
/
icon_plot_lib.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
;---------------------------------------------------------------
; ICON PLOT LIB
; This library aimes to provide an abstract way to access and plot ICON data
; fields from NetCDF files. It should obey some general requirements:
; * Dont-Repeat-Yourself-Principle: Every method has one responsibility, which no
; other method has. Or in other words: Don't just CopyAndPaste.
; * Use as much information from the input files as possible. If something is
; missing, create a better file or let the user input this piece of
; information. Examples are coordinates of data variables and bounds of them.
; There are attributes for this.
; * naming conventions:
; # check* methods are procedure which can exit the whole program
; # get* methods are functions with return, what they describe with their
; name. This is similar to NCL itself, e.g. getfilevardims()
; # set* methods are procedures
;---------------------------------------------------------------
; Authors : Ralf Mueller ([email protected])
; Stephan Lorenz ([email protected])
; VERSION = "0.1.0"
;-------------------------------------------------------------------------------
; some global defaults
DEFAULTLON = "clon"
DEFAULTLAT = "clat"
RAD2DEG = 45./atan(1.)
ABORTMSG = "Abort Script."
NUMLEVS = 10
CDO = "cdo"
; DEFAULTCOLOR = "BlWhRe" ; ncolors = 103
DEFAULTCOLOR = "BlueDarkRed18" ; ncolors = 256
DEFAULTCOLOR = "BlueWhiteOrangeRed" ; ncolors = 256
;-------------------------------------------------------------------------------
undef("setCDO")
procedure setCDO(path2Cdo)
begin
CDO=path2Cdo
end
;-------------------------------------------------------------------------------
; signum function
undef("sign")
function sign(val)
begin
if (val.eq.0) then
return(1)
else
return(val/abs(val))
end if
end
;---------------------------------------------------------------
; return ncl version string
undef("getNclVersion")
function getNclVersion()
begin
version_string = systemfunc("ncl -V")
version_array = str_split(version_string, ".")
; use integers instead
return stringtointeger(version_array)
end
;---------------------------------------------------------------
; return true if ncl uses groups for netcdf4 input (release 6.1.1 with netcdf4 input)
undef("versionWithNC4GroupNotationSupport")
function versionWithNC4GroupNotationSupport()
begin
return (/6,1,1/)
end
undef("versionIsGE")
function versionIsGE(otherVersion)
begin
return True
diff = otherVersion .gt. getNclVersion()
print(""+diff)
do i=0,dimsizes(diff)
if (.not. diff(i)) then
return False
end if
end do
return True
end
;-------------------------------------------------------------------------------
; return a filehandle that can access variables names without leading '/' even
; if the input it nc4 and ncl has version 6.1.1 or above
undef("nc4Aware_addfile")
function nc4Aware_addfile(filename,debug)
begin
filehandle = addfile( filename, "r" )
filetype = get_file_version(filehandle)
if debug then
print("[nc4Aware_addfile]Read in file:'" + filename +"'")
print("[nc4Aware_addfile] Filetype:'" + filetype +"'")
print("[nc4Aware_addfile]NCL version:'" + str_join(getNclVersion(),".") +"'")
print("[nc4Aware_addfile]version > 611'" + versionIsGE((/6,1,1/)) )
end if
if ((get_file_version(filehandle) .eq. "NETCDF4") .and. versionIsGE((/6,1,1/))) then
print("[nc4Aware_addfile]switch to root group '/'")
;filehandle = filehandle=>/
end if
return filehandle
end
;-------------------------------------------------------------------------------
; Is the 'time' variable a dimesions in the input dimensions array?
undef("has_time")
function has_time(dims)
begin
return any(dims.eq."time")
end
;-------------------------------------------------------------------------------
; Has the input variable the dimension 'time'?
undef("var_has_time")
function var_has_time(varname,filehandle)
begin
dims = getfilevardims(filehandle,varname)
if ( any(dims.eq."time") ) then
return(True)
else
return(False)
end if
end
;-------------------------------------------------------------------------------
; has a file a certain variable
undef("has_var")
function has_var(filehandle,varname)
begin
vNames = getfilevarnames(filehandle)
n = dimsizes (vNames)
do i=0,n-1 ; loop through each variable
if (vNames(i).eq.varname) return True end if
end do
return False
end
;---------------------------------------------------------------
; get vertical dimension
undef("getVertDim")
function getVertDim(filehandle,var)
begin
dimNames = getvardims(var)
matches = (/"lev","depth","height","z"/)
do i=0,dimsizes(matches)-1
retval = str_match(dimNames,matches(i))
if (.not.ismissing(retval)) return retval end if
end do
return(str_match(dimNames,matches(0)))
end
;---------------------------------------------------------------
; procedure warper for array_append_record
undef("array_append_record_proc")
procedure array_append_record_proc(array,addition)
begin
arrayCopy = array_append_record(array,addition,0)
array = arrayCopy
end
;-------------------------------------------------------------------------------
; Read a horizontal field of the variable 'varname' from the given filehandle.
; If it is time dependant or has a vertical axis, use the given values for
; timstep and levelindex for selecting the field.
;
; Currently this limited to 3 dimensions, so that 4D tracer variables are ignored
undef("selIconField")
function selIconField(varname,filehandle,timestep,levelindex)
begin
dims = getfilevardims(filehandle,varname)
noOfDims = dimsizes(dims)
noOfDims = noOfDims(0)
usableVar = False
if ( has_time(dims) ) then
if (noOfDims .eq. 3) then ; dims: (time,lev,cell)
var = filehandle->$varname$(timestep,levelindex,:)
usableVar = True
end if
if (noOfDims .eq. 2) then ; dims: (time,cell)
var = filehandle->$varname$(timestep,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (time)
var = filehandle->$varname$(:)
usableVar = True
end if
else
; TODO: support for 4D tracer fields
; if (noOfDims .eq. 3) then ; dims: (lev,cell,tracerID)
; var = filehandle->$varname$(levIndex,:,tracer)
; usableVar = True
; end if
if (noOfDims .eq. 2) then ; dims: (lev,cell)
var = filehandle->$varname$(levelindex,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (cell)
var = filehandle->$varname$(:)
usableVar = True
end if
end if
if (.not. usableVar) then
print("Warning! Could not read " + varname + " from input file")
exit
end if
return var
end
;-------------------------------------------------------------------------------
; get full 3d ICON variable
undef("selIconVar")
function selIconVar(varname,filehandle,timestep)
begin
dims = getfilevardims(filehandle,varname)
noOfDims = dimsizes(dims)
noOfDims = noOfDims(0)
usableVar = False
if ( has_time(dims) ) then
if (noOfDims .eq. 3) then ; dims: (time,lev,cells)
var = filehandle->$varname$(timestep,:,:)
usableVar = True
end if
if (noOfDims .eq. 2) then ; dims: (time,cells)
var = filehandle->$varname$(timestep,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (cells)
var = filehandle->$varname$(:)
usableVar = True
end if
else
if (noOfDims .eq. 2) then ; dims: (lev,cells)
var = filehandle->$varname$(:,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (cells)
var = filehandle->$varname$(:)
usableVar = True
end if
end if
if (.not. usableVar) then
print("Warning! Could not read " + varname + " from input file")
exit
end if
return var
end
;-------------------------------------------------------------------------------
; see selIconField(), but for fields on a regular lonlat grid
undef("selRegularField")
function selRegularField(varname,filehandle,timestep,levelindex)
begin
dims = getfilevardims(filehandle,varname)
noOfDims = dimsizes(dims)
noOfDims = noOfDims(0)
usableVar = False
if ( has_time(dims) ) then
if (noOfDims .eq. 4) then ; dims: (time,lev,x,y)
var = filehandle->$varname$(timestep,levelindex,:,:)
usableVar = True
end if
if (noOfDims .eq. 3) then ; dims: (time,x,y)
var = filehandle->$varname$(timestep,:,:)
usableVar = True
end if
if (noOfDims .eq. 2) then ; dims: (x,y)
var = filehandle->$varname$(:,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (time)
var = filehandle->$varname$(:)
usableVar = True
end if
else
if (noOfDims .eq. 3) then ; dims: (lev,x,y)
var = filehandle->$varname$(levelindex,:,:)
usableVar = True
end if
if (noOfDims .eq. 2) then ; dims: (x,y)
var = filehandle->$varname$(:,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (x)
var = filehandle->$varname$(:)
usableVar = True
end if
end if
if (.not. usableVar) then
print("Warning! Could not read " + varname + " from input file")
exit
end if
return var
end
;-------------------------------------------------------------------------------
; see selRegularField(), but for 3d regular variable
undef("selRegularVar")
function selRegularVar(varname,filehandle,timestep)
begin
dims = getfilevardims(filehandle,varname)
noOfDims = dimsizes(dims)
noOfDims = noOfDims(0)
print(""+noOfDims)
usableVar = False
if ( has_time(dims) ) then
if (timestep.eq.-1) then
if (noOfDims .eq. 4) then ; dims: (time,lev,x,y)
var = filehandle->$varname$
usableVar = True
end if
if (noOfDims .eq. 3) then ; dims: (time,x,y)
var = filehandle->$varname$(:,:,:)
usableVar = True
end if
if (noOfDims .eq. 2) then ; dims: (x,y)
var = filehandle->$varname$(:,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (time)
var = filehandle->$varname$(:)
usableVar = False
end if
else
if (noOfDims .eq. 4) then ; dims: (time,lev,x,y)
var = filehandle->$varname$(timestep,:,:,:)
usableVar = True
end if
if (noOfDims .eq. 3) then ; dims: (time,x,y)
var = filehandle->$varname$(timestep,:,:)
usableVar = True
end if
if (noOfDims .eq. 2) then ; dims: (x,y)
var = filehandle->$varname$(:,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (time)
var = filehandle->$varname$(:)
usableVar = False
end if
end if
else
if (noOfDims .eq. 3) then ; dims: (lev,x,y)
var = filehandle->$varname$(:,:,:)
usableVar = True
end if
if (noOfDims .eq. 2) then ; dims: (x,y)
var = filehandle->$varname$(:,:)
usableVar = True
end if
if (noOfDims .eq. 1) then ; dims: (x)
var = filehandle->$varname$(:)
usableVar = True
end if
end if
if (.not. usableVar) then
print("Warning! Could not read " + varname + " from lonlat input file")
exit
end if
return var
end
;-------------------------------------------------------------------------------
; abstract Field selection
undef("selField")
function selField(varname,filehandle,timestep,levelindex,horizontalgridtype)
begin
if ( horizontalgridtype .eq. "unstructured") then
var = selIconField(varname,filehandle,timestep,levelindex)
else
var = selRegularField(varname,filehandle,timestep,levelindex)
end if
return var
end
;-------------------------------------------------------------------------------
; scale the given variable with the given factor, if it is NOT 1
undef("scaleVar")
procedure scaleVar(var,scalefactor)
local scalefactor
begin
if (scalefactor .ne. 1) then
var = var*scalefactor
end if
end
;-------------------------------------------------------------------------------
; Return the grid type of a given ICON variable (cell, vertex, or edge).
; This is based on the naming of the coordinates.
undef("getGridType")
function getGridType(variable)
begin
str = variable@coordinates
gtype = stringtocharacter(str)
if (gtype(0) .eq. "c") then
str = "cell"
end if
if (gtype(0) .eq. "e") then
str = "edge"
end if
if (gtype(0) .eq. "v") then
str = "vertex"
end if
return str
end
;-------------------------------------------------------------------------------
; Return the size of coordinates
undef("getCoordSizes")
function getCoordSizes(variable,filehandle)
begin
lonlat = str_split(variable@coordinates," ")
lon = lonlat(0)
lat = lonlat(1)
lonSize = dimsizes(filehandle->$lon$)
latSize = dimsizes(filehandle->$lat$)
return (/lonSize,latSize/)
end
;-------------------------------------------------------------------------------
; Return the coordinates of a given variable in radians
undef("getCoordinates")
function getCoordinates(variable,filehandle)
begin
lonlat = str_split(variable@coordinates," ")
lon = lonlat(0)
lat = lonlat(1)
x = filehandle->$lon$
y = filehandle->$lat$
return (/x,y/)
end
;-------------------------------------------------------------------------------
; Return the coordinates of a given variable in degrees
undef("getLonLats")
procedure getLonLats(variable,filehandle,x,y,isicon)
begin
lonlat = str_split(variable@coordinates," ")
lon = lonlat(0)
lat = lonlat(1)
x = filehandle->$lon$
y = filehandle->$lat$
if (isicon) then
x = x * RAD2DEG
y = y * RAD2DEG
end if
end
;-------------------------------------------------------------------------------
; get variable from a certain file
undef("getVarFromFile")
function getVarFromFile(varname,filename)
begin
f = addfile(filename+".nc","r")
var = f->$varname$
return var
end
;-------------------------------------------------------------------------------
; read a coordinates variable from icon file and return its lonlat representation
undef("getCoordinateFromFile")
function getCoordinateFromFile(varname,filename)
begin
var = getVarFromFile(varname,filename)
return var*RAD2DEG
end
;-------------------------------------------------------------------------------
; Create an ascending array of numbers with a 'halflog' interval, i.e. for
; every decade the numbers 1, 2 and 5 are present.
; Examples:
; (/1,2,5,10,20,50,100,200,500/) or
; (/-1,-5e-1,-2e-1,-1e-1,-5e-2,-2e-2,-1e-2,0,1e-2,2e-2,5e-2,1e-1,2e-1,5e-1,1/)
; scaleLimit determines the number of decades on the positive or negative axis of the output array
undef("createLevels")
function createLevels(minVar, maxVar, scaleLimit)
begin
delimiter = "|"
tics4positive = (/1, 2, 5/)
tics4negative = (/5, 2, 1/)
signMin = sign(minVar)
signMax = sign(maxVar)
if (minVar.eq.0) then
logfirst = 0.0
first = 0.0
else
logfirst = ceil(log10(abs(minVar)))
first = signMin*10^logfirst
end if
if (maxVar.eq.0) then
loglast = 0.0
last = 0.0
else
loglast = ceil(log10(abs(maxVar)))
last = signMax*10^loglast
end if
retval = (/first, last, logfirst, loglast, signMin, signMax, logfirst*signMin - loglast*signMax/)
myScale = logfirst
levels = ""
;levels = new(1,float)
if (first .eq. 0) then
levels = str_concat((/levels,delimiter,"0"/))
;array_append_record_proc(levels,0.0)
print(levels)
end if
current = first
if (current .lt. 0) then
tics = tics4negative
do scaleChange=1,scaleLimit
do ticIndex=0,2
current = - tics(ticIndex)*10^(logfirst-scaleChange)
levels = str_concat((/levels,delimiter,flt2string(doubletofloat(current))/))
;array_append_record_proc(levels,doubletofloat(current))
end do
end do
end if
if (first.lt.0 .and. last.gt.0) then
levels = str_concat((/levels,delimiter,"0"/))
;array_append_record_proc(levels,0.0)
end if
if (last .gt. 0) then
tics = tics4positive
if (first.lt.0) then
startScale = min((/logfirst, loglast/)) - scaleLimit
else
startScale = min((/logfirst, loglast/))
end if
j = 0
do while (current.lt.(last/10))
myScale = startScale + j
do ticIndex=0,2
current = tics(ticIndex)*10^(myScale)
levels = str_concat((/levels,delimiter,flt2string(doubletofloat(current))/))
;array_append_record_proc(levels,current)
end do
j = j + 1
end do
levels = str_concat((/levels,delimiter,flt2string(doubletofloat(last))/))
;array_append_record_proc(levels,last)
end if
return(str_split(levels,"|"))
;print(levels)
;return(levels)
end
;---------------------------------------------------------------
; Print all variable names of a given file
undef("printVarNames")
procedure printVarNames(filehandle)
begin
filevarnames = getfilevarnames(filehandle)
print("# === variable names in file: "+filevarnames)
end
;---------------------------------------------------------------
; Print information about dimensions and attributes of a given varaiable
undef("printVar")
procedure printVar(varname, filehandle)
begin
dims = getfilevardims(filehandle,varname)
sizes = filevardimsizes(filehandle,varname)
var = filehandle->$varname$
; Print info about variable, its dimensions and attributes
print(dimsizes(sizes) + " Dimensions:")
if(.not.any(ismissing(dims))) then
do j = 0, dimsizes(dims) -1
print( j + ") " + dims(j) + ": " + sizes(j))
end do
end if
atts = getfilevaratts(filehandle,varname)
if(.not.any(ismissing(atts))) then
do k = 0, dimsizes(atts) -1
print(atts(k) + ": " +filehandle->$varname$@$atts(k)$)
if (atts(k) .eq. "coordinates") then
coordinates = var@$atts(k)$
else
coordinates = DEFAULTLON + " " + DEFAULTLAT
end if
end do
end if
end
;---------------------------------------------------------------
; Check, if a given variable name is present in a file
undef("checkVarname")
procedure checkVarname(varname, filehandle)
begin
filevarnames = getfilevarnames(filehandle)
if (.not. any(filevarnames.eq.varname)) then
print("Could not find variable "+varname+"!")
exit
end if
return True
end
;---------------------------------------------------------------
; Check, if the dimensions of to variables are equal
undef("checkDimsOfVars")
procedure checkDimsOfVars(varname0,varname1,filehandle)
begin
print(getfilevardims(filehandle,varname0))
print(getfilevardims(filehandle,varname1))
if (str_join(getfilevardims(filehandle,varname0),"") .ne. str_join(getfilevardims(filehandle,varname1),"")) then
print("Variables '"+varname0+"' and '"+varname1+"' must have the same dimensions!")
print("ABORT!")
exit
end if
end
;---------------------------------------------------------------
; Read a variable from the given or an optional file
undef("getMaskVar")
function getMaskVar(maskvarname,filehandle,hasOtherMaskfile,maskfilename,timestep,levelindex,plotmode,horizontalgridtype)
begin
if (hasOtherMaskfile) then
mfilehandle = addfile( maskfilename+".nc","r" )
else
mfilehandle = filehandle
end if
checkVarname(maskvarname, mfilehandle)
if (plotmode.eq."scalar" .or. plotmode.eq."overlay") then
if (horizontalgridtype.eq."unstructured") then
maskvar = selIconField(maskvarname,mfilehandle,timestep,levelindex)
else
maskvar = selRegularField(maskvarname,mfilehandle,timestep,levelindex)
end if
else
if (plotmode.eq."vector") then
maskvar = selRegularField(maskvarname,mfilehandle,timestep,levelindex)
else ; section
maskvar = selRegularVar(maskvarname,mfilehandle,timestep)
end if
end if
return maskvar
end
;---------------------------------------------------------------
; Read the dimensions of a variables from the given ot an optional file
;
; GLOBALS: maskFile
undef("getMaskDim")
function getMaskDim(maskvarname,filehandle,hasOtherMaskfile)
begin
if (hasOtherMaskfile) then
filehandle = addfile( maskFile+".nc","r" )
end if
checkVarname(maskvarname, filehandle)
maskdims = getfilevardims(filehandle,maskvarname)
return maskdims
end
;---------------------------------------------------------------
; set new filename bases on the input filename and intput type
undef("setNewFilename")
function setNewFilename(filename,tag,itype,atmlev)
begin
atmtag = ""
if (itype .eq. "atm") atmtag = "atmlev"+str_capital(atmlev)+"_" end if
newFilename = tag+"_"+atmtag+systemfunc("basename "+filename)
return newFilename
end
;---------------------------------------------------------------
; set filename for the automatically remapped file
undef("setRemapFilename")
function setRemapFilename(filename,itype,resolution,atmlev,operator)
begin
return setNewFilename(filename,operator+"_"+resolution,itype,atmlev)
end
undef("setZonmeanFilename")
function setZonmeanFilename(filename,itype,atmlev)
begin
return setNewFilename(filename,"zonmean",itype,atmlev)
end
;---------------------------------------------------------------
; Check, if two files have the same number of time steps. Exit otherwise
undef("checkRemappedFile")
function checkRemappedFile(infilename,remapfilename,varname,itype,atmlev,atmplevs,atmhlevs)
begin
retval = True
if (.not. isfilepresent(remapfilename)) return False end if
orgFile = addfile (infilename+".nc" , "r")
orgTime = getfilevardimsizes(orgFile,"time")
if (.not. has_var(orgFile,varname)) then
print("Cannot find varname:"+varname)
exit
end if
orgvertdimname = getVertDim(orgFile,orgFile->$varname$)
if (.not. ismissing(orgvertdimname) ) then
orgvertdim = orgFile->$orgvertdimname$
end if
remapFile = addfile(remapfilename+".nc","r")
remapTime = getfilevardimsizes(remapFile,"time")
; test if the given variable can be found
if (.not. has_var(remapFile,varname)) then
print("Cannot find "+varname+" in remapped files:"+remapfilename)
retval = False
return retval
else
remappedvertdimname = getVertDim(remapFile,remapFile->$varname$)
if (.not. ismissing(remappedvertdimname)) then
remappedvertdim = remapFile->$remappedvertdimname$
end if
end if
if ( orgTime .eq. remapTime ) then
print("#=====================================================================================")
print("Remapped File '"+remapfilename+"' seems to have right time axes.")
else
print("#=====================================================================================")
print("Remapped File '"+remapfilename+"' seems to be wrong: Has different number of timesteps")
print("Original file has "+orgTime(0)+" timesteps")
print("Remapped File has "+remapTime(0)+" timesteps")
retval = False
end if
if (itype .eq. "atm") then
if (atmlev .eq. "p" .and. (str_join(atmplevs,",") .ne. str_join(remappedvertdim,",")) .and. (.not. ismissing(remappedvertdimname)))
print("Vertical pressure levels differ")
retval = False
else
print("Vertical pressure levels ok.")
end if
if (atmlev .eq. "h" .and. (str_join(atmhlevs,",") .ne. str_join(remappedvertdim,",")) .and. (.not. ismissing(remappedvertdimname)))
print("Vertical height levels differ")
retval = False
else
print("Vertical height levels ok.")
end if
end if
if (itype .eq. "oce") then
if ((.not. ismissing(orgvertdimname)) .and. (str_join(orgvertdim,",") .ne. str_join(remappedvertdim,",")) .and. (.not. ismissing(remappedvertdimname)))
print("Vertical depth levels differ")
retval = False
else
print("Vertical depth levels ok")
end if
end if
return retval
end
;---------------------------------------------------------------
; Perform a remapping (wich CDO) to a regular grid with a given resolution and return the
; filename of the remapped file. If the file is already present, the name is returned only
undef("remapForVecPlot")
procedure remapForVecPlot(iFile,remapFilename,resolution,useMask,plotMode,debug,addvars,operator)
begin
if (plotMode.eq."section") then vecVars=(/varName/) end if
print("#=====================================================================================")
print("Looking for remapped file: "+remapFilename)
print("Use CDO to perform remapping: Create intermediate file: "+remapFilename)
print("Remove this intermediate file, if it was NOT automatically created from your input file "+iFile+"!")
variables = str_join(vecVars,",")
if ( addvars(0) .ne. "")
variables = str_concat((/variables,",",str_join(addvars,",")/))
print(str_join(addvars,","))
print("variables:"+variables)
end if
if ( useMask ) then
variables=variables +","+maskName
end if
if (plotMode.eq."overlay") variables = variables+","+varName end if
cmd = CDO+" -P 8 -"+operator+","+resolution+" -selname,"+variables+" "+iFile+" "+remapFilename
if debug then
print(cmd)
end if
system(cmd)
end
;---------------------------------------------------------------
; perform zonal mean for hoffmueller plot
undef("zonmean4HoffmuellerPlot")
procedure zonmean4HoffmuellerPlot(filename,varname,zonmeanfilename)
begin
cmd = CDO+" zonmean -selname,"+varname+" "+filename+" "+zonmeanfilename
if debug then
print(cmd)
end if
system(cmd)
end
;---------------------------------------------------------------
; Return the bounds of the coordinates of a given variable
undef("getBoundsOfCoordinates")
function getBoundsOfCoordinates(variable,filehandle)
begin
lonlat = str_split(variable@coordinates," ")
lon = lonlat(0)
lat = lonlat(1)
; check for real names of coordinate; order is not pre-defines
lonIsLon = str_match(lon,"lon")
if (ismissing(lonIsLon)) then
lon = lonlat(1)
lat = lonlat(0)
end if
boundslonName = filehandle->$lon$@bounds
boundslatName = filehandle->$lat$@bounds
boundslon = filehandle->$boundslonName$ * RAD2DEG
boundslat = filehandle->$boundslatName$ * RAD2DEG
return (/boundslon, boundslat/)
end
;---------------------------------------------------------------
; Return the bounds of the coordinates of a given variable
undef("getBoundsFromFile")
function getBoundsFromFile(lonname,latname,filename)
begin
f = addfile(filename+".nc","r")
boundslonName = f->$lonname$@bounds
boundslatName = f->$latname$@bounds
boundslon = f->$boundslonName$ * RAD2DEG
boundslat = f->$boundslatName$ * RAD2DEG
return (/boundslon, boundslat/)
end
;---------------------------------------------------------------
; (Re)Set end of bounds. Required by the implementation of the grid plot
undef("setBoundsEnds")
procedure setBoundsEnds(blon,blat,lonmin,lonmax,latmin,latmax)
begin
latmax=max(blat)
latmin=min(blat)
lonmax=max(blon)
lonmin=min(blon)
if ( lonmin.lt.0 ) then
; longitudes are given in the range [-180,180]
lonmin = -180.
lonmax = 180.
else
; longitudes are given in the range [0,360]
lonmin = 0.
lonmax = 360.
end if
end
;---------------------------------------------------------------
; Bounds check for the grid plot
undef("checkLongitude")
procedure checkLongitude(x,boundslon,lonmin,lonmax,debug)
begin
dlon = 80.
ncell_tot = dimsizes(x) ; total # of cells
nfix = 0 ; count the # of problematic cells
do icell = 0, ncell_tot-1
if ( any(boundslon(icell,:).le.(lonmin+dlon)) .and. \
any(boundslon(icell,:).gt.(lonmax-dlon)) ) then
bl1=boundslon(icell,0)
bl2=boundslon(icell,1)
bl3=boundslon(icell,2)
boundslon(icell,:) = where(boundslon(icell,:).gt.(lonmax-dlon), \;
boundslon(icell,:)-360., \; where true
boundslon(icell,:) ) ; where false
bn1=boundslon(icell,0)
bn2=boundslon(icell,1)
bn3=boundslon(icell,2)
if (debug) then
print("cell="+icell+" lon="+bl1+", "+bl2+", "+bl3+" changed into " \
+" lon="+bn1+", "+bn2+", "+bn3)
end if
nfix = nfix +1
end if
end do
print("Longitude(s) of "+nfix+" cells corrected to avoid plotting problem.")
end
;---------------------------------------------------------------
; Set the Vertical Selection mode of the given NCL resource
undef("setLevels")
procedure setLevels(selmode,resource,minvar,maxvar,scalelimit,numlevs,debug)
begin
if (selmode .eq. "manual") then
if ( .not. isvar("plotLevs") ) then
resource@cnLevelSelectionMode = "ManualLevels"
resource@cnMinLevelValF = minvar
resource@cnMaxLevelValF = maxvar
; diffLog10 = log10(abs(maxVar-minVar))
;; if (diffLog10 .lt. 0) then
; resource@cnLevelSpacingF = 10^(floor(diffLog10))/numLevs
;; else
;; resource@cnLevelSpacingF = 10^(floor(diffLog10))/numLevs
;; end if
diffspacing = abs(maxvar-minvar)/(int2flt(numlevs))
resource@cnLevelSpacingF = diffspacing
else
resource@cnLevelSelectionMode = "ExplicitLevels"
plotLevels = plotLevs
if (debug) print("plotlevels = "+plotLevels) end if
resource@cnLevels = plotLevels
end if
end if
if (selmode .eq. "halflog") then
resource@cnLevelSelectionMode = "ExplicitLevels"
plotLevels = createLevels(minvar,maxvar,scalelimit)
if (debug) print("plotlevels = "+plotLevels) end if
resource@cnLevels = plotLevels
end if
if (debug) then
print("Manual Plotlevel setting......")
print(" selMode : " + selmode)
print(" minVal : " + minvar)
print(" maxVal : " + maxvar)
print(" numLevs: " + numlevs)
print(" scalelimit: " + scalelimit)
print(" plotLevels: " + resource@cnLevels)
end if
end
;---------------------------------------------------------------
; set the lines and their labels
undef("setLineAndLineLabels")
procedure setLineAndLineLabels(resource,withLines,withLineLabels)
begin
; lines/labels inside the plot ================================================
resource@cnLinesOn = False
resource@cnLineLabelsOn = False
if (withLineLabels ) then
withLines = True
resource@cnLineLabelsOn = True
resource@cnLineLabelFontHeightF = 0.008
resource@cnLineLabelFontThicknessF = 0.006
end if
if (withLines) resource@cnLinesOn = True end if
end
;---------------------------------------------------------------
; Create a default NCL resource
undef("setDefaultResource")
function setDefaultResource(verticallabelbar,withLines,withLineLabels,fillmode,maptype,makeYLinear)
begin
resource = True
resource@gsnMaximize = False
resource@gsnFrame = False
resource@gsnDraw = True
resource@gsnSpreadColors = True
resource@cnFillOn = True
resource@mapType = maptype
; lines/labels inside the plot ================================================
setLineAndLineLabels(resource,withLines,withLineLabels)
; representation of missing values ===========================================
delete(resource@cnMissingValFillPattern); no longer filling missing value areas
delete(resource@cnMissingValFillColor); no longer filling missing value areas
resource@cnMissingValPerimOn = True
resource@cnMissingValFillPattern = -1; set the missing value fill pattern to 5
resource@cnMissingValFillScaleF = 0.9; increase the density of the fill pattern (default = 1.0)
resource@cnMissingValPerimColor = "black"; change the missing value perimeter to black
resource@cnMissingValPerimDashPattern = 1; set the dash pattern of the missing value perimeter to 1
resource@cnMissingValPerimThicknessF = 1.0; increase the thickness of the missing value perimeter 3X
FontHeight0 = 0.012
FontHeight1 = 0.015
FontHeight2 = 0.017
FontHeight3 = 0.020
resource@tiXAxisFontHeightF = FontHeight0
resource@tiYAxisFontHeightF = FontHeight0
resource@tmXBLabelFontHeightF = FontHeight0
resource@tmYLLabelFontHeightF = FontHeight0
resource@tmXBLabelJust = "CenterCenter"
resource@gsnStringFontHeightF = FontHeight0
resource@mpFillOn = True
; label settings ==============================================================
resource@lbLabelBarOn = True
if (verticallabelbar .eq. True) then
resource@lbOrientation = "vertical"
else
resource@pmLabelBarHeightF = 0.12
resource@pmLabelBarWidthF = 0.80
resource@pmLabelBarOrthogonalPosF = 0.15
end if
resource@lbLabelAutoStride = True
resource@lbLabelFontHeightF = FontHeight1 ; color bar labels
resource@cnFillMode = fillmode
resource@cnRasterSmoothingOn = True
resource@mpGreatCircleLinesOn = True
resource@stMinArrowSpacingF = 0.001
; linearize y axis if desired ===============================================
resource@gsnYAxisIrregular2Linear = makeYLinear
return resource
end
;---------------------------------------------------------------
undef("setDefaultSectionResource")
function setDefaultSectionResource(points,secpoints,seclc,secrc,withLines,withLineLabels,makeYLinear)
begin
resource = True; plot mods desired
resource@gsnFrame = False; don't turn page yet
resource@gsnDraw = False; don't draw yet
resource@tmXBMode = "Explicit"; explicitly label x-axis
latDiff = stringtodouble(secrc(1)) - stringtodouble(seclc(1))
tic = latDiff/(stringtodouble(secpoints)-1)
p = (points * tic) + stringtodouble(seclc(1))
; resource@tmXBValues = points
resource@tmXBValues = (/points(0),points(secpoints-1)/); points to label values
labels = new(dimsizes(p),string)
labels = p
;print(""+labels)
;resource@tmXBLabels = labels
resource@tmXBLabels = (/ seclc(1) +"N, "+ seclc(0)+"E" , secrc(1)+"N, "+secrc(0)+"E" /)
resource@tmXBLabelFontHeightF = 0.01
resource@cnFillOn = True; turn on color
resource@lbLabelAutoStride = True; nice label bar label stride
resource@gsnSpreadColors = True; use full range of colormap
resource@lbOrientation = "vertical"; vertical label bar
resource@pmLabelBarOrthogonalPosF = -0.04; move label bar closer to plot
resource@lbTitleString = "kg/m^3"
resource@lbTitlePosition = "Bottom"