Solved

How to read data in Scentific notation in Fortran90

Posted on 2011-02-13
3
380 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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
A short article about a problem I had getting the GPS LocationListener working.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now