co2/41.metric+lines.ncl
author Forrest Hoffman <forrest@climatemodeling.org>
Thu, 26 Mar 2009 14:02:21 -0400
changeset 1 4be95183fbcd
permissions -rw-r--r--
Modifications to scoring and graphics production for the final version of code for the C-LAMP paper in GCB.
     1 ; ***********************************************
     2 ; combine 19.metric_plot.ncl and 24.lines.ncl
     3 ; ***********************************************
     4 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
     5 load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
     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 metric table plots
   111 ; column
   112   case_zone = (/"Stations","Amplitude Ratio", \
   113                 "Correlation Coef","M score","Combined Score"/)
   114   nCase_zone = dimsizes(case_zone ) 
   115 
   116 ; row
   117   var_zone = (/"60N-90N","30N-60N","EQ-30N","90S-EQ","Total"/)  
   118   nVar_zone = dimsizes(var_zone)                  
   119 
   120 ; arrays to be passed to diagram. 
   121   case_value_zone = new ((/nCase_zone, nVar_zone/),float )  
   122 ;--------------------------------------------------------------
   123 ; for station line plot
   124 
   125 ; for x-axis in xyplot
   126   mon = ispan(1,12,1)
   127   mon@long_name = "month"
   128 
   129   plot_type = "ps"
   130   plot_type_new = "png"
   131 
   132   res                   = True                      ; plot mods desired
   133   res@xyLineThicknesses = (/2.0,2.0,2.0/)           ; make 2nd lines thicker
   134   res@xyLineColors      = (/"red","black"/)  ; change line color
   135 
   136 ; Add a boxed legend using the more simple method
   137 
   138   res@pmLegendDisplayMode    = "Always"
   139 ; res@pmLegendWidthF         = 0.1
   140   res@pmLegendWidthF         = 0.08
   141   res@pmLegendHeightF        = 0.06
   142 ; res@pmLegendOrthogonalPosF = -1.17
   143 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
   144   res@pmLegendOrthogonalPosF = -0.30  ;(downward)
   145 
   146 ; res@pmLegendParallelPosF   =  0.18
   147   res@pmLegendParallelPosF   =  0.23  ;(rightward)
   148 
   149 ; res@lgPerimOn             = False
   150   res@lgLabelFontHeightF     = 0.015
   151   res@xyExplicitLegendLabels = (/"b30.061n","observed"/)
   152 ;-------------------------------------------------------------------
   153 
   154   do z = 0,nzone-1
   155 
   156   if (z .eq. 0) then 
   157 ;    maximum score for the zone, 60N-90N
   158      zone = "60N-90N" 
   159      score_max = 5.0
   160 ;    index of stations in this zone
   161      ind_z = ind(lat_ob .ge. 60.)
   162 ;    print (ind_z)
   163 ;    print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
   164 ;    print (val_ob(ind_z,:))
   165 ;    print (val_model(ind_z,:))
   166   end if
   167 
   168   if (z .eq. 1) then 
   169 ;    maximum score for the zone, 30N-60N
   170      zone = "30N-60N" 
   171      score_max = 5.0
   172 ;    index of stations in this zone
   173      ind_z = ind(lat_ob .ge. 30. .and. lat_ob .lt. 60.)
   174 ;    print (ind_z)
   175 ;    print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
   176 ;    print (val_ob(ind_z,:))
   177 ;    print (val_model(ind_z,:))
   178   end if
   179 
   180   if (z .eq. 2) then 
   181 ;    maximum score for the zone, EQ-30N 
   182      zone = "EQ-30N"
   183      score_max = 5.0
   184 ;    index of stations in this zone
   185      ind_z = ind(lat_ob .ge. 0. .and. lat_ob .lt. 30.)
   186 ;    print (ind_z)
   187 ;    print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
   188 ;    print (val_ob(ind_z,:))
   189 ;    print (val_model(ind_z,:))
   190   end if
   191 
   192   if (z .eq. 3) then 
   193 ;    maximum score for the zone, 90S-EQ
   194      zone = "90S-EQ" 
   195      score_max = 5.0
   196 ;    index of stations in this zone
   197      ind_z = ind(lat_ob .lt. 0. )
   198 ;    print (ind_z)
   199 ;    print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
   200 ;    print (val_ob(ind_z,:))
   201 ;    print (val_model(ind_z,:))
   202   end if
   203 
   204  npts = dimsizes(ind_z)
   205  print (npts)
   206 ;-------------------------------------------------------------------------
   207 ; for metric table plot
   208 
   209  amp_ob        = new((/npts/),float)
   210  amp_model     = new((/npts/),float)
   211 
   212  amp_ratio_sta = new((/npts/),float)
   213  ccr_sta       = new((/npts/),float)
   214  M_sta         = new((/npts/),float)
   215  score_sta     = new((/npts/),float)
   216 ;-------------------------------------------------------------------------
   217 ; for station line plot
   218 
   219   npts_str = ""
   220   npts_str = npts
   221 ; print (npts_str)
   222 
   223   plot_data   = new((/2,12,npts/),float)
   224   plot_data_0 = new((/12,npts/),float)
   225 
   226   plot_data!0 = "case"
   227   plot_data!1 = "month"
   228   plot_data!2 = "pts"
   229   plot_data@long_name   = "CO2 Seasonal"
   230 
   231   plot_data_0!0 = "month"
   232   plot_data_0!1 = "pts"
   233   plot_data_0@long_name = "CO2"
   234 ;--------------------------------------------------------------------------
   235  do n=0,npts-1
   236     amp_ob(n)    = max(val_ob(ind_z(n),:)) - min(val_ob(ind_z(n),:)) 
   237     amp_model(n) = max(val_model(ind_z(n),:)) - min(val_model(ind_z(n),:))
   238 
   239     amp_ratio_sta(n) = amp_model(n)/amp_ob(n)
   240     ccr_sta(n) = esccr(val_ob(ind_z(n),:),val_model(ind_z(n),:),0)
   241     M_sta(n) = 1.-abs(amp_ratio_sta(n)-1.)
   242     score_sta(n) = (ccr_sta(n)*ccr_sta(n) + M_sta(n))*0.5 * score_max
   243 
   244     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))
   245 ;----------------------------------------------------------------------
   246 ; for station line plot
   247 
   248     plot_data(0,:,n) = (/val_model(ind_z(n),:)/)
   249     plot_data(1,:,n) = (/val_ob(ind_z(n),:)/)
   250 
   251     plot_data_0(:,n) = (/val_model_0(ind_z(n),:)/)
   252    
   253     plot_name = sta(ind_z(n))    
   254     title = plot_name+"("+lat(ind_z(n))+","+lon(ind_z(n))+")"
   255 ;   print (title)
   256 ;   print (plot_name)
   257 
   258     wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
   259 ;------------------------------------------
   260 ;   for panel plot
   261    
   262     plot=new(2,graphic)                        ; create graphic array
   263     res@gsnFrame     = False                   ; Do not draw plot 
   264     res@gsnDraw      = False                   ; Do not advance frame
   265 
   266     pres                            = True     ; panel plot mods desired
   267     pres@gsnPanelYWhiteSpacePercent = 5        ; increase white space around
   268                                                ; indiv. plots in panel
   269     pres@gsnMaximize                = True     ; fill the page
   270 ;------------------------------------------
   271     res@tiMainString = title                           ; add title
   272 
   273     plot(0)=gsn_csm_xy(wks,mon,plot_data(:,:,n),res)   ; create plot 1
   274 
   275     plot(1)=gsn_csm_xy(wks,mon,plot_data_0(:,n),res) ; create plot 2
   276 
   277     gsn_panel(wks,plot,(/2,1/),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 ;---------------------------------------------------------------------------  
   283  end do
   284 ;-------------------------------------------------------------------------
   285 ;   fo line plot in a zone
   286 
   287     plot_name = "All_"+npts_str
   288     title = plot_name + " in "+ zone
   289 ;   print (title)
   290 ;   print (plot_name)
   291 
   292     wks = gsn_open_wks (plot_type,plot_name)        ; open workstation
   293 ;-----------------------------------------
   294 ;   for panel plot
   295    
   296     plot=new(2,graphic)                        ; create graphic array
   297     res@gsnFrame     = False                   ; Do not draw plot 
   298     res@gsnDraw      = False                   ; Do not advance frame
   299 
   300     pres                            = True     ; panel plot mods desired
   301     pres@gsnPanelYWhiteSpacePercent = 5        ; increase white space around
   302                                                ; indiv. plots in panel
   303     pres@gsnMaximize                = True     ; fill the page
   304 ;-----------------------------------------
   305     res@tiMainString = title                                     ; add title
   306 
   307     plot(0) = gsn_csm_xy (wks,mon,dim_avg_Wrap(plot_data),res)   ; create plot 1
   308     
   309     plot(1) = gsn_csm_xy (wks,mon,dim_avg_Wrap(plot_data_0),res) ; create plot 2
   310 
   311     gsn_panel(wks,plot,(/2,1/),pres)                 ; create panel plot
   312 
   313     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
   314     system("rm "+plot_name+"."+plot_type)
   315 
   316     clear (wks)
   317 ;   delete (ind_z)
   318     delete (plot_data)
   319     delete (plot_data_0)    
   320 ;---------------------------------------------------------------------------
   321 ;---------------------------------------------------------------------------
   322 ; metric table in a zone
   323 
   324  amp_ratio_zone = avg(amp_ratio_sta)
   325  ccr_zone       = avg(ccr_sta)
   326  M_zone   = 1.- (sum(abs(amp_model-amp_ob)/(amp_model+amp_ob))/npts) 
   327  score_zone   = (ccr_zone*ccr_zone + M_zone)*0.5 * score_max
   328 
   329  print (npts+"/"+amp_ratio_zone+"/"+ccr_zone+"/"+M_zone+"/"+score_zone)
   330 
   331   case_value_zone(0,z) = npts
   332   case_value_zone(1,z) = (/amp_ratio_zone/)
   333   case_value_zone(2,z) = (/ccr_zone/)
   334   case_value_zone(3,z) = (/M_zone/)
   335   case_value_zone(4,z) = (/score_zone/)  
   336 
   337 ; column for station table
   338   case_sta   = (/"Latitude","Longitude","Amplitude Ratio", \
   339                  "Correlation Coef","M score","Combined Score"/) 
   340   nCase_sta  = dimsizes(case_sta )                 
   341 
   342 ; row for station table
   343   var_sta  = sta(ind_z) 
   344   nVar_sta = dimsizes(var_sta)                 
   345 
   346 ; arrays to be passed to diagram. 
   347   case_value_sta = new ((/nCase_sta, nVar_sta/),float )  
   348 
   349   case_value_sta(0,:) = (/lat(ind_z)/)
   350   case_value_sta(1,:) = (/lon(ind_z)/)
   351   case_value_sta(2,:) = (/amp_ratio_sta/)
   352   case_value_sta(3,:) = (/ccr_sta/)
   353   case_value_sta(4,:) = (/M_sta/)
   354   case_value_sta(5,:) = (/score_sta/)
   355  
   356 ;**************************************************
   357 ; plot station table
   358 ;**************************************************
   359   tt_opt        = True
   360   tt_opt@pltType= "png" ; "eps" [default], "pdf", "ps"
   361                         ; "png", "gif" [if you have ImageMajik 'convert']
   362 
   363   tt_opt@tableTitle = "Zone "+ zone
   364   plot_name         = "table_sta." + zone
   365 
   366   metrics_table(plot_name, var_sta, case_sta , case_value_sta, tt_opt)
   367 
   368  delete (ind_z)
   369  delete (amp_model)
   370  delete (amp_ob)
   371  delete (amp_ratio_sta)
   372  delete (ccr_sta)
   373  delete (M_sta)
   374  delete (score_sta)
   375  delete (var_sta)
   376  delete (case_value_sta)
   377  end do
   378 ;**************************************************
   379 ; plot zone table
   380 ;**************************************************
   381   case_value_zone(0,4) = case_value_zone(0,0)+case_value_zone(0,1)+case_value_zone(0,2)+case_value_zone(0,3) 
   382   case_value_zone(1,4) = 0.
   383   case_value_zone(2,4) = 0.
   384   case_value_zone(3,4) = 0.
   385   case_value_zone(4,4) = case_value_zone(4,0)+case_value_zone(4,1)+case_value_zone(4,2)+case_value_zone(4,3) 
   386   
   387   tt_opt        = True
   388   tt_opt@pltType= "png" ; "eps" [default], "pdf", "ps"
   389                         ; "png", "gif" [if you have ImageMajik 'convert']
   390 
   391   tt_opt@tableTitle = "Zone"
   392   plot_name         = "table_zone" 
   393 
   394   metrics_table(plot_name, var_zone, case_zone , case_value_zone, tt_opt)
   395 
   396 end