      use grid_module
      use orbitals_module
      use basins_module
      implicit none
      integer i,j
      integer*2, dimension (:), allocatable :: new_code
      integer, dimension (:,:), allocatable :: index_basin,tab
      double precision, dimension (:), allocatable :: volume
      logical update
      character*1 auxname(40),blank,answer,yes
      character file_out*40,dunname*40
      character*40 filein,filegam,fileelf,filebas
      logical rho_and_elf
      common /files/ auxname,filein,filegam,fileelf,filebas,rho_and_elf
      equivalence (auxname(1),dunname)
      data blank /' '/, yes /'y'/
      write(*,*) ' input file '
      read(*,'(a)') fileelf
      call read_sbf
      dunname=fileelf
      i=1
      do while(auxname(i).ne.blank)
         i=i+1
      enddo
      auxname(i-7)='s'
      auxname(i-6)='y'
      auxname(i-5)='n'
      file_out=dunname
      npts=np(1)*np(2)*np(3)
      allocate(new_code(attract_label))
      write(*,*) ' update basin labels? y/n '
      read(*,'(a)') answer
      update=answer.eq.yes
      if(update) then
         write(*,*) ' input new labels '
         read(*,*)(new_code(i),i=1,attract_label)
         do i=1,npts
            j=attract_code(i)
            if(j.gt.0) then
               attract_code(i)=new_code(j)
            endif
         enddo
      else
         do i=1,npts
            j=attract_code(i)
            if(j.gt.0) then
               attract_code(i)=index_basin(j,1)
            else
               attract_code(i)=0
            endif
         enddo
      endif
      open(unit=2,file=file_out,status='unknown',form='unformatted')
      write(2) np
      write(2) xmin,xmax,ymin,ymax,zmin,zmax
      write(2) (attract_code(i),i=1,npts)
      close(unit=2)
      end
