C---------------------------------------------------------------------------
C---------------------------------------------------------------------------
	SUBROUTINE TBLWRITE(filename,len,*)
C
C	Writes out a table of hits and misses
C---------------------------------------------------------------------------
C
	include 'LOGIC.common'
	character*(*) filename		!  name of file to write
	INTEGER*4 LEN			!  length of filename
C	*				!  alternate return address (file error)

	integer*4 NWORDS		!  number of required words in table
	logical exists
C
	OPEN(UNIT=3,STATUS='NEW',FORM='UNFORMATTED',FILE=filename(1:len),
	1	defaultfile='.usrpdf',ERR=999)
	INQUIRE(FILE=filename(1:len),NAME=filename,
	1	defaultfile='.usrpdf')
	len = lench(filename)
	write (lo,'(a)') ' Writing file: '//filename(1:len)
	NWORDS = N_Lwords
	DO I=N_Lwords,1,-1
	  IF (table(i).eq. 0) THEN
	    NWORDS = I
	  ELSE
	    goto 100
	  ENDIF
	ENDDO

100	WRITE (3,err=998) N_cards,NWORDS,(table(i),i=1,NWORDS)
	WRITE (3,err=998) cnum
	do i=1,cnum	
	  WRITE (3,err=998) clen(i),context(i)(1:clen(i))
	enddo
	CLOSE(3)
	RETURN
999	WRITE(lo,'(A)') ' Error opening file '//filename
	RETURN 1
998	WRITE(lo,'(A)') ' Error writing file '//filename
	CLOSE(3)
	RETURN 1
	END
