Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reading semicolon ascii files in FORTRAN

283 views
Skip to first unread message

Nat

unread,
Feb 12, 2016, 4:19:56 PM2/12/16
to
Hi,

I have some files in semicolon format that I need to open them with fortran.

For example, I have the following file:

-54.9;-67.6;0.0104046;
-54.8;-68.3;1.07319;
-54.5;-67.2;0.0627663;
-53.8;-67.7;0.552511;
-53.3;-70.3;0.0115021;
-53.1;-70.9;0.0780824;
-52.4;-59.5;2.38555E-9;
-52.4;-59.4;6.61809E-9;
-52.3;-60.7;6.55159E-7;
-52.3;-60.6;7.22433E-7;

I have tried the following read statements but neither of them worked.

read(1,*) lat,lon, valdata
Fortran runtime error: Bad real number in item 1 of list input

read(1,'(2(f2.1,a),e10.2,a)') lat,lon, valdata
Fortran runtime error: Bad value during floating point read

read(1,'(2f2.1,e10.2)') lat,lon, valdata
Fortran runtime error: Bad value during floating point read

Can anyone help with this?

Thank you!

steve kargl

unread,
Feb 12, 2016, 4:54:21 PM2/12/16
to
tr ";" " " < infile > outfile
mv outfile infile

or

character(len=80) s1, s2

read(1,'(A)') s1
s2 = s1(:index(s1, ';')-1)
read(s2,*) lat
s1 = s1(index(s1, ';')+1:)
s2 = s1(:index(s1, ';')-1)
read(s2,*) lon
s1 = s1(index(s1, ';')+1:)
s2 = s1(:index(s1, ';')-1)
read(s2,*) valdata

--
steve

dpb

unread,
Feb 12, 2016, 5:22:42 PM2/12/16
to
On 02/12/2016 3:19 PM, Nat wrote:
...

> For example, I have the following file:
>
> -54.9;-67.6;0.0104046;
> -54.8;-68.3;1.07319;
> -54.5;-67.2;0.0627663;
...

> I have tried the following read statements but neither of them worked.
>
> read(1,*) lat,lon, valdata
> Fortran runtime error: Bad real number in item 1 of list input

Doesn't follow list-directed rules (semicolon)

> read(1,'(2(f2.1,a),e10.2,a)') lat,lon, valdata
> Fortran runtime error: Bad value during floating point read

> read(1,'(2(f2.1,a),e10.2,a)') lat,lon, valdata

The first two fields aren't F2.1, the form is Fw.d where s is the field
width which is the _total_ field width, not the number of digits before
the decimal.
...



read(lu,2(F5.1,1X),F???

You still run into trouble because the last field isn't fixed-width and
the trailing semicolon will cause a general F or E to fail.

Just commenting on the misunderstanding regarding definition of field
width primarily, Steve Kargl gave you the ways to solve the problem.

--

Nat

unread,
Feb 12, 2016, 5:52:55 PM2/12/16
to
Thank you very much Steve. I tried the second solution but I cannot compile the program. I use gfortran.

it returns the error Invalid character in name for all the above lines.

below you can find the code:

program test
implicit none
character*80 :: s1, s2
character*200 :: fname
integer :: ncellmax,ncell
real :: lat1,lon1,valdata1
real, allocatable, dimension(:) :: valdata, lon,lat

ncellmax = 10

open(100,file=fname)

allocate (valdata(ncellmax))
allocate (lon(ncellmax))
allocate (lat(ncellmax))

do ncell=1, ncellmax

read(100,'(a)') s1
s2 = s1(:index(s1, ';')-1)
read(s2,*) lat1
s1 = s1(index(s1, ';')+1:)
s2 = s1(:index(s1, ';')-1)
read(s2,*) lon1
s1 = s1(index(s1, ';')+1:)
s2 = s1(:index(s1, ';')-1)
read(s2,*) valdata1

lat(ncell)=lat1
lon(ncell)=lon1
valdata(ncell)=valdata1

end do

close(100)
end program

steve kargl

unread,
Feb 12, 2016, 6:32:38 PM2/12/16
to
> Thank you very much Steve. I tried the second solution but I cannot compile the program. I use gfortran.
>
> it returns the error Invalid character in name for all the above lines.
>
> below you can find the code:

"I cannot compile the program" isn't an error message that gfortran
would emit. I just compiled the code you posted with gfortran
4.8.something, 4.9.something, 5.4, and 6.0. Perhaps, you have
a copy-n-paste issue with some control character on your system.

--
steve

Ron Shepard

unread,
Feb 13, 2016, 1:24:56 AM2/13/16
to
On 2/12/16 3:19 PM, Nat wrote:
> read(1,*) lat,lon, valdata
> Fortran runtime error: Bad real number in item 1 of list input

This is close, but you need to do some preliminary work first.

1. read each line into a character string of sufficient length. It looks
like

character(len=40) :: line

should be large enough.

2. scan through the string and replace all ";" with " ". There are
several ways to do this in fortran, but for short lines like you have
the following simple code should suffice.

do i = 1, len_trim(line)
if ( line(i:i)==";" ) line(i:i)=" "
enddo

3. read(line,*) lat, lon, valdata

$.02 -Ron Shepard

Nat

unread,
Feb 13, 2016, 3:16:34 AM2/13/16
to
Yes you are right, thank you! It works properly now!
0 new messages