C TTTTTTTTT XXX   XXX TTTTTTTTT TTTTTTTTT   AAA    BBBBBBBBB
C    TTT     XXX XXX     TTT       TTT    AAA AAA  BBB   BBB
C    TTT       XXX       TTT       TTT   AAA   AAA BBBBBBB
C    TTT     XXX XXX     TTT       TTT   AAAAAAAAA BBB   BBB
C    TTT    XXX   XXX    TTT       TTT   AAA   AAA BBBBBBBBB
C
C     TXTTAB, PROGRAM TO FORM COLUMN FORMAT TABLES
C
C     DONALD E BARTH, CHEMISTRY DEPT, HARVARD UNIVERSITY
C
C     DATA IS READ ONE ENTRY PER LINE.
C     OUTPUT GENERATED HAS DATA IN PARALLEL COLUMNS.
C
      DIMENSION LTR(3960),IBGN(60),IBFR(60),IEND(60),IWIDTH(60)
C
C     IBATCH = 0, COLUMN FORMAT SPECIFICATIONS TO BE REQUESTED
C              FROM USER ON TELETYPE.
C            = 1, COLUMN SPECIFICATIONS ARE TO BE READ
C              FROM START OF INPUT FILE.
C              FIRST LINE OF INPUT FLE CONTAINS NUMBER OF
C              COLUMNS.
C              EACH OF THE FOLLOWING LINES CONTAINS THE LENGTH
C              AND WIDTH OF A SINGLE COLUMN.
C              A ZERO COLUMN LENGTH WILL CAUSE THE ALREADY
C              ESTABLISHED PATTERN OF LENGTHS AND WIDTHS TO BE
C              REPEATED ACROSS THE REMAINING WIDTH OF TABLE.
C     IBFR   = ARRAY WHICH HOLDS THE LINE CURRENTLY BEING
C              FORMATTED FOR OUTPUT.
C     IBGN   = ARRAY WHICH CONTAINS THE CHARACTER COUNT
C              OF THE FIRST CHARACTER IN CORRESPONDING
C              COLUMN.
C     IBLANK = HOLERITH BLANK.
C     IEND   = ARRAY WHICH CONTAINS THE CHARACTER COUNT
C              OF THE FINAL CHARACTER IN CORRESPONDING
C              COLUMN.
C     ITTY   = NUMBER OF UNIT ON WHICH CONVERSATION WITH
C              USER IS CONDUCTED.
C     IWIDTH = ARRAY CONTAINING WIDTHS OF EACH COLUMN.
C     LENGTH = DIMENSION OF LTR ARRAY.  THIS IS MAXIMUM
C              NUMBER OF CHARACTERS WHICH CAN BE READ.
C     LTR    = ARRAY INTO WHICH ALL INPUT IS STORED BEFORE
C              COLUMN FORMATTING IS BEGUN.
C     MANY   = DIMENSION OF IBGN ARRAY, IEND ARRAY AND
C              IWIDTH ARRAY.  THIS IS MAXIMUM NUMBER OF
C              COLUMNS.
C     MOST   = DIMENSION OF IBFR ARRAY.  THIS IS THE MAXIMUM
C              NUMBER OF CHARACTERS PER OUTPUT LINE.
C
      DATA IBLANK/1H /
      DATA ITTY/5/
      DATA LENGTH/3960/
      DATA MANY/60/
      DATA MOST/60/
C
C     ESTABLISH DEFAULT INPUT UNIT NUMBER AND FILE
C     NAME AND OUTPUT UNIT NUMBER AND FILE NAME.
      ITAPE=1
      INAME=5HINPUT
      JTAPE=20
      JNAME=5HOUTPU
      IBATCH=1
C
C     ASK USER FOR INPUT DEVICE
      WRITE(ITTY,1)
    1 FORMAT(1X,'INPUT UNIT NUMBER = ',$)
      READ(ITTY,11)I
      IF(I.LT.0)GO TO 7
      IF(I.GT.0)ITAPE=I
      WRITE(ITTY,2)
    2 FORMAT(1X,'INPUT FILE NAME = ',$)
      READ(ITTY,3)I
    3 FORMAT(1A5)
      IF(I.NE.IBLANK)INAME=I
C
C     ASK USER FOR OUTPUT DEVICE
      WRITE(ITTY,4)
    4 FORMAT(1X,'OUTPUT UNIT NUMBER = ',$)
      READ(ITTY,11)I
      IF(I.LT.0)GO TO 7
      IF(I.GT.0)JTAPE=I
      WRITE(ITTY,5)
    5 FORMAT(1X,'OUTPUT FILE NAME = ',$)
      READ(ITTY,3)I
      IF(I.NE.IBLANK)JNAME=I
C
C     ASK USER WHETHER BATCH OR CONVERSATIONAL MODE
      WRITE(ITTY,6)
    6 FORMAT(' IS FORMAT SPECIFIED IN INPUT FILE (Y OR N) = ',$)
      READ(ITTY,25)I
      IF(I.EQ.IBLANK)GO TO 7
      IF(I.NE.1HY)IBATCH=0
C
C     OPEN INPUT AND OUTPUT FILES
    7 CALL IFILE(ITAPE,INAME)
      CALL OFILE(JTAPE,JNAME)
C
C     IF IN BATCH MODE, READ IN NUMBER OF COLUMNS
C     AND LENGTH AND WIDTH OF EACH COLUMN
      IF(IBATCH.EQ.0)GO TO 9
      READ(ITAPE,11)I
      IF(I.LE.0)I=MANY
      IF(I.GT.MANY)I=MANY
      DO 8 J=1,I
      READ(ITAPE,11)IBFR(J),IWIDTH(J)
      IF(IBFR(J).LE.0)GO TO 12
    8 CONTINUE
      GO TO 12
C
C     ASK USER FOR NUMBER OF COLUMNS
    9 WRITE(ITTY,10)
   10 FORMAT(1X,'NUMBER OF COLUMNS = '$)
      READ(ITTY,11)I
   11 FORMAT(2I)
      IF(I.LE.0)I=MANY
      IF(I.GT.MANY)I=MANY
C
C     READ THE DATA INTO LTR ARRAY
   12 LIMIT=0
      MAX=0
      K=0
      L=0
      M=0
      N=0
      KOUNT=0
   13 KOUNT=KOUNT+1
      M=M+1
      IF(L.NE.0)GO TO 19
      IF(IBATCH.NE.0)GO TO 17
      IF(KOUNT.EQ.2)WRITE(ITTY,14)
   14 FORMAT(1X,'ZERO WHEN REST SAME')
      WRITE(ITTY,15)KOUNT
   15 FORMAT(1X,'COLUMN',1I3,' LENGTH = ',$)
      READ(ITTY,11)IBFR(KOUNT)
      IF(IBFR(KOUNT).LE.0)GO TO 18
      WRITE(ITTY,16)KOUNT
   16 FORMAT(1X,'COLUMN',1I3,' WIDTH  = ',$)
      READ(ITTY,11)IWIDTH(KOUNT)
   17 IF(IWIDTH(KOUNT).LT.0)IWIDTH(KOUNT)=0
      IF(IBFR(KOUNT).GT.0)GO TO 20
   18 IF(KOUNT.EQ.1)GO TO 34
      M=1
      L=KOUNT
   19 IF(M.GE.L)M=1
   20 J=IBFR(M)
      IWIDE=IWIDTH(M)
      N=N+IWIDE
      IF(N.LT.MOST)GO TO 21
      IWIDE=IWIDE-N+MOST
      I=KOUNT
   21 IBGN(KOUNT)=K+1
      K=K+(IWIDE*J)
      IF(K.LE.LENGTH)GO TO 24
      I=KOUNT
   22 K=K-IWIDE
      IF(K.LT.LENGTH)GO TO 23
      J=J-1
      GO TO 22
   23 K=LENGTH
   24 IF(J.GT.MAX)MAX=J
      LIMIT=LIMIT+J
      IEND(KOUNT)=K
      IWIDTH(KOUNT)=IWIDE
      IBFR(KOUNT)=J
      JJ=IBGN(KOUNT)
      KK=JJ-1
      DO 27 II=1,J
      IF(IWIDE.GT.0)GO TO 26
      READ(ITAPE,25,END=28)LL
   25 FORMAT(120A1)
      GO TO 27
   26 KK=KK+IWIDE
      IF(KK.GT.LENGTH)KK=LENGTH
      READ(ITAPE,25,END=28)(LTR(LL),LL=JJ,KK)
      JJ=JJ+IWIDE
   27 CONTINUE
      IF(KOUNT.LT.I)GO TO 13
   28 IF(JJ.GT.IBGN(KOUNT))GO TO 29
      KOUNT=KOUNT-1
      IF(KOUNT.GT.0)GO TO 28
      GO TO 34
   29 IEND(KOUNT)=JJ-1
C
C     WRITE OUT DATA IN PARALLEL COLUMNS
      DO 33 I=1,MAX
      K=0
      DO 32 J=1,KOUNT
      IWIDE=IWIDTH(J)
      IF(IWIDE.EQ.0)GO TO 32
      L=IBGN(J)
      IBGN(J)=IBGN(J)+IWIDE
      DO 31 M=1,IWIDE
      K=K+1
      IF(L.LE.IEND(J))GO TO 30
      IBFR(K)=IBLANK
      GO TO 31
   30 IBFR(K)=LTR(L)
      L=L+1
   31 CONTINUE
   32 CONTINUE
      IF(K.GT.0)WRITE(JTAPE,25)(IBFR(J),J=1,K)
   33 CONTINUE
C
C     STOP
   34 STOP
      END
