      SUBROUTINE TERM(ITERM,CL,DSP,ISP,JSP,IFL,JIN,HY,CW,CH)
C
      COMMON /SCN/ XS,XF,YS,YF,XXS,XXF,YYS,YYF,CSTR,CFIN,CYMIN,CYMAX,STR
     1,FIN,YMIN,YMAX,ZZ
      BYTE ITERM,CL,DSP,ISP,JSP,IFL,JIN
C
      IF(ITERM.EQ.2) CALL T4662(0,0)
      XS=0.0
      XF=1023.0
      IF(ITERM.EQ.6) XF=1020.
      YS=0.0
      YF=760.0  !  MFG -- CHANGED FOR ZENITH TERMINAL (ORIG YF=766.0)
      IF(ITERM.EQ.6) YF=744. ! VT240 MOD
      IF(ITERM.EQ.1) YF=670.0
      CW=14 ! ALPH CHARACTER WIDTH
      CH=22  ! ALPH CHARACTER HEIGHT
      IF(ITERM.EQ.1.OR.ITERM.EQ.6) CH=28
      IF(ITERM.EQ.6) CW=18
      XXS=XS+7*CW
      XXF=XF-2*CW
      IF(ITERM.EQ.4) XXF=XF
      IF(ISP.NE.2) GO TO 110
      IF(IFL.EQ.2) YF=YF/2.0
      IF(IFL.EQ.3) YF=YF-17.0
      IF(IFL.EQ.3) YS=373.0
  110 YYF=YF-3*CH
      IF(ISP.EQ.2) YYF=YF-CH
      YYS=YS+2*CH+6
      IF(ISP.NE.3) GO TO 120
      YYS=YYS+HY*(YYF-YYS)
      YYF=YYF+HY*(YYF-YYS)
  120 CONTINUE
      I=-1
      IF(ITERM.GE.3.AND.IFL.EQ.1) CALL ENVIS(I)
      RETURN
      END
C
      SUBROUTINE ENVIS(ISTAT)
      RETURN
      END
C
      SUBROUTINE VT240(ISTAT)
C
      BYTE ESC
C
C
C   ISTAT=1 RESET & RETURN TO NATIVE VT200 MODE
C   ISTAT=2 SET 4010 MODE & MAKE SCREEN BRIGHTER
C   ISTAT=3 ERASE SCREEN & HOME CURSOR
C
      ESC=27
C
      IF(ISTAT.EQ.1) THEN
        WRITE(7,1000) ESC
 1000   FORMAT(1X,A1,'[?38l')
        write(7,1010) ESC
 1010   FORMAT(1X,A1,'[62;1"p"')
        RETURN
      ENDIF
C
      IF(ISTAT.EQ.2) THEN
        WRITE(7,1020) ESC
 1020   FORMAT(1X,A1,'[?38h')
        write(7,1030) esc
 1030   format(1x,a1,'h')
        return
      endif
c
      if(istat.eq.3) then
        write(7,1040) esc
 1040   format(1x,a1,'[2J')
        write(7,1050) esc
 1050   format(1x,a1,'[?6l')
        return
      endif
c
      return
      end
C
      SUBROUTINE T4027(C1,WSPC,ISTAT)
C                                                               T4027
C   ISTAT=1  RESET
C   ISTAT=2  SEND SET SEQUENCE
C   ISTAT=3  SEND COLOR ONLY
      INTEGER C1,WSPC
      IF(ISTAT.EQ.1) WRITE(7,100)
  100 FORMAT(1X,'!WORKSPACE 0')
      IF(ISTAT.EQ.1) RETURN
      IF(ISTAT.EQ.3) WRITE(7,150) C1
  150 FORMAT(1X,'!COLOR ',A2)
      IF(ISTAT.EQ.2) GO TO 200
      RETURN
  200 WRITE(7,250)
  250 FORMAT(1X,'!ERASE')
      WRITE(7,300) WSPC
  300 FORMAT(1X,'!WORKSPACE ',A2)
      WRITE(7,510)
  510 FORMAT(1X,'!PAD 209')
      WRITE(7,520)
  520 FORMAT(1X,'!PAD 207')
      WRITE(7,400) WSPC
  400 FORMAT(1X,'!GRAPHIC 1,',A2)
      WRITE(7,500)
  500 FORMAT(1X,'!SHRINK')
      WRITE(7,600) C1
  600 FORMAT(1X,'!COLOR ',A2)
      WRITE(7,700)
  700 FORMAT(1X,'!WORKSPACE H')
      RETURN
      END
C
      SUBROUTINE T4662(PON,PEN)
C                                                               T4662
      INTEGER PON,PEN,ESC
      ESC="033
C  PON=0 PLOTTER OFF
C  PON=1 PLOTTER ON
C
C  PEN IS PEN NUMBER (COLOR)
C
      IF(PON.EQ.0) WRITE(7,10)ESC
   10 FORMAT(1X,A1,'AF')
      IF(PON.EQ.1) WRITE(7,20)ESC
   20 FORMAT(1X,A1,'AE')
      IF(PEN.GT.0) WRITE(7,30)ESC,PEN
   30 FORMAT(1X,A1,'ABP',I1)
      RETURN
      END

      SUBROUTINE TPLOT(I,X,Y)
C                                                           **********
C                                                           **TPLOT **
C                                                           **********
CLAB= TPLOT
C VALUE OF I DETERMINES IF BEAM ON OR OFF
C   I=-1 EMPTY CONTENTS OF BUFFER AND GO INTO ALPHA MODE
C   I=0  WITH BEAM OFF GO TO LOCATION X,Y
C   I=1  WITH BEAM ON GO TO LOCATION X,Y
C   I=2  SAME AS I=0 BUT BUFFER CONTENTS ARE EMPTIED AND
C        RETURN TO ALPHA MODE
C
C   GS= CHARACTER TO PUT TEKTRONIX INTO GRAPH MODE
C   US=     @                @       @  ALPHA MODE
      INTEGER BITE(4),CHAR(132)
      INTEGER GS,US
	BYTE BCHAR(132)
      DATA GS,US/29,31/
      DATA N/1/
      IY=Y
      IX=X
      GO TO (6,7,9,7),I+2
    6 I=2
      GO TO 8
    7 CHAR(N)=GS
      N=N+1
    9 BITE(1)=(IY/32)
      BITE(2)=MOD(IY,32)+64
      BITE(3)=IX/32
      BITE(4)=MOD(IX,32)+32
   17 DO 20 II=1,4
      CHAR(N)=BITE(II)-96
   20 N=N+1
      IF(I.EQ.2) GO TO 8
      IF(N+6.LE.132) RETURN
    8 CHAR(N)=US
      DO 10 J=1,N
      BCHAR(J)=CHAR(J)
      IF(BCHAR(J).EQ.127) BCHAR(J)=126
      IF(BCHAR(J).EQ.27) BCHAR(J)=26
   10 CONTINUE
      CALL OUTPUT(BCHAR,N)
      N=1
      IF(I.EQ.2) RETURN
      DO 15 J=1,4
   15 CHAR(J+1)=BITE(J)-96
      CHAR(1)=GS
      N=6
  100 RETURN
      END

	SUBROUTINE OUTPUT(STRING,NCHARS)                              ! OUTPUT
	BYTE	STRING(NCHARS)
	WRITE(7,100) STRING
100	FORMAT('+',$,132A1)
	RETURN
	END

      SUBROUTINE CURSOR(ID,X,Y,ITERM)
C                                                           **********
C                                                           **CURSOR**
C                                                           **********
C NOTE: ID CANNOT = COMMERCIAL AT SIGN
      DIMENSION IN(4)
	INTEGER CHARS
      BYTE ITERM
C
	ENCODE(2,300,CHARS) "33,"32
300	FORMAT(2A1)
      IF(ITERM.EQ.1) WRITE(7,100)
  100 FORMAT(1X,'!MON H')
	CALL OUTPUT(CHARS,2)
      READ(5,25) ID,(IN(I),I=1,4)
   25 FORMAT(5A1)
      WRITE(5,26)
   26 FORMAT(1X)
      IF(ITERM.EQ.1) WRITE(7,200)
  200 FORMAT(1X,'!WOR H')
      DO 30 I=1,4
      IN(I)=IAND(IN(I),31)
   30 CONTINUE
      X=FLOAT(32*IN(1) + IN(2))
      Y=FLOAT(32*IN(3) + IN(4))
   10 CONTINUE
      RETURN
      END
C
	SUBROUTINE CLEAR(ITERM)
C
C  ISSUE APPROPRIATE CLEAR SCREEN COMMAND
C
	BYTE ITERM,CHAR1,CHAR2,CHAR3,CHAR4,CHAR5
	BYTE CH1,CH2
C
        IF(ITERM.EQ.6) THEN
          CALL VT240(3)
          RETURN
        ENDIF
C
	IF(ITERM.EQ.1.OR.ITERM.EQ.3.OR.ITERM.EQ.4)GO TO 200
	CH1="32     !ASCII SUB  - ADM3A CLEAR SCREEN
	CH2="36     !ASCII RS - ADM3A HOME
	WRITE(6,9001)CH1,CH2!    CLEAR SCREEN FOR ADM3A
9001	FORMAT(1X,2A1)
	CHAR1="33 ! ASCII ESC  
	CHAR2="14
	WRITE(6,9000)CHAR1,CHAR2    !  CLEAR SCREEN FOR 4010
9000	FORMAT(1X,A1,A1)
	CALL WAIT(1,2,ISTAT)
	GO TO 999
C ENVISION TERMINALS
200	IF(ITERM.NE.3.AND.ITERM.NE.4)GO TO 300
	CHAR1=27 ! ASCII ESC
	CHAR2='['
	CHAR3='2'
	CHAR4='J'
	CHAR5='H'
	WRITE(6,9200)CHAR1,CHAR2,CHAR3,CHAR4    ! CLEAR SCREEN
9200	FORMAT('+',$,4A1)
	WRITE(6,9210)CHAR1,CHAR2,CHAR5       ! HOME CURSOR
9210	FORMAT('+',$,3A1)
  300 IF(ITERM.NE.1) GO TO 999
      CALL T4027(I,J,1)
C
999	RETURN
	END
C
      SUBROUTINE ERASE(ITERM,DSP,XW,YW,YF,CH,NN)
C                                                                     ERASE
	BYTE	IERASE(2),ITERM,DSP,ESC,BUF(4)
C
      XW=0
      YW=YF-CH
      NN=0  ! RESET CURSOR NUMBER
      IF(ITERM.EQ.2) RETURN  ! PLOTTER
      IF(ITERM.EQ.3.OR.ITERM.EQ.4) GO TO 100  ! ENVISION
	ENCODE(2,300,IERASE) "033,"014
300	FORMAT(2A1)

	CALL OUTPUT(IERASE,2)
	CALL WAIT(1,2,ISTAT)
	RETURN
  100 ESC=27
      ENCODE(4,1030,BUF) ESC
 1030 FORMAT(A1,'RG1')  ! TURN GRAPH MODE ON
      CALL OUTPUT(BUF,4)
      ENCODE(3,1020,BUF) ESC
 1020 FORMAT(A1,'F') ! GRAPHICS ERASE
      CALL OUTPUT(BUF,2)
      ENCODE(4,1010,BUF) ESC
 1010 FORMAT(A1,'[2J') ! ALPH ERASE
      CALL OUTPUT(BUF,4)
      ENCODE(3,1000,BUF) ESC
 1000 FORMAT(A1,'[H')  ! HOME ALPH CURSOR
      CALL OUTPUT(BUF,3)
      ENCODE(4,1001,BUF) ESC
 1001 FORMAT(A1,'RA0')  ! TURN OFF ALPH CURSOR
      CALL OUTPUT(BUF,4)
      RETURN
      END

      SUBROUTINE X2Y2(X,Y,N,CW,CH)
C                                                           **********
C                                                           ** X2Y2 **
C                                                           **********
C PART OF INTER
C WRITES THE NUMBER OVER THE PEAK
      INTEGER N
      BYTE BUF(2)
      X2=X-CH
      Y2=Y+CW
      N=N+1
      CALL TPLOT(2,X2,Y2)
      ENCODE(2,52,BUF) N
   52 FORMAT(I2)
      CALL OUTPUT(BUF,2)
      RETURN
      END
      SUBROUTINE PPX(XW,YW,PX,N,XS,XF,XL,IN,H,CW,CH)
C                                                           **********
C                                                           **  PPX **
C                                                           **********
C PPX IS PART OF INTER
C WRITES THE RESULT OF TOP (PARABOLIC FIT)
      INTEGER N
C
      W=14*CW
      CALL XWYW(XW,YW,XS,XF,W,CW,CH)
      I=PX
      IF(PX.LT.10000.0) GO TO 10
      WRITE(7,11) N,I
   11 FORMAT('+',$,'P(',I2,')=',I7)
      GO TO 37
   10 IF(PX.LT.1000.0) GO TO 34
      WRITE(7,57) N,PX
   57 FORMAT('+',$,'P(',I2,')=',F7.2)
      GO TO 37
   34 IF(PX.LT.100.0) GO TO 35
      WRITE(7,71) N,PX
   71 FORMAT('+',$,'P(',I2,')=',F7.3)
      GO TO 37
   35 WRITE(7,59) N,PX
   59 FORMAT('+',$,'P(',I2,')=',F7.4)
   37 XW=XW + 14*CW
      W=11*CW
      CALL XWYW(XW,YW,XS,XF,W,CW,CH)
C ADDED 3/85 RPG
	IF(IN.EQ.'ED') THEN
	Q=12.4/PX
	WRITE(7,300) Q
	WRITE(8,61)
	WRITE(8,1000) PX,Q,H
1000	FORMAT(1H+,3X,F9.3,F9.4,F9.2)
	GO TO 38
	ENDIF
C
      IF(IN.EQ.'DI') PXX=XL/(2.0*SIN(0.008726646*PX))
      IF(IN.EQ.'XR') PXX=XL*SIN(0.008726646*PX)
      Q=1./(PXX*PXX)
      IF(IN.EQ.'XR') Q=12.4/PXX
      WRITE(8,61)
   61 FORMAT(1X)
      WRITE(8,60) PX,PXX,Q,H
   60 FORMAT(1H+,3X,F9.3,2F9.4,F9.1)
      IF(PXX.LT.10.0)GO TO 12
      IF(IN.EQ.'DI') WRITE(7,1) PXX
    1 FORMAT('+',$,'D=',F7.3)
      IF(IN.EQ.'XR') WRITE(7,100) PXX
  100 FORMAT('+',$,' L=',F7.3)
      GO TO 38
   12 IF(PXX.LT.1.0)GO TO 13
      IF(IN.EQ.'DI') WRITE(7,2) PXX
    2 FORMAT('+',$,'D=',F7.4)
      IF(IN.EQ.'XR') WRITE(7,200) PXX
  200 FORMAT('+',$,' L=',F7.4)
      GO TO 38
   13 CONTINUE
      IF(IN.EQ.'DI') WRITE(7,3) PXX
    3 FORMAT('+',$,'D=',F7.5)
      IF(IN.EQ.'XR') WRITE(7,300) PXX
  300 FORMAT('+',$,' L=',F7.5)
   38 XW=XW+W
      RETURN
      END

      SUBROUTINE XWYW(XW,YW,XS,XF,PLUS,CW,CH)
C                                                           **********
C                                                           ** XWYW **
C                                                           **********
C PART OF INTER
C ASK HAS THE END OF THE LINE BEEN REACHED IF SO
C START A NEW LINE AND WRITE THE VALUE THERE
C
C
      XFF=XF
      IF(XS.GT.XF) XFF=XS
C IF XS.GT.XF THE PLOT HAS BEEN PLOTTED IN REVERSE
      IF(XFF.GT.(XW+PLUS)) GO TO 10
      YW=YW-CH
      XW=XS
      IF(XS.GT.XF) XW=XF
   10 CALL TPLOT(2,XW,YW)
      RETURN
      END                                                                       
