pro sxt_sff_composite,index,data,comp_index,comp_data, $
	smooth_factor=smooth_factor, _extra=_extra,    $
        sxt_cleanx=sxt_cleanx, vignette=vignette, destreak=destreak, $
        fillpix=fillpix, unc_out=unc_out, fillmap=fillmap
;+
; NAME:
;   sxt_sff_composite
;
; PURPOSE:
;   Prepare a composite SXT image from 2 or 3 Half or Quarter Res images.
;
; CALLING SEQUENCE EXAMPLES:
;
; INPUTS:
;   index = SXT index structure
;   data  = 3-d data cube (at least 2 images)
;
;
; OUTPUT:
;   comp_index - the adjusted index of the composite (history added)
;   comp_data  - the composite data array
;
; OPTIONAL INPUT KEYWORDS:
;
;   smooth_factor = Boxcar smooth the bleed pixels.  If not defined, the
;	     default is to smooth over 5 pixels.
;   _extra - all others passed through to sxt_prep
;  fillmap - map of pixels filled from SHORT(1) or MEDIUM(2)
;
; MODIFICATION HISTORY:<F5
;   15-sep-98  S.L.Freeland - from LWA 'sxt_composite.pro'
;              stripped version using 'new technology'
;              Calls sxt_prep internally
;   21-Mar-99  S.L.Freeland - call sxt_clean independent from sxt_prep
;              add /destreak, update history 
;    7-Apr-99  S.L.Freeland - verify all indices->.HIS.time_compos/.day_compos
;   26-Oct-99  S.L.Freeland - enabled keyword inherit->sxt_prep
;   18-Dec-00  S.L.Freeland - merge LWA 'sxt_suture'
;   15-Jan-01  S.L.Freeland - merge sxt_composite mods (fix 18-dec mod)
;                             "released" as version 5.0
;
;   18-Jan-2001 - S.L.Freeland - add FILLMAP output keyword
;
;    Previous History:
;    8-Nov-91, L. W. Acton (Wrote composite)
;   19-May-92, LWA  (Several more mods in betwee)
;   13-may-93, J. R. Lemen, Re-wrote.  Calls sxt_satpix
;    1-Dec-93, MDM - Corrected error with the use of "s2" (when it is -1)
;		- Modification on selection of replacement pixels.
;		  It used to check (a) that it was high signal (ge 90)
;				   (b) that it was not saturated
;		  Added check so that it replaced pixels in the longer 
;		  exposure only when the longer exposure was saturated
;    15-Dec-93, SLF - Fixed typo (stapix->satpix)
;    22-feb-94, JRL - Corrected the algorithm; 
;		    - added smooth_factor, sfd, dc_interpolate, registeq
;    17-Apr-94, LWA - Fixed /sfd scaling option (1000.*  was needed)
;		    - Changed /plot to plot raw input images.
;		    - Added RESTRICTIONS statement to header.
;		    - Added despike=despike keyword.
;    23-jun-94, JRL - Output is floating if input is a fltarr;
;		    - Call sfd_comp with index=index to normalize properly
;    08-May-95, LWA - Fixed to return negative values.
;    02-Nov-96, LWA - Added output of index with history record.
;		    - Made dc_data a keyword intput.
;		    - Made dc_interpolate the default is dc_data not input.
;    04-Nov-96, LWA - Removed forgotten diagnostic.
;    10-Jan-97, LWA - Corrected calling sequence examples.
;-

progverno=5.0

destreak=keyword_set(destreak)
cleanit=keyword_set(sxt_cleanx) or destreak

nparams = n_params()
if nparams lt 4 then begin
   box_message,'IDL> sxt_sff_composite,index,data,compindex,compdata
   return
endif

;-------------------------
;  Check input parameters
;-------------------------

sz0 = size(data)			; Data must be 3-d
nimg=data_chk(data,/nimage)

if nimg lt 2 or n_elements(index) lt 2 then begin
   print,'***  Error in sxt_composite:  index,data must be 3-d'
   return
endif

exporder=sort(gt_expdur(index))

; ----- the folling call orders output short->long
sxt_prep,index(exporder),data(*,*,exporder),cindex,cimage,unc,satp, $
  /register,/float,/dc_orbit_correct,ref_image=index(exporder(nimg-1)), $
  vignette=vignette, _extra=_extra               ; sxt_clean=sxt_clean

xsize = data_chk(img,/nx)
ysize = data_chk(img,/ny)

; ---------------------------------------------------------------------------

comp_data=sxt_composite(cindex,cimage,comp_index, $
   satpix=satp, unc_in=unc, unc_out=unc_out, fillmap=fillmap)
                  
his_index, /enable

; do clean/destreak after the fact
if cleanit then begin
   if n_elements(nsigma) eq 0 then nsigma=3
   comp_data=sxt_clean(comp_index, comp_data, nsigma=nsigma, xsaa=destreak)   
   his_index, comp_index, 0, 'extra1', nsigma
   his_index, comp_index, 0, 'q_extra', 4 + destreak ; 4=> clean (only)
endif

time_compos=gt_time(cindex)
day_compos =gt_day(cindex)

his_index, comp_index, 0, 'q_composite', progverno*1000
his_index, comp_index, 0, 'time_compos', time_compos
his_index, comp_index, 0, 'day_compos', day_compos

return
end

