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