1 ;**************************************************************
2 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
3 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
4 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
5 ;**************************************************************
6 procedure set_line(lines:string,nline:integer,newlines:string)
8 ; add line to ascci/html file
10 nnewlines = dimsizes(newlines)
11 if(nline+nnewlines-1.ge.dimsizes(lines))
12 print("set_line: bad index, not setting anything.")
15 lines(nline:nline+nnewlines-1) = newlines
16 ; print ("lines = " + lines(nline:nline+nnewlines-1))
17 nline = nline + nnewlines
20 ;**************************************************************
27 ;-----------------------------------------------------
28 ; edit table.html of current model for movel1_vs_model2
30 if (isvar("compare")) then
31 html_name2 = compare+"/table.html"
32 html_new2 = html_name2 +".new"
35 ;------------------------------------------------------
36 ; edit table.html for current model
38 html_name = model_name+"/table.html"
39 html_new = html_name +".new"
41 ;------------------------------------------------------
44 fm = addfile(dirm+film10,"r")
49 dsizes = dimsizes(laimod)
54 ;-----------------------------------
57 film_l = "lnd_"+ model_grid + ".nc"
58 fm_l = addfile (dirs+film_l,"r")
59 landfrac = fm_l->landfrac
62 ;----------------------------------
63 ; read biome data: model
65 biome_name_mod = "Model PFT Class"
67 film_c = "class_pft_"+model_grid+".nc"
68 fm_c = addfile(dirs+film_c,"r")
69 classmod = fm_c->CLASS_PFT
73 ; model data has 17 land-type classes
76 ;----------------------------------------------------------
79 ;----------------------------------
80 ; read biome data: observed
82 biome_name_ob = "MODIS LandCover"
85 filo_c = "land_class_"+model_grid+".nc"
86 fo = addfile(dir_c+filo_c,"r")
87 classob = tofloat(fo->LAND_CLASS)
91 ; input observed data has 20 land-type classes
94 ;---------------------------------
95 ; read lai data: observed
97 ;ob_name = "MODIS MOD 15A2 2000-2005"
98 ob_name = "MODIS MOD 15A2 2000-2004"
100 dir_l = diro + "lai/"
101 ;filo = "LAI_2000-2005_MONS_"+model_grid+".nc"
102 filo = "LAI_2000-2004_MONS_"+model_grid+".nc"
103 fo = addfile(dir_l+filo,"r")
108 ;-------------------------------------------------
109 ; take into account landfrac
111 laimod = laimod * conform(laimod,landfrac,(/1,2/))
112 laiob = laiob * conform(laiob ,landfrac,(/1,2/))
116 ;************************************************
118 ;************************************************
119 resg = True ; Use plot options
120 resg@cnFillOn = True ; Turn on color fill
121 resg@gsnSpreadColors = True ; use full colormap
122 resg@cnLinesOn = False ; Turn off contourn lines
123 resg@mpFillOn = False ; Turn off map fill
124 resg@cnLevelSelectionMode = "ManualLevels" ; Manual contour invtervals
126 ;************************************************
127 ; plot global biome class: (1) observed
128 ;************************************************
130 resg@cnMinLevelValF = 1. ; Min level
131 resg@cnMaxLevelValF = 19. ; Max level
132 resg@cnLevelSpacingF = 1. ; interval
134 classob@_FillValue = 1.e+36
135 classob = where(classob.eq.0,classob@_FillValue,classob)
137 plot_name = "global_class_ob"
138 title = biome_name_ob
139 resg@tiMainString = title
141 wks = gsn_open_wks (plot_type,plot_name) ; open workstation
142 gsn_define_colormap(wks,"gui_default") ; choose colormap
144 plot = gsn_csm_contour_map_ce(wks,classob,resg)
148 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
149 "rm "+plot_name+"."+plot_type)
151 ;************************************************
152 ; plot global biome class: (2) model
153 ;************************************************
155 resg@cnMinLevelValF = 0. ; Min level
156 resg@cnMaxLevelValF = 16. ; Max level
157 resg@cnLevelSpacingF = 1. ; interval
159 plot_name = "global_class_model"
160 title = biome_name_mod
161 resg@tiMainString = title
163 wks = gsn_open_wks (plot_type,plot_name) ; open workstation
164 gsn_define_colormap(wks,"gui_default") ; choose colormap
166 plot = gsn_csm_contour_map_ce(wks,classmod,resg)
170 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
171 "rm "+plot_name+"."+plot_type)
173 ;*******************************************************************
174 ; for html table : all 3 components (Mean, Max, Phase)
175 ;*******************************************************************
177 ; column (not including header column)
179 component = (/"Mean","Max","Phase"/)
181 col_head = (/"observed",model_name,"M_score" \
182 ,"observed",model_name,"M_score" \
183 ,"observed",model_name,"M_score" \
186 n_comp = dimsizes(component)
187 ncol = dimsizes(col_head )
189 ; row (not including header row)
191 ;----------------------------------------------------
192 ; using model biome class:
193 row_head = (/"Not Vegetated" \
194 ,"Needleleaf Evergreen Temperate Tree" \
195 ,"Needleleaf Evergreen Boreal Tree" \
196 ; ,"Needleleaf Deciduous Boreal Tree" \
197 ,"Broadleaf Evergreen Tropical Tree" \
198 ,"Broadleaf Evergreen Temperate Tree" \
199 ,"Broadleaf Deciduous Tropical Tree" \
200 ,"Broadleaf Deciduous Temperate Tree" \
201 ; ,"Broadleaf Deciduous Boreal Tree" \
202 ; ,"Broadleaf Evergreen Shrub" \
203 ,"Broadleaf Deciduous Temperate Shrub" \
204 ,"Broadleaf Deciduous Boreal Shrub" \
206 ,"C3 Non-Arctic Grass" \
213 nrow = dimsizes(row_head)
215 ; arrays to be passed to table.
216 text = new ((/nrow, ncol/),string )
221 ;********************************************************************
222 ; use land-type class to bin the data in equally spaced ranges
223 ;********************************************************************
225 ; using model biome class
228 range = fspan(0,nclass,nclass+1)
230 ; Use this range information to grab all the values in a
231 ; particular range, and then take an average.
233 nx = dimsizes(range) - 1
235 ; for 2 data: model and observed
238 ; using model biome class
240 base = ndtooned(classmod)
244 yvalues = new((/data_n,nx/),float)
245 count = new((/data_n,nx/),float)
247 ;************************************************************************
248 ; go through all components
249 ;************************************************************************
259 data_ob = dim_avg_Wrap(laiob (lat|:,lon|:,time|:))
260 data_mod = dim_avg_Wrap(laimod(lat|:,lon|:,time|:))
268 data_ob = laiob(0,:,:)
269 data_ob@long_name = "Leaf Area Index Max"
273 data_ob(j,i) = max(laiob(:,j,i))
278 data_mod = laimod(0,:,:)
279 data_mod@long_name = "Leaf Area Index Max"
283 data_mod(j,i) = max(laimod(:,j,i))
294 data_ob = laiob(0,:,:)
295 data_ob@long_name = "Leaf Area Index Max Month"
299 data_ob(j,i) = maxind(laiob(:,j,i)) + 1
304 data_mod = laimod(0,:,:)
305 data_mod@long_name = "Leaf Area Index Max Month"
309 data_mod(j,i) = maxind(laimod(:,j,i)) + 1
315 ;==============================
317 ;==============================
319 ; Loop through each range, using base
323 if (i.ne.(nx-1)) then
324 idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
326 idx = ind(base.ge.range(i))
329 ; loop through each dataset
334 data = ndtooned(data_ob)
338 data = ndtooned(data_mod)
343 if (.not.any(ismissing(idx))) then
344 yvalues(n,i) = avg(data(idx))
345 count(n,i) = dimsizes(idx)
347 yvalues(n,i) = yvalues@_FillValue
351 ;#############################################################
352 ; using model biome class:
354 ; set the following 4 classes to _FillValue:
355 ; (3)Needleleaf Deciduous Boreal Tree,
356 ; (8)Broadleaf Deciduous Boreal Tree,
357 ; (9)Broadleaf Evergreen Shrub,
360 if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
361 yvalues(n,i) = yvalues@_FillValue
364 ;#############################################################
372 ;=====================================
373 ; compute correlation coef and M score
374 ;=====================================
383 good = ind(.not.ismissing(u) .and. .not.ismissing(v))
387 uu_count = u_count(good)
388 vv_count = v_count(good)
390 ; compute correlation coef
394 bias = avg(abs(vv-uu))
395 bias = where((bias.gt. 6.),12.-bias,bias)
396 Mscore = ((6. - bias)/6.)*score_max
398 bias = sum(abs(vv-uu)/abs(vv+uu))
399 Mscore = (1.- (bias/dimsizes(uu)))*score_max
402 M_score = sprintf("%.2f", Mscore)
406 M_total = M_total + Mscore
408 ;================================
409 ; output M_score to score sheet
410 ;===============================
412 if (isvar("compare")) then
413 system("sed -e '1,/M_lai_"+component(m)+"/s/M_lai_"+component(m)+"/"+M_score+"/' "+html_name2+" > "+html_new2+";"+ \
414 "mv -f "+html_new2+" "+html_name2)
417 system("sed s#M_lai_"+component(m)+"#"+M_score+"# "+html_name+" > "+html_new+";"+ \
418 "mv -f "+html_new+" "+html_name)
420 ;==============================
421 ; output M_score to html table
422 ;==============================
427 text(i,n) = sprintf("%.1f",uu(i))
428 text(i,n+1) = sprintf("%.1f",vv(i))
431 text(nrow-1,n) = sprintf("%.1f",sum(uu*uu_count)/sum(uu_count))
432 text(nrow-1,n+1) = sprintf("%.1f",sum(vv*vv_count)/sum(vv_count))
433 text(nrow-1,n+2) = M_score
445 ;========================================
446 ; global res changes for each component
447 ;========================================
451 resg@cnMinLevelValF = 0.
452 resg@cnMaxLevelValF = 10.
453 resg@cnLevelSpacingF = 1.
455 data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
459 resg@cnMinLevelValF = 0.
460 resg@cnMaxLevelValF = 10.
461 resg@cnLevelSpacingF = 1.
463 data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
467 resg@cnMinLevelValF = 1.
468 resg@cnMaxLevelValF = 12.
469 resg@cnLevelSpacingF = 1.
471 data_mod = where(ismissing(data_ob).or.(data_mod.lt.delta),data_mod@_FillValue,data_mod)
472 data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
476 ;=========================
477 ; global contour : ob
478 ;=========================
480 plot_name = "global_"+component(m)+"_ob"
482 resg@tiMainString = title
484 wks = gsn_open_wks (plot_type,plot_name) ; open workstation
485 gsn_define_colormap(wks,"gui_default") ; choose colormap
487 plot = gsn_csm_contour_map_ce(wks,data_ob,resg)
492 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
493 "rm "+plot_name+"."+plot_type)
495 ;============================
496 ; global contour : model
497 ;============================
499 plot_name = "global_"+component(m)+"_model"
500 title = "Model " + model_name
501 resg@tiMainString = title
503 wks = gsn_open_wks (plot_type,plot_name)
504 gsn_define_colormap(wks,"gui_default")
506 plot = gsn_csm_contour_map_ce(wks,data_mod,resg)
511 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
512 "rm "+plot_name+"."+plot_type)
514 ;================================
515 ; global contour: model vs ob
516 ;================================
518 plot_name = "global_"+component(m)+"_model_vs_ob"
520 wks = gsn_open_wks (plot_type,plot_name)
521 gsn_define_colormap(wks,"gui_default")
523 plot=new(3,graphic) ; create graphic array
525 resg@gsnFrame = False ; Do not draw plot
526 resg@gsnDraw = False ; Do not advance frame
528 ; plot correlation coef
531 gRes@txFontHeightF = 0.02
534 correlation_text = "(correlation coef = "+sprintf("%.2f", cc)+")"
536 gsn_text_ndc(wks,correlation_text,0.20,0.50,gRes)
541 resg@tiMainString = title
543 plot(0) = gsn_csm_contour_map_ce(wks,data_ob,resg)
547 title = "Model "+ model_name
548 resg@tiMainString = title
550 plot(1) = gsn_csm_contour_map_ce(wks,data_mod,resg)
555 resg@cnMinLevelValF = -2.
556 resg@cnMaxLevelValF = 2.
557 resg@cnLevelSpacingF = 0.4
561 resg@cnMinLevelValF = -6.
562 resg@cnMaxLevelValF = 6.
563 resg@cnLevelSpacingF = 1.
567 resg@cnMinLevelValF = -6.
568 resg@cnMaxLevelValF = 6.
569 resg@cnLevelSpacingF = 1.
573 zz = data_mod - data_ob
574 title = "Model_"+model_name+" - Observed"
575 resg@tiMainString = title
577 plot(2) = gsn_csm_contour_map_ce(wks,zz,resg)
581 pres = True ; panel plot mods desired
582 pres@gsnMaximize = True ; fill the page
584 gsn_panel(wks,plot,(/3,1/),pres) ; create panel plot
589 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
590 "rm "+plot_name+"."+plot_type)
595 resg@gsnFrame = True ; Do advance frame
596 resg@gsnDraw = True ; Do draw plot
600 ;**************************************************
602 ;**************************************************
603 output_html = "table_model_vs_ob.html"
605 header_text = "<H1>LAI: Model "+model_name+" vs. MODIS observations</H1>"
607 header = (/"<HTML>" \
609 ,"<TITLE>CLAMP metrics</TITLE>" \
616 "<table border=1 cellspacing=0 cellpadding=3 width=100%>" \
618 ," <th bgcolor=DDDDDD rowspan=2>Biome Class</th>" \
619 ," <th bgcolor=DDDDDD colspan=3>"+component(0)+" (m2/m2)</th>" \
620 ," <th bgcolor=DDDDDD colspan=3>"+component(1)+" (m2/m2)</th>" \
621 ," <th bgcolor=DDDDDD colspan=3>"+component(2)+" (months)</th>" \
622 ," <th bgcolor=DDDDDD rowspan=2>Annual Plot</th>" \
625 ," <th bgcolor=DDDDDD >observed</th>" \
626 ," <th bgcolor=DDDDDD >"+model_name+"</th>" \
627 ," <th bgcolor=DDDDDD >M_score</th>" \
628 ," <th bgcolor=DDDDDD >observed</th>" \
629 ," <th bgcolor=DDDDDD >"+model_name+"</th>" \
630 ," <th bgcolor=DDDDDD >M_score</th>" \
631 ," <th bgcolor=DDDDDD >observed</th>" \
632 ," <th bgcolor=DDDDDD >"+model_name+"</th>" \
633 ," <th bgcolor=DDDDDD >M_score</th>" \
636 table_footer = "</table>"
640 lines = new(50000,string)
643 set_line(lines,nline,header)
644 set_line(lines,nline,table_header)
645 ;-----------------------------------------------
649 set_line(lines,nline,row_header)
661 txt11 = "<a href=./annual_biome_"+n+".png>model_vs_ob</a>"
663 set_line(lines,nline,"<th>"+txt1+"</th>")
664 set_line(lines,nline,"<th>"+txt2+"</th>")
665 set_line(lines,nline,"<th>"+txt3+"</th>")
666 set_line(lines,nline,"<th>"+txt4+"</th>")
667 set_line(lines,nline,"<th>"+txt5+"</th>")
668 set_line(lines,nline,"<th>"+txt6+"</th>")
669 set_line(lines,nline,"<th>"+txt7+"</th>")
670 set_line(lines,nline,"<th>"+txt8+"</th>")
671 set_line(lines,nline,"<th>"+txt9+"</th>")
672 set_line(lines,nline,"<th>"+txt10+"</th>")
673 set_line(lines,nline,"<th>"+txt11+"</th>")
675 set_line(lines,nline,row_footer)
677 ;-----------------------------------------------
678 set_line(lines,nline,table_footer)
679 set_line(lines,nline,footer)
681 ; Now write to an HTML file
683 idx = ind(.not.ismissing(lines))
684 if(.not.any(ismissing(idx))) then
685 asciiwrite(output_html,lines(idx))
694 ;************************************************************************
695 ; go through all ntime
696 ;************************************************************************
698 ; for 2 data: model and observed
701 ; using model biome class
703 base = ndtooned(classmod)
707 yvalues = new((/data_n,nx,ntime/),float)
709 ;==============================
711 ;==============================
713 ; Loop through each range, using base
717 if (i.ne.(nx-1)) then
718 idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
720 idx = ind(base.ge.range(i))
723 ; loop through each dataset
730 data = ndtooned(laiob (m,:,:))
734 data = ndtooned(laimod(m,:,:))
739 if (.not.any(ismissing(idx))) then
740 yvalues(n,i,m) = avg(data(idx))
742 yvalues(n,i,m) = yvalues@_FillValue
745 ;#############################################################
746 ; using model biome class:
748 ; set the following 4 classes to _FillValue:
749 ; (3)Needleleaf Deciduous Boreal Tree,
750 ; (8)Broadleaf Deciduous Boreal Tree,
751 ; (9)Broadleaf Evergreen Shrub,
754 if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
755 yvalues(n,i,m) = yvalues@_FillValue
757 ;#############################################################
767 good = ind(.not.ismissing(yvalues(0,:,0)))
769 n_biome = dimsizes(good)
771 ;----------------------------------------------------------------
772 ; data for tseries plot
774 yvalues_g = new((/data_n,n_biome,ntime/),float)
778 yvalues_g = yvalues(:,good,:)
782 ;*******************************************************************
784 ;*******************************************************************
785 ; for x-axis in xyplot
787 mon@long_name = "month"
789 res = True ; plot mods desired
790 res@xyLineThicknesses = (/2.0,2.0,2.0/) ; make 2nd lines thicker
791 res@xyLineColors = (/"blue","red"/) ; change line color
793 ; res@tiMainFontHeightF = 0.025 ; size of title
795 ; Add a boxed legend using the more simple method
796 res@pmLegendDisplayMode = "Always"
797 ; res@pmLegendWidthF = 0.1
798 res@pmLegendWidthF = 0.08
799 res@pmLegendHeightF = 0.06
800 ; res@pmLegendOrthogonalPosF = -1.17
801 ; res@pmLegendOrthogonalPosF = -1.00 ;(downward)
802 res@pmLegendOrthogonalPosF = -0.30 ;(downward)
804 ; res@pmLegendParallelPosF = 0.18
805 res@pmLegendParallelPosF = 0.23 ;(rightward)
807 ; res@lgPerimOn = False
808 res@lgLabelFontHeightF = 0.015
809 res@xyExplicitLegendLabels = (/"observed",model_name/)
811 ;************************************************************
813 plot_data = new((/2,ntime/),float)
814 plot_data@long_name = "Leaf Area Index"
816 ;----------------------------------------------
817 ; time series plot : per biome
821 plot_name = "annual_biome_"+ m
823 wks = gsn_open_wks (plot_type,plot_name)
825 title = "LAI : "+ row_head(m)
826 res@tiMainString = title
828 plot_data(0,:) = yvalues_g(0,m,:)
829 plot_data(1,:) = yvalues_g(1,m,:)
831 plot = gsn_csm_xy(wks,mon,plot_data,res)
836 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
837 "rm "+plot_name+"."+plot_type)
840 ;--------------------------------------------
841 ; time series plot: all biome
843 plot_name = "annual_biome_"+ n_biome
845 wks = gsn_open_wks (plot_type,plot_name)
847 title = "LAI : "+ row_head(n_biome)
848 res@tiMainString = title
851 plot_data(0,k) = avg(yvalues_g(0,:,k))
852 plot_data(1,k) = avg(yvalues_g(1,:,k))
855 plot = gsn_csm_xy(wks,mon,plot_data,res)
860 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
861 "rm "+plot_name+"."+plot_type)
865 ;***************************************************************************
866 ; write total score to file
867 ;***************************************************************************
869 asciiwrite("M_save.lai", M_total)
871 ;***************************************************************************
873 ;***************************************************************************
874 output_dir = model_name+"/lai"
876 system("mv *.png *.html " + output_dir)
877 ;***************************************************************************