energy/99.all.ncl.2
author Forrest Hoffman <forrest@climatemodeling.org>
Mon, 26 Jan 2009 22:08:20 -0500
changeset 0 0c6405ab2ff4
permissions -rw-r--r--
Initial commit of C-LAMP Diagnostics from Jeff Lee
     1 ; ***********************************************
     2 ; using gsn_table for all
     3 ; output: line plot for each site (4 fields)
     4 ;         table for M_score
     5 ; ***********************************************
     6 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl.test"
     7 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl.test"
     8 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
     9 ;load "/fis/cgd/cseg/people/jeff/clamp/co2/metrics_table.ncl"
    10 ;************************************************
    11 begin
    12 
    13   plot_type     = "ps"
    14   plot_type_new = "png"
    15 
    16 ;************************************************
    17 ; read model data
    18 ;************************************************
    19   model_name = "10cn"
    20   film = "i01.10cn_1948-2004_MONS_climo.nc"
    21 
    22 ; model_name = "10casa"
    23 ; film = "i01.10casa_1948-2004_MONS_climo.nc"
    24 ;------------------------------------------------------------------------
    25  html_name = "table.html." + model_name
    26  html_new  = html_name +".new"
    27  system("sed s#model_name#"+model_name+"# "+html_name+" > "+html_new+";"+ \
    28          "mv -f "+html_new+" "+html_name)
    29 
    30   dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    31   fm    = addfile(dirm+film,"r")
    32 
    33   xm    = fm->lon
    34   ym    = fm->lat
    35   nlat = dimsizes(ym)
    36   nlon = dimsizes(xm)
    37 
    38   nmon      = 12
    39   nfield    = 4
    40 
    41   data_mod0 = new ((/nfield,nmon,nlat,nlon/),float)
    42 
    43 ; change to unit of observed (u mol/m2/s)
    44 ; Model_units [=] gC/m2/s
    45 ; 12. = molecular weight of C
    46 ; u mol = 1e-6 mol 
    47   data = fm->NEE
    48 
    49   factor = 1e6 /12.
    50 
    51   data_mod0(0,:,:,:) = data(:,:,:) * factor
    52   delete (data)
    53 
    54   data = fm->NETRAD
    55   data_mod0(1,:,:,:) = data(:,:,:) 
    56   delete (data)
    57 
    58   data = fm->LATENT
    59   data_mod0(2,:,:,:) = data(:,:,:) 
    60   delete (data)
    61 
    62   data = fm->SENSIBLE
    63   data_mod0(3,:,:,:) = data(:,:,:) 
    64   delete (data)
    65 
    66 ; printVarSummary (data_mod0)
    67 ;************************************************
    68 ; read in data: observed
    69 ;************************************************
    70  station = (/"BOREAS_NSA_OBS" \
    71             ,"CastelPorziano" \
    72             ,"Hyytiala" \
    73             ,"Kaamanen" \
    74             ,"LBA_Tapajos_KM67" \
    75             ,"Lethbridge" \
    76             ,"Tharandt" \
    77             ,"Vielsalm" \
    78             /)
    79 
    80  year_ob = (/"1994-2004" \
    81             ,"1997-2003" \
    82             ,"1996-2003" \
    83             ,"2000-2003" \
    84             ,"2002-2005" \
    85             ,"1999-2004" \
    86             ,"1996-2003" \
    87             ,"1998-2003" \
    88             /)
    89 
    90  field   = (/"CO2 Flux" \
    91             ,"Net Radiation" \
    92             ,"Latent Heat" \
    93             ,"Sensible Heat" \
    94             /)
    95 
    96  nstation  = dimsizes(station)
    97  nmon      = 12
    98  nfield    = dimsizes(field)
    99 
   100  data_ob   = new ((/nstation, nfield, nmon/),float)
   101  lat_ob    = new ((/nstation/),float)
   102  lon_ob    = new ((/nstation/),float)
   103 
   104  diri_root  = "/fis/cgd/cseg/people/jeff/clamp_data/fluxnet/"
   105 
   106  do n = 0,nstation-1
   107     diri = diri_root + station(n)+"/"
   108     fili = station(n)+"_"+year_ob(n)+"_monthly.nc"
   109     g     = addfile (diri+fili,"r")
   110  
   111     lon_ob(n) = g->lon 
   112     lat_ob(n) = g->lat
   113 
   114     data      = g->CO2_FLUX
   115     data_ob(n,0,:) = dim_avg(data(month|:,year|:))
   116     delete (data)
   117 
   118     data      = g->RAD_FLUX
   119     data_ob(n,1,:) = dim_avg(data(month|:,year|:))
   120     delete (data)
   121 
   122     data      = g->LH_FLUX
   123     data_ob(n,2,:) = dim_avg(data(month|:,year|:))
   124     delete (data)
   125 
   126     data      = g->SH_FLUX
   127     data_ob(n,3,:) = dim_avg(data(month|:,year|:))
   128     delete (data)
   129 
   130     delete (g)
   131  end do
   132  
   133 ;print (lat_ob)
   134 ;print (lon_ob)
   135 ;printVarSummary (data_ob)
   136 
   137 ;************************************************************
   138 ; interpolate model data into observed station
   139 ; note: model is 0-360E, 90S-90N
   140 ;************************************************************
   141 
   142 ; to be able to handle observation at (-89.98,-24.80)
   143 ; print (ym(0))
   144   ym(0) = -90.  
   145 
   146   yy = linint2_points_Wrap(xm,ym,data_mod0,True,lon_ob,lat_ob,0)
   147 
   148   delete (data_mod0)
   149   yy!0 = "field"
   150   data_mod = yy(pts|:,field|:,time|:)
   151 ; printVarSummary (data_mod)
   152 
   153 ;************************************************************
   154 ; compute correlation coef and M score
   155 ;************************************************************
   156 
   157  score_max = 5.
   158 
   159  ccr     = new ((/nstation, nfield/),float)
   160  M_score = new ((/nstation, nfield/),float) 
   161 
   162  do n=0,nstation-1
   163  do m=0,nfield-1   
   164     ccr(n,m) = esccr(data_ob(n,m,:),data_mod(n,m,:),0)
   165     bias = sum(abs(data_mod(n,m,:)-data_ob(n,m,:))/(abs(data_mod(n,m,:))+abs(data_ob(n,m,:))))
   166     M_score(n,m) = (1. -(bias/nmon)) * score_max
   167  end do
   168  end do
   169 
   170 ;*******************************************************************
   171 ; for station line plot
   172 ;*******************************************************************
   173 
   174 ; for x-axis in xyplot
   175   mon = ispan(1,12,1)
   176   mon@long_name = "month"
   177 
   178   res                   = True               ; plot mods desired
   179   res@xyLineThicknesses = (/2.0,2.0/)        ; make 2nd lines thicker
   180   res@xyLineColors      = (/"blue","red"/)   ; line color (ob,model)
   181 ;-------------------------------------------------------------------------
   182 ; Add a boxed legend using the more simple method
   183 
   184   res@pmLegendDisplayMode    = "Always"
   185 ; res@pmLegendWidthF         = 0.1
   186   res@pmLegendWidthF         = 0.08
   187   res@pmLegendHeightF        = 0.06
   188 ; res@pmLegendOrthogonalPosF = -1.17
   189 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
   190   res@pmLegendOrthogonalPosF = -0.30  ;(downward)
   191 
   192 ; res@pmLegendParallelPosF   =  0.18
   193   res@pmLegendParallelPosF   =  0.23  ;(rightward)
   194 
   195 ; res@lgPerimOn             = False
   196   res@lgLabelFontHeightF     = 0.015
   197   res@xyExplicitLegendLabels = (/"observed",model_name/)
   198 ;-------------------------------------------------------------------
   199 ; for panel plot
   200   res@gsnFrame     = False                   ; Do not draw plot 
   201   res@gsnDraw      = False                   ; Do not advance frame
   202 
   203   pres                            = True     ; panel plot mods desired
   204   pres@gsnPanelYWhiteSpacePercent = 5        ; increase white space around
   205                                              ; indiv. plots in panel
   206   pres@gsnMaximize                = True     ; fill the page
   207 ;-------------------------------------------------------------------
   208 
   209   plot_data   = new((/2,12/),float)
   210   plot_data!0 = "case"
   211   plot_data!1 = "month"
   212 
   213   do n = 0,nstation-1
   214 ;----------------------------
   215 ; for observed
   216 
   217     plot_name = station(n)+"_ob"    
   218     title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"
   219     res@tiMainString = title
   220 
   221     wks = gsn_open_wks (plot_type,plot_name)
   222     plot=new(4,graphic)                        ; create graphic array   
   223                            
   224     plot_data(0,:) = (/data_ob (n,0,:)/)
   225     plot_data@long_name = field(0)   
   226     plot(0)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 1
   227 
   228     plot_data(0,:) = (/data_ob (n,1,:)/)
   229     plot_data@long_name = field(1)
   230     plot(1)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 2
   231 
   232     plot_data(0,:) = (/data_ob (n,2,:)/)
   233     plot_data@long_name = field(2)   
   234     plot(2)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 3
   235 
   236     plot_data(0,:) = (/data_ob (n,3,:)/)
   237     plot_data@long_name = field(3)
   238     plot(3)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 4
   239 
   240     gsn_panel(wks,plot,(/2,2/),pres)                 ; create panel plot
   241 
   242     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   243            "rm "+plot_name+"."+plot_type)
   244 
   245     clear (wks)  
   246     delete (plot)
   247 ;----------------------------
   248 ; for model_vs_ob
   249 
   250     plot_name = station(n)+"_model_vs_ob"
   251     title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"    
   252     res@tiMainString = title
   253 
   254     wks = gsn_open_wks (plot_type,plot_name)
   255     plot=new(4,graphic)                        ; create graphic array   
   256                            
   257     plot_data(0,:) = (/data_ob (n,0,:)/)
   258     plot_data(1,:) = (/data_mod(n,0,:)/)
   259     plot_data@long_name = field(0)   
   260     plot(0)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 1
   261 
   262     plot_data(0,:) = (/data_ob (n,1,:)/)
   263     plot_data(1,:) = (/data_mod(n,1,:)/)
   264     plot_data@long_name = field(1)
   265     plot(1)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 2
   266 
   267     plot_data(0,:) = (/data_ob (n,2,:)/)
   268     plot_data(1,:) = (/data_mod(n,2,:)/)
   269     plot_data@long_name = field(2)   
   270     plot(2)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 3
   271 
   272     plot_data(0,:) = (/data_ob (n,3,:)/)
   273     plot_data(1,:) = (/data_mod(n,3,:)/)
   274     plot_data@long_name = field(3)
   275     plot(3)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 4
   276 
   277     gsn_panel(wks,plot,(/2,2/),pres)                 ; create panel plot
   278 
   279     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   280            "rm "+plot_name+"."+plot_type)
   281 
   282     clear (wks)  
   283     delete (plot)
   284  end do
   285 
   286 ;*******************************************************************
   287 ; for table of site score
   288 ;*******************************************************************
   289 
   290   table_length = 0.8 
   291 
   292 ; table header name
   293   table_header_name = "Site" 
   294 
   295 ; column (not including header column)
   296   col_header = (/"Latitude","Longitude","observed" \
   297                 ,"CO2 Flux","Net Radiation","Latent Heat" \
   298                 ,"Sensible Heat","Average" \
   299                 /)
   300   ncol = dimsizes(col_header) 
   301 
   302 ; row (not including header row) 
   303   nrow       = nstation + 1
   304   row_header = new ((/nrow/),string )
   305   row_header(0:nstation-1) = station(:)
   306   row_header(nrow-1)       = "All Sites" 
   307 
   308 ; Table header
   309   ncr1  = (/1,1/)               ; 1 row, 1 column
   310   x1    = (/0.005,0.15/)        ; Start and end X
   311   y1    = (/0.800,0.895/)       ; Start and end Y
   312   text1 = table_header_name
   313   res1               = True
   314   res1@txFontHeightF = 0.03
   315   res1@gsFillColor   = "CornFlowerBlue"
   316 
   317 ; Column header (equally space in x2)
   318   ncr2  = (/1,ncol/)            ; 1 rows, ncol columns
   319   x2    = (/x1(1),0.995/)       ; start from end of x1
   320   y2    = y1                    ; same as y1
   321   text2 = col_header
   322   res2               = True
   323   res2@txFontHeightF = 0.010
   324   res2@gsFillColor   = "Gray"
   325 
   326 ; Row header (equally space in y2)
   327   ncr3  = (/nrow,1/)                 ; nrow rows, 1 columns
   328   x3    = x1                         ; same as x1
   329   y3    = (/1.0-table_length,y1(0)/) ; end at start of y1
   330   text3 = row_header
   331   res3               = True
   332   res3@txFontHeightF = 0.010
   333   res3@gsFillColor   = "Gray"
   334 
   335 ; Main table body
   336   ncr4  = (/nrow,ncol/)           ; nrow rows, ncol columns
   337   x4    = x2                      ; Start and end x
   338   y4    = y3                      ; Start and end Y
   339   text4 = new((/nrow,ncol/),string)
   340 
   341   color_fill4           = new((/nrow,ncol/),string)
   342   color_fill4           = "white"
   343   color_fill4(:,ncol-1) = "grey"
   344   color_fill4(nrow-1,:) = "green"
   345 
   346   res4               = True       ; Set up resource list
   347 ; res4@gsnDebug      = True       ; Useful to print NDC row,col values used.
   348   res4@txFontHeightF = 0.015
   349   res4@gsFillColor   = color_fill4
   350 
   351   delete (color_fill4)
   352 ;-------------------------------------------------------------------
   353 ; for table value
   354 
   355   do n = 0,nrow-2
   356      text4(n,0) = sprintf("%5.2f", lat_ob(n))  
   357      text4(n,1) = sprintf("%5.2f", lon_ob(n))  
   358      text4(n,2) = year_ob(n)  
   359      text4(n,3) = sprintf("%5.2f", M_score(n,0))      ; CO2 Flux
   360      text4(n,4) = sprintf("%5.2f", M_score(n,1))      ; Net Radiation
   361      text4(n,5) = sprintf("%5.2f", M_score(n,2))      ; Latent Heat
   362      text4(n,6) = sprintf("%5.2f", M_score(n,3))      ; Sensible Heat
   363      text4(n,7) = sprintf("%5.2f", avg(M_score(n,:))) ; avg all fields    
   364   end do
   365 
   366 ;    for the last row
   367 
   368      M_co2 = avg(M_score(:,0))
   369      M_rad = avg(M_score(:,1))
   370      M_lh  = avg(M_score(:,2))
   371      M_sh  = avg(M_score(:,3))
   372      M_all = M_co2+ M_rad +M_lh + M_sh
   373      
   374      text4(nrow-1,0) = "-"
   375      text4(nrow-1,1) = "-"
   376      text4(nrow-1,2) = "-"
   377      text4(nrow-1,3) = sprintf("%5.2f", M_co2) ; avg all sites
   378      text4(nrow-1,4) = sprintf("%5.2f", M_rad) ; avg all sites
   379      text4(nrow-1,5) = sprintf("%5.2f", M_lh ) ; avg all sites
   380      text4(nrow-1,6) = sprintf("%5.2f", M_sh ) ; avg all sites
   381      text4(nrow-1,7) = sprintf("%5.2f", M_all) ; avg all sites
   382 
   383      print (M_co2)
   384      print (M_rad)
   385      print (M_lh )
   386      print (M_sh)
   387      print (M_all)
   388 
   389   M_energy_co2 = sprintf("%.2f", M_co2)
   390   system("sed s#M_energy_co2#"+M_energy_co2+"# "+html_name+" > "+html_new+";"+ \
   391          "mv -f "+html_new+" "+html_name)
   392   print (M_energy_co2)
   393  
   394   M_energy_rad = sprintf("%.2f", M_rad)
   395   system("sed s#M_energy_rad#"+M_energy_rad+"# "+html_name+" > "+html_new+";"+ \
   396          "mv -f "+html_new+" "+html_name)
   397   print (M_energy_rad)
   398  
   399   M_energy_lh = sprintf("%.2f", M_lh)
   400   system("sed s#M_energy_lh#"+M_energy_lh+"# "+html_name+" > "+html_new+";"+ \
   401          "mv -f "+html_new+" "+html_name)
   402   print (M_energy_lh)
   403  
   404   M_energy_sh = sprintf("%.2f", M_sh)
   405   system("sed s#M_energy_sh#"+M_energy_sh+"# "+html_name+" > "+html_new+";"+ \
   406          "mv -f "+html_new+" "+html_name)
   407   print (M_energy_sh) 
   408 ;---------------------------------------------------------------------------
   409  
   410   plot_name = "table_site_score"
   411  
   412   wks = gsn_open_wks (plot_type,plot_name)
   413 ;------------------------------------------
   414 ; for table title
   415 
   416   gRes               = True
   417   gRes@txFontHeightF = 0.02
   418 ; gRes@txAngleF      = 90
   419 
   420   title_text = "Model  " + model_name +  "  M_Score"
   421 
   422   gsn_text_ndc(wks,title_text,0.50,0.95,gRes)
   423 ;------------------------------------------   
   424 
   425   gsn_table(wks,ncr1,x1,y1,text1,res1)
   426   gsn_table(wks,ncr2,x2,y2,text2,res2)
   427   gsn_table(wks,ncr3,x3,y3,text3,res3)
   428   gsn_table(wks,ncr4,x4,y4,text4,res4) 
   429 
   430   frame(wks)
   431 
   432   system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
   433          "rm "+plot_name+"."+plot_type)
   434 ;-------------------------------------------------------------------
   435   temp_name = "energy." + model_name
   436   system("mkdir -p " + temp_name+";"+ \
   437          "cp "+ html_name + " " +temp_name+"/table.html"+";"+ \
   438          "mv *.png " + temp_name +";"+ \ 
   439          "tar cf "+ temp_name +".tar " + temp_name)
   440 ;------------------------------------------------------------------- 
   441 
   442 end