pro ext_subset, index_in, data_in, index_out, data_out, xsize=xsize
;
;+
;NAME:
;	ext_subset
;PURPOSE:
;	To extract a subset of a datacube and to update the output
;	index to reflect the change
;	NOTE: Index update is not implemented yet
;CALLING SEQUENCE:
;	ext_subset, index, data, index2, data2
;INPUT:
;	index_in-
;	data_in	-
;OUTPUT:
;	index_out-
;	data_out-
;HISTORY:
;	Written 11-Nov-91 by M.Morrison
;	14-Nov-91 (MDM) - Changed to update the "shape_cmd" field with the
;			  new image shape.
;	20-Nov-91 (MDM) - Minor patch for cases where box marked is outside
;			  the top or bottom
;	25-Feb-94 (MDM) - Modified to update the index record
;-
;
;---- Allow user to review the data
print, 'Now Running "stepper" to allow you to review the data'
print, 'Please note the image number that you want to use to select '
print, 'the region for extraction'
;
stepper, data_in, xsize=xsize
;
;---- Select region to be extracted
;
read, 'Enter the image number to display for extraction ', iimg
;
siz = size(data_in)
nx = siz(1) & ny = siz(2) & nimg = siz(3)
;
if n_elements(xsize) eq 0 then begin
    xsize = nx
    ysize = ny
    factor = 1
end else begin
    ysize = xsize/nx*ny
    factor = xsize/ny
end
;
qdone = 0
while not qdone do begin
    tvscl,rebin(data_in(*,*,iimg),xsize,ysize,/sample)
    get_boxcorn, x0, y0, x1, y1, /device
    draw_boxcorn, x0, y0, x1, y1, /device
    ;
    x0=x0/factor & y0=y0/factor & x1=x1/factor & y1=y1/factor
    nx0 = (x1-x0+1)
    ny0 = (y1-y0+1)
    ;
    nx_out = fix(nx0/64.+.99)*64		;make sure it rounds up
    ny_out = fix(ny0/64.+.99)*64
    dx = nx_out-nx0
    dy = ny_out-ny0
    x0 = (x0-dx/2)>0
    y0 = (y0-dy/2)>0
    ;
    x1 = (x0 + nx_out)<(nx-1) & if (x1 ge nx-1) then x0 = nx-nx_out-1
    y1 = (y0 + ny_out)<(ny-1) & if (y1 ge ny-1) then y0 = ny-ny_out-1
    ;
    print, 'Current restrictions make it necessary to save in 64x64 blocks'
    print, 'Adjustment to the selected window is shown now
    draw_boxcorn, x0*factor, y0*factor, x1*factor, y1*factor, /device
    ;
    yesnox, 'Is this ok? ', qdone, 'Y'
end
;
index_out = index_in
his_index, /enable
his_index, index_out		;copy relevant information
res = fix(gt_res(index_in))     ;made RES an integer - MDM 18-Nov-93
npix = 2^res    ;1,2,4
corner = gt_corner(index_in)    ; corner address of input image - center of summed pixel
corner(0,*) = corner(0,*) + x0*npix
corner(1,*) = corner(1,*) + y0*npix
shape = [x1-x0, y1-y0]
his_index, index_out, -1, 'corner_ext', corner
his_index, index_out, -1, 'corner_sav', corner
for i=0,nimg-1 do index_out(i).sxt.shape_sav = shape
;
data_out = data_in(x0:x1-1, y0:y1-1, *)
end

