Compiling an IDL Program from Within Another Program
QUESTION: I know how to compile a program from the IDL command line, but how do I compile an IDL program from within another IDL program?
ANSWER: Normally programs don't have to be explictly compiled from within other program modules because IDL's automatic compilation rules take care of resolving program references. There are times, however, when it is necessary to explictly compile a program module from within another program module. In these cases, the RESOLVE_ROUTINE command is used.
The RESOLVE_ROUTINE command takes one parameter, the name (or names if you use a string array) of the IDL procedure you wish to compile or resolve. If the IDL routine is a function, you must also set the IS_FUNCTION keyword. For example, suppose I want to explictly compile the IDL function I wrote and named GETIMAGE from within one of my programs. My code might look like this:
PRO TESTIMAGE RESOLVE_ROUTINE, 'getimage', /IS_FUNCTION image = GETIMAGE() TVSCL, image END
The RESOLVE_ROUTINE command uses the usual IDL rules of looking in the subdirectories specified in the !PATH system variable in the order in which they are specified for the unresolved routine. Note that the routine is compiled even if a compiled version already exists in the IDL session.
There are just a couple of odd restrictions to RESOLVE_ROUTINE that I've found in using it on my Macintosh computer. (I assume these restrictions are generally applicable, although it is not always a good idea to make assumptions like this in IDL.) One restriction is that you cannot use a ".PRO" extension (or any extension, I guess) to the name of the routine. For example, at the IDL command line you can do this:
IDL> .COMPILE getimage.pro IDL. .COMPILE getimage
The two commands above are equivalent. But you can't do this when you use RESOLVE_ROUTINE:
RESOLVE_ROUTINE, 'getimage.pro', /IS_FUNCTION
Similarly, you can't use a fully qualified file name like you can with the .COMPILE command:
IDL> .COMPILE /usr/idl/myfiles/getimage.pro
This command also fails in IDL:
RESOLVE_ROUTINE, '/usr/idl/myfiles/getimage.pro', /IS_FUNCTION
The two restrictions above mean you can't get the name of a file to compile with a program like FINDFILE or FILEPATH.
Another restriction comes about in compiling IDL programs that may be procedures or functions. For example, if I want to compile the function GETIMAGE and the procedure XCOLORS I can do both at the IDL command like, like this:
IDL> .COMPILE getimage, xcolors
If I want to do the same thing from within an IDL program, I must segregate them into procedures and functions. In this case, I would need two commands:
RESOLVE_ROUTINE, 'getimage.pro', /IS_FUNCTION RESOLVE_ROUTINE, 'xcolors'
Compiling All Unresolved Program Modules
You can use the IDL command RESOLVE_ALL to iteratively resolve or compile any uncompiled user-written or library routines that are called in the program module containing the RESOLVE_ALL command. This is a useful command to include in program modules that are going to be part of an IDL SAVE file. (Remember that you can see which directories a compiled program module came from by using the "HELP, /SOURCE" command.)
RESOLVE_ALL does not resolve or compile program modules that are called from the CALL_PROCEDURE, CALL_FUNCTION, or EXECUTE commands. It also does not compile or resolve IDL utility routines (i.e., those routines that are located in files having some name other than the name of the routine).
For those people interested in resolving even utility routines, Peter Mason has written an IDL routine named RESOLVE_EVENMORE that Peter describes as "slower than [RESOLVE_ALL], but more persistent." The file can be downloaded from Peter's anonymous ftp server.
Copyright © 1996 David W. Fanning
Last Updated 17 December 1996