pro sxt_model_dem,dem,tlog,filters,date=date,short=short,long=long, $
	te_sxt,em_sxt,xlum_sxt,xlum_dem,spec_dem=spec_dem,nu_dem=nu_dem, $
	debug=debug,plot=plot,no_dem_norm=no_dem_norm

;+
; NAME
;	model_spec
; PURPOSE
;	To compute, for a model DEM, the temperature, emission measure and
;	x-ray irradiance within a specified wavelength band that would be 
;	derived from an SXT 2-filter observation.  The "true" x-ray
;	irradiance calculated from DEM is output for comparison.
; CALLING SEQUENCE
;   sxt_model_dem,dem,tlog,filters,date=date,short=short,long=long, $
;        te_sxt,em_sxt,xlum_sxt,xlum_dem,spec_dem=spec_dem,nu_dem=nu_dem, $
;        debug=debug,plot=plot,no_dem_norm=no_dem_norm
;   sxt_model_dem,dem,tlog,[2,3]
; INPUTS
;	dem 	= log10(DEM distribution) [Units: 10^44/cm^3/MK]
;	          {Unless you use /no_dem_norm the program normalizes
;                 the total DEM to 1.0 to negate over/underflow problems.}
;	tlog	= vector of the log10(T) [Units:  K]
;	filters = numbers of SXT analysis filter pair (MUST BE A VECTOR)
;			#   Filter    area.filter designation
;         		1   Open       		0
;         		2   Al.1       		1
;         		3   AlMg       		2
;         		4   Be119      		5
;         		5   Al12       		4
;         		6   Mg3        		3
; OPTIONAL INPUT KEYWORDS:
;   date     = 'date' in the standard format to account for change
;               in entrance filter transmission in November 1992.
;   short    = short wavelength (Angstrom), default = 1
;   long     = long wavelength (Angstrom), default = 300
;   spec_dem = spectral array for prscribed DEM,
;	       input to save time for subsequent runs with the same DEM.
;   nu_dem   = switch to force run with new DEM, i.e., /nu_dem.
;   plot     = plot synthesized spectra
;   debug    = /debug to stop inside program at end to examine variables.
;   no_dem_norm  = /no_dem_norm to NOT normalize DEM to 1 cm^-3.
; OUTPUTS
;	te_sxt	= temperatured derived from simulated SXT observation.
;	em_sxt	= emission meax.  "      "      "      "       "
;	xlum_sxt= alog10(x-ray luminosity from SXT)
;	xlum_dem= alog10(x-ray luminosity calculated from DEM)
; OPTIONAL KEYWORD OUTPUT
;	spec_dem = synthesized spectrum for the input DEM.
; RESTRICTIONS:
;       Getting the DEM units right is tricky:
;	   For each temperature bin the units must be 
;	   a.  Divided by 10^44 because MEWE_SPEC returns flux 
;              for an EM of 10^44 cm^-3.
;	   b.  Divided by the temperature bin in MK because they will
;              later be multiplied by this parameter within the program.
;	   c.  Include one dummy value as the last value in the array
;              because this is later multiplied by a d(Te) of 0.0.
;
;  	If the dates of interest span the time of 17-nov-92, then 
;	separate calls must be made for data before and after this date.
;
;  	log10(Te) must be in the range 5.50 to 8.00
;
;  	Short must be 1 or greater.
;  	Long must be 300 or smaller.
; HISTORY
;	1-Aug-94, completed by LWA.
;-

; --------------------------------------------------------------------
;  1. Read the SXT effective areas and form wavelength array.
;  2. Form the spectrum for TLOG values for the entire 1-300 A range.
;  3. Multiply the spectrum by DEM(T), dT(T) sum over T.
;  4. Compute the SXT signal for the 2 filters.
;  5. Compute the Te and EM that SXT would have recorded.
;  6. Compute the x-ray luminosity for desired spectral interval:
;	a. Derived from SXT Te, EM with sxt_erg_per_dn.
;	b. Derived from the model DEM spectrum.
;
; Logarithmic variables:  	tlog,dem,xlum_sxt,xlum_dem
; Non-logarithmic variables:	wave,dwave,te6,flux,dte6,dem_tot,
;				flux_dem,spec_dem,derg,area.filter
; --------------------------------------------------------------------

if n_params() eq 0 then begin
  doc_library,'model_spec'
  return
endif

if (max(tlog) gt 8.0) or (min(tlog) lt 5.5) then begin
  print,'*** OUT OF RANGE TEMPERATURE, MUST LIE WITHIN alog10(t)=5.5-8.0 ***'
  return
endif

; --------------------------------------------------------------------
;  Read the effective areas and wavelength array
; --------------------------------------------------------------------

file=CONCAT_DIR('$DIR_SXT_SENSITIVE','sra930211_01.genx')
restgen,file=file,str=area
wave=area.wave
dwave=fltarr(n_elements(wave))
for i=1,n_elements(wave)-1 do dwave(i-1)=wave(i)-wave(i-1)
ii_nodup=where(dwave ne 0.)
wave=wave(ii_nodup)

; --------------------------------------------------------------------
;  Form the spectrum for the given temperature array.
; --------------------------------------------------------------------
if ((n_elements(spec_dem) le 1) or (keyword_set(nu_dem))) then begin

   te6=10^(tlog-6)

   flux=mewe_spec(te6,wave,/photon)

   ; --------------------------------------------------------------------
   ; Multiply by the DEM, dT binwidth and sum over T.
   ; --------------------------------------------------------------------

   dte6=fltarr(n_elements(te6))
   for i=1,n_elements(te6)-1 do dte6(i-1)=te6(i)-te6(i-1)

   if NOT keyword_set(no_dem_norm) then begin
   print,'*** NORMALIZING DEM to 1.0 cm^-3 ***

      ;  Normalize the total emission measure to 1.0 cm^-3.
      ;  Since MEWE_SPEC returns the flux for an EM of 1e44 this means
      ;     that we are working with a total EM of 1e44 cm^-3.
   
      dem_tot=total((10^dem)*dte6)
      dem2=dem-alog10(dem_tot)
   endif else dem2=dem

   flux_dem=flux*0.

   for i=0,n_elements(te6)-1 do begin
      flux_dem(i,0)=flux(i,*)*10^(dem2(i))*dte6(i)
   endfor

   ;  Sum over the DEM and convert to ergs

   spec_dem=total(flux_dem,1)*1.986e-8/wave

endif

if keyword_set(plot) then begin
   plot_oo,wave,spec_dem,xtitle='Wavelength (A)',$
	ytitle='Ergs/bin'
endif

; --------------------------------------------------------------------
;  Compute the SXT signal for each filter.
; --------------------------------------------------------------------

filt_swap=[0,0,1,2,5,4,3]

signal=fltarr(2)
AU_factor=3.55e-28  ; cm^-2

for j=0,1 do begin
   signal(j) = total(area.filter(filt_swap(filters(j)),ii_nodup)* $
		spec_dem*(6.242e+11/3.65/100.)*AU_factor)
endfor
print,'Filter ',gt_filtb(filters(0),/str),' records',signal(0),' DN.'
print,'Filter ',gt_filtb(filters(1),/str),' records',signal(1),' DN.'

; --------------------------------------------------------------------
;  Compute the Te and EM that SXT would have recorded.
; --------------------------------------------------------------------

sxt_teem1,filters(0),signal(0),filters(1),signal(1),te_sxt,em_sxt

; --------------------------------------------------------------------
;  Compute the x-ray luminosity for desired spectral interval:
;	a. Derived from SXT Te, SIGNAL with sxt_erg_per_dn.
;	b. Derived from the model DEM spectrum.
; --------------------------------------------------------------------

;  X-ray luminosity from SXT

if NOT keyword_set(short) then short = 1       ; Set short wavelength.
if NOT keyword_set(long) then long = 300       ; Set long wavelength..

; xlum_sxt=bandflux(te_sxt,filters(0),date=date,short,long)+alog10(signal(0))
;   NOTE: Change BANDFLUX to SXT_ERG_PER_DN when revision comes online.

;  X-ray luminosity from DEM model spectrum
;  Set a wavelength range corresponding to short and long

iwave=where(wave gt short and wave lt long)

xlum_dem=alog10(total(spec_dem(iwave)))

xlum=mewe_spec(10^(te_sxt-6.),wave,/photon)*1.986e-8/wave
xlum=xlum*10^(em_sxt-44.)   ; Normalize to SXT derived DEM.
xlum_sxt=alog10(total(xlum(iwave)))

print,'Log10(SXT Temp) =',te_sxt,'  or',10^te_sxt,' K.'
print,'Log10(SXT EM)   =',em_sxt
print,'SXT x-ray luminosity in',short,' to',$
	long, ' A interval is',10^xlum_sxt
print,'DEM x-ray luminosity in',short,' to',$
	long, ' A interval is',10^xlum_dem

if keyword_set(debug) then stop

end

