program names, file names
variable names
prompts, commands, program code
In a given IDL session, data is stored in named variables. The user creates these variables with the values of the interesting data. These variables are lost when the IDL session ends, unless the user explicitly saves them.
Variables have different types: integer, floating point number, double precision floating point number, complex number, character string, an array of a single type, a structure whose elements may have several different types, etc. Creating an integer variable whose name is intvar1 and whose value is 8 is accomplished with a simple assignment statement:
IDLprompt> intvar1=8 |
IDLprompt> fltvar1=2. |
A variable is undefined until a value is assigned to it. Trying to access the value of an undefined variable causes an error. And sending an undefined variable to a function (discussed below) is just asking for trouble.
Mathematical functions are defined for most types. For example, one-fourth of intvar1 can be assigned to intvar2   like this:
IDLprompt> intvar2 = intvar1 / 4 |
IDLprompt> intvar3 = intvar1 / 5 |
IDLprompt> intvar1 = 7 / 2. |
IDLprompt> numarray1 = [ 2 , 7 , 6 , 9. ] |
IDLprompt> numarray2 = numarray1 + [ 8 , 4 , 4 , 3 ] |
IDLprompt> numarray3 = numarray1 * numarray2 |
To save variables (whatever their type), use the following command before exitting the IDL session:
IDLprompt> save, varname1, varname2, varname3, filename='filename.dat' |
IDLprompt> restore, 'filename.dat' |
You can delete variables to save memory. This is a nice thing to do, as the image data starts to take up lots of space on the computer running IDL. Deleting a variable removes the reference to that piece of data and returns that address in memory to the "available" state. The following command will delete the variables var1, var2, var3, ..., varn:
IDLprompt> delvar, var1, var2, var3, ..., varn |
Programs: procedures v. functions
In case you get bored and stop reading this section, I'll go ahead and mention this now: a very important thing to keep in mind about IDL is that it is a call-by-reference language, which means that any arguments sent to a program CAN be changed by the program's code. See the program's documentation to be sure.
There are lots of IDL programs at your disposal already: the progams which come with the IDL software (the startup file puts those in your !path), the programs in the ~/idlpro directory you created earlier (also in your !path), and any programs you write in the future.
IDL programs come in two varieties: procedures and functions. They have slightly different syntax and somewhat different purposes. We've already seen a few procedures: ls, pwd, c, mkdir, rm, save, and restore. The general format for running a procedure is the following:
IDLprompt> procedurename, arg1, arg2, ... , argn, key1=key1val, $ IDLprompt> key2=key2val, ... , keym=keymval |
IDLprompt> save, var1, var2, filename='datafile.dat' IDLprompt> save, var1, filename='datafile.dat', var2 |
The dollar sign ($) in the above statement means that the current command is too long for the line: type as far as you want, hit the dollar sign and the carriage return, then resume typing the command on the next line (a new prompt will be generated, but that's OK). This is not strictly necessary at the command line (it'll just wrap the text to the next line for you -- just keep typing), but it's quite useful in written programs -- it can really help readability.
An IDL function call looks like the following:
IDLprompt> result = functionname( arg1, ... , argn, key1=key1val, $ IDLprompt> ... , keym=keymval ) |
Another feature of IDL keywords is that there is a shorthand for setting Boolean keywords to positive states:
IDLprompt> procedurename, arg1, arg2, key1=key1val, /boolkey |
IDLprompt> procedurename, arg1, arg2, key1=key1val, boolkey=value |
When IDL starts, it has no executable code. All programs must be compiled before being run. Whenever a program call is made, IDL looks in its list of compiled programs to see if executable code for that program has been created. If not, IDL looks through the !path for source code and compiles the source code into an executable (assuming that the source code could be located). If, on the other hand, IDL does find executable code, it runs that code without checking to see if the source code has been changed since the last compilation. The point of that is that if you alter source code in an IDL session after the program has been compiled, you will need to recompile the source code for the executable to reflect the changes you've made. To compile the code in a file called newprog.pro (which is within the !path), use the following command:
IDLprompt> .r newprog |
IDLprompt> .r 'reldir/progfilename.pro' |