turnover/03.biome_ob.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
; required command line input parameters:
forrest@0
     3
;  ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
forrest@0
     4
;
forrest@0
     5
; histogram normalized by rain and compute correleration
forrest@0
     6
;**************************************************************
forrest@0
     7
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
forrest@0
     8
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
forrest@0
     9
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
forrest@0
    10
;**************************************************************
forrest@0
    11
procedure set_line(lines:string,nline:integer,newlines:string) 
forrest@0
    12
begin
forrest@0
    13
; add line to ascci/html file
forrest@0
    14
    
forrest@0
    15
  nnewlines = dimsizes(newlines)
forrest@0
    16
  if(nline+nnewlines-1.ge.dimsizes(lines))
forrest@0
    17
    print("set_line: bad index, not setting anything.") 
forrest@0
    18
    return
forrest@0
    19
  end if 
forrest@0
    20
  lines(nline:nline+nnewlines-1) = newlines
forrest@0
    21
;  print ("lines = " + lines(nline:nline+nnewlines-1))
forrest@0
    22
  nline = nline + nnewlines
forrest@0
    23
  return 
forrest@0
    24
end
forrest@0
    25
;**************************************************************
forrest@0
    26
; Main code.
forrest@0
    27
begin
forrest@0
    28
 
forrest@0
    29
 plot_type     = "ps"
forrest@0
    30
 plot_type_new = "png"
forrest@0
    31
forrest@0
    32
;components
forrest@0
    33
forrest@0
    34
 component = (/"Leaf","Wood","Fine_Root","Litter","Coarse_Woody_Debris","Soil"/)
forrest@0
    35
 n_comp = dimsizes(component)
forrest@0
    36
forrest@0
    37
 field_pool = (/"LEAFC","WOODC","FROOTC","LITTERC","CWDC","SOILC"/)
forrest@0
    38
 field_flux = (/"LEAFC_ALLOC","WOODC_ALLOC","FROOTC_ALLOC","LITTERC_LOSS","CWDC_LOSS","SOILC_HR"/)
forrest@0
    39
forrest@0
    40
;************************************************
forrest@0
    41
; read data: model       
forrest@0
    42
;************************************************
forrest@0
    43
forrest@0
    44
 model_grid = "T42"
forrest@0
    45
forrest@0
    46
;model_name = "i01.06cn"
forrest@0
    47
 model_name = "i01.06casa"
forrest@0
    48
forrest@0
    49
 dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
    50
 film = model_name + "_1980-2004_ANN_climo.nc"
forrest@0
    51
forrest@0
    52
 fm   = addfile (dirm+film,"r")
forrest@0
    53
  
forrest@0
    54
 do k = 0,n_comp-1
forrest@0
    55
forrest@0
    56
 pool  = fm->$field_pool(k)$
forrest@0
    57
 flux  = fm->$field_flux(k)$
forrest@0
    58
forrest@0
    59
;Units for these variables are:
forrest@0
    60
;pool: g C/m^2
forrest@0
    61
;flux: g C/m^2/s
forrest@0
    62
forrest@0
    63
 nsec_per_year = 60*60*24*365
forrest@0
    64
  
forrest@0
    65
 flux = flux *  nsec_per_year 
forrest@0
    66
forrest@0
    67
; casa only
forrest@0
    68
; all the plant pools (leaf, wood, and fine root) and
forrest@0
    69
; coarse woody debris (cwd) and litter pools for
forrest@0
    70
; CASA need to be divided by 1200.  The soil flux
forrest@0
    71
; and turnover time are fine and do not need to be adjusted.
forrest@0
    72
  
forrest@0
    73
  if (k .ne. n_comp-1) then
forrest@0
    74
      flux = flux/1200.
forrest@0
    75
  end if    
forrest@0
    76
;************************************************
forrest@0
    77
; read data: observed-biome
forrest@0
    78
;************************************************
forrest@0
    79
forrest@0
    80
  ob_name = "MODIS MOD 15A2 2000-2005"
forrest@0
    81
forrest@0
    82
  diro  = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/"
forrest@0
    83
  filo  = "land_class_"+model_grid+".nc"
forrest@0
    84
forrest@0
    85
  fo = addfile(diro+filo,"r")
forrest@0
    86
 
forrest@0
    87
  classob = tofloat(fo->LAND_CLASS)
forrest@0
    88
forrest@0
    89
  nclass = 20
forrest@0
    90
                
forrest@0
    91
;*******************************************************************
forrest@0
    92
; Calculate "nice" bins for binning the data in equally spaced ranges
forrest@0
    93
;********************************************************************
forrest@0
    94
forrest@0
    95
  nclassn     = nclass + 1
forrest@0
    96
  range       = fspan(0,nclassn-1,nclassn)
forrest@0
    97
; print (range)
forrest@0
    98
forrest@0
    99
; Use this range information to grab all the values in a
forrest@0
   100
; particular range, and then take an average.
forrest@0
   101
forrest@0
   102
  nr           = dimsizes(range)
forrest@0
   103
  nx           = nr-1
forrest@0
   104
  xvalues      = new((/2,nx/),float)
forrest@0
   105
  xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
forrest@0
   106
  dx           = xvalues(0,1) - xvalues(0,0)       ; range width
forrest@0
   107
  dx4          = dx/4                              ; 1/4 of the range
forrest@0
   108
  xvalues(1,:) = xvalues(0,:) - dx/5.
forrest@0
   109
forrest@0
   110
; get data
forrest@0
   111
forrest@0
   112
  base_1D  = ndtooned(classob)
forrest@0
   113
  data1_1D = ndtooned(pool)
forrest@0
   114
  data2_1D = ndtooned(flux)
forrest@0
   115
forrest@0
   116
; output
forrest@0
   117
forrest@0
   118
  yvalues = new((/2,nx/),float)
forrest@0
   119
  count   = new((/2,nx/),float)
forrest@0
   120
forrest@0
   121
  do nd=0,1
forrest@0
   122
forrest@0
   123
;   See if we are doing data1 (nd=0) or data2 (nd=1).
forrest@0
   124
forrest@0
   125
    base = base_1D
forrest@0
   126
forrest@0
   127
    if(nd.eq.0) then
forrest@0
   128
      data = data1_1D
forrest@0
   129
    else
forrest@0
   130
      data = data2_1D
forrest@0
   131
    end if
forrest@0
   132
forrest@0
   133
; Loop through each range, using base.
forrest@0
   134
forrest@0
   135
    do i=0,nr-2
forrest@0
   136
      if (i.ne.(nr-2)) then
forrest@0
   137
;        print("")
forrest@0
   138
;        print("In range ["+range(i)+","+range(i+1)+")")
forrest@0
   139
         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
forrest@0
   140
      else
forrest@0
   141
;        print("")
forrest@0
   142
;        print("In range ["+range(i)+",)")
forrest@0
   143
         idx = ind(base.ge.range(i))
forrest@0
   144
      end if
forrest@0
   145
forrest@0
   146
;     Calculate average
forrest@0
   147
 
forrest@0
   148
      if(.not.any(ismissing(idx))) then
forrest@0
   149
        yvalues(nd,i) = avg(data(idx))
forrest@0
   150
        count(nd,i)   = dimsizes(idx)
forrest@0
   151
      else
forrest@0
   152
        yvalues(nd,i) = yvalues@_FillValue
forrest@0
   153
        count(nd,i)   = 0
forrest@0
   154
      end if
forrest@0
   155
forrest@0
   156
;#############################################################
forrest@0
   157
; set the following 4 classes to _FillValue:
forrest@0
   158
; Water Bodies(0), Urban and Build-Up(13),
forrest@0
   159
; Permenant Snow and Ice(15), Unclassified(17)
forrest@0
   160
forrest@0
   161
      if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then
forrest@0
   162
         yvalues(nd,i) = yvalues@_FillValue
forrest@0
   163
         count(nd,i)   = 0
forrest@0
   164
      end if
forrest@0
   165
;############################################################# 
forrest@0
   166
forrest@0
   167
;     print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
forrest@0
   168
forrest@0
   169
; Clean up for next time in loop.
forrest@0
   170
forrest@0
   171
      delete(idx)
forrest@0
   172
    end do
forrest@0
   173
forrest@0
   174
    delete(data)
forrest@0
   175
  end do
forrest@0
   176
forrest@0
   177
;============================
forrest@0
   178
;compute turnover time
forrest@0
   179
;============================
forrest@0
   180
forrest@0
   181
 u       = yvalues(0,:)
forrest@0
   182
 v       = yvalues(1,:)
forrest@0
   183
 u_count = count(0,:)
forrest@0
   184
 v_count = count(1,:)
forrest@0
   185
forrest@0
   186
 good = ind(.not.ismissing(u) .and. .not.ismissing(v))
forrest@0
   187
forrest@0
   188
 uu       = u(good)
forrest@0
   189
 vv       = v(good)
forrest@0
   190
 uu_count = u_count(good)
forrest@0
   191
 vv_count = v_count(good)  
forrest@0
   192
forrest@0
   193
 n_biome = dimsizes(uu)
forrest@0
   194
 t_biome = new((/n_biome/),float)
forrest@0
   195
forrest@0
   196
 t_biome = uu/vv
forrest@0
   197
forrest@0
   198
;t_biome_avg = avg(t_biome)
forrest@0
   199
 t_biome_avg = sum(uu*uu_count)/sum(vv*vv_count)
forrest@0
   200
  
forrest@0
   201
;print (t_biome)
forrest@0
   202
;print (t_biome_avg)
forrest@0
   203
forrest@0
   204
;===========================
forrest@0
   205
; for html table - biome
forrest@0
   206
;===========================
forrest@0
   207
forrest@0
   208
  output_html = "table_"+component(k)+".html"
forrest@0
   209
forrest@0
   210
; column (not including header column)
forrest@0
   211
forrest@0
   212
  col_head = (/component(k)+" Flux",component(k)+" Pool",component(k)+" Turnover Time"/)
forrest@0
   213
forrest@0
   214
  ncol = dimsizes(col_head)
forrest@0
   215
forrest@0
   216
; row (not including header row)
forrest@0
   217
; 4 classes removed: Water Bodies, Urban and Build-Up, 
forrest@0
   218
;                    Unclassified, Permanent Snow and Ice                   
forrest@0
   219
forrest@0
   220
  row_head  = (/"Evergreen Needleleaf Forests" \
forrest@0
   221
               ,"Evergreen Broadleaf Forests" \
forrest@0
   222
               ,"Deciduous Needleleaf Forest" \
forrest@0
   223
               ,"Deciduous Broadleaf Forests" \
forrest@0
   224
               ,"Mixed Forests" \                      
forrest@0
   225
               ,"Closed Bushlands" \                   
forrest@0
   226
               ,"Open Bushlands" \                     
forrest@0
   227
               ,"Woody Savannas (S. Hem.)" \           
forrest@0
   228
               ,"Savannas (S. Hem.)" \                 
forrest@0
   229
               ,"Grasslands" \                         
forrest@0
   230
               ,"Permanent Wetlands" \                 
forrest@0
   231
               ,"Croplands" \                                           
forrest@0
   232
               ,"Cropland/Natural Vegetation Mosaic" \             
forrest@0
   233
               ,"Barren or Sparsely Vegetated" \                             
forrest@0
   234
               ,"Woody Savannas (N. Hem.)" \           
forrest@0
   235
               ,"Savannas (N. Hem.)" \
forrest@0
   236
               ,"All Biome" \                
forrest@0
   237
               /)  
forrest@0
   238
  nrow = dimsizes(row_head)                  
forrest@0
   239
forrest@0
   240
; arrays to be passed to table. 
forrest@0
   241
  text4 = new ((/nrow, ncol/),string )
forrest@0
   242
 
forrest@0
   243
 do i=0,nrow-2
forrest@0
   244
  text4(i,0) = sprintf("%.1f",vv(i))
forrest@0
   245
  text4(i,1) = sprintf("%.1f",uu(i))
forrest@0
   246
  text4(i,2) = sprintf("%.2f",t_biome(i))
forrest@0
   247
 end do
forrest@0
   248
  text4(nrow-1,0) = "-"
forrest@0
   249
  text4(nrow-1,1) = "-"
forrest@0
   250
  text4(nrow-1,2) = sprintf("%.2f",t_biome_avg)
forrest@0
   251
forrest@0
   252
;**************************************************
forrest@0
   253
; html table
forrest@0
   254
;**************************************************
forrest@0
   255
forrest@0
   256
  header_text = "<H1>"+component(k)+" Turnover Time:  Model "+model_name+"</H1>" 
forrest@0
   257
forrest@0
   258
  header = (/"<HTML>" \
forrest@0
   259
            ,"<HEAD>" \
forrest@0
   260
            ,"<TITLE>CLAMP metrics</TITLE>" \
forrest@0
   261
            ,"</HEAD>" \
forrest@0
   262
            ,header_text \
forrest@0
   263
            /) 
forrest@0
   264
  footer = "</HTML>"
forrest@0
   265
forrest@0
   266
  table_header = (/ \
forrest@0
   267
        "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
forrest@0
   268
       ,"<tr>" \
forrest@0
   269
       ,"   <th bgcolor=DDDDDD >Biome Class</th>" \
forrest@0
   270
       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
forrest@0
   271
       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
forrest@0
   272
       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
forrest@0
   273
       ,"</tr>" \
forrest@0
   274
       /)
forrest@0
   275
  table_footer = "</table>"
forrest@0
   276
  row_header = "<tr>"
forrest@0
   277
  row_footer = "</tr>"
forrest@0
   278
forrest@0
   279
  lines = new(50000,string)
forrest@0
   280
  nline = 0
forrest@0
   281
forrest@0
   282
  set_line(lines,nline,header)
forrest@0
   283
  set_line(lines,nline,table_header)
forrest@0
   284
;-----------------------------------------------
forrest@0
   285
;row of table
forrest@0
   286
forrest@0
   287
  do n = 0,nrow-1
forrest@0
   288
     set_line(lines,nline,row_header)
forrest@0
   289
forrest@0
   290
     txt1  = row_head(n)
forrest@0
   291
     txt2  = text4(n,0)
forrest@0
   292
     txt3  = text4(n,1)
forrest@0
   293
     txt4  = text4(n,2)
forrest@0
   294
forrest@0
   295
     set_line(lines,nline,"<th>"+txt1+"</th>")
forrest@0
   296
     set_line(lines,nline,"<th>"+txt2+"</th>")
forrest@0
   297
     set_line(lines,nline,"<th>"+txt3+"</th>")
forrest@0
   298
     set_line(lines,nline,"<th>"+txt4+"</th>")
forrest@0
   299
forrest@0
   300
     set_line(lines,nline,row_footer)
forrest@0
   301
  end do
forrest@0
   302
;-----------------------------------------------
forrest@0
   303
  set_line(lines,nline,table_footer)
forrest@0
   304
  set_line(lines,nline,footer) 
forrest@0
   305
forrest@0
   306
; Now write to an HTML file.
forrest@0
   307
  idx = ind(.not.ismissing(lines))
forrest@0
   308
  if(.not.any(ismissing(idx))) then
forrest@0
   309
    asciiwrite(output_html,lines(idx))
forrest@0
   310
  else
forrest@0
   311
   print ("error?")
forrest@0
   312
  end if
forrest@0
   313
forrest@0
   314
  delete (idx)
forrest@0
   315
end do
forrest@0
   316
end
forrest@0
   317