def rebin(arr, new_shape):
    """
    Rebin 2D array arr to shape new_shape by averaging.

    inputs:
    arr: numpy array to reduce at the size new_shape
    new_shape: tuple of the wanted size as (width, height)

    output:
    numpy array of the reduced arr

    from https://scipython.com/blog/binning-a-2d-array-in-numpy/
    """

    shape = (new_shape[0], arr.shape[0] // new_shape[0],
             new_shape[1], arr.shape[1] // new_shape[1])
    return arr.reshape(shape).mean(-1).mean(1)