       subroutine cvals2(nx,ny,tabx,taby,tabc,xi,yi,ci,cxi,cyi,
     & cxxi,cyyi,cxyi)

c***********************************************************************
c      CVALS2.FOR
c      (2D) Interpolation of sound speed and its derivatives.
c      Gambelas, Wed Jun 24 15:09:32 WEST 2015
c      Written by Orlando Camargo Rodriguez
c****&******************************************************************

c      Include global (common) variables:

       include 'globli.for'

c      Define local variables:
       
       integer*4 i, j, nx, ny, il

       real*8 tabc(nc02d,nc02d)
       real*8 fc(4,4)
       real*8 fl(2,2)
       
       real*8 tabx(nx),taby(ny)
       real*8 xc(4),yc(4)
       real*8 xl(2),yl(2)
       
       real*8   xi,  yi, ci
       real*8  cxi, cyi
       real*8 cxxi,cyyi,cxyi

c-----------------------------------------------------------------------

       il   = 0
       cxxi = 0.0 
       cyyi = 0.0
       
c-----------------------------------------------------------------------

       if (xi.le.tabx(2)) then
          i  = 1
	  il = 1
       else if (xi.ge.tabx(nx-1)) then
          i = nx - 1
	  il = 1 
       else 
          call brcket(nx,tabx,xi,i)
       end if
       
c-----------------------------------------------------------------------

       if (yi.le.taby(2)) then
          j  = 1
	  il = 1
       else if (yi.ge.taby(ny-1)) then
          j = ny - 1
	  il = 1
       else 
          call brcket(ny,taby,yi,j)
       end if

c-----------------------------------------------------------------------
       
       if (il.eq.1) then
       
       xl(1) = tabx( i )
       xl(2) = tabx(i+1)
       
       yl(1) = taby( j )
       yl(2) = taby(j+1)
       
       fl(1,1) = tabc( j , i ) 
       fl(1,2) = tabc( j ,i+1)
       fl(2,1) = tabc(j+1, i )
       fl(2,2) = tabc(j+1,i+1)
       
       call blii2d(xl,yl,fl,xi,yi,ci,cxi,cyi,cxyi)
       
       else
       
       xc(1) = tabx(i-1)
       xc(2) = tabx( i )
       xc(3) = tabx(i+1)
       xc(4) = tabx(i+2)
       
       yc(1) = taby(j-1)
       yc(2) = taby( j )
       yc(3) = taby(j+1)
       yc(4) = taby(j+2)
       
       fc(1,1) = tabc(j-1,i-1)
       fc(1,2) = tabc(j-1, i )
       fc(1,3) = tabc(j-1,i+1)
       fc(1,4) = tabc(j-1,i+2)
       
       fc(2,1) = tabc( j ,i-1)
       fc(2,2) = tabc( j , i )
       fc(2,3) = tabc( j ,i+1)
       fc(2,4) = tabc( j ,i+2)
       
       fc(3,1) = tabc(j+1,i-1)
       fc(3,2) = tabc(j+1, i )
       fc(3,3) = tabc(j+1,i+1)
       fc(3,4) = tabc(j+1,i+2)
       
       fc(4,1) = tabc(j+2,i-1)
       fc(4,2) = tabc(j+2, i )
       fc(4,3) = tabc(j+2,i+1)
       fc(4,4) = tabc(j+2,i+2)
       
       call bcui2d(xc,yc,fc,xi,yi,ci,cxi,cyi,cxxi,cyyi,cxyi)
       
       end if
       
c***********************************************************************
c     Back to main:
c***********************************************************************
       
       return
       	
       end

