pro mk_sdw, infil, outfil, week=week, run_time=run_time
;
;+
;NAME:
;	mk_sd2
;PURPOSE:
;	Generate the SXT Dark Current SDA Warm image (SDW) file from the
;	SFR files of all 100% data images that are take with the CCD 
;	warm.
;OPTONAL INPUT:
;	infil	- A list of the SFR file names that were used to make
;		  the SDL file.  If this parameter is not passed, the
;		  program will use FINDDIR to get the directory for that
;		  week of data and then use FINDFILE.
;KEYWORD INPUT:
;	week	- a string with the year/week if the format
;		  91_49 where 91 is the year, 49 is the week
;HISTORY:
;	Written 26-Apr-92 by M.Morrison
;Ver 1.11 26-May-92 (MDM) - Changed to write a temporary file and then
;                           move it to on-line after finished generating it
;	   8-Jul-92 (MDM) - Added "outfil"
;	  18-Aug-93 (MDM) - Removed reference to DIR_SXT_DC
;Ver 1.20  4-Aug-94 (MDM) - Modified to exclude dark images which have staturated
;                           pixels
;-
;
start_time = systime(1)
run_time = 0.
;
progVerNo = 1.200*1000
progName = 'MK_SDW.PRO'
;
if (n_elements(week) eq 0) then begin
    if (infil(0) eq '') then return		;no SFR files found
    rd_roadmap, infil, roadmap
    tarr = anytim2ex(roadmap(0))
    week = ex2week(tarr)
    week = string(tarr(6), week, format="(i2.2, '_', i2.2)")
end
;
weekid = string(week, fix(progverno/1000), format="(a, 'a.', i2.2)")
sdl_infil    = concat_dir('$DIR_SXT_SDL', 'sdl'+weekid)
outfil       = concat_dir('$DIR_REFORMAT_SC', 'sdw'+weekid)
outfil_final = concat_dir('$DIR_SXT_SDW', 'sdw'+weekid)
rd_sdl, xx, yy, sdl_index, sdl_data, infil=sdl_infil
if (n_elements(sdl_data) eq 0) then begin
    message, 'No SDL file (so assume there are no dark current images', /info
    return
end
;
if (n_elements(infil) eq 0) then begin
    indir_arr = [finddir(week+'a'), finddir(week+'b')]
    infil = file_list(indir_arr, 'sfr*')
end
;
rd_roadmap, infil, roadmap
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_darklog=sxt_darklog
;
rsiz = ref.SXT_Fil_Rec_Siz
open_da_file, lunout, '', '', outfil, rsiz, bytout, fpoint, /nolower

maxsamps = 0
;
rbm = sdl_index.gen.rbm_status
act_expdur = gt_expdur(sdl_index)		;msec
exp_expdur = gt_dpe(sdl_index, /conv)		;msec
exp_expdur = fix(exp_expdur/8.0+1)*8.0		;there is a 8 millisec granularity in exposure duration because of Line Sync
err_expdur = (act_expdur-exp_expdur)/exp_expdur
get_sdl_info, sdl_index, sdl_data, act_avg, act_dev, exp_avg=exp_avg
err_avg    = (act_avg-exp_avg)/exp_avg
temp = gt_temp_ccd(sdl_index)
err_temp = abs(temp-(-21))
;
v = gt_res(sdl_index) + sdl_index.sxt.explevmode*256 + sdl_index.sxt.temp_ccd*256L*256L
vuniq = v( uniq(v, sort(v)) )

for iuniq=0,n_elements(vuniq)-1 do begin
    ss0 = where(v eq vuniq(iuniq))
    tit = gt_res(sdl_index(ss0(0)), /str) + gt_dpe(sdl_index(ss0(0)), /conv, space=4) + ' msec exposure ' + $
                  gt_temp_ccd(sdl_index(ss0(0)), /str) + $
		 ' C (' + strtrim(n_elements(ss0),2) + ' images) '
 

    good = bytarr(n_elements(sdl_index))
    ;sel = where((sdl_index.sxt.percentd eq 255) and (v eq vuniq(iuniq)) and (abs(err_expdur) lt 0.05) and (temp gt -20))
    sel = where((sdl_index.sxt.percentd eq 255) and (v eq vuniq(iuniq)) and $
		(abs(err_expdur) lt 0.05) and (temp gt -20) and (sdl_index.sxt.percentover le 50))
    ;there can be some saturation in the long exposure images since they are warm

    if (sel(0) ne -1) then good(sel) = 1
    ss = where(good and (rbm eq 0), count)
    if (count eq 0) then begin
	if (qdebug) then print, 'No good images when looking at RBM flag - now looking at RBM images'
	ss = where(good, count)
    end

    if (count eq 0) then begin
	tit2 = 'No good exposures for this selection'
	;stop
    end else begin
	tit2 = strtrim(n_elements(ss),2) + ' good exposures for this selection'

	ss2 = tim2dset(roadmap, sdl_index(ss(0)))
	rd_xda, infil, ss2(0), index, data, roadmap	;read the first image only

	rdwrt, 'W', lunout, bytout, rsiz, index, 1
	rdwrt, 'W', lunout, bytout, rsiz, data, 1
	maxsamps = max([maxsamps, get_nbytes(data)])
    end
    if (qdebug) then print, tit, tit2
end

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

if (maxsamps gt 0) then begin
    wrtSXTMap, lunout, fpoint, bytout, fhead, rsiz
    file_id = weekid
    wrt1p_fh, lunout, fpoint, fhead, rsiz, progName, progVerNo, file_id, 'SDW', 'SXT'
    close, lunout
end else begin
    message, 'No good dark images found', /info
    close, lunout
    spawn, 'rm -f ' + outfil
end
;
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_SDW took', run_time, ' minutes to run'
;
end
