      SUBROUTINE HCOUNT                                                 HCOU001
C-----------------------------------------------------------------------HCOU002
C                                                                       HCOU003
C  SUBROUTINE HCOUNT  -  Count up the numbers of lengths/angles in each HCOU004
C                        histogram bar                                  HCOU005
C                                                                       HCOU006
C----------------------------------------------------------------------+HCOU007
                                                                        HCOU008
      INCLUDE 'bplot.inc'                                               HCOU009
                                                                        HCOU010
      CHARACTER*1   INCHN                                               HCOU011
      CHARACTER*80  IREC                                                HCOU012
      INTEGER       IBAR, IMAIN, IPLANE, LINE                           HCOU013
      REAL          VALUE, WIDTH                                        HCOU014
                                                                        HCOU015
C---- Initialise variables                                              HCOU016
      REWIND(2)                                                         HCOU017
      LINE = 0                                                          HCOU018
      DO 50, IMAIN = 1, NALP                                            HCOU019
          MAXFRQ(IMAIN) = 0                                             HCOU020
          DO 30, IBAR = 1, MXBARS + 2                                   HCOU021
              BCOUNT(IBAR,IMAIN) = 0                                    HCOU022
 30       CONTINUE                                                      HCOU023
 50   CONTINUE                                                          HCOU024
                                                                        HCOU025
C---- Read through the header records to skip                           HCOU026
      DO 100, IMAIN = 1, NALP                                           HCOU027
          READ(2,*,END=900)                                             HCOU028
          LINE = LINE + 1                                               HCOU029
 100  CONTINUE                                                          HCOU030
                                                                        HCOU031
C---- Loop through the values in the .lan file                          HCOU032
 200  CONTINUE                                                          HCOU033
                                                                        HCOU034
C----     Read in the next record                                       HCOU035
          READ(2,220,END=500,ERR=904) IREC                              HCOU036
 220      FORMAT(A)                                                     HCOU037
                                                                        HCOU038
C----     Read in the data from the record                              HCOU039
          READ(IREC,240,ERR=906) INCHN, IMAIN, VALUE                    HCOU040
 240      FORMAT(6X,A1,8X,I2,F9.4)                                      HCOU041
          LINE = LINE + 1                                               HCOU042
                                                                        HCOU043
C----     Only process this residue if it belongs to the required chain HCOU044
          IF (CHAIN.EQ.' ' .OR. INCHN.EQ.CHAIN) THEN                    HCOU045
                                                                        HCOU046
C----         Check whether this is a planar group                      HCOU047
              IF (IREC(29:33).EQ.'PLANE') THEN                          HCOU048
                  IPLANE = IMAIN                                        HCOU049
                                                                        HCOU050
C----             Check that this is a valid planaer group              HCOU051
                  IF (IPLANE.GT.0 .AND. IPLANE.LE.NPLANE) THEN          HCOU052
                      IMAIN = NMAIN + IPLANE                            HCOU053
                  ELSE                                                  HCOU054
                      IMAIN = 0                                         HCOU055
                  ENDIF                                                 HCOU056
              ENDIF                                                     HCOU057
                                                                        HCOU058
C----         Check that the length/angle/plane type is valid           HCOU059
              IF (IMAIN.GT.0 .AND. IMAIN.LE.NALP) THEN                  HCOU060
                                                                        HCOU061
C----             Check whether this is a bond length, angle, or planar HCOU062
C                 group                                                 HCOU063
                  IF (AL(IMAIN).EQ.'L') THEN                            HCOU064
                      WIDTH = LWIDTH                                    HCOU065
                  ELSE IF (AL(IMAIN).EQ.'A') THEN                       HCOU066
                      WIDTH = AWIDTH                                    HCOU067
                  ELSE                                                  HCOU068
                      WIDTH = PWIDTH                                    HCOU069
                  ENDIF                                                 HCOU070
                                                                        HCOU071
C----             Determine in which histogram bar this value falls     HCOU072
                  IBAR = 1 + (VALUE - MCHMIN(IMAIN)) / WIDTH            HCOU073
                  IF (IBAR.LT.1) THEN                                   HCOU074
                      IBAR = MXBARS + 1                                 HCOU075
                  ELSE IF (IBAR.GT.NBARS(IMAIN)) THEN                   HCOU076
                      IBAR = MXBARS + 2                                 HCOU077
                  ENDIF                                                 HCOU078
                                                                        HCOU079
C----             Increment count of values in this bar and update      HCOU080
C                 maximum frequency for this graph                      HCOU081
                  BCOUNT(IBAR,IMAIN) = BCOUNT(IBAR,IMAIN) + 1           HCOU082
                  MAXFRQ(IMAIN) = MAX(BCOUNT(IBAR,IMAIN),               HCOU083
     &                MAXFRQ(IMAIN))                                    HCOU084
              ENDIF                                                     HCOU085
          ENDIF                                                         HCOU086
                                                                        HCOU087
C---- Loop back for next record in file                                 HCOU088
      GO TO 200                                                         HCOU089
                                                                        HCOU090
C---- End of file reached                                               HCOU091
 500  CONTINUE                                                          HCOU092
                                                                        HCOU093
      GO TO 999                                                         HCOU094
                                                                        HCOU095
C---- Fatal errors                                                      HCOU096
 900  CONTINUE                                                          HCOU097
      PRINT*, '*** ERROR. Premature EOF in .lan file at line:',         HCOU098
     &    LINE + 1                                                      HCOU099
      GO TO 990                                                         HCOU100
                                                                        HCOU101
 904  CONTINUE                                                          HCOU102
      PRINT*, '*** ERROR. Error reading .lan file at line:', LINE + 1   HCOU103
      GO TO 990                                                         HCOU104
                                                                        HCOU105
 906  CONTINUE                                                          HCOU106
      PRINT*, '*** ERROR. Data error in .lan file at line:', LINE + 1   HCOU107
      PRINT*, IREC                                                      HCOU108
      GO TO 990                                                         HCOU109
                                                                        HCOU110
990   CONTINUE                                                          HCOU111
      IFAIL = .TRUE.                                                    HCOU112
                                                                        HCOU113
999   CONTINUE                                                          HCOU114
      RETURN                                                            HCOU115
      END                                                               HCOU116
