Python读取Fortran二进制文件

6
我正在尝试读取下面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)

谢谢大家,但我尝试了两种解决方案。在我的Fortran代码中有一个三维变量和实数*4精度。 - marcelorodrigues
3个回答

5

因为您正在将real*4数据写入顺序文件,所以只需尝试在read_reals()中将dtype=float替换为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

由于这是一种非标准的文件格式,其内容取决于编译器和机器的大小端,因此需要谨慎处理。已知来自x86_64上的gfortran 4.8.0和gfortran 4.1.2的文件可以正常工作。

考虑使用Fortran直接访问文件或来自较新流I/O的文件,后者可以轻松地通过numpy.fromfile进行读取。

根据情况,使用numpy.fromfile()可能会更简单(如StanleyR的答案所示)。


2

使用numpy.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.]]

3
你的回答似乎开始说:“你应该更改Fortran程序,使用直接输出而不是顺序输出。” 如果这是意图(这是一个重大变化),可能更好的做法是更明确地表达? - francescalus
是的,@francescalus是正确的,大多数情况下将Fortran代码更改为创建直接访问文件根本不是一个选项。而且,这个numpy解决方案对于经典的非格式化Fortran二进制文件是不起作用的。 - CoolKoon

-1

最简单的方法是使用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


1
这个问题是关于读取一个二进制文件的。 - Vladimir F Героям слава

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接