C**********************************************************************
C
	SUBROUTINE PKSCAN( IEND)
C
C	THESE ARE THE IMPORTANT MEASUREMENTS ON A PEAK
C	FIRST SOME BACKGROUND POINTS ARE MEASURED ON THE LOW ANGLE SIDE,
C	THEN SOME POINTS ARE MEASURED AT THE TOP OF THE PEAK
C	THEN OPTIONALLY SOME MORE BACKGROUND POINTS ARE MEASURED ON THE HIGH
C	ANGLE SIDE.
C
C======================================================================
C
C
C	The argument IEND is set to 1 if the end of the run file is hit,
C   otherwise IEND is set to 0 to indicate the peak was measured.
C
C
      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.
C
	EXTERNAL WRCNT
	BYTE NAM(6)
	DIMENSION JKL(3)
C
C======================================================================
C
C	PROCESS THE LINE PARAMETERS RECORD OF THE FILES
C
C   Read the records from the RUN file.
C
	READ(IRUN,104,END=9000) BANGD, PANGD, EANGD, DANGD, RATE, MVCNT,
     1	TIMLIM, FIXERR, CNTLIM, NAM, JKL, KODE, IOVLP, IPK, NPK, NBG,
     2	FREL, SIGREL, STD, SIGSTD, SCONC1, ICHEM
104	FORMAT(3F7.3,F7.4,F4.1,I1,
     1  F5.1,F5.2,F7.0,6A1,3I2,3I1,2I2,
     2	F6.1,F4.1,F9.4,F8.4, 12X, F8.3,/,128A1)
C
C  Copy mu/rho of standard if analyzing unknown by intensity ratio.
C $$ Or, if internal standard method, copy concentration of standard.
	IF( (IFLAG(3).EQ.2.OR.IFLAG(3).EQ.1) .AND.
     &     IFLAG(2).EQ.1) SCONC = SCONC1
C
C   End of file not hit.
	IEND = 0
C
C   Make sure we are supposed to be doing this peak.
C
	IF( .NOT.LPEAK(IPEAK)) GOTO 9999
	IF(IBUG.NE.0) WRITE(IBUG,204) PANG(IPEAK)*STPSIZ
     &   BANGD,EANGD,DANGD,BKGTIM(IPEAK)
204	FORMAT(' > PEAK SCAN',4F9.4,F6.2)
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( BANGD, BANG)
	CALL IFANGL( EANGD, EANG)
	CALL IFANGL( DANGD, DANG)
C
C   The values written to the file should be rounded off to what the
C   interface actually used.
	BANGD = BANG * STPSIZ
	PANGD = PANG(IPEAK) * STPSIZ
	EANGD = EANG * STPSIZ
	DANGD = DANG * STPSIZ
	CALL IFSPED( RATE, RATE)
C
C   Write the line parameters and chemestry information to the raw file.
C
	WRITE(IRAW,4010) BANGD, PANGD, EANGD, DANGD, RATE, MVCNT,
     1	TIMLIM, FIXERR, CNTLIM, NAM, JKL, KODE, IOVLP, IPK, NPK, NBG,
     2  FREL, SIGREL, STD, SIGSTD,
     3	CONC, PKTIM(IPEAK),BKGTIM(IPEAK), IPKFND(IPEAK), SCONC, ICHEM
4010	FORMAT(3F7.3,F7.4,F4.1,I1,
     1  F5.1,F5.2,F7.0,6A1,3I2,3I1,2I2,
     2	F6.1,F4.1,F9.4,F8.4,
     3  F7.3,2F7.2,I2,F7.3, /,128A1)
	NREC = NREC + 2
C
C======================================================================
C
C	MAKE THE MEASUREMENTS
C
C   Start at beginning of counts array.
	ICOUNT = 0
C
C   Measure the low angle background.
C
	BG2 = - FLOAT(NBG)
	CALL IFSSCN(BANG,BG2,DANG,RATE,BKGTIM(IPEAK),CNTLIM,WRCNT)
C
C   Measure the top of the peak.
C
	IF(IPK.EQ.0) THEN	! Integrate mode
	   SANG = BANG + DANG * NBG
	   PK2 = EANG - DANG * NBG
	ELSE			! Peak height mode
	   SANG = PANG(IPEAK) - DANG * (NPK/2)
	   PK2 = - FLOAT(NPK)
	ENDIF
	CALL IFSSCN(SANG,PK2,DANG,RATE,PKTIM(IPEAK),CNTLIM,WRCNT)
C
C   Measure the high angle background if necessary
C
	IF(EANGD.EQ.0.0) GOTO 8000
	SANG = EANG - DANG * (NBG-1)
	CALL IFSSCN(SANG,EANG,DANG,RATE,BKGTIM(IPEAK),CNTLIM,WRCNT)
	GOTO 8000
C
C   Write the last line of counts.
8000	IF(ICOUNT.GT.0) CALL WRREC
	GOTO 9999
C
C======================================================================
C
C   End of run file
9000	IEND = 1
C
C  All done here.
9999	RETURN
	END
