      program x
      implicit none
      integer          NDIM
      parameter(NDIM=1000)
      double precision u(NDIM,3),uref(NDIM,3),r
      integer          i,n,k,mode,min,max,natoms
      character*10     dump_str
      character        dump_char
      integer          dump_int

      integer          iargc,nargs
      character*80     fref,mstr,minstr,maxstr

c     check arguments
      nargs=iargc()
      if (nargs.eq.4) then
         call getarg(1,fref)
         call getarg(2,minstr)
         call getarg(3,maxstr)
         call getarg(4,mstr)
      else
         write(*,*) 'syntax:'
         write(*,*) 'cvib fort.22.ref min max mode '
         stop
      endif

      open(unit=10,file=fref)
      open(unit=11,file="fort.22")
      read(minstr,*) min
      read(maxstr,*) max
      read(mstr,*) mode

      read(10,*) natoms

C     read mode coordinates
      do i=1,mode
        read(11,*) dump_int,dump_char,dump_str
        do n=1,natoms
          read(11,*) (u(n,k),k=1,3)
        end do
      end do

C     read ref mode coordinates and compare
      do i=1,min-1
        read(10,*) dump_int,dump_char,dump_str
        do n=1,natoms
          read(10,*) (uref(n,k),k=1,3)
        end do
      end do

      do i=min,max
        read(10,*) dump_int,dump_char,dump_str
        r=0.0d0
        do n=1,natoms
          read(10,*) (uref(n,k),k=1,3)
        end do
        do n=1,natoms
          do k=1,3
            r=r+uref(n,k)*u(n,k)
          end do
        end do
        r=abs(r)
        write(*,20) i,r
      end do
20    format(I6,3F16.10)
      stop
      end
