python code to fortran 77's

L

Larry

Friends,

I need to read a binary file using a Fortran 77 code to integrate with
a legacy code.... It looked very much complicated to me for I have no
knowledge in Fortran.

I could read the file with ease using Python, as shown in the
following.

###################
from numpy import* #Importing modules
from struct import unpack

f = open('bindata', 'rb') #Opening binary file for reading
A = zeros(20) #Initializing "empty" array

for i in xrange(20):
data = unpack('f', f.read(4)) # Unpacking 32-bit data, C-float
A+=data

============
Sample output:
array([ 239., 309., 298., 280., 286., 250., 190., 200., 226.,
.
.
.
214., 243., 439., 565., 564., 546., 142., 87.,
118.])

######################

As you can see, data values are 4-byte long (float) and byte order is
little endian (Intel machine).

I intend to post this to a fortran users group but if you know, kindly
give a piece of advice.

Thanks to all those who will help.
 
J

John Machin

Friends,

I need to read a binary file using a Fortran 77 code to integrate with
a legacy code.... It looked very much complicated to me for I have no
knowledge in Fortran.

I could read the file with ease using Python, as shown in the
following.

###################
from numpy import*              #Importing modules
from struct import unpack

f = open('bindata', 'rb')               #Opening binary file for reading
A = zeros(20)                   #Initializing "empty" array

for i in xrange(20):
        data = unpack('f', f.read(4))           # Unpacking 32-bit data, C-float
        A+=data

============
Sample output:

array([ 239.,  309.,  298.,  280.,  286.,  250.,  190.,  200.,  226.,
        .
        .
        .
               214.,  243.,  439.,  565.,  564.,  546.,  142.,   87.,
118.])

######################

As you can see, data values are 4-byte long (float) and byte order is
little endian (Intel machine).

I intend to post this to a fortran users group but if you know, kindly
give a piece of advice.

Thanks to all those who will help.


Have you tried google("f77 read binary")?

Not much help with your f77 problem, but you might like to see a less
verbose way of doing it in Python:

from struct import unpack
f = open('bindata', 'rb')
a_tuple = unpack('<20f', f.read(80))

Cheers,
John
 
L

Larry

I need to read a binary file using a Fortran 77 code to integrate with
a legacy code.... It looked very much complicated to me for I have no
knowledge in Fortran.
I could read the file with ease using Python, as shown in the
following.
###################
from numpy import* #Importing modules
from struct import unpack
f = open('bindata', 'rb') #Opening binary file for reading
A = zeros(20) #Initializing "empty" array
for i in xrange(20):
data = unpack('f', f.read(4)) # Unpacking 32-bit data, C-float
A+=data

============
Sample output:
array([ 239., 309., 298., 280., 286., 250., 190., 200., 226.,
.
.
.
214., 243., 439., 565., 564., 546., 142., 87.,
118.])
######################

As you can see, data values are 4-byte long (float) and byte order is
little endian (Intel machine).
I intend to post this to a fortran users group but if you know, kindly
give a piece of advice.
Thanks to all those who will help.

Have you tried google("f77 read binary")?

Not much help with your f77 problem, but you might like to see a less
verbose way of doing it in Python:

from struct import unpack
f = open('bindata', 'rb')
a_tuple = unpack('<20f', f.read(80))

Cheers,
John



Thanks. Gonna try....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top