Finds contiguous True regions of the boolean array 'condition'.

    Args
    ----
    condition: numpy.ndarray, dtype bool
        boolean mask array, but can pass the condition itself (e.g. x > 5)

    Returns
    -------
    start_ind: numpy.ndarray, dtype int
        array with the start indices for each contiguous region
    stop_ind: numpy.ndarray, dtype int
        array with the stop indices for each contiguous region

    Notes
    -----
    This function is adpated from Joe Kington's answer on StackOverflow:
    http://stackoverflow.com/a/4495197/943773

def contiguous_regions(condition):
    '''Finds contiguous True regions of the boolean array 'condition'.

    Args
    ----
    condition: numpy.ndarray, dtype bool
        boolean mask array, but can pass the condition itself (e.g. x > 5)

    Returns
    -------
    start_ind: numpy.ndarray, dtype int
        array with the start indices for each contiguous region
    stop_ind: numpy.ndarray, dtype int
        array with the stop indices for each contiguous region

    Notes
    -----
    This function is adpated from Joe Kington's answer on StackOverflow:
    http://stackoverflow.com/a/4495197/943773
    '''
    import numpy

    if condition.ndim > 1:
        raise IndexError('contiguous_regions(): condition must be 1-D')

    # Find the indicies of changes in 'condition'
    idx = numpy.diff(condition).nonzero()[0]

    if condition[0]:
        # If the start of condition is True prepend a 0
        idx = numpy.r_[0, idx]

    if condition[-1]:
        # If the end of condition is True, append the length of the array
        idx = numpy.r_[idx, condition.size] # Edit

    # Reshape the result into two columns
    idx.shape = (-1,2)

    # We need to start things after the change in 'condition'. Therefore,
    # we'll shift the index by 1 to the right.
    start_ind = idx[:,0] + 1
    # keep the stop ending just before the change in condition
    stop_ind  = idx[:,1]

    # remove reversed or same start/stop index
    good_vals = (stop_ind-start_ind) > 0
    start_ind = start_ind[good_vals]
    stop_ind = stop_ind[good_vals]

    return start_ind, stop_ind