[Numpy-discussion] example reading binary Fortran file

Neil Martinsen-Burrell nmb at wartburg.edu
Sun Feb 1 16:30:52 EST 2009


David Froger <david.froger.info <at> gmail.com> writes:

> Hy,My question is about reading Fortran binary file (oh no this question
> again...)

I've posted this before, but I finally got it cleaned up for the Cookbook.  
For this purpose I use a subclass of file that has methods for reading
unformatted Fortran data.  See
http://www.scipy.org/Cookbook/FortranIO/FortranFile.  I'd gladly see this in
numpy or scipy somewhere, but I'm not sure where it belongs.

> program makeArray

> implicit none
> integer,parameter:: nx=10,ny=20
> real(4),dimension(nx,ny):: ux,uy,p
> integer :: i,j
> open(11,file='uxuyp.bin',form='unformatted')
> do i = 1,nx
>   do j = 1,ny   
>     ux(i,j) = real(i*j)   
>     uy(i,j) = real(i)/real(j)   
>     p (i,j)  = real(i) + real(j)
>   enddo
> enddo
> write(11) ux,uy
> write(11) p
> close(11)
> end program makeArray

When I run the above program compiled with gfortran on my Intel Mac, 
I can read it back with::

>>> import numpy as np
>>> from fortranfile import FortranFile
>>> f=FortranFile('uxuyp.bin', endian='<')
>>> uxuy = f.readReals(prec='f') # 'f' for default reals
>>> len(uxuy)
400
>>> ux = np.array(uxuy[:200]).reshape((20,10)).T
>>> uy = np.array(uxuy[200:]).reshape((20,10)).T
>>> p = f.readReals('f').reshape((20,10)).T
>>> ux
array([[   1.,    2.,    3.,    4.,    5.,    6.,    7.,    8.,    9.,
          10.,   11.,   12.,   13.,   14.,   15.,   16.,   17.,   18.,
          19.,   20.],
       [   2.,    4.,    6.,    8.,   10.,   12.,   14.,   16.,   18.,
          20.,   22.,   24.,   26.,   28.,   30.,   32.,   34.,   36.,
          38.,   40.],
       [   3.,    6.,    9.,   12.,   15.,   18.,   21.,   24.,   27.,
          30.,   33.,   36.,   39.,   42.,   45.,   48.,   51.,   54.,
          57.,   60.],
       [   4.,    8.,   12.,   16.,   20.,   24.,   28.,   32.,   36.,
          40.,   44.,   48.,   52.,   56.,   60.,   64.,   68.,   72.,
          76.,   80.],
       [   5.,   10.,   15.,   20.,   25.,   30.,   35.,   40.,   45.,
          50.,   55.,   60.,   65.,   70.,   75.,   80.,   85.,   90.,
          95.,  100.],
       [   6.,   12.,   18.,   24.,   30.,   36.,   42.,   48.,   54.,
          60.,   66.,   72.,   78.,   84.,   90.,   96.,  102.,  108.,
         114.,  120.],
       [   7.,Proxy-Connection: keep-alive
Cache-Control: max-age=0

 14.,   21.,   28.,   35.,   42.,   49.,   56.,   63.,
          70.,   77.,   84.,   91.,   98.,  105.,  112.,  119.,  126.,
         133.,  140.],
       [   8.,   16.,   24.,   32.,   40.,   48.,   56.,   64.,   72.,
          80.,   88.,   96.,  104.,  112.,  120.,  128.,  136.,  144.,
         152.,  160.],
       [   9.,   18.,   27.,   36.,   45.,   54.,   63.,   72.,   81.,
          90.,   99.,  108.,  117.,  126.,  135.,  144.,  153.,  162.,
         171.,  180.],
       [  10.,   20.,   30.,   40.,   50.,   60.,   70.,   80.,   90.,
         100.,  110.,  120.,  130.,  140.,  150.,  160.,  170.,  180.,
         190.,  200.]])
>>> uy
array([[  1.        ,   0.5       ,   0.33333334,   0.25      ,
          0.2       ,   0.16666667,   0.14285715,   0.125     ,
          0.11111111,   0.1       ,   0.09090909,   0.08333334,
          0.07692308,   0.07142857,   0.06666667,   0.0625    ,
          0.05882353,   0.05555556,   0.05263158,   0.05      ],
       [  2.        ,   1.        ,   0.66666669,   0.5       ,
          0.40000001,   0.33333334,   0.2857143 ,   0.25      ,
          0.22222222,   0.2       ,   0.18181819,   0.16666667,
          0.15384616,   0.14285715,   0.13333334,   0.125     ,
          0.11764706,   0.11111111,   0.10526316,   0.1       ],
       [  3.        ,   1.5       ,   1.        ,   0.75      ,
          0.60000002,   0.5       ,   0.42857143,   0.375     ,
          0.33333334,   0.30000001,   0.27272728,   0.25      ,
          0.23076923,   0.21428572,   0.2       ,   0.1875    ,
          0.17647059,   0.16666667,   0.15789473,   0.15000001],
       [  4.        ,   2.        ,   1.33333337,   1.        ,
          0.80000001,   0.66666669,   0.5714286 ,   0.5       ,
          0.44444445,   0.40000001,   0.36363637,   0.33333334,
          0.30769232,   0.2857143 ,   0.26666668,   0.25      ,
          0.23529412,   0.22222222,   0.21052632,   0.2       ],
       [  5.        ,   2.5       ,   1.66666663,   1.25      ,
          1.        ,   0.83333331,   0.71428573,   0.625     ,
          0.55555558,   0.5       ,   0.45454547,   0.41666666,
          0.38461539,   0.35714287,   0.33333334,   0.3125    ,
          0.29411766,   0.27777779,   0.2631579 ,   0.25      ],
       [  6.        ,   3.        ,   2.        ,   1.5       ,
          1.20000005,   1.        ,   0.85714287,   0.75      ,
          0.66666669,   0.60000002,   0.54545456,   0.5       ,
          0.46153846,   0.42857143,   0.40000001,   0.375     ,
          0.35294119,   0.33333334,   0.31578946,   0.30000001],
       [  7.        ,   3.5       ,   2.33333325,   1.75      ,
          1.39999998,   1.16666663,   1.        ,   0.875     ,
          0.77777779,   0.69999999,   0.63636363,   0.58333331,
          0.53846157,   0.5       ,   0.46666667,   0.4375    ,
          0.41176471,   0.3888889 ,   0.36842105,   0.34999999],
       [  8.        ,   4.        ,   2.66666675,   2.        ,
          1.60000002,   1.33333337,   1.14285719,   1.        ,
          0.8888889 ,   0.80000001,   0.72727275,   0.66666669,
          0.61538464,   0.5714286 ,   0.53333336,   0.5       ,
          0.47058824,   0.44444445,   0.42105263,   0.40000001],
       [  9.        ,   4.5       ,   3.        ,   2.25      ,
          1.79999995,   1.5       ,   1.28571427,   1.125     ,
          1.        ,   0.89999998,   0.81818181,   0.75      ,
          0.69230771,   0.64285713,   0.60000002,   0.5625    ,
          0.52941179,   0.5       ,   0.47368422,   0.44999999],
       [ 10.        ,   5.        ,   3.33333325,   2.5       ,
          2.        ,   1.66666663,   1.42857146,   1.25      ,
          1.11111116,   1.        ,   0.90909094,   0.83333331,
          0.76923078,   0.71428573,   0.66666669,   0.625     ,
          0.58823532,   0.55555558,   0.52631581,   0.5       ]])
>>> p
array([[  2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,
         13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.],
       [  3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,
         14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.],
       [  4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.,
         15.,  16.,  17.,  18.,  19.,  20.,  21.,  22.,  23.],
       [  5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.,  15.,
         16.,  17.,  18.,  19.,  20.,  21.,  22.,  23.,  24.],
       [  6.,   7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.,  15.,  16.,
         17.,  18.,  19.,  20.,  21.,  22.,  23.,  24.,  25.],
       [  7.,   8.,   9.,  10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,
         18.,  19.,  20.,  21.,  22.,  23.,  24.,  25.,  26.],
       [  8.,   9.,  10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,
         19.,  20.,  21.,  22.,  23.,  24.,  25.,  26.,  27.],
       [  9.,  10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,
         20.,  21.,  22.,  23.,  24.,  25.,  26.,  27.,  28.],
       [ 10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,
         21.,  22.,  23.,  24.,  25.,  26.,  27.,  28.,  29.],
       [ 11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.,  20.,  21.,
         22.,  23.,  24.,  25.,  26.,  27.,  28.,  29.,  30.]])

Note that you have to provide the shape information for ux and uy because
fortran writes them together as a stream of 400 numbers.

-Neil





More information about the NumPy-Discussion mailing list