      SUBROUTINE SUBSET(X,N,D,DMIN,DMAX,NEWN)
C
C     PURPOSE--THIS SUBROUTINE RETAINS ALL OBSERVATIONS IN THE
C              SINGLE PRECISION VECTOR X FOR WHICH THE
C              CORRESPONDING ELEMENTS IN THE
C              SINGLE PRECISION VECTOR D ARE INSIDE
C              THE CLOSED (INCLUSIVE) INTERVAL
C              DEFINED BY DMIN AND DMAX,
C              WHILE DELETING ALL OBSERVATIONS IN X
C              CORRESPONDING TO ELEMENTS OF D
C              OUTSIDE OF THIS INTERVAL.
C              THUS ALL OBSERVATIONS IN X WHICH
C              CORRESPOND TO ELEMENTS IN D WHICH ARE SMALLER
C              THAN DMIN OR LARGER THAN DMAX ARE DELETED FROM X.
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                     --D      = THE SINGLE PRECISION VECTOR
C                                WHICH 'DEFINES' THE VARIOUS
C                                POSSIBLE SUBSETS OF X.
C                     --DMIN   = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES THE LOWER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                SUBSET OF INTEREST TO BE RETAINED.
C                     --DMAX   = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES THE UPPER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                SUBSET OF INTEREST TO BE RETAINED.
C     OUTPUT ARGUMENTS--NEWN   = THE INTEGER NUMBER OF OBSERVATIONS
C                                REMAINING (RETAINED) IN X AFTER ALL
C                                OF THE OBSERVATIONS IN X
C                                HAVE BEEN DELETED WHICH
C                                CORRESPOND TO VALUES IN THE
C                                VECTOR D OUTSIDE THE
C                                INTERVAL OF INTEREST.
C     OUTPUT--THE SINGLE PRECISION VECTOR X
C             IN WHICH ONLY THOSE VALUES
C             HAVE BEEN RETAINED WHICH
C             CORRESPOND TO VALUES
C             IN THE D VECTOR INSIDE
C             (INCLUSIVELY) THE INTERVAL OF
C             INTEREST, AND
C             THE INTEGER VALUE NEWN
C             WHICH GIVES THE NUMBER OF 
C             OBSERVATIONS RETAINED IN X.
C             ALSO, 12 LINES OF SUMMARY INFORMATION
C             WILL BE GENERATED INDICATING
C             1) WHAT THE INTERVAL OF INTEREST WAS
C                IN THE D VECTOR;
C             2) HOW MANY OBSERVATIONS WERE DELETED;
C             3) WHAT THE OLD (ORIGINAL) SAMPLE SIZE WAS (N);
C             4) WHAT THE NEW SAMPLE SIZE IS (NEWN).
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--IN THE END, AFTER THIS SUBROUTINE HAS
C              MADE WHATEVER DELETIONS ARE APPROPRIATE,
C              THE OUTPUT VECTOR X WILL BE 'PACKED';
C              THAT IS, NO 'HOLES' WILL EXIST IN THE
C              VECTOR X--ALL OF THE RETAINED ELEMENTS
C              OF X WILL BE PACKED INTO THE FIRST AVAILABLE 
C              LOCATIONS IN X, WHILE THE REMAINDER
C              OF THE N LOCATIONS IN X WILL BE ZERO-FILLED. 
C     COMMENT--CAUTION IS TO BE EXERCISED IN
C              USING THIS SUBROUTINE FOR THE
C              FOLLOWING REASON--THE INPUT VECTOR X
C              IS IRREVOCABLY ALTERED BY APPLICATION
C              OF THIS SUBROUTINE.  ALTHOUGH THERE
C              MAY BE A CORRESPONDANCE BETWEEN THE
C              ELEMENTS OF THE X AND D VECTORS
C              BEFORE APPLICATION OF
C              THIS SUBROUTINE, THERE WILL
C              BE NO CORRESPONDANCE BETWEEN
C              X AND D (DUE TO THE PACKING OF
C              THE RETAINED ELEMENTS OF X)
C              AFTER APPLICATION OF THIS SUBROUTINE.
C              TO SUCCESSIVELY EXTRACT EACH POSSIBLE
C              SUBSET OF X, IT IS
C              RECOMMENDED THAT THE
C              ANALYST USE THE      SUBSA2
C              SUBROUTINE WHICH LEAVES
C              THE ORIGINAL INPUT VECTOR X
C              UNALTERED AND OUTPUTS THE
C              RETAINED ELEMENTS IN A
C              SEPARATE SECOND VECTOR Y.
C     COMMENT--IN THE MAIN (CALLING) ROUTINE, IT IS
C              PERMISSABLE (IF THE ANALYST SO DESIRES)
C              TO USE THE SAME VARIABLE NAME
C              IN THE SIXTH ARGUMENT AS USED IN THE SECOND
C              ARGUMENT IN THE CALLING SEQUENCE TO THIS
C              SUBSET SUBROUTINE--NO CONFLICT WILL RESULT
C              IN THE INTERNAL OPERATION OF THE     SUBSET
C              SUBROUTINE.  FOR EXAMPLE, IT IS PERMISSIBLE
C              TO HAVE     CALL SUBSET(X,N,D,0.5,1.5,N)
C              IN WHICH THE VARIABLE NAME      N    IS USED 
C              AS BOTH THE SECOND AND SIXTH ARGUMENTS.
C     COMMENT--THIS IS ONE OF THE FEW SUBROUTINES IN DATAPAC
C              IN WHICH THE INPUT VECTOR X IS ALTERED.
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--NOVEMBER  1975. 
C     UPDATED         --FEBRUARY  1976. 
C
C---------------------------------------------------------------------
C
      DIMENSION X(1)
      DIMENSION D(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 SUBSET SUBROUTINE HAS ALL ELEMENTS = ,E15.8,6
     1H *****)
   15 FORMAT(1H , 91H***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO THE
     1 SUBSET SUBROUTINE IS NON-POSITIVE *****)
   18 FORMAT(1H ,100H***** NON-FATAL DIAGNOSTIC--THE SECOND INPUT ARGUME
     1NT TO THE SUBSET SUBROUTINE HAS THE VALUE 1 *****)
   47 FORMAT(1H , 35H***** THE VALUE OF THE ARGUMENT IS ,I8   ,6H *****)
C
C-----START POINT-----------------------------------------------------
C
      POINTL=DMIN
      POINTU=DMAX
      IF(DMIN.GT.DMAX)POINTL=DMAX
      IF(DMIN.GT.DMAX)POINTU=DMIN
C
      NOLD=N
      K=0 
      DO100I=1,NOLD 
      IF(D(I).LT.POINTL.OR.D(I).GT.POINTU)GOTO100 
      K=K+1
      X(K)=X(I)
  100 CONTINUE
      NEWN=K
      NDEL=NOLD-NEWN
C
      NEWNP1=NEWN+1 
      IF(NEWNP1.GT.NOLD)GOTO250
      DO200I=NEWNP1,NOLD
      X(I)=0.0
  200 CONTINUE
  250 CONTINUE
C
C     WRITE OUT A BRIEF SUMMARY
C
      WRITE(IPR,999)
      WRITE(IPR,101)
      WRITE(IPR,105)POINTL,POINTU
      WRITE(IPR,110)
      WRITE(IPR,111)
      WRITE(IPR,112)
      WRITE(IPR,113)
      WRITE(IPR,114)
      WRITE(IPR,115)
      WRITE(IPR,116)
      WRITE(IPR,120)NOLD
      WRITE(IPR,125)NEWN
      WRITE(IPR,130)NDEL
  101 FORMAT(1H ,35HOUTPUT FROM THE SUBSET SUBROUTINE--)
  105 FORMAT(1H ,7X,26HD  LIMITS (INCLUSIVE)--   ,E15.8,5H AND ,E15.8)
  110 FORMAT(1H ,7X,28HONLY THOSE OBSERVATIONS IN X)
  111 FORMAT(1H ,7X,16HWILL BE RETAINED)
  112 FORMAT(1H ,7X,40HFOR WHICH THECORRESPONDING ELEMENTS OF D)
  113 FORMAT(1H ,7X,22HARE WITHIN (INCLUSIVE))
  114 FORMAT(1H ,7X,21HTHE SPECIFIED LIMITS.)
  115 FORMAT(1H ,7X,41HALL OBSERVATIONS OUTSIDE OF THIS INTERVAL)
  116 FORMAT(1H ,7X,23HHAVE BEEN DELETED IN X.)
  120 FORMAT(1H ,7X,44HTHE INPUT  NUMBER OF OBSERVATIONS (IN X) IS ,I6)
  125 FORMAT(1H ,7X,44HTHE OUTPUT NUMBER OF OBSERVATIONS (IN X) IS ,I6)
  130 FORMAT(1H ,7X,44HTHE NUMBER OF OBSERVATIONS DELETED       IS ,I6)
  999 FORMAT(1H )
C
      RETURN
      END 
