1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/turnover/02.biome.ncl Mon Jan 26 22:08:20 2009 -0500
1.3 @@ -0,0 +1,303 @@
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 +;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 +;************************************************
1.71 +; read data: observed-biome
1.72 +;************************************************
1.73 +
1.74 + ob_name = "MODIS MOD 15A2 2000-2005"
1.75 +
1.76 + diro = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/"
1.77 + filo = "land_class_"+model_grid+".nc"
1.78 +
1.79 + fo = addfile(diro+filo,"r")
1.80 +
1.81 + classob = tofloat(fo->LAND_CLASS)
1.82 +
1.83 + nclass = 20
1.84 +
1.85 +;*******************************************************************
1.86 +; Calculate "nice" bins for binning the data in equally spaced ranges
1.87 +;********************************************************************
1.88 +
1.89 + nclassn = nclass + 1
1.90 + range = fspan(0,nclassn-1,nclassn)
1.91 +; print (range)
1.92 +
1.93 +; Use this range information to grab all the values in a
1.94 +; particular range, and then take an average.
1.95 +
1.96 + nr = dimsizes(range)
1.97 + nx = nr-1
1.98 + xvalues = new((/2,nx/),float)
1.99 + xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
1.100 + dx = xvalues(0,1) - xvalues(0,0) ; range width
1.101 + dx4 = dx/4 ; 1/4 of the range
1.102 + xvalues(1,:) = xvalues(0,:) - dx/5.
1.103 +
1.104 +; get data
1.105 +
1.106 + base_1D = ndtooned(classob)
1.107 + data1_1D = ndtooned(pool)
1.108 + data2_1D = ndtooned(flux)
1.109 +
1.110 +; output
1.111 +
1.112 + yvalues = new((/2,nx/),float)
1.113 +
1.114 + do nd=0,1
1.115 +
1.116 +; See if we are doing data1 (nd=0) or data2 (nd=1).
1.117 +
1.118 + base = base_1D
1.119 +
1.120 + if(nd.eq.0) then
1.121 + data = data1_1D
1.122 + else
1.123 + data = data2_1D
1.124 + end if
1.125 +
1.126 +; Loop through each range, using base.
1.127 +
1.128 + do i=0,nr-2
1.129 + if (i.ne.(nr-2)) then
1.130 +; print("")
1.131 +; print("In range ["+range(i)+","+range(i+1)+")")
1.132 + idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
1.133 + else
1.134 +; print("")
1.135 +; print("In range ["+range(i)+",)")
1.136 + idx = ind(base.ge.range(i))
1.137 + end if
1.138 +
1.139 +; Calculate average
1.140 +
1.141 + if(.not.any(ismissing(idx))) then
1.142 + yvalues(nd,i) = avg(data(idx))
1.143 + count = dimsizes(idx)
1.144 + else
1.145 + yvalues(nd,i) = yvalues@_FillValue
1.146 + count = 0
1.147 + end if
1.148 +
1.149 +;#############################################################
1.150 +; set the following 4 classes to _FillValue:
1.151 +; Water Bodies(0), Urban and Build-Up(13),
1.152 +; Permenant Snow and Ice(15), Unclassified(17)
1.153 +
1.154 + if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then
1.155 + yvalues(nd,i) = yvalues@_FillValue
1.156 + count = 0
1.157 + end if
1.158 +;#############################################################
1.159 +
1.160 +; print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
1.161 +
1.162 +; Clean up for next time in loop.
1.163 +
1.164 + delete(idx)
1.165 + end do
1.166 +
1.167 + delete(data)
1.168 + end do
1.169 +
1.170 +;============================
1.171 +;compute turnover time
1.172 +;============================
1.173 +
1.174 + u = yvalues(0,:)
1.175 + v = yvalues(1,:)
1.176 +
1.177 + good = ind(.not.ismissing(u) .and. .not.ismissing(v))
1.178 +
1.179 + uu = u(good)
1.180 + vv = v(good)
1.181 +
1.182 + n_biome = dimsizes(uu)
1.183 +
1.184 + t_biome = new((/n_biome/),float)
1.185 +
1.186 + t_biome = uu/vv
1.187 +
1.188 + t_biome_avg = avg(t_biome)
1.189 +
1.190 +;print (t_biome)
1.191 +;print (t_biome_avg)
1.192 +
1.193 +;===========================
1.194 +; for html table - biome
1.195 +;===========================
1.196 +
1.197 + output_html = "table_"+component(k)+".html"
1.198 +
1.199 +; column (not including header column)
1.200 +
1.201 + col_head = (/component(k)+" Flux",component(k)+" Pool",component(k)+" Turnover Time"/)
1.202 +
1.203 + ncol = dimsizes(col_head)
1.204 +
1.205 +; row (not including header row)
1.206 +; 4 classes removed: Water Bodies, Urban and Build-Up,
1.207 +; Unclassified, Permanent Snow and Ice
1.208 +
1.209 + row_head = (/"Evergreen Needleleaf Forests" \
1.210 + ,"Evergreen Broadleaf Forests" \
1.211 + ,"Deciduous Needleleaf Forest" \
1.212 + ,"Deciduous Broadleaf Forests" \
1.213 + ,"Mixed Forests" \
1.214 + ,"Closed Bushlands" \
1.215 + ,"Open Bushlands" \
1.216 + ,"Woody Savannas (S. Hem.)" \
1.217 + ,"Savannas (S. Hem.)" \
1.218 + ,"Grasslands" \
1.219 + ,"Permanent Wetlands" \
1.220 + ,"Croplands" \
1.221 + ,"Cropland/Natural Vegetation Mosaic" \
1.222 + ,"Barren or Sparsely Vegetated" \
1.223 + ,"Woody Savannas (N. Hem.)" \
1.224 + ,"Savannas (N. Hem.)" \
1.225 + ,"All Biome" \
1.226 + /)
1.227 + nrow = dimsizes(row_head)
1.228 +
1.229 +; arrays to be passed to table.
1.230 + text4 = new ((/nrow, ncol/),string )
1.231 +
1.232 + do i=0,nrow-2
1.233 + text4(i,0) = sprintf("%.1f",vv(i))
1.234 + text4(i,1) = sprintf("%.1f",uu(i))
1.235 + text4(i,2) = sprintf("%.1f",t_biome(i))
1.236 + end do
1.237 + text4(nrow-1,0) = "-"
1.238 + text4(nrow-1,1) = "-"
1.239 + text4(nrow-1,2) = sprintf("%.1f",t_biome_avg)
1.240 +
1.241 +;**************************************************
1.242 +; html table
1.243 +;**************************************************
1.244 +
1.245 + header_text = "<H1>"+component(k)+" Turnover Time: Model "+model_name+"</H1>"
1.246 +
1.247 + header = (/"<HTML>" \
1.248 + ,"<HEAD>" \
1.249 + ,"<TITLE>CLAMP metrics</TITLE>" \
1.250 + ,"</HEAD>" \
1.251 + ,header_text \
1.252 + /)
1.253 + footer = "</HTML>"
1.254 +
1.255 + table_header = (/ \
1.256 + "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
1.257 + ,"<tr>" \
1.258 + ," <th bgcolor=DDDDDD >Biome Class</th>" \
1.259 + ," <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
1.260 + ," <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
1.261 + ," <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
1.262 + ,"</tr>" \
1.263 + /)
1.264 + table_footer = "</table>"
1.265 + row_header = "<tr>"
1.266 + row_footer = "</tr>"
1.267 +
1.268 + lines = new(50000,string)
1.269 + nline = 0
1.270 +
1.271 + set_line(lines,nline,header)
1.272 + set_line(lines,nline,table_header)
1.273 +;-----------------------------------------------
1.274 +;row of table
1.275 +
1.276 + do n = 0,nrow-1
1.277 + set_line(lines,nline,row_header)
1.278 +
1.279 + txt1 = row_head(n)
1.280 + txt2 = text4(n,0)
1.281 + txt3 = text4(n,1)
1.282 + txt4 = text4(n,2)
1.283 +
1.284 + set_line(lines,nline,"<th>"+txt1+"</th>")
1.285 + set_line(lines,nline,"<th>"+txt2+"</th>")
1.286 + set_line(lines,nline,"<th>"+txt3+"</th>")
1.287 + set_line(lines,nline,"<th>"+txt4+"</th>")
1.288 +
1.289 + set_line(lines,nline,row_footer)
1.290 + end do
1.291 +;-----------------------------------------------
1.292 + set_line(lines,nline,table_footer)
1.293 + set_line(lines,nline,footer)
1.294 +
1.295 +; Now write to an HTML file.
1.296 + idx = ind(.not.ismissing(lines))
1.297 + if(.not.any(ismissing(idx))) then
1.298 + asciiwrite(output_html,lines(idx))
1.299 + else
1.300 + print ("error?")
1.301 + end if
1.302 +
1.303 + delete (idx)
1.304 +end do
1.305 +end
1.306 +