      SUBROUTINE AVGIN(DEP,GRAD,FINT,NP,MX,H,AVG,LH)
C
C     THIS SUBROUTINE COMPUTES THE AVERAGE AVG OF THE (PIECEWISE 
C     LINEAR) SQUARE OF THE INDEX OF REFRACTION ON THE INTERVAL 
C     0 TO H. IT ALSO RETURNS THE INDEX LH OF THE INTERVAL IN THE 
C     SOUND VELOCITY PROFILE WHICH CONTAINS H. IF H .GT. DEP(NP)
C     THEN LH=NP AND THE SQUARE OF THE INDEX OF REFRACTION IS
C     LINEARLY EXTRAPOLATED TO H TO OBTAIN THE AVERAGE.      
C
      IMPLICIT REAL*8 (A-H,O-Z)
      COMMON /BLKEVN/ HB,CW,CB,FKW,FKB,ROHW,ROHB,ATEN
C
      DIMENSION DEP(MX),GRAD(MX),FINT(MX)
C
      AVG=0.0
C
      DO 100 LH=1,NP-1
      IF(DEP(LH+1) .GE. H) GO TO 200
  100 CONTINUE
C
C     IF H .GT. DEP(NP), EXTRAPOLATION WILL BE DONE
C
      LH=NP
  200 CONTINUE
C
      IF(LH .GT. 1)   THEN
C
      DO 300 I=1,LH-1
      DD=DEP(I+1)-DEP(I)
      DDSD2=(DEP(I+1)**2-DEP(I)**2)/2.0
      AVG=AVG+GRAD(I)*DDSD2+FINT(I)*DD
  300 CONTINUE
C
      END IF
C
      DD=H-DEP(LH)
      DDSD2=(H**2-DEP(LH)**2)/2.0
      AVG=AVG+GRAD(LH)*DDSD2+FINT(LH)*DD
C      
      AVG=AVG/H
C
      RETURN
      END
