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 EllipticalCurveOfGrowth

# Create an artificial elliptical source
gmodel = Gaussian2D(42.1, 47.8, 52.4, 9.4, 4.7, np.deg2rad(42))
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 elliptical curve of growth
radii = np.arange(1, 40)
ecog = EllipticalCurveOfGrowth(data, xycen, radii, axis_ratio=0.5,
                               theta=np.deg2rad(42), error=error)
ecog.normalize(method='max')

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