	FUNCTION GET_TIME_DELAY, DATE, TYPE, OLD=OLD, ERRMSG=ERRMSG,	$
		RETAIN=RETAIN
;+
; Project     :	SOHO - CDS
;
; Name        :	GET_TIME_DELAY()
;
; Purpose     :	Get the time delay between SOHO and the Earth
;
; Category    :	Class3, Orbit, Time
;
; Explanation :	Calculate the distance between the Earth and the Sun, and the
;		SOHO spacecraft and the sun.  Convert this into a time delay to
;		be used in converting onboard time into Earth-based time.
;
;		Reads orbit information from either the definitive or
;		predictive orbit file, whichever it can find first.
;
; Syntax      :	Result = GET_TIME_DELAY( DATE  [, TYPE ] )
;
; Examples    :	In the following example, OBT_TIME is the onboard time, and
;		DATE_OBS is the time the same solar event would be observed at
;		the earth.  Note that the delay is not implemented if it can't
;		be calculated.
;
;		DELAY = GET_TIME_DELAY(OBT_TIME)
;		DATE_OBS = OBT_TIME
;		IF DELAY GT 0 THEN DATE_OBS = DATE_OBS + DELAY
;
; Inputs      :	DATE	= The date/time value to get the time delay for.  Can
;			  be in any CDS time format.
;
; Opt. Inputs :	None.
;
; Outputs     :	The result of the function is the time delay in seconds.  If
;		unable to calculate, then -1 is returned instead.
;
; Opt. Outputs:	TYPE	= Returns whether predictive or definitive data was
;			  used to calculate the result.  Returned as either
;			  "Definitive" or "Predictive".  If the routine fails
;			  to return an answer, then the null string is
;			  returned.
;
; Keywords    :	RETAIN	= If set, then the orbit FITS file will be left open.
;			  This speeds up subsequent reads.
;
;		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_TIME_DELAY( ERRMSG=ERRMSG, ... )
;				IF ERRMSG NE '' THEN ...
;
;		OLD	= If set, then files are read in from the subdirectory
;			  "old_samples".  This is used to test the software
;			  until real data files are available.
;
; Calls       :	GET_ORBIT
;
; Common      :	None.
;
; Restrictions:	The orbit entries for the time closest to that requested is
;		used to calculate the orbit parameters.  Since the orbit data
;		is calculated every 10 minutes, this should be correct within
;		+/-5 minutes.  No attempt is made to interpolate to closer
;		accuracy than that.
;
; Side effects:	None.
;
; Prev. Hist. :	None.
;
; History     :	Version 1, 04-Dec-1995, William Thompson, GSFC
;		Version 2, 07-Dec-1995, William Thompson, GSFC
;			Rewrote to use GET_ORBIT.
;			Added keyword RETAIN
;		Version 3, 11-Dec-1995, William Thompson, GSFC
;			Simplified calculation
;
; Contact     :	WTHOMPSON
;-
;
	ON_ERROR, 2
;
;  Initialize RESULT to -1.  If the routine is successful, this will be updated
;  later.
;
	RESULT = -1
	TYPE = ""
;
;  Check the number of parameters.
;
	IF N_PARAMS() LT 1 THEN BEGIN
		MESSAGE = 'Syntax:  Result = GET_TIME_DELAY( DATE  [, TYPE ] )'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Read in the orbit information from either the definitive or predictive orbit
;  file.
;
	O = GET_ORBIT(DATE, TYPE, OLD=OLD, ERRMSG=ERRMSG, RETAIN=RETAIN)
	IF N_ELEMENTS(ERRMSG) NE 0 THEN BEGIN
		MESSAGE = ERRMSG
		IF MESSAGE NE "" THEN GOTO, HANDLE_ERROR
	ENDIF
	IF TYPE EQ "" THEN GOTO, FINISH
;
;  Calculate the spacecraft-Sun distance, and the Earth-Sun distance, and use
;  the speed of light to convert into seconds.
;
	L = SQRT(O.HEC_X^2 + O.HEC_Y^2 + O.HEC_Z^2)	;SC-Sun distance
	R = SQRT(O.SUN_VECTOR_X^2 + O.SUN_VECTOR_Y^2 + O.SUN_VECTOR_Z^2)
	RESULT = (R - L) / 2.99773E5
	GOTO, FINISH
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'GET_TIME_DELAY: ' + MESSAGE $
		ELSE MESSAGE, MESSAGE, /CONTINUE
;
;  Return the result.
;
FINISH:
	RETURN, RESULT
	END
