SUBROUTINE SUBSE2(X,N,D1,D1MIN,D1MAX,D2,D2MIN,D2MAX,Y,NY)
C
C     PURPOSE--THIS SUBROUTINE CARRIES OVER INTO Y ALL OBSERVATIONS
C              OF THE SINGLE PRECISION VECTOR X FOR WHICH THE
C              CORRESPONDING ELEMENTS IN THE
C              SINGLE PRECISION VECTOR D1 ARE INSIDE
C              THE CLOSED (INCLUSIVE) INTERVAL
C              DEFINED BY D1MIN AND D1MAX,
C              AND ALSO FOR WHICH THE
C              CORRESPONDING ELEMENTS IN THE
C              SINGLE PRECISION VECTOR D2 ARE INSIDE
C              THE CLOSED (INCLUSIVE) INTERVAL
C              DEFINED BY D2MIN AND D2MAX.
C              NO OBSERVATIONS IN X
C              CORRESPONDING TO ELEMENTS OF D1 OR D2
C              OUTSIDE OF THEIR RESPECTIVE INTERVALS
C              ARE CARRIED OVER INTO Y. 
C              THE INPUT VECTOR X IS ITSELF UNALTERED;
C              THOSE ELEMENTS OF X TO BE RETAINED ARE
C              COPIED OVER INTO THE SINGLE PRECISION
C              OUTPUT VECTOR Y.
C              THUS ALL OBSERVATIONS OF X WHICH
C              CORRESPOND TO ELEMENTS IN D1 WHICH ARE SMALLER
C              THAN D1MIN OR LARGER THAN D1MAX, OR WHICH
C              CORRESPOND TO ELEMENTS IN D2 WHICH ARE SMALLER
C              THAN D2MIN OR LARGER THAN D2MAX,
C              ARE NOT COPIED OVER INTO Y.
C              THE USE OF THIS SUBROUTINE
C              GIVES THE DATA ANALYST THE CAPABILITY TO
C              EASILY EXTRACT SUBSETS OF THE DATA 
C              PRIOR TO DATA ANALYSIS ON EACH SUBSET.
C     INPUT  ARGUMENTS--X      = THE SINGLE PRECISION VECTOR OF
C                                (UNSORTED OR SORTED) OBSERVATIONS.
C                     --N      = THE INTEGER NUMBER OF OBSERVATIONS
C                                IN THE VECTOR X. 
C                     --D1     = A SINGLE PRECISION VECTOR
C                                WHICH (IN CONJUNCTION WITH D2)
C                                'DEFINES' THE VARIOUS
C                                POSSIBLE SUBSETS OF X.
C                     --D1MIN  = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES IN D1 THE LOWER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                SUBSET OF INTEREST TO BE RETAINED.
C                     --D1MAX  = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES IN D1 THE UPPER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                SUBSET OF INTEREST TO BE RETAINED.
C                     --D2     = A SINGLE PRECISION VECTOR
C                                WHICH (IN CONJUNCTION WITH D2)
C                                'DEFINES' THE VARIOUS
C                                POSSIBLE SUBSETS OF X.
C                     --D2MIN  = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES IN D2 THE LOWER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                SUBSET OF INTEREST TO BE RETAINED.
C                     --D2MAX  = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES IN D2 THE UPPER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                SUBSET OF INTEREST TO BE RETAINED.
C     OUTPUT ARGUMENTS--Y      = THE SINGLE PRECISION VECTOR
C                                CONTAINING ONLY THOSE ELEMENTS
C                                OF X SIMULTANEOUSLY CORRESPONDING TO 
C                                VALUES OF THE D1 VECTOR
C                                IN THE INTERVAL D1MIN TO D1MAX
C                                (INCLUSIVE), AND 
C                                VALUES OF THE D2 VECTOR
C                                IN THE INTERVAL D2MIN TO D2MAX
C                                (INCLUSIVE).
C                     --NY     = THE INTEGER NUMBER OF RETAINED
C                                OBSERVATIONS COPIED INTO
C                                THE VECTOR Y.
C     OUTPUT--THE SINGLE PRECISION VECTOR Y
C             INTO WHICH HAVE BEEN COPIED
C             ONLY THOSE VALUES OF X WHICH
C             SIMULTANEOUSLY CORRESPOND TO VALUES 
C             IN THE D1 AND D2 VECTORS INSIDE
C             (INCLUSIVELY) THE RESPECTIVE
C             INTERVALS OF INTEREST, AND
C             THE INTEGER VALUE NY
C             WHICH GIVES THE NUMBER OF 
C             OBSERVATIONS COPIED INTO Y.
C             ALSO, 13 LINES OF SUMMARY INFORMATION
C             WILL BE GENERATED INDICATING
C             1) WHAT THE INTERVAL OF INTEREST WAS
C                IN THE D1 VECTOR;
C             2) WHAT THE INTERVAL OF INTEREST WAS
C                IN THE D2 VECTOR;
C             3) HOW MANY OBSERVATIONS WERE DELETED;
C             4) WHAT THE SAMPLE SIZE OF X WAS (N);
C             5) WHAT THE SAMPLE SIZE OF Y WAS (NY);
C     PRINTING--YES.
C     RESTRICTIONS--THERE IS NO RESTRICTION ON THE MAXIMUM VALUE
C                   OF N FOR THIS SUBROUTINE.
C     OTHER DATAPAC   SUBROUTINES NEEDED--NONE.
C     FORTRAN LIBRARY SUBROUTINES NEEDED--NONE.
C     MODE OF INTERNAL OPERATIONS--SINGLE PRECISION.
C     LANGUAGE--ANSI FORTRAN. 
C     COMMENT--THE INPUT VECTOR X IS NOT ALTERED
C              BY APPLICATION OF THIS SUBROUTINE. 
C              THIS IS A MAJOR DISTINCTION
C              BETWEEN THIS SUBROUTINE AND, SAY,
C              THE SUBSET SUBROUTINE.
C     COMMENT--IN THE END, AFTER THIS SUBROUTINE HAS
C              MADE WHATEVER DELETIONS ARE APPROPRIATE,
C              THE OUTPUT VECTOR Y WILL BE 'PACKED';
C              THAT IS, NO 'HOLES' WILL EXIST IN THE
C              VECTOR Y--ALL OF THE RETAINED ELEMENTS
C              OF Y WILL BE PACKED INTO THE FIRST AVAILABLE 
C              LOCATIONS IN Y, WHILE THE REMAINDER
C              OF THE N LOCATIONS IN Y WILL BE ZERO-FILLED. 
C     COMMENT--ALTHOUGH THERE 
C              MAY BE A CORRESPONDANCE BETWEEN
C              THE ELEMENTS OF THE X AND D1 VECTORS
C              AND ELEMENTS OF THE X AND D2 VECTORS
C              BEFORE APPLICATION OF
C              THIS SUBROUTINE, THERE WILL
C              BE NO CORRESPONDANCE BETWEEN
C              Y AND D1, AND Y AND D2
C              (DUE TO THE PACKING OF
C              THE RETAINED ELEMENTS IN Y)
C              AFTER APPLICATION OF THIS SUBROUTINE.
C     REFERENCES--NONE.
C     WRITTEN BY--JAMES J. FILLIBEN
C                 STATISTICAL ENGINEERING LABORATORY (205.03)
C                 NATIONAL BUREAU OF STANDARDS
C                 WASHINGTON, D. C. 20234
C                 PHONE--301-921-2315
C     ORIGINAL VERSION--FEBRUARY  1976. 
C
C---------------------------------------------------------------------
C
      DIMENSION X(1)
      DIMENSION Y(1)
      DIMENSION D1(1)
      DIMENSION D2(1)
C
      IPR=6
C
C     CHECK THE INPUT ARGUMENTS FOR ERRORS
C
      IF(N.LT.1)GOTO50
      IF(N.EQ.1)GOTO55
      HOLD=X(1)
      DO60I=2,N
      IF(X(I).NE.HOLD)GOTO90
   60 CONTINUE
      WRITE(IPR, 9)HOLD
      GOTO90
   50 WRITE(IPR,15) 
      WRITE(IPR,47)N
      RETURN
   55 WRITE(IPR,18) 
   90 CONTINUE
    9 FORMAT(1H ,108H***** NON-FATAL DIAGNOSTIC--THE FIRST  INPUT ARGUME
     1NT (A VECTOR) TO THE SUBSE2 SUBROUTINE HAS ALL ELEMENTS = ,E15.8,6
     1H *****)
   15 FORMAT(1H , 91H***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO THE
     1 SUBSE2 SUBROUTINE IS NON-POSITIVE *****)
   18 FORMAT(1H ,100H***** NON-FATAL DIAGNOSTIC--THE SECOND INPUT ARGUME
     1NT TO THE SUBSE2 SUBROUTINE HAS THE VALUE 1 *****)
   47 FORMAT(1H , 35H***** THE VALUE OF THE ARGUMENT IS ,I8   ,6H *****)
C
C-----START POINT-----------------------------------------------------
C
      POIN1L=D1MIN
      POIN1U=D1MAX
      IF(D1MIN.GT.D1MAX)POIN1L=D1MAX
      IF(D1MIN.GT.D1MAX)POIN1U=D1MIN
C
      POIN2L=D2MIN
      POIN2U=D2MAX
      IF(D2MIN.GT.D2MAX)POIN2L=D2MAX
      IF(D2MIN.GT.D2MAX)POIN2U=D2MIN
      K=0 
      DO100I=1,N
      IF(D1(I).LT.POIN1L.OR.D1(I).GT.POIN1U)GOTO100
      IF(D2(I).LT.POIN2L.OR.D2(I).GT.POIN2U)GOTO100
      K=K+1
      Y(K)=X(I)
  100 CONTINUE
      NY=K
      NDEL=N-NY
C
C     WRITE OUT A BRIEF SUMMARY
C
      WRITE(IPR,999)
      WRITE(IPR,101)
      WRITE(IPR,111)POIN1L,POIN1U
      WRITE(IPR,112)POIN2L,POIN2U
      WRITE(IPR,160)
      WRITE(IPR,161)
      WRITE(IPR,162)
      WRITE(IPR,163)
      WRITE(IPR,164)
      WRITE(IPR,165)
      WRITE(IPR,166)
      WRITE(IPR,185)N
      WRITE(IPR,190)NY
      WRITE(IPR,195)NDEL
  101 FORMAT(1H ,35HOUTPUT FROM THE SUBSE2 SUBROUTINE--)
  111 FORMAT(1H ,7X,26HD1 LIMITS (INCLUSIVE)--   ,E15.8,5H AND ,E15.8)
  112 FORMAT(1H ,7X,26HD2 LIMITS (INCLUSIVE)--   ,E15.8,5H AND ,E15.8)
  160 FORMAT(1H ,7X,28HONLY THOSE OBSERVATIONS IN X)
  161 FORMAT(1H ,7X,27HWILL BE CARRIED OVER INTO Y)
  162 FORMAT(1H ,7X,40HFOR WHICH THE CORRESPONDING ELEMENTS OF ,
     1               9HD1 AND D2)
  163 FORMAT(1H ,7X,37HARE SIMULTANEOUSLY WITHIN (INCLUSIVE))
  164 FORMAT(1H ,7X,21HEACH SPECIFIED LIMIT.)
  165 FORMAT(1H ,7X,40HNO OBSERVATIONS OUTSIDE OF THIS INTERVAL)
  166 FORMAT(1H ,7X,30HHAVE BEEN CARRIED OVER INTO Y.)
  185 FORMAT(1H ,7X,44HTHE INPUT  NUMBER OF OBSERVATIONS (IN X) IS ,I6)
  190 FORMAT(1H ,7X,44HTHE OUTPUT NUMBER OF OBSERVATIONS (IN Y) IS ,I6)
  195 FORMAT(1H ,7X,44HTHE NUMBER OF OBSERVATIONS DELETED       IS ,I6)
  999 FORMAT(1H )
C
      RETURN
      END