function ext_subimg, index1, data1, index2, qdebug=qdebug, use_avg=use_avg, $
			line_map=line_map
;
;+
;NAME:
;	ext_subimg
;PURPOSE:
;	Given an SXT full frame image, extract the portion of the PFI image
;	described in the second parameter.
;CALLING SEQUENCE:
;	subimg = ext_subimg(index1, data1, index2)
;INPUT:
;	index1	- The FFI index structure
;	data1	- The FFI data array (NX x NY)
;	index2	- The PFI index structure
;OUTPUT:
;	returns the PFI portion of the FFI image
;OPTIONAL KEYWORD INPUT:
;	use_avg	- If set and the overlap of the PFI and the FFI is not 
;		  complete, then find the average of the signal level in the
;		  edge portion of the FFI image, and set all non-overlapping
;		  portions in the PFI image to that value.  Used for dark
;		  current subtraction method.
;	qdebug	- If set, print some debugging messages
;OPTIONAL KEYWORD OUTPUT:
;	line_map - A byte array of length equal to the number of lines in the
;		   PFI.  A value of 1 is set for every line that has overlap
;		   with the FFI.
;HISTORY:
;	Written 7-Apr-93 by M.Morrison (taking DARK_SUB work as the beginning)
;-
;
siz = size(data1)
typ = siz( siz(0)+1 )
nx = siz(1)
ny = siz(2)
;
qprint_message1 = 1
;
pfi_ffi = gt_pfi_ffi(index2)
if ((pfi_ffi eq 0) or (pfi_ffi eq 2)) then begin	;PFI or OR image
    c_cmd_x = index2.sxt.corner_cmd(0)                ;commanded value
    s_cmd_x = index2.sxt.shape_cmd(0)                 ;already in output pixels
    c_cmd_y = index2.sxt.corner_cmd(1)                ;commanded value
    s_cmd_y = index2.sxt.shape_cmd(1)                 ;already in output pixels

    npix = 2^gt_res(index2)                         ;number of pixels binned (1,2 or 4)
    x1 = 1024/npix - c_cmd_x/npix - 1
    x0 = x1 - (s_cmd_x-1)
    y0 = c_cmd_y/npix - index1(0).sxt.corner_cmd(1)*4/npix	;FR FFI do not start at line 0
								;the *4 is because of a reformatting error in FFI data
								;the CORNER_CMD value is commanded value, not 1x1 value
    y1 = y0 + s_cmd_y - 1				;OR versus PFI stip

    if ((y0 lt 0) or (y1 ge ny)) then begin
	if (qprint_message1) then print, 'EXT_SUBIMG: PFI/OR falls outside of FFI dark frame'
	sub_img = bytarr(nx, ny)*data1(0)	;*data1(0) converts the datatype to be the same
	y00 = 0
	if (y0 lt 0) then begin
	    y00 = abs(y0)
	    if (keyword_set(use_avg)) then begin
		temp = data1(*, 0:49)
		signal = fix(total(temp)/n_elements(temp) + 0.5)
		sub_img(*,0:y00-1) = signal
		if (qprint_message1) then print, 'EXT_SUBIMG: Setting lines 0 to ', $
							strtrim(y00-1,2), ' equal to ', strtrim(signal,2), ' DN'
	    end
	end

	if (y0 lt ny) then sub_img(0,y00) = data1(x0:x1, y0>0:y1<(ny-1))

	if (y1 ge ny) then begin
	    n_over = y1-(ny-1)
	    y11 = ny - n_over
	    if (keyword_set(use_avg)) then begin
		temp = data1(*, ny-50:ny-1)
		signal = fix(total(temp)/n_elements(temp) + 0.5)
		sub_img(*,y11:ny-1) = signal
		if (qprint_message1) then print, 'DARK_SUB: Setting lines ', strtrim(y11,2), ' to ', $
							strtrim(ny-1,2), ' equal to ', strtrim(signal,2), ' DN'
	    end
	end
    end else begin
	sub_img = data1(x0:x1, y0:y1)
    end
    qprint_message1 = 0
end else begin
    sub_img = data1
end

if (keyword_set(qdebug)) then stop
return, sub_img
end

