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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading data from a text file 1

Status
Not open for further replies.

Babakjingo

Technical User
Mar 6, 2011
5
AU
Dear All,


I have an urgent question. I have a list of data in "m" rows and "n" columns. The following shows the sample in wich x,y and z are REAL numbers.

y1 y2 y3 y4

x1 z1 z2 z3 z4
x2 z7 z8 z9 z10
x3 z13 z14 z15 z16
x4 z19 z20 z21 z22


I need to read these data in fortran 90 in such a way that the values of z are function of x and y, eg. F(x1,y1)=z1 or F(x3,y3)=z15. I could not use Read (1,*) command. It only reads data starting from the first column and after that goes to the second column. But I want to be read row by row i.e. first y(i), then second row reading x1 and corresponding z data for y(i)and x1.Would you please help me through this? Many thanks.
 
I would read the data in vectors X, Y and matrix Z, something like this:
Code:
1. Read the 1st line in vector Y, i.e. y(1), y(2), y(3), y(4)
2. From the second line on, read the vector X and matrix Z, i.e
   for i = 1..4:   
     read x(i), z(i,1), z(i,2), z(i,3), z(i, 4) 

Then for given i,j your function value will be F(x(i),y(j))=z(i,j)
 
Thanks. But how can I read from the text file? As I meantioned Read command from the text

OPEN(1,FILE=DATA.TXT'
READ(1,*)X

first reads the data in first column then goes to the second column. It does not read data row by row. The problem is how to read initially the first row then second and so on>?
 
For this given data file
foo3d.dat
Code:
   1  2  3  4
1 11 12 13 14
2 21 22 23 24
3 31 32 33 34
4 41 42 43 44
here is the example program
foo3d.f95
Code:
[COLOR=#a020f0]program[/color] foo3d
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: fname
[COLOR=#2e8b57][b]integer[/b][/color] :: i, j, stat
[COLOR=#2e8b57][b]real[/b][/color] :: x([COLOR=#ff00ff]4[/color]), y([COLOR=#ff00ff]4[/color]), z([COLOR=#ff00ff]4[/color],[COLOR=#ff00ff]4[/color])

fname[COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'foo3d.dat'[/color]
[COLOR=#0000ff]! open the file[/color]
[COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname,[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
[COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname, [COLOR=#ff00ff]' cannot be opened !'[/color]
  [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]
[COLOR=#804040][b]end if[/b][/color]

[COLOR=#0000ff]! read array Y from the 1.line of the file[/color]
[COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color]) (y(i), i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]4[/color])

[COLOR=#0000ff]! read array elements of X and Z from the file[/color]
[COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]4[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color]) x(i), (z(i,j), j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]4[/color])
[COLOR=#804040][b]end do[/b][/color]

[COLOR=#0000ff]! close the file[/color]
[COLOR=#6a5acd]99[/color] [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])

[COLOR=#0000ff]! print the function[/color]
[COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]4[/color]
  [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]4[/color]
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]10[/color],[COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'no'[/color]) x(i), y(j), z(i,j)
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#804040][b]end do[/b][/color]

[COLOR=#6a5acd]10[/color] [COLOR=#804040][b]format[/b][/color]([COLOR=#ff00ff]'F('[/color] [COLOR=#008080]f3.1[/color] [COLOR=#ff00ff]','[/color] [COLOR=#008080]f3.1[/color] [COLOR=#ff00ff]') = '[/color] [COLOR=#008080]f4.1[/color] [COLOR=#ff00ff]', '[/color]) 
[COLOR=#a020f0]end program[/color] foo3d

Output:
Code:
$ g95 foo3d.f95 -o foo3d

$ foo3d
F(1.0,1.0) = 11.0, F(1.0,2.0) = 12.0, F(1.0,3.0) = 13.0, F(1.0,4.0) = 14.0, 
F(2.0,1.0) = 21.0, F(2.0,2.0) = 22.0, F(2.0,3.0) = 23.0, F(2.0,4.0) = 24.0, 
F(3.0,1.0) = 31.0, F(3.0,2.0) = 32.0, F(3.0,3.0) = 33.0, F(3.0,4.0) = 34.0, 
F(4.0,1.0) = 41.0, F(4.0,2.0) = 42.0, F(4.0,3.0) = 43.0, F(4.0,4.0) = 44.0,
 
Would you please let me know

1. Imagine that your example data set was f0r w=1,if the text file containes a data similar to the following (with list of data associated to w=2), is the only adding command would be "do loop" for w? (w,x,y are 3D coordinates and z is the corresponding function. as w changes, the location of the plane changes, so for constant w, only x and y varies and therefore Function z)

for w=1:

 1  2  3  4
1 11 12 13 14
2 21 22 23 24
3 31 32 33 34
4 41 42 43 44

for w=2:
 5  6  7  8
1 15 16 17 18
2 25 26 27 28
3 35 36 37 38
4 45 46 47 48

2. the source file is excel file, what commands I should use for reading the data?

Kind regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top