##
## "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
##   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. 
##
##
## makes a curve out of a data curve and an error curve
## new curve plots as error bars of original data
## needs hacking for saner undefined points

## CAGED_process="do $F $M"

define errbar  curve = data : error
   local clength
   local tmpcurve tmpcurve_x
   local data_x=${data}_x
   local small = 1E-9
   local undef
   if cvlen(data)<>cvlen(error)
      echo $data and $error have different basis
      quit [proc]
   endif
   basis undef=0:1:1
   ##undef=undef+cvrelidx('undef',2)
   undef=log(undef)
   clength=cvlen(data)
   bas tmpcurve=1:4*clength:1
   tmpcurve_x=tmpcurve
   tmpcurve=0:tmpcurve
   tmpcurve=log(tmpcurve)     ## some severe kludgery here for undefined's
   for i=1:clength:1
      local j=4*(i-1)+1
      if cvvalid(data,i)&&cvvalid(error,i)
           ##poke tmpcurve@j         =  cvidx('undef',1)
         poke tmpcurve_x@j       =  cvidx('data_x',i)*(1-2*small)
         poke tmpcurve@(j+1)     =  cvidx(data,i)-cvidx(error,i)/2
         poke tmpcurve_x@(j+1)   =  cvidx('data_x',i)*(1-small)
         poke tmpcurve@(j+2)     =  cvidx(data,i)+cvidx(error,i)/2
         poke tmpcurve_x@(j+2)   =  cvidx('data_x',i)*(1+small)
           ##poke tmpcurve@(j+3)     =  'x'   ## hack for undefined point
         poke tmpcurve_x@(j+3)   =  cvidx('data_x',i)*(1+2*small)
      endif
   endfor
   splice $curve=tmpcurve_x:tmpcurve
enddef

addhelp errbar curve=data:error
addhelp   >  make <curve> of error bars for <data> with errors from <error>

