嗨,我是 Julia 的新手,我正在尝试用它进行一些处理。我有很多二进制文件。我使用F90进行处理。我写的文件是这样的
open(11,file="ring.grid",form="unformatted")
write(11)nv
write(11)Nx,Ny,Nzt
write(11) xx
write(11) yy
write(11) zz
close(11)
其中,nv,nx,ny,nzt 是整数,xx,yy,zz 是 3D 矩阵。在 F90 中我使用这个简单的块来读取文件
open(11,file='ring.grid',form='unformatted')
read(11)nd
read(11)n1,n2,n3
read(11)xx
read(11)yy
read(11)zz
close(11)
但是我很难和朱莉娅一起阅读它。有人可以帮我解决这个问题吗? 谢谢你
我尝试使用
io=read(NAMEFILE)
y=Array{UInt8,1}(undef,1)
read!(io,y)
但我不知道如何分离不同数组中的 nv,n1,n2,n3,xx,yy,zz
假设写入数据的完整 F90 代码如下所示:
program SaveGrid
implicit none
integer :: nv
integer :: Nx, Ny, Nzt
real, dimension(:), allocatable :: xx, yy, zz
integer :: i
nv = 3
Nx = 5
Ny = 7
Nzt = 10
allocate(xx(Nx), yy(Ny), zz(Nzt))
do i = 1, Nx
xx(i) = float(i)
end do
do i = 1, Ny
yy(i) = float(i) * 2.0
end do
do i = 1, Nzt
zz(i) = float(i) * 3.0
end do
open(unit=11, file="ring.grid", form="unformatted")
write(11) nv
write(11) Nx, Ny, Nzt
write(11) xx
write(11) yy
write(11) zz
close(11)
deallocate(xx, yy, zz)
end program SaveGrid
要将其读入 Julia,您需要处理 F90 写入未格式化数据的方式。
function read_fortran_binary(filename)
open(filename, "r") do file
# Read record marker.
record_size = read(file, Int32)
# Read data.
nv = read(file, Int32)
# Read record marker.
end_record_size = read(file, Int32)
# Read record marker.
read(file, Int32)
# Read data.
Nx = read(file, Int32)
Ny = read(file, Int32)
Nzt = read(file, Int32)
# Read record marker.
read(file, Int32)
return nv, Nx, Ny, Nzt
end
end
nv, Nx, Ny, Nzt = read_fortran_binary("ring.grid")
println("nv: ", nv)
println("Nx: ", Nx)
println("Ny: ", Ny)
println("Nzt: ", Nzt)
写入文件的每个数据块之前和之后都有记录标记。您也需要阅读这些内容。显然,您不需要忽略这些数据,并且记录大小可以通过适应未格式化的 F90 数据的变化来使您的 Julia 代码更加健壮和灵活。
nv: 3
Nx: 5
Ny: 7
Nzt: 10