def rebin_sum(a, shape, dtype): sh = shape[0],a.shape[0]//shape[0],shape[1],a.shape[1]//shape[1] return a.reshape(sh).sum(-1, dtype=dtype).sum(1, dtype=dtype)