Compiling f90 function that returns array using f2py
<p>I have a subroutine that calculates a large array and writes it to a file. I'm trying to transform that into a function that returns that array. However, I'm getting a very weird error which seems to be connected to the fact that I am returning an array. When I try to return a float (as a test) it works perfectly fine.</p>

<p>Here's the MWE, which I call from python with <code>mwe('dir', 'postpfile', 150, 90.)</code>:</p>

<pre><code>FUNCTION mwe(dir, postpfile, nz, z_scale)
IMPLICIT NONE

INTEGER         :: nz
REAL(KIND=8)    :: z_scale
CHARACTER(len=100)      :: postpfile
CHARACTER(len=100)         :: dir
REAL(kind=8)  :: mwe

print*,'dir ', dir
print*,'postpfile ', postpfile
print*,'nz ', nz
print*,'Lz ', z_scale

mwe = 4.5d0
END FUNCTION mwe
</code></pre>

<p>This works well and prints, as expected:</p>

<pre><code> dir dir                                                                                                 
 postpfile postpfile                                                                                           
 nz          150
 Lz    90.000000000000000     
</code></pre>

<p>However, if I define the function as an array:</p>

<pre><code>FUNCTION mwe(dir, postpfile, nz, z_scale)
IMPLICIT NONE

INTEGER         :: nz
REAL(KIND=8)    :: z_scale
CHARACTER(len=100)      :: postpfile
CHARACTER(len=100)         :: dir
REAL(KIND=8),DIMENSION (2,23)   :: mwe

print*,'dir ', dir
print*,'postpfile ', postpfile
print*,'nz ', nz
print*,'Lz ', z_scale

mwe = 4.5d0
END FUNCTION mwe
</code></pre>

<p>Then it prints this:</p>

<pre><code> dir postpfile                                                                                           
 postpfile ï¿½ï¿½:ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½kï¿½ï¿½ 2ï¿½ï¿½ï¿½ï¿½V@(ï¿½ï¿½ï¿½ï¿½Hï¿½ï¿½ï¿½;ï¿½!ï¿½ï¿½v
 nz            0
Segmentation fault (core dumped)
</code></pre>

<p>I am running f2py version 2, NumPy 1.11.1 and Python 3.5.1.</p>

<p><strong>EDIT</strong></p>

<p>I'm compiling with <code>f2py -c -m fmwe fmwe.f90</code>, and calling the function with <code>mwe('dir', 'postpfile', 150, 90.)</code>.</p>

<p><em>I think the problem is coming from somewhere from lack of the explicit interface. (not sure may be someone else can point out what is the problem more precisely.)</em></p>

<p>Even though, I am not sure about my explanation, I have 2 working cases. <strong>Changing your function into a subroutine</strong> or <strong>putting your function inside a module</strong> (which generates the explicit interface by itself) solves the issue you mentioned. </p>

<p>Below script still can be called like <code>my_sub('dir', 'postpfile', 150, 90.)</code> from python.</p>

<pre><code>subroutine my_sub(mwe, dir, postpfile, nz, z_scale)
implicit none

integer,intent(in)             :: nz
real(KIND=8),intent(in)        :: z_scale
chracter(len=100),intent(in)   :: postpfile
character(len=100), intent(in) :: dir
real(KIND=8), intent(out)      :: mwe(2,23)

print*,'dir ', dir
print*,'postpfile ', postpfile
print*,'nz ', nz
print*,'Lz ', z_scale

mwe = 4.5d0
end subroutine my_sub
</code></pre>

<p>If you use the function within a module, you need to make the call from python a little differently; <code>test('dir', 'postpfile', 150, 90.)</code>.</p>

<pre><code>module test

contains 
function mwe(dir, postpfile, nz, z_scale)
implicit none

integer            :: nz
real(KIND=8)       :: z_scale
chracter           :: postpfile
character(len=100) :: dir
real(KIND=8)       :: mwe(2,23)

print*,'dir ', dir
print*,'postpfile ', postpfile
print*,'nz ', nz
print*,'Lz ', z_scale

mwe = 4.5d0
end function mwe

end module test
</code></pre>

<p><em>I did not try, but it will probably work with proper Fortran <code>interface</code> including your function.(assuming existence of the explicit interface is the key)</em></p>

<p>I hope that someone will complete/correct my answer.   </p>

python arrays fortran f2py