Fortran 90 help reading from file

In summary, a person is seeking help with creating a program in Fortran 90 that can open a formatted data file and read in specific values into the program's variables. The person has already written code to open the file and is now looking for assistance with the code to read in the data and assign it to the correct variables. The conversation includes a sample of the data file and a sample of the code provided by another person to help with the task.
  • #1
jospaghetti
2
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
  • #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!
 

1. How do I read data from a file in Fortran 90?

To read data from a file in Fortran 90, you can use the READ statement followed by the variable(s) you want to store the data in and the specific format of the data. For example, if your data is in a comma-separated format, you can use READ(unit,*) var1, var2, var3 to read the data into the variables var1, var2, and var3.

2. What is the difference between READ and READ* in Fortran 90?

The READ statement reads data from a file into specific variables, while READ* reads data into an array. This means that READ* is more useful for reading in large amounts of data or data with varying lengths.

3. How do I open and close a file in Fortran 90?

You can open a file in Fortran 90 by using the OPEN statement followed by the unit number, the file name, and the STATUS flag (e.g. STATUS='OLD' to open an existing file). To close the file, use the CLOSE statement followed by the unit number. It is important to close files after you are finished using them to avoid data corruption.

4. Can I read/write to a specific line in a file in Fortran 90?

No, Fortran 90 does not have a built-in way to read/write to a specific line in a file. However, you can use the ACCESS='DIRECT' flag in the OPEN statement to access a specific record in a file using record numbers.

5. How do I handle errors while reading from a file in Fortran 90?

You can use the IOSTAT variable to check for errors while reading from a file. If the value of IOSTAT is positive, it indicates the number of bytes that were successfully read. If the value is negative, it indicates an error has occurred. You can then use the ERR statement to handle the error and continue with the program.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
1
Views
523
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top