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