pro ext_subimg2, index_in, data_in, index_out, data_out, $
	index_ref=index_ref, $
	fri=fri0, nfr_pix=nfr_pix, center=center, $
	conv2fr = conv2fr, $
	override=override, $
	qdebug=qdebug
;
;+
;NAME:
;	ext_subimg2
;PURPOSE:
;	To extract a sub image out of a full frame image
;INPUT:
;	index_in - The index for the FFI image
;	data_in	- The data array of the FFI image
;OUTPUT:
;	index_out- The index for the output.  
;	data_out- The data array extracted from the FFI
;OPTIONAL KEYWORD INPUT:
;	fri	- The Full Resolution IDL coordinate of the lower left sub-pixel
;		  in the lower left of the observing region to extract. [x0,y0]
;	nfri_pix- The number of full resolution pixels to extract [nx,ny]
;	center	- If set, then the "FRI" coordinates are for the center of the
;		  field of view to extract
;	index_ref- It is possible to pass a PFI index structure and have the
;		  corresponding portion extracted.
;HISTORY:
;	Written 6-Jun-93 by M.Morrison
;	20-Jun-93 (MDM) - Added more header information
;			- Inserted call to GT_CORNER
;-
;
progverno = 1.0*1000
;
res = gt_res(index_in)
sum = 2^res
;
;HR: FRI-1,2 are in IDL-0
;    FRI-3,4 are in IDL-1
;QR: FRI-3,4,5,6 are in IDL-0
;
if (keyword_set(index_ref)) then begin
    ;fri = gt_corner_cmd(index_ref)
    ;fri(0) = 1023 - fri(0)	;reverse from CCD coord to FRI
    ;fri(0) = fri(0) - (gt_shape_cmd(index_ref, /x)-1) * 2^gt_res(index_ref)
    fri = gt_corner(index_ref, /lower_left)

    ;fri(0) is now the lower left sub pixel of the summed pixels
    nfr_pix = gt_shape_cmd(index_ref)*2^gt_res(index_ref)

    resout = gt_res(index_ref)
end
if (keyword_set(fri0)) then begin
    if (keyword_set(center)) then fri = fri0 - fix(nfr_pix/2) $
				else fri = fri0
end
;
if (n_elements(fri) eq 0) then begin
    message, 'Corner must be specified either by FRI or INDEX_REF keyword
    return
end
;
case res of	;resolution of the input FFI image
	0: begin & x0 = fri(0) 		& y0=fri(1)	& xtrim = 0 			& ytrim = 0 		& end
	1: begin & x0 = (fri(0)-1)/2.	& y0=fri(1)/2	& xtrim = (fri(0)-1) mod 2 	& ytrim = fri(1) mod 2	& end
	2: begin & x0 = (fri(0)-3)/4.	& y0=fri(1)/4	& xtrim = (fri(0)-3) mod 4	& ytrim = fri(1) mod 4	& end
endcase
;
;Samples:
;	QR Input: Ask for FRI 5, means extract IDL-0 but drop off the first two pixels
;	HR Input: Ask for FRI 0, means there is an extra column to pad
;
xpad = 0
if (x0 lt 0) then begin
    xtrim = 0
    xpad = abs(x0) * 2^res	;For FRI=0, xpad will be 1 for HR and 3 for QR
				;The xpad value is in FR pixels
    x0 = 0
end
;
nx = nfr_pix(0) / sum + (xtrim ne 0)	;need an extra pixel if we need to trim it
ny = nfr_pix(1) / sum + (ytrim ne 0)
x1 = x0 + nx-1
y1 = y0 + ny-1
;
if (keyword_set(qdebug)) then begin
    print, 'FRI Requested: ', fri
    print, 'Extracting x0,x1,y0,y1 from the input image', x0, x1, y0, y1
    print, 'Xtrim, ytrim, xpad', xtrim, ytrim, xpad
end
data_out = data_in(x0:x1, y0:y1)
;
if (n_elements(resout) eq 0) then resout = gt_res(index_in)	;0,1 or 2
if (keyword_set(conv2fr)) then resout = 0	;convert to full resolution
factor = 2.^res / 2.^resout	;Going from HR to FR ==> 2/1 ==> factor = 2
				;Going from FR to QR ==> 1/4 ==> factor = 0.25
;
npix = 2.^resout	;1,2,4
index_out = index_in
his_index, index_out, 0, 'corner_sav', fri + (npix-1)/2.	;shift from center of lower left FR pixel to the middle of summed pix
index_out.sxt.shape_sav = [x1-x0+1, y1-y0+1]
;
;if (factor ne 1) then data_out = rebin(data_out, nx*factor, ny*factor, /sample)
if (factor ne 1) then data_out = mod_res(index_out, data_out, index1, outres=resout, override=override)
index_out = index1

nxout = nfr_pix(0) / 2.^resout
nyout = nfr_pix(1) / 2.^resout

if ((xtrim ne 0) or (ytrim ne 0)) then begin
    x0 = xtrim
    x1 = x0 + nxout - 1
    y0 = ytrim
    y1 = y0 + nyout - 1
    data_out = temporary(data_out(x0:x1, y0:y1))
end
;
if (xpad ne 0) then begin
    siz = size(data_out)
    nny = siz(2)
    typ = siz( siz(0)+1 )
    temp = make_array(xpad/2^resout, nny, type=typ)
    data_out = [temporary(temp), temporary(data_out)]
    data_out = temporary(data_out(0:nxout-1, 0:nyout-1))
end
;
;---------------------------------------- Update the history index
;
his_index, index_out, 0, 'q_subimg', progverno
his_index, index_out, 0, 'corner_ext', fri
his_index, index_out, 0, 'corner_sav', fri
his_index, index_out, 0, 'shape_ext', nfr_pix
his_index, index_out, 0, 'pixel_size', resout
index_out.sxt.shape_sav = [nxout, nyout]
;
end
