pro mk_sft, infil, outdir, run_time=run_time, append=append, week=week
;
;+
;NAME:
;       mk_sft
;PURPOSE:
;       Given a set of FFI files, read the roadmaps and save the 
;	terminator images (SFT images)
;INPUT:
;       infil   - an array of input SFR file names
;       outdir  - the output directory where the file should be written
;		  It defaults to $DIR_SXT_SFT
;OPTIONAL KEYWORD INPUT:
;	append	- If set, append to an existing file if it exists.
;	week	- If set, find the appropriate directory and make the
;		  SFR file listing from it.
;HISTORY:
;       Written 19-Nov-93 by M.Morrison
;               11-Dec-94 (SLF) - add/update history records with sun_center
;                7-Feb-95 (SLF) - update selection criteria based upon
;                                 LWA/JRL reccomendations
;
;
;V1.01 19-Nov-93 (MDM) - Added SAA check
;-
if (n_elements(outdir) eq 0) then outdir = getenv('DIR_SXT_SFT')
;
progverno=1.01
start_time = systime(1)
run_time = 0.
prefix = 'sft'
;
if (keyword_set(week)) then begin
    indir = finddir(week+'a')
    infil = file_list(indir, 'sfr*')
end

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

rd_roadmap, infil, roadmap, filidx=filidx
tim2orbit, roadmap, tim2night=tim2night, saa=saa

aspect = mask(roadmap.periph, 7, 1)	;aspect door
asp_closed=(aspect eq 1)
asp_open  = 1-asp_closed

; slf - Filter selection different before/after entrance filter failure
timess=sel_timrange(roadmap,get_yo_dates(/launch), $
                             (get_yo_dates(/entrance))(1),/bet)

ok_filt = ( (gt_filta(roadmap) eq 1) or (gt_filta(roadmap) eq 6) ) and $
          asp_closed

; take everything between launch and entrance filter failure
if n_elements(timess) eq n_elements(roadmap) then $
   ok_filt = lonarr(n_elements(roadmap)) + 1   

; half or quarter resolution only
ok_res  = (gt_res(roadmap) eq 1)  or (gt_res(roadmap) eq 2)

; slf 8-feb-95 - update selection criteria based upon LWA research
;    between 5-30 seconds before sunset
;    resolution: half and quarter
;    No SAA
;    Normal Exposures
;    Filters (all before entrance filter)
;    DPE  le 27 

ss=where( (gt_comp(roadmap) eq 0) and (gt_expmode(roadmap) eq 0) and  $
        (gt_dpe(roadmap) le 27) and $
        (ok_filt) and (ok_res) and (roadmap.percentd eq 255) and $
        (tim2night*60 le 30) and (tim2night*60 ge 5) and (not saa) )	

if (ss(0) eq -1) then return	;no matches
;
tim2orbit, roadmap(ss(0)), wid=wid
wid = wid + '.' + string(fix(progverno), format='(i2.2)')
outfil = concat_dir(outdir, prefix+wid)
qexist = file_exist(outfil)
;
n = n_elements(ss)
ds=mk_dset_str(filidx,ss)
;
if (keyword_set(append) and qexist) then begin
    append = 1
    spawn, 'cp ' + outfil + ' ' + concat_dir(outdir, 'dum.temp')	;sloppy way to make this person running the file owner
    file_delete, outfil
    spawn, 'mv ' + concat_dir(outdir, 'dum.temp') + ' ' + outfil
end else begin
    append = 0
    if (qexist) then file_delete, outfil		;remove the file if it exists
end

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

    print,i,' ',fmt_tim(index1)

    sav_sda, outfil, index1, data1, append=append
    append = 1
end

; update history records with sun_center
his_suncen,outfil
	
end_time = systime(1)
run_time = (end_time-start_time)/60.
end

