#!/bin/bash
####################################################################
#                  PLPIT                                           #
# This shell script plots the potfit error measures using gnuplot. #
# HDM 03/00                                                        #
####################################################################

#-----------------------------------------------------------------------
#  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}_$$

lpr="no"
G=0
milli=0
logy=0
ar='*'
xr='*'
yr='*'
zr='*'
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

purp="Purpose: Plot of the potfit error measures (file iteration)."
usage="Usage: plpit [ -m -a -x -y -z -G -p -P printer ] [ -h ] task(s)."
help1='-h    : print this help text.'
help2='-G    : draw grid lines.'
hlp2a='-m    : Energy unit is meV rather than eV.'
hlp2b='-c    : Energy unit is cm-1 rather than eV.'
hlp2c='-l    : use logarithmic y-scale.'
help3='-a val: set lower range of x to val.'
help4='-x val: set upper range of x to val.'
help5='-y val: set upper range of y to val.'
help6='-z val: set lower range of y to val.'
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).'
hlp10='"task(s)" specifies the error measures to be plotted.'
hlp11='Tasks = wr ur ua mr ma f1 f2 f3 f4 .... (wr = wighted relevant)'
hlp12='ur = unwighted relevant, ua = unwighted all, mr = max relevant'
hlp13='ma = max all, fx = normalized (tr-sum) for dof x (x=1,2,..)'
minus40='----------------------------------------'


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

while getopts ":hlmcv:a:x:y:z:GpP:" opt; do
    case $opt in
    h  ) echo "$minus40$minus40"
         echo "$purp"
         echo "$usage"
         echo " $help1"
         echo " $help2"
         echo " $hlp2a"
         echo " $hlp2b"
         echo " $hlp2c"
         echo " $help3"
         echo " $help4"
         echo " $help5"
         echo " $help6"
         echo " $help7"
         echo " $help8 $help9"
         echo " $hlp10"
         echo " $hlp11"
         echo " $hlp12"
         echo " $hlp13"
         echo "$minus40$minus40"
         exit ;;
    a  ) ar=$OPTARG ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    v  ) ver=$OPTARG ;;
    l  ) logy=1 ;;
    m  ) milli=1 ;;
    c  ) milli=2 ;;
    G  ) G=1 ;;
    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##-*}" ] || [ "$lpr" != "${lpr#[0-9]}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plnat -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plnat -P prfile  :  writes postscript-output '\
                   'to the file "prfile".'
  echo
  echo $usage
  echo $help1
  echo $help2
  echo $hlp2a
  echo $hlp2b
  echo $hlp2c
  echo $help3
  echo $help4
  echo $help6
  echo $help7
  echo $help8 $help9
  echo $hlp10
  echo $hlp11
  echo $hlp12
  echo $hlp13
  exit 2
fi


if [ -z "$1" ]  ; then
  echo ' Input a blank separated list of tasks.'
  echo ' Possible tasks are :  wr (wighted relevant), ur (unwighted relevant)'
  echo ' ua (unwighted all), mr (max relevant), ma (max all)'
  echo ' f1, f2,..  (normalized (tr-sum) for dof 1, 2,..) '
  echo -n ' task(s) = ? '
  read tasklist
else
  tasklist=$*
fi

if [ ! -r iteration ] ; then
 echo 'Sorry, there is no (readable) iteration file in the current directory!'
 exit
fi

#-----------------------------------------------------------------------
#  Write gnuopt and call gnuplot.
#-----------------------------------------------------------------------

echo 'set style data lines'           >  $gnuop
echo 'set xrange ['$ar':'$xr']'       >> $gnuop
echo 'set yrange ['$zr':'$yr']'       >> $gnuop
echo 'set xlabel "Number of iterations"' >> $gnuop
if [ $milli -eq 0 ] ; then
    echo 'set ylabel "Error [eV]"'    >> $gnuop
elif [ $milli -eq 1 ] ; then
    echo 'set ylabel "Error [meV]"'   >> $gnuop
elif [ $milli -eq 2 ] ; then
    echo 'set ylabel "Error [cm-1]"'  >> $gnuop
fi
if [ "$G" = "1" ] ; then
    echo 'set grid'                   >> $gnuop
else
    echo 'set nogrid'                 >> $gnuop
fi
if [ $logy -eq 1 ] ; then
  echo 'set logscale y'               >> $gnuop
else
  echo 'set nologscale'               >> $gnuop
fi

pp='plot "iteration"'

if [ $milli -eq 0 ] ; then
    for task in $tasklist ; do
        if   [ "$task" = wr ] ; then
            echo -n $pp' us 1:2 tit "wr"' >> $gnuop
        elif [ "$task" = ur ] ; then
            echo -n $pp' us 1:3 tit "ur"' >> $gnuop
        elif [ "$task" = ua ] ; then
            echo -n $pp' us 1:4 tit "ua"' >> $gnuop
        elif [ "$task" = mr ] ; then
            echo -n $pp' us 1:5 tit "mr"' >> $gnuop
        elif [ "$task" = ma ] ; then
            echo -n $pp' us 1:6 tit "ma"' >> $gnuop
        elif [ "$task" = f1 ] ; then
            echo -n $pp' us 1:7 tit "f1"' >> $gnuop
        elif [ "$task" = f2 ] ; then
            echo -n $pp' us 1:8 tit "f2"' >> $gnuop
        elif [ "$task" = f3 ] ; then
            echo -n $pp' us 1:9 tit "f3"' >> $gnuop
        elif [ "$task" = f4 ] ; then
            echo -n $pp' us 1:10 tit "f4"' >> $gnuop
        elif [ "$task" = f5 ] ; then
            echo -n $pp' us 1:11 tit "f5"' >> $gnuop
    else
        echo ' ***  Unknown task ***  '$task
        exit 1
    fi
    pp=', ""'
    done
elif [ $milli -eq 1 ] ; then
    for task in $tasklist ; do
        if   [ "$task" = wr ] ; then
            echo -n $pp' us 1:($2*1000) tit "wr"' >> $gnuop
        elif [ "$task" = ur ] ; then
            echo -n $pp' us 1:($3*1000) tit "ur"' >> $gnuop
        elif [ "$task" = ua ] ; then
            echo -n $pp' us 1:($4*1000) tit "ua"' >> $gnuop
        elif [ "$task" = mr ] ; then
            echo -n $pp' us 1:($5*1000) tit "mr"' >> $gnuop
        elif [ "$task" = ma ] ; then
            echo -n $pp' us 1:($6*1000) tit "ma"' >> $gnuop
        elif [ "$task" = f1 ] ; then
            echo -n $pp' us 1:($7*1000) tit "f1"' >> $gnuop
        elif [ "$task" = f2 ] ; then
            echo -n $pp' us 1:($8*1000) tit "f2"' >> $gnuop
        elif [ "$task" = f3 ] ; then
            echo -n $pp' us 1:($9*1000) tit "f3"' >> $gnuop
        elif [ "$task" = f4 ] ; then
            echo -n $pp' us 1:($10*1000) tit "f4"' >> $gnuop
        elif [ "$task" = f5 ] ; then
            echo -n $pp' us 1:($11*1000) tit "f5"' >> $gnuop
        else
    echo ' ***  Unknown task ***  '$task
    exit 1
    fi
    pp=', ""'
    done
elif [ $milli -eq 2 ] ; then
    for task in $tasklist ; do
        if   [ "$task" = wr ] ; then
            echo -n $pp' us 1:($2*8065.54353) tit "wr"' >> $gnuop
        elif [ "$task" = ur ] ; then
            echo -n $pp' us 1:($3*8065.54353) tit "ur"' >> $gnuop
        elif [ "$task" = ua ] ; then
            echo -n $pp' us 1:($4*8065.54353) tit "ua"' >> $gnuop
        elif [ "$task" = mr ] ; then
            echo -n $pp' us 1:($5*8065.54353) tit "mr"' >> $gnuop
        elif [ "$task" = ma ] ; then
            echo -n $pp' us 1:($6*8065.54353) tit "ma"' >> $gnuop
        elif [ "$task" = f1 ] ; then
            echo -n $pp' us 1:($7*8065.54353) tit "f1"' >> $gnuop
        elif [ "$task" = f2 ] ; then
            echo -n $pp' us 1:($8*8065.54353) tit "f2"' >> $gnuop
        elif [ "$task" = f3 ] ; then
            echo -n $pp' us 1:($9*8065.54353) tit "f3"' >> $gnuop
        elif [ "$task" = f4 ] ; then
            echo -n $pp' us 1:($10*8065.54353) tit "f4"' >> $gnuop
        elif [ "$task" = f5 ] ; then
            echo -n $pp' us 1:($11*8065.54353) tit "f5"' >> $gnuop
        else
    echo ' ***  Unknown task ***  '$task
    exit 1
    fi
    pp=', ""'
    done
fi

echo ' '  >> $gnuop

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

