|
Application Programming: Creating and Running Programs in IDL |
|
A $MAIN$ (main-level) program can be created in two ways: at the command line and in a text editor. You typically create a $MAIN$ program at the IDL command line when you have a few commands you want to run without creating a separate file to contain them. Creating a $ MAIN$ program in a text file allows you to combine the functionality of named procedures and functions with the ability to have command line access to variable data that is defined in the $MAIN$ scope.
$MAIN$ programs are not explicitly named; they consist of a series of statements that are not preceded by a procedure (PRO) or function (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 either as the result of a .RUN executive command, or in a text file, it compiles it into the special program named $MAIN$ and immediately executes it. Afterwards, it can be executed again using the .GO executive command.
To create a $MAIN$ level program at the command line, start IDL and complete the following steps:
A = 2
.RUN
The command line prompt changes from IDL> to -.
A = A * 2 PRINT, A END
The $MAIN$ program is immediately compiled and executed when you enter the END statement. IDL prints 4.
.GO
The $MAIN$ program is executed again, and now IDL prints 8.
When you create a $MAIN$ program in a named text file, you can execute the program and have command line access to variables. This is an easy way to run and test various variable values without having to modify the code and rerun the entire program, or set breakpoints. The following example allows you to
FUNCTION stretchImage, img, value
; Stretch image by input amount.
image = img > value
RETURN, image
End
; --- Begin $MAIN$ program.---------------------
; Display the image, solicit threshold value and
; display new results.
; Set up display.
DEVICE, DECOMPOSED = 0, RETAIN = 2
LOADCT, 0
; Access image data and display.
img = READ_PNG(FILEPATH('mineral.png', $
SUBDIRECTORY = ['examples', 'data']))
dims = SIZE(img, /DIMENSIONS)
WINDOW, 0, XSIZE = dims[0], YSIZE = dims[1]
TVSCL, img
; Ask for a threshold value and stretch image.
READ, threshold, PROMPT='Enter Numerical Value: '
newImg = stretchImage(threshold, img)
; Display the results.
TVSCL, newImg
END
interactivestretch.pro. It is important to note that a $MAIN$ program is not given a name that is the same as any internal procedures or functions.
.RUN interactiveStretch.pro
This compiles internal functions and procedures, and executes the $MAIN program. The command line prompt changes from IDL> to -.
.GO
Enter a different value and press enter to see the results. These two final steps can be repeated as many times as you like.
IDL Online Help (March 06, 2007)