Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read data from specific line in a file 1

Status
Not open for further replies.

sougandh

Technical User
Dec 21, 2006
1
US
Hi,
Here is my problem. My file is in the following format.
It has 5 columns with first two being integers and 3-5 are floating values.

file format (similar format with different values)

col 1 col 2 col 3 col 4 col 5

5001 1001 12.34 11.34 43.454
5001 1002 11.54 65.43 65.234
5001 1003 12.54 54.64 65.778
. . . . .
. . . . .
5001 1099 14.65 16.53 12.568
5001 1100 13.65 13.54 12.545
5002 1001 12.34 11.34 43.454
5002 1002 11.54 65.43 65.234
5002 1003 12.54 54.64 65.778
. . . . .
. . . . .
5002 1099 13.45 51.15 61.542
5002 1100 13.65 13.54 12.545

so on...

That means, for each number in column 1 i have 100 values. Or i should say the column 1 number doesnt change for for 100 rows and then it increases. Infact my original file has (5760 x 4633) rows.

Now here is how i need to read it:

There is a variable 'crhr' which returns a value which corresponds to any of the values in column 1. For example it may be 5002 or 5050 or anything. So when this variable has a value of 5002 what i need is i should be able to read the rows corresponding to the number 5002 and store it in an array.

My problem now is how do i make Fortran start reading the file from a particular line based on the value returned by the variable 'crhr'


thanks
sougandh
































 
Are all the lines the same size (in characters). If so, then you can use formatted reads on direct access files
Code:
program main
   integer FHAND, TMAX, RMAX
   parameter (TMAX=10, RMAX=5, FHAND=88)
   real t(TMAX)
   integer reqd(RMAX)
   data reqd/ 1, 2, 16, 8, 4 /
      
   ! file - the data file
   ! status - always old
   ! form - formatted for human readable data
   !      - unformatted for binary data
   ! access - always direct.  The alternative is sequential
   ! recl - number of characters incl LF
   open (FHAND, &
      file='data.txt', &
      status='old', &
      form='FORMATTED', &
      access='direct', &
      recl=51)
   do i = 1, RMAX
      ! Read record reqd(i)
      read (FHAND, '(10F5.1)', rec=reqd(i)) (t(j), j = 1, TMAX)
      write (*, '(10F5.1)') (t(j), j = 1, TMAX)
   enddo
   close (FHAND)
   stop
end program
Your read format, number of values per line, record length will be different
 
Well, there is no need to have the data in direct access mode (this would require they were saved in direct access mode, which is not so much a common feature if you have it from the outside).

Option 1:

rewind your (sequential acess)- and start reading the lines and start to process the lines when your first number equals the one you want


integer chrcr ! would not work on real numbers
integer iend,flag
open(unit = 10 ...)
.
.
.
chrcr = (value)
flag=.false.
rewind 10
do
read(10,20,iostat=iend)int1,int2,float1,float2,float3
20 format(2i15,3f15.8) ! if you select the field width inexcess of what you expect in the file you are sure your numbers get read completely.
if(int1.eq.chrcr)then
flag =.true. ! indicate that numnber is found
exit ! leave loop when number is found
endif
if(iend.lt.0)exit ! same when eof is reached
enddo
if(flag)then
read(10,20)int1,int2,array....
endif

second option:

if you know the numbers of your first column, that is their increment and the number of rows where the value of int1 does not change you can compute the number of the first line to be read. Just rewind your file as above and just read over the appropriate number of lines by

rewind 10
do n=1,number_of_line - 1,1
read (10,30)
30 format ()
enddo

and then start reading the values to your array.

Reading, especially this empty reading is pretty fast. The files that I handle have about 60,000 lines and the program moves through this - with some computing on the line - in less time than a second.

Norbert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top