pro mk_ssl, indir, outfil, week=week, run_time=run_time
;
;+
;NAME:
;	mk_ssl
;PURPOSE:
;	Generate the SXT Summary Log (SSL) file from the SPR and
;	SFR files.  Also need the output from MK_SEL.
;OPTONAL INPUT:
;	indir	- The input directories where the SFR and SPR files
;		  reside.  If not defined, "week" must be passed
;		  and the program will find the files in the week
;		  directory.
;	outfil	- The output file name.  If undefined, it derives
;		  a week ID filename from the first data file.
;KEYWORD INPUT:
;	week	- a string with the year/week if the format
;		  91_49 where 91 is the year, 49 is the week
;		  This is required input.
;HISTORY:
;	Written 23-Apr-92 by M.Morrison
;Ver 1.01 28-Apr-92 (MDM) - Changed logging of expdur to be in
;			microseconds instead of millisec.  Also, GT_EXPDUR
;			had been modified to return the corrected expdur.
;Ver 1.02  1-May-92 (MDM) - Fixed bug where # images were 1 and a sort was
;			being attempted on it
;Ver 1.03 26-May-92 (MDM) - Changed to write a temporary file and then
;                           move it to on-line after finished generating it
;Ver 1.04  9-Jul-92 (MDM) - Modified to not access the TEMP files from the
;			    reformating
;Ver 1.05 16-Oct-92 (MDM) - Modified SERNUM extraction.  Last serial num of
;			    selected files was sometimes bad, so when it is
;			    less than the starting serial num, then find the
;			    maximum instead.
;-
;
start_time = systime(1)
run_time = 0.
;
progVerNo = 1.050*1000
progName = 'MK_SSL.PRO'
;
if (n_elements(week) eq 0) then begin
    ff = file_list(indir, 'sfr*')
    if (ff(0) eq '') then return	;no SFR files
    rd_roadmap, ff(0), roadmap
    tarr = anytim2ex(roadmap(0))
    week = ex2week(tarr)
    week = string(tarr(6), week, format="(i2.2, '_', i2.2)")
end
if (n_elements(indir) eq 0) then begin
    indir = [finddir(week+'a'), finddir(week+'b')]
    if (indir(1) eq '') then indir = indir(0)		;no b directory
end
;
weekid = string(week, fix(progverno/1000), format="(a, 'a.', i2.2)")
;
if (n_elements(outfil) eq 0) then begin
    outfil       = concat_dir('$DIR_REFORMAT_SC', 'ssl'+weekid)
    outfil_final = concat_dir('$DIR_SXT_SSL', 'ssl'+weekid)
end
;
infil = file_list(indir, ['sfr*','spr*'], file=file)
if (infil(0) eq '') then return			;no files found
tmp = strmid(file, 3, 6)
part_fid = tmp( uniq(tmp,sort(tmp)) )
;
rd_fheader, infil(0), fheader_in
gen_struct, file_header=fhead
fhead.refverno = fheader_in.refverno
fhead.ref2verno = fheader_in.ref2verno
;
;
;----- Diagnostic variables
;
qprint = 1
qdebug = 1
;
;---------------------------------------- General Stuff
;
ref_struct, ref
lunout = 2	;output log
sxt_struct, sxt_version=sxt_version, sxt_sumlog=sumlog
;
rsiz = ref.SXT_Fil_Rec_Siz
open_da_file, lunout, '', '', outfil, rsiz, bytout, fpoint, /nolower

maxsamps = 0
ndatasets = 0
for ipart=0,n_elements(part_fid)-1 do begin
    if (qdebug) then print, part_fid(ipart)

    infil = file_list(indir, 's*'+part_fid(ipart)+'*')
    rd_fheader, infil, fh, ndset_arr
    if (total(ndset_arr) eq 0) then goto, skip			;sorry, afterthought
    ;
    rd_xda, infil, -1, index, /nodata
    ;
    outrec = sumlog
    outrec.time		= index(0).gen.time		;make this something from FEM
    outrec.day		= index(0).gen.day

    for imbe=0,31 do begin
	ss = where(gt_mbe(index) eq imbe, count)
	if (count gt 0) then begin
	    expdur = gt_expdur(index(ss), /shutdur)*1000	;convert to microsec
	    outrec.n_expdur(imbe)	= count
	    outrec.avg_expdur(imbe)	= total(expdur)/count
	    outrec.min_expdur(imbe)	= min(expdur)
	    outrec.max_expdur(imbe)	= max(expdur)
	end
    end

    ss = where(gt_expmode(index) eq 0, count)	;normal exposures
    if (count gt 0) then begin
	explat = get_explat(index(ss))*1000			;convert to microsec	;TODO - rename GET_EXPLAT to GT_EXPLAT
	outrec.avg_explat_norm	= total(explat)/count
	outrec.min_explat_norm	= min(explat)
	outrec.max_explat_norm	= max(explat)
    end
    ss = where(gt_expmode(index) eq 1, count)	;dark exposures
    if (count gt 0) then begin
	explat = get_explat(index(ss))*1000			;TODO - see above
	outrec.avg_explat_dark	= total(explat)/count
	outrec.min_explat_dark	= min(explat)
	outrec.max_explat_dark	= max(explat)
    end

    pf = gt_pfi_ffi(index)
    dp_mode = gt_dp_mode(index)
    sernum = index.sxt.serial_num
    ;
    ss = where((pf eq 0) and (sernum ne 0), count)		;list of PFI images
    if (count gt 0) then begin
	outrec.pfi_sernum(0)	= index(ss(0)).sxt.serial_num
	outrec.pfi_sernum(1)	= index(ss(count-1)).sxt.serial_num
	if (outrec.pfi_sernum(1) lt outrec.pfi_sernum(0)) then outrec.pfi_sernum(1) = max(index(ss).sxt.serial_num)	;16-Oct-92
    end
    ss = where((pf eq 1) and (sernum ne 0), count)		;list of FFI images
    if (count gt 0) then begin
	outrec.ffi_sernum(0)	= index(ss(0)).sxt.serial_num
	outrec.ffi_sernum(1)	= index(ss(count-1)).sxt.serial_num
	if (outrec.ffi_sernum(1) lt outrec.ffi_sernum(0)) then outrec.ffi_sernum(1) = max(index(ss).sxt.serial_num)	;16-Oct-92
    end
    ss = where((pf eq 0) and (dp_mode eq 13), count)
    if (count gt 0) then begin
	outrec.pfi_qt(0) = count
	temp = index(ss).sxt.serial_num
	if (n_elements(temp) eq 1) then outrec.pfi_qt(1) = 1 $
				else outrec.pfi_qt(1) = n_elements(uniq(temp, sort(temp)))
    end
    ss = where((pf eq 0) and (dp_mode eq 9), count)
    if (count gt 0) then begin
	outrec.pfi_fl(0) = count
	temp = index(ss).sxt.serial_num
	if (n_elements(temp) eq 1) then outrec.pfi_fl(1) = 1 $
				else outrec.pfi_fl(1) = n_elements(uniq(temp, sort(temp)))
    end
    ss = where((pf eq 1), count)
    outrec.ffi_qt = count
    ss = where((pf eq 3), count)
    outrec.ffi_buff = count
    ss = where(mask(index.sxt.imgparam, 6, 2) ne 0, count)
    outrec.pfi_hi_cad = count

    ;TODO outrec.odom

    good = bytarr(n_elements(index))
    ss = where(gt_expmode(index) eq 0, count)	;normal exposures
    if (count ne 0) then good(ss) = 1
    mk_ssl_sub1, outrec, index, where(good and (gt_filtb(index) eq 2)), 0
    mk_ssl_sub1, outrec, index, where(good and (gt_filtb(index) eq 3)), 1
    mk_ssl_sub1, outrec, index, where(good and (gt_filtb(index) eq 4)), 2
    mk_ssl_sub1, outrec, index, where(good and (gt_filtb(index) eq 5)), 3
    mk_ssl_sub1, outrec, index, where(good and (gt_filtb(index) eq 6)), 4
    mk_ssl_sub1, outrec, index, where(good and (gt_filta(index) eq 2)), 5
    mk_ssl_sub1, outrec, index, where(good and (gt_filta(index) eq 3)), 6
    mk_ssl_sub1, outrec, index, where(good and (gt_filta(index) eq 4)), 7
    mk_ssl_sub1, outrec, index, where(good and (gt_filta(index) eq 5)), 8
    mk_ssl_sub1, outrec, index, where(good and (gt_filta(index) eq 6)), 9
    mk_ssl_sub1, outrec, index, where(good and (gt_filta(index) eq 1) and (gt_filtb(index) eq 1)), 10
    mk_ssl_sub1, outrec, index, where(gt_expmode(index) eq 1), 11	;dark images
    mk_ssl_sub1, outrec, index, where(gt_expmode(index) eq 2), 12	;cal images

    ;TODO - err_max
    ;TODO - u_hardreset
    ;TODO - j_hardreset
    ;TODO - j_softreset
    ;TODO - error1 and error2

    ss = where(index.sxt.temp_hk(0) ne 0, count)	;find where the image was taken with odd major frame number
    if (count ne 0) then begin
	for itemp=0,(n_elements(index(0).sxt.temp_hk)-1)<14 do begin
	    temp = index(ss).sxt.temp_hk(itemp)
	    outrec.avg_temp_hk(itemp)	= total(temp)/count
	    outrec.min_temp_hk(itemp)	= min(temp)
	    outrec.max_temp_hk(itemp)	= max(temp)
	end
    end
    temp = index.sxt.temp_ccd
    outrec.avg_temp_ccd	= total(temp)/n_elements(index)
    outrec.min_temp_ccd	= min(temp)
    outrec.max_temp_ccd	= max(temp)

    ;TODO aspect_encode
    ;TODO pow_stat
    ;TODO sw_stat

    infil = file_list(indir, 'wda*'+part_fid(ipart)+'*')
    if (infil(0) ne '') then begin
	rd_roadmap, infil, roadmap2
	n = n_elements(roadmap2)
	outrec.avg_rbmsd	= total(roadmap2.rbmsd)/n
	outrec.max_rbmsd	= max(roadmap2.rbmsd)
	outrec.avg_sxs1		= total(roadmap2.sxs1)/n
	outrec.max_sxs1		= max(roadmap2.sxs1)
	outrec.avg_sxs2		= total(roadmap2.sxs2)/n
	outrec.max_sxs2		= max(roadmap2.sxs2)
    end
    infil = file_list(indir, 'hda*'+part_fid(ipart)+'*')
    if (infil(0) ne '') then begin
	rd_roadmap, infil, roadmap2
	n = n_elements(roadmap2)
	outrec.avg_hxt_sum_l	= total(roadmap2.sum_l)/n
	outrec.max_hxt_sum_l	= max(roadmap2.sum_l)
    end

    rdwrt, 'W', lunout, bytout, rsiz, outrec, 1
    ndatasets = ndatasets + 1

    skip:
end

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

maxsamps = get_nbytes(outrec)
totbytes = bytout

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.ssl
fhead.refverno          = fheader_in.refverno
fhead.ref2verno         = fheader_in.ref2verno

file_id = weekid
wrt1p_fh, lunout, fpoint, fhead, rsiz, progName, progVerNo, file_id, 'SSL', '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_SSL took', run_time, ' minutes to run'
;
end
