C   20/10/81 508071748  MEMBER NAME  MUFMAT   (JADEMUS)     FORTRAN
C
C LAST CHANGE 18.00  7/08/85 ROGER BARLOW - SYMIN4 BUG AVOIDED
C      CHANGE 17.20 06/05/83 CHRIS BOWDERY-CORRECTED ERROR MESSAGE
C      CHANGE 14.30 21/10/81 HUGH MCCANN - RENAMED TO MUFMAT.
C      CHANGE 11.30 18/10/81 HUGH MCCANN - SYMIN4 SUBSTITUTED FOR MATIN100000400
C
C-----------------------------------------------------------------------
      SUBROUTINE MUFMAT(NTHISS,MERR)
C-----------------------------------------------------------------------
C
      IMPLICIT INTEGER*2 (H)
C
      DIMENSION HAMB(1)
C
C ASSEMBLES THE ERROR MATRIX FOR A GIVEN HIT/ AMBIGUITY PERMUTATION
C AND INVERTS IT.
C MERR =1 IF NDFT .LE. 0 OR THERE IS A SYMIN4 ERROR, OTHERWISE , =0.
C
C-----------------------------------------------------------------------
C
C             COMMONS.
C
#include "cmufwork.for"
C             INITIALISE ERROR FLAG.
      MERR=1
C
C             SELECT ERROR MATRIX AND INVERT IT.
      NDFT=0
      K=1
      DO 40 I=1,NTHISS
      IT=2*I-1
C             IS  THIS  HIT  IN  THE  PRESENT  HIT  PERMUTATION  ?
C             EVEN TEMPORARILY ?
      IF(IHTPER(I).NE.1.AND.IHTEMP(I).NE.1)GO TO 40
      IF(BADA(IT))GO TO 40
      L=1
      DO 41 J=1,NTHISS
      JT=2*J-1
      IF(IHTPER(J).NE.1.AND.IHTEMP(J).NE.1)GO TO 41
      IF(BADA(JT))GO TO 41
      G(K,L)=EM(IT,JT)
      L=L+1
 41   CONTINUE
      K=K+1
 40   CONTINUE
      NDFT=K-1
      IF(NDFT.LE.0)RETURN
      MERR=0
C
C++++
C     WRITE(6,2848)NDFT
C2848 FORMAT(I5,' COORDS IN DRIFT CALCULATION.',
C    *' SPECIFIC ERROR MATRIX IS')
C     CALL UCOPY(G,EM1,400)
C     DO 2849 I=1,NDFT
C     WRITE(6,2858)(EM1(I,J),J=1,NDFT)
C2858 FORMAT(12F10.0)
C2849 CONTINUE
C     WRITE(6,2859)(DEVA(I),I=1,NDFTL)
C2859 FORMAT(' CORRESPONDING DEVIATIONS'/(12F10.0))
C++++
C
C             INVERT MATRIX.
       IF (NDFT.NE.2) GOTO 664
       DETER=G(1,1)*G(2,2)-G(1,2)**2
       IER=1
       IF(DETER.EQ.0.0) GOTO 98
       IER=0
       G(1,2)=-G(1,2)/DETER
       G(2,1)=-G(2,1)/DETER
       SSAVE=G(1,1)
       G(1,1)=G(2,2)/DETER
       G(2,2)=SSAVE/DETER
       GOTO 665
664   CALL SYMIN4(G,20,NDFT,MWORK,IER)
665   IF(IER.NE.0)GO TO 98
C++++
C     WRITE(6,2861)IER
C2861 FORMAT(' IER',I5)
C     WRITE(6,2838)
C2838 FORMAT(' INVERSE ERROR MATRIX IS')
C     DO 2839 I=1,NDFT
C     WRITE(6,2840)(G(I,J),J=1,NDFT)
C2840 FORMAT(10E12.4)
C2839 CONTINUE
C     DO 3951 I=1,NDFT
C     DO 3951 J=1,NDFT
C     EMG(I,J)=0.
C     DO 3951 K=1,NDFT
C3951 EMG(I,J)=EMG(I,J)+EM1(I,K)*G(K,J)
C     WRITE(6,2138)
C2138 FORMAT(' PRODUCT MATRIX IS')
C     DO 2139 I=1,NDFT
C     WRITE(6,2140)(EMG(I,J),J=1,NDFT)
C2140 FORMAT(10E12.4)
C2139 CONTINUE
C++++
C
      GO TO 99
C
C-----------------------------------------------------------------------
C
 98   CONTINUE
      CALL MUERRY('MUFMAT',IER,'ERROR RETURN FROM SYMIN4.^')
      MERR=1
      GO TO 99
C
C-----------------------------------------------------------------------
C
 99   CONTINUE
      RETURN
      END
