      subroutine xyzout (condis,itot,did,elemt,ide,ia,avern,ispa,       XYZO001
     &title,iat,iclust,idim,charge,foc,igr)                             XYZO002
c ----------------------------------------------------------------------XYZO003
c     This program module is part of the author's X-ray Library         XYZO004
c     and may not be sold or reproduced for commercial purposes.        XYZO005
c     Although it has been tested carefully, there is no warranty       XYZO006
c     regarding accuracy or freedom from side-effects.                  XYZO007
c                   (C) 1986-1994 Bernhard Rupp                         XYZO008
c ----------------------------------------------------------------------XYZO009
c     XYZOUT groups did into shells strictly according to distance      XYZO010
c     and atom kind (no subshell business)                              XYZO011
c ----------------------------------------------------------------------XYZO012
      character elemt*2,ide*4,avern*3,elsub*2,title*60                  XYZO013
      dimension elemt(iat),ide(iat),did(iclust,idim),igr(36)            XYZO014
      dimension charge(iat),foc(iat)                                    XYZO015
                                                                        XYZO016
c --- FEFF- open file containing the FEFF data in overwrite mode --     XYZO017
      open (3,file='infeff.xyz',status='unknown')                       XYZO018
      write(3,*) 'File infeff.xyz - Version',avern,'(FEFF5 and CHARGE)' XYZO019
      write(3,*) title                                                  XYZO020
      write (3,'(a,i3,a,12a1)') ' Space group Nr. ',ispa,' : ',         XYZO021
     &                          (igr(i),i=1,12)                         XYZO022
                                                                        XYZO023
c --- first shell, central atom located at 0, 0, 0, distance 0  --      XYZO024
      write(3,*)'SHELL           0 '                                    XYZO025
      write(3,*)'# atoms in shell, x,y,z, dist, element, atomic #'      XYZO026
      write(3,*)'       1'                                              XYZO027
      do 0001 j=1,4                                                     XYZO028
         write(3,*) 0.0                                                 XYZO029
 0001 continue                                                          XYZO030
      call upstrg (elemt(ia),2)                                         XYZO031
      iz=ielect (elemt(ia))                                             XYZO032
c --- make the element symbol mixed case again                          XYZO033
      call makels(elemt(ia))                                            XYZO034
      write(3,'(1x,a2,1x,a4,2f8.4)')elemt(ia),ide(ia),charge(ia),foc(ia)XYZO035
      write(3,*) iz                                                     XYZO036
                                                                        XYZO037
c --- group into shells                                                 XYZO038
      nshell=1                                                          XYZO039
                                                                        XYZO040
c --- did(1,*) contains central atom, so begin at did(2.*) ---          XYZO041
      istart=2                                                          XYZO042
 1000 continue                                                          XYZO043
      ibeg=istart                                                       XYZO044
      do 0002 i=ibeg,itot-1                                             XYZO045
c ---    reset ishell (number of atoms in a specific shell) ---         XYZO046
         ishell=1                                                       XYZO047
c ---    take the first atom and use it as reference atom ---           XYZO048
         refdis=did(i,4)+condis                                         XYZO049
c ---    initialize distance averaging --                               XYZO050
         averag=did(i,4)                                                XYZO051
c ---    check the following atoms for closeness within condis ---      XYZO052
         do 0003 ii=i+1,itot                                            XYZO053
            if ((did(ii,4).le.refdis).and.(ii.lt.itot).                 XYZO054
     &         and.(did(ii,5).eq.did(ii-1,5))) then                     XYZO055
c ---          same shell : increment number of atoms in shell ishell --XYZO056
               ishell=ishell+1                                          XYZO057
               averag=averag+did(ii,4)                                  XYZO058
            else                                                        XYZO059
c ---          finshed this shell (but must increment if very last atom)XYZO060
               if(ii.eq.itot) then                                      XYZO061
                 ishell=ishell+1                                        XYZO062
                 averag=averag+did(ii,4)                                XYZO063
               end if                                                   XYZO064
               averag=averag/ishell                                     XYZO065
c ---          print out shell information --                           XYZO066
               write(3,*) 'SHELL',nshell                                XYZO067
               write(3,*)                                               XYZO068
     &  '# atoms in shell, x,y,z, dist, element, atomic#, charge, focc' XYZO069
               write(3,*) ishell                                        XYZO070
c ---          crank out atom table --                                  XYZO071
               do 0004 iii=ibeg,ibeg+ishell-1                           XYZO072
c ---          subtract central atom coordinate --                      XYZO073
                  do 0005 m=1,3                                         XYZO074
                     write(3,*) did(iii,m+5)-did(1,m+5)                 XYZO075
 0005             continue                                              XYZO076
 0004          continue                                                 XYZO077
c ---          shell distance ---                                       XYZO078
               write(3,*) averag                                        XYZO079
               elsub=elemt(did(ibeg,5))                                 XYZO080
               call upstrg(elsub,2)                                     XYZO081
               iz=ielect(elsub)                                         XYZO082
               call makels(elsub)                                       XYZO083
               write(3,'(1x,a2,1x,a4,2f8.4)') elsub,ide(did(ibeg,5)),   XYZO084
     &         charge(did(ibeg,5)),foc(did(ibeg,5))                     XYZO085
               write(3,*) iz                                            XYZO086
c ---          save position of last atom found (istart for next shell) XYZO087
               istart=ii                                                XYZO088
c ---          increase shell counter for next shell --                 XYZO089
               nshell=nshell+1                                          XYZO090
               goto 1000                                                XYZO091
            end if                                                      XYZO092
 0003    continue                                                       XYZO093
 0002 continue                                                          XYZO094
      write(3,*)  'EOF'                                                 XYZO095
      close(3,status='keep')                                            XYZO096
      return                                                            XYZO097
      end                                                               XYZO098
