python code to fortran 77's
John Machin
sjmachin at lexicon.net
Sun Mar 8 21:55:31 EDT 2009
On Mar 9, 12:09 pm, Larry <larry.cebu... at gmail.com> wrote:
> 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[i]+=data
>
> ============
> Sample output:
>
> >>> A
>
> 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
More information about the Python-list
mailing list