	FUNCTION ADD_RESOURCE, RES, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS
;
; Name        :	ADD_RESOURCE()
;
; Purpose     :	Adds an entry to the general resource database.
;
; Explanation :	This procedure adds an entry for a general resource at a given
;		time to the "resource" database.
;
; Use         :	Result = ADD_RESOURCE( RES )
;
;		IF ADD_RESOURCE( RES ) EQ 0 THEN ...
;
; Inputs      :	RES = A structure variable containing the following tags:
;
;			RES_TYPE   = The resource type, such as "DSN_CONTACT".
;			START_TIME = The start time of the resource.
;			END_TIME   = The end time of the resource, if any.  For
;				     some resources this parameter is ignored.
;
;		     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_RESOURCE( ERRMSG=ERRMSG, ... )
;                       IF ERRMSG NE '' THEN ...
;
; Calls       :	DATATYPE, DBOPEN, DB_INFO, DBBUILD, DBCLOSE, DBFIND, DBMATCH
;
; Common      :	None.
;
; Restrictions:	Only this routine or CLR_RESOURCE can be used to add or delete
;		resource descriptions 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.
;
;		The resource type must be listed in the resource_type database.
;
;		!PRIV must be 2 or greater to use this routine.
;
; Side effects:	None.
;
; Category    :	Planning, Databases.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 31 March 1995
;
; Modified    :	Version 1, William Thompson, GSFC, 3 April 1995
;		Version 2, William Thompson, GSFC, 22 May 1995
;			Modified to allow array inputs.
;		Version 3, William Thompson, GSFC, 14 August 1995
;			Modified to call LIST_RES_TYPE.  Together with changes
;			to that routine, this routine is speeded up.
;
; Version     :	Version 3, 14 August 1995.
;-
;
	ON_ERROR, 2
;
;  Check the input parameters
;
	IF N_PARAMS() NE 1 THEN BEGIN
		MESSAGE = 'Syntax:  Result = ADD_RESOURCE(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
;
;  Initialize the RES_PTR and TYPE_ID arrays.
;
	RES_PTR = LONARR(N_ELEMENTS(RES))
	TYPE_ID = INTARR(N_ELEMENTS(RES))
;
;  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).RES_TYPE,1) NE 'String' THEN BEGIN
	       MESSAGE = 'Tag RES_TYPE must be a character string'
	       GOTO, HANDLE_ERROR
	    END ELSE IF N_ELEMENTS(RES(I).RES_TYPE) NE 1 THEN BEGIN
	       MESSAGE = 'Tag RES_TYPE must be a scalar'
	       GOTO, HANDLE_ERROR
	    END ELSE IF STRLEN(RES(I).RES_TYPE) GT 30 THEN BEGIN $
	       MESSAGE = 'Tag RES_TYPE must be 30 characters or less'
	       GOTO, HANDLE_ERROR
	    ENDIF
;
	    IF DATATYPE(RES(I).START_TIME,1) NE 'Double' THEN BEGIN
	       MESSAGE = 'Tag START_TIME must be a double precision number'
	       GOTO, HANDLE_ERROR
	    END ELSE IF N_ELEMENTS(RES(I).START_TIME) NE 1 THEN BEGIN
	       MESSAGE = 'Tag START_TIME must be a scalar'
	       GOTO, HANDLE_ERROR
	    ENDIF
;
	    IF DATATYPE(RES(I).END_TIME,1) NE 'Double' THEN BEGIN
	       MESSAGE = 'Tag END_TIME must be a double precision number'
	       GOTO, HANDLE_ERROR
	    END ELSE IF N_ELEMENTS(RES(I).END_TIME) NE 1 THEN BEGIN
	       MESSAGE = 'Tag END_TIME must be a scalar'
	       GOTO, HANDLE_ERROR
	    ENDIF
;
;  Find the pointer to the resource types database.
;
	    LIST_RES_TYPE, TYPES, IDS
	    TEMP = WHERE(RES(I).RES_TYPE EQ TYPES)
	    RES_PTR(I) = TEMP(0) + 1
	    IF RES_PTR(I) LE 0 THEN BEGIN
		MESSAGE = 'Resource type ' + RES(I).RES_TYPE + ' not found'
		GOTO, HANDLE_ERROR
	    ENDIF
;
;  Extract the type ID number.
;
	    TYPE_ID(I) = IDS(TEMP(0))
	ENDFOR
;
;  Open the database for write access.
;
	DBOPEN, 'resource', 1
;
;  Append the data to the opened database.
;
	DBBUILD, TYPE_ID, RES_PTR, RES.START_TIME, RES.END_TIME, STATUS=STATUS
	IF STATUS EQ 0 THEN BEGIN
	   MESSAGE = 'Write to resource 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_RESOURCE: ' + MESSAGE $
		ELSE MESSAGE, MESSAGE 
	RESULT = 0
;
;  Close the database, and return whether the routine was successful or not.
;
FINISH:
	DBCLOSE
;
	RETURN, RESULT
	END
