Contents of a f77 unformatted binary file

▼魔方 西西 提交于 2021-02-17 05:12:04

问题


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.

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.

I prefer to use C to access the file.


回答1:


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

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(record1 header), probably 12.
  • float32, float32, int32 (the three 'other values' you talked about)
  • uint32(record1 header, same as first value)
  • uint32(record2 header, probably 512^3*4)
  • float32*512^3
  • uint32(record2 header, same as before)

You might have to check endianness.

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.

Update:

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 -^

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.

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.

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()


来源:https://stackoverflow.com/questions/53710955/contents-of-a-f77-unformatted-binary-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!