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