#!/bin/sh

# Plot QM and MM Potential Energy Surfaces from 2 flat files with energies.
# Run without command-line arguments for usage information.

# Copyright (C) 2008 Kenno Vanommeslaeghe
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

unset id help
getopts i: f 2> /dev/null
if [ $? -eq 0 ] ; then
  if [ "$f" = "i" ] ; then
    id=$OPTARG
    shift 2
   else
    help=1
   fi
 fi
if [ $# -ne 2 -o "$help" ] ; then
  echo "Usage: `basename $0` [-i <id>] <qm energy file> <mm energy file>
Where <id> is an identifier to be integrated in the output file names."
  exit 1
 fi
qmf=${1%.*}.qne
bas=${2%.*}
mmf=$bas$id.mne
out=$bas$id.gpt
alias normalize="awk 'BEGIN{absm=32000}
  {r=\$1;rec[NR]=r;if (absm>r) absm=r}
  END{for(i=1;i<=NR;i++) print rec[i]-absm}'"
normalize $1 > $qmf
normalize $2 > $mmf
echo "#!/usr/bin/gnuplot
plot \"$qmf\" using 1 with lines t \"qm\" lw 2, \"$mmf\" using 1 with lines t \"mm\" lw 2
pause mouse" > $out
chmod 755 $out
./$out
