#!/bin/bash
#######################################################################
#                          PLAUTO                                     #
# This shell script plots the autocorrelation function using gnuplot. #
# HDM 07/99                                                           #
#######################################################################

#-----------------------------------------------------------------------
#  Definitions :
#-----------------------------------------------------------------------
if [ -d /tmpa ] && [ -w /tmpa ] ; then
   tmp="/tmpa"
elif [ -d /work ] && [ -w /work ] ; then
   tmp="/work"
elif [ -d /tmp ] && [ -w /tmp ] ; then
   tmp="/tmp"
elif [ -d "$HOME" ] && [ -w "$HOME" ] ; then
   tmp="$HOME"
else
   tmp="."
fi

gnuop=$tmp/gnu_options.${LOGNAME}_$$
gnupr=$tmp/gnu_print.${LOGNAME}_$$
file="auto"
dir=`pwd`
lpr="no"
sout=0
grid=0
logscale=0
reim=0
ar=0
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

purp="Purpose: Plot of the absolut value of the autocorrelation function."
usage='Usage: plauto [-f -a val -x val -y val -z val]'
usag1='[-l -G -r] [-p|-P printer] [-h].'
help1='-h     : print this help text.'
hlp1b='-f path: use "path" as input file rather than "./auto".'
hlp1a='-a val : set lower range of x to val.'
help2='-x val : set upper range of x to val.'
help3='-y val : set upper range of y to val.'
hlp3a='-z val : set lower range of y to val.'
help4='-l     : use logarithmic y-scale.'
help5='-G     : draw grid lines.'
help6='-r     : plot real and imaginary parts rather than abs.'
hlp6a='-n val : multi-packet run. E.g.: "plauto -n 2x3"'
hlp6b='         Take the jxk-th entry of the cross-corr-matrix.'
hlp6c='         Generates the file ./auto_2x3 (for this example).'
help7='-p     : prompt for printing the plot at default printer lpr.'
help8='-P printer : specify alternative printer'
help9=' (e.g. -P "| lpr -Pps2"  or  -P file).'

#-----------------------------------------------------------------------
#  Read the options (if any).
#-----------------------------------------------------------------------

while getopts ":hlrG12f:a:x:y:z:n:pP:" opt; do
    case $opt in
    h  ) echo "$purp"
         echo "$usage $usag1"
         echo " $help1"
	 echo " $hlp1b"
	 echo " $hlp1a"
	 echo " $help2"
         echo " $help3"
         echo " $hlp3a"
         echo " $help4"
         echo " $help5"
         echo " $help6"
         echo " $hlp6a"
         echo " $hlp6b"
         echo " $hlp6c"
         echo " $help7"
         echo " $help8 $help9"
         exit ;;
    f  ) file=$OPTARG
	 if [ "$file" != "${file%/}" ] ; then file=$file/auto ; fi ;;
    1  ) au=1 ;;
    2  ) au=2 ;;
    a  ) ar=$OPTARG ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    l  ) logscale=1 ;;
    G  ) grid=1 ;;
    r  ) reim=1 ;;
    n  ) nxn=$OPTARG
         file2=./auto_$nxn;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    \? ) echo ' Unkown option!'
         echo $usage
         echo ' -h provides a help text.'
         exit 1 ;;
  esac
done
shift $(($OPTIND - 1))

if [ -z "${lpr##-*}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plauto -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plauto -P prfile  :  writes postscript-output '\
                   'to the file "prfile".'
  echo
  echo "$usage $usag1"
  echo " $help1"
  echo " $hlp1b"
  echo " $hlp1a"
  echo " $help2"
  echo " $help3"
  echo " $hlp3a"
  echo " $help4"
  echo " $help5"
  echo " $help6"
  echo " $help7"
  echo " $help8 $help9"
  exit 2
fi

if [ -n "${file%%/*}" ] ; then
    file=$dir/$file
fi
cd `dirname $file`
file="`pwd`/`basename $file`"

file=${file}$au

if [ ! -r "$file" ] || [ ! -f "$file" ] ; then
  echo ' The file "'$file'" does not exist or is not a readable regular file!'
  exit 2
fi


if [ -z "$zr" ] ; then
    if [ $logscale -eq 1 ] ; then
	zr='1.e-7'
	reim=0
    else
	if [ $reim -eq 1 ] ; then
	    zr='-0.6'
        else
            zr='0'
        fi
    fi
fi

#-----------------------------------------------------------------------
#  If option -n is set (multipackets), re-write auto-file.
#-----------------------------------------------------------------------
if [ -n "$nxn" ] ; then
   i=0
   while [ -n "$i" ] && read LINE
     do
       if [ -n "${LINE##\#*}" ] ; then
          i=$(($i+1))
          if [ `echo $LINE | cut -c 1-9` != "0.0000000" ] ; then
              packq=$((i-1))
              unset i
          fi
       fi
     done < $file

   packets=`echo "sqrt($packq)" | bc`
   echo "There are $packets Packets"
   n1=`echo $nxn | cut -dx -f1 `
   n2=`echo $nxn | cut -dx -f2 `

   if [ $n1 -lt 1 ] || [ $n1 -gt $packets ] || [ $n2 -lt 1 ] || \
                                    [ $n2 -gt $packets ] ; then
      echo "Invalid value given for option -n"
      echo "Both n1 = $n1 and n2 = $n2 must be larger 0 and not"\
           "larger packets = $packets"
      exit
   fi

   n=$(($n1+$n2*$packets-$packets))
   i=0
   while read LINE
     do
       if [ -z "${LINE##\#*}" ] ; then
          echo "$LINE" > $file2
       else
          i=$(($i+1))
       fi
       if [ $i -eq $n ] ; then
         echo "$LINE" >> $file2
         i=$(($i-$packq))
       fi
     done < $file
else
   file2=$file
fi

#-----------------------------------------------------------------------
#  Write GNU options file.
#-----------------------------------------------------------------------
echo 'set title "Autocorrelation,   '$file $nxn'"' > $gnuop
echo 'set style data lines'     >> $gnuop
echo 'set xrange ['$ar':'$xr']' >> $gnuop
echo 'set xlabel "time[fs]"'    >> $gnuop
echo 'set yrange ['$zr':'$yr']' >> $gnuop

if [ $logscale -eq 1 ] ; then
  echo 'set logscale y'    >> $gnuop
else
  echo 'set nologscale'    >> $gnuop
fi

if [ $grid -eq 1 ] ; then
  echo 'set grid'          >> $gnuop
else
  echo 'set nogrid'        >> $gnuop
fi

if [ $reim -eq 1 ] ; then
  echo 'set xzeroaxis'     >> $gnuop
  echo 'plot "'$file2'" us 1:2 notit, "" us 1:3 notit' >> $gnuop
else
  echo 'plot "'$file2'" using 1:4 notitle' >> $gnuop
fi

#-----------------------------------------------------------------------
#  Call gnuplot.
#-----------------------------------------------------------------------

gnuplot -geometry +0+0 -persist "$gnuop"

if [ "$lpr" != "no" ] ; then
  echo ' Print the plot to "'$lpr'" ?   (y/n; return=no) '
  read print
  if [ ${print:-n} = 'y' ]  ; then
     echo 'set terminal postscript' >> $gnupr
     echo 'set output "'$lpr'" '    >> $gnupr
     gnuplot "$gnupr" "$gnuop"
     /bin/rm -f $gnupr
  fi
fi
/bin/rm -f $gnuop
exit












