[Numpy-discussion] Fortran binary files and numpy/scipy

Nils Wagner nwagner at iam.uni-stuttgart.de
Mon Feb 2 14:18:25 EST 2009


On Mon, 02 Feb 2009 10:17:13 -0600
  Ryan May <rmay31 at gmail.com> wrote:
> Nils Wagner wrote:
>> On Mon, 2 Feb 2009 13:39:32 +0100
>>  Matthieu Brucher <matthieu.brucher at gmail.com> wrote:
>>> Hi,
>>>
>>> There was a discussion about this last week. You can 
>>>find it int he
>>> archives ;)
>>>
>>> Matthieu
>>  
>> Hi Matthieu,
>> 
>> Sorry but I missed that.
>> Anyway I have some trouble with my short example.
>> 
>> g77 -c binary_fortran.f
>> g77 -o io binary_fortran.o
>> ./io
>> 
>>  11 254  254.
>>  12 253  126.
>>  13 252  84.
>>  14 251  62.
>>  15 250  50.
>>  16 249  41.
>>  17 248  35.
>>  18 247  30.
>>  19 246  27.
>>  20 245  24.
>> 
>> python -i read_fortran.py
>> 
>>>>> a
>> array([(16, 1090921693195, 254.0), (16, 16, 
>>5.3686493512014268e-312),
>>        (4638566878703255552, 16, 
>>7.9050503334599447e-323),
>>        (1082331758605, 4635611391447793664, 
>>7.9050503334599447e-323),
>>        (16, 1078036791310, 62.0), (16, 16, 
>>5.3049894774872906e-312),
>>        (4632233691727265792, 16, 
>>7.9050503334599447e-323),
>>        (1069446856720, 4630967054332067840, 
>>7.9050503334599447e-323),
>>        (16, 1065151889425, 35.0), (16, 16, 
>>5.2413296037731544e-312),
>>        (4629137466983448576, 16, 
>>7.9050503334599447e-323),
>>        (1056561954835, 4628293042053316608, 
>>7.9050503334599447e-323),
>>        (16, 1052266987540, 24.0)],
>>       dtype=[('irow', '<i8'), ('icol', '<i8'), ('value', 
>>'<f8')])
>> 
>> How can I fix the problem ?
>> 
> 
> Every write statement in fortran first writes out the 
>number of bytes that will
> follow, *then* the actual data.  So, for instance, the 
>first write to file in
> your program will write the bytes corresponding to these 
>values:
> 
> 16 X(1) Y(1) Z(1)
> 
> The 16 comes from the size of 2 ints and 1 double. 
> Since you're always writing
> out the 3 values, and they're always the same size, try 
>adding another integer
> column as the first field in your array.
> 
> Ryan
> 
> -- 
> Ryan May
> Graduate Research Assistant
> School of Meteorology
> University of Oklahoma
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
  
Hi Ryan,

I have modified the python script.

import numpy as np
fname = open("bin.dat",'rb')
dt = 
np.dtype([('isize',int),('irow',int),('icol',int),('value',float)])
a  = np.fromfile(fname,dtype=dt)


>>> a
array([(16, 1090921693195, 4643140847074803712, 
7.9050503334599447e-323),
        (16, 1086626725900, 4638566878703255552, 
7.9050503334599447e-323),
        (16, 1082331758605, 4635611391447793664, 
7.9050503334599447e-323),
        (16, 1078036791310, 4633922541587529728, 
7.9050503334599447e-323),
        (16, 1073741824015, 4632233691727265792, 
7.9050503334599447e-323),
        (16, 1069446856720, 4630967054332067840, 
7.9050503334599447e-323),
        (16, 1065151889425, 4630122629401935872, 
7.9050503334599447e-323),
        (16, 1060856922130, 4629137466983448576, 
7.9050503334599447e-323),
        (16, 1056561954835, 4628293042053316608, 
7.9050503334599447e-323),
        (16, 1052266987540, 4627448617123184640, 
7.9050503334599447e-323)],
       dtype=[('isize', '<i8'), ('irow', '<i8'), ('icol', 
'<i8'), ('value', '<f8')])

Is this a 64-bit problem ?

Nils



More information about the NumPy-Discussion mailing list