IDL to Python Translation Guide
This part of the guide is going to be more personal and contains lessons from my
own (Joel Dunstan, SkyWa7ch3r) approx. 3 years
experience of translating the majority of FHD from IDL to Python to create pyfhd.
Introduction
Hello there weary traveler, you wish to translate from IDL to Python or have been
tasked with doing so, first thing my sincerest apologies for your future sanity
and also to your colleagues as you likely vent to them about the annoyances of
doing the translation. Here I’ll try to put all the discoveries, roadbumps and
potholes that will slow you down along the way. Be prepared to laugh, sigh, be
filled with rage and maybe shed a tear if you find out a problem you’ve been
facing for a week is because of some dumb interesting behaviour.
Getting IDL
First thing, you’ll need access to IDL, this is easier said than done as you
require a license to use the language if you’re lucky your instituion has an IDL
license or the HPC you use has an IDL license. If you plan on installing IDL to
your local machine you will need to contact
NV5 Geospatial directly to
get a license and get the download link through their locked download links. If
you’re institution has a license server you should be able to get a license from
the server and an install package from your institution. There are open source
versions of IDL, GDL and
Fawlty, however in my experience they didn’t
support running all the functions needed for FHD or produced different results
to what’s expected even in simple scenarios, hopefully that has changed and you
can try to use them, feel free to let me know how that went.
Now that you have IDL, you’ll need to follow the FHD install steps. You’ll notice that all the functions get added to the IDL path, IDL only really has one namespace for functions, do keep that in mind unlike Python which has packages/libraries, modules/subpackages which divide up the name spaces and require explicit imports.
FHD Dependency Equivalents
Now in terms of matching up some of the FHD dependencies and pyfhd dependencies,
anything from the FHD, fhdps_utils, and pipeline_scripts repositories will
require straight translation line by line. For the IDLAstro library from NASA,
a mix of astropy, numpy and scipy should allow you to either drectly replace
functions when they appear in the FHD code or allow much easier translation/rewriting
of the function. For the IDLAstro library all the pro files are publicly
available so if there is a need to directly translate the function into Python
that is possible. coyote does much of the graphical heavy lifting in FHD, for
any plots inside of pyfhd, the plotting/imaging libraries matplotlib,
seaborn, plotly, Pillow and OpenCV should get you what you need to make
almost any visualization you wish to make (plotly is excellent when you need
interactivity). For HEALPix you have the healpy library maintained by the
same organization, so functions should have similar names and similar purposes
without any translation needed.
The Right Tools for the Right Job
In terms of tools you can use to translate IDL to Python, after all this time,
unfortunately for you, I’ve decided the best tool is you. None of the tools
out there like idl2py successfully
translate to Python in a way that makes it faster than you translating the
function line by line, they even occasionally miss things like FLTARR which
translate perfectly into np.zeros. To be fair on these tools its an almost
impossible task to automate because in IDL keywords in functions can
entirely change how the function works thus any code you make to automatically
translate will be full of exceptions and will still have problems translating
pointer arrays due to the shapes of said arrays not always being easy to know
ahead of time. Unfortunately, not even ChatGPT or LLMs can completely help you
either, although to be honest they do a somewhat decent job, feel free to
experiment with them and let me know how it goes, although I suspect it will miss
some of the interesting behaviour that I will point out later. If you’re using
VSCode, NV5 recently brought out an official VSCode extension for IDL, check it
out here.
IDL for VSCode will give you proper syntax highlighting for pro files, and
hopefully they will continue to increase it’s usefulness in the future.
Another pair of tools that are worth talking about is the IDL to Python Bridge
and Python to IDL Bridge that is provided as a part of the IDL package. The
Python to IDL Bridge can provide some nice to have features such as running
IDL code in a Jupyter notebook which can be particularly useful for debugging
some IDL to Python translations because you can rerun just parts of the code
you’re having issues with translating. The IDL to Python Bridge does also make
it possible to use Python within IDL, while the Python to IDL Bridge makes it
possible to use IDL in Python. At the time of writing this its now possible to
pass objects to and from IDL in the Python to IDL Bridge, however I cannot
confirm if you can still only run the built-in IDL functions in the
Python to IDL Bridge. When I previously used the bridge I wasn’t able to run
functions made inside of FHD such as weight_invert (I suspected this was due
to the IDL_PATH being setup everytime you re-ran IDL.run for every command).
Hopefully if you try the bridges on the latest version of IDL, you may have
better luck meaning you could have more power for debugging than I did because
you can directly run the IDL code side by side and directly compare the IDL and
Python outputs without the need for sav files. Feel free to let me know how
this changes or to update this section yourself.
Thanks for Reading, and Ye Be Warned
I hope this contribution guide helps you in your translation efforts with much less pain than me, and all of this helps you get your function into Python as quick as possible. Translation and testing of IDL to Python code can be a frustrating task even at the best of times. So…
