将存储在Fortran 90的二进制文件,以可读格式数据 [英] Converting data stored in Fortran 90 binaries to human readable format

查看:550
本文介绍了将存储在Fortran 90的二进制文件,以可读格式数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你的经验,在Fortran 90中,什么是存储大型阵列输出文件的最好方法? previously,我一直想写大型阵列,以ASCII文本文件。例如,我会在页面的<一个底部做这样的事情(感谢推荐href=\"http://stackoverflow.com/questions/6526112/in-fortran-90-what-is-a-good-way-to-write-an-array-to-a-text-file-row-wise\">In Fortran 90的,什么是写一个数组到一个文本文件的好方法,横行):

In your experience, in Fortran 90, what is the best way to store large arrays in output files? Previously, I had been trying to write large arrays to ASCII text files. For example, I would do something like this (thanks to the recommendation at the bottom of the page In Fortran 90, what is a good way to write an array to a text file, row-wise?):

PROGRAM testing1
  IMPLICIT NONE
  INTEGER :: i, j, k
  INTEGER, DIMENSION(4,10) :: a

  k=1
  DO i=1,4
    DO j=1,10
      a(i,j)=k
      k=k+1
    END DO
  END DO

  OPEN(UNIT=12, FILE="output.txt", ACTION="WRITE", STATUS="REPLACE")
  DO i=1,4
    DO j=1,10
      WRITE(12, "(i2,x)", ADVANCE="NO") a(i,j)
    END DO
    WRITE(12, *)
  END DO
  CLOSE(UNIT=12)
END PROGRAM testing1

这工作,但作为<一指出了最上面的回复href=\"http://stackoverflow.com/questions/6526112/in-fortran-90-what-is-a-good-way-to-write-an-array-to-a-text-file-row-wise\">In Fortran 90的,什么是写一个数组到一个文本文件的好方法,按行?,编写大型阵列,以文本文件很慢,创建面积略大于是必要的文件。海报有建议,而不是写一个未格式化的Fortran二进制文件,使用这样的:

This works, but as pointed out by the topmost reply at In Fortran 90, what is a good way to write an array to a text file, row-wise?, writing large arrays to text files is very slow and creates files that are somewhat larger in size than is necessary. The poster there recommended instead writing to an unformatted Fortran binary, using something like:

PROGRAM testing2
  IMPLICIT NONE
  INTEGER :: i, j, k
  INTEGER, DIMENSION(4,10) :: a

  k=1
  DO i=1,4
    DO j=1,10
      a(i,j)=k
      k=k+1
    END DO
  END DO

  OPEN(UNIT=13, FILE="output.dat", ACTION="WRITE", STATUS="REPLACE", &
      FORM="UNFORMATTED")
  WRITE(13) a
  CLOSE(UNIT=13)
END PROGRAM testing2

这似乎工作,而且的确是更快,更小的文件大小的结果,从回复<一个如许href=\"http://stackoverflow.com/questions/6526112/in-fortran-90-what-is-a-good-way-to-write-an-array-to-a-text-file-row-wise\">here.但是,我该怎么办,如果我想能够以后的工作与存储在Fortran语言的二进制数据(例如,output.dat以上),并分析其内容是什么?例如,如果我想打开存放在二进制程序中,如Microsoft Excel?

This seems to work, and is indeed much faster and results in smaller file sizes, as promised by the reply here. However, what do I do if I would like to be able to later work with the data stored in Fortran binary (e.g., output.dat above) and analyze its contents? For example, what if I want to open the array stored in the binary in a program such as Microsoft Excel?

当我在previous <一提到MATLAB href=\"http://stackoverflow.com/questions/6526112/in-fortran-90-what-is-a-good-way-to-write-an-array-to-a-text-file-row-wise\">post,回复建议我打开二进制为十六进制文件,并找出并从那里提取的记录。但是,我很紧张,我陷入深深的水,因为我在十六进制侦探没有现成的经验。当我问在MATLAB板(此处为:<一href=\"http://www.mathworks.com/matlabcentral/answers/12639-advice-on-reading-an-unformatted-fortran-binary-file-into-matlab\" rel=\"nofollow\">http://www.mathworks.com/matlabcentral/answers/12639-advice-on-reading-an-unformatted-fortran-binary-file-into-matlab)关于阅读的Fortran文件到MATLAB,人有建议使用的Fortran流可能很容易。但是,Fortran语言流(即使用指令 ACCESS =STREAM打开命令)可能会在时间和文件大小与我在我的上述第一个例子创建的ASCII文本文件相似?

When I mentioned matlab in my previous post, the reply suggested that I open the binary as a hexadecimal file and figure out and extract the records from there. But, I am nervous that I am getting into deep water since I have no prior experience in hexadecimal sleuthing. When I asked on the matlab board (here: http://www.mathworks.com/matlabcentral/answers/12639-advice-on-reading-an-unformatted-fortran-binary-file-into-matlab) about reading Fortran files into matlab, the person there suggested that using Fortran stream might be easy. But is Fortran stream (i.e., using the directive ACCESS="STREAM" in the OPEN command) likely to be similar in time and file size to the ASCII text file that I created in my first example above?

或者,你知道,如果有任何其他的软件,可以自动读取Fortran语言的二进制文件成某种可读形式的? (或者,你知道的十六进制侦探或Fortran语言流的任何好的教程吗?)

Or, do you know if there is any other software that can automatically read Fortran binaries into some sort of human readable form? (Or, do you know of any good tutorials on either hexadecimal sleuthing or Fortran stream?)

非常感谢您的宝贵时间。

Thank you very much for your time.

推荐答案

流是选择独立的格式化/格式化选择的 - 一个是访问,其他的格式Fortran的我的默认/​​ O是面向记录的访问。一个Fortran编译用于记录的典型的方法(至少未格式化)之前和每次记录后写一个四字节的记录长度。 (本经是让阅读更容易向后)使用十六进制编辑,你可以验证我描述这些额外的数据项及其在Matlab跳过它们。但是他们并不是语言标准的一部分,是不可移植的,而且肯定是不明显的其他语言。如果您选择流和未格式化的,你只会得到相应的数据项字节的原始序列 - 没有额外的数据项不用担心在其他语言!在我的经验,这个输出往往是很容易在其他语言(MatLab中没试过)来读取。如果这是一个小&安培;简单的项目与文件移植到其他计算机不是一个问题,我可能会使用这种方法(流和安培;未格式化),而不是一个文件格式规范,如HDF5或FITS。我会写数组作为写(13),作为最终的例子。根据其它语言,你可能要调换尺寸。如果这是便携一项重大而长期存在的项目的关注,那么便携式和标准的文件接口是值得考虑的。

Stream is a choice independent of the choice of formatted / unformatted -- one is "access", the other "format" The default for Fortran I/O is record oriented access. The typical approach of a Fortran compiler for records (at least unformatted) to write a 4-byte record length before and after each record. (The "after" is to make reading backwards easier.) Using a hex edit you could verify these extra data items that I described and skip them in MatLab. But they are not part of the language standard and are not portable and are certainly not obvious in other languages. If you select stream and unformatted you will just get the raw sequence of bytes corresponding to your data items -- no extra data items to worry about in the other language! In my experience this output tends to be fairly easy to read in other languages (not tried in MatLab). If this is a small & simple project with portability of the files to other computers not an issue, I would probably use this approach (stream & unformatted) rather than a file format specification such as HDF5 or FITS. I'd write the array as write (13) a, as in your final example. Depending on the other language, you might have to transpose the dimensions. If this is a major and long-lived project with portability a concern, then a portable and standard file interface is worth considering.

我不知道是否有任何这些格式可以从Excel读取。更多的研究....你可能需要编写一个程序来读取任何格式和输出在Excel中理解的格式文件的二进制文件。

I don't know whether any of these formats can be read from Excel. More research.... You might have to write a program to read the binary file of whatever format and output a file in a format that Excel understands.

这篇关于将存储在Fortran 90的二进制文件,以可读格式数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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