      PROGRAM DATA
      IMPLICIT NONE

C     Writes the results of POLPREC to a file, which can
C     be run in Magma to calculate the abelinizations and 
C     abelinizations of all index 3 subgroups. 

C     Takes as input
C     the T_list.txt (triangles), cubes.txt (the number of cubes in each
C     group) and nblist.txt (the numbers of groups to be considered).

      INTEGER TRI(30*10000,3),N,L,J,I,SUM, MAX
      INTEGER CUBEL(4964), PLACE(4964)
      INTEGER NBS(4964), CTRI(30*4964,3), CCUBEL(4964) 
      

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

C     Read in the data:

      MAX=4964

C Read in the representatives of the classes
C      READ (5,*) (NBS(I), I=1,128)
C      CLOSE(UNIT=5)

      DO 5 I=1,MAX
         NBS(I)=I
 5    CONTINUE

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

      I=1
 10   IF (I.LE. MAX) THEN
         CCUBEL(I)=CUBEL(NBS(I))
         I=I+1
         GOTO 10
      END IF

      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


C      WRITE(*,*) CCUBEL

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

      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 .LT. 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 representatives for the classes:

      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(NBS(I))+L,1)
            CTRI((I-1)*30+L,2)=TRI(PLACE(NBS(I))+L,2)
            CTRI((I-1)*30+L,3)=TRI(PLACE(NBS(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      WRITE(*,3000) ((CTRI(I,J), J=1,3), I=1,30*4694)

C     Now we have the representatives of each class.  
C     Then we print what we want to calculate with magma.

      N=1

 100  IF(N.LE. MAX) THEN

         IF(N.LT.10) WRITE(9,'(A,I1)') 'G', N
         IF(N.GT.9 .AND. N.LT.100) WRITE(9,'(A,I2)') 'G', N
         IF(N.GT.99 .AND. N.LT.1000) WRITE(9,'(A,I3)') 'G', N
         IF(N.GT.999) WRITE(9,'(A,I4)') 'G', N

         WRITE(9,'(A)') '<x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,'
         WRITE(9,'(A)') 'x11,x12,x13,x14,x15>:='
         WRITE(9,'(A)') 'Group<x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,'
         WRITE(9,'(A)') 'x11,x12,x13,x14,x15|'
        
         L=1

 110     IF(L .LT. 15+((CCUBEL(N)+2)/3)*2) THEN
            
            IF(CTRI((N-1)*30+L,1) .LT. 10) THEN
               WRITE(9, '(A,I1,A)') 'x',CTRI((N-1)*30+L,1),'*'
            ELSE
               WRITE(9, '(A,I2,A)') 'x',CTRI((N-1)*30+L,1),'*'
            END IF
            IF(CTRI((N-1)*30+L,2) .LT. 10) THEN
               WRITE(9, '(A,I1,A)') 'x',CTRI((N-1)*30+L,2),'*'
            ELSE
               WRITE(9, '(A,I2,A)') 'x',CTRI((N-1)*30+L,2),'*'
            END IF
            IF(CTRI((N-1)*30+L,3) .LT. 10) THEN
               WRITE(9, '(A,I1,A)') 'x',CTRI((N-1)*30+L,3),','
            ELSE
               WRITE(9, '(A,I2,A)') 'x',CTRI((N-1)*30+L,3),','
            END IF
            L=L+1
            GOTO 110
         END IF
            IF(CTRI((N-1)*30+L,1) .LT. 10) THEN
               WRITE(9, '(A,I1,A)') 'x',CTRI((N-1)*30+L,1),'*'
            ELSE
               WRITE(9, '(A,I2,A)') 'x',CTRI((N-1)*30+L,1),'*'
            END IF
            IF(CTRI((N-1)*30+L,2) .LT. 10) THEN
               WRITE(9, '(A,I1,A)') 'x',CTRI((N-1)*30+L,2),'*'
            ELSE
               WRITE(9, '(A,I2,A)') 'x',CTRI((N-1)*30+L,2),'*'
            END IF
            IF(CTRI((N-1)*30+L,3) .LT. 10) THEN
               WRITE(9, '(A,I1,A)') 'x',CTRI((N-1)*30+L,3),'>;'
            ELSE
               WRITE(9, '(A,I2,A)') 'x',CTRI((N-1)*30+L,3),'>;'
            END IF


      ELSE
         GOTO 9999
      END IF
CC For each group compute its abelinization 
CC and all index 3 subgroups and theri abelinizations with magma:

      IF(N.LT.10) THEN
         WRITE(9,'(A,I1,A)') 'print ', N, ';'
         WRITE(9,'(A,I1,A)')'AbelianQuotientInvariants(G',N,');' 
C         WRITE(9,'(A,I1,A)')'printf "%o\n",AbelianQuotientInvariants(G', 
C     +        N,');' 
         WRITE(9,'(A,I1,A)')'p:=LowIndexProcess(G',N,',<3,3>);'
      ELSE IF(N.LT.100) THEN 
         WRITE(9,'(A,I2,A)') 'print ', N, ';'
         WRITE(9,'(A,I2,A)')'AbelianQuotientInvariants(G',N,');' 
C         WRITE(9,'(A,I2,A)')'printf "%o\n",AbelianQuotientInvariants(G', 
C     +        N,');' 
         WRITE(9,'(A,I2,A)')'p:=LowIndexProcess(G',N,',<3,3>);'
      ELSE IF(N.LT.1000) THEN
         WRITE(9,'(A,I3,A)') 'print ', N, ';'
         WRITE(9,'(A,I3,A)')'AbelianQuotientInvariants(G',N,');' 
C         WRITE(9,'(A,I3,A)')'printf "%o\n",AbelianQuotientInvariants(G', 
C     +        N,');' 
         WRITE(9,'(A,I3,A)')'p:=LowIndexProcess(G',N,',<3,3>);'
      ELSE
         WRITE(9,'(A,I4,A)') 'print ', N, ';'
         WRITE(9,'(A,I4,A)')'AbelianQuotientInvariants(G',N,');' 
C         WRITE(9,'(A,I4,A)')'printf "%o\n",AbelianQuotientInvariants(G', 
C     +        N,');' 
         WRITE(9,'(A,I4,A)')'p:=LowIndexProcess(G',N,',<3,3>);'
      END IF
      WRITE(9,'(A)') 'while IsEmpty(p) ne true do'
      WRITE(9,'(A)') 'K:=ExtractGroup(p);'
      WRITE(9,'(A)') 'AbelianQuotientInvariants(K);'
      WRITE(9,'(A)') 'NextSubgroup(~p);'
      WRITE(9,'(A)') 'end while;'

      N=N+1
      GOTO 100

 3000  FORMAT (3I3)

 9999 END
