##
## "VIEW", a data viewing program,
##   Copyright (C) 1987, 1990 California Institute of Technology.
##   Original authors: Dave Gillespie, port by Rick Koshi
##   Unix Port Maintainer: John Lazzaro
##   Author of this macro: Tobi Delbruck
##   Maintainers's address: lazzaro@hobiecat.cs.caltech.edu;
##                          CB 425/ CU Boulder/Boulder CO 91125. 
##
##
##This program is free software; you can redistribute it and/or modify
##it under the terms of the GNU General Public License as published by
##the Free Software Foundation (Version 1, Feb 1989).
##
##This program is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##GNU General Public License for more details.
##
##You should have received a copy of the GNU General Public License
##along with this program; see the file ../COPYING.  If not, write to
##the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
##
##

## histogramming VIEW functions
## by Tobi Delbruck

define histogram hist = curve : min : max : number
   local incr i len y tmp index tmphist
   incr = (($max-$min)/$number)
   basis tmphist = $min : $max : incr
   len=cvlen(curve)
   for i=1:cvlen('tmphist'):1
      poke tmphist @ i = 0
   endfor
   for i=1:len:1
      y=cvidx(curve,i)
      index= floor((y-$min)/$incr)+1
      tmp = cvidx('tmphist',index)+1
      poke tmphist @ index = tmp
   endfor
   basis $hist=1:(3*($number)+1):1
   local ${hist}_x=$hist
   poke ${hist} @ 0 = 0
   poke ${hist} @ 1 = 0
   for i=0 : $number : 1
      local val=($min+i*incr)
      poke $hist @ (3*i) = cvidx('tmphist',(i<1?1:i))
      poke ${hist}_x @ (3*i) = val
      poke ${hist} @ (3*i + 2) = cvidx('tmphist',(i+1))
      poke ${hist}_x @ (3*i+1) = val
      poke ${hist} @ (3*i + 1) = 0
      poke ${hist}_x @ (3*i+2) = val
   endfor
   poke ${hist} @ (3*${number}) = cvidx('tmphist', $number )
   poke ${hist}_x @ (3*${number}) = $max
   splice ${hist} =  ${hist}_x : ${hist}
enddef


define [before] histogram hist = curve : number
   local i len min max
   len = cvlen(curve)
   min=cvidx(curve,1);; max=min
   for i=2:len:1
      min=(min>cvidx(curve,i)?cvidx(curve,i):min)
      max=(max<cvidx(curve,i)?cvidx(curve,i):max)
   endfor
   print 'min=' min '  max=' max
   histogram $hist = $curve : $min : $max : $number
enddef

define [before] histogram hist = curve
   histogram $hist = $curve : 20
enddef

addhelp histogram <hist> = <curve> : <min> : <max> : <number>
addhelp histogram <hist> = <curve> : <number>
addhelp histogram <hist> = <curve>
addhelp  >  histogram values in <curve> from <min> to <max> with <number> bins
addhelp  >  If <min> and <max> are omitted values are automatically ranged
addhelp  >  If <number> is omitted 20 bins are used


