1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/turnover/03.biome_ob.ncl Mon Jan 26 22:08:20 2009 -0500
1.3 @@ -0,0 +1,317 @@
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"
1.11 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
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 +;components
1.36 +
1.37 + component = (/"Leaf","Wood","Fine_Root","Litter","Coarse_Woody_Debris","Soil"/)
1.38 + n_comp = dimsizes(component)
1.39 +
1.40 + field_pool = (/"LEAFC","WOODC","FROOTC","LITTERC","CWDC","SOILC"/)
1.41 + field_flux = (/"LEAFC_ALLOC","WOODC_ALLOC","FROOTC_ALLOC","LITTERC_LOSS","CWDC_LOSS","SOILC_HR"/)
1.42 +
1.43 +;************************************************
1.44 +; read data: model
1.45 +;************************************************
1.46 +
1.47 + model_grid = "T42"
1.48 +
1.49 +;model_name = "i01.06cn"
1.50 + model_name = "i01.06casa"
1.51 +
1.52 + dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
1.53 + film = model_name + "_1980-2004_ANN_climo.nc"
1.54 +
1.55 + fm = addfile (dirm+film,"r")
1.56 +
1.57 + do k = 0,n_comp-1
1.58 +
1.59 + pool = fm->$field_pool(k)$
1.60 + flux = fm->$field_flux(k)$
1.61 +
1.62 +;Units for these variables are:
1.63 +;pool: g C/m^2
1.64 +;flux: g C/m^2/s
1.65 +
1.66 + nsec_per_year = 60*60*24*365
1.67 +
1.68 + flux = flux * nsec_per_year
1.69 +
1.70 +; casa only
1.71 +; all the plant pools (leaf, wood, and fine root) and
1.72 +; coarse woody debris (cwd) and litter pools for
1.73 +; CASA need to be divided by 1200. The soil flux
1.74 +; and turnover time are fine and do not need to be adjusted.
1.75 +
1.76 + if (k .ne. n_comp-1) then
1.77 + flux = flux/1200.
1.78 + end if
1.79 +;************************************************
1.80 +; read data: observed-biome
1.81 +;************************************************
1.82 +
1.83 + ob_name = "MODIS MOD 15A2 2000-2005"
1.84 +
1.85 + diro = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/"
1.86 + filo = "land_class_"+model_grid+".nc"
1.87 +
1.88 + fo = addfile(diro+filo,"r")
1.89 +
1.90 + classob = tofloat(fo->LAND_CLASS)
1.91 +
1.92 + nclass = 20
1.93 +
1.94 +;*******************************************************************
1.95 +; Calculate "nice" bins for binning the data in equally spaced ranges
1.96 +;********************************************************************
1.97 +
1.98 + nclassn = nclass + 1
1.99 + range = fspan(0,nclassn-1,nclassn)
1.100 +; print (range)
1.101 +
1.102 +; Use this range information to grab all the values in a
1.103 +; particular range, and then take an average.
1.104 +
1.105 + nr = dimsizes(range)
1.106 + nx = nr-1
1.107 + xvalues = new((/2,nx/),float)
1.108 + xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
1.109 + dx = xvalues(0,1) - xvalues(0,0) ; range width
1.110 + dx4 = dx/4 ; 1/4 of the range
1.111 + xvalues(1,:) = xvalues(0,:) - dx/5.
1.112 +
1.113 +; get data
1.114 +
1.115 + base_1D = ndtooned(classob)
1.116 + data1_1D = ndtooned(pool)
1.117 + data2_1D = ndtooned(flux)
1.118 +
1.119 +; output
1.120 +
1.121 + yvalues = new((/2,nx/),float)
1.122 + count = new((/2,nx/),float)
1.123 +
1.124 + do nd=0,1
1.125 +
1.126 +; See if we are doing data1 (nd=0) or data2 (nd=1).
1.127 +
1.128 + base = base_1D
1.129 +
1.130 + if(nd.eq.0) then
1.131 + data = data1_1D
1.132 + else
1.133 + data = data2_1D
1.134 + end if
1.135 +
1.136 +; Loop through each range, using base.
1.137 +
1.138 + do i=0,nr-2
1.139 + if (i.ne.(nr-2)) then
1.140 +; print("")
1.141 +; print("In range ["+range(i)+","+range(i+1)+")")
1.142 + idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
1.143 + else
1.144 +; print("")
1.145 +; print("In range ["+range(i)+",)")
1.146 + idx = ind(base.ge.range(i))
1.147 + end if
1.148 +
1.149 +; Calculate average
1.150 +
1.151 + if(.not.any(ismissing(idx))) then
1.152 + yvalues(nd,i) = avg(data(idx))
1.153 + count(nd,i) = dimsizes(idx)
1.154 + else
1.155 + yvalues(nd,i) = yvalues@_FillValue
1.156 + count(nd,i) = 0
1.157 + end if
1.158 +
1.159 +;#############################################################
1.160 +; set the following 4 classes to _FillValue:
1.161 +; Water Bodies(0), Urban and Build-Up(13),
1.162 +; Permenant Snow and Ice(15), Unclassified(17)
1.163 +
1.164 + if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then
1.165 + yvalues(nd,i) = yvalues@_FillValue
1.166 + count(nd,i) = 0
1.167 + end if
1.168 +;#############################################################
1.169 +
1.170 +; print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
1.171 +
1.172 +; Clean up for next time in loop.
1.173 +
1.174 + delete(idx)
1.175 + end do
1.176 +
1.177 + delete(data)
1.178 + end do
1.179 +
1.180 +;============================
1.181 +;compute turnover time
1.182 +;============================
1.183 +
1.184 + u = yvalues(0,:)
1.185 + v = yvalues(1,:)
1.186 + u_count = count(0,:)
1.187 + v_count = count(1,:)
1.188 +
1.189 + good = ind(.not.ismissing(u) .and. .not.ismissing(v))
1.190 +
1.191 + uu = u(good)
1.192 + vv = v(good)
1.193 + uu_count = u_count(good)
1.194 + vv_count = v_count(good)
1.195 +
1.196 + n_biome = dimsizes(uu)
1.197 + t_biome = new((/n_biome/),float)
1.198 +
1.199 + t_biome = uu/vv
1.200 +
1.201 +;t_biome_avg = avg(t_biome)
1.202 + t_biome_avg = sum(uu*uu_count)/sum(vv*vv_count)
1.203 +
1.204 +;print (t_biome)
1.205 +;print (t_biome_avg)
1.206 +
1.207 +;===========================
1.208 +; for html table - biome
1.209 +;===========================
1.210 +
1.211 + output_html = "table_"+component(k)+".html"
1.212 +
1.213 +; column (not including header column)
1.214 +
1.215 + col_head = (/component(k)+" Flux",component(k)+" Pool",component(k)+" Turnover Time"/)
1.216 +
1.217 + ncol = dimsizes(col_head)
1.218 +
1.219 +; row (not including header row)
1.220 +; 4 classes removed: Water Bodies, Urban and Build-Up,
1.221 +; Unclassified, Permanent Snow and Ice
1.222 +
1.223 + row_head = (/"Evergreen Needleleaf Forests" \
1.224 + ,"Evergreen Broadleaf Forests" \
1.225 + ,"Deciduous Needleleaf Forest" \
1.226 + ,"Deciduous Broadleaf Forests" \
1.227 + ,"Mixed Forests" \
1.228 + ,"Closed Bushlands" \
1.229 + ,"Open Bushlands" \
1.230 + ,"Woody Savannas (S. Hem.)" \
1.231 + ,"Savannas (S. Hem.)" \
1.232 + ,"Grasslands" \
1.233 + ,"Permanent Wetlands" \
1.234 + ,"Croplands" \
1.235 + ,"Cropland/Natural Vegetation Mosaic" \
1.236 + ,"Barren or Sparsely Vegetated" \
1.237 + ,"Woody Savannas (N. Hem.)" \
1.238 + ,"Savannas (N. Hem.)" \
1.239 + ,"All Biome" \
1.240 + /)
1.241 + nrow = dimsizes(row_head)
1.242 +
1.243 +; arrays to be passed to table.
1.244 + text4 = new ((/nrow, ncol/),string )
1.245 +
1.246 + do i=0,nrow-2
1.247 + text4(i,0) = sprintf("%.1f",vv(i))
1.248 + text4(i,1) = sprintf("%.1f",uu(i))
1.249 + text4(i,2) = sprintf("%.2f",t_biome(i))
1.250 + end do
1.251 + text4(nrow-1,0) = "-"
1.252 + text4(nrow-1,1) = "-"
1.253 + text4(nrow-1,2) = sprintf("%.2f",t_biome_avg)
1.254 +
1.255 +;**************************************************
1.256 +; html table
1.257 +;**************************************************
1.258 +
1.259 + header_text = "<H1>"+component(k)+" Turnover Time: Model "+model_name+"</H1>"
1.260 +
1.261 + header = (/"<HTML>" \
1.262 + ,"<HEAD>" \
1.263 + ,"<TITLE>CLAMP metrics</TITLE>" \
1.264 + ,"</HEAD>" \
1.265 + ,header_text \
1.266 + /)
1.267 + footer = "</HTML>"
1.268 +
1.269 + table_header = (/ \
1.270 + "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
1.271 + ,"<tr>" \
1.272 + ," <th bgcolor=DDDDDD >Biome Class</th>" \
1.273 + ," <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
1.274 + ," <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
1.275 + ," <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
1.276 + ,"</tr>" \
1.277 + /)
1.278 + table_footer = "</table>"
1.279 + row_header = "<tr>"
1.280 + row_footer = "</tr>"
1.281 +
1.282 + lines = new(50000,string)
1.283 + nline = 0
1.284 +
1.285 + set_line(lines,nline,header)
1.286 + set_line(lines,nline,table_header)
1.287 +;-----------------------------------------------
1.288 +;row of table
1.289 +
1.290 + do n = 0,nrow-1
1.291 + set_line(lines,nline,row_header)
1.292 +
1.293 + txt1 = row_head(n)
1.294 + txt2 = text4(n,0)
1.295 + txt3 = text4(n,1)
1.296 + txt4 = text4(n,2)
1.297 +
1.298 + set_line(lines,nline,"<th>"+txt1+"</th>")
1.299 + set_line(lines,nline,"<th>"+txt2+"</th>")
1.300 + set_line(lines,nline,"<th>"+txt3+"</th>")
1.301 + set_line(lines,nline,"<th>"+txt4+"</th>")
1.302 +
1.303 + set_line(lines,nline,row_footer)
1.304 + end do
1.305 +;-----------------------------------------------
1.306 + set_line(lines,nline,table_footer)
1.307 + set_line(lines,nline,footer)
1.308 +
1.309 +; Now write to an HTML file.
1.310 + idx = ind(.not.ismissing(lines))
1.311 + if(.not.any(ismissing(idx))) then
1.312 + asciiwrite(output_html,lines(idx))
1.313 + else
1.314 + print ("error?")
1.315 + end if
1.316 +
1.317 + delete (idx)
1.318 +end do
1.319 +end
1.320 +