pro mk_sl, infil, outfil, week=week, run_time=run_time, $
	prefix=prefix, y0=y0, y1=y1
;
;+
;NAME:
;	mk_sl
;PURPOSE:
;	Generate the SXT Leak Dark image (SLD) or SXT Leak scatter (SLS)
;	log from the SFR files.  It also generates the SXT X-ray Average
;	(SXA) files from the SFD files.
;INPUT:
;	infil	- A string array with a list of the SFR file
;		  names.  It should include the directory
;		  path.  The inputfiles can be derived if the
;		  "week" option is used.
;REQUIRED KEYWORD INPUT:
;	prefix	- The prefix of the output file.  Either SLS, SLD, or SXA
;OPTIONAL INPUT:
;	outfil	- The output filename to create.  The output
;		  file can be derived from the input file list.
;OPTIONAL INPUT:
;	week	- a string with the year/week if the format
;		  91_49 where 91 is the year, 49 is the week
;HISTORY:
;	Written 8-Dec-92 by M.Morrison
;	Ver 1.01 (8-Dec-92) MDM - Exclude full resolution images
;	Ver 1.02 (15-Jan-93) MDM - Added check to make sure it is past 11-oct-92
;	Ver 1.03 ( 9-Mar-93) MDM - Changed code to use lower case for the
;				   filename prefix
;	Ver 1.04 (12-Jul-93) MDM - Build/Save the roadmap
;	Ver 1.05 (11-Oct-93) MDM - Added option of creating SXA files.
;-
;
start_time = systime(1)
;
progVerNo = 1.04*1000
progName = 'MK_SL.PRO'
;
weekid = ''
if (keyword_set(week)) then begin
    indir_arr = [finddir(week+'a'), finddir(week+'b')]
    infil = file_list(indir_arr, 'sfr*')
    if (infil(0) eq '') then begin
	message, 'No files found - stopping', /info
	stop
    end
    ;
    weekid = string(week, fix(progverno/1000), format="(a, 'a.', i2.2)")
    outfil       = concat_dir('$DIR_REFORMAT_SC', strlowcase(prefix)+weekid)
    outfil_final = concat_dir('$DIR_SXT_'+strupcase(prefix), strlowcase(prefix)+weekid)
end
;
if (n_elements(outfil) eq 0) then begin
    if (infil(0) eq '') then return
    rd_roadmap, infil, roadmap
    tarr = anytim2ex(roadmap(0))
    week = ex2week(tarr)
    weekid = string(tarr(6), week, fix(progverno/1000), format="(i2.2, '_', i2.2, 'a.', i2.2)")
    outfil       = concat_dir('$DIR_REFORMAT_SC', strlowcase(prefix)+weekid)
    outfil_final = concat_dir('$DIR_SXT_'+strupcase(prefix), strlowcase(prefix)+weekid)
end
;
if (infil(0) eq '') then return
rd_roadmap, infil, roadmap
tim2orbit, roadmap, tim2night=tim2night
tarr = int2secarr(roadmap, '11-Oct-92')
;
case strupcase(prefix) of
   'SLD': begin
		;dark image; open/non-open; DPE=13; 100% data present
		ss = where((gt_expmode(roadmap) eq 1) and (gt_filta(roadmap) eq 1) and (gt_filtb(roadmap) ne 1) and $
			(gt_comp(roadmap) eq 0) and (roadmap.percentd eq 255) and $
			(tim2night gt 2.0) and (gt_dpe(roadmap) eq 13) and (gt_res(roadmap) ge 1) and (tarr gt 0))
		if (n_elements(y0) eq 0) then y0 = 20		;whole image
		if (n_elements(y1) eq 0) then y1 = 1000
	  end
   'SLS': begin
		;dark image; narrowband/open; DPE=2; 100% data present
		ss = where((gt_expmode(roadmap) eq 1) and (gt_filta(roadmap) eq 2) and (gt_filtb(roadmap) eq 1) and $
			(gt_comp(roadmap) eq 0) and (roadmap.percentd eq 255) and $
			(tim2night gt 2.0) and (gt_dpe(roadmap) eq 2) and (gt_res(roadmap) ge 1) and (tarr gt 0))
		if (n_elements(y0) eq 0) then y0 = 500		;top half of the image
		if (n_elements(y1) eq 0) then y1 = 1000
	  end
    'SXA': begin
		ss = indgen(n_elements(roadmap))		;do all images
		if (n_elements(y0) eq 0) then y0 = 20		;whole image
		if (n_elements(y1) eq 0) then y1 = 1000
	  end
    else: return
end

ndatasets = n_elements(ss)
;
if (ss(0) eq -1) then begin
    message, 'No data found for ' + strupcase(prefix), /info
    message, 'Returning...', /info
    return
end
dset_str = mk_dset_str(infil, ss)
;
rd_fheader, infil(0), fheader_in
;
;----- Diagnostic variables
;
qprint = 1
qdebug = 1
;
;---------------------------------------- General Stuff
;
ref_struct, ref
lunout = 2	;output log
gen_struct, file_header=fhead
sxt_struct, sxt_version=sxt_version, sxt_leak=outrec
;
rsiz = ref.SXT_Fil_Rec_Siz
open_da_file, lunout, '', '', outfil, rsiz, bytout, fpoint, /nolower

qfirst_time = 1
first_time      = roadmap(ss(0)).time
first_day       = roadmap(ss(0)).day
last_time       = roadmap(ss(ndatasets-1)).time
last_day        = roadmap(ss(ndatasets-1)).day
;
for idset=0,ndatasets-1 do begin
    if (qdebug) then print, 'MK_SL working on dataset #: ', idset+1, ' of ', ndatasets, '  ', fmt_tim(roadmap(ss(idset)))

    rd_xda, infil, dset_str(idset), index, data, roadmap
    if (strupcase(prefix) eq 'SXA') then data = sfd_decomp(data) else data = sxt_decomp(data)

    siz = size(data)
    nx = siz(1)
    ny = siz(2)

    split_files, infil(dset_str(idset).ifil), dir0, filnam0

    outrec.st$fileid	= byte(strmid(filnam0, 3, 11))
    outrec.dset		= dset_str(idset).dset

    outrec.y0		= y0
    outrec.y1		= y1

    sum = 2^gt_res(index)	;1,2,4
    for i=0,50 do begin
	x0 = i*20/sum + 1	;start at column 1
	x1 = x0 + 20/sum - 1	;sum 20 FR, 10 HR, and 5 QR columns
	temp = data(x0:x1, y0/sum:y1/sum)
	outrec.avg_arr(i)	= total(temp)/n_elements(temp)
    end

    index.gen.ndatabyte = get_nbytes(outrec)    
    rdwrt, 'W', lunout, bytout, rsiz, index, 1
    rdwrt, 'W', lunout, bytout, rsiz, outrec, 1
end
;
maxsamps = get_nbytes(outrec)
totbytes = bytout

;-------------------- Write out pointer and file header
;

fpoint.TotBytes		= totbytes

fhead.first_Time	= first_time
fhead.first_Day		= first_day
fhead.last_Time		= last_time
fhead.last_Day		= last_day

fhead.nDataSets		= nDataSets
fhead.maxSamps		= maxSamps

fpoint.data_version	= sxt_version.sl
fhead.refverno		= fheader_in.refverno
fhead.ref2verno		= fheader_in.ref2verno

file_id = weekid
wrtSXTMap, lunout, fpoint, bytout, fhead, rsiz
wrt1p_fh, lunout, fpoint, fhead, rsiz, progName, progVerNo, file_id, strupcase(prefix), 'SXT'
;
close, lunout
;
if (n_elements(outfil_final) ne 0) then begin
    cmd = 'rm -f ' + outfil_final               ;remove any copy of the file that might already exist
    spawn, cmd
    cmd = 'mv ' + outfil + ' ' + outfil_final
    spawn, cmd
    outfil = outfil_final
end
;
end_time = systime(1)
run_time = (end_time-start_time)/60.
print, 'MK_SL took', run_time, ' minutes to run'
;
end
