#!/bin/bash

#-----------------------------------------------------------------------
#
#                                  PLEIGVAL
#
# Plots the eigenvalues computed in a diagonalisation run using Gnuplot.
#
# 2/00 MB
#
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Exit status:
#   0: o.k.
#   1: illegal options
#   2: file not existent or readable
#-----------------------------------------------------------------------
# set locale, so that date etc will be in uniform format.
export LC_ALL=C

#-----------------------------------------------------------------------
# Help Text
#-----------------------------------------------------------------------
purp="Purpose: Plot of the energies computed in a diagonalisation run."
usage="Usage: pleigval [-h] [-a emin] [-x emax] [-z imin] [-y imax]\n
\t\t [-l] [-G][-p] [-P printer] [file]"
options='Options:\n
-h:\t\t print this help text\n
-a emin:\t set the lower range of energy to emin\n
-x emax:\t set the upper range of energy to emax\n
-y ymax:\t set the upper range of intensity to ymax\n
-z ymin:\t set the lower range of intensity to ymin\n
-l:\t\t use a logarithmic scale for the intensities\n
-G:\t\t show grid lines\n
-p:\t\t prompt for printing the plot at default printer lpr\n
-P printer:\t specify printer (e.g. -P "| lpr -Pps2" or -P file.ps)\n
file:\t\t name of file containing the spectrum (default: eigval)\n'

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

#-----------------------------------------------------------------------
# Read and check arguments
#-----------------------------------------------------------------------
while getopts ":a:x:y:z:GhlpP:" option; do
  case $option in
    h ) echo
        echo -e $purp
        echo -e $usage
        echo -e $options
        exit 0 ;;
    a ) xmin=$OPTARG ;;
    x ) xmax=$OPTARG ;;
    z ) ymin=$OPTARG ;;
    y ) ymax=$OPTARG ;;
    l ) logscale=1 ;;
    G ) grid=1 ;;
    p ) lpr="| lpr" ;;
    P ) lpr=$OPTARG
        if [ -z "${lpr##-*}" ] ; then
     	  echo -e "\nERROR: no printer specified with -P option!"
	  $0 -h
	  exit 1
	fi ;;
    \?) echo -e "\nERROR: option $OPTARG unknown!"
	$0 -h
	exit 1 ;;
  esac
done

file=${!OPTIND}
if [ -z "$file" ] ; then file="eigval" ; fi
if [ ! -r $file ] ; then
  echo -e "\nERROR: file $file not existent or readable!\n"
  exit 2
fi

#-----------------------------------------------------------------------
# Define Gnuplot options
#-----------------------------------------------------------------------
dir=$(pwd)
dir1="${dir##$HOME/}"
if [ "$dir" != "$dir1" ] ; then  dir="~/$dir1" ; fi

echo -e 'set style data impulses'                 >  $gnuop
if [ $logscale -eq 1 ] ; then
    ymin=${ymin:="1e-12"}
    echo -e 'set logscale y'                      >> $gnuop
fi
if [ -n "$grid" ] ; then
    echo -e 'set grid'                            >> $gnuop
fi
echo -e 'set xlabel "Energy"'                     >> $gnuop
echo -e 'set ylabel "Intensity"'                  >> $gnuop
echo -e "set xrange [$xmin:$xmax]"                >> $gnuop
echo -e "set yrange [$ymin:$ymax]"                >> $gnuop
echo -e "set tics out"                            >> $gnuop
echo -e "set nokey"                               >> $gnuop
echo -e 'set title "'$dir'/'$file',   '$(date)'"' >> $gnuop
echo -e 'plot "'$file'" using 2:3'                >> $gnuop

#-----------------------------------------------------------------------
# Call Gnuplot
#-----------------------------------------------------------------------
gnuplot -geometry +0+0 -persist $gnuop
if [ ! -z "$lpr" ] ; then
  echo -e 'Print the plot to "'$lpr'" (y/n, return=no)? \c'
  read print
  if [ ${print:-n} = 'y' ]  ; then
     echo "set terminal postscript" >> $gnupr
     echo 'set output "'$lpr'"'     >> $gnupr
     gnuplot $gnupr $gnuop
  fi
fi

#-----------------------------------------------------------------------
# Remove garbage and exit
#-----------------------------------------------------------------------
/bin/rm -f $gnuop
/bin/rm -f $gnupr
exit 0








