beta/06.biome_ob.ncl
changeset 0 0c6405ab2ff4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/beta/06.biome_ob.ncl	Mon Jan 26 22:08:20 2009 -0500
     1.3 @@ -0,0 +1,494 @@
     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 + co2_i = 283.1878
    1.39 + co2_f = 364.1252
    1.40 +
    1.41 + model_grid = "T42"
    1.42 +
    1.43 + model_name_i = "i01.07cn"
    1.44 + model_name_f = "i01.10cn"
    1.45 +
    1.46 +;model_name_i = "i01.07casa"
    1.47 +;model_name_f = "i01.10casa"
    1.48 +
    1.49 + model_name = model_name_f
    1.50 +
    1.51 + dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    1.52 + film_i = model_name_i + "_1990-2004_ANN_climo.nc"
    1.53 + film_f = model_name_f + "_1990-2004_ANN_climo.nc"
    1.54 +
    1.55 + fm_i   = addfile (dirm+film_i,"r")
    1.56 + fm_f   = addfile (dirm+film_f,"r")
    1.57 +  
    1.58 + xm     = fm_f->lon  
    1.59 + ym     = fm_f->lat
    1.60 +
    1.61 + npp_i  = fm_i->NPP
    1.62 + npp_f  = fm_f->NPP
    1.63 +
    1.64 +;Units for these variables are:
    1.65 +;npp_i: g C/m^2/s
    1.66 +
    1.67 + nsec_per_year = 60*60*24*365
    1.68 +  
    1.69 + npp_i = npp_i *  nsec_per_year
    1.70 + npp_f = npp_f *  nsec_per_year 
    1.71 +   
    1.72 +;===================================================
    1.73 +; read data: observed -station
    1.74 +;===================================================
    1.75 +
    1.76 + station = (/"DukeFACE" \
    1.77 +            ,"AspenFACE" \
    1.78 +            ,"ORNL-FACE" \
    1.79 +            ,"POP-EUROFACE" \
    1.80 +            /)
    1.81 +
    1.82 + lat_ob = (/ 35.58,  45.40,  35.54, 42.22/)
    1.83 + lon_ob = (/-79.05, -89.37, -84.20, 11.48/)
    1.84 + lon_ob = where(lon_ob.lt.0.,lon_ob+360.,lon_ob)
    1.85 +;print (lon_ob)
    1.86 +
    1.87 + n_sta  = dimsizes(station)
    1.88 + beta_4_ob = new((/n_sta/),float)
    1.89 + beta_4_ob = 0.60
    1.90 +
    1.91 +;===================================================
    1.92 +; get model data at station 
    1.93 +;===================================================
    1.94 +
    1.95 + npp_i_4  =linint2_points(xm,ym,npp_i,True,lon_ob,lat_ob,0)
    1.96 +
    1.97 + npp_f_4  =linint2_points(xm,ym,npp_f,True,lon_ob,lat_ob,0)
    1.98 +
    1.99 +;print (npp_i_4)
   1.100 +;print (npp_f_4)
   1.101 +
   1.102 +;============================
   1.103 +;compute beta_4
   1.104 +;============================
   1.105 +
   1.106 + beta_4 = new((/n_sta/),float)
   1.107 +
   1.108 + beta_4 = ((npp_f_4/npp_i_4) - 1.)/log(co2_f/co2_i)
   1.109 +
   1.110 + beta_4_avg = avg(beta_4)
   1.111 +
   1.112 +;print (beta_4)
   1.113 +;print (beta_4_avg)
   1.114 +
   1.115 +;M_beta = abs((beta_4_avg/beta_4_ob) - 1.)* 3.
   1.116 +
   1.117 + bias = sum(abs(beta_4-beta_4_ob)/(abs(beta_4)+abs(beta_4_ob))) 
   1.118 + M_beta  = (1. - (bias/n_sta))*3.
   1.119 + 
   1.120 + print (M_beta)
   1.121 +
   1.122 +;=========================
   1.123 +; for html table - station
   1.124 +;=========================
   1.125 +
   1.126 +  output_html = "table_station.html"
   1.127 +
   1.128 +; column (not including header column)
   1.129 +
   1.130 +  col_head = (/"Latitude","Longitude","CO2_i","CO2_f","NPP_i","NPP_f","Beta_model","Beta_ob"/)
   1.131 +
   1.132 +  ncol = dimsizes(col_head)
   1.133 +
   1.134 +; row (not including header row)
   1.135 +  row_head = (/"DukeFACE" \
   1.136 +            ,"AspenFACE" \
   1.137 +            ,"ORNL-FACE" \
   1.138 +            ,"POP-EUROFACE" \
   1.139 +            ,"All Station" \                
   1.140 +            /)  
   1.141 +  nrow = dimsizes(row_head)                  
   1.142 +
   1.143 +; arrays to be passed to table. 
   1.144 +  text4 = new ((/nrow, ncol/),string )
   1.145 +
   1.146 + do i=0,nrow-2
   1.147 +  text4(i,0) = sprintf("%.1f",lat_ob(i))
   1.148 +  text4(i,1) = sprintf("%.1f",lon_ob(i))
   1.149 +  text4(i,2) = sprintf("%.1f",co2_i)
   1.150 +  text4(i,3) = sprintf("%.1f",co2_f)
   1.151 +  text4(i,4) = sprintf("%.1f",npp_i_4(0,i))
   1.152 +  text4(i,5) = sprintf("%.1f",npp_f_4(0,i))
   1.153 +  text4(i,6) = sprintf("%.2f",beta_4(i))
   1.154 +  text4(i,7) = "-"
   1.155 + end do
   1.156 +  text4(nrow-1,0) = "-"
   1.157 +  text4(nrow-1,1) = "-"
   1.158 +  text4(nrow-1,2) = "-"
   1.159 +  text4(nrow-1,3) = "-"
   1.160 +  text4(nrow-1,4) = "-"
   1.161 +  text4(nrow-1,5) = "-"
   1.162 +  text4(nrow-1,6) = sprintf("%.2f",beta_4_avg)
   1.163 +  text4(nrow-1,7) = sprintf("%.2f",avg(beta_4_ob))
   1.164 +
   1.165 +;-----------
   1.166 +; html table
   1.167 +;-----------
   1.168 +
   1.169 +  header_text = "<H1>Beta Factor: Model "+model_name+"</H1>" 
   1.170 +
   1.171 +  header = (/"<HTML>" \
   1.172 +            ,"<HEAD>" \
   1.173 +            ,"<TITLE>CLAMP metrics</TITLE>" \
   1.174 +            ,"</HEAD>" \
   1.175 +            ,header_text \
   1.176 +            /) 
   1.177 +  footer = "</HTML>"
   1.178 +
   1.179 +  table_header = (/ \
   1.180 +        "<table border=1 cellspacing=0 cellpadding=3 width=80%>" \
   1.181 +       ,"<tr>" \
   1.182 +       ,"   <th bgcolor=DDDDDD >Station</th>" \
   1.183 +       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
   1.184 +       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
   1.185 +       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
   1.186 +       ,"   <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
   1.187 +       ,"   <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
   1.188 +       ,"   <th bgcolor=DDDDDD >"+col_head(5)+"</th>" \
   1.189 +       ,"   <th bgcolor=DDDDDD >"+col_head(6)+"</th>" \
   1.190 +       ,"   <th bgcolor=DDDDDD >"+col_head(7)+"</th>" \
   1.191 +       ,"</tr>" \
   1.192 +       /)
   1.193 +  table_footer = "</table>"
   1.194 +  row_header = "<tr>"
   1.195 +  row_footer = "</tr>"
   1.196 +
   1.197 +  lines = new(50000,string)
   1.198 +  nline = 0
   1.199 +
   1.200 +  set_line(lines,nline,header)
   1.201 +  set_line(lines,nline,table_header)
   1.202 +;-----------------------------------------------
   1.203 +;row of table
   1.204 +
   1.205 +  do n = 0,nrow-1
   1.206 +     set_line(lines,nline,row_header)
   1.207 +
   1.208 +     txt1  = row_head(n)
   1.209 +     txt2  = text4(n,0)
   1.210 +     txt3  = text4(n,1)
   1.211 +     txt4  = text4(n,2)
   1.212 +     txt5  = text4(n,3)
   1.213 +     txt6  = text4(n,4)
   1.214 +     txt7  = text4(n,5)
   1.215 +     txt8  = text4(n,6)
   1.216 +     txt9  = text4(n,7)
   1.217 +
   1.218 +     set_line(lines,nline,"<th>"+txt1+"</th>")
   1.219 +     set_line(lines,nline,"<th>"+txt2+"</th>")
   1.220 +     set_line(lines,nline,"<th>"+txt3+"</th>")
   1.221 +     set_line(lines,nline,"<th>"+txt4+"</th>")
   1.222 +     set_line(lines,nline,"<th>"+txt5+"</th>")
   1.223 +     set_line(lines,nline,"<th>"+txt6+"</th>")
   1.224 +     set_line(lines,nline,"<th>"+txt7+"</th>")
   1.225 +     set_line(lines,nline,"<th>"+txt8+"</th>")
   1.226 +     set_line(lines,nline,"<th>"+txt9+"</th>")
   1.227 +
   1.228 +     set_line(lines,nline,row_footer)
   1.229 +  end do
   1.230 +;-----------------------------------------------
   1.231 +  set_line(lines,nline,table_footer)
   1.232 +  set_line(lines,nline,footer) 
   1.233 +
   1.234 +; Now write to an HTML file.
   1.235 +  idx = ind(.not.ismissing(lines))
   1.236 +  if(.not.any(ismissing(idx))) then
   1.237 +    asciiwrite(output_html,lines(idx))
   1.238 +  else
   1.239 +   print ("error?")
   1.240 +  end if
   1.241 +
   1.242 +  delete (col_head)
   1.243 +  delete (row_head)
   1.244 +  delete (text4)
   1.245 +  delete (table_header)
   1.246 +  delete (idx)
   1.247 +      
   1.248 +;************************************************
   1.249 +; read data: observed-2
   1.250 +;************************************************
   1.251 +
   1.252 +  ob_name = "MODIS MOD 15A2 2000-2005"
   1.253 +
   1.254 +  diro  = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/"
   1.255 +  filo  = "land_class_"+model_grid+".nc"
   1.256 +
   1.257 +  fo = addfile(diro+filo,"r")
   1.258 + 
   1.259 +  classob = tofloat(fo->LAND_CLASS)
   1.260 +
   1.261 +; observed data has 20 land-type classes
   1.262 +  nclass = 20
   1.263 +                
   1.264 +;*******************************************************************
   1.265 +; Calculate "nice" bins for binning the data in equally spaced ranges
   1.266 +;********************************************************************
   1.267 +
   1.268 +  nclassn     = nclass + 1
   1.269 +  range       = fspan(0,nclassn-1,nclassn)
   1.270 +; print (range)
   1.271 +
   1.272 +; Use this range information to grab all the values in a
   1.273 +; particular range, and then take an average.
   1.274 +
   1.275 +  nr           = dimsizes(range)
   1.276 +  nx           = nr-1
   1.277 +  xvalues      = new((/2,nx/),float)
   1.278 +  xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
   1.279 +  dx           = xvalues(0,1) - xvalues(0,0)       ; range width
   1.280 +  dx4          = dx/4                              ; 1/4 of the range
   1.281 +  xvalues(1,:) = xvalues(0,:) - dx/5.
   1.282 +; get data
   1.283 +
   1.284 +  base_1D  = ndtooned(classob)
   1.285 +  data1_1D = ndtooned(npp_i)
   1.286 +  data2_1D = ndtooned(npp_f)
   1.287 +
   1.288 +; output
   1.289 +
   1.290 +  yvalues = new((/2,nx/),float)
   1.291 +  count   = new((/2,nx/),float)
   1.292 +
   1.293 +  do nd=0,1
   1.294 +
   1.295 +;   See if we are doing data1 (nd=0) or data2 (nd=1).
   1.296 +
   1.297 +    base = base_1D
   1.298 +
   1.299 +    if(nd.eq.0) then
   1.300 +      data = data1_1D
   1.301 +    else
   1.302 +      data = data2_1D
   1.303 +    end if
   1.304 +
   1.305 +; Loop through each range, using base.
   1.306 +
   1.307 +    do i=0,nr-2
   1.308 +      if (i.ne.(nr-2)) then
   1.309 +;        print("")
   1.310 +;        print("In range ["+range(i)+","+range(i+1)+")")
   1.311 +         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
   1.312 +      else
   1.313 +;        print("")
   1.314 +;        print("In range ["+range(i)+",)")
   1.315 +         idx = ind(base.ge.range(i))
   1.316 +      end if
   1.317 +
   1.318 +;     Calculate average 
   1.319 +
   1.320 +      if(.not.any(ismissing(idx))) then
   1.321 +        yvalues(nd,i) = avg(data(idx))
   1.322 +        count(nd,i)   = dimsizes(idx)
   1.323 +      else
   1.324 +        yvalues(nd,i) = yvalues@_FillValue
   1.325 +        count(nd,i)   = 0
   1.326 +      end if
   1.327 +
   1.328 +;#############################################################
   1.329 +; set the following 4 classes to _FillValue:
   1.330 +; Water Bodies(0), Urban and Build-Up(13),
   1.331 +; Permenant Snow and Ice(15), Unclassified(17)
   1.332 +
   1.333 +      if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then
   1.334 +         yvalues(nd,i) = yvalues@_FillValue
   1.335 +         count(nd,i)   = 0
   1.336 +      end if
   1.337 +;############################################################# 
   1.338 +
   1.339 +;     print(nd + ": " + count(nd,i) + " points, avg = " + yvalues(nd,i))
   1.340 +
   1.341 +; Clean up for next time in loop.
   1.342 +
   1.343 +      delete(idx)
   1.344 +    end do
   1.345 +
   1.346 +    delete(data)
   1.347 +  end do
   1.348 +
   1.349 +;============================
   1.350 +;compute beta
   1.351 +;============================
   1.352 +
   1.353 + u       = yvalues(0,:)
   1.354 + v       = yvalues(1,:)
   1.355 + u_count = count(0,:)
   1.356 + v_count = count(1,:)
   1.357 +
   1.358 + good = ind(.not.ismissing(u) .and. .not.ismissing(v))
   1.359 +
   1.360 + uu       = u(good)
   1.361 + vv       = v(good)
   1.362 + uu_count = u_count(good)
   1.363 + vv_count = v_count(good) 
   1.364 +
   1.365 + n_biome = dimsizes(uu)
   1.366 + beta_biome = new((/n_biome/),float)
   1.367 +
   1.368 + beta_biome = ((vv/uu) - 1.)/log(co2_f/co2_i)
   1.369 +
   1.370 +;beta_biome_avg = avg(beta_biome)
   1.371 + beta_biome_avg = (sum(vv*vv_count)/sum(uu*uu_count) - 1.)/log(co2_f/co2_i)
   1.372 +  
   1.373 + print (beta_biome_avg)
   1.374 +
   1.375 +;===========================
   1.376 +; for html table - biome
   1.377 +;===========================
   1.378 +
   1.379 +  output_html = "table_biome.html"
   1.380 +
   1.381 +; column (not including header column)
   1.382 +
   1.383 +  col_head = (/"CO2_i","CO2_f","NPP_i","NPP_f","Beta_model"/)
   1.384 +
   1.385 +  ncol = dimsizes(col_head)
   1.386 +
   1.387 +; row (not including header row)
   1.388 +; 3 classes removed: Water Bodies, Urban and Build-Up, Unclassified
   1.389 +; function "good" removed the last 2 classes
   1.390 +; text4(i,2) = sprintf("%.2f",uu(i+1)) remove the first class
   1.391 +
   1.392 +  row_head  = (/"Evergreen Needleleaf Forests" \
   1.393 +               ,"Evergreen Broadleaf Forests" \
   1.394 +               ,"Deciduous Needleleaf Forest" \
   1.395 +               ,"Deciduous Broadleaf Forests" \
   1.396 +               ,"Mixed Forests" \                      
   1.397 +               ,"Closed Bushlands" \                   
   1.398 +               ,"Open Bushlands" \                     
   1.399 +               ,"Woody Savannas (S. Hem.)" \           
   1.400 +               ,"Savannas (S. Hem.)" \                 
   1.401 +               ,"Grasslands" \                         
   1.402 +               ,"Permanent Wetlands" \                 
   1.403 +               ,"Croplands" \                                           
   1.404 +               ,"Cropland/Natural Vegetation Mosaic" \             
   1.405 +               ,"Barren or Sparsely Vegetated" \                             
   1.406 +               ,"Woody Savannas (N. Hem.)" \           
   1.407 +               ,"Savannas (N. Hem.)" \
   1.408 +               ,"All Biome" \                
   1.409 +               /)  
   1.410 +  nrow = dimsizes(row_head)                  
   1.411 +
   1.412 +; arrays to be passed to table. 
   1.413 +  text4 = new ((/nrow, ncol/),string )
   1.414 + 
   1.415 + do i=0,nrow-2
   1.416 +  text4(i,0) = sprintf("%.1f",co2_i)
   1.417 +  text4(i,1) = sprintf("%.1f",co2_f)
   1.418 +  text4(i,2) = sprintf("%.1f",uu(i))
   1.419 +  text4(i,3) = sprintf("%.1f",vv(i))
   1.420 +  text4(i,4) = sprintf("%.2f",beta_biome(i))
   1.421 + end do
   1.422 +  text4(nrow-1,0) = "-"
   1.423 +  text4(nrow-1,1) = "-"
   1.424 +  text4(nrow-1,2) = "-"
   1.425 +  text4(nrow-1,3) = "-"
   1.426 +  text4(nrow-1,4) = sprintf("%.2f",beta_biome_avg)
   1.427 +
   1.428 +;**************************************************
   1.429 +; html table
   1.430 +;**************************************************
   1.431 +
   1.432 +  header_text = "<H1>Beta Factor: Model "+model_name+"</H1>" 
   1.433 +
   1.434 +  header = (/"<HTML>" \
   1.435 +            ,"<HEAD>" \
   1.436 +            ,"<TITLE>CLAMP metrics</TITLE>" \
   1.437 +            ,"</HEAD>" \
   1.438 +            ,header_text \
   1.439 +            /) 
   1.440 +  footer = "</HTML>"
   1.441 +
   1.442 +  table_header = (/ \
   1.443 +        "<table border=1 cellspacing=0 cellpadding=3 width=80%>" \
   1.444 +       ,"<tr>" \
   1.445 +       ,"   <th bgcolor=DDDDDD >Biome Class</th>" \
   1.446 +       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
   1.447 +       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
   1.448 +       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
   1.449 +       ,"   <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
   1.450 +       ,"   <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
   1.451 +       ,"</tr>" \
   1.452 +       /)
   1.453 +  table_footer = "</table>"
   1.454 +  row_header = "<tr>"
   1.455 +  row_footer = "</tr>"
   1.456 +
   1.457 +  lines = new(50000,string)
   1.458 +  nline = 0
   1.459 +
   1.460 +  set_line(lines,nline,header)
   1.461 +  set_line(lines,nline,table_header)
   1.462 +;-----------------------------------------------
   1.463 +;row of table
   1.464 +
   1.465 +  do n = 0,nrow-1
   1.466 +     set_line(lines,nline,row_header)
   1.467 +
   1.468 +     txt1  = row_head(n)
   1.469 +     txt2  = text4(n,0)
   1.470 +     txt3  = text4(n,1)
   1.471 +     txt4  = text4(n,2)
   1.472 +     txt5  = text4(n,3)
   1.473 +     txt6  = text4(n,4)
   1.474 +
   1.475 +     set_line(lines,nline,"<th>"+txt1+"</th>")
   1.476 +     set_line(lines,nline,"<th>"+txt2+"</th>")
   1.477 +     set_line(lines,nline,"<th>"+txt3+"</th>")
   1.478 +     set_line(lines,nline,"<th>"+txt4+"</th>")
   1.479 +     set_line(lines,nline,"<th>"+txt5+"</th>")
   1.480 +     set_line(lines,nline,"<th>"+txt6+"</th>")
   1.481 +
   1.482 +     set_line(lines,nline,row_footer)
   1.483 +  end do
   1.484 +;-----------------------------------------------
   1.485 +  set_line(lines,nline,table_footer)
   1.486 +  set_line(lines,nline,footer) 
   1.487 +
   1.488 +; Now write to an HTML file.
   1.489 +  idx = ind(.not.ismissing(lines))
   1.490 +  if(.not.any(ismissing(idx))) then
   1.491 +    asciiwrite(output_html,lines(idx))
   1.492 +  else
   1.493 +   print ("error?")
   1.494 +  end if
   1.495 +
   1.496 +end
   1.497 +