Beginner's IDL I

The objective of this course is to give an overview of the IDL environment at the beginner's level, assuming that the audience has little or no prior exposure to IDL. Here we will cover the basics of IDL, with an emphasis on using IDL as a tool for data visualization. The course aims at to provide a survey of the various IDL features. It is expected that the audience will do the accompanying exercise and continue to explore the concepts using the resources provided. Programming in IDL will be covered in the next session: Beginner's IDL II.




Brief Introduction to IDL

IDL is:

IDL is not:


The best way to learn IDL is by using it!


Where to find help and resources

Manuals

Personal copies of the manuals can be picked up from the UNIX/VMS help desk, Room 331.

IDL Online Help

Within IDL, one can start the "IDL Online Help" by typing "?" at the prompt.

IDL> ?

This will start the IDL help tool, and one can find pretty much everything about IDL.

Also, you can start the help browser from the shell by typing "idlhelp" at shell.

unix> idlhelp

IDL Demo

At shell, "idldemo" will start a guided tour of the various IDL features.

unix> idldemo

IDL 5.1 at STScI

Information on IDL 5.1 running at the Institute can be found at:

http://scisoft.stsci.edu/astro_soft/IDL5.html

For external libraries, such as the "astron" library, the "widget_olh" command will start the help browser.

IDL> widget_olh

Other IDL resources

IDL Newsgroups
There are several Usenet newsgroups. The official IDL newsgroup is:

comp.lang.idl-pvwave

Ray Sterner of the Johns Hopkins Applied Physics Laboratory publishes an IDL Frequently Asked questions (IDL-FAQ) list to this newsgroup. This FAQ can be found at:

http://fermi.jhuapl.edu/s1r/idl/idl_faq/idl_faq.html

Unfortunately, the FAQ has not been updated for a while.

Other useful WWW Sites

Research Systems has information on IDL in general at:

http://www.rsinc.com/

Pete Riley's IDL Home Page

http://nis-www.lanl.gov/~uk2/idl/

Coyote's Guide to IDL Programming

http://www.dfanning.com/

Wayne Landsman's IDL Astronomy Library WWW page is at:

http://idlastro.gsfc.nasa.gov/homepage.html

The JHU/APL/S1R IDL library WWW page is at:

http://fermi.jhuapl.edu/s1r/idl/idl.html

Ray Sterner's Color Shaded Relief Maps made by IDL are at:

http://fermi.jhuapl.edu/states/states.html


The IDL environment


IDL is an interpreted environment, i.e., the IDL interface interprets commands, converts the commands into instructions and then executes them. IDL programs needs the IDL environment to run; you cannot create standalone applications (except with a runtime IDL license with a special version of IDL).

IDL provides an ideal environment for work in these three areas: Data visualization, Data Analysis, and Programming. The strength of IDL derives from the vector or array-oriented processing.

Data visualization

Examples:

Data Analysis

Examples:

Programming



IDL Basic Syntax and some general features

IDL Basic Syntax

procedure, [arguments 1...n], [modifiers 1...n]

variable = function (arguments 1..n)

Keeping a log of your work

Logging your idl session

IDL> journal, 'sequence.pro'

The journal command creates a file named sequence.pro in your current working directory. IDL will record all IDL activity in this file until you stop it by typing another journal command with no argument.

Also, the state of variables, system variables, and procedures and functions can be saved as well as restored at a later time within IDL. For example, if the three variable flux, velocity, and error contain your spectroscopic data, then

IDL> save, filename='spectra.dat', flux, velocity, error

will save everything contain by these variables. Later,

IDL> restore,'spectra.dat'

will restore all three variables within the IDL environment.

A quick run...


Within the IDL environment, one can perform many different type of calculations. Below are some examples, and you are encouraged to try them out in front of your computer.

Note: the "print" command will print whatever is given as its argument. Notice that the argument is evaluated before it is printed.

Simple arithmatic

IDL> print,3+4
7
IDL> print,3*5
15

Array arithmatic

IDL> array_x=[3,5,8]
IDL> array_y=[4,6,7]
IDL> print,array_x*array_y
12 30 56

Create a blank array of 40 floating point elements

IDL> num=fltarr(40)
IDL> print,num
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000

Fill the array with numbers, using a for loop

IDL> for i=0,39 do num(i)=i
IDL> print,num
0.00000 1.00000 2.00000 3.00000 4.00000 5.00000
6.00000 7.00000 8.00000 9.00000 10.0000 11.0000
12.0000 13.0000 14.0000 15.0000 16.0000 17.0000
18.0000 19.0000 20.0000 21.0000 22.0000 23.0000
24.0000 25.0000 26.0000 27.0000 28.0000 29.0000
30.0000 31.0000 32.0000 33.0000 34.0000 35.0000
36.0000 37.0000 38.0000 39.0000

An example of IDL's strength - do above 2 steps with 1 line

IDL> num2=findgen(40)
IDL> print,num2
0.00000 1.00000 2.00000 3.00000 4.00000 5.00000
6.00000 7.00000 8.00000 9.00000 10.0000 11.0000
12.0000 13.0000 14.0000 15.0000 16.0000 17.0000
18.0000 19.0000 20.0000 21.0000 22.0000 23.0000
24.0000 25.0000 26.0000 27.0000 28.0000 29.0000
30.0000 31.0000 32.0000 33.0000 34.0000 35.0000
36.0000 37.0000 38.0000 39.0000

Just want to print the first 10 elements...

IDL> print,num2(0:9)
0.00000 1.00000 2.00000 3.00000 4.00000 5.00000
6.00000 7.00000 8.00000 9.00000

Moving on...

Before we move on to using IDL as a data visualization tool, we will create a dataset for us to use.

IDL> num=findgen(40)*10
IDL> line=sin(num*!DtoR)

The first line again generates an array of 40 elements, and then each element is multiplied by 10. The second command multiplies the values in num by the system variable !DtoR, which contains a conversion factor for converting degrees to radians, takes the sine of those values, and stores the result in the variable line.



IDL as a data visualization tool

One of the key strengths of IDL is the capability to quickly display data in a variety of formats.

How to plot data

Plots the line we created

IDL> plot, line

Plots the independent data in the variable num versus the dependent data in the variable line.

IDL> plot, num, line

Plots the data as plus sign symbols (+).

IDL> plot, num, line, psym=1

Plots the data as a dashed line.

IDL> plot, num, line, linestyle=1

Produces a plot with annotation.

IDL> plot, num, line, xrange=[0,360], ytitle='Sin(x)', $

Title='Sine Plot'

Produces a plot with a specified x range.

IDL> plot, num, line, xrange=[0,360], xtitle='Degrees', $

ytitle='Sin(x)', title='Sine Plot', xstyle=1

Draws a line on the plot at Sin(x) = 0

IDL> plots, [0,360],[0,0]

Short exercise: Can you plot out a cosine curve? Exponential?

How to plot 3-D data

Generate the dataset - first we need to generate a two-dimensional dataset to visualize. We will create a Gaussian distribution here:

IDL> Z = shift(dist(40), 20, 20)
IDL> Z = exp(-(Z/10)^2)

The first command creates a 40-element by 40-element array where each element holds a value equal to its Euclidian distance from the origin and then shifts the origin to the center. The second command makes a Gaussian with a "1/e" width of 10.

To visualize the 3-D data

IDL> surface, z

To view a 2-D array as a light-source shaded surface, first load a pre-defined color table

IDL> loadct, 3

Then plot the shaded surface

IDL> shade_surf, z

Look at the array from another angle, enlarge the label text, and add a title by entering:

IDL> shade_surf, z, ax=45, az=20, charsize=1.5, $
IDL> title='Shaded Surface Representation
'

You can create a different kind of shaded surface, where the shading information is provided by the elevation of each point by entering the command:

IDL> shade_surf, z, shade=bytscl(z)


You can plot a wire-frame surface of the Gaussian right over the existing plot:

IDL> surface, z, xstyle=4, ystyle=4, zstyle=4, /noerase


Or a contour plot

IDL> contour, z

How to read and write files

Read data


How to read in a table with 2 columns, 5 rows

Method 1:
IDL> data=fltarr(2,5) ; create the data array
IDL> openr,lun,'in_array.dat',/get_lun ; open the data file
IDL> readf,lun,data ; read data from the file
IDL> close,/all ; close the data file
IDL> help,data ; check the data array
DATA FLOAT = Array[2, 5]
IDL> print,data
1.00000 5.00000
2.00000 6.00000
3.00000 7.00000
4.00000 8.00000
5.00000 9.00000
IDL> x=reform(data(0,*)) ; separate the columns
IDL> y=reform(data(1,*))
IDL> print,x
1.00000 2.00000 3.00000 4.00000 5.00000
IDL> print,y
5.00000 6.00000 7.00000 8.00000 9.00000

Method 2:
IDL> x=fltarr(5) ; create the x data array
IDL> y=fltarr(5) ; create the y data array
IDL> openr,lun,'in_array.dat',/get_lun ; open the data file
IDL> .run ; execute a group of control statement
- for j=0,4 do begin ;start of the loop
- readf,lun,tmp1,tmp2 ; read the j-th row
- x(j)=tmp1 ; put the data into the x array
- y(j)=tmp2 ; put the data into the y array
- endfor ; end of the for loop
- end ; end of the control group
% Compiled module: $MAIN$.
IDL> print,x
1.00000 2.00000 3.00000 4.00000 5.00000
IDL> print,y
5.00000 6.00000 7.00000 8.00000 9.00000

How to read in unformatted binary data

IDL> openr, lun, 'galaxy.dat', /get_lun ; open the data file
IDL> image=bytarr(256,256) ; create a 256x256 byte array
IDL> readu, lun, image ; read in the data
IDL> free_lun, lun
; close the file

How to read in HST data

Check out the widget_olh

Write data


Replace openr (open-read) with openw (open-write), and replace readf (read-file) with printf (print-file).

How to write the 2 by 5 array back:

IDL> openw,lun,'out_array.dat',/get_lun
IDL> printf, lun, data
IDL> Free_lun, lun


or

IDL> openw,lun,'out_array.dat',/get_lun
IDL> printf, lun, x, y
IDL> Free_lun, lun



Short exercise: Can you create and read back in a 3x3 matrix?

How to display image

To display the image we read into the 256x256 image array:

IDL> Window, /Free, xsize=256, ysize=256

This command creates a 256-by-256 pixel window in which you can display the image. The Free keyword creates a new window with an unused window index. The current graphics window is automatically set to this newly created window.

IDL> tvscl, image

This command displays the image in the new window.

IDL> xloadct

This command allows you to choose one of the 41 predefined color tables that come with IDL.

Short Exercise: Try to read other data files and display them!

Generate a PostScript plot

After all the hardwork, we should generate a hardcopy of the picture so that you can post it right outside your office to show other people that you have mastered IDL.

The idea is simple. Instead of displaying the image on screen, you now direct IDL to "display" it in a PostScript file.

IDL> set_plot,'ps' ; set the plotting window to PostScript
IDL> device,filename='niceimage.ps',/portrait ; set the filename and tell IDL to generate in portrait mode
IDL> tvscl, image ; recreate the image
IDL> device,/close ; close the file
IDL> set_plot,'x' ; set the plotting window back to X-window


Short Exercise: Try to plot out the sine curve.

Putting everything together

How about doing a little analysis?

There are several functions and procedures in IDL that can help you do some simple data reduction. Use what you have learned here to find out more information about the Smooth and Curvefit functions in IDL.

Then do the following exercise.



Exercise

To do this exercise, copy the files from this directory: /data/sol1/plee/idl1 to your local directory.

In this exercise, using the concepts from the discussion, you will do the following:

A sample solution is available here (after 11/9/98).



Paul Lee
updated: 11/2/98