	SUBROUTINE linoext ( Nz, Nzz, z, zz, A, AA )
c
c -------------------------------------------------------
c
c        Linear interpolation, NO extrapolation !!
c        all A for zz that are outside z range are set
c        equal to A(1) or A(last)
c  !!!   arguments should be in increasing order  !!!
c
c -------------------------------------------------------
c
	INTEGER Nz, Nzz
	REAL z(Nz), zz(Nzz), A(Nz), AA(Nzz)
c
	INTEGER i,j
	REAL zz1, z1, z2
c     
        i0 = 1
        DO  i = 1, Nzz
            IF (z(1).GT.zz(i)) THEN 
              AA(i) = A(1) 
              i0 = i0+1
            ELSE
              GOTO 11 
            END IF
        END DO
   11   CONTINUE 
c
        j0=1
        zNz = z(Nz)
	DO i = i0, Nzz
	 zz1 = zz(i)
         IF (zz1.LE.zNz) THEN
	 DO j=j0,Nz-1
	  z1 = z(j)
	  z2 = z(j+1)
	  IF ( (zz1 .GE. z1) .AND. (zz1 .LE. z2) ) THEN  
           AA(i) = A(j) + (A(j+1)-A(j))*(zz1-z1)/(z2-z1)
           goto 1
          else
           j0=j0+1
	  ENDIF
          
	 ENDDO
c
         ELSE

           AA(i) = A(Nz) 

         END IF
 1	 continue

	ENDDO
c
	RETURN
	END    	

c =======================================================

	SUBROUTINE lintext ( Nz, Nzz, z, zz, A, AA )
c
c -------------------------------------------------------
c
c        Linear interpolation AND extrapolation
c  !!!   arguments should be in increasing order  !!!
c
c -------------------------------------------------------
c
	INTEGER Nz, Nzz
	REAL z(Nz), zz(Nzz), A(Nz), AA(Nzz)
c
	INTEGER i,j
	REAL zz1, z1, z2
c     
        i0=1
        DO  i = 1, Nzz
            IF (z(1).GT.zz(i)) THEN 
              AA(i) = A(1) + (A(2)-A(1))*
     *    (zz(i)-z(1))/(z(2)-z(1))
              i0 = i0+1
            ELSE
              GOTO 11 
            END IF
        END DO
   11   CONTINUE 
c
        j0=1
        zNz = z(Nz)
	DO i = i0, Nzz
	 zz1 = zz(i)
         IF (zz1.LE.zNz) THEN
	 DO j=j0,Nz-1
	  z1 = z(j)
	  z2 = z(j+1)
	  IF ( (zz1 .GE. z1) .AND. (zz1 .LE. z2) ) THEN  
           AA(i) = A(j) + (A(j+1)-A(j))*(zz1-z1)/(z2-z1)
           goto 1
          else
           j0=j0+1
	  ENDIF

	 ENDDO
c
         ELSE
c
           AA(i) = A(Nz) + (A(Nz)-A(Nz-1))*
     *   (zz(i)-zNz)/(zNz-z(Nz-1))
c
         END IF
 1	 continue
                  
	ENDDO
c
	RETURN
	END
c ======================================================
	SUBROUTINE linoextdec ( Nz, Nzz, z, zz, A, AA )
c
c -------------------------------------------------------
c
c        Linear interpolation, NO extrapolation !!
c        all A for zz that are outside z range are set
c        equal to A(1) or A(last)
c  !!!   arguments should be in DECreasing order  !!!
c
c -------------------------------------------------------
c
	INTEGER Nz, Nzz
	REAL z(Nz), zz(Nzz), A(Nz), AA(Nzz)
c
	INTEGER i,j
	REAL zz1, z1, z2
c     
        i0 = 1
        DO  i = 1, Nzz
            IF (z(1).LT.zz(i)) THEN 
              AA(i) = A(1) 
              i0 = i0+1
            ELSE
              GOTO 11 
            END IF
        END DO
   11   CONTINUE 
c
        j0=1
        zNz = z(Nz)
	DO i = i0, Nzz
	 zz1 = zz(i)
         IF (zz1.GE.zNz) THEN
	 DO j=j0,Nz-1
	  z1 = z(j)
	  z2 = z(j+1)
	  IF ( (zz1 .LE. z1) .AND. (zz1 .GE. z2) ) THEN  
           AA(i) = A(j+1) + (A(j)-A(j+1))*(zz1-z2)/(z1-z2)
           goto 1
          else
           j0=j0+1
	  ENDIF
          
	 ENDDO
c
         ELSE

           AA(i) = A(Nz) 

         END IF
 1	 continue

	ENDDO
c
	RETURN
	END    	
c ======================================================

	SUBROUTINE linmarkdec ( Nz, Nzz, z, zz, A, AA, wierd )
c
c -------------------------------------------------------
c
c        Linear interpolation, NO extrapolation !!
c        all A for zz that are outside z range are MARKED
c        by `wierd' value
c  !!!   arguments should be in DECreasing order  !!!
c
c -------------------------------------------------------
c
	implicit none

	INTEGER Nz, Nzz
	REAL z(Nz), zz(Nzz), A(Nz), AA(Nzz), wierd
c
	INTEGER i,j, i0, j0
	REAL zz1, z1, z2, zNz
c     
        i0 = 1
        DO  i = 1, Nzz
            IF (z(1).LT.zz(i)) THEN 
              AA(i) = wierd
              i0 = i0+1
            ELSE
              GOTO 11 
            END IF
        END DO
   11   CONTINUE 
c
        j0=1
        zNz = z(Nz)
	DO i = i0, Nzz
	 zz1 = zz(i)
         IF (zz1.GE.zNz) THEN
	 DO j=j0,Nz-1
	  z1 = z(j)
	  z2 = z(j+1)
	  IF ( (zz1 .LE. z1) .AND. (zz1 .GE. z2) ) THEN  
           AA(i) = A(j+1) + (A(j)-A(j+1))*(zz1-z2)/(z1-z2)
           goto 1
          else
           j0=j0+1
	  ENDIF
          
	 ENDDO
c
         ELSE

           AA(i) = wierd

         END IF
 1	 continue

	ENDDO
c
	RETURN
	END    	
c =================================================
c       program testlin

c        parameter (Nz=7,Nzz=10)
c 	real z(Nz),zz(Nzz),A(Nz),AA(Nzz)

c        data z /5.,4.,3.,2.,1.,0.,-1./,
c     $      zz /-2.3,-.4,.1,.3,1.3,1.6,3.5,4.,4.4,5.2/,
c     $      zz /5.2,4.4,4.,3.5,1.6,1.3,.3,.1,-.4,-1.3/
c     $       A /5,4,3,2,1,0,-1/

c	wierd = -1.e5

c        print *, 'A= ', A
c        call linmarkdec( Nz, Nzz, z, zz, A, AA, -1.e5 )
c        print *,'AA= ', AA
c        call linoextdec( Nz, Nzz, z, zz, A, AA )
c        print *,'AA= ', AA

c	do i=1,7
c	   if ( nint(AA(i)) .eq. nint(wierd) ) print *, 'wierd'
c        enddo

c        stop
c        end
