IDL philosophy

program names, file names
variable names
prompts, commands, program code


Contents:

Variables

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
The following command creates a floating-point variable whose name is fltvar1 and assigns to it a value of 2:
IDLprompt> fltvar1=2.
Note the decimal point in the second example.

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
This will create the variable intvar2   and give it an integer value of 2  . However, the following operation produces a result which you might not expect:
IDLprompt> intvar3 = intvar1 / 5
intvar3   will have a value of 1, not 1.6, because intvar1 and 5 are integer values, so integer arithmatic is performed. Promotions are made in mathematical operations on mixed types:
IDLprompt> intvar1 = 7 / 2.
will convert intvar1 to a floating point number and give it a value of 3.5 (it previously had an integer value of 8), because   2.   is a floating point number, and so the result is a floating point number.

You can also make arrays:
IDLprompt> numarray1 = [ 2 , 7 , 6 , 9. ]
This will be a floating point array (because 9.   is a floating point number) of 4 elements. IDL indexes arrays from zero, so numarray1(2)   is a floating point number of value 6.  . And you can do array arithmatic:
IDLprompt> numarray2 = numarray1 + [ 8 , 4 , 4 , 3 ]
numarray2   will have floating point values of   [ 10. , 11. , 10. , 12. ] . Array multiplication (or division) is done element-by-element -- it is NOT matrix multiplication, as in linear algebra:
IDLprompt> numarray3 = numarray1 * numarray2
creates the array numarray3 and gives the following values to its elements:   [ 20. , 77. , 60. , 108. ] .

To save variables (whatever their type), use the following command before exitting the IDL session:
IDLprompt> save, varname1, varname2, varname3, filename='filename.dat'
where varname1, etc., are the names of the variables you want to save (this list can be as long or as short as you want), and the name in single quotes is the pathname (relative to the current working directory) of a file to which you want to save the variable -- the .dat extention is not necessary, but is recommended (makes the file easily recognizeable for what it is). This command will save the variable names, types, and values to the specified file. The filename='filename.dat' argument may be omitted, in which case IDL will save the variables to a file called idlsave.dat in the current working directory. The values in the file may be restored in any IDL session by using the following command:
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
This list can be as short or as long as necessary.


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
where this procedure expects n positional arguments (arg1, etc.) and m keyword arguments (key1, etc.). The arguments (arg2, key3val, ...) which you are providing for this procedure call can be variable names or explicit values (if key3 wants the number of days in the current workweek, you could give it a named variable which has the correct value, or you could type key3=5 in the procedure call). Positional arguments must be in the correct order, as specified by the procedure documentation. Keyword arguments can be anywhere in the call. The following two commands will have the same effect:
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 )
The above discussion regarding the placement of positional and keyword arguments and the use of dollar signs is the same. The major difference is that a function returns a single value (or a single array, or a singe structure, etc.) which is assigned to result.

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
(with the slash in front of boolkey). This syntax sets the Boolean keyword boolkey, so that when procedurename runs, the code knows to take whatever action is appropriate to having that keyword set (hopefully this is explained in the documentation to procedurename). The above code has the same effect as the following:
IDLprompt> procedurename, arg1, arg2, key1=key1val, boolkey=value
where value is a defined variable which has any nonzero value off any type.


Source code v. executalbes

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
Yes, that's newprog, not newprog.pro -- the above command tells IDL to look in the !path for a file callend newprog.pro and, upon finding it, to compile the code in the file. You can also tell IDL to compile the contents of a particular file by enclosing the pathname (relative to the current IDL directory) in single quotes:
IDLprompt> .r 'reldir/progfilename.pro'
Note that there is no comma after .r   .


Carl Welch