idlwrap¶
import idlwrap
idlwrap.indgen(3, 4)
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
:
import numpy as np
np.round(3.5), np.round(4.5), np.round(5.5), np.round(6.5)
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
idlwrap.round(3.5), idlwrap.round(4.5), idlwrap.round(5.5), idlwrap.round(6.5)
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