Solved

How to read data in Scentific notation in Fortran90

Posted on 2011-02-13
3
384 Views
Last Modified: 2013-11-25
Have data set as follows
ABC  1  4.2023E-02 100.1
AAA 10 1.2345E-03  200.2
.....
How to read the data into the variables in FORTRAN 90?? I tried the following, but it did not work.
read(UNIT=1, '(A3, I3, E7.4, F6.1)', END=25) NAME, ID, VAL1, VAL2

Gurus, please advise.
0
Comment
Question by:jl66
  • 2
3 Comments
 
LVL 5

Expert Comment

by:dcesari
ID: 34886692
Hi, when reading floating point data with fixed format in Fortran, it is better to use a format like *.0, otherwise strange data scaling will happen, and the difference between E and F descriptors is not so important in reading, so, try to use the following:

read(UNIT=1, '(A3, I3, F7.0, F6.0)', END=25) NAME, ID, VAL1, VAL2

this should work correctly regardless of the number of digits you have after the decimal point in your floating point numbers, and regardless of having exponential notation or not, the only constraint is that the length of each field (including possible blanks) is fixed, so 7 and 6 characters respectively for the two real numbers.
0
 
LVL 5

Accepted Solution

by:
dcesari earned 500 total points
ID: 34886727
A little update: the right format for your lines (provided that they are pasted with the correct number of spaces) is:

read(10, '(A3, I3, E12.0, E6.0)', END=25) NAME, ID, VAL1, VAL2

i.e. the third field is 12 characters wide, not 7, I show what I used as input text in the attached code (there seems to be some misalignment, however. The other attachment shows the code giving the correct result.
ROGRAM prova
CHARACTER(len=3) :: name
INTEGER :: id
REAL :: val1, val2

OPEN(10,file='prova.txt')
DO WHILE(.TRUE.)
read(10, '(A3, I3, E12.0, E6.0)', END=25) NAME, ID, VAL1, VAL2
PRINT*,name,id,val1,val2
ENDDO

25 CLOSE(10)

END PROGRAM prova

Open in new window

ABC  1  4.2023E-02 100.1
AAA 10 1.2345E-03  200.2

Open in new window

0
 

Author Closing Comment

by:jl66
ID: 34887996
Excellent!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question