       parameter(idim=6500000)
       character*400 filename
       dimension rm(idim),x(idim),y(idim),z(idim),vx(idim),vy(idim),vz(idim)
       dimension pot(idim)
       character *200 infile
	
	nstr=1000000

      write(filename,'(''../data/satellite.dat'')')
      write(filename,'(''../data/isolation.dat'')')
      open(1,file=filename)
	do i=1,nstr
	read(1,*)rm(i),x(i),y(i),z(i),vx(i),vy(i),vz(i),pot(i)
	enddo

        n=nstr

        xcm = 0.0
        ycm = 0.0
        zcm = 0.0
	totmass = 0.0
        do i = 1,n
	  xcm = xcm + rm(i)*x(i)
	  ycm = ycm + rm(i)*y(i)
	  zcm = zcm + rm(i)*z(i)
	  totmass = totmass + rm(i)
        enddo
        xcm = xcm/totmass
        ycm = ycm/totmass
        zcm = zcm/totmass
        x0 = xcm
        y0 = ycm
        z0 = zcm


      write(filename,'(''../data/minpota.dat'')')
      open(22,file=filename)
        do k = 1,50
	  rden = 0.0
	  x1 = 0.0
	  y1 = 0.0
	  z1 = 0.0
        do i = 1,n
	     aux1 = (x(i)-x0)**2+(y(i)-y0)**2+(z(i)-z0)**2
	     if(aux1.eq.0)aux1 = 0.5**2
	     aux2 = rm(i)/sqrt(aux1)/aux1
	     x1 = x1 + x(i)*aux2
	     y1 = y1 + y(i)*aux2
	     z1 = z1 + z(i)*aux2
	     rden = rden + aux2
          enddo
	  x1 = x1/rden
	  y1 = y1/rden
	  z1 = z1/rden
	  dist = (x1-x0)**2+(y1-y0)**2+(z0-z1)**2
	  x0 = x1
	  y0 = y1
	  z0 = z1
	  if( sqrt(dist).lt.1.0e-8 ) goto 101
        enddo
101     write(22,1)x1,y1,z1,zred,sqrt(dist)
        write(*,1)x1,y1,z1,zred,sqrt(dist)
 1    format(30e18.8)
      close(22)
      end
