[SciPy-user] Help needed: Problem in translating a file read routine from Matlab to Python-Scipy

Travis Oliphant oliphant at ee.byu.edu
Tue Oct 12 12:01:26 EDT 2004


Guasina Luca wrote:

>I am trying to translate a Matlab program to Python - Scipy.
>The task consist of reading a big data file from a FORTRAN code; the
>file is written UNFORMATTED and contain a series of arrays.
>Anyone can help me ? I am new user of Python and Scipy...
>I am working in Windows XP with IDLE 1.02 and Python 2.3.3
>
>  
>
Binary files written from FORTRAN have special support in scipy.io

The expected usage is for you to open the file using

fid = io.fopen()
and then use fid.fort_read  (to handle the Fortran record mark 
automatically) or
fid.fread (to handle binary reading yourself).

The record you are skipping past is a 4-byte integer surrounding any 
data written using a single fortran write statement that gives the 
number of bytes written.
fid.fort_read will use this record to read the correct number of bytes, 
but it should not be necessary.

The attached luca.py file works on my Linux box just fine.  The output 
is also attached

What you did is less clean and not the expected form.  However it should 
have worked and probably would have if you would have opened the file in 
binary mode (this is handled automatically by io.fopen).  Windows 
unfortunately treats text files and binary files differently so you 
probably need to open the file using 'rb' .

But, I suggest you look at the attached luca.py file to see a more 
elegant way to do it and to realize that your elem data is redundant 
(and should be written using integers anyway).

-Travis Oliphant

-------------- next part --------------
A non-text attachment was scrubbed...
Name: luca.zip
Type: application/zip
Size: 8244 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20041012/d78f141a/attachment.zip>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: output
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20041012/d78f141a/attachment.ksh>


More information about the SciPy-User mailing list