	SUBROUTINE STSCAN(ARRAY,FIRST,IDS,NSTEP,TIME,STEP,IFMT,MTHSYN)
C	Jim Walker AUG 1987 for Diffrac-500 on Radix Databox.
C	This routine emulates a Siemes subroutine.
C	References: ECHO, GOSTAR, INTRPT, NULL, READ, IFTIME
C
C	First call needs all arguments.  Subsequent calls should
C	be CALL STACAN(ARRAY,.FALSE.,IERR(4),,,,,)
C++
C++     NSTEP : number of step for this measurement
C++     TIME  : duration of one single step in seconds
C++     STEP  : 2 theta - magnitude of one single step
C++     IFMT  : # of data items per record (must be in the range 1 - 12[10])
C      MTHSYN : Ignored
C++
C++     IDS   : directive status
C++     IDS = +1 for one line of data transferred without error
C++     IDS = +2 if the last line of data has been transferred without error
C
C	IDS = -2 illegal or unexpected answer from uP
C	IDS = -10 if no valid data record is received in the allotted time
C	IDS = -350 or -360 if interrupted.
C
C+++ Extra stuff for radix interface.
	INCLUDE '[102,2]INP.COM/NOLIST'	
	INCLUDE '[102,2]IOBLK.COM/NOLIST'	
	INCLUDE '[102,2]ERRBLK.COM/NOLIST'	
	INCLUDE 'INTERFACE.COM'
	COMMON /RADIXC/ INBUF
	CHARACTER*255 INBUF
	INTEGER*2 I,J,K, MTHSYN
	INTEGER*4 NSTEPR, ISTAT, NCHAR
	REAL*4 EANG
	CHARACTER*80 BUFFER
C---
	COMMON /ALFRED/ ANGLE, NSTEPR, NSTEPL, CTIME, DANG, IDIM
	REAL*4 ARRAY(1), TIME, STEP, ANGLE, CTIME, DANG
	INTEGER*2 NSTEP1, NSTEPL, IDIM, IDS, NSTEP, IFMT
	LOGICAL FIRST
C
C  First time in save the parameters
	IF(FIRST) THEN
		NSTEPR=NSTEP
		NSTEPL=0
		CALL IFTIME( TIME, CTIME)
		DANG=STEP
		IDIM=IFMT
	ENDIF
C
C  START THE INTERFACE RUNNING ON THIS RANGE.
C
	IF(NSTEPL.EQ.0) THEN
C  Make sure we are where we think we are.
	CALL GOSTAR( CURANG)
	IF(IERR(5).NE.0) GOTO 9000
C  Change this number -----v  to the number of points the interface can hold.
		NSTEPL = ( 4096 / IDIM) * IDIM
		NSTEPL = MIN( NSTEPL, NSTEPR)
		NSTEPR = NSTEPR - NSTEPL
		EANG = (NSTEPL-1) * DANG + CURANG
C
C  Get into Edit mode on range 1
		CALL NULL('E'//CHAR(13), 2, ISTAT)
		IF(ISTAT.NE.1) GOTO 9000
		DO 20, I=1,100
		J=INDEX(INBUF, '# 1')
		IF(J.NE.0) GOTO 22
		   CALL NULL('P', 1, ISTAT)
		   IF(ISTAT.NE.1) GOTO 9000
20		CONTINUE
C
C  Set up range 1, undefine range 2, zero the data memory & goto run mode.
22		WRITE(BUFFER,24) 13, 'I',CURANG,13, 'F',EANG,13,
	1	'S',DANG,13, 'C',INT(CTIME*10.0+0.5),13, 13,13,13,13,13
24		FORMAT('U',a1, 3(a1,F8.4,a1),a1,I5,a1,
	1	'NU',a1,'E',a1,'C',a1,'Z',a1,'YE',a1,'S')
		CALL NULL(BUFFER, 65, ISTAT)
		IF(ISTAT.NE.1) GOTO 9000
C
C  Enter a blank title to get things moving.  This time don't wait
C  for a null.  Echo will only read back 1 char, and set ISTAT=-2
C  because it wasnt a carriage return.
		CALL ECHO(CHAR(13), 1, ISTAT)
		IF(ISTAT.NE.-2) GOTO 9000
C
C  Throw away heading junk.
25		CALL READ(BUFFER, NCHAR, ISTAT, 5)
		IF(ISTAT.LT.-3 .OR. ISTAT.GT.-1) GOTO 9000
		IF(INDEX(BUFFER,'Count').EQ.0 .AND. ISTAT.NE.-1) GOTO 25
	ENDIF
C
C  How many steps for this call?
	NSTEP1 = MIN(IDIM,NSTEPL)
C
C  Stepscan loop.
C
	DO 7, I=1,NSTEP1
C
C  Read a line of data from the interface.
	CALL READ(BUFFER, NCHAR, ISTAT, INT(CTIME)+5 )
	IF(ISTAT.EQ.-1) THEN
		IDS = -10
		GOTO 9999
	ENDIF
	IF(ISTAT.NE.-2) GOTO 9000
C
C  The angle is the number with the decimal point; the counts follow.
	J=INDEX(BUFFER(1:NCHAR),'.')	! Find . in angle
	J=J+INDEX(BUFFER(J:NCHAR),' ')	! Find end of angle
C  Find beginning of counts
	DO 74, J=J,NCHAR
	IF(BUFFER(J:J).GE.'0' .AND. BUFFER(J:J).LE.'9') GOTO 75
74	CONTINUE
C  Didn't find anything, bail out.
	GOTO 9000
C  Find end of counts
75	DO 76, K=J,NCHAR
	IF(BUFFER(K:K).LT.'0' .OR. BUFFER(K:K).GT.'9') GOTO 77
76	CONTINUE
C  Didn't find anything, bail out (this can't happen)
	GOTO 9000
77	READ(BUFFER(J:K-1), '(G8.0)') ARRAY(I)
	TMPRTR = 25.0
C  Find beginning of temperature, if present.
	DO 54, J=K,NCHAR
	IF(BUFFER(J:J).GE.'0' .AND. BUFFER(J:J).LE.'9') GOTO 55
54	CONTINUE
C  Didn't find anything, skip it..
	GOTO 78
C  Find end of temperature.
55	DO 56, K=J,NCHAR
	IF(BUFFER(K:K).EQ.'.') GOTO 56
	IF(BUFFER(K:K).LT.'0' .OR. BUFFER(K:K).GT.'9') GOTO 57
56	CONTINUE
	K = NCHAR+1
57	READ(BUFFER(J:K-1), '(G8.0)') TMPRTR
C
C  Echo the data to the terminal if required.
78	IF(IDSPLY.GT.0) THEN
	   IDSPLY = IDSPLY - 1
	   IF(IDIF.EQ.0) IDIF=6
	   WRITE(IDIF,79)CURANG, ARRAY(I)/CTIME, TMPRTR
79	   FORMAT(' Angle =',F8.3,'   Counts/sec =',F10.2,
     &      '   Temp =',F8.2)
C  If we are done typing, close the listing device
	   IF(IDSPLY.EQ.0 .AND. IDIF.NE.6) CLOSE(UNIT=IDIF)
	ENDIF
C
C  Update where we think the goniometer is.
	CURANG = CURANG + DANG
C
C  Check for interrupt
	CALL INTRPT(IDS)
	IF(IDS.NE.0) THEN
		CALL NULL('T'//CHAR(13), 2, ISTAT)
		GOTO 9999
	ENDIF
C
C  End of stepscan loop.
 7	CONTINUE
C
C  Successful returns
	NSTEPL = NSTEPL - NSTEP1
	IDS = 1				! one line transferred
	IF(NSTEPL.EQ.0) THEN	! all done
	IF(NSTEPR.EQ.0) IDS = 2
C
C  Throw away any remaining junk.
80		CALL READ(BUFFER, NCHAR, ISTAT, 5)
		IF(ISTAT.EQ.-2 .OR. ISTAT.EQ.-3) GOTO 80
	ENDIF
	GOTO 9999
C
C  Error returns
 9000	IDS=-2			! illegal response from interface
	IF(ISTAT.EQ.-1) IDS = -1 ! Timeout
C
 9999	RETURN
	END
