      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 'Ctable'
      Include 'Cplot'
      Include 'Cflags'
c
      Virtual Data(1)
      Character*60 File_Name
c
      Integer Ivec(Ncolmx),Choice
      Equivalence (Y(1),Ivec(1))
c
c            Get the filename to read
c
      Write(6,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) File_Name
150   Format(A)
c
c            Check the file name syntax for errors
c
      Call Chknam(File_Name,Iflag)
      If (Iflag .EQ. 1) Return
c
c                 See if it is a binary file
c
      Open(Unit=12,Name=File_Name,Type='OLD',Form=
     $              'UNFORMATTED',ERR=10,Readonly)
      Read(12,END=10,ERR=10) Iflag1
      If (Iflag1 .GT. 0 .AND. Iflag1 .LT. 1000) Then ! 1000>Ncolmx
         Call Redbin(Data,Iflag1)
         Return
      End If
c
10    Continue
      Close(Unit=12)
      Open(Unit=12,NAME=File_Name,TYPE='OLD',ERR=98,Readonly)
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(6,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,80A1)
         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(6,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(6,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(6,460)
460   Format(' ERROR - Bad file name - routine aborted')
      Return
c
 98   Continue
      Write(6,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(6,470) 'read'
      Close(Unit=12)
      Return
      End
      Subroutine PLOTT(Data,Nflag)
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
      Include 'Cmain'
      Include 'Clabls'
      Include 'Ctxblk'
      Include 'Cplot'
      Include 'Clegnd'
      Include 'Cflags'
c
      Virtual Data(1)
c
      Integer*4 Isave,Isavea
      Character Com*17,File_Name*60
c
c           Check the axis data
c
      Call Plcdat(Data,Iflag)
      If (Iflag .EQ. 1) Return
c
c            Get the device to plot to
c
      If (Nflag .EQ. 0) Then
         Call Pltdev(Idev,0,Ibatch)
      Else
         Idev   = -1
         Ibatch = 0.0
      End If
c
c            Size the plot
c
      Call Sizplt
c
c            Get the scratch file name and detach command
c
      Call Getusr(File_Name,Com)
c
c            Open the scratch file
c
      Open (Unit=12,NAME=File_Name,TYPE='NEW',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(Idev,Ierr)
         If (Ierr .EQ. 0) Then
            Write(6,175)
175         Format(' Plot is being done as a batch 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',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(6,180)
180   Format(' Computing plot.  Please wait...'/)
      Call Runcmd(Com)
      Call Exit
c
50    Continue
      Write(6,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)
      Character*60 File_Name
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(6,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) File_name
150   Format(A)
c
c            Check the file name syntax for errors
c
      Call Chknam(File_Name,Iflag)
      If (Iflag .EQ. 1) Return
      Open(Unit=12,NAME=File_Name,TYPE='OLD',ERR=98,FORM='UNFORMATTED',
     $             Readonly)
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(6,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(6,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(6,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(6,460)
460   Format(' ERROR - Bad file name - routine aborted')
      Return
c
c          Error in opening the file
c
 98   Continue
      Write(6,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(6,470) 'read'
      Close(Unit=12)
      Return
      End
