Healpy pix2ang: Convert from HEALPix index to RA,Dec or glong,glat
Question: I am new to HEALPix and fairly new to Python as well. I try to use healpy to
convert a HEALPix index to RA,Dec. I get that I have to use pix2ang, but
cannot figure how to convert the output theta,phi into RA,Dec... I tried this:

    
    
    import healpy as hp
    import numpy as np
    theta, phi = hp.pix2ang(256, 632668    ,nest=True) 
    ra= phi*180./np.pi
    dec = 90.-(theta*180./np.pi)
    

but it does not seem to give the correct result.

Hope someone can help!


Answer: First of all the method `pix2ang(nside,indx)` gives you the coordinates of
pixel with number indx. The pixel number is not directly related to a
coordinate, i.e. two consecutive pixel numbers are not necessarily next to
each other.

Second, as written in the manual of Healpix (which is the underlying code for
healpy) (<http://healpix.sourceforge.net/html/csubnode2.htm>) the angle theta
is defined in range [0,pi] and therefore it cannot directly represent
declination [-pi/2,pi/2].

So what I do is I define a transformation and I implement it in two functions,
for instance:

    
    
    def IndexToDeclRa(index):
        theta,phi=hp.pixelfunc.pix2ang(NSIDE,index)
        return -np.degrees(theta-pi/2.),np.degrees(pi*2.-phi)
    
    def DeclRaToIndex(decl,RA):
        return hp.pixelfunc.ang2pix(NSIDE,np.radians(-decl+90.),np.radians(360.-RA))
    

then the map itself will not be in Decl&RA but if you stick to using
`IndexToDeclRa` and `DeclRaToIndex` you'll effecively have what you need.

