How can I easily convert FORTRAN code to Python code (real code, not wrappers)
Asked Answered
M

2

20

I have a numerical library in FORTRAN (I believe FORTRAN IV) and I want to convert it to Python code. I want real source code that I can import on any Python virtual machine --- Windows, MacOS-X, Linux, Android. I started to do this by hand, but there are about 1,000 routines in the library, so that's not a reasonable solution.

Mayhew answered 14/8, 2013 at 3:3 Comment(6)
Sadly, there are no conversion tools (to my knowledge). Feel doubly bad for you... first, the number of lines you have to convert, but mostly because you have to deal with FORTRAN IV.Kylakylah
Even the oft-used f2py does not do a good job converting Fortran code to Python code, so there is definitely no easy method.Dose
@KyleKanos f2py doesn't convert Fortran to Python code.. it creates a C wrapper around the Fortran code that is then compiled to a shared library object that Python can import.Kylakylah
@SethMMorton: good to know, I wasn't aware of the how it worked. ThanksDose
This is almost certainly a bad idea. As well as being a huge waste of effort, the tight inner loops of the fortran code will result in extremely slow python code. It should be possible to compile your fortran code on any of those platforms using gfortran, then wrap it with f2py. The one down side is that for windows there aren't any good free 64 bit compilers, so you'll have to use 32 bit python.Smitherman
gfortran is not too bad - if the OP wants support for Android, I suspect he is not running "top500" applications... And anyway, if he's considering pure Python, he definitely does not want high speed.Ineffective
I
18

Such a tool exists for Fortran to Lisp, or Fortran to C, or even Fortran to Java. But you will never have a Fortran to Python tool, for a simple reason: unlike Fortran, Lisp or C, Python does not have GOTO [1]. And there are many GOTOs in Fortran (especially Fortran IV) code. Even if there is a theorem by Jacopini stating that you can emulate GOTO with structured programming, it's far too cumbersome to implement a real (and efficient) language conversion tool.

So not only will you need to translate the code of 1000 routines, but you will also need to understand each algorithm, with all its imbricated gotos, and translate the algorithm into a structured program before writing it in Python. Good luck!

Hey, why do you think a wrapper is bad? Windows, OSX, and Linux all have Fortran and C [2] compilers and good wrappers!

For C (not your language here, but f2c may be an option), there is SWIG, and Fortran has f2py, now integrated with numpy. SWIG has some support for Android.

By the way, instead of converting to "pure" Python, you can use NumPy: NumPy capabilities are similar to Fortran 90 (see a comparison here), so you may consider first translating your programs to F90 for a smoother transition. There seems to be also a Numpy on Adnroid. And in case you need NumPy on 64-bit Windows, there are binaries here.

If you decide to use wrappers, gfortran runs on Linux (simply install from distribution packages), Windows (MinGW), and Android. If you go along that line, don't forget you compile FIV code, so there is the usual "one-trip loop" problem (usually a compiler option is fine). You will probably have also to manually convert some old, non-standard statements, not found in modern compilers.

You have also, obviously, the option to switch your project language to Lisp or Java...

[1] You may ask: but if GOTO is the problem, how come there is a Fortran to Java tool? Well, it uses tricks with the JVM, which has internally the GOTO instruction. There is also a GOTO in Python bytecode (look for JUMP here), so there may be something to investigate here. So my previous statement is wrong: there may be a Fortran to Python tool, using bytecode tricks like in Java. But it remains to develop, and the availability of good libraries (like NumPy, matplotlib, pandas...) makes it unnecessary, to say the least.

Ineffective answered 14/8, 2013 at 5:57 Comment(3)
you can use goto to create structured program constructs. The converse does not follow, if the code willy nilly gotos all over the place it may not be possible to make it structured. If i had to tackle this task i would first try to convert to modern fortran (get rid of all goto's). It should quickly become evident if the code is nicely structured or not.Vinni
I'm working on code that's nearly over 20 years old (still maintained though) - and it has no GOTOs. I imagine that would be pretty common for any well-maintained fortran code, since GOTOs are really hard to maintain. A fortran-to-python converter would still be useful for all that code.Bender
@Bender I still believe it's better to write a wrapper, to benefit from both Fortran speed and Python nice I/O and GUI capabilities. Converting computing code to a language like Python, which is among the slowest in existence, is not the wise thing to do. There is a good reason why Python is used for glue code and interfacing, even (or especially!) in large projects, like Alliances. And integration of Fortran code to numpy offer a Matlab-like syntax with still your code as backend. I see only advantages to this approach.Ineffective
S
4

I wrote a translator that converts a subset of Fortran into Python (and several other languages). It is only compatible with a small subset of Fortran, but I hope it will still be useful.

The translator can parse this Fortran function:

LOGICAL function is_greater_than(a, b) 
    real,intent(in) :: a
    real,intent(in) :: b 
    is_greater_than = a<b
end function is_greater_than

...and translate it into this Python function:

def is_greater_than(a,b):
    return a<b
Strength answered 14/9, 2018 at 0:30 Comment(1)
Python officially have type annotations now, so it could be a good opportunity to make use of it.Jedjedd

© 2022 - 2024 — McMap. All rights reserved.