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
forrest@0
     1
;********************************************************
forrest@0
     2
;using model biome vlass
forrest@0
     3
;
forrest@0
     4
; required command line input parameters:
forrest@0
     5
;  ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
forrest@0
     6
;
forrest@0
     7
; histogram normalized by rain and compute correleration
forrest@0
     8
;**************************************************************
forrest@0
     9
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
forrest@0
    10
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
forrest@0
    11
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
forrest@0
    12
;**************************************************************
forrest@0
    13
procedure set_line(lines:string,nline:integer,newlines:string) 
forrest@0
    14
begin
forrest@0
    15
; add line to ascci/html file
forrest@0
    16
    
forrest@0
    17
  nnewlines = dimsizes(newlines)
forrest@0
    18
  if(nline+nnewlines-1.ge.dimsizes(lines))
forrest@0
    19
    print("set_line: bad index, not setting anything.") 
forrest@0
    20
    return
forrest@0
    21
  end if 
forrest@0
    22
  lines(nline:nline+nnewlines-1) = newlines
forrest@0
    23
;  print ("lines = " + lines(nline:nline+nnewlines-1))
forrest@0
    24
  nline = nline + nnewlines
forrest@0
    25
  return 
forrest@0
    26
end
forrest@0
    27
;**************************************************************
forrest@0
    28
; Main code.
forrest@0
    29
begin
forrest@0
    30
 
forrest@0
    31
 plot_type     = "ps"
forrest@0
    32
 plot_type_new = "png"
forrest@0
    33
forrest@0
    34
;************************************************
forrest@0
    35
; model name and grid       
forrest@0
    36
;************************************************
forrest@0
    37
forrest@0
    38
 model_grid = "T42"
forrest@0
    39
forrest@0
    40
 model_name  = "cn"
forrest@0
    41
 model_name1 = "i01.06cn"
forrest@0
    42
 model_name2 = "i01.10cn"
forrest@0
    43
forrest@0
    44
;---------------------------------------------------
forrest@0
    45
; get biome data: model
forrest@0
    46
forrest@0
    47
  biome_name_mod = "Model PFT Class"
forrest@0
    48
forrest@0
    49
  dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
    50
  film = "class_pft_"+model_grid+".nc"
forrest@0
    51
forrest@0
    52
  fm = addfile(dirm+film,"r")
forrest@0
    53
 
forrest@0
    54
  classmod = fm->CLASS_PFT
forrest@0
    55
forrest@0
    56
  delete (fm)
forrest@0
    57
forrest@0
    58
; model data has 17 land-type classes
forrest@0
    59
forrest@0
    60
  nclass_mod = 17
forrest@0
    61
forrest@0
    62
;--------------------------------------------------
forrest@0
    63
; get model data: landfrac and area
forrest@0
    64
forrest@0
    65
 dirm_l= "/fis/cgd/cseg/people/jeff/surface_data/" 
forrest@0
    66
 film_l = "lnd_T42.nc"
forrest@0
    67
 fm_l   = addfile (dirm_l+film_l,"r")
forrest@0
    68
  
forrest@0
    69
 landfrac = fm_l->landfrac
forrest@0
    70
 area     = fm_l->area
forrest@0
    71
forrest@0
    72
; change area from km**2 to m**2
forrest@0
    73
  area = area * 1.e6             
forrest@0
    74
forrest@0
    75
;---------------------------------------------------
forrest@0
    76
; take into account landfrac
forrest@0
    77
forrest@0
    78
  area = area * landfrac
forrest@0
    79
forrest@0
    80
  delete (landfrac)
forrest@0
    81
forrest@0
    82
;---------------------------------------------------
forrest@0
    83
; read data: model, group 1
forrest@0
    84
forrest@0
    85
 dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
    86
 film = model_name1 + "_1980-2004_ANN_climo.nc"
forrest@0
    87
 fm   = addfile (dirm+film,"r")
forrest@0
    88
 
forrest@0
    89
 NPP1   = fm->NPP
forrest@0
    90
forrest@0
    91
 leafc  = fm->LEAFC
forrest@0
    92
 woodc  = fm->WOODC
forrest@0
    93
 frootc = fm->FROOTC
forrest@0
    94
 VegC   = leafc
forrest@0
    95
 VegC   = leafc + woodc + frootc
forrest@0
    96
forrest@0
    97
 litterc = fm->LITTERC
forrest@0
    98
 cwdc    = fm->CWDC
forrest@0
    99
 LiCwC   = litterc
forrest@0
   100
 LiCwC   = litterc + cwdc
forrest@0
   101
forrest@0
   102
 SoilC   = fm->SOILC
forrest@0
   103
forrest@0
   104
 delete (fm)
forrest@0
   105
;--------------------------------------------------- 
forrest@0
   106
; read data: model, group 2
forrest@0
   107
forrest@0
   108
 dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
   109
 film = model_name2 + "_1990-2004_ANN_climo.nc"
forrest@0
   110
 fm   = addfile (dirm+film,"r")
forrest@0
   111
forrest@0
   112
 NPP2   = fm->NPP 
forrest@0
   113
 NEE2   = fm->NEE 
forrest@0
   114
forrest@0
   115
;---------------------------------------------------
forrest@0
   116
; Units for these variables are:
forrest@0
   117
forrest@0
   118
;NPP1: g C/m^2/s
forrest@0
   119
;NPP2: g C/m^2/s
forrest@0
   120
;NEE2: g C/m^2/s
forrest@0
   121
forrest@0
   122
;VegC:  g C/m^2
forrest@0
   123
;LiCwC: g C/m^2
forrest@0
   124
;SoilC: g C/m^2
forrest@0
   125
forrest@0
   126
 nsec_per_year = 60*60*24*365
forrest@0
   127
forrest@0
   128
; change unit to g C/m^2/year
forrest@0
   129
  
forrest@0
   130
 NPP1 = NPP1 *  nsec_per_year
forrest@0
   131
 NPP2 = NPP2 *  nsec_per_year
forrest@0
   132
 NEE2 = NEE2 *  nsec_per_year 
forrest@0
   133
forrest@0
   134
 data_n = 7
forrest@0
   135
                
forrest@0
   136
;*******************************************************************
forrest@0
   137
; Calculate "nice" bins for binning the data in equally spaced ranges
forrest@0
   138
;********************************************************************
forrest@0
   139
forrest@0
   140
; using model biome class
forrest@0
   141
  nclass      = nclass_mod
forrest@0
   142
forrest@0
   143
  range       = fspan(0,nclass,nclass+1)
forrest@0
   144
forrest@0
   145
; print (range)
forrest@0
   146
; Use this range information to grab all the values in a
forrest@0
   147
; particular range, and then take an average.
forrest@0
   148
forrest@0
   149
  nx           = dimsizes(range) - 1
forrest@0
   150
forrest@0
   151
;==============================
forrest@0
   152
; put data into bins
forrest@0
   153
;==============================
forrest@0
   154
forrest@0
   155
; using observed biome class
forrest@0
   156
; base  = ndtooned(classob)
forrest@0
   157
; using model biome class
forrest@0
   158
  base  = ndtooned(classmod)
forrest@0
   159
forrest@0
   160
  area_1d = ndtooned(area)
forrest@0
   161
forrest@0
   162
; output
forrest@0
   163
forrest@0
   164
  yvalues   = new((/data_n,nx/),float)
forrest@0
   165
  yvalues_t = new((/data_n,nx/),float)
forrest@0
   166
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) = "<a href=./Fire_monthly_biome_"+i+".png>monthly_plot</a> <br> <a href=./Fire_annual_biome_"+i+".png>annual_plot</a>"
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) = "<a href=./Fire_monthly_global.png>monthly_plot</a> <br> <a href=./Fire_annual_global.png>annual_plot</a>"
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
forrest@0
   596
 dsizes = dimsizes(NPP3)
forrest@0
   597
 nyear  = dsizes(0)
forrest@0
   598
 nmonth = dsizes(1)
forrest@0
   599
 ntime  = nyear * nmonth
forrest@0
   600
forrest@0
   601
 year_start = 1979
forrest@0
   602
 year_end   = 2004
forrest@0
   603
                
forrest@0
   604
;*******************************************************************
forrest@0
   605
; Calculate "nice" bins for binning the data in equally spaced ranges
forrest@0
   606
;********************************************************************
forrest@0
   607
forrest@0
   608
; using model biome class
forrest@0
   609
  nclass = nclass_mod
forrest@0
   610
forrest@0
   611
  range  = fspan(0,nclass,nclass+1)
forrest@0
   612
forrest@0
   613
; print (range)
forrest@0
   614
; Use this range information to grab all the values in a
forrest@0
   615
; particular range, and then take an average.
forrest@0
   616
forrest@0
   617
  nx = dimsizes(range) - 1
forrest@0
   618
forrest@0
   619
;==============================
forrest@0
   620
; put data into bins
forrest@0
   621
;==============================
forrest@0
   622
forrest@0
   623
; using observed biome class
forrest@0
   624
; base  = ndtooned(classob)
forrest@0
   625
; using model biome class
forrest@0
   626
  base  = ndtooned(classmod)
forrest@0
   627
forrest@0
   628
; output
forrest@0
   629
forrest@0
   630
  yvalues = new((/ntime,data_n,nx/),float)
forrest@0
   631
forrest@0
   632
; Loop through each range, using base.
forrest@0
   633
forrest@0
   634
  do i=0,nx-1
forrest@0
   635
forrest@0
   636
     if (i.ne.(nx-1)) then
forrest@0
   637
        idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
forrest@0
   638
     else
forrest@0
   639
        idx = ind(base.ge.range(i))
forrest@0
   640
     end if
forrest@0
   641
forrest@0
   642
  do n = 0,data_n-1
forrest@0
   643
forrest@0
   644
     t = -1
forrest@0
   645
     do m = 0,nyear-1
forrest@0
   646
     do k = 0,nmonth-1
forrest@0
   647
    
forrest@0
   648
        t = t + 1 
forrest@0
   649
forrest@0
   650
        if (n.eq.0) then
forrest@0
   651
           data = ndtooned(NPP3(m,k,:,:))
forrest@0
   652
        end if
forrest@0
   653
forrest@0
   654
        if (n.eq.1) then
forrest@0
   655
           data = ndtooned(NEE3(m,k,:,:))
forrest@0
   656
        end if
forrest@0
   657
forrest@0
   658
        if (n.eq.2) then
forrest@0
   659
           data = ndtooned(Fire(m,k,:,:))
forrest@0
   660
        end if
forrest@0
   661
forrest@0
   662
;       Calculate average
forrest@0
   663
 
forrest@0
   664
        if (.not.any(ismissing(idx))) then 
forrest@0
   665
           yvalues(t,n,i) = sum(data(idx)*area_1d(idx))
forrest@0
   666
        else
forrest@0
   667
           yvalues(t,n,i) = yvalues@_FillValue
forrest@0
   668
        end if
forrest@0
   669
forrest@0
   670
;#############################################################
forrest@0
   671
; using model biome class:
forrest@0
   672
;     set the following 4 classes to _FillValue:
forrest@0
   673
;     (3)Needleleaf Deciduous Boreal Tree,
forrest@0
   674
;     (8)Broadleaf Deciduous Boreal Tree,
forrest@0
   675
;     (9)Broadleaf Evergreen Shrub,
forrest@0
   676
;     (16)Wheat
forrest@0
   677
forrest@0
   678
        if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
forrest@0
   679
           yvalues(t,n,i) = yvalues@_FillValue
forrest@0
   680
        end if
forrest@0
   681
;#############################################################  
forrest@0
   682
forrest@0
   683
     end do
forrest@0
   684
     end do
forrest@0
   685
forrest@0
   686
     delete(data)
forrest@0
   687
  end do 
forrest@0
   688
forrest@0
   689
    delete(idx)
forrest@0
   690
  end do
forrest@0
   691
forrest@0
   692
  delete (base)
forrest@0
   693
  delete (NPP3)
forrest@0
   694
  delete (NEE3)
forrest@0
   695
  delete (Fire)
forrest@0
   696
forrest@0
   697
;----------------------------------------------------------------
forrest@0
   698
; data for tseries plot
forrest@0
   699
forrest@0
   700
  yvalues_g = new((/ntime,data_n,n_biome/),float)
forrest@0
   701
forrest@0
   702
  yvalues_g@units = "TgC/month"
forrest@0
   703
forrest@0
   704
; change unit to Tg C/month
forrest@0
   705
; change unit from g to Tg (Tera gram)
forrest@0
   706
  factor_unit = 1.e-12
forrest@0
   707
forrest@0
   708
  yvalues_g = yvalues(:,:,good) * factor_unit
forrest@0
   709
forrest@0
   710
;*******************************************************************
forrest@0
   711
; general settings for line plot
forrest@0
   712
;*******************************************************************
forrest@0
   713
forrest@0
   714
; res
forrest@0
   715
  res                   = True               
forrest@0
   716
  res@xyDashPatterns    = (/0/)                ; make lines solid
forrest@0
   717
  res@xyLineThicknesses = (/2.0/)          ; make lines thicker
forrest@0
   718
  res@xyLineColors      = (/"blue"/) ; line color
forrest@0
   719
forrest@0
   720
  res@trXMinF   = year_start
forrest@0
   721
  res@trXMaxF   = year_end + 1
forrest@0
   722
forrest@0
   723
  res@vpHeightF = 0.4                 ; change aspect ratio of plot
forrest@0
   724
; res@vpWidthF  = 0.8
forrest@0
   725
  res@vpWidthF  = 0.75   
forrest@0
   726
forrest@0
   727
; res@gsnMaximize = True
forrest@0
   728
forrest@0
   729
;*******************************************************************
forrest@0
   730
; (A) 1 component in each biome: monthly
forrest@0
   731
;*******************************************************************
forrest@0
   732
forrest@0
   733
  component = (/"NPP","NEE","Fire"/)
forrest@0
   734
forrest@0
   735
; for x-axis in xyplot
forrest@0
   736
forrest@0
   737
  timeI = new((/ntime/),integer)
forrest@0
   738
  timeF = new((/ntime/),float)
forrest@0
   739
  timeI = ispan(1,ntime,1)
forrest@0
   740
  timeF = year_start + (timeI-1)/12.
forrest@0
   741
  timeF@long_name = "year" 
forrest@0
   742
forrest@0
   743
  plot_data = new((/ntime/),float)
forrest@0
   744
  plot_data@long_name = "TgC/month"
forrest@0
   745
 
forrest@0
   746
  do n = 0, data_n-1
forrest@0
   747
  do m = 0, n_biome-1
forrest@0
   748
forrest@0
   749
     plot_name = component(n)+"_monthly_biome_"+ m
forrest@0
   750
forrest@0
   751
     wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   752
forrest@0
   753
     title = component(n)+ ": "+ row_head(m)
forrest@0
   754
     res@tiMainString = title
forrest@0
   755
     res@tiMainFontHeightF = 0.025
forrest@0
   756
forrest@0
   757
     plot_data(:) = yvalues_g(:,n,m)
forrest@0
   758
                                 
forrest@0
   759
     plot=gsn_csm_xy(wks,timeF,plot_data,res)
forrest@0
   760
forrest@0
   761
    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   762
           "rm "+plot_name+"."+plot_type)
forrest@0
   763
forrest@0
   764
     clear (wks)  
forrest@0
   765
     delete (plot)   
forrest@0
   766
  end do
forrest@0
   767
  end do
forrest@0
   768
forrest@0
   769
  do n = 0, data_n-1
forrest@0
   770
forrest@0
   771
     plot_name = component(n)+"_monthly_global"
forrest@0
   772
forrest@0
   773
     wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   774
forrest@0
   775
     title = component(n)+ ": Global"
forrest@0
   776
     res@tiMainString = title
forrest@0
   777
     res@tiMainFontHeightF = 0.025
forrest@0
   778
 
forrest@0
   779
     do k = 0,ntime-1
forrest@0
   780
        plot_data(k) = sum(yvalues_g(k,n,:))
forrest@0
   781
     end do
forrest@0
   782
                                 
forrest@0
   783
     plot=gsn_csm_xy(wks,timeF,plot_data,res)
forrest@0
   784
forrest@0
   785
    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   786
           "rm "+plot_name+"."+plot_type)
forrest@0
   787
forrest@0
   788
     clear (wks)  
forrest@0
   789
     delete (plot)   
forrest@0
   790
  end do
forrest@0
   791
forrest@0
   792
  delete (plot_data)
forrest@0
   793
  delete (timeI)
forrest@0
   794
  delete (timeF)
forrest@0
   795
forrest@0
   796
;*******************************************************************
forrest@0
   797
; (B) 1 component in each biome: annually
forrest@0
   798
;*******************************************************************
forrest@0
   799
forrest@0
   800
  yvalues_a = new((/nyear,data_n,n_biome/),float)
forrest@0
   801
  yvalues_g!0 = "time"
forrest@0
   802
  yvalues_g!1 = "case"
forrest@0
   803
  yvalues_g!2 = "record"
forrest@0
   804
forrest@0
   805
  yvalues_a = month_to_annual(yvalues_g,0)
forrest@0
   806
forrest@0
   807
  delete (yvalues_g) 
forrest@0
   808
forrest@0
   809
; for x-axis in xyplot
forrest@0
   810
forrest@0
   811
  timeI = new((/nyear/),integer)
forrest@0
   812
  timeF = new((/nyear/),float)
forrest@0
   813
  timeI = ispan(1,nyear,1)
forrest@0
   814
  timeF = year_start + (timeI-1)
forrest@0
   815
  timeF@long_name = "year" 
forrest@0
   816
forrest@0
   817
  plot_data = new((/nyear/),float)
forrest@0
   818
  plot_data@long_name = "TgC/year"
forrest@0
   819
 
forrest@0
   820
  do n = 0, data_n-1
forrest@0
   821
  do m = 0, n_biome-1
forrest@0
   822
forrest@0
   823
     plot_name = component(n)+"_annual_biome_"+ m
forrest@0
   824
forrest@0
   825
     wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   826
forrest@0
   827
     title = component(n)+ ": "+ row_head(m)
forrest@0
   828
     res@tiMainString = title
forrest@0
   829
     res@tiMainFontHeightF = 0.025
forrest@0
   830
forrest@0
   831
     plot_data(:) = yvalues_a(:,n,m)
forrest@0
   832
                                 
forrest@0
   833
     plot=gsn_csm_xy(wks,timeF,plot_data,res)
forrest@0
   834
forrest@0
   835
    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   836
           "rm "+plot_name+"."+plot_type)
forrest@0
   837
forrest@0
   838
     clear (wks)  
forrest@0
   839
     delete (plot)   
forrest@0
   840
  end do
forrest@0
   841
  end do
forrest@0
   842
forrest@0
   843
  do n = 0, data_n-1
forrest@0
   844
forrest@0
   845
     plot_name = component(n)+"_annual_global"
forrest@0
   846
forrest@0
   847
     wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   848
forrest@0
   849
     title = component(n)+ ": Global"
forrest@0
   850
     res@tiMainString = title
forrest@0
   851
     res@tiMainFontHeightF = 0.025
forrest@0
   852
 
forrest@0
   853
     do k = 0,nyear-1
forrest@0
   854
        plot_data(k) = sum(yvalues_a(k,n,:))
forrest@0
   855
     end do
forrest@0
   856
                                 
forrest@0
   857
     plot=gsn_csm_xy(wks,timeF,plot_data,res)
forrest@0
   858
forrest@0
   859
    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   860
           "rm "+plot_name+"."+plot_type)
forrest@0
   861
forrest@0
   862
     clear (wks)  
forrest@0
   863
     delete (plot)   
forrest@0
   864
  end do
forrest@0
   865
forrest@0
   866
end
forrest@0
   867