##
## "VIEW", a data viewing program,
##   Copyright (C) 1987, 1990 California Institute of Technology.
##   Original authors: Dave Gillespie, port by Rick Koshi
##   Authors of macro in this file: Tobi Delbruck and Dave Gillespie
##   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. 
##
##
## CAGED_process="do $F $M"

## makes a curve out of a data curve and an error curve
## new curve plots as error bars of original data

## Originally by Tobi Delbruck
## Modified by Dave Gillespie


define errbar  curve = data : error
   local clength = cvlen(data)
   local data_plus = ($data) + ($error)  ## also checks if bases are compatible
   local data_minus = ($data) - ($error)
   local tmpcurve tmpcurve_x
   local _i _j     ## just in case the data or error are called i or j
   local small_number=1e-5  ## so basis values are unique
   basis tmpcurve_x = 1:3*clength-1:1
   tmpcurve = cvidx(data, clength+1) : tmpcurve_x   ## fill with undef points
   for _i = 1 : clength
      _j = 3*_i-2
      if cvvalid('data_plus', _i)
         poke tmpcurve_x@_j    =  cvidx(data+'_x',    _i)*(1-small_number)
         poke tmpcurve  @_j    =  cvidx('data_plus',  _i)
         poke tmpcurve_x@_j+1  =  cvidx(data+'_x',    _i)*(1+small_number)
         poke tmpcurve  @_j+1  =  cvidx('data_minus', _i)
         poke tmpcurve_x@_j+2  =  cvidx(data+'_x',    _i)*(1+2*small_number)
      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>.
addhelp   >  Error bars go from <data>-<error> to <data>+<error>.  The
addhelp   >  curve <error> may be any curve expression with <data>'s basis.

