      SUBROUTINE CGAUSSJ(COVAR,MFIT,NCA,DA,M,MP)
C Adapted from Numerical Recipes, p. 28, for ERRBIN, uncertainty
C estimator for binary star data.
C     J.T. Armstrong          15 Feb. 1991

      INCLUDE 'BINFIT.INC'

      INTEGER*4 MFIT,NCA,M,MP,IPIV(MXPARM),INDXR(MXPARM),INDXC(MXPARM)
      INTEGER*4 IROW,ICOL,LL

      REAL*8    DA(MXPARM,MP), COVAR(MXPARM,MXPARM)
      REAL*8    BIG, DUM, PIVINV

      DO 11 J=1,MFIT
        IPIV(J)=0
11    CONTINUE
      DO 22 I=1,MFIT
        BIG=0.
        DO 13 J=1,MFIT
          IF(IPIV(J).NE.1)THEN
            DO 12 K=1,MFIT
              IF (IPIV(K).EQ.0) THEN
                IF (ABS(COVAR(J,K)).GE.BIG)THEN
                  BIG=ABS(COVAR(J,K))
                  IROW=J
                  ICOL=K
                ENDIF
              ELSE IF (IPIV(K).GT.1) THEN
                WRITE(6,*) ' Singular matrix in CGAUSSJ at 1'
                RETURN
              ENDIF
12          CONTINUE
          ENDIF
13      CONTINUE
        IPIV(ICOL)=IPIV(ICOL)+1
        IF (IROW.NE.ICOL) THEN
          DO 14 L=1,MFIT
            DUM=COVAR(IROW,L)
            COVAR(IROW,L)=COVAR(ICOL,L)
            COVAR(ICOL,L)=DUM
14        CONTINUE
          DO 15 L=1,M
            DUM=DA(IROW,L)
            DA(IROW,L)=DA(ICOL,L)
            DA(ICOL,L)=DUM
15        CONTINUE
        ENDIF
        INDXR(I)=IROW
        INDXC(I)=ICOL
        IF (COVAR(ICOL,ICOL).EQ.0.)THEN
          WRITE(6,*) ' Singular matrix in CGAUSSJ at 2.'
          RETURN
        ENDIF
        PIVINV=1./COVAR(ICOL,ICOL)
        COVAR(ICOL,ICOL)=1.
        DO 16 L=1,MFIT
          COVAR(ICOL,L)=COVAR(ICOL,L)*PIVINV
16      CONTINUE
        DO 17 L=1,M
          DA(ICOL,L)=DA(ICOL,L)*PIVINV
17      CONTINUE
        DO 21 LL=1,MFIT
          IF(LL.NE.ICOL)THEN
            DUM=COVAR(LL,ICOL)
            COVAR(LL,ICOL)=0.
            DO 18 L=1,MFIT
              COVAR(LL,L)=COVAR(LL,L)-COVAR(ICOL,L)*DUM
18          CONTINUE
            DO 19 L=1,M
              DA(LL,L)=DA(LL,L)-DA(ICOL,L)*DUM
19          CONTINUE
          ENDIF
21      CONTINUE
22    CONTINUE
      DO 24 L=MFIT,1,-1
        IF(INDXR(L).NE.INDXC(L))THEN
          DO 23 K=1,MFIT
            DUM=COVAR(K,INDXR(L))
            COVAR(K,INDXR(L))=COVAR(K,INDXC(L))
            COVAR(K,INDXC(L))=DUM
23        CONTINUE
        ENDIF
24    CONTINUE
      RETURN
      END

