For some reasons I need to use astropy to convert comoving distance to redshift. Basically this involves reading in, looping through and writing out a list or a numpy array... My problem is that each of my lists typically consists of ~ 9.5 x 10^6 elements. And this gives me the MemoryError every time I try to save the output into a new txt file using numpy.savetxt. The memory usage quickly grows, eventually slows down a bit, but always gets higher the 128Gb limit I have.
If anybody has any idea how I can improve the script below, I am very willing to listen. Thank you!
import os
import sys
import glob
import math
import numpy
import astropy
import astropy.units as unit
from astropy.cosmology import *
cosmo = FlatLambdaCDM(H0=70, Om0=0.3)
inFile=sys.argv[1]
outFile=sys.argv[2]
comovingDistance = numpy.loadtxt(inFile, usecols=(2,))
Redshift = numpy.zeros(len(comovingDistance))
for i in range(len(comovingDistance)):
Redshift[i] = z_at_value(cosmo.comoving_distance, comovingDistance[i] * unit.kpc)
output = open(outFile,'w')
numpy.savetxt(output, Redshift, fmt='%1.8e')
output.close()
Below is the error log file:
Traceback (most recent call last):
File "comoving2redshift.py", line 21, in <module>
Redshift[i] = z_at_value(cosmo.comoving_distance, comovingDistance[i] * unit.kpc)
File "/afs/mpa/home/minh/.local/lib/python2.7/site-packages/astropy/cosmology/funcs.py", line 119, in z_at_value
fval_zmax = func(zmax)
File "/afs/mpa/home/minh/.local/lib/python2.7/site-packages/astropy/cosmology/core.py", line 1195, in comoving_distance
return self._comoving_distance_z1z2(0, z)
File "/afs/mpa/home/minh/.local/lib/python2.7/site-packages/astropy/cosmology/core.py", line 1219, in _comoving_distance_z1z2
return self._hubble_distance * vectorize_if_needed(f, z1, z2)
File "/afs/mpa/home/minh/.local/lib/python2.7/site-packages/astropy/units/quantity.py", line 924, in __mul__
return super(Quantity, self).__mul__(other)
File "/afs/mpa/home/minh/.local/lib/python2.7/site-packages/astropy/units/quantity.py", line 368, in __array_prepare__
from .quantity_helper import UNSUPPORTED_UFUNCS, UFUNC_HELPERS
MemoryError
MemoryError?z_at_valueseems relevant to your case, you may want to try that approach. Also, have you tried to see ifz_at_valueis vectorized? It is very likely that you don't need to iterate your self and can simply doRedshift = z_at_value(cosmo.comoving_distance, comovingDistance * unit.kpc)without needing to allocate theRedshiftarray either.