I have a large number of galaxies. I need to sort these galaxies into a spheres of radius N, calculate the average numbers of galaxies in each sphere, and plot a graph of this against radius N.
The galaxies are stored in a .fits file as radial coordinates (right ascension, declination and redshift). I'm using pyFITS and astropy to convert the galaxies coordinates into cartesian coordinates with Earth at (0,0,0) and then store the coordinates in a numpy array with the structure: ((x,y,z),(x1,y1,z1),etc.)
In order to seperate the galaxies into spheres of radius N, I am randomly selecting a galaxy from the array, then iterating through the array calcuating the distance between the randomly selected galaxy and the current galaxy. If the distance is less than or equal to the radius, it is added to the sphere. This is repeated as many times as the number of bubbles that need to be calculated.
My current method for this is really slow. I'm unfamiliar with numpy (I've been figuring things out as I'm going along), and I can't really see a better method than just iterating through all the galaxies.
Is there a way to do this any faster (something to do with numpy arrays - I'm converting them to a normal python list right now)? This is what I'm doing right now (https://github.com/humz2k/EngineeringProjectBethe/blob/humza/bubbles.py).