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