Readind x's and y's

05-13-2008, 05:11 PM
hello i need to read in a file that looks like this:

header ..........(usually really long but < 60 char)


1 22992.9 29920.4 1.6
2 29039.4 29920.6 1.5
3 28395.8 19293.0 1.3
1 2 4
2 4 5
3 5 3

and compute some stuff(avg,sin,cos,etc...) with columns

22992.9 29920.4
29039.4 29920.6
28395.8 19293.0

also i'd like to read them into two separate vectors



05-13-2008, 06:17 PM
hello i need to read in a file that looks like this:
I'm sorry, but that's simply not good enough. Tell us exactly what is in the file.

1. OK, I get the first one: A header (throw it away or print it or whatever).

Then what?

2. What the heck does "NUMBER1,NUMBER2" mean? Are these something that the program should know about? Are they numbers or what? What kind of numbers? What do they mean? (But I said that already.)

3. Are there blank lines in the places that you indicated? Or what?

4. Exactly what information do you need to get from the data lines? Are there always at least three numeric data items on each line and you want to read item number 2 and item number 3 on each line into the separate arrays or what?

5. Does the file specification tell us how many data lines are in the file? Or do the mysterious "NUMBER1,NUMBER" thingies tell us something? Or are we just supposed to keep reading until there are no more lines in the file? (In which case it might be important for us to know what is the maximum number of data lines that we are expected to handle.)

Bottom line: I mean no disrespect, and someone might be smart enough and psychic enough to know what is in the file. Lots of luck writing a program that insightful.

My main point is: why should we (or the program) have to guess?



Footnote: If the file was given to you, then whoever created it must tell you (and you must tell us) what the specification is. Exactly. If you are creating the file, then you must tell us how the file is organized. Exactly.

05-13-2008, 08:54 PM
Sorry. I attached a copy of the file.

2. The program should only know about NUMBER2 which will always be an integer. NUMBER2 is the number of rows in the first array of values.

4. I need to get columns 2 and 3 from the first array of values. In other words I need to set some vector A = column 2 and another vector B = column 3.

5. NUMBER2 tell us how many data lines there are in the first array of values. I do not need to read or do anything with the remaining arrays.



05-13-2008, 10:10 PM
I attached a copy of the file...

You could try something like:

REAL x(100), y(100)
INTEGER number1, number2

read(7,*) line

!Uncomment the next two if you want to print the header line
! write(*,'(" The header line")')
! write(*,*) line

read(7,*) number1, number2

!Comment out the following line if you don't want to see the values
write(*,'(" number1 = ", I0, " number2 = ", I0)') number1, number2

if (number2 > 100) then
write(*,*) 'Dimension of arrays is too small'
end if

do i=1,number2
read(7,*, END=1000) n, x(i), y(i)
end do
1000 continue

n = i-1
write(*,'(" Number of data items = ", I0)') n
write(*,'(A4, A11, A14)') 'i', 'x(i)', 'y(i)'

do i=1,n
write(*,'(I5, A1, 2(1PE14.6))') i, ':', x(i), y(i)
end do


Output, using your example.txt file:

number1 = 96 number2 = 10
Number of data items = 10
i x(i) y(i)
1: 6.096000E+04 0.000000E+00
2: 7.620000E+04 0.000000E+00
3: 9.144000E+04 0.000000E+00
4: 1.066800E+05 0.000000E+00
5: 1.219200E+05 0.000000E+00
6: 1.371600E+05 0.000000E+00
7: 1.524000E+05 0.000000E+00
8: 5.978870E+04 1.189270E+04
9: 7.473570E+04 1.486600E+04
10: 8.968310E+04 1.783900E+04

I respectfully suggest that you get a book or other reference material (on-line, etc.) and read about Fortran I/O and arrays.



Footnote: This is the kind of 'old-fashioned' code that is similar to what would have been used for Fortran 77 (or earlier versions). It assumes that you will know the maximum expected sizes of the arrays when you compile it. With later versions of Fortran it is possible to allocate storage dynamically, but I think it's important to get simple-minded stuff (old stuff like this) running first.