import math


def flip_phi(phi):
    """
    Correct phi to within the 0 <= to <= 2pi range

    :return: phi in >=0 and <=2Pi
    """
    Pi = math.pi
    if phi < 0:
        phi_out = phi + (2 * Pi)
    elif phi > (2 * Pi):
        phi_out = phi - (2 * Pi)
    else:
        phi_out = phi
    return phi_out