pro mk_sdmi, infil, week=week, test=test, run_time=run_time
;+
;NAME:
;	mk_sdmi
;PURPOSE:
;	To make the SDM Input database "SDMI" which holds 
;	information on most of the dark current images
;	and is used to select which images are used for SDM
;SAMPLE CALLING SEQUENCE:
;	mk_sdmi,v,test=[92,50]
;HISTORY:
;	Written 27-Mar-95 by M.Morrison using CHECK_SDM and 
;					SDM_CHECK2BIN
;	28-Mar-95 (MDM) - Modified the logic somewhat
;			- Added TEST option
;	31-Mar-95 (MDM) - Modified to have keyword RUN_TIME
;	18-Oct-96 (GLS) - Changed '/ydb' ref to '$ydb'
;-

start_time = systime(1)
run_time = 0.0
outdir = '$ydb/sdmi'
;
if (keyword_set(test)) then begin
    tarr = week2ex(test(0), test(1))
    sttim = anytim2ints(tarr)
    entim = anytim2ints(sttim, off=7*86400.)
    rd_obs, sttim, entim, bcs, rmap, /sxtf
end else begin
    if (keyword_set(week)) then begin
        indir = finddir(week+'a')
        infil = file_list(indir, 'sfr*')
    end else begin
        break_file, infil(0), dsk_log, indir, filnam, ext
        fid = strmid(filnam, 3, 6) + ext
        tim2orbit, fid2ex(fid), wid=week
	week = week(0)
    end
end
;
if (not keyword_set(test)) then rd_roadmap, infil, rmap
tim2orbit, rmap, saa=saa
val = get_sdm_val(rmap)
ss = where((val ne -1) and (gt_percentd(rmap) eq 100) and (not saa), n)
if (ss(0) eq -1) then begin
    print, 'MK_SDMI: No valid input dark images'
    return
end
if (keyword_set(test)) then begin
    infil = rmap(ss)
    return
end
;
dset = mk_dset_str(infil, ss)
;
;------------------ Do the work
;
sat_limit = [235, 255, 255]             ; Full, half, quarter resolution
;
out0 = {ival: 0, day: 0, time: 0L, fms: 0.0, res: 0, dpe: 0, corn: 0.0, temp: 0.0, $
        avg: 0.0, dev: 0.0, $
        nover_sig: lonarr(2), nover_fixed: lonarr(2), nsat: 0L, $
        intercept: 0.0, slope: 0.0, $
        st$filename: bytarr(20), $
        avoid: 0, status1: 0, status2: 0, $                     ;used in sdml
        sdm_imgnum: 0, tim2week_cen: 0.0, time_weight: 0.0 }    ;used in sdml
out = replicate(out0, n)
;
for i=0,n-1 do begin
        ifil = dset(i).ifil
	;--- Taken from CHECK_SDM
	rd_xda, infil, dset(i), index, data, rmap
	nx = n_elements(data(*,0))
	data = data(1:nx-2,*)		;trim off first and last column
	tim2orbit, index, tim2fms=tim2fms
	ss = where( data ge sat_limit(gt_res(index)), nsat)
	data = sxt_decomp(data)
	idev = stdev(data, iavg)
	temp = abs(data-iavg)
	ss = where( temp ge 1*idev, nover1)
	ss = where( temp ge 2*idev, nover2)
	ss = where( temp ge 3*idev, nover3)
	ss = where( temp ge 4*idev, nover4)
	fixed_v = ((gt_dpe(index)/1000*5./3*2)>1) * (2^gt_res(index))
	ss = where( data ge iavg+fixed_v*2, nover5)
	ss = where( data ge iavg+fixed_v*4, nover6)
	ival = get_sdm_val(index)
	ival = ival(0)
	;
	lin = total(data, 1) / n_elements(data(*,0))
	xx = findgen(n_elements(lin))
	coeff = poly_fit(xx(20:*), lin(20:*), 1)
	;
	str = string(ival, gt_day(index), gt_time(index), tim2fms, $
		gt_res(index), gt_dpe(index), fix(gt_corner(index,/y)), gt_temp_ccd(index), $
		iavg, idev, nover1, nover4, nover5, nover6, nsat, coeff, $
		ifil, $
		format='(2i5, i10, f7.2, 3i5, f7.2, 2f9.3, 5i8, f8.3, f8.5, i6)')
	print, str
	;
	;--- Taken from SDM_CHECK2BIN
	break_file, infil(dset(i).ifil), dsk_log, dir00, filnam, ext
	;
        out(i).ival         = ival
        out(i).day          = gt_day(index)
        out(i).time         = gt_time(index)
        out(i).fms          = tim2fms
        out(i).res          = gt_res(index)
        out(i).dpe          = gt_dpe(index)
        out(i).corn         = fix(gt_corner(index,/y))
        out(i).temp         = gt_temp_ccd(index)
        out(i).avg          = iavg
        out(i).dev          = idev
        out(i).nover_sig    = [nover1, nover4]
        out(i).nover_fixed  = [nover5, nover6]
        out(i).nsat         = nsat
        out(i).intercept    = coeff(0)
        out(i).slope        = coeff(1)
        out(i).st$filename  = byte(filnam+ext)
end
;
outfil = concat_dir(outdir, 'sdmi' + week)
print, 'Saving ', outfil, ' with ', n, ' entries
file_delete, outfil
savegen, out, file=outfil

run_time = (systime(1) - start_time)/60.
end
