如何在R中编写.bin文件并在Fortran中打开它 [英] How to write .bin file in R and open it in Fortran
问题描述
我有一个Fortran模型,我想强制使用来自R开发模型的数据,但是我无法在两种环境之间传输数据。我正在考虑使用.bin文件。
我在R中所做的是使用 writeBin
来存储multi-dim数组在.bin文件中:
a = seq(1,150,1)
my.array< ; - 数组(a,dim = c(10,5,3))
zz< - file(test.bin,wb)
writeBin(as.vector (my.array),zz)
close(zz)
我可以阅读。在R中没有问题的bin文件...
zz< - file(test.bin,rb)
a1 = readBin(zz,numeric,n = 150)
close(zz)
my.array1< - array(a1,dim = c(10, 5,3))
my.array1
但是,当我尝试在Fortran中打开它时使用下面的代码,格式不能正确识别。
PROGRAM游乐场
隐式无
!类型声明
实数,维(5,10,3):: a
!读取二进制
open(unit = 10,status ='old',file ='test.bin',form ='formatted',action ='read')
read(10,(f11。 3))a
print *,a
END PROGRAM操场
程序操场
我假设我在 read
命令中使用了错误的格式,但不确定Fortran的错在哪里对我来说是新的。任何建议?
您试图打开文件为 格式化意味着人类可读,即文本文件。 您绝对需要 您应该使用 很容易适应你的问题。只需声明数组为: 您必须测试 使用C双精度实数。 然后只是 I have a Fortran model that I would like to force with data coming from a model developed in R, but I have trouble transferring the data between the two environments. I was thinking about using a .bin file. What I am doing in R is using I can read the .bin file in R without problem again... However when I try to open it in Fortran using the following code, the format is not recognized correctly. I assume I am using the wrong format in the You are trying to open the file as Formatted means human readable, that is, a text file. You definitely need You should use The code from the linked answer can be adapted very easily to your problem. Just declare the array as: and you must test which value of to use the C double precision real. Then just
这篇关于如何在R中编写.bin文件并在Fortran中打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! form ='formatted'$
形式= '无格式'
。但是默认值是 access ='sequential'
这对你并不好,参见 https://stackoverflow.com/a/11570277/721644
access ='stream'
(或者更复杂的 access ='direct'
)。
real(rp),dimension(5,10,3):: a
rp
的哪个值是正确的。我认为您尝试的默认实际是错误的,您需要使用内部:: iso_c_binding
integer,parameter :: rp = c_double
open(unit = 10,status ='old',file = 'test.bin',form ='unformatted',&
access ='stream',action ='read')
read(10)a
writeBin
to store a multi-dim array in a .bin file:a = seq(1,150,1)
my.array <- array(a, dim=c(10,5,3))
zz <- file("test.bin", "wb")
writeBin (as.vector(my.array),zz)
close(zz)
zz <- file("test.bin", "rb")
a1 = readBin(zz,"numeric", n=150)
close(zz)
my.array1 <- array(a1, dim=c(10,5,3))
my.array1
PROGRAM playground
implicit none
! Type declarations
real, dimension (5,10,3) :: a
! read binary
open(unit = 10, status='old',file='test.bin', form='formatted', action='read')
read(10,"(f11.3)") a
print*,a
END PROGRAM playground
PROGRAM playground
read
command, but not sure where I am going wrong as Fortran is new to me. Any suggestions?form='formatted'
.form='unformatted'
. But the default would be access='sequential'
which is not good for you, see https://stackoverflow.com/a/11570277/721644access='stream'
(or more complicated access='direct'
).real(rp), dimension (5,10,3) :: a
rp
is the right one. I think the default real you tried is wrong and you needuse, intrinsic :: iso_c_binding
integer, parameter :: rp = c_double
open(unit = 10, status='old',file='test.bin', form='unformatted', &
access='stream', action='read')
read(10) a