      SUBROUTINE REAPLN(LINE,RMS,LMAIN,LSTCHN,LSTRES,LSTSEQ,FOUND,      REAP001
     &    COORD,ANAME,NATOM,ENDFIL)                                     REAP002
C-----------------------------------------------------------------------REAP003
C                                                                       REAP004
C  SUBROUTINE REAPLN  -  Read through the .pln file until find the next REAP005
C                        distorted planar group                         REAP006
C                                                                       REAP007
C----------------------------------------------------------------------+REAP008
                                                                        REAP009
      SAVE                                                              REAP010
                                                                        REAP011
      INCLUDE 'bplot.inc'                                               REAP012
                                                                        REAP013
      CHARACTER*1   INCHN, LSTCHN                                       REAP014
      CHARACTER*3   ANAME(NPLATM), ATNAM, RESNAM, LSTRES                REAP015
      CHARACTER*5   LSTSEQ, SEQNO                                       REAP016
      CHARACTER*80  IREC                                                REAP017
CHECK v.3.2-->                                                          REAP018
C      INTEGER       IATOM, IMAIN, IRESID, LINE, LMAIN, LRESID, NATOM   REAP019
      INTEGER       IATOM, IMAIN, LINE, LMAIN, NATOM                    REAP020
CHECK v.3.2<--                                                          REAP021
      LOGICAL       ENDFIL, FOUND                                       REAP022
      REAL          COORD(3,NPLATM), DGLIM, RMS, RMSDIF, X, Y, Z        REAP023
                                                                        REAP024
C---- Initialise variables                                              REAP025
      FOUND = .FALSE.                                                   REAP026
                                                                        REAP027
C---- If first time, then read in first record                          REAP028
      IF (LINE.EQ.0) THEN                                               REAP029
          READ(3,20,END=500,ERR=900) IREC                               REAP030
 20       FORMAT(A)                                                     REAP031
          LINE = LINE + 1                                               REAP032
          IATOM = 0                                                     REAP033
          LSTSEQ = ' '                                                  REAP034
      ENDIF                                                             REAP035
                                                                        REAP036
C---- Loop while reading in records                                     REAP037
 100  CONTINUE                                                          REAP038
                                                                        REAP039
C----     Read in data from the record                                  REAP040
          READ(IREC,140,ERR=902) IMAIN, ATNAM, RESNAM, INCHN, SEQNO,    REAP041
     &        X, Y, Z, RMSDIF                                           REAP042
 140      FORMAT(9X,I2,2X,A3,1X,A3,1X,A1,A5,3X,3F8.3,6X,F8.4)           REAP043
                                                                        REAP044
C----     Check whether this is a new residue                           REAP045
          IF (SEQNO.NE.LSTSEQ .OR. LINE.EQ.1 .OR. ENDFIL) THEN          REAP046
                                                                        REAP047
C----         Check whether have a previous residue stored              REAP048
              IF (IATOM.GT.0) THEN                                      REAP049
                  NATOM = IATOM                                         REAP050
                  IATOM = 0                                             REAP051
                                                                        REAP052
C----             Check whether the previous residue warrants plotting  REAP053
                  IF (LMAIN.GT.0 .AND. LMAIN.LE.NPLANE) THEN            REAP054
                      IF (NPATOM(LMAIN).EQ.4) THEN                      REAP055
                          DGLIM = DGLIMP                                REAP056
                      ELSE                                              REAP057
                          DGLIM = DGLIMR                                REAP058
                      ENDIF                                             REAP059
                      IF (RMS.GT.DGLIM) THEN                            REAP060
                          FOUND = .TRUE.                                REAP061
                          GO TO 999                                     REAP062
                      ENDIF                                             REAP063
                  ENDIF                                                 REAP064
              ENDIF                                                     REAP065
          ENDIF                                                         REAP066
                                                                        REAP067
C----     Store this atom                                               REAP068
          IF (.NOT.ENDFIL) THEN                                         REAP069
              IATOM = IATOM + 1                                         REAP070
              IF (IATOM.LE.NPLATM) THEN                                 REAP071
                  ANAME(IATOM) = ATNAM                                  REAP072
                  COORD(1,IATOM) = X                                    REAP073
                  COORD(2,IATOM) = Y                                    REAP074
                  COORD(3,IATOM) = Z                                    REAP075
                  LMAIN = IMAIN                                         REAP076
CHECK v.3.2-->                                                          REAP077
C                  LRESID = IRESID                                      REAP078
CHECK v.3.2<--                                                          REAP079
                  LSTCHN = INCHN                                        REAP080
                  LSTRES = RESNAM                                       REAP081
                  LSTSEQ = SEQNO                                        REAP082
                  RMS = RMSDIF                                          REAP083
              ENDIF                                                     REAP084
                                                                        REAP085
C----         Read the next record                                      REAP086
              READ(3,20,END=500,ERR=900) IREC                           REAP087
              LINE = LINE + 1                                           REAP088
          ELSE                                                          REAP089
              GO TO 999                                                 REAP090
          ENDIF                                                         REAP091
                                                                        REAP092
C---- Loop back to process this record                                  REAP093
      GO TO 100                                                         REAP094
                                                                        REAP095
C---- End of file reached                                               REAP096
 500  CONTINUE                                                          REAP097
      ENDFIL = .TRUE.                                                   REAP098
      GO TO 100                                                         REAP099
                                                                        REAP100
C---- Fatal errors                                                      REAP101
 900  CONTINUE                                                          REAP102
      PRINT*, '*** ERROR reading record from .pln file at line',        REAP103
     &    LINE + 1                                                      REAP104
      GO TO 990                                                         REAP105
                                                                        REAP106
 902  CONTINUE                                                          REAP107
      PRINT*, '*** ERROR. Data error in .pln file at line:', LINE       REAP108
      GO TO 990                                                         REAP109
                                                                        REAP110
990   CONTINUE                                                          REAP111
      IFAIL = .TRUE.                                                    REAP112
                                                                        REAP113
999   CONTINUE                                                          REAP114
      RETURN                                                            REAP115
      END                                                               REAP116
