SUBROUTINE REPLAC(X,N,XMIN,XMAX,XNEW)
C
C     PURPOSE--THIS SUBROUTINE REPLACES (WITH THE VALUE XNEW)
C              ALL OBSERVATIONS IN THE
C              SINGLE PRECISION VECTOR X WHICH ARE INSIDE
C              THE CLOSED (INCLUSIVE) INTERVAL
C              DEFINED BY XMIN AND XMAX.
C              ALL OBSERVATIONS OUTSIDE OF
C              THIS INTERVAL ARE LEFT UNCHANGED.
C              THUS ALL OBSERVATIONS IN X WHICH ARE
C              EQUAL TO OR LARGER THAN XMIN AND
C              EQUAL TO OR SMALLER THAN XMAX,
C              WILL BE REPLACED BY XNEW.
C              THIS SUBROUTINE (AND THE 
C              RETAIN AND DELETE SUBROUTINES)
C              GIVES THE DATA ANALYST THE ABILITY TO
C              EASILY 'CLEAN UP' A DATA SET WHICH HAS
C              MISSING AND/OR OUTLYING OBSERVATIONS
C              SO THAT A MORE APPROPRIATE SUBSEQUENT
C              DATA ANALYSIS MAY BE PERFORMED.
C              FOR EXAMPLE, REPLACEMENT OF AN OUTLIER WITH
C              A MORE APPROPRIATE VALUE CAN EASILY
C              BE DONE BY THIS SUBROUTINE.
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                     --XMIN   = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES THE LOWER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                INTERVAL OF INTEREST FOR REPLACEMENT.
C                     --XMAX   = THE SINGLE PRECISION VALUE 
C                                WHICH DEFINES THE UPPER LIMIT
C                                (INCLUSIVELY) OF THE PARTICULAR
C                                INTERVAL OF INTEREST FOR REPLACEMENT.
C                     --XNEW   = THE SINGLE PRECISION VALUE 
C                                WITH WHICH ALL OF THE
C                                OBSERVATIONS IN THE INTERVAL
C                                OF INTEREST
C                                WILL BE REPLACED.
C     OUTPUT--THE SINGLE PRECISION VECTOR X
C             IN WHICH ONLY THOSE VALUES INSIDE
C             (INCLUSIVELY) THE INTERVAL OF INTEREST
C             HAVE BEEN REPLACED BY XNEW.
C             ALSO, 6 LINES OF SUMMARY INFORMATION
C             WILL BE GENERATED INDICATING
C             1) WHAT THE INTERVAL OF INTEREST WAS;
C             2) WHAT THE REPLACEMENT VALUE WAS;
C             3) HOW MANY OBSERVATIONS WERE REPLACED;
C             4) WHAT THE SAMPLE SIZE WAS (N);
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--THIS SUBROUTINE MAY BE USEFULLY EMPLOYED
C              IN CONJUNCTION WITH THE DATAPAC
C              PLOTTING SUBROUTINES INASMUCH
C              AS THE LATTER HAVE BEEN
C              SET UP WITH THE CONVENTION
C              THAT ALL VALUES IN THE VERTICAL AXIS
C              VECTOR OR HORIZONTAL AXIS VECTOR
C              WHICH ARE EQUAL TO OR IN EXCESS OF 10.0**10
C              WILL BE AUTOMATICALLY IGNORED
C              IN THE PLOT (THAT IS, NOT PLOTTED).
C              THIS CONVENTION GREATLY SIMPLIFIES THE PROBLEM
C              OF PLOTTING WHEN SOME ELEMENTS IN THE VERTICAL
C              OR HORIZONTAL AXIS VECTORS
C              ARE 'MISSING DATA', OR WHEN WE PURPOSELY
C              WANT TO IGNORE CERTAIN ELEMENTS IN THESE VECTORS
C              FOR PLOTTING PURPOSES (THAT IS, WE DO NOT
C              WANT CERTAIN ELEMENTS TO BE PLOTTED).
C              TO CAUSE SPECIFIC ELEMENTS IN THE VERTICAL
C              OR HORIZONTAL AXIS VECTORS TO BE
C              IGNORED, WE REPLACE THE ELEMENTS BEFOREHAND
C              (BY USE OF THE   REPLAC   SUBROUTINE)
C              BY SOME LARGE VALUE (LIKE, SAY, 10.0**10) AND
C              THEY WILL SUBSEQUENTLY BE IGNORED IN THE PLOTTING
C              SUBROUTINES.
C     COMMENT--THIS IS ONE OF THE FEW SUBRUTINES 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  1972. 
C     UPDATED         --NOVEMBER  1975. 
C
C---------------------------------------------------------------------
C
      DIMENSION X(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 REPLAC SUBROUTINE HAS ALL ELEMENTS = ,E15.8,6
     1H *****)
   15 FORMAT(1H , 91H***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO THE
     1 REPLAC SUBROUTINE IS NON-POSITIVE *****)
   18 FORMAT(1H ,100H***** NON-FATAL DIAGNOSTIC--THE SECOND INPUT ARGUME
     1NT TO THE REPLAC SUBROUTINE HAS THE VALUE 1 *****)
   47 FORMAT(1H , 35H***** THE VALUE OF THE ARGUMENT IS ,I8   ,6H *****)
C
C-----START POINT-----------------------------------------------------
C
      POINTL=XMIN
      POINTU=XMAX
      IF(XMIN.GT.XMAX)POINTL=XMAX
      IF(XMIN.GT.XMAX)POINTU=XMIN
C
      K=0 
      DO100I=1,N
      IF(X(I).LT.POINTL.OR.X(I).GT.POINTU)GOTO100 
      K=K+1
      X(I)=XNEW
  100 CONTINUE
      NDEL=N-K
C
C     WRITE OUT A BRIEF SUMMARY
C
      WRITE(IPR,999)
      WRITE(IPR,101)
      WRITE(IPR,105)POINTL,POINTU
      WRITE(IPR,106)
      WRITE(IPR,107)
      WRITE(IPR,108)
      WRITE(IPR,109)XNEW
      WRITE(IPR,110)N
      WRITE(IPR,115)K
      WRITE(IPR,120)NDEL
  101 FORMAT(1H ,35HOUTPUT FROM THE REPLAC SUBROUTINE--)
  105 FORMAT(1H ,7X,26HONLY OBSERVATIONS BETWEEN ,E15.8,5H AND ,E15.8)
  106 FORMAT(1H ,7X,31H(INCLUSIVE) HAVE BEEN REPLACED.)
  107 FORMAT(1H ,7X,41HALL OBSERVATIONS OUTSIDE OF THIS INTERVAL)
  108 FORMAT(1H ,7X,25HHAVE BEEN LEFT UNCHANGED.) 
  109 FORMAT(1H ,7X,25HTHE REPLACEMENT VALUE IS ,E15.8)
  110 FORMAT(1H ,7X,40HTHE INPUT  NUMBER OF OBSERVATIONS    IS ,I6)
  115 FORMAT(1H ,7X,40HTHE NUMBER OF OBSERVATIONS REPLACED  IS ,I6)
  120 FORMAT(1H ,7X,40HTHE NUMBER OF OBSERVATIONS UNCHANGED IS ,I6)
  999 FORMAT(1H )
C
      RETURN
      END