fire/22.table+tseries.ncl
changeset 0 0c6405ab2ff4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/fire/22.table+tseries.ncl	Mon Jan 26 22:08:20 2009 -0500
     1.3 @@ -0,0 +1,554 @@
     1.4 +;********************************************************
     1.5 +;using model biome vlass
     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 +; histogram normalized by rain and compute correleration
    1.11 +;**************************************************************
    1.12 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
    1.13 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
    1.14 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
    1.15 +;**************************************************************
    1.16 +procedure set_line(lines:string,nline:integer,newlines:string) 
    1.17 +begin
    1.18 +; add line to ascci/html file
    1.19 +    
    1.20 +  nnewlines = dimsizes(newlines)
    1.21 +  if(nline+nnewlines-1.ge.dimsizes(lines))
    1.22 +    print("set_line: bad index, not setting anything.") 
    1.23 +    return
    1.24 +  end if 
    1.25 +  lines(nline:nline+nnewlines-1) = newlines
    1.26 +;  print ("lines = " + lines(nline:nline+nnewlines-1))
    1.27 +  nline = nline + nnewlines
    1.28 +  return 
    1.29 +end
    1.30 +;**************************************************************
    1.31 +; Main code.
    1.32 +begin
    1.33 + 
    1.34 +  plot_type     = "ps"
    1.35 +  plot_type_new = "png"
    1.36 +
    1.37 +;---------------------------------------------------
    1.38 +; model name and grid       
    1.39 +
    1.40 +  model_grid = "T42"
    1.41 +
    1.42 +  model_name  = "cn"
    1.43 +  model_name1 = "i01.06cn"
    1.44 +  model_name2 = "i01.10cn"
    1.45 +
    1.46 +;---------------------------------------------------
    1.47 +; get biome data: model
    1.48 +
    1.49 +  biome_name_mod = "Model PFT Class"
    1.50 +
    1.51 +  dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    1.52 +  film = "class_pft_"+model_grid+".nc"
    1.53 +  fm   = addfile(dirm+film,"r")
    1.54 + 
    1.55 +  classmod = fm->CLASS_PFT
    1.56 +
    1.57 +  delete (fm)
    1.58 +
    1.59 +; model data has 17 land-type classes
    1.60 +
    1.61 +  nclass_mod = 17
    1.62 +
    1.63 +;--------------------------------------------------
    1.64 +; get model data: landmask, landfrac and area
    1.65 +
    1.66 +  dirm = "/fis/cgd/cseg/people/jeff/surface_data/" 
    1.67 +  film = "lnd_T42.nc"
    1.68 +  fm   = addfile (dirm+film,"r")
    1.69 +  
    1.70 +  landmask = fm->landmask
    1.71 +  landfrac = fm->landfrac
    1.72 +  area     = fm->area
    1.73 +
    1.74 +  delete (fm)
    1.75 +
    1.76 +; change area from km**2 to m**2
    1.77 +  area = area * 1.e6             
    1.78 +
    1.79 +;----------------------------------------------------
    1.80 +; read data: time series, model
    1.81 +
    1.82 + dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    1.83 + film = model_name2 + "_Fire_C_1979-2004_monthly.nc"
    1.84 + fm   = addfile (dirm+film,"r")
    1.85 +
    1.86 + data_mod = fm->COL_FIRE_CLOSS(18:25,:,:,:)
    1.87 +
    1.88 + delete (fm)
    1.89 +
    1.90 +; Units for these variables are:
    1.91 +; g C/m^2/s
    1.92 +
    1.93 +; change unit to g C/m^2/month
    1.94 +
    1.95 +  nsec_per_month = 60*60*24*30
    1.96 + 
    1.97 +  data_mod = data_mod * nsec_per_month 
    1.98 +
    1.99 +  data_mod@unit = "gC/m2/month"
   1.100 +;----------------------------------------------------
   1.101 +; read data: time series, observed
   1.102 +
   1.103 + dirm = "/fis/cgd/cseg/people/jeff/fire_data/ob/GFEDv2_C/"
   1.104 + film = "Fire_C_1997-2006_monthly_"+ model_grid+".nc"
   1.105 + fm   = addfile (dirm+film,"r")
   1.106 +
   1.107 + data_ob = fm->FIRE_C(0:7,:,:,:)
   1.108 +
   1.109 + delete (fm)
   1.110 +
   1.111 + ob_name = "GFEDv2"
   1.112 +
   1.113 +; Units for these variables are:
   1.114 +; g C/m^2/month
   1.115 +
   1.116 +;---------------------------------------------------
   1.117 +; take into account landfrac
   1.118 +
   1.119 + area     = area * landfrac
   1.120 + data_mod = data_mod * conform(data_mod, landfrac, (/2,3/))
   1.121 + data_ob  = data_ob  * conform(data_ob,  landfrac, (/2,3/))
   1.122 +
   1.123 + delete (landfrac)
   1.124 +
   1.125 +;-------------------------------------------------------------
   1.126 +; html table1 data
   1.127 +
   1.128 +; column (not including header column)
   1.129 +
   1.130 +  col_head  = (/"Model Fire_Flux (PgC/yr)" \
   1.131 +               ,"Observed Fire_Flux (PgC/yr)" \
   1.132 +               ,"Correlation Coefficient" \
   1.133 +               ,"Ratio model/observed" \
   1.134 +               ,"M_score" \
   1.135 +               ,"Timeseries plot" \
   1.136 +               /)
   1.137 +
   1.138 +  ncol = dimsizes(col_head)
   1.139 +
   1.140 +; row (not including header row)                   
   1.141 +
   1.142 +; using model biome class:  
   1.143 +  row_head  = (/"Not Vegetated" \
   1.144 +               ,"Needleleaf Evergreen Temperate Tree" \
   1.145 +               ,"Needleleaf Evergreen Boreal Tree" \
   1.146 +;              ,"Needleleaf Deciduous Boreal Tree" \
   1.147 +               ,"Broadleaf Evergreen Tropical Tree" \
   1.148 +               ,"Broadleaf Evergreen Temperate Tree" \
   1.149 +               ,"Broadleaf Deciduous Tropical Tree" \
   1.150 +               ,"Broadleaf Deciduous Temperate Tree" \
   1.151 +;              ,"Broadleaf Deciduous Boreal Tree" \
   1.152 +;              ,"Broadleaf Evergreen Shrub" \
   1.153 +               ,"Broadleaf Deciduous Temperate Shrub" \
   1.154 +               ,"Broadleaf Deciduous Boreal Shrub" \
   1.155 +               ,"C3 Arctic Grass" \
   1.156 +               ,"C3 Non-Arctic Grass" \
   1.157 +               ,"C4 Grass" \
   1.158 +               ,"Corn" \
   1.159 +;              ,"Wheat" \                      
   1.160 +               ,"All Biome" \                
   1.161 +               /)  
   1.162 +  nrow = dimsizes(row_head)                  
   1.163 +
   1.164 +; arrays to be passed to table. 
   1.165 +  text = new ((/nrow, ncol/),string ) 
   1.166 +
   1.167 +;*****************************************************************
   1.168 +; (A) get time-mean
   1.169 +;*****************************************************************
   1.170 +  
   1.171 +  x          = dim_avg_Wrap(data_mod(lat|:,lon|:,month|:,year|:))
   1.172 +  data_mod_m = dim_avg_Wrap(       x(lat|:,lon|:,month|:))
   1.173 +  delete (x)
   1.174 +
   1.175 +  x          = dim_avg_Wrap( data_ob(lat|:,lon|:,month|:,year|:))
   1.176 +  data_ob_m  = dim_avg_Wrap(       x(lat|:,lon|:,month|:))
   1.177 +  delete (x)
   1.178 +
   1.179 +;----------------------------------------------------
   1.180 +; compute correlation coef
   1.181 +
   1.182 +  landmask_1d = ndtooned(landmask)
   1.183 +  data_mod_1d = ndtooned(data_mod_m)
   1.184 +  data_ob_1d  = ndtooned(data_ob_m)
   1.185 +
   1.186 +  good = ind(landmask_1d .gt. 0.)
   1.187 +; print (dimsizes(good))
   1.188 +
   1.189 +  cc = esccr(data_mod_1d(good),data_ob_1d(good),0)
   1.190 +; print (cc)
   1.191 +
   1.192 +  delete (landmask_1d)
   1.193 +  delete (data_mod_1d)
   1.194 +  delete (data_ob_1d)
   1.195 +  delete (good)
   1.196 +
   1.197 +;----------------------------------------------------
   1.198 +; compute M_global
   1.199 +
   1.200 +  score_max = 1.
   1.201 +
   1.202 +  Mscore = cc * cc * score_max
   1.203 +
   1.204 +  M_global = sprintf("%.2f", Mscore)
   1.205 + 
   1.206 +;----------------------------------------------------
   1.207 +; global res
   1.208 +
   1.209 +  resg                      = True             ; Use plot options
   1.210 +  resg@cnFillOn             = True             ; Turn on color fill
   1.211 +  resg@gsnSpreadColors      = True             ; use full colormap
   1.212 +  resg@cnLinesOn            = False            ; Turn off contourn lines
   1.213 +  resg@mpFillOn             = False            ; Turn off map fill
   1.214 +  resg@cnLevelSelectionMode = "ManualLevels"   ; Manual contour invtervals
   1.215 +      
   1.216 +;----------------------------------------------------
   1.217 +; global contour: model vs ob
   1.218 +
   1.219 +  plot_name = "global_model_vs_ob"
   1.220 +
   1.221 +  wks = gsn_open_wks (plot_type,plot_name)   
   1.222 +  gsn_define_colormap(wks,"gui_default")     
   1.223 +
   1.224 +  plot=new(3,graphic)                        ; create graphic array
   1.225 +
   1.226 +  resg@gsnFrame             = False          ; Do not draw plot 
   1.227 +  resg@gsnDraw              = False          ; Do not advance frame
   1.228 +
   1.229 +;----------------------
   1.230 +; plot correlation coef
   1.231 +
   1.232 +  gRes               = True
   1.233 +  gRes@txFontHeightF = 0.02
   1.234 +  gRes@txAngleF      = 90
   1.235 +
   1.236 +  correlation_text = "(correlation coef = "+sprintf("%.2f", cc)+")"
   1.237 +
   1.238 +  gsn_text_ndc(wks,correlation_text,0.20,0.50,gRes)
   1.239 +
   1.240 +;-----------------------  
   1.241 +; plot ob
   1.242 +
   1.243 +  data_ob_m = where(landmask .gt. 0., data_ob_m, data_ob_m@_FillValue)
   1.244 +
   1.245 +  title     = ob_name
   1.246 +  resg@tiMainString  = title
   1.247 +
   1.248 +  resg@cnMinLevelValF       = 1.             
   1.249 +  resg@cnMaxLevelValF       = 10.             
   1.250 +  resg@cnLevelSpacingF      = 1.
   1.251 +
   1.252 +  plot(0) = gsn_csm_contour_map_ce(wks,data_ob_m,resg)       
   1.253 +
   1.254 +;-----------------------
   1.255 +; plot model
   1.256 +
   1.257 +  data_mod_m = where(landmask .gt. 0., data_mod_m, data_mod_m@_FillValue)
   1.258 +
   1.259 +  title     = "Model "+ model_name
   1.260 +  resg@tiMainString  = title
   1.261 +
   1.262 +  resg@cnMinLevelValF       = 1.             
   1.263 +  resg@cnMaxLevelValF       = 10.             
   1.264 +  resg@cnLevelSpacingF      = 1.
   1.265 +
   1.266 +  plot(1) = gsn_csm_contour_map_ce(wks,data_mod_m,resg) 
   1.267 +
   1.268 +;-----------------------
   1.269 +; plot model-ob
   1.270 +
   1.271 +     resg@cnMinLevelValF  = -8.           
   1.272 +     resg@cnMaxLevelValF  =  2.            
   1.273 +     resg@cnLevelSpacingF =  1.
   1.274 +
   1.275 +  zz = data_ob_m
   1.276 +  zz = data_mod_m - data_ob_m
   1.277 +  title = "Model_"+model_name+" - Observed"
   1.278 +  resg@tiMainString    = title
   1.279 +
   1.280 +  plot(2) = gsn_csm_contour_map_ce(wks,zz,resg) 
   1.281 +
   1.282 +; plot panel
   1.283 +
   1.284 +  pres                            = True        ; panel plot mods desired
   1.285 +  pres@gsnMaximize                = True        ; fill the page
   1.286 +
   1.287 +  gsn_panel(wks,plot,(/3,1/),pres)              ; create panel plot
   1.288 +
   1.289 +; system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   1.290 +;        "rm "+plot_name+"."+plot_type)
   1.291 +
   1.292 +  clear (wks)
   1.293 +  delete (plot)
   1.294 +
   1.295 +  delete (data_ob_m)
   1.296 +  delete (data_mod_m)
   1.297 +  delete (zz)
   1.298 +
   1.299 +  resg@gsnFrame             = True          ; Do advance frame 
   1.300 +  resg@gsnDraw              = True          ; Do draw plot
   1.301 +
   1.302 +;*******************************************************************
   1.303 +; (B) Time series : per biome
   1.304 +;*******************************************************************
   1.305 +
   1.306 + data_n = 2
   1.307 +
   1.308 + dsizes = dimsizes(data_mod)
   1.309 + nyear  = dsizes(0)
   1.310 + nmonth = dsizes(1)
   1.311 + ntime  = nyear * nmonth
   1.312 +
   1.313 + year_start = 1997
   1.314 + year_end   = 2004
   1.315 +                
   1.316 +;-------------------------------------------
   1.317 +; Calculate "nice" bins for binning the data
   1.318 +
   1.319 +; using model biome class
   1.320 +  nclass = nclass_mod
   1.321 +
   1.322 +  range  = fspan(0,nclass,nclass+1)
   1.323 +
   1.324 +; print (range)
   1.325 +; Use this range information to grab all the values in a
   1.326 +; particular range, and then take an average.
   1.327 +
   1.328 +  nx = dimsizes(range) - 1
   1.329 +
   1.330 +;-------------------------------------------
   1.331 +; put data into bins
   1.332 +
   1.333 +; using observed biome class
   1.334 +; base  = ndtooned(classob)
   1.335 +; using model biome class
   1.336 +  base  = ndtooned(classmod)
   1.337 +
   1.338 +; output
   1.339 +
   1.340 +  area_bin = new((/nx/),float)
   1.341 +  yvalues  = new((/ntime,data_n,nx/),float)
   1.342 +
   1.343 +; Loop through each range, using base.
   1.344 +
   1.345 +  do i=0,nx-1
   1.346 +
   1.347 +     if (i.ne.(nx-1)) then
   1.348 +        idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
   1.349 +     else
   1.350 +        idx = ind(base.ge.range(i))
   1.351 +     end if
   1.352 +;---------------------
   1.353 +;    for area
   1.354 +
   1.355 +     data = ndtooned(area)  
   1.356 +
   1.357 +     if (.not.any(ismissing(idx))) then 
   1.358 +        area_bin(i) = sum(data(idx))
   1.359 +     else
   1.360 +        area_bin(i) = area_bin@_FillValue
   1.361 +     end if
   1.362 +
   1.363 +;#############################################################
   1.364 +; using model biome class:
   1.365 +;     set the following 4 classes to _FillValue:
   1.366 +;     (3)Needleleaf Deciduous Boreal Tree,
   1.367 +;     (8)Broadleaf Deciduous Boreal Tree,
   1.368 +;     (9)Broadleaf Evergreen Shrub,
   1.369 +;     (16)Wheat
   1.370 +
   1.371 +     if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   1.372 +        area_bin(i) = area_bin@_FillValue
   1.373 +     end if
   1.374 +;#############################################################  
   1.375 +
   1.376 +  delete (data)
   1.377 +
   1.378 +;---------------------
   1.379 +; for data_mod and data_ob
   1.380 +
   1.381 +  do n = 0,data_n-1
   1.382 +
   1.383 +     t = -1
   1.384 +     do m = 0,nyear-1
   1.385 +     do k = 0,nmonth-1
   1.386 +    
   1.387 +        t = t + 1 
   1.388 +
   1.389 +        if (n.eq.0) then
   1.390 +           data = ndtooned(data_ob(m,k,:,:))
   1.391 +        end if
   1.392 +
   1.393 +        if (n.eq.1) then
   1.394 +           data = ndtooned(data_mod(m,k,:,:))
   1.395 +        end if
   1.396 +
   1.397 +;       Calculate average
   1.398 + 
   1.399 +        if (.not.any(ismissing(idx))) then 
   1.400 +           yvalues(t,n,i) = avg(data(idx))
   1.401 +        else
   1.402 +           yvalues(t,n,i) = yvalues@_FillValue
   1.403 +        end if
   1.404 +
   1.405 +;#############################################################
   1.406 +; using model biome class:
   1.407 +;     set the following 4 classes to _FillValue:
   1.408 +;     (3)Needleleaf Deciduous Boreal Tree,
   1.409 +;     (8)Broadleaf Deciduous Boreal Tree,
   1.410 +;     (9)Broadleaf Evergreen Shrub,
   1.411 +;     (16)Wheat
   1.412 +
   1.413 +        if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   1.414 +           yvalues(t,n,i) = yvalues@_FillValue
   1.415 +        end if
   1.416 +;#############################################################  
   1.417 +
   1.418 +     end do
   1.419 +     end do
   1.420 +
   1.421 +     delete(data)
   1.422 +  end do 
   1.423 +
   1.424 +    delete(idx)
   1.425 +  end do
   1.426 +
   1.427 +  delete (base)
   1.428 +  delete (data_mod)
   1.429 +  delete (data_ob)
   1.430 +
   1.431 +;----------------------------------------------------------------
   1.432 +; get area_good
   1.433 +
   1.434 +  good = ind(.not.ismissing(area_bin))
   1.435 +
   1.436 +  area_g = area_bin(good)  
   1.437 +
   1.438 +  n_biome = dimsizes(good)
   1.439 +
   1.440 +;----------------------------------------------------------------
   1.441 +; data for tseries plot
   1.442 +
   1.443 +  yvalues_g = new((/ntime,data_n,n_biome/),float)
   1.444 +
   1.445 +  yvalues_g@units = "TgC/month"
   1.446 +
   1.447 +; change unit to Tg C/month
   1.448 +; change unit from g to Tg (Tera gram)
   1.449 +  factor_unit = 1.e-12
   1.450 +
   1.451 +  yvalues_g = yvalues(:,:,good) * conform(yvalues_g,area_g,2) * factor_unit
   1.452 +
   1.453 +;-------------------------------------------------------------------
   1.454 +; general settings for line plot
   1.455 +
   1.456 +  res                   = True               
   1.457 +  res@xyDashPatterns    = (/0,0/)          ; make lines solid
   1.458 +  res@xyLineThicknesses = (/2.0,2.0/)      ; make lines thicker
   1.459 +  res@xyLineColors      = (/"blue","red"/) ; line color
   1.460 +
   1.461 +  res@trXMinF   = year_start
   1.462 +  res@trXMaxF   = year_end + 1
   1.463 +
   1.464 +  res@vpHeightF = 0.4                 ; change aspect ratio of plot
   1.465 +; res@vpWidthF  = 0.8
   1.466 +  res@vpWidthF  = 0.75   
   1.467 +
   1.468 +  res@tiMainFontHeightF = 0.025       ; size of title 
   1.469 +
   1.470 +  res@tmXBFormat  = "f"               ; not to add trailing zeros
   1.471 +
   1.472 +; res@gsnMaximize = True
   1.473 +
   1.474 +;----------------------------------------------
   1.475 +; Add a boxed legend using the simple method
   1.476 +
   1.477 +  res@pmLegendDisplayMode    = "Always"
   1.478 +; res@pmLegendWidthF         = 0.1
   1.479 +  res@pmLegendWidthF         = 0.08
   1.480 +  res@pmLegendHeightF        = 0.06
   1.481 +  res@pmLegendOrthogonalPosF = -1.17
   1.482 +; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
   1.483 +; res@pmLegendOrthogonalPosF = -0.30  ;(downward)
   1.484 +
   1.485 +; res@pmLegendParallelPosF   =  0.18
   1.486 +  res@pmLegendParallelPosF   =  0.23  ;(rightward)
   1.487 +  res@pmLegendParallelPosF   =  0.73  ;(rightward)
   1.488 +  res@pmLegendParallelPosF   =  0.83  ;(rightward)
   1.489 +
   1.490 +; res@lgPerimOn             = False
   1.491 +  res@lgLabelFontHeightF     = 0.015
   1.492 +  res@xyExplicitLegendLabels = (/"observed",model_name/)
   1.493 +
   1.494 +;*******************************************************************
   1.495 +; (A) time series plot: monthly ( 2 lines per plot)
   1.496 +;*******************************************************************
   1.497 +
   1.498 +; x-axis in time series plot
   1.499 +
   1.500 +  timeI = new((/ntime/),integer)
   1.501 +  timeF = new((/ntime/),float)
   1.502 +  timeI = ispan(1,ntime,1)
   1.503 +  timeF = year_start + (timeI-1)/12.
   1.504 +  timeF@long_name = "year" 
   1.505 +
   1.506 +  plot_data = new((/2,ntime/),float)
   1.507 +  plot_data@long_name = "TgC/month"
   1.508 +
   1.509 +;----------------------------------------------
   1.510 +; time series : per biome
   1.511 + 
   1.512 +  do m = 0, n_biome-1
   1.513 +
   1.514 +     plot_name = "monthly_biome_"+ m
   1.515 +
   1.516 +     wks = gsn_open_wks (plot_type,plot_name)   
   1.517 +
   1.518 +     title = "Fire : "+ row_head(m)
   1.519 +     res@tiMainString = title
   1.520 +
   1.521 +     plot_data(0,:) = yvalues_g(:,0,m)
   1.522 +     plot_data(1,:) = yvalues_g(:,1,m)
   1.523 +                                  
   1.524 +     plot = gsn_csm_xy(wks,timeF,plot_data,res)
   1.525 +
   1.526 +;    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   1.527 +;          "rm "+plot_name+"."+plot_type)
   1.528 +
   1.529 +     clear (wks)  
   1.530 +     delete (plot)
   1.531 +   
   1.532 +  end do
   1.533 +
   1.534 +;--------------------------------------------
   1.535 +; time series: global
   1.536 +
   1.537 +     plot_name = "monthly_global"
   1.538 +
   1.539 +     wks = gsn_open_wks (plot_type,plot_name)   
   1.540 +
   1.541 +     title = "Fire : "+ row_head(n_biome)
   1.542 +     res@tiMainString = title
   1.543 +
   1.544 +     do k = 0,ntime-1
   1.545 +        plot_data(0,k) = sum(yvalues_g(k,0,:))
   1.546 +        plot_data(1,k) = sum(yvalues_g(k,1,:))
   1.547 +     end do
   1.548 +                                  
   1.549 +     plot = gsn_csm_xy(wks,timeF,plot_data,res)
   1.550 +
   1.551 +;    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   1.552 +;          "rm "+plot_name+"."+plot_type)
   1.553 +
   1.554 +     clear (wks)  
   1.555 +     delete (plot)
   1.556 +end
   1.557 +