Table of Contents

Introduction
A brief introduction, with links to help you get idl running on your display.
Getting started
A few basic hints that will help you get started with the command line interface.
Programs and batch mode
IDL can be run by typing commands interactively, by creating programs interactively, by reading programs in from files, or it can be run in batch mode.
Variables and arithmetic
Examples of setting scalar, vector, and array variables, and performing some basic operations.
Matrices
Examples of matrix operations.
Plots of Y vs. X
Plots of a function of one variable.
Surface plots
Examples of surface plots of functions of two variables.
Animation
Example animation
Hardcopy
How to save your images to a postscript file.
Why do my graphics get erased?
When a window gets covered, then uncovered, someone has to keep a copy of the obscured part of the image.

 


Introduction

IDL (Interactive Data Language) is a general purpose scientific computing package which provides a suite of mathematical functions, data analysis tools, as well as some scientific visualization and animation tools. IDL is a mature, robust, commercial package sold by NV5 Geospatial.  IDL underlies the ENVI image processing and analysis software and can be used to script and automate many operations performed using the ENVI graphical user interface.

IDL can be used interactively or in “batch” mode. There is an efficient command interpreter for interactive use, as well as the option of developing scripts which may be interpreted or compiled. There is also a graphical IDL development environment available using the command idlde which we will not discuss in this tutorial.

This tutorial is organized as a set of examples with explanations. This tutorial assumes that you are working in a directory named idl, that IDL programs are in a subdirectory named pro, and that data is in a subdirectory named dat. The examples used in this tutorial are available for download here Examples.

The most effective way for you to go through this tutorial is by running IDL in a separate window, and trying out the commands and programs as you read the tutorial. It should be kept in mind that this tutorial is only a glimpse at what you can do with IDL. Once you start using it, you will want to look at the online and/or hardcopy documentation, and explore.


Getting started

SCC users, please go to the IDL module page for information about the versions that are available via the module system

In this tutorial, we will be running IDL directly from the Unix prompt. You start IDL by typing the command “idl”:

scc1% idl

which will present you with a prompt:

IDL>

Try typing the following four commands at the IDL prompt:

IDL> a = 5
IDL> print, a
IDL> x = randomu(999)
IDL> print, x

Observe the difference in syntax between the “print, a” statement and the “x = randomu(999) statement. IDL distinguishes between procedures (such as print) which do not return a value and do not have parentheses in their call syntax, and functions (such as randomu) which do use parentheses when called and always return a value.

Now try out these commands:

IDL> a = [55, 38, -17.333]
IDL> b = indgen(3)
IDL> print, "a = ", a
IDL> print, "b = ", b
IDL> print, "a*b = ", a*b

Observe that array variables look just like scalar variables. IDL is designed to work on multi-dimensional arrays, and many operators work on arrays in “the natural way.” We will see more on this later in the tutorial.

To repeat a command, you can go up and down through previous commands using the arrow keys. When you reach the command you want to repeat, hit return.

Type a question mark ? for online help. This will pop up a separate GUI for accessing IDL user information. (Note that typing “help” at the IDL prompt does something quite different – it gives you a list of all current variables and functions).

Preceding a command with a dollar sign “$” allows typing commands to the Unix shell, e.g., to print a listing of the current working directory:

IDL> $ls

A semicolon “;” indicates that the rest of a line consists of comments. A dollar sign “$” at the end of a line indicates line continuation, and an ampersand “&” groups commands together. We will see more of this below in the section on running modes.

To exit IDL, type:

IDL> exit

There are some special characters which you should be aware of for execution management:

Ctl-C Stops running (runaway) program if possible, without killing IDL
Ctl-Z Suspends IDL in usual Unix manner
Ctl- Stops (aborts) running program, may leave variables and files in uncertain state
Ctl-D Stops IDL, like exit

Programs and batch mode

IDL can be run by typing commands interactively, by creating programs interactively, by reading programs in from the command line, or it can be run in batch mode.

When you type commands on the command line, each line is executed immediately when you hit the return key. (It is possible to carry over to the next line using a dollar sign “$” at the end of the line).

Batch mode

Running in batch mode is similar, except the commands come from a file. You precede the file name with the at-sign “@“. The file batch_two_prints in the pro subdirectory contains the four lines

a = 5
print, a
a = [2, 3]
print, a

Type the following at the IDL prompt

IDL> @pro/batch_two_prints

This will execute the four commands exactly as if you had typed them. Also, this can also be done directly from your Unix shell, by typing

scc1% idl pro/batch_two_prints

Programs

When typing interactively and running in batch mode, each line is executed immediately. You can write programs which can be stored and run multiple times, and you can write functions which may be called from programs and other functions.

To create a program from the command line, use the executive command .RUN, enter your commands, then type END. At this time, your program will be compiled and executed:

IDL> .RUN
- a = 25
- b = 3
- c = a * b
- print, a, b, c
- END
% Compiled module: $MAIN$.
      25       3      75

If you had the same set of commands in a file, you could execute the same program:

IDL> .RUN pro/simple_main.pro

If you don’t want the program to be executed immediately, use .COMPILE instead of .RUN. The variables and procedures defined will then be available.

The examples above make it look like batch mode and running programs is very similar. Consider the following simple computation of the factorial function. As a batch file, pro/batch_loop looks like:

f = 1
for k=1,6 do begin $
    f = k * f & $
    print, f & $
end

The commands to be separated by ampersands “&“, and the lines must be continued. The entire loop must essentially be on one line, since each line is executed as soon as it is encountered. Imagine nested loops with long calculations inside.

The same computation as a program in a file does not require the &s and $s:

f = 1
for k=1,6 do begin
    f = k * f
    print, f
endfor
end

For an example of the use of functions:

IDL> .RUN pro/simple_routine
IDL> simple

A program can be run from a batch file. Try:

IDL> @batch_run

This reads and runs simple_main.pro, then runs it again, then reads and runs simple_routine.pro.

For more information on creating and running programs, including other commands, see the IDL online help.


Variables and arithmetic

All of the examples shown in this section can be found in pro/arithmetic.pro. You can explicitly type variables, or not. See chapter 4 for information on type conversion, etc. The simplest thing to work with is scalars.

IDL> y = 2.5
IDL> z = x + y
IDL> w = x^y + sin(z)
IDL> print, x, y, z, w
       3      2.50000      5.50000      14.8829

Square braces are used to define vectors (1-dimensional arrays):

IDL> v1 = [1, 2, 0]
IDL> v2 = [1, 0, 0]
IDL> print, "v1 = ", v1
v1 =        1       2       0
IDL> print, "v2 = ", v2
v2 =        1       0       0

Vectors can be componentwise added, multiplied, etc.:

IDL> v3 = v1 + v2
IDL> print, "v3 = v1 + v2 = ", v3
v3 = v1 + v2 =        2       2       0
IDL> print, "v1 * v2 = ", v1 * v2
v1 * v2 =        1       0       0
IDL> print, "v1 * sin(v3) = ", v1 * sin(v3)
v1 * sin(v3) =      0.909297      1.81859      0.00000

There are other useful operators, such as min and max:

IDL> min1 = min(v1)
IDL> max1 = max(v1)
IDL> print, "min(v1), max(v1) = ", min1, max1
min(v1), max(v1) =        0       2

Scalars and arrays can be allocated with specific types. Scalar examples:

IDL> x = float(1.3)
IDL> sx = fix(x)
IDL> lx = long(x)
IDL> bx = byte(x)
IDL> dx = double(x)
IDL> cx = complex(x)
IDL> print, x, sx, lx, bx, dx, cx
       1.30000       1           1   1       1.3000000
(      1.30000,      0.00000)

Array examples:

IDL> a = fltarr(5)
IDL> for i=0, 4 do $
IDL>     a(i) = 2*i
IDL> b = complex(a)
IDL> print, "b = ", b
b = (      0.00000,      0.00000)(      2.00000,      0.00000)
    (      4.00000,      0.00000)(      6.00000,      0.00000)
    (      8.00000,      0.00000)

Matrices

All of the examples shown in this section can be found in pro/matrix.pro. A matrix (which is a 2-dimensional array) may be defined algorithmically:

IDL> A = dblarr(2, 4)
IDL> for i = 0, 1 do begin $
IDL>     for j = 0, 3 do begin $
IDL>         a(i, j) = 10 * i + j
IDL> print, A
        0.0000000       10.000000
        1.0000000       11.000000
        2.0000000       12.000000
        3.0000000       13.000000

Note that as it is printed, the first index corresponds to the column, and the second index to the row. Another way to think of it is that the way the data is stored, the first index varies fastest, and the second varies the slowest. This agrees with the way the data is printed.

A matrix may be constructed explicitly from vectors:

IDL> v1 = [1, 2, 0]
IDL> v2 = [1, 0, 0]
IDL> v3 = [4, 5, 6]
IDL> A = [[v1], [v2], [v3]]
IDL> print, A
    1       2       0
    1       0       0
    4       5       6

Create the transpose:

IDL> Atrans = transpose(A)
IDL> print, Atrans
    1       1       4
    2       0       5
    0       0       6

Take the determinant:

IDL> d = determ(float(A))
% Compiled module: DETERM.
IDL> print, d
     -12.0000

Invert:

IDL> Ainv = invert(A)
IDL> print, Ainv
       0.00000      1.00000      0.00000
      0.500000    -0.500000      0.00000
     -0.416667    -0.250000     0.166667

Multiply vectors by matrices:

IDL> v = [1, 2, 3]
IDL> print, A
       1       2       0
       1       0       0
       4       5       6
IDL> print, v
       1       2       3
IDL> print, A ## v
           5
           1
          32
IDL> print, v ## A
          15          17          18

You can solve a linear system Ax = b for x by Cramer’s rule (the cramer function expects float or double inputs, requiring an explicit type conversion):

IDL> b = float([1, 2, 16])
IDL> A = float(A)
IDL> x = cramer(A, b)
IDL> print, x
      2.00000    -0.500000      1.75000

Plots of Y vs. X

Two examples of plots of a function of one variable.

The program pro/fit_y.pro reads in a set of 10 y values. It creates 10 x values from 0 to 9 using the “findgen” command. It then fits a cubic to the data, and plots the original data as points, and the fitted function as a curve.

The program pro/fit_xy.pro reads in a set of 10 (x,y) pairs, into a 2 x 10 array. It separates them into two 1-dimensional arrays, and fits a cubic to it, and plots this as in the preceding example.


Surface plots

IDL provides an interactive viewer for surface plots, called xsurface. An example of its use is in pro/xsurface.pro

An example of drawing a surface plot directly, rendered as a wire mesh, is in pro/mesh_surf.pro.

An example of drawing a surface plot directly, rendered as a shaded surface, is in pro/shade_surf.pro.

There are two examples of producing surface plots from scattered data. This is data in which the (x,y) locations for which we have the function evaluated do not lie on a regular gird. To make a surface plot, IDL needs to have the function evaluated on a regular rectangular grid. There are two steps involved. The first is to form a triangulation using the input (x,y) points to use for interpolation, and the second is to produce a mesh from that interpolation. The first example pro/triangulate.pro shows this process and renders the result as a wire mesh surface plot. The second example pro/tri_shade does the same thing, and in addition draws a shaded surface plot, using two different kinds of shading. (To see this example, first load the program: IDL> .run pro/tri_shade, then execute the compiled procedure: IDL> triangulated_surf)


Animation

One method of producing animation is to create a sequence of images and then display them in order. This is quite easy using IDL. An example is in pro/making_waves. (To see this example, first load the program:
IDL> .run pro/making_waves, then execute the compiled procedure: IDL> making_waves)


Hardcopy

You can save your plots and other images by rendering them to a postscript file instead of to an X window. An example is given in pro/tri_save_ps.pro. Basically, you set your plotting area to be a file, then change the graphics device to be a postscript device, and close that device when you are done:

IDL> set_plot,'PS'
IDL> device, filename='your_filename.ps'
IDL> ...
IDL> ... plot some stuff ...
IDL> ...
IDL> device, /close

Why do my graphics get erased?

When a window gets covered, then uncovered, someone has to keep a copy of the obscured part of the image. You may or may not want to have all the images saved when they are obscured, by reasons of speed and memory.

In any case, this topic is called “backing store”. It can be done by IDL, done by the windowing system, or not done. By default, the X window system does not have backing store turned on.

In IDL, there is a keyword RETAIN, for specifying which kind of backing store to use.

  • RETAIN = 0 implies no backing store,
  • RETAIN = 1 IDL asks the window system to do it,
  • RETAIN = 2 IDL does it

This may be done on a per window basis, e.g.,

IDL> window,0,retain=2,xsize=500,ysize=500

Backing store will now be maintained for this window by IDL.