
;	(26-mar-91)
FUNCTION GET_OFF2, DATA, SAMPLE=SAMPLE, MAG_FAC=MAG_FAC
;+
; NAME:
;	GET_OFF2
; PURPOSE:
;	Coalign a series of images using FFT cross correlation
;	technique.
; CALLING SEQUENCE:
;	OFFSETS = GET_OFF(DATA, SAMPLE=SAMPLE, MAG_FAC=MAG_FAC)
; INPUTS:
;	DATA = Data cube of images to be coaligned
; OPTIONAL KEYWARDS
;	SAMPLE = Flag to determine whether nearest neighbor or
;		 interpolation is used for expanding subarray prior
;		 to cross correlation
;	MAG_FAC = Magnification factor for expanding subarray prior
;		  to cross correlation
; OUTPUTS:
;	OFFSETS = 2 x n_image array of offsets where n_image is the 
; MODIFICATION HISTORY:
;	Version 1.0 - Mar, 1991, G. L. Slater, LPARL
;-

  if n_elements(sample) eq 0 then sample = 1
  if n_elements(mag_fac) eq 0 then mag_fac = 1
  siz = size(data) & n_image = siz(3)
  offsets = fltarr(2,n_image)	; x,y offsets of each image from
				;   first image

; Select sub-array within full ref image for cross correlation:

  ref_im = data(*,*,n_image/2)

  win_siz = max([siz(1),siz(2),256])
  xwin,n=0,win_siz
  erase & tvscl,ref_im

  print,' Click on lower left and upper right corners of sub-region'
  print,'   to be used for offset determination using left hand mouse' 
  print,'   key.  Repeat until satisfied with boxed region.  Then'
  print,'   click on right hand mouse key. (NOTE: selected area will'
  print,'   be made square for use by FFT.)'

  cursor,xbuf,ybuf,/dev & wait,.5
  repeat begin
    x0 = xbuf & y0 = ybuf
    plots,/dev,[x0],[y0],psym = 6
    cursor,x1,y1,/dev & wait,.5
    fov_siz = max([x1-x0+1,y1-y0+1])
    x1 = x0 + fov_siz-1 & y1 = y0 + fov_siz-1
    plots,/dev,[x0,x0,x1,x1,x0],[y0,y1,y1,y0,y0]
    cursor,xbuf,ybuf,/dev & wait,.5
  endrep until !err ne 1

  siz_img = mag_fac*fov_siz
  win_siz = max([siz_img,256])
  xwin,n=1,win_siz
  for i=0,n_image-2 do begin
    tvscl,data(*,*,i)
    plots,/dev,[x0,x0,x1,x1,x0],[y0,y1,y1,y0,y0]

    img0 = rebin(data(x0:x1,y0:y1,i)  ,siz_img,siz_img,sample=sample)
    img1 = rebin(data(x0:x1,y0:y1,i+1),siz_img,siz_img,sample=sample)
    offset_buff = cc_off(img0,img1)
    if i eq 0 then begin
      offsets(0,i) = offset_buff(0)/mag_fac
      offsets(1,i) = offset_buff(1)/mag_fac
    endif else begin
      offsets(0,i) = offsets(0,i-1) + offset_buff(0)/mag_fac
      offsets(1,i) = offsets(1,i-1) + offset_buff(1)/mag_fac
    endelse

    print,' Completed correlation on pair (' + $
      string(i,i+1,'(i2,",",i2,")")')
  endfor
; ***************************************************************
; slf - modified to match translate2 coordinate - sign diif x and y 
  offsets=-(shift(offsets,0,1))	;**** slf, kludge?	27-march-1992
; **************************************************************
  return, offsets
  end

