energy/99.all.ncl
author Forrest Hoffman <forrest@climatemodeling.org>
Thu, 26 Mar 2009 14:02:21 -0400
changeset 1 4be95183fbcd
permissions -rw-r--r--
Modifications to scoring and graphics production for the final version of code for the C-LAMP paper in GCB.
     1 ;************************************************************
     2 ; required command line input parameters:
     3 ;  ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
     4 ;
     5 ; using gsn_table for all
     6 ; output: line plot for each site (4 fields)
     7 ;         table for M_score
     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 begin
    29 
    30   plot_type     = "ps"
    31   plot_type_new = "png"
    32 
    33 ;************************************************
    34 ; read model data
    35 ;************************************************
    36 
    37 ; from command line inputs
    38 
    39 ;--------------------------------------------------
    40 ; edit table.html of current model for movel1_vs_model2
    41 
    42  if (isvar("compare")) then
    43     html_name2 = compare+"/table.html"  
    44     html_new2  = html_name2 +".new"
    45 
    46 ; the following only needs to execute once
    47 ;   system("sed 1,/model_nameA/s//"+model_name+"/ "+html_name2+" > "+html_new2+";"+ \
    48 ;          "mv -f "+html_new2+" "+html_name2+";"+ \
    49 ;          "sed 1,/model_nameB/s//"+model_name+"/ "+html_name2+" > "+html_new2+";"+ \
    50 ;          "mv -f "+html_new2+" "+html_name2+";"+ \
    51 ;          "sed s#"+modeln+"#"+model_name+"# "+html_name2+" > "+html_new2+";"+ \
    52 ;          "mv -f "+html_new2+" "+html_name2)
    53  end if
    54 
    55 ;-------------------------------------
    56 ; edit table.html for current model
    57 
    58  html_name = model_name+"/table.html"  
    59  html_new  = html_name +".new"
    60 
    61 ; the following only needs to execute once
    62 ; system("sed s#model_name#"+model_name+"# "+html_name+" > "+html_new+";"+ \
    63 ;        "mv -f "+html_new+" "+html_name)
    64 ;------------------------------------------------
    65   fm    = addfile(dirm+film2,"r")
    66   
    67   xm    = fm->lon
    68   ym    = fm->lat
    69 ;------------------------------------------------
    70   nlat = dimsizes(ym)
    71   nlon = dimsizes(xm)
    72 
    73 ; for 4 fields, 12-monthly
    74   nmon      = 12
    75   nfield    = 4
    76 
    77   data_mod0 = new ((/nfield,nmon,nlat,nlon/),float)
    78 
    79 ; change to unit of observed (u mol/m2/s)
    80 ; Model_units [=] gC/m2/s
    81 ; 12. = molecular weight of C
    82 ; u mol = 1e-6 mol
    83   factor = 1e6 /12.
    84 
    85 if (ENERGY .eq. "old") then
    86 
    87   data = fm->NEE
    88   data_mod0(0,:,:,:) = data(:,:,:) * factor
    89   delete (data)
    90 
    91 ; data  = fm->LATENT
    92   data1 = fm->FCEV
    93   data2 = fm->FCTR
    94   data3 = fm->FGEV
    95   data_mod0(2,:,:,:) = data1(:,:,:)+data2(:,:,:)+data3(:,:,:) 
    96   delete (data1)
    97   delete (data2)
    98   delete (data3)
    99 
   100 ; data = fm->SENSIBLE
   101   data  = fm->FSH
   102   data_mod0(3,:,:,:) = data(:,:,:) 
   103   delete (data)
   104 
   105 ; data  = fm->NETRAD
   106   data1 = fm->FSA
   107   data2 = fm->FIRA
   108   data_mod0(1,:,:,:) = data1(:,:,:)-data2(:,:,:)-data_mod0(2,:,:,:)-data_mod0(3,:,:,:) 
   109   delete (data1)
   110   delete (data2)
   111 
   112 else
   113 
   114   data = fm->NEE
   115   data_mod0(0,:,:,:) = data(:,:,:) * factor
   116   delete (data)
   117 
   118   data = fm->NETRAD
   119   data_mod0(1,:,:,:) = data(:,:,:) 
   120   delete (data)
   121 
   122   data = fm->LATENT
   123   data_mod0(2,:,:,:) = data(:,:,:) 
   124   delete (data)
   125 
   126   data = fm->SENSIBLE
   127   data_mod0(3,:,:,:) = data(:,:,:) 
   128   delete (data)
   129 end if
   130 
   131 ;************************************************
   132 ; read data: observed
   133 ;************************************************
   134 
   135  station = (/"BOREAS_NSA_OBS" \
   136             ,"CastelPorziano" \
   137             ,"Hyytiala" \
   138             ,"Kaamanen" \
   139             ,"LBA_Tapajos_KM67" \
   140             ,"Lethbridge" \
   141             ,"Tharandt" \
   142             ,"Vielsalm" \
   143             /)
   144 
   145  year_ob = (/"1994-2004" \
   146             ,"1997-2003" \
   147             ,"1996-2003" \
   148             ,"2000-2003" \
   149             ,"2002-2005" \
   150             ,"1999-2004" \
   151             ,"1996-2003" \
   152             ,"1998-2003" \
   153             /)
   154 
   155  field   = (/"CO2 Flux" \
   156             ,"Net Radiation" \
   157             ,"Latent Heat" \
   158             ,"Sensible Heat" \
   159             /)
   160 
   161  nstation  = dimsizes(station)
   162  nmon      = 12
   163  nfield    = dimsizes(field)
   164 
   165  data_ob   = new ((/nstation, nfield, nmon/),float)
   166  lat_ob    = new ((/nstation/),float)
   167  lon_ob    = new ((/nstation/),float)
   168 
   169  diri_root  = "/fis/cgd/cseg/people/jeff/clamp_data/fluxnet/"
   170 
   171  do n = 0,nstation-1
   172     diri = diri_root + station(n)+"/"
   173     fili = station(n)+"_"+year_ob(n)+"_monthly.nc"
   174     g     = addfile (diri+fili,"r")
   175  
   176     lon_ob(n) = g->lon 
   177     lat_ob(n) = g->lat
   178 
   179     data      = g->CO2_FLUX
   180     data_ob(n,0,:) = dim_avg(data(month|:,year|:))
   181     delete (data)
   182 
   183     data      = g->RAD_FLUX
   184     data_ob(n,1,:) = dim_avg(data(month|:,year|:))
   185     delete (data)
   186 
   187     data      = g->LH_FLUX
   188     data_ob(n,2,:) = dim_avg(data(month|:,year|:))
   189     delete (data)
   190 
   191     data      = g->SH_FLUX
   192     data_ob(n,3,:) = dim_avg(data(month|:,year|:))
   193     delete (data)
   194 
   195     delete (g)
   196  end do
   197 
   198 ;************************************************************
   199 ; interpolate model data into observed station
   200 ; note: model is 0-360E, 90S-90N
   201 ;************************************************************
   202 
   203 ; to be able to handle observation at (-89.98,-24.80)
   204   ym(0) = -90.  
   205 
   206   yy = linint2_points_Wrap(xm,ym,data_mod0,True,lon_ob,lat_ob,0)
   207 
   208   delete (data_mod0)
   209   yy!0 = "field"
   210   data_mod = yy(pts|:,field|:,time|:)
   211 
   212 ;************************************************************
   213 ; compute correlation coef and M score
   214 ;************************************************************
   215 
   216  score_max = 5.
   217 
   218  ccr     = new ((/nstation, nfield/),float)
   219  M_score = new ((/nstation, nfield/),float) 
   220 
   221  do n=0,nstation-1
   222  do m=0,nfield-1   
   223     ccr(n,m) = esccr(data_ob(n,m,:),data_mod(n,m,:),0)
   224     bias = sum(abs(data_mod(n,m,:)-data_ob(n,m,:))/(abs(data_mod(n,m,:))+abs(data_ob(n,m,:))))
   225     M_score(n,m) = (1. -(bias/nmon)) * score_max
   226  end do
   227  end do
   228 
   229  M_co2 = avg(M_score(:,0))
   230  M_rad = avg(M_score(:,1))
   231  M_lh  = avg(M_score(:,2))
   232  M_sh  = avg(M_score(:,3))
   233  M_all = M_co2+ M_rad +M_lh + M_sh
   234 
   235  M_energy_co2 = sprintf("%.2f", M_co2)
   236  M_energy_rad = sprintf("%.2f", M_rad)
   237  M_energy_lh  = sprintf("%.2f", M_lh )
   238  M_energy_sh  = sprintf("%.2f", M_sh )
   239  M_energy_all = sprintf("%.2f", M_all)
   240 
   241 ;*******************************************************************
   242 ; for station line plot
   243 ;*******************************************************************
   244 
   245 ; for x-axis in xyplot
   246   mon = ispan(1,12,1)
   247   mon@long_name = "month"
   248 
   249   res                   = True               ; plot mods desired
   250   res@xyLineThicknesses = (/2.0,2.0/)        ; make 2nd lines thicker
   251   res@xyLineColors      = (/"blue","red"/)   ; line color (ob,model)
   252 ;-------------------------------------------------------------------------
   253 ; Add a boxed legend using the more simple method
   254 
   255   res@pmLegendDisplayMode    = "Always"
   256 ; res@pmLegendWidthF         = 0.1
   257   res@pmLegendWidthF         = 0.08
   258   res@pmLegendHeightF        = 0.06
   259 ; res@pmLegendOrthogonalPosF = -1.17
   260 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
   261   res@pmLegendOrthogonalPosF = -0.30  ;(downward)
   262 
   263 ; res@pmLegendParallelPosF   =  0.18
   264   res@pmLegendParallelPosF   =  0.23  ;(rightward)
   265 
   266 ; res@lgPerimOn             = False
   267   res@lgLabelFontHeightF     = 0.015
   268   res@xyExplicitLegendLabels = (/"observed",model_name/)
   269 ;-------------------------------------------------------------------
   270 ; for panel plot
   271   res@gsnFrame     = False                   ; Do not draw plot 
   272   res@gsnDraw      = False                   ; Do not advance frame
   273 
   274   pres                            = True     ; panel plot mods desired
   275   pres@gsnPanelYWhiteSpacePercent = 5        ; increase white space around
   276                                              ; indiv. plots in panel
   277   pres@gsnMaximize                = True     ; fill the page
   278 ;-------------------------------------------------------------------
   279 
   280   plot_data   = new((/2,12/),float)
   281   plot_data!0 = "case"
   282   plot_data!1 = "month"
   283 
   284   do n = 0,nstation-1
   285 ;----------------------------
   286 ; for observed
   287 
   288     plot_name = station(n)+"_ob"    
   289     title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"
   290     res@tiMainString = title
   291 
   292     wks = gsn_open_wks (plot_type,plot_name)
   293     plot=new(4,graphic)                        ; create graphic array   
   294                            
   295     plot_data(0,:) = (/data_ob (n,0,:)/)
   296     plot_data@long_name = field(0)   
   297     plot(0)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 1
   298 
   299     plot_data(0,:) = (/data_ob (n,1,:)/)
   300     plot_data@long_name = field(1)
   301     plot(1)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 2
   302 
   303     plot_data(0,:) = (/data_ob (n,2,:)/)
   304     plot_data@long_name = field(2)   
   305     plot(2)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 3
   306 
   307     plot_data(0,:) = (/data_ob (n,3,:)/)
   308     plot_data@long_name = field(3)
   309     plot(3)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 4
   310 
   311     gsn_panel(wks,plot,(/2,2/),pres)                 ; create panel plot
   312 
   313     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   314            "rm "+plot_name+"."+plot_type)
   315 
   316     clear (wks)  
   317     delete (plot)
   318 ;----------------------------
   319 ; for model_vs_ob
   320 
   321     plot_name = station(n)+"_model_vs_ob"
   322     title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"    
   323     res@tiMainString = title
   324 
   325     wks = gsn_open_wks (plot_type,plot_name)
   326     plot=new(4,graphic)                        ; create graphic array   
   327                            
   328     plot_data(0,:) = (/data_ob (n,0,:)/)
   329     plot_data(1,:) = (/data_mod(n,0,:)/)
   330     plot_data@long_name = field(0)   
   331     plot(0)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 1
   332 
   333     plot_data(0,:) = (/data_ob (n,1,:)/)
   334     plot_data(1,:) = (/data_mod(n,1,:)/)
   335     plot_data@long_name = field(1)
   336     plot(1)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 2
   337 
   338     plot_data(0,:) = (/data_ob (n,2,:)/)
   339     plot_data(1,:) = (/data_mod(n,2,:)/)
   340     plot_data@long_name = field(2)   
   341     plot(2)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 3
   342 
   343     plot_data(0,:) = (/data_ob (n,3,:)/)
   344     plot_data(1,:) = (/data_mod(n,3,:)/)
   345     plot_data@long_name = field(3)
   346     plot(3)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 4
   347 
   348     gsn_panel(wks,plot,(/2,2/),pres)                 ; create panel plot
   349 
   350     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   351            "rm "+plot_name+"."+plot_type)
   352 
   353     clear (wks)  
   354     delete (plot)
   355  end do
   356 
   357 ;*******************************************************************
   358 ; html table of site: observed
   359 ;*******************************************************************
   360   output_html = "line_ob.html"
   361 
   362   header = (/"<HTML>" \
   363             ,"<HEAD>" \
   364             ,"<TITLE>CLAMP metrics</TITLE>" \
   365             ,"</HEAD>" \
   366             ,"<H1>Energy at Site: Observation</H1>" \
   367             /) 
   368   footer = "</HTML>"
   369 
   370   table_header = (/ \
   371         "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
   372        ,"<tr>" \
   373        ,"   <th bgcolor=DDDDDD >Site Name</th>" \
   374        ,"   <th bgcolor=DDDDDD >Latitude</th>" \
   375        ,"   <th bgcolor=DDDDDD >Longitude</th>" \
   376        ,"   <th bgcolor=DDDDDD >Observed</th>" \ 
   377        ,"</tr>" \
   378        /)
   379   table_footer = "</table>"
   380   row_header = "<tr>"
   381   row_footer = "</tr>"
   382 
   383   lines = new(50000,string)
   384   nline = 0
   385 
   386   set_line(lines,nline,header)
   387   set_line(lines,nline,table_header)
   388 ;-----------------------------------------------
   389 ; row of table
   390   
   391   do n = 0,nstation-1
   392      set_line(lines,nline,row_header)
   393 
   394      txt0 = station(n)
   395      txt1 = sprintf("%5.2f", lat_ob(n))
   396      txt2 = sprintf("%5.2f", lon_ob(n))
   397      txt3 = year_ob(n)
   398 
   399      set_line(lines,nline,"<th><a href="+txt0+"_ob.png>"+txt0+"</a></th>")
   400      set_line(lines,nline,"<th>"+txt1+"</th>")
   401      set_line(lines,nline,"<th>"+txt2+"</th>")
   402      set_line(lines,nline,"<th>"+txt3+"</th>")
   403 
   404      set_line(lines,nline,row_footer)
   405   end do
   406 ;-----------------------------------------------
   407   set_line(lines,nline,table_footer)
   408   set_line(lines,nline,footer) 
   409 
   410 ; Now write to an HTML file.
   411   idx = ind(.not.ismissing(lines))
   412   if(.not.any(ismissing(idx))) then
   413     asciiwrite(output_html,lines(idx))
   414   else
   415    print ("error?")
   416   end if
   417   delete (idx)
   418 
   419 ;*******************************************************************
   420 ; score and line table : model vs observed
   421 ;*******************************************************************
   422   output_html = "score+line_vs_ob.html"
   423 
   424   header = (/"<HTML>" \
   425             ,"<HEAD>" \
   426             ,"<TITLE>CLAMP metrics</TITLE>" \
   427             ,"</HEAD>" \
   428             ,"<H1>Energy at Site: Model "+model_name+"</H1>" \
   429             /) 
   430   footer = "</HTML>"
   431 
   432   delete (table_header)
   433   table_header = (/ \
   434         "<table border=1 cellspacing=0 cellpadding=3 width=100%>" \
   435        ,"<tr>" \
   436        ,"   <th bgcolor=DDDDDD >Site Name</th>" \
   437        ,"   <th bgcolor=DDDDDD >Latitude</th>" \
   438        ,"   <th bgcolor=DDDDDD >Longitude</th>" \
   439        ,"   <th bgcolor=DDDDDD >Observed</th>" \
   440        ,"   <th bgcolor=DDDDDD >CO2 Flux</th>" \
   441        ,"   <th bgcolor=DDDDDD >Net Radiation</th>" \
   442        ,"   <th bgcolor=DDDDDD >Latent Heat</th>" \
   443        ,"   <th bgcolor=DDDDDD >Sensible Heat</th>" \
   444        ,"   <th bgcolor=DDDDDD >Average</th>" \
   445        ,"</tr>" \
   446        /)
   447   table_footer = "</table>"
   448   row_header = "<tr>"
   449   row_footer = "</tr>"
   450 
   451   lines = new(50000,string)
   452   nline = 0
   453 
   454   set_line(lines,nline,header)
   455   set_line(lines,nline,table_header)
   456 ;-----------------------------------------------
   457 ; row of table
   458   
   459   do n = 0,nstation-1
   460      set_line(lines,nline,row_header)
   461 
   462      txt0 = station(n)
   463      txt1 = sprintf("%5.2f", lat_ob(n))
   464      txt2 = sprintf("%5.2f", lon_ob(n))
   465      txt3 = year_ob(n)
   466      txt4 = sprintf("%5.2f", M_score(n,0))
   467      txt5 = sprintf("%5.2f", M_score(n,1))
   468      txt6 = sprintf("%5.2f", M_score(n,2))
   469      txt7 = sprintf("%5.2f", M_score(n,3))
   470      txt8 = sprintf("%5.2f", avg(M_score(n,:)))
   471 
   472      set_line(lines,nline,"<th><a href="+txt0+"_model_vs_ob.png>"+txt0+"</a></th>")
   473      set_line(lines,nline,"<th>"+txt1+"</th>")
   474      set_line(lines,nline,"<th>"+txt2+"</th>")
   475      set_line(lines,nline,"<th>"+txt3+"</th>")
   476      set_line(lines,nline,"<th>"+txt4+"</th>")
   477      set_line(lines,nline,"<th>"+txt5+"</th>")
   478      set_line(lines,nline,"<th>"+txt6+"</th>")
   479      set_line(lines,nline,"<th>"+txt7+"</th>")
   480      set_line(lines,nline,"<th>"+txt8+"</th>")
   481 
   482      set_line(lines,nline,row_footer)
   483   end do
   484 
   485 ; last row, summary
   486   set_line(lines,nline,row_header)
   487 
   488   txt0 = "All_"+sprintf("%.0f", nstation)
   489   txt1 = "-"
   490   txt2 = "-"
   491   txt3 = "-"
   492   txt4 = M_energy_co2
   493   txt5 = M_energy_rad
   494   txt6 = M_energy_lh
   495   txt7 = M_energy_sh
   496   txt8 = M_energy_all
   497 
   498   set_line(lines,nline,"<th>"+txt0+"</th>")
   499   set_line(lines,nline,"<th>"+txt1+"</th>")
   500   set_line(lines,nline,"<th>"+txt2+"</th>")
   501   set_line(lines,nline,"<th>"+txt3+"</th>")
   502   set_line(lines,nline,"<th>"+txt4+"</th>")
   503   set_line(lines,nline,"<th>"+txt5+"</th>")
   504   set_line(lines,nline,"<th>"+txt6+"</th>")
   505   set_line(lines,nline,"<th>"+txt7+"</th>")
   506   set_line(lines,nline,"<th>"+txt8+"</th>")
   507 
   508   set_line(lines,nline,row_footer)
   509 ;-----------------------------------------------
   510   set_line(lines,nline,table_footer)
   511   set_line(lines,nline,footer) 
   512 
   513 ; Now write to an HTML file.
   514   idx = ind(.not.ismissing(lines))
   515   if(.not.any(ismissing(idx))) then
   516     asciiwrite(output_html,lines(idx))
   517   else
   518    print ("error?")
   519   end if
   520   delete (idx)
   521 
   522 ;**************************************************************************************
   523 ; update score
   524 ;**************************************************************************************
   525  
   526   if (isvar("compare")) then
   527      system("sed 1,/M_energy_co2/s//"+M_energy_co2+"/ "+html_name2+" > "+html_new2+";"+ \
   528             "mv -f "+html_new2+" "+html_name2+";"+ \
   529             "sed 1,/M_energy_rad/s//"+M_energy_rad+"/ "+html_name2+" > "+html_new2+";"+ \
   530             "mv -f "+html_new2+" "+html_name2+";"+ \
   531             "sed 1,/M_energy_lh/s//"+M_energy_lh+"/ "+html_name2+" > "+html_new2+";"+ \
   532             "mv -f "+html_new2+" "+html_name2+";"+ \
   533             "sed 1,/M_energy_sh/s//"+M_energy_sh+"/ "+html_name2+" > "+html_new2+";"+ \
   534             "mv -f "+html_new2+" "+html_name2)
   535   end if
   536 
   537   system("sed s#M_energy_co2#"+M_energy_co2+"# "+html_name+" > "+html_new+";"+ \
   538          "mv -f "+html_new+" "+html_name+";"+ \
   539          "sed s#M_energy_rad#"+M_energy_rad+"# "+html_name+" > "+html_new+";"+ \
   540          "mv -f "+html_new+" "+html_name+";"+ \
   541          "sed s#M_energy_lh#"+M_energy_lh+"# "+html_name+" > "+html_new+";"+ \
   542          "mv -f "+html_new+" "+html_name+";"+ \
   543          "sed s#M_energy_sh#"+M_energy_sh+"# "+html_name+" > "+html_new+";"+ \
   544          "mv -f "+html_new+" "+html_name) 
   545 
   546 ;***************************************************************************
   547 ; output plots
   548 ;***************************************************************************
   549   output_dir = model_name+"/energy"
   550 
   551   system("mv *.png *.html " + output_dir) 
   552 ;***************************************************************************
   553 end