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