

FUNCTION CENTER_LIMB_COMP, x1, x2, y1, y2, p, img, rwin, n
;+
; NAME:
;    CENTER_LIMB_COMP
;
; PURPOSE:
;    Function to find improved center coordinates for off-center full disk
;    images with absolute difference method.
;
; CATEGORY:
;    BBSO Archiving System.
;    Image Processing.
;
; CALLING SEQUENCE:
;    dxy = CENTER_ABS_DIFF ( x1, x2, y1, y2, p, img, rwin, n )
;
; INPUTS:
;    X1:   Left x coordinate of off-center image.
;    X2:   Right x coordinate of off-center image.
;    Y1:   Bottom y coordinate of off-center image.
;    Y2:   Top y coordinate of off-center image.
;    P:    STRUCTURE image specs.
;    IMG:  Image frame.
;    RWIN: Limb window of the centered image.
;    N:    Range over which the absolute difference of the limb windows is
;          computed.
;
; OUTPUTS:
;    DXY:  Offsets in x or y from old center coordinates.
;
; KEYWORDS:
;    None.
;
; MODIFICATION HISTORY:
;    1997-12-05: Anders Johanneson, Big Bear Solar Observatory
;        Original verion of LIMBCOMPX.PRO and LIMBCOMPY.PRO.
;    1999-12-14: Carsten Denker, Big Bear Solar Observatory
;        Intergration in the new BBSO archiving system, variables are passed
;        as structures, routine now handles all 4 limb windows assuming that
;        the larger dimension of the window is tangential to the solar limb,
;        change from WHERE to MIN function to find the location of the minimum
;        absolute difference.
;
;-

a = FLTARR( n * 2 + 1 )
FOR i = -n, n DO BEGIN

    ;----- limb 2 or 4
    IF ( x2 - x1 ) LT ( y2 - y1 ) THEN BEGIN
        xx1 = ( x1 + i ) > 0
        xx2 = ( x2 + i ) < ( p.nx - 1 )
        cwin = img ( xx1 : xx2, y1 : y2 )

    ;----- limb 1 or 3
    ENDIF ELSE BEGIN
        yy1 = ( y1 + i ) > 0
        yy2 = ( y2 + i ) < ( p.ny - 1 )
        cwin = img ( x1 : x2, yy1 : yy2 )
    ENDELSE
    a( i + n ) =  TOTAL( ABS( cwin - rwin ) )
ENDFOR
m = MIN( a, index )



RETURN, index - n

END






