def split_rayfans(P, chunksizes, S=None):
    """Split P and S from a raytrace history back into the input chunks.

    The typical pattern would be to generate N fans for N fields, concat them,
    trace them all, then re-split them for plotting, so the colors may be made
    different.

    Parameters
    ----------
    P : numpy.ndarray
        ndarray of shape (N, 3)
        position (or position history)
    chunksizes : iterable of int
        the size of each chunk of P
        for example, if P was made by concat_rayfans(N=3,N=1,N=5)
        then chunksizes=[3,1,5]
    S : numpy.ndarray
        ndarray of shape (N, 3)
        direction cosine (or history of)

    Returns
    -------
    list, list
        views into P (and S, if not None) that are just the requested chunks

    """
    expected_N = sum(chunksizes)
    if P.size[0] != expected_N:
        return ValueError('P is not sum(chunksizes) in length')

    ps = []
    low = 0
    for size in chunksizes:
        chunk = P[low:low+size]
        ps.append(chunk)
        low += size

    if S is None:
        return ps

    ss = []
    low = 0
    for size in chunksizes:
        chunk = S[low:low+size]
        ss.append(chunk)
        low += size

    return ps, ss