#!/bin/sh
#*******************************************************************************
#                                                                              *
#                                   Viewmol                                    *
#                                                                              *
#                                R E A D P Q S                                 *
#                                                                              *
#                    Copyright (c) Gabor Magyarfalvi, 1998                     *
#                                                                              *
#*******************************************************************************
#
# $Id: readpqs,v 1.1 2003/11/07 12:58:24 jrh Exp $
# $Log: readpqs,v $
# Revision 1.1  2003/11/07 12:58:24  jrh
# Initial revision
#
#
# readpqs is a script to read the output of PQS into viewmol
# Copyright © 1998 Gabor Magyarfalvi
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
if [ ! -f "$1" ]
then
  echo "\$error noFile 1 \"$1\""
  echo "\$end"
  exit 1
fi


export LANG="C"
awk  'BEGIN {cycle=0;
             readcoo=0;
             readgra=0;
             readfrq=0;
             prevcoo=0;
             nfreq=0;
             i=0;
             l=0;
             na=0
            }
      /Coordinates \(Angstroms\)/ {readcoo=1;i=0;prevcoo=1}
      /^    [1-9]|^   [1-9]|^  [1-9]/ {if (readcoo == 1)
                         {
                         coord[i,1]=$3;
                         coord[i,2]=$4;
                         coord[i,3]=$5;
                         coord[i++,4]=$2;
#                         printf("%d %d %10.6f %10.6f %10.6f \n",cycle,i,\
#                                 coord[i-1,1],coord[i-1,2],coord[i-1,3]);
                         }
                        }
      /^   Point Group/ {na=i;readcoo=0;i=0}
      /force-x/{readgra=1; k=0}
      /^  [1-9]|^ [1-9]|^[1-9]/ {if (readgra == 1 && prevcoo == 1 && k<na)
                         {
                         grad[k,1]=$3;
                         grad[k,2]=$4;
                         grad[k++,3]=$5;
#                         printf("%d %d %10.6f %10.6f %10.6f \n",cycle,k,\
#                             grad[k-1,1],grad[k-1,2],grad[k-1,3]);
                         gnorm+=$3*$3+$4*$4+$5*$5;
                         }
                        }
      /^ Maximum Cartesian force/{readgra=0;norm=gnorm;gnorm=0.0;k=0;prevcoo=0}
      /^   Energy is/ {energy=$3;
                       cycle++;
                       if (cycle == 1) printf("$grad\n");
                       printf("  cycle =    %d  energy =%18.10f |dE/dxyz| =%10.6f\n", cycle, energy, sqrt(norm));
                       for (j=0; j<na; j++)
                       {
                         printf("%22.14f%22.14f%22.14f  %s\n", coord[j,1], coord[j,2], coord[j,3], coord[j,4]);
                       }
                       for (j=0; j<na; j++)
                       {
                         printf("%22.14f%22.14f%22.14f\n", grad[j,1], grad[j,2], grad[j,3]);
                       }
                       gnorm=0.0;
                      }
      /^ \*\* VIBRATIONAL FREQUENCIES/ {printf("$vibrational spectrum\n")}
      /^ Symmetry:/ {readfreq = 0;
		     l++;
                     ii=0;
                     for (m=1; m<NF; m++)
                     {
                        sym[m]=$(m+1);
                        if (sym[m]=="???") sym[m]="A1";
                     }
                     getline;
                     for (m=1; m<NF; m++)
                     {
                        freq[m]=$(m+1);
                     }
                     nfreq+=m-1;
                    }
      /^ IR Intens:/ {for (m=1; m<NF; m++)
                      {
                        iri[m]=$(m+2)
                      }
                     }
      /^ Raman Active:/ {for (m=1; m<NF-1; m++)
                         {
                          if ($(m+2)=="YES")
                           {ri[m]=1.0}
                          else {ri[m]=0.0} 
                          printf("%s %10.1f %10.5f %10.5f\n", sym[m], freq[m], iri[m], ri[m]);
                         }
                         readfreq = 1;
                        }
      /^[ A-z][A-z][ A-z] / {if (readfreq == 1)
                              {
                                for (j=0; (j+1)*3+1<=NF; j++)
                                {
                                  for (m=0; m<3; m++)
                                  {nm[3*(l-1)+j,ii+m]=$(3*j+m+2)}
                                }
                                ii=ii+3;
                              }
                             }
      /^   Zero point vibrational energy/ {readfreq=0;
                                         printf("$vibrational normal modes\n");
                                         for (j=0; j<3*na; j++)
                                         {
                                           for (k=0; k<nfreq; k++)
                                           {
                                             if (k % 5 == 0)
                                             {
                                               if (k != 0) printf("\n");
                                               printf("%d %d", j+1, j+1);
                                             }
                                             printf(" %10.6f", nm[k,j]);
                                           }
                                           printf("\n");
                                         }
                                        }
      END {printf("$coord\n");
           for (j=0; j<na; j++)
           {
             printf("%22.14f%22.14f%22.14f  %s\n", coord[j,1], coord[j,2], coord[j,3], coord[j,4]);
           }
           printf("$end\n");}' "$1"
