all/08.turnover.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
; hardwire: flux = flux/1200. (for casa only)
forrest@0
     3
;**************************************************************
forrest@0
     4
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
forrest@0
     5
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
forrest@0
     6
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
forrest@0
     7
;**************************************************************
forrest@0
     8
procedure set_line(lines:string,nline:integer,newlines:string) 
forrest@0
     9
begin
forrest@0
    10
; add line to ascci/html file
forrest@0
    11
    
forrest@0
    12
  nnewlines = dimsizes(newlines)
forrest@0
    13
  if(nline+nnewlines-1.ge.dimsizes(lines))
forrest@0
    14
    print("set_line: bad index, not setting anything.") 
forrest@0
    15
    return
forrest@0
    16
  end if 
forrest@0
    17
  lines(nline:nline+nnewlines-1) = newlines
forrest@0
    18
;  print ("lines = " + lines(nline:nline+nnewlines-1))
forrest@0
    19
  nline = nline + nnewlines
forrest@0
    20
  return 
forrest@0
    21
end
forrest@0
    22
;**************************************************************
forrest@0
    23
; Main code.
forrest@0
    24
begin
forrest@0
    25
 
forrest@0
    26
 plot_type     = "ps"
forrest@0
    27
 plot_type_new = "png"
forrest@0
    28
forrest@0
    29
;------------------------------------------------------
forrest@0
    30
; edit table.html of current model for movel1_vs_model2
forrest@0
    31
forrest@0
    32
 if (isvar("compare")) then
forrest@0
    33
    html_name2 = compare+"/table.html"  
forrest@0
    34
    html_new2  = html_name2 +".new"
forrest@0
    35
 end if
forrest@0
    36
forrest@0
    37
;------------------------------------------------------
forrest@0
    38
; edit table.html for current model
forrest@0
    39
forrest@0
    40
 html_name = model_name+"/table.html"  
forrest@0
    41
 html_new  = html_name +".new"
forrest@0
    42
forrest@0
    43
;---------------------------------------------------------------
forrest@0
    44
;components
forrest@0
    45
forrest@0
    46
 component = (/"Leaf","Wood","Fine_Root","Litter","Coarse_Woody_Debris","Soil"/)
forrest@0
    47
 n_comp = dimsizes(component)
forrest@0
    48
forrest@0
    49
 field_pool = (/"LEAFC","WOODC","FROOTC","LITTERC","CWDC","SOILC"/)
forrest@0
    50
 field_flux = (/"LEAFC_ALLOC","WOODC_ALLOC","FROOTC_ALLOC","LITTERC_LOSS","CWDC_LOSS","SOILC_HR"/)
forrest@0
    51
forrest@0
    52
;--------------------------------------------------
forrest@0
    53
; get landfrac data
forrest@0
    54
 
forrest@0
    55
 film_l   = "lnd_"+ model_grid +".nc"
forrest@0
    56
 fm_l     = addfile (dirs+film_l,"r")  
forrest@0
    57
 landfrac = fm_l->landfrac
forrest@0
    58
forrest@0
    59
 delete (fm_l)
forrest@0
    60
;---------------------------------------------------
forrest@0
    61
; read biome data: model
forrest@0
    62
forrest@0
    63
  biome_name_mod = "Model PFT Class"
forrest@0
    64
forrest@0
    65
  film_c   = "class_pft_"+ model_grid +".nc"
forrest@0
    66
  fm_c     = addfile (dirs+film_c,"r") 
forrest@0
    67
  classmod = fm_c->CLASS_PFT
forrest@0
    68
forrest@0
    69
  delete (fm_c)
forrest@0
    70
forrest@0
    71
; model data has 17 land-type classes
forrest@0
    72
  nclass_mod = 17
forrest@0
    73
forrest@0
    74
;********************************************************************
forrest@0
    75
; use land-type class to bin the data in equally spaced ranges
forrest@0
    76
;********************************************************************
forrest@0
    77
forrest@0
    78
; using model biome class
forrest@0
    79
  nclass = nclass_mod
forrest@0
    80
forrest@0
    81
  range  = fspan(0,nclass,nclass+1)
forrest@0
    82
forrest@0
    83
; Use this range information to grab all the values in a
forrest@0
    84
; particular range, and then take an average.
forrest@0
    85
forrest@0
    86
  nx = dimsizes(range) - 1
forrest@0
    87
forrest@0
    88
; for 2 data: pool and flux
forrest@0
    89
  data_n = 2
forrest@0
    90
forrest@0
    91
; using model biome class
forrest@0
    92
forrest@0
    93
  base = ndtooned(classmod)
forrest@0
    94
forrest@0
    95
; output
forrest@0
    96
forrest@0
    97
  yvalues = new((/data_n,nx/),float)
forrest@0
    98
  count   = new((/data_n,nx/),float)
forrest@0
    99
forrest@0
   100
;--------------------------------------------------
forrest@0
   101
; read model data, each component:      
forrest@0
   102
forrest@0
   103
 fm = addfile (dirm+film4,"r")
forrest@0
   104
forrest@0
   105
 do k = 0,n_comp-1
forrest@0
   106
forrest@0
   107
    pool  = fm->$field_pool(k)$
forrest@0
   108
    flux  = fm->$field_flux(k)$
forrest@0
   109
forrest@0
   110
;   Units for these variables are:
forrest@0
   111
;   pool: g C/m^2
forrest@0
   112
;   flux: g C/m^2/s
forrest@0
   113
forrest@0
   114
    nsec_per_year = 60*60*24*365
forrest@0
   115
  
forrest@0
   116
    flux = flux *  nsec_per_year 
forrest@0
   117
forrest@0
   118
    unit_p = "gC/m2"
forrest@0
   119
    unit_f = "gC/m2/year"
forrest@0
   120
    unit_t = "year"
forrest@0
   121
forrest@0
   122
;#############################################################
forrest@0
   123
;   casa only
forrest@0
   124
;   all the plant pools (leaf, wood, and fine root) and
forrest@0
   125
;   coarse woody debris (cwd) and litter pools for
forrest@0
   126
;   CASA need to be divided by 1200.  The soil flux
forrest@0
   127
;   and turnover time are fine and do not need to be adjusted.
forrest@0
   128
forrest@0
   129
    if (BGC .eq. "casa") then   
forrest@0
   130
       if (k .ne. n_comp-1) then
forrest@0
   131
          flux = flux/1200.
forrest@0
   132
       end if    
forrest@0
   133
    end if
forrest@0
   134
;##############################################################
forrest@0
   135
forrest@0
   136
;   take into account landfrac
forrest@0
   137
forrest@0
   138
    pool = pool * conform(pool,landfrac,(/1,2/))
forrest@0
   139
    flux = flux * conform(flux,landfrac,(/1,2/))
forrest@0
   140
forrest@0
   141
; Loop through each range, using base
forrest@0
   142
forrest@0
   143
  do i=0,nx-1
forrest@0
   144
forrest@0
   145
     if (i.ne.(nx-1)) then
forrest@0
   146
        idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
forrest@0
   147
     else
forrest@0
   148
        idx = ind(base.ge.range(i))
forrest@0
   149
     end if
forrest@0
   150
forrest@0
   151
;    loop through each dataset
forrest@0
   152
 
forrest@0
   153
     do n = 0,data_n-1
forrest@0
   154
forrest@0
   155
        if (n .eq. 0) then
forrest@0
   156
           data = ndtooned(pool)
forrest@0
   157
        end if
forrest@0
   158
forrest@0
   159
        if (n .eq. 1) then
forrest@0
   160
           data = ndtooned(flux)
forrest@0
   161
        end if
forrest@0
   162
forrest@0
   163
;       Calculate average 
forrest@0
   164
forrest@0
   165
        if (.not.any(ismissing(idx))) then
forrest@0
   166
           yvalues(n,i) = avg(data(idx))
forrest@0
   167
           count(n,i)   = dimsizes(idx)
forrest@0
   168
        else
forrest@0
   169
           yvalues(n,i) = yvalues@_FillValue
forrest@0
   170
           count(n,i)   = 0
forrest@0
   171
        end if
forrest@0
   172
forrest@0
   173
;#############################################################
forrest@0
   174
; using model biome class:
forrest@0
   175
;
forrest@0
   176
;     set the following 4 classes to _FillValue:
forrest@0
   177
;     (3)Needleleaf Deciduous Boreal Tree,
forrest@0
   178
;     (8)Broadleaf Deciduous Boreal Tree,
forrest@0
   179
;     (9)Broadleaf Evergreen Shrub,
forrest@0
   180
;     (16)Wheat
forrest@0
   181
forrest@0
   182
      if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
forrest@0
   183
         yvalues(n,i) = yvalues@_FillValue
forrest@0
   184
         count(n,i)   = 0
forrest@0
   185
      end if
forrest@0
   186
;############################################################# 
forrest@0
   187
forrest@0
   188
      delete(data)
forrest@0
   189
    end do                 ; n-loop
forrest@0
   190
forrest@0
   191
    delete(idx)
forrest@0
   192
  end do                   ; i-loop
forrest@0
   193
forrest@0
   194
  delete (pool)
forrest@0
   195
  delete (flux)
forrest@0
   196
forrest@0
   197
;============================
forrest@0
   198
;compute turnover time
forrest@0
   199
;============================
forrest@0
   200
forrest@0
   201
 u       = yvalues(0,:)
forrest@0
   202
 v       = yvalues(1,:)
forrest@0
   203
 u_count = count(0,:)
forrest@0
   204
 v_count = count(1,:)
forrest@0
   205
forrest@0
   206
 good = ind(.not.ismissing(u) .and. .not.ismissing(v))
forrest@0
   207
forrest@0
   208
 uu       = u(good)
forrest@0
   209
 vv       = v(good)
forrest@0
   210
 uu_count = u_count(good)
forrest@0
   211
 vv_count = v_count(good)  
forrest@0
   212
forrest@0
   213
 n_biome = dimsizes(uu)
forrest@0
   214
 t_biome = new((/n_biome/),float)
forrest@0
   215
forrest@0
   216
 t_biome = uu/vv
forrest@0
   217
forrest@0
   218
 t_biome_avg = sum(uu*uu_count)/sum(vv*vv_count)
forrest@0
   219
forrest@0
   220
;===========================
forrest@0
   221
; for html table - biome
forrest@0
   222
;===========================
forrest@0
   223
forrest@0
   224
  output_html = "table_"+component(k)+".html"
forrest@0
   225
forrest@0
   226
; column (not including header column)
forrest@0
   227
forrest@0
   228
  col_head = (/component(k)+" Flux",component(k)+" Pool",component(k)+" Turnover Time"/)
forrest@0
   229
forrest@0
   230
  ncol = dimsizes(col_head)
forrest@0
   231
forrest@0
   232
; row (not including header row)                   
forrest@0
   233
forrest@0
   234
;----------------------------------------------------
forrest@0
   235
; using model biome class:  
forrest@0
   236
  row_head  = (/"Not Vegetated" \
forrest@0
   237
               ,"Needleleaf Evergreen Temperate Tree" \
forrest@0
   238
               ,"Needleleaf Evergreen Boreal Tree" \
forrest@0
   239
;              ,"Needleleaf Deciduous Boreal Tree" \
forrest@0
   240
               ,"Broadleaf Evergreen Tropical Tree" \
forrest@0
   241
               ,"Broadleaf Evergreen Temperate Tree" \
forrest@0
   242
               ,"Broadleaf Deciduous Tropical Tree" \
forrest@0
   243
               ,"Broadleaf Deciduous Temperate Tree" \
forrest@0
   244
;              ,"Broadleaf Deciduous Boreal Tree" \
forrest@0
   245
;              ,"Broadleaf Evergreen Shrub" \
forrest@0
   246
               ,"Broadleaf Deciduous Temperate Shrub" \
forrest@0
   247
               ,"Broadleaf Deciduous Boreal Shrub" \
forrest@0
   248
               ,"C3 Arctic Grass" \
forrest@0
   249
               ,"C3 Non-Arctic Grass" \
forrest@0
   250
               ,"C4 Grass" \
forrest@0
   251
               ,"Corn" \
forrest@0
   252
;              ,"Wheat" \                      
forrest@0
   253
               ,"All Biome" \                
forrest@0
   254
               /)  
forrest@0
   255
  nrow = dimsizes(row_head)                  
forrest@0
   256
forrest@0
   257
; arrays to be passed to table. 
forrest@0
   258
  text = new ((/nrow, ncol/),string )
forrest@0
   259
 
forrest@0
   260
 do i=0,nrow-2
forrest@0
   261
  text(i,0) = sprintf("%.1f",vv(i))
forrest@0
   262
  text(i,1) = sprintf("%.1f",uu(i))
forrest@0
   263
  text(i,2) = sprintf("%.2f",t_biome(i))
forrest@0
   264
 end do
forrest@0
   265
  text(nrow-1,0) = "-"
forrest@0
   266
  text(nrow-1,1) = "-"
forrest@0
   267
  text(nrow-1,2) = sprintf("%.2f",t_biome_avg)
forrest@0
   268
forrest@0
   269
;**************************************************
forrest@0
   270
; html table
forrest@0
   271
;**************************************************
forrest@0
   272
forrest@0
   273
  header_text = "<H1>"+component(k)+" Turnover Time:  Model "+model_name+"</H1>" 
forrest@0
   274
forrest@0
   275
  header = (/"<HTML>" \
forrest@0
   276
            ,"<HEAD>" \
forrest@0
   277
            ,"<TITLE>CLAMP metrics</TITLE>" \
forrest@0
   278
            ,"</HEAD>" \
forrest@0
   279
            ,header_text \
forrest@0
   280
            /) 
forrest@0
   281
  footer = "</HTML>"
forrest@0
   282
forrest@0
   283
  table_header = (/ \
forrest@0
   284
        "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
forrest@0
   285
       ,"<tr>" \
forrest@0
   286
       ,"   <th bgcolor=DDDDDD >Biome Class</th>" \
forrest@0
   287
       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"<br>("+unit_f+")</th>" \
forrest@0
   288
       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"<br>("+unit_p+")</th>" \
forrest@0
   289
       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"<br>("+unit_t+")</th>" \
forrest@0
   290
       ,"</tr>" \
forrest@0
   291
       /)
forrest@0
   292
  table_footer = "</table>"
forrest@0
   293
  row_header = "<tr>"
forrest@0
   294
  row_footer = "</tr>"
forrest@0
   295
forrest@0
   296
  lines = new(50000,string)
forrest@0
   297
  nline = 0
forrest@0
   298
forrest@0
   299
  set_line(lines,nline,header)
forrest@0
   300
  set_line(lines,nline,table_header)
forrest@0
   301
;-----------------------------------------------
forrest@0
   302
; row of table
forrest@0
   303
forrest@0
   304
  do n = 0,nrow-1
forrest@0
   305
     set_line(lines,nline,row_header)
forrest@0
   306
forrest@0
   307
     txt1  = row_head(n)
forrest@0
   308
     txt2  = text(n,0)
forrest@0
   309
     txt3  = text(n,1)
forrest@0
   310
     txt4  = text(n,2)
forrest@0
   311
forrest@0
   312
     set_line(lines,nline,"<th>"+txt1+"</th>")
forrest@0
   313
     set_line(lines,nline,"<th>"+txt2+"</th>")
forrest@0
   314
     set_line(lines,nline,"<th>"+txt3+"</th>")
forrest@0
   315
     set_line(lines,nline,"<th>"+txt4+"</th>")
forrest@0
   316
forrest@0
   317
     set_line(lines,nline,row_footer)
forrest@0
   318
  end do
forrest@0
   319
;-----------------------------------------------
forrest@0
   320
  set_line(lines,nline,table_footer)
forrest@0
   321
  set_line(lines,nline,footer) 
forrest@0
   322
forrest@0
   323
; Now write to an HTML file
forrest@0
   324
forrest@0
   325
  idx = ind(.not.ismissing(lines))
forrest@0
   326
  if(.not.any(ismissing(idx))) then
forrest@0
   327
    asciiwrite(output_html,lines(idx))
forrest@0
   328
  else
forrest@0
   329
   print ("error?")
forrest@0
   330
  end if
forrest@0
   331
forrest@0
   332
  delete (idx)
forrest@0
   333
forrest@0
   334
  delete (good)
forrest@0
   335
  delete (t_biome)
forrest@0
   336
  delete (text)
forrest@0
   337
forrest@0
   338
 end do          ; k-loop
forrest@0
   339
forrest@0
   340
 delete (fm)
forrest@0
   341
forrest@0
   342
;***************************************************************************
forrest@0
   343
; output plot and html
forrest@0
   344
;***************************************************************************
forrest@0
   345
  output_dir = model_name+"/turnover"
forrest@0
   346
forrest@0
   347
  system("mv *.html " + output_dir) 
forrest@0
   348
;******************************
forrest@0
   349
forrest@0
   350
end
forrest@0
   351