      subroutine mkshel (condis,conang,itot,did,elemt,ide,isearc,ia2,   MKSH001
     &ish,ims,nshell,icomp,iat,iclust,idim,lv,disoct,igeom,geom,igeo,   MKSH002
     &itetra,omit,ihyd,iwater)                                          MKSH003
c ----------------------------------------------------------------------MKSH004
c     This program module is part of the author's X-ray Library         MKSH005
c     and may not be sold or reproduced for commercial purposes.        MKSH006
c     Although it has been tested carefully, there is no warranty       MKSH007
c     regarding accuracy or freedom from side-effects.                  MKSH008
c                   (C) 1986-1994 Bernhard Rupp                         MKSH009
c ----------------------------------------------------------------------MKSH010
c     MKSHL groups did into shells, prepares coordination search        MKSH011
c ----------------------------------------------------------------------MKSH012
      parameter (ipr=6)                                                 MKSH013
      character elemt*2,ide*4,star*2,geom*25,lv,icomp*4,comma,elsub*2   MKSH014
      character idsub*4                                                 MKSH015
      real omit                                                         MKSH016
      dimension elemt(iat),ide(iat),did(iclust,idim),geom(igeo),ish(ims)MKSH017
      dimension igeom(ims),itetra(iat),ihyd(20,2),idummy(100)           MKSH018
                                                                        MKSH019
c --- write shell table header                                          MKSH020
      do 0001 i=ipr,ipr+1                                               MKSH021
         call frames (i,1,icomp)                                        MKSH022
         write(i,'(1x,a1,a,a2,1x,a4,18x,a1)') lv,                       MKSH023
     &   ' Listing of coordination shells around central atom ',        MKSH024
     &   elemt(ia2),ide(ia2),lv                                         MKSH025
         call frames (i,2,icomp)                                        MKSH026
         write(i,'(1x,a1,a,a,3x,a1)') lv,                               MKSH027
     &   ' Shell   Ligand(s)     Coord.Nr.  Mean Distance  ',           MKSH028
     &   '   Possible Coordination ',lv                                 MKSH029
         call frames (i,2,icomp)                                        MKSH030
 0001 continue                                                          MKSH031
                                                                        MKSH032
c --- group into shells                                                 MKSH033
      nshell=1                                                          MKSH034
c --- isasu is marker for final print of multiple subshell footnote *) -MKSH035
      isasu=0                                                           MKSH036
                                                                        MKSH037
c --- did(1,*) contains central atom, so begin at did(2.*) ---          MKSH038
      istart=2                                                          MKSH039
 1000 continue                                                          MKSH040
      star='  '                                                         MKSH041
      ibeg=istart                                                       MKSH042
      ihyd(nshell,1)=0                                                  MKSH043
      ihyd(nshell,2)=0                                                  MKSH044
      do 0002 i=ibeg,itot-1                                             MKSH045
c ---    reset ishell (number of atoms in a specific shell) ---         MKSH046
         ishell=1                                                       MKSH047
         isubsh=0                                                       MKSH048
         elsub='  '                                                     MKSH049
         idsub='    '                                                   MKSH050
         comma=' '                                                      MKSH051
c ---    take the first atom and use it as reference atom ---           MKSH052
         refdis=did(i,4)+condis                                         MKSH053
c ---    initialize distance averaging --                               MKSH054
         averag=did(i,4)                                                MKSH055
c ---    check the following atoms for closeness within condis ---      MKSH056
         do 0003 ii=i+1,itot                                            MKSH057
            if ((did(ii,4).le.refdis).and.(ii.lt.itot)) then            MKSH058
c ---          same shell : increment number of atoms in shell ishell --MKSH059
               ishell=ishell+1                                          MKSH060
               averag=averag+did(ii,4)                                  MKSH061
c ---          warning if different atom --                             MKSH062
               if (did(ii,5).ne.did(ii-1,5)) then                       MKSH063
                  isubsh=isubsh+1                                       MKSH064
c ---             print footnote when more than 2 atom kinds --         MKSH065
                  if (isubsh.gt.1) then                                 MKSH066
                     star='*)'                                          MKSH067
                     isasu=1                                            MKSH068
                  else                                                  MKSH069
                     elsub=elemt(did(ii,5))                             MKSH070
                     idsub=ide(did(ii,5))                               MKSH071
                     comma=','                                          MKSH072
                  end if                                                MKSH073
               end if                                                   MKSH074
            else                                                        MKSH075
c ---          finshed this shell (but must increment if very last atom)MKSH076
               if(ii.eq.itot) then                                      MKSH077
                 ishell=ishell+1                                        MKSH078
                 averag=averag+did(ii,4)                                MKSH079
               end if                                                   MKSH080
               averag=averag/ishell                                     MKSH081
c ---          store number of atoms in this shell (ishell) in ish --   MKSH082
               ish(nshell)=ishell                                       MKSH083
c ---          start coordination geometry search if flagged to do so --MKSH084
               if(isearc.eq.1) then                                     MKSH085
                 call  geomet (nshell,did,ipoly,conang,igeo,            MKSH086
     &                   iclust,idim,ish,ims,averag,disoct)             MKSH087
               else                                                     MKSH088
                 ipoly=igeo                                             MKSH089
               end if                                                   MKSH090
c ---          save ipoly for water search --                           MKSH091
               if(nshell.eq.1) itetra(did(1,5))=ipoly                   MKSH092
c ---          save iploy for use in priver ---                         MKSH093
               igeom(nshell)=ipoly                                      MKSH094
c ---          for some reason, igeom forgets its contents (sometimes)--MKSH095
               idummy(nshell)=ipoly                                     MKSH096
c ---          print out shell information ---                          MKSH097
               if (averag.le.omit) then                                 MKSH098
               do 0004 k=ipr,ipr+1                                      MKSH099
                 write(k,'(1x,a1,1x,i3,3x,a2,1x,a4,1x,a,1x,a2,1x,a4,1x, MKSH100
     &           i4,a3,3x,f9.4,8x,a,a1)')lv, nshell,elemt(did(i,5)),    MKSH101
     &           ide(did(i,5)),comma,elsub,idsub,ishell,star,averag,    MKSH102
     &           geom(ipoly),lv                                         MKSH103
 0004          continue                                                 MKSH104
               end if                                                   MKSH105
c ---          check what atoms are around water atom ---               MKSH106
               if(iwater.eq.1) then                                     MKSH107
                 do 0100 ix=ibeg,ibeg+ishell-1                          MKSH108
                    if(elemt(did(ix,5)).eq.'OH') then                   MKSH109
                      ihyd(nshell,1)=ihyd(nshell,1)+1                   MKSH110
                    else                                                MKSH111
                      ihyd(nshell,2)=ihyd(nshell,2)+1                   MKSH112
                    end if                                              MKSH113
 0100            continue                                               MKSH114
               end if                                                   MKSH115
c ---          save position of last atom found (istart for next shell) MKSH116
               istart=ii                                                MKSH117
c ---          increase shell counter for next shell --                 MKSH118
               nshell=nshell+1                                          MKSH119
               if (nshell.gt.ims) then                                  MKSH120
                  goto 1001                                             MKSH121
               else                                                     MKSH122
                  goto 1000                                             MKSH123
               end if                                                   MKSH124
            end if                                                      MKSH125
 0003    continue                                                       MKSH126
 0002 continue                                                          MKSH127
 1001 nshell=nshell-1                                                   MKSH128
      do 0005 k=ipr,ipr+1                                               MKSH129
         if (isasu.gt.0) then                                           MKSH130
            call frames (k,2,icomp)                                     MKSH131
            if (isasu.gt.0) write (k,'(1x,a1,a,a,a1)') lv,              MKSH132
     &      ' *) more than two atom kinds contribute to this shell -',  MKSH133
     &      ' see atom listing file',lv                                 MKSH134
            call frames (k,3,icomp)                                     MKSH135
         else                                                           MKSH136
            call frames (k,3,icomp)                                     MKSH137
         end if                                                         MKSH138
 0005 continue                                                          MKSH139
                                                                        MKSH140
      write (ipr+1,'(/a/)')'  Full listing of ligand coordinates and angMKSH141
     &les follows '                                                     MKSH142
                                                                        MKSH143
c --- no bloody idea why igeom looses its contents ---                  MKSH144
      do 0009 i=1,nshell                                                MKSH145
         igeom(i)=idummy(i)                                             MKSH146
 0009 continue                                                          MKSH147
                                                                        MKSH148
      return                                                            MKSH149
      end                                                               MKSH150
