C***********************************************************************
C
	SUBROUTINE OPTIM(BANG,DANG,EANG,FIXERR,IPK,NBG,NPK,TIMLIM)
C
C		COUNT OPTIMIZATION ROUTINE
C
C	DESCRIPTION:
C     THIS ROUTINE OPTIMIZES THE TIME SPENT COUNTING THE PEAK AND
C  BACKGROUND POINTS IN EITHER FIXED TOTAL TIME MODE OR FIXED ERROR
C  MODE.  IN FIXED ERROR MODE THE TOTAL TIME IS CONSTRAINED TO BE
C  LESS THAN LIMTIM.  IF THE TOTAL TIME CALCULATES TO BE GREATER THEN
C  THE ROUTINE CONVERTS TO FIXED TIME OPTIMIZATION.
C  ## This routine was taken from the old version with little modification.
C
      INCLUDE 'COMMON.FTN'
C
C Uses NBG, NPK, FIXERR, CNT(1), CNT(2), TIMLIM, IPK, BANG, EANG, DANG
C Uses outputs: BKGTIM(IPEAK), PKTIM(IPEAK), FIXERR
C
C***********************************************************************
C
C  DETERMINE THE NUMBER OF BACKGROUND POINTS TO BE COLLECTED
C
	NPTBG=NBG
	IF (EANG.NE.0.0) NPTBG=NBG+NBG
C
C  DETERMINE THE NUMBER OF PEAK OR SCAN POINTS TO BE COLLECTED
C
	NPTPK=NPK
	IF (IPK.EQ.0) NPTPK=IFIX(((EANG-BANG)/DANG)+1.0-NPTBG)
C
C***********************************************************************
C
C  TEST IF BACKGROUND POINT WAS MEASURED. IF NOT SET R AND RINV TO 0.0
C  AND SKIP THE RATIO CALCULATION
C
	R    = 0.0
	RINV = 0.0
	IF (NPTBG.EQ.0) GOTO 100
C
C  CALCULATE THE RATIO OF PEAK TO BACKGROUND INTENSITIES
C
	R = SQRT(CNT(2)/CNT(1))
	IF (R.LT.1.0) R = 1.0
	RINV = 1.0/R
C
C***********************************************************************
C
C  BRANCH TO THE PROPER COUNT MODE ROUTINE
C
100	IF (FIXERR.EQ.0.0) GOTO 200
C
C***********************************************************************
C
C  WE ARE IN THE FIXED ERROR MODE.  CHECK THAT PEAK AND BKG
C  COUNTS ARE DIFFERENT - IF NOT CONVERT TO FIXED TIME MODE
C
	IF (CNT(1).GE.CNT(2)) GOTO 200
C
C  COMPUTE THE PEAK COUNT TIME (IN SECONDS) FOR FIXED ERROR MODE
C
	TOTIME = (CNT(2)+R*CNT(1)) / ((FIXERR*FIXERR/10000.)*
     1	(CNT(2)-CNT(1))*(CNT(2)-CNT(1)))
C
C***********************************************************************
C
C  IF THE TIME REQUIRED IS STILL UNDER THE LIMIT THEN GO TO THE
C  FINAL CALCULATION SECTION
C
	IF (((TOTIME+TOTIME*RINV)/60.).LE.TIMLIM) GOTO 300
C
C***********************************************************************
C
C  WE ARE IN FIXED TIME MODE (TIMLIM IS IN MINUTES) DETERMINE
C  COUNT TIME = TIME IN SECONDS.
C
200	CONTINUE
C
D	IF (FIXERR.GT.0.0 .AND. IBUG.GT.0 ) WRITE(IBUG,210)
D210		FORMAT('  >> OPTIM <<  CONVERTED FROM FIXED ERROR '
D    1		'TO FIXED TIME MODE.')
C
	FIXERR = 0.0
C
	TOTIME = 60.0*TIMLIM / (1.0+RINV)
C
C***********************************************************************
C
C  NOW COMPUTE THE BACKGROUND COUNT TIME FOR BOTH MODES
C
C  IF THE MAXIMUM TIME COUNT ALLOWED IN THE SCALER USING A TIME
C  BASE OF .01 SEC IS EXCEEDED DEFAULT TO MAX TIME OF 9000.
C
300	IF (TOTIME/NPTPK.GT.9000.) TOTIME = 9000.*NPTPK
C
	PKTIM(IPEAK) = TOTIME/NPTPK
C
	BKGTIM(IPEAK) = 0.0
	IF (NPTBG.EQ.0) GOTO 9999
	BKGTIM(IPEAK) = TOTIME / (NPTBG * R)
C
C***********************************************************************
C
C  DEBUG PRINT OUT
C
9999	CONTINUE
C
D	IF(IBUG.NE.0) WRITE(IBUG,510) IPEAK,CNT(1),CNT(2),FIXERR,
D    1	TIMLIM,NPTPK,NPTBG,PKTIM(IPEAK),BKGTIM(IPEAK)
D510	FORMAT(' >> DEBUG0 <<  OPTIM RESULTS FOR PEAK #',I3,/,
D    1  ' > BKG CNTS:',4X,F8.0,7X,'PEAK CNTS: ',3X,F8.0,/,
D    2  ' > COUNT ERROR:',5X,F6.2,5X,'TIME LIMIT:',7X,F6.2,/,
D    3  ' > # PEAK PTS:',4X,I5,8X,'# BKG PTS:',6X,I5,/,
D    4  ' > PEAK CNT TIME: ',F8.2,5X,'BKG CNT TIME:',3X,F8.2)
C
C***********************************************************************
C
	CALL IFTIME(PKTIM(IPEAK),PKTIM(IPEAK))
	CALL IFTIME(BKGTIM(IPEAK),BKGTIM(IPEAK))
	RETURN
	END
