#!/bin/csh
#  V. Kairys, U. of Iowa, August 27 1999
#
#    Usage: 
#
#           match_efp file1 file2
# 
#  Given two sets of EFP points  with overlapping regions,
#  this awk script prints out the coordinates of both EFP
#  sets; however, the matching EFP points have "F" prefix in front
#  This allows to view overlapping atoms as different from other
#  atoms in MacMolPlt, i.e. they show up as fluorine atoms.
#  (The prefix could be easily changed, look for BEGIN in the script.)
#
#   The names of the coordinate files are coords.1 and coord.2
#   the list of correspondences between the two EFP sets is in
#   file table_of_matches.
#
#   The way to work with those files:
#   1. Paste coordinates in coords.1 and coords.2 into MacMolPlt,
#      display "atom numbers". 
#   2. Print out table_of matches.
#   3. One of the two matching points has to be removed; work
#      with the table_of matches until one point in all corresponding pairs 
#      is crossed out (by hand, of course).
#      It is also possible that the tolerance was not enough to reflect all the
#      correspondences; you might have to remove some more points.
#   4. Now is up to you how you remove the unwanted points. Maybe it is a good idea
#      to remove them from coords.1 and coords.2 and then view the corrected
#      points agin using MacMolPlt just to see if you did everything correctly.
#      
#  
#   Distance tolerance is set to 0.05 bohr
#
#  The backslashes (\) are actually newline escapes because the
#  shell wants to see one command in a line
#
if ($#argv < 2) then
    echo "Usage: match_efp efpfile1 efpfile2"
    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
if (! -r $2 || -d $2) 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 '\
function distance(ax,ay,az,bx,by,bz) {\
dist=sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)+(az-bz)*(az-bz))\
return dist\
}\
BEGIN {\
#    prefix F could be replaced with something else\
prefix="F"\
tolerance=0.05\
}\
/COORD/{\
#   print  \
#   getline\
   m++\
   print  "Reading-in coordinates from the file #" m \
   while ($1 !~ /STOP/)\
          {\
           field[m,number,1]=$1\
           field[m,number,2]=$2\
           field[m,number,3]=$3\
           field[m,number,4]=$4\
           number++\
           getline\
           }\
   maxnum[m]=number-1\
   number=0\
        }\
END{\
   icommon=1\
   print "Trying to find matching coordinates"\
   for(i=1;i<=maxnum[1];i++)\
     { \
      hit=0\
      for(j=1;j<=maxnum[2];j++)\
         { if(distance(field[1,i,2],field[1,i,3],field[1,i,4],\\
                    field[2,j,2],field[2,j,3],field[2,j,4])<tolerance) \
            { commonA[icommon]=i\
              commonB[icommon]=j\
              icommon++\
              hit=1\
            }\
         }\
      if(hit==0) \
          print field[1,i,1] "  " field[1,i,2] " " field[1,i,3] " " field[1,i,4] > "coords.1"\
      else\
          print prefix field[1,i,1] "  " field[1,i,2] " " field[1,i,3] " " field[1,i,4] > "coords.1"\
     }\
#  now print out the second set of EFP points\
   for(i=1;i<=maxnum[2];i++)\
     { \
      hit=0\
      for(j=1;j<=icommon-1;j++)\
         { \
         if (commonB[j]==i) hit=1\
         }\
      if(hit==0) \
          print field[2,i,1] "  " field[2,i,2] " " field[2,i,3] " " field[2,i,4] > "coords.2"\
      else\
          print prefix field[2,i,1] "  " field[2,i,2] " " field[2,i,3] " " field[2,i,4] > "coords.2"\
     } \
#  print out the table of matches\
      print "List of position matches between the first and second EFP set" > "table_of_matches"\
   for(i=1;i<=icommon-1;i++)\
      { \
      print field[1,commonA[i],1] "   " field[2,commonB[i],1] > "table_of_matches"\
      }\
   }\
   ' $1 $2
   echo "The coordinates of multipole expansion points printed in coords.1 and coords.2"
   echo "The list of matches printed in table_of_matches"
