def rebin2d(a, shape):
    """Rebin a 2d array to a smaller size.

    Parameters
    ----------
    a : 2d array
        Input array.
    shape : length-2 list or array
        Shape of output array.
    """
    sh = shape[0],a.shape[0]//shape[0],shape[1],a.shape[1]//shape[1]
    return a.reshape(sh).mean(-1).mean(1)