[R] Help running a Fortran subroutine from R

Berend Hasselman bhh at xs4all.nl
Wed Mar 17 12:16:13 CET 2010



dc896148 wrote:
> 
> Then I pass the parameters according to the order they are specified in
> the filter:
> array <- data.matrix(read.table("time702.txt",header=F))
> array1 <- array
> nx <- 60
> ny <- 120
> halfintx <- 3
> halfinty <- 3
> mask <- matrix(array(rep(1.0,25)),5,5)
> subarray <- matrix(0,5,5)
> subarray1 <- matrix(0,5,5)
> 
> Then I run the Fortran subroutine...
> out <- .Fortran("filter2d",
>       	as.single(array),
>             as.single(array),
> 		as.integer(nx),
> 		as.integer(ny),
> 		as.integer(halfintx),
> 		as.integer(halfinty),
> 		as.single(mask),
> 		as.single(subarray),
> 		as.single(subarray1))
> 
> The smoothed output is 'array1', which I just passed as 'array' in the
> specification.  It can be any matrix, but must be the same dimension as
> 'array', which is given as nx by ny.
> Missing values in time702.txt are denoted by 999.00, and are defined that
> way in the subroutine.
> 
> Can anyone see where I may be doing something wrong?  I am not good with
> fortran, as this code was previously written and now I am trying to adapt
> it to R "on the fly".
> 

First of all, if array1 is the output array of the fortran subroutine, then
you should do as.single(array1) instead of as.single(array) as the second
argument in the call of filter2d.

Furthermore you have set both halfintx and halfinty to 3.

Looking in the fortran code this means that subarray and subarray1 are
fortran arrays with dimension (7,7).

You should parametrize the definition of these arrays in the R code:

subarray <- matrix(0, 2*halfintx+1,2*halfinty+1)
subarray1 <- matrix(0, 2*halfintx+1,2*halfinty+1)

Similar for mask since it is indexed in fortran with indices -halfintx :
halfintx which means 2*halfintx+1 elements.

mask <-
matrix(array(rep(1.0,(2*halfintx+1)*(2*halfinty+1))),2*halfintx+1,2*halfinty+1)

(Hopefully no typos here; I haven't tested this).

Finally, I am not sure whether you can get array1 returned by the fortran as
a single precision real array.

I would consider using R numeric doubles. You would only have to change the
real in the Fortran with double precision and the 999 with 999D0.

And make a small example with required output to make testing easier.

Good luck

Berend

-- 
View this message in context: http://n4.nabble.com/Help-running-a-Fortran-subroutine-from-R-tp1595641p1596231.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list