def phase_magseries(times, mags, period, epoch, wrap=True, sort=True):
<EOL>finiteind = np.isfinite(mags) & np.isfinite(times)<EOL>finite_times = times[finiteind]<EOL>finite_mags = mags[finiteind]<EOL>magseries_phase = (<EOL>(finite_times - epoch)/period -<EOL>np.floor(((finite_times - epoch)/period))<EOL>)<EOL>outdict = {'<STR_LIT>':magseries_phase,<EOL>'<STR_LIT>':finite_mags,<EOL>'<STR_LIT>':period,<EOL>'<STR_LIT>':epoch}<EOL>if sort:<EOL><INDENT>sortorder = np.argsort(outdict['<STR_LIT>'])<EOL>outdict['<STR_LIT>'] = outdict['<STR_LIT>'][sortorder]<EOL>outdict['<STR_LIT>'] = outdict['<STR_LIT>'][sortorder]<EOL><DEDENT>if wrap:<EOL><INDENT>outdict['<STR_LIT>'] = np.concatenate((outdict['<STR_LIT>']-<NUM_LIT:1.0>,<EOL>outdict['<STR_LIT>']))<EOL>outdict['<STR_LIT>'] = np.concatenate((outdict['<STR_LIT>'],<EOL>outdict['<STR_LIT>']))<EOL><DEDENT>return outdict<EOL>
Phases a magnitude/flux time-series using a given period and epoch.

    The equation used is::

        phase = (times - epoch)/period - floor((times - epoch)/period)

    This phases the given magnitude timeseries using the given period and
    epoch. If wrap is True, wraps the result around 0.0 (and returns an array
    that has twice the number of the original elements). If sort is True,
    returns the magnitude timeseries in phase sorted order.

    Parameters
    ----------

    times,mags : np.array
        The magnitude/flux time-series values to phase using the provided
        `period` and `epoch`. Non-fiinite values will be removed.

    period : float
        The period to use to phase the time-series.

    epoch : float
        The epoch to phase the time-series. This is usually the time-of-minimum
        or time-of-maximum of some periodic light curve
        phenomenon. Alternatively, one can use the minimum time value in
        `times`.

    wrap : bool
        If this is True, the returned phased time-series will be wrapped around
        phase 0.0, which is useful for plotting purposes. The arrays returned
        will have twice the number of input elements because of this wrapping.

    sort : bool
        If this is True, the returned phased time-series will be sorted in
        increasing phase order.

    Returns
    -------

    dict
        A dict of the following form is returned::

            {'phase': the phase values,
             'mags': the mags/flux values at each phase,
             'period': the input `period` used to phase the time-series,
             'epoch': the input `epoch` used to phase the time-series}