C*************************************************************************
	subroutine GETBIT(iset,NAME,len_name,*)
C*************************************************************************
C Subroutine to read in a SUBFILE (BITMAP) 
C*************************************************************************
C
	CHARACTER*(*) name
	INTEGER len_name

	include '[-.shared]jcpds.parms'
	include 'SEARCH.cmn'
	include 'SEARCH.par'
        include '[-.shared]TBL.cmn'
	INTEGER*4 NW,NCARDS

	write (lo,71)
71	FORMAT(A)
C-------------------------------------------------------------------------
	IF (iset .le. -1) THEN
C Check: Is this a valid name? try to open file
	  open(unit=in,file=name(1:len_name),defaultfile='.USRPDF',
	1	err=711,status='old',readonly,shared,form='unformatted')
	  INQUIRE(FILE=name(1:len_name),defaultfile='.usrpdf',
	1	name=name)
	  len_name = lench(name)
	  write (lo,71) ' Reading bitmap file: '//name(1:len_name)

	  READ(IN,err=712) NCARDS,NW,(table1(i),i=1,MIN(N_Lwords,NW))
	  READ(IN,err=91) cnum
	  if (cnum .gt. 0) write (lo,71) ' Commands read from file:'
	  do i=1,cnum
	    READ(IN,err=91) CLEN(i),CONTEXT(i)(1:CLEN(i))
	    write(lo,'(5x,a)') CONTEXT(i)(1:CLEN(i))
	  enddo
	  write (lo,71) 
91	  CLOSE(IN)

	  IF (N_Lwords .lt. NW) THEN
	    Write (lo,*) 'GETBIT ERROR: too many words on file:'
	1	//name(1:len_name),NCARDS,N_cards
	    GOTO 3
	  ELSEIF (NW .lt. N_Lwords) THEN
	    DO I=NW+1,N_Lwords
	      table1(i) = 0
	    ENDDO
	  ENDIF
	  IF (NCARDS .ne. N_cards) then
	    Write (lo,*) 'GETBIT ERROR: wrong number of cards on file:'
	2	//name(1:len_name),NCARDS,N_cards
	    GOTO 3
	  ENDIF
	  WRITE(LO,172) 'User subfile: '//name(1:len_name)
172	  format(' Reading the ',a)
C-------------------------------------------------------------------------
	ELSEIF (iset .eq. max_sub+3 .or. iset .eq. 0) then
	  CALL TBLSET(TABLE1,N_cards,nph1)
	  WRITE(LO,172) SUBLBL(max_sub+3)(:LENCH(SUBLBL(max_sub+3)))
C-------------------------------------------------------------------------
	ELSEIF (iset .le. max_sub .and. subrec(iset) .gt. 0) then
C Open and read from Subfile
	  OPEN (UNIT=IN,NAME='PS_bitmap:misc.bitmap',STATUS='OLD',
	1	FORM='UNFORMATTED',ACCESS='DIRECT',shared,readonly,
	1	err=971)
	  WRITE(LO,172) SUBLBL(ISET)(:LENCH(SUBLBL(ISET)))
	  READ (IN,REC=subrec(iset),err=971) (TABLE1(I),i=1,N_Lwords)
	  CLOSE (UNIT=IN)
	  cnum = 0
	ELSE
	  write (lo,*) 'GETBIT ERROR -- invalid ISET number:',ISET
	  goto 3
	ENDIF
C-------------------------------------------------------------------------
	IF (iset .gt. 0 .and. iset .le. max_sub .and. 
	1	subrec(iset) .eq. DL_sub .and. .NOT. DELETED) THEN
	  WRITE(LO,'(A)') 
	1	' Setting DELETED/ENABLE ... to use deleted entries'
	  DELETED = .true.
	ELSEIF (.not. DELETED) THEN
C Open and read from Subfile
	  WRITE(LO,'(A)') ' ...Removing deleted entries'
	  OPEN (UNIT=IN,NAME='PS_bitmap:misc.bitmap',STATUS='OLD',
	1	FORM='UNFORMATTED',ACCESS='DIRECT',shared,readonly,
	1	err=971)
C load the deleted entries
	  READ (IN,REC=DL_sub,err=971) (TABLE2(I),i=1,N_Lwords)
	  CLOSE (UNIT=IN)
C set the table to all undeleted entries
	  CALL TBLnot(TABLE2,TABLE2,N_cards)
	  CALL TBLand(TABLE1,TABLE2,TABLE1,N_Lwords)
	ENDIF
C-------------------------------------------------------------------------
C  Count the number of bits in the table
	CALL TBLCOUNT(TABLE1,N_Lwords,nph1)
	WRITE(LO,75) nph1,N_cards
75	FORMAT('     This subfile has',i6,' selected cards, out of',I6
	1	,' total')
	RETURN
C------------------------------------------------------------------------
3	RETURN 1
C-------------------------------------------------------------------------
711	WRITE (LO,71) ' GETBIT ERROR -- User SUBFILE file not found:'//
	1	name(1:len_name)
	goto 3
C------------------------------------------------------------------------
712	WRITE (LO,71) ' GETBIT ERROR -- Unable to read SUBFILE file:'//
	1	name(1:len_name)
	goto 3
C------------------------------------------------------------------------
971	STOP 'ERROR opening/reading PS_bitmap:MISC.BITMAP File'
	END
