Reading numbers from FORTRAN

  • Follow


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)
0
Reply Rafael 9/13/2006 6:11:55 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.
0
Reply ISO 9/13/2006 7:44:14 PM


Jürgen Kahrs wrote:

> 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.

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.
0
Reply Rafael 9/13/2006 8:50:19 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

0
Reply UTF 9/13/2006 9:24:39 PM

Jürgen Kahrs wrote:

> 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

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.
0
Reply Rafael 9/13/2006 9:33:30 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.
0
Reply UTF 9/13/2006 9:58:21 PM

Rafael Rodr=EDguez Pappalardo wrote:
> J=FCrgen Kahrs wrote:
>
> > Rafael Rodr=EDguez 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 k=
ind
> >> 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 Jav=
a=2E
>
> 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 )
}

0
Reply William 9/14/2006 9:20:19 PM

William James wrote:

> 
> Rafael Rodríguez Pappalardo wrote:
>> Jürgen Kahrs wrote:
>>
>> > 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.
>>
>> 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)))
>> }
> 
> 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".

0
Reply Rafael 9/14/2006 10:10:01 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 ?
0
Reply UTF 9/14/2006 10:35:51 PM

8 Replies
85 Views

(page loaded in 0.163 seconds)

Similiar Articles:













7/26/2012 3:50:35 AM


Reply: