IDL Tips and Code

Below are some home grown IDL procedures. Please note that these are neither optimized nor intended for distribution. They are not well-commented and getting them to work for you is up to you. However, they may be a good starting point for something you need. I hope you find them useful. Feel free to email me if you need help.


GW_MCMC.PRO – A procedure that runs a Markov chain Monte Carlo simulation using the algorithm described in Goodman & Weare (2010) (hence the GW in the procedure title) and more commonly seen in the Python distribution emcee ( Note that you need to create and specify your own model function and a function that calculates the priors for your variables. The program needs these function names as input. The emcee link above is a great resource for all things MCMC. The blog of Jake Vanderplas is also a good resource for astro-statistics and Bayesian analysis.

NOTE: In this routine are a couple of lines that point to directories on my machine. These are used as a kludgy way of getting around problems with IDL’s random number generators (see below). Edit these lines out or save and restore your own seed variable.

ARRAYCOMBO.PRO – Spits out an array of indices that includes all of the possible combinations of a vector of length given a minimum number of selections m and a maximum number of selections p. For example, if you have a vector of length n=5 and you want all possible vectors of at least length m=2 selected from the original vector, you would get an array with 26 different vectors in it. Beware that the procedure, which essentially loops through the possible combos and keeps one if it hasn’t been found yet, is  inefficient for large vectors of n>12 or 13. Note: keyword /KEEPTIME calls modified versions of the procedures TIC.PRO and TOC.PRO. Click here for that file (copyright (c) 2013, Fanning Software Consulting, Inc.): TIC.PRO

CONFINT_WILSON.PRO – Calculates confidence intervals on a binomial variable for a sample, e.g., exoplanets around a star (yes or no). Confidence intervals may be asymmetric since they are calculated using Wilson’s score test and thus the “true” value is supposed to be adjusted from the measured value.

PMSMASSINTERP.PRO – Interpolates (crudely) pre-main sequence masses based on input T_eff and luminosity values. The mass tracks are the Siess et al. 2000 tracks. This program is cumbersome for outside users since you have to have the mass track and isochrone data stored locally AND they have to be in a specific format (see the header material in the program). Be sure to change any paths to where you want them. This is also a good program for generating plots of the tracks and your input objects. The subroutine TRACKPLOT.PRO is required to run the program. MCONVECTIVE.PRO, which spits out the convective envelope mass coordinate, is also needed, although you can comment this out in the routine with no loss of utility. Note: the program does not interpolate outside of the track boundaries!

General tips

For most things IDL, I highly recommend The Coyote (David Fanning): Most of one’s IDL questions are likely covered thoroughly and expertly by The Coyote. Below are some things I’ve come across in my IDL usage that aren’t necessarily obvious and I wish someone had told me when I started. Some of these things are also part of the official Exelis IDL manual but I’ve rehashed some of it that I’ve found critical and non-obvious.

Single value arrays – One annoying problem I’ve often run into is when a single value is selected from a multi-value array and the new value is itself an array! This causes problems when that new value, that you want to be a single integer/floating/double point value, is multiplied by other vectors since multiplying a one-element vector by a greater-than-one-element vector results in a single-element vector. I get around this by simply stating explicitly that I want the zeroth element of the array. For example, if A is a five-element floating point array (A=findgen(5)) and aa is the array index where A equals 2 (aa=where(A eq 2)), then I would state aa=aa[0] before using it in any operations.

Changing directories – You can change the working directory of IDL using the cd,’directory’ command. If you need to know which directory you’re currently in, use cd,current=variable -> print, variable.

Plots on top of plots – If you want to embed a plot within another plot, without erasing the first plot, use the keyword /NOERASE in your second plot command. This places the second plot on top of the first, at the specified location, but leaves all previously plotted stuff where it is.

Transparent images – One thing that is non-obvious in IDL is how to create figures or images with transparent shapes or layers. The best solution I’ve come across, and the one which allows you the most customization, is to create a foreground and background image and then combine them with an array specifying the fractional contribution of each. This is explained by The Coyote here: I’ll note that this works for any type of output image; it does not need to be a PNG.

The transparent image is created by specifying your background image, an x 3 array where each NN layer is a color channel: red, green, and blue. The same is done for the foreground image. The third array, which we’ll call the alpha array, needs to be an x 3 array containing values 0.0 < alpha < 1.0. You can vary the fractional contribution across alpha to give different pieces of the array different transparency weights. We then create the final image by combining the foreground and background arrays with the alpha array (I like to use CGIMAGE to display images, either on-screen or to a file):


An example of what this produces can be seen in the contrast videos (the transparent red planet) on my Research page.

Array manipulations are not always faster! – I know there has been a lot written about this already but I thought I’d throw my hat in the ring. I’ve experienced scenarios in IDL where array manipulations are slower than FOR loops due to the size of the array. I don’t have any precise rules for you, but if the FOR loops are small (order of ~100 iterations) and you’re not nesting them inside large FOR loops or inside multiple other FOR loops, manipulating even medium-sized arrays (100×100) can be slower. Don’t fall into the elegant programming trap: just because there are more loops and lines in your code does not make it inferior! (Disclaimer: I am not an expert programmer and I’ve learned most of my habits through trial and error. I also do not write general purpose code; most of my procedures are for specific tasks related to my research)

Random numbers – Random numbers in IDL are actually pseudo-random. When you initiate a program that generates random numbers, it’s taking the numbers from a very, very long sequence of pseudo-random numbers. If your routine crashes and you have to restart it, you will also reset the number chain and you will get the exact same random number sequence as you did the first time. The way to prevent this is to use the same seed variable at all levels in all routines. I do this by saving my seed variable and restoring inside of every subroutine. This is kludgy and there are more elegant solutions ( but it works fine for me. 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.