energy/97.all.ncl
changeset 1 4be95183fbcd
equal deleted inserted replaced
-1:000000000000 0:9159dc37794d
       
     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 = "newcn"
       
    20   film = "newcn05_ncep_1i_MONS_climo_lnd.nc"
       
    21 
       
    22 ; model_name = "b30.061n"
       
    23 ; film = "b30.061n_1995-2004_MONS_climo_lnd.nc"
       
    24 ;--------------------------------------------
       
    25   dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
       
    26   fm    = addfile(dirm+film,"r")
       
    27 
       
    28   xm    = fm->lon
       
    29   ym    = fm->lat
       
    30   nlat = dimsizes(ym)
       
    31   nlon = dimsizes(xm)
       
    32 
       
    33   nmon      = 12
       
    34   nfield    = 4
       
    35 
       
    36   data_mod0 = new ((/nfield,nmon,nlat,nlon/),float)
       
    37 
       
    38 ; change to unit of observed (u mol/m2/s)
       
    39 ; Model_units [=] gC/m2/s
       
    40 ; 12. = molecular weight of C
       
    41 ; u mol = 1e-6 mol 
       
    42   data = fm->NEE
       
    43 
       
    44   factor = 1e6/12.
       
    45 
       
    46   data_mod0(0,:,:,:) = data(:,:,:) * factor
       
    47   delete (data)
       
    48 ; data  = fm->LATENT
       
    49   data1 = fm->FCEV
       
    50   data2 = fm->FCTR
       
    51   data3 = fm->FGEV
       
    52   data_mod0(2,:,:,:) = data1(:,:,:)+data2(:,:,:)+data3(:,:,:) 
       
    53   delete (data1)
       
    54   delete (data2)
       
    55   delete (data3)
       
    56 
       
    57   data  = fm->FSH
       
    58   data_mod0(3,:,:,:) = data(:,:,:) 
       
    59   delete (data)
       
    60 
       
    61 ; data  = fm->NETRAD
       
    62   data1 = fm->FSA
       
    63   data2 = fm->FIRA
       
    64   data_mod0(1,:,:,:) = data1(:,:,:)-data2(:,:,:)-data_mod0(2,:,:,:)-data_mod0(3,:,:,:) 
       
    65   delete (data1)
       
    66   delete (data2)
       
    67 
       
    68 ; printVarSummary (data_mod0)
       
    69 ;************************************************
       
    70 ; read in data: observed
       
    71 ;************************************************
       
    72  station = (/"BOREAS_NSA_OBS" \
       
    73             ,"CastelPorziano" \
       
    74             ,"Hyytiala" \
       
    75             ,"Kaamanen" \
       
    76             ,"LBA_Tapajos_KM67" \
       
    77             ,"Lethbridge" \
       
    78             ,"Tharandt" \
       
    79             ,"Vielsalm" \
       
    80             /)
       
    81 
       
    82  year_ob = (/"1994-2004" \
       
    83             ,"1997-2003" \
       
    84             ,"1996-2003" \
       
    85             ,"2000-2003" \
       
    86             ,"2002-2005" \
       
    87             ,"1999-2004" \
       
    88             ,"1996-2003" \
       
    89             ,"1998-2003" \
       
    90             /)
       
    91 
       
    92  field   = (/"CO2 Flux" \
       
    93             ,"Net Radiation" \
       
    94             ,"Latent Heat" \
       
    95             ,"Sensible Heat" \
       
    96             /)
       
    97 
       
    98  nstation  = dimsizes(station)
       
    99  nmon      = 12
       
   100  nfield    = dimsizes(field)
       
   101 
       
   102  data_ob   = new ((/nstation, nfield, nmon/),float)
       
   103  lat_ob    = new ((/nstation/),float)
       
   104  lon_ob    = new ((/nstation/),float)
       
   105 
       
   106  diri_root  = "/fis/cgd/cseg/people/jeff/clamp_data/fluxnet/"
       
   107 
       
   108  do n = 0,nstation-1
       
   109     diri = diri_root + station(n)+"/"
       
   110     fili = station(n)+"_"+year_ob(n)+"_monthly.nc"
       
   111     g     = addfile (diri+fili,"r")
       
   112  
       
   113     lon_ob(n) = g->lon 
       
   114     lat_ob(n) = g->lat
       
   115 
       
   116     data      = g->CO2_FLUX
       
   117     data_ob(n,0,:) = dim_avg(data(month|:,year|:))
       
   118     delete (data)
       
   119 
       
   120     data      = g->RAD_FLUX
       
   121     data_ob(n,1,:) = dim_avg(data(month|:,year|:))
       
   122     delete (data)
       
   123 
       
   124     data      = g->LH_FLUX
       
   125     data_ob(n,2,:) = dim_avg(data(month|:,year|:))
       
   126     delete (data)
       
   127 
       
   128     data      = g->SH_FLUX
       
   129     data_ob(n,3,:) = dim_avg(data(month|:,year|:))
       
   130     delete (data)
       
   131 
       
   132     delete (g)
       
   133  end do
       
   134  
       
   135 ;print (lat_ob)
       
   136 ;print (lon_ob)
       
   137 ;printVarSummary (data_ob)
       
   138 
       
   139 ;************************************************************
       
   140 ; interpolate model data into observed station
       
   141 ; note: model is 0-360E, 90S-90N
       
   142 ;************************************************************
       
   143 
       
   144 ; to be able to handle observation at (-89.98,-24.80)
       
   145 ; print (ym(0))
       
   146   ym(0) = -90.  
       
   147 
       
   148   yy = linint2_points_Wrap(xm,ym,data_mod0,True,lon_ob,lat_ob,0)
       
   149 
       
   150   delete (data_mod0)
       
   151   yy!0 = "field"
       
   152   data_mod = yy(pts|:,field|:,time|:)
       
   153 ; printVarSummary (data_mod)
       
   154 
       
   155 ;************************************************************
       
   156 ; compute correlation coef and M score
       
   157 ;************************************************************
       
   158 
       
   159  score_max = 5.
       
   160 
       
   161  ccr     = new ((/nstation, nfield/),float)
       
   162  M_score = new ((/nstation, nfield/),float) 
       
   163 
       
   164  do n=0,nstation-1
       
   165  do m=0,nfield-1   
       
   166     ccr(n,m) = esccr(data_ob(n,m,:),data_mod(n,m,:),0)
       
   167     bias = sum(abs(data_mod(n,m,:)-data_ob(n,m,:))/(abs(data_mod(n,m,:))+abs(data_ob(n,m,:))))
       
   168     M_score(n,m) = (1. -(bias/nmon)) * score_max
       
   169  end do
       
   170  end do
       
   171 
       
   172 ;*******************************************************************
       
   173 ; for station line plot
       
   174 ;*******************************************************************
       
   175 
       
   176 ; for x-axis in xyplot
       
   177   mon = ispan(1,12,1)
       
   178   mon@long_name = "month"
       
   179 
       
   180   res                   = True               ; plot mods desired
       
   181   res@xyLineThicknesses = (/2.0,2.0/)        ; make 2nd lines thicker
       
   182   res@xyLineColors      = (/"blue","red"/)   ; line color (ob,model)
       
   183 ;-------------------------------------------------------------------------
       
   184 ; Add a boxed legend using the more simple method
       
   185 
       
   186   res@pmLegendDisplayMode    = "Always"
       
   187 ; res@pmLegendWidthF         = 0.1
       
   188   res@pmLegendWidthF         = 0.08
       
   189   res@pmLegendHeightF        = 0.06
       
   190 ; res@pmLegendOrthogonalPosF = -1.17
       
   191 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
       
   192   res@pmLegendOrthogonalPosF = -0.30  ;(downward)
       
   193 
       
   194 ; res@pmLegendParallelPosF   =  0.18
       
   195   res@pmLegendParallelPosF   =  0.23  ;(rightward)
       
   196 
       
   197 ; res@lgPerimOn             = False
       
   198   res@lgLabelFontHeightF     = 0.015
       
   199   res@xyExplicitLegendLabels = (/"observed",model_name/)
       
   200 ;-------------------------------------------------------------------
       
   201 ; for panel plot
       
   202   res@gsnFrame     = False                   ; Do not draw plot 
       
   203   res@gsnDraw      = False                   ; Do not advance frame
       
   204 
       
   205   pres                            = True     ; panel plot mods desired
       
   206   pres@gsnPanelYWhiteSpacePercent = 5        ; increase white space around
       
   207                                              ; indiv. plots in panel
       
   208   pres@gsnMaximize                = True     ; fill the page
       
   209 ;-------------------------------------------------------------------
       
   210 
       
   211   plot_data   = new((/2,12/),float)
       
   212   plot_data!0 = "case"
       
   213   plot_data!1 = "month"
       
   214 
       
   215   do n = 0,nstation-1
       
   216 ;----------------------------
       
   217 ; for observed
       
   218 
       
   219     plot_name = station(n)+"_ob"    
       
   220     title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"
       
   221     res@tiMainString = title
       
   222 
       
   223     wks = gsn_open_wks (plot_type,plot_name)
       
   224     plot=new(4,graphic)                        ; create graphic array   
       
   225                            
       
   226     plot_data(0,:) = (/data_ob (n,0,:)/)
       
   227     plot_data@long_name = field(0)   
       
   228     plot(0)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 1
       
   229 
       
   230     plot_data(0,:) = (/data_ob (n,1,:)/)
       
   231     plot_data@long_name = field(1)
       
   232     plot(1)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 2
       
   233 
       
   234     plot_data(0,:) = (/data_ob (n,2,:)/)
       
   235     plot_data@long_name = field(2)   
       
   236     plot(2)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 3
       
   237 
       
   238     plot_data(0,:) = (/data_ob (n,3,:)/)
       
   239     plot_data@long_name = field(3)
       
   240     plot(3)=gsn_csm_xy(wks,mon,plot_data(0,:),res)   ; create plot 4
       
   241 
       
   242     gsn_panel(wks,plot,(/2,2/),pres)                 ; create panel plot
       
   243 
       
   244     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
       
   245     system("rm "+plot_name+"."+plot_type)
       
   246     clear (wks)  
       
   247     delete (plot)
       
   248 ;----------------------------
       
   249 ; for model_vs_ob
       
   250 
       
   251     plot_name = station(n)+"_model_vs_ob"
       
   252     title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"    
       
   253     res@tiMainString = title
       
   254 
       
   255     wks = gsn_open_wks (plot_type,plot_name)
       
   256     plot=new(4,graphic)                        ; create graphic array   
       
   257                            
       
   258     plot_data(0,:) = (/data_ob (n,0,:)/)
       
   259     plot_data(1,:) = (/data_mod(n,0,:)/)
       
   260     plot_data@long_name = field(0)   
       
   261     plot(0)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 1
       
   262 
       
   263     plot_data(0,:) = (/data_ob (n,1,:)/)
       
   264     plot_data(1,:) = (/data_mod(n,1,:)/)
       
   265     plot_data@long_name = field(1)
       
   266     plot(1)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 2
       
   267 
       
   268     plot_data(0,:) = (/data_ob (n,2,:)/)
       
   269     plot_data(1,:) = (/data_mod(n,2,:)/)
       
   270     plot_data@long_name = field(2)   
       
   271     plot(2)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 3
       
   272 
       
   273     plot_data(0,:) = (/data_ob (n,3,:)/)
       
   274     plot_data(1,:) = (/data_mod(n,3,:)/)
       
   275     plot_data@long_name = field(3)
       
   276     plot(3)=gsn_csm_xy(wks,mon,plot_data,res)   ; create plot 4
       
   277 
       
   278     gsn_panel(wks,plot,(/2,2/),pres)                 ; create panel plot
       
   279 
       
   280     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
       
   281     system("rm "+plot_name+"."+plot_type)
       
   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  
       
   390   plot_name = "table_site_score"
       
   391  
       
   392   wks = gsn_open_wks (plot_type,plot_name)
       
   393 ;------------------------------------------
       
   394 ; for table title
       
   395 
       
   396   gRes               = True
       
   397   gRes@txFontHeightF = 0.02
       
   398 ; gRes@txAngleF      = 90
       
   399 
       
   400   title_text = "Model  " + model_name +  "  M_Score"
       
   401 
       
   402   gsn_text_ndc(wks,title_text,0.50,0.95,gRes)
       
   403 ;------------------------------------------   
       
   404 
       
   405   gsn_table(wks,ncr1,x1,y1,text1,res1)
       
   406   gsn_table(wks,ncr2,x2,y2,text2,res2)
       
   407   gsn_table(wks,ncr3,x3,y3,text3,res3)
       
   408   gsn_table(wks,ncr4,x4,y4,text4,res4) 
       
   409 
       
   410   frame(wks)
       
   411 
       
   412   system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
       
   413   system("rm "+plot_name+"."+plot_type)
       
   414 ;-------------------------------------------------------------------
       
   415   temp_name = "temp." + model_name
       
   416   system("mkdir -p " + temp_name)
       
   417   system("mv *.png " + temp_name)
       
   418   system("tar cf "+ temp_name +".tar " + temp_name)
       
   419 ;------------------------------------------------------------------- 
       
   420 
       
   421 end