#!/bin/bash
####################################################################
#                  PLSTATE                                         #
# This shell script plots the state populations using gnuplot.     #
# HDM 07/99                                                        #
####################################################################

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

gnupr=$tmp/gnu_print.${LOGNAME}_$$
file=$tmp/chk_${LOGNAME}_$$.pl
file2=$tmp/chk_${LOGNAME}_$$.pl2
datap=$tmp/chk_${LOGNAME}_$$.dat.pl
data=$tmp/chk_${LOGNAME}_$$.dat
dirin=./
lpr="no"
sout=0
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

if [ -n "$MCTDH_VERSION" ] ; then
   ver="$MCTDH_VERSION"
else
   ver=84
fi

purp='Purpose: Plot of the electronic state populations.'
usage='Usage:   plstate [ -l -G -f -i -s -v -a -x -y -z -n -p ] [ -P printer ] [ -h ]'
help1='-h     : print this help text.'
hlp1a='-f path: use "path" as input file rather than "./check".'
hlp1b='-i path: use "path/check" as input file rather than "./check".'
help2='-s     : suppress messages of rdcheck'$ver'.'
hlp2a='-l     : use logarithmic scale.'
hlp2b='-G     : set grid lines.'
hlp3a='-a val : set lower range of x to val.'
help3='-x val : set upper range of x to val.'
help4='-y val : set upper range of y to val.'
help5='-z val : set lower range of y to val.'
hlp5a="-n     : No units. Sets fs=1. To be used if 'time-not-fs' is set."
help6='-p     : prompt for printing the plot at default printer lpr.'
help7='-P printer : specify alternative printer'
help8=' (e.g. -P "| lpr -Pps2"  or  -P file).'
help13='-v val : specifies the program version.'
help14='The default version (can be set by MCTDH_VERSION) is: '$ver
minus40='----------------------------------------'
unset log
unset G

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

while getopts ":hslnGf:v:a:x:y:z:pP:i:" opt; do
    case $opt in
    h  ) echo "$purp"
         echo "$usage"
         echo " $help1"
         echo " $hlp1a"
         echo " $hlp1b"
         echo " $help2"
         echo " $hlp2a"
         echo " $hlp2b"
         echo " $help13"
         echo "         $help14"
         echo " $hlp3a"
         echo " $help3"
         echo " $help4"
         echo " $help5"
         echo " $hlp5a"
         echo " $help6"
         echo " $help7 $help8"
         echo "$minus40$minus40"
         exit ;;
    s  ) sout=1 ;;
    G  ) G=1 ;;
    l  ) log=1 ;;
    f  ) path=$OPTARG
         if [ "$path" != "${path%/}" ] ; then path=$path/check ; fi
         io=' -f '$path ;;
    v  ) ver=$OPTARG ;;
    a  ) ar=$OPTARG ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    n  ) nounit='-n' ;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    \? ) echo ' Unkown option!'
         echo ' -h provides a help text.'
         echo $usage
         exit 1 ;;
    i  ) dirin=$OPTARG
         io=' -i '$dirin ;;
  esac
done

if [ -z "${lpr##-*}" ] || [ "$lpr" != "${lpr#[0-9]}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plstate -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plstate -P prfile  :  writes postscript-output '
  echo '                                   to the file "prfile".'
  echo
  echo $usage
  echo ' '$help1
  echo ' '$help2
  echo ' '$hlp2a
  echo ' '$hlp2b
  echo ' '$help3
  echo ' '$help4
  echo ' '$help5
  echo ' '$hlp5a
  echo ' '$help6
  echo ' '$help7
  exit 2
fi

if [ -n "$path" ] ; then
   if [ ! -r $path ] || [ ! -f $path ] ; then
     echo 'Sorry, there is no (readable) check file: '$path
     exit
   fi
else
   if [ ! -r $dirin/check ] ; then
      echo 'Sorry, there is no (readable) check file in the directory: '$dirin
      exit
   fi
fi

#-----------------------------------------------------------------------
#  Call rdcheck and change the GNUplot command lines.
#-----------------------------------------------------------------------

if [ $sout -eq 0 ] ; then
  rdcheck$ver $io $nounit -g -oq no -oc $datap 1 0
else
  rdcheck$ver $io $nounit -g -oq no -oc $datap 1 0   > /dev/null
fi

sed -e '/#--#--#/,$ d' $datap > $file

if [ -n "$ar" ] || [ -n "$xr" ]  || [ -n "$yr" ] || [ -n "$zr" ] \
                || [ -n "$log" ] || [ -n "$G" ]  ; then

  ar=${ar:=0}
  xr=${xr:=*}
  yr=${yr:=1.0}
  zr=${zr:=1e-4}
  echo 'set xrange ['$ar':'$xr']'  >  $file2
  echo 'set yrange ['$zr':'$yr']'  >> $file2
  if [  -n "$G" ] ; then echo 'set grid' >> $file2 ; fi
  if [  -n "$log" ] ; then
      echo 'set   logscale y'     >> $file2
  else
      echo 'set nologscale y'     >> $file2
  fi
  sed -e '/set nologscale/ d' $file > $data
  sed -e '/set yrange/ d'     $data > $file
  cat $file >> $file2
  cat $file2 > $file
fi

/bin/rm -f $file2
sed -e '1,/#--#--#/ d' $datap > $data
cat $data > $datap
/bin/rm -f $data

#-----------------------------------------------------------------------
#  Check, if there is more than one state. If not, write a message.
#-----------------------------------------------------------------------
onestate=`grep 'us 1: 3' "$file"`
if [ -z "$onestate" ] ; then
   echo "There is only one multi-set electronic state!"
   echo "Only the norm-squared of the WF is thus plotted."
   echo "If this is a multi-state single-set run use"
   echo "showd1d84 -a -E f<el.DOF> to plot the state populations."
fi

#-----------------------------------------------------------------------
#  Call gnuplot command and write print command lines.
#-----------------------------------------------------------------------
gnuplot -geometry +0+0 -persist "$file"  2> /dev/null

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"  "$file" 2> /dev/null
     /bin/rm -f $gnupr
  fi
fi
/bin/rm -f $file
/bin/rm -f $datap
exit

