Fortran二进制输出大于预期 [英] Fortran binary output bigger than expected

查看:83
本文介绍了Fortran二进制输出大于预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以NxMxL矩阵的二进制格式编写输出Fortran数据,如下所示

I am writing output Fortran data in binary format of an NxMxL matrix as follows

open(94, file = 'mean_flow_sp.dat', status = 'replace', action = 'write', form = 'unformatted')
  do k = 0,L-1
    do j = 0,M-1
      do i = 0,N-1
        write(94) u(i,j,k), v(i,j,k), w(i,j,k)
      enddo
    enddo
  enddo
close(94)

其中u, v, w是分配为例如的单精度值u(0:N-1,0:M-1,0:L-1).然后我按如下方式在Python中读取输出文件

where u, v, w are single precision values allocated as e.g. u(0:N-1,0:M-1,0:L-1). Then I read the output file in Python as follows

f = open('mean_flow_sp.dat', 'rb')
data = np.fromfile(file=f, dtype=np.single).reshape(N,M,L)
f.close()

我注意到的第一个奇怪的事情是输出的Fortran文件长10,066,329,600字节(使用L = 640,M = 512,N = 1536).那么问题来了,为什么这个文件的长度不是1536 * 512 * 640 * 3(变量)* 4(字节)= 6,039,797,760字节长?

The first odd thing I notice is that the output Fortran file is 10,066,329,600 bytes long (this is using L = 640, M = 512, N = 1536). So the question is why this file is not 1536*512*640*3(variables)*4(bytes) = 6,039,797,760 bytes long?

很明显,当尝试重整读取数据的形状时,Python脚本抛出了一个错误,因为它不是NxLxM x3的大小(单精度).

Obviously, the Python script throws me an error when trying to reshape the read data as is not of the size of NxLxM x3 (in single precision).

为什么输出文件这么大?

Why is the output file so big?

推荐答案

好,所以我刚刚意识到,正如

Ok, so I just realized that, as posted here, "Fortran compilers typically write the length of the record at the beginning and end of the record.", so then the size of the output file checks out.

这篇关于Fortran二进制输出大于预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆