Gri*_*iff 7 python binary fortran numpy matrix
我在Fortran中写了一个矩阵,如下所示:
real(kind=kind(0.0d0)), dimension(256,256,256) :: dense
[...CALCULATION...]
inquire(iolength=reclen)dense
open(unit=8,file=fname,&
form='unformatted',access='direct',recl=reclen)
write(unit=8,rec=1)dense(:,:,:)
close(unit=8)
Run Code Online (Sandbox Code Playgroud)
我想把它读回Python.我见过的所有东西都是2D NxN阵列而不是3D阵列.在Matlab中我可以读作:
fid = fopen(nfilename,'rb');
mesh_raw = fread(fid,ndim*ndim*ndim,'double');
fclose(fid);
mesh_reshape = reshape(mesh_raw,[ndim ndim ndim]);
Run Code Online (Sandbox Code Playgroud)
我只需要Python中的等价物 - 可能有一个类似的加载/重塑工具可用.如果有一个更友好的紧凑方式来写出来让Python理解,我愿意接受建议.它可能看起来像这样:.我只是不熟悉我的情况的等效语法.一个很好的参考就足够了.谢谢.
使用IRO-bot的链接我为我的脚本修改/制作了这个(只有numpy魔法):
def readslice(inputfilename,ndim):
shape = (ndim,ndim,ndim)
fd = open(fname, 'rb')
data = np.fromfile(file=fd, dtype=np.double).reshape(shape)
fd.close()
return data
Run Code Online (Sandbox Code Playgroud)
我在立方体上做了一个mean,max,min和sum,它与我的fortran代码相匹配.谢谢你的帮助.