      SUBROUTINE TK4010(X,Y,IND)
C
C         Notice of Public Domain nature of this Program
C
C      'This computer program is a work of the United States 
C       Government and as such is not subject to protection by 
C       copyright (17 U.S.C. # 105.)  Any person who fraudulently 
C       places a copyright notice or does any other act contrary 
C       to the provisions of 17 U.S. Code 506(c) shall be subject 
C       to the penalties provided therein.  This notice shall not 
C       be altered or removed from this software and is to be on 
C       all reproductions.'
C
      COMMON/ OUTPUT/ IPAPER,IPAGE
      COMMON /DEVICE/ ITYPE, ISCRN, KOROFF
      CHARACTER TEKCOR*4, TEKINT*4, ASCII*1, ESC*1, GS*1, FF*1, US*1
      CHARACTER GRAFCH*1, NUL*1
      COMMON/ASCIIC/ ASCII( 0: 255)
      INTEGER PIXROW, PIXCOL, PIXEL
      COMMON /TERM/ IMAXR, IMAXC, PIXROW, PIXCOL, PIXEL, LCOUNT, INGRAF,
     .              IXL, IXR, IYT, IYB, NCOLOR, MCOLOR( 64 ), ITTRM,
     .              MARGX, MARGY
* VARIABLES IN COMMON TERM:
*  IMAXR = MAX NUMBER OF ROWS
*  IMAXC = MAX NUMBER OF COLUMNS
*  PIXROW = NUMBER OF PIXELS PER ROW
*  PIXCOL = NUMBER OF PIXELS PER COLUMN
*  PIXEL  = LESSER OF PIXROW OR PIXCOL
*  LCOUNT = NUMBER OF LINES CURRENTLY WRITTEN TO DIALOG AREA
*  INGRAF = 0=> HOST TEXT GOES TO MONITOR SPACE\\ 1=> TEXT TO GRAPHICS
*  IXL    = LEFT MOST VALUE OF X
*  IXR    = RIGHT MOST VALUE OF X
*  IYT    = VALUE OF Y AT TOP OF SCREEN
*  IYB    = VALUE OF Y AT BOTTOM OF SCREEN
*  NCOLOR = NUMBER OF COLORS IN MCOLOR MAP
*  MCOLOR = MAP OF COLORS
*  ITTRM  = UNIQUE MODEL NUMBER OF GENERAL TERMINAL TYPE
*  MARGX  = NUMBER OF PIXELS PADDED TO X-COORDINATE
*  MARGY  = NUMBER OF PIXELS PADDED TO Y-COORDINATE
*
*
      EQUIVALENCE ( ESC, ASCII(27)), ( FF, ASCII(12)), ( GS, ASCII(29))
      EQUIVALENCE ( NUL, ASCII(0) )
*
C CODES FOR IND:
C
C     IND = 0   =>  REMOVE ALL GRAPHICS TO QUIT
C     IND = 1   =>  ANY ONE TIME ONLY INITIALIZATION
C     IND = 2   =>  MOVE GRAPHICS TO POINT (X,Y) -- DO NOT DRAW LINE
C     IND = 3   =>  DRAW LINE FROM LAST POINT TO NEW (X,Y)
C     IND = 4   =>  DRAW DARK LINE
C     IND = 5   =>  DRAW BRIGHT SOLID LINE
C     IND = 6   =>  INITIALIZE & SET-UP FOR NEXT PICTURE
C     IND = 8   =>  TEMPORARY RELEASE FROM GRAPHICS
C     IND = 9   =>  RETURN TO GRAPHICS FROM TEMP RELEASE
*     IND = 10  =>  DONE DRAWING PICTURE
*
*     IND = 99  =>  COLOR CHANGE (NOT USED HERE)
*
 2000 FORMAT ( '+', A, $)
      IF (IND.EQ.0) THEN
*  ALL DONE -- CLEAR TERMINAL AND WE WILL QUIT
         INGRAF = 0
         WRITE ( *, 2000 ) ESC//FF
      ELSEIF (IND.EQ.1) THEN
*  ONE TIME ONLY INITIALIZATION OF TERMINAL  (IF NEEDED)
         ISCRN = 24
         NCOLOR = 0
         IXL = 0
         IXR = 1024
         IYT = 0
         IYB = 770
         PIXROW = 770 / 30
         PIXCOL = 1024 / 80
*     WORKSPACE IS 29 LINES OUT OF 33 MAX
         MAXPIX = 29. * PIXROW
         PIXEL = MAXPIX * IPAGE / 100.
         WRITE ( *, 2000 ) ESC//FF
         WRITE ( *, 2000 ) ESC//'9'
         INGRAF = 1
      ELSEIF (IND.EQ.2 ) THEN
* MOVE GRAPHICS CURSOR TO NEW POINT
         IXLX = X * PIXEL
         IYLY = Y * PIXEL
         CALL C4010( IXLX, IYLY, TEKCOR )
         WRITE ( *, 2000 ) GS//TEKCOR
      ELSEIF (IND.EQ.3) THEN
* DRAW LINE FROM LAST POINT TO NEW POINT
         IXLX = X * PIXEL
         IYLY = Y * PIXEL
         CALL C4010( IXLX, IYLY, TEKCOR )
         IF ( GRAFCH .EQ. NUL ) THEN
            WRITE ( *, 2000 ) TEKCOR
         ELSE
            WRITE ( *, 2000 ) GRAFCH//TEKCOR
         ENDIF
      ELSEIF (IND.EQ.4) THEN
* DRAW BLACK LINE TO ERASE
         GRAFCH = GS
      ELSEIF (IND.EQ.5) THEN
* DRAW WHITE LINE
         GRAFCH = NUL
      ELSEIF (IND.EQ.6) THEN
*  INITIALIZE FOR DRAWING NEXT PICTURE
         WRITE ( *, 2000 ) ESC//FF
         INGRAF = 1
         GRAFCH = NUL
      ELSEIF (IND.EQ.8) THEN
* TEMPORARY RELEASE FROM GRAPHICS  (USUALLY FOR TEXT)
C?         WRITE ( *, 2000 ) 
         INGRAF = 0
         CALL ST4010( 25, 1, ' ')
      ELSEIF (IND.EQ.9) THEN
* RETURN TO GRAPHICS FROM TEMPORARY RELEASE
         WRITE ( *, 2000 ) GS
         INGRAF = 1
      ELSEIF (IND.EQ.10) THEN
* DONE DRAWING PICTURE
         INGRAF = 0
      ELSEIF ( IND.EQ.99 ) THEN
* COLOR CHANGE IMPLEMENTED AS SET SOLID LINE ( SAME AS IND=5)
C?       WRITE ( *, 2000 ) ESC//'/0d'
      ELSE
* WE HAVE AN ERROR IN IND
         WRITE ( *, *) ' ERROR IN PLOT, IND=',IND
      ENDIF
      RETURN
      END
*
      SUBROUTINE ST4010(IROW, ICOL, STRING)
*
*  TO PLACE TEXT SRING IN GRAPHIC AREA
*    THE STRING STARTS AT LOCATION (ROW, COL)
* RESPONSIBILITY FOR WRAPPING, ETC IS UP TO THE USER
*
      CHARACTER*(80) STRING
      CHARACTER TEKCOR*4, TEKINT*4, ASCII*1, ESC*1, GS*1, FF*1, US*1
      COMMON/ASCIIC/ ASCII( 0: 255)
      INTEGER PIXROW, PIXCOL, PIXEL
      COMMON /TERM/ IMAXR, IMAXC, PIXROW, PIXCOL, PIXEL, LCOUNT, INGRAF,
     .              IXL, IXR, IYT, IYB, NCOLOR, MCOLOR( 64 ), ITTRM,
     .              MARGX, MARGY
* VARIABLES IN COMMON TERM:
*  IMAXR = MAX NUMBER OF ROWS
*  IMAXC = MAX NUMBER OF COLUMNS
*  PIXROW = NUMBER OF PIXELS PER ROW
*  PIXCOL = NUMBER OF PIXELS PER COLUMN
*  PIXEL  = LESSER OF PIXROW OR PIXCOL
*  LCOUNT = NUMBER OF LINES CURRENTLY WRITTEN TO DIALOG AREA
*  INGRAF = 0=> HOST TEXT GOES TO MONITOR SPACE\\ 1=> TEXT TO GRAPHICS
*  IXL    = LEFT MOST VALUE OF X
*  IXR    = RIGHT MOST VALUE OF X
*  IYT    = VALUE OF Y AT TOP OF SCREEN
*  IYB    = VALUE OF Y AT BOTTOM OF SCREEN
*  NCOLOR = NUMBER OF COLORS IN MCOLOR MAP
*  MCOLOR = MAP OF COLORS
*  ITTRM  = UNIQUE MODEL NUMBER OF GENERAL TERMINAL TYPE
*  MARGX  = NUMBER OF PIXELS PADDED TO X-COORDINATE
*  MARGY  = NUMBER OF PIXELS PADDED TO Y-COORDINATE
*
*
      EQUIVALENCE ( ESC, ASCII(27)), ( FF, ASCII(12)), ( GS, ASCII(29))
      EQUIVALENCE ( US, ASCII(31))
*
 2000 FORMAT ( '+', A, $)
      IF ( ICOL*IROW .NE. 0 ) THEN
         CALL C4010( ICOL*PIXCOL, (31-IROW)*PIXROW, TEKCOR )
         WRITE ( *, 2000 ) GS//TEKCOR
      ENDIF
      IF(LLENG(STRING).GT.0) THEN
        WRITE ( *, 2000 ) US//STRING(1:LLENG(STRING) )
        WRITE ( *, 2000 ) GS
      ENDIF
      RETURN
      END

      SUBROUTINE C4010( IX, IY, TEKCOR )
* CONVERT COORDINATES ( IX, IY ) TO ENCODED STRING
      IMPLICIT INTEGER ( A-Z )
      CHARACTER TEKCOR*4
      INTEGER PIXROW, PIXCOL, PIXEL
      COMMON /TERM/ IMAXR, IMAXC, PIXROW, PIXCOL, PIXEL, LCOUNT, INGRAF,
     .              IXL, IXR, IYT, IYB, NCOLOR, MCOLOR( 64 ), ITTRM,
     .              MARGX, MARGY
* VARIABLES IN COMMON TERM:
*  IMAXR = MAX NUMBER OF ROWS
*  IMAXC = MAX NUMBER OF COLUMNS
*  PIXROW = NUMBER OF PIXELS PER ROW
*  PIXCOL = NUMBER OF PIXELS PER COLUMN
*  PIXEL  = LESSER OF PIXROW OR PIXCOL
*  LCOUNT = NUMBER OF LINES CURRENTLY WRITTEN TO DIALOG AREA
*  INGRAF = 0=> HOST TEXT GOES TO MONITOR SPACE\\ 1=> TEXT TO GRAPHICS
*  IXL    = LEFT MOST VALUE OF X
*  IXR    = RIGHT MOST VALUE OF X
*  IYT    = VALUE OF Y AT TOP OF SCREEN
*  IYB    = VALUE OF Y AT BOTTOM OF SCREEN
*  NCOLOR = NUMBER OF COLORS IN MCOLOR MAP
*  MCOLOR = MAP OF COLORS
*  ITTRM  = UNIQUE MODEL NUMBER OF GENERAL TERMINAL TYPE
*  MARGX  = NUMBER OF PIXELS PADDED TO X-COORDINATE
*  MARGY  = NUMBER OF PIXELS PADDED TO Y-COORDINATE
*
*
      INX = IX
      INY = IY
* CHECK FOR CLIPPING
      IF ( INX .LT. IXL ) THEN
         INX = IXL
      ELSEIF ( INX .GT. IXR ) THEN
         INX = IXR
      ENDIF
      IF ( INY .LT. IYT ) THEN
         INY = IYT
      ELSEIF ( INY .GT. IYB ) THEN
         INY = IYB
      ENDIF
* NOW ENCODE IT
      HIGHX = INX/32
      LOWX = INX - HIGHX*32
      HIGHY = INY/32
      LOWY = INY - HIGHY*32
*
      TEKCOR = CHAR(32+HIGHY)//CHAR(64+32+LOWY)//
     .         CHAR(32+HIGHX)//CHAR(64+LOWX)
      RETURN
      END
