function rest_low8_ffi,index,data,qdebug=qdebug,bug2=bug2,lc=lc,$
	comp_out=comp_out
;+
;NAME:
;	rest_low8_ffi
;PURPOSE:
;	To restore a cube of low8 FFI 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 FFI data.
;	Program requires raw SFR data.
;	Low8 and compressed images must be taken within 20 minutes
;	   of each other.  This is determined in sxt_low8_match.pro.
;SAMPLE CALLING SEQUENCE:
;	data = rest_low8_ffi(index, data)
;INPUT:
;	index	- The index structure
;	data	- The data cube, may be mixed resolutions and 
;		  can contain any sort of images in addition
;		  to the NaBan and WdBan images.
;OPTIONAL KEYWORD INPUT
;	/qdebug, stop before return.
;	/bug2, create window 12 and show diagnostic alignment images, stop.
;OUTPUT
;	data, Returns interger datacube with 12-bit restored images replacing
;	  all for qualifying low8 images.  Comp images are only decompressed.
;	  Returned datacube has been decompressed but
;	  not otherwise processed.  
;	index, The index structure is changed to reflect the image
;	  alignment and Low8 restoration.
;OPTIONAL KEYWORD OUTPUT
;       lc, Array of subscripts, dimensioned 2 x n where n is the number
;         of elements in the index array.  All non-optical (e.g., NaBan,
;         WdBan or diffuser) entries in the array are set=-1.  The (0,n) values
;         are the subscripts of the Low8 images.  The (1,n) values
;         are the subscripts of the Compressed image to use.
;         If no valid Compressed image exists, (1,n) = -1.
;	comp_out, returns the shifted and decompressed compressed images.
;PROCEDURE
;       Except for diffuser images align_prep and align1img are used 
;         to align the raw compressed image to the raw low8 image
;	  priot to restore_low8.
;         Alignment shift isn't done unless dx or dy are > 0.5 FR pixel.
;HISTORY:
;	 2-Nov-2004 LWA  Written, borrowing from rest_low8_cube.
;       11-Nov-2004 S.L.Freeland - update HIS vis modified restore_low8
;       13-Dec-2004 S.L.Freeland - externalize HIS properly
;	10-Feb-2005 LWA  Set up to handle diffuser images
;	30-Apr-2008 LWA  Clarified header.
;	 1-Sep-2008 LWA  Modified ocontours for /bug2, updated header.
;	 4-Sep-2008 LWA  Major revision, replace sxt_prep w/ align1img.
;-

if not required_tags(index,'HIS') then his_index,index,/enable
qdebug=keyword_set(qdebug)
siz=size(data)
cx=siz(1)
cy=siz(2)
if qdebug then help,index,data
dat=data
ind=index
out=sxt_decomp(data)

lc=sxt_low8_match(index)
oklc=where(lc(1,*) ne -1,nne)
comp_out=intarr(cx,cy,nne)
if qdebug then help,out,oklc,nne
if (nne ne 0) then begin
   for i=0,nne-1 do begin	
      l8=lc(0,oklc(i))
      c8=lc(1,oklc(i))
      both_ind=index([l8,c8])
      res=gt_res(both_ind(0))
      outres=res
      case 1 of
         res eq 1 : begin & nx=512 & ny=512 & end
         res eq 2 : begin & nx=256 & ny=256 & end
      endcase
      ;Align C image to L image, no roll correction (usually tiny).
      suncen=sxt_cen(both_ind)
      dx=suncen(0,0)-suncen(0,1)
      dy=suncen(1,0)-suncen(1,1)
      if qdebug then help,nx,ny,dx,dy
      if gt_filta(index(l8)) ne 4 and (abs(dx) gt 0.5 or abs(dy) gt 0.5) then begin 
	 align_prep,both_ind(1),xx,yy,sc,code,ref_image=both_ind(0),qroll=0
         oddeven=nx mod 2                ; added 20-Nov-98 by DMcK
         xc = xx - (nx-(1-oddeven))/2.*2^outres
         oddeven=ny mod 2
         yc = yy - (ny-(1-oddeven))/2.*2^outres
	 if qdebug then begin
	    print,get_info(both_ind,/non)
	    help,l8,c8,xx,yy,xc,yc
	    print,xx,yy,xc,yc
	 endif   
	 if res eq 2 then begin
	    low8=data(0:255,0:255,l8)
	    comp=data(0:255,0:255,c8) 
	 endif else begin
	    low8=data(*,*,l8)
	    comp=data(*,*,c8)
	 endelse   
	 if qdebug then begin
	    help,comp
	    qstop=1 
	 endif else begin
	    qstop=0
	 endelse   
	 align1img,both_ind(1),comp,xc,yc,nx,ny,$
	    ox0,oy0,res,typout=1,typ_in=1,qdebug=qdebug,$
	    qstop=qstop
        if qdebug then help,comp 
        if (keyword_set(qdebug)) then print, 'Image being inserted into: ox0, oy0, i', ox0, oy0, i
        if ((ox0 lt nx-1) and (oy0 le ny-1)) then begin
	   tmp=bytarr(nx,ny)
           csiz=size(comp)
           tmp(ox0:ox0+csiz(1)-1,oy0:oy0+csiz(2)-1) = comp
	   comp=tmp
         endif
      endif else begin
	 if res eq 2 then begin
	    low8=data(0:255,0:255,l8)
	    comp=data(0:255,0:255,c8) 
	 endif else begin
	    low8=data(*,*,l8)
	    comp=data(*,*,c8)
	 endelse   
      endelse	 
      ;Now do the restoration.
      lo_index=both_ind(0)
      comp_index=both_ind(1)
      out(0,0,oklc(i)) = restore_low8(low8,comp, $
	 index=lo_index,$
         comp_index=comp_index)
      index(l8)=lo_index
      if (keyword_set(bug2)) then begin
         wdef,12,cx*2,cy,/already,/ur
         tvscl,low8	;low-8 data
         ocontour,comp,c_thick=2
         tvscl,comp,cx,0
         ocontour,comp,c_thick=2,color=1,cx,0
      endif
   comp_out(0,0,i)=sxt_decomp(comp)
   endfor
endif
;if keyword_set(qdebug) then stop
return, out
end
