;+
;
;  Name:
;       CALIB_HIST
;
;
; PURPOSE:  compile and update the BATSE SPEC calibration info
;	for each detector for every flare
;
;
; CATEGORY:    
;	BATSE
;
;
; CALLING SEQUENCE:  calib_hist, filename
;
;
; CALLED BY:
;
;
; CALLS:
;	none
;
; INPUTS:
;       Filename- File to read.
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	Updates SSWDB_BATSE:cal_spec.dat
;
; RESTRICTIONS:
;	VMS only
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	ras, 15-feb-95
;               RAS, 25-Jul-1997, changed PERM_DATA to SSWDB_BATSE
;	Version 3, richard.schwartz@gsfc.nasa.gov, 24-jun-1998, update for IDL 5.1 VAX IEEE float.
;-

pro calib_hist, filename

batse_read_cat

@flare_catalog

read_cal_spec
common cal_catalog, cal_spec

@spectral_history.str

openr,lu,/get,filename
readu,lu,spec_hist_fdr

spec_hist_fdr = conv_vax_unix( use_vax_float(/old2new, spec_hist_fdr))

nrec=spec_hist_fdr.next_rec_num -2
maxsize = spec_hist_fdr.maxsize
status = fstat(lu)
realsize = status.size
if realsize gt maxsize*512L then nrec=maxsize 
start_time = dblarr(nrec)
duration   = intarr(nrec)
block = replicate(spectral_hist,100)
sub_block = replicate(spectral_hist, nrec mod 100)
chan_511 = dblarr(nrec)
dmin = min( abs(511.-spec_hist_fdr.line_energy), i511)

total_rec = 0
on_ioerror, surprise
for i=0,nrec/100-1 do begin  
  readu,lu,block  
  block = conv_vax_unix( use_vax_float( block, /old2new ))
  tjd = tjd2ymd( block.start_day) +block.start_sec  
  start_time(i*100) = tjd 
  duration(i*100) = tjd2ymd( block.stop_day) +block.stop_sec - tjd
  chan_511(i*100) = block.peak_chan(i511) 
  total_rec = total_rec + 100
endfor
readu,lu,sub_block
  sub_block = conv_vax_unix( use_vax_float( sub_block, /old2new ))
  tjd = tjd2ymd( sub_block.start_day) +sub_block.start_sec  
  start_time(i*100) =  tjd
  duration(i*100) = tjd2ymd( sub_block.stop_day) +sub_block.stop_sec - tjd
  chan_511(i*100) = sub_block.peak_chan(i511) 
  total_rec = n_elements(start_time)
  print, total_rec,' records read.'
goto, around
surprise:
	print,'Not the expected number of records.'
	print,nrec, ' expected, ',total_rec,' read.'
on_ioerror, null	

around:
free_lun,lu

if total_rec lt nrec then begin
  start_time=start_time(0:total_rec-1)
  duration=duration(0:total_rec-1)
  chan_511=chan_511(0:total_rec-1)
endif

ord = sort(start_time)

start_time = start_time(ord)
duration = duration(ord)
chan_511 = chan_511(ord)
id = fix( string(spec_hist_fdr.det_num))

t1 = start_time(0)
t2= max(start_time+duration)

wbef = where( fldata.peak_secs lt t1, nwbef) ;all those before
if nwbef ge 1 then begin
	dbef = min( t1 - fldata(wbef).peak_secs, ibef) ;this is the flare right before
	cal_spec( wbef(ibef)).flare_num = fldata(wbef(ibef)).flare_num
	cal_spec( wbef(ibef)).utime(1,id)  = t1
	cal_spec( wbef(ibef)).chan_511(1,id)  = chan_511(0)
endif

	
waft = where( fldata.peak_secs gt t2, nwaft) ;all those after
if nwaft ge 1 then begin
	daft = min( fldata(waft).peak_secs - t2, iaft) ;this is the flare right after
	cal_spec( waft(iaft)).flare_num = fldata(waft(iaft)).flare_num
	cal_spec( waft(iaft)).utime(0,id)  = max(start_time,imax)
	cal_spec( waft(iaft)).chan_511(0,id)  = chan_511(imax)
endif

;for events surrounded by the calibration data fill in the 511 line positions
w = where(fldata.peak_secs ge t1 and fldata.peak_secs le t2,nw)
cal_spec(w).flare_num = fldata(w).flare_num

for i=0,nw-1 do begin  
  wcal = where(  start_time le fldata(w(i)).peak_secs, nwcal)  
  cal_spec(w(i)).utime(0,id) = start_time(wcal(nwcal-1))
  cal_spec(w(i)).chan_511(0,id) = chan_511(wcal(nwcal-1))
  wcal = where(  start_time gt fldata(w(i)).peak_secs, nwcal)  
  if nwcal ge 1 then begin
	cal_spec(w(i)).utime(1,id) = start_time(wcal(0))
	cal_spec(w(i)).chan_511(1,id) = chan_511(wcal(0))
  endif
  
endfor
;stop

openw, lu, /get, concat_dir('SSWDB_BATSE','cal_spec.dat')
fnum = n_elements(cal_spec)
writeu, lu, fnum
for i=0,fnum-1 do writeu, lu, use_vax_float(/new2old, cal_spec(i))
free_lun,lu

end

