http://scv.bu.edu/documentation/tutorials/IDL/ SCV IDL tutorial on the web

Using IDL to Manipulate and Visualize Scientific Data

An online tutorial

http://scv.bu.edu/documentation/tutorials/IDL/idl_webtut.html


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 (the 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. It uses a command line interface and is fairly robust.

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. To set this directory structure up in your home directory, and start IDL, you can use the following Unix commands:

    % mkdir idl
    % cd idl
    % mkdir dat
    % mkdir pro
    % idl

This tutorial is organized as a set of examples with explanations. The example directory is organized as described above, so that you can copy programs and data from the example directories into your own, and modify the programs and data to try out different features of IDL. The sample programs and data used in this tutorial are located on SCV machines in the directories /usr/local/examples/idl/pro and /usr/local/examples/idl/dat. You can look at these now IDL local examples. (A hint: if you want to save them using your browser, save them as "source").

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.

There is a demo supplied with idl, which you might want to look at. Just type "demo" at the IDL prompt.

You may also want to look at the IDL supplied examples on ursa-major in /usr/local/rsi/idl_4/examples

You might also find it useful and interesting to visit the IDL Home Page.


Getting started

First, go to the
SCV IDL Help Page for information specific to the installation at this site. This will tell you on what machines IDL is running, how to set up your environment, how to set your display, and where the documentation is.

A few basic hints that will help you get started with the command line interface.

Type a question mark "?" for online help.

Try typing the following four commands at the IDL prompt:

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

Observe that commands are followed by a comma, before the parameter list.

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 (this is like VAX/VMS and the Unix tcsh shell).

IDL programs can be stopped using control-C. (Hold down the control key and hit the letter c).

IDL can be aborted immediately using control-\. (All variables are lost and the state of open files will be uncertain).


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. This can also be done directly from your Unix shell, by typing
    % 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 don't want the program to be run immediately, use .COMPILE instead of .RUN.

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

    IDL> .RUN pro/simple_main.pro

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.

As a program:

    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 Chapter 2 of the User's Guide.


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 suing 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/wire_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.


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.


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

This may be done on a per window basis, e.g.,
    window,0,retain=2,xsize=500,ysize=500

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


Back to the
SCV Home Page

Back to the SCV Tutorials Page