Recently, I heavily made use of <code>f2py</code>—a tool that converts your FORTRAN code into a Python module.
Here is how to use it:
$ f2py -c -m module_name fortran_code.f90
In the current directory there is now a shared object of your module module_name.so
.
To use it in Python, just do the usual import. A docstring is generated for you as well, so you know how to call the methods, i.e., FORTRAN subroutines or functions
import module_name as mymodule
print mymodule.__doc__
Let's suppose you've written a subroutine that does some calculations that are faster in FORTRAN than in Python
! fortran_code.f90
subroutine some_calulations(a,b,c)
implicit none
real, intent(in) :: a, b
real, intent(out) :: c
real :: some_result
! start some calculations
...
c = some_result
end subroutine
To call that subroutine, our Python script has to look like this
import module_name as mymodule
print mymodule.__doc__
x1 = <some value>
x2 = <also some value>
result = mymodule.some_calculations(x1,x2)
That's it. Pretty easy, huh?
Now you can use your fast code and let Python do the rest, like making a proper data analysis using numpy
or scipy
, or doing the graphics using matplotlib
.