How to use parameters in scripts

Parameters in a script can replace (parts of) object names, operations, etc. Parameters in scripts work as DOS replaceable parameters in DOS batch files, and must be written on the Script tab of the Script editor as %1, %2, %3, up to %9.

To run a script with parameters using a Run Script dialog box

Example: calculating slope maps in degrees and in percentages

Suppose you have a segment map 'Contour' and an existing georeference 'MyGeoref' with a certain pixel size, and you create a script called 'slopes' to calculate slope maps in degrees and in percentages.

To obtain a slope map in degrees and a slope map in percentages, create the following script:

  1. Type on the Script tab of the script editor:
  2. %3 = MapInterpolContour(%1, %2)

    DX = MapFilter(%3, dfdx)

    DY = MapFilter(%3, dfdy)

    %4 = 100 * HYP(DX, DY) / PIXSIZE(%3)

    %5 = RADDEG(ATAN(%4 / 100))

    calc %3.mpr

    calc %4.mpr

    calc %5.mpr

    This script is more elaborately explained in MapCalc & TabCalc : creating and running scripts.

     

  3. On the Parameters tab of the script editor, select the number of parameters (in this case 5), type a name for every parameter and select for each parameter the object type. For example:
  4.  

    Parameter Name Type
    %1 Input contour segment map Segment Map
    %2 Georef for all output maps Georeference
    %3 Output DEM Filename
    %4 Output slope map (in percentages) Filename
    %5 Output slope map (in degrees) Filename

     

     

  5. On the Default Values tab, you may specify default objects and default filenames, for all variables/parameters that you wish to use in the Run Script dialog box.
  6. For instance:

     

  7. To run this script:
  8. The Run Script dialog box will appear:

     

    Run Script dialog box

    In the Run Script dialog box, all parameter names are listed as specified on the Parameters tab of the script editor.

    Click OK to execute the script.

Tips:

To run a script with parameters without using a Run Script dialog box

Example 1: calculating slope maps in degrees and in percentages

  1. If a Slopes script is defined as follows on the Script tab of the script editor:
  2. %3 = MapInterpolContour(%1, %2)

    DX = MapFilter(%3, dfdx)

    DY = MapFilter(%3, dfdy)

    %4 = 100 * HYP(DX, DY) / PIXSIZE(%3)

    %5 = RADDEG(ATAN(%4 / 100))

    calc %3.mpr

    calc %4.mpr

    calc %5.mpr

     

  3. You can run the script and skip the Run Script dialog box, by directly filling out all parameters the command line of the Main window:
  4.  

    run Slopes Contour.mps MyGeoref.grf DEM SlopePct SlopeDeg

    The first string found after the script name Slope will replace %1 in the script, the second one %2, etc. In this example it means that:

     

    Contour.mps replaces %1
    MyGeoref.grf replaces %2
    DEM replaces %3
    SlopePct replaces %4
    SlopeDeg replaces %5

Mind:

When you use long filenames as parameters, you must enclose the object name within single quotes; the .ext extension of ILWIS objects should be left outside the quotes. A valid example of using long names is for instance:
run Slopes 'My Contours'.mps 'My Georef'.grf 'my DEM' 'Slope Map in Percentages' 'Slope Map in Degrees'

For more information, refer to How to use long object names.

Of course, you could also specify the object names directly in the script itself, thus creating a script without parameters.

Example 2: calculating yield maps of different years

Suppose you have three landuse maps (LU70, LU80, and LU90) of different years (1970, 1980, 1990). Each map has an attribute table linked to it (LUTBL70, LUTBL80, LUTBL90), and each table contains a column YieldMainCrop.

To obtain attribute maps (yield maps) of all landuse maps, you can create the following script:

 

  1. Type on the Script tab of the script editor:

    Yield%1=MapAttribute(LU%1, YieldMainCrop)

    Yield%2=MapAttribute(LU%2, YieldMainCrop)

    Yield%3=MapAttribute(LU%3, YieldMainCrop)

  2.  

  3. If you do not specify parameters or default objects/filenames on the Parameters tab and the Default Values tab of the script editor, you can run the script 'Yields' from the command line of the Main window, by typing:
  4. run Yields 70 80 90

    In script Yields, '70' replaces %1, '80' replaces %2 and '90' replaces %3. You thus obtain three attribute maps (Yield70, Yield80, Yield90) from the three input maps (LU70, LU80, LU90).

Tips:

See also: