def interval_data_from_time_series(data, use_left_endpoint=False):
    """
    This function converts time series data to piecewise constant
    interval data. A series of N time points and values yields
    N-1 intervals. By default, each interval takes the value of
    its right endpoint.

    In:
        ([t0, ...], {str(cuid): [value0, ...], },)
    Out:
        {str(cuid): {(t0, t1): value0 or value1, ...},}

    Arguments
    ---------
    data: tuple
        First entry is a list of time points, second entry is a dict
        mapping names each to a list of values at the corresponding time
        point
    use_left_endpoint: bool
        Optional. Indicates whether each interval should take the value
        of its left endpoint. Default is False, i.e. each interval takes
        the value of its right endpoint.

    Returns
    -------
    dict of dicts
        Maps names to a dict that maps interval-tuples each to the value
        over that interval

    """
    time, value_dict  = data
    n_t = len(time)
    if n_t == 1:
        t0 = time[0]
        return {
            name: {(t0, t0): values[0]}
            for name, values in value_dict.items()
        }
    else:
        # This covers the case of n_t > 1 and n_t == 0
        interval_data = {}
        intervals = [(time[i-1], time[i]) for i in range(1, n_t)]
        for name, values in value_dict.items():
            interval_values = [
                values[i-1] if use_left_endpoint else values[i]
                for i in range(1, n_t)
            ]
            interval_data[name] = dict(zip(intervals, interval_values))
        return interval_data