	PRO LIST_DLYD_TIMES, START, EEND, COM, N_FOUND, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS
;
; Name        :	LIST_DLYD_TIMES
;
; Purpose     :	List the SoHO delayed command times for a given period
;
; Explanation :	Extracts all the SoHO Deep Space Network (COM) contact periods
;		in the input time range for which delayed commanding can take
;		place.  Currently, this is all the payload reserved times.
;
; Use         :	LIST_DLYD_TIMES, START, END, COM, N_FOUND
;
; Inputs      :	START, END = The range of date/time values to use in searching
;			     the database.  Can be in any standard CDS time
;			     format.
;
; Opt. Inputs :	None.
;
; Outputs     :	COM	= A structure variable containing the following tags
;			  for each planned contact period:
;
;			START_TIME   = Date/time of beginning of the command
;				       contact period, in TAI format
;			END_TIME     = Date/time of end of command contact
;				       period, in TAI format
;
;		N_FOUND	= Number of planned contact periods found.
;
; 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 = ''
;                       LIST_DLYD_TIMES, ERRMSG=ERRMSG, ... 
;                       IF ERRMSG NE '' THEN ...
;
;
; Calls       :	DATATYPE, DBOPEN, DBFIND, DBEXT, DBCLOSE, TRIM, GET_INSTRUMENT
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	If the number of planned contact periods found is zero, then
;		the output parameter COM is not modified.
;
; Category    :	Planning, Database.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 14 April 1995
;
; Modified    :	Version 1, William Thompson, GSFC, 14 April 1995
;
; Version     :	Version 1, 14 April 1995
;-
;
	ON_ERROR, 2
;
;  Check the number of parameters.
;
        IF N_PARAMS() LT 4 THEN BEGIN
           MESSAGE = 'Syntax:  LIST_DLYD_TIMES, START, END, COM, N_FOUND'
	   GOTO, HANDLE_ERROR
        ENDIF
;
;  Check the input parameters.
;
	N_FOUND = 0
	IF N_ELEMENTS(START) NE 1 THEN BEGIN
           MESSAGE = 'START must be a scalar'
           GOTO, HANDLE_ERROR
	END ELSE IF N_ELEMENTS(EEND) NE 1 THEN BEGIN
           MESSAGE = 'END must be a scalar'
           GOTO, HANDLE_ERROR
	ENDIF
;
;  Convert the input dates to TAI format.
;
	IF DATATYPE(START,1) EQ 'Double' THEN TAI_START = START ELSE	$
		TAI_START = UTC2TAI(START)
	IF DATATYPE(EEND,1) EQ 'Double' THEN TAI_END = EEND ELSE	$
		TAI_END = UTC2TAI(EEND)
;
;  Open the resource database.
;
	DBOPEN, 'resource,resource_type'
;
;  Find all the entries in the requested time range.
;
	ENTRIES = DBFIND('END_TIME>' + TRIM(TAI_START,'(F15.3)') +	$
		',START_TIME<' + TRIM(TAI_END,'(F15.3)') +	$
		',RES_TYPE=PAYLOAD_RESERVED', /SILENT)
	IF ENTRIES(0) LE 0 THEN BEGIN
		N_FOUND = 0
		GOTO, FINISH
	END ELSE N_FOUND = N_ELEMENTS(ENTRIES)
;
;  Extract the requested entries, sorted by start times.
;
	ENTRIES = DBSORT(ENTRIES,'start_time')
	DBEXT, ENTRIES, 'start_time,end_time', START_TIME, END_TIME
;
;  Remove any entries that only touch the requested time range.
;
	W = WHERE((END_TIME NE TAI_START) AND (START_TIME NE TAI_END), N_FOUND)
	IF N_FOUND EQ 0 THEN GOTO, FINISH
;
;  Define the structure that the data will be returned in.
;
	COM = { START_TIME: 0.0D0,	$
		END_TIME: 0.0D0}
;
	IF N_FOUND EQ 1 THEN BEGIN
		COM.START_TIME	= START_TIME(W(0))
		COM.END_TIME	= END_TIME(W(0))
	END ELSE BEGIN
		COM = REPLICATE(COM, N_FOUND)
		COM.START_TIME	= START_TIME(W)
		COM.END_TIME	= END_TIME(W)
	ENDELSE
;
	GOTO, FINISH
;
;  Handle any errors encountered.
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN		$
		ERRMSG = 'LIST_DLYD_TIMES: ' + MESSAGE ELSE MESSAGE, MESSAGE 
;
;  Close the database and return.
;
FINISH:
	DBCLOSE
;
	RETURN
	END
