	PRO LIST_DURATION, START_TIME, END_TIME, OUTPUT=OUTPUT, PRINT=PRINT, $
		QUEUE=QUEUE, WEEKLY=WEEKLY
;+
; Project     :	SOHO - CDS
;
; Name        :	LIST_DURATION
;
; Purpose     :	Find the actual durations of rasters from the catalog
;
; Category    :	Class3, Operations
;
; Explanation :	The CDS catalog is searched for the stop and start
;		times, and the duration is calculated in seconds.  This is
;		compared against the durations as listed in the database.
;
; Syntax      :	LIST_DURATION  [, START_TIME, END_TIME ]
;
; Examples    :	LIST_DURATION, /PRINT
;
; Inputs      :	None required.
;
; Opt. Inputs :	START_TIME = Start time to look in the catalog.  If not passed,
;			     then the last 36 hours are searched.
;		END_TIME   = End time to go along with the start time.
;
; Outputs     :	Information about the raster duration is printed to the screen.
;
; Opt. Outputs:	None.
;
; Keywords    :	WEEKLY	 = If set, then the studies run over the last week
;			   (actually 8 days) are searched.
;
;		OUTPUT	 = Name of a file to write the output to, instead of
;			   the screen.
;
;		PRINT	 = If set, then the information is sent to the printer.
;			   If OUTPUT is not specified, then a temporary file is
;			   used.
;
;		QUEUE	 = The name of the print queue to use, when /PRINT is
;			   selected.  If not passed, then a menu is presented
;			   to the user.
;
; Calls       :	LIST_EXPER, XPRINT, SEND_PRINT, DELETE_FILE
;
; Common      :	None.
;
; Restrictions:	Unix only.
;
; Side effects:	None.
;
; Prev. Hist. :	None.
;
; History     :	Version 1, 09-Jul-1996, William Thompson, GSFC
;		Version 2, 11-Jul-1996, William Thompson, GSFC
;			Added keyword WEEKLY
;			Modified format of output.
;		Version 3, 17-Oct-1997, William Thompson, GSFC
;			Call FIND_FILE_DUR
;
; Contact     :	WTHOMPSON
;-
;
	ON_ERROR, 2
;
;  Check the number of input parameters.
;
	IF (N_PARAMS() NE 0) AND (N_PARAMS() NE 2) THEN MESSAGE,	$
		'Syntax:  LIST_DURATION  [, START_TIME, END_TIME ]'
;
;  If not passed, then get the default start and end times.
;
	IF N_PARAMS() EQ 0 THEN BEGIN
	    GET_UTC, END_TIME
	    START_TIME = END_TIME
	    IF KEYWORD_SET(WEEKLY) THEN BEGIN
		START_TIME.MJD = START_TIME.MJD - 8
	    END ELSE BEGIN
		START_TIME.MJD = START_TIME.MJD - 1
		START_TIME.TIME = START_TIME.TIME - 12L*3600L*1000L
		CHECK_INT_TIME, START_TIME
	    ENDELSE
	ENDIF
;
;  List all the entries in the catalog for that time period.  If none were
;  found, then don't continue.
;
	LIST_MAIN, START_TIME, END_TIME, MAIN, N_FOUND
	IF N_FOUND GT 0 THEN BEGIN
;
;  Initialize the arrays.
;
	    OBS_PROG = 'String'
	    RAS_ID   = -1
	    RAS_VAR  = -1
	    DURATION = -1
;
;  Step through each observation, and collect the information about it.
;
	    FOR J = 0,N_FOUND-1 DO BEGIN
		LIST_EXPER, MAIN(J).PROG_NUM, OBS, N_OBS
		FOR I = 0,N_OBS-1 DO BEGIN
		    OBS_PROG = [OBS_PROG, OBS(I).OBS_PROG]
		    RAS_ID   = [RAS_ID,   OBS(I).RAS_ID]
		    RAS_VAR  = [RAS_VAR,  OBS(I).RAS_VAR]
		    DUR = ROUND(FIND_FILE_DUR(TRIM(OBS(I).FILENAME)))
		    DURATION = [DURATION, DUR]
		ENDFOR
	    ENDFOR
;
;  Remove the dummy first value from each of the arrays.
;
	    OBS_PROG = OBS_PROG(1:*)
	    RAS_ID   = RAS_ID(1:*)
	    RAS_VAR  = RAS_VAR(1:*)
	    DURATION = DURATION(1:*)
;
;  Sort the data by raster ID and raster variation number.
;
	    S = SORT(RAS_ID*(MAX(RAS_VAR)+1) + RAS_VAR)
	    OBS_PROG = OBS_PROG(S)
	    RAS_ID   = RAS_ID(S)
	    RAS_VAR  = RAS_VAR(S)
	    DURATION = DURATION(S)
;
;  Open the proper output file or terminal.
;
	    IF N_ELEMENTS(OUTPUT) EQ 1 THEN BEGIN
		DELETE = 0
		OUTFILE = OUTPUT
		OPENW, OUT, OUTPUT, /GET_LUN
	    END ELSE IF KEYWORD_SET(PRINT) THEN BEGIN
		DELETE = 1
		OUTFILE = '~/TEMPORARY.txt'
		OPENW, OUT, OUTFILE, /GET_LUN
	    END ELSE BEGIN
		OPENW, OUT, FILEPATH(/TERMINAL), /MORE, /GET_LUN
	    ENDELSE
;
; Print out some header lines.  
;
            PRINTF, OUT, "SOHO CDS: Science Study Durations."
            PRINTF, OUT, "          Data compiled from actual flight studies."
	    GET_UTC, UTC, /VMS
            PRINTF, OUT, "Compiled: " + STRMID (UTC, 0, 17) + " UT"
;
;  Step through the collected information, and print the information out for
;  each raster.
;
	    I = 0
	    WHILE I LT N_ELEMENTS(RAS_ID) DO BEGIN
		W = WHERE((RAS_ID(I) EQ RAS_ID) AND (RAS_VAR(I) EQ RAS_VAR))
		ERRMSG = ''
		GET_RASTER, RAS_ID(I), RAS_VAR(I), RASTER, ERRMSG=ERRMSG
		IF ERRMSG EQ '' THEN EXPECTED = ABS(RASTER.DURATION) ELSE $
			EXPECTED = -1
		PRINTF, OUT, ''
	        PRINTF, OUT, OBS_PROG(I), "Raster ID: ", RAS_ID(I),	$
			"Variation: ", RAS_VAR(I),			$
			FORMAT = '(A,T10,A,I4,2X,A,I4)'
;
;  Calculate the statistical information, and print it out.
;
		DURS = DURATION(W)
		PRINTF, OUT, ''
		PRINTF, OUT, "Expected Duration:            ", EXPECTED, $
			FORMAT = '(10X, A, I0)'
		PRINTF, OUT, ''
		PRINTF, OUT, "Number of Studies in Sample:  ",	$
			N_ELEMENTS(DURS), FORMAT = '(10X, A, I0)'
		PRINTF, OUT, "Median Sample Duration:       ",	$
			MEDIAN(DURS), FORMAT = '(10X, A, I0)'
		PRINTF, OUT, "Standard Deviation of Sample: ",	$
			TRIM(SIGMA(DURS)), FORMAT = '(10X, A, A)'
		PRINTF, OUT, "Minimum Study Duration:       ",	$
			MIN(DURS, MAX=DMAX), FORMAT = '(10X, A, I0)'
		PRINTF, OUT, "Maximum Study Duration:       ", DMAX, $
			FORMAT = '(10X, A, I0)'
		PRINTF, OUT, ''
		I = MAX(W) + 1
	    ENDWHILE
;
;  Close all files, and free all the unit numbers.
;
	    FREE_LUN, OUT
;
;  If /PRINT was selected, then send it to the printer.  If a temporary file,
;  then delete it.
;
	    IF KEYWORD_SET(PRINT) THEN BEGIN
		IF N_ELEMENTS(QUEUE) NE 1 THEN XPRINT, OUTFILE ELSE	$
			SEND_PRINT, OUTFILE, QUEUE=QUEUE
		IF KEYWORD_SET(DELETE) THEN	$
			TEST = DELETE_FILE(OUTFILE,/NOCONFIRM)
	    ENDIF
	ENDIF
;
	RETURN
	END
