Ten little IDL programs
posted Thu 18 Feb 2016 by Michael Galloy under IDLHere are ten little programs of ten lines or less to introduce new programmers to IDL. This post[1] is motivated by this comp.lang.python thread which became this page on the Python wiki.
1 line: output, calling a procedure:
print, 'Hello, world!'
2 lines: assignment, calling a function, system variables, array operations, keywords:
x = findgen(360) * !dtor
plot, x, sin(x), xstyle=9, ystyle=8
3 lines: input, output format codes:
name = ''
read, 'What is your name? ', name
print, name, format='("Hello, ", A, "!")'
5 lines: filename handling, reading images, variable metadata, boolean keywords, displaying image:
filename = filepath('people.jpg', subdir=['examples', 'data'])
im = read_image(filename)
dim = size(im, /dimensions)
window, xsize=dim[1], ysize=dim[2], /free, title=file_basename(filename)
tv, im, true=1
6 lines: logical unit numbers, read binary data, contour plots, line continuation:
convec = bytarr(248, 248)
openr, lun, file_which('convec.dat'), /get_lun
readu, lun, convec
free_lun, lun
window, xsize=500, ysize=500, /free
contour, convec, xstyle=1, ystyle=1, $
levels=(max(convec) - min(convec)) * findgen(10) / 9. + min(convec)
7 lines (contributed by Mark Piper): query image, image processing, automatic positioning of images:
file = file_which('moon_landing.png')
!null = query_image(file, info)
astronaut = read_image(file)
window, /free, xsize=3 * info.dimensions[0], ysize=info.dimensions[1]
tv, astronaut, 0
tvscl, sobel(astronaut), 1
tvscl, canny(astronaut), 2
8 lines: writing a function, compile_opt
statement, if
statements, for
loops:
.compile
function mg_fibonacci, x
compile_opt strictarr
if (x eq 0) then return, 0
if (x eq 1) then return, 1
return, mg_fibonacci(x - 1) + mg_fibonacci(x - 2)
end
for i = 0L, 20L do print, i, mg_fibonacci(i)
9 lines (contributed by Mark Piper): array generation, FFTs, line plots, multiple plots/window, query for screen size:
x = (2.0 * !pi) * findgen(100) / 100
y = sin(3.0 * x) + cos(12.0 * x) + cos(25.2 * x)
magspec_y = abs(fft(y))
ss = get_screen_size()
window, /free, xsize=0.4 * ss[0], ysize=0.25 * ss[1]
!p.multi = [0,2,1]
plot, x, y, xtitle='sample number', ytitle='value', title='Series', xstyle=1
plot, magspec_y, xtitle='mode', ytitle='spectral density', $
title='Magnitude Spectrum', xrange=[0, n_elements(x) / 2], xstyle=1<br />!p.multi = 0
10 lines: maps, read ASCII file, indexed color, structures:
header = strarr(5
data = replicate({loc:fltarr(2), elev:0, temp:0, dew:0, wspd:0, wdir:0}, 15)
openr, lun, file_which('ascii.txt'), /get_lun
readf, lun, header<br />readf, lun, data
free_lun, lun
device, decomposed=0
loadct, 5
map_set, limit=[min(data.loc[1, *], max=maxlat), $
min(data.loc[0, *], max=maxlon), maxlat, maxlon], /mercator, /usa
plots, data.loc[0, *], data.loc[1, *], psym=4, color=bytscl(data.temp), $
symsize=2., thick=2
I’m not sure why, but I’ve had a draft of this post around for almost seven years. ??
February 18th, 2016 at 9:18 pm
I remember when you came up with these, Mike! I think I was in my 20s.
February 22nd, 2016 at 8:30 am
Nice post, showing the power of IDL classic. But I’m feeling cheated — what happened to #4?
February 22nd, 2016 at 9:27 am
Do you have a suggestion for a four line program?
March 17th, 2016 at 4:37 am
[…] I recently saw Michael Galloy’s post at http://michaelgalloy.com/2016/02/18/ten-little-idl-programs.html, showing some short (less than ten lines long) programs in IDL. I used to do a lot of programming […]
March 17th, 2016 at 5:12 am
You may be interested in a blog post I’ve just published, taking these programs and converting them to Python and then thinking about the differences between the IDL code and the Python code, and their relative advantages/disadvantages. The blog post is at http://blog.rtwilson.com/ten-little-idl-programs-in-python/.
Thanks for this post – it was interesting to try my reimplementation of them and I learnt a lot!
March 17th, 2016 at 5:36 am
[…] This post was originally published on this blog I recently saw Michael Galloy’s post at http://michaelgalloy.com/2016/02/18/ten-little-idl-programs.html, showing some short (less than ten lines long) programs in IDL. I used to do a lot of programming […]