0

I have a python main program that calls a subroutine from a fortran program, the subroutine works well when i write without the 'module, implicit none' sentence but doesnt work with it

heres a reproducible exaple of the code that works:

#data.csv is where i read the data from

a,b,c
1,5,0.3
2,8,0.6
3,9,0.7
4,16,0.2
5,23,0.4

#from python

import pandas as pd 
import pymod
from numpy import *
data = pd.read_csv('data.csv')
column = data.columns
x_data = data[column[0]].values
y_data = data[column[1]].values
data_len = len(data)
a, b, sa, sb, r_squared, sigma  = pymod.simple_lin_fit(x_data, y_data, data_len)
print(a, b, sa ,sb, r_squared)

#pymod.f90

subroutine simple_lin_fit(x_data, y_data, N, a, b, sa, sb, r_squared, sigma)    
    implicit none
    real(kind = 8), dimension(N) , intent(in) :: x_data, y_data
    real(kind = 8), intent(out) :: a, b, sa, sb, r_squared, sigma
    real(kind = 8) :: sum_x, sum_xx, sum_xy, sum_y, sum_yy, SSE, SXX, SXY, SYY
    integer, intent(in) :: N
    
    sum_x = sum(x_data)
    sum_y = sum(y_data)
    sum_xx = sum(x_data**2) 
    sum_yy = sum(y_data**2)
    sum_xy = sum(x_data*y_data)
    
    SXY = sum_xy - sum_x*sum_y/N
    SXX = sum_xx - sum_x**2/N
    SYY = sum_yy - sum_y**2/N

    a = SXY/SXX
    b = (sum_y - a*sum_x)/N

    SSE = sum((y_data-a*x_data-b)**2)

    sigma = sqrt(SSE/(N-2))
    r_squared = 1 - SSE/SYY

    sa = sigma/sqrt(SXX)
    sb = sa*sqrt(sum_xx/N)
end subroutine simple_lin_fit

it works very well, when i compile it, but it doesnt work when i put the scentences

module pymod
implicit none
contains
<subroutine>
end module

in it,and i need to write it as a fortran module because I need to add some other modules to the fortran module

the error i've got is this

Traceback (most recent call last):
  File "lin_fit.py", line 27, in <module>
    a, b, sa, sb, r_squared, sigma  = pymod.simple_lin_fit(x_data, y_data, N)
AttributeError: module 'pymod' has no attribute 'simple_lin_fit'

im compiling it with

f2py3 -m pymod -c pymod.f90 mv .so pymod.so

python3 pythonprogram.py

  • Welcome, please take the [tour]. I think we have a duplicate, there is a Fortran module inside the f2py module. Just examine the content of `pymod` and look for another `pymod` inside. – Vladimir F Героям слава Feb 18 '21 at 14:36
  • sorry for the delay, I edited the post with more reproducible code – Eric Cardozo Feb 18 '21 at 18:09
  • the error message that refers to simple_lin_fit rather than weighted_lin_fit was because i was trying with other subroutines in the fortran module and i copied the wrong error message, but i get the same error for all the routines in the f90 file, everything works perfect till i put the scentences 'module pymod; implicit none; contains; end module; in the f.90 file – Eric Cardozo Feb 18 '21 at 18:14
  • i also compiled the f90 module with the gfortran compiler and works fine, I think python is ignoring the subroutines in the module because of the 'contains' in it or something like that – Eric Cardozo Feb 18 '21 at 18:16
  • Did you actually look into the link https://stackoverflow.com/questions/8564771/compile-fortran-module-with-f2py?rq=1 and in the examples in my answer? Threre is another module for your Fortran module. You will have `pymod.pymod.simple_lin_fit`. I think my original duplicate vote was actually correct. – Vladimir F Героям слава Feb 18 '21 at 18:28
  • Just do `from pymod import pymod`. I really think it is a duplicate and my original closure was order @francescalus – Vladimir F Героям слава Feb 18 '21 at 18:37
  • Hey that last one worked!!!!!!!!!!!!!!!! thanks youuuuuuuuuu! – Eric Cardozo Feb 18 '21 at 18:57

0 Answers0