idlastro / DAOPHOT-Type Photometry Procedures: GCNTRD

[Source code]

NAME
GCNTRD
PURPOSE
Compute the stellar centroid by Gaussian fits to marginal X,Y, sums 
XPLANATION
GCNTRD uses the DAOPHOT "FIND" centroid algorithm by fitting Gaussians
to the marginal X,Y distributions.     User can specify bad pixels 
(either by using the MAXGOOD keyword or setting them to NaN) to be
ignored in the fit.    Pixel values are weighted toward the center to
avoid contamination by neighboring stars. 
CALLING SEQUENCE
GCNTRD, img, x, y, xcen, ycen, [ fwhm , /SILENT, /DEBUG, MAXGOOD = ,
                     /KEEPCENTER ]
INPUTS
IMG - Two dimensional image array
X,Y - Scalar or vector integers giving approximate stellar center
OPTIONAL INPUT
FWHM - floating scalar; Centroid is computed using a box of half
        width equal to 1.5 sigma = 0.637* FWHM.  GCNTRD will prompt
        for FWHM if not supplied
OUTPUTS
XCEN - the computed X centroid position, same number of points as X
YCEN - computed Y centroid position, same number of points as Y
Values for XCEN and YCEN will not be computed if the computed
centroid falls outside of the box, or if there are too many bad pixels,
or if the best-fit Gaussian has a negative height.   If the centroid 
cannot be computed, then a  message is displayed (unless /SILENT is 
set) and XCEN and YCEN are set to -1.
OPTIONAL OUTPUT KEYWORDS
MAXGOOD=  Only pixels with values less than MAXGOOD are used to in
        Gaussian fits to determine the centroid.    For non-integer
        data, one can also flag bad pixels using NaN values.
/SILENT - Normally GCNTRD prints an error message if it is unable
        to compute the centroid.   Set /SILENT to suppress this.
/DEBUG - If this keyword is set, then GCNTRD will display the subarray
        it is using to compute the centroid.
/KeepCenter  By default, GCNTRD first convolves a small region around 
       the supplied position with a lowered Gaussian filter, and then 
       finds the maximum pixel in a box centered on the input X,Y 
       coordinates, and then extracts a new box about this maximum 
       pixel.   Set the /KeepCenter keyword  to skip the convolution 
       and finding the maximum pixel, and instead use a box 
       centered on the input X,Y coordinates.                          
PROCEDURE
Unless /KEEPCENTER is set, a small area around the initial X,Y is 
convolved with a Gaussian kernel, and the maximum pixel is found.
This pixel is used as the  center of a square, within 
which the centroid is computed as the Gaussian least-squares fit
to the  marginal sums in the X and Y directions. 
EXAMPLE
Find the centroid of a star in an image im, with approximate center
631, 48.    Assume that bad (saturated) pixels have a value of 4096 or
or higher, and that the approximate FWHM is 3 pixels.
IDL> GCNTRD, IM, 631, 48, XCEN, YCEN, 3, MAXGOOD = 4096       
MODIFICATION HISTORY
Written June 2004, W. Landsman  following algorithm used by P. Stetson 
      in DAOPHOT2.
Modified centroid computation (as in IRAF/DAOFIND) to allow shifts of
ore than 1 pixel from initial guess.    March 2008
irst perform Gaussian convolution prior to finding maximum pixel 
o smooth out noise  W. Landsman  Jan 2009
dius is 1.5 sigma
of pixels in side of convolution box
ntral X pixel
ntral Y pixel
eate the Gaussian convolution kernel in variable "g"
sk identifies valid pixels in convolution box
lue of c are now equal to distance to center
ke g into a Gaussian kernel
n fitting Gaussians to the marginal sums, pixels will arbitrarily be
ssigned weights ranging from unity at the corners of the box to
HALF^2 at the center (e.g. if NBOX = 5 or 7, the weights will be
1   2   3   4   3   2   1
2   4   6   8   6   4   2
3   6   9  12   9   6   3
4   8  12  16  12   8   4
3   6   9  12   9   6   3
2   4   6   8   6   4   2
1   2   3   4   3   2   1
espectively). This is done to desensitize the derived parameters to
ossible neighboring, brighter stars.
recompute convolution kernel
nvolution kernel now in c
op over number of points to centroid
ximum pixel value in BIGBOX
w many pixels have maximum value?
coordinate of Max pixel
coordinate of Max pixel
re than 1 pixel at maximum?
coordinate in original image array
coordinate in original image array
--------------------------------------------------------------------
heck *new* center location for range
dded by Hogg
--------------------------------------------------------------------
Extract subimage centered on maximum pixel
t least 3 points are needed in the partial sum to compute the Gaussian
entroid computation The centroid computation was modified in Mar 2008 and
ow differs from DAOPHOT which multiplies the correction dx by 1/(1+abs(dx)).
he DAOPHOT method is more robust (e.g. two different sources will not merge)
specially in a package where the centroid will be subsequently be
edetermined using PSF fitting. However, it is less accurate, and introduces
iases in the centroid histogram. The change here is the same made in the
RAF DAOFIND routine (see
ttp//iraf.net/article.php?story=7211&query=daofind )
X is the height of the best-fitting marginal Gaussian. If this is not
ositive then the centroid does not make sense
centroid in original array
w repeat computation for Y centroid
centroid in original array