#!/bin/bash
####################################################################
#                  PLNAT                                           #
# This shell script plots the natural populations using gnuplot.   #
# HDM 07/99        Last Change CC 03/01                            #
####################################################################

#-----------------------------------------------------------------------
#  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

gnupr=$tmp/gnu_print.${LOGNAME}_$$
file=$tmp/nat_${LOGNAME}_$$.pl
file2=$tmp/nat_${LOGNAME}_$$.pl2
dirin=./

lpr="no"
sout=0
G=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 natural populations for a given state and mode."
usage="Usage: plnat [ -i -s -v -a -x -y -z -n -r -p -P printer ] [ -h ] state mode."
help1='-h     : print this help text.'
hlp1a='-i dir : Use dir/check as input file.'
help2='-s     : suppress messages of rdcheck'$ver'.'
hlp2a='-G     : draw 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='-r     : re-normalise the nat pops by devision'
help7='through state-populations.'
help8='-p     : prompt for printing the plot at default printer lpr.'
help9='-P printer : specify alternative printer'
help10='(e.g. -P "| lpr -Pps2"  or  -P file).'
help11='"state" and "mode" are integers specifying the set of'
help12='natural populations to be plotted.'
help13='-v val : specifies the program version.'
help14='The default version (can be set by MCTDH_VERSION) is: '$ver


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

while getopts ":hv:a:x:y:z:GnsrpP:i:" opt; do
    case $opt in
    h  ) echo $purp
         echo $usage
         echo ' '"$help1"
         echo ' '"$hlp1a"
         echo ' '"$help6 $help7"
         echo ' '"$help2"
         echo ' '"$hlp2a"
         echo ' '"$hlp3a"
         echo ' '"$help3"
         echo ' '"$help4"
         echo ' '"$help5"
         echo ' '"$hlp5a"
         echo ' '"$help13"
         echo '          '"$help14"
         echo ' '"$help8"
         echo ' '"$help9 $help10"
         echo ' '
         echo ' '"$help11"
         echo ' '"$help12"
         echo ' '
         exit ;;
    a  ) ar=$OPTARG ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    v  ) ver=$OPTARG ;;
    G  ) G=1 ;;
    s  ) sout=1 ;;
    n  ) nounit="-n" ;;
    r  ) renorm="-r" ;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    \? ) echo ' Unkown option!'
         echo $usage
         echo ' -h provides a help text.'
         exit 1 ;;
    i  ) dirin=$OPTARG
         io=' -i '$dirin ;;
  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 $help6 $help7
  echo $help2
  echo $hlp2a
  echo $hlp3a
  echo $help3
  echo $help4
  echo $help5
  echo $hlp5a
  echo $help13
  echo '         '$help14
  echo $help8
  echo $help9 $help10
  echo $help11 $help12
  exit 2
fi

state="$1"
mode="$2"

if [ -z "$1" ] || [ -z "$2" ] ; then
    echo ' State = ?   (electronic state)'
    read state
    echo ' mode = ?   (mode (or particle) for which the natpops are to be shown)'
    read mode

fi
if [ -z "$state" ] || [ -z "$mode" ] || [ $state -le 0 ] || [ $mode -le 0 ] ; then
    echo ' Incorrect state and mode given!'
    echo ' state : '$state
    echo ' mode  : '$mode
    echo $usage
    exit 3
fi

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

#-----------------------------------------------------------------------
#  Call rdcheck84 and gnuplot.
#-----------------------------------------------------------------------
if [ $sout -eq 0 ] ; then
  rdcheck$ver $io -g -oc no -oq no -on $file $renorm $nounit $state $mode
else
  rdcheck$ver $io -g -oc no -oq no -on $file $renorm $nounit $state $mode \
  | fgrep -2 Illegal
fi

if [ -n "$ar" ] || [ -n "$xr" ] || [ -n "$yr" ] || [ -n "$zr" ] \
                || [ -n "$zr" ]; then
ar=${ar:=0}
xr=${xr:=*}
yr=${yr:=1.0}
zr=${zr:=1e-6}
echo 'set xrange ['$ar':'$xr']'     >> $file2
sed -e '3,5s/\[1e-6/\['$zr'/' $file >> $file2
/bin/rm -f $file
sed -e '3,5s/1.0\]/'$yr'\]/' $file2  > $file
/bin/rm -f $file2
fi
if [ "$G" = "1" ] ; then
    echo 'set grid'                  > $file2
    cat $file >> $file2
    /bin/mv -f $file2  $file
fi

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






