      SUBROUTINE MATCH2(MIN,AERR,ERR,STD2T,P2T,M2T,DEL,N,NSTD,NM,XL,LIST
     1,TTY,D2)
C
C  SUBROUTINE TO MATCH INTERNAL STD WITH OBSERVED PEAKS
C    WRITTEN BY RAY GOEHNER  9/83
C
      COMMON /DPRC/ IDPRC,IBKGR,ISCH,INRM,ISTD,NDER,MF,MB,IK2,ISM,IPF,
     1NTP,SENS,INSTD,EXSTD,E2T
C
      BYTE IBKGR,IK2,ISCH,INRM,IDPRC,ISTD,NDER,MF,MB,ISM,INSTD(30)
     1,EXSTD(30)
      INTEGER TTY
      DIMENSION STD2T(1),P2T(1)
      REAL*8 M2T(1),DEL(1)
      BYTE INP(8),DUM(8),B9,D2(12)
C
      M=0
      B9='9'
      S=0
      DO 401 I=1,8
  401 INP(I)=INSTD(I)
      IF(INSTD(1).GT.B9) GO TO 500
      NSTD=0
      CALL PDFNUM(INP,IFILE,ICARD,J)
      IF(J.EQ.0) GO TO 10
      CALL GETSTD(IFILE,ICARD,STD2T,NSTD,XL,TTY,1,D2)
      IF(NSTD.EQ.0) GO TO 10
      GO TO 402
  500 DO 501 I=1,30
  501 IF(INSTD(I).EQ.'.') J=I
      INSTD(J+4)=0
      OPEN(UNIT=1,NAME=INSTD,TYPE='OLD',ERR=9001,READONLY)
  510 I=1
      READ(1,2005,END=9001) DUM  ! moved label 300 from here 
 2005 FORMAT(8A1)
  300 READ(1,2000,END=350,ERR=9001) STD2T(I) ! to here 5/85 -- mfg
 2000 FORMAT(F10.3)  ! FORMAT CHANGED 3/85 RPG
      I=I+1
      GO TO 300
  350 CLOSE(UNIT=1)
      NSTD=I-1
C
  402 DO 100 I=1,N
      DO 100 J=1,NSTD
      DIF=ABS(STD2T(J)-P2T(I))
      IF(DIF.GT.E2T) GO TO 100
C MATCHED
      S=S+DIF
      M=M+1
  100 CONTINUE
      IF(M.LT.MIN) GO TO 900
      AERR=S/FLOAT(M)
      IF(LIST.EQ.1) WRITE(TTY,1120)
 1120 FORMAT(3X,'OBS 2THETA',2X,'STD 2THETA',5X,'STD-OBS')
  110 S=0
      NM=0
      ERR=0.0
      DO 200 J=1,M
      DO 200 I=1,N
      DIF=ABS(STD2T(J)-P2T(I))
      IF(DIF.GT.E2T) GO TO 200 ! NOT MATCHED
C MAKE SURE NEXT LINE ISN'T BETTER MATCH
      IF(I.EQ.N)GO TO 190  !  LAST LINE -- NO NEXT TO COMPARE
      DIF2=ABS(STD2T(J)-P2T(I+1))
      IF(DIF2.LT.DIF)GO TO 200 ! LET NEXT LINE BE MATCHED
C  FINAL MATCH
190   S=S+DIF  ! CALCULATE APPROPRIATE VALUES FOR MATCHED LINE
      NM=NM+1
      M2T(NM)=P2T(I)
      DEL(NM)=STD2T(J)-P2T(I)
      ERR=ERR+DEL(NM)
      IF(LIST.EQ.1) WRITE(TTY,1000) P2T(I),STD2T(J),DEL(NM)
 1000 FORMAT(1X,3F12.4)
  200 CONTINUE
      IF(NM.GE.MIN) GO TO 301
      AERR=2.0*AERR
      GO TO 110
  301 AERR=S/FLOAT(NM)
      ERR=ERR/FLOAT(NM)
      IF(LIST.EQ.0) RETURN
      WRITE(TTY,1010) AERR,NM
 1010 FORMAT(/1X,'AVERAGE ERROR=',F8.4,'  NUMBER OF STD LINES MATCHED='
     1,I4)
      WRITE(TTY,1020) ERR
 1020 FORMAT(1X,'AVERAGE DIFFERENCE IN OBS  & STD =',F9.4)
      RETURN
  900 WRITE(TTY,2001) E2T,AERR
 2001 FORMAT(1X,'PROBLEM IN MATCHING INTERNAL STANDARD LINES'
     1,' TO PEAKS FOUND'/' E2T=',F8.4,'  AERR=',F8.4)
      STOP 'MATCH FAILED'
 9001 WRITE(TTY,2003) F
 2003 FORMAT(1X,'FILE ',30A1,' WAS NOT OPENED')
  999 STOP 'WHAT CAN I DO ?'
   10 WRITE(TTY,2004) IFILE,ICARD
 2004 FORMAT(1X,I2,'-',I4,' NO SUCH CARD')
      STOP
      END
      SUBROUTINE GETSTD(IFILE,ICARD,STD2T,NSTD,XL,TTY,LUNPDF,D2)
C
C  GET STANDARD DATA FOR INTERNAL STANDARD CORRECTION FROM PDF FILE
C              WRITTEN BY RAY GOEHNER  9/83
C
      DIMENSION STD2T(100)
      INTEGER TTY
      INTEGER INDEX(2500),INT(100) 
      BYTE FILE(20),D2(12),INDX(22)
      BYTE CNAME(110),IN(10),NF
C
      ENCODE(20,2000,FILE) D2
 2000 FORMAT(12A1,'PDF.    ')
      ENCODE(22,2010,INDX) D2
 2010 FORMAT(12A1,'INDEX.PDF ')
      INDX(22)=0
      NF='T'
      OPEN(UNIT=LUNPDF,NAME=INDX,ACCESS='DIRECT', 
     &RECORDSIZE=1250,SHARED,TYPE='OLD',READONLY,ERR=900) 
      ENCODE(3,9300,FILE(17))IFILE
9300  FORMAT(I3)
      IF(FILE(17).EQ.' ')FILE(17)='0'
      IF(FILE(18).EQ.' ')FILE(18)='0'
      FILE(20)=0
      READ(LUNPDF'IFILE) INDEX 
      CLOSE(UNIT=LUNPDF)
      NREC=INDEX(ICARD) 
      IF(NREC.NE.0) GO TO 160 
22      WRITE(TTY,150) IFILE 
  150   FORMAT(1X,'SORRY, NO SUCH CARD IN FILE ',I2) 
      RETURN
160   OPEN(UNIT=LUNPDF,NAME=FILE,ACCESS='DIRECT', RECORDSIZE=16,SHARED
     %,TYPE='OLD',READONLY,ERR=910)
      IF(NREC.LT.0)NREC=-NREC
      CALL RCARD(NREC,STD2T,INT,NSTD,CNAME,NSYM,LUNPDF,RIC,NF,XL)
      CLOSE(UNIT=LUNPDF)
60    RETURN
  900 WRITE(5,2100) INDX
 2100 FORMAT(1X,'INDEX FILE OPEN ERROR ',22A1)
      STOP
  910 WRITE(5,2110) FILE
 2110 FORMAT(1X,'PDF FILE OPEN ERROR ',20A1)
      STOP
	END
C
      SUBROUTINE PDFNUM(INP,IFILE,ICARD,J)
C
C  GET PDF FILE & CARD NUMBER
C
      BYTE INP(8)
      IFILE=0
      ICARD=0
C
      IF(INP(1).EQ.' ') GO TO 9998
      DO 1100 I=1,8
      IF(INP(I).EQ.','.OR.INP(I).EQ.'-') J=I
 1100 CONTINUE
      IF(J.EQ.0) RETURN
      DO 1400 I=(J+1),8
      IF(INP(I).LT.'0') GO TO 1500
      IF(INP(I).GT.'9') GO TO 1500
 1400 IQ=I
 1500 K=J-1
      DO 1200 I=1,(J-1)
      K=K-1
 1200 IFILE=IFILE+(INP(I)-48)*10**K
      K=IQ-J
      DO 1300 I=(J+1),IQ
      K=K-1
 1300 ICARD=ICARD+(INP(I)-48)*10**K
      IF(IFILE.LE.0) GO TO 9998
      IF(ICARD.LE.0.OR.ICARD.GE.2500) GO TO 9998
      TYPE *,IFILE,ICARD
      RETURN
 9998 J=0
      RETURN
      END
