	PRO FIND_DURATION,                 $
	    FILENAME,                      $
            OUTPUT=OUTPUT,                 $
            PRINT=PRINT,                   $
            QUEUE=QUEUE,                   $
            STAT = STATFILE
;+
; Project     :	SOHO - CDS
;
; Name        :	FIND_DURATION
;
; Purpose     :	Find the actual durations of rasters from the FITS headers
;
; Category    :	Class3, Operations
;
; Explanation :	The CDS FITS file headers are searched for the stop and start
;		times, and the duration is calculated in seconds.
; Syntax      :	
;
; Examples    :	FIND_DURATION, 's2735r*.fits', /PRINT
;
; Inputs      :	FILENAME = Name of the FITS file to process.  Wildcards are
;		allowed.
;
; Opt. Inputs :	None.
;
; Outputs     :	Information about the raster duration is printed to the screen.
;
; Opt. Outputs:	None.
;
; Keywords    :	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       :	FIND_FILES, FXHREAD, FXPAR, XPRINT, SEND_PRINT, DELETE_FILE
;
; Common      :	None.
;
; Restrictions:	Unix only.
;
; Side effects:	None.
;
; Prev. Hist. :	None.
;
; History     :	Version 1, 17-May-1996, William Thompson, GSFC
;		Version 2, 24-Jan-1996, William Thompson, GSFC
;			Corrected bug when only one raster type was processed.
;			Corrected terminology (raster vs. study)
;
; Contact     :	WTHOMPSON
;-
;
	ON_ERROR, 2
;
;  Check the number of input parameters.
;
	IF N_PARAMS() NE 1 THEN MESSAGE, 'Syntax:  FIND_DURATION, FILENAME'

;
;  Make an array to hold each type data.
;

	OBS_PROG_ARRAY = STRARR (1)
	NAME_ARRAY = STRARR (1)
	RAS_ID_ARRAY = INTARR (1)
	RAS_VAR_ARRAY = INTARR (1)
	DUR_ARRAY = INTARR (1)
	INDEX_ARRAY = STRARR (1)

;
;  Find all the FITS files based on the filename.  If none were found, then
;  don't continue.
;
	FILES = FIND_FILES(FILENAME, '$CDS_FITS_DATA')
	IF FILES(0) NE '' THEN BEGIN
;
;  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), /GET_LUN
		OPENW, OUT, FILEPATH (/TERMINAL), /MORE, /GET_LUN
	    ENDELSE

;
;  Check if the name of seperate file to hold the statistics on raster 
;  durations was passed to the program.  If it was then open a file
;  to hold them.  Otherwise, the statistics will just show up at the 
;  bottom of the normal output.
;

	    IF N_ELEMENTS (STATFILE) EQ 1 THEN BEGIN
	       OPENW, STATOUT, STATFILE, /GET_LUN
	    ENDIF ELSE BEGIN
               STATOUT = OUT
            ENDELSE

;
;  Set NO_VALUES to 0.  NO_VALUES will keep track of the number of data
;  points we have collected.
;

   	    NO_VALUES = 0 

;
; Get the current UTC time.
;

	    GET_UTC, UTC, /VMS

;
; Print out a couple of header lines.  
;

            HD1 = "SOHO CDS: Science Raster Durations."
            HD2 = "          Data compiled from actual flight studies."
            HD3 = STRING ("Compiled: ", STRMID (UTC, 0, 17), " UT")

            PRINTF, STATOUT

            PRINTF, STATOUT, HD1
	    PRINTF, STATOUT, HD2
	    PRINTF, STATOUT, HD3

	    PRINTF, STATOUT


;
;  Print out the column labels.
;  

	    FORMAT = "(2X,'Filename',2X,'Study',4X,'Raster',4X,'Duration')"
;	    PRINTF, OUT, FORMAT=FORMAT
;	    PRINTF, OUT, ''
;
;  Set Up the Index Format
;

            INDEX_FMT = '(A,".", I3.3, ".", I3.3)'

;
;  Step through each file, and read in the FITS header.
;
	    GET_LUN, UNIT
	    FOR I = 0,N_ELEMENTS(FILES)-1 DO BEGIN
		OPENR, UNIT, FILES(I)
		FXHREAD, UNIT, HEADER
		CLOSE, UNIT
;
;  Extract the information from the header.
;
		OBS_PROG = FXPAR(HEADER,'OBS_PROG')
		RAS_ID   = FXPAR(HEADER,'RAS_ID')
		RAS_VAR  = FXPAR(HEADER,'RAS_VAR')
		OBT_TIME = FXPAR(HEADER,'OBT_TIME')
		OBT_END  = FXPAR(HEADER,'OBT_END')
		DUR = ROUND(OBT_END - OBT_TIME)

;
;  Create the index string
;

                INDEX = STRING (FORMAT = INDEX_FMT, OBS_PROG, RAS_ID, RAS_VAR)
;
;  Print it out.
		BREAK_FILE, FILES(I), DISK, DIR, NAME
;		PRINTF, OUT, NAME, OBS_PROG, RAS_ID, RAS_VAR, DUR,	$
;			FORMAT="(A10,2X,A8,2X,I2,'/',I2,2X,I10)"

;
;  Shove all of our data into arrays
;

		OBS_PROG_ARRAY = [OBS_PROG_ARRAY, OBS_PROG]
		RAS_ID_ARRAY = [RAS_ID_ARRAY, RAS_ID]
		RAS_VAR_ARRAY = [RAS_VAR_ARRAY, RAS_VAR]
		NAME_ARRAY = [NAME_ARRAY, NAME]
		DUR_ARRAY = [DUR_ARRAY, DUR]
		INDEX_ARRAY = [INDEX_ARRAY, INDEX]

;
;  Increment the count of the number data points processed.
;

                NO_VALUES = NO_VALUES + 1

	    ENDFOR
;
;  Print out a space, just in case we don't have a seperate statistics
;  file.
;

;            PRINTF, OUT
;            PRINTF, OUT

;
; Get rid of element 0 (dummy element) from all of our arrays.
;

	    OBS_PROG_ARRAY = OBS_PROG_ARRAY (1:*)
	    RAS_ID_ARRAY = RAS_ID_ARRAY (1:*)
	    RAS_VAR_ARRAY = RAS_VAR_ARRAY (1:*)
	    NAME_ARRAY = NAME_ARRAY (1:*)
	    DUR_ARRAY = DUR_ARRAY (1:*)
            INDEX_ARRAY = INDEX_ARRAY (1:*)

;
; Sort the INDEX_ARRAY.  This will in effect sort all of our data.
;

	    ORDER = SORT (INDEX_ARRAY)

;
; Get a list of unigue rasters and variations.
;

            RASTER_LIST = UNIQ (INDEX_ARRAY, ORDER)

;
; Find out how many unique rasters we have.
;

	    NO_RASTER = N_ELEMENTS (RASTER_LIST)

;
; Display the mean and standard deviation of the durations for each 
; unique combination of study and raster variation.
;

	    FOR I = 0, NO_RASTER - 1 DO BEGIN

;
; Get the index of all the rasters which match the search criterion.
;
		RASTER_SET = WHERE(INDEX_ARRAY EQ INDEX_ARRAY(RASTER_LIST(I)))

;
; Set NO_DATA to the number of rasters in our current group of rasters.
;

	        NO_DATA = N_ELEMENTS(RASTER_SET)

;
; DBAR to the median raster duration of all the rasters in the 
; current group.
; 

                DBAR = MEDIAN (DUR_ARRAY (RASTER_SET))

;
; Set DSIGMA to the standard deviation of the durations of all the
; rasters in the current group.
;

	        DSIGMA = SIGMA (DUR_ARRAY (RASTER_SET))
;
; Set OBS_PROG_NAME to the name of the observation program.  This is taken
; from the array OBS_PROG_ARRAY
;

                OBS_PROG_NAME = OBS_PROG_ARRAY (RASTER_LIST (I))

;
; Set DMIN to the minimum raster duration of all the rasters in the
; current group.  Set DMAX to the maximum raster duration of all the 
; rasters in the current group.
;

		DMIN = MIN (DUR_ARRAY (RASTER_SET), MAX = DMAX)

;
; Check if this is an engineering study.  If it is, then set its name
; to ENG (it previously would be blank)
;

	        IF RAS_ID_ARRAY (RASTER_LIST (I)) EQ -1 THEN BEGIN
	           OBS_PROG_NAME = "ENG     "
	        ENDIF

;
; Print out the study name, variation, and raster ID.
;

                PRINTF, STATOUT,                                  $
                        OBS_PROG_NAME,                            $
                        "Raster ID: ",                            $
                        RAS_ID_ARRAY (RASTER_LIST (I)),            $
                        "Variation: ",                            $
                        RAS_VAR_ARRAY (RASTER_LIST (I)),           $
                        FORMAT = '(A, "  ", A, I4, "  ", A, I4)'
;
; Print out a blank line
; 

	        PRINTF, STATOUT

;
; Print out the number of rasters that were sampled.
;

                PRINTF, STATOUT,                                  $ 
                        "Number of Rasters in Sample:  ",         $
                        NO_DATA,                                  $
                        FORMAT = '(10X, A, I0)'

;
; Print out the median raster duration.
;

                PRINTF, STATOUT,                                  $ 
                        "Median Sample Duration:       ",         $
                        DBAR,                                     $
                        FORMAT = '(10X, A, I0)'

;
; Print out the stardard deviation of the sample of rasters.
;

                PRINTF, STATOUT,                                  $ 
                        "Standard Deviation of Sample: ",         $
                        DSIGMA,                                   $
                        FORMAT = '(10X, A, F0.0)'

;
; Print out the minimum raster duration.
;

                PRINTF, STATOUT,                                  $ 
                        "Minimum Raster Duration:       ",         $
                        DMIN,                                     $
                        FORMAT = '(10X, A, I0)'

;
; Print out the maximum raster duration.
;

                PRINTF, STATOUT,                                  $ 
                        "Maximum Raster Duration:       ",         $
                        DMAX,                                     $
                        FORMAT = '(10X, A, I0)'
;
; Print out two blank lines as seperators.
;

                PRINTF, STATOUT
                PRINTF, STATOUT


            ENDFOR
                 
;
;  Close all files, and free all the unit numbers.
;

            IF STATOUT NE OUT THEN FREE_LUN, STATOUT

	    FREE_LUN, UNIT
	    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
