pro mk_sdl, infil, outfil, week=week, run_time=run_time
;
;+
;NAME:
;	mk_sdl
;PURPOSE:
;	Generate the SXT Dark current log (SDL) file from the SFR file
;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.
;	outfil	- The output filename to create.  The output
;		  file can be derived if the "week" option is used
;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 15-Apr-92 by M.Morrison
;Ver 1.10 20-Apr-92 (MDM) - Changed from SDC to SDL
;			  - Changed to use structures for data
;Ver 1.11 26-May-92 (MDM) - Adjusted how weekid is derived
;			  - Changed to write a temporary file and then
;			    move it to on-line after finished generating it
;Ver 1.12 12-Jul-93 (MDM) - Build/save the roadmap
;	  18-Aug-93 (MDM) - Modified to use DIR_SXT_SDL directory
;	  19-Aug-93 (MDM) - Corrected typo in 18-Aug-93 fix
;-
;
start_time = systime(1)
run_time = 0.
;
progVerNo = 1.120*1000
progName = 'MK_SDL.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', 'sdl'+weekid)
    outfil_final = concat_dir('$DIR_SXT_SDL', 'sdl'+weekid)
end
;
if (infil(0) eq '') then return
;
if (n_elements(outfil) eq 0) then begin
    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', 'sdl'+weekid)
    outfil_final = concat_dir('$DIR_SXT_SDL', 'sdl'+weekid)
end
;
rd_roadmap, infil, roadmap
ss = keysea('ExpMode=Dark', roadmap)
ndatasets = n_elements(ss)
;
if (ss(0) eq -1) then begin
    message, 'No dark current images in the files listed', /info
    message, 'Returning...', /info
    return
end
;
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_darklog=sxt_darklog
;
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_SDL working on dataset #: ', idset+1, ' of ', ndatasets, '  ', fmt_tim(roadmap(ss(idset)))

    rd_xda, infil, ss(idset), index, data, roadmap
    siz = size(data)
    nx = siz(1)
    ny = siz(2)

    hist = histogram(data)
    nover = fltarr(256)
    for i=0,255 do nover(i) = total(hist(i:255))	;nover(i) is the number of pixels that are GREATER THAN OR EQUAL to "i"
    sss_cutoff = where(nover le 128)			;find where the cut-off is to get 128 different pixels
    cutoff = sss_cutoff(0)
    if (cutoff eq -1) then cutoff = 255			;lots of saturated pixels ==> take first 128 with value 255
    ;
    cutoff_m1 = cutoff -1
    while (hist(cutoff_m1) eq 0) do cutoff_m1 = cutoff_m1 - 1
    ;
    n = 0
    sss_nonzero = where(hist ne 0, count)	;find where the values are non-zero
    i = count-1					;start with the last non-zero value
    repeat begin
	if (n eq 0) then sss = where(data eq cutoff_m1) $
			else sss = where(data ge cutoff)
	;;sss = where(data eq sss_nonzero(i))	;not the slickest way to do it, but it works
	if (sss(0) ne -1) then begin
	    x0 = fix(sss mod nx)
	    y0 = fix(sss/nx)
	    int0 = data(sss)
	    if (n eq 0) then begin
		x = x0
		y = y0
		int = int0
	    end else begin
		x = [x, x0]
		y = [y, y0]
		int = [int, int0]
	    end
	    n = n_elements(x)
	end
	;;i = i-1
    end until (n ge 128)

    sxt_darklog.hist = hist
    sxt_darklog.x = x(0:127)
    sxt_darklog.y = y(0:127)
    sxt_darklog.int = int(0:127)

    index.gen.ndatabyte = get_nbytes(sxt_darklog)
    rdwrt, 'W', lunout, bytout, rsiz, index, 1
    rdwrt, 'W', lunout, bytout, rsiz, sxt_darklog, 1
end
;
maxsamps = get_nbytes(sxt_darklog)
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.sdl
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, 'SDL', '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_SDL took', run_time, ' minutes to run'
;
end
