Skip to content

.Fortran() again

2 messages · Simon Blomberg, Martin Maechler

#
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
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.
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
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
#
simon> Hi, First ,please excuse my poor English. 

no problem - we have read much worse ;-)

  <...........>


    simon> Another way I have tried uses .Fortran (). The fortran code were.
    simon> 1.f

    simon> subroutine readmm5(mm5file,IFLAG,MIF)
    simon> integer iflag,MIF(50,20),MRF(20,20)
    simon> CHARACTER*80 mm5file,MIFC(50,20),MRFC(20,20)
    simon> OPEN (11,FILE=mm5file,FORM='UNFORMATTED')
    simon> READ(11) IFLAG
    simon> IF ( IFLAG .EQ. 0 ) THEN
    simon> READ(mm5file) MIF,RMRF,MIFC,MRFC
    simon> endif
    simon> end

    simon> $R CMD SHLIB 1.f
    simon> $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))))

    simon> This works alright until starts to read the 50 by 20 integer array.
    simon> The bit about using as.matrix() to read the array do seem suspicious.
{indeed!}

    simon> I do not know what is the proper way to read it.

Instead of your as.matrix(..) you could use matrix(),
also you should *assign* the result of .Fortran()
and you should use your space keybar to make things more
readable.

This leads to
    r <- .Fortran("readmm5",
                  file =  as.character("mmout"),
		  iflag = as.integer(1),
		  mif = matrix(as.integer(0), nrow=50,ncol=20))

where r is list where  r[["mif"]] should be your desired matrix.
Alternatively, in your case,
    mif <- .Fortran("readmm5",
	            file =  as.character("mmout"),
		    iflag = as.integer(1),
		    mif = matrix(as.integer(0), nrow=50,ncol=20))[["mif"]]

Hoping this helps further,
Martin