carbon_sink/20.table+tseries.ncl
author Forrest Hoffman <forrest@climatemodeling.org>
Mon, 26 Jan 2009 22:08:20 -0500
changeset 0 0c6405ab2ff4
permissions -rw-r--r--
Initial commit of C-LAMP Diagnostics from Jeff Lee
     1 ;********************************************************
     2 ;using model biome vlass
     3 ;
     4 ; required command line input parameters:
     5 ;  ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
     6 ;
     7 ; histogram normalized by rain and compute correleration
     8 ;**************************************************************
     9 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
    10 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
    11 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
    12 ;**************************************************************
    13 procedure set_line(lines:string,nline:integer,newlines:string) 
    14 begin
    15 ; add line to ascci/html file
    16     
    17   nnewlines = dimsizes(newlines)
    18   if(nline+nnewlines-1.ge.dimsizes(lines))
    19     print("set_line: bad index, not setting anything.") 
    20     return
    21   end if 
    22   lines(nline:nline+nnewlines-1) = newlines
    23 ;  print ("lines = " + lines(nline:nline+nnewlines-1))
    24   nline = nline + nnewlines
    25   return 
    26 end
    27 ;**************************************************************
    28 ; Main code.
    29 begin
    30  
    31  plot_type     = "ps"
    32  plot_type_new = "png"
    33 
    34 ;************************************************
    35 ; model name and grid       
    36 ;************************************************
    37 
    38  model_grid = "T42"
    39 
    40  model_name  = "cn"
    41  model_name1 = "i01.06cn"
    42  model_name2 = "i01.10cn"
    43 
    44 ;---------------------------------------------------
    45 ; get biome data: model
    46 
    47   biome_name_mod = "Model PFT Class"
    48 
    49   dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    50   film = "class_pft_"+model_grid+".nc"
    51 
    52   fm = addfile(dirm+film,"r")
    53  
    54   classmod = fm->CLASS_PFT
    55 
    56   delete (fm)
    57 
    58 ; model data has 17 land-type classes
    59 
    60   nclass_mod = 17
    61 
    62 ;--------------------------------------------------
    63 ; get model data: landfrac and area
    64 
    65  dirm_l= "/fis/cgd/cseg/people/jeff/surface_data/" 
    66  film_l = "lnd_T42.nc"
    67  fm_l   = addfile (dirm_l+film_l,"r")
    68   
    69  landfrac = fm_l->landfrac
    70  area     = fm_l->area
    71 
    72 ; change area from km**2 to m**2
    73   area = area * 1.e6             
    74 
    75 ;---------------------------------------------------
    76 ; take into account landfrac
    77 
    78   area = area * landfrac
    79 
    80   delete (landfrac)
    81 
    82 ;---------------------------------------------------
    83 ; read data: model, group 1
    84 
    85  dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    86  film = model_name1 + "_1980-2004_ANN_climo.nc"
    87  fm   = addfile (dirm+film,"r")
    88  
    89  NPP1   = fm->NPP
    90 
    91  leafc  = fm->LEAFC
    92  woodc  = fm->WOODC
    93  frootc = fm->FROOTC
    94  VegC   = leafc
    95  VegC   = leafc + woodc + frootc
    96 
    97  litterc = fm->LITTERC
    98  cwdc    = fm->CWDC
    99  LiCwC   = litterc
   100  LiCwC   = litterc + cwdc
   101 
   102  SoilC   = fm->SOILC
   103 
   104  delete (fm)
   105 ;--------------------------------------------------- 
   106 ; read data: model, group 2
   107 
   108  dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
   109  film = model_name2 + "_1990-2004_ANN_climo.nc"
   110  fm   = addfile (dirm+film,"r")
   111 
   112  NPP2   = fm->NPP 
   113  NEE2   = fm->NEE 
   114 
   115 ;---------------------------------------------------
   116 ; Units for these variables are:
   117 
   118 ;NPP1: g C/m^2/s
   119 ;NPP2: g C/m^2/s
   120 ;NEE2: g C/m^2/s
   121 
   122 ;VegC:  g C/m^2
   123 ;LiCwC: g C/m^2
   124 ;SoilC: g C/m^2
   125 
   126  nsec_per_year = 60*60*24*365
   127 
   128 ; change unit to g C/m^2/year
   129   
   130  NPP1 = NPP1 *  nsec_per_year
   131  NPP2 = NPP2 *  nsec_per_year
   132  NEE2 = NEE2 *  nsec_per_year 
   133 
   134  data_n = 7
   135                 
   136 ;*******************************************************************
   137 ; Calculate "nice" bins for binning the data in equally spaced ranges
   138 ;********************************************************************
   139 
   140 ; using model biome class
   141   nclass      = nclass_mod
   142 
   143   range       = fspan(0,nclass,nclass+1)
   144 
   145 ; print (range)
   146 ; Use this range information to grab all the values in a
   147 ; particular range, and then take an average.
   148 
   149   nx           = dimsizes(range) - 1
   150 
   151 ;==============================
   152 ; put data into bins
   153 ;==============================
   154 
   155 ; using observed biome class
   156 ; base  = ndtooned(classob)
   157 ; using model biome class
   158   base  = ndtooned(classmod)
   159 
   160   area_1d = ndtooned(area)
   161 
   162 ; output
   163 
   164   yvalues   = new((/data_n,nx/),float)
   165   yvalues_t = new((/data_n,nx/),float)
   166 
   167  
   168 ; Loop through each range, using base.
   169 
   170   do i=0,nx-1
   171 
   172      if (i.ne.(nx-1)) then
   173         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))        
   174      else
   175         idx = ind(base.ge.range(i))
   176      end if
   177 
   178   do n = 0,data_n-1
   179 
   180      if (n.eq.0) then
   181         data = ndtooned(area)
   182      end if
   183 
   184      if (n.eq.1) then
   185         data = ndtooned(NPP1)
   186      end if
   187 
   188      if (n.eq.2) then
   189         data = ndtooned(VegC)
   190      end if
   191 
   192      if (n.eq.3) then
   193         data = ndtooned(LiCwC)
   194      end if
   195 
   196      if (n.eq.4) then
   197         data = ndtooned(SoilC)
   198      end if
   199 
   200      if (n.eq.5) then
   201         data = ndtooned(NPP2)
   202      end if
   203 
   204      if (n.eq.6) then
   205         data = ndtooned(NEE2)
   206      end if
   207 
   208 ;    Calculate sum and average
   209  
   210      if (.not.any(ismissing(idx))) then
   211         if (n.eq.0) then 
   212            yvalues(n,i)   = sum(data(idx))
   213            yvalues_t(n,i) = sum(data(idx))   
   214         else 
   215            yvalues(n,i)   = avg(data(idx))
   216            yvalues_t(n,i) = sum(data(idx)*area_1d(idx))
   217         end if
   218      else
   219         yvalues(n,i)   = yvalues@_FillValue
   220         yvalues_t(n,i) = yvalues@_FillValue
   221      end if
   222 
   223 ;#############################################################
   224 ; using model biome class:
   225 ;     set the following 4 classes to _FillValue:
   226 ;     (3)Needleleaf Deciduous Boreal Tree,
   227 ;     (8)Broadleaf Deciduous Boreal Tree,
   228 ;     (9)Broadleaf Evergreen Shrub,
   229 ;     (16)Wheat
   230 
   231       if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   232          yvalues(n,i)   = yvalues@_FillValue
   233          yvalues_t(n,i) = yvalues@_FillValue
   234       end if
   235 ;#############################################################  
   236 
   237      delete (data)
   238   end do 
   239 
   240   delete (idx)
   241   end do
   242 
   243   delete (base)
   244   delete (area)
   245   delete (NPP1)
   246   delete (VegC)
   247   delete (LiCwC)
   248   delete (SoilC)
   249   delete (NPP2)
   250   delete (NEE2)
   251 
   252 ;----------------------------------------------------------------
   253 ; data for table1
   254 
   255  good = ind(.not.ismissing(yvalues(5,:)) .and. .not.ismissing(yvalues(1,:)))
   256 ;print (good)
   257 
   258  area_g  = yvalues(0,good)
   259  NPP1_g  = yvalues(1,good)
   260  VegC_g  = yvalues(2,good)
   261  LiCwC_g = yvalues(3,good)
   262  SoilC_g = yvalues(4,good)
   263  NPP2_g  = yvalues(5,good)
   264  NEE2_g  = yvalues(6,good)
   265 
   266  n_biome = dimsizes(NPP1_g)
   267 
   268  NPP_ratio = NPP2_g/NPP1_g
   269 
   270 ;-----------------------------------------------------------------
   271 ; data for table2
   272 
   273 ; change unit from g to Pg (Peta gram)
   274  factor_unit = 1.e-15
   275 
   276  NPP1_t  = yvalues_t(1,good) * factor_unit
   277  VegC_t  = yvalues_t(2,good) * factor_unit
   278  LiCwC_t = yvalues_t(3,good) * factor_unit 
   279  SoilC_t = yvalues_t(4,good) * factor_unit
   280  NEE2_t  = yvalues_t(6,good) * factor_unit
   281 
   282  delete (yvalues)
   283  delete (yvalues_t)
   284  
   285 ;-------------------------------------------------------------
   286 ; html table1 data
   287 
   288 ; column (not including header column)
   289 
   290   col_head  = (/"Area (1.e12m2)" \
   291                ,"NPP (gC/m2/yr)" \
   292                ,"VegC (gC/m2)" \
   293                ,"Litter+CWD (gC/m2)" \
   294                ,"SoilC (gC/m2)" \
   295                ,"NPP_ratio" \
   296                ,"NEE (gC/m2/yr)" \
   297                /)
   298 
   299   ncol = dimsizes(col_head)
   300 
   301 ; row (not including header row)                   
   302 
   303 ; using model biome class:  
   304   row_head  = (/"Not Vegetated" \
   305                ,"Needleleaf Evergreen Temperate Tree" \
   306                ,"Needleleaf Evergreen Boreal Tree" \
   307 ;              ,"Needleleaf Deciduous Boreal Tree" \
   308                ,"Broadleaf Evergreen Tropical Tree" \
   309                ,"Broadleaf Evergreen Temperate Tree" \
   310                ,"Broadleaf Deciduous Tropical Tree" \
   311                ,"Broadleaf Deciduous Temperate Tree" \
   312 ;              ,"Broadleaf Deciduous Boreal Tree" \
   313 ;              ,"Broadleaf Evergreen Shrub" \
   314                ,"Broadleaf Deciduous Temperate Shrub" \
   315                ,"Broadleaf Deciduous Boreal Shrub" \
   316                ,"C3 Arctic Grass" \
   317                ,"C3 Non-Arctic Grass" \
   318                ,"C4 Grass" \
   319                ,"Corn" \
   320 ;              ,"Wheat" \                      
   321                ,"All Biome" \                
   322                /)  
   323   nrow = dimsizes(row_head)                  
   324 
   325 ; arrays to be passed to table. 
   326   text4 = new ((/nrow, ncol/),string )
   327  
   328  do i=0,nrow-2
   329   text4(i,0) = sprintf("%.1f",area_g(i)*1.e-12)
   330   text4(i,1) = sprintf("%.1f",NPP1_g(i))
   331   text4(i,2) = sprintf("%.1f",VegC_g(i))
   332   text4(i,3) = sprintf("%.1f",LiCwC_g(i))
   333   text4(i,4) = sprintf("%.1f",SoilC_g(i))
   334   text4(i,5) = sprintf("%.2f",NPP_ratio(i))
   335   text4(i,6) = sprintf("%.1f",NEE2_g(i))
   336  end do
   337 
   338 ;-------------------------------------------------------
   339 ; create html table1
   340 
   341   header_text = "<H1>NEE and Carbon Stocks and Fluxes:  Model "+model_name+"</H1>" 
   342 
   343   header = (/"<HTML>" \
   344             ,"<HEAD>" \
   345             ,"<TITLE>CLAMP metrics</TITLE>" \
   346             ,"</HEAD>" \
   347             ,header_text \
   348             /) 
   349   footer = "</HTML>"
   350 
   351   table_header = (/ \
   352         "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
   353        ,"<tr>" \
   354        ,"   <th bgcolor=DDDDDD >Biome Type</th>" \
   355        ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
   356        ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
   357        ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
   358        ,"   <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
   359        ,"   <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
   360        ,"   <th bgcolor=DDDDDD >"+col_head(5)+"</th>" \
   361        ,"   <th bgcolor=DDDDDD >"+col_head(6)+"</th>" \
   362        ,"</tr>" \
   363        /)
   364   table_footer = "</table>"
   365   row_header = "<tr>"
   366   row_footer = "</tr>"
   367 
   368   lines = new(50000,string)
   369   nline = 0
   370 
   371   set_line(lines,nline,header)
   372   set_line(lines,nline,table_header)
   373 
   374 ;----------------------------
   375 ;row of table
   376 
   377   do n = 0,nrow-2
   378      set_line(lines,nline,row_header)
   379 
   380      txt0  = row_head(n)
   381      txt1  = text4(n,0)
   382      txt2  = text4(n,1)
   383      txt3  = text4(n,2)
   384      txt4  = text4(n,3)
   385      txt5  = text4(n,4)
   386      txt6  = text4(n,5)
   387      txt7  = text4(n,6)
   388 
   389      set_line(lines,nline,"<th>"+txt0+"</th>")
   390      set_line(lines,nline,"<th>"+txt1+"</th>")
   391      set_line(lines,nline,"<th>"+txt2+"</th>")
   392      set_line(lines,nline,"<th>"+txt3+"</th>")
   393      set_line(lines,nline,"<th>"+txt4+"</th>")
   394      set_line(lines,nline,"<th>"+txt5+"</th>")
   395      set_line(lines,nline,"<th>"+txt6+"</th>")
   396      set_line(lines,nline,"<th>"+txt7+"</th>")
   397 
   398      set_line(lines,nline,row_footer)
   399   end do
   400 ;----------------------------
   401   set_line(lines,nline,table_footer)
   402   set_line(lines,nline,footer) 
   403 
   404 ; Now write to an HTML file.
   405 
   406   output_html = "table_carbon_sink1.html"
   407 
   408   idx = ind(.not.ismissing(lines))
   409   if(.not.any(ismissing(idx))) then
   410     asciiwrite(output_html,lines(idx))
   411   else
   412    print ("error?")
   413   end if
   414 
   415   delete (idx)
   416 
   417   delete (col_head)
   418   delete (row_head)
   419   delete (text4)
   420   delete (table_header)
   421 
   422 ;-----------------------------------------------------------------
   423 ; html table2 data
   424 
   425 ; column (not including header column)
   426 
   427   col_head  = (/"NPP (PgC/yr)" \
   428                ,"VegC (PgC)" \
   429                ,"Litter+CWD (PgC)" \
   430                ,"SoilC (PgC)" \
   431                ,"NEE (PgC/yr)" \
   432                ,"NPP timeseries" \
   433                ,"NEE timeseries" \
   434                ,"Fire timeseries" \
   435                /)
   436 
   437   ncol = dimsizes(col_head)
   438 
   439 ; row (not including header row)                   
   440 
   441 ; using model biome class:  
   442   row_head  = (/"Not Vegetated" \
   443                ,"Needleleaf Evergreen Temperate Tree" \
   444                ,"Needleleaf Evergreen Boreal Tree" \
   445 ;              ,"Needleleaf Deciduous Boreal Tree" \
   446                ,"Broadleaf Evergreen Tropical Tree" \
   447                ,"Broadleaf Evergreen Temperate Tree" \
   448                ,"Broadleaf Deciduous Tropical Tree" \
   449                ,"Broadleaf Deciduous Temperate Tree" \
   450 ;              ,"Broadleaf Deciduous Boreal Tree" \
   451 ;              ,"Broadleaf Evergreen Shrub" \
   452                ,"Broadleaf Deciduous Temperate Shrub" \
   453                ,"Broadleaf Deciduous Boreal Shrub" \
   454                ,"C3 Arctic Grass" \
   455                ,"C3 Non-Arctic Grass" \
   456                ,"C4 Grass" \
   457                ,"Corn" \
   458 ;              ,"Wheat" \                      
   459                ,"All Biome" \                
   460                /)  
   461   nrow = dimsizes(row_head)                  
   462 
   463 ; arrays to be passed to table. 
   464   text4 = new ((/nrow, ncol/),string )
   465  
   466  do i=0,nrow-2
   467   text4(i,0) = sprintf("%.1f",NPP1_t(i))
   468   text4(i,1) = sprintf("%.1f",VegC_t(i))
   469   text4(i,2) = sprintf("%.1f",LiCwC_t(i))
   470   text4(i,3) = sprintf("%.1f",SoilC_t(i))
   471   text4(i,4) = sprintf("%.1f",NEE2_t(i))
   472   text4(i,5) = "<a href=./NPP_monthly_biome_"+i+".png>monthly_plot</a> <br> <a href=./NPP_annual_biome_"+i+".png>annual_plot</a>"
   473   text4(i,6) = "<a href=./NEE_monthly_biome_"+i+".png>monthly_plot</a> <br> <a href=./NEE_annual_biome_"+i+".png>annual_plot</a>"
   474   text4(i,7) = "<a href=./Fire_monthly_biome_"+i+".png>monthly_plot</a> <br> <a href=./Fire_annual_biome_"+i+".png>annual_plot</a>"
   475  end do
   476   text4(nrow-1,0) = sprintf("%.1f",sum(NPP1_t))
   477   text4(nrow-1,1) = sprintf("%.1f",sum(VegC_t))
   478   text4(nrow-1,2) = sprintf("%.1f",sum(LiCwC_t))
   479   text4(nrow-1,3) = sprintf("%.1f",sum(SoilC_t))
   480   text4(nrow-1,4) = sprintf("%.1f",sum(NEE2_t))
   481   text4(nrow-1,5) = "<a href=./NPP_monthly_global.png>monthly_plot</a> <br> <a href=./NPP_annual_global.png>annual_plot</a>"
   482   text4(nrow-1,6) = "<a href=./NEE_monthly_global.png>monthly_plot</a> <br> <a href=./NEE_annual_global.png>annual_plot</a>"
   483   text4(nrow-1,7) = "<a href=./Fire_monthly_global.png>monthly_plot</a> <br> <a href=./Fire_annual_global.png>annual_plot</a>"
   484 
   485 ;**************************************************
   486 ; create html table2
   487 ;**************************************************
   488 
   489   header_text = "<H1>NEE and Carbon Stocks and Fluxes (per biome):  Model "+model_name+"</H1>" 
   490 
   491   header = (/"<HTML>" \
   492             ,"<HEAD>" \
   493             ,"<TITLE>CLAMP metrics</TITLE>" \
   494             ,"</HEAD>" \
   495             ,header_text \
   496             /) 
   497   footer = "</HTML>"
   498 
   499   table_header = (/ \
   500         "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
   501        ,"<tr>" \
   502        ,"   <th bgcolor=DDDDDD >Biome Type</th>" \
   503        ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
   504        ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
   505        ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
   506        ,"   <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
   507        ,"   <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
   508        ,"   <th bgcolor=DDDDDD >"+col_head(5)+"</th>" \
   509        ,"   <th bgcolor=DDDDDD >"+col_head(6)+"</th>" \
   510        ,"   <th bgcolor=DDDDDD >"+col_head(7)+"</th>" \
   511        ,"</tr>" \
   512        /)
   513   table_footer = "</table>"
   514   row_header = "<tr>"
   515   row_footer = "</tr>"
   516 
   517   lines = new(50000,string)
   518   nline = 0
   519 
   520   set_line(lines,nline,header)
   521   set_line(lines,nline,table_header)
   522 ;-----------------------------------------------
   523 ;row of table
   524 
   525   do n = 0,nrow-1
   526      set_line(lines,nline,row_header)
   527 
   528      txt0  = row_head(n)
   529      txt1  = text4(n,0)
   530      txt2  = text4(n,1)
   531      txt3  = text4(n,2)
   532      txt4  = text4(n,3)
   533      txt5  = text4(n,4)
   534      txt6  = text4(n,5)
   535      txt7  = text4(n,6)
   536      txt8  = text4(n,7)
   537 
   538      set_line(lines,nline,"<th>"+txt0+"</th>")
   539      set_line(lines,nline,"<th>"+txt1+"</th>")
   540      set_line(lines,nline,"<th>"+txt2+"</th>")
   541      set_line(lines,nline,"<th>"+txt3+"</th>")
   542      set_line(lines,nline,"<th>"+txt4+"</th>")
   543      set_line(lines,nline,"<th>"+txt5+"</th>")
   544      set_line(lines,nline,"<th>"+txt6+"</th>")
   545      set_line(lines,nline,"<th>"+txt7+"</th>")
   546      set_line(lines,nline,"<th>"+txt8+"</th>")
   547 
   548      set_line(lines,nline,row_footer)
   549   end do
   550 ;-----------------------------------------------
   551   set_line(lines,nline,table_footer)
   552   set_line(lines,nline,footer) 
   553 
   554 ; Now write to an HTML file.
   555 
   556   output_html = "table_carbon_sink2.html"
   557 
   558   idx = ind(.not.ismissing(lines))
   559   if(.not.any(ismissing(idx))) then
   560     asciiwrite(output_html,lines(idx))
   561   else
   562    print ("error?")
   563   end if
   564 
   565   delete (idx)
   566 
   567 ;---------------------------------------------------
   568 ; read data: model, time series
   569 
   570  dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
   571  film = model_name2 + "_Fire_C_1979-2004_monthly.nc"
   572  fm   = addfile (dirm+film,"r")
   573 
   574  NPP3 = fm->NPP 
   575  NEE3 = fm->NEE 
   576  Fire = fm->COL_FIRE_CLOSS
   577 
   578  delete (fm)
   579 
   580 ; Units for these variables are:
   581 
   582 ;NPP3: g C/m^2/s
   583 ;NEE3: g C/m^2/s
   584 ;Fire: g C/m^2/s
   585 
   586  nsec_per_month = 60*60*24*30
   587 
   588 ; change unit to g C/m^2/month
   589   
   590  NPP3 = NPP3 * nsec_per_month
   591  NEE3 = NEE3 * nsec_per_month
   592  Fire = Fire * nsec_per_month 
   593 
   594  data_n = 3
   595 
   596  dsizes = dimsizes(NPP3)
   597  nyear  = dsizes(0)
   598  nmonth = dsizes(1)
   599  ntime  = nyear * nmonth
   600 
   601  year_start = 1979
   602  year_end   = 2004
   603                 
   604 ;*******************************************************************
   605 ; Calculate "nice" bins for binning the data in equally spaced ranges
   606 ;********************************************************************
   607 
   608 ; using model biome class
   609   nclass = nclass_mod
   610 
   611   range  = fspan(0,nclass,nclass+1)
   612 
   613 ; print (range)
   614 ; Use this range information to grab all the values in a
   615 ; particular range, and then take an average.
   616 
   617   nx = dimsizes(range) - 1
   618 
   619 ;==============================
   620 ; put data into bins
   621 ;==============================
   622 
   623 ; using observed biome class
   624 ; base  = ndtooned(classob)
   625 ; using model biome class
   626   base  = ndtooned(classmod)
   627 
   628 ; output
   629 
   630   yvalues = new((/ntime,data_n,nx/),float)
   631 
   632 ; Loop through each range, using base.
   633 
   634   do i=0,nx-1
   635 
   636      if (i.ne.(nx-1)) then
   637         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
   638      else
   639         idx = ind(base.ge.range(i))
   640      end if
   641 
   642   do n = 0,data_n-1
   643 
   644      t = -1
   645      do m = 0,nyear-1
   646      do k = 0,nmonth-1
   647     
   648         t = t + 1 
   649 
   650         if (n.eq.0) then
   651            data = ndtooned(NPP3(m,k,:,:))
   652         end if
   653 
   654         if (n.eq.1) then
   655            data = ndtooned(NEE3(m,k,:,:))
   656         end if
   657 
   658         if (n.eq.2) then
   659            data = ndtooned(Fire(m,k,:,:))
   660         end if
   661 
   662 ;       Calculate average
   663  
   664         if (.not.any(ismissing(idx))) then 
   665            yvalues(t,n,i) = sum(data(idx)*area_1d(idx))
   666         else
   667            yvalues(t,n,i) = yvalues@_FillValue
   668         end if
   669 
   670 ;#############################################################
   671 ; using model biome class:
   672 ;     set the following 4 classes to _FillValue:
   673 ;     (3)Needleleaf Deciduous Boreal Tree,
   674 ;     (8)Broadleaf Deciduous Boreal Tree,
   675 ;     (9)Broadleaf Evergreen Shrub,
   676 ;     (16)Wheat
   677 
   678         if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   679            yvalues(t,n,i) = yvalues@_FillValue
   680         end if
   681 ;#############################################################  
   682 
   683      end do
   684      end do
   685 
   686      delete(data)
   687   end do 
   688 
   689     delete(idx)
   690   end do
   691 
   692   delete (base)
   693   delete (NPP3)
   694   delete (NEE3)
   695   delete (Fire)
   696 
   697 ;----------------------------------------------------------------
   698 ; data for tseries plot
   699 
   700   yvalues_g = new((/ntime,data_n,n_biome/),float)
   701 
   702   yvalues_g@units = "TgC/month"
   703 
   704 ; change unit to Tg C/month
   705 ; change unit from g to Tg (Tera gram)
   706   factor_unit = 1.e-12
   707 
   708   yvalues_g = yvalues(:,:,good) * factor_unit
   709 
   710 ;*******************************************************************
   711 ; general settings for line plot
   712 ;*******************************************************************
   713 
   714 ; res
   715   res                   = True               
   716   res@xyDashPatterns    = (/0/)                ; make lines solid
   717   res@xyLineThicknesses = (/2.0/)          ; make lines thicker
   718   res@xyLineColors      = (/"blue"/) ; line color
   719 
   720   res@trXMinF   = year_start
   721   res@trXMaxF   = year_end + 1
   722 
   723   res@vpHeightF = 0.4                 ; change aspect ratio of plot
   724 ; res@vpWidthF  = 0.8
   725   res@vpWidthF  = 0.75   
   726 
   727 ; res@gsnMaximize = True
   728 
   729 ;*******************************************************************
   730 ; (A) 1 component in each biome: monthly
   731 ;*******************************************************************
   732 
   733   component = (/"NPP","NEE","Fire"/)
   734 
   735 ; for x-axis in xyplot
   736 
   737   timeI = new((/ntime/),integer)
   738   timeF = new((/ntime/),float)
   739   timeI = ispan(1,ntime,1)
   740   timeF = year_start + (timeI-1)/12.
   741   timeF@long_name = "year" 
   742 
   743   plot_data = new((/ntime/),float)
   744   plot_data@long_name = "TgC/month"
   745  
   746   do n = 0, data_n-1
   747   do m = 0, n_biome-1
   748 
   749      plot_name = component(n)+"_monthly_biome_"+ m
   750 
   751      wks = gsn_open_wks (plot_type,plot_name)   
   752 
   753      title = component(n)+ ": "+ row_head(m)
   754      res@tiMainString = title
   755      res@tiMainFontHeightF = 0.025
   756 
   757      plot_data(:) = yvalues_g(:,n,m)
   758                                  
   759      plot=gsn_csm_xy(wks,timeF,plot_data,res)
   760 
   761     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   762            "rm "+plot_name+"."+plot_type)
   763 
   764      clear (wks)  
   765      delete (plot)   
   766   end do
   767   end do
   768 
   769   do n = 0, data_n-1
   770 
   771      plot_name = component(n)+"_monthly_global"
   772 
   773      wks = gsn_open_wks (plot_type,plot_name)   
   774 
   775      title = component(n)+ ": Global"
   776      res@tiMainString = title
   777      res@tiMainFontHeightF = 0.025
   778  
   779      do k = 0,ntime-1
   780         plot_data(k) = sum(yvalues_g(k,n,:))
   781      end do
   782                                  
   783      plot=gsn_csm_xy(wks,timeF,plot_data,res)
   784 
   785     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   786            "rm "+plot_name+"."+plot_type)
   787 
   788      clear (wks)  
   789      delete (plot)   
   790   end do
   791 
   792   delete (plot_data)
   793   delete (timeI)
   794   delete (timeF)
   795 
   796 ;*******************************************************************
   797 ; (B) 1 component in each biome: annually
   798 ;*******************************************************************
   799 
   800   yvalues_a = new((/nyear,data_n,n_biome/),float)
   801   yvalues_g!0 = "time"
   802   yvalues_g!1 = "case"
   803   yvalues_g!2 = "record"
   804 
   805   yvalues_a = month_to_annual(yvalues_g,0)
   806 
   807   delete (yvalues_g) 
   808 
   809 ; for x-axis in xyplot
   810 
   811   timeI = new((/nyear/),integer)
   812   timeF = new((/nyear/),float)
   813   timeI = ispan(1,nyear,1)
   814   timeF = year_start + (timeI-1)
   815   timeF@long_name = "year" 
   816 
   817   plot_data = new((/nyear/),float)
   818   plot_data@long_name = "TgC/year"
   819  
   820   do n = 0, data_n-1
   821   do m = 0, n_biome-1
   822 
   823      plot_name = component(n)+"_annual_biome_"+ m
   824 
   825      wks = gsn_open_wks (plot_type,plot_name)   
   826 
   827      title = component(n)+ ": "+ row_head(m)
   828      res@tiMainString = title
   829      res@tiMainFontHeightF = 0.025
   830 
   831      plot_data(:) = yvalues_a(:,n,m)
   832                                  
   833      plot=gsn_csm_xy(wks,timeF,plot_data,res)
   834 
   835     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   836            "rm "+plot_name+"."+plot_type)
   837 
   838      clear (wks)  
   839      delete (plot)   
   840   end do
   841   end do
   842 
   843   do n = 0, data_n-1
   844 
   845      plot_name = component(n)+"_annual_global"
   846 
   847      wks = gsn_open_wks (plot_type,plot_name)   
   848 
   849      title = component(n)+ ": Global"
   850      res@tiMainString = title
   851      res@tiMainFontHeightF = 0.025
   852  
   853      do k = 0,nyear-1
   854         plot_data(k) = sum(yvalues_a(k,n,:))
   855      end do
   856                                  
   857      plot=gsn_csm_xy(wks,timeF,plot_data,res)
   858 
   859     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   860            "rm "+plot_name+"."+plot_type)
   861 
   862      clear (wks)  
   863      delete (plot)   
   864   end do
   865 
   866 end
   867