[Numpy-discussion] example reading binary Fortran file

David Froger david.froger.info at gmail.com
Fri May 29 11:12:06 EDT 2009


I think the FortranFile class is not intended to read arrays written with
the syntax 'write(11) array1, array2, array3'  (correct me if I'm wrong).
This is the use in the laboratory where I'm currently completing a phd.

I'm going to dive into struc, FotranFile etc.. to propose something
convenient for people who have to read unformatted binary fortran file very
often.

2009/5/28 David Froger <david.froger.info at gmail.com>

> Sorry, I still don't understand how to use FortranFile ...
>
> ============
> The fortran code
> ============
>
> program writeArray
>
> implicit none
> integer,parameter:: nx=2,ny=5
> real(4),dimension(nx,ny):: ux,uy,p
> integer :: i,j
>
> do i = 1,nx
> do j = 1,ny
>    ux(i,j) = 100.  + j+(i-1.)*10.
>    uy(i,j) = 200. + j+(i-1.)*10.
>    p(i,j) = 300. + j+(i-1.)*10.
> enddo
> enddo
>
> open(11,file='uxuyp.bin',form='unformatted')
> write(11) ux,uy
> write(11) p
> close(11)
>
> end program writeArray
>
> =============
> The Python script
> =============
>
> from fortranfile import FortranFile
>
> f = FortranFile('uxuyp.bin')
>
> x = f.readReals()
>
> =============
> The output
> =============
>
> Traceback (most recent call last):
>   File "readArray.py", line 5, in <module>
>     x = f.readReals()
>   File
> "/home/users/redone/file2/froger/travail/codes/lib/Tests/fortranread/fortranfile.py",
> line 181, in readReals
>     data_str = self.readRecord()
>   File
> "/home/users/redone/file2/froger/travail/codes/lib/Tests/fortranread/fortranfile.py",
> line 128, in readRecord
>     raise IOError('Could not read enough data')
> IOError: Could not read enough dat
>
>
> => How to read the file 'uxuyp.bin' ?
>
>
> 2009/5/28 David Froger <david.froger.info at gmail.com>
>
> Thank you very much :-)
>>
>> 2009/5/28 Neil Martinsen-Burrell <nmb at wartburg.edu>
>>
>>> On 2009-05-28 09:32 , David Froger wrote:
>>>
>>>> Hy Neil Martinsen-Burrell,
>>>>
>>>> I'm trying the FortranFile class,
>>>> http://www.scipy.org/Cookbook/FortranIO/FortranFile
>>>>
>>>> It looks like there are some bug in the last revision (7):
>>>>
>>>>    * There are errors cause by lines 60,61,63 in
>>>>
>>>>    * There are indentation errors on lines 97 and 113.
>>>>
>>>
>>> There seem to have been some problems in putting the file on the wiki
>>> ("Proxy-Connection: keep-alive\nCache-Control: max-age=0" seems to come from
>>> an HTML communication).  I've attached my current version of the file to
>>> this email.  Let me know if you have problems with this.  I will try to get
>>> the working version up on the wiki.  Peace,
>>>
>>> -Neil
>>>
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090529/a1f61970/attachment.html>


More information about the NumPy-Discussion mailing list