-
Notifications
You must be signed in to change notification settings - Fork 15
/
func_plot_cn.ncl
644 lines (564 loc) · 19.1 KB
/
func_plot_cn.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
;;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
;;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
;;load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
undef("plot_latlon")
function plot_latlon(title,filename,var[*][*],ires)
begin
; input figtitle, fig filename and 2d var
if(all(ismissing(var)))then
print("plot plot_latlon(): input var is all missing.")
return False
end if
print("plot plot_latlon(): "+filename)
res = True
res@cnFillOn = True
res@cnLinesOn = False
res@gsnSpreadColors = True
res@mpFillOn = False
res@mpCenterLonF = 180.
res@tiMainString = ""
res@gsnStringFont = 22
res@gsnLeftString = title
if(isatt(var,"units"))then
res@gsnRightString = var@units
end if
res@lbLabelAutoStride = True
if(min(var)*max(var).ge.0)then
else
symMinMaxPlt(var,21,True,res)
end if
wks = gsn_open_wks("ps",filename)
colormap = "BlWhRe"
copy_VarAtts(ires,res)
if(isatt(res,"colormap"))then
cc = stringtochar(res@colormap)
lcc = dimsizes(cc)
if(cc(lcc-5:lcc-1).eq.".rgb")then
if(fileexists(res@colormap))then
colormap := RGBtoCmap(res@colormap)
end if
else
colormap = res@colormap
end if
delete_VarAtts(res, "colormap")
end if
;;print("plot_latlon(): set color map to "+res@colormap)
gsn_define_colormap(wks, colormap)
if(isatt(res,"reversecolor"))then
delete_VarAtts(res, "reversecolor")
gsn_reverse_colormap(wks)
end if
if(isatt(res,"plotaddcng"))then ;; will plot green contour
addcng = res@plotaddcng ;; should be (lat,lon)
copy_VarCoords(var,addcng)
delete_VarAtts(res,"plotaddcng")
res@gsnFrame = False
res@gsnDraw = False
atts = getvaratts(res)
cngres = True
do i = 0, dimsizes(atts)-1
if(.not.ismissing(str_match(atts(i),"cngres")))then
att = str_sub_str(atts(i),"cngres","")
cngres@$att$ = res@$atts(i)$
delete_VarAtts(res,atts(i))
delete(att)
end if
end do
delete(atts)
end if
if(isatt(res,"plotaddcnr"))then ;; will plot red contour
addcnr = res@plotaddcnr ;; should be (lat,lon)
copy_VarCoords(var,addcnr)
delete_VarAtts(res,"plotaddcnr")
res@gsnFrame = False
res@gsnDraw = False
end if
if(isatt(res,"plotaddvcu"))then ;; will plot vector
addvcu = res@plotaddvcu ;; should be (lat,lon)
addvcv = res@plotaddvcv ;; should be (lat,lon)
assignFillValue(var,addvcu)
assignFillValue(var,addvcv)
if(isatt(res,"plotaddvcc"))then ;; vector color
;;; vector color is limited by colormap in wks
;;; still need improve to usable
addvcc = res@plotaddvcc ;; should be (lat,lon)
copy_VarCoords(var,addvcc)
delete_VarAtts(res,"plotaddvcc")
end if
;;;not done
copy_VarCoords(var,addvcu)
copy_VarCoords(var,addvcv)
delete_VarAtts(res,"plotaddvcu")
delete_VarAtts(res,"plotaddvcv")
res@gsnFrame = False
res@gsnDraw = False
end if
if(isatt(res,"plotline"))then
lines = res@plotline ;; should be (nline,npoint,lat/lon)
delete_VarAtts(res,"plotline")
res@gsnFrame = False
res@gsnDraw = False
if(isatt(res,"plotlinecolors"))then
linecolors = res@plotlinecolors
delete_VarAtts(res,"plotlinecolors")
else
linecolors = "red"
end if
nlcolor = dimsizes(linecolor)
end if
if(isatt(res,"nomap") .and. res@nomap)then
delete(res@nomap)
plot = gsn_csm_contour(wks,var,res)
else
plot = gsn_csm_contour_map_ce(wks,var,res)
end if
if(isvar("lines"))then
ldims = dimsizes(lines)
;;print("ploting lines: "+ldims(0))
lineres = True
lineres@gsLineColor = "mediumaquamarine"
lineres@gsLineThicknessF = 3
do l = 0,ldims(0)-1
lineres@gsLineColor = linecolors(where(l.gt.nlcolor,nlcolor,l))
plotstr = "plot"+l
ldims@$plotstr$ = gsn_add_polyline(wks,plot,lines(l,:,1),lines(l,:,0),lineres)
plotstr = "plotm"+l
maxp = min(ind(ismissing(lines(l,:,1))))-1
lineres@gsMarkerIndex = 1
if(ismissing(maxp))then
maxp = 0
;print(lines(l,:,0)+","+lines(l,:,1))
;exit
end if
ldims@$plotstr$ = gsn_add_polymarker(wks,plot,lines(l,maxp,1),lines(l,maxp,0),lineres)
end do
end if
if(isvar("addcng"))then ;; plot red contour
gres = True
gres@cnLineColor = "green"
gres@cnLineLabelsOn = False
gres@cnLineThicknessF = 2.
gres@gsnRightString = ""
gres@gsnLeftString = ""
gres@gsnCenterString = ""
gres@gsnDraw = False
gres@gsnFrame = False
gres@cnInfoLabelOn = False
copy_VarAtts(cngres,gres)
plotr = gsn_csm_contour(wks,addcng,gres)
overlay(plot,plotr)
end if
if(isvar("addcnr"))then ;; plot red contour
rres = True
rres@cnLineLabelsOn = False
rres@cnLineColor = "red"
rres@cnLineThicknessF = 2.
rres@gsnRightString = ""
rres@gsnLeftString = ""
rres@gsnCenterString = ""
rres@gsnDraw = False
rres@gsnFrame = False
rres@cnInfoLabelOn = False
plotr = gsn_csm_contour(wks,addcnr,rres)
overlay(plot,plotr)
end if
if(isvar("addvcu").and.(.not.all(ismissing(addvcu))))then ;; plot vector
;;print("ploting vector")
vres = True
vres@vcRefAnnoOn = False
vres@vcGlyphStyle = "CurlyVector"
vres@vcMinDistanceF = 0.017
vres@gsnDraw = False
vres@gsnFrame = False
vres@vcMinMagnitudeF = 0.001
vres@vcRefLengthF = 0.045
vres@vcRefMagnitudeF = 20.0
vres@vcLineArrowColor = "green"
if(isvar("addvcc"))then
plotv = gsn_csm_vector_scalar(wks,addvcu,addvcv,addvcc,vres)
else
plotv = gsn_csm_vector(wks,addvcu,addvcv,vres)
end if
;;print("ploting vector before overlay")
overlay(plot,plotv)
end if
if(isatt(res,"gsnDraw").and. .not.res@gsnDraw)then
draw(plot)
end if
if(isatt(res,"gsnFrame").and. .not.res@gsnFrame)then
frame(wks)
end if
return plot
end
undef("plot_cn_PL")
function plot_cn_PL(title,filename,var[*][*],ires)
begin
; assume var(p,latorlon)
print("plot plot_cn_PL(): "+filename)
if(isatt(var,"_FillValue_original"))then
var@_FillValue = var@_FillValue_original
end if
res = True
res@cnFillOn = True
res@gsnSpreadColors = True
res@tiMainString = ""
res@gsnStringFont = 22
res@gsnLeftString = title
if(isatt(var,"units"))then
res@gsnRightString = var@units
end if
res@lbLabelAutoStride = True
if(min(var)*max(var).ge.0)then
mmi = nice_mnmxintvl(min(var),max(var),21,True)
res@cnLevelSelectionMode = "ManualLevels"
res@cnMinLevelValF = mmi(0)
res@cnMaxLevelValF = mmi(1)
res@cnLevelSpacingF = mmi(2)
else
symMinMaxPlt(var,21,True,res)
end if
wks = gsn_open_wks("ps",filename)
colormap = "BlWhRe"
copy_VarAtts(ires,res)
if(isatt(res,"colormap"))then
colormap = res@colormap
delete_VarAtts(res, "colormap")
if(isatt(res,"clmcolormap"))then
delete_VarAtts(res, "clmcolormap")
end if
if(isatt(res,"difcolormap"))then
delete_VarAtts(res, "difcolormap")
end if
end if
gsn_define_colormap(wks, colormap)
if(isatt(res,"reversecolor"))then
delete_VarAtts(res, "reversecolor")
gsn_reverse_colormap(wks)
end if
if(isatt(res,"plotline"))then
lines = res@plotline ;; should be (nline,npoint,plev/x)
delete_VarAtts(res,"plotline")
res@gsnFrame = False
res@gsnDraw = False
if(isatt(res,"plotlinecolors"))then
linecolors = res@plotlinecolors
delete_VarAtts(res,"plotlinecolors")
else
linecolors = "red"
end if
nlcolor = dimsizes(linecolors)
end if
plot = gsn_csm_pres_hgt(wks,var,res)
if(isvar("lines"))then
ldims = dimsizes(lines)
;;print("ploting lines: "+ldims(0))
lineres = True
;;lineres@gsLineColor = "mediumaquamarine"
lineres@gsLineThicknessF = 3
do l = 0,ldims(0)-1
if(all(ismissing(lines(l,:,:))))then
continue
end if
lineres@gsLineColor = linecolors(where(l.gt.nlcolor,nlcolor,l))
plotstr = "plot"+l
ldims@$plotstr$ = gsn_add_polyline(wks,plot,lines(l,:,1),lines(l,:,0),lineres)
plotstr = "plotm"+l
maxp = min(ind(ismissing(lines(l,:,1))))-1
lineres@gsMarkerIndex = 1
if(ismissing(maxp))then
maxp = 0
;print(lines(l,:,0)+","+lines(l,:,1))
;exit
end if
ldims@$plotstr$ = gsn_add_polymarker(wks,plot,lines(l,maxp,1),lines(l,maxp,0),lineres)
end do
end if
if(isatt(res,"gsnDraw").and. .not.res@gsnDraw)then
draw(plot)
end if
if(isatt(res,"gsnFrame").and. .not.res@gsnFrame)then
frame(wks)
end if
return plot
end
undef("plot_vc_cn_PL")
function plot_vc_cn_PL(title,filename,var[*][*],xvar[*][*],zvar[*][*],ires)
begin
; assume var(p,latorlon)
print("plot "+filename)
if(isatt(var,"_FillValue_original"))then
var@_FillValue = var@_FillValue_original
end if
res = True
res@cnFillOn = True
res@cnLinesOn = False
res@gsnSpreadColors = True
res@vcRefLengthF = .03
res@vcRefMagnitudeF = 30.
res@vcGlyphStyle = "CurlyVector"
res@tiMainString = ""
res@gsnStringFont = 22
res@gsnLeftString = title
res@vcMinDistanceF = 0.01
vcxmaxpoint = 36
dims = dimsizes(xvar)
xstride = max((/1,(dims(1)/vcxmaxpoint)/))
scale = 1.
if(isatt(var,"units"))then
res@gsnRightString = var@units+"*"+scale
end if
res@lbLabelAutoStride = True
if(min(var)*max(var).ge.0)then
mmi = nice_mnmxintvl(min(var),max(var),21,True)
res@cnLevelSelectionMode = "ManualLevels"
res@cnMinLevelValF = mmi(0)
res@cnMaxLevelValF = mmi(1)
res@cnLevelSpacingF = mmi(2)
else
symMinMaxPlt(var,21,True,res)
end if
wks = gsn_open_wks("ps",filename)
colormap = "BlWhRe"
copy_VarAtts(ires,res)
if(isatt(res,"colormap"))then
colormap = res@colormap
delete_VarAtts(res, "colormap")
end if
gsn_define_colormap(wks, colormap)
if(isatt(res,"reversecolor"))then
delete_VarAtts(res, "reversecolor")
gsn_reverse_colormap(wks)
end if
pzvar = zvar
pzvar = zvar*scale
pxvar = xvar
plot = gsn_csm_pres_hgt_vector(wks,var,pxvar(:,::xstride),pzvar(:,::xstride),res)
return plot
end
undef("plot_Pprofiles2")
function plot_Pprofiles2(title,filename,varname1,var1[*],varname2,var2[*],ires)
begin
; assume 2 vars
res = ires
print("plot plot_Pprofiles2(): "+filename)
linecolors = (/"Black","Red"/)
linedashpattern = (/0,1/)
if(isatt(var1,"_FillValue_original"))then
var1@_FillValue = var1@_FillValue_original
end if
if(isatt(var2,"_FillValue_original"))then
var2@_FillValue = var2@_FillValue_original
end if
res = True
res@tiMainString = ""
res@gsnStringFont = 22
res@gsnLeftString = title
res@gsnFrame = False
res@trYReverse = True
;res@pmLegendDisplayMode = "Always"
;res@pmLegendParallelPosF = .90
;res@pmLegendOrthogonalPosF = -0.8
;res@pmLegendWidthF = 0.52
;res@pmLegendHeightF = 0.25
res@xyYStyle = "Log"
res@tmYLMode = "Explicit"
res@xyLineColor = linecolors(0)
res@xyDashPattern = linedashpattern(0)
res@tmYLValues = (/ 1000, 700,500,400,300,200,100,50,30,10,5,1/)
res@tmYLLabels = ""+res@tmYLValues
res1Leg = True
res1Leg@gsLineColor = linecolors(0)
res1Leg@gsLineDashPattern = linedashpattern(0)
res2 = True
res2@gsLineColor = linecolors(1)
res2@gsLineDashPattern = linedashpattern(1)
resText = True
resText@txFontHeightF = 0.020
wks = gsn_open_wks("ps",filename)
colormap = "BlWhRe"
copy_VarAtts(ires,res)
if(isatt(res,"colormap"))then
colormap = res@colormap
delete_VarAtts(res, "colormap")
end if
gsn_define_colormap(wks, colormap)
if(isatt(res,"reversecolor"))then
delete_VarAtts(res, "reversecolor")
gsn_reverse_colormap(wks)
end if
plot = gsn_csm_xy(wks,var1,var1&$var1!0$,res)
gsn_polyline(wks,plot,var2,var2&$var2!0$,res2)
gsn_text_ndc(wks,varname1,0.7,0.75 ,resText)
gsn_text_ndc(wks,varname2,0.7,0.70,resText)
gsn_polyline_ndc(wks,(/0.60,0.65/),(/0.75,0.75/),res1Leg)
gsn_polyline_ndc(wks,(/0.60,0.65/),(/0.70,0.70/),res2)
frame(wks)
return plot
end
undef("plot_Pprofiles2_dif")
function plot_Pprofiles2_dif(title,filename,var1[*],var2[*],ires)
begin
; assume 2 vars
res = ires
print("plot "+filename)
linecolors = (/"Black"/)
linedashpattern = (/0/)
if(isatt(var1,"_FillValue_original"))then
var1@_FillValue = var1@_FillValue_original
end if
if(isatt(var2,"_FillValue_original"))then
var2@_FillValue = var2@_FillValue_original
end if
nvar1 = int2p_Wrap(var1&$var1!0$,var1,var2&$var2!0$,2)
vardif = nvar1
vardif = nvar1 - var2
res = True
res@tiMainString = ""
res@gsnStringFont = 22
res@gsnLeftString = title
res@gsnFrame = False
res@trYReverse = True
res@xyYStyle = "Log"
res@tmYLMode = "Explicit"
res@xyLineColor = linecolors(0)
res@xyDashPattern = linedashpattern(0)
res@tmYLValues = (/ 1000, 700,500,400,300,200,100,50,30,10,5,1/)
res@tmYLLabels = ""+res@tmYLValues
resref = True
resref@gsLineColor = "green"
wks = gsn_open_wks("ps",filename)
colormap = "BlAqGrYeOrReVi200"
copy_VarAtts(ires,res)
if(isatt(res,"colormap"))then
colormap = res@colormap
delete_VarAtts(res, "colormap")
end if
gsn_define_colormap(wks, colormap)
if(isatt(res,"reversecolor"))then
delete_VarAtts(res, "reversecolor")
gsn_reverse_colormap(wks)
end if
plot = gsn_csm_xy(wks,vardif,vardif&$vardif!0$,res)
gsn_polyline(wks,plot,(/0,0/),(/1000,100/),resref)
frame(wks)
return plot
end
undef("plot_cn")
function plot_cn(title,filename,var[*][*],ires)
begin
;; detet var dimname to plot lon-lat or lon-p or lat-p
dimnames = getvardims(var)
if(isatt(var,"valid_range"))then
rag = var@valid_range
var = where((var.lt.rag(0)).or.(var.gt.rag(1)),var@_FillValue,var)
end if
lat = True ;str_index_of_substr(dimnames(0),"lat",-1).gt.-1
lon = True ;str_index_of_substr(dimnames(1),"lon",-1).gt.-1
if(lat.and.lon)then
a = plot_latlon(title,filename,var,ires)
else
a = plot_cn_PL(title,filename,var,ires)
end if
return a
end
undef("plot_vc_cn")
function plot_vc_cn(title,filename,u[*][*],v[*][*],cn[*][*],ires)
begin
print("plot vector-countour : "+filename)
res = True
res@gsnSpreadColors = True
res@cnFillOn = True
res@cnLinesOn = False
res@tiMainString = ""
res@gsnStringFont = 22
res@gsnLeftString = title
if(isatt(cn,"units"))then
res@gsnRightString = cn@units
end if
res@lbLabelAutoStride = True
res@pmLabelBarOrthogonalPosF = -0.15 ; move label bar closer
res@mpFillOn = False
res@mpCenterLonF = 180.
res@gsnScalarContour = True
res@vcLineArrowColor = "darkgreen" ; change vector color
res@vcGlyphStyle = "CurlyVector" ; turn on curley vectors
res@vcMinDistanceF = 0.02
vref = nice_mnmxintvl (0.,max(abs(u)),15,True)
res@vcRefMagnitudeF = vref(1) ; define vector ref mag
res@vcRefLengthF = 0.045 ; define length of vec ref
res@vcRefAnnoArrowLineColor = "black" ; change ref vector color
wks = gsn_open_wks("ps",filename)
copy_VarAtts(ires,res)
colormap = "BlWhRe"
if(isatt(res,"colormap"))then
colormap = res@colormap
delete_VarAtts(res, "colormap")
end if
if(isatt(res,"reversecolor"))then
delete_VarAtts(res, "reversecolor")
gsn_reverse_colormap(wks)
end if
gsn_define_colormap(wks, colormap)
plot = gsn_csm_vector_scalar_map_ce(wks,u,v,cn,res)
return plot
end
undef("diff_var")
function diff_var(var1,var2)
begin
dims1 = dimsizes(var1)
dims2 = dimsizes(var2)
nvar1 = linint2(var1&$var1!1$,var1&$var1!0$,var1,False,var2&$var2!1$,var2&$var2!0$,0)
vardif = nvar1 - var2
copy_VarCoords(var2,vardif)
return vardif
end
undef("diff_var_PL")
function diff_var_PL(var1,var2)
begin
dims1 = dimsizes(var1)
dims2 = dimsizes(var2)
npvar1 = int2p_n_Wrap(var1&$var1!0$,var1,var2&$var2!0$,2,0)
nvar1 = linint1_n_Wrap(npvar1&$npvar1!1$,npvar1,False,var2&$var2!1$,0,1)
vardif = nvar1 - var2
copy_VarCoords(var2,vardif)
return vardif
end
undef("plot_diff_cn")
function plot_diff_cn(title[1]:string,filename[1]:string,var1,var2,res[1]:logical)
begin
;; plot contour of va1-var2
vardif = diff_var(var1,var2)
a = plot_cn(title,filename,vardif,res)
return vardif
end
undef("plot_diff_cn_PL")
function plot_diff_cn_PL(title[1]:string,filename[1]:string,var1,var2,res[1]:logical)
begin
;; plot contour of va1-var2
vardif = diff_var_PL(var1,var2)
a = plot_cn_PL(title,filename,vardif,res)
;print(num(ismissing(vardif)))
;print(num(.not.ismissing(vardif)))
return vardif
end
undef("plot_diff_vc_cn")
function plot_diff_vc_cn(title[1]:string,filename[1]:string,u1,v1,c1,u2,v2,c2,res[1]:logical)
begin
;; plot vector and contour of va1-var2
udif = diff_var(u1,u2)
vdif = diff_var(v1,v2)
cdif = diff_var(c1,c2)
a = plot_vc_cn(title,filename,udif,vdif,cdif,res)
return a
end
;; test
;;load "$HOME/HiRAM_ncl/func_read_c360.ncl"
;a = read_1month("ta",1999,8)
;printVarSummary(a)
;res = True
;b = plot_cn("lon-lat map","test1",a(0,:,:),res)
;c = plot_cn("lat-p profile","test1",a(:,:,0),res)
;u = read_1month("c384","ucomp",1979,1)
;v = read_1month("c384","vcomp",1979,1)
;t = read_1month("c384","temp" ,1979,1)
;a = plot_vc_cn("test","test",u(5,::20,::20),v(5,::20,::20),t(5,::20,::20),True)