pro mk_sfw, infil, outdir, prefix, interactive=interactive, run_time=run_time
;
;+
;NAME:
;       mk_sfw
;PURPOSE:
;       Given a set of files, read the roadmaps and find where there
;       are complete narrow band images.  Write the subset to a datafile.
;INPUT:
;       infil   - an array of input file names
;       outdir  - the output directory where the file should be written
;OPTIONAL KEYWORD INPUT:
;       interactive- If set, display the results to the screen as the
;                  processing occurs.
;HISTORY:
;       draft version: 28 January 1992 (KTS)
;       20-Mar-92 (MDM) - Took Keith Strong's program "db_wlext.pro"
;                         and made the procedure "mk_desat_wl"
;	14-Apr-92 (KTS) - Removed prefix option and set to 'sfw'
;			- Changed so as to output week id (91_51a.01)
;	16-Apr-92 (MDM) - Changed file name definition to use the 
;			  "progverno" variable to determine the extension
;	20-Apr-92 (MDM) - Added RUN_TIME parameter
;Ver 2.1 26-Apr-92 (MDM) - Renamed from "mk_desat_wl" to "mk_sfw"
;			- Added check for compressed data.
;Ver 2.11 30-Apr-92 (MDM) - Fixed bug where couldn't tell if should
;			    open a new file if first image is skipped
;Ver 2.12 30-Jul-92 (MDM) - Check to make sure image was taken at least
;			    2 minutes before predicted night fall.
;Ver 3.00 17-Aug-92 (MDM) - Changed to match SFD extension *.03
;Ver 3.01 28-Aug-92 (MDM) - Simply return if no matches
;			    Pass PROVERNO and PROGNAME to SAV_SDA
;Ver 3.02 31-Aug-92 (MDM) - Fixed typo in SAV_SDA command - wasn't saving
;			    the images
;Ver 3.03 11-Nov-92 (MDM) - Updated to use GT_ADOOR to check if the
;			    aspect door is open.
;Ver 3.04 25-Nov-92 (MDM) - Updated to not select files after 13-Nov-92
;-

if (n_elements(prefix) eq 0) then prefix='sfw'

progverno=3.04					;program ver. #
progname = 'MK_SFW.PRO'
start_time = systime(1)
run_time = 0.

if (infil(0) eq '') then return			;no SFR files

rd_roadmap, infil, roadmap, filidx=filidx
tim2orbit, roadmap, tim2night=tim2night
tarr = int2secarr(roadmap, '13-NOV-92  17:06')          ;added 25-Nov-92

ss=where(roadmap.percentd eq 255 and (gt_filta(roadmap) eq 2 or $
		 gt_filta(roadmap) eq 5) and roadmap.imgparam ne 5 and  $
		 gt_res(roadmap) ge 1 and gt_expmode(roadmap) eq 0 and $
		 gt_comp(roadmap) eq 0 and $
		 gt_adoor(roadmap) eq 1 and $
		 tim2night gt 2 and $
		 tarr lt 0)

if (ss(0) eq -1) then return	;no matches
;
numb = n_elements(ss)
print,numb

ds=mk_dset_str(filidx,ss)

iflag = 0

for i=0,numb-1 do begin
	
	rd_sda,infil(ds(i).ifil),ds(i).dset,index1,data1	

	print,i,' ',fmt_tim(index1)

	ss=where(data1 gt 100)
	if gt_res(index1) eq 2 then begin
	if (n_elements(ss) lt 10000) then goto, lab1
	endif else begin
	if (n_elements(ss) lt 50000) then goto, lab1
	endelse

	if (keyword_set(interactive)) then begin
	    tvscl,data1
	    xyouts,10,10,gt_day(index1,/string)+' '+gt_time(index1,/string),/device
	end

	if (iflag eq 0) then begin
            anum=strlen(infil(0))                           ;length of file id
            fileid = getobsid(startt=strmid(infil(0),anum-11,6)) ;create Week ID
            fileid=fileid + '.' + string(fix(progverno), format='(i2.2)')
            filename = concat_dir(outdir, prefix+fileid)
	end

	sav_sda,filename,index1,data1, append=iflag, progname=progname, progverno=progverno*1000
	iflag=1                                     ;reset flag

	lab1:	dumy=0
endfor
	
end_time = systime(1)
run_time = (end_time-start_time)/60.
end