Short Introduction to IDL

Originally from lecture notes of Prof. Pat Henry (IfA)

Advantages:

  1. Complete language, can be used interactively (i.e., no compilation), create functions or procedures (i.e., programs).
  2. Everything is alike: scalars, vectors, arrays, matrices..., without DoLoops.
  3. Many numerical routines included.
  4. Runs the same across UNIX, MS Windows, Mac...
  5. Can link to FORTRAN & C programs
  6. A large library for astronomy (already installed in NRAO idl)

 

Starting, Interrupting, Existing, Aborting

Starting

"> idl" at system prompt.  This gets to a command line interpreter (CLI, more commonly used).
"> idlde" - GUI

Interrupting:

"ctl-c" will stop what idl is doing.  Sometimes variables disappear.
"IDL> return" goes up one level.
"IDL> retall" goes to top level.

Exiting:

"IDL> exit" ("quit" will not work.)

Aborting:

"ctl-\", only in emergency, will stop the whole idl.

 

Documentation

Printed manuals:

"IDL Basis", "Using IDL"

Online manuals:

"IDL> ?" or "IDL> ? keyword"
"IDL> help" doesn't give any help, rather overview of current session. "IDL> help, variable" gives info about the variable.

 

Data Types

IDL is dynamically typed. Operation can change variable's type. The result is the type involved that has highest precision.
There are 8 basic types + structure (aggregation of variable types).
Can force variable to a certain type. Name of the type is the command, e.g., "IDL> float, a", except interger, which is "IDL> fix, a"

 

Arrays

Multidimensional datasets all at the same type, e.g.,
1D array or vercor: "IDL> a = [1,2,3,4,5]"
2D array or matrix: "IDL> b = [ [1,2,3], [4,5,6] ]"

Subscripts start with 0. In the above examples, a[0] = 1, b[1,1] = 5.

Can use "IDL> c = b/2.0" or "IDL> d = a + 5" without DoLoops.

 

Variables

Named repositories of information. Has to begin with a letter. 1-128 characters long. Second and subsequent charaters can be letters, numbers, underscore, or the dollar sign.


Operators

Combine variables and constants into expressions. When in doubt about procedure, use parentheses.

Example:

a<2+1 : smaller of a or 2, plus 1.
a>2*3 : larger of a or 6

 

Commands (Procedures)

"IDL> procedure_name, arg1, arg2, ..."
where arg1, arg2 can be either input or output.

 

Function

"IDL> return = function_name(arg1, arg2, ...)"

 

Keywords

"IDL> procedure_name, arg1, keyword=3"

Like arguements.

/keyword is the same as keyword=1.

 

Special Commands

$command : unix shell command. (use "spawn" procedure for more complex unix commands)

@script : issue a series of idl commands stored in an ASCII file

$ at the end of line : continuation to the next line

& : issues two commands in the same line

; : comments. Anything after a ";" sign will be ignored.

 

FITS Files

Standard format for astronomy.

See "FITS I/O" in IDL Astronomy Libraty.

Read FITS file:

"IDL> image = mrdfits('filename', 0, header, /fscale, /silent)"
where

"image" is the idl file name, will read the fits file into this idl file, can be any name.
" 'filename' " is the name of the fits file.
"0" means fits extension number.  It's usually 0.
"header" will return FITS header info in idl
"/fscale" rescale to original data
"/silent" supress info messages.

Write FITS file:

"IDL> mwrfits, image, 'filename', header, /create"

"/create" overwrite exisitng file.

 

2D Plotting

"IDL> plot, x, y"

"IDL> oplot, a, b" overplot a, b on the x-y plot

Useful keywords:

"IDL> plot, x, y , xtitle=' ', ytitle=' ', xrange=[x1, x2], yrange=[y1, y2], /xstyle, /ystyle, /xlog, /ylog, psym=n"
where /xstyle and /ystyle plot the exact x/y range as given.

Where does the plot go?

"IDL> set_plot, 'display name' "
'display name' can be 'x' (x window), 'win' (MS Windows), 'ps' (default output name = idl.ps).

"IDL> device, filename = 'filename', /encapsulated "
create an encapsulated ps file called 'filename', if device is 'ps'

"IDL> device, /close"
sends the plot to the chosen device and close the plot.

 

Example

Download this script file and this FITS file. Open the script file and execute it line by line in IDL to see what happens.