Viewing contents of file '../idllib/ghrs/pro/boxave.pro'
function boxave,array,xsize,ysize
;+
; NAME:
;   BOXAVE
; PURPOSE:
;   Box-average a 1 or 2 dimensional array.   This procedure differs
;   from REBIN in that INTEGER arrays are boxaveraged using REAL arithmetic.
;   For boxaveraging REAL and DOUBLE arrays, the REBIN function should be
;   used (it is faster).
; CALLING SEQUENCE:
;   RESULT = BOXAVE(ARRAY)	        ;Prompt for Box Size
;   RESULT = BOXAVE(ARRAY,XSIZE)        ;Use a square box
;   RESULT = BOXAVE(ARRAY,XSIZE,YSIZE)	;Rectangular box for a 2-D array
; INPUTS:
;   ARRAY - Two dimensional input Array to be box-averaged.  Array may be 
;           one or 2 dimensions and of any type except character.   The
;           REBIN function is faster for floating or double arrays.
; OPTIONAL INPUTS:
;   XSIZE - Size of box in the X direction, over which the array is to
;           be averaged.  If omitted, program will prompt for this 
;           parameter.  
;   YSIZE - For 2 dimensional arrays, the box size in the Y direction.
;           If omitted, then the box size in the X and Y directions are 
;           assumed to be equal
; OUTPUT:
;  RESULT - Output array after box averaging.  If the input array has 
;           dimensions XDIM by YDIM, then RESULT has dimensions
;           XDIM/NBOX by YDIM/NBOX.  The type of RESULT is the same as
;           the input array.  However, the averaging is always computed using
;           REAL arithmetic, so that the calculation should be exact.
;           If the box size did not exactly divide the input array, then
;           then not all of the input array will be boxaveraged.
; PROCEDURE:
;   Program boxaverages all points simultaneously using vector subscripting
; REVISION HISTORY:
;   Written, W. Landsman, October 1986
;   Modified for version 2 IDL, B. Pfarr, Jan 1990
;-
npar = n_params(0)
if npar lt 2 then read,'BOXAVE: Enter box size: ',xsize 
if npar lt 3 then ysize = xsize
s = size(array)
ninx = s(1)                                  
noutx = ninx/xsize	
type = datatype(array)
if s(0) eq 1 then begin		; 1 dimension?
	output = fltarr(noutx)
	counter = indgen(noutx)*xsize
	for i=0,xsize-1 do output = output + array(counter + i)
	nboxsq = float(xsize)
endif else begin		; 2 dimensions
	niny = s(2)
	nouty = niny/ysize
	nboxsq = float(xsize*ysize)
	output = fltarr(noutx,nouty)         ;Real*4 arithmetic
	counter = lindgen(noutx*nouty)	
	counter = xsize*(counter mod noutx) + (ysize*ninx)*fix((counter/noutx))
;
	for i=0,xsize-1 do $
	for j=0,ysize-1 do $
	     output = output + array(counter + (i + j*ninx))
endelse
;
if type eq 'INT'  then  $
       return,nint(output/nboxsq) $          ;Convert back to integer format?
else if type eq 'BYT' then $ 
       return,byte(output/nboxsq) $          ;Convert back to byte format?
else return,output/nboxsq
end