[R] .Fortran() again

simon chou sentientc at gmail.com
Thu Apr 21 12:36:17 CEST 2005


Hi,
First ,please excuse my poor English. Can someone help me on reading
fortran binary object under R?
I was trying to read mm5 data under R. However, I seem to stuck at
reading fortran binary file storing met. data array. At the beginning,
I used readBin() to read mm5 output directly with the following
command.
#mmout is a mmout file generated with mm5
>mm5file<-file("mmout","rb")
>readBin(mm5file,integer(),n=1,size=1)
This gives output of "0", which is expect for this file. However, when
I started to read the rest of the file, I cannot get the result as
expected. The next bit of data is an 50 by 20 integer array, which I
cannot read proporly. Here is the command I tried.
>readBin( mm5file,integer(),n=1)

Another way I have tried uses .Fortran (). The fortran code were.
1.f
     subroutine readmm5(mm5file,IFLAG,MIF)
      integer iflag,MIF(50,20),MRF(20,20)
      CHARACTER*80 mm5file,MIFC(50,20),MRFC(20,20)
      OPEN (11,FILE=mm5file,FORM='UNFORMATTED')
      READ(11) IFLAG
        IF ( IFLAG .EQ. 0 ) THEN
          READ(mm5file) MIF,RMRF,MIFC,MRFC
        endif
      end
$R CMD SHLIB 1.f
$R
>library(foreign)
>dyn.load("1.so")
>.Fortran("readmm5",as.character("mmout"),as.integer(1),as.matrix(nrow=50,ncol=20,data=as.integer(rep(0,50))))
This works alright until starts to read the 50 by 20 integer array.
The bit about using as.matrix() to read the array do seem suspicious.
I do not know what is the proper way to read it.
Thanks in advance,
lazysc




More information about the R-help mailing list