#!/bin/bash
####################################################################
#                  PLSPEC                                          #
# This shell script plots the spectrum using gnuplot.              #
# HDM 07/99                                                        #
####################################################################

#-----------------------------------------------------------------------
#  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/spec_${LOGNAME}_$$.pl
dirin=./
lpr="no"
sout=0
G=0
go=' -g 1 '
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

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

minus40='----------------------------------------'
purp="Purpose: Plot of the spectrum obtained from a fourier transform"
purp2="of the autocorrelation function."
usage="Usage: plspec [-s -p -v -G -i -f -y -z -e -t -r -c -n -N -q -FT]"
usag1=" [-p|-P] [-h] args"
help1='-h     : print this help text.'
hlp1a='-f path: use "path" as input file rather than "./auto".'
hlp1b='-i dir : use "dir" as input directory rather than "./".'
help2='-s     : suppress messages of autospec'$ver'.'
help3='-p     : prompt for printing the plot at default printer lpr.'
help4='-P printer : specify alternative printer (e.g. -P "| lpr -Pps2").'
help5='-G     : set grid lines.'
help6='-y val : set upper range of y to val.'
help7='-z val : set lower range of y to val.'
help8='     -- The following options are passed to autospec'$ver' --'
help9='-e R Unit : the offset energy is set to R in units Unit; e.g. 0.3 ev.'
hlp9a='-r     : Plot the resolution rather than the spectrum.'
hlp9b='-c  ph : Use phase correction exp(i*ph*1.d-6*t^2).'
hlp9c='-S     : Use sine-transformation (for ctrace files).'
hlp10='-n     : The maxima of the three spectra are normalised to 1.'
hp10a='-N     : No transformation to fs. Use when 'time-not-fs' was set in mctdh.'
hlp11='-q I   : The no. of plot-points is I rather than  500.'
hlp12='-g ncos: ncos is the exponent of the cosine damping function.'
hlp13='         ncos = 0,1,2,3,4,5 ; "-g 1" is default.'
hp13a='         ncos=3 sets a linear filter, see -lin in autospec84 -h.'
hlp14='-t tcut: The autocorrelation function is read only up to t=tcut.'
hl14a='-EP    : Multiplication with the energy prefactor.'
hlp15='-FT    : No multiplication with the energy prefactor.'
hlp16='     Fourier transform only. (This is now the default!)'
hl16a='-Mb norm: Convert to Mb. The norm ||D*Psi|| must be given (in au).'
hl16b='          D is the dipole moment. -Mb sets -EP.'
hlp17='args are the autospec'$ver' arguments: E1, E2, unit, tau, iexp.'
hl17a='The two last arguments may be dropped, they are then set to 1.E+40, 1,'
hl17b='i.e. there is then no exponential or gaussian damping.'
hlp18="Example: plspec -sp  -g 2 -e 5 ev -n  10. 15.3 ev 100 1 ."
hlp19='(use the command "autospec'$ver' -h" for explanation of the arguments)'
hl19a='(use "no" as unit when "time-not-fs" and "energy-not-ev" was set in mctdh)'
hlp30='When using the options -EP or -Mb one must make sure that the zero point'
hlp31='of the energy is set appropriately. Use the option -e if the total'
hlp32='energy differs from photon energy.'
hlp20=' '
hlp21='Simply type "plspec" and you will be prompted for the input.'
hlp22='-v val : sets the program version to val.'
hlp23='The default version (can be set by MCTDH_VERSION) is: '$ver

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

while getopts ":hnF:E:M:Gf:t:c:g:e:q:v:y:z:rsNSpP:i:" opt; do
    case $opt in
    h  ) echo $minus40$minus40
         echo $purp
         echo '         '$purp2
         echo $usage$usag1
         echo ' '$help1
         echo ' '$hlp1a
         echo ' '$hlp1b
         echo ' '$help2
         echo ' '$help3
         echo ' '$help4
         echo ' '$help5
         echo ' '$help6
         echo ' '$help7
         echo ' '$help8
         echo ' '$help9
         echo ' '$hlp9a
         echo ' '$hlp9b
         echo ' '$hlp9c
         echo ' '$hlp10
         echo ' '$hp10a
         echo ' '$hlp11
         echo ' '$hlp12
         echo '         '$hlp13
         echo '         '$hp13a
         echo ' '$hlp14
         echo ' '$hl14a
         echo ' '$hlp15
         echo "  $hlp16"
         echo ' '$hl16a
         echo "  $hl16b"
         echo ' '$hlp22
         echo ' '$hlp23
         echo ' '
         echo ' '$hlp17
         echo ' '$hl17a
         echo ' '$hl17b
         echo ' '$hlp18
         echo ' '$hlp19
         echo ' '$hl19a
         echo ' '
         echo ' '$hlp30
         echo ' '$hlp31
         echo ' '$hlp32
         echo ' '$hlp20
         echo ' '$hlp21
         echo $minus40$minus40
         exit ;;
    G  ) G=1 ;;
    F  ) if [ $OPTARG = T ] ; then
            FT=' -FT '
         else
            echo "plspec: Unknown option : -F$OPTARG"
            exit
         fi ;;
    E  ) if [ $OPTARG = P ] ; then
            FT=' -EP '
         else
            echo "plspec: Unknown option : -F$OPTARG"
            exit
         fi ;;
    M  ) if [ $OPTARG = b ] ; then
            FT=' -Mb '
         else
            echo "plspec: Unknown option : -M$OPTARG"
            exit
         fi
         shift $(($OPTIND - 1))
         FT=$FT' '$1
         shift 1
         OPTIND=1  ;;
    f  ) path=$OPTARG
         if [ "$path" != "${path%/}" ] ; then path=${path}auto ; fi
         io=' -f '$path ;;
    r  ) resol=' -r' ;;
    c  ) ph=' -ph '$OPTARG ;;
    s  ) sout=1 ;;
    t  ) to=' -t '$OPTARG ;;
    e  ) eo=' -e '$OPTARG
         shift $(($OPTIND - 1))
         eo=$eo' '$1
         shift 1
         OPTIND=1  ;;
    n  ) no=' -n ' ;;
    N  ) nofs=' -nofs ' ;;
    S  ) ctr=' -ctr ' ;;
    g  ) go=' -g '$OPTARG ;;
    q  ) po=' -p '$OPTARG ;;
    v  ) ver=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    \? ) shift $(($OPTIND - 1))
	 if [ -n "${1##-[0-9\.]*}" ] ; then
	    echo ' plspec: Unkown option! '$1
	    echo ' -h provides a help text.'
	    echo $usage$usag1
	    exit 1
         else
#-----------this is for negative energies
            arg=$@
            OPTIND=6
         fi ;;
    i  ) dirin=$OPTARG
         io=' -i '$dirin ;;
  esac
done

if [ -z "$arg" ] ; then
     shift $(($OPTIND - 1))
     arg=$@
fi

num=`echo $arg | wc -w `
if [ $num -eq 3 ] ; then arg=$arg' 0' ; fi

if [ -z "${lpr##-*}" ] || [ "$lpr" != "${lpr#[0-9]}" ] ; then
  echo $minus40$minus40
  echo ' No printer specified with the -P option!'
  echo ' Example :  plspec -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plspec -P prfile  :  writes postscript-output '
  echo '                             to the file "prfile".'
  echo
  echo $usage$usag1

  echo ' '$help1
  echo ' '$help2
  echo ' '$help3
  echo ' '$help4
  echo ' '$help5
  echo ' '$help6
  echo ' '$help7
  echo ' '$help8
  echo ' '$help9
  echo ' '$hlp9a
  echo ' '$hlp9b
  echo ' '$hlp9c
  echo ' '$hlp10
  echo ' '$hp10a
  echo ' '$hlp11
  echo ' '$hlp12
  echo '         '$hlp13
  echo '         '$hp13a
  echo ' '$hlp14
  echo ' '$hlp15$hlp16
  exit 2
fi

if [ -n "$path" ] ; then
  if [ ! -r $path ] ; then
   echo 'Sorry, the auto file '$path ' does not exist (or is not readable).'
  fi
else
  if [ ! -r $dirin/auto ] && [ -z $resol ] ; then
   echo 'Sorry, there is no (readable) auto file in the directory '$dirin
   exit
  fi
fi

echo 'set yrange ['$zr':'$yr']' > $gnuop
if [ $G -eq 1 ] ; then echo 'set grid' >> $gnuop ; fi

#-----------------------------------------------------------------------
#  Call autospec and gnuplot.
#-----------------------------------------------------------------------
if [ "$go" = " -g 3" ]; then
   go=" -g 0 -lin"
elif [ "$go" = " -g 4" ]; then
   go=" -g 1 -lin"
elif [ "$go" = " -g 5" ]; then
   go=" -g 2 -lin"
fi
echo ' args : '$nofs$io$go$to$no$ctr$po$eo$FT$ph$resol $arg
if [ $sout -eq 0 ] ; then
  autospec$ver -o $file $nofs$io$go$to$no$ctr$po$eo$FT$ph$resol $arg
else
  autospec$ver -o $file $nofs$io$go$to$no$ctr$po$eo$FT$ph$resol $arg > /dev/null
fi
gnuplot -geometry +0+0 -persist "$gnuop" "$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
     echo 'set yrange ['$zr':'$yr']' >> $gnupr
     if [ $G -eq 1 ] ; then echo 'set grid' >> $gnupr ; fi
     gnuplot "$gnupr"  "$file" 2> /dev/null
     /bin/rm -f $gnupr
  fi
fi
/bin/rm -f $file
/bin/rm -f $gnuop
exit














