我的MRedies NPY-for-Fortran 使用得还不够,但它似乎正是您要找的。p>
来自自述文件
Fortran 的 NPY
此 Fortran 模块允许将数字 Fortran 数组保存为 Numpy 的 .npy 或 .npz 格式。目前支持的有:
1. integer(1), integer(2), integer(4), integer(8)
2. real(4), real(8)
3. complex(4), complex(8)
*.npy 文件 将数组保存到 .npy 文件中只需调用:
call save_npy("filename.npy", array)
另外,请留意fortran standard library 上的这个pull request
另一个选项,我没有测试过
不是我的想法先看CAZT'sCAZT's stackoverflow answer评论
libnpy 似乎是一个库,它提供了使用 NumPy 的二进制格式将 C 或 Fortran 数组保存到数据文件的简单例程。
以下摘自https://scipy-cookbook.readthedocs.io/items/InputOutput.html
program fex
use fnpy
use iso_c_binding
implicit none
integer :: i
real(C_DOUBLE) :: a(2,4) = reshape([(i, i=1,8)], [2,4])
call save_double("fa.npy", shape(a), a)
end program fe
程序会创建一个文件fa.npy,您可以按照通常的方式将其加载到python中。
但 NumPy 数组的条目现在遵循 Fortran(列优先)排序。
>>> fa = np.load('fa.npy')
>>> print fa
[[ 1. 3. 5. 7.]
[ 2. 4. 6. 8.]]
您可以使用命令在与fex.f95 相同的目录中构建带有npy.mod 和libnpy.a 的可执行文件。
gfortran -o fex fex.f95 libnpy.a
仅供参考,我在https://github.com/kovalp/libnpy偶然发现了一个更新版本的libnpy
可能的错误
来自Matthias Redies
如果您使用 libnpy 保存一些数组切片 call save_npy(a(:,1,:)),您将不会高兴。