Introduction to IDL

Contents

  1. STARTING IDL
  2. The IDLDE Interface
  3. IDL Demo
  4. IDL_PATH
  5. Journal File
  6. Quitting IDL
  7. 2D PLOTTING
  8. Simple Plotting
  9. live_plot
  10. SURFACE & CONTOUR PLOTS
  11. Displaying a Surface
  12. Displaying a Shaded Surface
  13. Displaying a Contour
  14. Plotting with show3
  15. live_surface
  16. IMAGES
  17. Image Basics
  18. Other Manipulations
  19. Extracting Profiles
  20. live_image
  21. ANIMATION
  22. Animating a Series of Images
  23. Displaying Animation as a Wire Mesh
  24. Aniamation with xinteranimate/a>
  25. Cleaning Up Animation Windows

STARTING IDL

Starting idl

If you will be running idl on kepler, but displaying its output on another unix or linux machine you must allow idl to display its output on the local machine you are sitting in front of. There are TWO steps in this process.

  1. Give kepler permission to open a display on the local computer you are sitting in front of.
    Using a window on the local computer type:
    xhost kepler.pa.msu.edu
  2. Tell kepler to display the idl graphics on the local computer you are sitting in front of.
    Using a window on kepler (after you have logged into kepler) type:
    setenv DISPLAY your_local_computer_name:0.0
    For instance, if you are using pavo to connect to kepler,
    On pavo type
    xhost kepler.pa.msu.edu
    telnet kepler
    On kepler
    loginid
    password
    setenv DISPLAY pavo.pa.msu.edu:0.0
ssh is supposed to take care of both these things, but sometimes doesn't.

You also need to set up the proper environment for idl. On kepler type:

source /h1/steinr/idl_5.5/bin/idl_setup
On my MAC I put in my .tcshrc file
source /Applications/idl/bin/idl_setup


Start IDL by typing (in the window on kepler):

idl
A gui interface is also available.  To start the gui interface type the following at the prompt:
idlde

The IDLDE Interface

When you start idlde, one window will appear: the "IDL Development Environment."  This is IDL's multiple-document interface which includes built-in editing and debugging tools.

The Command Line is a single IDL prompt where you can enter IDL commands.  If you click the right mouse button while positioning over the Command Line, a popup menu appears displaying the command history, with a default of 10 entries and a maximum of 100 entries.  IDL is an interpreted language and commands are executed immediately at the command line.  To see the IDL Command Line in action, enter the following in the Command Line at the prompt and press Enter:

print, 'Hello World'
Output from IDL is displayed in the Output Log window, which appears by default.  Notice the command and the result of our print command in the Output Log.

The other windows of the IDL Development Environment are either obvious or will be covered in later sections.
 

IDL Demo

You can run a simple idl demo by typing at the prompt IDL>

.r /h1/steinr/idl_5.5/lib/testdemo
The instructions for the demo are in /h1/steinr/idl_5.5/lib/testdemo.demo The source code for the demo can be found in
/h1/steinr/idl_5.5/examples/demo/demosrc
This will allow you to see how the various things that are demonstrated are produced. A demo of the widget oriented idl can be run by typing at the prompt IDL>
demo
.

When you install your own idl, the simple demo is run from $IDL_DIR/lib/testdemo and the widget demo is run by just typing demo.

IDL_PATH

When you run idl you need to set your preferences, in particular the path to your own individual procedures. In idlde, select preferences from the file menu, then select paths, and add them (one at a time). In idl, and a unix environment, set the environment variable IDL_PATH, e.g. on kepler

setenv IDL_PATH \+/h1/stein/Idl:\+$IDL_DIR/lib:\+$IDL_DIR/examples
on my MAC
setenv IDL_PATH \+/Users/Research/Idl:\+$IDL_DIR/lib:\+$IDL_DIR/examples
The escaped + sign tells idl to include all the subdirectories in the path.

Journal File

Idl_pathIDL can keep a record of everything you do in a journal file. This is aTH good idea so you can look back and see what you actually did and note how it worked. Start the journal file by typing at the prompt IDL>

journal
This will create a journal file in the directory where you started idl with a name idlsave.pro. You can use a procedure we have written called journ to create a jorunal file named yymmdd.hrmm.jou, e.g 011019.1739.jou.

Quitting IDL


To quit the current IDL session and return to the operating system, type the following at the Command Line:

exit
In the IDL Development Environment select File -> Exit

2D PLOTTING

Simple Plotting

Simple plots can be charted using the PLOT procedure. Each call to PLOT establishes the plot window (the rectangular area enclosed by the axis), the plot region (the box enclosing the plot window and its annotation), the axis types (linear or logarithmic), and the scaling.

First, we'll plot a simple graph using a sine function. Use the FINDGEN function here to specify the dimensions of the array. The FINDGEN function returns a single-precision, floating-point array, with the specified dimension, where each element of the array is set to the value of its one-dimensional subscript.  We also use a constant in creating this data (!pi).  The character (!) is used to indicate constant pre-defined values.

1. First, create a value for the X axis:
x=2*!pi/100*findgen(100)
2. Now, use PLOT to visualize the array:
plot,sin(x)
Also, additional data can be added, as before, using the OPLOT procedure.  Frequently, the color index, linestyle, or line thickness parameters are changed in each call to OPLOT to distinguish the data sets.

Now use OPLOT to plot the new information over the existing plot:

1. Plot at twice the frequency:
oplot,sin(2*x)
2. Plot at three times the frequency:
oplot,sin(3*x)
The results are shown in the following figure.

live_plot

The LIVE_PLOT procedure allows you to create an interactive plotting environment. Once plotted, you can double click on a section of the plot to display a properties dialog. A set of buttons in the upper left corner of the image window allows you to print, undo the last operation, redo the last undone operation, copy, draw a line, draw a rectangle, or add text. Using any of several auxiliary routines, you can control your LIVE window after it is created.
2. Create a LIVE plot
live_plot,plot_ascii.Temp1,plot_ascii.Temp2, $ name={data:['Temp1','Temp2']}
The result is shown in the following figure:


 

SURFACE and CONTOUR PLOTS

Displaying a Surface

The binary file that we will read in the following example contains an image of the Maroon Bells mountains, a group of mountains located in the Rocky Mountains of Colorado, stored as a binary integer array.  To read in the binary data we use the following command:
maroon_bells=read_binary('/usr/local/rsi/idl_5.4/idl_5.4/examples/data/surface.dat',$
data_dims=[350,450],data_type=2,endian='little')
First, view the array MARBELLS_BINARY.A as a three-dimensional, wire-mesh surface. Use the CONGRID procedure initially to resample the data set so that the mesh can be displayed at a size visible to the human eye.
1. Here resample the array size to 35 by 45, or one tenth its original size. To do this enter:
a=congrid(maroon_bells,35,45)
2. Now we are ready to visualize the mesh using the SURFACE command:
surface,a
The SURFACE command can be used to view your data from different angles. AX and AZ are plotting keywords that are used to control the SURFACE command. The keyword AX specifies the angle of rotation of the surface (in degrees towards the viewer) about the X axis. The AZ keyword specifies the rotation of the surface in degrees counterclockwise around the Z axis.
3. View the array from a different angle by entering the following command:
surface,a, ax=70, az=25

Displaying a Shaded Surface

You can also view a two-dimensional array as a light-source shaded surface.
1. First, load one of the pre-defined IDL color tables by entering:
loadct, 3
2. To view the light-source shaded surface, shown in the following, simply enter the command:
shade_surf,a
3. To look at the array from another angle, enlarge the label text, and add a title.  Again, keywords are used to control certain features of the shaded surface plot.  The AX and AZ keywords control the viewing angle, just as they did with the SURFACE command.  The CHARSIZE keyword controls the size of plotted text. The TITLE keyword was used to add the title Shaded Surface Representation.
shade_surf,a,ax=45,az=20,charsize=1.5, $
title='Shaded Surface Representation'
4. You can create a different kind of shaded surface, where the shading information is provided by the elevation of each point. Now different shading colors on the plot correspond to different elevations (the BYTSCL function scales the data values into the range of bytes). You could also specify a different array for the shading colors.
shade_surf,a,shade=bytscl(a)
5. You can plot a wire-frame surface of the Maroon Bells (mountains) right over the existing plot. The XSTYLE, YSTYLE, and ZSTYLE keywords are used to select different styles of axis. Here, SURFACE is set to not draw the X, Y, and Z axis because they were already drawn by the SHADE_SURF command.  The /NOERASE keyword allows the SURFACE plot to be drawn over the existing SHADE_SURF plot. Enter the following:
surface,a,xstyle=4,ystyle=4,zstyle=4,/noerase

Displaying a Contour

Another way to view a two-dimensional array is as a contour plot. A simple contour plot of the Data can be created.
1. Set the array size back to its original 350 by 450 size by entering:
a=maroon_bells
2. Plot the contour:
contour,a

That command was very simple, but the resulting plot was not as informative as it could be.

3. Create a customized CONTOUR plot with more elevations and labels by entering:
contour,a,nlevels=8,c_labels=[0,1]

By using the NLEVELS keyword, CONTOUR was told to plot eight equally-spaced elevation levels. The C_LABELS keyword specifies which contour levels should be labeled. By default, every other contour is labeled. C_LABELS allows you to override this default and explicitly specify the levels to label.

4. Similarly, you can create a filled contour plot where each contour level is filled with a different color (or shade of gray) by setting the FILL keyword. To do this, enter:
contour,a,nlevels=8,/fill
5. To outline the resulting contours, make another call to CONTOUR and set the OVERPLOT keyword to keep the previous plot from being erased.  You can add tickmarks that indicate the slope of the contours (the tickmarks point in the downhill direction) by setting the DOWNHILL keyword:
contour,a,nlevels=8,/overplot,/downhill
6. CONTOUR plots can be rendered from a three-dimensional perspective.  First, set up the default 3-D viewing angle by entering:
surfr
7. By using the T3D keyword in the next call to CONTOUR, the contours will be drawn as seen from a 3-D perspective. Enter:
contour,a,nlevels=8,/t3d

Plotting with show3

In addition to IDLs built-in routines, there are many functions and procedures included with IDL that are written in the IDL language and that can be changed, customized, or even rewritten by IDL users.  The SHOW3 procedure is one of these routines.
1. Create a plot that shows a two-dimensional array as an image, wire-frame surface, and contour simultaneously.
show3,a

live_surface

The LIVE_SURFACE procedure allows interactive manipulation using the mouse and keyboard. Usually, LIVE_SURFACE is most suitable for relatively small data sets since the interactive environment requires extra system resources.  After you execute LIVE_SURFACE, you can double-click on a section of the surface to display a properties dialog. The buttons in the upper left of the image window allow you many options (print, undo, redo, copy, line, rectangle, text and so on).
1. Here, to visualize a surface representation, enter the following:
live_surface,a

 

IMAGES

Image Basics

First we must import an image to be processed. Reading data files into IDL is easy.  The file that we will read contains the image we used in a previous section,Reading and Writing Data,of an aerial view above Manhattan in TIFF format.
1. Enter the following at the IDL Command Line:
nyny=read_tiff('/usr/local/rsi/idl_5.4/idl_5.4/examples/data/image.tif')
You can view an image in IDL with two different routines. The TV procedure writes an array to the display as an image without scaling. Enter the commands below at the IDL Command Line.
2. Display the image:
tv,nyny
2. Enter WDELETE at the Command Line to dismiss the graphics window.
wdelete
3. The TVSCL procedure displays the image with the color values scaled to use the whole color table. Display the scaled image:
tvscl,nyny
4. Enter WDELETE at the Command Line to dismiss the graphics window.
wdelete
The REBIN function in IDL makes it easy to resize a vector or array to new dimensions. The supplied dimensions must be proportionate (that is, integral multiples or factors) to the dimensions of the original image. Since our original image array here is 768 by 512, well need to decide the correct dimensions of our new resized image. If we want to resize the image to half the original size then simply take half of the arrays original dimensions.
5. Create a new image with new dimensions using the REBIN function:
newimage=rebin(nyny,384,256)
6. Now display the image:
tv,newimage
7. Enter WDELETE at the Command Line to dismiss the graphics window.
wdelete
IDL automatically creates a window for displayed graphics if one does not already exist. You can use the WINDOW command to create new windows with custom sizes.
8. To display Manhattan in a larger graphics window, enter:
window,0,xsize=800,ysize=600
9. Then enter:
tv,nyny
10. Enter WDELETE at the Command Line to dismiss the graphics window.
wdelete
The WINDOW command above creates a new version of window number 0 that is 800 pixels wide (specified with the XSIZE keyword) and 500 pixels tall (specified with the YSIZE keyword).

Other Manipulations

Loading Different Color Tables

Try loading some of the pre-defined IDL color tables to make this image more visible. While the graphics window is visible, type xloadct at the IDL Command Input Line. The XLOADCT widget application appears. Select a color table from the field; the window will reflect the color scheme. Click Done to accept a color table. When you are finished looking at the effects of different tables, click on the first color table in the field, B-W Linear, and click Done to load the original black and white color table.

Sections of an Image

Sections of images can be easily displayed by using subarrays.

1. Erase the current display, create a new array that contains Manhattan and display it by entering:
erase
e=nyny[100:300, 150:250]
2. Then enter:
tv,e
3. Enter WDELETE at the Command Line to dismiss the graphics window.
wdelete


Rotating an Image

Simple rotation in multiples of 90 degrees can be accomplished with the ROTATE function.

4. Rotate the image by 90 degrees, as shown below, by entering:
r=rotate(e,1)
5. Now enter to display:
tvscl,r

The second parameter of ROTATE is an integer from 1 to 8 that specifies which one of the eight possible combinations of rotation and axis reversal to use.

Extracting Profiles

Another useful image processing tool is the PROFILES routine. This routine interactively draws row or column profiles of an image. It allows you to view an image and an X-Y plot of the pixel brightnesses in any row or column of the image simultaneously.
1. Use the PROFILES routine with the rotated image that you just displayed by entering the following:
profiles,r

A new window for displaying the profiles appears. Move the cursor in the window containing the image r to display the profiles of different rows andcolumns.
 

2. Click the left mouse button while the cursor is in the image window to switch between displaying row and column profiles.

3. Click the right mouse button while the cursor is in the image window to exit the PROFILES routine.

live_image

The LIVE_IMAGE procedure displays visualizations and allows interactive manipulation using the mouse and keyboard.
1. Enter:
live_image,nyny

You can click once on the image and then drag the cursor across the image to read the image values.

You may also double-click on the image to display a properties dialog. The set of buttons in the upper left corner of the image window allows you to print, undo the last operation, redo the last undone operation, copy, draw a line, draw a rectangle, or add text. The LIVE_IMAGE window may also be resized using the mouse.

 

ANIMATION

Animating a Series of Images

To create an animation that shows a series of images that represent an abnormal heartbeat, first read in the images to be displayed. The file holds 16 images of a human heart as 64 by 64 element arrays of bytes.
1. Enter:
heart=read_binary('/usr/local/rsi/idl_5.4/idl_5.4/examples/data/abnorm.dat',$
data_dims=[64,64,16],data_type=1)
2. Load an appropriate color table:
loadct, 3
3. Display the first slice of our 3-D array:
tv,heart[*, *, 0]
The asterisks (*) in the first two element positions tell IDL to use all of the elements in those positions. Hence, the TV procedure displays a 64 by 64 byte image. The image is rather small.
4. Now resize each image in the array with bi-linear interpolation by entering:
h=rebin(heart,320,320,16)
5. Then display:
tv,h[*, *, 0]
Each image in H is 5 times its previous size.  Now a simple FOR statement can be used to animate the images. (A more robust and convenient animation routine, XINTERANIMATE, is described next.)
6. To animate, enter:
for i=0,15 do tvscl,h[*,*,i]

IDL displays the 16 images in the array H sequentially. To repeat the animation, press the up arrow key to recall the command and press enter.

7. Dismiss the window:
wdelete

Displaying Animation as a Wire Mesh

The same series of images can be displayed as different types of animations. For example, each frame of the animation could be displayed as a SURFACE plot.
1. Create a new array to hold the heartbeat data:
s=rebin(heart,32,32,16)
s now holds 32 byte by 32 byte versions of the heartbeat images. SURFACE plots are often more legible when made from a resized version of the dataset with fewer data points in it.
2. Display the first image in s, as a wire-mesh surface by entering:
surface,s[*,*,0]

Now create a whole series of SURFACE plots, one for each image in the original dataset.

3. To do this, first create a three-dimensional array to hold all of the images by entering:
frames=bytarr(300,300,16)
The variable frames will hold sixteen, 300 by 300 byte images.
4. Now create a 300 by 300 pixel window in which to display the images:
window,1,title='IDL Animation',xsize=300,ysize=300
The next command will draw each frame of the animation. A SURFACE plot is drawn in the window and then the TVRD function is used to read the image from the plotting window into the frames array. The FOR loop is used to increment the array indices. The lines which follow are actually a single IDL command. The dollar sign ($) works as a continuation character in IDL and the ampersand (&) allows multiple commands in the same line.
5. Enter:
for i=0,15 do begin surface,s[*,*,i],zrange=[0,250]$
& frames[0,0,i]=tvrd()&end
You should see a series of SURFACE plots being drawn in the animation window, as shown in below. The ZRANGE keyword is used to keep theheight axis the same for each plot.
6. Now display the new images in series by entering:
for i=0,15 do tv,frames[*,*,i]

Aniamation with XINTERANIMATE

IDL includes a powerful, widget-based animation tool called XINTERANIMATE.  Sometimes it is useful to view a single wire-mesh surface or shaded surface from a number of different angles. Lets make a SURFACE plot from one of the S dataset frames and view it rotating through 360 degrees. by entering:
1. Save the first frame of the S dataset in the variable A to simplify the next set of commands:
a=s[*,*,0]
2. Create a window in which to display your surface:
window,0,xsize=300,ysize=300
3. Display a as a wire-mesh surface:
surface,a,xstyle=4,ystyle=4,zstyle=4
Setting the XSTYLE, YSTYLE, and ZSTYLE keywords equal to 4 turns axis drawing off. Usually, IDL automatically scales the axes of plots to best display all of the data points sent to the plotting routine. However, for this sequence of images, it is best if each SURFACE plot is drawn with the same size axes. The SCALE3 procedure can be used to control various aspects of the three-dimensional transformation used to display plots.
4. Force the X and Y axis ranges to run from 0 to 32 and the Z axis range to run from 0 to 250:
scale3,xrange=[0,31],yrange=[0,31],zrange=[0,250]
5. Set up the XINTERANIMATE routine to hold 40, 300 by 300 byte images:
xinteranimate,set=[300,300,40],/showload
6. Generate each frame of the animation and store it for the XINTERANIMATE routine. Once a 3-D transformation has been established, most IDL plotting routines can be made to use it by including the T3D keyword. The [XYZ]STYLE keywords are shortened to [XYZ]ST:
for i=0,39 do begin scale3,az= -i * 9 & surface,a, $
/t3d,xstyle=4,ystyle=4,zstyle=4 & xinteranimate,$
frame=i,win=0 & end
7. Play images back as an animation after all the images have been saved in the XINTERANIMATE routine:
xinteranimate

The XINTERANIMATE window should appear, as shown above. Tape recorderstyle controls can be used to play the animation forward, play it backward, or stop.  Individual frames can also be selected by moving the Animation Frame slider. TheOptions menu controls the style and direction of image playback. Click on End Animation when you are ready to return to the IDL Command Line.

Cleaning Up Animation Windows

Before continuing with the rest of the tutorials, delete the two windows you used to create the animations. The WDELETE procedure is used to delete IDL windows.
1. Delete both window 0 and window 1 by entering:
wdelete, 0
wdelete, 1