0
votes

I have the following external file with 8 rows ad 11 columns. This file cannot be changed in anyway.

Name        Sun         Jupiter         Saturn          Uranus          Neptune         EarthBC         Mercury         Venus           Mars            Pluto
mass(Msun)  1.000       9.547922048e-4  2.858857575e-4  4.366245355e-5  5.151391279e-5  3.040433607e-6  1.660477111e-7  2.448326284e-6  3.227778035e-7  6.607313077e-9              
a(AU)                   5.20219308      9.54531447      19.19247127     30.13430686     1.00000159      0.38709889      0.72332614      1.52364259      39.80634014
e                       0.04891224      0.05409072      0.04723911      0.00734566      0.01669714      0.20563613      0.00676922      0.09330305      0.25439724
I(deg)                  1.30376425      2.48750693      0.77193683      1.77045595      0.00090235      7.00457121      3.39460666      1.84908137      17.12113756
M(deg)                  240.35086842    045.76754755    171.41809349    293.26102612    094.81131358    242.19484206    345.30814403    330.93171908    024.68081529
w(deg)                  274.15634048    339.60245769    098.79773610    255.50375800    286.84104687    029.14401042    054.54948603    286.56509772    114.39445491
OMEGA(deg)              100.50994468    113.63306105    073.98592654    131.78208581    176.14784451    048.32221297    076.66204037    049.53656349    110.32482041

This file is read by the following program which compiles properly

  program readtable

  implicit none
  integer :: i, j, num_col, num_row
  double precision, dimension (8,11) :: a
  character(14), dimension (8) :: par

  num_col = 4  
  num_row = 8

  open(100,file='SSL.dat',status='old')
  do j=1, num_row

        read(100,*) par(j), (a(i,j), i=1,num_col)

  end do

  print *, par
  print *, a(2,3)    !Jupiter's Mass

  end program 

When I run this program as Fortran90 I get the following message:

At line 14 of file test.f (unit = 100, file='SSL.dat')
Fortran runtime error: Bad real number in item 2 of list input

I think I need to make a FORMAT() statement to help the program read the file properly but I can't seem to get the format right.

1
You can try to skip the header row by calling once read(100,*) par(1) before the loop. - Pierre de Buyl
Tried it, doesn't work. I even tried skipping the first row in the file but nothing - Frederico Arez
Consider providing an example datafile along with the code, this will help to help. - Pierre de Buyl
How do I do that? - Frederico Arez
list directed read actually should work just fine here. You must skip the header, then read the second line individually because it has an extra column, then read the rest in a loop (Just exactly as you have it, except j will go from 2 to 7 ). Just beware your lead "strings" may not contain spaces or commas. - agentp

1 Answers

1
votes

As agentp said list directed is fine here, you just have to account for the first 2 lines being different. I'd do it it something like (slight guess here - I'm not 100% convinced I understand what you want):

ian-admin@agon ~/test $ cat r.f90
Program readtable

  Implicit None

  Integer, Parameter :: wp = Selected_real_kind( 13, 70 )

  Integer :: i, j, num_col, num_row
  Real( wp ) :: msun
  Real( wp ), Dimension (9,11) :: a
  Character(14), Dimension (8) :: par

  num_col = 9 
  num_row = 7

  Open( 100, file = 'SSL.dat', status = 'old' )
  Read( 100, * )
  j = 1
  Read( 100, * ) par(j), msun, (a(i,j), i=1,num_col)
  Do j = 2, num_row

     Read(100,*) par(j), (a(i,j), i=1,num_col)

  End Do

  Write( *, * ) par
  Write( *, * ) a(2,3)    !Jupiter's Mass

End Program readtable
ian-admin@agon ~/test $ gfortran -std=f2003 -Wall -Wextra -O -fcheck=all r.f90
ian-admin@agon ~/test $ ./a.out  
 mass(Msun)    a(AU)         e             I(deg)        M(deg)        w(deg)        OMEGA(deg)    
   5.4090720000000002E-002