	FUNCTION ADD_INST_DLYD, RES, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS
;
; Name        :	ADD_INST_DLYD()
;
; Purpose     :	Adds an entry to the instrument delayed command database.
;
; Explanation :	This procedure adds an entry for a given instrument for delayed
;		command upload time to the "inst_delayed_cmd" database.
;
; Use         :	Result = ADD_INST_DLYD( RES )
;
;		IF ADD_INST_DLYD( RES ) EQ 0 THEN ...
;
; Inputs      :	RES = A structure variable containing the following tags:
;
;			INSTRUME   = Single letter code specifying the
;				     instrument.
;			EARLIEST = The start time of the reserved time
;			LATEST   = The end time of the reserved time.
;			NUM_CMDS   = The expected average number of OBDH block
;				     commands per minute between the start time
;				     and end time.
;
;		      It can also be an array of such structures.
;
; Opt. Inputs :	None.
;
; Outputs     :	The result of the function is a logical value representing
;		whether or not the operation was successful, where 1 is
;		successful and 0 is unsuccessful.
;
; 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 = ADD_INST_DLYD( ERRMSG=ERRMSG, ... )
;                       IF ERRMSG NE '' THEN ...
;
; Calls       :	DATATYPE, DBOPEN, DB_INFO, DBBUILD, DBCLOSE, DBFIND, DBMATCH
;
; Common      :	None.
;
; Restrictions:	Only this routine or CLR_INST_DLYD can be used to add or delete
;		delayed command time entries to the database.  Modifying the
;		database by hand could corrupt its integrity.
;
;		The data types and sizes of the structure elements must match
;		the definitions in the database.
;
;		!PRIV must be 2 or greater to use this routine.
;
; Side effects:	None.
;
; Category    :	Planning, Databases.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 6 March 1996
;
; Modified    :	Version 1, William Thompson, GSFC, 6 March 1996
;
; Version     :	Version 1, 6 March 1996
;-
;
	ON_ERROR, 2
;
;  Check the input parameters
;
	IF N_PARAMS() NE 1 THEN BEGIN
		MESSAGE = 'Syntax:  Result = ADD_INST_DLYD(RES)'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Make sure that the user has privilege to write into the database.
;
	IF !PRIV LT 2 THEN BEGIN
	   MESSAGE = '!PRIV must be 2 or greater to write into the database'
	   GOTO, HANDLE_ERROR
	ENDIF
;
;  Check each of the structure components to verify that it is of the correct
;  type and size.
;
	FOR I = 0,N_ELEMENTS(RES)-1 DO BEGIN
	    IF DATATYPE(RES(I).INSTRUME,1) NE 'String' THEN BEGIN
	       MESSAGE = 'Tag INSTRUME must be a character string'
	       GOTO, HANDLE_ERROR
	    END ELSE IF N_ELEMENTS(RES(I).INSTRUME) NE 1 THEN BEGIN
	       MESSAGE = 'Tag INSTRUME must be a scalar'
	       GOTO, HANDLE_ERROR
	    END ELSE IF STRLEN(RES(I).INSTRUME) NE 1 THEN BEGIN $
	       MESSAGE = 'Tag INSTRUME must be a single character'
	       GOTO, HANDLE_ERROR
	    ENDIF
;
	    IF DATATYPE(RES(I).EARLIEST,1) NE 'Double' THEN BEGIN
	       MESSAGE = 'Tag EARLIEST must be a double precision number'
	       GOTO, HANDLE_ERROR
	    END ELSE IF N_ELEMENTS(RES(I).EARLIEST) NE 1 THEN BEGIN
	       MESSAGE = 'Tag EARLIEST must be a scalar'
	       GOTO, HANDLE_ERROR
	    ENDIF
;
	    IF DATATYPE(RES(I).LATEST,1) NE 'Double' THEN BEGIN
	       MESSAGE = 'Tag LATEST must be a double precision number'
	       GOTO, HANDLE_ERROR
	    END ELSE IF N_ELEMENTS(RES(I).LATEST) NE 1 THEN BEGIN
	       MESSAGE = 'Tag LATEST must be a scalar'
	       GOTO, HANDLE_ERROR
	    ENDIF
;
	    IF DATATYPE(RES(I).NUM_CMDS,1) NE 'Integer' THEN BEGIN
	       MESSAGE = 'Tag NUM_CMDS must be a short integer'
	       GOTO, HANDLE_ERROR
	    END ELSE IF N_ELEMENTS(RES(I).NUM_CMDS) NE 1 THEN BEGIN
	       MESSAGE = 'Tag NUM_CMDS must be a scalar'
	       GOTO, HANDLE_ERROR
	    ENDIF
;
;  Make sure that the instrument code is valid.
;
	    GET_INSTRUMENT, RES(I).INSTRUME, TEMP
	    IF TEMP.CODE EQ '' THEN BEGIN
	        MESSAGE = 'Instrument ' + RES(I).INSTRUME +	$
			' not found in database'
	        GOTO, HANDLE_ERROR
	    ENDIF
	    IF I EQ 0 THEN INS = REPLICATE(TEMP,N_ELEMENTS(RES)) ELSE	$
		INS(I) = TEMP
	ENDFOR
;
;  Open the database for write access.
;
	DBOPEN, 'inst_delayed_cmd', 1
;
;  Append the data to the opened database.
;
	DBBUILD, RES.EARLIEST, RES.LATEST, INS.CODE, RES.NUM_CMDS,	$
		STATUS=STATUS
	IF STATUS EQ 0 THEN BEGIN
	   MESSAGE = 'Write to inst_delayed_cmd database was not successful'
	   GOTO, HANDLE_ERROR
	ENDIF
;
	RESULT = 1
	GOTO, FINISH
;
;  Handle any errors encountered.
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'ADD_INST_DLYD: ' + MESSAGE $
		ELSE MESSAGE, MESSAGE, /CONTINUE
	RESULT = 0
;
;  Close the database, and return whether the routine was successful or not.
;
FINISH:
	DBCLOSE
;
	RETURN, RESULT
	END
