;+
; NAME:
;	GET_SPEC_CAL
;
; PURPOSE:
;	Return linear channel number of the 511 line for a given time for
;	the 8 BATSE detectors as well as width64 values.  Obtained by
;	active calibration using LOAD_SHER, ...,/just_cal.
;	Values are loaded into the file using the write keyword.
;	If the intime is equal to an existing time, the existing
;	values are overwritten and a message is issued.
;
; CALLING SEQUENCE:
;	get_spec_cal, intime, times, chans, widths [,/write]
;
; RESTRICTIONS:
;	Reads an ASCII file loaded using the routine load_sher.  The file
;	contains a time and the linear channel numbers and the value of
;	width64 for 8 detectors on
;	each line.  The file will have to be updated at regular intervals 
;	to contain the latest information.
;	Each line may not contain information for every detector. For each
;	detector, GET_SPEC_CAL finds the closest time that is before the
;	requested time that contains non-zero information for that det.
;
; INPUTS:
;	INTIME - Time to retrieve 511 line channels for in string format or
;		seconds since 79/1/1.  GET_SPEC_CAL will find the closest 
;		time in the file to INTIME.  If there is no time within
;		5000 seconds, a complaint message will be issued.
;		If INTIME is before start of file, earliest data in file
;		will be returned.  If after last time of file, last data
;		will be returned.
;
;	Optional - /write 
;		New values are written into the table
; OUTPUTS:
;	TIMES - Times associated with the data returned
;	CHANS - Linear channel number of the 511 line for 8 detectors
;	WIDTHS- WIDTH64 for each detector
;
; SAMPLE CALL:  get_spec_cal, '92/4/5,1200', times, chans, widths
;
; MODIFICATION HISTORY:
;	Written:  RAS, 1-dec-93
;
;               RAS, 7-Jul-1997, changed PERM_DATA to SSWDB_BATSE
;----------------------------------------------------------------------

        
pro get_spec_cal, intime, outtimes, outchans, outwidths, write=write

; Read calibration file.  LINES will contain lines that look like:
; 91/06/30, 0255:30.  459.662  335.735  115.540   48.932   53.376  102.192 
read_seqfile, lines,CONCAT_DIR(getenv('SSWDB_BATSE'),'spec_cal.dat')

nlines = n_elements(lines)
times = utime(strmid(lines, 0, 18))
chans = fltarr(8, nlines)
widths = intarr(8,nlines)
reads, strmid(lines, 19, 120), chans, widths

; User's time may be any format.  Convert to seconds.
usertime = anytim (intime, /sec)



; find closest time to USERTIME 
; data.
q = min( abs( usertime - times), index)

if not keyword_set(write) then begin
   outtimes = times(index)
   outchans = chans(*,index)
   outwidths= widths(*,index)
   if q gt 5000. then print,'Warning, calibration is more than 5000 seconds from requested time.'
   return
endif

;write the new data into the file
line = strmid( atime(usertime, /hx), 0, 18) + $
	string(outchans, form='(8f9.3)') + string(outwidths, form='(8i3)')
if q lt 10.0 then lines(index) = line else $ ;overwrite
	lines = [lines,line]
openu,lu,/get, CONCAT_DIR(getenv('SSWDB_BATSE'),'spec_cal.dat')
for i=0,n_elements(lines)-1 do printf,lu, lines(i)
free_lun,lu

return&end

