C********

      SUBROUTINE set_times
C
C
C The moody bcs which are sent to saco are defined at a larger timestep
C than the model time step (both saco and moody). 
C Let the model time counter be k 
C Let the bc time counter be j
C
C k:    -1    0    1     2     3    k-1   k    k+1    N-1  N  N+1  N+2
C              
C j:          1                  j                j+1     Jmax
C
C j = 1 corresponds to k = 0
C j = Jmax  corresponds to N 
C
C For each k we want 
C    1) the next j in the list  i.e. min(j) s.t time(k)<= time(j)  
C    2) theta defined as: 
C             let the time from j-1 to j be DTBCS
C             let the time from j-1 to k be dt 
C             let theta = dt/DTBCS 
C
C The present calculation (time_basis.f) yields the above EXCEPT
C for the case time(k)=time(j).  In this case it returns the next j
C and theta=0.  Which is equivalent to the above but a bit goofy. 
C
C End Effects:
C    For time(k) < time(j<=1)    we set j =2,     theta = 0.
C    For time(k) >= time(j=Jmax) we set j = ntbc, theta = 1.
C
C We have insisted on  time(k=0) = time(j=1)
C                  and time(k=N) = time(j=Jmax)
C

      INCLUDE 'CASCO.DIM'
      include 'startstop.h'
      include 'sacotime.h'
      include 'deltabcs.h'
      include 'constants.h'
      include 'casco_params.h' 

C local variables
      integer k
      integer iyear
      real    ydayutc0
      real    totalhr 


      write(*,*)'set_times' 
C number of time steps in the boundary conditions
C  set ktime and jtime equal 
c     ntbc = nsteps+1 

      call get_sacotime(kd0,sec0,kdl,secl,delt,
     $         kdf,secf,kd_saco,sec_saco,nsteps,NSTEPDIM)

         write(*,*)'Number of time steps = ',nsteps
         if(nsteps.gt.NSTEPDIM) then
            write(2,*)'set_time: too many time steps. ',
     $                'Increase NSTEPDIM ',NSTEPDIM
            write(*,*)'set_time: too many time steps. ',
     $                'Increase NSTEPDIM ',NSTEPDIM
            stop
         endif
         write(*,*)'final time in Gregorian date ',kdf,secf
         call gday_utc0(kd0,sec0,iyear,ydayutc0)
         write(*,*)'start time in UTC0 ',iyear,ydayutc0
         call gday_utc0(kdl,secl,iyear,ydayutc0)
         write(*,*)'stop time in UTC0 ',iyear,ydayutc0
         call gday_utc0(kdf,secf,iyear,ydayutc0)
         write(*,*)'final time in UTC0 ',iyear,ydayutc0


C compute the times at the BC time points i.e. at the j's
CGH   dhour = 6.
CGH   dhour = 12.42/12.
C
c      Jmax = 500 
c      DELTTBC = totalhr*3600/float(Jmax)

       totalhr = (kdf - kd0)*24 + (secf - sec0)/3600. 
       ntbc = nint(totalhr/bcs_avg_tscale) + 1
       write(*,*)'xxx',ntbc,totalhr,bcs_avg_tscale
       write(*,*)'totalhr, bcs_avg_tscale, ntbc ',
     $            totalhr, bcs_avg_tscale, ntbc 

      call get_bcstime(kd0,sec0,kdf,secf,ntbc,kdbc,secbc,delttbc)

      write(*,*)' DELTTBC (s), DELTTBC (hr), ntbc', DELTTBC,
     $    DELTTBC/3600.,  ntbc
      write(*,*)kd0, sec0, kdf, secf
      if(DELTTBC.lt.DELT) then 
         write(*,*)'Warning DELTTBC < DELT ',  DELTTBC, DELT
      endif

      write(*,*)'nsteps = ', nsteps
      do k = 1, nsteps
	 call time_basis(kd_saco(k),sec_saco(k),kdbc,secbc,ntbc,
     $        DELTTBC,pkj(k),kjweight(k))
	 if(pkj(k).le.1) then 
	    write(*,*)'set_time: pkj(k) <= 1', k,pkj(k)
	    pkj(k) = 2
	    kjweight(k) = 0.
	 elseif(pkj(k).gt.ntbc) then 
	    write(*,*)'set_time: pkj(k) greater than ntbc', k,pkj(k),ntbc
	    pkj(k) = ntbc
	    kjweight(k) = 1.0 
	 endif
      enddo
      do k = 1, nsteps
	 write(80,*)k,kd_saco(k),sec_saco(k),pkj(k),kjweight(k)
      enddo
      close(80)
      do k = 1, ntbc
	 write(81,*)k,kdbc(k),secbc(k)
      enddo
      close(81)


      RETURN 
      END

