Silverfrost Forums

Welcome to our forums

Reading header

21 Apr 2021 2:16 #27590

Hello,

Just a quick query. How do you read a header, store the values in variables (first 6 lines) and the rest of the data are z values:

ncols 120 nrows 120 xllcorner -5.000000 yllcorner -5.000000 cellsize 0.0166666675 NODATA_value -9999.000000 8.278781 7.810413 7.679254 7.670572 7.507757 7.489618 7.549579 7.712960 7.627653 6.931419 5.598992 4.097134 2.129512 0.308146 -1.081559 -1.578681 -1.929566 -2.209486 -1.927538 -0.758010 1.033233 2.433419 2.993177 2.734593 etc

From the header info, it will be possible to generate an X, Y, Z array to work from, for processing etc.

I'm sure this is a simple one, thanks for any pointers.

Thanks

Lester

21 Apr 2021 3:59 #27592

Modify to suit:

program readzvals
implicit none
integer :: ncols,nrows,i
real :: xllc,yllc,cellsize,nodata_value
real, allocatable, dimension(:,:) :: z
character(len=12) :: name
read(*,'(A5,1x,I3)')name,ncols
read(*,'(A5,1x,I3)')name,nrows
read(*,'(A9,1x,F9.6)')name,xllc
read(*,'(A9,1x,F9.6)')name,yllc
read(*,'(A8,1x,F11.6)')name,cellsize
read(*,'(A12,1x,F12.6)')name,nodata_value
print *,nrows,' rows and ',ncols,' columns'
allocate(z(nrows,ncols))
read(*,*)(z(i,:),i=1,nrows)
print *,z(4,4)
end program

Compile and link:

ftn95 readz.f90 /link

Put your data into a file, say, 'hdr.txt'.

Run the program:

R:\lang>readz < hdr.txt
            6 rows and            4 columns
     -1.57868
24 Apr 2021 1:24 #27622

Many thanks

Quoted from mecej4 Modify to suit:

program readzvals
implicit none
integer :: ncols,nrows,i
real :: xllc,yllc,cellsize,nodata_value
real, allocatable, dimension(:,:) :: z
character(len=12) :: name
read(*,'(A5,1x,I3)')name,ncols
read(*,'(A5,1x,I3)')name,nrows
read(*,'(A9,1x,F9.6)')name,xllc
read(*,'(A9,1x,F9.6)')name,yllc
read(*,'(A8,1x,F11.6)')name,cellsize
read(*,'(A12,1x,F12.6)')name,nodata_value
print *,nrows,' rows and ',ncols,' columns'
allocate(z(nrows,ncols))
read(*,*)(z(i,:),i=1,nrows)
print *,z(4,4)
end program

Compile and link:

ftn95 readz.f90 /link

Put your data into a file, say, 'hdr.txt'.

Run the program:

R:\lang>readz < hdr.txt
            6 rows and            4 columns
     -1.57868
Please login to reply.