[Tutor] Request for advice on Python code

Andre' Walker-Loud walksloud at gmail.com
Tue Feb 14 03:38:28 CET 2012


Hi Elaina,

just reading your question (and responses).  There is an issue with what you wrote in the email, which may help.

> for row in dat:
>      gcoords=ICRSCoordinates(dat['ra-drad'],dat['dec-drad'],radians=True).convert(GalacticCoordinates)

This line is identical to the one that broke before, and now you are just looping over it, so it is not surprising to fail in the same way.  If a loop will work, you would need the following instead

"""
for row in dat:
	gcoords = ICRSCoordinates(row['ra-drad'],row['dec-drad'],radians=True).convert(GalacticCoordinates)
"""

notice the "dat" --> "row" inside the loop.  Now if that works, you can get it to work along the following lines

Example: suppose the output (gcoords) is a scalar float, and you would like to store these data in an array.  Then you can do

"""
import numpy as np #just copying the style from a previous reply

gcoords = np.zeros([len(dat)]) #this creates a numpy array of the length of dat, and fills it with all zeros
for i,row in enumerate(dat):
	gcoords[i] = ICRSCoordinates(row['ra-drad'],row['dec-drad'],radians=True).convert(GalacticCoordinates)
"""

If the loop doesn't work, then you need to investigate the astrophysics library you are using, as previously mentioned, and hopefully there is someone who is familiar with it you can ask questions to.  

Actually, even if the loop does work, you want to be more familiar with the astrophysics library.  You certainly would not want to produce results with it (and base some conclusions on them/publish them) without being 100% sure you know what it is doing.


Good luck,

Andre


More information about the Tutor mailing list