C==========================================================================
	Subroutine RET3_INIT(STATUS,PDFDISK,PDFCDROM,PDFLOCAL,
	1	LPDF2,LPDF2L)
C==========================================================================
C open the PDF-2 databases; set flags according to which files are opened
	INTEGER STATUS,LPDF2,LPDF2L
	LOGICAL PDFDISK,PDFCDROM,PDFLOCAL
C==========================================================================

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

	logical flag,get_logical
	character*11 lgprc
	parameter (lgprc='lnm$process')
	character*80 file
	character*40 node,device,dir,name*60,type*20,ver*5
	integer*2 len_node,len_device,len_dir,len_name,len_type,len_ver

C assume failure of open
	PDFDISK = .false.
	PDFCDROM = .false.
	PDFLOCAL = .false.
	STATUS = 1

C Try to open the PDF-2 on disk, then try the CDROM
	flag = get_logical('PS_PDF2_DISK',file,80,ln,lgprc)
	if (flag) then
	  OPEN (FILE='PS_PDF2_DISK:',defaultfile='JCPDS.DIRECT',
     1		UNIT=LPDF2, ACCESS='DIRECT', FORM='UNFORMATTED', 
     2		RECORDTYPE='FIXED', RECL=20, STATUS='OLD',ERR=191,
     3		READONLY, SHARED)
	  PDFDISK = .true.
	  WRITE (*,'(/A/)') 
	  inquire(unit=LPDF2,name=file)
	  write (*,'(2A)') ' Opening the PDF-2 disk file '
     1		,file(:lench(file))
	  goto 200
	ENDIF
C Disk was not successful, try the CDROM
C ...get the definition of PS_PDF2_CDROM
191	flag = get_logical('PS_PDF2_CDROM',file,80,ln,lgprc)
C ...fill in unspecified fields with PDF2.DAT
	if (flag) then
	  call fparse(file,node,len_node,device,len_device,
     1		dir,len_dir,name,len_name,type,len_type,ver,len_ver)
	  if (name(:len_name) .eq. ' ') then
	    name = 'pdf2'
	    len_name = 4
	  endif
	  if (type(:len_type) .eq. ' ') then
	    type = '.dat'
	    len_type = 4
	  endif
	  file = device(:len_device)//dir(:len_dir)
     1		//name(:len_name)//type(:len_type)
C	...Open files and perform initialization
	  CALL CDOPEN (FILE, CDROM_CHAN, CDROM_SBLK, CDROM_FSIZE, ierr)
	  IF(ierr .EQ. 0) THEN
	    PDFCDROM = .true.
	    write (*,'(A)') ' Opening the PDF-2 on CDROM'
	  ENDIF
	  goto 200
	ENDIF
C---------------------------------------------------------------------------
C no PDF-2 logical name specified
C---------------------------------------------------------------------------
C do not flag error on exit
	STATUS = 0
	RETURN
C---------------------------------------------------------------------------
C Open the local PDF-2 file, if one exists
C---------------------------------------------------------------------------
200	OPEN (FILE='PS_PDF2_LOCAL:',defaultfile='LOCAL.JCPDS',
     1		UNIT=LPDF2L, ACCESS='DIRECT', FORM='UNFORMATTED', 
     2		RECORDTYPE='FIXED', RECL=20, STATUS='OLD',ERR=1000,
     3		READONLY, SHARED)
	inquire(unit=LPDF2L,name=file)
	write (*,'(2A)') ' Opening local PDF-2 file ',file(:lench(file))
	PDFLOCAL = .true.
1000	continue

	IF (PDFDISK .or. PDFCDROM) CALL INITRET3(STATUS)
	IF (STATUS .NE. 0) THEN
	  PDFDISK = .false.
	  PDFCDROM = .false.
	  PDFLOCAL = .false.
	ENDIF
	RETURN
	END
C======================================================================
	SUBROUTINE INITRET3 (STATUS)
C
C	Initializes stuff for the PDF-2 Database
C	   VERSION for both CDROM and DISK
C
C	   By: Mark Holomany     Date: 17-Oct-1985
C	   Modified by B. H. Toby for the Powder Suite
C======================================================================

	IMPLICIT INTEGER (A-Z)

!	CHARACTER*1  ESC
	CHARACTER*2  INPI
	CHARACTER*3  INCODE
	CHARACTER*5  MINVER
	CHARACTER*6  TMPCOD
	CHARACTER*10 INTEXT
	CHARACTER*64 FLSPEC
	CHARACTER*73 EXPBUF
	CHARACTER*80 LINBUF
	INTEGER*2    ININDX
	LOGICAL      WRTITL

	INCLUDE  'DSPCDS.CMN'

	CHARACTER*2  DSPFN(MAXFNC)
	DATA  DSPFN /'* ', 'DA', 'DD', 'SE', 'PG'/

	INCLUDE  'COUNTS.CMN'
	INCLUDE  'GRPIND.CMN'        
	INCLUDE  'IORET.CMN'
	INCLUDE  'PICDS.CMN'
	INCLUDE  'RET3.CMN'
	INCLUDE  'STDCDS.CMN'
CCCCC	INCLUDE  '($FORIOSDEF)/nolist'
                    
	STATUS = 1
!	ESC = CHAR(27)
C set flag to PDF2
C
C      	...Initialize time, date, etc
	REVNO = '1.10 '
	RDATE = '28-Nov-90'
	CTIME = 'XXXXXXXX'
	CDATE = 'XXXXXXXXX'
	HOST  = 'VAX'
	DBSW = 'PDF2'

C
C	 ... Read the following in to initialize.
C
C	 ...   NUMMIN - Mineral names index
C	 ...   NUMCOD - CODENS file
C	 ...   NUMENT - Database (total possible entries in the bitmap)
C	 ...   INIT_SET - Current PDF Set number to which the init file
C	 ...              pertains
C
C!	FLSPEC = 'RET$DATA:DISP.INIT'
	FLSPEC = 'PS_PDF2_INDEX:DISP.INIT'
	OPEN (UNIT=NFTMP1, FILE=FLSPEC, ERR=901, 
	1     STATUS='OLD',READONLY, SHARED)
	READ (NFTMP1, 1101) INIT_SET, NUMMIN, NUMCOD, NUMENT, MINVER
1101	FORMAT(A2 / I6 / I6 / I6 / A5)
	CLOSE (UNIT=NFTMP1, DISP='KEEP')
C

C	... Display title screen
!	CALL BANNER
!	CALL TITLE (WRTITL)
!	IF(WRTITL) WRITE(NOUT,1659) ESC,ESC,ESC
!1659	FORMAT(' ',  A,'[24;1H',  A,'[7m',
!	1      29X,'Push RETURN to continue ',26X,A,'[m',$)
C
C	... Open all files
C!	FLSPEC = 'RET$DATA:MINERAL' // INIT_SET // '.DAT'
	FLSPEC = 'PS_PDF2_INDEX:MINERAL' // INIT_SET // '.DAT'
	OPEN (UNIT=NMINGP, 
	1	FILE=FLSPEC, ERR=901, 
	2     FORM='FORMATTED', ACCESS='DIRECT', 
	3     STATUS = 'OLD', SHARED, READONLY)

C!	FLSPEC = 'RET$DATA:MINNAM' // INIT_SET // '.IND'
!	FLSPEC = 'PS_PDF2_INDEX:MINNAM' // INIT_SET // '.IND'
!	OPEN (UNIT=NMINRL, 
!	1	FILE=FLSPEC, ERR=901, 
!	2     ACCESS='DIRECT', FORM='UNFORMATTED',
!	3     RECORDTYPE='FIXED', RECL=13,
!	4     STATUS='OLD', SHARED, READONLY)

C!	FLSPEC = 'RET$DATA:CODENS' // INIT_SET // '.DAT'
	FLSPEC = 'PS_PDF2_INDEX:CODENS' // INIT_SET // '.DAT'
	OPEN (UNIT=NCODEN, 
	1	FILE=FLSPEC, ERR=901, 
	2       ACCESS='DIRECT', FORM='FORMATTED',
	3       STATUS='OLD', SHARED, READONLY)

C!	FLSPEC = 'RET$DATA:RETHELP' // INIT_SET // '.HLP'
!	FLSPEC = 'PS_PDF2_INDEX:RETHELP' // INIT_SET // '.HLP'
!	OPEN (UNIT=HLPTXT, 
!	1	FILE=FLSPEC, ERR=901, 
!	1     ACCESS='DIRECT', FORM='FORMATTED', RECL=80,
!	2     STATUS='OLD', SHARED, READONLY)
!
C!	FLSPEC = 'RET$DATA:RETHELP' // INIT_SET // '.IND'
!	FLSPEC = 'PS_PDF2_INDEX:RETHELP' // INIT_SET // '.IND'
!	OPEN (UNIT=HLPIND, 
!	1	FILE=FLSPEC, ERR=901, 
!	1     ACCESS='DIRECT', FORM='UNFORMATTED', RECL=84,
!	2     STATUS='OLD', SHARED, READONLY)
!
!	OPEN (UNIT=PRINTR, FILE='PRINT.PDFCARD', STATUS='NEW')
!	OPEN (UNIT=PRINTR,
!	1     FILE='RET$SCRATCH:PRINT.PDFCARD',
!	2     STATUS='NEW')
!
C
C       ... Read binary file to fill commons
C!	FLSPEC = 'RET$DATA:VAXPDF' // INIT_SET // '.BIN'
	FLSPEC = 'PS_PDF2_INDEX:VAXPDF' // INIT_SET // '.BIN'
      OPEN (UNIT=NFTMP1, 
	1	FILE=FLSPEC, ERR=901, 
     1      FORM='UNFORMATTED',
     2      STATUS='OLD', READONLY, SHARED)
      READ (NFTMP1) GRPCDS, NUMCDS, CODIND
      READ (NFTMP1) NPICDS, LNTEXT, LNTEX2, PICDS, PICD2, PLTEXT, PLTEX2
      READ (NFTMP1) NCODES, LENEXP, BEGEXP, CODE, EXPAND

C
C	... Set default translation to non-ASCII
	ASCII = .FALSE.
C
C	... Define d-spacing footnote codes
	DO 4010 N=1,MAXFNC
	CALL PITRAN (DSPFN(N), INTEXT, LEN)
	IF(DSPFN(N) .EQ. '* ') THEN
	   DSPFNC(N) = '*'
	ELSE
	   DSPFNC(N) = INTEXT(1:1)
	   IF(LEN .GT. 1) CALL ERRTXT ('E',
	1    'Footnote code expansion > 1 char')
	ENDIF
4010	CONTINUE
C
C	... Establish line drawing character set
	CALL LINES
C
C	... Reset photocomp routine execution switches (init)
	CALL ONCE
C
C	... Send errors to file for demo/production
	CALL ERRTXT ('*', 'F1:PDF2_ERR.LIS')
C DEBUG mode: display and record error messages
D	CALL ERRTXT ('*', 'F2:PDF2_ERR.LIS')
!	IF(WRTITL) READ (NIN, '(A)', ERR=9000, END=9000) REPLY

9000	STATUS = 0
	RETURN
901	STATUS = 1
	WRITE(*,'(2A)') ' PDF-2 ERROR: unable to open file: ',
	1	FLSPEC(:LENCH(FLSPEC))
	RETURN

	END
