# Path: PySpectrograph/Utilities/fit.py
# def fit(function, parameters, y, x=None, var=1, warn=False):
#     def f(params):
#         i = 0
#         for p in parameters:
#             p.set(params[i])
#             i += 1
#         return (y - function(x)) / var
# 
#     if x is None:
#         x = np.arange(y.shape[0])
#     p = [param() for param in parameters]
#     return optimize.leastsq(f, p, full_output=1, warning=warn)


import numpy as np
from PySpectrograph.Utilities import fit as ft
       var--array with the variance values
    """

    def __init__(self, x, y, sgraph, xlen=3162, yval=0, order=3):

        # set up the variables
        self.x = x
        self.y = y
        self.sgraph = sgraph
        self.yval = yval
        self.xlen = xlen
        self.order = order
        self.function = 'model'

        # set the parameters
        self.reset_coef()

    def reset_coef(self):
        xpos = self.sgraph.detector.xpos
        ypos = self.yval + self.sgraph.detector.ypos
        fcam = self.sgraph.camera.focallength
        self.set_spcoef(fcam, xpos, ypos)
        self.spcoef = [self.fcam, self.xpos, self.ypos]
        self.set_ndcoef()

        # the total coefficient
        self.set_coef()

    def set_spcoef(self, fcam, xpos, ypos):
        self.fcam = ft.Parameter(fcam)

# Path: PySpectrograph/Utilities/fit.py
# def fit(function, parameters, y, x=None, var=1, warn=False):
#     def f(params):
#         i = 0
#         for p in parameters:
#             p.set(params[i])
#             i += 1
#         return (y - function(x)) / var
# 
#     if x is None:
#         x = np.arange(y.shape[0])
#     p = [param() for param in parameters]
#     return optimize.leastsq(f, p, full_output=1, warning=warn)


# Path: PySpectrograph/WavelengthSolution/ModelSolution.py
import numpy as np
from PySpectrograph.Utilities import fit as ft

       var--array with the variance values
    """

    def __init__(self, x, y, sgraph, xlen=3162, yval=0, order=3):

        # set up the variables
        self.x = x
        self.y = y
        self.sgraph = sgraph
        self.yval = yval
        self.xlen = xlen
        self.order = order
        self.function = 'model'

        # set the parameters
        self.reset_coef()

    def reset_coef(self):
        xpos = self.sgraph.detector.xpos
        ypos = self.yval + self.sgraph.detector.ypos
        fcam = self.sgraph.camera.focallength
        self.set_spcoef(fcam, xpos, ypos)
        self.spcoef = [self.fcam, self.xpos, self.ypos]
        self.set_ndcoef()

        # the total coefficient
        self.set_coef()

    def set_spcoef(self, fcam, xpos, ypos):
        self.fcam = ft.Parameter(fcam)

        self.xpos = ft.Parameter(xpos)