	FUNCTION OPEN_NEXT, TEL_MODE, UNIT
;+
; Project     : SOHO - CDS
;
; Name        : OPEN_NEXT()
;
; Purpose     : Opens the next CDS science telemetry stream file.
;
; Explanation : Opens the next CDS science telemetry stream file.  If FITSGEN
;		is running, then the next file in the FILE_NAME array is
;		opened.  Also, if RUN_STATE from FITSGEN is set, and one has
;		already reached the end of the FILE_NAME array, then the
;		program tries to find the next filename on its own.
;
; Use         : OPEN_NEXT, TEL_MODE, UNIT
;
; Inputs      : TEL_MODE = Input mode.  The following modes have been defined.
;
;				0:	Replay stored telemetry.
;				1:	Realtime telemetry.
;				2:	Quicklook tape recorder dumps
;				3:	Realtime telemetry archived by ECS
;				4:	Final distribution
;
;		UNIT	 = Unit number that current file was opened with.  Next
;			   file will also be opened with this unit number.
;
; Opt. Inputs : None.
;
; Outputs     : TEL_MODE = The input mode of the next telemetry file in
;			   sequence may be different from the previous one.
;			   This allows processing rasters that span across the
;			   division between tape recorder dumps and realtime
;			   telemetry.
;
;		The result of the function is either true (1) for successful or
;		false (0) for unsuccessful.
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Calls       : BREAK_FILE, CDS_MESSAGE, OPEN_TM_FILE
;
; Common      : FITSGEN from the include file "fitsgen.cmn".
;
; Restrictions: There must already be a telemetry file open.
;
; Side effects: None.
;
; Category    : Data Handling, Telemetry.
;
; Prev. Hist. : Although there is an older routine with this same name, this
;		routine is a complete rewrite.
;
; Written     : William Thompson, GSFC, 16 April 1993.
;
; Modified    : Version 1, William Thompson, GSFC, 16 April 1993.
;		Version 2, William Thompson, GSFC, 1 September 1994
;			Added FITSGEN common block.
;		Version 3, William Thompson, GSFC, 31 January 1995
;			Modified to work with realtime telemetry files.
;			Also implemented strategy for determining the next file
;			in the series.
;		Version 4, William Thompson, GSFC, 3 February 1995
;			Added support for .QKL and and .REL files.
;		Version 5, William Thompson, GSFC, 6 February 1995
;			Modified to support final distribution format.
;		Version 6, William Thompson, GSFC, 27 June 1995
;			Fixed check for trying to reopen same RT file.
;		Version 7, William Thompson, GSFC, 21 December 1995
;			Added test for AUTOSTOP.
;		Version 8, William Thompson, GSFC, 27 December 1995
;			Added test for time in filename for files other than
;			"tm." format.
;		Version 9, William Thompson, GSFC, 23 January 1996
;			Corrected bug with FD filenames.
;		Version 10, William Thompson, GSFC, 26 February 1996
;			Improved error message.
;		Version 11, William Thompson, GSFC, 6 March 1996
;			If it can't find the next telemetry file of the same
;			type, then it will try changing TEL_MODE before giving
;			up completely.
;		Version 12, William Thompson, GSFC, 11 March 1996
;			Corrected error introduced in version 11.
;		Version 13, William Thompson, GSFC, 15 March 1996
;			Corrected bug where the same quicklook file could be
;			read over and over in a loop.
;			Search for .REL files as well as .QKL and tm.
;		Version 14, William Thompson, GSFC, 17 April 1996
;			Fixed bug introduced in version 8 of GET_SCI_PKT, when
;			telemetry stops in the middle of the hour.
;		Version 15, William Thompson, GSFC, 20 May 1996
;			Call CDS_MESSAGE with /CONTINUE so attention message is
;			not generated.
;		Version 16, William Thompson, GSFC, 18 August 1997
;			When processing CDROM data, keep trying filenames until
;			the end of the day.
;		Version 17, 5-Jan-2000, William Thompson, GSFC
;			Try both forms of filename for "tm.*" to get around Y2K
;			problem in Spacetek software.
;
; Version     :	Version 17, 5-Jan-2000
;-
;
	ON_ERROR,2
;
;  Include the common block to support the FITSGEN routine.
;
@fitsgen.cmn
;
;  Check the number of parameters.
;
	IF N_PARAMS() NE 2 THEN MESSAGE,	$
		'Syntax:  Result = OPEN_NEXT( TEL_MODE, UNIT )'
;
;  Save the current telemetry mode for later.  Define MESSAGE to be the null
;  string.  Initialize N_TRIES.
;
	OLD_MODE = TEL_MODE
	MESSAGE = ''
	N_TRIES = 0
;
;  In realtime mode, get the current name of the realtime telemetry file and
;  check it against the name of the file currently open.
;
	IF TEL_MODE EQ 1 THEN BEGIN
		FILENAME = GT_RT_FILE()
		FILE_STATUS = FSTAT(UNIT)
		BREAK_FILE, FILE_STATUS.NAME, DISK, DIR, NAME, EXT
		IF (FILENAME EQ NAME+EXT) OR (NAME+EXT EQ '') THEN GOTO, ABORT
		FILENAME = CONCAT_DIR('$CDS_TM_DATA', FILENAME)
		GOTO, OPEN_FILE
;
;  Otherwise, if FITSGEN is running, then open the next file in the sequence.
;
	END ELSE IF KEYWORD_SET(NUM_DATA_FILES) THEN BEGIN
		FILE_SEQ_NO = FILE_SEQ_NO + 1
		IF FILE_SEQ_NO GE NUM_DATA_FILES THEN GOTO, TRY_NEXT
		FILENAME = FILE_NAME(FILE_SEQ_NO)
		GOTO, OPEN_FILE
	ENDIF
;
;  If RUN_STATE is set, then attempt to figure out what the name of the next
;  file should be.
;
TRY_NEXT:
	IF KEYWORD_SET(RUN_STATE) THEN BEGIN
		FILE_STATUS = FSTAT(UNIT)
		FILENAME = FILE_STATUS.NAME
;
;  If processing CDROM data, then keep trying the next name up until the end of
;  the day.
;
TRY_NEXT_NAME:
		BREAK_FILE, FILENAME, DISK, DIR, RFILE, REXT
;
;  For realtime telemetry files stored by the CDS EGSE, assume the filename is
;  of the form "tm.YY_DDD_HH" or "tm.YYY_DDD_H" (Y2K problem).
;
		IF (TEL_MODE EQ 0) OR (TEL_MODE EQ 1) THEN BEGIN
;
;  If DATE is not already defined, then extract it from the filename, and add
;  one hour.
;
		    IF N_ELEMENTS(DATE) EQ 0 THEN BEGIN
			REXT = STR_SEP(STRMID(REXT,1,STRLEN(REXT)-1), '_')
			YEAR = REXT(0)
			IF STRLEN(YEAR) EQ 3 THEN YEAR = STRMID(YEAR,1,2)
			DOY  = REXT(1)
			HOUR = REXT(2)
			IF STRLEN(HOUR) EQ 2 THEN BEGIN
			    HOUR = FIX(HOUR)
			    HOUR = STRMID(STRTRIM(HOUR + 101,2),1,2)
			END ELSE CASE HOUR OF
			    '0': HOUR = '10'
			    '1': HOUR = '20'
			    '2': HOUR = '24'
			ENDCASE
			ERRMSG = ""
			UTC  = STR2UTC(YEAR+'-'+DOY+'T'+HOUR, ERRMSG=ERRMSG)
			IF ERRMSG NE '' THEN CDS_MESSAGE, ERRMSG, /CONTINUE
			CHECK_INT_TIME, UTC
			UTC = INT2UTC(UTC)
			IF ERRMSG NE "" THEN BEGIN
			    MESSAGE = 'Unable to form next file name'
			    GOTO, ABORT
			ENDIF
			PATH = DISK + DIR
		    END ELSE UTC = ANYTIM2UTC(DATE,/EXTERNAL)
;
;  Form the filename.
;
		    YEAR = STRMID(TRIM(UTC.YEAR),2,2)
		    DOY  = STRMID(STRING(UTC2DOY(UTC)+1000,	$
			    FORM='(I4)'),1,3)
		    HOUR = STRMID(TRIM(UTC.HOUR+100),1,2)
		    FILENAME = CONCAT_DIR(PATH, 'tm.'+YEAR+'_'+DOY+'_'+HOUR)
;
;  If the file doesn't exist, then try the alternate form.
;
		    IF NOT FILE_EXIST(FILENAME) THEN BEGIN
			YEAR = '1' + YEAR
			HOUR = STRMID(HOUR,0,1)
		    ENDIF
		    FILENAME = CONCAT_DIR(PATH, 'tm.'+YEAR+'_'+DOY+'_'+HOUR)
;
;  Otherwise, search for the file with the correct prefix and extension, which
;  has the closest time to the time of the last packet.
;
		END ELSE BEGIN
;
;  Extract the date from the packet time, and determine the dates for the days
;  on either side.  Collect all the filenames for those three days.
;
		    IF N_ELEMENTS(DATE) EQ 0 THEN PATH = DISK + DIR
		    PACKETTIME = OBT2TAI(LPACKET.HDR.TIME)
		    FILES = ''
		    FOR II = -1,1 DO BEGIN
			UTC = ANYTIM2UTC(PACKETTIME)
			UTC.MJD = UTC.MJD + II
			IF TEL_MODE EQ 4 THEN BEGIN
			    F = '*_' + ANYTIM2UTC(UTC,/CCSDS,/DATE) + 'T*.D*'
			END ELSE BEGIN
			    F = STRMID(ANYTIM2CAL(UTC,FORM=8,/DATE),2,6)
			    F = '*_' + F + '_*.*'
			ENDELSE
			F = FINDFILE(CONCAT_DIR(PATH,F),COUNT=COUNT)
			IF COUNT GT 0 THEN FILES = [FILES,F]
		    ENDFOR
;
		    IF N_ELEMENTS(FILES) EQ 1 THEN GOTO, ABORT ELSE	$
			    FILES = FILES(1:*)
;
;  Parse the filenames for dates and times.
;
		    IF TEL_MODE EQ 4 THEN BEGIN
			BREAK_FILE, FILES, DISKS, DIRS, NAMES
			FILETIME = STRMID(NAMES,13,13) + ':' +	$
				STRMID(NAMES,27,2) + ':' + STRMID(NAMES,30,2)
			FILETIME = UTC2TAI(STR2UTC(FILETIME))
		    END ELSE BEGIN
			BREAK_FILE, FILES, DISKS, DIRS, NAMES
			FILETIME = STRMID(NAMES, 7,2) + '-' + $
				   STRMID(NAMES, 9,2) + '-' + $
				   STRMID(NAMES,11,2) + ' ' + $
				   STRMID(NAMES,14,2) + ':' + $
				   STRMID(NAMES,16,2) + ':' + $
				   STRMID(NAMES,18,2)
			FILETIME = UTC2TAI(STR2UTC(FILETIME))
		    ENDELSE
;
;  Find the file with the closest time to that of the last packet which was
;  read.
;
		    DIFF = ABS(FILETIME-PACKETTIME)
FIND_CLOSEST:
		    W = WHERE(DIFF EQ MIN(DIFF))
		    W = W(0)
		    FILENAME = FILES(W)
;
;  If the filename hasn't changed, then try the next closest time.
;
		    IF FILENAME EQ FILE_STATUS.NAME THEN BEGIN
			IF N_ELEMENTS(DIFF) EQ 1 THEN GOTO, ABORT
			REMOVE, W, DIFF
			GOTO, FIND_CLOSEST
		    ENDIF
;
;  Compare this time against that of the last packet.  Reject the file if more
;  than 30 minutes different.
;
		    IF DIFF(W) GT 1800 THEN BEGIN
			MESSAGE = 'Unable to find next file'
			GOTO, ABORT
		    ENDIF
		ENDELSE
	END ELSE BEGIN
		CDS_MESSAGE, 'Reached end of requested data files', /CONTINUE
		GOTO, ABORT
	ENDELSE
;
;  Open the next file in the series, whether realtime or replay.  If one is in
;  an AUTOSTOP condition, and the telemetry mode changes, then unset RUNNING.
;  If one is processing CDROM data, then don't abort until one has reached the
;  end of the day.
;
OPEN_FILE:
	IF NOT OPEN_TM_FILE(UNIT, FILENAME, TEL_MODE) THEN BEGIN
	    IF FINALDIST THEN IF HOUR NE '00' THEN GOTO, TRY_NEXT_NAME
	    GOTO, ABORT
	ENDIF
	IF KEYWORD_SET(AUTOSTOP) AND (OLD_MODE NE TEL_MODE) THEN RUNNING = 0
;
;  If FITSGEN is running, then update the filename displayed in the widget.
;
	IF KEYWORD_SET(NUM_DATA_FILES) THEN BEGIN
		BREAK_FILE, FILENAME, DISK, DIR, RFILE, REXT
		WIDGET_CONTROL, REP_WIDG, SET_VALUE=RFILE+REXT
	ENDIF
;
;  Signal that the file has been opened and return 1 for success.
;
	CDS_MESSAGE, /CONTINUE, 'Opened file ' + FILENAME
	RETURN, 1
;
;  If this point is reached, and we're reprocessing stored telemetry, then we
;  might try again with a different telemetry mode.  This is only tried twice.
;
ABORT:
	IF KEYWORD_SET(RUN_STATE) AND N_TRIES LT 2 THEN BEGIN
		N_TRIES = N_TRIES + 1
		IF N_ELEMENTS(DATE) EQ 0 THEN DATE = OBT2TAI(LPACKET.HDR.TIME)
		IF TEL_MODE EQ 0 THEN BEGIN
			TEL_MODE = 2
			PATH = '$CDS_QKLSCI_DATA'
			GOTO, TRY_NEXT
		END ELSE IF TEL_MODE EQ 2 THEN BEGIN
			TEL_MODE = 3
			PATH = '$CDS_RELSCI_DATA'
			GOTO, TRY_NEXT
		END ELSE IF TEL_MODE EQ 3 THEN BEGIN
			TEL_MODE = 0
			PATH = '$CDS_TM_DATA'
			GOTO, TRY_NEXT
		ENDIF
	ENDIF
;
;  Otherwise, simply close the file and return 0.
;
	IF MESSAGE NE '' THEN CDS_MESSAGE, MESSAGE, /CONTINUE
	FREE_LUN, UNIT
	RETURN, 0
;
	END
