def statistic_by_interval(values, interval, statistic='mean'):
    """
    Calculates statistics from values across an interval.

    For example, to sum monthly data up to annual data: statistic_by_interval(monthy_values, 12, 'sum') => one entry per year

    :param values: values (must have 3 dimensions)
    :param interval: interval over which to sum  (e.g., 12 for summing months to year)
    :param statistic: one of 'mean', 'sum'
    :return: a numpy array with shape (values.shape[0] / interval, values.shape[1], values.shape[2])
    """
    if not statistic in ('mean', 'sum'):
        raise ValueError('Unsupported statistic {0}'.format(statistic))
    assert len(values.shape) == 3
    assert values.shape[0] % interval == 0
    num_intervals = int(values.shape[0] / interval)
    temp = values.reshape(num_intervals, interval, values.shape[1], values.
        shape[2])
    if statistic == 'mean':
        return temp[:, :interval, :, :].mean(axis=1)
    elif statistic == 'sum':
        return temp[:, :interval, :, :].sum(axis=1)
