See http://scipy-cookbook.readthedocs.io/items/Rebinning.html

    Note: integer division in the computation of 'factor' has been
    included to avoid the following runtime message:
    VisibleDeprecationWarning: using a non-integer number instead of
    an integer will result in an error in the future
    from __future__ import division

def rebin(a, *args):
    """See http://scipy-cookbook.readthedocs.io/items/Rebinning.html

    Note: integer division in the computation of 'factor' has been
    included to avoid the following runtime message:
    VisibleDeprecationWarning: using a non-integer number instead of
    an integer will result in an error in the future
    from __future__ import division

    """

    shape = a.shape
    len_shape = len(shape)
    factor = np.asarray(shape) // np.asarray(args)
    ev_list = ['a.reshape('] + \
              ['args[%d], factor[%d], ' % (i, i) for i in range(len_shape)] + \
              [')'] + ['.mean(%d)' % (i+1) for i in range(len_shape)]
    # print(''.join(ev_list))
    return eval(''.join(ev_list))