    	SUBROUTINE IFSEG_BLINK( NSG )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINES TURNS ON( NSG>0 ) OR TURNS OFF( NSG<0 ) THE
C	BLINKING OF A SEGMENT "NSG" ON TEK-4100 SERIES TERMINALS.
C
    	INCLUDE 'TEKLIB.CBS'

     	IF( CTRL_C .OR. ITK.LT.4106 ) RETURN
C
C  IF BUFFER IS FULL, DUMP IT....
C
    	IF( IPL.GT.NBS-IPK-7 ) CALL IFINIT_DUMP

    	IPB(IPL+1) = 27
    	IPB(IPL+2) = 83
    	IPB(IPL+3) = 72
    	IPL = IPL + 3
     	III = IFSEG_NUMBER(NSG)
    	CALL IFPACK_TEKINT(III)
    	IPL = IPL + 1
    	IF( NSG.GT.0 ) IPB(IPL) = 49
    	IF( NSG.LT.0 ) IPB(IPL) = 48

    	RETURN
    	END

    	SUBROUTINE IFSEG_MOVE( NSG, XAD, YAD )
C
C  PROGRAMMER: R.S. ZHOU	MARCH-1986
C
C	THIS ROUTINE MOVES AN EXISTING GRAPH SEGMENT FROM
C	ITS CURRENT POSITION BY "XAD" AND "YAD"
C
    	INCLUDE 'TEKLIB.CBS'

     	IF( CTRL_C .OR. ITK.LT.4106 ) RETURN
C
C  GET THE ABSOLUTE SCREEN ADDRESS...
C
     	IF( XANG.NE.0.0 .OR. YANG.NE.0.0 ) THEN
     	    XPS = XAD*XCTH-YAD*YSTH
    	    YPS = XAD*XSTH+YAD*YCTH
     	ELSE
     	    XPS = XAD
     	    YPS = YAD
     	ENDIF
     	IXA = IXO + INT(FUNI*(XORS+XSCL*XPS))
    	IYA = IYO + INT(FUNI*(YORS+YSCL*YPS))
C
C  IF BUFFER IS FULL, DUMP IT...
C
    	IF( IPL.GT.NBS-IPK-11 ) CALL IFINIT_DUMP
C
C  SET SEGMENT POSITION: <ESC>SX seg-number x-y coords
C
    	IPB(IPL+1) = 27
    	IPB(IPL+2) = 83
    	IPB(IPL+3) = 88
    	IPL = IPL + 3
     	III = IFSEG_NUMBER(NSG)
    	CALL IFPACK_TEKINT(III)			!pack the segment number
    	CALL IFPACK_TEKXY( IXA, IYA ) 		!pack the position

    	RETURN
    	END

    	SUBROUTINE IFSEG_OPEN( ICODE )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE DOES THE FOLLOWINGS:
C
C	ICODE =999999: DELETE ALL SEGMENTS FROM THE MEMERY
C	ICODE = 0 : CLOSE THE CURRENT GRAPH SEGMENT
C	ICODE > 0 : OPEN A NEW GRAPH SEGMENT OF NUMBER "ICODE"
C	ICODE < 0 : DELETE A GRAPH SEGMENT OF NUMBER "ICODE"
C
    	INCLUDE 'TEKLIB.CBS'

     	IF( CTRL_C .OR. ITK.LT.4106 ) RETURN

    	ICD = ICODE
    	IF( ICD.EQ.0 ) THEN			!CLOSE THE SEGMENT
    	    IF( IPL.GT.NBS-IPK-3 ) CALL IFINIT_DUMP
    	    IPB(IPL+1) = 27			!<ESC>SC
    	    IPB(IPL+2) = 83
    	    IPB(IPL+3) = 67
    	    IPL = IPL + 3
    	ENDIF
    	IF( ICD.GT.0 .AND. ICD.LT.999999 ) THEN	!OPEN A NEW SEGMENT
    	    IF( IPL.GT.NBS-IPK-6 ) CALL IFINIT_DUMP
    	    IPB(IPL+1) = 27			!<ESC>SO segment-number
    	    IPB(IPL+2) = 83
    	    IPB(IPL+3) = 79
    	    IPL = IPL + 3
     	    III = IFSEG_NUMBER(ICD)
    	    CALL IFPACK_TEKINT(III)
    	ENDIF
    	IF( ICD.LT.0 ) THEN			!DELETE A SEGEMENT
    	    IF( IPL.GT.NBS-IPK-6 ) CALL IFINIT_DUMP
    	    IPB(IPL+1) = 27			!<ESC>SK segment-number
    	    IPB(IPL+2) = 83
    	    IPB(IPL+3) = 75
    	    IPL = IPL + 3
     	    III = IFSEG_NUMBER(ICD)
    	    CALL IFPACK_TEKINT(III)
    	ENDIF
    	IF( ICD.EQ.999999 ) THEN		!DELETE ALL SEGMENTS
    	    IF( IPL.GT.NBS-IPK-10 ) CALL IFINIT_DUMP
    	    IPB(IPL+1) = 27			!DELETE THE CURRENT VIEW
    	    IPB(IPL+2) = 82
    	    IPB(IPL+3) = 75
    	    IPB(IPL+4) = 48
    	    IPB(IPL+5) = 27			!DELETE ALL SEGMENTS
    	    IPB(IPL+6) = 83
    	    IPB(IPL+7) = 75
    	    IPL = IPL + 7
    	    CALL IFPACK_TEKINT( -1 )
    	ENDIF

    	RETURN
    	END

    	SUBROUTINE IFSEG_RENAME( NOLD, NNEW )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE RENUMBERS AN EXISTING GRAPH SEGMENT
C	FROM "NOLD" TO "NNEW".
C
    	INCLUDE 'TEKLIB.CBS'

     	IF( CTRL_C .OR. ITK.LT.4106 .OR. NOLD.EQ.NNEW ) RETURN
C
C  IF BUFFER IS FULL, DUMP IT...
C
    	IF( IPL.GT.NBS-IPK-9 ) CALL IFINIT_DUMP

    	IPB(IPL+1) = 27		!<ESC>SR old-number new-number
    	IPB(IPL+2) = 83
    	IPB(IPL+3) = 82
    	IPL = IPL + 3
     	III = IFSEG_NUMBER(NOLD)
    	CALL IFPACK_TEKINT( III)
     	III = IFSEG_NUMBER(NNEW)
    	CALL IFPACK_TEKINT( III)

    	RETURN
    	END

    	SUBROUTINE IFSEG_TRAN( NSEG, XAD,YAD, ANGLE, XFT, YFT )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE TRANSFORMS A GRAPH SEGMENT OF NUMBER "NSEG"
C	BY "XAD" AND "YAD" WITH A ROTATION SPECIFIED BY "ANGLE"
C	IN DEGREE VIA TEK-4100 COMMANDS.
C
C	XFT, YFT --- SCALE FACTORS IN X,Y DIMENSIONS
C
    	INCLUDE 'TEKLIB.CBS'

     	IF( CTRL_C .OR. ITK.LT.4106 ) RETURN
C
C  GET THE ABSOLUTE SCREEN ADDRESS...
C
     	IF( XANG.NE.0.0 .OR. YANG.NE.0.0 ) THEN
     	    XPS = XAD*XCTH-YAD*YSTH
    	    YPS = XAD*XSTH+YAD*YCTH
     	ELSE
     	    XPS = XAD
     	    YPS = YAD
     	ENDIF
     	IXA = IXO + INT(FUNI*(XORS+XSCL*XPS))
    	IYA = IYO + INT(FUNI*(YORS+YSCL*YPS))
C
C  IF BUFFER IS FULL, DUMP IT...
C
    	IF( IPL.GT.NBS-IPK-29 ) CALL IFINIT_DUMP
C
C  SET SEGMENT IMAGE TRANSFORM:
C  <ESC>SI segment-number x-scale-factor
C  y-scale-factor rotation-angle postion
C
    	IPB(IPL+1) = 27
    	IPB(IPL+2) = 83
    	IPB(IPL+3) = 73
    	IPL = IPL + 3
     	III = IFSEG_NUMBER(NSEG)
    	CALL IFPACK_TEKINT( III     )	!pack the segment number
    	CALL IFPACK_TEKDEC( XFT     )	!pack the x-scale factor
    	CALL IFPACK_TEKDEC( YFT     )	!pack the y-scale factor
    	CALL IFPACK_TEKDEC( ANGLE   )	!pack the rotation angle
    	CALL IFPACK_TEKXY ( IXA,IYA ) 	!pack the position

    	RETURN
    	END

    	SUBROUTINE IFSEG_HIDE( NSG )
C
C  PROGRAMMER: R.S. ZHOU		JUNE-1986
C
C	THIS ROUTINES SETS THE VISIBILITY OF A SEGMENT "NSG":
C		NSG<0: INVISIBLE
C		NSG>0: VISIBLE
C	ON TEK-4100 SERIES TERMINALS.
C
    	INCLUDE 'TEKLIB.CBS'

     	IF( CTRL_C .OR. ITK.LT.4106 ) RETURN
C
C  IF BUFFER IS FULL, DUMP IT....
C
    	IF( IPL.GT.NBS-IPK-7 ) CALL IFINIT_DUMP

    	IPB(IPL+1) = 27
    	IPB(IPL+2) = 83
    	IPB(IPL+3) = 86
    	IPL = IPL + 3
     	III = IFSEG_NUMBER(NSG)
    	CALL IFPACK_TEKINT(III)
    	IPL = IPL + 1
    	IF( NSG.LT.0 ) IPB(IPL) = 48
    	IF( NSG.GT.0 ) IPB(IPL) = 49

    	RETURN
    	END

    	SUBROUTINE IFSEG_PIVOT( XAD, YAD )
C
C  PROGRAMMER: R.S. ZHOU		MARCH-1986
C
C	THIS ROUTINE SETS THE PIVOT FOR THE SUBSEQUENT
C	DEFINITION OF A GRAPH SEGMENT VIA TEK-4100 COMMANDS.
C
    	INCLUDE 'TEKLIB.CBS'

    	IF( CTRL_C .OR. ITM.NE.2 ) RETURN
C
C  GET THE ABSOLUTE SCREEN ADDRESS...
C
     	IF( XANG.NE.0.0 .OR. YANG.NE.0.0 ) THEN
     	    XPS = XAD*XCTH-YAD*YSTH
    	    YPS = XAD*XSTH+YAD*YCTH
     	ELSE
     	    XPS = XAD
     	    YPS = YAD
     	ENDIF
     	IXA = IXO + INT(FUNI*(XORS+XSCL*XPS))
    	IYA = IYO + INT(FUNI*(YORS+YSCL*YPS))
C
C  IF BUFFER IS FULL, DUMP IT...
C
    	IF( IPL.GT.NBS-IPK-8 ) CALL IFINIT_DUMP

    	IPB(IPL+1) = 27			!<ESC>SP position
    	IPB(IPL+2) = 83
    	IPB(IPL+3) = 80
    	IPL = IPL + 3
    	CALL IFPACK_TEKXY( IXA, IYA )

    	RETURN
    	END

     	FUNCTION IFSEG_NUMBER( IS )
     	INCLUDE 'TEKLIB.CBS'
     	NS = IABS(IS)
     	IF( NS.GT.100 ) NS = NS + 5000*NSE
     	IFSEG_NUMBER = NS
     	RETURN
     	END

