all/02.lai.ncl
author Forrest Hoffman <forrest@climatemodeling.org>
Thu, 26 Mar 2009 14:02:21 -0400
changeset 1 4be95183fbcd
parent 0 0c6405ab2ff4
permissions -rw-r--r--
Modifications to scoring and graphics production for the final version of code for the C-LAMP paper in GCB.
     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) 
     7 begin
     8 ; add line to ascci/html file
     9     
    10   nnewlines = dimsizes(newlines)
    11   if(nline+nnewlines-1.ge.dimsizes(lines))
    12     print("set_line: bad index, not setting anything.") 
    13     return
    14   end if 
    15   lines(nline:nline+nnewlines-1) = newlines
    16 ;  print ("lines = " + lines(nline:nline+nnewlines-1))
    17   nline = nline + nnewlines
    18   return 
    19 end
    20 ;**************************************************************
    21 ; Main code.
    22 begin
    23  
    24   plot_type     = "ps"
    25   plot_type_new = "png"
    26 
    27 ;-----------------------------------------------------
    28 ; edit table.html of current model for movel1_vs_model2
    29 
    30   if (isvar("compare")) then
    31      html_name2 = compare+"/table.html"  
    32      html_new2  = html_name2 +".new"
    33   end if
    34 
    35 ;------------------------------------------------------
    36 ; edit table.html for current model
    37 
    38   html_name = model_name+"/table.html"  
    39   html_new  = html_name +".new"
    40 
    41 ;------------------------------------------------------
    42 ; read data: model       
    43 
    44   fm = addfile(dirm+film10,"r")      
    45   laimod = fm->TLAI
    46 
    47   delete (fm)
    48 
    49   dsizes = dimsizes(laimod)
    50   ntime  = dsizes(0)
    51   nlat   = dsizes(1)
    52   nlon   = dsizes(2)
    53 
    54 ;-----------------------------------
    55 ; get landfrac data
    56 
    57  film_l   = "lnd_"+ model_grid + ".nc"
    58  fm_l     = addfile (dirs+film_l,"r")  
    59  landfrac = fm_l->landfrac
    60 
    61  delete (fm_l)
    62 ;----------------------------------
    63 ; read biome data: model
    64 
    65   biome_name_mod = "Model PFT Class"
    66 
    67   film_c   = "class_pft_"+model_grid+".nc"
    68   fm_c     = addfile(dirs+film_c,"r") 
    69   classmod = fm_c->CLASS_PFT               
    70 
    71   delete (fm_c)
    72 
    73 ; model data has 17 land-type classes
    74   nclass_mod = 17
    75 
    76 ;----------------------------------------------------------
    77 ; read data: ob       
    78 
    79 ;----------------------------------
    80 ; read biome data: observed
    81 
    82   biome_name_ob = "MODIS LandCover"
    83 
    84   dir_c = diro + "lai/"
    85   filo_c = "land_class_"+model_grid+".nc"
    86   fo = addfile(dir_c+filo_c,"r")
    87   classob = tofloat(fo->LAND_CLASS)               
    88 
    89   delete (fo)
    90 
    91 ; input observed data has 20 land-type classes
    92   nclass_ob = 20
    93 
    94 ;---------------------------------
    95 ; read lai data: observed
    96 
    97   ;ob_name = "MODIS MOD15A2 (2000-2005)"
    98   ob_name = "MODIS MOD15A2 (2000-2004)"
    99 
   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")                
   104   laiob = fo->LAI
   105 
   106   delete (fo)
   107 
   108 ;-------------------------------------------------
   109 ; take into account landfrac
   110 
   111   laimod = laimod * conform(laimod,landfrac,(/1,2/))
   112   laiob  = laiob  * conform(laiob ,landfrac,(/1,2/))
   113 
   114   delete (landfrac)
   115 
   116 ;************************************************
   117 ; global res
   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
   125       
   126 ;************************************************
   127 ; plot global biome class: (1) observed
   128 ;************************************************
   129 
   130   resg@cnMinLevelValF       = 1.              ; Min level
   131   resg@cnMaxLevelValF       = 19.             ; Max level
   132   resg@cnLevelSpacingF      = 1.              ; interval
   133 
   134   classob@_FillValue = 1.e+36
   135   classob = where(classob.eq.0,classob@_FillValue,classob)
   136   
   137   plot_name = "global_class_ob"
   138   title     = biome_name_ob
   139   resg@tiMainString  = title
   140 
   141   wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
   142   gsn_define_colormap(wks,"gui_default")     ; choose colormap
   143 
   144   plot = gsn_csm_contour_map_ce(wks,classob,resg)   
   145 
   146   delete (wks)
   147 
   148   system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   149          "rm "+plot_name+"."+plot_type)
   150  
   151 ;************************************************
   152 ; plot global biome class: (2) model
   153 ;************************************************
   154 
   155   resg@cnMinLevelValF       = 0.              ; Min level
   156   resg@cnMaxLevelValF       = 16.             ; Max level
   157   resg@cnLevelSpacingF      = 1.              ; interval
   158   
   159   plot_name = "global_class_model"
   160   title     = biome_name_mod
   161   resg@tiMainString  = title
   162 
   163   wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
   164   gsn_define_colormap(wks,"gui_default")     ; choose colormap
   165 
   166   plot = gsn_csm_contour_map_ce(wks,classmod,resg)   
   167 
   168   delete (wks)
   169 
   170   system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   171          "rm "+plot_name+"."+plot_type)
   172  
   173 ;*******************************************************************
   174 ; for html table : all 3 components (Mean, Max, Phase)
   175 ;*******************************************************************
   176 
   177 ; column (not including header column)
   178 
   179   component = (/"Mean","Max","Phase"/)
   180 
   181   col_head  = (/"observed",model_name,"M_score" \
   182                ,"observed",model_name,"M_score" \
   183                ,"observed",model_name,"M_score" \
   184                /)
   185   
   186   n_comp = dimsizes(component) 
   187   ncol   = dimsizes(col_head )
   188 
   189 ; row (not including header row)
   190 
   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" \
   205                ,"C3 Arctic Grass" \
   206                ,"C3 Non-Arctic Grass" \
   207                ,"C4 Grass" \
   208                ,"Corn" \
   209 ;              ,"Wheat" \                      
   210                ,"All Biomes" \                
   211                /)  
   212   
   213   nrow = dimsizes(row_head)                  
   214 
   215 ; arrays to be passed to table. 
   216   text = new ((/nrow, ncol/),string ) 
   217 
   218 ; total M_score
   219   M_total = 0.
   220 
   221 ;********************************************************************
   222 ; use land-type class to bin the data in equally spaced ranges
   223 ;********************************************************************
   224 
   225 ; using model biome class
   226   nclass = nclass_mod
   227 
   228   range  = fspan(0,nclass,nclass+1)
   229 
   230 ; Use this range information to grab all the values in a
   231 ; particular range, and then take an average.
   232 
   233   nx = dimsizes(range) - 1
   234 
   235 ; for 2 data: model and observed
   236   data_n = 2
   237 
   238 ; using model biome class
   239 
   240   base = ndtooned(classmod)
   241 
   242 ; output
   243 
   244   yvalues = new((/data_n,nx/),float)
   245   count   = new((/data_n,nx/),float)
   246 
   247 ;************************************************************************
   248 ; go through all components
   249 ;************************************************************************
   250 
   251  do m = 0,n_comp-1
   252 
   253 ;===================
   254 ; get data:
   255 ;===================
   256 ; (A) Mean
   257  
   258   if (m .eq. 0) then
   259      data_ob  = dim_avg_Wrap(laiob (lat|:,lon|:,time|:))
   260      data_mod = dim_avg_Wrap(laimod(lat|:,lon|:,time|:))
   261      score_max = 4.0
   262   end if
   263 
   264 ; (B) Max
   265 
   266   if (m .eq. 1) then
   267 
   268 ;    observed  
   269      data_ob = laiob(0,:,:)
   270      data_ob@long_name = "Leaf Area Index Max"
   271   
   272      do j = 0,nlat-1
   273      do i = 0,nlon-1
   274         data_ob(j,i) = max(laiob(:,j,i))
   275      end do
   276      end do          
   277 
   278 ;    model  
   279      data_mod = laimod(0,:,:)
   280      data_mod@long_name = "Leaf Area Index Max"
   281   
   282      do j = 0,nlat-1
   283      do i = 0,nlon-1
   284         data_mod(j,i) = max(laimod(:,j,i))
   285      end do
   286      end do
   287 
   288      score_max = 5.0
   289           
   290   end if
   291 
   292 ; (C) phase
   293 
   294   if (m .eq. 2) then  
   295 
   296 ;    observed
   297      data_ob = laiob(0,:,:)
   298      data_ob@long_name = "Leaf Area Index Max Month"
   299   
   300      do j = 0,nlat-1
   301      do i = 0,nlon-1 
   302         data_ob(j,i) = maxind(laiob(:,j,i)) + 1
   303      end do
   304      end do          
   305 
   306 ;    model
   307      data_mod = laimod(0,:,:)
   308      data_mod@long_name = "Leaf Area Index Max Month"
   309   
   310      do j = 0,nlat-1
   311      do i = 0,nlon-1 
   312         data_mod(j,i) = maxind(laimod(:,j,i)) + 1
   313      end do
   314      end do
   315 
   316      score_max = 6.0
   317         
   318   end if
   319 
   320 ;==============================
   321 ; put data into bins
   322 ;==============================
   323 
   324 ; Loop through each range, using base
   325 
   326   do i=0,nx-1
   327 
   328      if (i.ne.(nx-1)) then
   329         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
   330      else
   331         idx = ind(base.ge.range(i))
   332      end if
   333 
   334 ;    loop through each dataset
   335  
   336      do n = 0,data_n-1
   337 
   338         if (n .eq. 0) then
   339            data = ndtooned(data_ob)
   340         end if
   341 
   342         if (n .eq. 1) then
   343            data = ndtooned(data_mod)
   344         end if
   345 
   346 ;       Calculate average 
   347 
   348         if (.not.any(ismissing(idx))) then
   349            yvalues(n,i) = avg(data(idx))
   350            count(n,i)   = dimsizes(idx)
   351         else
   352            yvalues(n,i) = yvalues@_FillValue
   353            count(n,i)   = 0
   354         end if
   355 
   356 ;#############################################################
   357 ; using model biome class:
   358 ;
   359 ;     set the following 4 classes to _FillValue:
   360 ;     (3)Needleleaf Deciduous Boreal Tree,
   361 ;     (8)Broadleaf Deciduous Boreal Tree,
   362 ;     (9)Broadleaf Evergreen Shrub,
   363 ;     (16)Wheat
   364 
   365       if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   366          yvalues(n,i) = yvalues@_FillValue
   367          count(n,i)   = 0
   368       end if
   369 ;############################################################# 
   370 
   371       delete(data)
   372     end do                 ; n-loop
   373 
   374     delete(idx)
   375   end do                   ; i-loop
   376 
   377 ;=====================================
   378 ; compute correlation coef and M score 
   379 ;=====================================
   380 
   381   ; FMH: Max scores are depend on which component is being analyzed, so this
   382   ; is now set in each if statement above.
   383   ;score_max = 5.0
   384 
   385   u       = yvalues(0,:)
   386   v       = yvalues(1,:)
   387   u_count = count(0,:)
   388   v_count = count(1,:)
   389 
   390   good = ind(.not.ismissing(u) .and. .not.ismissing(v))
   391  
   392   uu = u(good)
   393   vv = v(good)
   394   uu_count = u_count(good)
   395   vv_count = v_count(good)
   396 
   397 ; compute correlation coef
   398   cc = esccr(uu,vv,0)
   399 
   400   if (n .eq. 2) then
   401      bias   = avg(abs(vv-uu))
   402      bias   = where((bias.gt. 6.),12.-bias,bias)
   403      Mscore = ((6. - bias)/6.)*score_max
   404   else
   405      bias  = sum(abs(vv-uu)/abs(vv+uu))
   406      Mscore = (1.- (bias/dimsizes(uu)))*score_max
   407   end if
   408 
   409   M_score = sprintf("%.2f", Mscore)
   410 
   411 ; compute M_total
   412   
   413   M_total = M_total + Mscore
   414 
   415 ;================================
   416 ; output M_score to score sheet
   417 ;===============================
   418 
   419   if (isvar("compare")) then
   420      system("sed -e '1,/M_lai_"+component(m)+"/s/M_lai_"+component(m)+"/"+M_score+"/' "+html_name2+" > "+html_new2+";"+ \
   421             "mv -f "+html_new2+" "+html_name2)
   422   end if
   423 
   424   system("sed s#M_lai_"+component(m)+"#"+M_score+"# "+html_name+" > "+html_new+";"+ \
   425          "mv -f "+html_new+" "+html_name)
   426 
   427 ;==============================
   428 ; output M_score to html table
   429 ;==============================
   430 
   431   n = m*3
   432 
   433   do i=0,nrow-2
   434      text(i,n)   = sprintf("%.1f",uu(i))
   435      text(i,n+1) = sprintf("%.1f",vv(i))
   436      text(i,n+2) = "-"
   437   end do
   438   text(nrow-1,n)   = sprintf("%.1f",sum(uu*uu_count)/sum(uu_count))
   439   text(nrow-1,n+1) = sprintf("%.1f",sum(vv*vv_count)/sum(vv_count))
   440   text(nrow-1,n+2) = M_score
   441 
   442   delete (u)
   443   delete (v)
   444   delete (uu)
   445   delete (vv)
   446   delete (u_count)
   447   delete (v_count)
   448   delete (uu_count)
   449   delete (vv_count)
   450   delete (good)
   451 
   452 ;======================================== 
   453 ; global res changes for each component
   454 ;========================================
   455   delta = 0.00001  
   456 
   457   if (m .eq. 0) then
   458      resg@cnMinLevelValF       = 0.             
   459      resg@cnMaxLevelValF       = 10.             
   460      resg@cnLevelSpacingF      = 1.
   461 
   462      data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
   463   end if
   464 
   465   if (m .eq. 1) then
   466      resg@cnMinLevelValF       = 0.             
   467      resg@cnMaxLevelValF       = 10.             
   468      resg@cnLevelSpacingF      = 1.
   469 
   470      data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
   471   end if
   472 
   473   if (m .eq. 2) then
   474      resg@cnMinLevelValF       = 1.             
   475      resg@cnMaxLevelValF       = 12.             
   476      resg@cnLevelSpacingF      = 1.
   477 
   478      data_mod = where(ismissing(data_ob).or.(data_mod.lt.delta),data_mod@_FillValue,data_mod)
   479      data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
   480 
   481   end if
   482 
   483 ;=========================
   484 ; global contour : ob
   485 ;=========================
   486   
   487   plot_name = "global_"+component(m)+"_ob"
   488   title     = ob_name
   489   resg@tiMainString  = title
   490 
   491   wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
   492   gsn_define_colormap(wks,"gui_default")     ; choose colormap
   493 
   494   plot = gsn_csm_contour_map_ce(wks,data_ob,resg)   
   495 
   496   delete (wks)
   497   delete (plot)
   498 
   499   system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   500          "rm "+plot_name+"."+plot_type)
   501   ;system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
   502   
   503 
   504 ;============================
   505 ; global contour : model
   506 ;============================
   507 
   508   plot_name = "global_"+component(m)+"_model"  
   509   title     = "Model " + model_name + " (2000-2004)"
   510   resg@tiMainString  = title
   511 
   512   wks = gsn_open_wks (plot_type,plot_name)
   513   gsn_define_colormap(wks,"gui_default")     
   514 
   515   plot = gsn_csm_contour_map_ce(wks,data_mod,resg)   
   516 
   517   delete (wks)
   518   delete (plot)
   519 
   520   system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   521          "rm "+plot_name+"."+plot_type)
   522   ;system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
   523 
   524 ;================================
   525 ; global contour: model vs ob
   526 ;================================
   527 
   528   plot_name = "global_"+component(m)+"_model_vs_ob"
   529 
   530   wks = gsn_open_wks (plot_type,plot_name)   
   531   gsn_define_colormap(wks,"gui_default")     
   532 
   533   plot=new(3,graphic)                        ; create graphic array
   534 
   535   resg@gsnFrame             = False          ; Do not draw plot 
   536   resg@gsnDraw              = False          ; Do not advance frame
   537 
   538 ; plot correlation coef
   539 
   540   gRes               = True
   541   gRes@txFontHeightF = 0.02
   542   gRes@txAngleF      = 90
   543 
   544   correlation_text = "(correlation coef = "+sprintf("%.2f", cc)+")"
   545 
   546   gsn_text_ndc(wks,correlation_text,0.20,0.50,gRes)
   547   
   548 ; plot ob
   549 
   550   title     = ob_name
   551   resg@tiMainString  = title
   552 
   553   plot(0) = gsn_csm_contour_map_ce(wks,data_ob,resg)       
   554 
   555 ; plot model
   556 
   557   title     = "Model "+ model_name + " (2000-2004)"
   558   resg@tiMainString  = title
   559 
   560   plot(1) = gsn_csm_contour_map_ce(wks,data_mod,resg) 
   561 
   562 ; plot model-ob
   563 
   564   if (m .eq. 0) then
   565      resg@cnMinLevelValF  = -2.           
   566      resg@cnMaxLevelValF  =  2.            
   567      resg@cnLevelSpacingF =  0.4
   568   end if
   569 
   570   if (m .eq. 1) then
   571      resg@cnMinLevelValF  = -6.           
   572      resg@cnMaxLevelValF  =  6.            
   573      resg@cnLevelSpacingF =  1.
   574   end if
   575 
   576   if (m .eq. 2) then
   577      resg@cnMinLevelValF  = -6.           
   578      resg@cnMaxLevelValF  =  6.            
   579      resg@cnLevelSpacingF =  1.
   580   end if
   581 
   582   zz = data_mod
   583   zz = data_mod - data_ob
   584   title = "Model_"+model_name+" - MODIS MOD15A2"
   585   resg@tiMainString    = title
   586 
   587   plot(2) = gsn_csm_contour_map_ce(wks,zz,resg) 
   588 
   589 ; plot panel
   590 
   591   pres                            = True        ; panel plot mods desired
   592   pres@gsnMaximize                = True        ; fill the page
   593 
   594   gsn_panel(wks,plot,(/3,1/),pres)              ; create panel plot
   595 
   596   delete (wks)
   597   delete (plot)
   598 
   599   system("convert -trim -density 150 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   600          "rm "+plot_name+"."+plot_type)
   601   ;system("convert -trim -density 150 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
   602 
   603   delete (data_ob)
   604   delete (data_mod)
   605 
   606   resg@gsnFrame             = True          ; Do advance frame 
   607   resg@gsnDraw              = True          ; Do draw plot
   608 
   609  end do    ; m-loop
   610 
   611 ;**************************************************
   612 ; html table
   613 ;**************************************************
   614   output_html = "table_model_vs_ob.html"
   615 
   616   header_text = "<H1>LAI: Model "+model_name+" vs. MODIS observations</H1>" 
   617 
   618   header = (/"<HTML>" \
   619             ,"<HEAD>" \
   620             ,"<TITLE>CLAMP metrics</TITLE>" \
   621             ,"</HEAD>" \
   622             ,header_text \
   623             /) 
   624   footer = "</HTML>"
   625 
   626   table_header = (/ \
   627         "<table border=1 cellspacing=0 cellpadding=3 width=100%>" \
   628        ,"<tr>" \
   629        ,"   <th bgcolor=DDDDDD rowspan=2>Biome Class</th>" \
   630        ,"   <th bgcolor=DDDDDD colspan=3>"+component(0)+" (m2/m2)</th>" \
   631        ,"   <th bgcolor=DDDDDD colspan=3>"+component(1)+" (m2/m2)</th>" \
   632        ,"   <th bgcolor=DDDDDD colspan=3>"+component(2)+" (months)</th>" \
   633        ,"   <th bgcolor=DDDDDD rowspan=2>Annual Plot</th>" \
   634        ,"</tr>" \
   635        ,"<tr>" \
   636        ,"   <th bgcolor=DDDDDD >observed</th>" \
   637        ,"   <th bgcolor=DDDDDD >"+model_name+"</th>" \
   638        ,"   <th bgcolor=DDDDDD >M_score</th>" \
   639        ,"   <th bgcolor=DDDDDD >observed</th>" \
   640        ,"   <th bgcolor=DDDDDD >"+model_name+"</th>" \
   641        ,"   <th bgcolor=DDDDDD >M_score</th>" \
   642        ,"   <th bgcolor=DDDDDD >observed</th>" \
   643        ,"   <th bgcolor=DDDDDD >"+model_name+"</th>" \
   644        ,"   <th bgcolor=DDDDDD >M_score</th>" \
   645        ,"</tr>" \
   646        /)
   647   table_footer = "</table>"
   648   row_header = "<tr>"
   649   row_footer = "</tr>"
   650 
   651   lines = new(50000,string)
   652   nline = 0
   653 
   654   set_line(lines,nline,header)
   655   set_line(lines,nline,table_header)
   656 ;-----------------------------------------------
   657 ;row of table
   658 
   659   do n = 0,nrow-1
   660      set_line(lines,nline,row_header)
   661 
   662      txt1  = row_head(n)
   663      txt2  = text(n,0)
   664      txt3  = text(n,1)
   665      txt4  = text(n,2)
   666      txt5  = text(n,3)
   667      txt6  = text(n,4)
   668      txt7  = text(n,5)
   669      txt8  = text(n,6)
   670      txt9  = text(n,7)
   671      txt10 = text(n,8)
   672      txt11 = "<a href=./annual_biome_"+n+".png>model_vs_ob</a>" 
   673 
   674      set_line(lines,nline,"<th>"+txt1+"</th>")
   675      set_line(lines,nline,"<th>"+txt2+"</th>")
   676      set_line(lines,nline,"<th>"+txt3+"</th>")
   677      set_line(lines,nline,"<th>"+txt4+"</th>")
   678      set_line(lines,nline,"<th>"+txt5+"</th>")
   679      set_line(lines,nline,"<th>"+txt6+"</th>")
   680      set_line(lines,nline,"<th>"+txt7+"</th>")
   681      set_line(lines,nline,"<th>"+txt8+"</th>")
   682      set_line(lines,nline,"<th>"+txt9+"</th>")
   683      set_line(lines,nline,"<th>"+txt10+"</th>")
   684      set_line(lines,nline,"<th>"+txt11+"</th>")
   685 
   686      set_line(lines,nline,row_footer)
   687   end do
   688 ;-----------------------------------------------
   689   set_line(lines,nline,table_footer)
   690   set_line(lines,nline,footer) 
   691 
   692 ; Now write to an HTML file
   693 
   694   idx = ind(.not.ismissing(lines))
   695   if(.not.any(ismissing(idx))) then
   696     asciiwrite(output_html,lines(idx))
   697   else
   698    print ("error?")
   699   end if
   700 
   701   delete (yvalues)
   702   delete (count)
   703   delete (idx)
   704 
   705 ;************************************************************************
   706 ; go through all ntime
   707 ;************************************************************************
   708 
   709 ; for 2 data: model and observed
   710   data_n = 2
   711 
   712 ; using model biome class
   713 
   714   base = ndtooned(classmod)
   715 
   716 ; output
   717 
   718   yvalues = new((/data_n,nx,ntime/),float)
   719 
   720 ;==============================
   721 ; put data into bins
   722 ;==============================
   723 
   724 ; Loop through each range, using base
   725 
   726   do i=0,nx-1
   727 
   728      if (i.ne.(nx-1)) then
   729         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
   730      else
   731         idx = ind(base.ge.range(i))
   732      end if
   733 
   734 ;    loop through each dataset
   735  
   736      do n = 0,data_n-1
   737 
   738         do m = 0,ntime-1
   739 
   740         if (n .eq. 0) then
   741            data = ndtooned(laiob (m,:,:))
   742         end if
   743 
   744         if (n .eq. 1) then
   745            data = ndtooned(laimod(m,:,:))
   746         end if
   747 
   748 ;       Calculate average 
   749 
   750         if (.not.any(ismissing(idx))) then
   751            yvalues(n,i,m) = avg(data(idx))
   752         else
   753            yvalues(n,i,m) = yvalues@_FillValue
   754         end if
   755 
   756 ;#############################################################
   757 ; using model biome class:
   758 ;
   759 ;     set the following 4 classes to _FillValue:
   760 ;     (3)Needleleaf Deciduous Boreal Tree,
   761 ;     (8)Broadleaf Deciduous Boreal Tree,
   762 ;     (9)Broadleaf Evergreen Shrub,
   763 ;     (16)Wheat
   764 
   765       if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   766          yvalues(n,i,m) = yvalues@_FillValue
   767       end if
   768 ;############################################################# 
   769 
   770       end do               ; m-loop
   771 
   772       delete(data)
   773     end do                 ; n-loop
   774 
   775     delete(idx)
   776   end do                   ; i-loop
   777 
   778   good = ind(.not.ismissing(yvalues(0,:,0)))  
   779 
   780   n_biome = dimsizes(good)
   781 
   782 ;----------------------------------------------------------------
   783 ; data for tseries plot
   784 
   785   yvalues_g = new((/data_n,n_biome,ntime/),float)
   786 
   787   yvalues_g@units = ""
   788 
   789   yvalues_g = yvalues(:,good,:)
   790 
   791   delete (good)
   792 
   793 ;*******************************************************************
   794 ; res for line plot
   795 ;*******************************************************************
   796 ; for x-axis in xyplot
   797   mon = ispan(1,12,1)
   798   mon@long_name = "month"
   799 
   800   res                   = True                      ; plot mods desired
   801   res@xyLineThicknesses = (/2.0,2.0,2.0/)           ; make 2nd lines thicker
   802   res@xyLineColors      = (/"blue","red"/)         ; change line color
   803 
   804 ; res@tiMainFontHeightF = 0.025                     ; size of title 
   805 
   806 ; Add a boxed legend using the more simple method
   807   res@pmLegendDisplayMode    = "Always"
   808 ; res@pmLegendWidthF         = 0.1
   809   res@pmLegendWidthF         = 0.08
   810   res@pmLegendHeightF        = 0.06
   811 ; res@pmLegendOrthogonalPosF = -1.17
   812 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
   813   res@pmLegendOrthogonalPosF = -0.30  ;(downward)
   814 
   815 ; res@pmLegendParallelPosF   =  0.18
   816   res@pmLegendParallelPosF   =  0.23  ;(rightward)
   817 
   818 ; res@lgPerimOn             = False
   819   res@lgLabelFontHeightF     = 0.015
   820   res@xyExplicitLegendLabels = (/"observed",model_name/)
   821 
   822 ;************************************************************
   823 
   824   plot_data = new((/2,ntime/),float)
   825   plot_data@long_name = "Leaf Area Index"
   826 
   827 ;----------------------------------------------
   828 ; time series plot : per biome
   829  
   830   do m = 0, n_biome-1
   831 
   832      plot_name = "annual_biome_"+ m
   833 
   834      wks = gsn_open_wks (plot_type,plot_name) 
   835 
   836      title = "LAI : "+ row_head(m)
   837      res@tiMainString = title
   838 
   839      plot_data(0,:) = yvalues_g(0,m,:)
   840      plot_data(1,:) = yvalues_g(1,m,:)
   841                                   
   842      plot = gsn_csm_xy(wks,mon,plot_data,res)
   843 
   844      delete (wks)
   845      delete (plot)
   846 
   847      system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   848             "rm "+plot_name+"."+plot_type)
   849   end do
   850 
   851 ;--------------------------------------------
   852 ; time series plot: all biome
   853 
   854      plot_name = "annual_biome_"+ n_biome
   855 
   856      wks = gsn_open_wks (plot_type,plot_name)   
   857 
   858      title = "LAI : "+ row_head(n_biome)
   859      res@tiMainString = title
   860 
   861      do k = 0,ntime-1
   862         plot_data(0,k) = avg(yvalues_g(0,:,k))
   863         plot_data(1,k) = avg(yvalues_g(1,:,k))
   864      end do
   865                                   
   866      plot = gsn_csm_xy(wks,mon,plot_data,res)
   867 
   868      delete (wks)
   869      delete (plot)
   870 
   871      system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   872             "rm "+plot_name+"."+plot_type)
   873 
   874   delete (plot_data)
   875 
   876 ;***************************************************************************
   877 ; write total score to file
   878 ;***************************************************************************
   879 
   880   asciiwrite("M_save.lai", M_total)
   881 
   882 ;***************************************************************************
   883 ; output plots
   884 ;***************************************************************************
   885   output_dir = model_name+"/lai"
   886 
   887   system("mv *.png *.html " + output_dir) 
   888 ;***************************************************************************
   889 end
   890