We need to move away from the interactive mode into the compiled mode. Before doing so, we need to cover some vocabulary.
Procedure and Function : We are going to be using IDL as a programming language. To do so, we will be using procedures and functions.
A procedure is a series of command lines. We define a procedure by its name and a parameter list:
PRO PROCEDURE_NAME, parameter_list
; IDL is case insensitive, but we will use capital letters
; for clarity. Punctuation is important in programming!
; a semi-colon means "ignore everything after it"
A function performs a specific task or calculation, and returns a value. We define a function by its name and a parameter list:
FUNCTION FUNCTION_NAME, parameter_list
We may have a MAIN program (a program may have just one "main" program, but any number of procedures and functions) that is set up to call PROCEDURES and/or FUNCTIONS. What do we mean by "call"? We mean to be excellent programmers! We mean to break our programs into bite-size, bug-free (or debugging-friendly) units, where we can test each procedure to make sure it is doing what we want it to do. (Recall: garbage-in, garbage-out? The computer will do exactly what we tell it to do, which isn't always what we intended it to do!) So, let's imagine that we want to call procedures and functions. Here's how we do it, with some made-up names:
PROCEDURE1_NAME, parameters1
PROCEDURE2_NAME, parameters2
result_we_want = FUNCTION_NAME(parameter_list)
Notice that the syntax is definitely different! Assigning the result of calling the function to a variable and the use of parentheses are distinguishing features of calling a function. I've often found it helpful to think of a procedure as an overall block of code designed for a specific purpose, and a function as a sub-set of that purpose. Let's say we are going to write a procedure that takes us from being bare-naked to fully dressed. Within that procedure, we call a function that tell us to pick up an item of clothing and another procedure that tells us to put it on. You can cycle through these two functions at each stage, until there is a final test as to whether or not any pieces of clothing remain. If all items of clothing are gone, the program ends. Maybe you put in a condition statement at the start of the procedure that allows for input as to the weather conditions. In this case, you call another procedure (which could call the same set of functions so you only have to write them once) that takes into consideration the fact that you are going to have to put more clothes on. When all conditions are met, or you reach the end of the procedure, you come to an END.
Procedures are normally used when more than one variable is passed or returned from a program. It may have one or more input arguments (although it may have none) and it may have one or more output arguments (although it may have none). Functions are programs that return a result via an assignment statement (e.g., y = sin(x)). A function typically has one input argument (although it may have more or none).
Let's take a look at a simple program that takes a given temperature in Fahrenheit (we'll learn how to input different values later) and converts it to Celsius. Since we are no longer in interactive mode, this is code that you could create using a simple text editor (NOT MS Word or other formatting editors).
PRO CONVERT_TEMPS f_deg = 72.0 deg_c = f_to_c(f_deg) print, f_deg, 'degrees F equals', deg_c END ; ; FUNCTION F_TO_C, F_DEG return, (f_deg - 32.0) * (5.0 / 9.0) END
IDL does not require the capitalization of the procedure or function statement, nor the end statement; recall, IDL is case insensitive. However, when working in the UNIX or Linux environments, remember that these operating systems ARE case sensitive, and you should probably get into the habit of having file names always lower-case. The standard file-naming format for IDL source file is to have the procedure or function name followed by a .pro extension. For our temperature conversion program, that could be something like convert_temps.pro . You could have both the procedure and function in the same file. However, for large programs this is not a good idea. If we were to separate the procedure and function programs here, we could name the function something like f_to_c.pro .
Compiling Programs
We will be compiling all of our programs manually, to keep things simple. From now on, you should create a dedicated directory or set of directories for your programming work. Open a text editor (emacs is a good one), and type in the code given just above for the temperature conversion program, putting both programs into one file. If you were to use two different file, then you would have to compile both, which could be done on the same command line.
Start IDL.
IDL> .compile convert_temps.pro ;use your file name, if different, duh!
IDL> convert_temps ;did it work?
Let's modify the conversion program to allow the input of different Fahrenheit temperatures. Edit your procedure so that it reads like the following (don't forget to save the changed file):
PRO CONVERT_TEMPS, F_DEG deg_c = f_to_c(f_deg) print, f_deg, 'degrees F equals', deg_c END ; ; FUNCTION F_TO_C, F_DEG return, (f_deg - 32.0) * (5.0 / 9.0) END
You will need to recompile the program. You can do this without ever leaving your IDL session:
IDL> .compile convert_temps ; you can omit the .pro, as it is the default extension IDL> f_deg = 72.0 IDL> convert_temps, f_deg ;did it work?
Control Statements
These statements are just what they say they are: a way for you, the programmer, to control the order of the computations, the input, the output, the tests, etc. Here is a list of the control statements that we will be using the most:
if then else
if condition then statement begin
endif
if condition then begin
statement(s)
endif else begin
statement(s)
endelse
for (three different examples):
for i = var1, var2 do statement ;single statement is executed, incremented by 1
for i = var1, var2, increment do statement ;single statement executed, with specified -- increment
for i = var1, var2, increment do begin
statement(s)
endfor ;many statements executed. endfor is required
Increments can be positive or negative. The last iteration occurs the final time i (if positive) is less than or equal to var2 . When the increment is negative, the last iteration occurs the final time i is greater than or equal to var2 .
According to Gumley ¨ , floating-point loop variables are permitted, but not recommended. This is because floating-point variables have limited accuracy, and your program may jump out of the loop unexpectedly, giving you erroneous results and, most certainly, an even worse headache.
while
The while statement executes a single statement or a block of statements while a specified condition is true.
while condition do statement
while condition do begin
statement(s)
endwhile
Here condition is a scalar expression that evaluates to true or false.
Here is an example from Gumley that will also introduce you to how we open and read a file:
PRO TEST_WHILE, FILENAME
openr, lun, filename, /get_lun ;
open for r eading, l
ogical u nit n umber
record = '' ;makes record a string variable
while (eof(lun) ne 1) do begin ;
e nd o f f ile not
equal to true
readf, lun, record
print, record
endwhile
free_lun, lun ;closes the file and frees up the unit number
END
repeat
The repeat statement executes a single statement or a block of statements until a specified condition is true. For example:
repeat statement until condition
repeat begin
statement(s)
endrep until condition
A cute little program from Gumley follows. It counts the number of text lines entered entered until the user enters the string 'done':
PRO TEST_REPEAT
text = ' ';text defined as a string variable
count = 0 ;initialized
print, 'Enter text (enter done to quit)'
repeat begin
read, text
count = count + 1 ;What's being done here?
endrep until (text eq 'done')
print, 'Number of lines entered: ', count - 1
END
Create a file called test_repeat.pro and type in (or copy and paste) the above code. To run the program:
IDL> .compile test_repeat
IDL> test_repeat
Follow-up required reading (and practice):
You can get access to this book without purchasing it through the UW's subscription to eBooks! Sign up now:
http://legacy.netlibrary.com/titleselect/signup.asp
Now do assignment 2.
¨ Gumley, Liam E., 2001, Practical IDL Programming (Morgan Kaufmann Publishers, California), p. 102