       subroutine raybi(nr,tabr,tabz,itype,la,lb,li)
	  
c***********************************************************************
c      RAY/Boundary Intersection 
c      Written by Orlando Camargo Rodriguez
c      orodrig@ualg.pt
c      Universidade do Algarve
c      Physics Department
c      Signal Processing Laboratory
c      Faro, 29/08/2009 at 20:00
c****&******************************************************************

c      Include global (common) variables:

       include 'globli.for'
       
c      Define local variables:

       character*2 itype
       
       integer*4 i,n,nr
       
       real*8 tabr(nr),tabz(nr)
       real*8 rl(101),zl(101),dz(101)
       real*8 la(2),lb(2),li(2),q1(2),q2(2)
       real*8 taub(2),normal(2)
       real*8 ra,za,rb,zb,ri,zi
       
c***********************************************************************
       
       ra = la(1)
       za = la(2)
       
       rb = lb(1)
       zb = lb(2)
       
       if (itype.eq.'FL') then
       
       li(2) = tabz(1)
       li(1) = (rb-ra)/(zb-za)*(li(2)-za) + ra
       
       elseif (itype.eq.'SL') then
       
       q1(1) = tabr(1)
       q1(2) = tabz(1)
       q2(1) = tabr(2)
       q2(2) = tabz(2)
      
       call llintr(la,lb,q1,q2,i,ri,zi)
       
       li(1) = ri
       li(2) = zi

       else 
       
       n = 101 
       
       call linspc(n,ra,rb,rl)
       call linspc(n,za,zb,zl)
       
       do i = 1,n
          
          ri = rl(i)
          
          call bdryi(nr,tabr,tabz,itype,ri,zi,taub,normal)
          
          dz(i) = zl(i) - zi


       end do
       
       i = 1 
       
500    if (dz(1)*dz(i).gt.0.0) then 
       
       i = i + 1 

       goto 500
      
       end if
       
       ri = rl(i-1) - dz(i-1)*(rl(i)-rl(i-1))/(dz(i)-dz(i-1))

       li(1) = ri
       li(2) = (ri-ra)/(rb-ra)*(zb-za) + za
       
       if (abs(li(2)).lt.1.0e-12) then
       li(2) = 0.0
       end if
       
       end if 
       
c***********************************************************************
c      Back to main:
c***********************************************************************
       
       return
       
       end
