pro get_leak_image, struct_in, index, data, imap_out, match_qual=match_qual, qstop=qstop, qdebug=qdebug, save=save
;
;
;+
;NAME:
;	get_leak_image
;PURPOSE:
;	Given an index or roadmap structure, match and
;	extract the corresponding leak image
;CALLING SEQUENCE:
;	get_leak_image, index(3), leak_index, leak_data
;	get_leak_image, roadmap, leak_index, leak_data, imap
;INPUT:
;	struct_in - An index or roadmap 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.
;OUTPUT:
;	index	- The leak current index structure
;	data	- The leak current image DECOMPRESSED.  The exposure of
;		  the output leak image is NOT the exposure of the 
;		  reference input image.  The image is the proper resolution
;		  and location on the CCD though (LEAK_SUB does the 
;		  exposure normalization)
;	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.
;METHOD:
;	Can optionally save the last Leak image read to avoid I/O
;HISTORY:
;	Written 27-Aug-93 by M.Morrison (Using GET_DC_IMAGE as template)
;	 4-Jan-94 (MDM) - Modified the header
;       20-mar-95 (SLF) - allow floating sfc files
;	15-May-95 (MDM) - Added header info (exposure not normalized)
;V4.1	22-Aug-95 (MDM) - Corrected a but which was causing the IMAP to
;			  be off, and to not work when there were more
;			  than 1 filter/resolution type which did not 
;			  have a match within the leak database.
;-
;
common get_leak_image, lindex, ldata
;
progverno = 4.1*1000
;
sel_leak_image, struct_in, leak_files, dset, leak_rmap
if (max(dset) eq -1) then return	;no leak images selected
;
;----- imap is the map of source leak frames to use, imap_out is the map of what is output since the same frame can be
;      used multiple times if it is an OR which moves, but same exposure
;
nin = n_elements(struct_in)		;number of input images specified
;
xcorn = fix(gt_corner(struct_in, /lower, /x)/4)	;0-255
ycorn = fix(gt_corner(struct_in, /lower, /y)/4)
res = gt_res(struct_in)

;do all FR in order of DPE, then all HR, then all QR
val =      xcorn         +  ycorn*256L               + res*256L*256L  + dset*256L*256L*64	;dset specifies different leak image
;      low 8-bits 0:7      second 8-bits  8:15          third 8-bits     fourth 8-bits

if (n_elements(val) eq 1) then ss_val = 0 else ss_val = uniq(val, sort(val))		;in terms of datasets of input images
uval = val( ss_val )
ss22 = where(uval ge 0)		;MDM added 22-Aug-95 (remove the cases where there was no filter match .. ie: dset=-1)
ss_val = ss_val(ss22)		;MDM added 22-Aug-95
uval = uval(ss22)		;MDM added 22-Aug-95
imap_out = intarr(nin) - 1	;MDM added "-1" 22-Aug-95
for i=0,n_elements(uval)-1 do imap_out(where(val eq uval(i))) = i			;gotta be a better way

ishift = 0					;MDM removed following line 22-Aug-95
;;if (min(dset) eq -1) then ishift = 1		;sheesh - what a pain - skip first image (-1 image)
;
nx = max(gt_shape(struct_in, /x))
ny = max(gt_shape(struct_in, /y))
nout = n_elements(uval)		;number of uniq leak images


leak_dset = dset(ss_val(nout-1))	;read last LEAK index image which will be used
rd_xda, leak_files, leak_dset, index, /nodata

;data = intarr(nx, ny, nout-ishift)
; slf - 20-mar-95 - allow floating sfc files
data=make_array(nx,ny,nout-ishift, $
   type=max(index.gen.data_word_type and 7)> 2)

index = replicate(index(0), nout)
his_index, /enable
his_index, index

;------------------------------------------------------------------------------
;		Loop through each leak image to be read
;------------------------------------------------------------------------------

for i=0,nout-1 do begin
    idset = ss_val(i)	;the dataset of the uniq input "struct_in" image that is being processed
    leak_dset = dset(idset)
    struct_in0 = struct_in(idset)
    lrmap0 = leak_rmap(leak_dset)

    qffi = gt_pfi_ffi(struct_in0, /true_ffi)	;0 for PFI,OR,FFI2PFI - 1 for true FFI
    qextract = (qffi eq 0)
    qextract = qextract or (gt_res(struct_in0) ne gt_res(lrmap0))	;

    if (leak_dset  ne -1) then begin		;there is actually a
	qread_leak = 1
	if (get_nbytes(lindex) gt 10) then if (int2secarr(lrmap0, lindex) eq 0) then qread_leak = 0
	if (qread_leak) then begin
	    if (keyword_set(qdebug)) then print, 'Reading image: ', fmt_tim(lrmap0)
	    rd_xda, leak_files, leak_dset, lindex, ldata
	end
	index0 = lindex
	his_index, index0

	if (qextract) then begin
	    out_img0 = ldata
	    align1img, index0, out_img0, ref_image=struct_in0		;changes resolution and extracts
	    data(0,0,i-ishift) = temporary(out_img0)
	end else begin
	    data(0,0,i-ishift) = ldata
	end

	time_leak = gt_time(index0)
	day_leak  = gt_day(index0)
	index_copy = str_copy_tags(index(0), index0)		;copy the "index0" into the output array structure type
	index(i-ishift) = index_copy
	his_index, index, i-ishift, 'q_leak_sub', progverno
	his_index, index, i-ishift, 'time_leak', time_leak
	his_index, index, i-ishift, 'day_leak', day_leak
    end
end
;
if (not keyword_set(save)) then begin
    lindex = 0
    ldata = 0
end
;
if (keyword_set(qstop)) then stop
end
