1 ;**************************************************************
2 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
3 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
4 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
5 ;**************************************************************
6 procedure set_line(lines:string,nline:integer,newlines:string)
8 ; add line to ascci/html file
10 nnewlines = dimsizes(newlines)
11 if(nline+nnewlines-1.ge.dimsizes(lines))
12 print("set_line: bad index, not setting anything.")
15 lines(nline:nline+nnewlines-1) = newlines
16 ; print ("lines = " + lines(nline:nline+nnewlines-1))
17 nline = nline + nnewlines
20 ;**************************************************************
27 ;------------------------------------------------------
28 ; edit table.html of current model for movel1_vs_model2
30 if (isvar("compare")) then
31 html_name2 = compare+"/table.html"
32 html_new2 = html_name2 +".new"
35 ;------------------------------------------------------
36 ; edit table.html for current model
38 html_name = model_name+"/table.html"
39 html_new = html_name +".new"
41 ;------------------------------------------------------
42 ; get biome data: model
44 biome_name_mod = "Model PFT Class"
46 film_c = "class_pft_"+ model_grid +".nc"
47 fm_c = addfile (dirs+film_c,"r")
48 classmod = fm_c->CLASS_PFT
52 ; model data has 17 land-type classes
55 ;--------------------------------
56 ; get model data: landmask, landfrac and area
58 film_l = "lnd_"+ model_grid +".nc"
59 fm_l = addfile (dirs+film_l,"r")
60 landmask = fm_l->landmask
61 landfrac = fm_l->landfrac
66 ; change area from km**2 to m**2
69 ; take into account landfrac
70 area = area * landfrac
72 ;--------------------------------
73 ; read data: time series, model
75 fm = addfile (dirm+film7,"r")
77 data_mod = fm->COL_FIRE_CLOSS(18:25,:,:,:)
81 ; Units for these variables are:
84 ; change unit to gC/m2/month
86 nsec_per_month = 60*60*24*30
88 data_mod = data_mod * nsec_per_month
90 data_mod@units = "gC/m2/month"
92 ;----------------------------------------------------
93 ; read data: time series, observed
95 dir_f = diro + "fire/"
96 fil_f = "Fire_C_1997-2006_monthly_"+ model_grid+".nc"
97 fm = addfile (dir_f+fil_f,"r")
98 data_ob = fm->FIRE_C(0:7,:,:,:)
104 ; Units for these variables are: gC/m2/month
106 data_ob@units = "gC/m2/month"
108 ;-------------------------------------------------------------
111 ; column (not including header column)
113 col_head = (/"Observed Fire_Flux (PgC/yr)" \
114 ,"Model Fire_Flux (PgC/yr)" \
115 ,"Correlation Coefficient" \
116 ,"Ratio model/observed" \
121 ncol = dimsizes(col_head)
123 ; row (not including header row)
125 ; using model biome class:
126 row_head = (/"Not Vegetated" \
127 ,"Needleleaf Evergreen Temperate Tree" \
128 ,"Needleleaf Evergreen Boreal Tree" \
129 ; ,"Needleleaf Deciduous Boreal Tree" \
130 ,"Broadleaf Evergreen Tropical Tree" \
131 ,"Broadleaf Evergreen Temperate Tree" \
132 ,"Broadleaf Deciduous Tropical Tree" \
133 ,"Broadleaf Deciduous Temperate Tree" \
134 ; ,"Broadleaf Deciduous Boreal Tree" \
135 ; ,"Broadleaf Evergreen Shrub" \
136 ,"Broadleaf Deciduous Temperate Shrub" \
137 ,"Broadleaf Deciduous Boreal Shrub" \
139 ,"C3 Non-Arctic Grass" \
145 nrow = dimsizes(row_head)
147 ; arrays to be passed to table.
148 text = new ((/nrow, ncol/),string )
150 ;*****************************************************************
152 ;*****************************************************************
154 x = dim_avg_Wrap(data_mod(lat|:,lon|:,month|:,year|:))
155 data_mod_m = dim_avg_Wrap( x(lat|:,lon|:,month|:))
158 x = dim_avg_Wrap( data_ob(lat|:,lon|:,month|:,year|:))
159 data_ob_m = dim_avg_Wrap( x(lat|:,lon|:,month|:))
162 ;----------------------------------------------------
163 ; compute correlation coef: space
165 landmask_1d = ndtooned(landmask)
166 data_mod_1d = ndtooned(data_mod_m)
167 data_ob_1d = ndtooned(data_ob_m )
168 area_1d = ndtooned(area)
169 landfrac_1d = ndtooned(landfrac)
171 good = ind(landmask_1d .gt. 0.)
173 global_mod = sum(data_mod_1d(good)*area_1d(good)) * 1.e-15 * 12.
174 global_ob = sum(data_ob_1d(good) *area_1d(good)) * 1.e-15 * 12.
178 global_area= sum(area_1d)
179 global_land= sum(area_1d(good))
180 ; print (global_area)
181 ; print (global_land)
183 cc_space = esccr(data_mod_1d(good)*landfrac_1d(good),data_ob_1d(good)*landfrac_1d(good),0)
192 ;----------------------------------------------------
197 Mscore1 = cc_space * cc_space * score_max
199 M_global = sprintf("%.2f", Mscore1)
201 ;----------------------------------------------------
204 resg = True ; Use plot options
205 resg@cnFillOn = True ; Turn on color fill
206 resg@gsnSpreadColors = True ; use full colormap
207 resg@cnLinesOn = False ; Turn off contourn lines
208 resg@mpFillOn = False ; Turn off map fill
209 resg@cnLevelSelectionMode = "ManualLevels" ; Manual contour invtervals
211 ;----------------------------------------------------
212 ; global contour: model vs ob
214 plot_name = "global_model_vs_ob"
216 wks = gsn_open_wks (plot_type,plot_name)
217 gsn_define_colormap(wks,"gui_default")
219 plot=new(3,graphic) ; create graphic array
221 resg@gsnFrame = False ; Do not draw plot
222 resg@gsnDraw = False ; Do not advance frame
224 ;----------------------
225 ; plot correlation coef
228 gRes@txFontHeightF = 0.02
231 correlation_text = "(correlation coef = "+sprintf("%.2f", cc_space)+")"
233 gsn_text_ndc(wks,correlation_text,0.20,0.50,gRes)
235 ;-----------------------
237 ; change from gC/m2/month to gC/m2/yr
240 data_ob_m@units = "gC/m2/yr"
241 data_mod_m@units = "gC/m2/yr"
243 data_ob_m = data_ob_m * month_to_year
244 data_ob_m = where(landmask .gt. 0., data_ob_m, data_ob_m@_FillValue)
247 resg@tiMainString = title
249 resg@cnMinLevelValF = 10.
250 resg@cnMaxLevelValF = 100.
251 resg@cnLevelSpacingF = 10.
253 plot(0) = gsn_csm_contour_map_ce(wks,data_ob_m,resg)
255 ;-----------------------
258 data_mod_m = data_mod_m * month_to_year
260 data_mod_m = where(landmask .gt. 0., data_mod_m, data_mod_m@_FillValue)
262 title = "Model "+ model_name
263 resg@tiMainString = title
265 resg@cnMinLevelValF = 10.
266 resg@cnMaxLevelValF = 100.
267 resg@cnLevelSpacingF = 10.
269 plot(1) = gsn_csm_contour_map_ce(wks,data_mod_m,resg)
271 ;-----------------------
274 resg@cnMinLevelValF = -80.
275 resg@cnMaxLevelValF = 20.
276 resg@cnLevelSpacingF = 10.
279 zz = data_mod_m - data_ob_m
280 title = "Model_"+model_name+" - Observed"
281 resg@tiMainString = title
283 plot(2) = gsn_csm_contour_map_ce(wks,zz,resg)
287 pres = True ; panel plot mods desired
288 pres@gsnMaximize = True ; fill the page
290 gsn_panel(wks,plot,(/3,1/),pres) ; create panel plot
295 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
296 "rm "+plot_name+"."+plot_type)
302 resg@gsnFrame = True ; Do advance frame
303 resg@gsnDraw = True ; Do draw plot
305 ;*******************************************************************
306 ; (B) Time series : per biome
307 ;*******************************************************************
311 dsizes = dimsizes(data_mod)
314 ntime = nyear * nmonth
319 ;-------------------------------------------
320 ; Calculate "nice" bins for binning the data
322 ; using model biome class
325 range = fspan(0,nclass,nclass+1)
327 ; Use this range information to grab all the values in a
328 ; particular range, and then take an average.
330 nx = dimsizes(range) - 1
332 ;-------------------------------------------
335 ; using observed biome class
336 ; base = ndtooned(classob)
337 ; using model biome class
338 base = ndtooned(classmod)
342 area_bin = new((/nx/),float)
343 yvalues = new((/ntime,data_n,nx/),float)
345 ; Loop through each range, using base.
349 if (i.ne.(nx-1)) then
350 idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
352 idx = ind(base.ge.range(i))
354 ;---------------------
357 if (.not.any(ismissing(idx))) then
358 area_bin(i) = sum(area_1d(idx))
360 area_bin(i) = area_bin@_FillValue
363 ;#############################################################
364 ; using model biome class:
365 ; set the following 4 classes to _FillValue:
366 ; (3)Needleleaf Deciduous Boreal Tree,
367 ; (8)Broadleaf Deciduous Boreal Tree,
368 ; (9)Broadleaf Evergreen Shrub,
371 if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
372 area_bin(i) = area_bin@_FillValue
374 ;#############################################################
376 ;---------------------
377 ; for data_mod and data_ob
388 data = ndtooned(data_ob(m,k,:,:))
392 data = ndtooned(data_mod(m,k,:,:))
397 if (.not.any(ismissing(idx))) then
398 yvalues(t,n,i) = sum(data(idx)*area_1d(idx))
400 yvalues(t,n,i) = yvalues@_FillValue
403 ;#############################################################
404 ; using model biome class:
405 ; set the following 4 classes to _FillValue:
406 ; (3)Needleleaf Deciduous Boreal Tree,
407 ; (8)Broadleaf Deciduous Boreal Tree,
408 ; (9)Broadleaf Evergreen Shrub,
411 if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
412 yvalues(t,n,i) = yvalues@_FillValue
414 ;#############################################################
429 global_bin = sum(area_bin)
432 ;----------------------------------------------------------------
435 good = ind(.not.ismissing(area_bin))
437 area_g = area_bin(good)
439 n_biome = dimsizes(good)
441 global_good = sum(area_g)
442 ; print (global_good)
444 ;----------------------------------------------------------------
445 ; data for tseries plot
447 yvalues_g = new((/ntime,data_n,n_biome/),float)
449 yvalues_g@units = "TgC/month"
451 ; change unit to Tg C/month
452 ; change unit from g to Tg (Tera gram)
455 yvalues_g = yvalues(:,:,good) * factor_unit
459 ;-------------------------------------------------------------------
460 ; general settings for line plot
463 res@xyDashPatterns = (/0,0/) ; make lines solid
464 res@xyLineThicknesses = (/2.0,2.0/) ; make lines thicker
465 res@xyLineColors = (/"blue","red"/) ; line color
467 res@trXMinF = year_start
468 res@trXMaxF = year_end + 1
470 res@vpHeightF = 0.4 ; change aspect ratio of plot
474 res@tiMainFontHeightF = 0.025 ; size of title
476 res@tmXBFormat = "f" ; not to add trailing zeros
478 ; res@gsnMaximize = True
480 ;----------------------------------------------
481 ; Add a boxed legend using the simple method
483 res@pmLegendDisplayMode = "Always"
484 ; res@pmLegendWidthF = 0.1
485 res@pmLegendWidthF = 0.08
486 res@pmLegendHeightF = 0.06
487 res@pmLegendOrthogonalPosF = -1.17
488 ; res@pmLegendOrthogonalPosF = -1.00 ;(downward)
489 ; res@pmLegendOrthogonalPosF = -0.30 ;(downward)
491 ; res@pmLegendParallelPosF = 0.18
492 res@pmLegendParallelPosF = 0.23 ;(rightward)
493 res@pmLegendParallelPosF = 0.73 ;(rightward)
494 res@pmLegendParallelPosF = 0.83 ;(rightward)
496 ; res@lgPerimOn = False
497 res@lgLabelFontHeightF = 0.015
498 res@xyExplicitLegendLabels = (/"observed",model_name/)
500 ;*******************************************************************
501 ; (A) time series plot: monthly ( 2 lines per plot)
502 ;*******************************************************************
504 ; x-axis in time series plot
506 timeI = new((/ntime/),integer)
507 timeF = new((/ntime/),float)
508 timeI = ispan(1,ntime,1)
509 timeF = year_start + (timeI-1)/12.
510 timeF@long_name = "year"
512 plot_data = new((/2,ntime/),float)
513 plot_data@long_name = "TgC/month"
515 ;----------------------------------------------
516 ; time series plot : per biome
520 plot_name = "monthly_biome_"+ m
522 wks = gsn_open_wks (plot_type,plot_name)
524 title = "Fire : "+ row_head(m)
525 res@tiMainString = title
527 plot_data(0,:) = yvalues_g(:,0,m)
528 plot_data(1,:) = yvalues_g(:,1,m)
530 plot = gsn_csm_xy(wks,timeF,plot_data,res)
535 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
536 "rm "+plot_name+"."+plot_type)
539 ;------------------------------------------
540 ; data for table : per biome
542 ; unit change from TgC/month to PgC/month
547 tmp_ob = new((/ntime/),float)
548 tmp_mod = new((/ntime/),float)
550 total_ob = new((/n_biome/),float)
551 total_mod = new((/n_biome/),float)
552 Mscore2 = new((/n_biome/),float)
556 tmp_ob = yvalues_g(:,0,m)
557 tmp_mod = yvalues_g(:,1,m)
559 total_ob(m) = avg(month_to_annual(tmp_ob, 0)) * unit_factor
560 total_mod(m) = avg(month_to_annual(tmp_mod,0)) * unit_factor
562 cc_time = esccr(tmp_mod,tmp_ob,0)
564 ratio = total_mod(m)/total_ob(m)
566 good = ind(tmp_ob .ne. 0. .and. tmp_mod .ne. 0.)
568 bias = sum( abs( tmp_mod(good)-tmp_ob(good) )/( abs(tmp_mod(good))+abs(tmp_ob(good)) ) )
569 Mscore2(m) = (1.- (bias/dimsizes(good)))*score_max
573 text(m,0) = sprintf("%.2f",total_ob(m))
574 text(m,1) = sprintf("%.2f",total_mod(m))
575 text(m,2) = sprintf("%.2f",cc_time)
576 text(m,3) = sprintf("%.2f",ratio)
577 text(m,4) = sprintf("%.2f",Mscore2(m))
578 text(m,5) = "<a href=./monthly_biome_"+m+".png>model_vs_ob</a>"
584 ;--------------------------------------------
585 ; time series plot: all biome
587 plot_name = "monthly_global"
589 wks = gsn_open_wks (plot_type,plot_name)
591 title = "Fire : "+ row_head(n_biome)
592 res@tiMainString = title
595 plot_data(0,k) = sum(yvalues_g(k,0,:))
596 plot_data(1,k) = sum(yvalues_g(k,1,:))
599 plot = gsn_csm_xy(wks,timeF,plot_data,res)
604 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
605 "rm "+plot_name+"."+plot_type)
607 ;------------------------------------------
608 ; data for table : global
612 tmp_ob = ndtooned(yvalues_g(:,0,:))
613 tmp_mod = ndtooned(yvalues_g(:,1,:))
615 cc_time = esccr(tmp_mod,tmp_ob,0)
617 ratio = sum(total_mod)/sum(total_ob)
619 good = ind(tmp_ob .ne. 0. .and. tmp_mod .ne. 0.)
621 bias = sum( abs( tmp_mod(good)-tmp_ob(good) )/( abs(tmp_mod(good))+abs(tmp_ob(good)) ) )
622 Mscore3 = (1.- (bias/dimsizes(good)))*score_max
628 text(nrow-1,0) = sprintf("%.2f",sum(total_ob))
629 text(nrow-1,1) = sprintf("%.2f",sum(total_mod))
630 text(nrow-1,2) = sprintf("%.2f",cc_time)
631 text(nrow-1,3) = sprintf("%.2f",ratio)
632 ; text(nrow-1,4) = sprintf("%.2f",avg(Mscore2))
633 text(nrow-1,4) = sprintf("%.2f", Mscore3)
634 text(nrow-1,5) = "<a href=./monthly_global.png>model_vs_ob</a>"
636 ;**************************************************
638 ;**************************************************
640 header_text = "<H1>Fire Emissions from GFEDv2 (1997-2004) vs "+model_name+"</H1>"
642 header = (/"<HTML>" \
644 ,"<TITLE>CLAMP metrics</TITLE>" \
651 "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
653 ," <th bgcolor=DDDDDD >Biome Type</th>" \
654 ," <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
655 ," <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
656 ," <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
657 ," <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
658 ," <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
659 ," <th bgcolor=DDDDDD >"+col_head(5)+"</th>" \
662 table_footer = "</table>"
666 lines = new(50000,string)
669 set_line(lines,nline,header)
670 set_line(lines,nline,table_header)
671 ;-----------------------------------------------
675 set_line(lines,nline,row_header)
685 set_line(lines,nline,"<th>"+txt0+"</th>")
686 set_line(lines,nline,"<th>"+txt1+"</th>")
687 set_line(lines,nline,"<th>"+txt2+"</th>")
688 set_line(lines,nline,"<th>"+txt3+"</th>")
689 set_line(lines,nline,"<th>"+txt4+"</th>")
690 set_line(lines,nline,"<th>"+txt5+"</th>")
691 set_line(lines,nline,"<th>"+txt6+"</th>")
693 set_line(lines,nline,row_footer)
695 ;-----------------------------------------------
696 set_line(lines,nline,table_footer)
697 set_line(lines,nline,footer)
699 ; Now write to an HTML file.
701 output_html = "table_fire.html"
703 idx = ind(.not.ismissing(lines))
704 if(.not.any(ismissing(idx))) then
705 asciiwrite(output_html,lines(idx))
712 ;**************************************************************************************
714 ;**************************************************************************************
716 M_all = Mscore1 + Mscore3
717 M_fire = sprintf("%.2f", M_all)
719 if (isvar("compare")) then
720 system("sed -e '1,/M_fire/s/M_fire/"+M_fire+"/' "+html_name2+" > "+html_new2+";"+ \
721 "mv -f "+html_new2+" "+html_name2)
724 system("sed s#M_fire#"+M_fire+"# "+html_name+" > "+html_new+";"+ \
725 "mv -f "+html_new+" "+html_name)
727 ;***************************************************************************
728 ; get total score and write to file
729 ;***************************************************************************
731 asciiwrite("M_save.fire", M_fire)
735 ;***************************************************************************
736 ; output plot and html
737 ;***************************************************************************
738 output_dir = model_name+"/fire"
740 system("mv *.png *.html " + output_dir)
741 ;***************************************************************************