#! /bin/sh
#*******************************************************************************
#                                                                              *
#                                   Viewmol                                    *
#                                                                              *
#                               R E A D G U L P                                *
#                                                                              *
#                 Copyright (c) Joerg-R. Hill, October 2003                    *
#                                                                              *
#*******************************************************************************
#
# $Id: readgulp,v 1.6 2003/11/07 12:56:34 jrh Exp $
# $Log: readgulp,v $
# Revision 1.6  2003/11/07 12:56:34  jrh
# Release 2.4
#
# Revision 1.5  2000/12/10 15:15:31  jrh
# Release 2.3
#
# Revision 1.4  1999/05/24 01:27:18  jrh
# Release 2.2.1
#
# Revision 1.3  1999/02/07 21:55:52  jrh
# Release 2.2
#
# Revision 1.2  1998/01/26 00:49:17  jrh
# Release 2.1
#
# Revision 1.1  1996/12/10  18:46:30  jrh
# Initial revision
#
#

if [ ! -f "$1" ]
then
  echo "\$error noFile 1 \"$1\""
  echo "\$end"
  exit 1
fi

case `uname -s` in
  SunOS*) AWK=nawk
	    ;;
  IRIX*)  AWK=nawk
	    ;;
  *)      AWK=awk
	    ;;
esac

export LANG="C"
$AWK 'BEGIN {eV2bohr=.001944673764445625802;
             readcoord=0;
             readgrad=0;
             readfreq=0;
             count=0;
             nfreq=0;
             i=1;}
     /^  a =/ {a=$3;
               alpha=$6;}
     /^  b =/ {b=$3;
               beta=$6;}
     /^  c =/ {c=$3;
               gamma=$6;
              }
     /^       a    / {a=$2;}
     /^       b    / {b=$2;}
     /^       c    / {c=$2;}
     /^       alpha/ {alpha=$2;}
     /^       beta / {beta=$2;}
     /^       gamma/ {gamma=$2;}
     /^  Final asymmetric unit coordinates/ {readcoord=1;
                                             printf("$coord fractional 1.0\n");
                                            }
     /^  Final fractional coordinates of atoms/ {readcoord=1;
                                                 printf("$coord fractional 1.0\n");
                                                }
     /^  Final cartesian coordinates of atoms/ {readcoord=1;
                                                printf("$coord 1.0\n");
                                                }
#    /^  Final internal derivatives/ {readgrad=1;
#                                     printf("$grad\n");
#                                     printf("  cycle =    1  energy =%18.10f |dE/dxyz| =%10.6f\n", energy, gnorm);
#                                     for (j=1; j<i; j++)
#                                     {
#                                       printf("%22.14f%22.14f%22.14f  %s\n", coord[j,1], coord[j,2], coord[j,3], coord[j,4]);
#                                     }
#                                    }
     /^  Final energy/ {energy=$4;}
     /^  Final Gnorm/  {gnorm=$4;}
     /^ Frequencies \(cm-1\) and Eigenvectors/ {readfreq=1;}
     /^ Frequency/ {freqs[nfreq]=$2;
                    freqs[nfreq+1]=$3;
                    freqs[nfreq+2]=$4;
		    if (NF > 4) {
                      freqs[nfreq+3]=$5;
                      freqs[nfreq+4]=$6;
                      freqs[nfreq+5]=$7
		    }
                   }
     /^ IR Intensity/ {ir[nfreq]=$3;
                       ir[nfreq+1]=$4;
                       ir[nfreq+2]=$5;
		    if (NF > 5) {
                      ir[nfreq+3]=$6;
                      ir[nfreq+4]=$7;
                      ir[nfreq+5]=$8
		    }
                   }
     /^ Raman Intsty/ {raman[nfreq]=$3;
                       raman[nfreq+1]=$4;
                       raman[nfreq+2]=$5;
		       if (NF > 5) {
                         raman[nfreq+3]=$6;
                         raman[nfreq+4]=$7;
                         raman[nfreq+5]=$8
		       }
                       nfreq += NF-2;
		       total = 0;
                      }
     /^                Real    Imaginary/ {total=0;
                                           nfreq+=3;}
     / *[1-9][0-9]* */ {if (readcoord == 1)
                        {
                          if ($3 == "c")
                          {
                            sub("[0-9][0-9]*", "", $2);
                            printf("%22.14f%22.14f%22.14f  %s\n", $4, $5, $6, $2);
                            coord[i,1]=$4;
                            coord[i,2]=$5;
                            coord[i,3]=$6;
                            coord[i,4]=$2;
                            i++;
                          }
                        }
#                       if (readgrad == 1)
#                       {
#                         if ($3 == "c")
#                         {
#                           printf("%22.14f%22.14f%22.14f\n", eV2bohr*$4, eV2bohr*$5, eV2bohr*$6);
#                         }
#                       }
		           }
     /^ *[0-9]* [xyz]/ {if (readfreq == 1)
                        {
		          if (NF <= 5 ) {
                             nm[nfreq-3,total]=$3;
                             nm[nfreq-2,total]=$4;
                             nm[nfreq-1,total]=$5;
			  } else {
                             nm[nfreq-3,total]=$3;
                             nm[nfreq-2,total]=$4;
                             nm[nfreq-1,total]=$5;
                             nm[nfreq-3,total]=$6;
                             nm[nfreq-2,total]=$7;
                             nm[nfreq-1,total]=$8;
	                  }
		          total++;
                        }
                       }
     /^-------/ {if (readcoord == 1 || readgrad == 1)
                 {
                   count++;
                 }
                 if (count == 3)
                 {
                   if (readcoord == 1)
                   {
                     readcoord=0;
                     count=0;
                   }
                   if (readgrad == 1)
                   {
                     readgrad=0;
                     count=0;
                   } 
                 }
                 if (readfreq == 1)
                 {
                   readfreq=0;
                   printf("$vibrational spectrum\n");
                   for (j=0; j<nfreq; j++)
                   {
                     printf("A1 %10.2f %10.6f %10.6f\n", freqs[j], ir[j], raman[j]);
                   }
                   printf("$vibrational normal modes\n");
                   for (j=0; j<total; j++)
                   {
                     for (k=0; k<nfreq; k++)
                     {
		           if (k % 5 == 0)
			     {
			       if (k != 0) printf("\n");
			       printf("%d %d", j, j);
			     }
                       printf(" %10.6f", nm[k,j]);
                     }
		         printf("\n");
                   }
                 }
                }
   END {printf("$unitcell %f %f %f %f %f %f\n", a, b, c, alpha, beta, gamma);
        printf("$end\n");}' "$1"
