;+
;ROUTINE:
;	TEST_RD
;PURPOSE:
;	Read an any of the reformatted data files
;HISTORY:
;	MDM	Early-1991
;       JRL     Updated 18-sep-91 to create info_array
;       MDM     Updated 25-Sep-91 to assemble observing regions
;	MDM     Updated  1-Oct-91 to only read BCS DP_Sync if asked to
;	MDM Ver 3.3 4-Oct  - Allowed SEQ_NUM selection for FFI (as well as PFI)
;	MDM Ver 3.4 10-Oct - Changed default answer for "Assemble OR" question
;			     to yes
;			   - Also, removed GET_INFO asking if the data should
;			     be written to disk
;			   - Also added RD_QS for BCS files
;	MDM Ver 3.5 11-Oct - Returned GET_INFO to printing out information
;	MDM Ver 3.6 11-Oct - Changed case statement that calls reading routines
;			     to only look at first letter of the file name
;			     (instead of the whole prefix)
;	MDM Ver 3.7 17-Oct - For SXT, when printing out the summary of what
;			     Sequence lines are used, print out the parameters
;			     for the FIRST (only first) image for that sequence
;			   - Also added option -888 to get out without reading
;			     any file.
;	MDM Ver 3.8 19-Oct - Put in "stop" when "ndset"=0
;			   - Also, if file # entered is -1, stop
;	MDM Ver 3.9 20-Oct - Modified how option -888 works (first read roadmap,
;			     and have option to read DP section). 
;	MDM Ver 4.0 21-Oct - Added the menu option for the input file.
;       CDP/JRL Ver 4.1 29-Oct - Set stdset_out=0 even when not used by
;                               not used by test_rd (used by other routines).
;	MDM Ver 4.2  7-Nov-91 - Expanded the SXT seq_num information being printed.
;	MDM Ver 4.3 10-Nov-91 - New method of determining what type of file it is
;				is to check the "fheader.st$instrument) field.
;				Old method was to look at the first letter of the
;				file name.
;	MDM Ver 4.4 13-Nov-91 - Used new calling sequence for "seq_summary"
;				Added option to select sequence by menu
;	MDM Ver 4.5 20-Nov-91 - Added capability to select multiple sequences
;				using the sequence menu option
;	MDM Ver 4.6 21-Nov-91 - Added "SAME" option to select a different instrument
;				with the same fileid
;	MDM Ver 4.7 22-Nov-91 - Fixed bug with using SXT -777 option (if asked for
;				invalid option, it never recovered)
;	MDM Ver 4.8  3-Dec-91 - Changed the "normal" option of entering # of data
;				sets and starting dataset to check that it does no
;				run past the last dataset in the file.
;				Also deleted the "significant update" message
;				and removed the compiliation of all "_struct" routines
;-
;-------------------------------------------------------------------------------
print,'*******  TEST_RD  V4.8 ( 3-Dec-91)  *******',format='(15x,a)'
print, ' '
;print, '********************************************************************'
;print, 'Significant update for reading SXT files.  If you wish to return to'
;print, 'the old version, please run "test_rd0"
;print, '********************************************************************'
;print, ' '

;
repeat begin
    infil0 = ' '
    if (n_elements(infil) ne 0) then print, 'Default file name is: ', infil
    print, 'Enter MENU if you want to use the menu option'
    print, 'Enter SAME if you want to access the same fileID for a different instrument'
    read, 'Enter file name (or wild cards) ', infil0
    if (strupcase(strmid(infil0,0,4)) eq 'SAME') then begin
	temp = str2arr(infil0, delim = ' ')
	if (n_elements(temp) eq 1) then begin
	    prefix = ' '
	    read, 'Enter file name prefix ', prefix
	end else begin
	    prefix = temp(1)
	end
	break_file, infil, bdsk_log, bdir, bfilnam, bext
	infil0 = bdsk_log + bdir + prefix + strmid(bfilnam, 3, 6) + bext
    end
    if (strupcase(strmid(infil0,0,4)) eq 'MENU') then begin
	dirs = data_paths()
	temp = str2arr(infil0, delim = ' ')
	if (n_elements(temp) eq 2) then infil0 = file_menu(dirs, temp(1)) $
					else infil0 = file_menu(dirs, /interactive)
	if (infil0 eq '') then stop	;user changed his mind?
    end
    if (infil0 eq '') then begin
	if (n_elements(infil) ne 0) then infil0=infil else infil0 = 'SPR*.*'
    end
    if ((infil0 eq '*') or (infil0 eq '')) then infil0 = 'SPR*.*'
    p1 = strpos(infil0,'*')
    p2 = strpos( strmid(infil0,p1,100),'.')
    if (p2 eq -1) then infil0 = infil0+'.*'
    ff = findfile(infil0)
    if (ff(0) eq '') then begin
	print, '** No files found **'
	qfound = 0
    end else begin
	qfound = 1
	n = n_elements(ff)
	if (n gt 1) then begin
	    for i=0,n-1 do print, '#', string(i,format='(i3)'), ' - ', ff(i)
	    read, 'Enter file number (-1 to exit) ', ifil
	    if (ifil eq -1) then stop
	    infil = ff(ifil)
	end else begin
	    infil = ff(0)
	end
    end
end until (qfound)
;
break_file, infil, dsk_log, dir, filnam, ext
;
rd_pointer, infil, pointer, recsiz
rd_fheader, infil, fheader, ndset
print, 'There are ', ndset, ' data sets in this file (', infil, ')'
instr = strupcase(fheader.st$instrument)
;
if (ndset eq 0) then begin
    print, 'TEST_RD: No Data in this file.  Stopping...', string(7b)
    stop
end
;
rd_roadmap, infil, roadmap
;
;----- Special selection options
case instr of
    'SXT': begin
	    seq_sum = seq_summary(roadmap, seq_param, /qprint)
	    ;
            if (btest0(roadmap(0).pfi_ffi, 0)) then entry = mask(roadmap.obsregion, 6, 2) $ ;ffi
                                else entry = mask(roadmap.obsregion, 4, 2)      ;pfi
            table = roadmap.seq_tab_serno
	    seq_num = roadmap.seq_num
	  end
    else:
endcase
;
print, 'Enter the number of data sets to extract'
print, '   * If you enter 0, all datasets will be extracted'
print, '   * If you enter -99, then it uses the datasets specified'
print, '     in the variable "SS"'
print, '   * If you enter -888, then the file is not read'
print, '   * For SXT, enter a negative # (from -1 to -13) 
print, '     to access only that seq#'
print, '   * For SXT, enter -777 for seqence menu option
;
stdset_out = 0                  ; Always gives in the file
read, ndset_out
if (ndset_out eq -888) then goto, skip		;sorry, it was an afterthought
;
if (ndset_out gt 0) then begin
    read, 'Enter the starting data set to extract (first=0) ', stdset_out
    if (stdset_out+ndset_out gt ndset) then begin			;patch made 3-Dec-91
	print, 'You selected more datasets than there are available'
	print, 'Stopping at the end of the file'
	ndset_out = ndset - stdset_out
    end
    dset_arr = indgen(ndset_out) + stdset_out
end 
if (ndset_out eq 0) then begin
    dset_arr = indgen(ndset)
end
if (ndset_out eq -99) then begin
    dset_arr = ss
end
;---- SXT specific

if ((ndset_out lt 0) and (ndset_out ge -13)) then begin
    repeat begin
	iseq_num = abs(fix(ndset_out))
	dset_arr = where(seq_num eq iseq_num)

	if (dset_arr(0) ne -1) then begin
	    entry = entry(dset_arr)
	    if (min(entry) ne max(entry)) then begin
		print, 'Available Entry Tables: ', entry(uniq(entry, sort(entry)))
		read, 'Enter the entry table to use (-1 for all tables) ', ientry
		if (ientry ge 0) then begin
	            abcd_temp = where(entry eq ientry)
                    if (abcd_temp(0) ne -1) then dset_arr = dset_arr(abcd_temp) $
                                        else dset_arr = -1
		end
	    end
	end
	if (dset_arr(0) ne -1) then begin
	    table = table(dset_arr)
	    if (min(table) ne max(table)) then begin
		print, 'Available Sequence Tables: ', table(uniq(table, sort(table)))
		read, 'Enter sequence table number (-1 for all tables)', itable
		if (itable ge 0) then begin
		    abcd_temp = where(table eq itable)
		    if (abcd_temp(0) ne -1) then dset_arr = dset_arr(abcd_temp) $
					else dset_arr = -1
		end
	    end
	end
	if (dset_arr(0) eq -1) then begin
	    print, 'No datasets with SEQ_NUM =', abs(ndset_out)
	    read, 'Enter a new seq# (positive value)', ndset_out
	end
    end until (dset_arr(0) ne -1)
end
if (ndset_out eq -777) then begin
    qfirst = 1
    qmulti_sel = 0
    seq_menu = [seq_sum, '* Enable option to select multiple sequences (reset)', $
			'* If enbled selecting mutiple options, now extract data']
    nseq_sum = n_elements(seq_sum)
    repeat begin
	print, '* Select title to quit
	imenu = wmenu(seq_menu)
	if (imenu eq 0) then stop
	if (imenu lt nseq_sum) then begin
	    dset_arr0 = where((table eq seq_param(imenu).table) and $
				(entry eq seq_param(imenu).entry) and (seq_num eq seq_param(imenu).seq))
	    if (qfirst) then begin
		dset_arr = dset_arr0
		qfirst = 0
	    end else begin
		if (dset_arr0(0) ne -1) then dset_arr = [dset_arr, dset_arr0]
	    end
	end
	if (imenu eq nseq_sum) then begin
	    qfirst = 1
	    qmulti_sel = 1
	end
	qdone = 0
	if (qmulti_sel and (imenu eq nseq_sum+1)) then qdone = 1
	if (not qmulti_sel) then qdone = 1
	if (qdone) then if (dset_arr(0) eq -1) then begin	;check to see if any valid data selected
	    print, 'Selected a non-valid option - please try again'
	    qdone = 0
	    qfirst = 1
	end
    end until (qdone)

    sss = uniq(dset_arr, sort(dset_arr))	;sort and find unique entries
    dset_arr = dset_arr(sss)
end
;
case instr of
    'BCS': rd_bda, infil, dset_arr, index, data, roadmap
    'HXT': rd_hda, infil, dset_arr, index, data, roadmap
    'SXT': rd_sda, infil, dset_arr, index, data, roadmap
    'WBS': rd_wda, infil, dset_arr, index, data, roadmap

    'CBA': rd_cba, infil, dset_arr, index, data, roadmap
    'ATT': rd_ada, infil, dset_arr, index, data, roadmap, hxa_scan
endcase

;---- Special handling
;
if (strupcase(fheader.st$file_type) eq 'SPR') then begin
    if (max(index.sxt.shape_cmd(1)) gt 64) then begin
	print, 'You have selected an observing region with multiple PFI N-S'
	yesnox, 'Do you wish to have it "assembled"', in, 'Y'
	if (in eq 1) then begin
	    yesnox, 'Do you want to avoid images that are missing "PFI strips"', only_full, 'Y'

	    print, 'Assembling data with "ASS_OR" now
	    data = ass_or(data, index, subs, only_full=only_full)
	    index = index(subs)
	    dset_arr = dset_arr(subs)
	end
    end
end
;
skip:
case instr of
    'SXT': begin
		if (ndset_out ne -888) then begin	;avoid doing this if exiting the read option
                    info_array = get_info2(roadmap(dset_arr))
                    print,'Information is contained in INFO_ARRAY'
		end
         end
    'BCS': begin
		rd_qs, infil, qs
		yesnox, 'Do you want to read the DP_Sync section', in
		if (in eq 1) then rd_bda_dp, infil, dp_sync
	 end
    else:
endcase
;
end
