X
77 Rate this article:
No rating

Internal: How To Pass UNIX or DOS Shell Command Line Arguments To an IDL Application

Anonym

For IDL 6.1 and earlier, IDL Runtime and Virtual Machine do not take arguments that can be passed to a .sav application that users are initializing. In other words, there is no command line syntax like:

   idl -rt=myidlapp.sav, 10, 'a string', myKeyword=2.14

which can pass values or references from a UNIX or DOS shell to a just-starting IDL program. The IDL language, however, does offer a very easy workaround for this, in the workings of a function named GETENV. This Tech Tip shows how a user can use a UNIX shell script or a DOS batch file program in conjunction with an IDL program, which has implemented GETENV(), to pass values from a UNIX or DOS command line to the IDL program.



In many cases, it would be useful to be able to pass command line arguments to an IDL Runtime or Virtual Machine application. IDL does not directly support command line arguments in this manner, but there is an easy workaround. To do this, you will need to write a shell script that converts command line arguments into environment variables and then starts an IDL application that retrieves the environment variables for processing.

For example, this is a UNIX shell script ("myidlapp") which makes command line arguments available to to an IDL application called "myidlapp.sav":
    #!/bin/sh

    # Filename: myidlapp
    #
    # This UNIX shell script should be called with the syntax:
    #
    #      myidlapp
    #
    # For example:
    #
    #      myidlapp PI 3.14159
    #

    # The three lines below define PARAM_ONE with the value of arg1
    # and PARAM_TWO with the value of arg2, as string-type arguments
    # that will be used by the IDL program "myidldapp.sav".

    PARAM_ONE=$1
    PARAM_TWO=$2
    export PARAM_ONE PARAM_TWO

    # Launch the IDL application 'myidlapp.sav' in the IDL Virtual
    # Machine environment. The command assumes that 'myidlapp.sav'
    # is in the current working directory.

    idl -vm=myidlapp.sav

    # If an IDL license is available, you can launch have the
    # IDL application launch in licensed IDL Runtime mode by
    # instead using the "-rt=" switch with the above command. For
    # example:
    #
    #      idl -rt=myidlapp.sav

Before we show the DOS batch program equivalent of the above, let us look at the minimum makeup of an IDL procedure that is able to take advantage of the above shell script. This example assumes that 'myidlapp.sav' is a compilation of 'myidlapp.pro':

    PRO myidlapp
       ; This example assumes that arg1/PARAM_ONE above
       ; is a string-type and arg2/PARAM_TWO is a float-type.
       my_runtime_string = GETENV('PARAM_ONE')
       my_runtime_float = FLOAT(GETENV('PARAM_TWO'))

       ; Prove that the command-line args passed in properly
       msg = STRARR(2)
       msg[0] = 'The first arg was "' + my_runtime_string + $
          '" of type ' + STRUPCASE(SIZE(my_runtime_string, /TNAME))
       msg[1] = 'The second arg was "' + $
          STRTRIM(my_runtime_float, 2) + $
          '" of type ' + STRUPCASE(SIZE(my_runtime_float, /TNAME))
       void = DIALOG_MESSAGE(msg, /INFO)
    END

The first shell script example demonstrated a UNIX scenario. Let's now take a look at the Windows DOS batch script program which could call the same 'myidlapp.sav' application binary:

    @ECHO OFF
    REM <--- This is the DOS comment command
    REM Filename: myidlapp.bat

    REM This DOS batch script should be called with the syntax:
    REM
    REM      myidlapp.bat
    REM
    REM For example:
    REM
    REM      myidlapp.bat PI 3.14159
    REM

    REM Restrict variable definitions to the local shell
    SETLOCAL

    REM The two lines below define PARAM_ONE with the value of arg1
    REM and PARAM_TWO with the value of arg2, as string-type arguments
    REM that will be used by the IDL program "myidldapp.sav".

    SET PARAM_ONE=%1
    SET PARAM_TWO=%2

    REM Launch the IDL application 'myidlapp.sav' in the IDL Virtual
    REM Machine environment. This example assumes that IDL 6.1
    REM is installed in the default location and that 'myidlapp.sav'
    REM is in the directory "C:\RSI".

    C:\RSI\IDL61\bin\bin.x86\idlrt.exe -vm="C:\RSI\myidlapp.sav"

    REM If an IDL license is available, you can launch have the
    REM IDL application launch in licensed IDL Runtime mode by
    REM removing the "-vm=" switch in the above command. For
    REM example:
    REM
    REM C:\RSI\IDL61\bin\bin.x86\idlrt.exe "C:\RSI\myidlapp.sav"

    REM End local environment
    ENDLOCAL

As you see, the principle remains the same, but the exact syntax of shell executables varies depending on what shell you are using. In all cases be careful to choose unique names for your environment variables, so that they do not clash with environment variables already in force on your system.

Finally, you can demonstrate the principles of this Tech Tip if you compile the PRO code we show above into a .sav file. Here is a simple way to do that:

    IDL> .full_reset_session
    IDL> .compile myidlapp
    IDL> resolve_all
    IDL> save, /routines, filename='myidlapp.sav'

Make sure that the 'myidlapp.sav' ends up in the directory path that your script is expecting. Then at your shell command line run the script command with a string argument followed by a numeric argument like the following. For UNIX:

    myidlapp PI 3.14159

Or for Windows:

    myidlapp.bat PI 3.14159

When the IDL application runs, it should display a dialog like this:

Download the example files: