#!/bin/bash
####################################################################
#                  PLQDQ                                           #
# This shell script plots <q> and <dq>                             #
# 03/01 CC                                                         #
####################################################################

#-----------------------------------------------------------------------
#  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=$tmp/qdq_${LOGNAME}_$$.pl
file2=$tmp/qdq_${LOGNAME}_$$.pl2
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 the expectation values <q> .'
usage='Usage:   plqdq [-h -i -s -l -G -v -a -n -x -y -z -p] [-P printer] state dof'
help1='-h    : print this help text.'
hlp1a='-i dir: Use dir/check as input file.'
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).'
help9=' "state" is an integer specifying the state'
help10='of which all the <q> will be plotted.'
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 ":hnslGv:a:x:y:z:pP:i:" opt; do
    case $opt in
    h  ) echo "$purp"
         echo "$usage"
         echo " $help1"
         echo " $hlp1a"
         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 ' '
	 echo " $help9 $help10"
	 echo "$minus40$minus40"
	 exit ;;
    s  ) sout=1 ;;
    G  ) G=1 ;;
    l  ) log=1 ;;
    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

shift $(($OPTIND - 1))

if [ -z "$1" ] ; then
    echo ' State = ?   (electronic state)'
    read state
else
    state=$1
fi
if [ -z "$2" ] ; then
    echo ' dof = ?   (0 = show all degrees of freedom simultaneously)'
    read qdof
else
    qdof=$2
fi

if [ -z "${lpr##-*}" ] || [ "$lpr" != "${lpr#[0-9]}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plqdq -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plqdq -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 [ ! -r $dirin/check ] ; then
   echo 'Sorry, there is no (readable) check file in the current directory!'
   exit
fi

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

if [ $sout -eq 0 ] ; then
  rdcheck$ver $io -gq $qdof -on no -oc no -oq $file $nounit $state 1
else
  rdcheck$ver $io -gq $qdof -on no -oc no -oq $file $nounit $state 1 \
  | fgrep -2 Illegal
fi


if [ -n "$ar" ] || [ -n "$xr" ]   || [ -n "$yr" ] || [ -n "$zr" ] \
                || [ -n "$log" ] || [ -n "$G" ]  ; then
  ar=${ar:=0}
  xr=${xr:=*}
  yr=${yr:=*}
  zr=${zr:=*}
  echo 'set xrange ['$ar':'$xr'] ; ' \
       '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 '3,7s/\[0/\['$zr'/' -e /'set nologscale'/d   $file  >> $file2
  /bin/rm -f $file
  cat $file2  > $file
  /bin/rm -f $file2
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

exit

















