#!/bin/sh

# prepare gnuplot graph from "plotspec" output,
# i.e. plot the files graph.dat and impulses.dat
# (CD and UV spectrum from ADF computation)

# adjust this for the location of your gnuplot binary:
PL=/usr/bin/gnuplot

scosc=100  # default scaling of oscillator strengths
scrot=1    # default scaling of rotational strengths
shiftev=0  # default shift of excitation energies
lscale="set nologscale y"

# ----------------------
# parse the command line
# ----------------------
 
set -- ` getopt lr:o:s: $*`
if [ $? != 0 ]
then
        exit 2
fi
while [ $1 != -- ]
do    
    case $1 in           
        -r)
        scrot="${2}"
        shift;
        shift;
        ;;
        -l)
        lscale="set logscale y"
	shift;
        ;;
	-o)
        scosc="${2}"
        shift;
        shift;
        ;;     
	-s)
        shiftev="${2}"
        shift;
        shift;
        ;;    
	--)
	shift;
        break;;
     esac
done
shift                            

if test $scrot != "1"
then
  rotarg="(x $scrot)"
else
  rotarg=""
fi

if test $scosc != "1"
then
  oscarg="(x $scosc)"
else
  oscarg=""
fi

# ---------------------------
# check for filename argument
# ---------------------------

if test -z "$1"
then
echo " "
echo " output filename specification is missing ... aborting"
echo " "
echo " usage: graphit filespec"
echo " "
exit 1
else

# --------------------------------
# check if input files are present 
# --------------------------------

if test -r "graph.dat"
then

if test -r "impulses.dat"
then

#echo $scosc
#echo $scrot
#echo $shiftev

# -------------------------
# plot on energy scale (eV)
# -------------------------

$PL << eor
set xzeroaxis
set term postscript enhanced "Helvetica" 20
set xlabel "{/Symbol D}E / eV"
#set x2label "{/Symbol D}E / cm^{-1}"
set ylabel "{/Symbol De} / l / (mol cm)"
set y2label "Rotatory Strength ${rotarg}/ 10^{-40} cgs"
set nokey 
set title "$1"
set xrange [] writeback
set out "tmp.$$"
plot "graph.dat" using (\$1 + ($shiftev)):3   w l lt 1 lw 4
set out
#set xrange restore
#show xrange
set noautoscale x
set out "$1-graph-eV-cd.ps"
plot "graph.dat" using (\$1 + ($shiftev)):3   w l lt 1 lw 4 , \
  "impulses.dat" using (\$1 + ($shiftev)):(\$3*$scrot)  w imp lt 1 lw 2
set out
#
set ylabel "{/Symbol e} / 10^{-3} l / (mol cm)"
set y2label "Oscillator Strength $oscarg"
set out "$1-graph-eV-UV.ps"
set nokey
$lscale
set title "$1"
plot "graph.dat" using (\$1 + ($shiftev)):2   w l lt 1 lw 4 ,\
     "impulses.dat" using (\$1 + ($shiftev)):(\$2*$scosc)  w imp lt 1 lw 2
set out "$1-graph-eV-UV-log.ps"
set nokey
set ylabel "log {/Symbol e}"
set y2label ""
set nologscale y
set noxzeroaxis
set title "$1"
plot "graph.dat" using (\$1 + ($shiftev)):(log10(\$2*1000))   w l lt 1 lw 4 
set out                                           
eor

# -----------------------------
# plot on wavelength scale (nm)  
# -----------------------------

$PL << eor
set xzeroaxis
set term postscript enhanced "Helvetica" 20
set xlabel "{/Symbol l} / nm"
set ylabel "{/Symbol De} / l / (mol cm)"
set y2label "Rotatory Strength ${rotarg}/ 10^{-40} cgs"
set xrange [] writeback
set out "tmp.$$"
plot "graph.dat" using (1239.8557/(\$1 + ($shiftev))):3  w l lt 1 lw 4
set noautoscale x
#set xrange restore
set out "$1-graph-nm-cd.ps"
set title "$1"
set nokey  
plot "graph.dat" using (1239.8557/(\$1 + ($shiftev))):3  w l lt 1 lw 4 ,\
     "impulses.dat" using (1239.8557/(\$1 + ($shiftev))):(\$3*$scrot) \
                    w imp lt 1 lw 2
set out
set ylabel "{/Symbol e} / 10^{-3} l / (mol cm)"
set y2label "Oscillator Strength $oscarg"
set out "$1-graph-nm-UV.ps"
set title "$1"
set nokey
plot "graph.dat" using (1239.8557/(\$1 + ($shiftev))):2  w l lt 1 lw 4 ,\
     "impulses.dat" using (1239.8557/(\$1 + ($shiftev))):(\$2*$scosc)  \
                    w imp lt 1 lw 2
set out                                                       
q
eor

else # test -r impulses.dat
echo " "
echo " input file impulses.dat not found ... aborting"
echo " "
exit 2
fi # test -r impulses.dat

else # test -r graph.dat
echo " "
echo " input file graph.dat not found ... aborting"
echo " "
exit 3
fi # test -r graph.dat

fi # test -z $1
rm -f tmp.$$
exit 0


