turnover/01.biome.ncl
changeset 0 0c6405ab2ff4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/turnover/01.biome.ncl	Mon Jan 26 22:08:20 2009 -0500
     1.3 @@ -0,0 +1,294 @@
     1.4 +;********************************************************
     1.5 +; required command line input parameters:
     1.6 +;  ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
     1.7 +;
     1.8 +; histogram normalized by rain and compute correleration
     1.9 +;**************************************************************
    1.10 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl.test"
    1.11 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl.test"
    1.12 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
    1.13 +;**************************************************************
    1.14 +procedure set_line(lines:string,nline:integer,newlines:string) 
    1.15 +begin
    1.16 +; add line to ascci/html file
    1.17 +    
    1.18 +  nnewlines = dimsizes(newlines)
    1.19 +  if(nline+nnewlines-1.ge.dimsizes(lines))
    1.20 +    print("set_line: bad index, not setting anything.") 
    1.21 +    return
    1.22 +  end if 
    1.23 +  lines(nline:nline+nnewlines-1) = newlines
    1.24 +;  print ("lines = " + lines(nline:nline+nnewlines-1))
    1.25 +  nline = nline + nnewlines
    1.26 +  return 
    1.27 +end
    1.28 +;**************************************************************
    1.29 +; Main code.
    1.30 +begin
    1.31 + 
    1.32 + plot_type     = "ps"
    1.33 + plot_type_new = "png"
    1.34 +
    1.35 +;************************************************
    1.36 +; read data: model       
    1.37 +;************************************************
    1.38 +
    1.39 + model_grid = "T42"
    1.40 +
    1.41 + model_name = "i01.06cn"
    1.42 +;model_name = "i01.06casa"
    1.43 +
    1.44 + dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    1.45 + film = model_name + "_1980-2004_ANN_climo.nc"
    1.46 +
    1.47 + fm   = addfile (dirm+film,"r")
    1.48 +  
    1.49 + xm     = fm->lon  
    1.50 + ym     = fm->lat
    1.51 +
    1.52 + pool  = fm->WOODC
    1.53 + flux  = fm->WOODC_ALLOC
    1.54 +
    1.55 +;Units for these variables are:
    1.56 +;pool: g C/m^2
    1.57 +;flux: g C/m^2/s
    1.58 +
    1.59 + nsec_per_year = 60*60*24*365
    1.60 +  
    1.61 + flux = flux *  nsec_per_year 
    1.62 +      
    1.63 +;************************************************
    1.64 +; read data: observed-biome
    1.65 +;************************************************
    1.66 +
    1.67 +  ob_name = "MODIS MOD 15A2 2000-2005"
    1.68 +
    1.69 +  diro  = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/"
    1.70 +  filo  = "land_class_"+model_grid+".nc"
    1.71 +
    1.72 +  fo = addfile(diro+filo,"r")
    1.73 + 
    1.74 +  classob = tofloat(fo->LAND_CLASS)
    1.75 +
    1.76 +  nclass = 20
    1.77 +                
    1.78 +;*******************************************************************
    1.79 +; Calculate "nice" bins for binning the data in equally spaced ranges
    1.80 +;********************************************************************
    1.81 +
    1.82 +  nclassn     = nclass + 1
    1.83 +  range       = fspan(0,nclassn-1,nclassn)
    1.84 +; print (range)
    1.85 +
    1.86 +; Use this range information to grab all the values in a
    1.87 +; particular range, and then take an average.
    1.88 +
    1.89 +  nr           = dimsizes(range)
    1.90 +  nx           = nr-1
    1.91 +  xvalues      = new((/2,nx/),float)
    1.92 +  xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
    1.93 +  dx           = xvalues(0,1) - xvalues(0,0)       ; range width
    1.94 +  dx4          = dx/4                              ; 1/4 of the range
    1.95 +  xvalues(1,:) = xvalues(0,:) - dx/5.
    1.96 +
    1.97 +; get data
    1.98 +
    1.99 +  base_1D  = ndtooned(classob)
   1.100 +  data1_1D = ndtooned(pool)
   1.101 +  data2_1D = ndtooned(flux)
   1.102 +
   1.103 +; output
   1.104 +
   1.105 +  yvalues      = new((/2,nx/),float)
   1.106 +
   1.107 +  do nd=0,1
   1.108 +
   1.109 +;   See if we are doing data1 (nd=0) or data2 (nd=1).
   1.110 +
   1.111 +    base = base_1D
   1.112 +
   1.113 +    if(nd.eq.0) then
   1.114 +      data = data1_1D
   1.115 +    else
   1.116 +      data = data2_1D
   1.117 +    end if
   1.118 +
   1.119 +; Loop through each range, using base.
   1.120 +
   1.121 +    do i=0,nr-2
   1.122 +      if (i.ne.(nr-2)) then
   1.123 +;        print("")
   1.124 +;        print("In range ["+range(i)+","+range(i+1)+")")
   1.125 +         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
   1.126 +      else
   1.127 +;        print("")
   1.128 +;        print("In range ["+range(i)+",)")
   1.129 +         idx = ind(base.ge.range(i))
   1.130 +      end if
   1.131 +
   1.132 +;     Calculate average 
   1.133 +
   1.134 +      if(.not.any(ismissing(idx))) then
   1.135 +        yvalues(nd,i)    = avg(data(idx))
   1.136 +        count = dimsizes(idx)
   1.137 +      else
   1.138 +        yvalues(nd,i)    = yvalues@_FillValue
   1.139 +        count            = 0
   1.140 +      end if
   1.141 +
   1.142 +;#############################################################
   1.143 +; set the following 4 classes to _FillValue:
   1.144 +; Water Bodies(0), Urban and Build-Up(13),
   1.145 +; Permenant Snow and Ice(15), Unclassified(17)
   1.146 +
   1.147 +      if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then
   1.148 +         yvalues(nd,i)    = yvalues@_FillValue
   1.149 +         count            = 0
   1.150 +      end if
   1.151 +;############################################################# 
   1.152 +
   1.153 +;     print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
   1.154 +
   1.155 +; Clean up for next time in loop.
   1.156 +
   1.157 +      delete(idx)
   1.158 +    end do
   1.159 +
   1.160 +    delete(data)
   1.161 +  end do
   1.162 +
   1.163 +;============================
   1.164 +;compute turnover time
   1.165 +;============================
   1.166 +
   1.167 + u = yvalues(0,:)
   1.168 + v = yvalues(1,:)
   1.169 +
   1.170 + good = ind(.not.ismissing(u) .and. .not.ismissing(v))
   1.171 +
   1.172 + uu = u(good)
   1.173 + vv = v(good) 
   1.174 +
   1.175 + n_biome = dimsizes(uu)
   1.176 +
   1.177 + t_biome = new((/n_biome/),float)
   1.178 +
   1.179 + t_biome = uu/vv
   1.180 +
   1.181 + t_biome_avg = avg(t_biome)
   1.182 +  
   1.183 +;print (t_biome)
   1.184 +;print (t_biome_avg)
   1.185 +
   1.186 +;===========================
   1.187 +; for html table - biome
   1.188 +;===========================
   1.189 +
   1.190 +  output_html = "table_biome.html"
   1.191 +
   1.192 +; column (not including header column)
   1.193 +
   1.194 +  col_head = (/"WOOD Flux","WOOD Pool","WOOD Turnover Time"/)
   1.195 +
   1.196 +  ncol = dimsizes(col_head)
   1.197 +
   1.198 +; row (not including header row)
   1.199 +; 4 classes removed: Water Bodies, Urban and Build-Up, 
   1.200 +;                    Unclassified, Permanent Snow and Ice                   
   1.201 +
   1.202 +  row_head  = (/"Evergreen Needleleaf Forests" \
   1.203 +               ,"Evergreen Broadleaf Forests" \
   1.204 +               ,"Deciduous Needleleaf Forest" \
   1.205 +               ,"Deciduous Broadleaf Forests" \
   1.206 +               ,"Mixed Forests" \                      
   1.207 +               ,"Closed Bushlands" \                   
   1.208 +               ,"Open Bushlands" \                     
   1.209 +               ,"Woody Savannas (S. Hem.)" \           
   1.210 +               ,"Savannas (S. Hem.)" \                 
   1.211 +               ,"Grasslands" \                         
   1.212 +               ,"Permanent Wetlands" \                 
   1.213 +               ,"Croplands" \                                           
   1.214 +               ,"Cropland/Natural Vegetation Mosaic" \             
   1.215 +               ,"Barren or Sparsely Vegetated" \                             
   1.216 +               ,"Woody Savannas (N. Hem.)" \           
   1.217 +               ,"Savannas (N. Hem.)" \
   1.218 +               ,"All Biome" \                
   1.219 +               /)  
   1.220 +  nrow = dimsizes(row_head)                  
   1.221 +
   1.222 +; arrays to be passed to table. 
   1.223 +  text4 = new ((/nrow, ncol/),string )
   1.224 + 
   1.225 + do i=0,nrow-2
   1.226 +  text4(i,0) = sprintf("%.1f",vv(i))
   1.227 +  text4(i,1) = sprintf("%.1f",uu(i))
   1.228 +  text4(i,2) = sprintf("%.1f",t_biome(i))
   1.229 + end do
   1.230 +  text4(nrow-1,0) = "-"
   1.231 +  text4(nrow-1,1) = "-"
   1.232 +  text4(nrow-1,2) = sprintf("%.1f",t_biome_avg)
   1.233 +
   1.234 +;**************************************************
   1.235 +; html table
   1.236 +;**************************************************
   1.237 +
   1.238 +  header_text = "<H1>WOOD Turnover Time:  Model "+model_name+"</H1>" 
   1.239 +
   1.240 +  header = (/"<HTML>" \
   1.241 +            ,"<HEAD>" \
   1.242 +            ,"<TITLE>CLAMP metrics</TITLE>" \
   1.243 +            ,"</HEAD>" \
   1.244 +            ,header_text \
   1.245 +            /) 
   1.246 +  footer = "</HTML>"
   1.247 +
   1.248 +  table_header = (/ \
   1.249 +        "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
   1.250 +       ,"<tr>" \
   1.251 +       ,"   <th bgcolor=DDDDDD >Biome Class</th>" \
   1.252 +       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
   1.253 +       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
   1.254 +       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
   1.255 +       ,"</tr>" \
   1.256 +       /)
   1.257 +  table_footer = "</table>"
   1.258 +  row_header = "<tr>"
   1.259 +  row_footer = "</tr>"
   1.260 +
   1.261 +  lines = new(50000,string)
   1.262 +  nline = 0
   1.263 +
   1.264 +  set_line(lines,nline,header)
   1.265 +  set_line(lines,nline,table_header)
   1.266 +;-----------------------------------------------
   1.267 +;row of table
   1.268 +
   1.269 +  do n = 0,nrow-1
   1.270 +     set_line(lines,nline,row_header)
   1.271 +
   1.272 +     txt1  = row_head(n)
   1.273 +     txt2  = text4(n,0)
   1.274 +     txt3  = text4(n,1)
   1.275 +     txt4  = text4(n,2)
   1.276 +
   1.277 +     set_line(lines,nline,"<th>"+txt1+"</th>")
   1.278 +     set_line(lines,nline,"<th>"+txt2+"</th>")
   1.279 +     set_line(lines,nline,"<th>"+txt3+"</th>")
   1.280 +     set_line(lines,nline,"<th>"+txt4+"</th>")
   1.281 +
   1.282 +     set_line(lines,nline,row_footer)
   1.283 +  end do
   1.284 +;-----------------------------------------------
   1.285 +  set_line(lines,nline,table_footer)
   1.286 +  set_line(lines,nline,footer) 
   1.287 +
   1.288 +; Now write to an HTML file.
   1.289 +  idx = ind(.not.ismissing(lines))
   1.290 +  if(.not.any(ismissing(idx))) then
   1.291 +    asciiwrite(output_html,lines(idx))
   1.292 +  else
   1.293 +   print ("error?")
   1.294 +  end if
   1.295 +
   1.296 +end
   1.297 +