subroutine linear(x, z, km, xeta, zeta, lp1)
!**********************************************************************
!                                                                     *
!  Linear interpolation from km values of z defined at points x to    *
!  lp1 values of zeta defined at points xeta                          *
!                                                                     *
!**********************************************************************
implicit none
	integer :: km, lp1, l, k
	real, dimension (km) :: x, z
	real, dimension (lp1) :: xeta, zeta
   real :: x0, z0, z1, alfa, beta
!----------------------------------------------------------------------

	do l=1,lp1
	  x0=xeta(l)
		  if ( x0 > x(km) ) then
			  alfa = ( x0 - x(km-1) ) / ( x(km) - x(km-1) )
			  beta = 1. - alfa
			  z1 = z(km)
			  z0 = z(km-1)
           zeta(l) = alfa * z1  +  beta * z0
        else 
		     do k=1,km-1
			    if( x(k) <= x0 .and. x0 <= x(k+1) ) then
				    alfa = ( x0 - x(k) ) / ( x(k+1) - x(k) )
				    beta = 1. - alfa
				    z1 = z(k+1)
				    z0 = z(k)
				    exit
             end if
           end do 
           zeta(l) = alfa * z1  +  beta * z0
		  end if
   end do
!----------------------------------------------------------------------
end subroutine linear
