Added function to write grid to file