【问题标题】:Output as a matrix in fortran在fortran中输出为矩阵
【发布时间】:2015-03-24 05:23:00
【问题描述】:

我希望输出为二进制格式的矩阵,但我一直将其作为单个长数组获取。谁能建议我哪里出错了

             program ascii_to_binary
              implicit none

c  
c declarations
                 integer iw,jy,i
                 real  A(47,52),z(47,52),undef
                 real  x(47,52)
                 logical exist 


c----------------------------------------------------
c read in the index, insert name of file in input_file

               undef =  -9999.         

c read in the index, insert name of file in input_file

         inquire(file="weekly_mean_sce.txt", exist=exist)
         if (exist) then
         print *,"Exist"

         open(43,file='weekly_mean_sce.txt',status='old')

         do jy=1,47

         read(43,*) A(jy,:)


          enddo  
          print *, maxval(A)
         write(6,*)'read in input data' 

         else 
         print *,"not there"
         end if
         x=reshape(A,(/47,52/))


          OPEN(UNIT=15, FILE="sce.dat",ACTION="write")
          do i=1,47
          write(15, '(F9.2)')( real(x(i,iw)) ,iw=1,52)
          end do 
          write(15,"(F9.2)") x(1:47,1:52) 


          END PROGRAM ascii_to_binary

【问题讨论】:

  • 你想要哪种二进制格式?什么是“单长数组”?尝试access=stream 并摆脱这些格式。阅读有关未格式化 IO 的 fortran 手册。
  • 文件扩展名在 fortran 中没有意义(猜猜你为什么期望二进制)

标签: matrix binary fortran


【解决方案1】:

普通 Fortran I/O 使用记录结构。如果您希望在保持记录结构的同时未格式化 I/O,我会推荐

  open(15,file="sce.dat", form="unformatted")
  do iw=1,52    ! Better to use named constants here
     write (15) x(:,iw)
  end do

这会以自然的 Fortran 数组顺序写出值,其中存储顺序是 a(1,1)、a(2,1) 等,加上一些附加信息(记录标记)来识别记录的开始位置和它在哪里结束。

如果你不需要记录结构,你可以使用

  open(15,file="sce.dat",form="unformatted",access="stream")
  write (15) x

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多