      subroutine d_time(tobs,uobs,tcalc,interfce,iflag)
c                 
c     calculate the time perturbation associated with a known
c     slowness perturbation 
c                 
      include 'ray.par'
      include 'ray.com'
c
      real length
      integer iback(nray),ci(nray,3),iipt(nray)
c
      length=0.
      iflag=0
      nipts=0
      ixl=0
      iyl=0
      izl=0
      nback=0
c
c     resample raypath onto inverse grid
c
      do 50 i=1,npts-1
         xmid=(xray(i)+xray(i+1))/2.
         ymid=(yray(i)+yray(i+1))/2.
         zmid=(zray(i)+zray(i+1))/2.
c
         ix=min(int((xmid-xmin)/xii+1),inx)
         if(ny.ne.1) then
           iy=min(int((ymid-ymin)/yii+1),iny)
         else
           iy=1
         end if
         iz=min(int((zmid-zmin)/zii+1),inz)
c
         if(ix.ne.ixl.or.iy.ne.iyl.or.iz.ne.izl) then
           nipts=nipts+1
           lseg(nipts)=sqrt((xray(i)-xray(i+1))**2+
     +            (yray(i)-yray(i+1))**2+(zray(i)-zray(i+1))**2)
           ixl=ix
           iyl=iy
           izl=iz
           ci(nipts,1)=ix
           ci(nipts,2)=iy
           ci(nipts,3)=iz
         else
           lseg(nipts)=lseg(nipts)+sqrt((xray(i)-xray(i+1))**2+
     +           (yray(i)-yray(i+1))**2+(zray(i)-zray(i+1))**2)
         end if
         iipt(i)=nipts
50    continue
c
c     determine effective length of raypath
c
      if(interfce.eq.0) then
        do 10 i=1,nipts
           length=length+lseg(i)
10      continue
      else
        do 31 i=1,nipts
31         iback(i)=1
c
        do 30 i=1,npts-1
           z1=bndinterp(xray(i),yray(i),cell(i,1),cell(i,2),1)
           if(z1.gt.zray(i)) iback(iipt(i))=0
           z2=bndinterp(xray(i+1),yray(i+1),cell(i,1),cell(i,2),1)
           if(z2.gt.zray(i+1)) iback(iipt(i))=0
30      continue
c
        do 70 i=1,nipts
           if(iback(i).ne.0) length=length+lseg(i)
70      continue
      end if
c
      if(length.eq.0.) then
        iflag=1
        return
      end if
c
      deltat=tobs-tcalc
      if(iunc.eq.0) then
        unc=1.
      else
        unc=uobs
      end if
      sum=0.
c
      if(interfce.eq.0) then
        do 20 i=1,nipts
           sum=sum+pert1(ci(i,1),ci(i,2),ci(i,3))*lseg(i)
20      continue
      else
        do 40 i=1,nipts
           if(iback(i).eq.1) then
             sum=sum+pert1(ci(i,1),ci(i,2),ci(i,3))*lseg(i)
           end if
40      continue
      end if
c
      write(28) deltat/uobs,sum/uobs
c
      return
      end