我正在使用Python以2D阵列的形式生成一些初始数据,例如' X'然后使用Fortran对它们进行一些计算。最初,当阵列大小约为10,000 x 10,000时,np.savetxt在速度方面表现良好。但是一旦我开始增加数组的维度,savetxt的速度就会显着降低。所以我尝试了np.save,这导致了更快的保存速度,但文件以.npy格式保存。我如何在Fortran中读取这样的文件来重建原始数组?从我所读过的内容来看,二进制文件通常会产生最低的空间消耗和最快的速度。
在Fortran 90中,
open(10,file='/home/X.npy')
read(10,*)X
我收到以下错误:Fortran运行时错误:列表输入的第1项中的实数错误
编辑: 在python中我这样做,
import numpy as np
x = np.linspace(-50,50,10)
y = np.linspace(-50,50,10)
X,Y = np.meshgrid(x,y)
np.save('X',X)
然后在Fortran中我这样做,
program read_unformatted_binary_from_python
use iso_c_binding
implicit none
integer, parameter :: DPR = selected_real_kind(p=15)
character(len=*), parameter :: filename = 'X.npy'
integer, parameter :: N = 10
real(DPR), allocatable, dimension(:, :) :: X
allocate(X(N, N))
open(40, file=filename, status='old', access='stream', form='unformatted')
read(40) X
close(40)
write(*,*) X
end program read_unformatted_binary_from_python
输出开始于1.8758506894003703E-309 1.1711999948023422E + 171 5.2274167985502976E-037 8.4474009688929314E + 252 2.6514123210345660E + 180 9.9215260506210473E + 247 2.1620996769994603E + 233 7.5805790251297605E-096 3.4756671925988047E-152 6.5549091408576423E-260 -50.000000000000000 - 38.888888888888886 -27.777777777777779等
使用.bin格式时不会发生此错误,仅在使用导致npy的np.save时才会出现。
答案 0 :(得分:2)
弗拉基米尔F是正确的,你想要' stream'访问原始二进制文件'文件在Fortran中。这是一个MWE:
的Python
import numpy as np
A = np.random.rand(10000, 10000)
print(A.sum())
A.tofile('data.bin')
的Fortran
program read_unformatted_binary_from_python
use iso_c_binding
implicit none
integer, parameter :: DPR = selected_real_kind(p=15)
character(len=*), parameter :: filename = 'data.bin'
integer, parameter :: N = 10000
real(DPR), allocatable, dimension(:, :) :: dat
allocate(dat(N, N))
open(40, file=filename, status='old', access='stream', form='unformatted')
read(40) dat
close(40)
write(*,*) sum(dat)
end program read_unformatted_binary_from_python
我的Fortran示例可能比必要的时间长一些,因为我使用了许多不同的系统和编译器套件,并且还讨厌大型静态数组(毕竟我是Fortran用户)。
我在MacBook Pro上使用Python 2.7.x,Numpy 13.x和来自Homebrew GCC 6.3.0_1的gfortran快速编写了这个,但这应该适用于所有系统。
更新:
这里需要特别注意阵列形状和尺寸。如果dat
被分配为大于文件中的内容,则流read
应尝试填充整个数组,请点击EOF
符号,然后发出错误。在Python中,np.fromfile()
方法将读取EOF
然后返回具有适当长度的一维数组,即使A
最初是多维的。这是因为原始二进制文件没有元数据,只是来自RAM的连续字节串。
因此,Python中的以下行生成相同的文件:
A = np.random.rand(10000, 10000)
A.tofile('file.whatever')
A.ravel().tofile('file.whatever')
A.reshape((100, 1000, 1000)).tofile('file.whatever')
该文件可以读入并重新整形为:
B = np.fromfile('file.whatever').reshape(A.shape)
B = np.fromfile('file.whatever').reshape((100, 1000, 100, 10))
# or something like
B = np.fromfile('file.whatever') # just a 1D array
B.resize(A.shape) # resized in-place
在Fortran中,使用流访问很容易读取整个原始文件而不知道它的大小,尽管您显然需要某种用户输入才能重塑数据:
program read_unformatted_binary_from_python
use iso_c_binding
implicit none
integer, parameter :: DPR = selected_real_kind(p=15)
character(len=*), parameter :: filename = 'data.bin'
integer :: N = 10000, bytes, reals, M
real(DPR), allocatable :: A(:,:), D(:, :), zeros(:)
real(DPR), allocatable, target :: B(:)
real(DPR), pointer :: C(:, :)
allocate(A(N, N))
open(40, file=filename, status='old', access='stream', form='unformatted')
read(40) A
write(*,*) 'sum of A', sum(A)
inquire(unit=40, size=bytes)
reals = bytes/8
allocate(B(reals))
read(40, pos=1) B
write(*,*) 'sum of B', sum(B)
! now reshape B in-place assuming the user wants the first dimension
! (which would be the outer dimension in Python) to be length 100
N = 100
if(mod(reals, N) == 0) then
M = reals/N
call C_F_POINTER (C_LOC(B), C, [N, M])
write(*, *) 'sum of C', sum(C)
write(*, *) 'shape of C', shape(C)
else
write(*,*) 'file size is not divisible by N!, did not point C to B'
end if
! now reshape B out-of-place with first dimension length 9900, and
! pad the result so that there is no size mismatch error
N = 9900
M = reals/N
if(mod(reals, N) > 0) M=M+1
allocate(D(N, M))
allocate(zeros(N), source=real(0.0, DPR))
D = reshape(B, [N, M], pad=zeros)
write(*,*) 'sum of D', sum(D)
write(*,*) 'shape of D', shape(D)
! obviously you can also work with shape(A) in fortran the same way you
! would use A.shape() in Python, if you already knew that A was the
! correct shape of the data
deallocate(D)
allocate(D, mold=A)
D = reshape(B, shape(A))
write(*,*) 'sum of D', sum(D)
write(*,*) 'shape of D', shape(D)
! or, just directly read it in, skipping the whole reshape B part
read(40, pos=1) D
write(*,*) 'sum of D', sum(D)
close(40)
end program read_unformatted_binary_from_python