Fortran Fortran 90 help reading from file

AI Thread Summary
The discussion centers around creating a Fortran 90 program to read formatted data from a file named "DEM.in." The user is new to Fortran and seeks guidance on how to read specific variable values from the file, which include ncols, nrows, xllcorner, yllcorner, cellsize, and NODATA. The initial code provided successfully opens the file and checks for errors. A response suggests that as long as the variables are correctly defined, an unformatted read can be used to import the values. The example code illustrates how to read the integer values for ncols and nrows, followed by a loop to read the data into an array. The response emphasizes the importance of proper variable definition and provides a clear example to assist the novice user.
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!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
12
Views
3K
Replies
5
Views
5K
Replies
1
Views
3K
Replies
5
Views
2K
Replies
2
Views
2K
Replies
8
Views
2K
Back
Top