简体   繁体   English

从python编写fortran排序的二进制文件

[英]Writing fortran ordered binary files from python

I have some python code which generates a 256^3 numpy array of data that I'd like to read in with a different fortran code.我有一些 python 代码,它生成一个 256^3 的 numpy 数据数组,我想用不同的 fortran 代码读取这些数据。

This would be relatively easy if the numpy ndarray function tofile() had an option to write fortran ordered data, but it does not and will always write C-ordered data.如果 numpy ndarray 函数 tofile() 可以选择写入 fortran 有序数据,这将相对容易,但它不会并且将始终写入 C 有序数据。 Is there an equivalent function (in scipy maybe?) that will do what I want?是否有等效的功能(可能在 scipy 中?)可以执行我想要的操作?

您可以在写出之前transpose数组。

numpy.asfortranarray() could help you achieve your goal. numpy.asfortranarray()可以帮助您实现目标。

Assuming that data is a 256^3 numpy array, the code snippet is shown as follows假设data是一个256^3的numpy数组,代码片段如下所示

import numpy as np

np.asfortranarray(data).tofile('.../.../fortran_unformatted_data.dat')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM