# Path: PySpectrograph/Utilities/fit.py
# class interfit(curfit):
# 
#     """Given an x and y data arrays,  find the best fitting curve.
#         After the initial fit, iterate on the solution to reject any
#         points which are away from the solution
# 
#     * x - list or array of x data
#     * y - list or array of y data
#     * yerr - error on y data
#     * coef - Initial coefficients for fit
#     * function - function to be fit to the data:
#                  options include polynomial, legendre, chebyshev, or spline
#     * order - order of the function that is fit
#     * thresh - threshold for rejection
#     * niter - number of times to iterate
# 
# 
#     """
# 
#     def __init__(self, x, y, yerr=None, coef=None, function='poly', order=3,
#                  thresh=3, niter=5):
#         # set up the variables
#         self.x_orig = x
#         self.y_orig = y
#         self.npts = len(self.x_orig)
#         if yerr is None:
#             self.yerr_orig = np.ones(self.npts)
#         else:
#             self.yerr_orig = yerr
# 
#         self.order = order
#         self.thresh = thresh
#         self.niter = niter
# 
#         self.set_func(function)
#         self.set_coef(coef)
#         self.set_mask(init=True)
#         self.set_arrays(self.x_orig, self.y_orig, self.mask, err=self.yerr_orig)
# 
#     def set_mask(self, init=False):
#         """Set the mask according to the values for rejecting points"""
#         self.mask = np.ones(self.npts, dtype=bool)
#         if init:
#             return
# 
#         # difference the arrays
#         diff = self.y_orig - self(self.x_orig)
#         sigma = self.sigma(self.x, self.y)
#         self.mask = (abs(diff) < self.thresh * sigma)
# 
#     def set_arrays(self, x, y, mask, err=None):
#         """set the arrays using a mask"""
#         self.x = x[mask]
#         self.y = y[mask]
#         if err is not None:
#             self.yerr = err[mask]
# 
#     def interfit(self):
#         """Fit a function and then iterate it to reject possible outlyiers"""
#         self.fit()
#         for i in range(self.niter):
#             self.set_mask()
#             self.set_arrays(self.x_orig, self.y_orig, self.mask, err=self.yerr_orig)
#             self.fit()


from scipy import optimize
from PySpectrograph.Utilities.fit import interfit
"""LineFit is a task describing the functional form for transforming
pixel position to wavelength by fitting a model spectrum.  The inputs for this task
are an observed spectrum and a calibrated spectrum
and the corresponding wavelength.  The user selects an input functional form and
order for that form.  The task then calculates the coefficients for that form.
Possible options for the wavelength solution include polynomial, legendre, spline.

HISTORY
20090915  SMC Initially Written by SM Crawford
20101018  SMC Updated to use interfit

LIMITATIONS

"""





# Path: PySpectrograph/Utilities/fit.py
# class interfit(curfit):
# 
#     """Given an x and y data arrays,  find the best fitting curve.
#         After the initial fit, iterate on the solution to reject any
#         points which are away from the solution
# 
#     * x - list or array of x data
#     * y - list or array of y data
#     * yerr - error on y data
#     * coef - Initial coefficients for fit
#     * function - function to be fit to the data:
#                  options include polynomial, legendre, chebyshev, or spline
#     * order - order of the function that is fit
#     * thresh - threshold for rejection
#     * niter - number of times to iterate
# 
# 
#     """
# 
#     def __init__(self, x, y, yerr=None, coef=None, function='poly', order=3,
#                  thresh=3, niter=5):
#         # set up the variables
#         self.x_orig = x
#         self.y_orig = y
#         self.npts = len(self.x_orig)
#         if yerr is None:
#             self.yerr_orig = np.ones(self.npts)
#         else:
#             self.yerr_orig = yerr
# 
#         self.order = order
#         self.thresh = thresh
#         self.niter = niter
# 
#         self.set_func(function)
#         self.set_coef(coef)
#         self.set_mask(init=True)
#         self.set_arrays(self.x_orig, self.y_orig, self.mask, err=self.yerr_orig)
# 
#     def set_mask(self, init=False):
#         """Set the mask according to the values for rejecting points"""
#         self.mask = np.ones(self.npts, dtype=bool)
#         if init:
#             return
# 
#         # difference the arrays
#         diff = self.y_orig - self(self.x_orig)
#         sigma = self.sigma(self.x, self.y)
#         self.mask = (abs(diff) < self.thresh * sigma)
# 
#     def set_arrays(self, x, y, mask, err=None):
#         """set the arrays using a mask"""
#         self.x = x[mask]
#         self.y = y[mask]
#         if err is not None:
#             self.yerr = err[mask]
# 
#     def interfit(self):
#         """Fit a function and then iterate it to reject possible outlyiers"""
#         self.fit()
#         for i in range(self.niter):
#             self.set_mask()
#             self.set_arrays(self.x_orig, self.y_orig, self.mask, err=self.yerr_orig)
#             self.fit()


# Path: PySpectrograph/WavelengthSolution/LineFit.py
from scipy import optimize
from PySpectrograph.Utilities.fit import interfit

"""LineFit is a task describing the functional form for transforming
pixel position to wavelength by fitting a model spectrum.  The inputs for this task
are an observed spectrum and a calibrated spectrum
and the corresponding wavelength.  The user selects an input functional form and
order for that form.  The task then calculates the coefficients for that form.
Possible options for the wavelength solution include polynomial, legendre, spline.

HISTORY
20090915  SMC Initially Written by SM Crawford
20101018  SMC Updated to use interfit

LIMITATIONS

"""





class LineFit(interfit):