For Programmers: Free Programming Magazines  


Home > Archive > AWK > September 2006 > Reading numbers from FORTRAN









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reading numbers from FORTRAN
Rafael Rodríguez Pappalardo

2006-09-13, 6:56 pm

I need to read a file from FORTRAN program which have numbers in the format:
1.234D+02.
gawk refuse to recognize the D+XX part. Is there a way to treat this kind of
numbers directly in awk or I need to define a function to convert them?

Thanks a lot for the help.

Rafael R. Pappalardo
Physical Chem. Dept., Univ. of Seville (Spain)
Jürgen Kahrs

2006-09-13, 6:56 pm

Rafael Rodríguez Pappalardo wrote:

> I need to read a file from FORTRAN program which have numbers in the format:
> 1.234D+02.
> gawk refuse to recognize the D+XX part. Is there a way to treat this kind of
> numbers directly in awk or I need to define a function to convert them?


I dont think that you can read them directly.
But you can replace the D with an E and that should be it.

> Thanks a lot for the help.
>
> Rafael R. Pappalardo
> Physical Chem. Dept., Univ. of Seville (Spain)


Interesting to see that FORTRAN is still alive in Physical Chemistry.
Last year I bought the third edition of "Computer Simulation Methods"
by Gould and Tobochnik (with simulations in Java). Now i wonder if
it was really a good idea that the authors switched from FORTRAN to Java.
Rafael Rodríguez Pappalardo

2006-09-13, 6:56 pm

Jürgen Kahrs wrote:

> Rafael RodrÃ_guez Pappalardo wrote:
>
>
> I dont think that you can read them directly.
> But you can replace the D with an E and that should be it.
>
>
> Interesting to see that FORTRAN is still alive in Physical Chemistry.
> Last year I bought the third edition of "Computer Simulation Methods"
> by Gould and Tobochnik (with simulations in Java). Now i wonder if
> it was really a good idea that the authors switched from FORTRAN to Java.


I have finally defined a function like:

function change_D(number){
position_D=0
position_D=index(number,"D")
if (position_D==0)
return number
else
return strtonum(substr(number,1,position_D-1))*
10**(strtonum(substr(number,position_D+1
,3)))
}

We have lots of well tested software which are reluctant to change just to
switch language. Old habits are hard to break.

Thanks a lot for the help.
Jürgen Kahrs

2006-09-13, 6:56 pm

Rafael RodrÃ_guez Pappalardo wrote:

> I have finally defined a function like:
>
> function change_D(number){
> position_D=0
> position_D=index(number,"D")
> if (position_D==0)
> return number
> else
> return strtonum(substr(number,1,position_D-1))*
> 10**(strtonum(substr(number,position_D+1
,3)))
> }


Hmm, this should be much easier:

echo 1.234D+02 | awk '{gsub(/D/, "E"); print $1+1}'
124.4

Rafael Rodríguez Pappalardo

2006-09-13, 6:56 pm

Jürgen Kahrs wrote:

> Rafael RodrÃ_guez Pappalardo wrote:
>
>
> Hmm, this should be much easier:
>
> echo 1.234D+02 | awk '{gsub(/D/, "E"); print $1+1}'
> 124.4


OK, I should have mentioned that the E is not valid because the file
produced by awk will be processed by another utility which does not
understand it.
Also your approach has problems with lines with:
1.234D+022.345D+03
I am sure that the exponent is lower than 99 and I am using only the first
number.

Thanks again.
Jürgen Kahrs

2006-09-13, 6:56 pm

Rafael RodrÃ_guez Pappalardo wrote:

> OK, I should have mentioned that the E is not valid because the file
> produced by awk will be processed by another utility which does not
> understand it.


The E can be converted back to D by AWK.

> Also your approach has problems with lines with:
> 1.234D+022.345D+03
> I am sure that the exponent is lower than 99 and I am using only the first
> number.


GNU AWK has the FIELDWIDTHS variable (see the man page).
Reading FORTRAN data has often been done by AWK scripts.
GNU AWK has solutions for such common problems like
fixed field width.
William James

2006-09-14, 6:56 pm


Rafael Rodr=EDguez Pappalardo wrote:
> J=FCrgen Kahrs wrote:
>
ind[color=darkred]
a=2E[color=darkred]
>
> I have finally defined a function like:
>
> function change_D(number){
> position_D=3D0
> position_D=3Dindex(number,"D")
> if (position_D=3D=3D0)
> return number
> else
> return strtonum(substr(number,1,position_D-1))*
> 10**(strtonum(substr(number,position_D+1
,3)))
> }


function change_D( number )
{=20
sub( /D/, "E", number )
return strtonum( number )
}

Rafael Rodríguez Pappalardo

2006-09-14, 6:56 pm

William James wrote:

>
> Rafael RodrÃ_guez Pappalardo wrote:
>
> function change_D( number )
> {
> sub( /D/, "E", number )
> return strtonum( number )
> }

Thanks for your suggestion. The trouble is that the numbers are going to be
processed by bc which does not understand de "scientific notation".

Jürgen Kahrs

2006-09-14, 6:56 pm

Rafael RodrÃ_guez Pappalardo wrote:

> Thanks for your suggestion. The trouble is that the numbers are going to be
> processed by bc which does not understand de "scientific notation".


By bc ? Why do you need bc ? Are the number so large ?
Rafael Rodríguez Pappalardo

2006-09-23, 6:56 pm

Dear Kahrs and James,

sorry for the delay. The use of bc is a long history which I am not able to
fully explain. I was trying to help a colleage who use it inside a shell to
do maths calculations. He needed to read some FORTRAN output and I thought
about using AWK to preprocess it. Anyway, I believe that my solution albeit
a bit convoluted make him happy (I has not hear from him again).

Thanks a lot for the suggestions.

Best regards,

Rafael

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com