#!/bin/sh

############################################################################
#
# MODULE:       i.oif
# AUTHOR(S):    Markus Neteler
# PURPOSE:      calculates the Optimum Index factor of all band combinations
#               for LANDSAT TM 1,2,3,4,5,7
# COPYRIGHT:    (C) 1999 by the GRASS Development Team
#
#               This program is free software under the GNU General Public
#               License (>=v2). Read the file COPYING that comes with GRASS
#               for details.
#
#############################################################################
# Ref.: Jensen: Introductory digital image processing 1996, p.98
#
# Input: tm1 - tm5, tm7 (not tm6)
#
# written by Markus Neteler 21.July 1998
#            neteler geog.uni-hannover.de
# updated for GRASS 5.7 by Michael Barton 2004/04/05


#% Module
#%  description: Calculates Optimum-Index-Factor table for LANDSAT TM bands 1-5, & 7
#%  keywords: raster, imagery, statistics
#% End
#% option
#% key: image1
#% type: string
#% gisprompt: old,cell,raster
#% description: LANDSAT TM band 1.
#% required : yes
#% end
#% option
#% key: image2
#% type: string
#% gisprompt: old,cell,raster
#% description: LANDSAT TM band 2.
#% required : yes
#% end
#% option
#% key: image3
#% type: string
#% gisprompt: old,cell,raster
#% description: LANDSAT TM band 3.
#% required : yes
#% end
#% option
#% key: image4
#% type: string
#% gisprompt: old,cell,raster
#% description: LANDSAT TM band 4.
#% required : yes
#% end
#% option
#% key: image5
#% type: string
#% gisprompt: old,cell,raster
#% description: LANDSAT TM band 5.
#% required : yes
#% end
#% option
#% key: image7
#% type: string
#% gisprompt: old,cell,raster
#% description: LANDSAT TM band 7.
#% required : yes
#% end
#% Flag
#% key: g
#% description: Print in shell script style
#% End

if  [ -z "$GISBASE" ]
then
	echo "You must be in GRASS GIS to run this program" >&2
	exit 1
fi

if   [ "$1" != "@ARGS_PARSED@" ]
then
	exec g.parser "$0" "$@"
fi

eval `g.gisenv`
: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"

# Open tempfiles
temp_stddev="`g.tempfile $$`"
export temp_stddev
temp_correlation="`g.tempfile $$`"
export temp_correlation
temp_sum="`g.tempfile $$`"
export temp_sum
temp_calc="`g.tempfile $$`"
export temp_calc
temp_result="`g.tempfile $$`"
temp_result2="`g.tempfile $$`"
temp_bands="`g.tempfile $$`"

# save the Stddev for TM bands
g.message "Calculating Standard deviations for all bands:"
g.message "band 1"
r.univar -g "$GIS_OPT_IMAGE1" | grep stddev | cut -f2 -d= > "$temp_stddev"
g.message "band 2"
r.univar -g "$GIS_OPT_IMAGE2" | grep stddev | cut -f2 -d= >> "$temp_stddev"
g.message "band 3"
r.univar -g "$GIS_OPT_IMAGE3" | grep stddev | cut -f2 -d= >> "$temp_stddev"
g.message "band 4"
r.univar -g "$GIS_OPT_IMAGE4" | grep stddev | cut -f2 -d= >> "$temp_stddev"
g.message "band 5"
r.univar -g "$GIS_OPT_IMAGE5" | grep stddev | cut -f2 -d= >> "$temp_stddev"
g.message "band 7"
r.univar -g "$GIS_OPT_IMAGE7" | grep stddev | cut -f2 -d= >> "$temp_stddev"

g.message "Calculating Correlation Matrix"
r.covar -r map=$GIS_OPT_IMAGE1,$GIS_OPT_IMAGE2,$GIS_OPT_IMAGE3,$GIS_OPT_IMAGE4,$GIS_OPT_IMAGE5,$GIS_OPT_IMAGE7 |tail -6 > "$temp_correlation"

# Calculate all combinations
g.message "Calculating OIF for the 20 band combinations..."
g.message "Calculate OIF for combination 123"
echo "123: "> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 2 3 >  "$temp_result"

g.message "Calculate OIF for combination 124"
echo "124: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 2 4 >> "$temp_result"

g.message "Calculate OIF for combination 125"
echo "125: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 2 5 >> "$temp_result"

g.message "Calculate OIF for combination 127"   
echo "127: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 2 7 >> "$temp_result"

g.message "Calculate OIF for combination 134"   
echo "134: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 3 4 >> "$temp_result"

g.message "Calculate OIF for combination 135"   
echo "135: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 3 5 >> "$temp_result"

g.message "Calculate OIF for combination 137"   
echo "137: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 3 7 >> "$temp_result"

g.message "Calculate OIF for combination 145"   
echo "145: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 4 5 >> "$temp_result"

g.message "Calculate OIF for combination 147"   
echo "147: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 4 7 >> "$temp_result"

g.message "Calculate OIF for combination 157"   
echo "157: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 1 5 7 >> "$temp_result"

g.message "Calculate OIF for combination 234"   
echo "234: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 2 3 4 >> "$temp_result"

g.message "Calculate OIF for combination 235"   
echo "235: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 2 3 5 >> "$temp_result"

g.message "Calculate OIF for combination 237"   
echo "237: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 2 3 7 >> "$temp_result"

g.message "Calculate OIF for combination 245"   
echo "245: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 2 4 5 >> "$temp_result"

g.message "Calculate OIF for combination 247"   
echo "247: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 2 4 7 >> "$temp_result"

g.message "Calculate OIF for combination 257"   
echo "257: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 2 5 7 >> "$temp_result"

g.message "Calculate OIF for combination 345"   
echo "345: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 3 4 5 >> "$temp_result"

g.message "Calculate OIF for combination 347"   
echo "347: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 3 4 7 >> "$temp_result"

g.message "Calculate OIF for combination 357"   
echo "357: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 3 5 7 >> "$temp_result"

g.message "Calculate OIF for combination 457"   
echo "457: " >> "$temp_bands"
"$GISBASE"/etc/i.oif/i.oifcalc 4 5 7 >> "$temp_result"

paste -d' ' "$temp_bands" "$temp_result" > "$temp_result2"
g.message "Ready."
g.message "The Optimum Index Factor analysis result:"
g.message "    (Best combination comes first.)"

if [ $GIS_FLAG_G -eq 1 ] ; then
   sort -k 2 -n -r "$temp_result2" | tee ./i.oif.result | sed 's+:  +:+g'
else
   sort -k 2 -n -r "$temp_result2" | tee ./i.oif.result
fi

rm -f "$temp_stddev"
rm -f "$temp_correlation"
rm -f "$temp_sum"
rm -f "$temp_calc"
rm -f "$temp_result"
rm -f "$temp_result2"
rm -f "$temp_bands"
