      SUBROUTINE COVAR(N, M1, N1, IPIVOT, C, D, Z, NN)
C SUBROUTINE COVAR USES RESULTS FROM THE ORTHOGONAL DECOMPOSITION
C STORED IN C, D AND IPIVOT TO COMPUTE THE UNSCALED COVARIANCE MATRIX
C OF THE LEAST SQUARES COEFFICIENTS.
C ON ENTRY, THE FIRST N ROWS AND THE FIRST N COLUMNS OF C CONTAIN THE
C UPPER TRIANGULAR MATRIX OBTAINED FROM THE DECOMPOSITION.  THIS INPUT
C MATRIX IS DESTROYED IN SUBSEQUENT CALCULATIONS.
C ON EXIT, THE LOWER TRIANGULAR PORTION OF THE SYMMETRIC UNSCALED
C COVARIANCE MATRIX IS CONTAINED IN
C     C(1,1)
C     C(2,1) C(2,2)
C     . . .
C     C(N,1) C(N,2) ... C(N,N)
C IF N1 IS LESS THAN N, ONE OR MORE COLUMNS OF THE MATRIX
C H = (SQRT(W))*A WERE REJECTED AS BEING LINEARLY DEPENDENT.  WHENEVER
C THE K-TH COLUMN OF H WAS SO REJECTED, C(I,J) IS SET EQUAL TO ZERO,
C FOR I = K OR J = K, I.GE.J.
      INTEGER IPIVOT(N)
      REAL C(NN,N), D(1), Z(1)
      DOUBLE PRECISION SUM
      L = N1
      IF (L.GT.M1) C(L,L) = 1.0/D(L)
      IF (L.EQ.1) GO TO 60
   10 J = L - 1
      IF (J.GT.M1) C(J,J) = 1.0/D(J)
      DO 20 K=L,N1
        Z(K) = C(J,K)
   20 CONTINUE
      I = N1
      DO 40 KA=J,N1
        SUM = 0.0
        IF (I.EQ.J) SUM = DBLE(C(I,J))
        DO 30 K=L,N1
          SUM = SUM - DBLE(Z(K))*DBLE(C(K,I))
   30   CONTINUE
        C(I,J) = SUM
        I = I - 1
   40 CONTINUE
      DO 50 K=L,N1
        C(J,K) = C(K,J)
   50 CONTINUE
      L = L - 1
      IF (L.GT.1) GO TO 10
   60 IF (N1.EQ.N) GO TO 90
      N1P1 = N1 + 1
      DO 80 I=1,N
        DO 70 J=N1P1,N
          C(I,J) = 0.0
   70   CONTINUE
   80 CONTINUE
C PERMUTE THE COLUMNS AND ROWS OF MATRIX C TO ACCOUNT FOR PIVOTING.
   90 DO 120 I=1,N
        DO 100 J=1,N
          K = IPIVOT(J)
          Z(K) = C(I,J)
  100   CONTINUE
        DO 110 J=1,N
          C(I,J) = Z(J)
  110   CONTINUE
  120 CONTINUE
      DO 150 I=1,N
        DO 130 J=1,N
          K = IPIVOT(J)
          Z(K) = C(J,I)
  130   CONTINUE
        DO 140 J=I,N
          C(J,I) = Z(J)
  140   CONTINUE
  150 CONTINUE
      RETURN
      END
