;+
; NAME:
;	Responses
; CALLING SEQUENCE:
;	Responses,infile,ncm,nsc,esc,rsc0,rsc1,e1,e2,ebar
; PURPOSE: 
; 	Responses reads in the response for one detector.
; INPUT:
;	infile= input file name
; OUTPUT:
;	ncm= no. of channels,
;	nsc=no. of energies at which the response is given, 
;	rsc0=sc response function
;	rsc1=rsc0*de,
;	e1,e2 = the channel boundaries,
;	ebar= the channel midpoint energy.
; HISTORY:
;	Spring '92 by JMcT
;-
PRO Responses, infile, ncm, nsc, esc, rsc0, rsc1, e1, e2, ebar

;first find number of dummy lines in the resp. file
   n_dummy = dummy_rd(infile)
   
   openr, two, infile, /get_lun

;If necessary, read the dummy lines
   IF(n_dummy GT 0) THEN BEGIN
      hdr = strarr(n_dummy)
      tstr = strarr(1)
      FOR j = 0, n_dummy-1 DO BEGIN
         readf, two, tstr
         hdr(j) = tstr(0)
      ENDFOR
   ENDIF
   
   readf, two, ncm		;number of channels
   ncm = fix(ncm)               ;surprise, input's are floating pt.
   ncmp1 = ncm+1
   ncmm1 = ncm-1
   
   e1 = fltarr(ncm)		;the low boundary of the channels
   e2 = fltarr(ncm)		;the high boundary of the channels
   ebar = fltarr(ncm)           ;the energy at the channel midpoint
   
   readf, two, e1               ;input the channel edges
   readf, two, e2               ;input the channel edges
   
   ebar = (e1+e2)/2.0           ;get ebar
   
   readf, two, nsc              ;input total area and dimension of input array
   nsc = fix(nsc)
   nsc1 = nsc-1
   nsc2 = nsc-2
   
   escin = fltarr(nsc)		;declare arrays for energy and responses
   rscin = fltarr(ncm, nsc)     ;escin(0:nsc1),rscin(0:ncmm1,0:nsc1)
   
   temp = fltarr(ncmp1)
   subs = indgen(ncm)
   FOR i = 0, nsc1 DO BEGIN
      readf, two, temp
      escin(i) = temp(0)
      rscin(subs, i) = temp(1:ncm)
   ENDFOR
   
;now calculate the midpoint energies and response*de, 
;which we will use to do the integration.
;first, get de
   de = escin*alog(shift(escin, -1)/shift(escin, 1))/2.0
   IF(nsc EQ ncm) THEN BEGIN
      de(0) = e2(0)-e1(0)
      de(nsc1) = e2(nsc1)-e1(nsc1)
   ENDIF ELSE BEGIN
      de(0) = (escin(1)+escin(0))*alog(escin(1)/escin(0))/4.0 ;jmm 29-apr-1997
      de(nsc1) = (escin(nsc1)+escin(nsc1-1))*alog(escin(nsc1)/escin(nsc1-1))/4.0
   ENDELSE
   esc = escin
   rsc1 = fltarr(ncm, nsc)
; de*response 
   FOR i = 0, nsc1 DO FOR j = 0, ncmm1 DO rsc1(j, i) = rscin(j, i)*de(i)
; here, you need rsc with no de
   rsc0 = rscin
   
   free_lun, two                ;close input file
   RETURN
END
