def prewhiten_magseries(times, mags, errs,<EOL>whitenperiod,<EOL>whitenparams,<EOL>sigclip=<NUM_LIT>,<EOL>magsarefluxes=False,<EOL>plotfit=None,<EOL>plotfitphasedlconly=True,<EOL>rescaletomedian=True):
stimes, smags, serrs = sigclip_magseries(times, mags, errs,<EOL>sigclip=sigclip,<EOL>magsarefluxes=magsarefluxes)<EOL>median_mag = np.median(smags)<EOL>mintime = np.min(stimes)<EOL>iphase = (<EOL>(stimes - mintime)/whitenperiod -<EOL>np.floor((stimes - mintime)/whitenperiod)<EOL>)<EOL>phasesortind = np.argsort(iphase)<EOL>phase = iphase[phasesortind]<EOL>pmags = smags[phasesortind]<EOL>perrs = serrs[phasesortind]<EOL>ptimes = stimes[phasesortind]<EOL>wmags = pmags - _fourier_func(whitenparams, phase, pmags)<EOL>wtimeorder = np.argsort(ptimes)<EOL>wtimes = ptimes[wtimeorder]<EOL>wphase = phase[wtimeorder]<EOL>wmags = wmags[wtimeorder]<EOL>werrs = perrs[wtimeorder]<EOL>if rescaletomedian:<EOL><INDENT>wmags = wmags + median_mag<EOL><DEDENT>returndict = {'<STR_LIT>':wtimes,  <EOL>'<STR_LIT>':wphase,<EOL>'<STR_LIT>':wmags,<EOL>'<STR_LIT>':werrs,<EOL>'<STR_LIT>':whitenparams,<EOL>'<STR_LIT>':whitenperiod}<EOL>if plotfit and (isinstance(plotfit, str) or isinstance(plotfit, Strio)):<EOL><INDENT>if plotfitphasedlconly:<EOL><INDENT>plt.figure(figsize=(<NUM_LIT:10>,<NUM_LIT>))<EOL><DEDENT>else:<EOL><INDENT>plt.figure(figsize=(<NUM_LIT:16>,<NUM_LIT>))<EOL><DEDENT>if plotfitphasedlconly:<EOL><INDENT>plt.subplot(<NUM_LIT>)<EOL>plt.plot(phase,pmags,<EOL>marker='<STR_LIT:.>',<EOL>color='<STR_LIT:k>',<EOL>linestyle='<STR_LIT:None>',<EOL>markersize=<NUM_LIT>,<EOL>markeredgewidth=<NUM_LIT:0>)<EOL>if not magsarefluxes:<EOL><INDENT>plt.gca().invert_yaxis()<EOL>plt.ylabel('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.ylabel('<STR_LIT>')<EOL><DEDENT>plt.xlabel('<STR_LIT>')<EOL>plt.title('<STR_LIT>')<EOL>plt.subplot(<NUM_LIT>)<EOL>plt.plot(wphase,wmags,<EOL>marker='<STR_LIT:.>',<EOL>color='<STR_LIT:g>',<EOL>linestyle='<STR_LIT:None>',<EOL>markersize=<NUM_LIT>,<EOL>markeredgewidth=<NUM_LIT:0>)<EOL>if not magsarefluxes:<EOL><INDENT>plt.gca().invert_yaxis()<EOL>plt.ylabel('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.ylabel('<STR_LIT>')<EOL><DEDENT>plt.xlabel('<STR_LIT>')<EOL>plt.title('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.subplot(<NUM_LIT>)<EOL>plt.plot(stimes,smags,<EOL>marker='<STR_LIT:.>',<EOL>color='<STR_LIT:k>',<EOL>linestyle='<STR_LIT:None>',<EOL>markersize=<NUM_LIT>,<EOL>markeredgewidth=<NUM_LIT:0>)<EOL>if not magsarefluxes:<EOL><INDENT>plt.gca().invert_yaxis()<EOL>plt.ylabel('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.ylabel('<STR_LIT>')<EOL><DEDENT>plt.xlabel('<STR_LIT>')<EOL>plt.title('<STR_LIT>')<EOL>plt.subplot(<NUM_LIT>)<EOL>plt.plot(wtimes,wmags,<EOL>marker='<STR_LIT:.>',<EOL>color='<STR_LIT:g>',<EOL>linestyle='<STR_LIT:None>',<EOL>markersize=<NUM_LIT>,<EOL>markeredgewidth=<NUM_LIT:0>)<EOL>if not magsarefluxes:<EOL><INDENT>plt.gca().invert_yaxis()<EOL>plt.ylabel('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.ylabel('<STR_LIT>')<EOL><DEDENT>plt.xlabel('<STR_LIT>')<EOL>plt.title('<STR_LIT>' % whitenperiod)<EOL>plt.subplot(<NUM_LIT>)<EOL>plt.plot(phase,pmags,<EOL>marker='<STR_LIT:.>',<EOL>color='<STR_LIT:k>',<EOL>linestyle='<STR_LIT:None>',<EOL>markersize=<NUM_LIT>,<EOL>markeredgewidth=<NUM_LIT:0>)<EOL>if not magsarefluxes:<EOL><INDENT>plt.gca().invert_yaxis()<EOL>plt.ylabel('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.ylabel('<STR_LIT>')<EOL><DEDENT>plt.xlabel('<STR_LIT>')<EOL>plt.title('<STR_LIT>')<EOL>plt.subplot(<NUM_LIT>)<EOL>plt.plot(wphase,wmags,<EOL>marker='<STR_LIT:.>',<EOL>color='<STR_LIT:g>',<EOL>linestyle='<STR_LIT:None>',<EOL>markersize=<NUM_LIT>,<EOL>markeredgewidth=<NUM_LIT:0>)<EOL>if not magsarefluxes:<EOL><INDENT>plt.gca().invert_yaxis()<EOL>plt.ylabel('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>plt.ylabel('<STR_LIT>')<EOL><DEDENT>plt.xlabel('<STR_LIT>')<EOL>plt.title('<STR_LIT>')<EOL><DEDENT>plt.tight_layout()<EOL>plt.savefig(plotfit, format='<STR_LIT>', pad_inches=<NUM_LIT:0.0>)<EOL>plt.close()<EOL>if isinstance(plotfit, str) or isinstance(plotfit, Strio):<EOL><INDENT>returndict['<STR_LIT>'] = plotfit<EOL><DEDENT><DEDENT>return returndict<EOL>
Removes a periodic sinusoidal signal generated using whitenparams from
    the input magnitude time series.

    Parameters
    ----------

    times,mags,errs : np.array
        The input mag/flux time-series to prewhiten.

    whitenperiod : float
        The period of the sinusoidal signal to remove.

    whitenparams : list of floats
        This contains the Fourier amplitude and phase coefficients of the
        sinusoidal signal to remove::

            [ampl_1, ampl_2, ampl_3, ..., ampl_X,
             pha_1, pha_2, pha_3, ..., pha_X]

        where `X` is the Fourier order. These are usually the output of a
        previous Fourier fit to the light curve (from
        :py:func:`astrobase.lcfit.sinusoidal.fourier_fit_magseries` for
        example).

    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 showing the effect
        of the pre-whitening on the mag/flux time-series and write the plot to
        the path specified here.

    plotfitphasedlconly : bool
        If True, will plot only the phased LC for showing the effect of
        pre-whitening, and skip plotting the unphased LC.

    rescaletomedian : bool
        If this is True, then we add back the constant median term of the
        magnitudes to the final pre-whitened mag series.

    Returns
    -------

    dict
        Returns a dict of the form::

            {'wtimes':times array after pre-whitening,
             'wphase':phase array after pre-whitening,
             'wmags':mags array after pre-whitening,
             'werrs':errs array after pre-whitening,
             'whitenparams':the input pre-whitening params used,
             'whitenperiod':the input pre-whitening period used,
             'fitplotfile':the output plot file if plotfit was set}