PRO SXT_TEEM, arg1, image1, arg2, image2, te, em, d_te, d_em, Valid,	$
		t1=t1, t2=t2,	interp=interp, gain=gain,	$ ; Input
		thresh1=thresh1, thresh2=thresh2, date=date,	$ ; Input
		average=average, sum=sum, satval=satval,	$ ; Input
		sat1=sat1, sat2=sat2, unc1=unc1, unc2=unc2,	$ ; Input
		subs=subs,version=version  			  ; Returned
;+
; NAME:
;       SXT_TEEM
; PURPOSE:
;       Compute temperature from the ratio of the two input data arrays.
; CALLING SEQUENCE:
;       sxt_teem, index1, Image1, index2, Image2, Te 
;       sxt_teem, index1, Image1, index2, Image2, Te, EM, gain=gain, subs=subs
;       sxt_teem, filt1,  Image1, filt2,  Image2, Te, EM, t1=t1, t2=t2
;	sxt_teem, filt1,  Image1, index2, Image2, Te, thresh1=thresh1, /average
;	sxt_teem, index1, Image1, index2, Image2, Te, EM, d_Te, d_EM, Valid
;	sxt_teem, index1, Image1, index2, Image2, Te, EM, d_Te, d_EM, sum=sum
; INPUTS:
;	index1	= index (structure)    of Image1 
;	filt1	= Filter B value (1-6) of Image1
;	Image1	= SXT counts as DN
;	index2	= index (structure)    of Image2
;	filt2	= Filter B value (1-5) of Image2
;	Image2	= SXT counts as DN
;
;	The filt1 or filt2 parameters, define the filter number
;	the same what that is given from: print,gt_filtb()
;	  1   Open       Op
;	  2   Al.1       A1
;	  3   AlMg       AM
;	  4   Be119      Be
;	  5   Al12       A2
;	  6   Mg3        Mg
;
;
; OPTIONAL INPUT KEYWORDS:
;       t1,t2	= Exposure times in msec.  These values will override
;		  those in index1 and index2 if index1 and index2 are supplied.
;		  If index1 and t1 are not supplied, default to 1000 msec.
;	subs	= If image1 and image2 are 3-d, index of images to process.
;       gain    = camera gain in e-/Dn.  
;                 If not supplied, default= (~100). Used in EM calculation.
;       interp  = If set, use Spline interpolation of SXT response functions
;		  Default is linear interpolation.  
;		  (NOTE: SPLINE [IDL user library] fails on large images.)
;	sat1,sat2=Array of saturated pixels (if Image is not byte-type).
;	unc1,unc2=Array of decompression uncertainties (if Image not byte-type).
;	sum	= Sum over sum X sum pixels before computing Te.  
;	satval	= value of sat1 or sat2 to treat as a saturated pixel (def=1).
;	average	= If set, return average Te and total EM for entire image.
;	thresh1,thresh2 = Minimum threshold (in DN) of background subtracted
;			  and rebinned (sum=X) image1, image2
;	date	= Time in any format (including structure). Used to
;		  determine entrance filter transmission (not needed if
;		  index1 or index3 is an SXT index structure).
; OUTPUTS:
;	Te	= log10(Temp)  (invalid temps = 0)
; OPTIONAL OUTPUTS:
;	EM	= log10(Emission measure).  
;	d_Te	= Statistical uncertainties of Te
;	d_EM	= Statistical uncertainties of EM (includes uncertainty of d_Te)
;	Valid	= Array of valid pixels (=0).  -1 = bad ratio values.
; OPTIONAL OUTPUT KEYWORDS:
;       version  = version number of input data base file
; COMMON BLOCKs:
;	None. (In sxt_teem2, sxt_teem_db contains SXT response curves: sre*genx)
; RESTRICTIONS:
;       o Image1 and Image2 must be the same size (1-d or 2-d or 3-d).
;  Possible calling options:
;
;       Index       Image
;       --------    ------------
;       Scalar      Scalar
;       Scalar      (n,m)  array
;       Vector(k)   (n,m,k) array
;
;       The output (Te, EM, dTe, dEM) will generally be the same dimensions as the
;       input image array unless the sum or average keyword options are specified.
; PROCEDURE:
;	o Will compute ratio of thicker/thinner to determine Te.
;	o Calls sxt_prep if imageX is  byte type
;	o Use thinner image for EM determination.
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  Double Valued Functions:
;    The following filter ratios are doubled valued (launch entr. filters)
;        Al12/Noback, Al12/Al.1, AL12/AlMg, Al12/Mg3
;        Mg3 /Noback, Mg3 /Al.1, Mg3 /AlMg
;    The temperatures returned may be a LOWER limit to the actual value.
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; MODIFICATION HISTORY:
;	29-mar-93, J. Lemen,  Can now handle data cubes.
;			      (sxt_teem calls sxt_teem1 which calls sxt_teem2)
;	 2-apr-93, J. Lemen,  Fixed bug for t1, t2 in single image case.
;	22-dec-93, JRL, Fixed an IDL V3.1 related bug
;	 8-sep-94, JRL, d_EM now includes uncertainty caused by d_Te
;			Minor change to the informational timing messages
;	18-nov-94, JRL, Fixed an bug with cubes that are (1,1,N)
;	28-Jan-95, JRL, Updated the documentation for filter number
;	 8-mar-95, JRL, Renamed one of the input variables.
;-


;----------------------------------------------------------------------------
;  ****  Step 1:  Information Section
;----------------------------------------------------------------------------

n_params0 = n_params()			; Tell sxt_teem1 what calculations to do

; if no parameters are present, assume information mode

if n_params0 lt 5 then begin
  doc_library,'sxt_teem'
  print,format="(75('-'))"
  fastdoc,'sxt_teem',/summ			; Give the full parameter list
  print,format="(75('-'))"
  return
endif

;----------------------------------------------------------------------------
;  ****  Step 2:  Perform Checks on Input Data
;----------------------------------------------------------------------------

Te=0. & EM=0. & d_Te=0. & d_EM=0. & Valid=0	; In case we return early

sz1 = size(image1)
sz2 = size(image2)

if sz1(0) gt 2 then nimg1 = sz1(3) else nimg1 = 1
if sz2(0) gt 2 then nimg2 = sz2(3) else nimg2 = 1

if nimg1 ne nimg2 then begin
   print,' *** Error in SXT_TEEM (1)***'
   print,'     image1 and image2 must contain same number of images'
   help,image1,image2 & tbeep
   return
endif

;----------------------------------------------------------------------------
;  ****  Step 3:  Call SXT_TEEM1 to calculate Te, Em, d_Te, d_EM
;----------------------------------------------------------------------------

if n_elements(sum) eq 0 then sum = 1
if sum gt 1 then print,'***  Sum mode: ',strtrim(sum,2),' by ',strtrim(sum,2)

if nimg1 gt 1 then begin		; Do we need to loop?

  if n_elements(subs) eq 0 then subs1 = indgen(nimg1) else begin
	subs1 = subs
	nimg1 = n_elements(subs)
  endelse

  t0 = systime(1) & t00 = t0
  print,'SXT_TEEM status                 (subs)        (s) Remain (min)'
  for i=0,nimg1-1 do begin
    j = subs1(i)
    if n_elements(sat1) gt 0 then ssat1 = sat1(*,*,j)
    if n_elements(sat2) gt 0 then ssat2 = sat2(*,*,j)
    if n_elements(unc1) gt 0 then uunc1 = unc1(*,*,j)
    if n_elements(unc2) gt 0 then uunc2 = unc2(*,*,j)
    if n_elements(t1)   gt 0 then tt1 = t1(j)
    if n_elements(t2)   gt 0 then tt2 = t2(j)
    sxt_teem1, arg1(j),image1(*,*,j),arg2(j),image2(*,*,j),		$
		te1,em1,d_te1,d_em1,valid1, 				$
		t1=tt1, t2=tt2,	interp=interp, gain=gain,		$ 
		thresh1=thresh1, thresh2=thresh2, date=date,		$
		average=average, sum=sum, satval=satval,		$
		sat1=ssat1,sat2=ssat2,unc1=uunc1,unc2=uunc2,		$
		n_params0=n_params0, version=version

;  Print a message about the time

    run_time0 = systime(1) - t0		; Time for this call
    run_time1 = systime(1) - t00	; Total time

    remain_time = (run_time1 / (i+1) * (nimg1-i-1)) / 60.	; Remaining time in min
    print,'SXT_TEEM finished',i+1,' out of',nimg1,'  ('+strtrim(j,2)+')', $
		run_time0,remain_time,format='(a,i4,a,i4,a,t43,f7.0,f7.1)'
    t0 = systime(1)			; Reset the clock

;  Set up the output variables:
    if i eq 0 then begin
  	if keyword_set(average) then 	te = fltarr(nimg1) else 	$
					te = fltarr(sz1(1),sz1(2),nimg1)
	if n_params0 ge 6 then em = te 
        if n_params0 ge 7 then d_te = te 
        if n_params0 ge 8 then d_em = te
	if n_params0 ge 9 then valid = intarr(sz1(1),sz1(2),nimg1)
    endif

;  Assign the results:
    if keyword_set(average) then begin
      	te(i) = te1
      	if n_params0 ge 6 then em(i)      = em1
      	if n_params0 ge 7 then d_te(i)    = d_te1
      	if n_params0 ge 8 then d_em(i)    = d_em1
    endif else begin
      te(0,0,i) = te1
      if n_params0 ge 6 then em(0,0,i)  = em1
      if n_params0 ge 7 then d_te(0,0,i)= d_te1
      if n_params0 ge 8 then d_em(0,0,i)= d_em1
    endelse
    if n_params0 ge 9 then valid(0,0,i) = valid1
  endfor
  print,'SXT_TEEM took',run_time1/60.,' min',format='(a,f6.1,a)'
endif else 					$
  SXT_TEEM1, arg1, image1, arg2, image2, te, em, d_te, d_em, Valid,	$
		t1=t1, t2=t2, interp=interp, gain=gain,		$ ; Input
		thresh1=thresh1, thresh2=thresh2, date=date,	$ ; Input
		average=average, sum=sum, satval=satval,	$ ; Input
		sat1=sat1, sat2=sat2, unc1=unc1, unc2=unc2,	$ ; Input
		n_params0=n_params0, version=version		  ; Returned

end
