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