Python阅读Fortran二进制文件

时间:2016-05-30 22:13:39

标签: python file binary fortran

我正在尝试从下面的Fortran代码中读取二进制文件输出,但结果与输出文件的结果不同。

Fortran 77代码:

    program test
    implicit none
    integer i,j,k,l
    real*4       pcp(2,3,4)
    open(10, file='pcp.bin', form='unformatted')
    l = 0
    do i=1,2
      do j=1,2
        do k=1,2
          print*,k+l*2
          pcp(i,j,k)=k+l*2
          l = l + 1
        enddo
      enddo
    enddo
    do k=1,4
       write(10)pcp(:,:,k)
    enddo
    close(10)
    stop
    end

我正在尝试使用下面的Python代码:

from scipy.io import FortranFile
f = FortranFile('pcp.bin', 'r')
a = f.read_reals(dtype=float)
print(a)

3 个答案:

答案 0 :(得分:2)

由于您要在顺序文件上撰写real*4数据,只需尝试将dtype=float替换为read_reals()中的dtype='float32'(或dtype=np.float32):

>>> from scipy.io import FortranFile
>>> f = FortranFile( 'pcp.bin', 'r' )
>>> print( f.read_reals( dtype='float32' ) )
[  1.   9.   5.  13.   0.   0.]
>>> print( f.read_reals( dtype='float32' ) )
[  4.  12.   8.  16.   0.   0.]
>>> print( f.read_reals( dtype='float32' ) )
[ 0.  0.  0.  0.  0.  0.]
>>> print( f.read_reals( dtype='float32' ) )
[ 0.  0.  0.  0.  0.  0.]

获得的数据对应于Fortran中的每个pcp(:,:,k),由

验证
do k=1,4
   print "(6f8.3)", pcp(:,:,k)
enddo

给出(pcp初始化为零)

   1.0   9.0   5.0  13.0   0.0   0.0
   4.0  12.0   8.0  16.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0
   0.0   0.0   0.0   0.0   0.0   0.0

但是因为>>> help( FortranFile )

  

Fortran中未格式化的顺序文件的示例将写为::

     

OPEN(1, FILE=myfilename, FORM='unformatted')

     

WRITE(1) myvariable

     

由于这是一种非标准的文件格式,其内容取决于     编译器和机器的字节顺序,建议谨慎。来自的文件     已知gfortran 4.8.0和x86_64上的gfortran 4.1.2可以正常工作。

     

考虑使用Fortran直接访问文件或来自较新Stream的文件   I / O,numpy.fromfile可以轻松阅读。

根据案例使用numpy.fromfile()可能更简单(如StanleyR的回答所示)。

答案 1 :(得分:1)

使用nupy.fromfile(http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html

我猜你错过了fortran代码中的内容,写入二进制文件应用此代码:

program test
implicit none
integer i,j,k,l, reclen
real*4       pcp(2,3,4)

inquire(iolength=reclen)pcp(:,:,1)
open(10, file='pcp.bin', form='unformatted', access = 'direct', recl = reclen)
pcp = 0
l = 0
do i=1,2
do j=1,2
do k=1,2
   print*,i,j,k,k+l*2
   pcp(i,j,k)=k+l*2
   l = l + 1
enddo
enddo
enddo
do k=1,4
   write(10, rec=k)pcp(:,:,k)
enddo
close(10)
end

通过python读取文件:

import numpy as np
with open('pcp.bin','rb') as f:
    for k in xrange(4):
        data = np.fromfile(f, dtype=np.float32, count = 2*3)
        print np.reshape(data,(2,3))

输出:

[[  1.   9.   5.]
 [ 13.   0.   0.]]
[[  4.  12.   8.]
 [ 16.   0.   0.]]
[[ 0.  0.  0.]
 [ 0.  0.  0.]]
[[ 0.  0.  0.]
 [ 0.  0.  0.]]

答案 2 :(得分:0)

最简单的方法是使用 data_py 包。要安装,请输入 pip install data-py

示例使用

from data_py import datafile

NoOfLines=0   
lineNumber=2  # Line number to be read (Excluding lines starting with '#')
df1=datafile("C:/Folder/SubFolder/data-file-name.txt")
df1.separator=","  # No need to specify if separator is space(" "). For 'tab' separated values use '\t'
NoOfLines=df1.lines  # Total number of lines in the data file (Excluding lines starting with '#')
Col=["Null"]*5  # This will create 5 column variables with an intial string 'Null'. 
                # Number of column variables (here 5) should not be greater than number of columns in data file.
df1.read(Col,lineNumber) # Will read first five columns from the data file at the line number given, and stores in Col.
print(Col)  

详情请访问:https://www.respt.in/p/python-package-datapy.html