Home | Categories | Alphabetical | Classes | All Contents | [ < ] | [ > ]

What is an IDL Program?


An IDL program consists of one or more IDL commands, and may or may not be compiled before execution. IDL programs can take any of the following forms:

Batch Files

A batch file contains one or more IDL statements or commands. Each line of the batch file is read and executed before proceeding to the next line. This makes batch files different from main-level programs, which are compiled as a unit before being executed, and named programs, in which all program modules are compiled as an unit before being executed. A file created by the JOURNAL routine is an example of an batch file. For information on running include files, see Executing Batch Jobs in IDL.

Note
Batch files are sometimes referred to as include files, since they can be used to "include" the multiple IDL statements contained in the file in another file.

Main-Level Programs

Main-level programs are entered at the IDL command line, and are useful when you have a few commands you want to run without creating a separate file to contain your commands. Main-level programs are not explicitly named; they consist of a series of statements that are not preceded by a procedure or function heading. They do, however, require an END statement. Since there is no heading, the program cannot be called from other routines and cannot be passed arguments. When IDL encounters a main program as the result of a .RUN executive command, it compiles it into the special program named $MAIN$ and immediately executes it. Afterwards, it can be executed again by using the .GO executive command.

The following example creates a simple main-level program:

  1. Start IDL
  2. At the IDL command line, enter the following:
  3.    A = 2 
    
  4. Enter .RUN at the IDL command line. The command line prompt changes from IDL> to -.
  5. Enter the following:
  6.    A = A * 2 
       PRINT, A 
       END 
    
  7. This creates a main-level program, which compiles and executes. IDL prints 4.
  8. Enter .GO at the IDL command line. The main-level program is executed again, and now IDL prints 8.

Named Programs

Longer routines and programs, consisting of more than a few lines, are typically given explicit names, allowing them to be called from other programs as well as executed at the IDL command line. Named programs are stored in disk files created using a text editor.

Note
See Library Authoring for information on naming procedures to avoid conflicts with IDL routine names. It is important to implement and consistently use a naming scheme from the earliest stages of code development.

Files containing named programs are generally named with the name of the program and the filename extension .pro. See Compiling and Running Your Program for additional information on file naming.

Note
Although any text editor can be used to create an IDL program file, the IDL Editor contains features that simplify the process of writing IDL code. See Using the IDL Editor for details on using the IDL Editor.

Most IDL applications consist of one or more IDL procedures, functions, object definitions, and object method routines:

Example: A Simple IDL Program

For example, suppose you have a file called hello_world.pro containing the following code:

PRO hello_world 
   PRINT, 'Hello World' 
END 

This IDL "program" consists of a single user-defined procedure.

IDL program files are assumed to have the extension .pro or the extension .sav. When IDL searches for a user-defined procedure or function, it searches for files consisting of the name of the procedure or function, followed by the .pro or .sav extension.

Procedures and functions can also accept arguments and keywords. Both arguments and keywords allow the program that calls the routine to pass data in the form of IDL variables or expressions to the routine.

For example, the previous user-defined procedure could be changed to include an argument and a keyword:

PRO hello_world, name, INCLUDE_NAME = include 
   IF (KEYWORD_SET(include) && (N_ELEMENTS(name) NE 0)) THEN BEGIN 
      PRINT, 'Hello World From '+ name  
   ENDIF ELSE PRINT, 'Hello World' 
END 
 
PRO hello_world, name, INCLUDE_NAME = include 
   IF (KEYWORD_SET(include)) THEN PRINT, 'Hello World From ' + $ 
      name ELSE PRINT, 'Hello World' 
END 

Now if the INCLUDE_NAME keyword is set to a value greater than zero, the above procedure will include the string contained within the name variable if a value was supplied for the name argument

Procedures and functions are collectively referred to as routines. An IDL program file may contain one or many routines, which can be a mix of procedures and functions.


Home | Categories | Alphabetical | Classes | All Contents | [ < ] | [ > ]