ameriflux/24.plot.ncl
changeset 1 4be95183fbcd
equal deleted inserted replaced
-1:000000000000 0:3faccbf908d2
       
     1 ;************************************************************
       
     2 ; change data name to timeseries_L4_m.nc
       
     3 ; move data
       
     4 ; merge annual
       
     5 ; add table
       
     6 ; add time series plot
       
     7 ; sort by latitude in decending order (N->S)
       
     8 ; find year_ob >= 4
       
     9 ; add year_ob_i and year_ob_f
       
    10 ; change long_name
       
    11 ;************************************************************
       
    12 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
       
    13 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
       
    14 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
       
    15 ;************************************************************
       
    16 procedure set_line(lines:string,nline:integer,newlines:string) 
       
    17 begin
       
    18 ; add line to ascci/html file
       
    19     
       
    20   nnewlines = dimsizes(newlines)
       
    21   if(nline+nnewlines-1.ge.dimsizes(lines))
       
    22     print("set_line: bad index, not setting anything.") 
       
    23     return
       
    24   end if 
       
    25   lines(nline:nline+nnewlines-1) = newlines
       
    26 ;  print ("lines = " + lines(nline:nline+nnewlines-1))
       
    27   nline = nline + nnewlines
       
    28   return 
       
    29 end
       
    30 ;*************************************************************
       
    31 begin
       
    32 
       
    33   plot_type     = "ps"
       
    34   plot_type_new = "png"
       
    35 
       
    36 ;************************************************
       
    37 ; model data info
       
    38 ;************************************************
       
    39   model = "cn"
       
    40 ; model = "casa"
       
    41   
       
    42   model_name = "i01.10" + model
       
    43   
       
    44   ENERGY ="new"
       
    45 
       
    46   nmonth = 12
       
    47 
       
    48 ;************************************************
       
    49 ; observed data info
       
    50 ;************************************************
       
    51 
       
    52  station = (/"ARM_Oklahoma" \
       
    53             ,"ARM_Oklahoma_burn" \
       
    54             ,"ARM_Oklahoma_control" \
       
    55             ,"Atqasuk" \
       
    56             ,"Audubon" \
       
    57             ,"AustinCary" \
       
    58             ,"Bartlett" \
       
    59             ,"Bondville" \
       
    60             ,"Brookings" \
       
    61             ,"Donaldson" \
       
    62             ,"Duke_Forest_Hardwoods" \
       
    63             ,"Duke_Forest_Open_Field" \
       
    64             ,"Duke_Forest_Pine" \
       
    65             ,"Fermi_Ag" \
       
    66             ,"Fermi_Prairie" \
       
    67             ,"Flagstaff_Managed" \
       
    68             ,"Flagstaff_Unmanaged" \
       
    69             ,"Flagstaff_Wildfire" \
       
    70             ,"FortPeck" \
       
    71             ,"FreemanRanch_mesquite" \
       
    72             ,"Goodwin_Creek" \
       
    73             ,"HarvardForest" \
       
    74             ,"HarvardForestHemlock" \
       
    75             ,"HowlandForestMain" \
       
    76             ,"HowlandForestWest" \
       
    77             ,"Ivotuk" \
       
    78             ,"KendallGrasslands" \
       
    79             ,"KennedySpaceCenterPine" \
       
    80             ,"KennedySpaceCenterScrub" \
       
    81             ,"LittleProspect" \
       
    82             ,"LostCreek" \
       
    83             ,"Mead-irrigated" \
       
    84             ,"Mead-irrigated-rotation" \
       
    85             ,"Mead-rainfed" \
       
    86             ,"Metolius_2nd_YoungPonderosaPine" \
       
    87             ,"MetoliusEyerly" \
       
    88             ,"MetoliusIntermediatePine" \
       
    89             ,"MetoliusOldPonderosaPine" \
       
    90             ,"MissouriOzark" \
       
    91             ,"Mize" \
       
    92             ,"MorganMonroe" \
       
    93             ,"NiwotRidge" \
       
    94             ,"NorthCarolina_cc" \
       
    95             ,"NorthCarolina_lp" \
       
    96             ,"ParkFalls" \
       
    97             ,"Rayonier" \
       
    98             ,"SantaRita" \
       
    99             ,"SkyOaks_Old" \
       
   100             ,"SkyOaks_PostFire" \
       
   101             ,"SkyOaks_Young" \
       
   102             ,"SylvaniaWilderness" \
       
   103             ,"Toledo" \
       
   104             ,"Tonzi" \
       
   105             ,"UCI_1850" \
       
   106             ,"UCI_1930" \
       
   107             ,"UCI_1964" \
       
   108             ,"UCI_1964wet" \
       
   109             ,"UCI_1981" \
       
   110             ,"UCI_1989" \
       
   111             ,"UCI_1998" \
       
   112             ,"UMBS" \
       
   113             ,"Vaira" \
       
   114             ,"WalkerBranch" \
       
   115             ,"WillowCreek" \
       
   116             ,"WindRiver" \
       
   117             ,"Wisconsin_ihw" \
       
   118             ,"Wisconsin_irp" \
       
   119             ,"Wisconsin_mrp" \
       
   120             ,"Wisconsin_myjp" \
       
   121             ,"Wisconsin_pb" \
       
   122             ,"Wisconsin_rpcc" \
       
   123             ,"Wisconsin_yhw" \
       
   124             ,"Wisconsin_yjp" \
       
   125             ,"Wisconsin_yrp" \
       
   126             /)
       
   127 
       
   128  field   = (/"NEE Flux" \
       
   129             ,"Net Radiation" \
       
   130             ,"Latent Heat" \
       
   131             ,"Sensible Heat" \
       
   132             ,"GPP Flux" \
       
   133             ,"Respiration" \
       
   134             /)
       
   135 
       
   136  field_unit = (/"u mol/m2/s" \
       
   137                ,"W/m2" \
       
   138                ,"W/m2" \
       
   139                ,"W/m2" \
       
   140                ,"u mol/m2/s" \
       
   141                ,"u mol/m2/s" \
       
   142                /)
       
   143 
       
   144  nstation = dimsizes(station)
       
   145  nfield   = dimsizes(field)
       
   146 
       
   147 ;========================================================================
       
   148 ; get observed info: number of year, first/last year, lat, lon
       
   149 ; and annual data
       
   150 
       
   151  dir_root = "/fis/cgd/cseg/people/jeff/clamp_data/observed/ameriflux/"
       
   152 
       
   153  year_station = new ((/nstation/),integer) ; number of year
       
   154  year_ob      = new ((/nstation/),string)  ; observed year
       
   155  year_ob_i    = new ((/nstation/),integer) ; first year
       
   156  year_ob_f    = new ((/nstation/),integer) ; last year
       
   157  lat_ob       = new ((/nstation/),float)   ; latitude
       
   158  lon_ob       = new ((/nstation/),float)   ; longitude 
       
   159 
       
   160  data_ob_ann  = new ((/nfield, nmonth, nstation/),float)
       
   161 
       
   162  do n = 0, nstation-1
       
   163 
       
   164     diro = dir_root + station(n)+"/"
       
   165     filo = "timeseries_L4_m.nc"
       
   166     fo   = addfile (diro+filo,"r")
       
   167  
       
   168     lat_ob(n) = fo->lat
       
   169     lon_ob(n) = fo->lon
       
   170 
       
   171     year      = fo->year
       
   172 
       
   173     year_station(n) = dimsizes(year)
       
   174     year_ob_i(n)    = year(0)
       
   175     year_ob_f(n)    = year(year_station(n)-1)
       
   176     year_ob(n)      = year_ob_i(n) + "-" + year_ob_f(n)
       
   177  
       
   178     delete (year)
       
   179  
       
   180     data = fo->NEE_or_fMDS
       
   181     data_ob_ann(0,:,n) = dim_avg(data(month|:,year|:))
       
   182 
       
   183     data = fo->Rg_f
       
   184     data_ob_ann(1,:,n) = dim_avg(data(month|:,year|:))
       
   185 
       
   186     data = fo->LE_f
       
   187     data_ob_ann(2,:,n) = dim_avg(data(month|:,year|:))
       
   188 
       
   189     data = fo->H_f
       
   190     data_ob_ann(3,:,n) = dim_avg(data(month|:,year|:))
       
   191 
       
   192     data = fo->GPP_or_MDS
       
   193     data_ob_ann(4,:,n) = dim_avg(data(month|:,year|:))
       
   194 
       
   195     data = fo->Reco_or
       
   196     data_ob_ann(5,:,n) = dim_avg(data(month|:,year|:))
       
   197 
       
   198     delete (data) 
       
   199     delete (fo)   
       
   200  end do
       
   201 
       
   202 ;print (year_ob_i)
       
   203 ;print (year_ob_f)
       
   204 
       
   205 ;--------------------------------------------------------------
       
   206 ; find (# of year observed) >=4 and year_ob_i <= 2001
       
   207 
       
   208  i_long_ob = ind(year_station .ge. 4 .and. year_ob_i .le. 2001)
       
   209  
       
   210  station_long      = station(i_long_ob)
       
   211  lat_ob_long       = lat_ob(i_long_ob)
       
   212  lon_ob_long       = lat_ob(i_long_ob)
       
   213  year_ob_long      = year_ob(i_long_ob)
       
   214  year_ob_i_long    = year_ob_i(i_long_ob)
       
   215  year_ob_f_long    = year_ob_f(i_long_ob)
       
   216  year_station_long = year_station(i_long_ob)
       
   217 
       
   218  nstation_long     = dimsizes(station_long)
       
   219 
       
   220 ; print (i_long_ob)
       
   221 ; print (nstation_long)
       
   222 ; print (station_long)
       
   223 ; print (year_ob_i(i_long_ob))
       
   224 
       
   225 ;=========================================================
       
   226 ;   get model data at observed lat-lon
       
   227 
       
   228     dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"    
       
   229     film = model_name+"_ameriflux_1990-2004_monthly.nc"
       
   230     fm   = addfile (dirm+film,"r")
       
   231 
       
   232     xm   = fm->lon
       
   233     ym   = fm->lat 
       
   234     date = fm->date
       
   235 
       
   236     date_dim = dimsizes(date)
       
   237     nyear  = date_dim(0)
       
   238 
       
   239     data_mod      = new ((/nfield,nyear,nmonth,nstation/),float)
       
   240     data_mod_ann  = new ((/nfield,nmonth,nstation/),float)
       
   241     data_mod_long = new ((/nfield,nyear,nmonth,nstation_long/),float)
       
   242 
       
   243 ;   change to unit of observed (u mol/m2/s)
       
   244 ;   Model_units [=] gC/m2/s
       
   245 ;   12. = molecular weight of C
       
   246 ;   u mol = 1e-6 mol
       
   247 
       
   248     factor = 1.e6 /12.
       
   249 
       
   250 ;------------------------------------------------------------
       
   251 ;   interpolate model data into observed station
       
   252 ;   note: model is 0-360E, 90S-90N
       
   253 
       
   254 ;   to be able to handle observation at (-89.98,-24.80)
       
   255     ym(0) = -90.
       
   256 ;------------------------------------------------------------  
       
   257 
       
   258 if (ENERGY .eq. "old") then
       
   259 
       
   260   data = fm->NEE
       
   261   data_mod(0,:,:,:) = data(:,:,:) * factor
       
   262 
       
   263 ; data  = fm->LATENT
       
   264   data1 = fm->FCEV
       
   265   data2 = fm->FCTR
       
   266   data3 = fm->FGEV
       
   267   data_mod(2,:,:,:) = data1(:,:,:)+data2(:,:,:)+data3(:,:,:) 
       
   268   delete (data1)
       
   269   delete (data2)
       
   270   delete (data3)
       
   271 
       
   272 ; data = fm->SENSIBLE
       
   273   data  = fm->FSH
       
   274   data_mod(3,:,:,:) = data(:,:,:) 
       
   275 
       
   276 ; data  = fm->NETRAD
       
   277   data1 = fm->FSA
       
   278   data2 = fm->FIRA
       
   279   data_mod(1,:,:,:) = data1(:,:,:)-data2(:,:,:) 
       
   280   delete (data1)
       
   281   delete (data2)
       
   282 
       
   283 else
       
   284 
       
   285   data = fm->NEE
       
   286   yy = linint2_points_Wrap(xm,ym,data,True,lon_ob,lat_ob,0)
       
   287   data_mod_ann(0,:,:)= dim_avg(yy(month|:,pts|:,year|:)) * factor
       
   288   data_mod(0,:,:,:) = yy(:,:,:) * factor
       
   289 
       
   290 ; printVarSummary(yy)
       
   291 
       
   292   data = fm->NETRAD
       
   293   yy = linint2_points_Wrap(xm,ym,data,True,lon_ob,lat_ob,0)
       
   294   data_mod_ann(1,:,:)= dim_avg(yy(month|:,pts|:,year|:)) 
       
   295   data_mod(1,:,:,:) = yy(:,:,:) 
       
   296 
       
   297   data = fm->LATENT
       
   298   yy = linint2_points_Wrap(xm,ym,data,True,lon_ob,lat_ob,0)
       
   299   data_mod_ann(2,:,:)= dim_avg(yy(month|:,pts|:,year|:)) 
       
   300   data_mod(2,:,:,:) = yy(:,:,:) 
       
   301 
       
   302 ; data = fm->SENSIBLE
       
   303   data = fm->FSH
       
   304   yy = linint2_points_Wrap(xm,ym,data,True,lon_ob,lat_ob,0)
       
   305   data_mod_ann(3,:,:)= dim_avg(yy(month|:,pts|:,year|:)) 
       
   306   data_mod(3,:,:,:) = yy(:,:,:) 
       
   307 
       
   308 end if
       
   309 
       
   310   data = fm->GPP
       
   311   yy = linint2_points_Wrap(xm,ym,data,True,lon_ob,lat_ob,0)
       
   312   data_mod_ann(4,:,:)= dim_avg(yy(month|:,pts|:,year|:)) * factor
       
   313   data_mod(4,:,:,:) = yy(:,:,:) * factor 
       
   314 
       
   315   if (model .eq. "cn") then
       
   316      data = fm->ER
       
   317   else
       
   318      data1 = fm->AR
       
   319      data2 = fm->HR
       
   320      data  = data1 + data2
       
   321     
       
   322      delete (data1)
       
   323      delete (data2)
       
   324   end if
       
   325 
       
   326   yy = linint2_points_Wrap(xm,ym,data,True,lon_ob,lat_ob,0)
       
   327   data_mod_ann(5,:,:)= dim_avg(yy(month|:,pts|:,year|:)) * factor
       
   328   data_mod(5,:,:,:) = yy(:,:,:) * factor
       
   329 
       
   330   data_mod_long(:,:,:,:) = data_mod(:,:,:,i_long_ob)
       
   331 
       
   332   delete (data_mod)
       
   333   delete (fm) 
       
   334   delete (data)
       
   335   delete (yy)
       
   336 
       
   337 ;*******************************************************************
       
   338 ; for station line plot
       
   339 ;*******************************************************************
       
   340 
       
   341 ; for x-axis in xyplot
       
   342   mon = ispan(1,12,1)
       
   343   mon@long_name = "month"
       
   344 
       
   345   res                   = True               ; plot mods desired
       
   346   res@xyLineThicknesses = (/2.0,2.0/)        ; make 2nd lines thicker
       
   347   res@xyLineColors      = (/"blue","red"/)   ; line color (ob,model)
       
   348 
       
   349   res@tmXBFormat  = "f"                      ; not to add trailing zeros
       
   350 
       
   351 ;-------------------------------------------------------------------------
       
   352 ; Add a boxed legend using the more simple method
       
   353 
       
   354   res@pmLegendDisplayMode    = "Always"
       
   355 ; res@pmLegendWidthF         = 0.1
       
   356   res@pmLegendWidthF         = 0.08
       
   357   res@pmLegendHeightF        = 0.06
       
   358 ; res@pmLegendOrthogonalPosF = -1.17
       
   359 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
       
   360   res@pmLegendOrthogonalPosF = -0.30  ;(downward)
       
   361 
       
   362 ; res@pmLegendParallelPosF   =  0.18
       
   363   res@pmLegendParallelPosF   =  0.23  ;(rightward)
       
   364 
       
   365 ; res@lgPerimOn             = False
       
   366   res@lgLabelFontHeightF     = 0.015
       
   367   res@xyExplicitLegendLabels = (/"observed",model_name/)
       
   368 ;-------------------------------------------------------------------
       
   369 ; for panel plot
       
   370   res@gsnFrame     = False                   ; Do not draw plot 
       
   371   res@gsnDraw      = False                   ; Do not advance frame
       
   372 
       
   373   pres                            = True     ; panel plot mods desired
       
   374   pres@gsnPanelYWhiteSpacePercent = 5        ; increase white space around
       
   375                                              ; indiv. plots in panel
       
   376   pres@gsnMaximize                = True     ; fill the page
       
   377 ;-------------------------------------------------------------------
       
   378 
       
   379 ;==============================================================
       
   380 ; get ob data at each site with long observation
       
   381 
       
   382   dir_root = "/fis/cgd/cseg/people/jeff/clamp/ameriflux/data/"
       
   383 
       
   384   do n = 0,nstation_long-1
       
   385 ; do n = 0,0
       
   386 
       
   387     year_setback = 0
       
   388 
       
   389     nyear = year_station_long(n)
       
   390 
       
   391     if (year_ob_f_long(n).eq. 2006) then
       
   392        year_setback = 2006 -2004
       
   393     end if
       
   394     if (year_ob_f_long(n).eq. 2005) then
       
   395        year_setback = 2005 -2004
       
   396     end if
       
   397     
       
   398     ntime = (nyear - year_setback) * nmonth
       
   399 
       
   400     data_ob   = new ((/nfield, nyear, nmonth/),float)
       
   401 
       
   402     diro = dir_root + station_long(n)+"/"
       
   403     filo = year_ob_long(n)+"_L4_m.nc"
       
   404     fo   = addfile (diro+filo,"r")
       
   405 
       
   406 ;   print (diro)
       
   407 ;   print (filo)
       
   408 
       
   409     data_ob(0,:,:) = fo->NEE_or_fMDS
       
   410     data_ob(1,:,:) = fo->Rg_f
       
   411     data_ob(2,:,:) = fo->LE_f
       
   412     data_ob(3,:,:) = fo->H_f
       
   413     data_ob(4,:,:) = fo->GPP_or_MDS
       
   414     data_ob(5,:,:) = fo->Reco_or
       
   415 
       
   416     delete (fo)
       
   417 
       
   418     timeI = new((/ntime/),integer)
       
   419     timeF = new((/ntime/),float)
       
   420     timeI = ispan(1,ntime,1)
       
   421     timeF = year_ob_i_long(n) + (timeI-1)/12.
       
   422     timeF@long_name = "year" 
       
   423 
       
   424     plot_data = new((/2,ntime/),float)
       
   425  
       
   426 ;----------------------------
       
   427 ; for model_vs_ob
       
   428 
       
   429     plot_name = station_long(n)+"_tseries_vs_ob"
       
   430     title = station_long(n)+"("+sprintf("%5.2f",lat_ob_long(n))+","+sprintf("%5.2f",lon_ob_long(n))+")"    
       
   431     res@tiMainString = title
       
   432 
       
   433     wks = gsn_open_wks (plot_type,plot_name)
       
   434     plot=new(nfield,graphic)                         ; create graphic array   
       
   435 
       
   436     i_year_mod_i = year_ob_i_long(n) - 1990 
       
   437     i_year_mod_f = i_year_mod_i + nyear - 1 - year_setback
       
   438 
       
   439     i_year_ob_f =  nyear - year_setback - 1 
       
   440 
       
   441 ;   print (nyear)
       
   442 ;   print (i_year_ob_f)
       
   443 ;   print (i_year_mod_i)
       
   444 ;   print (i_year_mod_f)
       
   445 
       
   446     do i = 0,nfield-1                           
       
   447        plot_data(0,:) = ndtooned(data_ob (i,0:i_year_ob_f,:))
       
   448        plot_data(1,:) = ndtooned(data_mod_long(i,i_year_mod_i:i_year_mod_f,:,n))
       
   449        plot_data@long_name = field(i)+" ("+field_unit(i)+")"   
       
   450        plot(i)=gsn_csm_xy(wks,timeF,plot_data,res)            ; create plot 
       
   451     end do
       
   452    
       
   453     gsn_panel(wks,plot,(/3,2/),pres)                    ; create panel plot
       
   454 
       
   455     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
       
   456            "rm "+plot_name+"."+plot_type)
       
   457 
       
   458     clear (wks)  
       
   459     delete (plot)
       
   460 
       
   461     delete (data_ob)
       
   462     delete (timeI)    
       
   463     delete (timeF)
       
   464     delete (plot_data)
       
   465 
       
   466  end do
       
   467 
       
   468 ;###################################################################
       
   469 ; for the following tables,
       
   470 ; sort by latitude in decending order (N->S)
       
   471 
       
   472   isort = dim_pqsort(lat_ob_long,-1)
       
   473 
       
   474   station_sort = station_long(isort)
       
   475   year_ob_sort = year_ob_long(isort)
       
   476   lat_ob_sort  = lat_ob_long(isort)
       
   477   lon_ob_sort  = lon_ob_long(isort)
       
   478  
       
   479 ; print(isort)
       
   480 ; print(lat_ob_sort)
       
   481 
       
   482 ;*******************************************************************
       
   483 ; html table of site: observed
       
   484 ;*******************************************************************
       
   485   output_html = "tseries_vs_ob.html"
       
   486 
       
   487   header = (/"<HTML>" \
       
   488             ,"<HEAD>" \
       
   489             ,"<TITLE>CLAMP metrics</TITLE>" \
       
   490             ,"</HEAD>" \
       
   491             ,"<H1>Timeseries at Site: "+model_name+" vs Observation</H1>" \
       
   492             /) 
       
   493   footer = "</HTML>"
       
   494 
       
   495   table_header = (/ \
       
   496         "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
       
   497        ,"<tr>" \
       
   498        ,"   <th bgcolor=DDDDDD >Site Name</th>" \
       
   499        ,"   <th bgcolor=DDDDDD >Latitude</th>" \
       
   500        ,"   <th bgcolor=DDDDDD >Longitude</th>" \
       
   501        ,"   <th bgcolor=DDDDDD >Observed</th>" \ 
       
   502        ,"</tr>" \
       
   503        /)
       
   504   table_footer = "</table>"
       
   505   row_header = "<tr>"
       
   506   row_footer = "</tr>"
       
   507 
       
   508   lines = new(50000,string)
       
   509   nline = 0
       
   510 
       
   511   set_line(lines,nline,header)
       
   512   set_line(lines,nline,table_header)
       
   513 ;-----------------------------------------------
       
   514 ; row of table
       
   515   
       
   516   do n = 0,nstation_long-1
       
   517 
       
   518      set_line(lines,nline,row_header)
       
   519 
       
   520      txt0 = station_sort(n)
       
   521      txt1 = sprintf("%5.2f", lat_ob_sort(n))
       
   522      txt2 = sprintf("%5.2f", lon_ob_sort(n))
       
   523      txt3 = year_ob_sort(n)
       
   524 
       
   525      set_line(lines,nline,"<th><a href="+txt0+"_tseries_vs_ob.png>"+txt0+"</a></th>")
       
   526      set_line(lines,nline,"<th>"+txt1+"</th>")
       
   527      set_line(lines,nline,"<th>"+txt2+"</th>")
       
   528      set_line(lines,nline,"<th>"+txt3+"</th>")
       
   529 
       
   530      set_line(lines,nline,row_footer)
       
   531   end do
       
   532 ;-----------------------------------------------
       
   533   set_line(lines,nline,table_footer)
       
   534   set_line(lines,nline,footer) 
       
   535 
       
   536 ; Now write to an HTML file.
       
   537   idx = ind(.not.ismissing(lines))
       
   538   if(.not.any(ismissing(idx))) then
       
   539     asciiwrite(output_html,lines(idx))
       
   540   else
       
   541    print ("error?")
       
   542   end if
       
   543   delete (idx)
       
   544 
       
   545   delete (isort)
       
   546   delete (station_sort)
       
   547   delete (year_ob_sort)
       
   548   delete (lat_ob_sort)
       
   549   delete (lon_ob_sort)
       
   550 
       
   551 ;************************************************************
       
   552 ; compute annual cycle correlation coef and M score
       
   553 ;************************************************************
       
   554 
       
   555  score_max = 5.
       
   556 
       
   557  ccr     = new ((/nstation, nfield/),float)
       
   558  M_score = new ((/nstation, nfield/),float) 
       
   559 
       
   560  do n=0,nstation-1
       
   561  do m=0,nfield-1   
       
   562     ccr(n,m) = esccr(data_ob_ann(m,:,n),data_mod_ann(m,:,n),0)
       
   563     bias = sum(abs(data_mod_ann(m,:,n)-data_ob_ann(m,:,n))/(abs(data_mod_ann(m,:,n))+abs(data_ob_ann(m,:,n))))
       
   564     M_score(n,m) = (1. -(bias/nmonth)) * score_max
       
   565  end do
       
   566  end do
       
   567 
       
   568  M_co2 = avg(M_score(:,0))
       
   569  M_rad = avg(M_score(:,1))
       
   570  M_lh  = avg(M_score(:,2))
       
   571  M_sh  = avg(M_score(:,3))
       
   572  M_gpp = avg(M_score(:,4))
       
   573  M_er  = avg(M_score(:,5))
       
   574  M_all = M_co2+ M_rad +M_lh + M_sh + M_gpp + M_er
       
   575 
       
   576  M_energy_co2 = sprintf("%.2f", M_co2)
       
   577  M_energy_rad = sprintf("%.2f", M_rad)
       
   578  M_energy_lh  = sprintf("%.2f", M_lh )
       
   579  M_energy_sh  = sprintf("%.2f", M_sh )
       
   580  M_energy_gpp = sprintf("%.2f", M_gpp)
       
   581  M_energy_er  = sprintf("%.2f", M_er )
       
   582  M_energy_all = sprintf("%.2f", M_all)
       
   583 
       
   584 ;*******************************************************************
       
   585 ; for station line plot
       
   586 ;*******************************************************************
       
   587 
       
   588 ; for x-axis in xyplot
       
   589   mon = ispan(1,12,1)
       
   590   mon@long_name = "month"
       
   591 
       
   592 ;-------------------------------------------------------------------
       
   593 
       
   594   plot_data   = new((/2,nmonth/),float)
       
   595   plot_data!0 = "case"
       
   596   plot_data!1 = "month"
       
   597 
       
   598   do n = 0,nstation-1
       
   599 ;----------------------------
       
   600 ; for observed
       
   601 
       
   602     plot_name = station(n)+"_ob"    
       
   603     title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"
       
   604     res@tiMainString = title
       
   605 
       
   606     wks = gsn_open_wks (plot_type,plot_name)
       
   607     plot=new(nfield,graphic)                        ; create graphic array   
       
   608 
       
   609     do i = 0,nfield-1                           
       
   610        plot_data(0,:) = (/data_ob_ann(i,:,n)/)
       
   611        plot_data@long_name = field(i)+" ("+field_unit(i)+")"   
       
   612        plot(i)=gsn_csm_xy(wks,mon,plot_data(0,:),res)    ; create plot 
       
   613     end do
       
   614 
       
   615     gsn_panel(wks,plot,(/3,2/),pres)                 ; create panel plot
       
   616 
       
   617     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
       
   618            "rm "+plot_name+"."+plot_type)
       
   619 
       
   620     clear (wks)  
       
   621     delete (plot)
       
   622 ;----------------------------
       
   623 ; for model_vs_ob
       
   624 
       
   625     plot_name = station(n)+"_model_vs_ob"
       
   626     title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"    
       
   627     res@tiMainString = title
       
   628 
       
   629     wks = gsn_open_wks (plot_type,plot_name)
       
   630     plot=new(nfield,graphic)                         ; create graphic array   
       
   631 
       
   632     do i = 0,nfield-1                           
       
   633        plot_data(0,:) = (/data_ob_ann(i,:,n)/)
       
   634        plot_data(1,:) = (/data_mod_ann(i,:,n)/)
       
   635        plot_data@long_name = field(i)+" ("+field_unit(i)+")"   
       
   636        plot(i)=gsn_csm_xy(wks,mon,plot_data,res)     ; create plot 
       
   637     end do
       
   638    
       
   639     gsn_panel(wks,plot,(/3,2/),pres)                 ; create panel plot
       
   640 
       
   641     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
       
   642            "rm "+plot_name+"."+plot_type)
       
   643 
       
   644     clear (wks)  
       
   645     delete (plot)
       
   646  end do
       
   647 
       
   648 ;###################################################################
       
   649 ; for the following tables,
       
   650 ; sort by latitude in decending order (N->S)
       
   651 ; sort by lat in decending order (N->S)
       
   652 
       
   653   isort = dim_pqsort(lat_ob,-1)
       
   654 
       
   655   station_sort = station(isort)
       
   656   year_ob_sort = year_ob(isort)
       
   657   lat_ob_sort  = lat_ob(isort)
       
   658   lon_ob_sort  = lon_ob(isort)
       
   659   M_score_sort = M_score(isort,:)
       
   660  
       
   661 ; print(isort)
       
   662 ; print(lat_ob_sort)
       
   663 ;###################################################################
       
   664 ;*******************************************************************
       
   665 ; html table of site: observed
       
   666 ;*******************************************************************
       
   667   output_html = "line_ob.html"
       
   668 
       
   669   header = (/"<HTML>" \
       
   670             ,"<HEAD>" \
       
   671             ,"<TITLE>CLAMP metrics</TITLE>" \
       
   672             ,"</HEAD>" \
       
   673             ,"<H1>Energy at Site: Observation</H1>" \
       
   674             /) 
       
   675   footer = "</HTML>"
       
   676 
       
   677   table_header = (/ \
       
   678         "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
       
   679        ,"<tr>" \
       
   680        ,"   <th bgcolor=DDDDDD >Site Name</th>" \
       
   681        ,"   <th bgcolor=DDDDDD >Latitude</th>" \
       
   682        ,"   <th bgcolor=DDDDDD >Longitude</th>" \
       
   683        ,"   <th bgcolor=DDDDDD >Observed</th>" \ 
       
   684        ,"</tr>" \
       
   685        /)
       
   686   table_footer = "</table>"
       
   687   row_header = "<tr>"
       
   688   row_footer = "</tr>"
       
   689 
       
   690   lines = new(50000,string)
       
   691   nline = 0
       
   692 
       
   693   set_line(lines,nline,header)
       
   694   set_line(lines,nline,table_header)
       
   695 ;-----------------------------------------------
       
   696 ; row of table
       
   697   
       
   698   do n = 0,nstation-1
       
   699      set_line(lines,nline,row_header)
       
   700 
       
   701      txt0 = station_sort(n)
       
   702      txt1 = sprintf("%5.2f", lat_ob_sort(n))
       
   703      txt2 = sprintf("%5.2f", lon_ob_sort(n))
       
   704      txt3 = year_ob_sort(n)
       
   705 
       
   706      set_line(lines,nline,"<th><a href="+txt0+"_ob.png>"+txt0+"</a></th>")
       
   707      set_line(lines,nline,"<th>"+txt1+"</th>")
       
   708      set_line(lines,nline,"<th>"+txt2+"</th>")
       
   709      set_line(lines,nline,"<th>"+txt3+"</th>")
       
   710 
       
   711      set_line(lines,nline,row_footer)
       
   712   end do
       
   713 ;-----------------------------------------------
       
   714   set_line(lines,nline,table_footer)
       
   715   set_line(lines,nline,footer) 
       
   716 
       
   717 ; Now write to an HTML file.
       
   718   idx = ind(.not.ismissing(lines))
       
   719   if(.not.any(ismissing(idx))) then
       
   720     asciiwrite(output_html,lines(idx))
       
   721   else
       
   722    print ("error?")
       
   723   end if
       
   724   delete (idx)
       
   725 
       
   726 ;*******************************************************************
       
   727 ; score and line table : model vs observed
       
   728 ;*******************************************************************
       
   729   output_html = "score+line_vs_ob.html"
       
   730 
       
   731   header = (/"<HTML>" \
       
   732             ,"<HEAD>" \
       
   733             ,"<TITLE>CLAMP metrics</TITLE>" \
       
   734             ,"</HEAD>" \
       
   735             ,"<H1>Energy at Site: Model "+model_name+"</H1>" \
       
   736             /) 
       
   737   footer = "</HTML>"
       
   738 
       
   739   delete (table_header)
       
   740   table_header = (/ \
       
   741         "<table border=1 cellspacing=0 cellpadding=3 width=100%>" \
       
   742        ,"<tr>" \
       
   743        ,"   <th bgcolor=DDDDDD >Site Name</th>" \
       
   744        ,"   <th bgcolor=DDDDDD >Latitude</th>" \
       
   745        ,"   <th bgcolor=DDDDDD >Longitude</th>" \
       
   746        ,"   <th bgcolor=DDDDDD >Observed</th>" \
       
   747        ,"   <th bgcolor=DDDDDD >CO2 Flux</th>" \
       
   748        ,"   <th bgcolor=DDDDDD >Net Radiation</th>" \
       
   749        ,"   <th bgcolor=DDDDDD >Latent Heat</th>" \
       
   750        ,"   <th bgcolor=DDDDDD >Sensible Heat</th>" \
       
   751        ,"   <th bgcolor=DDDDDD >GPP Glux</th>" \
       
   752        ,"   <th bgcolor=DDDDDD >Respiration</th>" \
       
   753        ,"   <th bgcolor=DDDDDD >Average</th>" \
       
   754        ,"</tr>" \
       
   755        /)
       
   756   table_footer = "</table>"
       
   757   row_header = "<tr>"
       
   758   row_footer = "</tr>"
       
   759 
       
   760   lines = new(50000,string)
       
   761   nline = 0
       
   762 
       
   763   set_line(lines,nline,header)
       
   764   set_line(lines,nline,table_header)
       
   765 ;-----------------------------------------------
       
   766 ; row of table
       
   767   
       
   768   do n = 0,nstation-1
       
   769      set_line(lines,nline,row_header)
       
   770 
       
   771      txt0  = station_sort(n)
       
   772      txt1  = sprintf("%5.2f", lat_ob_sort(n))
       
   773      txt2  = sprintf("%5.2f", lon_ob_sort(n))
       
   774      txt3  = year_ob_sort(n)
       
   775      txt4  = sprintf("%5.2f", M_score_sort(n,0))
       
   776      txt5  = sprintf("%5.2f", M_score_sort(n,1))
       
   777      txt6  = sprintf("%5.2f", M_score_sort(n,2))
       
   778      txt7  = sprintf("%5.2f", M_score_sort(n,3))
       
   779      txt8  = sprintf("%5.2f", M_score_sort(n,4))
       
   780      txt9  = sprintf("%5.2f", M_score_sort(n,5))
       
   781      txt10 = sprintf("%5.2f", avg(M_score_sort(n,:)))
       
   782 
       
   783      set_line(lines,nline,"<th><a href="+txt0+"_model_vs_ob.png>"+txt0+"</a></th>")
       
   784      set_line(lines,nline,"<th>"+txt1+"</th>")
       
   785      set_line(lines,nline,"<th>"+txt2+"</th>")
       
   786      set_line(lines,nline,"<th>"+txt3+"</th>")
       
   787      set_line(lines,nline,"<th>"+txt4+"</th>")
       
   788      set_line(lines,nline,"<th>"+txt5+"</th>")
       
   789      set_line(lines,nline,"<th>"+txt6+"</th>")
       
   790      set_line(lines,nline,"<th>"+txt7+"</th>")
       
   791      set_line(lines,nline,"<th>"+txt8+"</th>")
       
   792      set_line(lines,nline,"<th>"+txt9+"</th>")
       
   793      set_line(lines,nline,"<th>"+txt10+"</th>")
       
   794 
       
   795      set_line(lines,nline,row_footer)
       
   796   end do
       
   797 
       
   798 ; last row, summary
       
   799   set_line(lines,nline,row_header)
       
   800 
       
   801   txt0  = "All_"+sprintf("%.0f", nstation)
       
   802   txt1  = "-"
       
   803   txt2  = "-"
       
   804   txt3  = "-"
       
   805   txt4  = M_energy_co2
       
   806   txt5  = M_energy_rad
       
   807   txt6  = M_energy_lh
       
   808   txt7  = M_energy_sh
       
   809   txt8  = M_energy_gpp
       
   810   txt9  = M_energy_er
       
   811   txt10 = M_energy_all
       
   812 
       
   813   set_line(lines,nline,"<th>"+txt0+"</th>")
       
   814   set_line(lines,nline,"<th>"+txt1+"</th>")
       
   815   set_line(lines,nline,"<th>"+txt2+"</th>")
       
   816   set_line(lines,nline,"<th>"+txt3+"</th>")
       
   817   set_line(lines,nline,"<th>"+txt4+"</th>")
       
   818   set_line(lines,nline,"<th>"+txt5+"</th>")
       
   819   set_line(lines,nline,"<th>"+txt6+"</th>")
       
   820   set_line(lines,nline,"<th>"+txt7+"</th>")
       
   821   set_line(lines,nline,"<th>"+txt8+"</th>")
       
   822   set_line(lines,nline,"<th>"+txt9+"</th>")
       
   823   set_line(lines,nline,"<th>"+txt10+"</th>")
       
   824 
       
   825   set_line(lines,nline,row_footer)
       
   826 ;-----------------------------------------------
       
   827   set_line(lines,nline,table_footer)
       
   828   set_line(lines,nline,footer) 
       
   829 
       
   830 ; Now write to an HTML file.
       
   831   idx = ind(.not.ismissing(lines))
       
   832   if(.not.any(ismissing(idx))) then
       
   833     asciiwrite(output_html,lines(idx))
       
   834   else
       
   835    print ("error?")
       
   836   end if
       
   837   delete (idx)
       
   838 
       
   839 ;***************************************************************************
       
   840 ; output plots
       
   841 ;***************************************************************************
       
   842   output_dir = model_name+"/ameriflux"
       
   843 
       
   844   system("mv *.png *.html " + output_dir) 
       
   845 ;***************************************************************************
       
   846 end
       
   847