;+
;  Name:
;       DISCSC_PL
;	
; PURPOSE: using the channel values of the discsc data, return the
;	best power-law index, may also be used for DISCLA data
;	by setting det_id switch
;	
; CALLING SEQUENCE:
;	pl = discsc_pl( flare, dselb, disc)
; CATEGORY:
;	BATSE
; INPUTS:
;	flare - SDAC BATSE archive flare number, referenced to BATSE_FLARE_CATALOG
;	dselb - byte mask of selected detectors,
;		or if det_id switch is set, then used as a detector id#
;		for a single detector	
;	disc  - a vector of two to four values, 1st DISCSC(LA) channel, 2nd,
;		3rd, and 4th
;
; OPTIONAL INPUTS:
;	new   - if set, new cdrm, chan_edges, and photon_edges are computed
;	
; OUTPUTS:	
;	returns the power-law spectral index(ices) for those channels at the 
;	attitude to the Sun for that detector(s)
;
; OPTIONAL INPUTS/OUTPUTS:
;	NB - IF you want to calculate the pl index for a succession of
;	     discsc(la) values for a single flare, then cdrm, chan_edges, and
;	     photon_edges must be passed back as arguments.  These are
;	     not saved in a common block, the calling program is responsible for
;	     returning these calculated values.
;	cdrm - detector response matrix expressed as counts/chan per photon/cm2
;       cosine - if you don't want to use flare reference for cosine, input this
;		value
;	chan_edges - nominal energy edges (keV) of last detector in dselb
;	photon_edges -photon energy edges (keV) used as inputs for cdrm
; OPTIONAL OUTPUTS:
;	a1_photon50 - what SMM/HXRBS used to call A1, the normalization
;		of the photon spectrum at 50 keV in photon/cm2/sec/keV
;	
; PROCEDURE:
;	creates a grid of ratios for model spectra and interpolates
;
; CALLS:
;
; COMMON BLOCKS:
;	common discsc_pl_com, plratios
;
; RESTRICTIONS:
;	BATSE_FLARE_CATALOG must be defined
;	INST_RESPONSE_LAD_... must exist on PERM_DATA
;
; MODIFICATION HISTORY:
;	ras, 26-apr-95
;	
;-
;
function discsc_pl, flare, dselb, disc, det_id=det_id, cdrm=cdrm, cosines=cosines, $
	chan_edges=edges, photon_edges=eph, a1_photon50=photon50, new=new



if not keyword_set(cdrm) or not keyword_set(edges) or not keyword_set(eph) $
	or keyword_set(new) then begin

	if exist(det_id) then begin
		nids = 1
		ids  = det_id
	endif else begin
	bits,fix(dselb(0)),bitarr
		ids = where(bitarr eq 1, nids)
	endelse
common discsc_pl_com, plratios, pl

if not exist(cosines) then $
	det_cosines,cos_lad=cosines, fla=flare,lad_sort=lad_sort,/sort
for i=0,nids-1 do begin
	det_id=ids(i)
	edge_products, (discla_edges())(0:4,det_id), edges_2=edges, width=wedg
	theta = acos( cosines(det_id))*!radeg
	batse_lad_drm, edges_out=edges, edges_in=eph, theta=theta,det_id=det_id,drm=drmi
	if n_elements(cdrm) eq 0 then cdrm = drmi*0.
	cdrm = cdrm + drmi*rebin(wedg,4,n_elements(drmi(0,*)))
	drmi= 0
  endfor
  cdrm = cdrm * 2025.



  wuse = where(eph(1,*) le 2000.)

  edge_products, eph, gmean=ephm, wid=weph 

  plratios = fltarr(3,50)
  pl = findgen(50)*.2 + 1.0

  for i=0,49 do begin
	out = cdrm(*,wuse) # ( (50./ephm(wuse))^pl(i)* weph(wuse))
	plratios(0,i) = out(0)/out(1)
	plratios(1,i) = out(0)/out(2)
	plratios(2,i) = out(0)/out(3)
  endfor
endif else begin
	wuse = where(eph(1,*) le 2000.)
        edge_products, eph, gmean=ephm, wid=weph 
endelse

out_index = fltarr(3)
values = fltarr(4)
values(0) = disc
ratios = f_div(values(0),values(1:*))
photon50 = fltarr(3)
for i=0,2 do begin
  if ratios(i) gt 0.0 then begin
    out_index(i)=interpol(pl, (plratios(i,*))(*),ratios(i))
    photon50(i) = values(0) / $
	total(cdrm(0,wuse)*((50./ephm(wuse))^out_index(i)*weph(wuse)))
  endif
endfor

return, out_index
end
