	FUNCTION FIND_FILE_DUR, FILENAME
;+
; Project     :	SOHO - CDS
;
; Name        :	FIND_FILE_DUR()
;
; Purpose     :	Find the duration of a single CDS FITS file.
;
; Category    :	Class3, Operations
;
; Explanation :	Reads in the DEL_TIME values from a FITS file, and determines
;		extrapolates the last two valid points to the end of the file.
;		Called from LIST_DURATION
;
; Syntax      :	Result = FIND_FILE_DUR( FILENAME )
;
; Examples    :	
;
; Inputs      :	FILENAME = Name of FITS file.
;
; Opt. Inputs :	None.
;
; Outputs     :	The result of the function is the calculated duration.
;
; Opt. Outputs:	None.
;
; Keywords    :	None.
;
; Calls       :	DATATYPE, FIND_WITH_DEF, FXBOPEN, FXBREAD, FXBCLOSE
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Prev. Hist. :	None.
;
; History     :	Version 1, 17-Oct-1997, William Thompson, GSFC
;		Version 2, 05-Aug-2002, William Thompson, GSFC
;			Modified to work with compressed files
;
; Contact     :	WTHOMPSON
;-
;
;
	ON_ERROR, 2
;
;  Check the input parameters.
;
	IF N_PARAMS() EQ 0 THEN MESSAGE,	$
		'Syntax:  Result = FIND_FILE_DUR( FILENAME )'
	IF DATATYPE(FILENAME, 1) NE 'String' THEN MESSAGE,	$
		'FILENAME must be a string'
	IF N_ELEMENTS(FILENAME) NE 1 THEN MESSAGE,	$
		'FILENAME must be a scalar'
;
;  Find the requested FITS file.
;
	EXT = '.fits,.fits.Z,.fits.gz,.Z,.gz'
	FILE = FIND_WITH_DEF(FILENAME, '$CDS_FITS_DATA', EXT)
	IF FILE EQ '' THEN MESSAGE, 'File ' + FILENAME + ' not found'
;
;  If the file is compressed, then uncompress the file before reading it.
;
	LEN = STRLEN(FILE)
	WRITE_DIR = GETENV("CDS_FITS_TEMP_W")
	IF WRITE_DIR EQ '' THEN WRITE_DIR = GETENV("CDS_FITS_DATA_W")
	BREAK_FILE, FILE, DISK, DIR, NAME, EXT
	NAME = CONCAT_DIR(WRITE_DIR, NAME+'.fits')
	IF STRMID(FILE,LEN-2,2) EQ '.Z' THEN BEGIN
	    SPAWN, 'umask 002 ; uncompress -c ' + FILE + ' > ' + NAME
	    FILE = NAME
	    DELETE_AFTER_READ = 1
	END ELSE IF STRMID(FILE,LEN-2,2) EQ '.Z' THEN BEGIN
	    SPAWN, 'umask 002 ; gunzip -c ' + FILE + ' > ' + NAME
	    FILE = NAME
	    DELETE_AFTER_READ = 1
	END ELSE DELETE_AFTER_READ = 0
;
;  Open the FITS file, and read in the delta time values.  Determine the total
;  number of exposures.
;
	FXBOPEN, UNIT, FILE, 1, HEADER
	FXBREAD, UNIT, DEL_TIME, 'DEL_TIME', NANVALUE=-1
	FXBCLOSE, UNIT
	IF KEYWORD_SET(DELETE_AFTER_READ) THEN SPAWN, 'rm '+FILE
	N_EXPOSURES = N_ELEMENTS(DEL_TIME)
;
;  Filter out any missing data.  If there aren't at least two values, then
;  return the duration based on the start and end times.
;  
	W = WHERE(DEL_TIME NE -1, COUNT)
	IF COUNT LE 1 THEN RETURN,	$
		FXPAR(HEADER, 'OBT_END') - FXPAR(HEADER, 'OBT_TIME')
	DEL_TIME = DEL_TIME(W)
;
;  Sort the time values into increasing order.  Take the difference between the
;  last two values.
;
	IF DEL_TIME(1) LT DEL_TIME(0) THEN BEGIN
		DEL_TIME = REVERSE(DEL_TIME)
		W = N_EXPOSURES - REVERSE(W) - 1
	ENDIF
	I1 = COUNT-2
	I2 = COUNT-1
	DIFF = (DEL_TIME(I2) - DEL_TIME(I1)) / (W(I2) - W(I1))
	DIFF = DIFF > FXPAR(HEADER, 'EXPTIME')
;
;  Extrapolate the last DEL_TIME value to the end of the last exposure.  This
;  is the total duration.
;
	RETURN, DEL_TIME(I2) + DIFF * (N_EXPOSURES-W(I2))
	END
