forrest@0: ;******************************************************** forrest@0: ;using model biome vlass forrest@0: ; forrest@0: ; required command line input parameters: forrest@0: ; ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl forrest@0: ; forrest@0: ; histogram normalized by rain and compute correleration forrest@0: ;************************************************************** forrest@0: load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" forrest@0: load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" forrest@0: load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" forrest@0: ;************************************************************** forrest@0: procedure set_line(lines:string,nline:integer,newlines:string) forrest@0: begin forrest@0: ; add line to ascci/html file forrest@0: forrest@0: nnewlines = dimsizes(newlines) forrest@0: if(nline+nnewlines-1.ge.dimsizes(lines)) forrest@0: print("set_line: bad index, not setting anything.") forrest@0: return forrest@0: end if forrest@0: lines(nline:nline+nnewlines-1) = newlines forrest@0: ; print ("lines = " + lines(nline:nline+nnewlines-1)) forrest@0: nline = nline + nnewlines forrest@0: return forrest@0: end forrest@0: ;************************************************************** forrest@0: ; Main code. forrest@0: begin forrest@0: forrest@0: plot_type = "ps" forrest@0: plot_type_new = "png" forrest@0: forrest@0: ;components forrest@0: forrest@0: component = (/"Leaf","Wood","Fine_Root","Litter","Coarse_Woody_Debris","Soil"/) forrest@0: n_comp = dimsizes(component) forrest@0: forrest@0: field_pool = (/"LEAFC","WOODC","FROOTC","LITTERC","CWDC","SOILC"/) forrest@0: field_flux = (/"LEAFC_ALLOC","WOODC_ALLOC","FROOTC_ALLOC","LITTERC_LOSS","CWDC_LOSS","SOILC_HR"/) forrest@0: forrest@0: ;************************************************ forrest@0: ; model name and grid forrest@0: ;************************************************ forrest@0: forrest@0: model_grid = "T42" forrest@0: forrest@0: model_name = "i01.06cn" forrest@0: ;model_name = "i01.06casa" forrest@0: forrest@0: ;************************************************ forrest@0: ; read data: ob forrest@0: ;************************************************ forrest@0: ;------------------------------------------------ forrest@0: ; read biome data: observed forrest@0: forrest@0: ; biome_name_ob = "MODIS LandCover" forrest@0: forrest@0: ; diro = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/" forrest@0: ; filo = "land_class_"+model_grid+".nc" forrest@0: forrest@0: ; fo = addfile(diro+filo,"r") forrest@0: forrest@0: ; classob = tofloat(fo->LAND_CLASS) forrest@0: forrest@0: ; delete (fo) forrest@0: forrest@0: ; observed data has 20 land-type classes forrest@0: forrest@0: ; nclass_ob = 20 forrest@0: forrest@0: ;--------------------------------------------------- forrest@0: ; read biome data: model forrest@0: forrest@0: biome_name_mod = "Model PFT Class" forrest@0: forrest@0: dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/" forrest@0: film = "class_pft_"+model_grid+".nc" forrest@0: forrest@0: fm = addfile(dirm+film,"r") forrest@0: forrest@0: classmod = fm->CLASS_PFT forrest@0: forrest@0: delete (fm) forrest@0: forrest@0: ; model data has 17 land-type classes forrest@0: forrest@0: nclass_mod = 17 forrest@0: forrest@0: ;--------------------------------------------------- forrest@0: ; read data: model, each component forrest@0: forrest@0: dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/" forrest@0: film = model_name + "_1980-2004_ANN_climo.nc" forrest@0: forrest@0: fm = addfile (dirm+film,"r") forrest@0: forrest@0: do k = 0,n_comp-1 forrest@0: forrest@0: pool = fm->$field_pool(k)$ forrest@0: flux = fm->$field_flux(k)$ forrest@0: forrest@0: ;Units for these variables are: forrest@0: ;pool: g C/m^2 forrest@0: ;flux: g C/m^2/s forrest@0: forrest@0: nsec_per_year = 60*60*24*365 forrest@0: forrest@0: flux = flux * nsec_per_year forrest@0: forrest@0: ; casa only forrest@0: ; all the plant pools (leaf, wood, and fine root) and forrest@0: ; coarse woody debris (cwd) and litter pools for forrest@0: ; CASA need to be divided by 1200. The soil flux forrest@0: ; and turnover time are fine and do not need to be adjusted. forrest@0: forrest@0: ; if (k .ne. n_comp-1) then forrest@0: ; flux = flux/1200. forrest@0: ; end if forrest@0: forrest@0: ;******************************************************************* forrest@0: ; Calculate "nice" bins for binning the data in equally spaced ranges forrest@0: ;******************************************************************** forrest@0: forrest@0: ; using observed biome class forrest@0: ; nclass = nclass_ob forrest@0: ; using model biome class forrest@0: nclass = nclass_mod forrest@0: forrest@0: nclassn = nclass + 1 forrest@0: range = fspan(0,nclassn-1,nclassn) forrest@0: ; print (range) forrest@0: forrest@0: ; Use this range information to grab all the values in a forrest@0: ; particular range, and then take an average. forrest@0: forrest@0: nr = dimsizes(range) forrest@0: nx = nr-1 forrest@0: xvalues = new((/2,nx/),float) forrest@0: xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2. forrest@0: dx = xvalues(0,1) - xvalues(0,0) ; range width forrest@0: dx4 = dx/4 ; 1/4 of the range forrest@0: xvalues(1,:) = xvalues(0,:) - dx/5. forrest@0: forrest@0: ;============================== forrest@0: ; put data into bins forrest@0: ;============================== forrest@0: forrest@0: ; using observed biome class forrest@0: ; base_1D = ndtooned(classob) forrest@0: ; using model biome class forrest@0: base_1D = ndtooned(classmod) forrest@0: forrest@0: data1_1D = ndtooned(pool) forrest@0: data2_1D = ndtooned(flux) forrest@0: forrest@0: ; output forrest@0: forrest@0: yvalues = new((/2,nx/),float) forrest@0: count = new((/2,nx/),float) forrest@0: forrest@0: do nd=0,1 forrest@0: forrest@0: ; See if we are doing data1 (nd=0) or data2 (nd=1). forrest@0: forrest@0: base = base_1D forrest@0: forrest@0: if(nd.eq.0) then forrest@0: data = data1_1D forrest@0: else forrest@0: data = data2_1D forrest@0: end if forrest@0: forrest@0: ; Loop through each range, using base. forrest@0: forrest@0: do i=0,nr-2 forrest@0: if (i.ne.(nr-2)) then forrest@0: ; print("") forrest@0: ; print("In range ["+range(i)+","+range(i+1)+")") forrest@0: idx = ind((base.ge.range(i)).and.(base.lt.range(i+1))) forrest@0: else forrest@0: ; print("") forrest@0: ; print("In range ["+range(i)+",)") forrest@0: idx = ind(base.ge.range(i)) forrest@0: end if forrest@0: forrest@0: ; Calculate average forrest@0: forrest@0: if(.not.any(ismissing(idx))) then forrest@0: yvalues(nd,i) = avg(data(idx)) forrest@0: count(nd,i) = dimsizes(idx) forrest@0: else forrest@0: yvalues(nd,i) = yvalues@_FillValue forrest@0: count(nd,i) = 0 forrest@0: end if forrest@0: forrest@0: ;############################################################# forrest@0: ;using observed biome class: forrest@0: ; set the following 4 classes to _FillValue: forrest@0: ; Water Bodies(0), Urban and Build-Up(13), forrest@0: ; Permenant Snow and Ice(15), Unclassified(17) forrest@0: forrest@0: ; if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then forrest@0: ; yvalues(nd,i) = yvalues@_FillValue forrest@0: ; count(nd,i) = 0 forrest@0: ; end if forrest@0: ;############################################################# forrest@0: forrest@0: ;############################################################# forrest@0: ;using model biome class: forrest@0: ; set the following 4 classes to _FillValue: forrest@0: ; (3)Needleleaf Deciduous Boreal Tree, forrest@0: ; (8)Broadleaf Deciduous Boreal Tree, forrest@0: ; (9)Broadleaf Evergreen Shrub, forrest@0: ; (16)Wheat forrest@0: forrest@0: if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then forrest@0: yvalues(nd,i) = yvalues@_FillValue forrest@0: count(nd,i) = 0 forrest@0: end if forrest@0: ;############################################################# forrest@0: forrest@0: ; print(nd + ": " + count + " points, avg = " + yvalues(nd,i)) forrest@0: forrest@0: ; Clean up for next time in loop. forrest@0: forrest@0: delete(idx) forrest@0: end do forrest@0: forrest@0: delete(data) forrest@0: end do forrest@0: forrest@0: ;============================ forrest@0: ;compute turnover time forrest@0: ;============================ forrest@0: forrest@0: u = yvalues(0,:) forrest@0: v = yvalues(1,:) forrest@0: u_count = count(0,:) forrest@0: v_count = count(1,:) forrest@0: forrest@0: ;print (dimsizes(u)) forrest@0: ;print (dimsizes(v)) forrest@0: forrest@0: good = ind(.not.ismissing(u) .and. .not.ismissing(v)) forrest@0: forrest@0: ;print (good) forrest@0: forrest@0: uu = u(good) forrest@0: vv = v(good) forrest@0: forrest@0: ;print (dimsizes(uu)) forrest@0: ;print (dimsizes(vv)) forrest@0: forrest@0: uu_count = u_count(good) forrest@0: vv_count = v_count(good) forrest@0: forrest@0: n_biome = dimsizes(uu) forrest@0: t_biome = new((/n_biome/),float) forrest@0: forrest@0: t_biome = uu/vv forrest@0: forrest@0: ;t_biome_avg = avg(t_biome) forrest@0: t_biome_avg = sum(uu*uu_count)/sum(vv*vv_count) forrest@0: forrest@0: ;print (t_biome) forrest@0: ;print (t_biome_avg) forrest@0: forrest@0: ;=========================== forrest@0: ; for html table - biome forrest@0: ;=========================== forrest@0: forrest@0: output_html = "table_"+component(k)+".html" forrest@0: forrest@0: ; column (not including header column) forrest@0: forrest@0: col_head = (/component(k)+" Flux",component(k)+" Pool",component(k)+" Turnover Time"/) forrest@0: forrest@0: ncol = dimsizes(col_head) forrest@0: forrest@0: ; row (not including header row) forrest@0: forrest@0: ;---------------------------------------------------- forrest@0: ; using observed biome class: forrest@0: ; row_head = (/"Evergreen Needleleaf Forests" \ forrest@0: ; ,"Evergreen Broadleaf Forests" \ forrest@0: ; ,"Deciduous Needleleaf Forest" \ forrest@0: ; ,"Deciduous Broadleaf Forests" \ forrest@0: ; ,"Mixed Forests" \ forrest@0: ; ,"Closed Bushlands" \ forrest@0: ; ,"Open Bushlands" \ forrest@0: ; ,"Woody Savannas (S. Hem.)" \ forrest@0: ; ,"Savannas (S. Hem.)" \ forrest@0: ; ,"Grasslands" \ forrest@0: ; ,"Permanent Wetlands" \ forrest@0: ; ,"Croplands" \ forrest@0: ; ,"Cropland/Natural Vegetation Mosaic" \ forrest@0: ; ,"Barren or Sparsely Vegetated" \ forrest@0: ; ,"Woody Savannas (N. Hem.)" \ forrest@0: ; ,"Savannas (N. Hem.)" \ forrest@0: ; ,"All Biome" \ forrest@0: ; /) forrest@0: forrest@0: ;---------------------------------------------------- forrest@0: ; using model biome class: forrest@0: row_head = (/"Not Vegetated" \ forrest@0: ,"Needleleaf Evergreen Temperate Tree" \ forrest@0: ,"Needleleaf Evergreen Boreal Tree" \ forrest@0: ; ,"Needleleaf Deciduous Boreal Tree" \ forrest@0: ,"Broadleaf Evergreen Tropical Tree" \ forrest@0: ,"Broadleaf Evergreen Temperate Tree" \ forrest@0: ,"Broadleaf Deciduous Tropical Tree" \ forrest@0: ,"Broadleaf Deciduous Temperate Tree" \ forrest@0: ; ,"Broadleaf Deciduous Boreal Tree" \ forrest@0: ; ,"Broadleaf Evergreen Shrub" \ forrest@0: ,"Broadleaf Deciduous Temperate Shrub" \ forrest@0: ,"Broadleaf Deciduous Boreal Shrub" \ forrest@0: ,"C3 Arctic Grass" \ forrest@0: ,"C3 Non-Arctic Grass" \ forrest@0: ,"C4 Grass" \ forrest@0: ,"Corn" \ forrest@0: ; ,"Wheat" \ forrest@0: ,"All Biome" \ forrest@0: /) forrest@0: nrow = dimsizes(row_head) forrest@0: forrest@0: ; arrays to be passed to table. forrest@0: text4 = new ((/nrow, ncol/),string ) forrest@0: forrest@0: do i=0,nrow-2 forrest@0: text4(i,0) = sprintf("%.1f",vv(i)) forrest@0: text4(i,1) = sprintf("%.1f",uu(i)) forrest@0: text4(i,2) = sprintf("%.2f",t_biome(i)) forrest@0: end do forrest@0: text4(nrow-1,0) = "-" forrest@0: text4(nrow-1,1) = "-" forrest@0: text4(nrow-1,2) = sprintf("%.2f",t_biome_avg) forrest@0: forrest@0: ;************************************************** forrest@0: ; html table forrest@0: ;************************************************** forrest@0: forrest@0: header_text = "

"+component(k)+" Turnover Time: Model "+model_name+"

" forrest@0: forrest@0: header = (/"" \ forrest@0: ,"" \ forrest@0: ,"CLAMP metrics" \ forrest@0: ,"" \ forrest@0: ,header_text \ forrest@0: /) forrest@0: footer = "" forrest@0: forrest@0: table_header = (/ \ forrest@0: "" \ forrest@0: ,"" \ forrest@0: ," " \ forrest@0: ," " \ forrest@0: ," " \ forrest@0: ," " \ forrest@0: ,"" \ forrest@0: /) forrest@0: table_footer = "
Biome Class"+col_head(0)+""+col_head(1)+""+col_head(2)+"
" forrest@0: row_header = "" forrest@0: row_footer = "" forrest@0: forrest@0: lines = new(50000,string) forrest@0: nline = 0 forrest@0: forrest@0: set_line(lines,nline,header) forrest@0: set_line(lines,nline,table_header) forrest@0: ;----------------------------------------------- forrest@0: ;row of table forrest@0: forrest@0: do n = 0,nrow-1 forrest@0: set_line(lines,nline,row_header) forrest@0: forrest@0: txt1 = row_head(n) forrest@0: txt2 = text4(n,0) forrest@0: txt3 = text4(n,1) forrest@0: txt4 = text4(n,2) forrest@0: forrest@0: set_line(lines,nline,""+txt1+"") forrest@0: set_line(lines,nline,""+txt2+"") forrest@0: set_line(lines,nline,""+txt3+"") forrest@0: set_line(lines,nline,""+txt4+"") forrest@0: forrest@0: set_line(lines,nline,row_footer) forrest@0: end do forrest@0: ;----------------------------------------------- forrest@0: set_line(lines,nline,table_footer) forrest@0: set_line(lines,nline,footer) forrest@0: forrest@0: ; Now write to an HTML file. forrest@0: idx = ind(.not.ismissing(lines)) forrest@0: if(.not.any(ismissing(idx))) then forrest@0: asciiwrite(output_html,lines(idx)) forrest@0: else forrest@0: print ("error?") forrest@0: end if forrest@0: forrest@0: delete (idx) forrest@0: end do forrest@0: end forrest@0: