#!/bin/csh

# Utility to extract Gaussian-like cube and GAMESS/FMO sparse cube files
# from GAMESS punch files (*.dat)
# version 1.1, 6.12.04
# Dmitri G. Fedorov
#    AIST, Japan
#
# Usage: 
# grabcube file.dat
# where file.dat is input (file.dat.000 and such will also be accepted).
#       file.i.cube or file.i.scube will be output, where i=0,1,2,3
#
 
if($# < 1 || ! -e $1) exit 1

set base=$1:r
if($base:e == dat) set base=$base:r
 
set nt=`wc -l <$1`

set ext=cube
foreach i (CUBE SCUBE SCM)
  foreach nb (`grep -n \$$i <$1 | cut -d: -f1`) 
    set ibody=`sed -n -e "$nb p" $1 | tr -d \$SCUBEM`
    @ lines = $nt - $nb
    set ne=`tail -n $lines <$1 | grep -m 1 -n '^ $END' | cut -d: -f1`
    @ ne--
    tail -n $lines <$1 | head -n $ne >$base.$ibody.$ext
  end
  set ext=scube
end
