tags:

views:

84

answers:

1

The following Fortran code fills a 2D array x with value v

      subroutine fill(x,v,m,n)
      real*8 x(m,n),v
      integer m,n,i
cf2py intent(in) :: x,v,m,n
      forall(i=1:m,j=1:n) x(i,j) = v
      end

When calling this function from Python:

x = numpy.array([[[0.0]]],order='F')
fill(x[:,:,0],2.0)
assert(x[0,0,0]==2.0) # Assertion failed

Why is this assertion failing ?

A: 

x should be declared as intent(inout) if you want it to pass values back to the caller.

However this causes an additional problem because passing array slices doesn't work for intent(inout) arrays. In this simple example you can get around it by calling from python:

fill(x, 2.0).

If you really wanted to pass a slice then you need to declare x as intent(in,out), and call from python: x[:,:,0] = fill(x[:,:,0],2.0)

The description of the different attributes can be found at:

http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#attributes

DaveP