
;+
; PROJECT:
;	SDAC
; CATEGORY:
;	YOHKOH, SXS, I/O, SPEX
; NAME: 
;	rd_sxs_pha	
; PURPOSE:
;	accumulate sxs pulse-height data
; CALLING SEQUENCE:
;	rd_sxs_pha, infil=infil, data=data, index=index, wuse=wuse,  $
;		pha=pha, live=live, ut=ut, sxs1=sxs1,  $
;		sxs2=sxs2, title=title
;	
; INPUTS:
;	Get SXS PHA data from INFIL or DATA whichever
;	is input.  If DATA and INDEX exist, they are used.  Usual
;	Yohkoh meanings (see Yodat).
; KEYWORD INPUTS:
;	infil
;	data
;	index
;	wuse  - frames to select from data, can be changed inside
;	/new  - select new data from index (or data) using plot_lcur
;	/sxs1 - return sxs
;	/sxs2
; OPTIONAL INPUTS:
;	Start_time & End_time- If these exist, then the directory with infil
;	is searched for all files satisfying these values.  Readable by anytim().	
; OUTPUTS:
;	pha - pulse-height analyzer data, fltarr(128,nbins)
;	ut  - time bins, start, stop, dblarr(2,nbins) relative to 1-jan-79
;	live- live time per bin
;	titl- descriptive title for plotting, string
; PROCEDURE:
;
; RESTRICTIONS:
;
; MODIFICATION HISTORY:
;	ras- 24-apr-94
;	Version 2,
;	richard.schwartz@gsfc.nasa.gov, 27-oct-1997, added start_time and end_time.
;	dramatically revise entire data selection procedure.
;
;-
;
pro rd_sxs_pha, start_time, end_time, infil=infil, data=data, index=index, wuse=w, new=new,  $
	pha=pha, live=live, ut=ut, sxs1=sxs1, sxs2=sxs2, title=title, error=error

;NB this only works in high rate mode for now



;find files corresponding to start and end times within infile directory
if keyword_set(start_time) and keyword_set(end_time) then begin
	st = anytim( start_time, /ints)
	et = anytim( end_time, /ints)
	break_file, infil, disk, dir 
	path = disk + dir
	test = execute('rd_obs, st, et, bcs, sf,sp, w_h,/nobcs, /nosxtf,/nosxtp,/quiet, fid')
	if datatype(fid) eq 'STC' then begin
	fnam = string(fid.st$fileid)
	times = fnam
	times = anytim( strmid(times,0,2)+'/'+strmid(times,2,2)+'/'+strmid(times,4,2)+','+strmid(times,7,4))
	wtimes= where(anytim(times) gt anytim(st) and anytim(times) le anytim(et), ntimes)
	if ntimes ge 1 then begin
	fnam = 'wda'+ fnam(wtimes)
	use_path = [path,curdir(),data_paths()]
	for i=0,n_elements(fnam)-1 do fnam(i) = loc_file(path=use_path,fnam(i))
	wf = where( fnam ne '', nwf)
	if nwf eq 0 then begin
		printx, "No files found corresponding to "+infil+" in path, "+arr2str(del=' ',use_path)
		error=1
		return
	endif
	fnam = fnam(wf)
	endif
	endif
	endif

checkvar, fnam, infil
;Use roadmap to interactively select time interval.

checkvar, sxs2, 0
sxs1 = 1- sxs2	;sxs2 can also be used as the index for data selection
rd_roadmap, fnam, roadmap
ndata = n_elements(roadmap)
ssss = indgen(ndata)
clear_utplot
if !d.name eq 'X' or !d.name eq 'TEK' then begin
	y = call_function('gt_sxs'+strtrim(([2,1])(sxs1),2), roadmap, title=title)

        psav=!p
        ut = anytim(roadmap,/sec) - anytim(roadmap(0),/date,/sec)
        replot: roadmap = roadmap(ssss) & ut=ut(ssss)
	y = call_function('gt_sxs'+strtrim(([2,1])(sxs1),2), roadmap, title=title)
        utplot,/ytype,yran=[1,1e4], ut, y, anytim(/date,roadmap(0),/yohkoh),title=title, psym=1
	dt = roadmap.unit_time/100.
        spex_intervals, ut, zoom, xwidth=dt, $
        style='b', v_styles=['b'], /query,color=fcolor(7), $
        xoffset= (indgen(3)*200+200),yoffset = (indgen(3)*200+200), $
        message='SELECT XRANGE FOR ZOOM',  error=error
        ssss = where( ut ge (limits(zoom))(0) and ut lt (limits(zoom))(1), nsss)
        if nsss le 1 then begin
		ssss = indgen(n_elements(roadmap))
		goto, replot
		endif
        !p = psav
        endif else ssss = indgen(n_elements(roadmap))
    printx,'SXS data selected.'
    help, ssss

	
rd_xda, fnam, ssss, index, data
w = indgen(n_elements(index))
ndata= n_elements(w)
print,'Processing data array for sxs.'

pha = reform(hxt_decomp( data(w).ph.(sxs2)), 128,2*ndata) *1.0 

;time covered by each record of index in seconds, x2 for WBS
dt = (rebin( reform(dprate2sec(index(w)) *2,1,ndata),2,ndata))(*) 
dt = dt/2	;for each sample in the frame

ut = mk_timarr( index(w), fltarr(2,ndata), /wbs) +anytim(index(w(0)), /sec)
ut = transpose( reform([ut(*),ut(*) + dt],2*ndata,2))

live = dt - 86.7e-6 * total(pha,1) >0;counts/interval

if sxs1 then $
       title= 'YOHKOH/SXS1 ' $
  else $
       title= 'YOHKOH/SXS2 '
title = title + anytim(ut(0),/date,/yohkoh)


end
	
