      Subroutine Tabprt(Data)
c
c ***********************************************************************
c *                                                                     *
c *        Subroutine Tabprt - to print out the table on the screen,    *
c *        or send it to a printer or file.                             *
c *                                                                     *
c *         Programmed by Steven C. Fisher, DTNSRDC Code 1522           *
c *                                                                     *
c ***********************************************************************
c
      Include 'Cmain'
      Include 'Ctable'
      Include 'Cflags'
c
      Integer*4 Ia,Ai
      Double Precision Av,Ar
c
      Virtual Data(1)
      Dimension Isfig(Nhedmx),Ihedln(Nhedmx),Ns(Nhedmx),Lesc(11),
     $          Iwid(11)
      Byte Line(150),Filnam(15),Byt,Esc
      Character Upesc*20,Dwnesc*20,Escape(13)*20,Chr*1,String*132,
     $             Pname*10
      Equivalence (Byt,Chr),(String,Line),(Line,X(100)),(Esc,Y),
     $            (Esc,Escape),(Lesc,Y(200)),(Iwid,Y(212)),
     $            (Upesc,Escape(12)),(Dwnesc,Escape(13)),
     $            (Y(230),Isfig),(Y(250),Ihedln),(Y(270),Ns)
c
c              See if all of the table data exists
c
      Iflag = 0
      Do 1 I=1,Ntab
         If (Tabcol(I) .GT. Ncols) Then
            Write(7,100) I
100         Format(' ERROR - There is no data for table column ',I3,
     $             '.  Table cannot be printed.')
            Iflag = 1
         End If
 1    Continue
 2    Continue
      If (Iflag .EQ. 1) Return
c
c              Find out what you will print to
c
 3    Continue
      Write(7,110)
110   Format(/' Select the device to print the table to:'//
     $       ' 1)   Screen'/
     $       ' 2)   System printer'/
     $       ' 3)   Printer attached to terminal'/
     $       ' 4)   Disk File'//' Enter 1-4  ',$)
      Call Inchek(1,4,Ichose)
c
c            Set up according to the proper output unit
c
      Lwidth = 80/2
c
      If (Ichose .EQ. 1) Then
c
c            To the screen;
c
         Write(7,120) 27,27
120      Format(1X,1A1,'[H',1A1,'[J')
         Iunit = 7
         Iup   = 1
         Upesc = '^'
         Idown = 0
      Else If (Ichose .EQ. 2) Then
c
c            System printer;
c
         Lwidth = 96/2
         Nwid = 1
         Iprt = 1
         Open(Unit=12,Name='DP:PLTPRG.PTR',TYPE='OLD',ERR=12)
         Call Instr(Nchars,String,1)
         Read(String(1:Nchars),130,ERR=3) Nprint
130      Format(20I10)
c
c                 If there are more than one system printers, ask
c                 which one to print the table out on.
c
         If (Nprint .GT. 1) Then
            Write(7,140)
140         Format(/' Select the printer to use:'/)
            Do 5 I=1,Nprint
               Call Instr(N,String,0)
               Write(7,150) I,String(1:N)
150            Format(I3,')   ',A)
 5          Continue
            Write(7,160) Nprint
160         Format(/' Enter 1-',I1,'    ',$)
            Call Inchek(1,Nprint,Iprint)
         Else
            Call Instr(N,String,0)
            Iprint = 1
         End If
c
c                Reading in the printer information - characters/line,
c                escape sequences, etc.
c
         Do 10 J=1,Iprint
            Call Instr(Nchars,String,1)
            Read(String(1:Nchars),130,ERR=3) Nwid,Ispool,
     $                           (Iwid(I),I=1,Nwid)
            Do 7 I=1,Nwid+1
               Call Instr(Lesc(I),Escape(I),0)
 7          Continue
            Call Instr(Iup,Upesc,0)
            Call Instr(Idown,Dwnesc,0)
 10      Continue
         Close(Unit=12)
c
c               If more than one page width, ask which one to use
c
         If (Nwid .GT. 1) Then
            Write(7,170) (I,Iwid(I),I=1,Nwid),Nwid
170         Format(/' Select the page width:'//
     $             <Nwid>(I3,')  ',I3,' Characters'/)/
     $             ' Enter 1-',I1,'    ',$)
            Call Inchek(1,Nwid,Iprt)
         End If
         Lwidth = Iwid(Iprt)/2
c
 12      Continue
         Byt = 48 + Iprint
         Pname = 'PR :'
         Pname(3:3) = Chr
         Open(Unit=12,Name=Pname,Type='NEW',ERR=1)
         Iunit = 12
         Write(12,180) Escape(Iprt) (1:Lesc(Iprt))
180      Format(1X,A,$)
      Else If (Ichose .EQ. 3) Then
c
c           To the printer hooked to the terminal
c
         Iunit = 7
c
         Nwid = 1
         Iprt = 1
c
c              Read the printer setup file; see first if the user
c              has their own customized printer setup file.
c
         Open(Unit=12,Name='LD0:PLTPRG.TPR',TYPE='OLD',ERR=16)
         Go To 20
 16      Continue
         Open(Unit=12,Name='DP:PLTPRG.TPR',TYPE='OLD',ERR=30)
 20      Continue
         Call Instr(Nchars,String,1)
         Read(String(1:Nchars),130,ERR=3) Nprint
c
c               If there are more than one printer types, ask the
c               user which one to use
c
         If (Nprint .GT. 1) Then
            Write(7,190)
190         Format(/' Select the terminal printer type'/)
            Do 22 I=1,Nprint
               Call Instr(N,String,0)
               Write(7,150) I,String(1:N)
 22         Continue
            Write(6,160) Nprint
            Call Inchek(1,Nprint,Iprint)
         Else
            Call Instr(N,String,0)
            Iprint = 1
         End If
c
c                Reading in the printer information - characters/line,
c                escape sequences, etc.
c
         Do 26 J=1,Iprint
            Call Instr(Nchars,String,1)
            Read(String(1:Nchars),130,ERR=30) Nwid,Lwidth,
     $                         (Iwid(I),I=1,Nwid)
            Do 24 I=1,Nwid+1
               Call Instr(Lesc(I),Escape(I),0)
 24         Continue
            Call Instr(Iup,Upesc,0)
            Call Instr(Idown,Dwnesc,0)
 26      Continue
c
         Close(Unit=12)
c
c               If more than one page width, ask which one to use
c
         If (Nwid .GT. 1) Then
            Write(7,170) (I,Iwid(I),I=1,Nwid),Nwid
            Call Inchek(1,Nwid,Iprt)
         End If
         Lwidth = Iwid(Iprt)/2
c
 30      Continue
c
         Close(Unit=12)
         Write(7,200) 27
200      Format(1X,1A1,'[5i',$)
         Write(7,180) Escape(Iprt) (1:Lesc(Iprt))
      Else
c
c           To a file;
c
         Write(7,250)
250      Format(' Enter the file name to write to:  ',$)
         Read(5,300,END=98,ERR=98) Filnam
300      Format(15A1)
         Call Chknam(Filnam,Iflag)
         If (Iflag .EQ. 1) Return
         Call Tabwid(Data,Ns,Ntab,Maxwid)
c
c           Compute file size
c
         Maxno = 0
         Ifl = 0
         Do 40 I=2,Ntab
            Maxno = Max0(Maxno,Npoint(Tabcol(I)))
            If (Isci(I) .NE. 0) Ifl = 1
 40      Continue
         Isave = (Maxwid/2.+45.)*Maxno/512. + 4
c
         Call Filchk(Filnam,Isave,Iflag)
         If (Iflag .EQ. 1) Return

         If (Ifl .NE. 0) Then
            Write(7,310)
310         Format(' There is scientific notation used - select:'//
     $             ' 1)  Dec printer type'/
     $             ' 2)  Diablo printer type'/
     $             ' 3)  Screen "^" symbol'//
     $             '  Select 1-3   ',$)
            Call Inchek(1,3,It)
c
            Byt = 27
            Upesc   = ' L'
            Upesc(1:1) = Chr
            Iup     = 2
            Dwnesc = ' K'
            Dwnesc(1:1) = Chr
            Idown   = 2
            If (It .EQ. 2) Then
               Upesc(2:2)   = 'D'
               Dwnesc(2:2) = 'U'
            Else If (It .EQ. 3) Then
               Upesc ='^'
               Iup = 1
               Idown = 0
            End If
         End If
c
         Open(Unit=12,Name=Filnam,Type='NEW',ERR=99,Initialsize=Isave)
         Iunit = 12
      End If
c
      Call Tabcmp(Data,Isfig,Ihedln)
c
c            Initialize parameters
c
      Lmax = Tcolen(2)
      Do 45 I=2,Ntab
         Lmax = Max0(Lmax,Tcolen(I))
 45   Continue
      Iloop = 0
      Jloop = 0
c
c        First, compute the maximum width of the table
c
      Maxwid = Isfig(Ntab)
      Do 54 I=2,Ntab-1
         Maxwid = Nspac(I-1) + Isfig(I) + Maxwid
 54   Continue
      If (Lwidth .LT. Maxwid/2) Lwidth = Maxwid/2 + 1
      If (Ichose .EQ. 1 .AND. Lwidth .GT. 40) Then
         Lwidth = 66
         Write(7,320) 27,'h'
320      Format('+',1A1,'[?3',A,$)
      End If
c
 50   Continue
c
c                 Form feed a printer for large number of pages
c
      If (Jloop .NE. 0 .AND. Ichose .NE. 1) Then
         Write(Iunit,325) 12
325      Format(1X,1A1)
      End If
      Jloop = Tcolen(1) + 5 + Lmax
      Iprtit = Max0(Jloop+2,Iprtln)
c
c            Print out the header
c
      Write(Iunit,350,END=98,ERR=98)
350   Format(/)
      Do 56 I=1,Tcolen(1)
         Call Name(Titl,I,Table,Tablen)
         Write(Iunit,400,ERR=98,END=98) (Titl(J),J=1,Tablen(I))
400      Format(<Lwidth-Tablen(I)/2>X,<Tablen(I)>A1)
 56   Continue
      Write(Iunit,350,END=98,ERR=98)
c
c           Write out the table header
c
      Do 68 I=1,Lmax
         Do 58 J=1,132
            Line(J) = ' '
 58      Continue
         Istart = Lwidth - Maxwid/2
         Do 64 J=2,Ntab
            Istart = Isfig(J)/2 + Istart
            If (Tcolen(J) .GE. I) Then
               Call Point(K,J,Tcolen)
               Call Name(Titl,K+I-1,Table,Tablen)
               Do 60 L=1,Tablen(K+I-1)
                  Line(Istart+L-1-Tablen(K+I-1)/2) = Titl(L)
 60            Continue
            End If
            If (J .LT. Ntab) Istart = Istart+(Isfig(J)-Isfig(J)/2)+
     $                       Nspac(J-1)
 64      Continue
         Iend = Istart + Max0(Ihedln(Ntab),Isfig(Ntab))-Isfig(Ntab)/2
         Write(Iunit,450,END=98,ERR=98) (Line(J),J=1,Iend)
450      Format(1X,<Iend>A1)
 68   Continue
c
c             Turn the data into an ASCII character string, and
c             write it out.
c
      Write(Iunit,500,END=98,ERR=98)
500   Format(1X)
c
 70   Continue
      Jloop = Jloop + 1
c
c           See if there are too many lines on this page
c
      If (Jloop .GT. Iprtit) Go To 50
c
      Iloop = Iloop + 1
      Ipoint = Lwidth - Maxwid/2
      Iflag = 0
      Do 72 J=1,132
         Line(J) = ' '
 72   Continue
c
c           Start getting the values loaded into a character string
c
      Do 90 J=2,Ntab
         If (Npoint(Tabcol(J)) .GE. Iloop) Then
            Iflag = 1
            Call Point(Ipt,Tabcol(J),Npoint)
            A = Data(Ipt+Iloop-1)
c
c          Round off the values
c
            If (Round(J) .NE. 0.0) Then
               Av = A
               Ar = Round(J)
               If (Av .LT. 0.0) Then
                  Av = Av/Ar - 0.499999D0
               Else
                  Av = Av/Ar + 0.5
               End If
c
c           See if we can round off - this requires that the value / round off
c           value must not be so large that it cannot be represented by a
c           four byte integer value
c
               If (Abs(Av) .LT. 2E9) Then
                  Ai = Av
                  A = Ai * Ar
               End If
            End If
            If (Ndeci(J) .GT. 0) Then
c
c                  If we have a decimal point
c
               Encode(Isfig(J),550,Line(Ipoint)) A
550            Format(F<Isfig(J)>.<Ndeci(J)>)
            Else
c
c                  No decimal point
c
               Ia = A
               Encode(Isfig(J),600,Line(Ipoint)) Ia
600            Format(I<Isfig(J)>)
            End If
c
c                  Add in the commas if required
c
            If (Icom(J) .EQ. 1) Then
               Nc = 0
               Isf = Isfig(J) - Ndeci(J)
               If (Ndeci(J) .GT. 0) Isf = Isf - 1
               If (A .NE. 0.0) Then
                  Av = A
                  Nc = DLog10(Dabs(Av))/3
                  If (Nc .LT. 0) Nc = 0
               End If
               Do 76 K=Nc,1,-1
                  Do 74 Kk=Ipoint,Ipoint+Isf-2-K*3
                     Line(Kk) = Line(Kk+1)
 74               Continue
                  Line(Ipoint+Isf-1-K*3) = ','
 76            Continue
            End If
c
c                 Add scientific notation exponent
c
            If (Isci(J) .NE. 0) Then
               If (Isci(J) .GT. 0) Then
                  Kk = 7
               Else
                  Kk = 8
               End If
               Do 78 K = Ipoint,Ipoint+Isfig(J)-1-Kk
                  Line(K) = Line(K+Kk)
 78            Continue
c
               K = Ipoint + Isfig(J) - Kk - 1
               Line(K+1) = ' '
               Line(K+2) = '*'
               Line(K+3) = ' '
               Line(K+4) = '1'
               Line(K+5) = '0'
c
               If (Isci(J) .LT. 0) Then
                  Line(K+6+Iup) = '-'
                  Line(K+7+Iup) = 48 - Isci(J)
                  Iskip = 2
               Else
                  Line(K+6+Iup) = 48 + Isci(J)
                  Iskip = 1
               End If
c
c                   Set up superscripts
c
               Do 80 I=1,Iup
                  Chr = Upesc(I:I)
                  Line(K+5+I) = Byt
80             Continue
c
c                   Return from the superscript
c
               Do 84 I=1,Idown
                  Chr = Dwnesc(I:I)
                  Line(K+Iup+5+Iskip+I) = Byt
 84            Continue
c
c                    If Idown is equal to zero, then the superscript
c                    is represented by a printed symbol, so don't
c                    update this pointer.
c
               If (Idown .GT. 0) Ipoint = Ipoint + Iup + Idown
            End If
c
         End If
         Ipoint = Ipoint + Isfig(J) + Nspac(J-1)
 90   Continue
c
      Write(Iunit,650,ERR=98,END=98) (Line(J),J=1,Ipoint)
650   Format(1X,<Ipoint>A1)
c
      If (Iflag .EQ. 1) Go To 70
c
c          End here; close files or close LA 50 printer port, or wait for
c          a return to continue (screen option only)
c
      Write(Iunit,500,END=98,ERR=98)
 97   Continue
      If (Iunit .EQ. 12 .AND. Ichose .NE. 2) Close(Unit=12)
c
      If (Ichose .EQ. 3) Then
         Write(7,180) Escape(Nwid+1) (1:Lesc(Iprt))
         Write(7,700) 27
700      Format(1X,1A1,'[4i')
      End If
c
      If (Ichose .EQ. 1) Call Spause
      If (Ichose .EQ. 1 .AND. Lwidth .GT. 40) Write(7,320) 27,'l'
      If (Ichose .EQ. 2) Then
c
c               Print file on system printer, and reset (if necessary)
c               the printer character width
c
         Write(12,180) Escape(Nwid+1) (1:Lesc(Iprt))
         Close(Unit=12)
      End If
      Return
c
c          ********** ERROR RETURNS **********
c
 98   Continue
c
      Write(7,750)
750   Format(' ERROR in write statement - routine aborted')
      Go To 97
c
 99   Continue
c
      Write(7,800)
800   Format(' ERROR in opening file - routine aborted')
      Go To 97
c
      End
      Subroutine Instr(Nchars,String,Iflag)
c
c ***********************************************************************
c *                                                                     *
c *        Subroutine Instr - To read in strings of dats?               *
c *                                                                     *
c *           Programmed by Steven C. Fisher, DTNSRDC Code 1522         *
c *                                                                     *
c ***********************************************************************
c
      Character*132 String
c
c               Read in the character string
c
 5    Continue
      Read(12,100,ERR=5) Nchars,String(1:Nchars)
100   Format(Q,A)
      If (String(1:1) .EQ. '!') Go To 5
c
c               Put commas in string if we are to read it later
c
      If (Iflag .EQ. 1) Then
         Do 10 J=2,Nchars
            If (String(J:J) .EQ. ' ' .AND. String(J-1:J-1) .NE. ','
     $          .AND. String(J-1:J-1) .NE. ' ') String(J:J) = ','
 10      Continue
c
      End If
      Return
      End
