	SUBROUTINE GETPAT (fulrec, BUFFER, NBYTES, STATUS)
	IMPLICIT INTEGER (A-Z)
	include 'logic.common'
	CHARACTER*80  BUFFER(135)
	IF (fulrec .gt. 0) THEN
	  offset = (fulrec-1)*80
	  IF (PDFCDROM) THEN 
	    CALL GETPAT_CD (offset, BUFFER, NBYTES, STATUS)
	  ELSE
	    CALL GETPAT_DISK (offset, BUFFER, NBYTES, STATUS, LPDF2)
	  ENDIF
	ELSEIF (fulrec .lt. 0) THEN
	  offset = (abs(fulrec)-1)*80
	  CALL GETPAT_DISK (offset, BUFFER, NBYTES, STATUS, LPDF2L)
	ELSE
	  NBYTES = 0
	  STATUS = 2
	ENDIF
D	type *,(BUFFER(I),I=1,nbytes/80)
	RETURN
	END
C======================================================================
	SUBROUTINE GETPAT_DISK (STARTING_BYTE, BUFFER, NBYTES, STATUS,
     1	LUNPDF2)
C
C	<<<<<<<<<<<<<<<< DISK VERSION, VAX >>>>>>>>>>>>>>>>>
C
C	   Routine to retrieve a pattern from the PDF-2 magnetic DISK based
C	   distributed data base (must be copied to and reside on disk),
C	   given it's byte offset (beginning at zero).
C
C	   STARTING_BYTE - Byte offset from the start of the file, from 0
C	   BUFFER        - an 80 col buffer to put the AIDS format cards in
C	   NBYTES        - number of bytes read for this pattern
C	   STATUS        - 0 if no error, 1 if no end of block found
C	   LUNPDF2       - LUN for PDF-2 file
C
C	   By: Mark Holomany      Date: 25-Apr-1988
C
C renamed to GETPAT_DISK for Powder Suite, LUN is now passed
C ERROR CORRECTED: NBYTES is now set properly
C                                                  Brian Toby 11/90
C======================================================================

	IMPLICIT INTEGER (A-Z)

	CHARACTER*80  BUFFER(135)

 	INCLUDE  'CDROM.CMN'
	INCLUDE  'IORET.CMN'
       	INCLUDE  'PDFDATA.CMN'

	STARTING_RECORD = (STARTING_BYTE + 80)/80
C
C	 ... Read in data set starting at STARTING_RECORD
	STATUS = 0
	DO 4010 NREC=1,135
	READ (LUNPDF2,REC=STARTING_RECORD+NREC-1,ERR=9002)
     1		BUFFER(NREC)
	IF(BUFFER(NREC)(80:80) .EQ. 'K') THEN
D	type *,NREC,' Records read, start=',starting_record
	   NBYTES = (NREC)*80
	   GO TO 9000
	ENDIF
4010	CONTINUE
	CALL ERRTXT ('E', '>135 cards read in AIDS format in GETPAT')
9002	STATUS = 1

9000	RETURN
	END
C=======================================================================
	SUBROUTINE GETPAT_CD (STARTING_BYTE, BUFFER, NBYTES, STATUS)
C
C	<<<<<<<<<<<<<<<< CD-ROM VERSION, VAX >>>>>>>>>>>>>>>>>
C
C	   Retrieve a pattern from the CD-ROM given it's byte offset from
C	   the beginning of the file, starting at 0.
C
C	   STARTING_BYTE - Byte offset from the start of the file, from 0
C	   BUFFER        - an 80 col buffer to put the AIDS format cards in
C	   NBYTES        - number of bytes read for this pattern
C	   STATUS        - 0 if no error, 1 if error
C
C	   By: Mark Holomany     Date: 25-Apr-1988
C
C renamed to GETPAT_CD for Powder Suite by Brian Toby 10/90; no other changes
C=======================================================================

	IMPLICIT INTEGER (A-Z)

	CHARACTER*80  BUFFER(135)
	CHARACTER*4096 C_IBUF
	BYTE          IBUF(4096)
	EQUIVALENCE  (C_IBUF, IBUF(1))

	INCLUDE  'CDROM.CMN'
	INCLUDE  'IORET.CMN'
       	INCLUDE  'PDFDATA.CMN'

	STATUS = 0
C                                          
C	 ... compute starting block and byte offset within block
	BYTE_OFFSET = JMOD (STARTING_BYTE + 1, 512)
	IF(BYTE_OFFSET .EQ. 0) THEN
	   BYTE_OFFSET = 1
	ENDIF

	STARTING_BLOCK = STARTING_BYTE/512 + 1
	
	VBLK   = STARTING_BLOCK
	SIZE   = 0
	NBYTES = 0
	NCARD  = 0

3010	CONTINUE
	CALL CDREAD (CDROM_CHAN, CDROM_SBLK, VBLK, 1, IBUF, IERR)
        IF(IERR .NE. 0) GO TO 8010
	IF(SIZE .GT. 0) THEN
	   BUFFER(NCARD)(SIZE+1:) = C_IBUF(1:80-SIZE)
	   IF(BUFFER(NCARD)(80:80) .EQ. 'K') GO TO 9000
	   START = 80 - SIZE + 1
	   SIZE = 0
	ELSE
	   START  = BYTE_OFFSET
	   IF(VBLK .NE. STARTING_BLOCK) START = 1
	ENDIF

	NC1 = START
	DO 4010 NC=NC1,511,80
	IF(512 - NC + 1  .GE. 80) THEN
C	    ... Full 80 columns left in buffer
	   NBYTES = NBYTES + 80
	   NCARD = NCARD + 1
	   IF(NCARD .GT. 135) GO TO 8010
	   BUFFER(NCARD) = C_IBUF(NC:NC+79)
	   IF(BUFFER(NCARD)(80:80) .EQ. 'K') GO TO 9000

	ELSE
C	    ... Fraction of 80 column card left in buffer
	   SIZE = 512 - NC + 1
	   NBYTES = NBYTES + 80
	   NCARD = NCARD + 1
	   IF(NCARD .GT. 135) GO TO 8010
	   BUFFER(NCARD)(1:SIZE) = C_IBUF(NC:512)
	   GO TO 50
	ENDIF

4010	CONTINUE
50	VBLK = VBLK + 1
	GO TO 3010

8010	STATUS = 1

9000	RETURN
	END
