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