      Subroutine REDFIL(Data)
c
c ***********************************************************************
c *                                                                     *
c *        Subroutine REDFIL - To read a file containing the data       *
c *        for program PLTPRG                                           *
c *                                                                     *
c *        Output Values: (returned in common blocks)                   *
c *                                                                     *
c *        Data   - Array (virtual) containing the program data         *
c *        Ncols  - Number of data columns in file                      *
c *        Npoint - Array containing the number of points per column    *
c *        Title  - Character string containing the column titles       *
c *        Ttllen - Array containing the lengths of the column titles   *
c *                                                                     *
c *          Programmed by Steven C. Fisher, DTNSRDC Code 1522          *
c *                                                                     *
c ***********************************************************************
c
      Include 'Cmain'
      Include 'Cplot'
      Include 'Ctable'
      Include 'Cflags'
c
      Virtual Data(1)
      Byte Filnam(15)
c
      Integer Ivec(Ncolmx),Choice
      Equivalence (Y(1),Ivec(1))
c
c            Get the filename to read
c
      Write(7,100) 27,27,27
100   Format(1X,1A1,'[H',1A1,'[J'/1X,1A1,'#6 READ DATA ROUTINE'//
     $'   Enter the file name containing the data')
      Read(5,150,END=97,ERR=97) Filnam
150   Format(15A1)
c
c            Check the file name syntax for errors
c
      Call Chknam(Filnam,Iflag)
      If (Iflag .EQ. 1) Return
c
c            See if it is binary
c
      Open(Unit=12,Name=Filnam,Type='OLD',ERR=10,Form=
     $             'UNFORMATTED')
      Read(12,END=10,ERR=10) Iflag1
      If (Iflag1 .GT. 0 .AND. Iflag1 .LT. 1000) Then
         Call Redbin(Data,Iflag1)
         Return
      End If
c
 10   Continue
      Close(Unit=12)
      Open(Unit=12,NAME=FILNAM,TYPE='OLD',ERR=98)
c
c          Read in the heading data - Number of columns and version number
c
 15   Continue
      Read(12,300,END=99,ERR=99) Ivers,Nc
300   Format(2I5)
      If (Ivers .EQ. 0 .AND. Nc .EQ. 0) Go To 15
c
c            See if we are to add to existing data
c
      Nstart = 1
      If (Ncols .GT. 0) Then
         Write(7,200)
200      Format(' Do you want to add to the existing data? (y/n)')
         Call Instrg('YyNn',4,Choice)
c
c          Replace the existing data
c
         If (Choice .GT. 2) Then
            Ncols = 0
            Nstart = 1
         Else
            Nstart = Min0(Ncols + 1,Ncolmx)
         End If
      End If
c
c          Set data flag to 1
c
      Idflag = 1
c
      If (Nc + Ncols .LE. Ncolmx) Then
         Read(12,*,END=99,ERR=99) (Npoint(I),I=Ncols+1,Ncols+Nc)
         If (Ivers .EQ. 0) Read(12,*,END=99,ERR=99)
     $           (Ttllen(I),I=Ncols+1,Ncols+Nc)
      Else
         Read(12,*,END=99,ERR=99) (Npoint(I),I=Ncols+1,Ncolmx),
     $        (Ivec(J),J=1,Ncols+Nc-Ncolmx)
         If (Ivers .EQ. 0) Read(12,*,END=99,ERR=99) (Ttllen(I),I=
     $        Ncols+1,Ncolmx),(Ivec(J),J=1,Ncols+Nc-Ncolmx)
      End If
c
c           Read the column titles
c
      Do 20 I=1,Nc
         If (I + Ncols .LE. Ncolmx) Then
            Call Point(Ipoint,Ncols+I,Ttllen)
            Read(12,400,END=99,ERR=99) Ttllen(I+Ncols),(Title(J),
     $           J=Ipoint,Ipoint + Ttllen(I+Ncols)-1)
400         Format(Q,132A1)
         Else
            Read(12,400,END=99,ERR=99) Nchars,(Titl(J),J=1,Nchars)
         End If
20    Continue
c
c          Read in the data
c
      Do 30 I=1,Nc
         If (Ncols+I .GT. Ncolmx) Go To 90
         Call Point(Ipoint,Ncols+I,Npoint)
         If (Ipoint+Npoint(I+Ncols)-1 .GT. Last) Go To 32
         Read(12,*,END=32,ERR=32) (Data(J),J=Ipoint,Ipoint+
     $        Npoint(I+Ncols)-1)
 30   Continue
c
      Go To 33
 32   Continue
      Write(7,425) 7,I-1,Nc
425   Format(1X,1A1,'WARNING - Only ',I3,' out of ',I3,' read in')
      Call Spause
 33   Continue
      Ncols = Ncols+I-1
      Close(Unit=12)
c
c             See if the plot and table presentations have to be updated
c
      If (Choice .GT. 2) Then
c
c              Curves;
c
         Nc = Ncurve + 1
 35      Continue
         Nc = Nc - 1
         Do 40 I=1,Nc
            If (Xcurve(I) .GT. Ncols .OR. Ycurve(I) .GT. Ncols) Then
               X(1) = I
               Call Dcurve(X,Y,1)
               Go To 35
            End If
 40      Continue
c
c                 Tables;
c
         Nt = Ntab + 1
 45      Continue
         Nt = Nt - 1
c
         Do 50 I=2,Nt
            If (Tabcol(I) .GT. Ncols) Then
               Call Tabdel(I,1)
               Go To 45
            End If
 50      Continue
      End If
c
      If (Nstart .GT. 1) Then
         Call Lisrng('ADDED',5,Nstart,Ncols)
      Else
         Call Lisrng('CURRENT',7,Nstart,Ncols)
      End If
      Return
c
c         Error returns
c
 90   Continue
      Write(7,455) 7,Ncolmx-Ncols
455   Format(1X,1A1,' WARNING - Insufficient space for all columns -',
     $       ' only ',I3,' columns read'/)
      Ncols = Ncolmx
      If (Nstart .GT. 1) Then
         Call Lisrng('ADDED',5,Nstart,Ncols)
      Else
         Call Lisrng('CURRENT',7,Nstart,Ncols)
      End If
      Close(Unit=12)
      Return
c
 97   Continue
      Write(7,460)
460   Format(' ERROR - Bad file name - routine aborted')
      Return
c
 98   Continue
c
c                We will try to read it as a binary file
c
      Write(7,470) 'open'
      Close(Unit=12)
470   Format(' WARNING - Error encountered when ',A4,'ing file - ',
     $    'routine aborted')
      Return
 99   Continue
c
c          If an error is encountered while reading
c
      Write(7,470) 'read'
      Close(Unit=12)
      Return
      End
      Subroutine PLOTT(Data,Iopt)
c
c ***********************************************************************
c *                                                                     *
c *      Subroutine PLOTT - To make a branch to the plotting program    *
c *      held outside this program.  Routine checks data, saves data    *
c *      for the plot in a file called 'PLTPRG.PLT', and prompts the    *
c *      user for the device that they are plotting to.  It then ends   *
c *      giving control to the plot program PLOTFL.                     *
c *                                                                     *
c *       Programmed by Steven C. Fisher, DTNSRDC Code 1522             *
c *                                                                     *
c ***********************************************************************
c
      Virtual Data(1)
      Include 'Cmain'
      Include 'Cplot'
      Include 'Clabls'
      Include 'Ctxblk'
      Include 'Clegnd'
      Include 'Cflags'
c
      Integer*4 Isave,Isavea
      Byte Com(17),Filnam(12)
c
c      Compute the filespace needed to store the plot data
c
      Isave = 0
      Do 10 I=1,Ncurve
         Isave = Isave + 26 * Npoint(Xcurve(I))
         If (Crvfit(I) .EQ. 7) Isave = Isave + 20 + Npoint(Cvcol(I))*13
 10   Continue
      Isavea = Isave
      Isave = Isave + 40 * Ncurve + 34
c
c           Check the plot data
c
      Call Plcdat(Data,Iflag)
      If (Iflag .EQ. 1) Return
c
c           Get the length of the labels
c
      Jlab = Nlab
      Do 20 I=1,Ntxbl
         Jlab = Jlab + Ntxl(I)
         Isave = Isave + 31
 20   Continue
      Call Point(K,Jlab+1,Llabel)
      Isave = Isave + K + Jlab * 46
c
c           Get the length of the Legend
c
      Call Point(K,Nleg+1,Lleg)
      Isave = Isave + 55 + K + Nleg * 16
c
c          Get the no. of blocks for the file (1.1 = size fudge factor)
c
      Isavea = (1.1 * (Isave - Isavea)) / 512 + 3
      Isave = (1.1 * Isave)/512 + 3
c
c            Get the device to plot to
c
      If (Iopt .EQ. 0) Then
         Call Pltdev(Idev,0,Ibatch)
      Else
         Idev   = -1
         Ibatch = 0
      End If
c
c            Get the scratch file name and detach command
c
      Call Getusr(Filnam,Com)
c
c            Size the plot
c
      Call Sizplt

c
c            Open the scratch file
c
      Open (Unit=12,NAME=Filnam,TYPE='NEW',Initialsize=Isave,ERR=50)
      Write(12,155) Idev
155   Format(I4)
      Call Wpldat(Data,Iflag,1)
      If (Iflag .EQ. 1) Return
c
c             See if we can run the job as a detached plot to the HP
c
      IF (Ibatch .EQ. 2) Then
         Call STDET(Com,Ierr)
         If (Ierr .EQ. 0) Then
            Write(7,175)
175         Format(' Plot is being done as a detached job')
            Return
         End If
         Ibatch = 1
      End If
c
c             Also save the presentation in file PLTPRG.PLT
c
      If (Ipflag .EQ. 1) Then
         Open (Unit=12,NAME='PLTPRG.PLT',TYPE='NEW',Initialsize=Isavea,
     $         ERR=50)
c
         Write(12,155) Idev
         Call Wpldat(Data,Iflag,0)
         If (Iflag .EQ. 1) Return
         Ipflag = 0
      End If
c
c
c          Write program data to file PLTPRG.DAT if data has been updated
c          since the last time the file was written
c
      If (Idflag .EQ. 1) Then
         Call Wrtnow(Data,Iflag)
         If (Iflag .EQ. 1) Go To 50
         Idflag = 0
      End If
c
c                    Save the table data here
c
      If (Itflag .EQ. 1) Then
         Iflag = 1
         Call Savtab(Iflag)
         If (Iflag .EQ. 2) Return
         Itflag = 0
      End If
c
c          Exit here, starting the plotting subprogram
c
      If (Ibatch .GT. 0)  Write(7,180)
180   Format(' Computing plot.  Please wait...'/)
      Call Runcmd(Com)
      Call Exit
c
50    Continue
      Write(7,200)
200   Format(/' ERROR - Insufficient file space for scratch file - ',
     $    'routine aborted'/)
      Return
      End
      Subroutine REDBIN(Data,Iflag1)
c
c ***********************************************************************
c *                                                                     *
c *        Subroutine REDBIN - To read a binary file containing the     *
c *        data for program PLTPRG                                      *
c *                                                                     *
c *        Output Values: (returned in common blocks)                   *
c *                                                                     *
c *        Data   - Array (virtual) containing the program data         *
c *        Ncols  - Number of data columns in file                      *
c *        Npoint - Array containing the number of points per column    *
c *        Title  - Character string containing the column titles       *
c *        Ttllen - Array containing the lengths of the column titles   *
c *                                                                     *
c *          Programmed by Steven C. Fisher, DTNSRDC Code 1522          *
c *                                                                     *
c ***********************************************************************
c
      Include 'Cmain'
      Include 'Cplot'
      Include 'Ctable'
      Include 'Cflags'
c
      Virtual Data(1)
      Byte Filnam(15)
c
      Integer Ivec(Ncolmx),Choice
      Equivalence (Y(1),Ivec(1))
c
      If (Iflag1 .GT. 0) Then
         Nc = Iflag1
         Go To 10
      End If
c
c            Get the filename to read
c
      Write(7,100) 27,27,27
100   Format(1X,1A1,'[H',1A1,'[J'/1X,1A1,'#6 READ BINARY DATA FILE ',
     $'ROUTINE'//'   Enter the file name containing the data')
      Read(5,150,END=97,ERR=97) Filnam
150   Format(15A1)
c
c            Check the file name syntax for errors
c
      Call Chknam(Filnam,Iflag)
      If (Iflag .EQ. 1) Return
      Open(Unit=12,NAME=FILNAM,TYPE='OLD',ERR=98,FORM='UNFORMATTED')
c
c          Read in the heading data - Number of columns in file
c
      Read(12,END=99,ERR=99) Nc
c
c            See if we are to add to existing data
c
 10   Continue
      If (Ncols .GT. 0) Then
         Write(7,200)
200      Format(' Do you want to add to the existing data? (y/n)')
         Call Instrg('YyNn',4,Choice)
c
c          Replace the existing data
c
         If (Choice .GT. 2) Ncols = 0
c
      End If
      Nstart = Ncols + 1
c
c          Set data flag to 1
c
      Idflag = 1
c
      If (Nc + Ncols .LE. Ncolmx) Then
         Read(12,END=99,ERR=99)  (Npoint(I),I=Ncols+1,Ncols+Nc)
         Read(12,END=99,ERR=99)  (Ttllen(I),I=Ncols+1,Ncols+Nc)
      Else
         Read(12,END=99,ERR=99) (Npoint(I),I=Ncols+1,Ncolmx),
     $        (Ivec(J),J=1,Ncols+Nc-Ncolmx)
         Read(12,END=99,ERR=99) (Ttllen(I),I=Ncols+1,Ncolmx),
     $        (Ivec(J),J=1,Ncols+Nc-Ncolmx)
      End If
c
c           Read the column titles
c
      If (Nc + Ncols .GT. Ncolmx) Then
         Call Point(It1,Nc+Ncols-(Ncolmx-1),Ivec)
         Call Point(It,Ncolmx+1,Ttllen)
      Else
         It1 = 0
         Call Point(It,Nc+Ncols+1,Ttllen)
      End If
      Call Point(Is,Ncols+1,Ttllen)
c
      Read(12,ERR=99,END=99) (Title(I),I=Is,It-1),(Titl(1),K=1,It1-1)
c
c          See if we have enough room for the data
c
      Do 20 I=1,Nc
         Call Point(It,I+Ncols+1,Npoint)
         If (It-1 .GT. Last) Then
            Write(7,300) I-1,Last
300         Format(' Warning - Only',I4,' columns read in -',
     $             ' - Only',I6,' total points allowed')
            Nc = I-1
            Go To 25
         End If
 20   Continue
 25   Continue
c
      Call Point(Ipoint,Ncols+1,Npoint)
      Call Point(Itot,Ncols+Nc+1,Npoint)
      Itot = Min0(Itot-1,Last)
c
c          Read in the data
c
      Read(12,END=99,ERR=99) (Data(J),J=Ipoint,Itot)
c
      Close(Unit=12)
      If (Ncols+Nc .GT. Ncolmx) Go To 90
      Ncols = Ncols+Nc
c
c             See if the plot and table presentations have to be updated
c
      If (Choice .GT. 2) Then
c
c              Curves;
c
         Nc = Ncurve + 1
 35      Continue
         Nc = Nc - 1
         Do 40 I=1,Nc
            If (Xcurve(I) .GT. Ncols .OR. Ycurve(I) .GT. Ncols) Then
               X(1) = I
               Call Dcurve(X,Y,1)
               Go To 35
            End If
 40      Continue
c
c                 Tables;
c
         Nt = Ntab + 1
 45      Continue
         Nt = Nt - 1
c
         Do 50 I=2,Nt
            If (Tabcol(I) .GT. Ncols) Then
               Call Tabdel(I,1)
               Go To 45
            End If
 50      Continue
      End If
c
      If (Nstart .GT. 1) Then
         Call Lisrng('ADDED',5,Nstart,Ncols)
      Else
         Call Lisrng('CURRENT',7,Nstart,Ncols)
      End If
      Return
c
c         Error returns
c
 90   Continue
      Write(7,455) 7,Ncolmx-Ncols
455   Format(1X,1A1,' WARNING - Insufficient space for all columns -',
     $       ' only ',I3,' columns read'/)
      Ncols = Ncolmx
      If (Nstart .GT. 1) Then
         Call Lisrng('ADDED',5,Nstart,Ncols)
      Else
         Call Lisrng('CURRENT',7,Nstart,Ncols)
      End If
      Close(Unit=12)
      Return
c
 97   Continue
      Write(7,460)
460   Format(' ERROR - Bad file name - routine aborted')
      Return
c
c          Error in opening the file
c
 98   Continue
      Write(7,470) 'open'
470   Format(' WARNING - Error encountered when ',A4,'ing file - ',
     $    'routine aborted')
      Return
 99   Continue
c
c          If an error is encountered while reading
c
      Write(7,470) 'read'
      Close(Unit=12)
      Return
      End
