Python 读取 Fortran 二进制文件

问题描述 投票:0回答:3

我正在尝试从下面的 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)
python file binary fortran
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 直接访问文件或来自较新 Stream 的文件 I/O,可以通过

numpy.fromfile
轻松读取。

根据情况使用

numpy.fromfile()
可能会更简单(如Serenity的答案所示)。


2
投票

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

-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)  
© www.soinside.com 2019 - 2024. All rights reserved.