如何在R中编写.bin文件并在Fortran中打开它 [英] How to write .bin file in R and open it in Fortran

查看:364
本文介绍了如何在R中编写.bin文件并在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的错在哪里对我来说是新的。任何建议?

解决方案

您试图打开文件为 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 

使用C双精度实数。



然后只是

  open(unit = 10,status ='old',file = 'test.bin',form ='unformatted',& 
access ='stream',action ='read')
read(10)a


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 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)

I can read the .bin file in R without problem again...

zz <- file("test.bin", "rb")
a1 = readBin(zz,"numeric", n=150)
close(zz)

my.array1 <- array(a1, dim=c(10,5,3)) 
my.array1

However when I try to open it in Fortran using the following code, the format is not recognized correctly.

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

I assume I am using the wrong format in the read command, but not sure where I am going wrong as Fortran is new to me. Any suggestions?

解决方案

You are trying to open the file as form='formatted'.

Formatted means human readable, that is, a text file.

You definitely need form='unformatted'. But the default would be access='sequential' which is not good for you, see https://stackoverflow.com/a/11570277/721644

You should use access='stream' (or more complicated access='direct').

The code from the linked answer can be adapted very easily to your problem. Just declare the array as:

real(rp), dimension (5,10,3) ::  a

and you must test which value of rp is the right one. I think the default real you tried is wrong and you need

use, intrinsic :: iso_c_binding

integer, parameter :: rp = c_double

to use the C double precision real.

Then just

open(unit = 10, status='old',file='test.bin', form='unformatted', &
     access='stream', action='read')  
read(10) a 

这篇关于如何在R中编写.bin文件并在Fortran中打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆