;+
;
;  Name:
;       READ_BDAS_DRM
;
;
; PURPOSE: input into IDL session the detector response matrix created from
;	the response generator at MSFC on animaldsk2:[schwartz.lad_response]
;	or animaldsk2:[schwartz.matrix].  The matrix is returned in the 
;	same units as for the BSAS generator cnts/keV/cm2 output per 
;	photon/bin/cm2 input.
;	
;
; CATEGORY:  I/O for fitting
;       BATSE
;
; CALLING SEQUENCE: Read_bdas_drm, file=file, drm=drm, head=head, $
;	raw_drm= raw_drm, area=area
;
;
; INPUTS: file - complete name of data file
;	  /rdhead - only read the header       
;
; OUTPUTS:
;	drm - response matrix as found in file, (nobins_out,nobins_in)
;	scaled a la BSAS
;	raw_drm - matrix as output by generator, i.e. counts/bin for
;	plane wave of flux 1 photon/bin/cm2, input to map_matrix.pro
;	area - Detector area for elevation angle of 90 degrees
;		127 cm2 for SPEC and 2025 cm 2for LAD
;
;       head - structure containing info on drm creation
;	head.file - filename (bytarr(80))
;	head.position - source phi, theta, earth, phi, theta in GRO coord. (deg)
;	head.det_type - SPEC or LAD , bytarr(4)
;	head.drm_type - e.g. 'DIRECT  ', 'SCATTER ','TOTAL   ', bytarr(8)
;	head.mat_vers - version of response generator program , bytarr(12)
;	head.detid - detector id (0-7)
;	head.nobins_in - number of input photon bins
;	head.nobins_out - number of output energy loss bins
;	head.e_in - input energy edges  (2,nobins_in)
;	head.e_out - output energy edges (2,nobins_out)
;
; MODIFICATION HISTORY: ras, 22-June-1993
;		        ras, 1-feb-1994, made it unix compatible by adding the
;			pad for each readu
;-



pro read_bdas_drm, file=file, drm=drm, head=head, $
	raw_drm= raw_drm, area=area, rdhead=rdhead

if !version.(1) eq 'vms' then vms=1 else vms=0
pad = bytarr(2)

fname = findfile(file(0))
break_file, fname(0), disk, dir, filnam, ext
pquote = strpos(disk, '"')
pcolon = strpos(disk, '::')
if pquote ne -1 then $
	disk = strmid( disk,0,pquote)+strmid(disk,pcolon,strlen(disk))

openr,/segm,lu,/get, disk+dir+filnam+ext
det_type=bytarr(4)
drm_type =bytarr(8)
mat_vers =bytarr(12)
nobins_in=0l
nobins_out=0l
detid=0l
                            
if vms then readu,lu, det_type, mat_vers, drm_type else $
	readu, lu, pad, det_type, mat_vers, drm_type

if vms then readu,lu, detid, nobins_in, nobins_out else $
	readu, lu, pad, detid, nobins_in, nobins_out 

edge_in = fltarr(nobins_in+1)
edge_out = fltarr(nobins_out+1)

if vms then readu,lu,edge_in else readu,lu, pad, edge_in
if vms then readu,lu,edge_out else readu,lu, pad, edge_out

thetbin=0.0
phibin=0.0
thetgin=0.0
phigin=0.0
if vms then readu,lu,phibin,thetabin,phigin,thetagin else $
	readu, lu, pad, phibin,thetabin,phigin,thetagin

if not keyword_set(rdhead) then begin
  raw_drm = fltarr(nobins_out,nobins_in)
  for i=0,nobins_in-1 do begin 
	jj=0L 
	if vms then readu,lu,jj else readu,lu,pad, jj 
	buff=fltarr(jj>1) 
	if vms then readu,lu,buff else readu,lu,pad, buff 
	raw_drm(0,i) = buff 
  endfor
raw_drm = conv_vax_unix(raw_drm)
endif

free_lun,lu


head = {head_drm, file:bytarr(80), position:[phibin,thetabin, $
	phigin,thetagin],det_type:det_type, drm_type:drm_type,$
	mat_vers:mat_vers, $
	detid:detid, nobins_in:nobins_in, nobins_out:nobins_out, $
	e_in:fltarr(2,276), e_out:fltarr(2,276)}

line = head.file 
;Take the last 80 characters of the file name

filnam = byte(filnam(0))
line(0) = filnam(n_elements(filnam) -80 > 0 :*)
head.file = line
head.e_in(0,0:nobins_in-1) = edge_in(0:nobins_in-1)
head.e_in(1,0:nobins_in-1) = edge_in(1:nobins_in)
head.e_out(0,0:nobins_out-1) = edge_out(0:nobins_out-1)
head.e_out(1,0:nobins_out-1) = edge_out(1:nobins_out)
head = conv_vax_unix(head)

if string(head.det_type) eq 'LAD ' then area = 2025. else area = 127.

if not keyword_set(rdhead) then begin
;rescale output rows to counts/keV/cm2, BSAS normalization

	w_out =	 (head.e_out(1,*) - head.e_out(0,*))(0:nobins_out-1)
	drm = raw_drm * 0.0
	for i=0,nobins_in-1 do drm(*,i) = raw_drm(*,i)/w_out/area
;end rescaling
endif

return
end


