;+
;
; NAME:
;	READ_HXRS_4_SPEX
;
; PURPOSE:
;       Based on settings, calls read_hxrs_fits to get parameters of interest.
;		Used as an interface to Spex.
;
;
; CATEGORY:
;       SPECTRAL FITTING, SPEX - HXRS
;
; CALLING SEQUENCE:
;
; CALLS:
;       CHECKVAR, EDGE_HXRS, F_DIV, ATIME, EDGE_PRODUCTS
;
; INPUTS:
;	Through keywords so you don't have to remember the input order.
;
; OUTPUTS:
;	Through keywords so you don't have to remember the output order.
;
; KEYWORDS:
;        file:	hxrs fits file name to read (input)
;        ut:	time in seconds since 1-jan-1979, middle of datapoint,
;			dblarr(2,n) (start and end of each interval) (output)
;        syarray, nyarray, totyarray: shielded, nonsh and total raw counts/interval
;        esyarray, enyarray,etotyarray: uncertainties, i.e. sqrt(yarray(10,N)
;        deltat:	accumulation time interval, scalar, in seconds (output)
;        slivetime, nlivetime, totlivetime: livetimes per interval for detectors and their total
;        edges:	output energy loss bands in keV  fltarr(2,10) (output)
;        h0:	primary header (string array) (output)
;        h1:	header from first (time) extension (string array) (output)
;        error:	if =1, then error reading file.  Otherwise 0. (output)
;
; PROCEDURE:
;	User may explicitly pass the name of a HXRS FITS file;
; 	Sample call:
;	read_hxrs_fits, file='hxrs_fits:hxrs_23.fits', ut=ut, syarray=syarray
;
; MODIFICATION HISTORY:
;        Adapted from the get_hxrbs_fits routine
;	Written by Kim Tolbert (NASA/GSFC), 9-sep-1994.
;        Adapted for HXRS by Alan Kiplinger May 2000.
;   5-June-2000 Paul Bilodeau, general re-organization, error handling
;-

pro read_hxrs_4_spex, dformat=dformat, files=files, $
start_time = start_time, end_time=end_time, flare=flare, $
det_id=det_id, id_style=id_style, p_read_data=p_read_data, $
flux=flux, eflux=eflux, ut=ut, units=units, area=area, cosines=cosines, $
ltime=ltime, edges=edges, delta_light=delta_light, wchan=wchan, id_use=id_use, title=title, $
auto=auto, noplot=noplot, mode=mode, error=error, $
command_out=command_out, efficiency = eff, _extra = _extra


common read_spex_com, data, ut_data, data_file_read, read_start, read_end
checkvar,data,0
checkvar,data_file_read,''
checkvar,read_start,''
checkvar,read_end,''
checkvar, command_out, ''
if command_out(0) ne '' then com_param=command_out else com_param=''

checkvar,    units, ' s!u-1!n cm!u-2!n keV!u-1!n'
format = strupcase(dformat(0))
area=4.91
command_out='build_drm'
wchan=indgen(8)
nchan=8

case format of
    'SHIELD': begin
        command_out = 'build_drm'

		read_hxrs_fits, file=files[0], ut=ut, syarray=flux, esyarray=eflux, $
			deltat=deltat, slivetime=ltime, edges=edges

		; remove the two upper HXRS channels from the flux and eflux
		flux = flux[0:nchan-1,*]
		eflux = eflux[0:nchan-1,*]
		edges = edges[*,0:nchan-1]

		;convert to fluxes, counts/s/cm2/keV
		edge_products, edges, width=ww
		s=size(ut)
		nbin=s[s[0]]
        count_2_flux =  area * ( ltime ## ww )
        eflux = f_div(eflux, count_2_flux) ;sqrt counts/ factors
        flux  = f_div(flux, count_2_flux)       ; counts / factors

        title = 'HXRS SHIELDED  '+atime(/yoh, ut(0),/date)
        end
    'NOSHIELD':  begin
        command_out = 'build_drm'

		read_hxrs_fits, file=files[0], ut=ut, nyarray=flux, enyarray=eflux, $
			deltat=deltat, nlivetime=ltime, edges=edges

		; remove the two upper HXRS channels from the flux and eflux
		flux = flux[0:nchan-1,*]
		eflux = eflux[0:nchan-1,*]
		edges = edges[*,0:nchan-1]

		;convert to fluxes, counts/s/cm2/keV
		edge_products, edges, width=ww
		s=size(ut)
		nbin=s[s[0]]
        count_2_flux =  area * ( ltime ## ww )
        eflux = f_div(sqrt(flux), count_2_flux) ;sqrt counts/ factors
        flux  = f_div(flux, count_2_flux)       ; counts / factors

       	title = 'HXRS NON-SHIELDED '+atime(/yoh, ut(0),/date)
        end
    'TOTAL': begin
        command_out = 'build_drm'

		read_hxrs_fits, file=files[0], ut=ut, totyarray=flux, etotyarray=eflux, $
			deltat=deltat, totlivetime=ltime, edges=edges

		; remove the two upper HXRS channels from the flux and eflux
		flux = flux[0:nchan-1,*]
		eflux = eflux[0:nchan-1,*]
		edges = edges[*,0:nchan-1]

		;convert to fluxes, counts/s/cm2/keV
		edge_products, edges, width=ww
		s=size(ut)
		nbin=s[s[0]]
        count_2_flux =  area * ( ltime ## ww )
        eflux = f_div(sqrt(flux), count_2_flux) ;sqrt counts/ factors
        flux  = f_div(flux, count_2_flux)       ; counts / factors

       	title = 'HXRS TOTAL '+atime(/yoh, ut(0),/date)
        end
    endcase

start_time=ut[0]
end_time=ut[n_elements(ut)-1]
delta_light=edges[1,*]-edges[0,*]

end