Fortran 90 help reading from file

  • Context: Fortran 
  • Thread starter Thread starter jospaghetti
  • Start date Start date
  • Tags Tags
    File Fortran Reading
Click For Summary
SUMMARY

This discussion focuses on reading formatted data files in Fortran 90, specifically how to extract variable values into program variables. The user, Jospaghetti, seeks assistance in reading values such as ncols, nrows, xllcorner, yllcorner, cellsize, and NODATA from a file named DEM.in. The solution provided involves using the OPEN and READ statements to correctly read the data into defined variables, with an emphasis on using an implicit in-line DO loop for reading multiple rows of data.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with file handling in Fortran (OPEN and READ statements)
  • Knowledge of variable types in Fortran (INTEGER and REAL)
  • Basic programming concepts such as loops and error handling
NEXT STEPS
  • Learn about Fortran 90 file I/O operations in detail
  • Explore the use of implicit and explicit loops in Fortran
  • Study error handling techniques in Fortran programs
  • Investigate advanced data structures in Fortran for handling larger datasets
USEFUL FOR

Novice programmers, students learning Fortran 90, and anyone interested in file handling and data extraction techniques in Fortran.

jospaghetti
Messages
2
Reaction score
0
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
 
Technology news on Phys.org
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
 
Thank you! Really helpful!
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K