;+
;
;  Name:
;       BUILD_DRM
;
;
; PURPOSE: generate a BATSE LAD or SPEC response matrix for direct interactions
;	   for any direction cosine between 0.0 and 1.0,
;
; CATEGORY:  Spectral analysis, fitting.
;       BATSE
;
; CALLING SEQUENCE: drm = build_drm( cosine=cosine, chan_edges=chan_edges, $
;		 det_type=det_type, high=high,e_in = e_in, area=area )
;
; CALLS TO: 
;	batse_spec_drm, x_eout_drm, batse_lad_drm, printx
;	
;
; INPUTS:
;       keywords:
;	cosine - direction cosine to source with respect to LAD front
;		surface normal, default is cosine=0.0
;
;	chan_edges - 2 by n array of low and high energy edges, keV
;                            default is the edges of the data file, 100x100
;	det_type - 'lad' or 'spec'
;	  /high    - if set, use the high energy response tables up to 30 MeV
;	test_drm - 0 => default batse spec drm, 1 => 1st test drms, 2 => 2nd test
;		
; OUTPUTS:
;       keywords:
;	e_in - 2 by m array of low and high energy edges for input
;		energy bins of incident photons, keV
;               area - detector normal area, 2025 cm^2 for LAD, 127 cm2 for SPEC
;
; RESTRICTIONS: 0.0 < cosine < 1.0
; Note:
;	Edges_out - lower and upper limits are forced to be within the low and upper limits
;		   of the output edge of the drm read from the file!
;	BATSE response database must be installed!
;
; PROCEDURE: values are interpolated from a stored table of 100x100x12
;		output energies x input energies x cosines
;
; MODIFICATION HISTORY: ras, 2 Feb. 1994
;	ras, 27-mar-1994, uses linear interpolation over output energy bins
;	ras, 11-nov-1994, enable testing new spec drms by looking for environment
;		variable, DRM_NEW
;	ras, 3-mar-1995, enable testing newest spec drms 
;               RAS, 7-Jul-1997, changed PERM_DATA to SSWDB_BATSE
;       Version 5, richard.schwartz@gsfc.nasa.gov, 8-feb-1998, 
;       corrected print statement for LAD build, energy/light conversion is all
;       mixed up with pha energy calibration.  Not considered adequate by ras.
;	added lld cutoff keyword for batse_lad_drm.
;       
;-
function build_drm, cosine=cin, chan_edges=chan_edges, det_type=det_type, $
	high=high, e_in=e_in, area=area, det_id=det_id, test_drm=test_drm




checkvar,test_drm, 0
checkvar,det_type,'lad'
mat_type = 'direct'
det_type = strlowcase(det_type)
if det_type eq 'spec' then area = 127. else area =2025.
checkvar, high, 0
checkvar, cin, 1.0

if test_drm eq 2 and det_type eq 'spec' then begin
	print,'Building SPEC DRM from database files, extracting fluxes.'
	print,'Correcting properly for energy/light conversion!!'
	batse_spec_drm, edges_out=chan_edges, edges_in=e_in, $
	  theta=acos(cin)*!radeg, det_id=det_id, drm=drm, high=high
	return, drm
endif

if test_drm eq 2 and det_type eq 'lad' then begin
	print,'Building LAD DRM from database files, extracting fluxes.'
	print,'Not correcting for energy/light conversion!!'
	batse_lad_drm, edges_out=chan_edges, edges_in=e_in, lld =(discla_edges( det_id ))(0), $
	  theta=acos(cin)*!radeg, det_id=det_id, drm=drm, high=high
	return, drm
endif
case 1 of
        det_type eq 'spec' and not high: savename = 'spec_drm.sav'
        det_type eq 'spec' and high: savename = 'spec_drm_30mev.sav'
        det_type eq 'lad' and not high: savename  = 'lad_drm.sav'
        det_type eq 'lad' and high:  savename = 'lad_drm_30mev.sav'
        1:
endcase

if test_drm eq 1 then begin
	printx,'USING MODIFIED DRMS IN SPEC_DRM.SAV_NEW'
	savename = 'spec_drm.sav_new'
endif

savename = concat_dir(getenv('SSWDB_BATSE'),savename)

;set up for restore
	filenames = strarr(12)
	tdrm = fltarr(100,100,12)
	cosines = fltarr(12)
	e_out = fltarr(2,100)
	e_in  =  fltarr(2,100)
        area  = 0.0
	restore,savename
	tdrm = tdrm  > 0.0
checkvar, chan_edges, e_out
 
checkvar, cin, 1.0


;Data in place ready to interpolate
	if (cin lt 0.0) or (cin gt 1.0) then begin
       		print,'Input cosine out of range 0.0<cosine<1.0, Error!'
		return, 0
	endif

;INTERPOLATE on these indices

	idc =(sort(abs(cosines - cin)))(0:1)

	dx_base= cosines(idc(1))-cosines(idc(0))
	dx = cin - cosines(idc(0))
	rdx = dx/dx_base

	idrm = ((tdrm(*,*,idc(0))*(1.-rdx)/cosines(idc(0)) + $
		rdx*tdrm(*,*,idc(1))/cosines(idc(1))) * cin) >0.0
	
	chan_edges = chan_edges < max(e_out) > min(e_out)

if total( (chan_edges-e_out)^2 ) ne 0.0 then $
	drm = x_eout_drm(in_drm= idrm, in_e_out=e_out,$
		out_e_out= chan_edges,  $
		nsub_bins= n_elements( e_out) / n_elements(chan_edges)) else $
	drm = idrm

return, drm
end

