From an xterminal type idl to start your IDL session. In what follows IDL commands are written in bold while variable names are written in italics. Remember that IDL is not case sensitive (so that the variable bobo is the same as Bobo is the same as BOBO). Don't be afraid to experiment as you pick up on the syntax. To speed up typing use the up and down arrows to scroll through lines you have recently typed. Use BACKSPACE to delete the previous character, Ctrl-e to move the cursor to the end of the line, Ctrl-a to move the cursor to the beginning of the line, Ctrl-u to delete everything to the left of the cursor, and Ctrl-k to delete everything to the right of the cursor. If you want to suspend your IDL session type Ctrl-z and then fg to bring the process back to the foreground. If IDL is choking on something use Ctrl-c to interrupt the process (the same way you would interrupt a UNIX command). To get started type
print, 3*5
This command prints the integer 15. Note that integers do not have a decimal point. Now define a variable x and set it equal to the result of the above mathematical operation:
x = 3*5 help, x
The command help tells you about the variable x. Notice that in IDL commands are followed by a comma. See what happens if you leave the comma out. Now, the line
x = sqrt(x) & help, x
redefines the variable x to be the square root of itself and tells you about x all in one line. Multiple commands can be placed on one line with the ampersand &. Note that now x is a floating-point number (it has a decimal point). We'll explore datatypes in more detail later. Next define x as a floating-point number, then a string, then an array. Try using help after each line.
x = 4.3 x = 'Wassup!' x = [1,2,3,4,5,6]
You can use either single or double quotation marks to define string variables. For example, here string1 and string2 are equivalent:
string1 = 'Wassappening' string2 = "Wassappening"
To print the elements of an array use the print command:
print, x
If we want to do some mathematics on this array we don't need a FORTRAN-esque DO loop! (A FORTRAN DO loop is called a for loop in IDL). Try typing
print, 2.0*x
Each element in x has been multiplied by 2.0. IDL is optimized for array operations and is relatively slow when it comes to for loops so you should always maximize array and matrix operations in your coding. Moving on, define the following two variables:
y = sqrt(x) z = x^(0.5)
Now try to predict what
print, y-z
will give you. You should notice that IDL has done the subtraction on each element in our two arrays. Note that the carrot symbol (^) raises a number (or a variable) to some power. Like in a calculator, the order of operations is crucial here. Now let's play around with arrays by using some of IDL's built-in functions (a function in IDL takes one or more arguments in parenthesis and its output is set equal to some new variable). The command
x = findgen(100)
defines x as an array of 100 floating-point numbers that increase monotonically from 0 to 99. To access individual elements in this array we use square brackets:
print, x[0], x[99]
You'll notice that IDL is zero-indexed (unlike FORTRAN) such that the indices of the array begin at zero and end at the number of elements minus one. To access a snipet of the array try
print, x[20:30]
Alternatively we can use a for loop to print each element one at a time. We will use the n_elements() function to figure out how many elements are in the array x (even though we know it is 100) and then loop on each element and print it:
narray = n_elements(x) for i = 0, narray-1 do print, i, x[i]
There are several convenient internal variables that IDL knows about. To get the number pi (i.e., 3.14) in single and double precision try
print, !pi print, !dpi
To convert from degrees to radians and back again try this bit of code:
theta_deg = 45.0 ; [degrees] theta_rad = 45.0*!dtor ; [radians] print, theta_rad*!radeg ; [degrees]
The text after the semi-colon is a comment: IDL ignores any text to the right of a semi-color and you should use this to comment your programs frequently. To get e, the base of the natural logarithm, do
e = exp(1) print, e
Now let's make a sine curve and plot it. Use what you've already learned to figure out what findgen() does.
theta = 2*!pi*findgen(100)/100.0 z = sin(theta) plot, theta, z
To generate random numbers use the randomu() and randomn() functions in IDL. Find out what inputs these functions require and what their outputs are by using IDL's HELP facility. Type
?randomu ?randomn
Let's see if randomn() truly gives us a Gaussian distribution of random numbers with a mean of zero and a variance of one. According to the central limit theorem in the limit of a large number of samplings of the parent distribution we should get a Gaussian distribution. Let's see this happen. Type each set of two lines and examine the resultant histogram plot:
g = randomn(seed,10) plothist, g, bin=0.1 g = randomn(seed,100) plothist, g, bin=0.1 g = randomn(seed,1000) plothist, g, bin=0.1 g = randomn(seed,10000) plothist, g, bin=0.1 g = randomn(seed,1E6) plothist, g, bin=0.1
Next we will learn how to write a file, read it back in, and then plot the result. Let's generate a low-order polynomial function sampled at 100 random points along the independent axis. We need to sort the random points such that they increase monotonically using the sort() function.
x = randomu(seed,100) srt = sort(x) x = x[srt] y = 13.0 + 3.0*x^2 - 3.9*x^3 plot, x, y, xstyle=3, ystyle=3
Next open a file (unit number 10) and use a for loop to write each element of the arrays x and y to the file. Learn about these IDL procedures by using HELP.
npts = n_elements(x) openw, 10, 'mydata.dat' printf, 10, '# These are my data.' for i = 0, npts-1 do printf, 10, x[i], y[i] close, 10
Take a look at the file mydata.dat in your local path by suspending your current IDL session. Now, let's use the Goddard Library readcol procedure to read in our data in two new variables (xpts and ypts) and plot them and generate postscript output. To learn what readcol does try either
.run -t readcol
or
doc_library, 'readcol'
To find out where readcol lives in your path (this is very useful!) use the which procedure:
which, 'readcol'
The argument required by which is the string name of the procedure for which you are looking. Now, type
readcol, 'mydata.dat', xpts, ypts, format='F,F', /silent plot, xpts, ypts, xsty=3, ysty=3
This should look right. To generate postscript output type
set_plot, 'ps'
device, filename='myplot.ps', xsize=8, ysize=8, xoffset=0, $
yoffset=0.5+(10-8), /inches
plot, xpts, ypts, xsty=3, ysty=3, xthick=2.0, ythick=2.0, $
thick=2.0, title='My Plot', charsize=2.0, charthick=2.0, $
xtitle='X Axis', ytitle='Y Axis'
device, /close
set_plot, 'X'
The dollar sign is used to continue one line of code on subsequent lines. We will cover plotting, I/O, and many more topics in greater detail later on.
Last update 2003 Nov 12. Email J. Moustakas with questions or comments.