subroutine egrad_turbomole(e,geomcart,gradcart)

  ! This routines calculates the energy and the gradient of 
  ! a molecule, using TurboMole 

  use Path_module, only : Nat, AtName, unit,ProgExe,renum,order,OrderInv
  use Io_module

  IMPLICIT NONE

  ! Energy (calculated if F300K=.F., else estimated)
  REAL(KREAL), INTENT (OUT) :: e
  ! Nb degree of freedom
  REAL(KREAL), INTENT (IN) :: geomcart(Nat,3)
  ! Gradient calculated at Geom geometry
  REAL(KREAL), INTENT (OUT) :: gradcart(3*NAt)

  ! ======================================================================

  character(LCHARS) :: LINE

  logical           :: debug

  REAL(KREAL), ALLOCATABLE :: GradTmp(:)

  REAL(KREAL) :: d, a_val, Pi

  REAL(KREAL) :: coef,x
  INTEGER(KINT) :: iat, jat, kat, i, j, k,n3at
  INTEGER(KINT) :: ITmp1,ITmp2, Idx
  INTEGER(KINT), PARAMETER :: IOLOG=12, IOCOM=11

  CHARACTER(132) :: FileIn, FileOut, FileChk

  CHARACTER(3) :: SepIn=' ', SepOut=' '
  CHARACTER(VLCHARS), SAVE ::  RunCommand

  ! ======================================================================

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

  ! ======================================================================

  Pi=dacos(-1.0d0)
  n3at=3*nat

  debug=valid('EGRAD')
  if (debug) WRITE(*,*) '================ Entering Egrad_TurboMole ===================='


  gradcart=0.
  E=0.

  FileIn="coord"
  FileOut="gradient"

  RunCommand=Trim(ProgExe)

  IF (DEBUG) WRITE(*,*)'RunCommand:',TRIM(RunCommand)

  ! we create the input file

  OPEN(IOTMP,File=FileIn)
  WRITE(IOTMP,'(1X,A)') '$coord'
  DO I=1,Nat
     If (renum) THEN
        Iat=Order(I)
        WRITE(IOTMP,'(1X,3(1X,F15.8),A5)') GeomCart(Iat,:)*Unit,Trim(AtName(Iat))
     ELSE
        Iat=OrderInv(I)
        WRITE(IOTMP,'(1X,3(1X,F15.8),A5)') GeomCart(I,:)*Unit,Trim(AtName(Iat))
     END IF
  END DO
  WRITE(IOTMP,'(1X,A)') '$user-defined bonds'
  WRITE(IOTMP,'(1X,A)') '$end'
  CLOSE(IOTMP)

  IF (debug) THEN
     WRITE(*,*) 'DBG EGRAD ' //  Trim(FileIn)
     call system('cat ' // Trim(FileIn))
  END IF


  call system(RunCommand)

  if (debug) WRITE(*,*) 'DBG EGRAD, back from calculation'

  ! Whatever the coordinate system, we read the cartesian gradient
  ! and we convert it to Zmat or other if needed

  OPEN(IOTMP,FILE=FileOut, STATUS='old')
! We check that gradient file is ok
  READ(IOTMP,'(A)') LINE 
  Line=AdjustL(Line)
  if (Line(1:5).NE.'$grad') THEN
     WRITE(*,*) "Error with Turbomole, gradient file does not start with $grad"
     STOP
  END IF

! we read the energy
  READ(IOTMP,'(A)') LINE
! energy is after the second = sign:
!  cycle =      1    SCF energy =    -2889.2212497430   |dE/dxyz| =  0.064137
  Idx=Index(Line,'=')
  Line=Line(Idx+1:)
  Idx=Index(Line,'=')
  Line=Line(Idx+1:)
 READ(Line,*) E

! We skip the coordinates
 DO I=1,Nat
    READ(IOTMP,'(A)') LINE
 END DO

! We read the gradient
  DO I=1,Nat
     Iat=I
     IF (renum) Iat=Order(I)
     READ(IOTMP,*)  GradCart(3*Iat-2:3*Iat)
     if (debug) WRITE(*,*) "I,Iat,GradCart:",I,Iat,GradCart(3*Iat-2:3*Iat)
  END DO

! TurboMole gives the Forces in au/a0, so we convert it into the 
! gradient in ua/Angstrom 
  gradCart=gradCart*unit

  CLOSE(IOTMP)

  if (debug) WRITE(*,*) '================  Egrad_TurboMole Over ===================='

  RETURN

999 CONTINUE
  WRITE(*,*) 'We should not be here !!!!'
  STOP
  ! ======================================================================
end subroutine egrad_TurboMole
