      SUBROUTINE GETLAN                                                 GETL001
C-----------------------------------------------------------------------GETL002
C                                                                       GETL003
C  SUBROUTINE GETLAN  -  Read through the bond lengths and angles file  GETL004
C                        to calculate minimu, maxmimum, and mean values GETL005
C                                                                       GETL006
C----------------------------------------------------------------------+GETL007
                                                                        GETL008
      INCLUDE 'bplot.inc'                                               GETL009
                                                                        GETL010
      CHARACTER*1   ALP, INCHN                                          GETL011
      CHARACTER*3   RESNAM                                              GETL012
      CHARACTER*80  IREC                                                GETL013
      INTEGER       IMAIN, IPLANE, IPOS, IRESID, LINE, NGAP             GETL014
      REAL          SEP, VALUE                                          GETL015
                                                                        GETL016
C---- Initialise variables                                              GETL017
      LINE = 0                                                          GETL018
                                                                        GETL019
C---- Open lengths and angles file, <filename>.lan                      GETL020
WINNT - write file name                                                 GETL021
      write(*,*) 'Opening distance and angle file ',fillan              GETL022
      OPEN(UNIT=2, FILE=FILLAN, STATUS='OLD', FORM='FORMATTED',         GETL023
     &    ACCESS='SEQUENTIAL',                                          GETL024
CVAX     -    READONLY,                                                 GETL025
     &    ERR=900)                                                      GETL026
                                                                        GETL027
C---- Read through the header records to pick up the Engh & Huber       GETL028
C     means and st devs for bond lengths and bond angles derived from   GETL029
C     analysis of small molecule data                                   GETL030
      DO 40, IMAIN = 1, NMAIN                                           GETL031
          READ(2,20,END=902,ERR=904) ALP, LENANG(IMAIN),                GETL032
     &        ENCODE(IMAIN), ENDESC(IMAIN), ENGMEA(IMAIN),              GETL033
     &        ENGSTD(IMAIN)                                             GETL034
 20       FORMAT(A1,1X,A8,6X,A13,4X,A16,2F8.3)                          GETL035
          LINE = LINE + 1                                               GETL036
          AL(IMAIN) = ALP                                               GETL037
          IF (AL(IMAIN).NE.'A' .AND. AL(IMAIN).NE.'L') GO TO 903        GETL038
 40   CONTINUE                                                          GETL039
                                                                        GETL040
C---- Read through the header record to read in the planar groups       GETL041
      DO 60, IPLANE = 1, NPLANE                                         GETL042
          IPOS = NMAIN + IPLANE                                         GETL043
          READ(2,50,END=902,ERR=904) ALP, NPATOM(IPLANE), PNAME(IPLANE) GETL044
 50       FORMAT(A1,1X,I4,27X,A3)                                       GETL045
          AL(IPOS) = ALP                                                GETL046
          IF (AL(IPOS).NE.'P') GO TO 905                                GETL047
 60   CONTINUE                                                          GETL048
                                                                        GETL049
C---- Initialise calculated parameters, setting minimum and maximum     GETL050
C     values to Engh & Huber small-molecule data                        GETL051
      DO 80, IMAIN = 1, NMAIN                                           GETL052
          MCHMEA(IMAIN) = 0.0                                           GETL053
          MCHSTD(IMAIN) = 0.0                                           GETL054
          MCHNUM(IMAIN) = 0                                             GETL055
          IF (AL(IMAIN).EQ.'L') THEN                                    GETL056
              NGAP = NGAPL                                              GETL057
              SEP = LSEP                                                GETL058
          ELSE                                                          GETL059
              NGAP = NGAPA                                              GETL060
              SEP = ASEP                                                GETL061
          ENDIF                                                         GETL062
          MCHMIN(IMAIN) = ENGMEA(IMAIN) - NGAP * SEP / 2.0              GETL063
 80   CONTINUE                                                          GETL064
                                                                        GETL065
C---- Initialise values for planar groups                               GETL066
      DO 100, IPLANE = 1, NPLANE                                        GETL067
          IPOS = NMAIN + IPLANE                                         GETL068
          MCHMEA(IPOS) = 0.0                                            GETL069
          MCHSTD(IPOS) = 0.0                                            GETL070
          MCHNUM(IPOS) = 0                                              GETL071
          MCHMAX(IPOS) = 0.05                                           GETL072
          MCHMIN(IPOS) = 0.0                                            GETL073
          NBARS(IPOS) = MCHMAX(IPOS) / PWIDTH                           GETL074
          NTICKS(IPOS) = 6                                              GETL075
 100  CONTINUE                                                          GETL076
                                                                        GETL077
C---- Loop while reading in records                                     GETL078
 200  CONTINUE                                                          GETL079
          READ(2,220,END=500,ERR=904) IREC                              GETL080
 220      FORMAT(A)                                                     GETL081
                                                                        GETL082
C----     Read in the data from the record                              GETL083
          READ(IREC,240,ERR=906) IRESID, INCHN, RESNAM, IMAIN, VALUE    GETL084
 240      FORMAT(I6,A1,5X,A3,I2,F9.4)                                   GETL085
          LINE = LINE + 1                                               GETL086
                                                                        GETL087
C----     Only process this residue if it belongs to the required chain GETL088
          IF (CHAIN.EQ.' ' .OR. INCHN.EQ.CHAIN) THEN                    GETL089
                                                                        GETL090
C----         Check whether this is a planar group record               GETL091
              IF (IREC(29:33).EQ.'PLANE') THEN                          GETL092
                  IPLANE = IMAIN                                        GETL093
                                                                        GETL094
C----             Check that this is a valid planar group               GETL095
                  IF (IPLANE.GT.0 .AND. IPLANE.LE.NPLANE) THEN          GETL096
                                                                        GETL097
C----                 Accumulate mean values                            GETL098
                      IMAIN = NMAIN + IPLANE                            GETL099
                      MCHMEA(IMAIN) = MCHMEA(IMAIN) + VALUE             GETL100
                      MCHNUM(IMAIN) = MCHNUM(IMAIN) + 1                 GETL101
                      TOPLOT(3) = .TRUE.                                GETL102
                                                                        GETL103
C----                 If the value qualifies it for the "distorted"     GETL104
C                     geometry plot, increment count of those           GETL105
                      IF (NPATOM(IPLANE).EQ.4) THEN                     GETL106
                          IF (VALUE.GT.DGLIMP)                          GETL107
     &                        TODO(3) = TODO(3) + 1                     GETL108
                      ELSE                                              GETL109
                          IF (VALUE.GT.DGLIMR)                          GETL110
     &                        TODO(3) = TODO(3) + 1                     GETL111
                      ENDIF                                             GETL112
                  ENDIF                                                 GETL113
                                                                        GETL114
C----         If not a planar group, must be a bond length or angle     GETL115
              ELSE                                                      GETL116
                                                                        GETL117
C----             Check that this is a valid length/angle               GETL118
                  IF (IMAIN.GT.0 .AND. IMAIN.LE.NMAIN) THEN             GETL119
                                                                        GETL120
C----                 Accumulate mean values                            GETL121
                      MCHMEA(IMAIN) = MCHMEA(IMAIN) + VALUE             GETL122
                      MCHNUM(IMAIN) = MCHNUM(IMAIN) + 1                 GETL123
                                                                        GETL124
C----                 If the value qualifies it for the "distorted"     GETL125
C                     geometry plot, increment count of those           GETL126
                      IF (AL(IMAIN).EQ.'L') THEN                        GETL127
                          TOPLOT(1) = .TRUE.                            GETL128
                          IF (ABS(VALUE - ENGMEA(IMAIN)).GT.DGLIML)     GETL129
     &                        TODO(1) = TODO(1) + 1                     GETL130
                      ELSE                                              GETL131
                          TOPLOT(2) = .TRUE.                            GETL132
                          IF (ABS(VALUE - ENGMEA(IMAIN)).GT.DGLIMA)     GETL133
     &                        TODO(2) = TODO(2) + 1                     GETL134
                      ENDIF                                             GETL135
                  ENDIF                                                 GETL136
              ENDIF                                                     GETL137
          ENDIF                                                         GETL138
                                                                        GETL139
C---- Loop back for next record in file                                 GETL140
      GO TO 200                                                         GETL141
                                                                        GETL142
C---- End of file reached                                               GETL143
 500  CONTINUE                                                          GETL144
                                                                        GETL145
C---- Calculate mean values                                             GETL146
      DO 800, IMAIN = 1, NALP                                           GETL147
          IF (MCHNUM(IMAIN).GT.0) THEN                                  GETL148
              MCHMEA(IMAIN) = MCHMEA(IMAIN) / REAL(MCHNUM(IMAIN))       GETL149
          ENDIF                                                         GETL150
 800  CONTINUE                                                          GETL151
      GO TO 999                                                         GETL152
                                                                        GETL153
C---- Fatal errors                                                      GETL154
 900  CONTINUE                                                          GETL155
      PRINT*, '*** ERROR. Unable to open .lan file'                     GETL156
      GO TO 990                                                         GETL157
                                                                        GETL158
 902  CONTINUE                                                          GETL159
      PRINT*, '*** ERROR. Premature EOF in .lan file at line:',         GETL160
     &    LINE + 1                                                      GETL161
      GO TO 990                                                         GETL162
                                                                        GETL163
 903  CONTINUE                                                          GETL164
      PRINT*, '*** ERROR in bond length/angle parameters in .lan ',     GETL165
     &    'file at line:', LINE, '   [', ALP, ']'                       GETL166
      GO TO 990                                                         GETL167
                                                                        GETL168
 904  CONTINUE                                                          GETL169
      PRINT*, '*** ERROR. Error reading .lan file at line:', LINE + 1   GETL170
      GO TO 990                                                         GETL171
                                                                        GETL172
 905  CONTINUE                                                          GETL173
      PRINT*, '*** ERROR in planar group parameters in .lan ',          GETL174
     &    'file at line:', LINE, '   [', ALP, ']'                       GETL175
      GO TO 990                                                         GETL176
                                                                        GETL177
 906  CONTINUE                                                          GETL178
      PRINT*, '*** ERROR. Data error in .lan file at line:', LINE + 1   GETL179
      PRINT*, IREC                                                      GETL180
      GO TO 990                                                         GETL181
                                                                        GETL182
990   CONTINUE                                                          GETL183
      IFAIL = .TRUE.                                                    GETL184
                                                                        GETL185
999   CONTINUE                                                          GETL186
      RETURN                                                            GETL187
      END                                                               GETL188
