      SUBROUTINE LNOFF (N,DATA,RMS)
************************************************************************
*     remove slope between start and end points of data in array
* inputs :
*     N       integer*2       last valid point in array
*     DATA    real*4          data array
* outputs :
*     DATA    real*4          data array with slope removed
*     RMS     real*4          RMS scatter in first ten and last ten points
*
      IMPLICIT REAL*8 (A-H,O-Z)
      REAL*4     DATA(N), RMS
      INTEGER*2  ISTART(2), IEND(2)
*
*     use first ten and last ten points of data for slope
*     linear regression :
      MRANGE = 10
      SX  = 0.0
      SXX = 0.0
      SY  = 0.0
      SXY = 0.0
      DO K = 1,2
          IF (K .EQ. 1) THEN
              ISTART(K) = 1
              IEND(K)   = ISTART(K) + MRANGE - 1
          ELSE
              ISTART(K) = N - MRANGE + 1
              IEND(K)   = N
          END IF
*
          DO M = ISTART(K), IEND(K)
              X   = M
              SX  = SX + X
              SXX = SXX + X**2
              SY  = SY + DATA(M)
              SXY = SXY + X * DATA(M)
          END DO
      END DO
*
*     regression coefficients :
      M2 = MRANGE * 2
      SLOPE = ( SXY - SX*SY/M2 ) / ( SXX - SX*SX/M2 )
      CONSTANT = SY/M2 - SLOPE*SX/M2
*     subtract linear drift :
      DO I = 1, N
          DATA(I) = DATA(I) - ( SLOPE*I + CONSTANT )
      END DO
*
*     find rms noise over fitted region :
      VAR = 0.0
      DO K = 1,2
          DO M = ISTART(K), IEND(K)
              VAR = VAR + DATA(M) * DATA(M)
          END DO
      END DO
      RMS = SQRT (VAR/M2)
      RETURN
      END
*
