我想将输出作为二进制格式的矩阵,但我一直把它作为一个长数组。任何人都可以建议我出错的地方
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
答案 0 :(得分: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数组顺序写出值,其中存储顺序为(1,1),a(2,1)等,加上一些附加信息(记录标记)以标识记录的开始位置和它结束的地方。
如果您不需要记录结构,可以使用
open(15,file="sce.dat",form="unformatted",access="stream")
write (15) x