SUBROUTINE Mixed2Cart(Nat,IndZmat,IntCoord,XyzCoord)

!----------------------------------------------------------------------
!  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 Path_module, ONLY : Pi,NCart,NCoord

  IMPLICIT NONE

  INTEGER, PARAMETER :: Kint=Kind(1)
  INTEGER, PARAMETER :: KREAL=Kind(1.0D0)

  INTEGER(KINT), INTENT(IN) :: Nat
  INTEGER(KINT), INTENT(IN) :: IndZmat(Nat,5)
  REAL(KREAL), INTENT(IN) :: IntCoord(*)
  REAL(KREAL), INTENT(OUT) :: XyzCoord(Nat,3)

  INTEGER(KINT) :: I, J, Idx, IBeg, Iat
  REAl(KREAL), ALLOCATABLE :: ValZmat(:,:),XyzTmp(:,:)
  REAL(KREAL) :: d, a_val

  LOGICAL :: Debug

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

  debug=valid('mixed2cart')

  if (debug) WRITE(*,*) "================ Entering Mixed2Cart =================="


  ALLOCATE(ValZmat(Nat,3),XyzTmp(Nat,3))

  if (debug) WRITE(*,*) "DBG Mixed2Cart, IntCoord:",IntCoord(1:NCoord)
  Idx=1
  valzmat=0.
  XyzTmp=0.
  DO I=1,NCart
     XYZTmp(I,1:3)=IntCoord(Idx:Idx+2)
     valzmat(I,1:3)=IntCoord(Idx:Idx+2)
     Idx=Idx+3
  END DO

  IBeg=NCart+1
  SELECT CASE (NCart)
  CaSE (1)
     Idx=4
     XyzTmp(2,1)=IntCoord(Idx)
     XyzTmp(2,1:3)= XyzTmp(2,1:3)+ XyzTmp(1,1:3) 
     if (Nat.GE.3) THEN
        Idx=Idx+1
        d=IntCoord(Idx)
        Idx=Idx+1
        a_val=IntCoord(Idx)
        Idx=Idx+1
        call ConvertZmat_cart_3(3,IndZmat,d,a_val,       &
             XyzTmp(1,1), XyzTmp(1,2),XyzTmp(1,3))
     ENDIF
     IBeg=4
  CASE (2)
     Idx=7
     if (Nat.GE.3) THEN
        d=IntCoord(Idx)
        idx=idx+1
        a_val=IntCoord(Idx)
        Idx=Idx+1
        call ConvertZmat_cart_3(3,IndZmat,d,a_val,       &
             XyzTmp(1,1), XyzTmp(1,2),XyzTmp(1,3))
     ENDIF
     IBeg=4
  CASE DEFAULT
     Ibeg=NCart+1
  END SELECT

  DO iat=IBeg,Nat
     ValZmat(iat,1)=IntCoord(idx)
     Idx=Idx+1
     do j=2,3
        ValZmat(iat,J)=IntCoord(idx)*180./Pi
        Idx=Idx+1
     END DO
  END DO

  DO iat=iBeg,Nat
     call ConvertZmat_cart(iat,IndZmat,ValZmat,       &
          XyzTmp(1,1), XyzTmp(1,2),XyzTmp(1,3))
  END DO

  if (debug) THEN 
     WRITE(*,*) 'DBG Mixed2Cart XyzTmp'
     DO I=1,Nat
        WRITe(*,'(1X,I3,3(1X,F10.4))') I, XyzTmp(I,1:3)
     END DO
  END IF

!  XyzCoord=Reshape(XyzTmp,(/3,Nat/),ORDER=(/2,1/))
  XyzCoord=XyzTmp

  DEALLOCATE(valzmat,xyztmp)
  if (debug) WRITE(*,*) "================ Mixed2Cart Over =================="

END SUBROUTINE Mixed2Cart
