python code to fortran 77's

Larry larry.cebuala at gmail.com
Sun Mar 8 21:09:53 EDT 2009


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.



More information about the Python-list mailing list