C-----------------------------------------------------------------------
	SUBROUTINE READPEAK(pname,nfil,STATUS,filform)
C-----------------------------------------------------------------------
C	USED TO READ A PEAK FILE IN ONE OF A NUMBER OF FORMATS
C
C   PNAME     name of file to read (file extension optional), the 
C   		  actual name of the file read is returned
C   NFIL      is the array to use for storage
C   STATUS    is returned as .TRUE. if the requested information is located
C   FILFORM   file format (optional), returned with actual format
C-----------------------------------------------------------------------
C
	include 'shared$common'
C
	character*(*) pname,filform
	CHARACTER*4 thisform/'PEAK'/
	INTEGER nfil

	LOGICAL exist,card1(80),card2(80)
!	CHARACTER*3 month,monlst(12)/'JAN','FEB','MAR','APR','MAY','JUN',
!	1	'JUL','AUG','SEP','OCT','NOV','DEC'/
	LOGICAL DTOTT,TTTOD,DTOPDF,PDFTOD,DTOIND,INDTOD,flag
	CHARACTER  temp*6
	LOGICAL STATUS
C---------------------------------------------------------------------------
C Assume that the read was not successful
	STATUS = .false.

C check the file format qualifier, if one exists
	IF (filform .ne. ' ') THEN
	  I = LENCH(filform)
	  IF (filform(:I) .ne. thisform(:I)) RETURN
	ENDIF

	NPKS(nfil) = 0
C----------------------------------------------------------------------
C attempt to read in file in Penn State Univ Standard D-I format
C----------------------------------------------------------------------
	INQUIRE(FILE=pname,EXIST=exist,defaultfile='.adr')
	IF (.not. exist) goto 101
	OPEN(UNIT=IN,FILE=pname,defaultfile='.adr',TYPE='OLD',
	1	ERR=210,FORM='FORMATTED',readonly,shared)
C read through the file until we find 'PEAK ' or run out of lines
	READ(IN,'(A)',err=211) ATITLE(nfil)
!	IF (ATITLE(nfil)(1:5) .eq. 'PEAK ') goto 222
	IF (ATITLE(nfil)(1:5) .eq. 'PEAK') goto 222
221	READ(IN,'(A)',err=211) temp
	IF (temp(1:5) .ne. 'PEAK ') goto 221
C	
222	IF (WAVE(nfil) .eq. 0.0) THEN
C set the wavelength to CU K alpha 1 for convenience
	  IRAD(nfil) = 'CU'
	  CALL WAVESET(IRAD(nfil),WAVE(nfil),WKA1(nfil),WKA2(nfil),*233)
233	  MONO(nfil) = .true.
	  WAVE(nfil) = WKA1(nfil)
	  WKA1(nfil) = WAVE(nfil)
	  WKA2(nfil) = WAVE(nfil)
	ENDIF
	do j=1,m$pks
	  READ (IN,'(14X,F8.0,2X,F3.0)',end=299,err=211) 
	1	DSPACE(J,nfil),PKHT(J,nfil)
	  NPKS(nfil) = j
	  call dTOtt(DSPACE(J,nfil),TT(J,nfil),wave(nfil))
	ENDDO
299	INQUIRE(UNIT=IN,NAME=pname)
	write (LO,'(I5,A)') NPKS(nfil),' Peaks read from PSU Std D-I file'
	WRITE (LO,'(A)') ' File: '//pname(:lench(pname))
	close(unit=IN)
	if (NPKS(nfil) .gt. 0) goto 2000
C----------------------------------------------------------------------
C attempt to read in file containing reflections (typ. LAZY/Pulverix)
C----------------------------------------------------------------------
101	INQUIRE(FILE=pname,EXIST=exist,defaultfile='.refl')
	IF (.not. exist) goto 210
	OPEN(UNIT=IN,FILE=pname,defaultfile='.refl',TYPE='OLD',
	1	ERR=210,FORM='FORMATTED',readonly,shared)
C read through the file until we find 'TABLE' or run out of lines
	READ(IN,'(A)',err=211) ATITLE(nfil)
121	READ(IN,'(A5,30X,f9.0)',err=211) temp,wave(nfil)
	IF (temp(1:5) .ne. 'TABLE') goto 121
C	
122	do j=1,m$pks
          READ (IN,'(3I3,i4,F9.0,F8.0,4F12.0)',end=199,err=211) 
	1	IH(J,nfil),IK(J,nfil),IL(J,nfil),M,
	1	dspace(J,nfil),TT(J,nfil),FLPCOR(J,nfil),
	1	STRU,FINTEG(J,nfil)
	  NPKS(nfil) = j
	ENDDO
199	INQUIRE(UNIT=IN,NAME=pname)
	write (LO,'(I5,A)') NPKS(nfil),' Peaks read from Reflection file'
	WRITE (LO,'(A)') ' File: '//pname(:lench(pname))
	close(unit=IN)
	ymax = FINTEG(1,nfil)
	do j=2,NPKS(nfil)
	  ymax = max(ymax,FINTEG(J,nfil))
	enddo
	do j=1,NPKS(nfil)
	  PKHT(J,nfil) = 1000. * FINTEG(J,nfil) / ymax
	enddo
	WKA1(nfil) = WAVE(nfil)
	WKA2(nfil) = WAVE(nfil)
	if (NPKS(nfil) .gt. 0) goto 2000
C----------------------------------------------------------------------
C attempt to read in file a SEARCH input file
C----------------------------------------------------------------------
211	close(unit=IN)
210	INQUIRE(FILE=pname,EXIST=exist,defaultfile='.SEA_IN')
	IF (.not. exist) goto 410
	OPEN(UNIT=IN,FILE=pname,defaultfile='.SEA_IN',
	1	TYPE='OLD',ERR=410,FORM='FORMATTED',readonly,shared)
	READ(IN,'(A80,a6,/)',err=410) ATITLE(nfil),temp
	if (temp .ne. 'SEARCH') goto 410
	read (in,*,err=411) IMIN,JCMSMX,mphase,MINMATCH
	read (in,*,err=411) D2T
	read (in,*,err=411) K
	IF (K .gt. M$PKS) THEN 
	  write (lo,'(A,i7,A)') ' Program cannot accept all',K,
	1	' peaks on file'
	  K = M$PKS
	ENDIF
	read (in,*,err=411) (DSPACE(I,nfil),i=1,K)
	read (in,*,err=411) (PKHT(I,nfil)  ,i=1,K)
	INQUIRE(UNIT=IN,NAME=pname)
	close(unit=IN)
	if (K .le. 0) goto 411
	NPKS(nfil) = k
	write (LO,'(I5,A)') NPKS(nfil),' Peaks read from SEARCH file'
	1	//pname(:lench(pname))
	if (WAVE(nfil) .gt. 0) then 
	  WKA1(nfil) = WAVE(nfil)
	  WKA2(nfil) = WAVE(nfil)
	  MONO(nfil) = .true.
	ELSE
	  IRAD(nfil) = 'CU'
	  CALL WAVESET(IRAD(nfil),WAVE(nfil),WKA1(nfil),WKA2(nfil),*133)
133	  MONO(nfil) = .true.
	  WAVE(nfil) = WKA1(nfil)
	  WKA2(nfil) = WAVE(nfil)
	ENDIF
	goto 2000
C----------------------------------------------------------------------
C attempt to read in file in "free" format -- must have extension .PEAK
C----------------------------------------------------------------------
411	close(IN)
410	INQUIRE(defaultFILE=pname,EXIST=exist,file='.peak')
	IF (.not. exist) goto 910
	OPEN(UNIT=IN,defaultFILE=pname,file='.peak',TYPE='OLD',
	1	ERR=910,FORM='FORMATTED',readonly,shared)
	READ(IN,'(A)',err=910) ATITLE(nfil)
	READ(IN,'(f10.0)',err=910) WAVE(nfil)
C	
	if (WAVE(nfil) .gt. 0) then 
	  do j=1,m$pks
	    READ (IN,'(2f10.0)',end=99,err=910) TT(J,nfil),PKHT(J,nfil)
	    flag = TTTOD(DSPACE(J,nfil),TT(J,nfil),wave(nfil))
	    NPKS(nfil) = j
	  ENDDO
	ELSE
	  IRAD(nfil) = 'CU'
	  CALL WAVESET(IRAD(nfil),WAVE(nfil),WKA1(nfil),WKA2(nfil),*33)
33	  MONO(nfil) = .true.
	  WAVE(nfil) = WKA1(nfil)
	  do j=1,m$pks
	    READ (IN,'(2f10.0)',end=99,err=910) DSPACE(J,nfil),PKHT(J,nfil)
	    flag = dTOtt(DSPACE(J,nfil),TT(J,nfil),wave(nfil))
	    NPKS(nfil) = j
	  ENDDO
	ENDIF
99	write (LO,'(I5,A)') NPKS(nfil),' Peaks read from free-format file'
	INQUIRE(UNIT=IN,NAME=pname)
	WRITE (LO,'(A)') ' File: '//pname(:lench(pname))
	close(unit=IN)
	WKA1(nfil) = WAVE(nfil)
	WKA2(nfil) = WAVE(nfil)
C----------------------------------------------------------------------
C----------------------------------------------------------------------
C scale maximum relative intensity to 100.0
2000	if (NPKS(nfil) .le. 0) goto 910
	IF (FYTYP(nfil) .eq. 0) FYTYP(nfil) = 2
	IF (FXTYP(nfil) .eq. 0) FXTYP(nfil) = XTYP_TT
	ymax = PKHT(1,nfil)
	do j=2,NPKS(nfil)
	  ymax = max(ymax,PKHT(J,nfil))
	enddo
	do j=1,NPKS(nfil)
	  RELINT(J,nfil) = 100. 
	  IF (ymax .gt. 0) RELINT(J,nfil) = 100. * PKHT(J,nfil) / ymax
	enddo
	filform = thisform
	STATUS = .TRUE.
	return 
C-----------------------------------------------------------------------
911	write (LO,'(A)') ' READPEAK ERROR -- error reading file '//
	1	pname(:lench(pname))
910	close(unit=IN)
	STATUS = .FALSE.
	return
	END
