Reading specific lines with fortran


sakurali03
11-25-2008, 06:59 AM
hi everyone..

I have a problem reading specific lines in the Fortran output file.
Below is the file that i want to read.
__________________________________________________ _____________________

KASE = 5613 IDEBUG = 0 TRACE = .00000D+00

TP = F HP = F SP = F TV = F UV = F SV = F RKT = T SHOCK = F DETN = F

TRNSPT = T TRPACC = .999950E+00 NODATA = F

OF = F FA = F FPCT = F ERATIO = F PHI = F

S0 = .00000000E+00 U = .00000000E+00 H = .00000000E+00

P = .20000E+07

T = .00000E+00

MIX = .10000E+01

SPECIES BEING CONSIDERED IN THIS SYSTEM
J 3/77 H L 5/89 HO2 J 3/77 H2
J 3/79 H2O L 3/85 H2O2 J 3/77 O
J 6/77 OH J 3/77 O2 J 6/61 O3
L 3/81 H2O(S) J 3/79 H2O(L)

SPECIES WITH TRANSPORT PROPERTIES
PURE SPECIES

H H2 H2O H2O2
O OH
O2


***RKTINP***

EQL = F FROZ = T NFZ = 1 TCEST = 3800.000 FAC = F


PCP: .15000000E+02

SUBAR =

SUPAR =

OF = .000000


POINT ITN T H O
1 10 1029.541 -19.058 -12.440



THEORETICAL PERFORMANCE

PINF = 290.1 PSIA
CASE NO. 5613


O/F= .0000 PERCENT FUEL= 100.0000 EQUIVALENCE RATIO= .5475 PHI= .0000

CHAMBER THROAT EXIT
PINF/P 1.0000 1.8188 15.000
P, MPA 2.0000 1.0996 .13333
T, DEG K 1029.54 906.39 559.65
RHO, KG/CU M 5.1645 0 3.2253 0 6.3338-1
H, KJ/KG -6555.10 -6772.68 -7345.77
U, KJ/KG -6942.36 -7113.61 -7556.28
G, KJ/KG -16670.5 -15678.1 -12844.4
S, KJ/(KG)(K) 9.8251 9.8251 9.8251

M, MOL WT 22.105 22.105 22.105
CP, KJ/(KG)(K) 1.7966 1.7371 1.5671
GAMMA (S) 1.2648 1.2764 1.3158
SON VEL,M/SEC 699.9 659.7 526.3
MACH NUMBER .000 1.000 2.389

so the lines that i should read are the bold and red color.
however the value that needed to be read by fortran is only for the chamber value.

The output should be showing:
Rho = 5.1645
Pressure = 2
Mol = 22.105


I've read the I/O in fortran and suppose we can read the file by using (//), to go to specific lines. But everytimes i wrote the program, an error occur.

Can anyone help me..

davekw7x
11-27-2008, 02:40 PM
...I have a problem...Below is the file that i want to read...The output should be showing:..


You haven't given a very good problem specification. What, exactly, do you know about the file?

Exactly what can the file contain? Do you know, exactly, what line numbers you want to read? How, exactly, do you know that you are reading the right line? Do you know, exactly, what columns hold the data?

I'm guessing that you want a program that handles different files that may contain different numbers of lines and the data may appear in on different lines and in different columns and formats. Etc.

Well, here's the story as I see it:

Historically, researchers using Fortran for problem solving have been willing to accept some restrictions on file formats to make it easy for the program to know exactly where (in what column of which line) it should look for the data.

Then the programs can be made (rather easily, I think) to ignore certain lines and to look in certain columns of specific lines to find the data.

If that's what you want, then, show us your code and tell us what you don't understand about errors and other misbehavior. Maybe someone can help, but I'm thinking that we have to know, exactly, what you expect the code to do.

On the other hand, if you want to write a program that accepts a rather looser specification of the file contents, then you and your program may have to do a little more work.

In any case, there simply has to be some kind of input specification.

I'll give an example.

Suppose we know that the file has the following characteristics

The file can have any number of lines.

All lines in the file have 80 or fewer characters.

Each line consists of a number of "fields."

Fields consist of one or more non-space characters, and fields are separated by one or more spaces.


Now, suppose we know the following about the line from which we want to extract the value of Pressure:

The line can appear anywhere in the file.

The line has at least three fields.

The first field consists of the characters 'P' and ','

The second field will be ignored. (You can make a specific requirement here if you want to, if further disambiguation is appropriate.)

The third field consists of a Fortran-style floating point number, whose numerical value we wish to store in a variable.


How to solve the problem???

First of all, you might try to find some examples of string manipulation in books. That is, breaking delimited strings into substrings and converting them to numbers. (Lot's of luck here. It's possible, but I don't have any handy-dandy references on this.)

Failing this, try to find (on the web) some examples of Fortran string manipulation software that someone else has created and documented and has kindly made freely available.

For instance: Fortran Character String Utilities (http://www.gbenthien.net/strings/index.html) Boola, boola!

Here's an example that uses stringmod from that site. I compiled with GNU gfortran.


!
! Subroutines "parse" and "value" are in the "strings"
! module, which comes from stringmod.f90.
!
! Here's where you can get stringmod.f90 (other stuff, too)
!
! http://www.gbenthien.net/strings/index.html
!
! This example from davekw7x
!
!
PROGRAM parser
USE strings

IMPLICIT NONE

! The line counter
INTEGER i
INTEGER j !Used in debugging loop

! Each line is read into a string
CHARACTER (len=80) :: line

! An array of strings to hold the individual fields on the line
CHARACTER (len=80) :: tokens(80)

! The number of fields on the line
INTEGER ntokens

! The error flag from the parse subroutine
INTEGER ios

! What we are looking for
REAL Pressure

open(7,file='data.txt',status='old')

do i=1,100
read(7,'(A)',END=1000,ERR=999) line
call parse(line, ' ', tokens, ntokens)
if (ntokens > 0) then
!Uncomment the following four statements for debugging
!write(*,'(" Line ",I0)') i
!do j=1,ntokens
! write(*,'(" tokens(",I0,"): <",A,">")') j, line
!enddo
if (tokens(1) == 'P,') then
write (*,'( " Found ''P,'' on line ", I0)',advance='no')i
if (ntokens >= 3) then
call value(tokens(3),Pressure,ios)
else
ios = 1
endif
if (ios == 0) then
write (*, '(": Pressure = ", 1PE13.6)') Pressure
else
write (*, '(": Error scanning Pressure")')
endif
endif
endif
enddo
999 write(*,'(" Error reading line ", I0)') i
close(7)
stop

1000 write(*, '(" Number of lines read = ", I0)') i-1
close(7)
stop

END PROGRAM parser


Output from your data file:

Found 'P,' on line 60: Pressure = 2.000000E+00
Number of lines read = 72


IWFM-YMMV (It Works For Me---Your Mileage May Vary)

Regards,

Dave