Hello
I am trying to set a code to read the grid data from an ESRI ascii raster file and output the data as X,Y,Z. The file structure is simple, but not found an easy solution to extract the header variable names and associated values on read.
This snippet works extracting the header but rather convoluted, used real*8 but real(kind=2) is also valid:
PROGRAM ReadHeader
IMPLICIT NONE
! Define the structure for the grid header
type ascii_raster
integer :: ncols
integer :: nrows
REAL*8 :: xllcorner
REAL*8 :: yllcorner
REAL*8 :: cellsize
REAL*8 :: NODATA_value
END type ascii_raster
TYPE(ascii_raster) :: grid
CHARACTER :: label
! Open the file for reading
OPEN(UNIT=10, FILE='header2.asc', STATUS='OLD')
! Read data from the file into the structure fields
READ(10,*) label, grid%ncols
READ(10,*) label, grid%nrows
READ(10,*) label, grid%xllcorner
READ(10,*) label, grid%yllcorner
READ(10,*) label, grid%cellsize
READ(10,*) label, grid%NODATA_value
! Close the file after reading
CLOSE(10)
! Print the values read to verify
PRINT *, 'ncols = ', grid%ncols
PRINT *, 'nrows = ', grid%nrows
PRINT *, 'xllcorner = ', grid%xllcorner
PRINT *, 'yllcorner = ', grid%yllcorner
PRINT *, 'cellsize = ', grid%cellsize
PRINT *, 'NODATA_value = ', grid%NODATA_value
END PROGRAM ReadHeader
The input file is small as a tester:
ncols 10
nrows 10
xllcorner 563435.00
yllcorner 5258305.00
cellsize 10.00
NODATA_value -9999.00
103 102 102 101 100 99 97 95 94 92
100 100 100 99 97 96 94 92 90 88
97 97 96 95 93 92 91 88 85 82
94 93 92 90 89 87 84 82 79 76
90 89 87 86 84 81 78 75 74 72
86 84 83 80 77 75 72 70 69 69
81 79 76 74 72 69 68 67 68 71
76 73 71 69 68 66 67 68 71 73
71 69 68 66 65 66 68 71 74 76
68 66 65 65 66 68 71 74 77 81
Is there a simpler methodology to extract the header variable names with their values and the zdata(ncols,nrows)?
Given the grid dimensions (ncols, nrows) is it an easy task to get the data into X,Y, Z ?
Ideally I want to build this into a subroutine where the only input is the data file, eg asc_2_xyz(filename, X, Y, Z) .
Thanks for any pointers
Lester