function mod_res, index_in, data_in, index_out, $
        update_index=update_index, $
	outres=outres, factor=factor_in, $
	qdebug=qdebug, qstop=qstop, override=override, $
	norm_fact=norm_fact
;
;+
;NAME:
;	mod_res
;PURPOSE:
;	Rebin SXT partial or full frame images, and keep track
;	of the corner pixel and resolution in the history records.
;	The signal is adjusted accordingly to conserve the total
;	charge.
;SAMPLE CALLING SEQUENCE
;	data_out = mod_res(index, data, index_out)
;	data_out = mod_res(index, data, index_out, outres=2)
;	data_out = mod_res(index, data, index_out, factor=0.429)
;INPUT:
;	index	- The SXT index record
;	data	- The data image or data cube.  The data must already be 
;		  decompressed AND background subtracted.  The output
;		  data type is the same as the input.
;OUTPUT:
;	index_out- The updated SXT index record
;	data_out- The rebinned data
;OPTIONAL KEYWORD INPUT:
;	outres	- The output resolution to rebin to.  Default is
;		  full resolution.  0=FR, 1=HR, 2=QR
;	factor	- The multiple of FR pixels that the image should
;		  be rebinned to.  FACTOR=0.6 means make the pixels
;		  0.6 FR is size (2.45*0.6 = 1.47 arcseconds).
;		  This should only be used if it "outres" cannot be used
;	norm_fact - If set to 1, then do not normalize the signal level
;		  for the number of pixels binned/spread.
;		  The default is to divide by the number of pixels that
;		  the charge is being spread into.
;		  For example, when going from HR to FR the signal is
;		  from one pixel is spread into 4 pixels.  The default
;		  is to divide the signal by 4.  /NORM_FACTOR will 
;		  leave the signal as is.
;		  If set to 2, then normalize by the sqrt(# pixels
;		  the charge is spread into.  For the example above,
;		  divide the signal by 2.  This is used for the handling
;		  the uncertainty arrays.
;METHOD:
;	It uses CONGRID when going to higher resolutions (simply
;	duplicates the pixel the proper number of time) and it uses
;	SXT_SUMXY for binning to lower resolutions (bin the pixels
;	accordingly)
;
;	For full resolution images, it shifts the image by the number
;	of columns necessary to align the image properly.
;
;	Replaces CHANGE_RES
;HISTORY:
;	Written 13-Aug-93 by M.Morrison
;	26-Aug-93 (MDM) - Added /UPDATE_INDEX
;	16-Sep-93 (MDM) - Added NORM_FACTOR option
;	16-Nov-93 (MDM) - Small patch for case where there is no resolution
;			  change
;	18-Nov-93 (MDM) - Thrashing around - fixed a problem which was not a problem
;			  - reversed it back to the original
;	13-Mar-95 (MDM) - Corrected anomaly where .SHAPE_SAV did not match
;			  the data array
;-
;
his_index, /enable
;
siz = size(data_in)
n = 1
if (siz(0) eq 3) then n = siz(3)
typ = siz( siz(0) + 1)
;
if ((typ eq 1) and (not keyword_set(override))) then begin
    tbeep, 3
    message, 'Data array input must be non-BYTE type.  Returning...', /info
    return, 0
end
;
if (n_elements(outres) eq 0) then outres = 0
if (n_elements(factor_in) eq 0) then begin
    factor = 2.^gt_res(index_in) / 2.^gt_res(outres)		;an array of the factor to adjust each image by
end else begin
    factor = 2.^gt_res(index_in) * factor_in		
end
;
index_out = index_in
res_in = gt_res(index_in)
shape = gt_shape(index_in)
nx = shape(0,*)		& nnx = nx
ny = shape(1,*)		& nny = ny
;
typ_out = typ			;match the input type (might have data overflow with large binning?)
nxout = max(nx*factor)
nyout = max(ny*factor)
data_out = make_array(nxout, nyout, n, type=typ_out)
;
for i=0,n-1 do begin
    index0 = index_in(i)
    factor0 = factor(i)
    npix = 2.^res_in(i)					;1,2 or 4
    corn_shift = (npix-1)/2. - (npix/factor0-1)/2.


    ;The center of the lower left corner pixel changes when the resolution changes
    ;EXAMPLE:	HR -> FR ==> factor0 = 2	npix = 2 	==> corn_shift = 0.5 - 0 = 0.5
    ;           QR -> FR ==> factor0 = 4	npix = 4	==> corn_shift = 1.5 - 0 = 1.5
    ;		HR -> QR ==> factor0 = 0.5	npix = 2	==> corn_shift = 0.5 - 1.5  = -1.0

    new_corn = gt_corner(index0) - corn_shift

    data0 = data_in(0:nx(i)-1, 0:ny(i)-1, i)
    ix0 = 0
    if (factor(i) ne 1) then begin	;change in resolution required
	ffi = gt_pfi_ffi(index0, /true_ffi)
	if (ffi) then begin			;FFI case
	    if (keyword_set(qdebug)) then print, 'SXT_REBIN: Processing FFI case'

	    ;---- See page 96 of the "Blue Book" to understand the peculiarities of the column summation
	    ist_arr = [3, 1, 0]
	    ffi_corn = [0, 1.5, 4.5]	;x address of the corner of full frame images
	    data0 = data0(ist_arr(res_in(i)):nx(i)-2, 0:ny(i)-1)
	    ;-- strip off first few columns (image is reversed) to make sure that when re-binning
	    ;-- the image is registered.  Also skip the first column since it is not binned (BLS)
	    ix0 = 0
	    off = (npix/factor0-1)/2.	;0.0, 0.5, or 1.5
	    new_corn(0) = new_corn(0) + ist_arr(res_in(i))*npix		;if we don't do the step below, we need
									;the corner address to be correct for the sub-image
									;that was re-binned.  We removed "ist_arr(res_in(i))"
									;columns (* "npix" to convert to full resolution)

	    if (not keyword_set(factor_in)) then begin		;align the sun in the image the way it would appear in
								;an exposure of the output resolution
		ix0 = ist_arr(outres)
		off = (npix/factor0-1)/2.	;0.0, 0.5, or 1.5
		new_corn = [ffi_corn(outres), off]
		if (keyword_set(qdebug)) then print, 'SXT_REBIN: FFI case being adjusted'
	    end
	end

	siz2 = size(data0)
	nnx = siz2(1)
	nny = siz2(2)

	norm_factor = factor0^2
	if (keyword_set(norm_fact)) then if (norm_fact eq 1) then norm_factor = 1	;no normalization (sat pix array)
	if (keyword_set(norm_fact)) then if (norm_fact eq 2) then norm_factor = factor0	;sqrt normalization (uncert array)

	if (factor0 gt 1) then begin			;making the array larger (higher resolution)
	    if (keyword_set(qdebug)) then print, 'SXT_REBIN: FACTOR > 1, using CONGRID
	    data0 = congrid(data0, nnx*factor0, nny*factor0) / norm_factor
	end else begin					;making the array smaller (lower resolution)
	    ifactor = 1/factor0		;inverse of factor
	    if ( (fix(ifactor)-ifactor) eq 0) then begin	;integer multiple
		if (keyword_set(qdebug)) then print, 'SXT_REBIN: FACTOR < 1, using SXT_SUMXY
		;;;sxt_sumxy, index0, data0, ifactor, ifactor, /silent	;bins the data
		data0 = congrid(data0, nnx*factor0, nny*factor0, /interp) / norm_factor
	    end else begin
		if (keyword_set(qdebug)) then print, 'SXT_REBIN: FACTOR < 1, using CONGRID
		data0 = congrid(data0, nnx*factor0, nny*factor0, /interp) / norm_factor
	    end
	end
    end

    if (keyword_set(factor_in)) then his_index, index_out, i, 'pixel_size', -1*factor_in $
				else his_index, index_out, i, 'pixel_size', outres
    his_index, index_out, i, 'corner_sav', new_corn
    ;index_out(i).sxt.shape_sav = fix([nnx, nny]*factor0)
    index_out(i).sxt.shape_sav = [nxout, nyout]		;MDM patch 13-Mar-95
    data_out(ix0, 0, i) = data0
end
;
if (keyword_set(update_index)) then index_in = index_out
if (keyword_set(qstop)) then stop
return, data_out
end
