turnover/03.biome_model.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
;components
forrest@0
    35
forrest@0
    36
 component = (/"Leaf","Wood","Fine_Root","Litter","Coarse_Woody_Debris","Soil"/)
forrest@0
    37
 n_comp = dimsizes(component)
forrest@0
    38
forrest@0
    39
 field_pool = (/"LEAFC","WOODC","FROOTC","LITTERC","CWDC","SOILC"/)
forrest@0
    40
 field_flux = (/"LEAFC_ALLOC","WOODC_ALLOC","FROOTC_ALLOC","LITTERC_LOSS","CWDC_LOSS","SOILC_HR"/)
forrest@0
    41
forrest@0
    42
;************************************************
forrest@0
    43
; model name and grid       
forrest@0
    44
;************************************************
forrest@0
    45
forrest@0
    46
 model_grid = "T42"
forrest@0
    47
forrest@0
    48
 model_name = "i01.06cn"
forrest@0
    49
;model_name = "i01.06casa"
forrest@0
    50
forrest@0
    51
;************************************************
forrest@0
    52
; read data: ob       
forrest@0
    53
;************************************************
forrest@0
    54
;------------------------------------------------
forrest@0
    55
; read biome data: observed
forrest@0
    56
forrest@0
    57
; biome_name_ob = "MODIS LandCover"
forrest@0
    58
forrest@0
    59
; diro = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/"
forrest@0
    60
; filo = "land_class_"+model_grid+".nc"
forrest@0
    61
forrest@0
    62
; fo = addfile(diro+filo,"r")
forrest@0
    63
 
forrest@0
    64
; classob = tofloat(fo->LAND_CLASS)               
forrest@0
    65
forrest@0
    66
; delete (fo)
forrest@0
    67
forrest@0
    68
; observed data has 20 land-type classes
forrest@0
    69
forrest@0
    70
; nclass_ob = 20
forrest@0
    71
forrest@0
    72
;---------------------------------------------------
forrest@0
    73
; read biome data: model
forrest@0
    74
forrest@0
    75
  biome_name_mod = "Model PFT Class"
forrest@0
    76
forrest@0
    77
  dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
    78
  film = "class_pft_"+model_grid+".nc"
forrest@0
    79
forrest@0
    80
  fm = addfile(dirm+film,"r")
forrest@0
    81
 
forrest@0
    82
  classmod = fm->CLASS_PFT
forrest@0
    83
forrest@0
    84
  delete (fm)
forrest@0
    85
forrest@0
    86
; model data has 17 land-type classes
forrest@0
    87
forrest@0
    88
  nclass_mod = 17
forrest@0
    89
forrest@0
    90
;---------------------------------------------------
forrest@0
    91
; read data: model, each component
forrest@0
    92
forrest@0
    93
 dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
    94
 film = model_name + "_1980-2004_ANN_climo.nc"
forrest@0
    95
forrest@0
    96
 fm   = addfile (dirm+film,"r")
forrest@0
    97
  
forrest@0
    98
 do k = 0,n_comp-1
forrest@0
    99
forrest@0
   100
 pool  = fm->$field_pool(k)$
forrest@0
   101
 flux  = fm->$field_flux(k)$
forrest@0
   102
forrest@0
   103
;Units for these variables are:
forrest@0
   104
;pool: g C/m^2
forrest@0
   105
;flux: g C/m^2/s
forrest@0
   106
forrest@0
   107
 nsec_per_year = 60*60*24*365
forrest@0
   108
  
forrest@0
   109
 flux = flux *  nsec_per_year 
forrest@0
   110
forrest@0
   111
; casa only
forrest@0
   112
; all the plant pools (leaf, wood, and fine root) and
forrest@0
   113
; coarse woody debris (cwd) and litter pools for
forrest@0
   114
; CASA need to be divided by 1200.  The soil flux
forrest@0
   115
; and turnover time are fine and do not need to be adjusted.
forrest@0
   116
  
forrest@0
   117
; if (k .ne. n_comp-1) then
forrest@0
   118
;     flux = flux/1200.
forrest@0
   119
; end if    
forrest@0
   120
                
forrest@0
   121
;*******************************************************************
forrest@0
   122
; Calculate "nice" bins for binning the data in equally spaced ranges
forrest@0
   123
;********************************************************************
forrest@0
   124
forrest@0
   125
; using observed biome class  
forrest@0
   126
; nclass      = nclass_ob
forrest@0
   127
; using model biome class
forrest@0
   128
  nclass      = nclass_mod
forrest@0
   129
forrest@0
   130
  nclassn     = nclass + 1
forrest@0
   131
  range       = fspan(0,nclassn-1,nclassn)
forrest@0
   132
; print (range)
forrest@0
   133
forrest@0
   134
; Use this range information to grab all the values in a
forrest@0
   135
; particular range, and then take an average.
forrest@0
   136
forrest@0
   137
  nr           = dimsizes(range)
forrest@0
   138
  nx           = nr-1
forrest@0
   139
  xvalues      = new((/2,nx/),float)
forrest@0
   140
  xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
forrest@0
   141
  dx           = xvalues(0,1) - xvalues(0,0)       ; range width
forrest@0
   142
  dx4          = dx/4                              ; 1/4 of the range
forrest@0
   143
  xvalues(1,:) = xvalues(0,:) - dx/5.
forrest@0
   144
forrest@0
   145
;==============================
forrest@0
   146
; put data into bins
forrest@0
   147
;==============================
forrest@0
   148
forrest@0
   149
; using observed biome class
forrest@0
   150
; base_1D  = ndtooned(classob)
forrest@0
   151
; using model biome class
forrest@0
   152
  base_1D  = ndtooned(classmod)
forrest@0
   153
forrest@0
   154
  data1_1D = ndtooned(pool)
forrest@0
   155
  data2_1D = ndtooned(flux)
forrest@0
   156
forrest@0
   157
; output
forrest@0
   158
forrest@0
   159
  yvalues = new((/2,nx/),float)
forrest@0
   160
  count   = new((/2,nx/),float)
forrest@0
   161
forrest@0
   162
  do nd=0,1
forrest@0
   163
forrest@0
   164
;   See if we are doing data1 (nd=0) or data2 (nd=1).
forrest@0
   165
forrest@0
   166
    base = base_1D
forrest@0
   167
forrest@0
   168
    if(nd.eq.0) then
forrest@0
   169
      data = data1_1D
forrest@0
   170
    else
forrest@0
   171
      data = data2_1D
forrest@0
   172
    end if
forrest@0
   173
forrest@0
   174
; Loop through each range, using base.
forrest@0
   175
forrest@0
   176
    do i=0,nr-2
forrest@0
   177
      if (i.ne.(nr-2)) then
forrest@0
   178
;        print("")
forrest@0
   179
;        print("In range ["+range(i)+","+range(i+1)+")")
forrest@0
   180
         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
forrest@0
   181
      else
forrest@0
   182
;        print("")
forrest@0
   183
;        print("In range ["+range(i)+",)")
forrest@0
   184
         idx = ind(base.ge.range(i))
forrest@0
   185
      end if
forrest@0
   186
forrest@0
   187
;     Calculate average
forrest@0
   188
 
forrest@0
   189
      if(.not.any(ismissing(idx))) then
forrest@0
   190
        yvalues(nd,i) = avg(data(idx))
forrest@0
   191
        count(nd,i)   = dimsizes(idx)
forrest@0
   192
      else
forrest@0
   193
        yvalues(nd,i) = yvalues@_FillValue
forrest@0
   194
        count(nd,i)   = 0
forrest@0
   195
      end if
forrest@0
   196
forrest@0
   197
;#############################################################
forrest@0
   198
;using observed biome class: 
forrest@0
   199
;     set the following 4 classes to _FillValue:
forrest@0
   200
;     Water Bodies(0), Urban and Build-Up(13),
forrest@0
   201
;     Permenant Snow and Ice(15), Unclassified(17)
forrest@0
   202
forrest@0
   203
;     if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then
forrest@0
   204
;        yvalues(nd,i) = yvalues@_FillValue
forrest@0
   205
;        count(nd,i)   = 0
forrest@0
   206
;     end if
forrest@0
   207
;############################################################# 
forrest@0
   208
forrest@0
   209
;#############################################################
forrest@0
   210
;using model biome class:
forrest@0
   211
;     set the following 4 classes to _FillValue:
forrest@0
   212
;     (3)Needleleaf Deciduous Boreal Tree,
forrest@0
   213
;     (8)Broadleaf Deciduous Boreal Tree,
forrest@0
   214
;     (9)Broadleaf Evergreen Shrub,
forrest@0
   215
;     (16)Wheat
forrest@0
   216
forrest@0
   217
      if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
forrest@0
   218
         yvalues(nd,i) = yvalues@_FillValue
forrest@0
   219
         count(nd,i)   = 0
forrest@0
   220
      end if
forrest@0
   221
;#############################################################  
forrest@0
   222
forrest@0
   223
;     print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
forrest@0
   224
forrest@0
   225
; Clean up for next time in loop.
forrest@0
   226
forrest@0
   227
      delete(idx)
forrest@0
   228
    end do
forrest@0
   229
forrest@0
   230
    delete(data)
forrest@0
   231
  end do
forrest@0
   232
forrest@0
   233
;============================
forrest@0
   234
;compute turnover time
forrest@0
   235
;============================
forrest@0
   236
forrest@0
   237
 u       = yvalues(0,:)
forrest@0
   238
 v       = yvalues(1,:)
forrest@0
   239
 u_count = count(0,:)
forrest@0
   240
 v_count = count(1,:)
forrest@0
   241
forrest@0
   242
;print (dimsizes(u))
forrest@0
   243
;print (dimsizes(v))
forrest@0
   244
forrest@0
   245
 good = ind(.not.ismissing(u) .and. .not.ismissing(v))
forrest@0
   246
forrest@0
   247
;print (good)
forrest@0
   248
forrest@0
   249
 uu       = u(good)
forrest@0
   250
 vv       = v(good)
forrest@0
   251
forrest@0
   252
;print (dimsizes(uu))
forrest@0
   253
;print (dimsizes(vv))
forrest@0
   254
 
forrest@0
   255
 uu_count = u_count(good)
forrest@0
   256
 vv_count = v_count(good)  
forrest@0
   257
forrest@0
   258
 n_biome = dimsizes(uu)
forrest@0
   259
 t_biome = new((/n_biome/),float)
forrest@0
   260
forrest@0
   261
 t_biome = uu/vv
forrest@0
   262
forrest@0
   263
;t_biome_avg = avg(t_biome)
forrest@0
   264
 t_biome_avg = sum(uu*uu_count)/sum(vv*vv_count)
forrest@0
   265
  
forrest@0
   266
;print (t_biome)
forrest@0
   267
;print (t_biome_avg)
forrest@0
   268
forrest@0
   269
;===========================
forrest@0
   270
; for html table - biome
forrest@0
   271
;===========================
forrest@0
   272
forrest@0
   273
  output_html = "table_"+component(k)+".html"
forrest@0
   274
forrest@0
   275
; column (not including header column)
forrest@0
   276
forrest@0
   277
  col_head = (/component(k)+" Flux",component(k)+" Pool",component(k)+" Turnover Time"/)
forrest@0
   278
forrest@0
   279
  ncol = dimsizes(col_head)
forrest@0
   280
forrest@0
   281
; row (not including header row)                   
forrest@0
   282
forrest@0
   283
;----------------------------------------------------
forrest@0
   284
; using observed biome class:  
forrest@0
   285
; row_head  = (/"Evergreen Needleleaf Forests" \
forrest@0
   286
;              ,"Evergreen Broadleaf Forests" \
forrest@0
   287
;              ,"Deciduous Needleleaf Forest" \
forrest@0
   288
;              ,"Deciduous Broadleaf Forests" \
forrest@0
   289
;              ,"Mixed Forests" \                      
forrest@0
   290
;              ,"Closed Bushlands" \                   
forrest@0
   291
;              ,"Open Bushlands" \                     
forrest@0
   292
;              ,"Woody Savannas (S. Hem.)" \           
forrest@0
   293
;              ,"Savannas (S. Hem.)" \                 
forrest@0
   294
;              ,"Grasslands" \                         
forrest@0
   295
;              ,"Permanent Wetlands" \                 
forrest@0
   296
;              ,"Croplands" \                                           
forrest@0
   297
;              ,"Cropland/Natural Vegetation Mosaic" \             
forrest@0
   298
;              ,"Barren or Sparsely Vegetated" \                             
forrest@0
   299
;              ,"Woody Savannas (N. Hem.)" \           
forrest@0
   300
;              ,"Savannas (N. Hem.)" \
forrest@0
   301
;              ,"All Biome" \                
forrest@0
   302
;              /)
forrest@0
   303
forrest@0
   304
;----------------------------------------------------
forrest@0
   305
; using model biome class:  
forrest@0
   306
  row_head  = (/"Not Vegetated" \
forrest@0
   307
               ,"Needleleaf Evergreen Temperate Tree" \
forrest@0
   308
               ,"Needleleaf Evergreen Boreal Tree" \
forrest@0
   309
;              ,"Needleleaf Deciduous Boreal Tree" \
forrest@0
   310
               ,"Broadleaf Evergreen Tropical Tree" \
forrest@0
   311
               ,"Broadleaf Evergreen Temperate Tree" \
forrest@0
   312
               ,"Broadleaf Deciduous Tropical Tree" \
forrest@0
   313
               ,"Broadleaf Deciduous Temperate Tree" \
forrest@0
   314
;              ,"Broadleaf Deciduous Boreal Tree" \
forrest@0
   315
;              ,"Broadleaf Evergreen Shrub" \
forrest@0
   316
               ,"Broadleaf Deciduous Temperate Shrub" \
forrest@0
   317
               ,"Broadleaf Deciduous Boreal Shrub" \
forrest@0
   318
               ,"C3 Arctic Grass" \
forrest@0
   319
               ,"C3 Non-Arctic Grass" \
forrest@0
   320
               ,"C4 Grass" \
forrest@0
   321
               ,"Corn" \
forrest@0
   322
;              ,"Wheat" \                      
forrest@0
   323
               ,"All Biome" \                
forrest@0
   324
               /)  
forrest@0
   325
  nrow = dimsizes(row_head)                  
forrest@0
   326
forrest@0
   327
; arrays to be passed to table. 
forrest@0
   328
  text4 = new ((/nrow, ncol/),string )
forrest@0
   329
 
forrest@0
   330
 do i=0,nrow-2
forrest@0
   331
  text4(i,0) = sprintf("%.1f",vv(i))
forrest@0
   332
  text4(i,1) = sprintf("%.1f",uu(i))
forrest@0
   333
  text4(i,2) = sprintf("%.2f",t_biome(i))
forrest@0
   334
 end do
forrest@0
   335
  text4(nrow-1,0) = "-"
forrest@0
   336
  text4(nrow-1,1) = "-"
forrest@0
   337
  text4(nrow-1,2) = sprintf("%.2f",t_biome_avg)
forrest@0
   338
forrest@0
   339
;**************************************************
forrest@0
   340
; html table
forrest@0
   341
;**************************************************
forrest@0
   342
forrest@0
   343
  header_text = "<H1>"+component(k)+" Turnover Time:  Model "+model_name+"</H1>" 
forrest@0
   344
forrest@0
   345
  header = (/"<HTML>" \
forrest@0
   346
            ,"<HEAD>" \
forrest@0
   347
            ,"<TITLE>CLAMP metrics</TITLE>" \
forrest@0
   348
            ,"</HEAD>" \
forrest@0
   349
            ,header_text \
forrest@0
   350
            /) 
forrest@0
   351
  footer = "</HTML>"
forrest@0
   352
forrest@0
   353
  table_header = (/ \
forrest@0
   354
        "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
forrest@0
   355
       ,"<tr>" \
forrest@0
   356
       ,"   <th bgcolor=DDDDDD >Biome Class</th>" \
forrest@0
   357
       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
forrest@0
   358
       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
forrest@0
   359
       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
forrest@0
   360
       ,"</tr>" \
forrest@0
   361
       /)
forrest@0
   362
  table_footer = "</table>"
forrest@0
   363
  row_header = "<tr>"
forrest@0
   364
  row_footer = "</tr>"
forrest@0
   365
forrest@0
   366
  lines = new(50000,string)
forrest@0
   367
  nline = 0
forrest@0
   368
forrest@0
   369
  set_line(lines,nline,header)
forrest@0
   370
  set_line(lines,nline,table_header)
forrest@0
   371
;-----------------------------------------------
forrest@0
   372
;row of table
forrest@0
   373
forrest@0
   374
  do n = 0,nrow-1
forrest@0
   375
     set_line(lines,nline,row_header)
forrest@0
   376
forrest@0
   377
     txt1  = row_head(n)
forrest@0
   378
     txt2  = text4(n,0)
forrest@0
   379
     txt3  = text4(n,1)
forrest@0
   380
     txt4  = text4(n,2)
forrest@0
   381
forrest@0
   382
     set_line(lines,nline,"<th>"+txt1+"</th>")
forrest@0
   383
     set_line(lines,nline,"<th>"+txt2+"</th>")
forrest@0
   384
     set_line(lines,nline,"<th>"+txt3+"</th>")
forrest@0
   385
     set_line(lines,nline,"<th>"+txt4+"</th>")
forrest@0
   386
forrest@0
   387
     set_line(lines,nline,row_footer)
forrest@0
   388
  end do
forrest@0
   389
;-----------------------------------------------
forrest@0
   390
  set_line(lines,nline,table_footer)
forrest@0
   391
  set_line(lines,nline,footer) 
forrest@0
   392
forrest@0
   393
; Now write to an HTML file.
forrest@0
   394
  idx = ind(.not.ismissing(lines))
forrest@0
   395
  if(.not.any(ismissing(idx))) then
forrest@0
   396
    asciiwrite(output_html,lines(idx))
forrest@0
   397
  else
forrest@0
   398
   print ("error?")
forrest@0
   399
  end if
forrest@0
   400
forrest@0
   401
  delete (idx)
forrest@0
   402
end do
forrest@0
   403
end
forrest@0
   404