def fourier_fit_magseries(times, mags, errs, period,<EOL>fourierorder=None,<EOL>fourierparams=None,<EOL>sigclip=<NUM_LIT>,<EOL>magsarefluxes=False,<EOL>plotfit=False,<EOL>ignoreinitfail=True,<EOL>verbose=True):
stimes, smags, serrs = sigclip_magseries(times, mags, errs,<EOL>sigclip=sigclip,<EOL>magsarefluxes=magsarefluxes)<EOL>nzind = npnonzero(serrs)<EOL>stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind]<EOL>phase, pmags, perrs, ptimes, mintime = (<EOL>get_phased_quantities(stimes, smags, serrs, period)<EOL>)<EOL>if fourierorder and fourierorder > <NUM_LIT:0> and not fourierparams:<EOL><INDENT>fourieramps = [<NUM_LIT>] + [<NUM_LIT>]*(fourierorder - <NUM_LIT:1>)<EOL>fourierphas = [<NUM_LIT:0.1>] + [<NUM_LIT:0.1>]*(fourierorder - <NUM_LIT:1>)<EOL>fourierparams = fourieramps + fourierphas<EOL><DEDENT>elif not fourierorder and fourierparams:<EOL><INDENT>fourierorder = int(len(fourierparams)/<NUM_LIT:2>)<EOL><DEDENT>else:<EOL><INDENT>LOGWARNING('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>fourierorder = <NUM_LIT:3><EOL>fourieramps = [<NUM_LIT>] + [<NUM_LIT>]*(fourierorder - <NUM_LIT:1>)<EOL>fourierphas = [<NUM_LIT:0.1>] + [<NUM_LIT:0.1>]*(fourierorder - <NUM_LIT:1>)<EOL>fourierparams = fourieramps + fourierphas<EOL><DEDENT>if verbose:<EOL><INDENT>LOGINFO('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>' % (fourierorder,<EOL>len(phase),<EOL>period,<EOL>mintime))<EOL><DEDENT>initialfit = spminimize(_fourier_chisq,<EOL>fourierparams,<EOL>method='<STR_LIT>',<EOL>args=(phase, pmags, perrs))<EOL>if initialfit.success or ignoreinitfail:<EOL><INDENT>if verbose:<EOL><INDENT>LOGINFO('<STR_LIT>')<EOL><DEDENT>leastsqparams = initialfit.x<EOL>try:<EOL><INDENT>leastsqfit = spleastsq(_fourier_residual,<EOL>leastsqparams,<EOL>args=(phase, pmags))<EOL><DEDENT>except Exception as e:<EOL><INDENT>leastsqfit = None<EOL><DEDENT>if leastsqfit and leastsqfit[-<NUM_LIT:1>] in (<NUM_LIT:1>,<NUM_LIT:2>,<NUM_LIT:3>,<NUM_LIT:4>):<EOL><INDENT>finalparams = leastsqfit[<NUM_LIT:0>]<EOL>fitmags = _fourier_func(finalparams, phase, pmags)<EOL>fitchisq = npsum(<EOL>((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs)<EOL>)<EOL>fitredchisq = fitchisq/(len(pmags) - len(finalparams) - <NUM_LIT:1>)<EOL>if verbose:<EOL><INDENT>LOGINFO(<EOL>'<STR_LIT>' %<EOL>(fitchisq,fitredchisq)<EOL>)<EOL><DEDENT>if not magsarefluxes:<EOL><INDENT>fitmagminind = npwhere(fitmags == npmax(fitmags))<EOL><DEDENT>else:<EOL><INDENT>fitmagminind = npwhere(fitmags == npmin(fitmags))<EOL><DEDENT>if len(fitmagminind[<NUM_LIT:0>]) > <NUM_LIT:1>:<EOL><INDENT>fitmagminind = (fitmagminind[<NUM_LIT:0>][<NUM_LIT:0>],)<EOL><DEDENT>returndict = {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':fourierorder,<EOL>'<STR_LIT>':finalparams,<EOL>'<STR_LIT>':initialfit,<EOL>'<STR_LIT>':leastsqfit,<EOL>'<STR_LIT>':fitmags,<EOL>'<STR_LIT>':mintime,<EOL>'<STR_LIT>':ptimes[fitmagminind]<EOL>},<EOL>'<STR_LIT>':fitchisq,<EOL>'<STR_LIT>':fitredchisq,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':ptimes,<EOL>'<STR_LIT>':phase,<EOL>'<STR_LIT>':pmags,<EOL>'<STR_LIT>':perrs,<EOL>'<STR_LIT>':magsarefluxes<EOL>},<EOL>}<EOL>if plotfit and isinstance(plotfit, str):<EOL><INDENT>make_fit_plot(phase, pmags, perrs, fitmags,<EOL>period, mintime, mintime,<EOL>plotfit,<EOL>magsarefluxes=magsarefluxes)<EOL>returndict['<STR_LIT>'] = plotfit<EOL><DEDENT>return returndict<EOL><DEDENT>else:<EOL><INDENT>LOGERROR('<STR_LIT>')<EOL>return {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':fourierorder,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':initialfit,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None<EOL>},<EOL>'<STR_LIT>':npnan,<EOL>'<STR_LIT>':npnan,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':ptimes,<EOL>'<STR_LIT>':phase,<EOL>'<STR_LIT>':pmags,<EOL>'<STR_LIT>':perrs,<EOL>'<STR_LIT>':magsarefluxes<EOL>}<EOL>}<EOL><DEDENT><DEDENT>else:<EOL><INDENT>LOGERROR('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>% initialfit.message)<EOL>return {<EOL>'<STR_LIT>':'<STR_LIT>',<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':fourierorder,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':initialfit,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':None<EOL>},<EOL>'<STR_LIT>':npnan,<EOL>'<STR_LIT>':npnan,<EOL>'<STR_LIT>':None,<EOL>'<STR_LIT>':{<EOL>'<STR_LIT>':ptimes,<EOL>'<STR_LIT>':phase,<EOL>'<STR_LIT>':pmags,<EOL>'<STR_LIT>':perrs,<EOL>'<STR_LIT>':magsarefluxes<EOL>}<EOL>}<EOL><DEDENT>
This fits a Fourier series to a mag/flux time series.

    Parameters
    ----------

    times,mags,errs : np.array
        The input mag/flux time-series to fit a Fourier cosine series to.

    period : float
        The period to use for the Fourier fit.

    fourierorder : None or int
        If this is an int, will be interpreted as the Fourier order of the
        series to fit to the input mag/flux times-series. If this is None and
        `fourierparams` is specified, `fourierparams` will be used directly to
        generate the fit Fourier series. If `fourierparams` is also None, this
        function will try to fit a Fourier cosine series of order 3 to the
        mag/flux time-series.

    fourierparams : list of floats or None
        If this is specified as a list of floats, it must be of the form below::

            [fourier_amp1, fourier_amp2, fourier_amp3,...,fourier_ampN,
             fourier_phase1, fourier_phase2, fourier_phase3,...,fourier_phaseN]

        to specify a Fourier cosine series of order N. If this is None and
        `fourierorder` is specified, the Fourier order specified there will be
        used to construct the Fourier cosine series used to fit the input
        mag/flux time-series. If both are None, this function will try to fit a
        Fourier cosine series of order 3 to the input mag/flux time-series.

    sigclip : float or int or sequence of two floats/ints or None
        If a single float or int, a symmetric sigma-clip will be performed using
        the number provided as the sigma-multiplier to cut out from the input
        time-series.

        If a list of two ints/floats is provided, the function will perform an
        'asymmetric' sigma-clip. The first element in this list is the sigma
        value to use for fainter flux/mag values; the second element in this
        list is the sigma value to use for brighter flux/mag values. For
        example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma
        dimmings and greater than 3-sigma brightenings. Here the meaning of
        "dimming" and "brightening" is set by *physics* (not the magnitude
        system), which is why the `magsarefluxes` kwarg must be correctly set.

        If `sigclip` is None, no sigma-clipping will be performed, and the
        time-series (with non-finite elems removed) will be passed through to
        the output.

    magsarefluxes : bool
        If True, will treat the input values of `mags` as fluxes for purposes of
        plotting the fit and sig-clipping.

    plotfit : str or False
        If this is a string, this function will make a plot for the fit to the
        mag/flux time-series and writes the plot to the path specified here.

    ignoreinitfail : bool
        If this is True, ignores the initial failure to find a set of optimized
        Fourier parameters using the global optimization function and proceeds
        to do a least-squares fit anyway.

    verbose : bool
        If True, will indicate progress and warn of any problems.

    Returns
    -------

    dict
        This function returns a dict containing the model fit parameters, the
        minimized chi-sq value and the reduced chi-sq value. The form of this
        dict is mostly standardized across all functions in this module::

            {
                'fittype':'fourier',
                'fitinfo':{
                    'finalparams': the list of final model fit params,
                    'leastsqfit':the full tuple returned by scipy.leastsq,
                    'fitmags': the model fit mags,
                    'fitepoch': the epoch of minimum light for the fit,
                    ... other fit function specific keys ...
                },
                'fitchisq': the minimized value of the fit's chi-sq,
                'fitredchisq':the reduced chi-sq value,
                'fitplotfile': the output fit plot if fitplot is not None,
                'magseries':{
                    'times':input times in phase order of the model,
                    'phase':the phases of the model mags,
                    'mags':input mags/fluxes in the phase order of the model,
                    'errs':errs in the phase order of the model,
                    'magsarefluxes':input value of magsarefluxes kwarg
                }
            }

        NOTE: the returned value of 'fitepoch' in the 'fitinfo' dict returned by
        this function is the time value of the first observation since this is
        where the LC is folded for the fit procedure. To get the actual time of
        minimum epoch as calculated by a spline fit to the phased LC, use the
        key 'actual_fitepoch' in the 'fitinfo' dict.