#!/bin/sh
version=1.4.5
license="Copyright (C) 1997, 2001, 2007, 2009 Dimitar Ivanov

License: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."
#set -vx
################################################################################
#
# lqfit - gnuplot-wrapper for 1-d linear and quadratic fitting
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
#
progname=`basename $0`
name_ver="$progname $version:"
xc=1                                 # x-data column
yc=2                                 # y-data column
FIT_LOG=/tmp/$progname.$$.fit.log    # Env variable used by Gnuplot to define
export FIT_LOG                       # the name of the fitting logfile

[ x$1 = x--help ] && set -- "-h" && name_ver=""
[ x$1 = x--version ] && set -- "-V"

gops="hVdr:x:y:q"
opts=`getopt $gops $*` || opts=-h
set -- $opts

while [ $# -gt 0 ]
do
   case $1 in
   -h)
      cat << HELP

$name_ver program for linear and quadratic fitting (requires gnuplot)

Usage: $progname [OPTION].. [FILE]
Options:
           -x <int>       x-data column ($xc is default). If 0, the x-column is
                          considered to be and index data
           -y <int>       y-data column ($yc is default)
           -r x_0:x_1     x-data range
           -q             quadratic fit (default is linear)
           -d             debug mode - print the fitting log from Gnuplot
           -V, --version  output version and copyright information
           -h, --help     this help

HELP
      exit 0
      ;;
  -V|--version)
     cat << !ver
$progname $version
$license
!ver
      exit 0
      ;;
  -r) xr="$2"
      shift 2
      ;;
  -x) xc=$2
      shift 2
      ;;
  -y) yc=$2
      shift 2
      ;;
  -q) qfit=yes
      shift
      ;;
  -d) debug=yes
      shift
      ;;
  -- ) shift
       break ;;
  esac
done

echo ""

cat $1 |awk -v xc="$xc" -v yc="$yc" -v xr="$xr" -v qfit="$qfit" \
'BEGIN \
{
  if( qfit == "yes" ) {
      print "f(x) = a0 + a1*x + a2*x**2"
      print "show fun"
      if( xr != "" ) printf("print \"Fitting range = [%s]\"\n", xr)
      printf("fit [x=%s] f(x) \"-\" using %d:%d via a0, a1, a2\n", xr, xc, yc)
  } else {
      print "f(x) = a0 + a1*x"
      print "show fun"
      if( xr != "" ) printf("print \"Fitting range = [%s]\"\n", xr)
      printf("fit [x=%s] f(x) \"-\" using %d:%d via a0, a1\n", xr, xc, yc)
  }
} 
{ print $0 }
END \
{ 
  print "e"
  if( qfit != "yes" ) {
      print "teta=atan(a1)"
      print "print \"slope (rad)     = \", teta"
      print "teta=teta/2/pi*360"
      print "print \"slope (deg)     = \", teta"
  }
}' |gnuplot 2>&1 \
   |sed -n "/===/,/matrix/p; /f(x)/p; /slope/p; /range/p;" \
   |egrep -v '===|matrix|^$'

echo ""

[ x$debug = xyes ] && cat $FIT_LOG

rm -f $FIT_LOG

exit 0
