;+
;
; NAME: HXT_CAL_DRM
;
;
; PURPOSE: Creates the detector response matrix for the HXT cal
;	data with 64 output channels.
;
;
; CATEGORY: YOHKOH/HXT, SPEX
;
;
; CALLING SEQUENCE:  
;	HXT_CAL_DRM, Drm, Eout, Ein, Area
;
; CALLED BY:
;	DRM_4_SPEX
;
; CALLS:
;	RESP_CALC, EDGE_PRODUCTS
;
; INPUTS:
;       none explicit
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;	Drm - detector response in cts/cm2/keV per photon/cm2
;	     same units as RD_HXT_DRM		
;	Eout- output channel pulse-heights in keV, 2x64, from HXT 
;		DatabookI
; 	Ein - input photon energy bins in keV, 2x200, from 10-200 keV
;	Area- Nominal maximum area of total HXT is 55 cm2   
; KEYWORDS:
;'	PULSE_SHAPE - resolution broadening matrix
;	DELTA_LIGHT - PHA channel widths in light units
;	ERROR	    - successful completion returns 0
;	NOLIGHT     - if set then don't consider light/energy function
;	 
;
; PROCEDURE:
;	Calls resp_calc with detector properties and interpolates
;	to output channels.
;
; MODIFICATION HISTORY:
;	ras, 17-jan-1996
;	ras, 24-jan-1996, added light/energy function taken from BATSE
;-
pro hxt_cal_drm, drm, eout, ein, area, error=error, eloss_mat=eloss_mat, $
	pulse_shape=pulse_shape, nolight=nolight, delta_light=delta_light

eout= (10.1 + findgen(65))*1.252
edge_products, eout, edges_2=eout, width=ww, mean=edgm
func_par=[1.3/2.36,.5,0,0]
area=(2.3/2)^2*!pi*64/4
;area = 55.
d=.5;cm nai thickness
elo=10.
ehi=200.
detector='nai'
gmcm=[.08*2.7,.42*1.7]
z=[13,6];al and c
nflux=200
func='fwhm'

checkvar, nolight, 0

resp_calc, detector,area,func,func_par,d,z,gmcm,nflux,elo,ehi, $;inputs
	eloss_mat, pls_ht_mat, eout_mat, smatrix, pulse_shape=pulse_shape, $
	nosigma = 1-nolight


edge_products, eout_mat, edges_2=ein, mean=em, wid=wm
area = (max(total( eloss_mat,1)))(0)

;The next term calculates the transparency of the grids.  It is
;black at low energy and never less than 0.25
wtrans=(1- exp(-(.05*xsec(em,73,'pe') <25.))) > 0.25;should be reconsidered
drm=fltarr(64,200)

if nolight then begin 

  smatrix=smatrix/area
  for i=0,nflux-1 do drm(0,i)=interpol(smatrix(*,i)/wtrans(i),em,edgm)

endif else begin


; Account for the energy-loss/light output function in NaI
; Eloss_mat is given for photon energies in in the bins given by
; Ein for output bins with the same energies.  These output bins
; must be translated to light output bins where an energy loss of 59.5 keV
; translates exactly to a light output of 59.5 keV (because this is how the
; light outputs for HXT are calibrated.  The matrix 
;
  g_c,59.5,el0
  el0=el0(0)

  edge_products, ein, edges_2=els, mean=elsm, wid=wels

  g_c, els(*), lc

  lc =lc/el0
  eph = reform(els(*)*lc,2,nflux)
  eph=reform(eph, 2, nflux)
  edge_products,eph,edges_1=eph1, wid=wph,mean=ephm
  pulse_shape = fltarr(nflux, nflux)


  sigmaxl = call_function( func, ephm, func_par)/2.36
  for i=0,nflux-1 do $
	pulse_shape(*,i) = gaussint((eph1(1:*)-ephm(i))/sigmaxl(i)) $
		- gaussint( (eph1-ephm(i))/sigmaxl(i))

  pls_ht_mat = pulse_shape # eloss_mat
  smatrix = f_div( pls_ht_mat , rebin(wph, nflux, nflux))/area
  for i=0,nflux-1 do drm(0,i)=interpol(smatrix(*,i)/wtrans(i),ephm,edgm)
  ;Express channel edges in terms of energy-loss peaks
  eoutph = eout
  f59 = fcrystal(59.5)/59.5
  for i=0,63 do eoutph(0,i) = fcrystal( eout(0,i) )
  eoutph(1,63 ) = fcrystal( eout(1,63) )
  eoutph(1,0:62) = eoutph(0,1:63)
  eoutph = eoutph / f59
  eout   = eoutph

endelse

delta_light =  ww ;pha channel widths in any event

error=0
end
