C ====COEFFICIENTS FOR HARTREE-FOCK-DIRAC EQUATIONS ==
      SUBROUTINE COEF2(GAM,DIR,S,KSU,LL,QQ,JJJ,
     *LO1, S1,CH,KK,SIGM1,SIGM2,G,MU)
C===============================================
      REAL*8 GAM(*),MU,RG1,RF1,JJJ(*),A
      INTEGER S,CH,G,LL(*),
     *    QQ(*),L1(1),S1(1),KK(*),SIGM1(*),DIR(*),
     *    SIGM2(*),PP(500),KSU,NU,NU1

      A=MU
      DO 10 I=1,CH
      DIR(I)=1
10    GAM(I)=0.0 D 00
      I2=0
      NU=I2
      L1(1)=LO1
      NU1=CH
      DO 20 I=1,S
      PP(I)=0
      IF((S1(1).LE.99.AND.(IDINT(2.*JJJ(I))+1)
     *.LT.QQ(I)).OR.(S1(1).GT.99.
     *AND.(IDINT(2.*JJJ(I))+1)/2.LT.QQ(I))) GO TO 1000
20    CONTINUE
C     WRITE(6,101)CH,NU,NU1
C101   FORMAT(' CH,NU,NU1 ',3I4)
C========ground state=======

      DO 23 I=1,S
      I3=I
      IF(KSU.EQ.3) I3=S
      DO 24 J=I3,S
      IF(I.EQ.J) GOTO 24
      L2=LL(I)
      IF ( LL(I).LT.0) L2=-LL(I)-1
      L3=LL(J)
      IF (LL(J).LT.0) L3=-LL(J)-1
      II1=IDINT(DABS(JJJ(I)-JJJ(J)))
      II2=IDINT(JJJ(I)+JJJ(J))
      DO 27 I1=II1,II2,1
C     PRINT 101,I,J,L2,L3,I1
C101  FORMAT('I,J,L2,L3,I1 ',5I4)
      IF (IPARI(L2+L3+I1).EQ.1) THEN
      NU=NU+1
      GAM(NU)=RG1(I1,JJJ(I),JJJ(J),QQ(I),QQ(J))
C     WRITE(6, 102)I1,I,J,GAM(NU)
C102  FORMAT('RG1 K,I,J',3I3,' GAM',E15.8)
      IF(S1(1).LE.99) GOTO 25
      GAM(NU)=GAM(NU)*2.
 25    KK(NU)=I1
      SIGM1(NU)=I
      SIGM2(NU)=J
      END IF
 27   CONTINUE
 24   CONTINUE
 23   CONTINUE
C======
       IF(KSU.EQ.1.OR.KSU.EQ.2) THEN
      DO 39 I=1,S
      IF(QQ(I).EQ.1) GOTO 39
      II9=IFIX(SNGL(JJJ(I)+JJJ(I)))
      IF(II9.LT.2) GOTO 39
      DO 392 I1=2,II9,1
C     PRINT 201,I,I1
C201  FORMAT(' I,I1',2I3)
      IF (IPARI(I1).EQ.1) THEN
      I2=I2+1
      IF(S1(1).LT.99) THEN
      GAM(NU1)=RF1(I1,JJJ(I),QQ(I))
C     WRITE(6, 202)NU1,GAM(NU1)
C202  FORMAT(' RF1 NU1',I3,' GAM',E15.8)
      GOTO 203
      END IF
      IF (S1(1).GT.99) THEN
C     GAM(NU1)=F4(I1,LL(I),QQ(I))
      GAM(NU1)=RF1(I1,JJJ(I),QQ(I))
      END IF
203   KK(NU1)=I1
      SIGM1(NU1)=I
      SIGM2(NU1)=I
      NU1=NU1-1
      END IF
392   CONTINUE
39    CONTINUE
      END IF
C============================
391   I1=0
      DO 40 I=1,NU
      IF(GAM(I).NE.0.D 00) GOTO 41
      J=I+1
42    IF(J.GT.NU) GOTO 45
      IF(GAM(J).EQ.0.D 00) GO TO 43
      GAM(I)=GAM(J)
      GAM(J)=0.D 00
      KK(I)=KK(J)
      SIGM1(I)=SIGM1(J)
      SIGM2(I)=SIGM2(J)
      GOTO 44
43    J=J+1
      GOTO 42
44    CONTINUE
45    CONTINUE
41    IF(GAM(I).NE.0.D 00) I1=I1+1
40    CONTINUE
      G=0
      DO 46 I=1,CH
C     WRITE(6,60)I,GAM(I)
C60   FORMAT(' I',I3,' GAM',E15.8)
      IF(GAM(I).NE.0.D 00) GOTO 47
      J=I+1
48    IF(J.GT.CH) GOTO 49
      IF(GAM(J).EQ.0.D 00) GOTO 50
      GAM(I)=GAM(J)
      GAM(J)=0.D 00
      KK(I)=KK(J)
      SIGM1(I)=SIGM1(J)
      SIGM2(I)=SIGM2(J)
      GOTO 49
50    J=J+1
      GOTO 48
49    CONTINUE
47    IF(GAM(I).EQ.0.D 00) GOTO 46
      G=G+1
46    CONTINUE
      CH=G
      G=NU
      G=I1
      IF(CH.NE.0) GOTO 53
      G=1
      CH=1
      GAM(1)=0.D 00
      KK(1)=0
      SIGM1(1)=1
      SIGM2(1)=2
53    CONTINUE
      RETURN
1000  PRINT 1001
1001  FORMAT(10X,' ERROR IN INPUT DATA')
      RETURN
      END
