#! /bin/csh

#    FMO result grabber
#       version 1.4
# written October 27, 2005
# last updated March 22, 2012
#   Dmitri G. Fedorov
#         AIST
# creates one file for all pair energies and NFG files for pair energies
# with fixed first monomer. 
# Required FMO version:
set reqver=4.2
# Arguments: GAMESS FMO output file
# Output: CSV files that can be directly read by either GNUplot or Excel.
# example:
# fmograbres FMOtuto.out
#

if($# != 1 || ! -e $1) then
  echo GAMESS output should be the only argument.
  exit
endif
set curver=`grep Version <$1 | awk '{ print $2 }'`
echo Grabbing the results from $1...
echo Required FMO version: $reqver or higher, your version: $curver.
set curver2=`echo $curver | cut -d. -f 1,2 | tr . " "`
set reqver2=`echo $reqver | cut -d. -f 1,2 | tr . " "`
set ver=`echo $reqver2 $curver2 | awk '{ print ($1 > $3 || ($1 == $3 && $2 > $4))?"old":"new" }'`
if( $ver == old ) then
  echo Your GAMESS output is not supported.
  exit 1
endif
set PIEDA=`grep "Pair interaction energy decomposition analysis" <$1`
if("a$PIEDA" != a) then
  echo This is a PIEDA output. Use piedares instead. Exiting... 
  exit 1
endif
set eij=$1:r.eij.csv
set ei_j=$1:r.eij
set tmp1=$1:r.tmp1
set tmp2=$1:r.tmp2
set nEIJ=`grep "I    J DL  Z" <$1 | wc -l` 
echo Detected $nEIJ sets of data, using the last one.
if($nEIJ == 0) then
  echo No data found. Exiting...
  exit 1
endif
set nst=`grep -n "I    J DL  Z" <$1 | tail -1 | cut -d: -f 1`
set nfi=`wc -l <$1 | cut -d: -f 1`
@ ncut = $nfi - $nst
tail -n $ncut <$1 >$tmp2
grep "^[0-9 ][0-9 ][0-9 ][0-9 ][0-9][0-9 ][0-9 ][0-9 ][0-9 ][0-9] [NCMSs][1-9]" <$tmp2 >$tmp1
set ncols=`head -1 <$tmp1 | wc -w`
set ifgcol=1
set jfgcol=2
set RIJcol=5
set QIJcol=6
set EIJcol=8
if($ncols > 11) set EIJcol=9
@ DIJcol = $ncols - 2
@ Gsol = $ncols - 1
set totcol=$ncols
echo Creating triangular pair interaction file... 
awk <$tmp1 -v ifgcol=$ifgcol -v jfgcol=$jfgcol -v RIJcol=$RIJcol -v QIJcol=$QIJcol -v EIJcol=$EIJcol -v DIJcol=$DIJcol -v Gsol=$Gsol -v totcol=$totcol '{ printf "%5d,%5d,%7.2f,%8.4f,%13.8f,%12.8f,%9.3f,%9.3f\n",$ifgcol,$jfgcol,$RIJcol,$QIJcol,$EIJcol,$DIJcol,$Gsol,$totcol }' > $eij
set nfg=`tail -1 <$eij | cut -d, -f1`
awk <$tmp1 -v ifgcol=$ifgcol -v jfgcol=$jfgcol -v RIJcol=$RIJcol -v QIJcol=$QIJcol -v EIJcol=$EIJcol -v DIJcol=$DIJcol -v Gsol=$Gsol -v totcol=$totcol '{ printf "%5d,%5d,%7.2f,%8.4f,%13.8f,%12.8f,%9.3f,%9.3f\n%5d,%5d,%7.2f,%8.4f,%13.8f,%12.8f,%9.3f,%9.3f\n",$ifgcol,$jfgcol,$RIJcol,$QIJcol,$EIJcol,$DIJcol,$Gsol,$totcol,$jfgcol,$ifgcol,$RIJcol,-$QIJcol,$EIJcol,$DIJcol,$Gsol,$totcol }' >$tmp2
# for easy processing add zero self-interaction
set ifg=1
while ($ifg <= $nfg)
  echo $ifg | awk '{ printf "%5d,%5d,%7.2f,%8.4f,%13.8f,%12.8f,%9.3f,%9.3f\n",$1,$1,0,0,0,0,0,0 }' >>$tmp2 
@ ifg ++
end
# sort the data once
#echo Sorting square pair interaction file... 
#sort -n <$tmp2 >$tmp1
# extract interaction for each monomer 
set ifg=1
while ($ifg <= $nfg )
# set i=`echo $ifg | awk '{ printf "^%4d",$1}'`
  echo Processing fragment $ifg out of $nfg
  grep "^[ ]*$ifg," < $tmp2 | cut -d, -f2- | sort -n >$ei_j.$ifg.csv
@ ifg ++
end
rm -f $tmp1 $tmp2
