      Subroutine STARTP
c
c ***********************************************************************
c *                                                                     *
c *        Subroutine STARTP - To initialize the plotting proceedure    *
c *        and to select the plot device.                               *
c *                                                                     *
c *        Variables set:                                               *
c *                                                                     *
c *        Iterm   - If plot device is a terminal, set Iterm = 1        *
c *                   Otherwise, set Iterm = 0                          *
c *        Irot    - If plot device can plot a rotated plot, set        *
c *                   Irot = 1; otherwise, set Irot = 0                 *
c *        Igraph  - If the device is to plot on graph paper, set       *
c *                   Igraph = 1; otherwise, set Igraph = 0             *
c *        Xplotl  - The length of the plot region in the X direction   *
c *        Yplotl  - The length of the plot region in the Y direction   *
c *                                                                     *
c *                      FOR PLOTTING ON GRAPH PAPER:                   *
c *                                                                     *
c *        Xr0     - The distance from the left edge of the paper to    *
c *                   the center of the plotting region, in inches.     *
c *        Yr0     - The distance from the bottom edge of the paper     *
c *                   to the center of the plotting region, in inches.  *
c *        Xr90    - The distance from the right edge of the paper      *
c *                   (when the paper is rotated 90 degrees) to the     *
c *                   center of the plotting region, in inches.         *
c *                   Used if the plot device can rotate plots 90 deg.  *
c *        Yr90    - The distance from the bottom edge of the paper     *
c *                   (when the paper is rotated 90 degrees) to the     *
c *                   center of the plotting region, in inches.         *
c *                   Used if the plot device can rotate plots 90 deg.  *
c *                                                                     *
c *       Programmed by Steven C. Fisher, DTNSRDC Code 1522             *
c *                                                                     *
c ***********************************************************************
c
      Integer Spchar,Xtop
      Character*2 Lines
      Character*15 Filnam,Line*80,Linea*80,Idevnm*4,Vals*80
      Byte Lineno(2),Linbyt(80),Id(4)
c
      Equivalence (Lines,Lineno),(Linbyt,Line),(Id,Idevnm)
c
      Common/Ptype/Idev,Xtop,Spchar,Chscal,Chsc1
      Common/Center/Xpmin,Xpmax,Ypmin,Ypmax,Xorg,Yorg,Xplotl,Yplotl
      Common/Pltfil/Lineno,Iterm
      Common/Graphp/Xr0,Yr0,Xr90,Yr90
      Common/Ends/Ireset,Icflag,Iout
c
c           Close the terminal.
c
      Close(Unit=7)
c
c           Setups for the various plot devices.  Here we set the plot region
c           size, flags for terminals, graph paper, and plot rotation,
c           initialize the plot, and set up the command when program execution
c           halts.
c
      Open(Unit=14,Name='DP:PLTPRG.DVC',Type='OLD',Readonly)
c
      Do 50 I=1,Idev
c
c             Get the device name
c
 10      Continue
         Read(14,100) Line
100      Format(A)
         If (Line(1:1) .EQ. '!') Go To 10
         Idevnm(1:4) = Line(1:4)
c
c             Get the output device name
c
 20      Continue
         Read(14,150) Linbyt
150      Format(80A1)
         If (Linbyt(1) .EQ. '!') Go To 20
         Do 21 J=1,20
            If (Linbyt(J) .GT. 'Z') Linbyt(J) = Linbyt(J) - 32
 21      Continue
c
c             Get the plot device information
c
 23      Continue
         Read(14,160) Nchars,Vals
160      Format(Q,A)
         If (Vals(1:1) .EQ. '!') Go To 23
         Do 24 J=2,Nchars
            If (Vals(J:J) .EQ. ' ' .AND. Vals(J-1:J-1) .NE. ',' .AND.
     $          Vals(J-1:J-1) .NE. ' ') Vals(J:J) = ','
 24      Continue
         Read(Vals(1:Nchars),'(2f10.0,3I5,4F10.0)') Xplotl,Yplotl,
     $               Iterm,Irot,Igraph,(Xr0,Yr0,Xr90,Yr90,J=1,Igraph)
c
 25      Continue
         Read(14,160) Nchars,Vals
         If (Vals(1:1) .EQ. '!') Go To 25
         Do 26 J=2,Nchars
            If (Vals(J:J) .EQ. ' ' .AND. Vals(J-1:J-1) .NE. ',' .AND.
     $          Vals(J-1:J-1) .NE. ' ') Vals(J:J) = ','
 26      Continue
         Read(Vals(1:Nchars),'(5I10)')  Iset,Iscale,Iscalr,
     $                                    Ireset,Icflag
c
         If (I .NE. Idev) Then
             Do 40 J=1,Iset+Iscale+Iscalr+Ireset+Icflag
 30             Continue
                Read(14,100) Linea
                If (Linea(1:1) .EQ. '!') Go To 30
 40          Continue
         End If
 50   Continue
c
c             Set the unit number to send the output to
c
         If (Iterm .EQ. 1) Then
            Iout = 7
         Else
            Iout = 15
         End If
c
c             Open the plot device
c
         If (Line(1:8) .EQ. 'FILENAME') Then
            Filnam(1:11)  = 'WP:PLTOUT.L'
            Filnam(12:13) = Lines
            Call Plopen(Filnam,Iout,Id,'TSX ')
         Else
            Call Plopen(Linbyt,Iout,Id,'TSX ')
         End If
c
c            Get the plot scaling. Note: If Irot was set > 0, and is
c            returned by Scaler as 2, the plot is to be rotated 90 degrees.
c
      Call Scaler(Irot,Irotat,Igraph)
c
c            Initialize the plotting device if necessary
c
      Do 60 I=1,Iset
 55      Continue
         Read(14,100) Line
         If (Line(1:1) .EQ. '!') Go To 55
         Write(Iout,200) Line
200      Format(1X,A)
 60   Continue
c
      Call Scinit
c
c             If necessary, read additional scaling info for the plot
c
c                If the plot is not rotated;
c
      Do 70 I=1,Iscale
 65      Continue
         Read(14,100) Line
         If (Line(1:1) .EQ. '!') Go To 65
         If (Irotat .NE. 2) Write(Iout,200) Line
 70   Continue
c
c             If plot is rotated 90 degrees;
c
      Do 80 I=1,Iscalr
 75      Continue
         Read(14,100) Line
         If (Line(1:1) .EQ. '!') Go To 75
         If (Irotat .EQ. 2) Write(Iout,200) Line
 80   Continue
c
c             Define the screen
c
      Call Scrdef(0.0,Xplotl,0.0,Yplotl)
c
c             Plot rotation for MGSPLB version 1.4
c
c      If ((Irotat .EQ. 2 .AND. Irot .EQ. 2) .OR. (Irotat .EQ. 1
c     $           .AND. Irot .EQ. 4) .OR. Irot .EQ. 6) Then
c         Call Scrdef(0.0,Yplotl,0.0,Yplotl)
c         Call Scrrot(90.0)
c         Call Scrtrn(0.0,-Yplotl)
c         Call Scrscl(Yplotl/Xplotl,1.0)
c      Else
c         Call Scrdef(0.0,Xplotl,0.0,Yplotl)
c      End If
c
      Return
      End
      Subroutine Scaler(Irot,Irotat,Igraph)
c
c ***********************************************************************
c *                                                                     *
c *        Subroutine Scaler - To scale the plot to fit the plot area   *
c *                                                                     *
c *           Programmed by Steven C. Fisher, DTNSRDC Code 1522         *
c *                                                                     *
c ***********************************************************************
c
      Dimension Xmin(2),Ymin(2),Xmax(2),Ymax(2),Xinc(2),Yinc(2)
      Integer Xnumbr,Ynumbr,Axchst,Axtypx,Axtypy,Axcolr,Ticdir
      Byte Xaxis(2,80),Yaxis(2,80)
      Integer Spchar,Atxclr,Xtop
c
      Dimension Siznum(10)
      Byte Titl(80)
c
      Common/Axis/Xmin,Xmax,Ymin,Ymax,Xinc,Yinc,Xlen,Ylen,Xnumbr,
     $ Ynumbr,Xaxis,Yaxis,Axthik,Axchst,Axtypx,Axtypy,Axcolr,Textsz,
     $ Ticdir,Atxclr
      Common/Ptype/Idev,Xtop,Spchar,Chscal,Chsc1
      Common/Center/Xpmin,Xpmax,Ypmin,Ypmax,Xorg,Yorg,Xplotl,Yplotl
      Common/Pscale/Delx,Dely,Space
      Common/Graphp/Xr0,Yr0,Xr90,Yr90
c
      Data Siznum/4*0.0312,3*0.0328,2*0.0312,0.0375/
c
c              Establish initial value for Irotat
c
      If (Irot .EQ. 0) Then
         Irotat = 0
      Else If (Irot .EQ. 1 .OR. Irot .EQ. 2) Then
         Irotat = 1
      Else If (Irot .EQ. 3 .OR. Irot .EQ. 4) Then
         Irotat = 1
         Xhold  = Xplotl
         Xplotl = Yplotl
         Yplotl = Xhold
      Else
         Irotat = 0
         Xhold  = Xplotl
         Xplotl = Yplotl
         Yplotl = Xhold
      End If
c
c              Compute the leftmost and rightmost spots on the plots
c
      Xpmink = Xpmin
      Xpmaxk = Xpmax
      Ypmink = Ypmin
      Ypmaxk = Ypmax
c
 5    Continue
c
      If (Xnumbr .EQ. 2 .AND. Xtop .EQ. 1) Then
         If (Ticdir .NE. 3) Then
            Dx = Yplotl/80.0
         Else
            Dx = 0.0
         End If
         Call Getl(Xaxis(1,1),1,Ilen,Titl)
         Call Supsub(Titl,Ilen,Iup,Idown)
         Adown = 0.4 * Idown
         If (Axtypx .EQ. 1) Then
            Yloc = - Dx - (2.6+2.3+Adown)*Textsz*0.042 -
     $             Axthik/2.0
         Else
            Yloc = - Dx - (3.5+2.3+Adown)*Textsz*0.042 -
     $             Axthik/2.0
         End If
         Call Getl(Xaxis(1,1),2,Ilen,Titl)
         Call Supsub(Titl,Ilen,Iup,Idown)
         If (Ticdir .EQ. 2) Then
            Dx = Yplotl/80.0
         Else
            Dx = 0.0
         End If
         If (Axtypx .EQ. 1) Then
            Yp = Yloc - Dx - (2.6+Idown*0.4+1.3)*Textsz*0.042 -
     $           Axthik/2.0
         Else
            Yp = Yloc - Dx - (3.8+Idown*0.4+1.3)*Textsz*0.042 -
     $           Axthik/2.0
         End If
         Ypmin = Amin1(Yp,Ypmin)
      End If
c
      Ypmin = Yplotl/2.0 + Ypmin-Ylen/2.0
      Ypmax = Yplotl/2.0 + Ypmax-Ylen/2.0
c
c              Compute the X axis leftmost point
c
      Call Getl(Yaxis(1,1),1,Ilen,Titl)
      Call Supsub(Titl,Ilen,Iup,Idown)
      If (Idown .GT. 3) Iup = 5
      Xp = Xplotl/2.0 - Xlen / 2.0 - Axthik/2.0 - Iup * 0.042 *
     $                  Textsz * 0.3
c
      If (Axtypy .EQ. 1) Then
c
c             For normal y axes:
c
         Ysiz = 0.042 * Textsz/Ylen*(Ymax(1)-Ymin(1))
         Yincre = Yinc(1)
 6       Continue
         If (Ysiz .LE. Yincre/3.0) Go To 7
         Yincre = Yincre * 2.0
         Call Tikchk(Yincre,Yinc(1))
         Go To 6
 7       Continue
c
         Call Inc(Ymin(1),Ymax(1),Yincre,Isfig,Ideci)
         Call Comcnt(Ymin(1),Ymax(1),Yincre,Icom)
c
         If (Axchst .EQ. 0) Then
            Xp = Xp - (Ideci+Isfig+3.0+Icom)*0.035*Textsz*1.5 -
     $                 1.2*0.042 * Textsz - Iup * Textsz * 0.3 *
     $                 0.042
         Else If (Spchar .GT. 1) Then
            Xp = Xp - ((Isfig+Ideci+3.0) * Siznum(Axchst) + 0.0165 *
     $                  Icom + 0.042) * Textsz
            If (Ideci .GT. 0) Xp = Xp - (0.0165 - Siznum(Axchst))*
     $                        Textsz
         Else
            Xp = Xp - 0.0375 * (Isfig+Ideci+2.9+Icom)*Textsz -
     $                0.042 * Textsz
         End If
      Else
c
c            For logarithmic Y axes;
c
         If (Axchst .EQ. 0) Then
            Xp = Xp - 1.4 * 8.0 * 0.035 * Textsz - 1.2*0.042 *
     $                Textsz - Iup * 0.3 * Textsz * 0.042
         Else If (Spchar .GT. 1) Then
            Xp = Xp - 5.5 * Siznum(Axchst) * Textsz - 0.042 *
     $            Textsz
         Else
            Xp = Xp - 5.5 * 0.0375 * Textsz - 0.042 * Textsz
         End If
      End If
c
      Xpl = Xp
      Xpmin = Amin1(Xplotl/2.0-Xlen/2.0+Xpmin,Xp)
c
c                Now compute the rightmost X location
c
      If (Ynumbr .GT. 1) Then
         Call Getl(Yaxis(1,1),2,Ilen,Titl)
         Call Supsub(Titl,Ilen,Iup,Idown)
         Xp = Xplotl/2.0 + Xlen / 2.0 + Axthik/2.0 + Idown *
     $                     0.042 * Textsz * 0.4! 0.7
         If (Axtypy .EQ. 1) Then
c
c                For normal Y axes;
c
            Ysiz = 0.042 * Textsz/Ylen*(Ymax(2)-Ymin(2))
            Yincre = Yinc(2)
 8          Continue
            If (Ysiz .LE. Yincre/3.0) Go To 9
            Yincre = Yincre * 2.0
            Call Tikchk(Yincre,Yinc(2))
            Go To 8
 9          Continue
            Call Inc(Ymin(2),Ymax(2),Yincre,Isfig,Ideci)
            Call Comcnt(Ymin(2),Ymax(2),Yincre,Icom)
c
            If (Axchst .EQ. 0) Then
               Xp = Xp + (Ideci+Isfig+1.5+Icom+2.0)*1.2*0.035*Textsz +
     $                    1.3 * 0.042 * Textsz
            Else If (Spchar .GT. 1) Then
               Xp = Xp + ((Isfig+Ideci+1.2+1.9) * Siznum(Axchst) +
     $                    0.0165 * Icom) * Textsz + 1.3*0.042 * Textsz
               If (Ideci .GT. 0) Xp = Xp + (0.0165 - Siznum(Axchst)) *
     $                           Textsz
            Else
               Xp = Xp + 0.0375 * (Isfig+Ideci+Icom+1.2+1.9) * Textsz +
     $                   1.3 * 0.042 * Textsz
            End If
         Else
c
c               For logarithmic Y axes;
c
            If (Axchst .EQ. 0) Then
               Xp = Xp + (6.5 * 1.5 * 0.035 + 1.3 * 0.042) * Textsz
            Else If (Spchar .GT. 1) Then
               Xp = Xp + 5.7 * Siznum(Axchst) * Textsz + 1.3 * 0.042 *
     $                Textsz
            Else
               Xp = Xp + 5.7 * 0.0375 * Textsz + 1.3 * 0.042 * Textsz
            End If
         End If
      Else
c
c             No secondary Y axes;
c
         If (Axtypx .EQ. 1) Then
c
c             Normal X axes;
c
            Call Inc(Xmin(1),Xmax(1),Xinc(1),Isfig,Ideci)
            Call Comcnt(Xmin(1),Xmax(1),Xinc(1),Icom)
            If (Axchst .EQ. 0) Then
               Xp1 = (Isfig+Ideci+Icom) * 1.2 * Textsz * 0.035
            Else If (Spchar .GT. 1) Then
               Xp1 = (Siznum(Axchst)*(Isfig+Ideci) + Icom*0.0165) *
     $                Textsz
               If (Ideci .GT. 0) Xp1 = Xp1 - (0.0165 - Siznum(Axchst))*
     $                Textsz
            Else
               Xp1 = 0.0375 * (Isfig+Ideci+Icom) * Textsz
            End If
            Xp2 = 0.0
            If (Xnumbr .GT. 1) Then
               Call Inc(Xmin(2),Xmax(2),Xinc(2),Isfig,Ideci)
               Call Comcnt(Xmin(2),Xmax(2),Xinc(2),Icom)
               If (Axchst .EQ. 0) Then
                  Xp2 = (Isfig+Ideci+Icom) * 1.2 * Textsz * 0.035
               Else If (Spchar .GT. 1) Then
                  Xp2 = (Siznum(Axchst)*(Isfig+Ideci) + Icom*0.0165) *
     $                   Textsz
                  If (Ideci .GT. 0) Xp2 = Xp2 - (0.0165 -
     $                    Siznum(Axchst)) * Textsz
               Else
                  Xp2 = 0.0375 * (Isfig+Ideci+Icom) * Textsz
               End If
            End If
         Else
            If (Axchst .EQ. 0) Then
               Xp1 = 3.5 * 1.2 * Textsz * 0.042
            Else If (Spchar .GT. 1) Then
               Xp1 = 3.8 * Siznum(Axchst) * Textsz
            Else
               Xp1 = 0.0375 * 3.5 * Textsz
            End If
            Xp2 = 0.0
         End If
         Xp = Xplotl/2.0 + Xlen/2.0 + Axthik/2.0 + Amax1(Xp2,Xp1)/2.0
      End If
      Xpmax = Amax1(Xplotl/2.0+Xpmax-Xlen/2.0,Xp)
c
c               Adjust the width for the tick marks
c
      If (Ticdir .EQ. 2) Then
         Lp = Amax1(Xplotl,(Xpmax-Xpmin)*1.025)
         Xpmax = Amax1(Xpmax,Xp+Lp/80.0)
         Xpmin = Amin1(Xpmin,Xpl-Lp/80.0)
      End If
c
      Iflag = 0
c
 10   Continue
c
      Delty = Ypmax - Ypmin
      Deltx = Xpmax - Xpmin
c
c            Compute the axes scale factors
c
      Chscal = 1.0
      Chsc1  = 1.0
c
c            If the plot fits within the plotting area, no scaling is needed
c
      If (Ypmax .LT. Yplotl .AND. Ypmin .GE. 0.0 .AND. Xpmin .GE. 0.0
     $        .AND. Xpmax .LT. Xplotl) Then
         Xorg = Xplotl/2.0
         Yorg = Yplotl/2.0
         Go To 20
      End If
c
c            Set the scaling so that the plot will fit in the Y direction
c
      If (Delty .GT. Yplotl) Then
         Chscal = Yplotl / Delty
      End If
c
c            Now see if the "scaled" plot will fit in the X direction;
c            if not, increase the scale factor
c
      If (Chscal .GT. Xplotl/Deltx) Then
         Chscal = Xplotl / Deltx
      End If
c
c          Compute the effect of rotating the plot 90 degrees; this is
c          only used with "rotatable" devices.
c
      If (Deltx .GT. Yplotl) Then
         Chsc1 = Yplotl / Deltx
      End If
c
      If (Chsc1 .GT. Xplotl/Delty) Then
         Chsc1 = Xplotl / Delty
      End If
c
c          If no rotation is to occur, set Chsc1 to zero;
c
      If (Irot .EQ. 0) Chsc1 = 0.0
c
c          If the plot to be done on graph paper would be reduced, then
c          re-set the plot area to be restricted to the inside plotting area
c
      If (Chscal .LT. 1.0 .AND. Chscal .GE. Chsc1 .AND. Igraph .EQ. 1
     $           .AND. Iflag .EQ. 0) Then
         Ypmax = Yplotl/2.0 + Ylen/2.0
         Ypmin = Yplotl/2.0 - Ylen/2.0
         Xpmax = Xplotl/2.0 + Xlen/2.0
         Xpmin = Xplotl/2.0 - Xlen/2.0
         Iflag = 1
         Go To 10
      End If
c
c         If rotating the plot 90 degrees will allow a correct size
c         plot (or closer to it) then Chsc1 will be greater than 1.0
c
      If (Chsc1 .GT. Chscal .AND. Irotat .EQ. 1) Then
         Xhold  = Xplotl
         Xplotl = Yplotl
         Yplotl = Xhold
         Xpmin  = Xpmink
         Xpmax  = Xpmaxk
         Ypmin  = Ypmink
         Ypmax  = Ypmaxk
         Irotat = 2
         Go To 5
      End If
c
c             Scale the origion location and axes lengths
c
      Yorg = Yplotl/2.0 - Chscal * (Ypmax + Ypmin - Yplotl) / 2.0
      Xorg = Xplotl/2.0 - Chscal * (Xpmax + Xpmin - Xplotl) / 2.0
c
 20   Continue
c
      If (Igraph .EQ. 1) Then
c
c             Make the plot fit on graph paper
c
         If (Irotat .EQ. 2) Then
c
c             If the plot is rotated 90 degrees;
c
            Xv = Xorg - Xplotl/2.0
            Yv = Yorg - Yplotl/2.0
            Xhold = Xr90 - Xlen/2.0 - Xv - Dely
            Yhold = Yr90 - Ylen/2.0 + Yv - Delx
            I = (Xhold + Space/2.0)/Space
            Xbar = I * Space
            I = (Yhold + Space/2.0)/Space
            Ybar = I * Space
            Xorg = Xorg + Xhold - Xbar
            Yorg = Yorg - Yhold + Ybar
         Else
c
            Xv = Xorg - Xplotl/2.0
            Yv = Yorg - Yplotl/2.0
            Xhold = Xr0 - Xlen/2.0 + Xv - Delx
            Yhold = Yr0 - Ylen/2.0 + Yv - Dely
            I = (Xhold + Space/2.0)/Space
            Xbar = I * Space
            I = (Yhold + Space/2.0)/Space
            Ybar = I * Space
            Xorg = Xorg - Xhold + Xbar
            Yorg = Yorg - Yhold + Ybar
         End If
      End If
c
      Xlen = Xlen * Chscal
      Ylen = Ylen * Chscal
      Textsz = Textsz * Chscal
c
      Return
      End
