useR's, I need help getting a Fortran DLL successfully returning the correct output. The attached fortran source code (filter2d.f) and DLL (filter2d.dll) are attached. Also, I attached a text file for which I want to apply the filter to (time702.txt). Here is what I am doing...> dyn.load("C:/f/NEW/filter2d.dll") > is.loaded("filter2d")[1] TRUE 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". Basically, filter2d smooths 'array' by averaging within the window halfintx by halfinty. Can anyone please help me out or provide some clarification? THanks, dxc13 http://n4.nabble.com/file/n1595641/filter2d.f filter2d.f http://n4.nabble.com/file/n1595641/filter2d.dll filter2d.dll http://n4.nabble.com/file/n1595641/time702.txt time702.txt -- View this message in context: http://n4.nabble.com/Help-running-a-Fortran-subroutine-from-R-tp1595641p1595641.html Sent from the R help mailing list archive at Nabble.com.
dc896148 wrote:> > useR's, > I need help getting a Fortran DLL successfully returning the correct > output. The attached fortran source code (filter2d.f) and DLL > (filter2d.dll) are attached. Also, I attached a text file for which I > want to apply the filter to (time702.txt). > > Here is what I am doing... >> dyn.load("C:/f/NEW/filter2d.dll") >> is.loaded("filter2d") > [1] TRUE > > 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". > > Basically, filter2d smooths 'array' by averaging within the window > halfintx by halfinty. > Can anyone please help me out or provide some clarification? > >You have done a great job of showing us how you set up the call to Fortran- but you haven't clearly stated what the problem is. As far as I can tell from your post, "the Fortran DLL is not returning the correct output". Could you elaborate on what output, if any, you are getting and what output you were expecting? -Charlie ----- Charlie Sharpsteen Undergraduate-- Environmental Resources Engineering Humboldt State University -- View this message in context: http://n4.nabble.com/Help-running-a-Fortran-subroutine-from-R-tp1595641p1595739.html Sent from the R help mailing list archive at Nabble.com.
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.