;function return_centroid
;
;PURPOSE:
;	To return a centroid to a small rectangular region that is known to 
;	contain a star. 
;
;INPUTS:
;	Image - The 2d image containing the star
;KEYWORDS:
;	REMOVEBAD - Set this to zero pixels with negative values
;
;OUTPUTS: A 4 element array
;	  1 - X Centroid Coord
;	  2 - Y Centroid Coord
;	  3 - Second Moment in X
;	  4 - Second Moment in Y
function return_centroid, Image, RemoveBad = RemoveBad

If N_Elements(RemoveBad) eq 0 then RemoveBad = 0
TmpImage = Float(Image)
If RemoveBad gt 0 then begin
  BadPix   = where(TmpImage lt 0, NumBad)
  If NumBad gt 0 then TmpImage(BadPix) = 0
EndIf

ImSize   = Size(TmpImage)
XCoords  = Indgen(ImSize(1),ImSize(2)) mod ImSize(1) 
XCoords  = XCoords - Mean(XCoords)
YCoords  = Indgen(ImSize(1),ImSize(2)) / ImSize(1) 
YCoords  = YCoords - Mean(YCoords)

Flux = Total(TmpImage)
X    = Total(XCoords*TmpImage)/Flux
Y    = Total(YCoords*TmpImage)/Flux
X2   = Total((XCoords-X)^2*TmpImage)/Flux
Y2   = Total((YCoords-Y)^2*TmpImage)/Flux
XY2  = Total((XCoords-X)*(YCoords-Y)*TmpImage)/Flux
E1   = (X2-Y2)/(X2+Y2)
E2   = (2*XY2)/(X2+Y2)
E    = sqrt(E1^2+E2^2)

return, [X, Y, X2, Y2, XY2, E1, E2, E]

end
