      module ttime 
      contains 
! ---------------------------------------------------------- !
      subroutine subtene(time,te,dna,iflag)
! ---------------------------------------------------------- !
      common/com5/timet(400),tet(400),dnat(400),tinitial,tfinal,nt

      iflag = 1
      if(time .gt. tfinal) iflag = 0  ! beyond time sequence
      call locate(time,timet,nt,i)
      if(i.eq.0) i=1
      if(i.eq.nt) i=nt-1
      call cubin(time,te,timet,tet,nt,i)
      call cubin(time,dna,timet,dnat,nt,i)
      dna=10.0**dna

      return
      end subroutine

! ---------------------------------------------------------- !
      subroutine timehist(d_tinitial,d_tfinal)
! ---------------------------------------------------------- !
! this program reads in time history
      common/com5/timet(400),tet(400),dnat(400),tinitial,tfinal,nt

      tinitial = d_tinitial
      tfinal = d_tfinal
      n=0
 10   n=n+1
      read(22,*,end=20) timet(n),tet(n),dna
      dnat(n)=log10(dna)
      go to 10
 20   nt=n-1
      if(nt.gt.100) write(6,*) 'timehist warn: nt.gt.100',nt

      return
      end subroutine

! ---------------------------------------------------------- !
         subroutine cubin(x,y,tx,ty,nmax,i)
! ---------------------------------------------------------- !
! Purpose: lagrange cubic interpolation routine
! ---------------------------------------------------------- !
         dimension tx(:),ty(:)
!
         ks=1
         if(i.eq.1) ks=2
         np=nmax-1
         ke=4
         if(i.eq.np) ke=3
!
         y=0.0
         do 20 k=ks,ke
         ii=i+k-2
         rlii=1.0
         do 10 kp=ks,ke
         jj=i+kp-2
         if(ii.eq.jj) go to 10
         rlii=rlii*(x-tx(jj))/(tx(ii)-tx(jj))
 10      continue
         y=y+rlii*ty(ii)
 20      continue
!
         return
         end subroutine
! ---------------------------------------------------------- !
      subroutine locate(x,r,nmax,j)
! ---------------------------------------------------------- !
      dimension r(:),ic(14)
      save ic
      data ic/4096,2048,1024,512,256,128,64,32,16,8,4,2,1,1/
      j=8192
      do 20 il=1,14
      if(j.gt.nmax)goto 12
      if(x.gt.r(j))goto 15
12    j=j-ic(il)
      goto 20
15    j=j+ic(il)
20    continue
      if(j.eq.0)return
      if(j.gt.nmax)j=nmax
      if(x.lt.r(j))j=j-1
      return
      end subroutine
! ---------------------------------------------------------- !
      subroutine timekeeper(nnn,iprint,nprints,ntsteps,iprt,  &
     &                      deltat,timep,time,istop)
! ---------------------------------------------------------- !
! Purpose: This subroutine should be called in a location 
!          at the end of a self consistent iteration sequence.
! ---------------------------------------------------------- !
      dimension timep(101)
      save
      nnn=nnn+1
      iprt=0
      istop=0
      if(nnn .gt. ntsteps) then
        iprint=iprint+1
        nnn=1
        iprt=1
      endif
      time=timep(iprint) + nnn*deltat
      if(iprint .gt. (nprints-1)) istop=1
      end subroutine timekeeper
! ---------------------------------------------------------- !
     end module ttime

