C!****************************************************************************
	SUBROUTINE USEPEAK
C!****************************************************************************
C Subroutine to determine the peaks to be used in the search
C Uses: numob, ds(*), inten(*),
C Sets: NOBS, NOBS1, OBH, OBL, OB2T, DOBS, IOBS
C!****************************************************************************
	real maxint

	include '[-.shared]jcpds.parms'
	include 'SEARCH.cmn'
	include 'SEARCH.par'

	real peak, x1

	LOGICAL flag,DTOTT,TTTOD,DTOPDF,PDFTOD,DTOIND,INDTOD
	LOGICAL PDFTOCU,CUTOPDF,PDFTOIND
	LOGICAL EXRtst
	INTEGER I
	INTEGER*2 i2
C++++++
	nobs = 0
	maxint = 0.0
C
	CALL DSORT(ds,inten,numob)

	DO 101 I=1,numob
C convert D-space to 2theta at Cu Ka
	  flag = dtott(ds(i), peak, 1.5405598)
C if peak is not in the Cu Ewald sphere, go on to the next
	  if (.not. flag) goto 101
C
	  flag = dtoPDF(OB2T(nobs+1),ds(i))
	  if (.not. flag) goto 101
C evaluate the PDF and IND index values on both edges of the peak window
C   get the high (angle) value
	  x1 = min(180., peak + abs(d2t))
	  OBH(nobs+1) = 31416
	  flag = CUtoPDF(OBH(nobs+1),x1)
C   get the low (angle) value
	  x1 = max(0., peak - abs(d2t))
	  OBL(nobs+1) = -31416
	  flag = CUtoPDF(OBL(nobs+1),x1)
C  See if any bits in the range have not been set as excluded
	  do i2=OBL(nobs+1),OBH(nobs+1)
	    if (.not. EXRtst(extbl,i2)) goto 707
	  enddo
	  goto 101

C OK this peak is a `keeper' -- make a copy of the non-excluded peak
707	  if (inten(i) .gt. 0) then
	    nobs = nobs + 1
	    iobs(nobs) = inten(i)
	    dobs(nobs) = ds(i)
	    maxint = max(maxint,inten(i))
	  endif
101	CONTINUE
C--------------------------------------------------------------------------
C normalize the non-excluded peaks; flag those above the threshold
	NOBS1 = 0
	DO I=1,nobs
	  SIobs(i) = .false.
	  iobs(nobs) = NINT(inten(i)* 100. / maxint)
	  IF (iobs(I) .gt. iomin) THEN
	    SIobs(i) = .true.
	    NOBS1 = NOBS1 + 1
	  ENDIF
	ENDDO
	return
	end
C!****************************************************************************
	SUBROUTINE SETNULL
C!****************************************************************************
C Subroutine to set the NULL regions table
C!****************************************************************************

	include '[-.shared]jcpds.parms'
	include 'SEARCH.cmn'
	include 'SEARCH.par'

	INTEGER N,I,J
	INTEGER*2 i2
	INTEGER*4 ind1,ind2
	REAL x1
	real peak
	real dmin,dmax

	LOGICAL EXRtst
	LOGICAL flag,DTOTT,TTTOD,DTOPDF,PDFTOD,DTOIND,INDTOD
	LOGICAL PDFTOCU,CUTOPDF,PDFTOIND
C Clear the flags for regions without peaks, except for ignored regions
	DO 100 n=1,N_REGIONS
C find the range of D's for this index value
	  FLAG = INDTOD(n,n,DMAX,DMIN,N_REGIONS)
C convert d's to PDF values
	  FLAG = DTOPDF(amin,DMAX)
	  IF (.not. FLAG) amin = -31416
	  FLAG = DTOPDF(amax,DMIN)
	  if (.not. flag) amax = 31416
C set NULLREG to be FALSE if any part of the region has been excluded
	  NULLREG(N) = .false.
C if any bit has been set, the region cannot be considered NULL
	  do i2=amin,amax
	    if (EXRtst(extbl,i2)) goto 100
	  ENDDO
	  NULLREG(N) = .true.
100	CONTINUE

C Set the NULLREG array to be FALSE wherever there is intensity above
C the threshold
	DO I=1,nobs
C evaluate the PDF and IND index values on both edges of the peak window
	  IF (iobs(i) .ge. iimin) then
	    flag = dtott(ds(i), peak, 1.5405598)
C   get the high (angle) value
	    x1 = min(180., peak + abs(FFWHM))
	    amin = 31416
	    flag = CUtoPDF(amin,x1)
	    flag = PDFTOIND(amin,ind2,N_REGIONS)
C   get the low (angle) value
	    x1 = max(0., peak - abs(FFWHM))
	    amax = -31416
	    flag = CUtoPDF(amax,x1)
	    flag = PDFTOIND(amax,ind1,N_REGIONS)
C set regions with peaks as present -- not devoid of intensity
	    DO j=ind1,ind2
	      NULLREG(j) = .false.
	    ENDDO
	  ENDIF
	ENDDO
	RETURN
	END
