function rest_low8_cube, index, data, qdebug=qdebug
;+
;NAME:
;	rest_low8_cube
;PURPOSE:
;	To restore a cube of low8 data.  The input data must
;	consist of low-8 AND compressed data.  The output will
;	have the same number of images as the input (ie: some
;	images are simple decompressed images, not 12-bit images)
;RESTRICTION:
;	The input data needs to all be all of the same type (ie:
;	all PFI data or all FFI data).  Do not mix image types.
;SAMPLE CALLING SEQUENCE:
;	data = rest_low8_cube(index, data)
;INPUT:
;	index	- The index structure
;	data	- The data cube
;OUTPUT;
;	Returns the 12-bit restored data.
;HISTORY:
;	Written 23-Mar-95 by M.Morrison
;	11-Apr-95 (MDM) - Added the /PFI_STRIP command to the 
;			  GT_CENTER call so the correct PFI strip
;			  is selected.
;			- Modified so that the final image selection
;			  it to minimize the POINTING error, not the
;			  closest image in time
;			- Major modification to shift the compressed
;			  image to the pointing of the low-8 image
;			  TAKING DIFFERENTIAL ROTATION INTO ACCOUNT!!!
;	17-Apr-95 (MDM) - Added a restriction that only PFI data be
;			  passed in.
;-
;
;;qdebug=1
;;qdebug2 =1
n = n_elements(index)
cen = gt_center(index, /angle, /pfi_strip)	;arcseconds
suncen = sxt_cen(index)
comp = gt_comp(index)
;
out = sxt_decomp(data)		;make the output INTEGER*2
;
for i=0,n-1 do begin
    index0 = index(i)
    ;
    if (comp(i) eq 1) then begin	;low-8 image
	;---- pnt offset between low-8 image and all images (in output pixels)
	;;diff_arr = cen
	;;for j=0,n-1 do begin
	;;    diff_rot_factor = gt_center(index(j),/angle,/cmd) - $
	;;			track_h2a(index(j), gt_center(index(i), /cmd, /helio), index(i))
	;;    diff_arr(0,*) = diff_rot_factor(0) - (cen(0,*)-cen(0,i))
	;;   diff_arr(1,*) = diff_rot_factor(1) - (cen(1,*)-cen(1,i))
	;;end
	;;pnt_err = sqrt( (diff_arr(0,*)-diff_arr(0,i))^2 + (diff_arr(1,*)-diff_arr(1,i))^2 )	/ gt_pix_size(index0)
	;;--^^ code is probably the right thing to do, but it is slow and does not appear to make a better choice
	pnt_err = sqrt( (cen(0,*)-cen(0,i))^2 + (cen(1,*)-cen(1,i))^2 )	/ gt_pix_size(index0)

	;---- find compressed, close in pointing, and the same filter
	ss3 = where((comp eq 0) and (pnt_err lt 4) and (gt_filta(index) eq gt_filta(index0)), nn3)
	if (keyword_set(qdebug)) then print, nn3, ' compressed images to choose from'
	if (nn3 ne 0) then begin
	    ;; ii1 = ss3(tim2dset(index(ss3), index0))
	    ii1 = ss3( where(pnt_err(ss3) eq min(pnt_err(ss3))) )
	    ii1 = ii1(0)
	    ;;if (keyword_set(qdebug)) then print, i, '  ', fmt_tim(index0), ii1, reform(diff_arr(*,i)-diff_arr(*,ii1)), pnt_err(ii1)
	    if (keyword_set(qdebug)) then print, i, '  ', fmt_tim(index0), ii1, pnt_err(ii1)
	    diff_rot_factor = gt_center(index(ii1),/angle,/cmd) - $
				track_h2a(index(ii1), gt_center(index(i), /cmd, /helio), index(i))
	    diff_rot_factor = diff_rot_factor / gt_pix_size()
	    dx = ( suncen(0,ii1)-suncen(0,i) ) 
	    dy = ( suncen(1,ii1)-suncen(1,i) ) 
	    xc = gt_corner(index(i), /x) - (diff_rot_factor(0)-dx)	;comp to right of low8==>xc is +;move comp corner to the left
	    yc = gt_corner(index(i), /y) - (diff_rot_factor(1)-dy)
	    nx = gt_shape(index(i), /x)
	    ny = gt_shape(index(i), /y)
	    comp_img = out(*,*,ii1)
	    index00 = index(ii1)
	    align1img, index00, comp_img, xc, yc, nx, ny, ox0, oy0, gt_res(index(i)) ;, /fast ,qdebug=qdebug2	;MDM added 11-Apr-95
	    if (keyword_set(qdebug)) then print,xc,yc,nx,ny,dx,dy,ox0,oy0,diff_rot_factor
	    comp_img2 = intarr(nx,ny)
	    comp_img2(ox0,oy0) = temporary(comp_img)
	    out0 = restore_low8( data(*,*,i), comp_img2 )
	    ss33 = where(comp_img2 eq 0, n33)
	    if (n33 ne 0) then out0(ss33) = 0	;mask out where there was no compressed data
	    out(0,0,i) = out0
	    if (keyword_set(qdebug2)) then begin
		wdef, 1, 512, 950, /already
		tvscl,rebin(data(*,*,i),128*4,64*4,/samp)	;low-8 data
		ocontour,comp_img2,bin=4			;
		tvscl,rebin(out(*,*,i),128*4,64*4,/samp),0,256
		tvscl,rebin(data(*,*,ii1),128*4,64*4,/samp),0,512
		tvscl,rebin(comp_img2,128*4,64*4,/samp),0,768
		pause
	    end
	end else begin
	    print, 'Cannot find COMPRESSED image for ', fmt_tim(index0), '.  Returning low-8'
	end
    end
end
;
return, out
end
