1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lai/26.histogram+bias_phase.ncl Mon Jan 26 22:08:20 2009 -0500
1.3 @@ -0,0 +1,341 @@
1.4 +;********************************************************
1.5 +; histogram normalized by rain and compute correleration
1.6 +;********************************************************
1.7 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
1.8 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
1.9 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
1.10 +
1.11 +procedure pminmax(data:numeric,name:string)
1.12 +begin
1.13 + print ("min/max " + name + " = " + min(data) + "/" + max(data))
1.14 + if(isatt(data,"units")) then
1.15 + print (name + " units = " + data@units)
1.16 + end if
1.17 +end
1.18 +
1.19 +;
1.20 +; Main code.
1.21 +;
1.22 +begin
1.23 +
1.24 +;nclass = 18
1.25 + nclass = 20
1.26 +
1.27 +;************************************************
1.28 +; read in data: observed
1.29 +;************************************************
1.30 + diri1 = "/fis/cgd/cseg/people/jeff/clamp_data/lai/"
1.31 +;fili1 = "land_class_T42.nc"
1.32 + fili1 = "land_class_T42_new.nc"
1.33 + fili2 = "LAI_2000-2005_ensemble_T42.nc"
1.34 + data_file_ob1 = addfile(diri1+fili1,"r")
1.35 + data_file_ob2 = addfile(diri1+fili2,"r")
1.36 +
1.37 + RAIN1 = tofloat(data_file_ob1->LAND_CLASS)
1.38 +
1.39 + z = data_file_ob2->LAI
1.40 + y = z(0,:,:)
1.41 + y@long_name = "Leaf Area Index Max Month"
1.42 + s = z(:,0,0)
1.43 +
1.44 + dsizes_z = dimsizes(z)
1.45 + ntime = dsizes_z(0)
1.46 + nlat = dsizes_z(1)
1.47 + nlon = dsizes_z(2)
1.48 +
1.49 + do j = 0,nlat-1
1.50 + do i = 0,nlon-1
1.51 + s = z(:,j,i)
1.52 + y(j,i) = maxind(s) + 1
1.53 + end do
1.54 + end do
1.55 +
1.56 + print (min(y)+"/"+max(y))
1.57 +
1.58 + NPP1 = y
1.59 +
1.60 + delete (z)
1.61 + delete (s)
1.62 + delete (y)
1.63 +;************************************************
1.64 +; read in data: model
1.65 +;************************************************
1.66 + diri2 = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
1.67 +;fili3 = "i01.03cn_1545-1569_MONS_climo.nc"
1.68 + fili3 = "i01.04casa_1605-1629_MONS_climo.nc"
1.69 + data_file_model = addfile(diri2+fili3,"r")
1.70 +
1.71 + z = data_file_model->TLAI
1.72 + y = z(0,:,:)
1.73 + y@long_name = "Leaf Area Index Max Month"
1.74 + s = z(:,0,0)
1.75 +
1.76 + dsizes_z = dimsizes(z)
1.77 + ntime = dsizes_z(0)
1.78 + nlat = dsizes_z(1)
1.79 + nlon = dsizes_z(2)
1.80 +
1.81 + do j = 0,nlat-1
1.82 + do i = 0,nlon-1
1.83 + s = z(:,j,i)
1.84 + y(j,i) = maxind(s) + 1
1.85 + end do
1.86 + end do
1.87 +
1.88 + print (min(y)+"/"+max(y))
1.89 +
1.90 + NPP2 = y
1.91 +
1.92 + delete (z)
1.93 + delete (s)
1.94 + delete (y)
1.95 +;************************************************
1.96 +; print min/max and unit
1.97 +;************************************************
1.98 + pminmax(RAIN1,"RAIN1")
1.99 + pminmax(NPP1,"NPP1")
1.100 + pminmax(NPP2,"NPP2")
1.101 +
1.102 + RAIN1_1D = ndtooned(RAIN1)
1.103 + NPP1_1D = ndtooned(NPP1)
1.104 + NPP2_1D = ndtooned(NPP2)
1.105 +;
1.106 +; Calculate some "nice" bins for binning the data in equally spaced
1.107 +; ranges.
1.108 +;
1.109 +
1.110 +; nbins = nclass + 1 ; Number of bins to use.
1.111 +; nicevals = nice_mnmxintvl(min(RAIN1_1D),max(RAIN1_1D),nbins,False)
1.112 +; nvals = floattoint((nicevals(1) - nicevals(0))/nicevals(2) + 1)
1.113 +; range = fspan(nicevals(0),nicevals(1),nvals)
1.114 +
1.115 + nclass = nclass + 1
1.116 + range = fspan(0,nclass-1,nclass)
1.117 +
1.118 +; print (nicevals)
1.119 +; print (nvals)
1.120 + print (range)
1.121 +; exit
1.122 +
1.123 +;
1.124 +; Use this range information to grab all the values in a
1.125 +; particular range, and then take an average.
1.126 +;
1.127 + nr = dimsizes(range)
1.128 + nx = nr-1
1.129 + xvalues = new((/2,nx/),typeof(RAIN1_1D))
1.130 + xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
1.131 + dx = xvalues(0,1) - xvalues(0,0) ; range width
1.132 + dx4 = dx/4 ; 1/4 of the range
1.133 + xvalues(1,:) = xvalues(0,:) - dx/5.
1.134 + yvalues = new((/2,nx/),typeof(RAIN1_1D))
1.135 + mn_yvalues = new((/2,nx/),typeof(RAIN1_1D))
1.136 + mx_yvalues = new((/2,nx/),typeof(RAIN1_1D))
1.137 +
1.138 + do nd=0,1
1.139 +;
1.140 +; See if we are doing model or observational data.
1.141 +;
1.142 + if(nd.eq.0) then
1.143 + data = RAIN1_1D
1.144 + npp_data = NPP1_1D
1.145 + else
1.146 + data = RAIN1_1D
1.147 + npp_data = NPP2_1D
1.148 + end if
1.149 +;
1.150 +; Loop through each range and check for values.
1.151 +;
1.152 + do i=0,nr-2
1.153 + if (i.ne.(nr-2)) then
1.154 + print("")
1.155 + print("In range ["+range(i)+","+range(i+1)+")")
1.156 + idx = ind((range(i).le.data).and.(data.lt.range(i+1)))
1.157 + else
1.158 + print("")
1.159 + print("In range ["+range(i)+",)")
1.160 + idx = ind(range(i).le.data)
1.161 + end if
1.162 +;
1.163 +; Calculate average, and get min and max.
1.164 +;
1.165 + if(.not.any(ismissing(idx))) then
1.166 + yvalues(nd,i) = avg(npp_data(idx))
1.167 + mn_yvalues(nd,i) = min(npp_data(idx))
1.168 + mx_yvalues(nd,i) = max(npp_data(idx))
1.169 + count = dimsizes(idx)
1.170 + else
1.171 + count = 0
1.172 + yvalues(nd,i) = yvalues@_FillValue
1.173 + mn_yvalues(nd,i) = yvalues@_FillValue
1.174 + mx_yvalues(nd,i) = yvalues@_FillValue
1.175 + end if
1.176 +;
1.177 +; Print out information.
1.178 +;
1.179 + print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
1.180 + print("Min/Max: " + mn_yvalues(nd,i) + "/" + mx_yvalues(nd,i))
1.181 +
1.182 +;
1.183 +; Clean up for next time in loop.
1.184 +;
1.185 + delete(idx)
1.186 + end do
1.187 + delete(data)
1.188 + delete(npp_data)
1.189 + end do
1.190 +
1.191 +;
1.192 +; Start the graphics.
1.193 +;
1.194 + wks = gsn_open_wks("ps","xy")
1.195 +
1.196 + res = True
1.197 + res@gsnMaximize = True
1.198 + res@gsnDraw = False
1.199 + res@gsnFrame = False
1.200 + res@xyMarkLineMode = "Markers"
1.201 + res@xyMarkerSizeF = 0.014
1.202 + res@xyMarker = 16
1.203 + res@xyMarkerColors = (/"Brown","Blue"/)
1.204 +; res@trYMinF = min(mn_yvalues) - 10.
1.205 +; res@trYMaxF = max(mx_yvalues) + 10.
1.206 + res@trYMinF = min(mn_yvalues) - 2
1.207 + res@trYMaxF = max(mx_yvalues) + 4
1.208 +
1.209 +; res@tiMainString = "Observed vs i01.03cn"
1.210 + res@tiMainString = "Observed vs i01.04casa"
1.211 +
1.212 + res@tiYAxisString = "Max LAI (Leaf Area Index) Month"
1.213 + res@tiXAxisString = "Land Cover Type"
1.214 +;
1.215 +; Add a boxed legend using the more simple method, which won't have
1.216 +; vertical lines going through the markers.
1.217 +;
1.218 + res@pmLegendDisplayMode = "Always"
1.219 +; res@pmLegendWidthF = 0.1
1.220 + res@pmLegendWidthF = 0.08
1.221 + res@pmLegendHeightF = 0.05
1.222 + res@pmLegendOrthogonalPosF = -1.17
1.223 +; res@pmLegendOrthogonalPosF = -1.00 ;(downward)
1.224 +; res@pmLegendParallelPosF = 0.18
1.225 + res@pmLegendParallelPosF = 0.23 ;(rightward)
1.226 +
1.227 +; res@lgPerimOn = False
1.228 + res@lgLabelFontHeightF = 0.015
1.229 +; res@xyExplicitLegendLabels = (/"observed","model_i01.03cn"/)
1.230 + res@xyExplicitLegendLabels = (/"observed","model_i01.04casa"/)
1.231 +
1.232 + xy = gsn_csm_xy(wks,xvalues,yvalues,res)
1.233 +
1.234 + max_bar = new((/2,nx/),graphic)
1.235 + min_bar = new((/2,nx/),graphic)
1.236 + max_cap = new((/2,nx/),graphic)
1.237 + min_cap = new((/2,nx/),graphic)
1.238 +
1.239 + lnres = True
1.240 +
1.241 + line_colors = (/"brown","blue"/)
1.242 + do nd=0,1
1.243 + lnres@gsLineColor = line_colors(nd)
1.244 + do i=0,nx-1
1.245 +
1.246 + if(.not.ismissing(mn_yvalues(nd,i)).and. \
1.247 + .not.ismissing(mx_yvalues(nd,i))) then
1.248 +;
1.249 +; Attach the vertical bar, both above and below the marker.
1.250 +;
1.251 + x1 = xvalues(nd,i)
1.252 + y1 = yvalues(nd,i)
1.253 + y2 = mn_yvalues(nd,i)
1.254 + min_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
1.255 +
1.256 + y2 = mx_yvalues(nd,i)
1.257 + max_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
1.258 +;
1.259 +; Attach the horizontal cap line, both above and below the marker.
1.260 +;
1.261 + x1 = xvalues(nd,i) - dx4
1.262 + x2 = xvalues(nd,i) + dx4
1.263 + y1 = mn_yvalues(nd,i)
1.264 + min_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
1.265 +
1.266 + y1 = mx_yvalues(nd,i)
1.267 + max_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
1.268 + end if
1.269 + end do
1.270 + end do
1.271 +
1.272 +;
1.273 +; Here's how to do the legend by hand.
1.274 +;
1.275 +; mkres = True ; Marker resources
1.276 +; txres = True ; Text resources
1.277 +; mkres@gsMarkerIndex = 16
1.278 +; mkres@gsMarkerSizeF = 0.02
1.279 +; txres@txFontHeightF = 0.02
1.280 +; txres@txJust = "CenterLeft"
1.281 +;
1.282 +; Change these values if you want to move the marker legend location.
1.283 +; These values are in the same data space as the plot.
1.284 +;
1.285 +; xlg1_cen = 0.2
1.286 +; ylg1_cen = 900.
1.287 +
1.288 +; xlg2_cen = 0.2
1.289 +; ylg2_cen = 760.
1.290 +
1.291 +; mkres@gsMarkerColor = "brown"
1.292 +; lnres@gsLineColor = "brown"
1.293 +
1.294 +; lg_mark_legend1 = gsn_add_polymarker(wks,xy,xlg1_cen,ylg1_cen,mkres)
1.295 +; lg_line_legend1 = gsn_add_polyline(wks,xy,(/xlg1_cen,xlg1_cen/), \
1.296 +; (/ylg1_cen-60,ylg1_cen+60/),lnres)
1.297 +; lg_cap_legend11 = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \
1.298 +; (/ylg1_cen-60,ylg1_cen-60/),lnres)
1.299 +; lg_cap_legend12 = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \
1.300 +; (/ylg1_cen+60,ylg1_cen+60/),lnres)
1.301 +
1.302 +; tx_legend1 = gsn_add_text(wks,xy,"observed",xlg1_cen+0.15,ylg1_cen,txres)
1.303 +
1.304 +; mkres@gsMarkerColor = "blue"
1.305 +; lnres@gsLineColor = "blue"
1.306 +
1.307 +; lg_mark_legend2 = gsn_add_polymarker(wks,xy,xlg2_cen,ylg2_cen,mkres)
1.308 +; lg_line_legend2 = gsn_add_polyline(wks,xy,(/xlg2_cen,xlg2_cen/), \
1.309 +; (/ylg2_cen-60,ylg2_cen+60/),lnres)
1.310 +; lg_cap_legend21 = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \
1.311 +; (/ylg2_cen-60,ylg2_cen-60/),lnres)
1.312 +; lg_cap_legend22 = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \
1.313 +; (/ylg2_cen+60,ylg2_cen+60/),lnres)
1.314 +; tx_legend2 = gsn_add_text(wks,xy,"model_i01.03cn",xlg2_cen+0.15,ylg2_cen,txres)
1.315 +
1.316 + draw(xy)
1.317 + frame(wks)
1.318 + system("convert xy.ps xy.png")
1.319 +
1.320 + u = yvalues(0,:)
1.321 + v = yvalues(1,:)
1.322 +
1.323 + good = ind(.not.ismissing(u) .and. .not.ismissing(v))
1.324 + uu = u(good)
1.325 + vv = v(good)
1.326 + nz = dimsizes(uu)
1.327 + print (nz)
1.328 +
1.329 + ccr = esccr(uu,vv,0)
1.330 + print (ccr)
1.331 +
1.332 +;old eq
1.333 +;bias = sum(((vv-uu)/uu)^2)
1.334 +;M = (1.- sqrt(bias/nz))*5.
1.335 +
1.336 +;new eq
1.337 + bias = abs(avg(vv)-avg(uu))
1.338 + bias = where((bias.gt. 6.),12.-bias,bias)
1.339 + M = ((6. - bias)/6.)*5.
1.340 + print (bias)
1.341 + print (M)
1.342 +
1.343 +end
1.344 +