	FUNCTION GET_CDS_TEMPS, DATE, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS
;
; Name        :	GET_CDS_TEMPS()
;
; Purpose     :	Get the CDS 5-minute average temperature date.
;
; Category    :	Class3, CDS, Engineering
;
; Explanation :	Reads CDS 5-minute average temperature data.
;
; Syntax      :	Result = GET_CDS_TEMPS( DATE )
;
; Examples    :	
;
; Inputs      :	DATE	= The date/time value to get the temperatures for.
;			  Can be in any CDS time format.
;
; Opt. Inputs :	None.
;
; Outputs     :	The result of the function is an array containing the 32 CDS
;               temperatures.  If unable to find this information, zeroes are
;               returned instead.
;
; Opt. Outputs:	None.
;
; Keywords    :	ERRMSG	= If defined and passed, then any error messages will
;			  be returned to the user in this parameter rather than
;			  depending on the MESSAGE routine in IDL.  If no
;			  errors are encountered, then a null string is
;			  returned.  In order to use this feature, ERRMSG must
;			  be defined first, e.g.
;
;				ERRMSG = ''
;				Result = GET_CDS_TEMPS( ERRMSG=ERRMSG, ... )
;				IF ERRMSG NE '' THEN ...
;
; Calls       :	CONCAT_DIR, FXBOPEN, FXBREAD
;
; Common      :	Private common block GET_CDS_TEMPS is used to store data from
;               the last temperature file read.  Speeds up subsequent reads
;               when the same day is referenced.
;
; Restrictions:	The temperatures entries are interpolated from the 5-minute
;               averages to the requested time.
;
; Side effects:	None.
;
; Prev. Hist. :	None.
;
; History     :	Version 1, 24-Nov-2004, William Thompson, GSFC
;
; Contact     :	WTHOMPSON
;-
;
;	ON_ERROR, 2
	COMMON GET_CDS_TEMPS, LAST_DATE, UNIT, TAI, BTM
;
;  Make sure that LAST_DATE is defined.
;
	IF N_ELEMENTS(LAST_DATE) EQ 0 THEN LAST_DATE = ''
;
;  Initialize RESULT.  If the routine is successful, this will be updated
;  later.
;
	RESULT = FLTARR(32)
;
;  Check the number of parameters.
;
	IF N_PARAMS() LT 1 THEN BEGIN
		MESSAGE = 'Syntax:  Result = GET_CDS_TEMPS( DATE )'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Form the filename for the temperature file.
;
	SDATE = ANYTIM2CAL(DATE,FORM=8,/DATE)
        SDATE = STRMID(SDATE,0,4) + '_' + STRMID(SDATE,4,2)
	NAME = 'temps_' + SDATE + '.fits'
	FILENAME = CONCAT_DIR('$CDS_VDS_CAL_WAV', NAME)
;
;  Look for any files that match the search criteria.
;
	IF SDATE NE LAST_DATE THEN BEGIN
	    FILES = FINDFILE(FILENAME, COUNT=COUNT)
	    IF COUNT EQ 0 THEN BEGIN
                MESSAGE = 'No temperature file found for requested date'
                GOTO, HANDLE_ERROR
	    ENDIF
;
;  Read in the file.
;
READ_FILE:
	    FXBOPEN, UNIT, FILES[0], 1
	    LAST_DATE = SDATE
;
;  Read in the data.
;
	    FXBREAD, UNIT, MJD, 'MJD'
	    FXBREAD, UNIT, TIME, 'TIME'
	    FXBREAD, UNIT, BTM, 'BTM'
            UTC = {CDS_INT_TIME, MJD: 0L, TIME: 0L}
            UTC = REPLICATE(UTC, N_ELEMENTS(MJD))
            UTC.MJD = MJD
            UTC.TIME = TIME
            TAI = UTC2TAI(UTC)
	ENDIF
;
;  Find the closest two entries to the target time.
;
	TARGET = ANYTIM2TAI(DATE)
	DIFF = ABS(TAI - TARGET)
	MINDIF = MIN(DIFF, I1)
	IF TAI[I1] LT TARGET THEN I2 = I1+1 ELSE I2 = I1-1
        IF I2 LT 0 THEN I2 = I1+1
        IF I2 GE N_ELEMENTS(TAI) THEN I2 = I1-1
;
;  Interpolate the result.
;
        RESULT = (BTM[*,I2]*(TARGET-TAI[I1]) - BTM[*,I1]*(TARGET-TAI[I2])) / $
          (TAI[I2] - TAI[I1])
        RESULT = FLOAT(RESULT)
	GOTO, FINISH
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'GET_CDS_TEMPS: ' + MESSAGE $
		ELSE MESSAGE, MESSAGE, /CONTINUE
;
;  Close the FITS file and return the result.
;
FINISH:
	IF FXBISOPEN(UNIT) THEN FXBCLOSE, UNIT
	UNIT = -1
	RETURN, RESULT
;
	END
