	SUBROUTINE QUAL
	INCLUDE 'COMMON.FTN'
C Uses ICOUNT, NREC, STPSIZ, BKGTIM, PKTIM, IPKFND, LPEAK, CONC, SCONC.
C Calls IFANGL, IFSPED, WRREC;
C Calls IFSSCN with subroutine WRCNT as an argument.
	BYTE IY
	EXTERNAL WRCNT
	BYTE NAM(6)
	DIMENSION JKL(3)
	INTEGER*2 ISTD(9)
C
C***********************************************************************
C
C  SAY HELLO
C
C  GET THE CURRENT DATE AND TIME
C
	CALL SYTIME
C
C  WRITE OUT THE GREETING
C
	WRITE(ITYP,1000) ITIM, IDAT
1000	FORMAT(/,'           POWDER PATTERN DATA COLLECTION AT ',
     1	I2,':',I2.2,':',I2.2,' ON ',I2,'/',I2,'/',I2)
C
C  GET THE STARTING ANGLE FOR THE RUN
C
110	WRITE(ITYP,1005)
1005	FORMAT(/,' ENTER STARTING ANGLE AND ENDING ANGLE FOR THE ',
     1	'RUN (F)',/,' (DEFAULTS: 5.0,90.0) ? ',$)
	READ(IRD,1010,END=9020) BANG,EANG
1010	FORMAT(F,F)
C
C***********************************************************************
C
C  CHECK THE ANGLES TO MAKE SURE THEY'RE OK
C
	IF (BANG.LT.0.001) BANG = 5.0
	IF (EANG.LT.0.001) EANG = 90.0
C
	IF (BANG.GE.EANG) GOTO 110
C
C======================================================================
C
C  Convert the angles from degrees to steps.  (No round off error with
C  whole numbers because they can be represented exactly in binary.)
	CALL IFANGL( BANG, BANG)
	BANG = BANG * STPSIZ
	CALL IFANGL( EANG, EANG)
	EANG = EANG * STPSIZ
C
C  ASK IF THE RESULTS ARE ACCEPTABLE
C
	WRITE(ITYP,120) BANG,EANG
120	FORMAT(/,' SCAN FROM ',F7.3,' TO ',F7.3,' OK (Y/N) ? ',$)
	READ(IRD,130,END=9020) IY
130	FORMAT(A1)
	IF ((IY.EQ.'N') .OR. (IY.EQ.'n')) GOTO 110
C
C***********************************************************************
C
C  SET RUN PARAMETERS FOR FIXED TIME COUNTING IN STEP SCAN MODE
C
	IPK = 0
	FIXERR = 0.0
C
C  INPUT STEP SIZE, COUNT TIME AND SPEED
C
	WRITE(ITYP,1020)
1020	FORMAT(/,' ENTER STEPSIZE, COUNT TIME(SEC),',
     1	' (DEFAULTS: 0.020,3.0) ? ',$)
C
	READ(IRD,1022,END=9020) DANG, TIME, MVCNT, CNTLIM
1022	FORMAT(F,F,I,F)
C
C***********************************************************************
C
C  SET THE DEFAULT VALUES
C
	IF (DANG.LT.0.001) DANG = 0.02
	IF (TIME.LT.0.01) TIME = 3.0
	MVCNT = 0
	CNTLIM = 900000.
C	IF (MVCNT.EQ.0) MVCNT = 1
C	IF (CNTLIM.LT.1.0) CNTLIM = 900000.
C
C  Set angles and count time to multiples of interface units.
	CALL IFANGL( DANG, DANG)
	DANG = DANG * STPSIZ
	CALL IFSPED( RATE, RATE)
	CALL IFTIME( TIME, TIME )
C
C  COMPUTE THE RUN TIME AND OUTPUT
C    NOTE: A PRECISE COMMUNICATION TIME FACTOR MUST BE MEASURED AND
C    INSERTED HERE. A TEMPORARY VALUE OF .22 SEC  WILL BE USED FOR NOW
C
	DELTA = (EANG - BANG) / DANG
C
C***********************************************************************
C
C  READ THE CODES FOR THE INTERNAL STANDARDS USED
C
	WRITE(ITYP,1045)
1045	FORMAT(/,' ENTER 6 LETTER CODES FOR UP TO 3 INTERNAL',
     1	' STANDARDS',/,' IN THE FORM:AAAAAA  AAAAAA  AAAAAA',
     2	/,             '            ?',$)
C
	READ(IRD,1048,END=9020) ISTD
1048	FORMAT(3(3A2,2X))
C
C	OPEN THE RAW DATA FILE
C
C  Prompt for the name
	WRITE(ITYP,1104)
1104	FORMAT(' Please input the name of the output data file',/,
     1	' Filename <.RAW> ? ',$)
C
C  Read the name
	READ(IRD,1014,END=9020) NMRAW
1014	FORMAT(80A1)
C
C  Create the file using .RAW as the default extension.
	CALL SYOPEN(IRAW,NMRAW,'.RAW',1)
C
C=====================================================================
C
C	GET THE CORRECT RUN TITLE AND USERS NAME
C
C  Run title:
	WRITE(ITYP,2004)
2004	FORMAT(' Enter the run title of up to 80 characters',/,
     &   ' Title ? ',$)
C
	READ(IRD,2014,END=9000) TITLE
2014	FORMAT(20A4)
C
C  User name:
	WRITE(ITYP,2104)
2104	FORMAT(' Enter your name ? ',$)
C
	READ(IRD,2014,END=9000) USER
C
C   WRITE THE FIRST 6 RECORDS OF THE FILE
C
C   Rec 1 is run title, user name, & flags.
	CALL SYTIME
	WRITE(IRAW,6104) TITLE,USER, IDAT,ITIM
6104	FORMAT(20A4,5A4, 3I2,3I2, '0000000000')
C
C   Records 2 through 5 are from the diffractometer configuration file.
	CALL COPY25
C
C***********************************************************************
C
C  PRINT OUT THE DATA JUST INPUT FOR FINAL SCRUTINY
C
	ID = ITYP
125	WRITE(ID,1000) ITIM,IDAT
C
	WRITE(ID,1031) TITLE
1031	FORMAT(/,1H ,29A4,A3)
	WRITE(ID,1035) USER
1035	FORMAT(' OPERATOR: ',5A4)
C
	WRITE(ID,1032) NMRAW
1032	FORMAT(' OUTPUT FILE: ',40A1)
	WRITE(ID,1033) ISTD
1033	FORMAT(' STANDARDS:'3(2X,3A2))
C
C  Calculate when the run will end.
	MINUTS = DELTA * (TIME+.22) / 60.
	ITIM(2) = ITIM(2) + MINUTS
	CALL TIMFIX( IDAT, ITIM)
C
	WRITE(ID,1030) BANG, EANG, DANG, TIME, MVCNT, CNTLIM, 
     &    MINUTS/60.0, ITIM(1),ITIM(2),IDAT
1030	FORMAT(/,' BEGINNING ANGLE(DEG)= ',F10.3/
     1	' ENDING ANGLE(DEG)   = ',F10.3/
     2	' STEP SIZE(DEG)      = ',F10.3/
     3	' COUNT TIME(SEC)     = ',F10.3/
     4	' MVCNT METHOD        = ',I6/
     5	' MAX COUNTS          = ',F9.0/
     6	' RUN TIME(HRS)       = ',F10.3,
     7	'     TO END AT ',I2,':',I2.2,' ON ',I2,'/',I2,'/',I2.2)
C
	IF(ID .EQ. IOX) THEN
	   CLOSE(UNIT=ID,DISPOSE='PRINT/DELETE')
	   GOTO 140
	ENDIF
C
C***********************************************************************
C
C  DOES 'HE' HAVE ANY CORRECTIONS TO MAKE
C
	WRITE(ITYP,1034)
1034	FORMAT(/,' CORRECTIONS (Y/N) ? ',$)
	READ(IRD,1040,END=9000) IY
1040	FORMAT(A1)
C
	IF ((IY.EQ.'Y') .OR. (IY.EQ.'y')) THEN
	   CLOSE(UNIT=IRAW, DISPOSE='DELETE')
	   GOTO 110
	ENDIF
C
	WRITE(ITYP,1042)
1042	FORMAT(/,' PRINT INPUT ON LINE PRINTER (Y/N) ? ',$)
	READ(IRD,1040,END=9000) IY
C
	ID = IOX
	IF ((IY.EQ.'Y') .OR. (IY.EQ.'y')) GOTO 125
C
C***********************************************************************
C
C  SET THE NUMBER OF PEAK AND BACKGROUND POINTS
C
140	NBG = 0
	NPK = IFIX(DELTA + 1.5 )
C
C  WRITE THE RUN PARAMETERS TO THE OUTPUT FILE
C
	WRITE(IRAW,1050) BANG, EANG, DANG, RATE,
     1	MVCNT,32700, ISTD, TIME
1050	FORMAT(F7.3,7X,2F7.3,F4.1,I1,12X,I5,9A2,48X,F7.2)
C
C  NUMBER OF RECORDS WRITTEN ON OUTPUT FILE
C
	NREC = 6
C
C======================================================================
C
C  LAST THING BEFORE LEAVING: CLEAR ARRAYS EXCEPT FOR LPEAK
C  Everything gets set up to function properly elsewhere before it is
C  used.  The only reason we do this is so that CALL STATUS won't print junk.
C
	DO 7010, I=1,20
	IPKFND(I) = 0
	BKGTIM(I) = 0.0
	PKTIM(I) = 0.0
	PANG(I) = 0.0
	CNT(I) = 0.0
	CNT(I+20) = 0.0
7010	CONTINUE
C
	DO 7020, I=1,6
C  CNT(45 & 46) get cleared twice; that's cheap.
	CNT(40+I) = 0.0
	CNT(44+I) = 0.0
	IENTIM(I) = 0
	ISTIM(I) = 0
7020	CONTINUE
C
C======================================================================
C
C	MAKE THE MEASUREMENTS
C
C   Initialize the interface and load the sample.
	IQUIT = 0
	CALL IFINIT( IQUIT)
	IF( IQUIT.NE.0) GOTO 9000
	CALL IFLOAD( IQUIT)
	IF( IQUIT.NE.0) GOTO 9000
C
C   Start at beginning of counts array.
	ICOUNT = 0
C
C   Measure the range
C
	CALL IFSSCN(BANG,EANG,DANG,RATE,TIME,CNTLIM,WRCNT)
C
C   Write the last line of counts.
	IF(ICOUNT.GT.0) CALL WRREC
C
C   Close the file and shut down the interface.
	CALL FINISH
	GOTO 9999
C
C======================================================================
C
C  RETURNS
C
C  Return to main if user types ^Z or an error occurs.
9000	CLOSE(UNIT=IRAW, DISPOSE='DELETE')
9020	WRITE(ITYP,9024)
9024	FORMAT(' RUN CANCELED')
C
C  All done here.
9999	RETURN
	END
