1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/co2/42.metric+lines.ncl.0 Mon Jan 26 22:08:20 2009 -0500
1.3 @@ -0,0 +1,448 @@
1.4 +; ***********************************************
1.5 +; combine 19.metric_plot.ncl and 24.lines.ncl
1.6 +; ***********************************************
1.7 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl.test"
1.8 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl.test"
1.9 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
1.10 +;load "/fis/cgd/cseg/people/jeff/clamp/co2/metrics_table.ncl"
1.11 +;************************************************
1.12 +begin
1.13 +;************************************************
1.14 +; read in data: observed
1.15 +;************************************************
1.16 + diri = "/fis/cgd/cseg/people/jeff/clamp_data/co2/"
1.17 + fili = "co2_globalView_98.nc"
1.18 + g = addfile (diri+fili,"r")
1.19 + val = g->CO2_SEAS
1.20 + lon = g->LON
1.21 + lat = g->LAT
1.22 + sta = chartostring(g->STATION)
1.23 + delete (g)
1.24 +
1.25 +;print (sta(0))
1.26 +
1.27 + ncase = dimsizes(lat)
1.28 +;print (ncase)
1.29 +
1.30 +;**************************************************************
1.31 +; get only the lowest level at each station
1.32 +;**************************************************************
1.33 + lat_tmp = lat
1.34 + lat_tmp@_FillValue = 1.e+36
1.35 +
1.36 + do n = 0,ncase-1
1.37 + if (.not. ismissing(lat_tmp(n))) then
1.38 + indexes = ind(lat(n) .eq. lat .and. lon(n) .eq. lon)
1.39 + if (dimsizes(indexes) .gt. 1) then
1.40 + lat_tmp(indexes(1:)) = lat_tmp@_FillValue
1.41 + end if
1.42 + delete (indexes)
1.43 + end if
1.44 + end do
1.45 +
1.46 + indexes = ind(.not. ismissing(lat_tmp))
1.47 +;print (dimsizes(indexes))
1.48 +;print (indexes)
1.49 +
1.50 + lat_ob = lat(indexes)
1.51 + lon_ob = lon(indexes)
1.52 + val_ob = val(indexes,:)
1.53 +;printVarSummary (val_ob)
1.54 +;print (lat_ob +"/"+lon_ob)
1.55 +
1.56 +;************************************************
1.57 +; read in model data
1.58 +;************************************************
1.59 + diri2 = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
1.60 +; fili2 = "b30.061m_401_425_MONS_climo_atm.nc"
1.61 + fili2 = "b30.061n_1995-2004_MONS_climo_atm.nc"
1.62 +
1.63 + g = addfile(diri2+fili2,"r")
1.64 + x = g->CO2
1.65 + xi = g->lon
1.66 + yi = g->lat
1.67 + xdim = dimsizes(x)
1.68 + nlev = xdim(1)
1.69 + y = x(:,0,:,:)
1.70 +; printVarSummary (y)
1.71 +
1.72 +; get the co2 at the lowest level
1.73 + y = x(:,nlev-1,:,:)
1.74 +
1.75 +; change to unit of observed (u mol/mol)
1.76 +; Model_units [=] kgCO2 / kgDryAir
1.77 +; 28.966 = molecular weight of dry air
1.78 +; 44. = molecular weight of CO2
1.79 +; u mol = 1e-6 mol
1.80 +
1.81 + factor = (28.966/44.) * 1e6
1.82 + y = y * factor
1.83 +
1.84 + y@_FillValue = 1.e36
1.85 + y@units = "u mol/mol"
1.86 +; y = where(y0 .lt. 287.,y@_FillValue,y)
1.87 +; printVarSummary (y)
1.88 +; print (min(y)+"/"+max(y))
1.89 +
1.90 +; interpolate into observed station
1.91 +; note: model is 0-360E, 90S-90N
1.92 +
1.93 +; to be able to handle observation at (-89.98,-24.80)
1.94 + print (yi(0))
1.95 + yi(0) = -90.
1.96 +
1.97 + i = ind(lon_ob .lt. 0.)
1.98 + lon_ob(i) = lon_ob(i) + 360.
1.99 +
1.100 + yo = linint2_points_Wrap(xi,yi,y,True,lon_ob,lat_ob,0)
1.101 +
1.102 + val_model = yo(pts|:,time|:)
1.103 + val_model_0 = val_model
1.104 +; printVarSummary (val_model)
1.105 +; print (min(val_model)+"/"+max(val_model))
1.106 +
1.107 +; remove annual mean
1.108 + val_model = val_model - conform(val_model,dim_avg(val_model),0)
1.109 +; print (min(val_model)+"/"+max(val_model))
1.110 +
1.111 + nzone = 4
1.112 +;--------------------------------------------------------------
1.113 +; for zone table
1.114 +;**********************************
1.115 +; table header name
1.116 + table_header_name = "Zone"
1.117 +
1.118 +; column (not including header column)
1.119 + col_header_zone = (/"Stations","Amplitude Ratio", \
1.120 + "Correlation Coef","M score","Combined Score"/)
1.121 + ncol_zone = dimsizes(col_header_zone )
1.122 +
1.123 +; row (not including header row)
1.124 + row_header_zone = (/"60N-90N","30N-60N","EQ-30N","90S-EQ","Total"/)
1.125 + nrow_zone = dimsizes(row_header_zone)
1.126 +
1.127 +; arrays to be passed to table.
1.128 + value_zone = new ((/nrow_zone, ncol_zone/),string )
1.129 +;**********************************
1.130 +
1.131 + table_length_zone = 0.4
1.132 +
1.133 +; Table header
1.134 + ncr1 = (/1,1/) ; 1 row, 1 column
1.135 + x1 = (/0.005,0.15/) ; Start and end X
1.136 + y1 = (/0.900,0.995/) ; Start and end Y
1.137 + text1 = table_header_name
1.138 + res1 = True
1.139 + res1@txFontHeightF = 0.03
1.140 + res1@gsFillColor = "CornFlowerBlue"
1.141 +
1.142 +; Column header (equally space in x2)
1.143 + ncr2 = (/1,ncol_zone/) ; 1 rows, 5 columns
1.144 + x2 = (/x1(1),0.995/) ; start from end of x1
1.145 + y2 = y1 ; same as y1
1.146 + text2 = col_header_zone
1.147 + res2 = True
1.148 + res2@txFontHeightF = 0.015
1.149 + res2@gsFillColor = "Gray"
1.150 +
1.151 +; Row header (equally space in y2)
1.152 + ncr3 = (/nrow_zone,1/) ; 5 rows, 1 columns
1.153 + x3 = x1 ; same as x1
1.154 + y3 = (/1.0-table_length_zone,0.900/) ; end at start of y1
1.155 + text3 = row_header_zone
1.156 + res3 = True
1.157 + res3@txFontHeightF = 0.02
1.158 + res3@gsFillColor = "Gray"
1.159 +
1.160 +; Main table body
1.161 + ncr4 = (/nrow_zone,ncol_zone/) ; 5 rows, 5 columns
1.162 + x4 = x2 ; Start and end x
1.163 + y4 = y3 ; Start and end Y
1.164 + text4 = new((/nrow_zone,ncol_zone/),string)
1.165 +
1.166 + color_fill4 = new((/nrow_zone,ncol_zone/),string)
1.167 + color_fill4 = "white"
1.168 + color_fill4(:,ncol_zone-1) = "grey"
1.169 +
1.170 + res4 = True ; Set up resource list
1.171 +; res4@gsnDebug = True ; Useful to print NDC row,col values used.
1.172 + res4@txFontHeightF = 0.02
1.173 + res4@gsFillColor = color_fill4
1.174 +
1.175 + delete (color_fill4)
1.176 +;--------------------------------------------------------------
1.177 +; for station line plot
1.178 +
1.179 +; for x-axis in xyplot
1.180 + mon = ispan(1,12,1)
1.181 + mon@long_name = "month"
1.182 +
1.183 + plot_type = "ps"
1.184 + plot_type_new = "png"
1.185 +
1.186 + res = True ; plot mods desired
1.187 + res@xyLineThicknesses = (/2.0,2.0,2.0/) ; make 2nd lines thicker
1.188 + res@xyLineColors = (/"red","black"/) ; change line color
1.189 +
1.190 +; Add a boxed legend using the more simple method
1.191 +
1.192 + res@pmLegendDisplayMode = "Always"
1.193 +; res@pmLegendWidthF = 0.1
1.194 + res@pmLegendWidthF = 0.08
1.195 + res@pmLegendHeightF = 0.06
1.196 +; res@pmLegendOrthogonalPosF = -1.17
1.197 +; res@pmLegendOrthogonalPosF = -1.00 ;(downward)
1.198 + res@pmLegendOrthogonalPosF = -0.30 ;(downward)
1.199 +
1.200 +; res@pmLegendParallelPosF = 0.18
1.201 + res@pmLegendParallelPosF = 0.23 ;(rightward)
1.202 +
1.203 +; res@lgPerimOn = False
1.204 + res@lgLabelFontHeightF = 0.015
1.205 + res@xyExplicitLegendLabels = (/"b30.061n","observed"/)
1.206 +;-------------------------------------------------------------------
1.207 +
1.208 + do z = 0,nzone-1
1.209 +
1.210 + if (z .eq. 0) then
1.211 +; maximum score for the zone, 60N-90N
1.212 + zone = "60N-90N"
1.213 + score_max = 5.0
1.214 +; index of stations in this zone
1.215 + ind_z = ind(lat_ob .ge. 60.)
1.216 +; print (ind_z)
1.217 +; print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
1.218 +; print (val_ob(ind_z,:))
1.219 +; print (val_model(ind_z,:))
1.220 + end if
1.221 +
1.222 + if (z .eq. 1) then
1.223 +; maximum score for the zone, 30N-60N
1.224 + zone = "30N-60N"
1.225 + score_max = 5.0
1.226 +; index of stations in this zone
1.227 + ind_z = ind(lat_ob .ge. 30. .and. lat_ob .lt. 60.)
1.228 +; print (ind_z)
1.229 +; print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
1.230 +; print (val_ob(ind_z,:))
1.231 +; print (val_model(ind_z,:))
1.232 + end if
1.233 +
1.234 + if (z .eq. 2) then
1.235 +; maximum score for the zone, EQ-30N
1.236 + zone = "EQ-30N"
1.237 + score_max = 5.0
1.238 +; index of stations in this zone
1.239 + ind_z = ind(lat_ob .ge. 0. .and. lat_ob .lt. 30.)
1.240 +; print (ind_z)
1.241 +; print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
1.242 +; print (val_ob(ind_z,:))
1.243 +; print (val_model(ind_z,:))
1.244 + end if
1.245 +
1.246 + if (z .eq. 3) then
1.247 +; maximum score for the zone, 90S-EQ
1.248 + zone = "90S-EQ"
1.249 + score_max = 5.0
1.250 +; index of stations in this zone
1.251 + ind_z = ind(lat_ob .lt. 0. )
1.252 +; print (ind_z)
1.253 +; print (lat_ob(ind_z)+"/"+lon_ob(ind_z))
1.254 +; print (val_ob(ind_z,:))
1.255 +; print (val_model(ind_z,:))
1.256 + end if
1.257 +
1.258 + npts = dimsizes(ind_z)
1.259 + print (npts)
1.260 +;-------------------------------------------------------------------------
1.261 +; for metric table
1.262 +
1.263 + amp_ob = new((/npts/),float)
1.264 + amp_model = new((/npts/),float)
1.265 +
1.266 + amp_ratio_sta = new((/npts/),float)
1.267 + ccr_sta = new((/npts/),float)
1.268 + M_sta = new((/npts/),float)
1.269 + score_sta = new((/npts/),float)
1.270 +;-------------------------------------------------------------------------
1.271 +; for station line plot
1.272 +
1.273 + npts_str = ""
1.274 + npts_str = npts
1.275 +; print (npts_str)
1.276 +
1.277 + plot_data = new((/2,12,npts/),float)
1.278 + plot_data_0 = new((/12,npts/),float)
1.279 +
1.280 + plot_data!0 = "case"
1.281 + plot_data!1 = "month"
1.282 + plot_data!2 = "pts"
1.283 + plot_data@long_name = "CO2 Seasonal"
1.284 +
1.285 + plot_data_0!0 = "month"
1.286 + plot_data_0!1 = "pts"
1.287 + plot_data_0@long_name = "CO2"
1.288 +;--------------------------------------------------------------------------
1.289 + do n=0,npts-1
1.290 + amp_ob(n) = max(val_ob(ind_z(n),:)) - min(val_ob(ind_z(n),:))
1.291 + amp_model(n) = max(val_model(ind_z(n),:)) - min(val_model(ind_z(n),:))
1.292 +
1.293 + amp_ratio_sta(n) = amp_model(n)/amp_ob(n)
1.294 + ccr_sta(n) = esccr(val_ob(ind_z(n),:),val_model(ind_z(n),:),0)
1.295 + M_sta(n) = 1.-abs(amp_ratio_sta(n)-1.)
1.296 + score_sta(n) = (ccr_sta(n)*ccr_sta(n) + M_sta(n))*0.5 * score_max
1.297 +
1.298 + 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))
1.299 +;----------------------------------------------------------------------
1.300 +; for station line plot
1.301 +
1.302 + plot_data(0,:,n) = (/val_model(ind_z(n),:)/)
1.303 + plot_data(1,:,n) = (/val_ob(ind_z(n),:)/)
1.304 +
1.305 + plot_data_0(:,n) = (/val_model_0(ind_z(n),:)/)
1.306 +
1.307 + plot_name = sta(ind_z(n))
1.308 + title = plot_name+"("+lat(ind_z(n))+","+lon(ind_z(n))+")"
1.309 +; print (title)
1.310 +; print (plot_name)
1.311 +
1.312 + wks = gsn_open_wks (plot_type,plot_name) ; open workstation
1.313 +;------------------------------------------
1.314 +; for panel plot
1.315 +
1.316 + plot=new(2,graphic) ; create graphic array
1.317 + res@gsnFrame = False ; Do not draw plot
1.318 + res@gsnDraw = False ; Do not advance frame
1.319 +
1.320 + pres = True ; panel plot mods desired
1.321 + pres@gsnPanelYWhiteSpacePercent = 5 ; increase white space around
1.322 + ; indiv. plots in panel
1.323 + pres@gsnMaximize = True ; fill the page
1.324 +;------------------------------------------
1.325 + res@tiMainString = title ; add title
1.326 +
1.327 + plot(0)=gsn_csm_xy(wks,mon,plot_data(:,:,n),res) ; create plot 1
1.328 +
1.329 + plot(1)=gsn_csm_xy(wks,mon,plot_data_0(:,n),res) ; create plot 2
1.330 +
1.331 + gsn_panel(wks,plot,(/2,1/),pres) ; create panel plot
1.332 +
1.333 + system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
1.334 + system("rm "+plot_name+"."+plot_type)
1.335 + clear (wks)
1.336 +;---------------------------------------------------------------------------
1.337 + end do
1.338 +;-------------------------------------------------------------------------
1.339 +; for line plot in a zone
1.340 +
1.341 + plot_name = "All_"+npts_str
1.342 + title = plot_name + " in "+ zone
1.343 +; print (title)
1.344 +; print (plot_name)
1.345 +
1.346 + wks = gsn_open_wks (plot_type,plot_name) ; open workstation
1.347 +;-----------------------------------------
1.348 +; for panel plot
1.349 +
1.350 + plot=new(2,graphic) ; create graphic array
1.351 + res@gsnFrame = False ; Do not draw plot
1.352 + res@gsnDraw = False ; Do not advance frame
1.353 +
1.354 + pres = True ; panel plot mods desired
1.355 + pres@gsnPanelYWhiteSpacePercent = 5 ; increase white space around
1.356 + ; indiv. plots in panel
1.357 + pres@gsnMaximize = True ; fill the page
1.358 +;-----------------------------------------
1.359 + res@tiMainString = title ; add title
1.360 +
1.361 + plot(0) = gsn_csm_xy (wks,mon,dim_avg_Wrap(plot_data),res) ; create plot 1
1.362 +
1.363 + plot(1) = gsn_csm_xy (wks,mon,dim_avg_Wrap(plot_data_0),res) ; create plot 2
1.364 +
1.365 + gsn_panel(wks,plot,(/2,1/),pres) ; create panel plot
1.366 +
1.367 + system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
1.368 + system("rm "+plot_name+"."+plot_type)
1.369 +
1.370 + clear (wks)
1.371 +; delete (ind_z)
1.372 + delete (plot_data)
1.373 + delete (plot_data_0)
1.374 +;---------------------------------------------------------------------------
1.375 +; for zone table
1.376 +
1.377 + amp_ratio_zone = avg(amp_ratio_sta)
1.378 + ccr_zone = avg(ccr_sta)
1.379 + M_zone = 1.- (sum(abs(amp_model-amp_ob)/(amp_model+amp_ob))/npts)
1.380 + score_zone = (ccr_zone*ccr_zone + M_zone)*0.5 * score_max
1.381 +
1.382 + print (npts+"/"+amp_ratio_zone+"/"+ccr_zone+"/"+M_zone+"/"+score_zone)
1.383 +
1.384 + text4(z,0) = sprintf("%5.2f", npts)
1.385 + text4(z,1) = sprintf("%5.2f", amp_ratio_zone)
1.386 + text4(z,2) = sprintf("%5.2f", ccr_zone)
1.387 + text4(z,3) = sprintf("%5.2f", M_zone)
1.388 + text4(z,4) = sprintf("%5.2f", score_zone)
1.389 +;---------------------------------------------------------------------------
1.390 +; column for station table
1.391 + case_sta = (/"Latitude","Longitude","Amplitude Ratio", \
1.392 + "Correlation Coef","M score","Combined Score"/)
1.393 + nCase_sta = dimsizes(case_sta )
1.394 +
1.395 +; row for station table
1.396 + var_sta = sta(ind_z)
1.397 + nVar_sta = dimsizes(var_sta)
1.398 +
1.399 +; arrays to be passed to diagram.
1.400 + case_value_sta = new ((/nCase_sta, nVar_sta/),float )
1.401 +
1.402 + case_value_sta(0,:) = (/lat(ind_z)/)
1.403 + case_value_sta(1,:) = (/lon(ind_z)/)
1.404 + case_value_sta(2,:) = (/amp_ratio_sta/)
1.405 + case_value_sta(3,:) = (/ccr_sta/)
1.406 + case_value_sta(4,:) = (/M_sta/)
1.407 + case_value_sta(5,:) = (/score_sta/)
1.408 +
1.409 +;**************************************************
1.410 +; plot station table
1.411 +;**************************************************
1.412 + tt_opt = True
1.413 + tt_opt@pltType= "png" ; "eps" [default], "pdf", "ps"
1.414 + ; "png", "gif" [if you have ImageMajik 'convert']
1.415 +
1.416 + tt_opt@tableTitle = "Zone "+ zone
1.417 + plot_name = "table_sta." + zone
1.418 +
1.419 +; metrics_table(plot_name, var_sta, case_sta , case_value_sta, tt_opt)
1.420 +
1.421 + delete (ind_z)
1.422 + delete (amp_model)
1.423 + delete (amp_ob)
1.424 + delete (amp_ratio_sta)
1.425 + delete (ccr_sta)
1.426 + delete (M_sta)
1.427 + delete (score_sta)
1.428 + delete (var_sta)
1.429 + delete (case_value_sta)
1.430 + end do
1.431 +;**************************************************
1.432 +; plot zone table
1.433 +;**************************************************
1.434 +
1.435 + text4(4,0) = sum(stringtofloat(text4(0:3,0)))
1.436 + text4(4,1) = 0.
1.437 + text4(4,2) = 0.
1.438 + text4(4,3) = 0.
1.439 + text4(4,4) = sum(stringtofloat(text4(0:3,4)))
1.440 +
1.441 + plot_name = "table_zone"
1.442 + wks = gsn_open_wks (plot_type,plot_name) ; open workstation
1.443 +
1.444 + gsn_table(wks,ncr1,x1,y1,text1,res1)
1.445 + gsn_table(wks,ncr2,x2,y2,text2,res2)
1.446 + gsn_table(wks,ncr3,x3,y3,text3,res3)
1.447 + gsn_table(wks,ncr4,x4,y4,text4,res4)
1.448 +
1.449 + frame(wks)
1.450 +
1.451 +end