lai/24.histogram+bias_mean.ncl
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 ; histogram normalized by rain and compute correleration
     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 
     8 procedure pminmax(data:numeric,name:string)
     9 begin
    10   print ("min/max " + name + " = " + min(data) + "/" + max(data))
    11   if(isatt(data,"units")) then
    12     print (name + " units = " + data@units)
    13   end if
    14 end
    15 
    16 ;
    17 ; Main code.
    18 ;
    19 begin
    20  
    21 ;nclass = 18
    22  nclass = 20
    23  
    24 ;************************************************
    25 ; read in data: observed
    26 ;************************************************
    27  diri1  = "/fis/cgd/cseg/people/jeff/clamp_data/lai/"
    28 ;fili1  = "land_class_T42.nc"
    29  fili1  = "land_class_T42_new.nc"
    30  fili2  = "LAI_2000-2005_mean_T42.nc"
    31  data_file_ob1 = addfile(diri1+fili1,"r")
    32  data_file_ob2 = addfile(diri1+fili2,"r")
    33  
    34  RAIN1 = tofloat(data_file_ob1->LAND_CLASS)               
    35  NPP1  = data_file_ob2->LAI      
    36 ;************************************************
    37 ; read in data: model       
    38 ;************************************************
    39  diri2  = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
    40 ;fili3  = "i01.03cn_1545-1569_ANN_climo.nc"
    41  fili3  = "i01.04casa_1605-1629_ANN_climo.nc"
    42  data_file_model = addfile(diri2+fili3,"r")
    43       
    44  NPP2  = data_file_model->TLAI      
    45 ;************************************************
    46 ; print min/max and unit
    47 ;************************************************
    48   pminmax(RAIN1,"RAIN1")
    49   pminmax(NPP1,"NPP1")
    50   pminmax(NPP2,"NPP2")
    51 
    52   RAIN1_1D = ndtooned(RAIN1)
    53   NPP1_1D  = ndtooned(NPP1)
    54   NPP2_1D  = ndtooned(NPP2)
    55 ;
    56 ; Calculate some "nice" bins for binning the data in equally spaced
    57 ; ranges.
    58 ;
    59 
    60 ; nbins       = nclass + 1         ; Number of bins to use.
    61 ; nicevals    = nice_mnmxintvl(min(RAIN1_1D),max(RAIN1_1D),nbins,False)
    62 ; nvals       = floattoint((nicevals(1) - nicevals(0))/nicevals(2) + 1)
    63 ; range       = fspan(nicevals(0),nicevals(1),nvals)
    64 
    65   nclassn     = nclass + 1
    66   range       = fspan(0,nclassn-1,nclassn)
    67 
    68 ; print (nicevals)
    69 ; print (nvals)
    70   print (range)
    71 ; exit
    72 
    73 ;
    74 ; Use this range information to grab all the values in a
    75 ; particular range, and then take an average.
    76 ;
    77   nr      = dimsizes(range)
    78   nx      = nr-1
    79   xvalues     = new((/2,nx/),typeof(RAIN1_1D))
    80   xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
    81   dx           = xvalues(0,1) - xvalues(0,0)       ; range width
    82   dx4          = dx/4                              ; 1/4 of the range
    83   xvalues(1,:) = xvalues(0,:) - dx/5.
    84   yvalues      = new((/2,nx/),typeof(RAIN1_1D))
    85   mn_yvalues   = new((/2,nx/),typeof(RAIN1_1D))
    86   mx_yvalues   = new((/2,nx/),typeof(RAIN1_1D))
    87 
    88   do nd=0,1
    89 ;
    90 ; See if we are doing model or observational data.
    91 ;
    92     if(nd.eq.0) then
    93       data     = RAIN1_1D
    94       npp_data = NPP1_1D
    95     else
    96       data     = RAIN1_1D
    97       npp_data = NPP2_1D
    98     end if
    99 ;
   100 ; Loop through each range and check for values.
   101 ;
   102     do i=0,nr-2
   103       if (i.ne.(nr-2)) then
   104          print("")
   105          print("In range ["+range(i)+","+range(i+1)+")")
   106         idx = ind((range(i).le.data).and.(data.lt.range(i+1)))
   107       else
   108          print("")
   109          print("In range ["+range(i)+",)")
   110         idx = ind(range(i).le.data)
   111       end if
   112 ;
   113 ; Calculate average, and get min and max.
   114 ;
   115       if(.not.any(ismissing(idx))) then
   116         yvalues(nd,i)    = avg(npp_data(idx))
   117         mn_yvalues(nd,i) = min(npp_data(idx))
   118         mx_yvalues(nd,i) = max(npp_data(idx))
   119         count = dimsizes(idx)
   120       else
   121         count            = 0
   122         yvalues(nd,i)    = yvalues@_FillValue
   123         mn_yvalues(nd,i) = yvalues@_FillValue
   124         mx_yvalues(nd,i) = yvalues@_FillValue
   125       end if
   126 ;
   127 ; Print out information.
   128 ;
   129        print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
   130        print("Min/Max:  " + mn_yvalues(nd,i) + "/" + mx_yvalues(nd,i))
   131 
   132 ;
   133 ; Clean up for next time in loop.
   134 ;
   135       delete(idx)
   136     end do
   137     delete(data)
   138     delete(npp_data)
   139   end do
   140 
   141 ;
   142 ; Start the graphics.
   143 ;
   144   wks = gsn_open_wks("ps","xy")
   145 
   146   res             = True
   147   res@gsnMaximize = True
   148   res@gsnDraw     = False
   149   res@gsnFrame    = False
   150   res@xyMarkLineMode = "Markers"
   151   res@xyMarkerSizeF   = 0.014
   152   res@xyMarker       = 16
   153   res@xyMarkerColors = (/"Brown","Blue"/)
   154 ; res@trYMinF        = min(mn_yvalues) - 10.
   155 ; res@trYMaxF        = max(mx_yvalues) + 10.
   156   res@trYMinF        = min(mn_yvalues) - 2
   157   res@trYMaxF        = max(mx_yvalues) + 4
   158 
   159 ; res@tiMainString  = "Observed vs i01.03cn"
   160   res@tiMainString  = "Observed vs i01.04casa"
   161 
   162   res@tiYAxisString = "Mean LAI (Leaf Area Index)"
   163   res@tiXAxisString = "Land Cover Type"
   164 ;
   165 ; Add a boxed legend using the more simple method, which won't have
   166 ; vertical lines going through the markers.
   167 ;
   168   res@pmLegendDisplayMode    = "Always"
   169 ; res@pmLegendWidthF         = 0.1
   170   res@pmLegendWidthF         = 0.08
   171   res@pmLegendHeightF        = 0.05
   172   res@pmLegendOrthogonalPosF = -1.17
   173 ; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
   174 ; res@pmLegendParallelPosF   =  0.18
   175   res@pmLegendParallelPosF   =  0.23  ;(rightward)
   176 
   177 ; res@lgPerimOn              = False
   178   res@lgLabelFontHeightF     = 0.015
   179 ; res@xyExplicitLegendLabels = (/"observed","model_i01.03cn"/)
   180   res@xyExplicitLegendLabels = (/"observed","model_i01.04casa"/)
   181 
   182   xy = gsn_csm_xy(wks,xvalues,yvalues,res)
   183 
   184   max_bar = new((/2,nx/),graphic)
   185   min_bar = new((/2,nx/),graphic)
   186   max_cap = new((/2,nx/),graphic)
   187   min_cap = new((/2,nx/),graphic)
   188 
   189   lnres = True
   190 
   191   line_colors = (/"brown","blue"/)
   192   do nd=0,1
   193     lnres@gsLineColor = line_colors(nd)
   194     do i=0,nx-1
   195      
   196       if(.not.ismissing(mn_yvalues(nd,i)).and. \
   197          .not.ismissing(mx_yvalues(nd,i))) then
   198 ;
   199 ; Attach the vertical bar, both above and below the marker.
   200 ;
   201         x1 = xvalues(nd,i)
   202         y1 = yvalues(nd,i)
   203         y2 = mn_yvalues(nd,i)
   204         min_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
   205 
   206         y2 = mx_yvalues(nd,i)
   207         max_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
   208 ;
   209 ; Attach the horizontal cap line, both above and below the marker.
   210 ;
   211         x1 = xvalues(nd,i) - dx4
   212         x2 = xvalues(nd,i) + dx4
   213         y1 = mn_yvalues(nd,i)
   214         min_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
   215 
   216         y1 = mx_yvalues(nd,i)
   217         max_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
   218       end if
   219     end do
   220   end do
   221 
   222 ;
   223 ; Here's how to do the legend by hand.
   224 ;
   225 ; mkres               = True    ; Marker resources
   226 ; txres               = True    ; Text resources
   227 ; mkres@gsMarkerIndex = 16
   228 ; mkres@gsMarkerSizeF = 0.02
   229 ; txres@txFontHeightF = 0.02
   230 ; txres@txJust        = "CenterLeft"
   231 ;
   232 ; Change these values if you want to move the marker legend location.
   233 ; These values are in the same data space as the plot. 
   234 ;
   235 ; xlg1_cen = 0.2
   236 ; ylg1_cen = 900.
   237 
   238 ; xlg2_cen = 0.2
   239 ; ylg2_cen = 760.
   240 
   241 ; mkres@gsMarkerColor = "brown"
   242 ; lnres@gsLineColor   = "brown"
   243 
   244 ; lg_mark_legend1 = gsn_add_polymarker(wks,xy,xlg1_cen,ylg1_cen,mkres)
   245 ; lg_line_legend1 = gsn_add_polyline(wks,xy,(/xlg1_cen,xlg1_cen/), \
   246 ;                                           (/ylg1_cen-60,ylg1_cen+60/),lnres)
   247 ; lg_cap_legend11  = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \
   248 ;                                           (/ylg1_cen-60,ylg1_cen-60/),lnres)
   249 ; lg_cap_legend12  = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \
   250 ;                                           (/ylg1_cen+60,ylg1_cen+60/),lnres)
   251 
   252 ; tx_legend1 = gsn_add_text(wks,xy,"observed",xlg1_cen+0.15,ylg1_cen,txres)
   253 
   254 ; mkres@gsMarkerColor = "blue"
   255 ; lnres@gsLineColor   = "blue"
   256 
   257 ; lg_mark_legend2 = gsn_add_polymarker(wks,xy,xlg2_cen,ylg2_cen,mkres)
   258 ; lg_line_legend2 = gsn_add_polyline(wks,xy,(/xlg2_cen,xlg2_cen/), \
   259 ;                                           (/ylg2_cen-60,ylg2_cen+60/),lnres)
   260 ; lg_cap_legend21  = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \
   261 ;                                           (/ylg2_cen-60,ylg2_cen-60/),lnres)
   262 ; lg_cap_legend22  = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \
   263 ;                                           (/ylg2_cen+60,ylg2_cen+60/),lnres)
   264 ; tx_legend2 = gsn_add_text(wks,xy,"model_i01.03cn",xlg2_cen+0.15,ylg2_cen,txres)
   265 
   266   draw(xy)
   267   frame(wks)
   268   system("convert xy.ps xy.png")
   269 
   270  u = yvalues(0,:)
   271  v = yvalues(1,:)
   272 
   273  good = ind(.not.ismissing(u) .and. .not.ismissing(v))
   274  uu = u(good)
   275  vv = v(good)
   276  nz = dimsizes(uu)
   277  print (nz)
   278 
   279  ccr = esccr(uu,vv,0)
   280  print (ccr)
   281 
   282 ;old eq
   283 ;bias = sum(((vv-uu)/uu)^2)
   284 ;M    = (1.- sqrt(bias/nz))*5.
   285 
   286 ;new eq
   287  bias = sum(abs(vv-uu)/(vv+uu))
   288  M    = (1.- (bias/nz))*5.
   289  print (bias)
   290  print (M)
   291 
   292 end
   293