      SUBROUTINE AVGIN(DEP,CGRAD,CINT,NP,MX,H,AVG,LH)
C
C     THIS SUBROUTINE COMPUTES THE AVERAGE AVG OF THE REAL PART OF
C     THE PIECEWISE LINEAR SQUARE OF THE INDEX OF REFRACTION ON THE 
C     INTERVAL 0 TO H. IT ALSO RETURNS THE INDEX LH OF THE INTERVAL 
C     IN THE SOUND VELOCITY PROFILE WHICH CONTAINS H. IF H .GT. 
C     DEP(NP) THEN LH=NP AND THE REAL PART OF THE SQUARE OF THE 
C     INDEX OF REFRACTION IS LINEARLY EXTRAPOLATED TO H TO OBTAIN 
C     THE AVERAGE.      
C
      IMPLICIT REAL*8 (A-H,O-Z)
C
      DIMENSION DEP(MX)
C      REAL*8 DREAL
      COMPLEX*16 CGRAD(MX),CINT(MX)
C
      AVG=0.0D0
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+DREAL(CGRAD(I))*DDSD2+DREAL(CINT(I))*DD
  300 CONTINUE
C
      END IF
C
      DD=H-DEP(LH)
      DDSD2=(H**2-DEP(LH)**2)/2.0
      AVG=AVG+DREAL(CGRAD(LH))*DDSD2+DREAL(CINT(LH))*DD
C      
      AVG=AVG/H
C
      RETURN
      END
