 SUBROUTINE AnalyzeGeom(GeomCart,Values)
! This routines read a list of geometrical variables to monitor
! This is inspired from Xyz2Path (that was inspired by Xyz2scan ...)

!----------------------------------------------------------------------
!  Copyright 2003-2014 Ecole Normale Supérieure de Lyon, 
!  Centre National de la Recherche Scientifique,
!  Université Claude Bernard Lyon 1. All rights reserved.
!
!  This work is registered with the Agency for the Protection of Programs 
!  as IDDN.FR.001.100009.000.S.P.2014.000.30625
!
!  Authors: P. Fleurat-Lessard, P. Dayal
!  Contact: optnpath@gmail.com
!
! This file is part of "Opt'n Path".
!
!  "Opt'n Path" is free software: you can redistribute it and/or modify
!  it under the terms of the GNU Affero General Public License as
!  published by the Free Software Foundation, either version 3 of the License,
!  or (at your option) any later version.
!
!  "Opt'n Path" 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 Affero General Public License for more details.
!
!  You should have received a copy of the GNU Affero General Public License
!  along with "Opt'n Path". If not, see <http://www.gnu.org/licenses/>.
!
! Contact The Office of Technology Licensing, valorisation@ens-lyon.fr,
! for commercial licensing opportunities.
!----------------------------------------------------------------------


  use VarTypes
  use Path_module
  use Io_module

  IMPLICIT NONE


  INTERFACE
     function valid(string) result (isValid)
       CHARACTER(*), intent(in) :: string
       logical                  :: isValid
     END function VALID

     SUBROUTINE die(routine, msg, file, line, unit)

       Use VarTypes
       Use io_module

       implicit none

       character(len=*), intent(in)           :: routine, msg
       character(len=*), intent(in), optional :: file
       integer(KINT), intent(in), optional      :: line, unit

     END SUBROUTINE die


     SUBROUTINE Calc_Xprim(nat,x,y,z,Coordinate,NPrim,XPrimitive,XPrimRef)

       Use VarTypes
       Use Io_module
       Use Path_module, only : pi

       IMPLICIT NONE

       Type (ListCoord), POINTER :: Coordinate
       INTEGER(KINT), INTENT(IN) :: Nat,NPrim
       REAL(KREAL), INTENT(IN) :: x(Nat), y(Nat), z(Nat)
       REAL(KREAL), INTENT(IN), OPTIONAL :: XPrimRef(NPrim) 
       REAL(KREAL), INTENT(OUT) :: XPrimitive(NPrim)

     END SUBROUTINE CALC_XPRIM

  END INTERFACE

! Input
  REAL(KREAL),INTENT(IN) :: GeomCart(Nat,3)
  REAL(KREAL), INTENT(OUT) :: Values(NbVar)

  LOGICAL :: Debug
  INTEGER(KINT) :: I,J,K,NatT
  REAL(KREAL), ALLOCATABLE :: GeoCartLoc(:,:) ! (Nat+NbCom,3)
  REAL(KREAL), ALLOCATABLE :: x(:),y(:),z(:) ! Nat+NbCom
  REAL(KREAL) :: COG(3),Weight



  Debug=Valid('AnaGeom')

  If (Debug) Call Header("Entering AnalyzeGeom")

  if (debug) THEN
     WRITE(*,*) "AnalyzeGeom  - GeomCart"
     DO K=1,Nat
        WRITE(*,'(1X,I5,3(1X,F15.8))') K,GeomCart(K,1:3)
     END DO
  END IF

  NAtt=Nat+NbCom
  ALLOCATE(GeoCartLoc(Natt,3),x(Natt),y(Natt),z(Natt))
  GeoCartLoc(1:Nat,:)=GeomCart(:,:)
  CurBary => Bary

  DO I=1, NbCom
     COG=0.
     Weight=0.
     DO j=1,CurBary%ListAtoms(0)
        DO k=1,3
           COG(k)=COG(k)+GeomCart(CurBary%ListAtoms(j),k)*CurBary%Weights(j)
        END DO
        Weight=Weight+CurBary%Weights(j)
     END DO
     COG=COG/Weight
     DO k=1,3
        GeoCartLoc(Nat+i,k)=COG(k)
     END DO
  END DO
     
  Values=0.

  if (debug) THEN
     WRITE(*,*) "AnalyzeGeom before Calc_Xprim - GeomCartLoc"
     DO K=1,Nat+NbCom
        WRITE(*,*) K,GeoCartLoc(K,1:3)
     END DO
  END IF

  x = GeoCartLoc(:,1)
  y = GeoCartLoc(:,2)
  z = GeoCartLoc(:,3)

   Call Calc_XPrim(Natt,x,y,z,GeomList,NbVar,Values)

   DeALLOCATE(GeoCartLoc,x,y,z)

   if (debug) THEN
      WRITE(*,*) 'AnalyzeGeom: NbVar,Values',NbVar,Values
   END IF

  If (Debug) Call Header("Exiting AnalizeGeom")

END SUBROUTINE AnalyzeGeom
