pro mk_sdc, infil, outfil, week=week, run_time=run_time, $
            correct_col_blem=correct_col_blem,nbest =nbest
;+
;NAME:
;	mk_sdc
;PURPOSE:
;	Generate the SXT Dark Current SDA image (SDC) file from the
;	SFR files of all unique (exposure and resolution) dark current 
;	images excluding SAA and non-100% images
;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
;       /correct_col_blem = call sdc_verify a second time to correct 
;                              column blemishes, when necessary.
;HISTORY:
;	Written 20-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"
;	  13-May-93 (MDM) - Changed GT_EXPDUR call to have /NOCORR to not
;			    correct for the shutter error
;			  - Added check for upper/lower half image (for FR)
;Ver 1.12 16-May-93 (MDM) - Patch to exclude BLS dark images taken "11-APR-93"
;			    which were "Qrtr Dark DPE=11 (38.0 )"
;	  18-Aug-93 (MDM) - Changed to use DIR_SXT_SDC and DIR_SXT_SDL
;Ver 1.13 19-Aug-93 (MDM) - Added check to avoid the SLS images which are
;			    QR, DPE=2, NaBan images which have scatter light
;			    in them.
;Ver 1.20  4-Aug-94 (MDM) - Modified to exclude dark images which have staturated
;			    pixels
;Ver 1.21  7-apr-95 (SLF) - avoid frames with fill data (show 100% "good")
;Ver 1.22  6-Jul-95 (MDM) - Added check that the image is not a patrol image
;			  - Updated ProgVerNo (it was not for Ver 1.21)
;Ver 1.23 23-Mar-99 (TRM) - added call to sdc_verify to select the best dark
;                           frame when multiple frames are available.
;                         - Added check of filters.  Before the Oct 1992 
;                           entrance filter failure any filter combination
;                           is allowed.  Between the Oct 92 and Nov 92 
;                           failures, only darks with a metal filter are
;                           allowed.  After the Nov 92 failure only darks
;                           with AlMg filter are allowed.
;Ver 1.24 11-May-99 (SLF) - add /CLEAR_COMPRESS in rd_xda call
;Ver 1.30 12-May-99 (SLF) - !!SIMPLIFY!! use sav_sda instead of lower level...
;                           use file_delete, /noshell etc.
;Ver 1.31 15-May-99 (TRM) - Simplified the call to sdc_verify so that it is
;                           only called when necessary.  Added a second call
;                           to sdc_verify to correct for column blemishes,
;                           when necessary.
;Ver 1.32 15-Jul-99 (TRM) - Make the default NOT to do the column blemish
;                           correction.
;Ver 1.33 28-jan-00 (PGS) - y2k fix: replaced sel_timrange with
;                           ssw_time_compare
;Ver 1.34 22-Nov-02 (TRM) - Added nbest keyword. Changed SAA flag to
;                           use saa_in.pro rather than just 
;                           the RBM flag.  Removed call to
;                           get_sdl_info and changed code to reject
;                           all darks from SAA times.  Changed the
;                           test around Nov 92 to allow gt_filtb GE 3
;                           so that the thicker filters are not
;                           rejected at that time.  I also noticed that when
;                           Phil did the y2k fix he did not update the
;                           version number so there probably is no
;                           1.33
;Ver 1.35 02-Dec-02 (TRM) - Despike the short exposure darks.
;                         - Added call to sxt_mksdc_history
;                         - Added call to sxt_bakeout_in with a
;                           cooling time of 12 hours to better
;                           eliminate bakeouts.
;
;-
start_time = systime(1)
run_time = 0.
;
progVerNo = 1.35*1000
progName = 'MK_SDC.PRO'
;
if NOT keyword_set(nbest) then nbest = 1L
;
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', 'sdc'+weekid)
outfil_final = concat_dir('$DIR_SXT_SDC', 'sdc'+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'), $
                 finddir('19'+week), finddir('20'+week)]
    infil = file_list(indir_arr, 'sfr*')
end

;
message,/info,'Reading Roadmap ... '+ $
              strcompress(string(n_elements(infil)),/remove_all)+ $
              ' files'
rd_roadmap, infil, roadmap

; TRM 1999-Jun-15 TRM commented out the dark frame quality check.  This is now 
; done in a simpler way below.
;
;; TRM 1999 Mar 23.
;ssdark = where(gt_expmode(roadmap) EQ 1)   ; find all the dark frames
;message,/info,'Reading dark frames: '+ $
;              strcompress(string(n_elements(ssdark)),/remove_all)
;rd_xda,infil, ssdark,  darkindex, darkdata, /clear_compress
;
;message,/info,'Checking quality of dark frames'
;dark_check = sdc_verify(darkindex,darkdata) ;quality of the dark frames
;; remap onto sdl_index ...  
;; is this necessary? Is there a 1-1 correspondence already?
;; oh well, can't hurt
;sdl_dark_check = fltarr(n_elements(sdl_index))
;stop
;for i=0L,n_elements(sdl_index)-1L do begin 
;   ss=tim2dset(darkindex,sdl_index(i),delta=delta)
;   if delta(0) LT 2 then sdl_dark_check(i) = dark_check(ss).badness $
;   else begin
;      ; I think this should never happen
;      message,/info, $
;         'WARNING: SDL - SFR time mismatch detected: '+string(delta)+' seconds'
;      sdl_dark_check(i) = 1000. ; Assume the dark is bad, though it hasn't been
;                                ; checked for some strange reason.
;   endelse
;endfor

;----- Diagnostic variables
;
qprint = 1
qdebug = 1
;
;---------------------------------------- General Stuff
;
ref_struct, ref
sxt_struct, sxt_version=sxt_version, sxt_darklog=sxt_darklog
;
rsiz = ref.SXT_Fil_Rec_Siz

maxsamps = 0
;
v = sdl_index.sxt.corner_cmd(1) + gt_res(sdl_index)*1024L + sdl_index.sxt.explevmode*2048L	;changed 13-May-93
vuniq = v( uniq(v, sort(v)) )
;rbm = sdl_index.gen.rbm_status
saa = saa_in(sdl_index)
bake = sxt_bakeout_in(sdl_index,cooling_time=12.0)
act_expdur = gt_expdur(sdl_index, /nocorr)	;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
; act_avg is the average signal and act_dev is the st. deviation.
; exp_avg is the expected average DN level
;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))
;

rd_fheader, infil(0), fhead0

sdc_verify_version = 0

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 ' + $
			'StLin:' + string(sdl_index(ss0(0)).sxt.corner_cmd(1), format='(i4)') + $
			' (' + 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 (err_temp lt 1.5))
    sel = where((sdl_index.sxt.percentd eq 255) and (v eq vuniq(iuniq)) and $
		(abs(err_expdur) lt 0.05) and (err_temp lt 1.5) and (NOT bake) and $
                (sdl_index.sxt.percentover le 5) and $
		(sdl_data.hist(170) lt gt_shape_cmd(sdl_index,/x)) and $
                (gt_shape_cmd(sdl_index,/x) NE 1024 OR gt_shape_cmd(sdl_index,/y) GE 512) AND $
		(gt_pfi_ffi(sdl_index) ne 3) and $

                (int2secarr(sdl_index,'27-oct-92 00:00') LT 0 or gt_filtb(sdl_index) GT 1) and $
                
                (int2secarr(sdl_index,'13-nov-92 18:00') LT 0 or gt_filtb(sdl_index) GE 3) and $
                (int2secarr(sdl_index,'24-nov-92 03:10') LT 0 or gt_filtb(sdl_index) EQ 3) $
                )
;   slf, 7-apr-95 - avoid frames with fill data ( 'AA'hex = 170 dec)

    if (sel(0) ne -1) then good(sel) = 1
    ;
    dummy = where( sel_timrange(sdl_index, '11-Apr-93', '18-Apr-93', /between,/bool) and (gt_dpe(sdl_index) eq 11)) ;MDM 16-May-93
    if (dummy(0) ne -1) then good(dummy) = 0	;do not use those image
;    dummy = where( sel_timrange(sdl_index, '17-Nov-92', '30-dec-99', /between,/bool) and (gt_filta(sdl_index) eq 2)) ;MDM 19-Aug-93
    dummy = where( ssw_time_compare(sdl_index,'17-Nov-92',/later) and (gt_filta(sdl_index) eq 2)) ;MDM 19-Aug-93
    if (dummy(0) ne -1) then good(dummy) = 0	;do not use those image (NaBan after Nov 92)
    ;
    ;ss = where(good and (rbm eq 0), count)
    ss = where(good and (saa eq 0), count)
    if (count eq 0) then begin 
	if (qdebug) then print, 'No good images when looking at SAA flag.'
	;if (qdebug) then print, 'No good images when looking at SAA flag - now expanding criteria to look at average'
;	ss = where(good and (abs(err_avg) lt 0.05), count) ; TRM commented out 22-Nov-2002
    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))
        rd_xda, infil, ss2, index, data, roadmap, /clear_compress
        ; sort based on badness from sdc_verify so
        ; that we pick the best of the selected images - TRM 1999 Mar 23
        sdl_dark_check = sdc_verify(index,data,version=sdc_verify_version)
        ;bestval = min(sdl_dark_check.badness,ssmin)
        ssbestval = sort(sdl_dark_check.badness)
        bestval = sdl_dark_check[ssbestval[0]]
        ssmin = reform(ssbestval[0L:(nbest<n_elements(ssbestval))-1L])
        index = index(ssmin)
        data = data(*,*,ssmin)
        sdl_dark_check = sdl_dark_check(ssmin)
        shortexp = where(gt_dpe(index) EQ 2,nshortexp)
        if nshortexp GE 1 then begin ; TRM 2002-Dec-02, Despike short exps
           ; Despike the short exposures
           for ishort =0L,nshortexp-1L do begin
              shortdata = float(data[*,*,shortexp[ishort]])
              ;dnoise = stdev(shortdata-shift(shortdata,1,1))
              data[*,*,shortexp[ishort]] = despike_gen(shortdata)
           endfor
        endif
        ; TRM 1999-Jun-15 Add call to correct column blemish
        if  keyword_set(correct_col_blem) then begin
           for ibest = 0L,n_elements(ssmin)-1L do begin
              if sdl_dark_check[ibest].nbleed GT 1 then $
                 junk = sdc_verify(index[ssmin[ibest]],data[*,*,ssmin[ibest]],/correct_col_blem)  ; Correct column blemish
           endfor
        endif


    ; 1999-Jun-15 TRM commented out the following
    ; block since it is done in a simpler way now.

    ;    ; sort based on badness from sdc_verify so
    ;    ; that we pick the best of the selected images - TRM 1999 Mar 23
    ;    ss = ss(sort(sdl_dark_check(ss)))
;	ss2 = tim2dset(roadmap, sdl_index(ss(0)))
;	rd_xda, infil, ss2(0), index, data, roadmap, /clear_compress	;read the first image only
        ; TRM 1999-Jun-15 Add call to correct column blemish
        ;if keyword_set(correct_col_blem) then $
        ;   junk = sdc_verify(index,data,/correct_col_blem)  ; Correct column blemish

        sxt_mksdc_history, index, sdl_dark_check.badness, $
                          mk_sdc_ver=fix(progVerNo), $
                          sdc_verify_ver=fix(sdc_verify_version)
;       -- slf, 12-May-1998 simplify writing process --
        sav_sda,outfil,index,data,fhead=fhead0, append=(iuniq ne 0)      ; slf, 12-may-1999
 	maxsamps = max([maxsamps, get_nbytes(data)])
    end
    if (qdebug) then print, tit, tit2
end

if maxsamps eq 0 then file_delete, outfil

if (n_elements(outfil_final) ne 0) then begin
    file_delete, outfil_final
    cmd = 'mv ' + outfil + ' ' + outfil_final
    spawn, str2arr(cmd,' '), /noshell
    outfil = outfil_final
end
;
end_time = systime(1)
run_time = (end_time-start_time)/60.
print, 'MK_SDC took', run_time, ' minutes to run'
;
end
