fire/22.table+tseries.ncl
author Forrest Hoffman <forrest@climatemodeling.org>
Mon, 26 Jan 2009 22:08:20 -0500
changeset 0 0c6405ab2ff4
permissions -rw-r--r--
Initial commit of C-LAMP Diagnostics from Jeff Lee
     1 ;********************************************************
     2 ;using model biome vlass
     3 ;
     4 ; required command line input parameters:
     5 ;  ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
     6 ;
     7 ; histogram normalized by rain and compute correleration
     8 ;**************************************************************
     9 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
    10 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
    11 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
    12 ;**************************************************************
    13 procedure set_line(lines:string,nline:integer,newlines:string) 
    14 begin
    15 ; add line to ascci/html file
    16     
    17   nnewlines = dimsizes(newlines)
    18   if(nline+nnewlines-1.ge.dimsizes(lines))
    19     print("set_line: bad index, not setting anything.") 
    20     return
    21   end if 
    22   lines(nline:nline+nnewlines-1) = newlines
    23 ;  print ("lines = " + lines(nline:nline+nnewlines-1))
    24   nline = nline + nnewlines
    25   return 
    26 end
    27 ;**************************************************************
    28 ; Main code.
    29 begin
    30  
    31   plot_type     = "ps"
    32   plot_type_new = "png"
    33 
    34 ;---------------------------------------------------
    35 ; model name and grid       
    36 
    37   model_grid = "T42"
    38 
    39   model_name  = "cn"
    40   model_name1 = "i01.06cn"
    41   model_name2 = "i01.10cn"
    42 
    43 ;---------------------------------------------------
    44 ; get biome data: model
    45 
    46   biome_name_mod = "Model PFT Class"
    47 
    48   dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    49   film = "class_pft_"+model_grid+".nc"
    50   fm   = addfile(dirm+film,"r")
    51  
    52   classmod = fm->CLASS_PFT
    53 
    54   delete (fm)
    55 
    56 ; model data has 17 land-type classes
    57 
    58   nclass_mod = 17
    59 
    60 ;--------------------------------------------------
    61 ; get model data: landmask, landfrac and area
    62 
    63   dirm = "/fis/cgd/cseg/people/jeff/surface_data/" 
    64   film = "lnd_T42.nc"
    65   fm   = addfile (dirm+film,"r")
    66   
    67   landmask = fm->landmask
    68   landfrac = fm->landfrac
    69   area     = fm->area
    70 
    71   delete (fm)
    72 
    73 ; change area from km**2 to m**2
    74   area = area * 1.e6             
    75 
    76 ;----------------------------------------------------
    77 ; read data: time series, model
    78 
    79  dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    80  film = model_name2 + "_Fire_C_1979-2004_monthly.nc"
    81  fm   = addfile (dirm+film,"r")
    82 
    83  data_mod = fm->COL_FIRE_CLOSS(18:25,:,:,:)
    84 
    85  delete (fm)
    86 
    87 ; Units for these variables are:
    88 ; g C/m^2/s
    89 
    90 ; change unit to g C/m^2/month
    91 
    92   nsec_per_month = 60*60*24*30
    93  
    94   data_mod = data_mod * nsec_per_month 
    95 
    96   data_mod@unit = "gC/m2/month"
    97 ;----------------------------------------------------
    98 ; read data: time series, observed
    99 
   100  dirm = "/fis/cgd/cseg/people/jeff/fire_data/ob/GFEDv2_C/"
   101  film = "Fire_C_1997-2006_monthly_"+ model_grid+".nc"
   102  fm   = addfile (dirm+film,"r")
   103 
   104  data_ob = fm->FIRE_C(0:7,:,:,:)
   105 
   106  delete (fm)
   107 
   108  ob_name = "GFEDv2"
   109 
   110 ; Units for these variables are:
   111 ; g C/m^2/month
   112 
   113 ;---------------------------------------------------
   114 ; take into account landfrac
   115 
   116  area     = area * landfrac
   117  data_mod = data_mod * conform(data_mod, landfrac, (/2,3/))
   118  data_ob  = data_ob  * conform(data_ob,  landfrac, (/2,3/))
   119 
   120  delete (landfrac)
   121 
   122 ;-------------------------------------------------------------
   123 ; html table1 data
   124 
   125 ; column (not including header column)
   126 
   127   col_head  = (/"Model Fire_Flux (PgC/yr)" \
   128                ,"Observed Fire_Flux (PgC/yr)" \
   129                ,"Correlation Coefficient" \
   130                ,"Ratio model/observed" \
   131                ,"M_score" \
   132                ,"Timeseries plot" \
   133                /)
   134 
   135   ncol = dimsizes(col_head)
   136 
   137 ; row (not including header row)                   
   138 
   139 ; using model biome class:  
   140   row_head  = (/"Not Vegetated" \
   141                ,"Needleleaf Evergreen Temperate Tree" \
   142                ,"Needleleaf Evergreen Boreal Tree" \
   143 ;              ,"Needleleaf Deciduous Boreal Tree" \
   144                ,"Broadleaf Evergreen Tropical Tree" \
   145                ,"Broadleaf Evergreen Temperate Tree" \
   146                ,"Broadleaf Deciduous Tropical Tree" \
   147                ,"Broadleaf Deciduous Temperate Tree" \
   148 ;              ,"Broadleaf Deciduous Boreal Tree" \
   149 ;              ,"Broadleaf Evergreen Shrub" \
   150                ,"Broadleaf Deciduous Temperate Shrub" \
   151                ,"Broadleaf Deciduous Boreal Shrub" \
   152                ,"C3 Arctic Grass" \
   153                ,"C3 Non-Arctic Grass" \
   154                ,"C4 Grass" \
   155                ,"Corn" \
   156 ;              ,"Wheat" \                      
   157                ,"All Biome" \                
   158                /)  
   159   nrow = dimsizes(row_head)                  
   160 
   161 ; arrays to be passed to table. 
   162   text = new ((/nrow, ncol/),string ) 
   163 
   164 ;*****************************************************************
   165 ; (A) get time-mean
   166 ;*****************************************************************
   167   
   168   x          = dim_avg_Wrap(data_mod(lat|:,lon|:,month|:,year|:))
   169   data_mod_m = dim_avg_Wrap(       x(lat|:,lon|:,month|:))
   170   delete (x)
   171 
   172   x          = dim_avg_Wrap( data_ob(lat|:,lon|:,month|:,year|:))
   173   data_ob_m  = dim_avg_Wrap(       x(lat|:,lon|:,month|:))
   174   delete (x)
   175 
   176 ;----------------------------------------------------
   177 ; compute correlation coef
   178 
   179   landmask_1d = ndtooned(landmask)
   180   data_mod_1d = ndtooned(data_mod_m)
   181   data_ob_1d  = ndtooned(data_ob_m)
   182 
   183   good = ind(landmask_1d .gt. 0.)
   184 ; print (dimsizes(good))
   185 
   186   cc = esccr(data_mod_1d(good),data_ob_1d(good),0)
   187 ; print (cc)
   188 
   189   delete (landmask_1d)
   190   delete (data_mod_1d)
   191   delete (data_ob_1d)
   192   delete (good)
   193 
   194 ;----------------------------------------------------
   195 ; compute M_global
   196 
   197   score_max = 1.
   198 
   199   Mscore = cc * cc * score_max
   200 
   201   M_global = sprintf("%.2f", Mscore)
   202  
   203 ;----------------------------------------------------
   204 ; global res
   205 
   206   resg                      = True             ; Use plot options
   207   resg@cnFillOn             = True             ; Turn on color fill
   208   resg@gsnSpreadColors      = True             ; use full colormap
   209   resg@cnLinesOn            = False            ; Turn off contourn lines
   210   resg@mpFillOn             = False            ; Turn off map fill
   211   resg@cnLevelSelectionMode = "ManualLevels"   ; Manual contour invtervals
   212       
   213 ;----------------------------------------------------
   214 ; global contour: model vs ob
   215 
   216   plot_name = "global_model_vs_ob"
   217 
   218   wks = gsn_open_wks (plot_type,plot_name)   
   219   gsn_define_colormap(wks,"gui_default")     
   220 
   221   plot=new(3,graphic)                        ; create graphic array
   222 
   223   resg@gsnFrame             = False          ; Do not draw plot 
   224   resg@gsnDraw              = False          ; Do not advance frame
   225 
   226 ;----------------------
   227 ; plot correlation coef
   228 
   229   gRes               = True
   230   gRes@txFontHeightF = 0.02
   231   gRes@txAngleF      = 90
   232 
   233   correlation_text = "(correlation coef = "+sprintf("%.2f", cc)+")"
   234 
   235   gsn_text_ndc(wks,correlation_text,0.20,0.50,gRes)
   236 
   237 ;-----------------------  
   238 ; plot ob
   239 
   240   data_ob_m = where(landmask .gt. 0., data_ob_m, data_ob_m@_FillValue)
   241 
   242   title     = ob_name
   243   resg@tiMainString  = title
   244 
   245   resg@cnMinLevelValF       = 1.             
   246   resg@cnMaxLevelValF       = 10.             
   247   resg@cnLevelSpacingF      = 1.
   248 
   249   plot(0) = gsn_csm_contour_map_ce(wks,data_ob_m,resg)       
   250 
   251 ;-----------------------
   252 ; plot model
   253 
   254   data_mod_m = where(landmask .gt. 0., data_mod_m, data_mod_m@_FillValue)
   255 
   256   title     = "Model "+ model_name
   257   resg@tiMainString  = title
   258 
   259   resg@cnMinLevelValF       = 1.             
   260   resg@cnMaxLevelValF       = 10.             
   261   resg@cnLevelSpacingF      = 1.
   262 
   263   plot(1) = gsn_csm_contour_map_ce(wks,data_mod_m,resg) 
   264 
   265 ;-----------------------
   266 ; plot model-ob
   267 
   268      resg@cnMinLevelValF  = -8.           
   269      resg@cnMaxLevelValF  =  2.            
   270      resg@cnLevelSpacingF =  1.
   271 
   272   zz = data_ob_m
   273   zz = data_mod_m - data_ob_m
   274   title = "Model_"+model_name+" - Observed"
   275   resg@tiMainString    = title
   276 
   277   plot(2) = gsn_csm_contour_map_ce(wks,zz,resg) 
   278 
   279 ; plot panel
   280 
   281   pres                            = True        ; panel plot mods desired
   282   pres@gsnMaximize                = True        ; fill the page
   283 
   284   gsn_panel(wks,plot,(/3,1/),pres)              ; create panel plot
   285 
   286 ; system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   287 ;        "rm "+plot_name+"."+plot_type)
   288 
   289   clear (wks)
   290   delete (plot)
   291 
   292   delete (data_ob_m)
   293   delete (data_mod_m)
   294   delete (zz)
   295 
   296   resg@gsnFrame             = True          ; Do advance frame 
   297   resg@gsnDraw              = True          ; Do draw plot
   298 
   299 ;*******************************************************************
   300 ; (B) Time series : per biome
   301 ;*******************************************************************
   302 
   303  data_n = 2
   304 
   305  dsizes = dimsizes(data_mod)
   306  nyear  = dsizes(0)
   307  nmonth = dsizes(1)
   308  ntime  = nyear * nmonth
   309 
   310  year_start = 1997
   311  year_end   = 2004
   312                 
   313 ;-------------------------------------------
   314 ; Calculate "nice" bins for binning the data
   315 
   316 ; using model biome class
   317   nclass = nclass_mod
   318 
   319   range  = fspan(0,nclass,nclass+1)
   320 
   321 ; print (range)
   322 ; Use this range information to grab all the values in a
   323 ; particular range, and then take an average.
   324 
   325   nx = dimsizes(range) - 1
   326 
   327 ;-------------------------------------------
   328 ; put data into bins
   329 
   330 ; using observed biome class
   331 ; base  = ndtooned(classob)
   332 ; using model biome class
   333   base  = ndtooned(classmod)
   334 
   335 ; output
   336 
   337   area_bin = new((/nx/),float)
   338   yvalues  = new((/ntime,data_n,nx/),float)
   339 
   340 ; Loop through each range, using base.
   341 
   342   do i=0,nx-1
   343 
   344      if (i.ne.(nx-1)) then
   345         idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
   346      else
   347         idx = ind(base.ge.range(i))
   348      end if
   349 ;---------------------
   350 ;    for area
   351 
   352      data = ndtooned(area)  
   353 
   354      if (.not.any(ismissing(idx))) then 
   355         area_bin(i) = sum(data(idx))
   356      else
   357         area_bin(i) = area_bin@_FillValue
   358      end if
   359 
   360 ;#############################################################
   361 ; using model biome class:
   362 ;     set the following 4 classes to _FillValue:
   363 ;     (3)Needleleaf Deciduous Boreal Tree,
   364 ;     (8)Broadleaf Deciduous Boreal Tree,
   365 ;     (9)Broadleaf Evergreen Shrub,
   366 ;     (16)Wheat
   367 
   368      if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   369         area_bin(i) = area_bin@_FillValue
   370      end if
   371 ;#############################################################  
   372 
   373   delete (data)
   374 
   375 ;---------------------
   376 ; for data_mod and data_ob
   377 
   378   do n = 0,data_n-1
   379 
   380      t = -1
   381      do m = 0,nyear-1
   382      do k = 0,nmonth-1
   383     
   384         t = t + 1 
   385 
   386         if (n.eq.0) then
   387            data = ndtooned(data_ob(m,k,:,:))
   388         end if
   389 
   390         if (n.eq.1) then
   391            data = ndtooned(data_mod(m,k,:,:))
   392         end if
   393 
   394 ;       Calculate average
   395  
   396         if (.not.any(ismissing(idx))) then 
   397            yvalues(t,n,i) = avg(data(idx))
   398         else
   399            yvalues(t,n,i) = yvalues@_FillValue
   400         end if
   401 
   402 ;#############################################################
   403 ; using model biome class:
   404 ;     set the following 4 classes to _FillValue:
   405 ;     (3)Needleleaf Deciduous Boreal Tree,
   406 ;     (8)Broadleaf Deciduous Boreal Tree,
   407 ;     (9)Broadleaf Evergreen Shrub,
   408 ;     (16)Wheat
   409 
   410         if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
   411            yvalues(t,n,i) = yvalues@_FillValue
   412         end if
   413 ;#############################################################  
   414 
   415      end do
   416      end do
   417 
   418      delete(data)
   419   end do 
   420 
   421     delete(idx)
   422   end do
   423 
   424   delete (base)
   425   delete (data_mod)
   426   delete (data_ob)
   427 
   428 ;----------------------------------------------------------------
   429 ; get area_good
   430 
   431   good = ind(.not.ismissing(area_bin))
   432 
   433   area_g = area_bin(good)  
   434 
   435   n_biome = dimsizes(good)
   436 
   437 ;----------------------------------------------------------------
   438 ; data for tseries plot
   439 
   440   yvalues_g = new((/ntime,data_n,n_biome/),float)
   441 
   442   yvalues_g@units = "TgC/month"
   443 
   444 ; change unit to Tg C/month
   445 ; change unit from g to Tg (Tera gram)
   446   factor_unit = 1.e-12
   447 
   448   yvalues_g = yvalues(:,:,good) * conform(yvalues_g,area_g,2) * factor_unit
   449 
   450 ;-------------------------------------------------------------------
   451 ; general settings for line plot
   452 
   453   res                   = True               
   454   res@xyDashPatterns    = (/0,0/)          ; make lines solid
   455   res@xyLineThicknesses = (/2.0,2.0/)      ; make lines thicker
   456   res@xyLineColors      = (/"blue","red"/) ; line color
   457 
   458   res@trXMinF   = year_start
   459   res@trXMaxF   = year_end + 1
   460 
   461   res@vpHeightF = 0.4                 ; change aspect ratio of plot
   462 ; res@vpWidthF  = 0.8
   463   res@vpWidthF  = 0.75   
   464 
   465   res@tiMainFontHeightF = 0.025       ; size of title 
   466 
   467   res@tmXBFormat  = "f"               ; not to add trailing zeros
   468 
   469 ; res@gsnMaximize = True
   470 
   471 ;----------------------------------------------
   472 ; Add a boxed legend using the simple method
   473 
   474   res@pmLegendDisplayMode    = "Always"
   475 ; res@pmLegendWidthF         = 0.1
   476   res@pmLegendWidthF         = 0.08
   477   res@pmLegendHeightF        = 0.06
   478   res@pmLegendOrthogonalPosF = -1.17
   479 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
   480 ; res@pmLegendOrthogonalPosF = -0.30  ;(downward)
   481 
   482 ; res@pmLegendParallelPosF   =  0.18
   483   res@pmLegendParallelPosF   =  0.23  ;(rightward)
   484   res@pmLegendParallelPosF   =  0.73  ;(rightward)
   485   res@pmLegendParallelPosF   =  0.83  ;(rightward)
   486 
   487 ; res@lgPerimOn             = False
   488   res@lgLabelFontHeightF     = 0.015
   489   res@xyExplicitLegendLabels = (/"observed",model_name/)
   490 
   491 ;*******************************************************************
   492 ; (A) time series plot: monthly ( 2 lines per plot)
   493 ;*******************************************************************
   494 
   495 ; x-axis in time series plot
   496 
   497   timeI = new((/ntime/),integer)
   498   timeF = new((/ntime/),float)
   499   timeI = ispan(1,ntime,1)
   500   timeF = year_start + (timeI-1)/12.
   501   timeF@long_name = "year" 
   502 
   503   plot_data = new((/2,ntime/),float)
   504   plot_data@long_name = "TgC/month"
   505 
   506 ;----------------------------------------------
   507 ; time series : per biome
   508  
   509   do m = 0, n_biome-1
   510 
   511      plot_name = "monthly_biome_"+ m
   512 
   513      wks = gsn_open_wks (plot_type,plot_name)   
   514 
   515      title = "Fire : "+ row_head(m)
   516      res@tiMainString = title
   517 
   518      plot_data(0,:) = yvalues_g(:,0,m)
   519      plot_data(1,:) = yvalues_g(:,1,m)
   520                                   
   521      plot = gsn_csm_xy(wks,timeF,plot_data,res)
   522 
   523 ;    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   524 ;          "rm "+plot_name+"."+plot_type)
   525 
   526      clear (wks)  
   527      delete (plot)
   528    
   529   end do
   530 
   531 ;--------------------------------------------
   532 ; time series: global
   533 
   534      plot_name = "monthly_global"
   535 
   536      wks = gsn_open_wks (plot_type,plot_name)   
   537 
   538      title = "Fire : "+ row_head(n_biome)
   539      res@tiMainString = title
   540 
   541      do k = 0,ntime-1
   542         plot_data(0,k) = sum(yvalues_g(k,0,:))
   543         plot_data(1,k) = sum(yvalues_g(k,1,:))
   544      end do
   545                                   
   546      plot = gsn_csm_xy(wks,timeF,plot_data,res)
   547 
   548 ;    system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   549 ;          "rm "+plot_name+"."+plot_type)
   550 
   551      clear (wks)  
   552      delete (plot)
   553 end
   554