


FUNCTION CENTER_ABS_DIFF, img, p
;+
; NAME:
;    CENTER_ABS_DIFF
;
; PURPOSE:
;    Function to improve centering of large full disk images with absolute
;    difference method.
;
; CATEGORY:
;    BBSO Archiving System.
;    Image Processing.
;
; CALLING SEQUENCE:
;    cc = CENTER_ABS_DIFF ( img, p )
;
; INPUTS:
;    IMG: Image frame.
;    P:   STRUCTURE image specs.
;
; OUTPUTS:
;    CC:  New center coordinates.
;
; KEYWORDS:
;    None.
;
; MODIFICATION HISTORY:
;    1997-01-01: Anders Johanneson, Big Bear Solar Observatory
;        Original verion of REALCENTER.PRO.
;    1999-12-13: Carsten Denker, Big Bear Solar Observatory
;        Intergration in the new BBSO archiving system, variables are passed
;        as structures.
;    2000-02-24: J. Varsik, BBSO
;        Adjust for size of Yunnan image.
;
;-

    ;----- definitions
n1 = -10
n2 =  10
hs = FLTARR( n2 - n1 + 1 )
vs = FLTARR( n2 - n1 + 1 )

    ;----- compute radius INTEGER
r = FIX ( ROUND( ( p.xd + p.yd ) / 4. ) )
if (p.xd < 1500) then r = FIX ( ROUND( p.yd / 2. ))
if (p.yd < 1500) then r = FIX ( ROUND (p.xd / 2. ))



    ;----- horizontal shift
FOR i = n1, n2 DO BEGIN

    ;----- absolute difference
;   a = img( p.xc - r - 30 + i : p.xc - r + 30 + i, p.yc - 60 : p.yc + 60 )
;   b = img( p.xc + r - 30 : p.xc + r + 30, p.yc - 60 : p.yc + 60 )
   yysize = SIZE(img)
   yysx = yysize(1)
   yysy = yysize(2)
   yyx1 = p.xc - r - 30 + i
   if (yyx1 < 0) then yya1 = 0 else yya1 = yyx1
   yyx2 = p.xc - r + 30 + i
   if (yyx2 > yysx - 1) then yya2 = yysx - 1 else yya2 = yyx2
   yyy1 = p.yc - 60
   if (yyy1 < 0) then yyb1 = 0 else yyb1 = yyy1
   yyy2 = p.yc + 60
   if (yyy2 > yysy - 1) then yyb2 = yysy - 1 else yyb2 = yyy2
   a = img( yya1 : yya2, yyb1 : yyb2)
   yyx1 = p.xc + r - 30
   if (yyx1 < 0) then yya1 = 0 else yya1 = yyx1
   yyx2 = p.xc + r + 30
   if (yyx2 > yysx - 1) then yya2 = yysx - 1 else yya2 = yyx2
   yyy1 = p.yc - 60
   if (yyy1 < 0) then yyb1 = 0 else yyb1 = yyy1
   yyy2 = p.yc + 60
   if (yyy2 > yysy - 1) then yyb2 = yysy - 1 else yyb2 = yyy2
   b = img( yya1 : yya2, yyb1 : yyb2)

   a_prof = TOTAL( a, 2 )
   b_prof = REVERSE( TOTAL( b, 2 ) )
   hs( i - n1 ) = TOTAL( ABS( a_prof - b_prof ) )
ENDFOR

    ;----- centroid
prof = MAX( hs ) - hs
prof = prof / MAX( prof )
prof = prof > 0.8 - 0.8
index = FINDGEN( N_ELEMENTS( prof ) ) + n1
dx = TOTAL( index * prof ) / TOTAL( prof ) / 2.



    ;----- vertical shift
FOR i = n1, n2 DO BEGIN

    ;----- absolute difference
   a = img( p.xc - 60 : p.xc + 60, p.yc - r - 30 + i : p.yc - r + 30 + i )
   b = img( p.xc - 60 : p.xc + 60, p.yc + r - 30 : p.yc + r + 30 )
   a_prof = TOTAL( a, 1 )
   b_prof = REVERSE( TOTAL( b, 1 ) )
   vs( i - n1 ) = TOTAL( ABS( a_prof - b_prof ) )
ENDFOR

    ;----- centroid
prof = MAX( vs ) - vs
prof = prof / MAX( prof )
prof = prof > 0.8 - 0.8
index = FINDGEN( N_ELEMENTS( prof ) ) + n1
dy = TOTAL( index * prof ) / TOTAL( prof ) / 2.



RETURN, FIX( ROUND( [ p.xc + dx, p.yc + dy ] ) )

END



