pro composite,cimg,data,index,dc_data,sx=sx,sd=sd 
;+ 
; pro composite,cimg,data,index,dc_data,sx=sx,sd=sd 
; 
; NAME: 
; 	COMPOSITE 
; PURPOSE: 
; 	Prepare a composite image from 2 or 3 SXT Half Res. images 
; CALLING SEQUENCE: 
; 	composite,cimg,data,index,sx=[16,17]
;	composite,cimg,data,index,dc_data,sx=[1,2,3],sd=[0,2,1] 
; INPUTS:
; 	data = 3 dimensional image array 
; 	index = corresponding index array 
; 	sx = vector of image indices data in order:
; 		shortest exposure, longer exposure, longest exposure 
;	cimg = name of output fltarr output image
; OPTIONAL INPUT PARAMETERS: 
;	dc_data = 3 dimensional dark frame array
;	sd = vector of dark frames indices
;	NOTE: If dc_data and sd are not included then the program
;	obtains the dark frames from GET_DC_IMAGE
; SPECIAL FEATURES:
;	Prepare 1024x1024 window to view progress.
;	cimg is put to window in log scaling but output in linear
; VERSION:
;       V1.1    8-May-92
; HISTORY:
;	lwa 11/08/91 ; preparation of wide dynamic range image from 3
;	lwa 1/31/91,  modified to use data and index and search strings.
;	sx = selected x-ray images in short-med-long order
;	sd = selected dark frames in short-med-long order
;	lwa 3/21/92, completely rewritten
;		installed the MDM bleed removal system 
;	lwa 5/06/92, modified to fix sxmax error
;		added possibility of separate dark current array
;	lwa 5/08/92, released
;	lwa 5/19/92, corrected oversight of missing dc_data mode.
;-

erase

;	img0 = short exposure
;	img1 = med exposure
;	img2 = longest exposure


;	Collect the dark frames
if n_elements(dc_data) eq 0 then begin
   get_dc_image,index(sx),dc_index,dc_data,sd
   tv,dc_data(*,*,0)
endif else begin
   dc_data=dc_data
endelse

;---------------- If there are 3 exposures. ----------------------------

if n_elements(sx) eq 3 and n_elements(sd) eq 3 then begin
;	Create background subtracted floating point arrays
   img0=float(sxt_decomp(data(*,*,sx(0)))-sxt_decomp(dc_data(*,*,sd(0))))
   img1=float(sxt_decomp(data(*,*,sx(1)))-sxt_decomp(dc_data(*,*,sd(1))))
   img2=float(sxt_decomp(data(*,*,sx(2)))-sxt_decomp(dc_data(*,*,sd(2))))
endif

if n_elements(sx) eq 3 and n_elements(sd) lt 3 then begin
;       Create background subtracted floating point arrays
   img0=float(sxt_decomp(data(*,*,sx(0)))-29.8)
   img1=float(sxt_decomp(data(*,*,sx(1)))-31.2)
   img2=float(sxt_decomp(data(*,*,sx(2)))-37.2)
endif

tv,data(*,*,sx(0)),0,512
tv,data(*,*,sx(1)),512,512

if n_elements(sx) eq 3 then begin
   tv,data(*,*,sx(2)),512,0
   ii = where(img0 le 0) & img0(ii) = 1.
;	Identify saturated pixels in med exposure array
  s1=where(data(*,*,sx(1)) ge 254,npts)
  xx = s1 mod 512
  yy = s1/512
  s1up = ((yy+1)<511)*512L + xx
  s1dn = ((yy-1)>0 )*512L + xx

;	Correct med and short exposures to longest exposure
   f0 = (gt_expdur(index(sx(2)))/gt_expdur(index(sx(0))))
   img0=f0(0)*img0
   f1 = (gt_expdur(index(sx(2)))/gt_expdur(index(sx(1))))
   img1=f1(0)*img1

;	Check to see if shortest exposure is required
;	If so, plug those parts into img1
   if (npts gt 0) then begin
     img1(s1) = img0(s1)
     img1(s1up) = img0(s1up)
     img1(s1dn) = img0(s1dn)
   endif

;       Identify good statistics areas in med exposure array
   m1=where(data(*,*,sx(0)) ge 90)
endif

;-----------------If there are only 2 exposures ------------------------

if n_elements(sx) eq 2 and n_elements(sd) eq 2 then begin
   img1=float(sxt_decomp(data(*,*,sx(0)))-sxt_decomp(dc_data(*,*,sd(0))))
   img2=float(sxt_decomp(data(*,*,sx(1)))-sxt_decomp(dc_data(*,*,sd(1))))
endif

if n_elements(sx) eq 2 and n_elements(sd) lt 2 then begin
   img1=float(sxt_decomp(data(*,*,sx(0)))-29.8)
   img2=float(sxt_decomp(data(*,*,sx(1)))-31.2)
endif

if n_elements(sx) eq 2 then begin
;       Correct short exposure to longer exposure
   f0 = (gt_expdur(index(sx(1)))/gt_expdur(index(sx(0))))
   img1=f0(0)*img1

;	Identify good statistics areas in short exposure array
   m1=where(data(*,*,sx(0)) ge 90)
endif

;-------------- Now deal with either 3 or 2 exposure cases -------------

;	Replace pixels with DN less than or equal 0 with 1
ii = where(img1 le 0) & img1(ii) = 1.
ii = where(img2 le 0) & img2(ii) = 1.

;	Identify saturated pixels in long exposure array
siz=size(sx)
sxmax=siz(1)-1
s2=where(data(*,*,sx(sxmax)) ge 253)
  xx = s2 mod 512
  yy = s2/512
  s2up = ((yy+1)<511)*512L + xx
  s2dn = ((yy-1)>0 )*512L + xx

;	Put the longest exposure in the working array 
;	and replace saturated pixels with smoothed med image
cimg=img2
cimg(s2) = img1(s2)
cimg(s2up) = img1(s2up)
cimg(s2dn) = img1(s2dn)

;	Make smoothed array and use it to replace bleed pixels
timg=smooth(cimg,5)
cimg(s2) = timg(s2)
cimg(s2up) = timg(s2up)
cimg(s2dn) = timg(s2dn)

;	Replace "good-signal" smoothed pixels with unsmoothed values
cimg(m1)=img1(m1)

tvscl,alog(cimg)

end

