我是一名有用的助手,可以翻译文本。
我经常在Fortran中编写数据,然后在Python中读取。这里我将为多个3D数组的情况进行解释,因为它更通用且更易于适应1D数组。
从Fortran中,我使用以下循环写入数据:
file = 'my_fortran_data.dat'
open(99, file=file, status = 'replace', action = 'write', form = 'unformatted', access='stream')
do k = 1,L
do j = 1,M
do i = 1,N
write(99) u(i,j,k), v(i,j,k), w(i,j,k)
end do
end do
end do
close(99)
请注意,我使用流式访问方式编写我的数据。因此,我不记录每个记录的开头和结尾(从而使文件大小更小)。
从Python中,我使用以下函数读取数据:
def read_data(file, dtype, stream):
"""
Return the components of a 3D vector field stored in binary format.
The data field is supposed to have been written as: (for k; for j; for i;) where the last dimension
is the quickest varying index. Each record should have been written as: u, v, w.
The returned components are always converted in np.double precision type.
Args:
dim: number of dimensions
dtype: numpy dtype object. Single or double precision expected.
stream: type of access of the binary output. If true, the file can only contain data.
If false, there is a 4-byte header and footer around each "record"
in the binary file (can happen in some Fortran compilers if access != 'stream').
"""
if stream:
shape = (L, M, N, 3)
f = open(file, 'rb')
data = np.fromfile(file=f, dtype=dtype).reshape(shape)
f.close()
u = data[:, :, :, 0].transpose(2, 1, 0)
v = data[:, :, :, 1].transpose(2, 1, 0)
w = data[:, :, :, 2].transpose(2, 1, 0)
del data
else:
shape = (L, M, N, 5)
f = open(file, 'rb')
data = np.fromfile(file=f, dtype=dtype).reshape(shape)
f.close()
u = data[:, :, :, 1].transpose(2, 1, 0)
v = data[:, :, :, 2].transpose(2, 1, 0)
w = data[:, :, :, 3].transpose(2, 1, 0)
del data
u = u.astype(np.float64, copy=False)
v = v.astype(np.float64, copy=False)
w = w.astype(np.float64, copy=False)
return(u, v, w)
请注意,我总是将数据转换为双精度,但如果不需要,您可以省略这一步骤。
对于您的情况,请使用
shape=(10,2)
来访问
stream
,否则请使用
shape=(10,4)
。
FortranFile
一次只能读取一个记录。请使用循环。 - Amadan