all/09x.carbon_sink.ncl
changeset 0 0c6405ab2ff4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/all/09x.carbon_sink.ncl	Mon Jan 26 22:08:20 2009 -0500
     1.3 @@ -0,0 +1,890 @@
     1.4 +;**************************************************************
     1.5 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
     1.6 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
     1.7 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
     1.8 +;**************************************************************
     1.9 +procedure set_line(lines:string,nline:integer,newlines:string) 
    1.10 +begin
    1.11 +; add line to ascci/html file
    1.12 +    
    1.13 +  nnewlines = dimsizes(newlines)
    1.14 +  if(nline+nnewlines-1.ge.dimsizes(lines))
    1.15 +    print("set_line: bad index, not setting anything.") 
    1.16 +    return
    1.17 +  end if 
    1.18 +  lines(nline:nline+nnewlines-1) = newlines
    1.19 +;  print ("lines = " + lines(nline:nline+nnewlines-1))
    1.20 +  nline = nline + nnewlines
    1.21 +  return 
    1.22 +end
    1.23 +;**************************************************************
    1.24 +; Main code.
    1.25 +begin
    1.26 + 
    1.27 + plot_type     = "ps"
    1.28 + plot_type_new = "png"
    1.29 +
    1.30 +;----------------------------------------------------------
    1.31 +; edit current model for movel1_vs_model2
    1.32 +
    1.33 + if (isvar("compare")) then
    1.34 +    html_name2 = compare+"/table.html"  
    1.35 +    html_new2  = html_name2 +".new"
    1.36 + end if
    1.37 +
    1.38 +;----------------------------------------------------------
    1.39 +; edit table.html for current model
    1.40 +
    1.41 + html_name = model_name+"/table.html"  
    1.42 + html_new  = html_name +".new"
    1.43 +
    1.44 +;----------------------------------------------------------
    1.45 +; get biome data: model
    1.46 +  
    1.47 +  biome_name_mod = "Model PFT Class"
    1.48 +
    1.49 +  film_c   = "class_pft_"+ model_grid +".nc"
    1.50 +  fm_c     = addfile (dirs+film_c,"r") 
    1.51 +  classmod = fm_c->CLASS_PFT
    1.52 +
    1.53 +  delete (fm_c)
    1.54 +
    1.55 +; model data has 17 land-type classes
    1.56 +  nclass_mod = 17
    1.57 +
    1.58 +;------------------------------------
    1.59 +; get model landfrac and area
    1.60 + 
    1.61 + film_l   = "lnd_"+ model_grid +".nc"
    1.62 + fm_l     = addfile (dirs+film_l,"r")  
    1.63 + landfrac = fm_l->landfrac
    1.64 + area     = fm_l->area
    1.65 +
    1.66 + delete (fm_l)
    1.67 +
    1.68 +; change area from km**2 to m**2
    1.69 + area = area * 1.e6             
    1.70 +
    1.71 +;-------------------------------------
    1.72 +; take into account landfrac
    1.73 +
    1.74 +; area = area * landfrac
    1.75 +
    1.76 +; delete (landfrac)
    1.77 +
    1.78 +;---------------------------------------------------
    1.79 +; read data: model, group 1
    1.80 +
    1.81 + fm      = addfile (dirm+film4,"r")
    1.82 + 
    1.83 + NPP1    = fm->NPP
    1.84 +
    1.85 + leafc   = fm->LEAFC
    1.86 + woodc   = fm->WOODC
    1.87 + frootc  = fm->FROOTC
    1.88 + VegC    = leafc
    1.89 + VegC    = leafc + woodc + frootc
    1.90 +
    1.91 + litterc = fm->LITTERC
    1.92 + cwdc    = fm->CWDC
    1.93 + LiCwC   = litterc
    1.94 + LiCwC   = litterc + cwdc
    1.95 +
    1.96 + SoilC   = fm->SOILC
    1.97 +
    1.98 + delete (fm)
    1.99 +;--------------------------------------------------- 
   1.100 +; read data: model, group 2
   1.101 +
   1.102 + fm     = addfile (dirm+film5,"r")
   1.103 +
   1.104 + NPP2   = fm->NPP 
   1.105 + NEE2   = fm->NEE
   1.106 + GPP2   = fm->GPP 
   1.107 +
   1.108 + delete (fm)
   1.109 +;---------------------------------------------------
   1.110 +; Units for these variables are:
   1.111 +
   1.112 +;NPP1: g C/m^2/s
   1.113 +;NPP2: g C/m^2/s
   1.114 +;NEE2: g C/m^2/s
   1.115 +;GPP2: g C/m^2/s
   1.116 +
   1.117 +;VegC:  g C/m^2
   1.118 +;LiCwC: g C/m^2
   1.119 +;SoilC: g C/m^2
   1.120 +
   1.121 + nsec_per_year = 60*60*24*365
   1.122 +
   1.123 +; change unit to g C/m^2/year
   1.124 +
   1.125 + NPP1 = NPP1 *  nsec_per_year * conform(NPP1,landfrac,(/1,2/))
   1.126 + NPP2 = NPP2 *  nsec_per_year * conform(NPP2,landfrac,(/1,2/))
   1.127 + NEE2 = NEE2 *  nsec_per_year * conform(NEE2,landfrac,(/1,2/))
   1.128 + GPP2 = GPP2 *  nsec_per_year * conform(GPP2,landfrac,(/1,2/))
   1.129 +
   1.130 + VegC  = VegC  * conform(VegC,landfrac,(/1,2/))
   1.131 + LiCwC = LiCwC * conform(LiCwC,landfrac,(/1,2/))
   1.132 + SoilC = SoilC * conform(SoilC,landfrac,(/1,2/))
   1.133 +
   1.134 + data_n = 8
   1.135 +                
   1.136 +;*******************************************************************
   1.137 +; Calculate "nice" bins for binning the data in equally spaced ranges
   1.138 +;********************************************************************
   1.139 +
   1.140 +; using model biome class
   1.141 +  nclass = nclass_mod
   1.142 +
   1.143 +  range  = fspan(0,nclass,nclass+1)
   1.144 +
   1.145 +; print (range)
   1.146 +; Use this range information to grab all the values in a
   1.147 +; particular range, and then take an average.
   1.148 +
   1.149 +  nx     = dimsizes(range) - 1
   1.150 +
   1.151 +;==============================
   1.152 +; put data into bins
   1.153 +;==============================
   1.154 +
   1.155 +; using observed biome class
   1.156 +; base  = ndtooned(classob)
   1.157 +; using model biome class
   1.158 +  base  = ndtooned(classmod)
   1.159 +
   1.160 +  area_1d = ndtooned(area)
   1.161 +
   1.162 +; output
   1.163 +
   1.164 +  yvalues   = new((/data_n,nx/),float) ; (per m2)
   1.165 +  yvalues_t = new((/data_n,nx/),float) ; (per biome)
   1.166 +
   1.167 +; Loop through each range, using base.
   1.168 +
   1.169 +  do i=0,nx-1
   1.170 +
   1.171 +     if (i.ne.(nx-1)) then
   1.172 +        idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))        
   1.173 +     else
   1.174 +        idx = ind(base.ge.range(i))
   1.175 +     end if
   1.176 +
   1.177 +  do n = 0,data_n-1
   1.178 +
   1.179 +     if (n.eq.0) then
   1.180 +        data = ndtooned(area)
   1.181 +     end if
   1.182 +
   1.183 +     if (n.eq.1) then
   1.184 +        data = ndtooned(NPP1)
   1.185 +     end if
   1.186 +
   1.187 +     if (n.eq.2) then
   1.188 +        data = ndtooned(VegC)
   1.189 +     end if
   1.190 +
   1.191 +     if (n.eq.3) then
   1.192 +        data = ndtooned(LiCwC)
   1.193 +     end if
   1.194 +
   1.195 +     if (n.eq.4) then
   1.196 +        data = ndtooned(SoilC)
   1.197 +     end if
   1.198 +
   1.199 +     if (n.eq.5) then
   1.200 +        data = ndtooned(NPP2)
   1.201 +     end if
   1.202 +
   1.203 +     if (n.eq.6) then
   1.204 +        data = ndtooned(NEE2)
   1.205 +     end if
   1.206 +
   1.207 +     if (n.eq.7) then
   1.208 +        data = ndtooned(GPP2)
   1.209 +     end if
   1.210 +
   1.211 +;    Calculate sum and average
   1.212 + 
   1.213 +     if (.not.any(ismissing(idx))) then
   1.214 +        if (n.eq.0) then 
   1.215 +           yvalues(n,i)   = sum(data(idx))
   1.216 +           yvalues_t(n,i) = sum(data(idx))   
   1.217 +        else 
   1.218 +           yvalues(n,i)   = avg(data(idx))
   1.219 +           yvalues_t(n,i) = sum(data(idx)*area_1d(idx))
   1.220 +        end if
   1.221 +     else
   1.222 +        yvalues(n,i)   = yvalues@_FillValue
   1.223 +        yvalues_t(n,i) = yvalues@_FillValue
   1.224 +     end if
   1.225 +
   1.226 +;#############################################################
   1.227 +; using model biome class:
   1.228 +;     set the following 4 classes to _FillValue:
   1.229 +;     (3)Needleleaf Deciduous Boreal Tree,
   1.230 +;     (8)Broadleaf Deciduous Boreal Tree,
   1.231 +;     (9)Broadleaf Evergreen Shrub,
   1.232 +;     (16)Wheat
   1.233 +
   1.234 +      if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   1.235 +         yvalues(n,i)   = yvalues@_FillValue
   1.236 +         yvalues_t(n,i) = yvalues@_FillValue
   1.237 +      end if
   1.238 +;#############################################################  
   1.239 +
   1.240 +     delete (data)
   1.241 +  end do 
   1.242 +
   1.243 +  delete (idx)
   1.244 +  end do
   1.245 +
   1.246 +  delete (base)
   1.247 +  delete (area)
   1.248 +  delete (NPP1)
   1.249 +  delete (VegC)
   1.250 +  delete (LiCwC)
   1.251 +  delete (SoilC)
   1.252 +  delete (NPP2)
   1.253 +  delete (NEE2)
   1.254 +  delete (GPP2)
   1.255 +
   1.256 +;----------------------------------------------------------------
   1.257 +; data for table1
   1.258 +
   1.259 + good = ind(.not.ismissing(yvalues(5,:)) .and. .not.ismissing(yvalues(1,:)))
   1.260 +;print (good)
   1.261 +
   1.262 + area_g  = yvalues(0,good)
   1.263 + NPP1_g  = yvalues(1,good)
   1.264 + VegC_g  = yvalues(2,good)
   1.265 + LiCwC_g = yvalues(3,good)
   1.266 + SoilC_g = yvalues(4,good)
   1.267 + NPP2_g  = yvalues(5,good)
   1.268 + NEE2_g  = yvalues(6,good)
   1.269 + GPP2_g  = yvalues(7,good)
   1.270 +
   1.271 + NPP_ratio = NPP2_g/NPP1_g
   1.272 +
   1.273 + n_biome = dimsizes(NPP1_g)
   1.274 +
   1.275 +;-----------------------------------------------------------------
   1.276 +; data for table2
   1.277 +
   1.278 +; change unit from g to Pg (Peta gram)
   1.279 + factor_unit = 1.e-15
   1.280 +
   1.281 + NPP1_t  = yvalues_t(1,good) * factor_unit
   1.282 + VegC_t  = yvalues_t(2,good) * factor_unit
   1.283 + LiCwC_t = yvalues_t(3,good) * factor_unit 
   1.284 + SoilC_t = yvalues_t(4,good) * factor_unit
   1.285 + NEE2_t  = yvalues_t(6,good) * factor_unit
   1.286 + GPP2_t  = yvalues_t(7,good) * factor_unit
   1.287 +
   1.288 + delete (yvalues)
   1.289 + delete (yvalues_t)
   1.290 + 
   1.291 +;-------------------------------------------------------------
   1.292 +; html table1 data
   1.293 +
   1.294 +; column (not including header column)
   1.295 +
   1.296 +  col_head  = (/"Area (1.e12m2)" \
   1.297 +               ,"NPP (gC/m2/yr)" \
   1.298 +               ,"VegC (gC/m2)" \
   1.299 +               ,"Litter+CWD (gC/m2)" \
   1.300 +               ,"SoilC (gC/m2)" \
   1.301 +               ,"NPP_ratio" \
   1.302 +               ,"NEE (gC/m2/yr)" \
   1.303 +               ,"GPP (gC/m2/yr)" \
   1.304 +               /)
   1.305 +
   1.306 +  ncol = dimsizes(col_head)
   1.307 +
   1.308 +; row (not including header row)                   
   1.309 +
   1.310 +; using model biome class:  
   1.311 +  row_head  = (/"Not Vegetated" \
   1.312 +               ,"Needleleaf Evergreen Temperate Tree" \
   1.313 +               ,"Needleleaf Evergreen Boreal Tree" \
   1.314 +;              ,"Needleleaf Deciduous Boreal Tree" \
   1.315 +               ,"Broadleaf Evergreen Tropical Tree" \
   1.316 +               ,"Broadleaf Evergreen Temperate Tree" \
   1.317 +               ,"Broadleaf Deciduous Tropical Tree" \
   1.318 +               ,"Broadleaf Deciduous Temperate Tree" \
   1.319 +;              ,"Broadleaf Deciduous Boreal Tree" \
   1.320 +;              ,"Broadleaf Evergreen Shrub" \
   1.321 +               ,"Broadleaf Deciduous Temperate Shrub" \
   1.322 +               ,"Broadleaf Deciduous Boreal Shrub" \
   1.323 +               ,"C3 Arctic Grass" \
   1.324 +               ,"C3 Non-Arctic Grass" \
   1.325 +               ,"C4 Grass" \
   1.326 +               ,"Corn" \
   1.327 +;              ,"Wheat" \                      
   1.328 +               ,"All Biome" \                
   1.329 +               /)  
   1.330 +  nrow = dimsizes(row_head)                  
   1.331 +
   1.332 +; arrays to be passed to table. 
   1.333 +  text = new ((/nrow, ncol/),string )
   1.334 + 
   1.335 + do i=0,nrow-2
   1.336 +  text(i,0) = sprintf("%.1f",area_g(i)*1.e-12)
   1.337 +  text(i,1) = sprintf("%.1f",NPP1_g(i))
   1.338 +  text(i,2) = sprintf("%.1f",VegC_g(i))
   1.339 +  text(i,3) = sprintf("%.1f",LiCwC_g(i))
   1.340 +  text(i,4) = sprintf("%.1f",SoilC_g(i))
   1.341 +  text(i,5) = sprintf("%.2f",NPP_ratio(i))
   1.342 +  text(i,6) = sprintf("%.1f",NEE2_g(i))
   1.343 +  text(i,7) = sprintf("%.1f",GPP2_g(i))
   1.344 + end do
   1.345 +
   1.346 +;-------------------------------------------------------
   1.347 +; create html table1
   1.348 +
   1.349 +  header_text = "<H1>NEE and Carbon Stocks and Fluxes:  Model "+model_name+"</H1>" 
   1.350 +
   1.351 +  header = (/"<HTML>" \
   1.352 +            ,"<HEAD>" \
   1.353 +            ,"<TITLE>CLAMP metrics</TITLE>" \
   1.354 +            ,"</HEAD>" \
   1.355 +            ,header_text \
   1.356 +            /) 
   1.357 +  footer = "</HTML>"
   1.358 +
   1.359 +  table_header = (/ \
   1.360 +        "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
   1.361 +       ,"<tr>" \
   1.362 +       ,"   <th bgcolor=DDDDDD >Biome Type</th>" \
   1.363 +       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
   1.364 +       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
   1.365 +       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
   1.366 +       ,"   <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
   1.367 +       ,"   <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
   1.368 +       ,"   <th bgcolor=DDDDDD >"+col_head(5)+"</th>" \
   1.369 +       ,"   <th bgcolor=DDDDDD >"+col_head(6)+"</th>" \
   1.370 +       ,"   <th bgcolor=DDDDDD >"+col_head(7)+"</th>" \
   1.371 +       ,"</tr>" \
   1.372 +       /)
   1.373 +  table_footer = "</table>"
   1.374 +  row_header = "<tr>"
   1.375 +  row_footer = "</tr>"
   1.376 +
   1.377 +  lines = new(50000,string)
   1.378 +  nline = 0
   1.379 +
   1.380 +  set_line(lines,nline,header)
   1.381 +  set_line(lines,nline,table_header)
   1.382 +
   1.383 +;----------------------------
   1.384 +;row of table
   1.385 +
   1.386 +  do n = 0,nrow-2
   1.387 +     set_line(lines,nline,row_header)
   1.388 +
   1.389 +     txt0  = row_head(n)
   1.390 +     txt1  = text(n,0)
   1.391 +     txt2  = text(n,1)
   1.392 +     txt3  = text(n,2)
   1.393 +     txt4  = text(n,3)
   1.394 +     txt5  = text(n,4)
   1.395 +     txt6  = text(n,5)
   1.396 +     txt7  = text(n,6)
   1.397 +     txt8  = text(n,7)
   1.398 +
   1.399 +     set_line(lines,nline,"<th>"+txt0+"</th>")
   1.400 +     set_line(lines,nline,"<th>"+txt1+"</th>")
   1.401 +     set_line(lines,nline,"<th>"+txt2+"</th>")
   1.402 +     set_line(lines,nline,"<th>"+txt3+"</th>")
   1.403 +     set_line(lines,nline,"<th>"+txt4+"</th>")
   1.404 +     set_line(lines,nline,"<th>"+txt5+"</th>")
   1.405 +     set_line(lines,nline,"<th>"+txt6+"</th>")
   1.406 +     set_line(lines,nline,"<th>"+txt7+"</th>")
   1.407 +     set_line(lines,nline,"<th>"+txt8+"</th>")
   1.408 +
   1.409 +     set_line(lines,nline,row_footer)
   1.410 +  end do
   1.411 +;----------------------------
   1.412 +  set_line(lines,nline,table_footer)
   1.413 +  set_line(lines,nline,footer) 
   1.414 +
   1.415 +; Now write to an HTML file.
   1.416 +
   1.417 +  output_html = "table_per_m2.html"
   1.418 +
   1.419 +  idx = ind(.not.ismissing(lines))
   1.420 +  if(.not.any(ismissing(idx))) then
   1.421 +    asciiwrite(output_html,lines(idx))
   1.422 +  else
   1.423 +   print ("error?")
   1.424 +  end if
   1.425 +
   1.426 +  delete (idx)
   1.427 +
   1.428 +  delete (col_head)
   1.429 +  delete (row_head)
   1.430 +  delete (text)
   1.431 +  delete (table_header)
   1.432 +
   1.433 +;-----------------------------------------------------------------
   1.434 +; html table2 data
   1.435 +
   1.436 +; column (not including header column)
   1.437 +
   1.438 +  col_head  = (/"NPP (PgC/yr)" \
   1.439 +               ,"VegC (PgC)" \
   1.440 +               ,"Litter+CWD (PgC)" \
   1.441 +               ,"SoilC (PgC)" \
   1.442 +               ,"NEE (PgC/yr)" \
   1.443 +               ,"GPP (PgC/yr)" \
   1.444 +               ,"NPP timeseries" \
   1.445 +               ,"NEE timeseries" \
   1.446 +               ,"Fire timeseries" \
   1.447 +               /)
   1.448 +
   1.449 +  ncol = dimsizes(col_head)
   1.450 +
   1.451 +; row (not including header row)                   
   1.452 +
   1.453 +; using model biome class:  
   1.454 +  row_head  = (/"Not Vegetated" \
   1.455 +               ,"Needleleaf Evergreen Temperate Tree" \
   1.456 +               ,"Needleleaf Evergreen Boreal Tree" \
   1.457 +;              ,"Needleleaf Deciduous Boreal Tree" \
   1.458 +               ,"Broadleaf Evergreen Tropical Tree" \
   1.459 +               ,"Broadleaf Evergreen Temperate Tree" \
   1.460 +               ,"Broadleaf Deciduous Tropical Tree" \
   1.461 +               ,"Broadleaf Deciduous Temperate Tree" \
   1.462 +;              ,"Broadleaf Deciduous Boreal Tree" \
   1.463 +;              ,"Broadleaf Evergreen Shrub" \
   1.464 +               ,"Broadleaf Deciduous Temperate Shrub" \
   1.465 +               ,"Broadleaf Deciduous Boreal Shrub" \
   1.466 +               ,"C3 Arctic Grass" \
   1.467 +               ,"C3 Non-Arctic Grass" \
   1.468 +               ,"C4 Grass" \
   1.469 +               ,"Corn" \
   1.470 +;              ,"Wheat" \                      
   1.471 +               ,"All Biome" \                
   1.472 +               /)  
   1.473 +  nrow = dimsizes(row_head)                  
   1.474 +
   1.475 +; arrays to be passed to table. 
   1.476 +  text = new ((/nrow, ncol/),string )
   1.477 + 
   1.478 + do i=0,nrow-2
   1.479 +  text(i,0) = sprintf("%.1f",NPP1_t(i))
   1.480 +  text(i,1) = sprintf("%.1f",VegC_t(i))
   1.481 +  text(i,2) = sprintf("%.1f",LiCwC_t(i))
   1.482 +  text(i,3) = sprintf("%.1f",SoilC_t(i))
   1.483 +  text(i,4) = sprintf("%.1f",NEE2_t(i))
   1.484 +  text(i,5) = sprintf("%.1f",GPP2_t(i))
   1.485 +  text(i,6) = "<a href=./NPP_monthly_biome_"+i+".png>monthly_plot</a> <br> <a href=./NPP_annual_biome_"+i+".png>annual_plot</a>"
   1.486 +  text(i,7) = "<a href=./NEE_monthly_biome_"+i+".png>monthly_plot</a> <br> <a href=./NEE_annual_biome_"+i+".png>annual_plot</a>"
   1.487 +  text(i,8) = "--"
   1.488 + end do
   1.489 +
   1.490 +  text(nrow-1,0) = sprintf("%.1f",sum(NPP1_t))
   1.491 +  text(nrow-1,1) = sprintf("%.1f",sum(VegC_t))
   1.492 +  text(nrow-1,2) = sprintf("%.1f",sum(LiCwC_t))
   1.493 +  text(nrow-1,3) = sprintf("%.1f",sum(SoilC_t))
   1.494 +  text(nrow-1,4) = sprintf("%.1f",sum(NEE2_t))
   1.495 +  text(nrow-1,5) = sprintf("%.1f",sum(GPP2_t))
   1.496 +  text(nrow-1,6) = "<a href=./NPP_monthly_global.png>monthly_plot</a> <br> <a href=./NPP_annual_global.png>annual_plot</a>"
   1.497 +  text(nrow-1,7) = "<a href=./NEE_monthly_global.png>monthly_plot</a> <br> <a href=./NEE_annual_global.png>annual_plot</a>"
   1.498 +  text(nrow-1,8) = "--"
   1.499 +
   1.500 +;**************************************************
   1.501 +; create html table2
   1.502 +;**************************************************
   1.503 +
   1.504 +  header_text = "<H1>NEE and Carbon Stocks and Fluxes (per biome):  Model "+model_name+"</H1>" 
   1.505 +
   1.506 +  header = (/"<HTML>" \
   1.507 +            ,"<HEAD>" \
   1.508 +            ,"<TITLE>CLAMP metrics</TITLE>" \
   1.509 +            ,"</HEAD>" \
   1.510 +            ,header_text \
   1.511 +            /) 
   1.512 +  footer = "</HTML>"
   1.513 +
   1.514 +  table_header = (/ \
   1.515 +        "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
   1.516 +       ,"<tr>" \
   1.517 +       ,"   <th bgcolor=DDDDDD >Biome Type</th>" \
   1.518 +       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
   1.519 +       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
   1.520 +       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
   1.521 +       ,"   <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
   1.522 +       ,"   <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
   1.523 +       ,"   <th bgcolor=DDDDDD >"+col_head(5)+"</th>" \
   1.524 +       ,"   <th bgcolor=DDDDDD >"+col_head(6)+"</th>" \
   1.525 +       ,"   <th bgcolor=DDDDDD >"+col_head(7)+"</th>" \
   1.526 +       ,"   <th bgcolor=DDDDDD >"+col_head(8)+"</th>" \
   1.527 +       ,"</tr>" \
   1.528 +       /)
   1.529 +  table_footer = "</table>"
   1.530 +  row_header = "<tr>"
   1.531 +  row_footer = "</tr>"
   1.532 +
   1.533 +  lines = new(50000,string)
   1.534 +  nline = 0
   1.535 +
   1.536 +  set_line(lines,nline,header)
   1.537 +  set_line(lines,nline,table_header)
   1.538 +;-----------------------------------------------
   1.539 +;row of table
   1.540 +
   1.541 +  do n = 0,nrow-1
   1.542 +     set_line(lines,nline,row_header)
   1.543 +
   1.544 +     txt0  = row_head(n)
   1.545 +     txt1  = text(n,0)
   1.546 +     txt2  = text(n,1)
   1.547 +     txt3  = text(n,2)
   1.548 +     txt4  = text(n,3)
   1.549 +     txt5  = text(n,4)
   1.550 +     txt6  = text(n,5)
   1.551 +     txt7  = text(n,6)
   1.552 +     txt8  = text(n,7)
   1.553 +     txt9  = text(n,8)
   1.554 +
   1.555 +     set_line(lines,nline,"<th>"+txt0+"</th>")
   1.556 +     set_line(lines,nline,"<th>"+txt1+"</th>")
   1.557 +     set_line(lines,nline,"<th>"+txt2+"</th>")
   1.558 +     set_line(lines,nline,"<th>"+txt3+"</th>")
   1.559 +     set_line(lines,nline,"<th>"+txt4+"</th>")
   1.560 +     set_line(lines,nline,"<th>"+txt5+"</th>")
   1.561 +     set_line(lines,nline,"<th>"+txt6+"</th>")
   1.562 +     set_line(lines,nline,"<th>"+txt7+"</th>")
   1.563 +     set_line(lines,nline,"<th>"+txt8+"</th>")
   1.564 +     set_line(lines,nline,"<th>"+txt9+"</th>")
   1.565 +
   1.566 +     set_line(lines,nline,row_footer)
   1.567 +  end do
   1.568 +;-----------------------------------------------
   1.569 +  set_line(lines,nline,table_footer)
   1.570 +  set_line(lines,nline,footer) 
   1.571 +
   1.572 +; Now write to an HTML file.
   1.573 +
   1.574 +  output_html = "table_per_biome.html"
   1.575 +
   1.576 +  idx = ind(.not.ismissing(lines))
   1.577 +  if(.not.any(ismissing(idx))) then
   1.578 +    asciiwrite(output_html,lines(idx))
   1.579 +  else
   1.580 +   print ("error?")
   1.581 +  end if
   1.582 +
   1.583 +  delete (idx)
   1.584 +
   1.585 +;---------------------------------------------------
   1.586 +; read model data, time series:
   1.587 +
   1.588 + fm   = addfile (dirm+film7,"r")
   1.589 +
   1.590 + NPP3 = fm->NPP 
   1.591 + NEE3 = fm->NEE 
   1.592 +;Fire = fm->COL_FIRE_CLOSS
   1.593 +
   1.594 + delete (fm)
   1.595 +
   1.596 +; Units for these variables are:
   1.597 +
   1.598 +;NPP3: g C/m^2/s
   1.599 +;NEE3: g C/m^2/s
   1.600 +;Fire: g C/m^2/s
   1.601 +
   1.602 + nsec_per_month = 60*60*24*30
   1.603 +
   1.604 +; change unit to g C/m^2/month
   1.605 +  
   1.606 + NPP3 = NPP3 * nsec_per_month * conform(NPP3,landfrac,(/2,3/))
   1.607 + NEE3 = NEE3 * nsec_per_month * conform(NEE3,landfrac,(/2,3/))
   1.608 +;Fire = Fire * nsec_per_month * conform(Fire,landfrac,(/2,3/))
   1.609 +
   1.610 +;data_n = 3
   1.611 + data_n = 2
   1.612 +
   1.613 + dsizes = dimsizes(NPP3)
   1.614 + nyear  = dsizes(0)
   1.615 + nmonth = dsizes(1)
   1.616 + ntime  = nyear * nmonth
   1.617 +
   1.618 + year_start = 1979
   1.619 + year_end   = 2004
   1.620 +                
   1.621 +;*******************************************************************
   1.622 +; Calculate "nice" bins for binning the data in equally spaced ranges
   1.623 +;********************************************************************
   1.624 +
   1.625 +; using model biome class
   1.626 +  nclass = nclass_mod
   1.627 +
   1.628 +  range  = fspan(0,nclass,nclass+1)
   1.629 +
   1.630 +; print (range)
   1.631 +; Use this range information to grab all the values in a
   1.632 +; particular range, and then take an average.
   1.633 +
   1.634 +  nx = dimsizes(range) - 1
   1.635 +
   1.636 +;==============================
   1.637 +; put data into bins
   1.638 +;==============================
   1.639 +
   1.640 +; using observed biome class
   1.641 +; base  = ndtooned(classob)
   1.642 +; using model biome class
   1.643 +  base  = ndtooned(classmod)
   1.644 +
   1.645 +; output
   1.646 +
   1.647 +  yvalues = new((/ntime,data_n,nx/),float)
   1.648 +
   1.649 +; Loop through each range, using base.
   1.650 +
   1.651 +  do i=0,nx-1
   1.652 +
   1.653 +     if (i.ne.(nx-1)) then
   1.654 +        idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
   1.655 +     else
   1.656 +        idx = ind(base.ge.range(i))
   1.657 +     end if
   1.658 +
   1.659 +  do n = 0,data_n-1
   1.660 +
   1.661 +     t = -1
   1.662 +     do m = 0,nyear-1
   1.663 +     do k = 0,nmonth-1
   1.664 +    
   1.665 +        t = t + 1 
   1.666 +
   1.667 +        if (n.eq.0) then
   1.668 +           data = ndtooned(NPP3(m,k,:,:))
   1.669 +        end if
   1.670 +
   1.671 +        if (n.eq.1) then
   1.672 +           data = ndtooned(NEE3(m,k,:,:))
   1.673 +        end if
   1.674 +
   1.675 +;       if (n.eq.2) then
   1.676 +;          data = ndtooned(Fire(m,k,:,:))
   1.677 +;       end if
   1.678 +
   1.679 +;       Calculate average
   1.680 + 
   1.681 +        if (.not.any(ismissing(idx))) then 
   1.682 +           yvalues(t,n,i) = sum(data(idx)*area_1d(idx))
   1.683 +        else
   1.684 +           yvalues(t,n,i) = yvalues@_FillValue
   1.685 +        end if
   1.686 +
   1.687 +;#############################################################
   1.688 +; using model biome class:
   1.689 +;     set the following 4 classes to _FillValue:
   1.690 +;     (3)Needleleaf Deciduous Boreal Tree,
   1.691 +;     (8)Broadleaf Deciduous Boreal Tree,
   1.692 +;     (9)Broadleaf Evergreen Shrub,
   1.693 +;     (16)Wheat
   1.694 +
   1.695 +        if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   1.696 +           yvalues(t,n,i) = yvalues@_FillValue
   1.697 +        end if
   1.698 +;#############################################################  
   1.699 +
   1.700 +     end do
   1.701 +     end do
   1.702 +
   1.703 +     delete(data)
   1.704 +  end do 
   1.705 +
   1.706 +    delete(idx)
   1.707 +  end do
   1.708 +
   1.709 +  delete (base)
   1.710 +  delete (NPP3)
   1.711 +  delete (NEE3)
   1.712 +; delete (Fire)
   1.713 +
   1.714 +;----------------------------------------------------------------
   1.715 +; data for tseries plot
   1.716 +
   1.717 +  yvalues_g = new((/ntime,data_n,n_biome/),float)
   1.718 +
   1.719 +  yvalues_g@units = "TgC/month"
   1.720 +
   1.721 +; change unit to Tg C/month
   1.722 +; change unit from g to Tg (Tera gram)
   1.723 +  factor_unit = 1.e-12
   1.724 +
   1.725 +  yvalues_g = yvalues(:,:,good) * factor_unit
   1.726 +
   1.727 +;*******************************************************************
   1.728 +; general settings for line plot
   1.729 +;*******************************************************************
   1.730 +
   1.731 +; res
   1.732 +  res                   = True               
   1.733 +  res@xyDashPatterns    = (/0/)                ; make lines solid
   1.734 +  res@xyLineThicknesses = (/2.0/)          ; make lines thicker
   1.735 +  res@xyLineColors      = (/"blue"/) ; line color
   1.736 +
   1.737 +  res@trXMinF   = year_start
   1.738 +  res@trXMaxF   = year_end + 1
   1.739 +
   1.740 +  res@vpHeightF = 0.4                 ; change aspect ratio of plot
   1.741 +; res@vpWidthF  = 0.8
   1.742 +  res@vpWidthF  = 0.75   
   1.743 +
   1.744 +; res@gsnMaximize = True
   1.745 +
   1.746 +;*******************************************************************
   1.747 +; (A) 1 component in each biome: monthly
   1.748 +;*******************************************************************
   1.749 +
   1.750 +; component = (/"NPP","NEE","Fire"/)
   1.751 +  component = (/"NPP","NEE"/)
   1.752 +
   1.753 +; for x-axis in xyplot
   1.754 +
   1.755 +  timeI = new((/ntime/),integer)
   1.756 +  timeF = new((/ntime/),float)
   1.757 +  timeI = ispan(1,ntime,1)
   1.758 +  timeF = year_start + (timeI-1)/12.
   1.759 +  timeF@long_name = "year" 
   1.760 +
   1.761 +  plot_data = new((/ntime/),float)
   1.762 +  plot_data@long_name = "TgC/month"
   1.763 + 
   1.764 +  do n = 0, data_n-1
   1.765 +  do m = 0, n_biome-1
   1.766 +
   1.767 +     plot_name = component(n)+"_monthly_biome_"+ m
   1.768 +
   1.769 +     wks = gsn_open_wks (plot_type,plot_name)   
   1.770 +
   1.771 +     title = component(n)+ ": "+ row_head(m)
   1.772 +     res@tiMainString = title
   1.773 +     res@tiMainFontHeightF = 0.025
   1.774 +
   1.775 +     plot_data(:) = yvalues_g(:,n,m)
   1.776 +                                 
   1.777 +     plot=gsn_csm_xy(wks,timeF,plot_data,res)
   1.778 +
   1.779 +     delete (wks)  
   1.780 +     delete (plot)
   1.781 + 
   1.782 +    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   1.783 +           "rm "+plot_name+"."+plot_type)  
   1.784 +  end do
   1.785 +  end do
   1.786 +
   1.787 +  do n = 0, data_n-1
   1.788 +
   1.789 +     plot_name = component(n)+"_monthly_global"
   1.790 +
   1.791 +     wks = gsn_open_wks (plot_type,plot_name)   
   1.792 +
   1.793 +     title = component(n)+ ": Global"
   1.794 +     res@tiMainString = title
   1.795 +     res@tiMainFontHeightF = 0.025
   1.796 + 
   1.797 +     do k = 0,ntime-1
   1.798 +        plot_data(k) = sum(yvalues_g(k,n,:))
   1.799 +     end do
   1.800 +                                 
   1.801 +     plot=gsn_csm_xy(wks,timeF,plot_data,res)
   1.802 +
   1.803 +     delete (wks)  
   1.804 +     delete (plot)
   1.805 + 
   1.806 +    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   1.807 +           "rm "+plot_name+"."+plot_type)   
   1.808 +  end do
   1.809 +
   1.810 +  delete (plot_data)
   1.811 +  delete (timeI)
   1.812 +  delete (timeF)
   1.813 +
   1.814 +;*******************************************************************
   1.815 +; (B) 1 component in each biome: annually
   1.816 +;*******************************************************************
   1.817 +
   1.818 +  yvalues_a = new((/nyear,data_n,n_biome/),float)
   1.819 +  yvalues_g!0 = "time"
   1.820 +  yvalues_g!1 = "case"
   1.821 +  yvalues_g!2 = "record"
   1.822 +
   1.823 +  yvalues_a = month_to_annual(yvalues_g,0)
   1.824 +
   1.825 +  delete (yvalues_g) 
   1.826 +
   1.827 +; for x-axis in xyplot
   1.828 +
   1.829 +  timeI = new((/nyear/),integer)
   1.830 +  timeF = new((/nyear/),float)
   1.831 +  timeI = ispan(1,nyear,1)
   1.832 +  timeF = year_start + (timeI-1)
   1.833 +  timeF@long_name = "year" 
   1.834 +
   1.835 +  plot_data = new((/nyear/),float)
   1.836 +  plot_data@long_name = "TgC/year"
   1.837 + 
   1.838 +  do n = 0, data_n-1
   1.839 +  do m = 0, n_biome-1
   1.840 +
   1.841 +     plot_name = component(n)+"_annual_biome_"+ m
   1.842 +
   1.843 +     wks = gsn_open_wks (plot_type,plot_name)   
   1.844 +
   1.845 +     title = component(n)+ ": "+ row_head(m)
   1.846 +     res@tiMainString = title
   1.847 +     res@tiMainFontHeightF = 0.025
   1.848 +
   1.849 +     plot_data(:) = yvalues_a(:,n,m)
   1.850 +                                 
   1.851 +     plot=gsn_csm_xy(wks,timeF,plot_data,res)
   1.852 +
   1.853 +     delete (wks)  
   1.854 +     delete (plot)
   1.855 + 
   1.856 +    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   1.857 +           "rm "+plot_name+"."+plot_type)   
   1.858 +  end do
   1.859 +  end do
   1.860 +
   1.861 +  do n = 0, data_n-1
   1.862 +
   1.863 +     plot_name = component(n)+"_annual_global"
   1.864 +
   1.865 +     wks = gsn_open_wks (plot_type,plot_name)   
   1.866 +
   1.867 +     title = component(n)+ ": Global"
   1.868 +     res@tiMainString = title
   1.869 +     res@tiMainFontHeightF = 0.025
   1.870 + 
   1.871 +     do k = 0,nyear-1
   1.872 +        plot_data(k) = sum(yvalues_a(k,n,:))
   1.873 +     end do
   1.874 +                                 
   1.875 +     plot=gsn_csm_xy(wks,timeF,plot_data,res)
   1.876 +
   1.877 +     delete (wks)  
   1.878 +     delete (plot)
   1.879 + 
   1.880 +    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   1.881 +           "rm "+plot_name+"."+plot_type)   
   1.882 +  end do
   1.883 +
   1.884 +;****************************************
   1.885 +; output plot and html
   1.886 +;****************************************
   1.887 +  output_dir = model_name+"/carbon_sink"
   1.888 +
   1.889 +  system("mv *.png *.html " + output_dir) 
   1.890 +;****************************************
   1.891 +
   1.892 +end
   1.893 +