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.

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-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.

Leave a Reply

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