IDL by Examples

This page is being rewritten from a Unix page to an IDL page.


Emner:

Declarations/Variables
Procedures/Functions
Control flow
Input/Output
Array handling


Definition of variables

Command Description
a = 1
b = 2.5
c = 1.3D0
d = fltarr(100)
e = fltarr(10,10)
index = indgen(50)
f = replicate(2.5, 100)
g = 'mytext'
h = make_array(3,4,/integer,value=5)
create an integer with value 1
create a real with value 2.5
create a double real with value 1.3
create reall array
create real 2D aray
create integer array with values [0.....49]
create real array with value 2.5
create string
create 2D integer array with value 5

Procedures/Functions

IDL programs are best constructed in a modular fashion using procedures or functions. The filename under which a procedure/function is saved must be the name of the procedure/function with the extension .pro.

Example of procedure definition
PRO mypro, arg1, arg2
.
.
.
END

Example of function
FUNCTION myfun, arg1, arg2, arg3
.
.
something = arg1*cos(arg3)
return, something
END

Control flow

In order to control the flow of a job the usual statements known from other programming languages can be used. All the usual constructions are present in IDL

If statement:
if a eq b then begin
.
.
endif

For loop:
for i=1,25,2 do begin
.
.
endfor

While loop:
while i lt n do begin
.
.
endwhile

Repeat loop:
repeat
.
.
endrep until j lt k

Case construction:
case x of
1: print, 'one'
2: print, 'two'
3: print, 'three'
endcase


Input/Output


Reading from a file:
openr, 1, 'name_of_myfile'
readf, 1, a, b, c
close, 1

Writing to file:
openw, 1, 'name_of_myfile'
printf, 1, format='(A20,F12.4, I5)', 'Output: ', realnumber, intnumber
close, 1

Writing to terminal window (standard output):
print, 'Some text'


Array handling

Command Description
a = b[1,5]
a = b[1,*]
a = b[6:10]
a[2:7] = b[6:10]
indx = sort(b) $ a = b(indx)
indx = where(b gt 0,c) $ a = b(indx)
select one element
second row
5 element part
move section from b to a
sort b into a
a = part of b > 0