Modifications to scoring and graphics production for the final version of code for the C-LAMP paper in GCB.
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 ; FMH: This was Jeff's, but I got an error
78 data_mod = fm->COL_FIRE_CLOSS(18:25,:,:,:)
79 ; FMH: So I'll try this
80 ;data_mod = fm->COL_FIRE_CLOSS(17:24,:,:,:)
84 ; Units for these variables are:
87 ; change unit to gC/m2/month
89 nsec_per_month = 60*60*24*30
91 data_mod = data_mod * nsec_per_month
93 data_mod@units = "gC/m2/month"
95 ;----------------------------------------------------
96 ; read data: time series, observed
98 dir_f = diro + "fire/"
99 fil_f = "Fire_C_1997-2006_monthly_"+ model_grid+".nc"
100 fm = addfile (dir_f+fil_f,"r")
101 data_ob = fm->FIRE_C(0:7,:,:,:)
107 ; Units for these variables are: gC/m2/month
109 data_ob@units = "gC/m2/month"
111 ;-------------------------------------------------------------
114 ; column (not including header column)
116 col_head = (/"Observed Fire_Flux (PgC/yr)" \
117 ,"Model Fire_Flux (PgC/yr)" \
118 ,"Correlation Coefficient" \
119 ,"Ratio model/observed" \
124 ncol = dimsizes(col_head)
126 ; row (not including header row)
128 ; using model biome class:
129 row_head = (/"Not Vegetated" \
130 ,"Needleleaf Evergreen Temperate Tree" \
131 ,"Needleleaf Evergreen Boreal Tree" \
132 ; ,"Needleleaf Deciduous Boreal Tree" \
133 ,"Broadleaf Evergreen Tropical Tree" \
134 ,"Broadleaf Evergreen Temperate Tree" \
135 ,"Broadleaf Deciduous Tropical Tree" \
136 ,"Broadleaf Deciduous Temperate Tree" \
137 ; ,"Broadleaf Deciduous Boreal Tree" \
138 ; ,"Broadleaf Evergreen Shrub" \
139 ,"Broadleaf Deciduous Temperate Shrub" \
140 ,"Broadleaf Deciduous Boreal Shrub" \
142 ,"C3 Non-Arctic Grass" \
148 nrow = dimsizes(row_head)
150 ; arrays to be passed to table.
151 text = new ((/nrow, ncol/),string )
153 ;*****************************************************************
155 ;*****************************************************************
157 x = dim_avg_Wrap(data_mod(lat|:,lon|:,month|:,year|:))
158 data_mod_m = dim_avg_Wrap( x(lat|:,lon|:,month|:))
161 x = dim_avg_Wrap( data_ob(lat|:,lon|:,month|:,year|:))
162 data_ob_m = dim_avg_Wrap( x(lat|:,lon|:,month|:))
165 ;----------------------------------------------------
166 ; compute correlation coef: space
168 landmask_1d = ndtooned(landmask)
169 data_mod_1d = ndtooned(data_mod_m)
170 data_ob_1d = ndtooned(data_ob_m )
171 area_1d = ndtooned(area)
172 landfrac_1d = ndtooned(landfrac)
174 good = ind(landmask_1d .gt. 0.)
176 global_mod = sum(data_mod_1d(good)*area_1d(good)) * 1.e-15 * 12.
177 global_ob = sum(data_ob_1d(good) *area_1d(good)) * 1.e-15 * 12.
181 global_area= sum(area_1d)
182 global_land= sum(area_1d(good))
183 ; print (global_area)
184 ; print (global_land)
186 cc_space = esccr(data_mod_1d(good)*landfrac_1d(good),data_ob_1d(good)*landfrac_1d(good),0)
195 ;----------------------------------------------------
200 Mscore1 = cc_space * cc_space * score_max
202 M_global = sprintf("%.2f", Mscore1)
204 ;----------------------------------------------------
207 resg = True ; Use plot options
208 resg@cnFillOn = True ; Turn on color fill
209 resg@gsnSpreadColors = True ; use full colormap
210 resg@cnLinesOn = False ; Turn off contourn lines
211 resg@mpFillOn = False ; Turn off map fill
212 resg@cnLevelSelectionMode = "ManualLevels" ; Manual contour invtervals
214 ;----------------------------------------------------
215 ; global contour: model vs ob
217 plot_name = "global_model_vs_ob"
219 wks = gsn_open_wks (plot_type,plot_name)
220 gsn_define_colormap(wks,"gui_default")
222 plot=new(3,graphic) ; create graphic array
224 resg@gsnFrame = False ; Do not draw plot
225 resg@gsnDraw = False ; Do not advance frame
227 ;----------------------
228 ; plot correlation coef
231 gRes@txFontHeightF = 0.02
234 correlation_text = "(correlation coef = "+sprintf("%.2f", cc_space)+")"
236 gsn_text_ndc(wks,correlation_text,0.20,0.50,gRes)
238 ;-----------------------
240 ; change from gC/m2/month to gC/m2/yr
243 data_ob_m@units = "gC/m2/yr"
244 data_mod_m@units = "gC/m2/yr"
246 data_ob_m = data_ob_m * month_to_year
247 data_ob_m = where(landmask .gt. 0., data_ob_m, data_ob_m@_FillValue)
250 resg@tiMainString = title
252 resg@cnMinLevelValF = 10.
253 resg@cnMaxLevelValF = 100.
254 resg@cnLevelSpacingF = 10.
256 plot(0) = gsn_csm_contour_map_ce(wks,data_ob_m,resg)
258 ;-----------------------
261 data_mod_m = data_mod_m * month_to_year
263 data_mod_m = where(landmask .gt. 0., data_mod_m, data_mod_m@_FillValue)
265 title = "Model "+ model_name
266 resg@tiMainString = title
268 resg@cnMinLevelValF = 10.
269 resg@cnMaxLevelValF = 100.
270 resg@cnLevelSpacingF = 10.
272 plot(1) = gsn_csm_contour_map_ce(wks,data_mod_m,resg)
274 ;-----------------------
277 resg@cnMinLevelValF = -80.
278 resg@cnMaxLevelValF = 20.
279 resg@cnLevelSpacingF = 10.
282 zz = data_mod_m - data_ob_m
283 title = "Model_"+model_name+" - Observed"
284 resg@tiMainString = title
286 plot(2) = gsn_csm_contour_map_ce(wks,zz,resg)
290 pres = True ; panel plot mods desired
291 pres@gsnMaximize = True ; fill the page
293 gsn_panel(wks,plot,(/3,1/),pres) ; create panel plot
298 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
299 "rm "+plot_name+"."+plot_type)
305 resg@gsnFrame = True ; Do advance frame
306 resg@gsnDraw = True ; Do draw plot
308 ;*******************************************************************
309 ; (B) Time series : per biome
310 ;*******************************************************************
314 dsizes = dimsizes(data_mod)
317 ntime = nyear * nmonth
322 ;-------------------------------------------
323 ; Calculate "nice" bins for binning the data
325 ; using model biome class
328 range = fspan(0,nclass,nclass+1)
330 ; Use this range information to grab all the values in a
331 ; particular range, and then take an average.
333 nx = dimsizes(range) - 1
335 ;-------------------------------------------
338 ; using observed biome class
339 ; base = ndtooned(classob)
340 ; using model biome class
341 base = ndtooned(classmod)
345 area_bin = new((/nx/),float)
346 yvalues = new((/ntime,data_n,nx/),float)
348 ; Loop through each range, using base.
352 if (i.ne.(nx-1)) then
353 idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
355 idx = ind(base.ge.range(i))
357 ;---------------------
360 if (.not.any(ismissing(idx))) then
361 area_bin(i) = sum(area_1d(idx))
363 area_bin(i) = area_bin@_FillValue
366 ;#############################################################
367 ; using model biome class:
368 ; set the following 4 classes to _FillValue:
369 ; (3)Needleleaf Deciduous Boreal Tree,
370 ; (8)Broadleaf Deciduous Boreal Tree,
371 ; (9)Broadleaf Evergreen Shrub,
374 if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
375 area_bin(i) = area_bin@_FillValue
377 ;#############################################################
379 ;---------------------
380 ; for data_mod and data_ob
391 data = ndtooned(data_ob(m,k,:,:))
395 data = ndtooned(data_mod(m,k,:,:))
400 if (.not.any(ismissing(idx))) then
401 yvalues(t,n,i) = sum(data(idx)*area_1d(idx))
403 yvalues(t,n,i) = yvalues@_FillValue
406 ;#############################################################
407 ; using model biome class:
408 ; set the following 4 classes to _FillValue:
409 ; (3)Needleleaf Deciduous Boreal Tree,
410 ; (8)Broadleaf Deciduous Boreal Tree,
411 ; (9)Broadleaf Evergreen Shrub,
414 if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
415 yvalues(t,n,i) = yvalues@_FillValue
417 ;#############################################################
432 global_bin = sum(area_bin)
435 ;----------------------------------------------------------------
438 good = ind(.not.ismissing(area_bin))
440 area_g = area_bin(good)
442 n_biome = dimsizes(good)
444 global_good = sum(area_g)
445 ; print (global_good)
447 ;----------------------------------------------------------------
448 ; data for tseries plot
450 yvalues_g = new((/ntime,data_n,n_biome/),float)
452 yvalues_g@units = "TgC/month"
454 ; change unit to Tg C/month
455 ; change unit from g to Tg (Tera gram)
458 yvalues_g = yvalues(:,:,good) * factor_unit
462 ;-------------------------------------------------------------------
463 ; general settings for line plot
466 res@xyDashPatterns = (/0,0/) ; make lines solid
467 res@xyLineThicknesses = (/2.0,2.0/) ; make lines thicker
468 res@xyLineColors = (/"blue","red"/) ; line color
470 res@trXMinF = year_start
471 res@trXMaxF = year_end + 1
473 res@vpHeightF = 0.4 ; change aspect ratio of plot
477 res@tiMainFontHeightF = 0.025 ; size of title
479 res@tmXBFormat = "f" ; not to add trailing zeros
481 ; res@gsnMaximize = True
483 ;----------------------------------------------
484 ; Add a boxed legend using the simple method
486 res@pmLegendDisplayMode = "Always"
487 ; res@pmLegendWidthF = 0.1
488 res@pmLegendWidthF = 0.08
489 res@pmLegendHeightF = 0.06
490 res@pmLegendOrthogonalPosF = -1.17
491 ; res@pmLegendOrthogonalPosF = -1.00 ;(downward)
492 ; res@pmLegendOrthogonalPosF = -0.30 ;(downward)
494 ; res@pmLegendParallelPosF = 0.18
495 res@pmLegendParallelPosF = 0.23 ;(rightward)
496 res@pmLegendParallelPosF = 0.73 ;(rightward)
497 res@pmLegendParallelPosF = 0.83 ;(rightward)
499 ; res@lgPerimOn = False
500 res@lgLabelFontHeightF = 0.015
501 res@xyExplicitLegendLabels = (/"observed",model_name/)
503 ;*******************************************************************
504 ; (A) time series plot: monthly ( 2 lines per plot)
505 ;*******************************************************************
507 ; x-axis in time series plot
509 timeI = new((/ntime/),integer)
510 timeF = new((/ntime/),float)
511 timeI = ispan(1,ntime,1)
512 timeF = year_start + (timeI-1)/12.
513 timeF@long_name = "year"
515 plot_data = new((/2,ntime/),float)
516 plot_data@long_name = "TgC/month"
518 ;----------------------------------------------
519 ; time series plot : per biome
523 plot_name = "monthly_biome_"+ m
525 wks = gsn_open_wks (plot_type,plot_name)
527 title = "Fire : "+ row_head(m)
528 res@tiMainString = title
530 plot_data(0,:) = yvalues_g(:,0,m)
531 plot_data(1,:) = yvalues_g(:,1,m)
533 plot = gsn_csm_xy(wks,timeF,plot_data,res)
538 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
539 "rm "+plot_name+"."+plot_type)
542 ;------------------------------------------
543 ; data for table : per biome
545 ; unit change from TgC/month to PgC/month
550 tmp_ob = new((/ntime/),float)
551 tmp_mod = new((/ntime/),float)
553 total_ob = new((/n_biome/),float)
554 total_mod = new((/n_biome/),float)
555 Mscore2 = new((/n_biome/),float)
559 tmp_ob = yvalues_g(:,0,m)
560 tmp_mod = yvalues_g(:,1,m)
562 total_ob(m) = avg(month_to_annual(tmp_ob, 0)) * unit_factor
563 total_mod(m) = avg(month_to_annual(tmp_mod,0)) * unit_factor
565 cc_time = esccr(tmp_mod,tmp_ob,0)
567 ratio = total_mod(m)/total_ob(m)
569 good = ind(tmp_ob .ne. 0. .and. tmp_mod .ne. 0.)
571 bias = sum( abs( tmp_mod(good)-tmp_ob(good) )/( abs(tmp_mod(good))+abs(tmp_ob(good)) ) )
572 Mscore2(m) = (1.- (bias/dimsizes(good)))*score_max
576 text(m,0) = sprintf("%.2f",total_ob(m))
577 text(m,1) = sprintf("%.2f",total_mod(m))
578 text(m,2) = sprintf("%.2f",cc_time)
579 text(m,3) = sprintf("%.2f",ratio)
580 text(m,4) = sprintf("%.2f",Mscore2(m))
581 text(m,5) = "<a href=./monthly_biome_"+m+".png>model_vs_ob</a>"
587 ;--------------------------------------------
588 ; time series plot: all biome
590 plot_name = "monthly_global"
592 wks = gsn_open_wks (plot_type,plot_name)
594 title = "Fire : "+ row_head(n_biome)
595 res@tiMainString = title
598 plot_data(0,k) = sum(yvalues_g(k,0,:))
599 plot_data(1,k) = sum(yvalues_g(k,1,:))
602 plot = gsn_csm_xy(wks,timeF,plot_data,res)
607 system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
608 "rm "+plot_name+"."+plot_type)
610 ;------------------------------------------
611 ; data for table : global
615 tmp_ob = ndtooned(yvalues_g(:,0,:))
616 tmp_mod = ndtooned(yvalues_g(:,1,:))
618 cc_time = esccr(tmp_mod,tmp_ob,0)
620 ratio = sum(total_mod)/sum(total_ob)
622 good = ind(tmp_ob .ne. 0. .and. tmp_mod .ne. 0.)
624 bias = sum( abs( tmp_mod(good)-tmp_ob(good) )/( abs(tmp_mod(good))+abs(tmp_ob(good)) ) )
625 Mscore3 = (1.- (bias/dimsizes(good)))*score_max
631 text(nrow-1,0) = sprintf("%.2f",sum(total_ob))
632 text(nrow-1,1) = sprintf("%.2f",sum(total_mod))
633 text(nrow-1,2) = sprintf("%.2f",cc_time)
634 text(nrow-1,3) = sprintf("%.2f",ratio)
635 ; text(nrow-1,4) = sprintf("%.2f",avg(Mscore2))
636 text(nrow-1,4) = sprintf("%.2f", Mscore3)
637 text(nrow-1,5) = "<a href=./monthly_global.png>model_vs_ob</a>"
639 ;**************************************************
641 ;**************************************************
643 header_text = "<H1>Fire Emissions from GFEDv2 (1997-2004) vs "+model_name+"</H1>"
645 header = (/"<HTML>" \
647 ,"<TITLE>CLAMP metrics</TITLE>" \
654 "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
656 ," <th bgcolor=DDDDDD >Biome Type</th>" \
657 ," <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
658 ," <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
659 ," <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
660 ," <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
661 ," <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
662 ," <th bgcolor=DDDDDD >"+col_head(5)+"</th>" \
665 table_footer = "</table>"
669 lines = new(50000,string)
672 set_line(lines,nline,header)
673 set_line(lines,nline,table_header)
674 ;-----------------------------------------------
678 set_line(lines,nline,row_header)
688 set_line(lines,nline,"<th>"+txt0+"</th>")
689 set_line(lines,nline,"<th>"+txt1+"</th>")
690 set_line(lines,nline,"<th>"+txt2+"</th>")
691 set_line(lines,nline,"<th>"+txt3+"</th>")
692 set_line(lines,nline,"<th>"+txt4+"</th>")
693 set_line(lines,nline,"<th>"+txt5+"</th>")
694 set_line(lines,nline,"<th>"+txt6+"</th>")
696 set_line(lines,nline,row_footer)
698 ;-----------------------------------------------
699 set_line(lines,nline,table_footer)
700 set_line(lines,nline,footer)
702 ; Now write to an HTML file.
704 output_html = "table_fire.html"
706 idx = ind(.not.ismissing(lines))
707 if(.not.any(ismissing(idx))) then
708 asciiwrite(output_html,lines(idx))
715 ;**************************************************************************************
717 ;**************************************************************************************
719 M_all = Mscore1 + Mscore3
720 M_fire = sprintf("%.2f", M_all)
722 if (isvar("compare")) then
723 system("sed -e '1,/M_fire/s/M_fire/"+M_fire+"/' "+html_name2+" > "+html_new2+";"+ \
724 "mv -f "+html_new2+" "+html_name2)
727 system("sed s#M_fire#"+M_fire+"# "+html_name+" > "+html_new+";"+ \
728 "mv -f "+html_new+" "+html_name)
730 ;***************************************************************************
731 ; get total score and write to file
732 ;***************************************************************************
734 asciiwrite("M_save.fire", M_fire)
738 ;***************************************************************************
739 ; output plot and html
740 ;***************************************************************************
741 output_dir = model_name+"/fire"
743 system("mv *.png *.html " + output_dir)
744 ;***************************************************************************