#!/bin/csh

# V. Kairys, U of Iowa, September 1999
# awk script to remove extraneous multipoles 
# absent in the EFP point list # under the heading COORDINATES 
# (This happens when you remove some EFP point coordinates manually;
# the script removes the corresponding redundant monopoles, dipoles, etc.)
#
# usage:
#
#  weed_efp file 
#
#  where "file" is the name of file containing EFPs
#
#  The backslashes (\) are actually newline escapes because the
#  shell wants to see one command in a line
#
if ($#argv < 1) then
    echo "Usage: weed_efp efpfile"
    exit 1
endif
if (! -r $1 || -d $1) then
    echo "File $1 does not exist or is not readable or it is a directory"
    exit 1
endif
# init is the offset
#set OFFSET=$2
#if (null$OFFSET == null) set OFFSET=1 
#
#  start the awk
#
awk '\
BEGIN{m=1\
     init=1\
     outfile=FILENAME ".cleaned"\
     print "The cleaned EFPs will be written to " outfile\
     }\
/COORD/{\
   print > outfile  #print COORDINATES\
   i=init\
   getline\
   while ($1 !~ /STOP/)\
          {\
           name[m]=$1\
           m++\
           print > outfile\
           getline\
           }\
        }\
/MONOP/{\
   print > outfile  #print MONOPOLES\
   i=init\
   while ($1 !~ /STOP/)\
          {\
           getline\
           for(i=1;i<=m-1;i++)\
             { \
                if($1 == name[i]) print > outfile\
             }\
           }\
        }\
\
/DIPOL/{\
   print > outfile  #print DIPOLES\
   i=init\
   while ($1 !~ /STOP/)\
          {\
           getline\
           for(i=1;i<=m-1;i++)\
             { \
                if($1 == name[i]) print > outfile\
             }\
           }\
        }\
/QUADR/{\
   print > outfile  #print QUADRUPOLES\
   i=init\
   while ($1 !~ /STOP/)\
          {\
           getline\
                if($1 == name[i]) \
                  { print  > outfile\
                    getline; print > outfile\
                    i++\
                  }\
           }\
        }\
/OCTUP/{\
   print > outfile  #print OCTUPOLES\
   i=init\
   while ($1 !~ /STOP/)\
          {\
           getline\
                if($1 == name[i]) \
                  { print > outfile \
                    getline; print > outfile\
                    getline; print > outfile\
                    i++\
                  }\
           }\
        }\
  {print > outfile}\
   ' $1

