Fortran 90 help reading from file

  • Fortran
  • Thread starter jospaghetti
  • Start date
  • #1
Hi All

I'm completely new to Fortran 90 and I have to create a program that opens a formatted data file and reads in certain values into my program's variables. The variables are represented in the first six rows with ncols, nrows, xllcorner, yllcorner, cellsize and NODATA being the variable's labels. The format of the data file looks like this but the content of the variables will change depending on data file:

ncols 5
nrows 4
xllcorner 2000
yllcorner 3000
cellsize 10
NODATA -9999
6 7 8 9 10
7 8 9 10 11
8 9 10 11 12
9 10 11 12 13

Ncols, nrows will always be integers but the other variables are most likely going to be double precision numbers.

So far I have programmed the code that opens the file (the file is supposed to be called DEM.in):

integer :: st

open(unit=10,file='DEM.in',status='old',action='read',iostat=st)
if (st>0) then
print*,'Error opening file. File load status:', st
stop
else
print*, 'File openend correctly'
end if

Can anyone help me or indicate me into the right direction on how I can program the code that will read in the values in my data file (5, 4, 2000, 3000, 10, -9999) and write them into my program's variables (ncols, nrows, xllcorner, yllcorner, cellsize, NODATA)? Any help is greatly appreciated; remember I'm a novice with Fortran so labelling any code will be extremely useful.

Thanks very much guys

Jospaghetti
 

Answers and Replies

  • #2
minger
Science Advisor
1,496
2
As long as your variables are defined correctly, then you can just do an unformatted read which will read in the variables correctly.
Code:
PROGRAM
IMPLICIT NONE
INTEGER :: ncols,nrows
REAL(kind=8) :: xllcorner,yllcorner
OPEN(11,file='file.x',form='formatted')
READ(11,*) ncols
READ(11,*) nrows
!--read the rest
DO i=1,nrows
 READ(11,*) (data(i,j),j=1,ncols) !--   <- implicit in-line do loop
END DO

END PROGRAM
 
  • #3
Thank you! Really helpful!
 

Related Threads on Fortran 90 help reading from file

Replies
9
Views
18K
Replies
2
Views
3K
Replies
12
Views
264
Replies
7
Views
1K
Replies
1
Views
5K
  • Last Post
Replies
4
Views
2K
Replies
1
Views
16K
Replies
5
Views
18K
Replies
5
Views
4K
Replies
6
Views
1K
Top