! Subroutines for timer functions, USING PORTLIB
MODULE Timer
! Function    : Date and time functions for DF6, most useful under Windows NT
! Version     : 1.0,  1999
! Author      : Bart Jan van Os
! Note        :
USE OptTreeCallBacks, ONLY: dp

PRIVATE

PUBLIC InitTimer,SetTimer,GetTimer,GetElapsedTime,GetSpecifiedElapsedTime,&
       WriteTimeFor,WriteElapsedTimeFor,WriteSpecifiedTimeFor,&
       WriteSpecifiedElapsedTimeFor,WriteDateAndTime,dp,sp

! Portable replacement for REAL(4)/REAL*4 notation.
INTEGER, PARAMETER :: sp = SELECTED_REAL_KIND(6, 37)

REAL Total,StartTimeA,StartTimeB, LastTimeA, LastTimeB

CONTAINS

SUBROUTINE InitTimer
! Function    : Initialize Timer, Total + Elapsed time. DOES FUNCTION IN
! Parameter block.
! Local variables.
! Program Start

   Total=0.0
   LastTimeA=0.0
   StartTimeA=0.0
   LastTimeB=0.0
   StartTimeB=0.0

END SUBROUTINE  InitTimer


SUBROUTINE SetTimer (CurrentTime)
!USE PORTLIB
! Function    : Initialize elapsed time. DOES FUNCTION IN WIN95
!               CurrentTime is allways the total elapsed time since prog
! Parameter block.
REAL(dp)          CurrentTime
! Local variables.
REAL(sp)          TA(2)
! Program Start

   Total = Total+DTIME(TA)
   CurrentTime=Total

   LastTimeA=LastTimeA+TA(1)
   StartTimeA=LastTimeA
   LastTimeB=LastTimeB+TA(2)
   StartTimeB=LastTimeB


END SUBROUTINE SetTimer



SUBROUTINE GetTimer (CurrentTime)
!USE PORTLIB
! Function    : Get elapsed time. DOES FUNCTION IN WIN95
!               CurrentTime is allways the total elapsed time since prog
! Parameter block.
REAL(dp)          CurrentTime
! Local variables
REAL(sp)          TA(2)
! Program Start

   Total = Total+DTIME(TA)
   CurrentTime=Total
   LastTimeA=LastTimeA+TA(1)
   LastTimeB=LastTimeB+TA(2)

END SUBROUTINE  GetTimer



SUBROUTINE GetElapsedTime (Elapsed)
! Function    : Get elapsed time.
!               Gives the elapsed time since last call to SetTime
! Parameter block.
REAL(dp)          Elapsed
! Local variables
! Program Start

   Elapsed = (LastTimeA-StartTimeA) + (LastTimeB-StartTimeB)

END SUBROUTINE  GetElapsedTime



SUBROUTINE GetSpecifiedElapsedTime (Elaps1,Elaps2)
! Function    : Get elapsed time.
!               Gives the elapsed time since last call to SetTime
!               specified in user and privelidged code time
! Parameter block.
REAL(dp)          Elaps1,Elaps2
! Local variables
! Program Start

   Elaps1 = LastTimeA-StartTimeA
   Elaps2 = LastTimeB-StartTimeB

END SUBROUTINE  GetSpecifiedElapsedTime



SUBROUTINE WriteTimeFor (Name,ElapsedTime,OutUnit)
! Function    : Print the elpased time for Name
! Parameter block.
INTEGER           OutUnit
REAL(dp)          ElapsedTime
CHARACTER(LEN=10)      Name
! Program Start

   !WRITE (OutUnit,'(1X,A10," needed ",F10.3," seconds of CPU time.")') &
    !  Name,ElapsedTime

Name=Name
ElapsedTime=ElapsedTime
OutUnit=OutUnit

END SUBROUTINE  WriteTimeFor



SUBROUTINE WriteElapsedTimeFor (Name,OutUnit)
! Function    : Print the elapsed time for Name
! Parameter block.
INTEGER           OutUnit
REAL(dp)           ElapsedTime
CHARACTER(LEN=10)      Name
! Program Start

Name=Name
OutUnit=OutUnit

   CALL GetElapsedTime (ElapsedTime)
   !CALL WriteTimeFor (Name,ElapsedTime,OutUnit)

END SUBROUTINE  WriteElapsedTimeFor




SUBROUTINE WriteSpecifiedTimeFor (Name,Elaps1,Elaps2,OutUnit)
! Function    : Print an elpased time for Name, specified over
!               user and system time.
! Parameter block.
INTEGER           OutUnit
REAL(dp)           Elaps1,Elaps2
CHARACTER(LEN=10)      Name
! Program Start

   !WRITE (OutUnit,'(1X,A10," needed ", F10.3, " seconds of CPU time.")') &
  !    Name,Elaps1+Elaps2
   !WRITE (OutUnit,'(1X,"(", F10.3, " seconds of user time",'//&
    !               '" and ",F10.3, " seconds of system time.)")') &
    !  Elaps1, Elaps2

Name=Name
Elaps1=Elaps1
Elaps2=Elaps2
OutUnit=OutUnit


END SUBROUTINE  WriteSpecifiedTimeFor



SUBROUTINE WriteSpecifiedElapsedTimeFor (Name,OutUnit)
! Function    : Print total elapsed time for Name, specified
!               over user and system time.
! Parameter block.
INTEGER           OutUnit
CHARACTER(LEN=10)      Name
! Local variables.
REAL(dp)           Elaps1,Elaps2
! Program Start

OutUnit=OutUnit
Name=Name

   CALL GetSpecifiedElapsedTime (Elaps1,Elaps2)
   !CALL WriteSpecifiedTimeFor (Name,Elaps1,Elaps2,OutUnit)

END SUBROUTINE  WriteSpecifiedElapsedTimeFor



SUBROUTINE WriteDateAndTime(OutUnit,Title)
! Function    :
! Parameter block.
INTEGER OutUnit
CHARACTER(*) Title
! Local variables.
CHARACTER (12) Dates,Times
! BEGIN ROUTINE

OutUnit=OutUnit
Title=Title

   CALL DATE_AND_TIME (Dates,Times)
   !WRITE (OutUnit,*) TRIM(Title),' ',Dates(7:8),'-',Dates(5:6),'-',Dates(1:4),&
   !    '    ',Times(1:2),':',Times(3:4),':',Times(5:6)

END SUBROUTINE  WriteDateAndTime



END MODULE Timer
