Converting grids to 1D arrays