	SUBROUTINE SETCRT(ICRT,lo,li,ANSI)
	INTEGER ICRT,lo,li
	LOGICAL ANSI

	COMMON /SETCRT/ CRTOPEN
	LOGICAL CRTOPEN

	CHARACTER*20 string
	INTEGER ln
	CHARACTER*11 LGPRC/'LNM$PROCESS'/
	CHARACTER*7 LGJOB/'LNM$JOB'/
	LOGICAL GET_LOGICAL,DEF_LOGICAL,flag
C---------------------------------------------------------------------------
C Subroutine to determine the terminal type; returns ICRT where:
C  ICRT < 0 for non-graphics terminal
C  ICRT > 0 for graphics terminal
C	ICRT = -2 for non-VT100 unsupported
C	ICRT = -1 for VT100 (ANSI) compatible (no graphics)
C	ICRT =  0 non-terminal operation (e.g. batch)
C	ICRT =  1 for VT125 compatible 
C	ICRT =  2 for Tektronix 4010 compatible 
C	ICRT =  3 for HDS VT100 text/Tektronix 4010 compatible 
C	ICRT =  4 for TEKTRONIX 41xx -- VT100 text/Tektronix 4010 graphics 
C	ICRT =  5 for VAXStation (VWS)
C	ICRT =  6 DECWindows (Xwindows V11)
C	ICRT = 98 for ANSI with graphics
C	ICRT = 99 for non-ANSI with graphics
C---------------------------------------------------------------------------
	CRTOPEN = .false.
	IF (GET_LOGICAL('PS_CRT',string,20,ln,lgprc)) then
	  CALL PRESS(STRING,LN)
	  read (unit=string(1:ln),fmt='(i)',err=49) ICRT
	ELSEIF (GET_LOGICAL('PS_CRT',string,20,ln,lgjob)) then
	  CALL PRESS(STRING,LN)
	  read (unit=string(1:ln),fmt='(i)',err=49) ICRT
	else
49	  write (LO,105)
105	  FORMAT (' ',70('='),/,
	1	' Choose your terminal type from the list below:',/
	1	,t5 ,'[1]  DEC REGIS (VT125 comp.)'
	1	,t45,'[2]  Tektronix 4010',/
	1	,t5 ,'[3]  HDS graphics terminal'
	1	,t45,'[4]  Tektronix 41xx',/
	1	,t5, '[5]  VAXStation (VWS)'
	1	,t45,'[6]  DECWindows (X11)',/,
	1	t4, '[98]  other PGPLOT ANSI (VT100 comp.)',
	1	t44,'[99]  other PGPLOT non-ANSI',/,
	1	t4, '[-1]  non-graphics, ANSI',
	1	t44,'[-2]  non-graphics, non-ANSI',/,' ',70('='),/,
	1	'$Enter a number for your terminal type: ')
	  READ (li,'(I)',end=18,err=18) ICRT
	endif
	if (ICRT .eq. 99 .or. ICRT .eq. 98 .or. 
	1	(ICRT .ge. -2 .and. ICRT .le. 6)) then
	  write (unit=string,fmt='(I)') ICRT
	  CALL PRESS(STRING,LN)
	  flag = DEF_LOGICAL('PS_CRT',string(1:LN),LN,1,lgPRC)
!	  flag = DEF_LOGICAL('PS_CRT',string(1:20),20,1,lgPRC)
	  if (icrt .eq. 1 .or. icrt .eq. -1 .or. icrt .eq. 3 
	1	.or. icrt .eq. 4 .or. icrt .eq. 5 
	1	.or. icrt .eq. 6 .or. icrt .eq. 98) ANSI = .true.
	else
	  write (lo,'(a,i,/)') ' Invalid terminal type code =',ICRT
	  goto 49
	endif
18	RETURN
	END
C!**************************************************************************
	SUBROUTINE BGRAF(ICRT,lo,li)
C Subroutine to start graphics support under PGPLOT
C   Put terminal into graphics mode, if terminal has more than one mode
	INTEGER ICRT,lo,li

	COMMON /SETCRT/ CRTOPEN
	LOGICAL CRTOPEN

	SAVE strlen,string
	integer strlen/0/
	character*1 esc,string*70
	INTEGER pgbegin,ierr
	esc = char(27)
	if (icrt .le. 0) then
	  ierr = pgbegin(1,'/nul',1,1)
	elseif (icrt .eq. 1) then
	  ierr = pgbegin(1,'/VT125',1,1)
	elseif (icrt .eq. 2) then
	  ierr = pgbegin(1,'/TEK4010',1,1)
	elseif (icrt .eq. 3) then
C  set HDS 2200 into graphics mode
	  WRITE(LO,'($,1x,2a)') esc,'[0/w'
	  ierr = pgbegin(1,'/TEK4010',1,1)
	elseif (icrt .eq. 4) then
C  clear text screen, then set TEK 41xx into graphics mode
	  WRITE(LO,'($,1x,4a)') esc,'[2J',esc,'[H'
	  WRITE(LO,'($,1x,2a)') esc,'%!0'
	  ierr = pgbegin(1,'/TK41',1,1)
	elseif (icrt .eq. 5) then
	  ierr = pgbegin(1,'/WS',1,1)
	elseif (icrt .eq. 6) then
C for DECWINDOWS open only if not open
	  IF (CRTOPEN) THEN
	    call GRPAGE
	    ierr = 1
	  ELSE
	    ierr = pgbegin(1,'/X11',1,1)
	  ENDIF
	elseif(strlen .gt. 0) then
	  ierr = pgbegin(1,string(1:strlen),1,1) 
	else
C unspecified graphics device, use pgplot to get device type, then save it
	  ierr = pgbegin(1,'?',1,1)
	  call pgqinf('DEV/TYPE',string,strlen)
C remove trailing comments from device string
	  DO I=strlen,2,-1
	    if (STRING(I:I) .eq. ' ') strlen = I-1
	  ENDDO
	endif
	if (ierr .ne. 1) then 
	  write(lo,'(a)') ' BGRAF ERROR -- opening terminal for graphics; '//
	1	'using null graphics device'
	  ierr = pgbegin(1,'/nul',1,1)
	ELSE
	  CRTOPEN = .true.
	ENDIF
	RETURN
	END
C!**************************************************************************
	SUBROUTINE EGRAF(ICRT,lo,li)
C subroutine to end graphics support under PGPLOT
C   Put terminal into text mode, if terminal has more than one mode
	INTEGER ICRT,lo,li

	COMMON /SETCRT/ CRTOPEN
	LOGICAL CRTOPEN

	character*1 esc,formf
	esc = char(27)
	formf = char(12)
	if (icrt .eq. 3) then
	  call pgend
C  set HDS 2200 into text mode
	  WRITE(LO,'($,1x,2a)') esc,'[?38l'
	  CRTOPEN = .false.
	elseif (icrt .eq. 4) then
C  set TEK mode, clear graphics screen, then into EDIT mode
	  call pgend
	  WRITE(LO,'($,1x,10a)') esc,'%!0',esc,formf,esc,'%!2'
	  CRTOPEN = .false.
	elseif (icrt .eq. 6) then
C for DECWindows, don't close the window, in case we need it later
C reset plot parameters
          call PGSCF (1)
	  call PGSCH (1.)
	  call PGSCI (1)
	  call PGSLS (1)
	  CALL PGSLW (1)
	  CALL PGVSTD
	else
	  call pgend
	  CRTOPEN = .false.
	endif
	RETURN
	END
C!**************************************************************************
	SUBROUTINE TERASE(ICRT,lo,li)
C Subroutine to erase terminal screen in text mode (use PGPAGE routine when 
C  in graphics mode)
	INTEGER ICRT,lo,li
	character*1 esc
	esc = char(27)
C ANSI compatable
	if (icrt .eq. 1 .or. icrt .eq. -1 .or. icrt .eq. 3 
	1	.or. icrt .eq. 4 .or. icrt .eq. 5 
	1	.or. icrt .eq. 6 .or. icrt .eq. 98) then
C    clear and home the screen
	  WRITE(LO,'(5a)') '$',esc,'[2J',esc,'[H'
	elseif (icrt .eq. 2) then
C Tektronix: try to clear screen
	  WRITE(LO,'(1x,2a1)') "033,"014
	  CALL PS_WAIT(2.0,.true.)
!	elseif (icrt .eq. 3) then
!	  WRITE(LO,'(3a)') '$',esc,'[1/w'
	elseif (icrt .ne. 0) then
C unsupported: send a form feed and hope for the best
	  WRITE(LO,'(2a1)') '$',char(12)
	endif
	RETURN
	END
