I’m a Python programmer looking to Fortran as a way to speed up code that couldn’t be handled by array operations with Numpy and SciPy. (There are of course many other ways you can do this but I was also motivated to learn Fortran to understand legacy code in my field, computational chemistry). My goal would be to include Fortran code under the hood to handle slow operations for my Python packages. This code could be packaged for example by having separate Fortran packages included with git submodules and then compiling platform specific packages and supplying them with conda/pip. I’ve explored a number of different alternatives of calling Fortran code from Python, all with their pros and cons.
Explicit C inteface
Coding explicit C interface in Fortran and calling that with Python libraries like ctypes, cffi etc. This is perfectly general but also quite annoying on the Fortran side in terms of having to write and maintain a separate API. The coding on the Python side is also annoying in terms of having to keep track of dimensions of the arrays sent back and forth etc. The implementations I have seen often uses a two-step procedure on the Python side, with a low-level interface and on top of that a more Pythonic one for the end user of the package.
Automatic C interface generation
Here some code generator takes care of writing the interface automatically, with alternatives like f2py, fmodpy etc. This is very convenient as you don’t have to write a separate API and the generated Python wrappers also take care of many things under the hood so that the programmer can directly send NumPy arrays. f2py is a mature project included in numpy, but doesn’t support Fortran derived types, and development seems to have halted completely. fmodpy seems very promising as although it still does not support derived types, it’s under active development and derived types are on the roadmap.
Probably one of the ways which Fortran will survive in the future is in this type of use case. Even if the interface generation question is solved I still see some issues in the short terms such as support for Windows which is expected by Python users.
Curious if anyone has any experience or views on this.