;+
; Project     : SOHO - CDS     
;                   
; Name        : NEXT_TM_FILE
;               
; Purpose     : Get the next telemetry file in sequence.
;               
; Explanation : This function returns the full pathname of the next telemetry
;		file in sequence after the telemetry file which is passed to
;		this fuction as input.  This fuction only works with telemetry
;		files generated by the Spacetek EGSE software.  If no 
;		telemetry file is available, then an empty string is returned.
;               
; Use         : filename = NEXT_TM_FILE (tmfile, [keyword])
;    
; Inputs      : tmfile:	Name of a telemetry file.  This string must be the
;			of a valid telemetry file or this function will not
;			work.
;               
; Opt. Inputs : None.
;               
; Outputs     : None.
;               
; Opt. Outputs: None.
;               
; Keywords    : ENDTIME If passed, then this will be set to the time
;			(in seconds) after which packets will be written
;			to the next telemetry file in sequence.  
;
; Returns     :	The complete path of the telemetry file that will follow
;		sequencially after the telemetry file that passed to the 
;		function as input.  If no such telemetry file is available,
;		then the empty string is returned.
;
; Calls       : SEC2UTC
;		UTC2SEC
;		STR2UTC
;		GET_TM_FILE
;
; Common      : None
;               
; Restrictions: Input must contain the name of a telemetry file, otherwise
;		the behavior of this function is undefined.
;               
; Side effects: None
;               
; Category    : Telemetry
;               
; Prev. Hist. : None
;
; Written     : Ron Yurow, SM&A, 12 May 1999
;               
; Modified    : Version 1, Initial Release		RY, 12 May 1999
;
; Version     : Version 1, 12 May 1999
;-            

	FUNCTION NEXT_TM_FILE, file, ENDTIME = endtime

;
; Find the current time.
;

	GET_UTC, utc

;
; Extract the file name (this may be a full path).
;

	tmfile = STRMID (file, STRPOS (file, 'tm.'), 12) 

;
; Convert the current time to seconds.
;

	now = UTC2SEC (utc) 

;
; Convert the tmfile to a time string.
;

	timestr = STRMID (tmfile, 3, 2) + '-' 
        timestr = timestr + STRMID (tmfile, 6, 3) + ' '
	timestr = timestr + STRMID (tmfile, 10, 2) + ':00'

;
; Convert the time string to seconds
;

	target = UTC2SEC (STR2UTC (timestr))

;
; Add 1 hour (in seconds) to target
;

	target = target + 3600

;
; Loop to check for the existence of a file at the target time.  If no file
; exists at this time, then the target will be moved foward by one hour 
; until either a file is found or the target time exceeds the current time.
;

	WHILE target LT now DO BEGIN

;
; Call GET_TM_FILE to check if a telemetry exist at the target time.  If it
; does, then this function will return its full pathname.   
;

	   path = GET_TM_FILE (SEC2UTC (target))

;
; Check if GET_TM_FILE was able to find a telemetry file.  If it was, then
; return it as the result of this fuction.  In addition, if the keyword
; endtime was passed, then set this to the estimated stop time of the file
; in seconds.  Were done.
;

	   IF path NE '' THEN BEGIN

	      IF N_ELEMENTS (endtime) NE 0 THEN endtime = target + 3600

	      RETURN, path

	   ENDIF

;
; Otherwise, add 1 hour to target and try again.
;
	   
	   target = target + 3600

	ENDWHILE

;
; If we got to here, then we have failed.  Just return an empty string in
; this case.
;

	RETURN, ''

	END
