#!/bin/sh
#
############################################################################
#
# MODULE:	r.regression line
# AUTHOR(S):	Dr. Agustin Lobo - alobo@ija.csic.es
#               Updated to GRASS 5.7 by Michael Barton
# PURPOSE:	Calculates linear regression from two raster maps: y = a + b*x
# COPYRIGHT:	(C) 2004 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.
#
#############################################################################


#%Module
#%  description: Calculates linear regression from two raster maps: y = a + b*x
#%  keywords: raster, statistics
#%End
#%option
#% key: map1
#% type: string
#% gisprompt: old,cell,raster
#% description: Map for x coefficient
#% required : yes
#%end
#%option
#% key: map2
#% type: string
#% gisprompt: old,cell,raster
#% description: Map for y coefficient
#% required : yes
#%end
#%option
#% key: output
#% type: string
#% gisprompt: new_file,file,output
#% description: ASCII file for storing regression coefficients (output to screen if file not specified).
#% required : no
#%end

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

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

PROG=`basename $0`

#### check if we have awk
if [ ! -x "`which awk`" ] ; then
    echo "$PROG: awk required, please install awk or gawk first" 2>&1
    exit 1
fi

# setting environment, so that awk works properly in all languages
unset LC_ALL
LC_NUMERIC=C
export LC_NUMERIC

TMP="`g.tempfile pid=$$`"
if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
    echo "ERROR: unable to create temporary files" 1>&2
    exit 1
fi


#calculate regression equation
r.stats -c input=$GIS_OPT_MAP1,$GIS_OPT_MAP2  > "$TMP"
awk '{tot += $3;sumX +=$1 * $3; sumsqX +=$1*$1*$3;sumY +=$2 * $3; sumsqY +=$2*$2*$3;\
 sumXY +=$1*$2*$3;\
}\
END {B=(sumXY - sumX*sumY/tot)/(sumsqX - sumX*sumX/tot);\
R= (sumXY - sumX*sumY/tot)/((sumsqX - sumX^2/tot)*(sumsqY - sumY^2/tot))^0.5;\
mediaX=sumX/tot;sumsqX=sumsqX/tot;varX=sumsqX-(mediaX^2);sdX=varX^0.5;\
mediaY=sumY/tot;sumsqY=sumsqY/tot;varY=sumsqY-(mediaY^2);sdY=varY^0.5;\
A=mediaY - B*mediaX; F= R^2/(1-R^2/tot-2);\
print A, B, R, tot, F, mediaX, sdX, mediaY, sdY}' "$TMP" > "$TMP"b
#cat "$TMP"b

#send output to screen or text file
if [ -z "$GIS_OPT_OUTPUT" ] 
then
	RESULTADO=`cat "$TMP"b`
	echo "y = a + b*x"
        echo "   a: offset"
        echo "   b: gain"
        echo "   R: sumXY - sumX*sumY/tot"
        echo "   N: number of elements"
        echo "   medX, medY: Medians"
        echo "   sdX, sdY: Standard deviations"
        echo "a  b  R  N  F medX  sdX  medY  sdY"
        echo ${RESULTADO}
else
        echo "y = a + b*x"
        echo "   a: offset"
        echo "   b: gain"
        echo "   R: sumXY - sumX*sumY/tot"
        echo "   N: number of elements"
        echo "   medX, medY: Medians"
        echo "   sdX, sdY: Standard deviations"
	echo "a  b  R  N  F medX  sdX  medY  sdY" > "$TMP"c
	cat "$TMP"b >> "$TMP"c
	mv "$TMP"c "$GIS_OPT_OUTPUT"
	cat "$GIS_OPT_OUTPUT"
fi
rm -f "$TMP" "$TMP"b "$TMP"c
