>  Docs Center  >  Using IDL  >  Creating $MAIN$ Programs

Creating $MAIN$ Programs

Creating $MAIN$ Programs

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.

Creating a $MAIN$ Program at the Command Line

To create a $MAIN$ level program at the command line, start IDL and complete the following steps:

  1. Initialize a variable. At the IDL command line, enter the following:

    A = 2

  2. Designate a command line $MAIN$ program. Enter .RUN at the IDL command line:


    The command line prompt changes from IDL> to -.

  3. Enter the program statements. Create a $MAIN$ level program consisting of the following statements:

    A = A * 2
    PRINT, A

    The $MAIN$ program is immediately compiled and executed when you enter the END statement. IDL prints 4.

  4. Re-execute the $MAIN program. Enter .GO at the IDL command line:


    The $MAIN$ program is executed again, and now IDL prints 8.

Creating a $MAIN$ Program in a Text File

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 create, save, run and test a $MAIN$ program.

  1. Create the $MAIN$ program file. Enter the following into the IDL Editor. This example consists of a function that modifies the image data, and a $MAIN$ program. The $MAIN program displays the original image, solicits a threshold value, passes the value to the function, and displays the new image data:

    FUNCTION stretchImage, img, value
    ; Stretch image by input amount.
    image = img > value
    RETURN, image
    ; --- Begin $MAIN$ program.--------------------- 
    ; Display the image, solicit threshold value and 
    ; display new results. 
    ; Set up display.
    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
  2. Save the $MAIN$ program. Save the file as interactivestretch.pro. It is important to note that a $MAIN$ program should not have the same name as any internal procedures or functions.
  3. Run the $MAIN program. Type the following at the command line to run the program:

    .RUN interactiveStretch.pro

    This compiles internal functions and procedures, and executes the $MAIN program. The command line prompt changes from IDL> to -.

  4. Enter a threshold value. Enter 67 (or any value between 0–255) at the command line and press Enter. This scales the image so that the remaining pixel values are stretched across all possible intensities (0 to 255).
  5. Test another threshold value. Enter .GO at the IDL command line:


    Enter a different value and press enter to see the results. These two final steps can be repeated as many times as you like.

© 2020 Harris Geospatial Solutions, Inc. |  Legal
My Account    |    Store    |    Contact Us