	FUNCTION CLR_TEL_SUBMODE, CLR_START, CLR_END, ERRMSG=ERRMSG
;+
; Project     :	SOHO - CDS
;
; Name        :	CLR_TEL_SUBMODE()
;
; Purpose     :	Deletes SoHO telemetry submode entries from the database
;
; Explanation :	This routine removes all entries with start times between
;		CLR_START and CLR_END from the SoHO telemetry submode database.
;
; Use         :	Result = CLR_TEL_SUBMODE( CLR_START, CLR_END )
;
;		IF NOT CLR_TEL_SUBMODE( CLR_START, CLR_END ) THEN ...
;
; Inputs      :	CLR_START = Together with CLR_END, these define a period of
;			    time.  All entries in the database with start times
;			    within this time period are deleted.
;		CLR_END	  = ...
;
; 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.  Having nothing that needs to
;		be deleted is counted as success.
;
; 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 = CLR_TEL_SUBMODE( ERRMSG=ERRMSG, ... )
;                       IF ERRMSG NE '' THEN ...
;
;
; Calls       :	DATATYPE, DBOPEN, DBFIND, DBEXT, DBCLOSE, TRIM
;
; Common      :	None.
;
; Restrictions:	Only this routine or ADD_TEL_SUBMODE can be used to add or
;		delete SoHO telemetry submode entries to or from the database.
;		Modifying the database by hand could corrupt its integrity.
;
;		!PRIV must be 3 or greater to use this routine.
;
; Side effects:	None.
;
; Category    :	Planning, Databases.
;
; Prev. Hist. :	None.
;
; Written     :	William Thompson, GSFC, 3 April 1995
;
; Modified    :	Version 1, William Thompson, GSFC, 3 April 1995
;
; Version     :	Version 1, 3 April 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 2 THEN BEGIN
           MESSAGE = 'Syntax:  Result = CLR_TEL_SUBMODE( CLR_START, CLR_END )'
	   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 parameters.
;
	IF N_ELEMENTS(CLR_START) NE 1 THEN BEGIN
           MESSAGE = 'CLR_START must be a scalar'
           GOTO, HANDLE_ERROR
	END ELSE IF N_ELEMENTS(CLR_END) NE 1 THEN BEGIN
           MESSAGE = 'CLR_END must be a scalar'
           GOTO, HANDLE_ERROR
	ENDIF
;
;  Convert the input dates to TAI format.
;
	IF DATATYPE(CLR_START,1) EQ 'Double' THEN TAI_START = CLR_START ELSE $
		TAI_START = UTC2TAI(CLR_START)
	IF DATATYPE(CLR_END,1) EQ 'Double' THEN TAI_END = CLR_END ELSE	$
		TAI_END = UTC2TAI(CLR_END)
;
;  Make sure that the start and stop times are in the correct order.
;
	IF TAI_END LE TAI_START THEN BEGIN
		MESSAGE = 'Start time must preceed end time'
		GOTO, HANDLE_ERROR
	ENDIF
;
;  Open the database for update.
;
	DBOPEN, 'telem_submode', 1
;
;  Search for entries within the date/time range given by the user.
;
	ENTRIES = DBFIND('START_TIME>' + TRIM(TAI_START,'(F15.3)') +	$
		',START_TIME<' + TRIM(TAI_END,'(F15.3)'), /SILENT)
;
;  If no entries were found, then return immediately.
;
	IF ENTRIES(0) LE 0 THEN BEGIN
		RESULT = 1
		GOTO, FINISH
	ENDIF
;
;  Extract the start times, and ignore any entries that start right at TAI_END.
;
	DBEXT, ENTRIES, 'START_TIME', START_TIME
	W = WHERE(START_TIME LT TAI_END, COUNT)
	IF COUNT EQ 0 THEN BEGIN
		RESULT = 1
		GOTO, FINISH
	ENDIF
;
;  Delete the entries.
;
	DBDELETE, ENTRIES(W)
;
;  Signal success.
;
	RESULT = 1
	GOTO, FINISH
;
;  Handle any errors encountered.
;
HANDLE_ERROR:
	IF N_ELEMENTS(ERRMSG) NE 0 THEN		$
		ERRMSG = 'CLR_TEL_SUBMODE: ' + MESSAGE ELSE MESSAGE, MESSAGE 
	RESULT = 0
;
;  Close the database, and return whether successful or not.
;
FINISH:
	DBCLOSE
;
	RETURN, RESULT
	END
