You are on page 1of 8

IDL Interactive Data Language This is just a quick tutorial about some of the basic tools that you

u will need to program in IDL. Many of the notes from this tutorial have been taken from ATOC 7500.

Variables IDL is a programming language that supports different variables: scalars, arrays (ndimensional), and structures. The variables can be integers, long integers, real numbers, double precision reals, strings, logical variables, or binary variables. To set the variable a to the value 5 type the following at the IDL prompt (IDL>): IDL> a=5 To set a 1D array b to the values 1 3 2 9 0 type: IDL> b=[1,3,2,9,0] Now type: IDL> help The output will read: % At $MAIN$ A INT = 5 B INT = Array[5] Compiled Procedures: $MAIN$ Compiled Functions: The help command in IDL gives us information about variables, procedures, and functions that are currently defined or loaded for use during the IDL session.

It is important to point out that the first index of an IDL array is 0. Now test this by typing in the following examples: IDL> print, b(0) 1 IDL> print, b(2) 2 In a 1D array it is very simple to find the maximum value in the array IDL> print, max(b) 9 You can also find the position of the maximum value in the array by typing:

IDL> q=where(b eq max(b)) IDL> print, q 3 IDL> print, b(q) 9 q is just a variable name, you can substitute any other variable name for q.

Now try: IDL> A=6 IDL> help You will notice that IDL is NOT case sensitive. In IDL, a variable named date is the same as Date, DATE, and DaTe. Now type: IDL> A=6. Notice that the type of the variable A (or a) changed to A FLOAT = 6.00000 Now build a float array with 10 elements: IDL> B=fltarr(10) IDL> help, B B FLOAT = Array[10] Now print out array B: IDL> print, B 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 Notice that by defining the array B (or b) as a floating point array of the length 10 each element of the array was set to zero value. Now try: IDL> a=1 IDL> c=a*b IDL> help, c C FLOAT = Array[10] Notice that the operator * is a multiplication operator and that multiplying an array by a scalar (1D array) resulted in a new array (c). Notice that the new array is a floating point array (although scalar a is an integer). Similarly we can add, subtract, or divide an array by a scalar: IDL> c=[1.,1.,1.,1.] IDL> d=c+1. IDL> print, d

2.00000 IDL> e=d/2. IDL> print, e 1.00000 IDL> f=e/d IDL> print, f 0.500000

2.00000

2.00000

2.00000

1.00000

1.00000

1.00000

0.500000

0.500000

0.500000

Notice that we can successfully divide one array by another; however, in order to perform any mathematical operation with two arrays they MUST be compatible in size. If they are not compatible in size you will introduce an error into your code! For example: IDL> g=[2.,1.] IDL> h=g*f IDL> print,h 1.00000 0.500000 You can also define multidimensional arrays in IDL. For example, IDL> p=fltarr(3,2) IDL> print, p 0.000000 0.000000 0.000000 0.000000

0.000000 0.000000

IDL uses [column, row] notation for 2D arrays. If we had an array A with c number of columns and r number of rows, the indexing for the array would look like this: A0,0 A1,0 Ac-1,0 A0,1 A1,1 Ac-1,1 A0,r-1 A1,r-1 Ac-1,r-1 We can set p(0,0) to 1 by typing: IDL> p(1,0)=1 IDL> print,p 0.000000 1.00000 0.000000 0.000000 0.000000 0.000000 Now type: IDL> p(0,2)=2 % Attempt to subscript P with <INT % Execution halted at: $MAIN$

2)> is out of range.

IDL will give you an error because there is not a row with an index of 2, recall that the indexing starts with 0. Now type: IDL> p(0,1)=2 3

IDL> print,p 0.000000 1.00000 2.00000 0.000000

0.000000 0.000000

Now set all elements in the p array to zero by using * IDL>p(*,*)=0 Now set all elements in the first two columns equal to 1. IDL> p(0:1,*)=1 IDL> print, p 1.00000 1.00000 0.000000 1.00000 1.00000 0.000000

To define a string, type: IDL> v=this is a string If x is defined as IDL> x=2 You can turn x into a string by typing in the following: IDL> x=string(x) Define an integer array of size 2x3: IDL> k=intarr(2,3) Define a single precision floating point array of size 2x3: IDL> k=fltarr(2,3) Define a string array of size 2x3: IDL> k=strarr(2,3) Define a long integer array of size 2x3: IDL> k=lonarr(2,3) Define an integer array, where the elements are set to the value of the indices: IDL> a=indgen(10) Define a real array, where the elements are set to the value of the indices: IDL> b=findgen(10) Define a string array, where the elements are set to the value of the indices: IDL> c=sindgen(10)

Mathematical Operations in IDL () group expressions, subscripts, and function parameter lists ^ exponentiation * multiplication / division + addition subtraction # matrix multiplication mod modulus abs absolute value sqrt square root sin sine cos cosine tan tangent asin arc sine acos arc cosine atan arc tangent exp exponential function alog natural log alog10 log base 10 min minimum value in the array max maximum value in the array not eq ne le lt ge gt and or Boolean negation equality inequality less or equal less than greater or equal greater than Boolean and Boolean or

Plotting in IDL Before we write a code to make a plot, lets make some plots from the IDL prompt (IDL>). To plot the content of a 1D array use the command plot IDL> a=[1,3,2] IDL> plot, a After you type these two lines at the IDL prompt a graphics window will open. The plot will display the content of the array a(i) as a function of the array index i=0,1,2.

To make an x y plot, do the following: IDL> X=[3,4,5] IDL> Y=[1,2,3] IDL> plot, x, y Now lets use symbols, instead of a line, and lets control the range of the x and y axes: IDL> plot, x, y, xrange=[2,6], yrange=[0,4], psym=2 You can control the type of symbols by setting psym to different values. You can write over an existing plot by using the command oplot. Try this; IDL> plot, x, y, xrange=[2,6], yrange=[0,4], psym=2 IDL> a=[3,4,5,6] IDL> b=[2,2,2,2] IDL> oplot, a,b

Basic Programming in IDL Now that we have gone over some basics about IDL, you are ready to write an IDL program. To make a program go to File > New > Editor. Write your program in the window labeled Untitled1. After you write your code, you must type end at the end of your program. For example, here is a simple program that will print out hi: print, hi end Save your program by going to File > Save. You must first compile the program before you can run it. To compile go to Run > compile name of program.pro. Now run your program by clicking on Run > name of program. If you make changes to your program, you must recompile the program before you run it.

Produce a PostScript Plot from IDL To produce a PostScript plot from your IDL code, you must include the following key lines in your code: set_plot, 'ps' device, /put in key words device, filename='nameofplot.ps' ;insert plotting routine here device,/close end

; some keywords are listed below ;the default is idl.ps

Some keywords that can be used with device are: /portrait /landscape /inches /encapsulated /color You can find more information about device by looking in IDL help. Some other useful plotting commands that you may want to look up are: !p.multi used to make multiple plots on a page /xlog specifies a x logarithmic axis /ylog specifies a y logarithmic axis symsize specifies the size of symbols that are drawn (the default is 1.0) psym specifies data points using symbols (1= plus symbol, 2=*,) linestyle specifies the line style used to draw the lines (0= solid line, 1= dotted, ) loadct loading color table title set the plot title xtitle set the x axis title (can not be called in oplot) ytitle set the y axis title ( ) thick set the thickness of a line xthick, ythick set the thickness of axis lines charthick set the thickness of annotation charsize set the size of annotation xyouts put label on the plot contour a command to make contour plots ; indicates a comment in the program. $ used to continue a command on the next line

Here is an example of a code that plots and x y plot with symbols and a dashed line. The code also uses some plotting commands that are listed above. set_plot, 'ps' device, /landscape ; some keywords are: /landscape or /portrait, /inches device, filename='name.ps' ;the default is idl.ps ;plotting routine X=[3,4,5] Y=[1,2,3] plot, x,y, xrange=[2,6], yrange=[0,4], psym=2, xtitle='put x title here', $ ytitle='put y title here', title='put title here', symsize=3 ; this uses symbols to plot oplot, x,y, linestyle=2 ; this plots a dashed line xyouts,2.4,3.5, 'Name of line 1' ;put a label on the plot

device,/close end

Another important tool for writing an IDL program is knowing how to restore a data file. Suppose you have an IDL save file, data.idlsav, that contains the following 1D arrays: DAY NUMBER INT = Array[4] INT = Array[4]

In order to plot the arrays you must first restore the data file by using the following command: restore, data.idlsav If the IDL save file is not in your local directory you may need to also include the pathname in the restore command: restore, C:/datadirectory/data.idlsav' Additional Help To get additional help in IDL type ? at the IDL prompt IDL> ? or check out http://www.dfanning.com/

You might also like