      Subroutine STARTI
c ***********************************************************************
c *                                                                     *
c *        Subroutine STARTI - 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 *       Programmed by Steven C. Fisher, DTNSRDC Code 1522             *
c *                                                                     *
c ***********************************************************************
c
      Integer Spchar,Xtop
      Common/Ptype/Idev,Xtop,Spchar,Chscal,Chsc1
      Common/Center/Xpmin,Xpmax,Ypmin,Ypmax,Xorg,Yorg,Xplotl,Yplotl
      Common/Tmdata/TmDvTy,TmGrSt,TmGrOf,TmGrCl,TmGrOn,
     $              CrHrOn,CrHrOf
      Character*20  TmDvTy,TmGrSt,TmGrOf,TmGrCl,TmGrOn,CrHrOn,CrHrOf
c      Character*4 Temp
c      Byte Type(4)
c      Equivalence (Temp,Type)
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
      Irot   = 0
      Igraph = 0
      Iterm  = 1
      Call Tmtype(Iterm)
      Call Tmdvin(Iterm)
      Write(7,100) 27,27
100   Format(1X,1A1,'[H',1A1,'[J')
      Call Pltst
      Xplotl = 10.25
      Yplotl = 7.75
c
c             Increase the area to plot so labels can go outside
c             of this region easily
c
      Dx = Xpmax - Xpmin
      Dy = Ypmax - Ypmin
      Xpmax = Xpmax + 0.1 * Dx
      Xpmin = Xpmin - 0.1 * Dx
      Ypmax = Ypmax + 0.30 * Dy
      Ypmin = Ypmin - 0.15 * Dy
c
c      Temp = TmDvty(1:4)
      Close(Unit=7)
      Call Plopen('TT:',7,Tmdvty,'TSX ')
c
      Call Scalr1(Irot,Irotat,Igraph)
      Call Scinit
      Call Scrdef(0.0,Xplotl,0.0,Yplotl)
c
      Return
      End
      Subroutine Setscl
      Common/Cross/Xsmin,Xsmax,Ysmin,Ysmax
      Common/Scrdat/Xleft,Xright,Ybot,Ytop
c
      Call Gtwind(Xwmin,Xwmax,Ywmin,Ywmax)
      Call Gtview(Xvmin,Xvmax,Yvmin,Yvmax)
      Call Gtscrn(Xleft,Xright,Ybot,Ytop)
c
      Dxw = (Xwmax-Xwmin)/(Xvmax-Xvmin)
      Dyw = (Ywmax-Ywmin)/(Yvmax-Yvmin)
c
      Xsmin = Xwmin - (Xvmin-Xleft)*Dxw
      Ysmin = Ywmin - (Yvmin-Ybot)*Dyw
c
      Xsmax = Xsmin + (Xright-Xleft)*Dxw
      Ysmax = Ysmin + (Ytop-Ybot)*Dyw
c
      Return
      End
      Subroutine Labset(Lset)
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)
c
      Byte Titl(80),Char
c
      Integer Atxclr,Spchar,Xtop
c
      Common/Ptype/Idev,Xtop,Spchar,Chscal
      Common/Labls/Nold,Lachst,Lacolr,Nleg,Lechst,Lecolr,Legtit,Labcrd,
     $ Iprtot,Irott
      Common/Axis/Xmin,Xmax,Ymin,Ymax,Xinc,Yinc,Xlen,Ylen,Xnumbr,
     $ Ynumbr,Xaxis,Yaxis,Axthik,Axchst,Axtypx,Axtypy,Axcolr,Textsz,
     $ Ticdir,Atxclr
c
c         Set the labels coordinate system
c
      If (Labcrd .EQ. 0) Then
c
c         Set up local coordinate system
c
         Call Plcord(Xmin(1),Xmax(1),Ymin(1),Ymax(1),Xlen,Ylen)
c
      Else
c
c         Set up global coordinate system
c
         Call Plcord(0.0,1.0,0.0,1.0,Xlen,Ylen)
c
      End If
c
c         Set the viewport clipping off, and then loop for the labels
c
      Call Stclof
      Call Sttxco(Lacolr)   ! Set the labels colors
c
c         Set the character set
c
      If (Lachst .GT. 0) Then
         Call Stsoft
         Call Stcstn(Lachst)
         If(Spchar .GT. 1) Call Stcspa(1,0,0)
      Else
         Call Sthard
      End If
c
      Call Sttpol(0)    ! Do not outline the protected text
      Call Stlint(1)
      Call Stlthc(Axthik)
      Lset = Lachst
c
      Return
      End
      Subroutine Scalr1(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
