; $Id: hfbs_rebinonedimdata.pro,v 1.4 2005/02/09 16:58:01 dimeo Exp $
;###############################################################################
;
; NAME:
;  HFBS_REBINONEDIMDATA
;
; PURPOSE:
;  Old rebinning routine that has been replaced in most applications
;  by DREBIN.
;
; CATEGORY:
;  DAVE, HFBS, utility
;
; AUTHOR:
;   Robert M. Dimeo, Ph.D.
;   NIST Center for Neutron Research
;   100 Bureau Drive
;   Gaithersburg, MD 20899
;   Phone: (301) 975-8135
;   E-mail: robert.dimeo@nist.gov
;   http://www.ncnr.nist.gov/staff/dimeo
;
; LICENSE:
;  The software in this file is written by an employee of
;  National Institute of Standards and Technology
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used of if the code in this file is
;  included in another product.
;
;###############################################################################
;==========================================
;+
; NAME:
;       hfbs_RebinOneDimData
;
; PURPOSE:
;       Fast rebinning algorithm for one-dimensional data
;
; CATEGORY:
;       Mathematical routine
;
; CALLING SEQUENCE:
;       Result = hfbs_RebinOneDimData(xobins,nbins,yo,yoerr)
;
; INPUTS:  xobins:    original bins
;          nbins:     new number of bins
;          yo:        unbinned data
;          yoerr:     error bars for unbinned data
;
;
; KEYWORD PARAMETERS:
;       None
;
; OUTPUTS:
;       This function returns a three dimensional array consisting of the
;       bins, data, and new error bars
;
; COMMON BLOCKS:
;       None.
;
; PROCEDURE:
;
;
; SIDE EFFECTS:
;       None known
;
; EXAMPLE:
;
;
; MODIFICATION HISTORY:
;       Written by:
;       Robert Dimeo
;       robert.dimeo@nist.gov
;       November 17, 1999
;
function hfbs_RebinOneDimData,xobins,nbins,yo,yoerr
oldpts = n_elements(xobins)
newpts = nbins
if nbins ge oldpts then begin
  print,'error:  number of bins,',nbins,', cannot exceed number of existing channels,',oldpts
  yout = fltarr(newpts,3)
endif else begin
  binlo = xobins[0] & binhi = xobins[oldpts-1]
  dbin = (binhi-binlo)/(newpts-1)
  newbins = fltarr(newpts)
  newbins = binlo+dbin*findgen(newpts)
  yn = fltarr(newpts)
  ynerr = fltarr(newpts)
  for i = 0,newpts-1 do begin
    wherevalid = where(abs(xobins-newbins[i]) le 0.5*dbin,count)
    if count eq 0 then begin
      yn[i] = 0.0 & ynerr[i] = 0.0
    endif else begin
      yn[i] = total(yo[wherevalid])/count
      ynerr[i] = sqrt(total((yoerr[wherevalid])^2))/count
    endelse
  endfor
  yout = fltarr(newpts,3)
  yout[0:newpts-1,0] = newbins[0:nbins-1]
  yout[0:newpts-1,1] = yn[0:nbins-1]
  yout[0:newpts-1,2] = ynerr[0:nbins-1]
endelse
return,yout
end