import matplotlib.pyplot as plt
import numpy as np
from astropy.modeling.models import Gaussian2D
from photutils.centroids import centroid_2dg
from photutils.datasets import make_noise_image
from photutils.profiles import EnsquaredCurveOfGrowth

# Create an artificial single source
gmodel = Gaussian2D(42.1, 47.8, 52.4, 4.7, 4.7, 0)
yy, xx = np.mgrid[0:100, 0:100]
data = gmodel(xx, yy)
bkg_sig = 2.1
noise = make_noise_image(data.shape, mean=0., stddev=bkg_sig, seed=0)
data += noise
error = np.zeros_like(data) + bkg_sig

# Find the source centroid
xycen = centroid_2dg(data)

# Create the ensquared curve of growth
half_sizes = np.arange(1, 26)
ecog = EnsquaredCurveOfGrowth(data, xycen, half_sizes, error=error)
ecog.normalize(method='max')

# Plot the ensquared curve of growth
fig, ax = plt.subplots(figsize=(8, 6))
ecog.plot(ax=ax)
ecog.plot_error(ax=ax)