	SUBROUTINE OPTIM( NPTBG, CPSBG, NPTPK, CPSPK, TIMLIM, FIXERR, 
     &     BKGTIM, PKTIM, IOPTIM )
C
C	Purpose:	COUNT OPTIMIZATION ROUTINE
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 TIMLIM.  If the total time calculates to be greater then
C  the routine converts to fixed time optimization.
C
C	Inputs: NPTBG, CPSBG, NPTPK, CPSPK, TIMLIM, and FIXERR.
C		NPTBG = Number of background points to be counted
C		CPSBG = Intensity at background in counts / second
C		NPTPK = Number of peak points to be counted
C		CPSPK = Intensity at the peak in counts / second
C	 	TIMLIM = Maximum time allowed to count this peak in minutes
C	 	FIXERR = desired error as a percent
C	Outputs: BKGTIM, PKTIM and IOPTIM.
C		BKGTIM = Time to count at each background point
C		PKTIM = Time to count at each peak point
C		IOPTIM = 1 for fixed time mode used or 2 for fixed error mode
C	Other variables:
C		R = ratio of peak to background intensities
C		RINV = ratio of background to peak intensities
C		TOTIME = The total time that will be spent counting this peak
C	References: SQRT 
C
C	Modifications:
C	Who	When	What
C	JDW01	9DEC86	force count time to be greater than 0.1 sec.
C	JDW02	19NOV87 Call RADIX count time round off routine.
C
	INCLUDE '[102,2]INP.COM'
	INTEGER*2 NPTBG, NPTPK, IOPTIM
	REAL*4 CPSBG, CPSPK, TIMLIM, FIXERR, BKGTIM, PKTIM, 
     &    R, RINV, TOTIME
C
	PARAMETER MINTIM = 0.1		!JDW01
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(CPSPK/CPSBG)
	IF (R.LT.1.0) R = 1.0
	RINV = 1.0/R
C
C***********************************************************************
C
C  BRANCH TO THE PROPER COUNT MODE ROUTINE
C
C  Did user ask for fix time mode
100	IF (FIXERR.EQ.0.0) GOTO 200
C
C  We can't do fix error mode with these equal.
	IF (CPSBG.GE.CPSPK) GOTO 200
C
C***********************************************************************
C
C  COMPUTE THE NUMBER OF SECONDS COUNTING THE PEAK POINTS
C  FOR FIXED ERROR MODE
C
	IOPTIM = 2
	TOTIME = (CPSPK+R*CPSBG) / 
     1   ((FIXERR*FIXERR/10000.)*(CPSPK-CPSBG)*(CPSPK-CPSBG))
C
D	TYPE 104, FIXERR,TOTIME
D 104	FORMAT('! OPTIM:  FIXERR=',F7.2,' TOTIM=',F7.2)
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  FIXED TIME MODE
C
200	IOPTIM = 1
	TOTIME = 60.0*TIMLIM / (1.0+RINV)
D	TYPE 104, FIXERR,TOTIME
D	IF(FIXERR.NE.0.0) TYPE*,'OPTIM CONVERTED TO FIX TIME MODE'
C
C***********************************************************************
C
C  FOR BOTH MODES:
C   GIVEN THE NUMBER OF SECONDS COUNTING PEAK POINTS
C   COMPUTE THE COUNT TIMES FOR EACH PEAK POINT AND BACKGROUND POINT
C
C Allow max of 9000. seconds (2.5 hours) per point (should be enough!)
300	IF (TOTIME/NPTPK.GT.9000.) TOTIME = 9000.*NPTPK
C
	PKTIM = TOTIME/NPTPK
C
	BKGTIM = 0.0
	IF (NPTBG.NE.0)	BKGTIM = TOTIME / (NPTBG * R)
C
C   Set some hard limits to cure a problem where the interface collected
C   data faster than it could transmit to the computer at 2400 baud.
C   Never count for less than .1 seconds.
	IF(PKTIM.LT. MINTIM) PKTIM = MINTIM		!JDW01
	IF(BKGTIM.LT. MINTIM) BKGTIM = MINTIM		!JDW01
C
C***********************************************************************
C
C  DEBUG PRINT OUT BEFORE RETURNING
C
D	TYPE 9004, 0, 
D    &  FIXERR,TIMLIM, NPTBG,CPSBG,BKGTIM, 
D    &  NPTPK,CPSPK,PKTIM,IOPTIM
D9004	FORMAT('! OPTIM RESULTS FOR PEAK #',I3,
D    &  '  COUNT ERROR:',F7.2,'  TIME LIMIT:',F7.2,/,
D    &  '!  # BKG PTS:',I7,',  BKG CPS:',F10.2,
D    &  ',  BKGCNT TIME:',F10.2, /, '!  # PEAK PTS:',I6,
D    &  ',  PEAK CPS:',F9.2,',  PEAK CNT TIME:',F9.2,/, '! IOPTIM: ',I1)
C
	CALL IFTIME( PKTIM,PKTIM)	!JDW02
	CALL IFTIME( BKGTIM,BKGTIM)	!JDW02
	RETURN
	END
