Bash Shell Script Example
This example creates a Normalized Difference Vegetation Index (NDVI) image from an input QuickBird image, by invoking envitaskengine through a shell script.
- Start a terminal window.
- Create a scratch directory with write permission, where any temporary output can be written. Change to that directory:
- Run the script with the required arguments, as follows. You should run the script from the directory that has write permission. An NDVI image will be created in the
sandbox
directory in your home space.
mkdir ~/sandbox
cd ~/sandbox
If arguments contain spaces, then you may need to add double quotes around them. If you want to use the ~ symbol, then double quotes are unnecessary.
/usr/local/INSTALL_DIR/idl/examples/taskengine/example_spectral_index.sh /usr/local/INSTALL_DIR/envi/data/qb_boulder_msi "Normalized Difference Vegetation Index" ~/sandbox/ndvi.dat
Copy of Script
Here is a copy of the example_spectral_index.sh
script. Use this as a template for writing your own script, changing the arguments as needed.
# ! /bin/bash
#-------
# Usage
#-------
usage () {
echo "Usage:"
echo ""
echo " example_spectral_index.sh inputRasterFilename spectralIndex outputRasterFilename"
echo ""
echo "Example:"
echo ""
echo " ./example_spectral_index.sh \"/data/input.dat\" \"Normalized Difference Vegetation Index\" \"/data/output.dat\""
exit 1
}
#----------------
# Input arguments
#----------------
input_file=$1
spectral_index=$2
output_raster_uri=$3
if [[ -z ${input_file} ]]
then
usage
fi
if [[ -z ${spectral_index} ]]
then
usage
fi
if [[ -z ${output_raster_uri} ]]
then
usage
fi
#---------------------------------------------
# Build input JSON describing the task to run.
#---------------------------------------------
input_json='{'
input_json+=' "taskName" : "SpectralIndex"'
input_json+=' ,"inputParameters" : {'
input_json+=' "input_raster":'
input_json+=' {"url" : "'${input_file}'", "factory":"URLRaster"}'
input_json+=' ,"index" : "'${spectral_index}'"'
input_json+=' ,"output_raster_uri": "'${output_raster_uri}'"'
input_json+=' }'
input_json+='}'
#echo ${input_json} | jq .
#--------------------------------------------
# Find the location of the envitaskengine executable
# relative to the location of this script.
#--------------------------------------------
SOURCE="${BASH_SOURCE[0]}"
# resolve $SOURCE until the file is no longer a symlink
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve
# it relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
LOC="$( cd -P "$DIR" && cd -P ../../../envi && pwd )"
#--------------
# Run the task.
# Task output JSON will be written to stdout.
#--------------
echo ${input_json} | "${LOC}/bin/envitaskengine"