C
C  p2 = 0 means kd, seckd is before the beginning of the kdbc, secbc


       subroutine time_basis(kd,seckd,kdbc,secbc,ntbc,DELTTBC,p2,theta)

         integer kd,kdbc(ntbc)
         real    seckd, secbc(ntbc) 
	 integer ntbc
         real    DELTTBC
         integer p2
         real    theta
C
         integer p1
         real    dday,dseconds
	 real    bt1, bt2

         integer igday_compare
        

C  find interval which contains the current time
c      write(*,*)'time_basis, kd, seckd', kd,seckd,kdbc(1),secbc(1)
       if(igday_compare(kd,seckd,kdbc(1),secbc(1)).lt.0) then 
c	  write(*,*)'too small'
          p2 = 2
          theta = 0.0
       elseif(igday_compare(kd,seckd,kdbc(ntbc),secbc(ntbc)).gt.0) then 
c	  write(*,*)'too big'
          p2 = ntbc
          theta = 1.0
       else 
c	  write(*,*)'just right'
          dday = kd - kdbc(1)
          dseconds = dday*24*3600 + (seckd - secbc(1))
          p1 = int(dseconds/DELTTBC)  + 1
          p2 = p1+1 
c      write(*,*)p1,p2,dseconds, dseconds/DELTTBC
C  now calculate the time domain basis functions

          dseconds = (kd - kdbc(p1))*24.*3600. + seckd - secbc(p1)
          BT2 = dseconds/DELTTBC 
c         BT1 =  1.0 - BT2 
c         write(*,*)dseconds,BT1, BT2
          theta = BT2 
       endif

       return 
       end

