      LOGICAL FUNCTION INTRVL(X,N,XVALUE,INDEX)
C
C**********************************************************************
C
C   NAME- INTRVL
C
C   LANGUAGE- FORTRAN 77     TYPE- FUNCTION        COMPUTER- CRAY     
C
C   VERSION- 1.0    DATE- 01/20/89   PROGRAMMER- KV JARVA (SMSRC)
C   VERSION- 3.0    DATE- 05/28/96   PROGRAMMER- A POZDNYAK (FDC)
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT AND
C      R*4 WITH REAL.
C   VERSION- 1.3    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function 
C 
C   FUNCTION- THIS IS TO SCAN MONOTONICALLY INCREASING OR DECREASING
C        LIST(X) FOR TWO SUCCESSIVE ELEMENTS THAT BOUND THE GIVEN
C        XVALUE.  IT RETURNS THE FIRST SUB-INTERVAL INDEX IN
C        (INDEX) ; I.E., IF XVALUE IS BOUNDED BY THE (I)TH AND THE
C        (I+1)TH ENTRIES OF LIST (X), (INDEX) IS RETURNED AS I.
C        INTERVALS CONTAINING MISSIG DATA ARE SKIPPED AND THE
C        SEARCH IS CONTINUED.  IF GOOD DATA BOUNDING XVALUE CANNOT
C        BE LOCATED, INTRVL WILL BE RETURNED AS FALSE AND (INDEX)
C        WILL BE RETURNED AS 0.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     X(N)              REAL  I   A LIST OF LENGTH N OF SUCCESSIVE
C                                 ELEMENTS IN ASCENDING OR DECREASING
C                                 ORDER WITH EXCEPTION OF MISSING DATA
C     N                 INT   I   TOTAL NUMBER OF LEVELS
C     XVALUE            REAL  I   THE VALUE TO BE LOCATED
C     INDEX             INT   O   THE SMALLER INDEX NUMBER OF THE
C                                 INTERVAL CONTAINING THE VALUE
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     LO                INT   THE INDEX POINTING TO THE FIRST ELEMENT
C                             OF SUB-INTERVAL UNDER CONSIDERATION.
C     HI                INT   THE INDEX POINTING TO THE LAST ELEMENT
C                             OF SUB-INTERVAL UNDER CONSIDERATION.
C     MID               INT   THE INDEX MIDWAY BETWEEN LOW AND HI
C     MIDLO             INT   INDEX USED TO TEST WHETHER THE SUB-
C                             INTERVAL CONTAINING XVALUE IS IN THE
C                             FIRST HALF OF THE LIST (X).
C     MIDHI             INT   INDEX USED WHEN A SUBINTERVAL BOUNDING
C                             XBALUE HASN'T BEEN FOUND IN THE FIRST
C                             HALF OF LIST (X).
C     RANGE             REAL  DIFFERENCE BETWEEN THE LOWEST AND HIGHEST
C                             VALUES IN LIST (X).
C     EPSLON            REAL  VALUE USED FOR TESTING WHETHER TWO VALUES
C                             ARE APPROXIMATELY EQUAL.
C
C
C     INTRINSIC FUNCTIONS USED: AMIN1, AMAX1
C
C**********************************************************************
C
      INTEGER   LO,HI,N,INDEX,MID,MIDHI,MIDLO
      REAL      X(N),XVALUE,RANGE,EPSLON
      LOGICAL   ISR4SPV
C
      INCLUDE'SPVAL'
C
C       INITIALIZE LOW AND HI INDEX NUMBERS
C
      LO = 1
      HI = N
C
C       INITIALIZE LOGICAL TO .FALSE.
C
      INTRVL = .FALSE.
C
      IF (N .LT.2) RETURN
C
      RANGE = X(N) - X(1)
      IF (RANGE .EQ. 0.0) RETURN
C
C       CALCULATE THE VALUE FOR APPROXIMATE EQUAL TEST.
C       IT WILL HAVE NEGATIVE VALUE IN DECREASING ORDER LIST,
C       AND POSITIVE VALUE IN INCREASING ORDER LIST.
C
      EPSLON = 0.0001 * RANGE
C
C        THE XVALUE IS NOT BOUNDED BY ANY SUB-INTERVAL OF LIST(X)
C         NOTE: THE LOWEST AND HIGHEST BOUNDARIES IN THE LIST, X(1)
C               X(N), IS EXTENDED SLIGHTLY BY THE VALUE,EPSLON.
C
C
      IF((XVALUE .GT. (AMAX1(X(1)-EPSLON,X(N)+EPSLON))) .OR.
     1   (XVALUE .LT. (AMIN1(X(1)-EPSLON,X(N)+EPSLON)))) THEN
         INTRVL = .FALSE.
         INDEX = 0
         RETURN
      ENDIF
C
C        XVALUE IS BOUNDED BETWEEN THE LOWEST(X(1)) AND
C        THE EXTENDED VALUE BY EPSLON.
C
      IF((XVALUE .GE. (AMIN1(X(1),X(1)-EPSLON))) .AND.
     1   (XVALUE .LE. (AMAX1(X(1),X(1)-EPSLON)))) THEN
         INTRVL = .TRUE.
         INDEX= 1
         RETURN
      ENDIF
C
C        XVALUE IS BOUNDED BETWEEN HIGHEST(X(N)) AND
C        THE EXTENDED VALUE BY EPSLON.
C
      IF((XVALUE .GE. (AMIN1(X(N),X(N)+EPSLON))) .AND.
     1   (XVALUE .LE. (AMAX1(X(N),X(N)+EPSLON)))) THEN
         INTRVL = .TRUE.
         INDEX= N-1
         RETURN
      ENDIF
C
C      SKIP ANY MISSING DATA AT EITHER END OF LIST.
C
      IF((LO .LE. HI) .AND. (ISR4SPV(X(LO))))  LO = LO +1
      IF((HI .GE. 1) .AND. (ISR4SPV(X(HI))))  HI = HI -1
C
C
      IF (LO .GE. HI) THEN
C
C           THERE ARE NO SUB-INTERVALS IN THIS LIST
C
          INDEX = 0
          INTRVL = .FALSE.
          RETURN
      ENDIF
C
C     WHILE ((HI - LO) .NE. 1 ) DO
C
   10 IF ((HI - LO) .NE. 1) THEN
C
C      SET MID TO HALF WAY BETWEEN HI AND LO
C
         MID = (HI + LO ) / 2
         MIDLO = MID
C
C          WHILE (X(MIDLO) = MISSING ) DO
C
   20    IF (ISR4SPV(X(MIDLO))) THEN
            MIDLO = MIDLO - 1
            GO TO 20
         ENDIF
         IF ((XVALUE .LE. AMAX1(X(LO),X(MIDLO))) .AND.
     1       (XVALUE .GE. AMIN1(X(LO),X(MIDLO)))) THEN
             HI = MIDLO
             GO TO 40
         ENDIF
C
C           SET MIDHI TO MID
C
         MIDHI = MID
C
C       WHILE (X(MIDHI)  = MISSING VALUE )  DO
C
   30    IF (ISR4SPV(X(MIDHI)))  THEN
             MIDHI = MIDHI +1
             GO TO 30
         ENDIF
         IF ((XVALUE .LE. AMAX1(X(MIDHI),X(HI))) .AND.
     1       (XVALUE .GE. AMIN1(X(MIDHI),X(HI)))) THEN
             LO = MIDHI
             GO TO 40
         ELSE
             INDEX = LO
             INTRVL = .FALSE.
             RETURN
         ENDIF
   40 CONTINUE
      GO TO 10
      ENDIF
C
C       ENDWHILE ((HI-LO).NE.1)
C
      INDEX = LO
      INTRVL = .TRUE.
      RETURN
      END
