C**********************************************************************
C
	SUBROUTINE PASS0
C
C	DO THE PRELIMINARY MEASUREMENTS ON A PEAK TO
C	FIND THE EXACT POSITION (PANG) IF NECESSARY
C	AND OPTIMIZE THE COUNTING TIMES (BKGTIM & PKTIM)
C	$$ While we are here estimate the finishing time.
C
      INCLUDE 'COMMON.FTN'
C Uses ITYP, IRUN, ITIM,IDAT, IPEAK,NPEAK, MPEAK, LPEAK, IPKFND, ISTIM,IENTIM
C Calls IFANGL, IFMOVE, IFCNT, IFSPED, IFINTE, OPTIM, PKFIND, SYTIM, TIMFIX.
C
	DIMENSION JKL(3)
	BYTE NAM(6)
	REAL*4 TAKTIM
C
	DELPK = 0.0
	TAKTIM = 0.0
C
C   Make sure we have the first peak in the run file
	REWIND IRUN
	READ(IRUN,4) JUNK
4	FORMAT(A1)
C
C  Announce start of run.
	CALL SYTIME
	WRITE(ITYP,104) ITIM, IDAT
104	FORMAT(' PRELIMINARY PASS STARTED AT',
     &   I3,':',I2,':',I2,' ON',I3,'/',I2,'/',I2)
C     &   I3,':',I2.2,':',I2.2,' ON',I3,'/',I2,'/',I2.2)
C
C   Loop through all peaks
	IPEAK = 0
1000	IPEAK = IPEAK + 1
C
C======================================================================
C
C	DO A PEAK
C
C   Read the records from the RUN file.
C
      READ(IRUN,1004,END=2000) BANGD,PANGD,EANGD, DANGD, RATE, MVCNT,
     1	TIMLIM, FIXERR, CNTLIM, NAM, JKL, KODE,IOVLP,IPK, NPK,NBG,
     2	FREL, SIGREL, STD, SIGSTD, JUNK
1004  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, /,A1)
C
C   Make sure we are supposed to be doing this peak.
C
	IF(.NOT.LPEAK(IPEAK)) GOTO 1000
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.)
C
	CALL IFANGL( BANGD, BANG)
	CALL IFANGL( PANGD, PANG(IPEAK))
	CALL IFANGL( EANGD, EANG)
	CALL IFANGL( DANGD, DANG)
C
C   PEAK FINDING
C
C   In peak height mode, we either find the peak . . .
	IF(IPK.EQ.1) CALL PKFIND( PANG(IPEAK), DANG, DELPK, IFOUND)
C   or assume a displacement equal to last peak found.
	IF(IPK.EQ.2) PANG(IPEAK) = PANG(IPEAK) + DELPK
C
C   OPTIMIZE COUNT TIMES
C
C   Count background point for 1 second with no count limit.
	CALL IFMOVE( BANG, 0.0)
	CALL IFCNT ( 1.0, 0.0)
	CNT(1) = COUNTS/CTIME
C
C   Collect peak intensity.
	IF(IPK.EQ.0) THEN
	   IF(IBUG.NE.0) WRITE(IBUG,1014) BANG*STPSIZ,EANG*STPSIZ,SPEED
1014	   FORMAT(' > INTEGRATE FROM',F8.3,' TO',F8.3,' AT',F8.3)
	   CALL IFINTE( EANG-BANG, COUNTS, CTIME)
	ELSE
	   IF(IBUG.NE.0) WRITE(IBUG,1024) PANG(IPEAK)*STPSIZ
1024	   FORMAT(' > COUNT AT ',F8.3)
	   CALL IFMOVE(PANG(IPEAK), 0.0)
	   CALL IFCNT( 1.0, 0.0)
	   CNT(2) = COUNTS/CTIME
	ENDIF
	IF(IBUG.NE.0) WRITE(IBUG,*) 
     &  ' > GOT ',COUNTS,' IN ',CTIME,' SEC'
C   Convert to counts per second.
	CNT(2) = COUNTS / CTIME
C   Take the two counts and calculate the optimum count times
	CALL OPTIM(BANG,DANG,EANG,FIXERR,IPK,NBG,NPK,TIMLIM)
C
C   Figure out how long this is going to take.
	TAKTIM = TAKTIM + BKGTIM(IPEAK) * NBG
	IF(EANG.NE.0.0) TAKTIM = TAKTIM + BKGTIM(IPEAK) * NBG
	IF(IPK.EQ.0) THEN
	    TAKTIM = TAKTIM + PKTIM(IPEAK)*
     &		IFIX(((EANG-BANG)/DANG)+1.5 - 2*NBG)
	ELSE
	    TAKTIM = TAKTIM + PKTIM(IPEAK)* NPK
	ENDIF
C
C   Tens digit of IPKFND(IPEAK) indicates fixed time or fixed error mode.
	IPKFND(IPEAK) = 10
	IF(FIXERR.GT.0.0) IPKFND(IPEAK) = IPKFND(IPEAK) + 10
C   One's digit of IPKFND(IPEAK) indicates how well peak was found.
	IF(IPK.EQ.1) IPKFND(IPEAK) = IPKFND(IPEAK) + IFOUND
C
C   Go do next peak.
	IF(IPEAK.LT.MPEAK) GOTO 1000
C
C   Check to see if we are at end of file.
	READ(IRAW,1004,END=2000) BANGD
C   No, we weren't.  Print a warning that we won't go any farther.
	WRITE(IERR,1904) IPEAK
1904	FORMAT(' >> WARNING << End of run file not hit after',I3,
     &   ' peaks.')
C
C======================================================================
C
C   FINISHED ALL PEAKS.
C
C   Keep track of how many peaks there are
2000	NPEAK = IPEAK
C
C   Print estimated time this mounting will be done.
	CALL SYTIME
	WRITE(ITYP,2004) ITIM, IDAT
2004	FORMAT(' PRELIMINARY PASS COMPLETED AT',
     &    I3,':',I2,':',I2,' ON',I3,'/',I2,'/',I2)
C     &    I3,':',I2.2,':',I2.2,' ON',I3,'/',I2,'/',I2.2)
	ITIM(2) = ITIM(2) + TAKTIM/60.0
	CALL TIMFIX(IDAT,ITIM)
	WRITE(ITYP,2104) TAKTIM/3600.0, ITIM(1),ITIM(2), IDAT
2104	FORMAT(' THIS MOUNTING SHOULD TAKE', F6.2,' HOURS',
     &    ' AND FINISH AT',
     &    I3,':',I2.2,' ON',I3,'/',I2,'/',I2.2)
C     &    I3,':',I2,' ON',I3,'/',I2,'/',I2)
C
C======================================================================
C
C  All done here.
9999	RETURN
	END
