added the write/read grid function