co2/42.metric+lines.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 ; combine 19.metric_plot.ncl and 24.lines.ncl
     3 ; ***********************************************
     4 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl.test"
     5 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl.test"
     6 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
     7 ;load "/fis/cgd/cseg/people/jeff/clamp/co2/metrics_table.ncl"
     8 ;************************************************
     9 begin
    10 ;************************************************
    11 ; read in data: observed
    12 ;************************************************
    13  diri  = "/fis/cgd/cseg/people/jeff/clamp_data/co2/"
    14  fili  = "co2_globalView_98.nc"
    15  g     = addfile (diri+fili,"r")
    16  val   = g->CO2_SEAS  
    17  lon   = g->LON 
    18  lat   = g->LAT
    19  sta   = chartostring(g->STATION) 
    20  delete (g)
    21  
    22 ;print (sta(0))
    23 
    24  ncase = dimsizes(lat)
    25 ;print (ncase)
    26 
    27 ;**************************************************************
    28 ; get only the lowest level at each station 
    29 ;**************************************************************
    30  lat_tmp = lat
    31  lat_tmp@_FillValue = 1.e+36
    32  
    33  do n = 0,ncase-1
    34     if (.not. ismissing(lat_tmp(n))) then 
    35        indexes = ind(lat(n) .eq. lat .and. lon(n) .eq. lon)
    36        if (dimsizes(indexes) .gt. 1) then
    37           lat_tmp(indexes(1:)) = lat_tmp@_FillValue
    38        end if
    39        delete (indexes)
    40     end if
    41  end do
    42 
    43  indexes = ind(.not. ismissing(lat_tmp))
    44 ;print (dimsizes(indexes))
    45 ;print (indexes)
    46  
    47  lat_ob = lat(indexes)
    48  lon_ob = lon(indexes)
    49  val_ob = val(indexes,:)
    50 ;printVarSummary (val_ob)
    51 ;print (lat_ob +"/"+lon_ob)
    52 
    53 ;************************************************
    54 ; read in model data
    55 ;************************************************
    56   diri2 = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    57 ; fili2 = "b30.061m_401_425_MONS_climo_atm.nc"
    58   fili2 = "b30.061n_1995-2004_MONS_climo_atm.nc"
    59 
    60   g     = addfile(diri2+fili2,"r")
    61   x     = g->CO2
    62   xi    = g->lon
    63   yi    = g->lat
    64   xdim  = dimsizes(x)
    65   nlev  = xdim(1)
    66   y     = x(:,0,:,:)
    67 ; printVarSummary (y)
    68   
    69 ; get the co2 at the lowest level
    70   y     = x(:,nlev-1,:,:)
    71 
    72 ; change to unit of observed (u mol/mol)
    73 ; Model_units [=] kgCO2 / kgDryAir
    74 ; 28.966 = molecular weight of dry air
    75 ; 44.       = molecular weight of CO2
    76 ; u mol = 1e-6 mol
    77 
    78   factor = (28.966/44.) * 1e6
    79   y      = y * factor
    80 
    81   y@_FillValue = 1.e36
    82   y@units      = "u mol/mol"
    83 ; y = where(y0 .lt. 287.,y@_FillValue,y)
    84 ; printVarSummary (y)
    85 ; print (min(y)+"/"+max(y))
    86 
    87 ; interpolate into observed station
    88 ; note: model is 0-360E, 90S-90N
    89 
    90 ; to be able to handle observation at (-89.98,-24.80)
    91   print (yi(0))
    92   yi(0) = -90.
    93 
    94   i = ind(lon_ob .lt. 0.)
    95   lon_ob(i) = lon_ob(i) + 360.  
    96 
    97   yo = linint2_points_Wrap(xi,yi,y,True,lon_ob,lat_ob,0)
    98 
    99   val_model = yo(pts|:,time|:)
   100   val_model_0 = val_model
   101 ; printVarSummary (val_model)
   102 ; print (min(val_model)+"/"+max(val_model))
   103 
   104 ; remove annual mean
   105   val_model = val_model - conform(val_model,dim_avg(val_model),0)
   106 ; print (min(val_model)+"/"+max(val_model))
   107 
   108   nzone = 4
   109 ;--------------------------------------------------------------
   110 ; for zone table 
   111 ;**********************************
   112 ; table header name
   113   table_header_name = "Zone" 
   114 
   115 ; column (not including header column)
   116   col_header_zone = (/"Stations","Amplitude Ratio", \
   117                       "Correlation Coef","M score","Combined Score"/)
   118   ncol_zone       = dimsizes(col_header_zone ) 
   119 
   120 ; row (not including header row)
   121   row_header_zone = (/"60N-90N","30N-60N","EQ-30N","90S-EQ","Total"/)  
   122   nrow_zone       = dimsizes(row_header_zone)                  
   123 
   124 ; arrays to be passed to table. 
   125   value_zone = new ((/nrow_zone, ncol_zone/),string ) 
   126 ;**********************************
   127 
   128   table_length_zone = 0.4 
   129 
   130 ; Table header
   131   ncr1  = (/1,1/)               ; 1 row, 1 column
   132   x1    = (/0.005,0.15/)        ; Start and end X
   133   y1    = (/0.900,0.995/)       ; Start and end Y
   134   text1 = table_header_name
   135   res1               = True
   136   res1@txFontHeightF = 0.03
   137   res1@gsFillColor   = "CornFlowerBlue"
   138 
   139 ; Column header (equally space in x2)
   140   ncr2  = (/1,ncol_zone/)         ; 1 rows, 5 columns
   141   x2    = (/x1(1),0.995/)          ; start from end of x1
   142   y2    = y1                      ; same as y1
   143   text2 = col_header_zone
   144   res2               = True
   145   res2@txFontHeightF = 0.015
   146   res2@gsFillColor   = "Gray"
   147 
   148 ; Row header (equally space in y2)
   149   ncr3  = (/nrow_zone,1/)         ; 5 rows, 1 columns
   150   x3    = x1                      ; same as x1
   151   y3    = (/1.0-table_length_zone,0.900/) ; end at start of y1
   152   text3 = row_header_zone
   153   res3               = True
   154   res3@txFontHeightF = 0.02
   155   res3@gsFillColor   = "Gray"
   156 
   157 ; Main table body
   158   ncr4  = (/nrow_zone,ncol_zone/) ; 5 rows, 5 columns
   159   x4    = x2                      ; Start and end x
   160   y4    = y3                      ; Start and end Y
   161   text4 = new((/nrow_zone,ncol_zone/),string)
   162 
   163   color_fill4      = new((/nrow_zone,ncol_zone/),string)
   164   color_fill4      = "white"
   165   color_fill4(:,ncol_zone-1) = "grey"
   166 
   167   res4               = True       ; Set up resource list
   168 ; res4@gsnDebug      = True       ; Useful to print NDC row,col values used.
   169   res4@txFontHeightF = 0.02
   170   res4@gsFillColor   = color_fill4
   171 
   172   delete (color_fill4)
   173 ;--------------------------------------------------------------
   174 ; for station line plot
   175 
   176 ; for x-axis in xyplot
   177   mon = ispan(1,12,1)
   178   mon@long_name = "month"
   179 
   180   plot_type = "ps"
   181   plot_type_new = "png"
   182 
   183   res                   = True                      ; plot mods desired
   184   res@xyLineThicknesses = (/2.0,2.0,2.0/)           ; make 2nd lines thicker
   185   res@xyLineColors      = (/"red","black"/)  ; change line color
   186 
   187 ; Add a boxed legend using the more simple method
   188 
   189   res@pmLegendDisplayMode    = "Always"
   190 ; res@pmLegendWidthF         = 0.1
   191   res@pmLegendWidthF         = 0.08
   192   res@pmLegendHeightF        = 0.06
   193 ; res@pmLegendOrthogonalPosF = -1.17
   194 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
   195   res@pmLegendOrthogonalPosF = -0.30  ;(downward)
   196 
   197 ; res@pmLegendParallelPosF   =  0.18
   198   res@pmLegendParallelPosF   =  0.23  ;(rightward)
   199 
   200 ; res@lgPerimOn             = False
   201   res@lgLabelFontHeightF     = 0.015
   202   res@xyExplicitLegendLabels = (/"b30.061n","observed"/)
   203 ;-------------------------------------------------------------------
   204 
   205   do z = 0,nzone-1
   206 
   207   if (z .eq. 0) then 
   208 ;    maximum score for the zone, 60N-90N
   209      zone = "60N-90N" 
   210      score_max = 5.0
   211 ;    index of stations in this zone
   212      ind_z = ind(lat_ob .ge. 60.)
   213 ;    print (ind_z)
   214 ;    print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
   215 ;    print (val_ob(ind_z,:))
   216 ;    print (val_model(ind_z,:))
   217   end if
   218 
   219   if (z .eq. 1) then 
   220 ;    maximum score for the zone, 30N-60N
   221      zone = "30N-60N" 
   222      score_max = 5.0
   223 ;    index of stations in this zone
   224      ind_z = ind(lat_ob .ge. 30. .and. lat_ob .lt. 60.)
   225 ;    print (ind_z)
   226 ;    print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
   227 ;    print (val_ob(ind_z,:))
   228 ;    print (val_model(ind_z,:))
   229   end if
   230 
   231   if (z .eq. 2) then 
   232 ;    maximum score for the zone, EQ-30N 
   233      zone = "EQ-30N"
   234      score_max = 5.0
   235 ;    index of stations in this zone
   236      ind_z = ind(lat_ob .ge. 0. .and. lat_ob .lt. 30.)
   237 ;    print (ind_z)
   238 ;    print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
   239 ;    print (val_ob(ind_z,:))
   240 ;    print (val_model(ind_z,:))
   241   end if
   242 
   243   if (z .eq. 3) then 
   244 ;    maximum score for the zone, 90S-EQ
   245      zone = "90S-EQ" 
   246      score_max = 5.0
   247 ;    index of stations in this zone
   248      ind_z = ind(lat_ob .lt. 0. )
   249 ;    print (ind_z)
   250 ;    print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
   251 ;    print (val_ob(ind_z,:))
   252 ;    print (val_model(ind_z,:))
   253   end if
   254 
   255  npts = dimsizes(ind_z)
   256  print (npts)
   257 ;-------------------------------------------------------------------------
   258 ; for metric table 
   259 
   260  amp_ob        = new((/npts/),float)
   261  amp_model     = new((/npts/),float)
   262 
   263  amp_ratio_sta = new((/npts/),float)
   264  ccr_sta       = new((/npts/),float)
   265  M_sta         = new((/npts/),float)
   266  score_sta     = new((/npts/),float)
   267 ;-------------------------------------------------------------------------
   268 ; for station line plot
   269 
   270   npts_str = ""
   271   npts_str = npts
   272 ; print (npts_str)
   273 
   274   plot_data   = new((/2,12,npts/),float)
   275   plot_data_0 = new((/12,npts/),float)
   276 
   277   plot_data!0 = "case"
   278   plot_data!1 = "month"
   279   plot_data!2 = "pts"
   280   plot_data@long_name   = "CO2 Seasonal"
   281 
   282   plot_data_0!0 = "month"
   283   plot_data_0!1 = "pts"
   284   plot_data_0@long_name = "CO2"
   285 ;--------------------------------------------------------------------------
   286  do n=0,npts-1
   287     amp_ob(n)    = max(val_ob(ind_z(n),:)) - min(val_ob(ind_z(n),:)) 
   288     amp_model(n) = max(val_model(ind_z(n),:)) - min(val_model(ind_z(n),:))
   289 
   290     amp_ratio_sta(n) = amp_model(n)/amp_ob(n)
   291     ccr_sta(n) = esccr(val_ob(ind_z(n),:),val_model(ind_z(n),:),0)
   292     M_sta(n) = 1.-abs(amp_ratio_sta(n)-1.)
   293     score_sta(n) = (ccr_sta(n)*ccr_sta(n) + M_sta(n))*0.5 * score_max
   294 
   295     print (sta(ind_z(n))+"/"+lat(ind_z(n))+"/"+lon(ind_z(n))+"/"+amp_ratio_sta(n)+"/"+ccr_sta(n)+"/"+M_sta(n)+"/"+score_sta(n))
   296 ;----------------------------------------------------------------------
   297 ; for station line plot
   298 
   299     plot_data(0,:,n) = (/val_model(ind_z(n),:)/)
   300     plot_data(1,:,n) = (/val_ob(ind_z(n),:)/)
   301 
   302     plot_data_0(:,n) = (/val_model_0(ind_z(n),:)/)
   303    
   304     plot_name = sta(ind_z(n))    
   305     title = plot_name+"("+lat(ind_z(n))+","+lon(ind_z(n))+")"
   306 ;   print (title)
   307 ;   print (plot_name)
   308 
   309     wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
   310 ;------------------------------------------
   311 ;   for panel plot
   312    
   313     plot=new(2,graphic)                        ; create graphic array
   314     res@gsnFrame     = False                   ; Do not draw plot 
   315     res@gsnDraw      = False                   ; Do not advance frame
   316 
   317     pres                            = True     ; panel plot mods desired
   318     pres@gsnPanelYWhiteSpacePercent = 5        ; increase white space around
   319                                                ; indiv. plots in panel
   320     pres@gsnMaximize                = True     ; fill the page
   321 ;------------------------------------------
   322     res@tiMainString = title                           ; add title
   323 
   324     plot(0)=gsn_csm_xy(wks,mon,plot_data(:,:,n),res)   ; create plot 1
   325 
   326     plot(1)=gsn_csm_xy(wks,mon,plot_data_0(:,n),res) ; create plot 2
   327 
   328     gsn_panel(wks,plot,(/2,1/),pres)                 ; create panel plot
   329 
   330     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
   331     system("rm "+plot_name+"."+plot_type)
   332     clear (wks)
   333 ;---------------------------------------------------------------------------  
   334  end do
   335 ;-------------------------------------------------------------------------
   336 ;   for line plot in a zone
   337 
   338     plot_name = "All_"+npts_str
   339     title = plot_name + " in "+ zone
   340 ;   print (title)
   341 ;   print (plot_name)
   342 
   343     wks = gsn_open_wks (plot_type,plot_name)        ; open workstation
   344 ;-----------------------------------------
   345 ;   for panel plot
   346    
   347     plot=new(2,graphic)                        ; create graphic array
   348     res@gsnFrame     = False                   ; Do not draw plot 
   349     res@gsnDraw      = False                   ; Do not advance frame
   350 
   351     pres                            = True     ; panel plot mods desired
   352     pres@gsnPanelYWhiteSpacePercent = 5        ; increase white space around
   353                                                ; indiv. plots in panel
   354     pres@gsnMaximize                = True     ; fill the page
   355 ;-----------------------------------------
   356     res@tiMainString = title                                     ; add title
   357 
   358     plot(0) = gsn_csm_xy (wks,mon,dim_avg_Wrap(plot_data),res)   ; create plot 1
   359     
   360     plot(1) = gsn_csm_xy (wks,mon,dim_avg_Wrap(plot_data_0),res) ; create plot 2
   361 
   362     gsn_panel(wks,plot,(/2,1/),pres)                 ; create panel plot
   363 
   364     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
   365     system("rm "+plot_name+"."+plot_type)
   366 
   367     clear (wks)
   368 ;   delete (ind_z)
   369     delete (plot_data)
   370     delete (plot_data_0)    
   371 ;---------------------------------------------------------------------------
   372 ; for zone table 
   373 
   374  amp_ratio_zone = avg(amp_ratio_sta)
   375  ccr_zone       = avg(ccr_sta)
   376  M_zone   = 1.- (sum(abs(amp_model-amp_ob)/(amp_model+amp_ob))/npts) 
   377  score_zone   = (ccr_zone*ccr_zone + M_zone)*0.5 * score_max
   378 
   379  print (npts+"/"+amp_ratio_zone+"/"+ccr_zone+"/"+M_zone+"/"+score_zone)
   380 
   381   text4(z,0) = sprintf("%5.2f", npts)
   382   text4(z,1) = sprintf("%5.2f", amp_ratio_zone)
   383   text4(z,2) = sprintf("%5.2f", ccr_zone)
   384   text4(z,3) = sprintf("%5.2f", M_zone)
   385   text4(z,4) = sprintf("%5.2f", score_zone)  
   386 ;---------------------------------------------------------------------------
   387 ; column for station table
   388   case_sta   = (/"Latitude","Longitude","Amplitude Ratio", \
   389                  "Correlation Coef","M score","Combined Score"/) 
   390   nCase_sta  = dimsizes(case_sta )                 
   391 
   392 ; row for station table
   393   var_sta  = sta(ind_z) 
   394   nVar_sta = dimsizes(var_sta)                 
   395 
   396 ; arrays to be passed to diagram. 
   397   case_value_sta = new ((/nCase_sta, nVar_sta/),float )  
   398 
   399   case_value_sta(0,:) = (/lat(ind_z)/)
   400   case_value_sta(1,:) = (/lon(ind_z)/)
   401   case_value_sta(2,:) = (/amp_ratio_sta/)
   402   case_value_sta(3,:) = (/ccr_sta/)
   403   case_value_sta(4,:) = (/M_sta/)
   404   case_value_sta(5,:) = (/score_sta/)
   405  
   406 ;**************************************************
   407 ; plot station table
   408 ;**************************************************
   409   tt_opt        = True
   410   tt_opt@pltType= "png" ; "eps" [default], "pdf", "ps"
   411                         ; "png", "gif" [if you have ImageMajik 'convert']
   412 
   413   tt_opt@tableTitle = "Zone "+ zone
   414   plot_name         = "table_sta." + zone
   415 
   416 ; metrics_table(plot_name, var_sta, case_sta , case_value_sta, tt_opt)
   417 
   418  delete (ind_z)
   419  delete (amp_model)
   420  delete (amp_ob)
   421  delete (amp_ratio_sta)
   422  delete (ccr_sta)
   423  delete (M_sta)
   424  delete (score_sta)
   425  delete (var_sta)
   426  delete (case_value_sta)
   427  end do
   428 ;**************************************************
   429 ; plot zone table
   430 ;**************************************************
   431  
   432   text4(4,0) = sum(stringtofloat(text4(0:3,0))) 
   433   text4(4,1) = 0.
   434   text4(4,2) = 0.
   435   text4(4,3) = 0.
   436   text4(4,4) = sum(stringtofloat(text4(0:3,4)))
   437 
   438   plot_name = "table_zone" 
   439   wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
   440 
   441   gsn_table(wks,ncr1,x1,y1,text1,res1)
   442   gsn_table(wks,ncr2,x2,y2,text2,res2)
   443   gsn_table(wks,ncr3,x3,y3,text3,res3)
   444   gsn_table(wks,ncr4,x4,y4,text4,res4) 
   445 
   446   frame(wks)
   447 
   448 end