      Subroutine Convrt(Data)
c
c ***********************************************************************
c *                                                                     *
c *        Subroutine CONVRT - To convert a datafile into a PLTPRG      *
c *        compatible datafile.                                         *
c *                                                                     *
c *                                                                     *
c *          Programmed by Steven C. Fisher, DTNSRDC Code 1522          *
c *                                                                     *
c ***********************************************************************
c
      Include 'Cmain'
      Include 'Cflags'
c
      Virtual Data(1)
      Byte Filnam(25)
c
      Integer*4 Ich(20)
c
      Data Ich/' (1)',' (2)',' (3)',' (4)',' (5)',' (6)',' (7)',
     $    ' (8)',' (9)','(10)','(11)','(12)','(13)','(14)','(15)',
     $    '(16)','(17)','(18)','(19)','(20)'/
c
      Ntail = 0
      Iblock = 1
      Nadd = 0
c
c            Clear the screen & get the filename to convert
c
      Write(7,100) 27,27,27
100   Format(1X,1A1,'[H',1A1,'[J'//1X,1A1,'#6  SUBROUTINE CONVRT'//
     $   ' To convert datafiles to a PLTPRG compatable format'//
     $   ' Enter the filename containing your data')
      Read(5,150,ERR=97,END=97) Filnam
150   Format(25A1)
      Call Chknam(Filnam,Iflag)
      If (Iflag .EQ. 1) Return
      Open(Unit=12,NAME=Filnam,TYPE='OLD',ERR=98,ReadOnly)
c
c              Set the flag to show that the data is changed
      Idflag = 1
c
c              See if this data will be added, or will it replace the
c              current data in the program
c
      If (Ncols .GT. 0) Then
         Write(7,175)
175      Format(' Do you want to add to the existing data?  (y/n)  ',$)
         Call Instrg('YyNn',4,Iflag)
         If (Iflag .GT. 2) Ncols = 0
      End If
c
      Call Point(Ipoint,Ncols+1,Npoint)
c
c              See if the column titles are stored in the file
c
      Write(7,200)
200   Format(/' Are the column titles stored in the file?  (y/n)  ',$)
      Call Instrg('YyNn',4,Iflag)
c
c              Set the unit to read the column titles from
c
      If (Iflag .GT. 2) Then
         Idev = 5
      Else
         Idev = 12
      End If
c
c         Prompt the user for the number of columns
c
      Iwrap = 0
10    Continue
c
      If (Ncols .EQ. Ncolmx) Then
         Write(7,225)
225      Format(' Program cannot accept more data columns')
         Close(Unit=12)
         Return
      End If
c
      Nold = Nadd
      Write(7,250) Iblock
250   Format(/' Enter the number of columns stored in block number ',I2/
     $        '  (enter 0 to skip block, or "S" to stop)   ',$)
c
c                Read in the number of columns, and verify value
c
 11   Continue
      Call Inpt(X,N,Iflag,2)
      If (Iflag .NE. 0) Go To 95
      If (N .EQ. 0 .OR. X(1) .GT. 20.0 .OR. X(1) .LT. 0.0) Then
         Write(7,260)
260      Format(' Bad input value.  Value has to be between 0 and ',
     $          '20, or "S"  ',$)
         Go To 11
      End If
      Nadd = X(1)
c
c              Skip this block;
c
      If (Nadd .EQ. 0) Then
12       Continue
         Read(12,450,END=95,ERR=95) Nchars,(Titl(I),I=1,Nchars)
         If (Titl(1) .EQ. '$') Then
            Iblock = Iblock + 1
            Go To 10
         End If
         Go To 12
      End If
c
c              See if we will exceed the number of colums allowed
c
      If (Nadd+Ncols .GT. Ncolmx) Write(7,275) -Ncols+Ncolmx
275   Format(' WARNING - Only ',I2,' columns will be added')
c
c              Get the title for the columns:
c
      If (Nold .NE. Nadd) Then
         Write(7,300)
300      Format(/' Do you want to enter:'//' 1)  One global title'/
     $           ' 2)  Individual titles for each column'//
     $           ' Enter 1 or 2:  ',$)
         Call Inchek(1,2,Kflag)
      Else
         Write(7,350)
350      Format(/' Do you want to enter:'//' 1)  One global title'/
     $           ' 2)  Individual titles for each column'/
     $           ' 3)  Same titles as last block'//
     $           ' Enter 1 - 3:  ',$)
         Call Inchek(1,3,Kflag)
      End If
c
      Ncol1 = Ncols
      If (Kflag .EQ. 1) Then
c
c             The titles are global titles:
c
         If (Idev .EQ. 5) Then
            Write(7,400)
400         Format(' Enter the global column title ')
            Call Linedt(Titl,Nchars,80,0)
         Else
            Read(Idev,450,END=95,ERR=95) Nchars,(Titl(I),I=1,Nchars)
450         Format(Q,132A1)
         End If
c
c             Store the title, appending 1-20 to the end of the global title
c
         Na = Min0(Ncolmx-Ncols,Nadd)
         Do 15 I=1,Na
            Call Point(Ititle,Ncol1+1,Ttllen)
            Call Rep(Title,Ititle-1,Ititle,0,Titl,Nchars)
            Ttllen(Ncol1+1) = Nchars + 4
            Ncol1 = Ncol1 + 1
            Call Rep(Title,Ititle+Nchars-1,Ititle+Nchars,0,Ich(I),4)
15       Continue
      Else If (Kflag .EQ. 2) Then
c
c                The titles are individual:
c
         Do 25 I=1,Nadd
            Call Point(Ititle,Ncol1+1,Ttllen)
            If (Idev .EQ. 5 .AND. Ncol1+1 .LE. Ncolmx) Write(7,500) I
500         Format(' Enter the title for column number',I2,':')
c
            If (Idev .EQ. 5 .AND. Ncol1+1 .LE. Ncolmx) Then
               Call Linedt(Title(Ititle),Nchars,70,0)
            Else If (Idev .NE. 5) Then
               Read(Idev,450,ERR=95,END=95) Nchars,(Title(Ititle+J-1),
     $                    J=1,Nchars)
            End If
c
            If (Ncol1 + 1 .LE. Ncolmx) Then
               Ttllen(Ncol1+1) = Nchars
               Ncol1 = Ncol1 + 1
            End If
25        Continue
      Else
c
c                 Use the last column titles
c
         Do 35 I=1,Nadd
            If (Ncols + I .LE. Ncolmx) Then
               Ncl = Ncols - Nold + I
               Call Point(Ititle,Ncl,Ttllen)
               Call Point(Itit,Ncols+I,Ttllen)
               Do 30 J=1,Ttllen(Ncl) - Ntail
                  Title(Itit+J-1) = Title(Ititle+J-1)
30             Continue
               Ttllen(Ncols+I) = Ttllen(Ncl) - Ntail
             End If
35        Continue
      End If
c
c               Only allow "Ncolmx" columns to be total;
c
      Nadd = Min0(Nadd,Ncolmx-Ncols)
c
c               Add an addendum to the titles
c
      Write(7,510)
510   Format(' Is there an addendum to these titles? (y/n)  ',$)
      Call Instrg('YyNn',4,Ichr)
      If (Ichr .LT. 3) Then
         If (Idev .EQ. 5) Write(7,520) Iblock
520      Format(' Enter the Addendum to block ',I3)
         Titl(1) = ' '
         Titl(2) = ' '
         If (Idev .EQ. 5) Then
            Call Linedt(Titl(3),Ntail,70,0)
         Else
            Read(Idev,450,ERR=95,END=95) Ntail,(Titl(I),I=3,Ntail+2)
         End If
         Ntail = Ntail + 2
         Do 38 I=1,Nadd
            Call Point(Ititle,I+Ncols+1,Ttllen)
            Call Point(Itittl,Ncols+Nadd+1,Ttllen)
            Call Rep(Title,Itittl,Ititle,0,Titl,Ntail)
            Ttllen(I+Ncols) = Ttllen(I+Ncols) + Ntail
38       Continue
      Else
         Ntail = 0
      End If
c
c             Data input loop:
c
      Loop = 1
c
c             Read the data using subroutine INPTT
c
40    Continue
c
      Call Inptt(X,Numbr,Iflag)
c
c             Note - Numbr is the number of values read on a line by INPTT
c
      If (Numbr .GT. 0) Then
c
c             Store the data in array "DATA"
c
         If (Iwrap .EQ. 0 .AND. Numbr .LT. Nadd) Then
            Write(7,530)
530         Format(' Is the data "wrapped" in the file?  ',$)
            Call Instrg('YyNn',4,Isee)
            If (Isee .LT. 3) Then
               Iwrap = 1
            Else
               Iwrap = -1
            End If
         End If
c
 43      Continue
         If (Numbr .LT. Nadd .AND. Iwrap .EQ. 1) Then
            Call Inptt(X(Numbr+1),Numbr1,Iflag)
            Numbr = Numbr + Numbr1
            Go To 43
         End If
c
         If (Loop .LE. Nptmax) Then
            Do 45 I = 1,Nadd
               If (Numbr .LT. I) X(I) = 1E20
               Data((Loop-1)*Nadd+Ipoint-1+I+Ilastp) = X(I)
45          Continue
         End If
         Loop = Loop + 1
c
c             Make certain that the "DATA" array isn't overfilled
c
         If ((Loop*Nadd+Ipoint) .GT. Last .OR. Iflag .NE. 0)
     $               Go To 50
      Else
c
c             Exit the data input routine if an end of file occurs
c            (this condition will also occur if a "$" is in column 1)
c
         Go To 50
      End If
c
c                Send a warning if too few points are read in
c
      If (Numbr .LT. Nadd)   Write(7,550) 7,Loop-1
550      Format(1X,1A1,' WARNING - Insufficient number of values at ',
     $          'point ',I4)
c
c                Loop back for more data
c
      Go To 40
c
50    Continue
c
c                If more than 300 points were read in;
c
      If (Loop .GT. Nptmax+1) Then
         Write(7,555) Loop-1,Nptmax,7
555      Format(/' WARNING - A total of',I5,' points/column were',
     $           ' read in'/' - only the first ',I4,' are kept',1A1)
         Loop = Nptmax+1
      End If
c
      If (Loop .NE. 1) Then
c
c                 Update the data pointers
c
         Do 60 I=1,Nadd
            Npoint(Ncols+I) = Loop - 1
60       Continue
         Loop = Loop - 1
         Ncols = Ncols + Nadd
         Nad = Nadd
c
c              This next part of the program will convert the data stored
c              in "DATA" from a sequential X(1),Y(1),Z(1),....X(2),Y(2),...
c              to a X(1),X(2),X(3),...Y(1),Y(2),Y(3),...Z(1),Z(2),...
c              format as is used in PLTPRG.
c
         Do 85 K = 2,Nadd
c
c              Get the last column data and save it in array X
c
            Do 65 I=1,Loop
               X(I) = Data(Ipoint + Nad*I - 1)
65          Continue
            J = 0
            Nad = Nad - 1
c
c                  Squeeze data, removing the last column data
c
            Do 70 I=1,Nad*Loop
               J = J + 1
               If (((I-1)/(Nad))*(Nad) .EQ. I-1 .AND. I .GT. 1)
     $                       J = J+1
               Data(I+Ipoint-1) = Data (J+Ipoint-1)
70          Continue
            Itop = Nad * Loop + Ipoint
c
c                  Restore the data saved in X at the end of data
c
            Do 80 I = 1,Loop
               Data (Itop + I - 1) = X(I)
 80         Continue
 85      Continue
c
c                  Display the columns just read in
c
         Write(7,560) 27,27
560      Format(/15X,1A1,'[1;7mAdded Columns:',1A1,'[0m'//
     $          ' Col.  #Pts         Title'/)
         Do 90 I=1,Nadd
            K = Ncols - Nadd + I
            Call Name(Titl,K,Title,Ttllen)
            Write(7,570) K,Npoint(K),(Titl(J),J=1,Ttllen(K))
570         Format(I4,1X,I4,4X,<Ttllen(K)>A1)
 90      Continue
c
      Else
         Loop = 0
      End If
c
c             See if there is another datablock to read in. This is done
c             if a "$" was encountered.  If so, go back to read more data.
c             (This is determined if Iflag, returned from INPTT, = 2)
c
      If (Iflag .EQ. 2) Then
         Iblock = Iblock + 1
         Ipoint = Ipoint + Loop * Nadd
         Go To 10                            ! Go read the next block
      End If
c
 95   Continue
      Close(Unit=12)
      Return
c
 96   Continue
      Write(7,585) 7
585   Format(' ERROR - End of file encountered - routine halted',1A1)
      Close(Unit=12)
      Return
c
 97   Continue
      Write(7,590)
590   Format(' ERROR - Bad filename - routine aborted')
      Return
c
 98   Continue
      Write(7,600)
600   Format(' ERROR encountered in opening the datafile - routine',
     $        ' aborted')
      Return
c
      End
      Subroutine INPTT(Array,Npoint,Istop)
c
c ************************************************************************
c *                                                                      *
c *        Subroutine Inptt - To input real data, when the number of     *
c *        points in an input line are unknown.  Similar to INPT,        *
c *        but this reads files attached to unit 12.                     *
c *                                                                      *
c *        Input values: None                                            *
c *                                                                      *
c *        Output values:                                                *
c *                                                                      *
c *        Array  - The array in which the points read in are stored     *
c *        Npoint - The number of points read in                         *
c *        Istop  - A flag to indicate whether a "$" (Istop = 2),        *
c *                 or end-of-file (Istop = 1) was encountered.          *
c *                                                                      *
c *        Programmed by Steven C. Fisher, DTNSRDC Code 1522             *
c *                                                                      *
c ************************************************************************
c
      Dimension Array(1)
      Byte Indata(132),Period,Plus,Minus,Zero,Nine

c
c          Set up the control characters to look for
c
      Data Period,Plus,Minus,Zero,Nine/'.','+','-','0','9'/
c
      Istop = 0
      Npoint = 0
10    Continue
c
c          Read in the character string
c
      Read(12,100,END=99,ERR=99) Nchars, (Indata(I),I=1,Nchars)
100   Format(Q,132A1)
c
c           If a "$" is encountered in the first column, a new set of data is
c           next, so set the flag and return.
c
      If (Indata(1) .EQ. '$' .AND. Nchars .GT. 0) Then
         Istop = 2
         Return
      End If
c
c          See if there are any numbers in the string
c
      Do 30 I=1,Nchars
         If (Indata(I) .GE. Zero .AND. Indata(I) .LE. Nine) Go To 40
30    Continue
40    Continue
c
c          If there are no numbers, try reading again
c
      If (I .GT. Nchars) Go To 10
c
c          Find the starting position of the current number
c
      Istart = 1
50    Continue
      Do 60 I=Istart,Nchars
         If ((Indata(I) .GE. Zero .AND. Indata(I) .LE. Nine) .OR.
     $      (Indata(I) .EQ. Plus) .OR. (Indata(I) .EQ. Minus) .OR.
     $      (Indata(I) .EQ. Period) .OR. Indata(I) .EQ. 'E' .OR.
     $       Indata(I) .EQ. 'e') Go To 70
60    Continue
c
c          If no numbers left (Istart greater than Nchars) then exit
c
      Go To 95
70    Continue
      Istart = I
      If (Istart .EQ. Nchars+1) Go To 95
c
c          Find the ending position of the current number
c
      Iperd = 0
      Ieflag = 0
      Ierr = 0
      If (Indata(Istart) .EQ. Period) Iperd = 1
      If (Indata(Istart) .EQ. 'E' .OR. Indata(Istart) .EQ. 'e') Ierr = 1
c
      Do 80 I=Istart+1,Nchars
c
c          Look for an ending character - Jump out if found
c
         If ((Indata(I) .LT. Zero .OR. Indata(I) .GT. Nine) .AND.
     $     (Indata(I) .NE. Plus) .AND. (Indata(I) .NE. Minus) .AND.
     $     (Indata(I) .NE. Period) .AND. (Indata(I) .NE. 'E' .AND.
     $     Indata(I) .NE. 'e')) Go To 90
         If (Indata(I) .EQ. Period) Iperd = Iperd + 1
c
c          Check for illegal +/- signs, or multiple periods
c
         If (((Indata(I) .EQ. '-' .OR. Indata(I) .EQ. '+') .AND.
     $        (Indata(I-1) .NE. 'E' .AND. Indata(I) .NE. 'e')) .OR.
     $         Iperd .GT. 1) Ierr = 1
c
c          Check for E format in the input data
c
         If ((Indata(I) .EQ. 'E' .OR. Indata(I) .EQ. 'e') .AND.
     $      (Indata(I+1) .LT. Zero .OR.
     $      Indata(I+1) .GT. Nine) .AND. (Indata(I+1) .NE. Plus) .AND.
     $      (Indata(I+1) .NE. Minus) .AND. (Indata(I+1) .NE. Period))
     $       Go To 90
c
c          See, If we have an exponent, if the exponent is of legal form.
c          If not, set Ierr = 1
c
         If (Indata(I) .EQ. 'E' .OR. Indata(I) .EQ. 'e') Then

c
c             More than 1 "E" found? If so, set error flag
c
            If (Ieflag .GT. 0) Ierr = 1
c
            If (Indata(I+1) .EQ. '+' .OR. Indata(I+1) .EQ. '-') Then
               L = I + 1
            Else
               L = I
            End If
c
            Ieflag = L
c
c                 Exponent greater than +/- 29? If so, set error flag
c
            If ((Indata(L+1) .GT. '2' .AND. Indata(L+1) .LE. Nine)
     $          .AND. (Indata(L+2) .GE. Zero .AND. Indata(L+2)
     $          .LE. Nine)) Ierr = 1
c
c                 Decimal point in exponent? If so set error flag
c
            If (Indata(L+1) .EQ. Period) Ierr = 1
            If (Indata(L+2) .EQ. Period .AND. Indata(L+1) .GE.
     $          Zero .AND. Indata(L+1) .LE. 9) Ierr = 1
         End If
80    Continue
90    Continue
      Ifinis = I
c
c          Check the exponent length (if present), and if an error occured.
c          If it did, then jump to section to notify user of problem
c
      If ((Ieflag .GT. 0 .AND. (Ifinis-Ieflag) .GT. 3) .OR. Ierr .GT.
     $       0) Then
         Iend = Ifinis - 1
         If (Istart-Iend .EQ. 0) Go To 93
         Go To 97
      End If

c
c          Decode the character string and stick it into Array
c
      Numlen = Ifinis-Istart
      Npoint = Npoint + 1
      Decode(Numlen,200,Indata(Istart)) Array(Npoint)
200   Format(E16.0)
c
c          Go back and get the next number
c
 93   Continue
      Istart = Ifinis
      If (Istart .EQ. Nchars) Go To 95
      Go to 50
c
c           See if we are to read another line; if so, loop back
c
 95   Continue
      Return
 97   Continue
c
c           Display the input error to the user, correct it and continue
c
      Write(7,300) 7,27,(Indata(L),L=1,Istart-1),27,(Indata(L),L=
     $             Istart,Iend),27,(Indata(L),L=Iend+1,Nchars),27,27,
     $             27,(Indata(L),L=Istart,Iend),27
300   Format(1X,1A1,1A1,'[1m',<Istart>A1,'[5;7m',<Iend-
     $       Istart+2>A1,'[0;1m',<Nchars-Iend+1>A1,'[0;1m'/
     $      ' ERROR',1A1,'[0m - Bad value encountered - "',1A1,'[1;4m',
     $      <Iend-Istart+2>A1,'[0m - Value Replaced with "1.E20"')
c
      Npoint = Npoint + 1
      Array(Npoint) = 1E20
      Istart = Ifinis
      If (Istart .EQ. Nchars) Return
      Go To 50
c
 99   Continue
c
c              ERROR return for end of file; set Istop to 1
c
      Istop = 1
c
      Return
      End
