      PROGRAM SUBGRPS
      IMPLICIT NONE

C     Creates the three covers of each given group.

      INTEGER TRI(30*10000,3),N,L,J,I,SUM, MAX
      INTEGER CUBEL(7159), PLACE(7159), CLASS(168)
      INTEGER CTRI(30*7159,3), CCUBEL(7159) 
      INTEGER INDTRI(3*15*7159,3)
      INTEGER ELEM(7159), JJ, KK, K, LL

C     Open the data file on units 3, 4 and 5
      OPEN (UNIT=3, FILE='T_list.txt')
      OPEN (UNIT=4, FILE='cubes.txt')
      OPEN (UNIT=5, FILE='classes.txt')
      OPEN (UNIT=7, FILE='inx3sgs.txt')


C     Read in the data:

      MAX=7159

C Read in the number of cubes for each representative
      READ (4,*) (CUBEL(I), I=1,MAX)
      CLOSE(UNIT=4)

      CCUBEL=CUBEL

      I=1
      SUM=0
 15   IF (I.LE. MAX) THEN
         SUM=SUM+15+(CUBEL(I)+2)/3*2
         I=I+1
         GOTO 15
      END IF


      READ (3,*) ((TRI(I,J), J=1,3), I=1,SUM)
      CLOSE(UNIT=3)

      READ (5,*) (CLASS(I), I=1,168)
      CLOSE(UNIT=5)

      WRITE(*,*) 'Data read ok'

      
C Make list of the last rows of each group on T_list
      I=2
      PLACE(1)=0
 20   IF(I .LE. MAX) THEN
         PLACE(I)=PLACE(I-1)+15+((CUBEL(I-1)+2)/3)*2
         I=I+1
         GOTO 20
      END IF


C Pick up the start rows for each set

      I=1
 30   IF(I .LE. MAX) THEN
         L=1
 35      IF(L.LE. (15+(CCUBEL(I)+2)/3*2)) THEN
            CTRI((I-1)*30+L,1)=TRI(PLACE(I)+L,1)
            CTRI((I-1)*30+L,2)=TRI(PLACE(I)+L,2)
            CTRI((I-1)*30+L,3)=TRI(PLACE(I)+L,3)
            L=L+1
            GOTO 35
         ELSE IF(L.LE. 30) THEN
            CTRI((I-1)*30+L,1)=0
            CTRI((I-1)*30+L,2)=0
            CTRI((I-1)*30+L,3)=0
            L=L+1
            GOTO 35
         END IF
         I=I+1
         GOTO 30
      END IF

C We start to create the dual groups.

      N=0
      I=1
      L=1
 100  IF(CTRI(N*30+I,1).NE.0 .AND. CTRI(N*30+I,1).EQ.CTRI(N*30+I,2) 
     +     .AND. CTRI(N*30+I,1).EQ.CTRI(N*30+I,3)) THEN
C we have a cube (x,x,x), take (3x,3x-1,3x-2)
         INDTRI(N*45+L,1)=3*CTRI(N*30+I,1)
         INDTRI(N*45+L,2)=3*CTRI(N*30+I,1)-1
         INDTRI(N*45+L,3)=3*CTRI(N*30+I,1)-2
         L=L+1
      ELSE
         INDTRI(N*45+L,1)=3*CTRI(N*30+I,1)
         INDTRI(N*45+L,2)=3*CTRI(N*30+I,2)-1
         INDTRI(N*45+L,3)=3*CTRI(N*30+I,3)-2
         L=L+1
         INDTRI(N*45+L,1)=3*CTRI(N*30+I,1)-2
         INDTRI(N*45+L,2)=3*CTRI(N*30+I,2)
         INDTRI(N*45+L,3)=3*CTRI(N*30+I,3)-1
         L=L+1
         INDTRI(N*45+L,1)=3*CTRI(N*30+I,1)-1
         INDTRI(N*45+L,2)=3*CTRI(N*30+I,2)-2
         INDTRI(N*45+L,3)=3*CTRI(N*30+I,3)
         L=L+1
      END IF
      I=I+1
C      IF(I.LE.(15+(CCUBEL(I)+2)/3*2)) THEN 
      IF(L.LE.45) THEN 
         GOTO 100
      ELSE
         N=N+1
         I=1
         L=1
         IF(N.LT. MAX) THEN
            GOTO 100
         END IF
      END IF


C Now we have the index 3 subgroups in INDTRI
C Each has 45 triangles.

C Let us write the data.

C      DO 2999 LL=1,168
C         IF(N.EQ.CLASS(LL)) THEN
      WRITE(7,'(3I3)') ((INDTRI(I,J),J=1,3),I=1,45*MAX)
C         END IF
C 2999 CONTINUE
      WRITE(*,*) 'Results saved in inx3sgs.txt'


 3000  FORMAT (3I4)
 4000  FORMAT (I4,F8.5)
 5000  FORMAT (15I3)

 9000 FORMAT (3I3)



 9999 END
