function sxt_dn_unc,index,data,unc_in,te=te,float=float,gain_ccd=gain_ccd
;+
; NAME:
;   sxt_dn_unc
; PURPOSE:
;   Estimate the uncertainty intensity of an SXT pixel in DN
; CALLING SEQUENCE:
;   unc = sxt_dn_unc(index,data)	; Assumes Te = alog10(3.e6)
;   unc = sxt_dn_unc(index,data,te=te)
;   unc = sxt_dn_unc(index,data,unc_in,te=te)
;
; INPUTS:
;   index	- Must be an SXT index structure
;   data	- decompressed DN values.  Can be a vector, or array.
;		  If data is a 3-d cube, then length of index must match
;		  the 3rd dimension of data:  index(N), data(M,K,N)
;					 or:  index(N), data(N)
;		  data may NOT be byte type.
; OPTIONAL INPUTS:
;   unc_in	- Uncertainties to add to statistical uncertainties in quadrature.
;		  unc_in can be a scalar or it must match the length of data.
;		  Units of unc must match data.  For example, if data is normalized
;		  to counts/sec, then unc_in must also be passed in as counts/sec.
;
;		  Normally unc is the non-statistical error and it is provided
;		  as an output variable from sxt_prep.  There are at least
;		  three non-statistical errors:
;		  	d_comp	= decompression error (sxt_decomp)
;			d_dark	= dark subtraction error (dark_sub)
;			d_reg	= Error from registration (align_prep)
;		  sxt_prep returns sqrt(d_comp^2+d_dark^2+d_reg^2).
;
;
; OPTIONAL INPUT KEYWORD:
;  te		- The log10 of electron temperature of the emitting plasma.  If omitted,
;		  then Te = alog10(3.e6) is assumed.
;  float	- Normally, the output array will be the same type as DATA.  If
;		  /float is set, the output array will be floating.
;
; RETURNED:
;  The statistical errors or sqrt(statistical_errors + unc^2) is returned.
;
; HISTORY:
;  28-feb-95, J. R. Lemen, Written.
;  24-mar-95, JRL, Fixed bug when n_elements(unc_in) = n_elements(data) (Thanks J. McT).
;-

; Check the input variables: INDEX
siz0 = size(index)
typ = siz0( siz0(0)+1 )
if typ eq 8 then tags = tag_names(index) else tags = strarr(2)
if ((typ ne 8) or (tags(1) ne 'SXT')) then begin
    message,'You must use an SXT index',/cont
    message,'unc = sxt_dn_unc(index,data,unc,te=te)',/cont
    return,0
endif

; Check the input variables: DATA
siz1 = size(data)
typ1 = siz1( siz1(0)+1 )
if typ1 le 1 then begin
    if typ1 eq 0 then message,'Error: Data is undefined',/cont
    if typ1 eq 1 then message,'Error: Data must not be byte-type',/cont
    return,0
endif

; Make sure index and data length are compatible:

if siz1(0) le 1 then begin	; data is a vector or a scalar
   if n_elements(index) ne n_elements(data) then begin
	message,'Lengths index and data are incompatible:',/cont
	help,index,data
	message,'Abort 1',/cont & return,0
   endif
endif else if siz1(0) gt 3 then begin	; data is 4-dimensional?
	message,'data must be a scalar, vector, 2-d or 3-d only:',/cont
	help,index,data
	message,'Abort 2',/cont & return,0
endif else if n_elements(index) ne n_elements(data(0,0,*)) then begin
	message,'Lengths of index and data are incompatible:',/cont
	help,index,data
	message,'Abort 3',/cont & return,0
endif
	
; Check the input variables: UNC_IN
if n_elements(unc_in) eq 0 then q_unc = 0 else begin
  siz2 = size(unc_in)
  if siz2(0) eq 0 then q_unc = 1 else $					; Allow unc_in to be a scalar
  if siz2(0) eq 1 and n_elements(unc_in) eq 1 then q_unc = 1 else $	; vector of length 1
  if n_elements(unc_in) ne n_elements(data) then begin
	message,'Lengths of data and unc_in are incompatible:',/cont
	help,data,unc_in
	message,'Abort 4',/cont & return,0
  endif else q_unc = 2
endelse

; Check the input variables: Te

if n_elements(Te) eq 0 then begin
   Te0 = alog10(3.e6)
   message,string('Will assume Te = alog10(',strtrim(10.^te0,2),')'),/info
endif else Te0 = Te

if (Te0 lt 5.5) or (Te0 gt 8.0) then begin
   message,'Te must be in the range: 5.5 <= Te <= 8.0',/cont
   help,Te
   message,'Abort 5',/cont & return,0
endif

if n_elements(Te0) gt 1 then begin
   message,'Te must be a scalar or vector of length 1',/cont
   return,0
endif


; Is the data already normalized?

t0 = gt_expdur(index)
t1 = gt_expdur(index,/orig)
dt = t0-t1
if max(abs(dt) ne 0) then begin
   tbeep & message,'**** Warning:  Assuming DN normalized to per pixel ****',/info
   pix = ([1,4,16])(gt_res(index))
endif

; Get the data for the specified temperature:

nimg = n_elements(index) & Te00 = replicate(Te0,nimg)
Ph_per_dn = sxt_flux(Te00,index,/photons,/nover) / $
            sxt_flux(Te00,index,gain_ccd=gain_ccd,/nover)

unc = float(data)

for i=0,nimg-1 do begin
   if abs(dt(i)) gt 1 then 							$
	unc(*,*,i) = (unc(*,*,i) / (Ph_per_dn(i) * (t1(i)/t0(i)) * pix(i)))>1   else $
	unc(*,*,i) = (unc(*,*,i) / Ph_per_dn(i))>1 
endfor
if q_unc gt 0 then unc = sqrt(unc + float(unc_in)^2) else unc = sqrt(unc)
if not keyword_set(float) then unc = long(unc + .5)	; Default is to return a long array

return,unc
end
