code: def aggregate(d, y_size, x_size):
          
          if d.ndim != 2:
              # we can't guarantee what blocks we are getting and how
              # it should be reshaped to do the averaging.
              raise ValueError("Can't aggregrate (reduce) data arrays with "
                               "more than 2 dimensions.")
          if not (x_size.is_integer() and y_size.is_integer()):
              raise ValueError("Aggregation factors are not integers")
          for agg_size, chunks in zip([y_size, x_size], d.chunks):
              for chunk_size in chunks:
                  if chunk_size % agg_size != 0:
                      raise ValueError("Aggregation requires arrays with "
                                       "shapes and chunks divisible by the "
                                       "factor")
  
          new_chunks = (tuple(int(x / y_size) for x in d.chunks[0]),
                        tuple(int(x / x_size) for x in d.chunks[1]))
          return da.core.map_blocks(_mean, d, y_size, x_size, dtype=d.dtype, chunks=new_chunks)
docs: Average every 4 elements (2x2) in a 2D array