	FUNCTION DEL_PLAN, START_TIME, INSTRUMENT=INSTRUMENT, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS
;
; Name        :	DEL_PLAN()
;
; Purpose     :	Deletes a SoHO/CDS science plan record from the database
;
; Explanation :	This routine removes an entry from the SoHO/CDS science
;		plan database.
;
; Use         :	DEL_PLAN, START_TIME
;
; Inputs      :	START_TIME = A double precision number giving the date/time
;			     value corresponding to the entry in the
;			     sci_plan database to be deleted.
;
; 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    :	INSTRUMENT = Instrument to delete plan entry for.  Can be
;			     passed either as the instrument name or as a
;			     single character code value.  Normally, this
;			     routine is used for deleting CDS records.
;			     However, the use of the INSTRUMENT keyword allows
;			     it to be used with the SOC planning tool.
;
;		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 = DEL_PLAN( ERRMSG=ERRMSG, ... )
;				IF ERRMSG NE '' THEN ...
;
;
; Calls       :	DATATYPE, DBOPEN, DBFIND, DBEXT, DBCLOSE, TRIM
;
; Common      :	None.
;
; Restrictions:	Only this routine or ADD_PLAN can be used to add or delete
;		SoHO/CDS science plan entries to or from the database.
;		Modifying the database by hand could corrupt its integrity.
;
; Side effects:	Marks records as deleted.  PRG_PLAN must be called to really
;		delete the records.
;
; Category    :	Planning, Databases.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 26 July 1994
;
; Modified    :	Version 1, William Thompson, GSFC, 3 August 1994
;               Version 2, Liyun Wang, GSFC/ARC, September 22, 1994
;                  Added the keyword ERRMSG.
;		Version 3, William Thompson, GSFC, 8 May 1995
;			Modified to pay attention to DELETED field in database
;		Version 4, William Thompson, GSFC, 16 May 1995
;			Added keyword INSTRUMENT
;		Version 5, William Thompson, GSFC, 19 May 1995
;			Changed way DBFIND is called, to speed up.
;		Version 6, William Thompson, GSFC, 22 May 1995
;			Made INS variable a named structure
;
; Version     :	Version 6, 22 May 1995
;-
;
	ON_ERROR, 2
;
;  Initialize RESULT to represent non-success.  If the routine is successful,
;  this value will be updated below.
;
	RESULT = 0
;
;  Check the number of parameters.
;
        IF N_PARAMS() NE 1 THEN BEGIN
           MESSAGE = 'Syntax:  DEL_PLAN, START_TIME'
	   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 the input parameter.
;
	IF DATATYPE(START_TIME,1) NE 'Double' THEN BEGIN
           MESSAGE = 'START_TIME must be double precision'
           GOTO, HANDLE_ERROR
	END ELSE IF N_ELEMENTS(START_TIME) NE 1 THEN BEGIN
           MESSAGE = 'START_TIME must be a scalar'
           GOTO, HANDLE_ERROR
	ENDIF
;
;  If the INSTRUMENT keyword was passed, then make sure that the instrument
;  code is valid.
;
	IF N_ELEMENTS(INSTRUMENT) EQ 1 THEN BEGIN
	    GET_INSTRUMENT, INSTRUMENT, INS
	    IF INS.CODE EQ '' THEN BEGIN
	        MESSAGE = 'Instrument ' + INSTRUMENT + ' not found in database'
	        GOTO, HANDLE_ERROR
	    ENDIF
;
;  Otherwise, assume CDS.
;
	END ELSE INS = {INS_CODE, CODE: 'C', NAME: 'CDS'}
;
;  Open the database for update.
;
	DBOPEN, 'sci_plan', 1
;
;  Search for the entry with the start date/time given by the user.
;
	ENTRIES = DBFIND('START_TIME=' + TRIM(START_TIME,'(F15.3)') +	$
		',INSTRUME=' + INS.CODE + ',DELETED=N', /SILENT)
;
;  If no entries were found, then return immediately.
;
	IF !ERR EQ 0 THEN BEGIN
           MESSAGE = 'No such entry in the database'
           GOTO, HANDLE_ERROR
	ENDIF
;
;  Mark the entry as deleted.
;
	DBUPDATE, ENTRIES, 'deleted', 'Y'
;
;  Signal success.
;
	RESULT = 1
	GOTO, FINISH
;
;  Error handling point.
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN ERRMSG = 'DEL_PLAN: ' + MESSAGE	$
		ELSE MESSAGE, MESSAGE, /CONTINUE
;
;  Close the database, and return.
;
FINISH:
	DBCLOSE
;
	RETURN, RESULT
	END
