C*************************************************************************
	SUBROUTINE WRBIT(tbl,SNAME,S_LN,bitmap,bitmap_ln,dname,d_ln,
	1	lsname,ls_ln,*)
C*************************************************************************
C Subroutine to write a BITMAP file of the hits from a search
C*************************************************************************
	include '[-.shared]jcpds.parms/list'
	include 'SEARCH.cmn'
	include 'SEARCH.par'
	include '[-.shared]TBL.cmn'

	INTEGER*4 TBL(*)
	CHARACTER*(*) sname,bitmap,dname,lsname
	INTEGER s_ln,bitmap_ln,d_ln,ls_ln

	CHARACTER line*100
	integer*4 NW
	real intnorm

C set the intensity normalization constant -- if needed
	intnorm = inten(1)
	DO i=2,numob
	  intnorm = max(intnorm,inten(i))
	ENDDO
	if (intnorm .gt. 9999.) then
	  intnorm = 100./intnorm
	else
	  intnorm = 1.0
	endif

C open output file for matching cards
	INQUIRE(defaultfile=sname(1:s_ln),file='.USRPDF',
	1	NAME=lsname)
	ls_ln = lench(lsname)
	open(unit=out,defaultfile=sname(1:s_ln),file='.USRPDF',err=701,
	1	status='new',form='unformatted')
	write(LO,'(A)') ' Writing search results onto file:'//
	1	lsname(1:ls_ln),' '
	NW = N_Lwords
	DO I=N_Lwords,1,-1
	  IF (tbl(i).eq. 0) THEN
	    NW = I
	  ELSE
	    goto 110
	  ENDIF
	ENDDO
110	WRITE(OUT,err=712) N_cards,NW,(tbl(i),i=1,NW)
	k = cnum + 8
	IF (d_ln .gt. 0) k = k + 1
	k = k + 2*((numob-1)/10 + 1)
	write(out,err=712) k

	write(out,err=712) 11,'SEARCH'//ver_no

	write (line,'(A)') '!... TITLE: '//TITLES
	len = lench(line)
	write(out,err=712) len,line
	IF (d_ln .gt. 0) write(out,err=712) d_ln+14,'!... Data file: '//
	1	dname(:d_ln)

	write(out,err=712) 31,'!... Input peaks, intensities:  '

	do I=1,numob,10
	  WRITE (line,'(a,10f6.2)') '!...D''s ',(DS(j),j=i,min(i+9,numob))
	  len = lench(line)
	  write(out,err=712) len,line
	  WRITE (line,'(a,10f6.1)') '!...I''s ',(INTEN(j)*INTNORM,
	1	j=i,min(i+9,numob))
	  len = lench(line)
	  write(out,err=712) len,line
	enddo

	write (line,240) d2t,JCMSMX,imin
240	Format('!...  DEL_2THETA=',f6.3,', DB_max_miss=',i3,
	1	', DB_min_int=',i2)
	len = lench(line)
	write(out,err=712) len,line

	write (line,241) MINMATCH,iomin,mphase
241	Format('!...  OBS_min_lines=',i3,', OBS_min_int=',i2,
	1	', MAX_hits=',i6)
	len = lench(line)
	write(out,err=712) len,line

	write (line,243) iindex,iimin,FFWHM
243	FORMAT('!... Forbid_Index =',i3,', F_Imin=',i2,', FWHM=',f6.2)
	len = lench(line)
	write(out,err=712) len,line

	write (line,242) pindex,pimin,NFOM,NSORT
242	FORMAT('!... Req_Index =',i3,', R_Imin=',i2,', Calc_type=',i1,
	1	', Num_Sort=',i3)
	len = lench(line)
	write(out,err=712) len,line

	IF (NSET .LE.-1) THEN
	  line = 'User subfile = '//bitmap(1:bitmap_ln)
	ELSE
	  if (nset .eq. 0) nset = max_sub + 3
	  line  = SUBLBL(NSET)
	ENDIF
	line = '!... Search from:'//line
	len = lench(line)
	write(out,err=712) len,line
	do i=1,cnum
	  write(out,err=712) CLEN(i)+5,'!... '//CONTEXT(i)(1:CLEN(i))
	enddo
	CLOSE(out)
	RETURN
C------------------------------------------------------------------------
701    write(LO,'(A)') ' WRBIT ERROR opening output file:'
	1	//lsname(1:ls_ln)
	ls_ln = 0
	RETURN 1
712	write(LO,'(A)') ' WRBIT ERROR writing to output file:'
	1	//lsname(1:ls_ln)
	CLOSE(out)
	ls_ln = 0
	RETURN 1
	END
