; dummy driver for demonstration
; calls observing log->reformatted data programs in sequence - only sxtf/sxtp
;
;7-Dec-91 (MDM) - Added some comments to the printed statement at the end of the program (RD_SDA2 stuff)
;		- Added key = strcompress(key, /remove_all) command
;
;if n_elements(obslog) eq 0 then obslog = '/0d0/ops/obs/observing.log'
;input, 'Enter observing log file to use', obslog,  '/0d0/ops/obs/obs_sept.log'
;obslog = ['/0d0/ops/obs/obs_sept.log', '/0d0/ops/obs/observing.log']
;obslog = ['/0p/gen/obs/obs_sept.log', '/0p/gen/obs/observing.log']
;obslog = ['/0p/gen/obs/obs_sept.log', '/0p/gen/obs/obs_oct.log', '/0p/gen/obs/obs_nov.log']
;obslog = ['$DIR_GEN_OBS/obs_sept.log', '$DIR_GEN_OBS/obs_oct.log', '$DIR_GEN_OBS/obs_nov.log']
obslog = findfile('$DIR_GEN_OBS/obs9*')
;
qdebug = 0
;
qsea = 1
if ((n_elements(sxtf) ne 0) or (n_elements(sxtp) ne 0)) then begin
    print, 'It appears that the observing log has already been searched'
    yesnox, 'Do you wish to search the observing log again (other option is to use "sxtp,sxtf" as is)', qsea, 'N'
end
;--- Allows a user to manually reduce the selection in the "sxtf" or "sxtp" and then re-run the last two steps
if (qsea) then begin
    fid = 0b
    dsxtf = 0b      ;to allow saving
    dsxtp = 0b
    flimits = 0b
    plimits = 0b
    ffiles = 0b
    pfiles = 0b
    ;
    if (n_elements(st) eq 0) then st = '1-Sep-91'
    if (n_elements(sp) eq 0) then sp = '1-Dec-91'
    input, 'Enter start time', st, st
    input, 'Enter end time  ', sp, sp
    time=utime(timstr2ex(st),timstr2ex(sp))
    ;print,time
    ;
    print, 'Sample search keys:'
    print, '        opt=naban;percentd=255;expmode=norm;mbe=5;Shape_Cmd(=512,=512)'
    print, '        xray=al14;percentd=255;expmode=norm;shape_cmd(=256,=256);res=1x1
    print, '        percentd>200;expmode=norm;shape_cmd(>255,>255)
    print, '        xray=AL;Shape_Cmd(=256,=256);MBE=12;percentd>200
    if (n_elements(key) eq 0) then key = 'opt=naban;percentd=255;mbe=5;Shape_Cmd(=512,=512)'
    input, 'Enter key', key, key
    key = strcompress(key, /remove_all)
    ;
    print, 'starting getobsdat'
    getobsdat, obslog, key=key, usertimes=time, which='sxtf,sxtp', fid=fid, sxtf=sxtf,sxtp=sxtp
    print,'getobsdat complete'
    ;
    ;--- Temporary work around since the obs-log being used is generated from the TEMPORARY reformatted data files
    ;    and there are overlaps in the data, and therefore duplicate entries in the obs-log
    ex2int, timstr2ex(st), st_msod, st_ds79
    ex2int, timstr2ex(sp), sp_msod, sp_ds79
    xend = int2secarr([sp_msod,sp_ds79], [st_msod,st_ds79])
    xend = xend(0)
    if (get_nbytes(sxtp) gt 1) then begin
	sxtp0 = sxtp
	x = int2secarr(sxtp, [st_msod,st_ds79])
	ss = uniq(x, sort(-1*x))	;reverse the order so that when repeats occur, the second file is selected
	ss = ss( rotate(indgen(n_elements(ss)),2) )	;reverse the order again 
	sxtp = sxtp(ss)
	;---- Temporary because the time range given does not work well within "getobsdat" right now
	x = int2secarr(sxtp, [st_msod,st_ds79])
	ss = where((x gt 0) and (x lt xend))
	if (ss(0) eq -1) then sxtp=0b else sxtp = sxtp(ss)
    end
    if (get_nbytes(sxtf) gt 1) then begin
	sxtf0 = sxtf
	x = int2secarr(sxtf, [st_msod,st_ds79])
	ss = uniq(x, sort(-1*x))	;reverse the order so that when repeats occur, the second file is selected
	ss = ss( rotate(indgen(n_elements(ss)),2) )	;reverse the order again 
        sxtf = sxtf(ss)
        ;---- Temporary because the time range given does not work well within "getobsdat" right now
	x = int2secarr(sxtf, [st_msod,st_ds79])
	ss = where((x gt 0) and (x lt xend))
        if (ss(0) eq -1) then sxtf=0b else sxtf = sxtf(ss)
    end
end
;
if ((get_nbytes(sxtf) le 1) and (get_nbytes(sxtp) le 1)) then begin
    print, 'Search did not find any matches'
    stop
end
if (qdebug) then begin & print, 'enter .cont to continue' & stop & end
;
fidmap, key=key, times=time,map=map, sxtp=sxtp, sxtf=sxtf, fid=fid
print,'fidmap complete
if (qdebug) then begin & print, 'enter .cont to continue' & stop & end
;
print,'starting credvec'
credvec, fidmap=map, tsxtf=sxtf, tsxtp=sxtp, dsxtf=dsxtf, dsxtp=dsxtp, fnmap=fnmap
print,'credvec complete'
;
print, 'Starting get_fninfo
if (get_nbytes(sxtf) gt 1) then ffiles = get_fninfo(fnmap, flimits, /sxtf)
if (get_nbytes(sxtp) gt 1) then pfiles = get_fninfo(fnmap, plimits, /sxtp)
;
sav_file = ' '
read, 'Enter name of file to save search results ', sav_file
;
savegen, sxtp, sxtf, fid, $
	ffiles, pfiles, dsxtf, dsxtp, flimits, plimits, $
	fnmap, map, key, time, $
	file = sav_file, $
	text = 'TEST_SEA search results'
;
print, 'Now use RD_SDA2 with something like one of the following commands'
print, 'to read the raw data files - this assumes you want the FFI results'
print, 'if you want to access PFI, use "pfiles", "plimits", "dsxtp"
print, '* To get all images in the first file:
print, '      RD_SDA2, ffiles, flimits, dsxtf, index, data, one_file=0
print, '* To only get the 11th image
print, '      RD_SDA2, ffiles, flimits, dsxtf, index, data, one_img=10
print, '* To get all selected images (careful, it can get large and is slow)
print, '      RD_SDA2, ffiles, flimits, dsxtf, index, data
print, ' '
print, 'You can use the PROGRAM "restore_gen" to restore these results
print, 'after you have saved them to disk.
;
end
