idlwrap

In [1]:
import idlwrap

idlwrap.indgen(3, 4)
Out[1]:
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

idlwrap aims to abstract away all differences in IDL and python and provide the interface and functions you know from IDL, but using scipy and numpy under the hood. It helps you transitionning from IDL to python by providing all the little things which are special about IDL — but being powered entirely by python.

No IDL is required to run idlwrap, as it is pure python!

what does idlwrap do?

Let's see what idlwrap does by looking at an example. This piece of code is taken directly from the idlwrap source code:

def round(x):
    return np.trunc(x+np.copysign(0.5,x)).astype(int)

The idlwrap.round() replicates the behaviour of IDL's ROUND function, by using numpy internally. In IDL, ROUND uses half-away-from-zero, meaning that 3.5 is rounded to 4, and 4.5 is rounded to 5:

IDL> PRINT, ROUND(3.5), ROUND(4.5), ROUND(5.5), ROUND(6.5)
           4           5           6           7

Whereas in python/numpy, rounding works differently: they round half-to-even, as defined in the IEEE-754 standard, meaning both 3.5 and 4.5 are rounded to 4:

In [2]:
import numpy as np
np.round(3.5), np.round(4.5), np.round(5.5), np.round(6.5)
Out[2]:
(4.0, 4.0, 6.0, 6.0)

Now, you do not have to worry about these little beasts when porting an old IDL codebase to python. We worried about that already! Just import idlwrap and you're ready to go!

With idlwrap, you'd just write

In [3]:
idlwrap.round(3.5), idlwrap.round(4.5), idlwrap.round(5.5), idlwrap.round(6.5)
Out[3]:
(4, 5, 6, 7)

and you get exactly what you would have expected form IDL.

installing

idlwrap can be installed through pip

pip install idlwrap

or download or clone the source code from the repository:

git clone "https://github.com/r4lv/idlwrap"
cd "idlwrap"
python setup.py install
Fork me on GitHub