代码之家  ›  专栏  ›  技术社区  ›  Fabricio Fernández

我怎样才能阅读。Fortran程序中的原始文件?

  •  1
  • Fabricio Fernández  · 技术社区  · 6 年前

    我正在使用Fortran中的旧有限元代码。这是大学里的一个研究项目。

    我有个文件 未经加工的 表示三维图像的。

    中的数据 未经加工的 文件存储在 uint16 ,或在 uint8 ,并且整数的总数是已知的。

    如何在Fortran程序中将此图像读取到整数数组?

    像这样的

    allocate(imgarray(total_int))
    call raw2array(filename,imgarray)
    

    我目前正在使用python读取图像,并将其转换为整数向量。此向量在Fortran中通过文本文件读取。

    在python中

    imgarray = np.fromfile(fid, dtype=np.uint16,count=total_int,sep='')
    

    但在读取超过 1000x1000x1000 整数该过程变得非常缓慢。

    项目要求图像 未经加工的 在Fortran程序中作为完成向量的子程序以二进制形式读取。

    如何读取此图像(二进制)并使用Fortran中的子例程将其转换为整数向量?

    2 回复  |  直到 6 年前
        1
  •  1
  •   agentp    6 年前

    如果您的编译器支持16位整数和流访问,那么就这么简单

       use iso_fortran_env
       implicit none
       integer(kind=INT16), allocatable::m(:,:,:)
       allocate(m(1000,1000,1000))
       open(100,file='test.raw',access='stream')
       read(100)m
       end
    
        2
  •  0
  •   Fabricio Fernández    6 年前

    根据@agentp给出的解决方案 未经加工的 文件在数组中读取为:

    use iso_fortran_env
    implicit none
    integer(kind=INT16), allocatable::m(:)
    integer total_int
    total_int = 1000*1000*1000
    allocate(m(total_int))
    open(100,file='test.raw',access='stream')
    read(100)m