f77无格式二进制文件的内容 [英] Contents of a f77 unformatted binary file

查看:60
本文介绍了f77无格式二进制文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个f77未格式化的二进制文件.我知道该文件包含2个浮点数和一个长整数以及数据.文件的大小为536870940字节,其中应包括512 ^ 3个浮点数据值以及2个浮点和长整数.512 ^ 3个浮点数据值组成536870912字节,剩下28个字节.

I have an f77 unformatted binary file. I know that the file contains 2 floats and a long integer as well as data. The size of the file is 536870940 bytes which should include 512^3 float data values together with the 2 floats and the long integer. The 512^3 float data values make up 536870912 bytes leaving a further 28 bytes.

我的问题是我需要弄清楚28个字节的开始位置以及如何跳过此存储量,以便我可以直接访问数据.

My problem is that I need to work out where the 28 bytes begins and how to skip this amount of storage so that I can directly access the data.

我更喜欢使用C来访问文件.

I prefer to use C to access the file.

推荐答案

不幸的是,未格式化的含义没有标准.但是某些方法比其他方法更常见.

Unfortunately, there is no standard what unformatted means. But some methods are more common than others.

在我使用过的许多Fortran版本中,每个 write 命令都会写一个标题(通常是无符号int 32),该标题包含数据的字节数,然后是数据,然后重复标题值,以防您从后面看.

In many Fortran versions I have used, every write command writes a header (often unsigned int 32) of how many bytes the data is, then the data, then repeats the header value in case you're reading from the rear.

根据您提供的值,可能是这样的:

From the values you have provided, it might be that you have something like this:

  • uint32(记录1标头),可能是12.
  • float32,float32,int32(您提到的三个其他值")
  • uint32(record1标头,与第一个值相同)
  • uint32(record2标头,可能为512 ^ 3 * 4)
  • float32 * 512 ^ 3
  • uint32(record2标头,与之前相同)

您可能必须检查字节序.

You might have to check endianness.

因此,我建议您在hexdump程序中打开文件,并检查字节0-3是否与字节16-19相同,以及是否再次在数据末尾重复字节20-23.

So I suggest you open the file in a hexdump program, and check whether bytes 0-3 are identical to bytes 16-19, and whether bytes 20-23 are repeated at the end of the data again.

如果是这种情况,我将尝试检查字节序,以查看值是小字节序还是大字节序,如果运气好的话,您将拥有自己的数据.

If that is the case, I'll try to check the endianness to see whether the values are little or big endian, and with a little luck you'll have your data.

注意:我假设这三个其他值是有关数据的元数据,因此将位于文件的开头.如果不是这种情况,您可能会在最后加上它们.

Note: I assume that these three other values are metadata about the data, and therefore would be at the beginning of the file. If that's not the case, you might have them at the end.

更新:

在您的评论中,您写道您的数据以这样的开头:

In your comment, you write that your data begins with something like this:

0C 00 00 00 XX XX XX XX XX XX XX XX XX XX XX XX 0C 00 00 00
^- header-^                                     ^-header -^
E8 09 FF 1F (many, many values) E8 09 FF 1F
^- header-^ ^--- your data ---^ ^-header -^

现在,我不知道如何在C语言中读取数据.我让您自己决定.您需要做的是跳过前24个字节,然后将数据读取为(可能是小端)4字节浮点值.您将剩下4个字节,不再需要.

Now I don't know how to read data in C. I leave this up to you. What you need to do is skip the first 24 bytes, then read the data as (probably little endian) 4-byte floating values. You will have 4 bytes left that you don't need any more.

重要说明:Fortran将数组存储为列主行,C afaik将其存储为行主行.因此请记住,索引的顺序将颠倒.

Important note: Fortran stores arrays column-major, C afaik stores them row-major. So keep in mind that the order of the indices will be reversed.

我知道如何在Python中阅读:

I know how to read this in Python:

from scipy.io import FortranFile
ff = FortranFile('data.dat', 'r', '<u4')
# read the three values you are not interested in
threevals = ff.read_record('<u4')
# read the data
data = ff.read_record('<f4')
ff.close()

这篇关于f77无格式二进制文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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