import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from astropy.modeling import models, fitting
x, y = np.loadtxt('data/fitting_data.txt', unpack=True)
plt.plot(x, y, 'o')
m1 = models.Linear1D()
m2 = models.Polynomial1D(2)
fitter = fitting.LevMarLSQFitter()
m1_new = fitter(m1, x, y)
m2_new = fitter(m2, x, y)
xfine = np.linspace(0, 100, 1000)
plt.plot(x, y, 'o')
plt.plot(xfine, m1_new(xfine), 'r-')
plt.plot(xfine, m2_new(xfine), 'b-')
from astropy.io import fits
hdu = fits.open('data/n57510ul.fits')[0]
flux = hdu.data
x = np.arange(len(flux))
plt.plot(flux)
m = models.Const1D() + models.Gaussian1D(mean=420)
m_new = fitter(m, x, flux)
m_new
plt.plot(x, flux)
plt.plot(x, m_new(x))