Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28994 Discussions

I need simple fortran code for read data from file

hamid_mosaddeghi
Beginner
1,562 Views
I have output file and need simple code for read data that highlited and list in new file as xy (two colume for excel)

MOSADDEGHI CASE 1: 2019 Water density=0.9986
ENERGY UNITS=kjoule/mol
200 2.000000E-01 38
-7.905859E+04 3.010087E+02 -9.421393E+04 1.741921E+04 -1.116331E+05
0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 -8.037317E+04
0.000000E+00 3.425443E+04 -3.975877E+05 1.116268E+05 0.000000E+00
0.000000E+00 3.202153E+05 0.000000E+00 6.048000E+04 0.000000E+00
0.000000E+00 0.000000E+00 9.000000E+01 9.000000E+01 9.000000E+01
0.000000E+00 -3.562117E-01 0.000000E+00 0.000000E+00 -4.113168E-01
-8.834648E-02 -3.593858E-02 -8.834648E-02 1.376611E-01 -2.073464E-01
-3.593858E-02 -2.073464E-01 -7.949794E-01
400 4.000000E-01 38
-7.897667E+04 2.990632E+02 -9.403406E+04 1.768077E+04 -1.117148E+05
0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 -7.918380E+04
0.000000E+00 3.073616E+04 -4.011339E+05 1.117082E+05 0.000000E+00
0.000000E+00 3.201619E+05 0.000000E+00 6.048000E+04 0.000000E+00
0.000000E+00 0.000000E+00 9.000000E+01 9.000000E+01 9.000000E+01
0.000000E+00 -5.612462E-02 0.000000E+00 0.000000E+00 8.705193E-01
1.079355E-01 1.559906E-01 1.079355E-01 -3.729550E-01 -5.621051E-02
1.559906E-01 -5.621051E-02 -6.659382E-01
...

original file attached.

thanks for attention.
0 Kudos
1 Solution
JVanB
Valued Contributor II
1,562 Views
[bash]program read_data
   implicit none
   integer stepno, arrsiz
   real steptime, datum
   real, allocatable :: array(:)
   integer iunit

   open(newunit=iunit,file='STATIS',status='old')
   read(iunit,'()')
   read(iunit,'()')
   do
      read(iunit,'(i10,f14.0,i10)',end=10) &
         stepno,steptime,arrsiz
      allocate(array(arrsiz))
      read(iunit,'(5f14.0)') array
      datum = array(36)
      write(*,'(2(a,es14.6e2))') 'Step time =', &
         steptime,', datum =',datum
      deallocate(array)
   end do
10 continue
end program read_data
[/bash]

View solution in original post

0 Kudos
5 Replies
JVanB
Valued Contributor II
1,563 Views
[bash]program read_data
   implicit none
   integer stepno, arrsiz
   real steptime, datum
   real, allocatable :: array(:)
   integer iunit

   open(newunit=iunit,file='STATIS',status='old')
   read(iunit,'()')
   read(iunit,'()')
   do
      read(iunit,'(i10,f14.0,i10)',end=10) &
         stepno,steptime,arrsiz
      allocate(array(arrsiz))
      read(iunit,'(5f14.0)') array
      datum = array(36)
      write(*,'(2(a,es14.6e2))') 'Step time =', &
         steptime,', datum =',datum
      deallocate(array)
   end do
10 continue
end program read_data
[/bash]
0 Kudos
hamid_mosaddeghi
Beginner
1,562 Views
Hi

I renamed it to tensor.f and comilpe it with STATIS but I get this error:



------------------------------------------------------------
: error #5149: Illegal character in statement label field [.]

: error #5149: Illegal character in statement label field



: error #5149: Illegal character in statement label field

: error #5149: Illegal character in statement label field

: error #5148: A continuation character is illegal on a line with a statement label

tensor.f(2): error #5149: Illegal character in statement label field [.]
2. implicit none
-^
tensor.f(2): error #5148: A continuation character is illegal on a line with a statement label
2. implicit none
-----^
tensor.f(1): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( % : . = =>
1.program read_data
----------------------^
tensor.f(3): error #5149: Illegal character in statement label field [.]
3. integer stepno, arrsiz
-^
tensor.f(3): error #5148: A continuation character is illegal on a line with a statement label
3. integer stepno, arrsiz
-----^
tensor.f(2): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( % : . = =>
2. implicit none
---------------------^
tensor.f(4): error #5149: Illegal character in statement label field [.]
4. real steptime, datum
-^
tensor.f(4): error #5148: A continuation character is illegal on a line with a statement label
4. real steptime, datum
-----^
tensor.f(3): error #5082: Syntax error, found ',' when expecting one of: ( % : . = =>
3. integer stepno, arrsiz
-------------------^
tensor.f(5): error #5149: Illegal character in statement label field [.]
5. real, allocatable :: array(:)
-^
tensor.f(5): error #5148: A continuation character is illegal on a line with a statement label
5. real, allocatable :: array(:)
-----^
tensor.f(4): error #5082: Syntax error, found ',' when expecting one of: ( % : . = =>
4. real steptime, datum
------------------^
tensor.f(6): error #5149: Illegal character in statement label field [.]
6. integer iunit
-^
tensor.f(6): error #5148: A continuation character is illegal on a line with a statement label
6. integer iunit
-----^
tensor.f(5): error #5082: Syntax error, found ',' when expecting one of: ( % : . = =>
5. real, allocatable :: array(:)
---------^
tensor.f(5): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ) ,
5. real, allocatable :: array(:)
-------------------------------------^
tensor.f(7): error #5149: Illegal character in statement label field [.]
7.
-^
tensor.f(8): error #5149: Illegal character in statement label field [.]
8. open(newunit=iunit,file='STATIS',status='old')
-^
tensor.f(8): error #5148: A continuation character is illegal on a line with a statement label
8. open(newunit=iunit,file='STATIS',status='old')
-----^
tensor.f(7): error #5156: Labelled statement is empty
7.
----^
tensor.f(6): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ( % : . = =>
6. integer iunit
---------------------^
tensor.f(7): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: BLOCK BLOCKDATA PROGRAM MODULE TYPE COMPLEX BYTE CHARACTER CLASS DOUBLE ...
7.
----^
tensor.f(9): error #5149: Illegal character in statement label field [.]
9. read(iunit,'()')
-^
tensor.f(9): error #5148: A continuation character is illegal on a line with a statement label
9. read(iunit,'()')
-----^
tensor.f(8): error #5082: Syntax error, found '=' when expecting one of: ( * ) :: , ; + . - % (/ [ : ] /) . ** / // ...
8. open(newunit=iunit,file='STATIS',status='old')
-----------------^
(10): catastrophic error: Too many errors, exiting
compilation aborted for tensor.f (code 1)
--------------------------------------------------------------------------------------

what's you idea?

0 Kudos
Alexis_R_
New Contributor I
1,562 Views
Beacause you gave the file a .f extension, the compiler assumed it would be fixed-form, whereas the code above is free-form.

Try renaming the file with a .f90 extension instead.
0 Kudos
hamid_mosaddeghi
Beginner
1,562 Views
Hi Dear Rohou

I do thing that you said (ifort tensor.f90) ,but I get this error:



tensor.f90(8): error #6640: This input/output keyword is invalid. [NEWUNIT]
open(newunit=iunit,file='STATIS',status='old')
--------^
tensor.f90(8): error #6332: A UNIT= specifier is required for this I/O operation.
open(newunit=iunit,file='STATIS',status='old')
---^
tensor.f90(12): error #6323: This label is not defined in this scoping unit. [10]
read(iunit,'(i10,f14.0,i10)',end=10) &
----------------------------------------^
compilation aborted for tensor.f90 (code 1)
0 Kudos
fercook
Beginner
1,562 Views
As for the first two, I think that NEWUNIT is a Fortran 2003 (or 2008?) keyword, and you can only use it with newer versions of the compiler. Change the lines to a fixed unit number like 23 in the following:
open(unit=23,file='STATIS',status='old')
and also change the iunit variable in the reads to 23 (or whatever unit number you use)
The other error, I am not sure. The line says that when if finds the end of the file, it should go to the line labelled 10 below. Make sure you wrote a 10 there (one zero)?
I compiled the code ok with ifort 12.0 and with 11.1.084, no changes were necessary.
0 Kudos
Reply