%install_ext https://raw.github.com/ebellm/ipython-idlmagic/master/idlmagic.py
When starting a new notebook, we load the magic:
%load_ext idlmagic
IDL not found, using GDL
(I am using GDL rather than IDL on this computer. idlmagic
will first look for the idl
interpreter on the search path and and fall back to gdl
if needed.)
The %idl
magic enables one-line execution of IDL commands in the IPython interpreter or notebook:
%idl print, findgen(5)
0.00000 1.00000 2.00000 3.00000 4.00000
Multi-line input can be entered with the %%idl
cell magic:
%%idl
x = findgen(5)
y = x^2.
; comments are supported
print, $ ; as are line continuations
mean(y)
% Compiled module: MEAN. 6.00000
The mechanisms for passing variables to and from IDL are based on those in the built-in %R
and %octave
magics.
Variables may be pushed from Python into IDL with %idl_push
:
msg = ' padded string '
import numpy as np
arr = np.arange(5)
%idl_push msg arr
%%idl
print, strcompress(msg,/REMOVE_ALL)
print, reverse(arr)
paddedstring 4 3 2 1 0
Similarly, variables can be pulled from IDL back to Python with %idl_pull
:
%idl arr += 1
%idl_pull arr
arr
array([1, 2, 3, 4, 5])
Variables can also be pushed and pulled from IDL inline using the -i
(or --input
) and -o
(or --output
) flags:
Z = np.array([1, 4, 5, 10])
%idl -i Z -o W W = sqrt(Z)
W
array([ 0.99999994, 1.99999988, 2.23606801, 3.1622777 ], dtype=float32)
Inline plots are displayed automatically by the IPython notebook. IDL Direct graphics are used. The optional -s width,height
argument (or --size
; default: 600,375
) specifies the size of the resulting png image.
%%idl -s 400,400
plot,findgen(10),xtitle='X',ytitle='Y'
% Compiled module: WRITE_PNG.
%idl
line magic fails with TypeError: coercing to Unicode: need string or buffer, dict found
in IPython 0.13.2 and below due to a known bug; IPython 1.0 and later should work as expected.