pro get_dc_warm, struct_in, index, data, imap, match_qual=match_qual, qstop=qstop
;
;+
;NAME:
;	get_dc_warm
;PURPOSE:
;	Given an index structure, match and extract the closest dark 
;	current image for that exposure (look at DPE, resolution, and
;	temperature)
;CALLING SEQUENCE:
;	get_dc_image, index(3), dc_index, dc_data
;INPUT:
;	struct_in - An index structure.  If the input
;		    is an array, then data will be a matrix and it
;		    is necessary to use the output variable "imap"
;		    to match up which dark frame goes with which input
;		    image.  This is done so that unnessary duplication
;		    of the same image is not done.
;		    Unlike "get_dc_image", this routine will not 
;		    accept the roadmap as input (the temperature is
;		    not stored in the roadmap).
;OUTPUT:
;	index	- The dark current index structure
;	data	- The dark current image
;	imap	- A list of indicies telling which image in "data" to
;		  use for each "struct_in" image.  The output is an
;		  array the same length as "struct_in" with values that
;		  go from 0 to the number of output images minus one.
;KEYWORD OUTPUT:
;	match_qual - The quality of the match.  It is a structure with
;		  the following fields.
;			.dpe  = # of DPE levels away from the actual
;				0 is perfect match, positive means DC
;				is longer exposure than requested.
;			.time = the number of days from the requested day.
;			        positive is for DC images taken past
;				the time of the input image
;			.temp = the number of degress of the match it is.
;METHOD:
;	The program will try to make an exact match to the exposure
;	level passed.  If it cannot match, it will take the next exposure
;	larger which is available.  
;
;	If the images requested for extraction match the images that are
;	passed in, then the data file is not read again.
;HISTORY:
;	Written 26-Apr-92 by M.Morrison (using "get_dc_image" as a start)
;	 1-May-92 (MDM) - Fixed trouble with "bad_match" option
;	25-Jan-93 (MDM) - Return a zero value when no match is found for
;			  data and for index
;	 6-May-93 (MDM) - Modified to work with VMS (used FILE_LIST)
;	18-Aug-93 (MDM) - Changed directory to DIR_SXT_SDW
;-
;
common get_dc_warm_blk, dc_infil, dc_roadmap, dc_index
;
timerr = 60	;try to get a match to within 45 days of time of exposure
		;choose something with a bigger DPE error but closer in time
max_temperr = 3.
qdebug = 0
;
match_qual0 = {dc_warm_match_qual, dpe: 0, time: 0.0, temp: 0.0}
;
if (n_elements(dc_infil) eq 0) then begin
    dc_infil = file_list('$DIR_SXT_SDW', 'sdw*')
    if (dc_infil(0) ne '') then rd_xda, dc_infil, -1, dc_index, dc_data, dc_roadmap, /nodata
end
if (dc_infil(0) eq '') then begin
    message, 'No SDW files found in $DIR_SXT_SDW', /info
    message, 'Returning with a value of zero', /info
    index = 0
    data = 0
    imap = 0
    match_qual = match_qual0
    match_qual.dpe = -99
    match_qual.time = 1e+6
    return
end
;
dpe = gt_dpe(dc_roadmap)
res = gt_res(dc_roadmap)
temp = gt_temp_ccd(dc_index)
;
ndc = n_elements(dc_roadmap)
nin = n_elements(struct_in)
imap = intarr(nin)-1
dset_arr = intarr(nin)-1
match_qual = replicate(match_qual0, nin)
;
for iin=0,nin-1 do begin
    struct_in0 = struct_in(iin)
    x = int2secarr(dc_roadmap, struct_in0)/86400	;# of days the xray/optical image was taken from each DC image
    good = bytarr(ndc)
    ss = where(res eq gt_res(struct_in0))
    if (ss(0) eq -1) then begin
	message, 'No images with resolution = ' + gt_res(struct_in0,/str), /info
    end else begin
	good(ss) = 1
    end

    qdone = 0
    err = 0
    dpe0 = gt_dpe(struct_in0)
    temp0 = gt_temp_ccd(struct_in0)
    dpe_err_arr = dpe-dpe0
    ;take all negative errors and increase them so that the higher DPE is more favored
    ss = where(dpe_err_arr lt 0)
    if (ss(0) ne -1) then dpe_err_arr(ss) = dpe_err_arr(ss)+1.5
    temp_err_arr = temp-temp0
    qbad_match = 0
    bad_match = -1
    while (not qdone) do begin
	ss = where(good and (abs(dpe_err_arr) le err) and (abs(temp_err_arr) le max_temperr), count)
	if (count ne 0) then begin
	    sss = where(abs(temp_err_arr(ss)) eq min(abs(temp_err_arr(ss))))	;get list of all images with closest temperature
	    ss = ss(sss)							;reduce the time search to those with the
										;closest temperature
	    xoff = x(ss)
	    imin = min(abs(xoff), isub)
	    idset = ss(isub)
	    if (abs(imin) lt timerr) then begin
		if (qdebug) then print, 'Found a match.  Time offset = ', imin, ' days'
		qdone = 1
		qbad_match = 0
		dset_arr(iin) = idset
		match_qual(iin).dpe	= dpe_err_arr(idset)
		match_qual(iin).time	= imin
		match_qual(iin).temp	= temp_err_arr(idset)
	    end else begin
		;print, 'Found a match, but the time is off by ', abs(imin), ' days'
		;print, 'Looking for a different exposure taken closer in time'
		qbad_match = 1
		if (bad_match eq -1) then bad_match = idset	;save the first bad match found
	        err = err + 1
	    end
	end else begin
	    err = err + 1
	end
	if (err gt 36) then begin
	    qdone = 1
	    print, 'No match - all DPEs searched'
	end
    end		;while

    if ((err gt 36) and (qbad_match)) then begin	;couldn't find a better match so go back to the best one 
							;regardless of time error
	idset = bad_match
	dset_arr(iin) 		= idset
	match_qual(iin).dpe	= dpe_err_arr(idset)
	match_qual(iin).time	= x(idset)
    end
end
;
ss_dsets = uniq(dset_arr, sort(dset_arr))
dsets = dset_arr(ss_dsets)
if (dsets(0) eq -1) then begin
    if (n_elements(dsets) eq 1) then begin
	message, 'No matches made', /info
	index = 0
	data = 0
	imap = 0
	match_qual = match_qual0
	match_qual.dpe = -99
	match_qual.time = 1e+6
	return
    end else begin
	dsets = dsets(1:*)		;drop off first value (-1)
    end
end
;
for i=0,n_elements(dsets)-1 do imap(where(dset_arr eq dsets(i))) = i	;gotta be a better way
;
qread = 1
siz = size(index)
typ = siz( siz(0) + 1)
if ((n_elements(index) eq n_elements(dsets)) and (typ eq 8)) then begin		;check to see if already read the images requested
    tot = 0
    for i=0,n_elements(dsets)-1 do tot = tot + int2secarr(index(i), dc_roadmap(dsets(i)))
    if (total(tot) eq 0) then qread = 0
end
if (qread) then rd_xda, dc_infil, dsets, index, data, dc_roadmap $
	else if (qdebug) then print, 'Reading of the DC file is not necessary'
;
if (keyword_set(stop)) then stop
end
