Search code examples
pythonfortranf2py

compiling basic fortran module with f2py


I am trying to use F2py but am getting some error messages or warnings on compilation involving `deprecated numpy'. I'm unsure how to fix this. I'm using python 2.7.17 and fortran90.

I compile by writing
f2py -c f2py_F90.f90 -m fortran

Note, compiling with : f2py -c f2py_F90.f90 -m fortran \
doesn't fix the problem either.

Below are the basic fortran and python codes that indicate the problem I'm having. This example is minimal, complete and verifiable. How can I fix this problem and succesfully have python execute the fortran module I'm passing into it?

The message I get is

warning "Using deprecated NumPy API"

The expected output should give a = [2,1,3] but instead I get the warning described above.

!fortran code
module fmodule
   implicit none
contains
   subroutine fast_reverse(a,n)

   integer, intent(in) :: n
   real, intent(inout) :: a(:)

   a(1:n) = a(n:1:-1)

   end subroutine fast_reverse
end module fmodule

#python code
import fortran
import numpy as np

a = np.array([1,2,3],np.float32)

fortran.fmodule.fast_reverse(a,2)

a #the output should give a = [2,1,3] 

Solution

    1. Ignore #warning "Using deprecated NumPy API, disable it with ..., see Cython Numpy warning about NPY_NO_DEPRECATED_API when using MemoryView and Cython docs;
    2. Change last line a #the output should give a = [2,1,3] to print(a) #the output should give a = [2,1,3] for printing a to stdout.

    For example:

    $ python2.7 -m numpy.f2py -c f2py_F90.f90 -m fortran
    running build
    ...
          /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h :it17 :with2 :"          "#define 
      NPY_NO_DEPRECATED_APIwarning : NPY_1_7_API_VERSION" [-W#warnings]
    
      "Using deprecated NumPy API, disable it with "          "#define
      NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
    #warning "Using deprecated NumPy API, disable it with " \
    ...
    
    $ cat test_f2py_F90.py 
    #python code
    import fortran
    import numpy as np
    
    a = np.array([1,2,3],np.float32)
    
    fortran.fmodule.fast_reverse(a,2)
    
    print(a) #the output should give a = [2,1,3]
    
    
    
    $ python2.7 test_f2py_F90.py 
    [2. 1. 3.]