
;	(26-mar-91)
FUNCTION CC_OFF2,A,B,FITDEG=FITDEG,CLEN=CLEN
;+
; NAME:
;	CC_OFF2
; PURPOSE:
;	Determine the spatial offset between two images from
;	their cross correlation
; CALLING SEQUENCE:
;	OFFSETS = CC_OFF(A,B,FITDEG=FITDEG,CLEN=CLEN)
; INPUTS:
;	A, B = Images whose relative offset is to be determined
; OPTIONAL KEYWORDS:
;	FITDEG = Degree of surface fit to input arrays. These fits
;		 are subtracted to remove background prior to cc.
;	CLEN = If supplied, the arrays are embedded in larger
;	       arrays of dimension XDIM + 2*CLEN, where XDIM is
;	       the dimension of the input arrays.
; OUTPUTS:
;	OFFSETS = Offsets in pixels between fov's of the two images
;
; MODIFICATION HISTORY:
;	Version 1.0 - Mar, 1991, G. L. Slater, LPARL
;-

if n_elements(fitdeg) eq 0 then fitdeg = 1
siz = size(a) & xdim = siz(1) & ydim = siz(2)
if xdim ne ydim then begin
  print,' ERROR IN CCOR: ARRAYS MUST BE SQUARE AND OF EQUAL LENGTH.'
  stop
endif
if n_elements(clen) eq 0 then clen = 0
  
if fitdeg ne 0 then begin
  a = a - surface_fit(float(a),fitdeg)
  b = b - surface_fit(float(b),fitdeg)
endif
x1_dim = xdim+2*clen
a1 = fltarr(x1_dim,x1_dim) & b1 = fltarr(x1_dim,x1_dim)
a1(clen,clen) = a & b1(clen,clen) = b

c = cc_fft(a1,b1)
d = shift(float(c),xdim/2,xdim/2) & d = d - min(d)
dmax = max(d)
xmax = !c mod xdim & ymax = !c / ydim
peak_coords = where(d ge .5*dmax) & d_peak = d(peak_coords)
peak_x = peak_coords mod xdim & peak_y = peak_coords / xdim
tot = total(d_peak)
xoff = xdim/2 - total(peak_x*d_peak)/tot
yoff = xdim/2 - total(peak_y*d_peak)/tot

return,[xoff,yoff]
end

