	SUBROUTINE LOGSUB(ilogic,*)
C This subroutine performs the actual logic for the ADD, MATCH and CULL 
C operations
	include 'LOGIC.common'

	INTEGER STATUS,CLI$DCL_PARSE,CLI$GET_VALUE,CLI$PRESENT
	EXTERNAL CLI$_NEGATED
	CHARACTER*1 ians,string*10
	INTEGER ln
	INTEGER*2 tracelen,templen
	CHARACTER*200 trace,temp
	CHARACTER*80 brline,line

	IF (CLI$PRESENT('PEAKS')) THEN
	  CALL GETD(trace,tracelen,ilogic,*3)
	ELSEIF (CLI$PRESENT('COUNT')) THEN
	  CALL GETELMC(trace,tracelen,ilogic,*3)
	ELSEIF (CLI$PRESENT('CARDS')) THEN
C set the list of cards as TABLE1
	  CALL GETCARDS(*3,trace,tracelen)
	ELSEIF (CLI$PRESENT('ELEMENTS') .or. CLI$PRESENT('E_SPAN') .or.
	1	(CLI$PRESENT('ELEMENTS') .eq. %LOC(CLI$_NEGATED)) .or.
	1	CLI$PRESENT('OPTIONAL') .or. CLI$PRESENT('O_SPAN')) THEN
C get the list of elements and the "search logic"
	  CALL GETELEM(*3,trace,tracelen)
C use the name against the current list
	ELSEIF (ilogic .ne. 1 .and. CLI$PRESENT('NAME')) THEN
	  CALL GET_NAME(*3,trace,tracelen)
	ELSE
	  write (lo,'(a)') ' The logical options are:',
	1	' 	(1) ELEMENTS -- search by chemical composition',
	1	' 	(2) CARDS    -- search by JCPDS entry number',
	1	' 	(3) PEAKS    -- search by angles/d-spaces',
	1	' 	(4) COUNT    -- search by # of elements in material'
	  IF (ilogic .ne. 1) then
83	format(1x,'	(5) NAME     -- search for chemical/mineral name',a)
	    IF (nphase .gt. 10000) then
		WRITE(lo,83) ' (VERY, VERY SLOW)'
 	    ELSEIF (nphase .gt. 1000) then
		WRITE(lo,83) ' (VERY SLOW)'
	    ELSE
		WRITE(lo,83) ' (SLOW)'
	    ENDIF
	  ENDIF
	  write (lo,'(a)') ' ','$Choose an option number: '
	  read (li,'(i)',err=3) i
	  IF (i .eq. 3) THEN
	    CALL GETD(trace,tracelen,ilogic,*3)
	  ELSEIF (i .eq. 4) THEN
	    CALL GETELMC(trace,tracelen,ilogic,*3)
	  ELSEIF (i .eq. 2) THEN
	    CALL GETCARDS(*3,trace,tracelen)
	  ELSEIF (i .eq. 1) THEN
	    CALL GETELEM(*3,trace,tracelen)
	  ELSEIF (i .eq. 5 .and. ilogic .ne. 1) THEN
	    CALL GET_NAME(*3,trace,tracelen)
	  ELSE
	    return 1
	  ENDIF
	ENDIF

C count the number of matching cards
	CALL TBLCOUNT(TABLE1,N_Lwords,nph1)
	WRITE(LO,104) NPH1
104	FORMAT(/,' There are',i6,' entries corresponding to logic:')
	temp = trace(1:tracelen)
32	line = brline(temp,65,ln)
	templen = lench(temp)
	if (templen .gt. 0) then
	  WRITE(LO,'(4x,a)') line(1:ln)//' -'
	  temp = '    '//temp
	  goto 32
	else
	  WRITE(LO,'(4x,a)') line(1:ln)
	endif
C remove deleted entries
	IF (ILOGIC .eq. 1 .and. .not. DELETED) then 
C Open and read from Subfile
	  OPEN (UNIT=IN,NAME='PS_BITMAP:misc.bitmap',STATUS='OLD',
	1	FORM='UNFORMATTED',ACCESS='DIRECT',READONLY,SHARED,
	1	err=971)
C---------------------------------------------------------------------
C load the deleted entries
	  READ (IN,REC=9) (TABLE2(I),i=1,N_Lwords)
	  CLOSE (UNIT=IN)
C remove them from table 
	  CALL TBLnot(TABLE2,TABLE2,N_cards)
	  CALL TBLand(TABLE1,TABLE2,TABLE1,N_Lwords)
	  CALL TBLCOUNT(TABLE1,N_Lwords,nph1)
	  WRITE(LO,'(A,i5,A)') 
	1  ' ...After removing deleted entries there are',nph1,' entries'
	ENDIF

	WRITE(LO,101) NPHASE,N_cards
101	FORMAT(/,' Current Table contains',i6,' entries, out of',I6)

	IF (ILOGIC .eq. 1) then 
	  CALL TBLOR(TABLE,TABLE1,TABLE2,N_Lwords)
	  string = 'ADD'
	ELSEIF (ILOGIC .eq. 2) then 
	  CALL TBLAND(TABLE,TABLE1,TABLE2,N_Lwords)
	  string = 'MATCH'
	ELSEIF (ILOGIC .eq. 3) then 
	  CALL TBLnot(TABLE1,TABLE1,N_cards)
	  CALL TBLand(TABLE,TABLE1,TABLE2,N_Lwords)
	  string = 'CULL'
	ELSE
	  RETURN 1
	ENDIF	

	CALL TBLCOUNT(TABLE2,N_Lwords,nph2)
	ln = lench(string)
	WRITE(LO,102) STRING(1:ln),nph2
102	FORMAT(' After the ',A,' operation, the Table will contain',
	1	i6,' entries')
	IF (PROMPT .and. nph2 .gt. 0) THEN
	  WRITE(LO,103) 'Y'
103	  FORMAT(/,'$Proceed with change to table? (Y/N) [default='
	1	,a,']: ')
	  read (li,'(a)') ians
	  if (ians .eq. 'N' .or. ians .eq. 'n') goto 3
	ELSEIF (PROMPT) THEN
	  WRITE(LO,103) 'N'
	  read (li,'(a)') ians
	  if (ians .ne. 'Y' .and. ians .ne. 'y') goto 3
	ENDIF
	trace = string(1:ln)//trace(1:tracelen)
31	cnum = min(100,cnum+1)
	context(cnum) = brline(trace,60,clen(cnum))
	tracelen = lench(trace)
	if (tracelen .gt. 0) then
	  context(cnum)(clen(cnum)+1:clen(cnum)+2) = ' -'
	  clen(cnum) = clen(cnum) + 2
	  trace = '    '//trace
	  goto 31
	endif

	CALL TBLMOVE(TABLE2,TABLE,N_Lwords)
	NPHASE = nph2
	RETURN
971	type *,'ERROR reading PS_BITMAP:misc.bitmap'
3	RETURN 1
	END
