Exemplo n.º 1
0
def EulerAngles(equinox, targetEquinox, coord=None, rot=None):
    #Return the Euler angles (passive Z(-Y)X) to go from equinox to targetEquinox in degrees
    #Rot is an extra rotation, after precessing
    #If coord is provided, the inverse coordinate transformation is applied first, then the precession, then the direct coordinate transformation, and then the rotation is calculated

    deg=apUnits.degree;

    #Unit vector in current epoch
    x0=array([1,0,0])
    y0=array([0,1,0])
    z0=array([0,0,1])

    #Go to original coordinates
    rCoord=Rotator(coord=coord)
    x=rCoord.I(x0)
    y=rCoord.I(y0)
    z=rCoord.I(z0)
    
    #Change to longitude and latitude
    xLonLat=vec2dir(x,lonlat=True)
    yLonLat=vec2dir(y,lonlat=True)
    zLonLat=vec2dir(z,lonlat=True)

    #Put them in astropy
    xAP=SkyCoord(ra=xLonLat[0]*deg,dec=xLonLat[1]*deg,frame=FK5(equinox=equinox))
    yAP=SkyCoord(ra=yLonLat[0]*deg,dec=yLonLat[1]*deg,frame=FK5(equinox=equinox))
    zAP=SkyCoord(ra=zLonLat[0]*deg,dec=zLonLat[1]*deg,frame=FK5(equinox=equinox))

    #Precess
    xpAP=xAP.transform_to(FK5(equinox=targetEquinox))
    ypAP=yAP.transform_to(FK5(equinox=targetEquinox))
    zpAP=zAP.transform_to(FK5(equinox=targetEquinox))

    #Go back to vectors
    xp=dir2vec(theta=xpAP.ra.degree, phi=xpAP.dec.degree, lonlat=True)
    yp=dir2vec(theta=ypAP.ra.degree, phi=ypAP.dec.degree, lonlat=True)
    zp=dir2vec(theta=zpAP.ra.degree, phi=zpAP.dec.degree, lonlat=True)

    #Go back to target coordinates
    xp=rCoord(xp)
    yp=rCoord(yp)
    zp=rCoord(zp)

    #Perform final rotation from argument rot
    rf = Rotator(rot=rot)
    xp=array(rf(xp))
    yp=array(rf(yp))
    zp=array(rf(zp))
    
    #Get Euler angles
    if fabs(zp[0])==1:
        alpha=0.0
        beta=zp[0]*pi/2
        gamma=arctan2(xp[1],xp[2])
    else:
        alpha=arctan2(yp[0],xp[0])
        beta=arcsin(zp[0])
        gamma=arctan2(zp[1],zp[2])
        
    return (rad2deg(alpha),rad2deg(beta),rad2deg(gamma))
Exemplo n.º 2
0
 def _to_celestial(sky):
     """ Convert the GSM from naitve Galactic to Equatorial
         coordinates.
     """
     rot = Rotator(deg=True, rot=[0, 0], coord=['G', 'C'])
     with _HiddenPrints():
         sky = rot.rotate_map_alms(sky)
     return sky
Exemplo n.º 3
0
    def rotate_to(self, position):
        """ Rotate the sky to the RA Dec `position`

            :param position:
                Equatorial position
            :type position: :class:`~astropy.coordinates.SkyCoord`
        """
        rot = Rotator(deg=True,
                      rot=[position.ra.deg, position.dec.deg],
                      coord=['C', 'C'])
        sky = rot.rotate_map_alms(self.skymap)
        return sky
Exemplo n.º 4
0
def coord_transform(imap, coord=['C','G'], cosmo2iau=False):
    """Transform IQU maps into new coordinates, this includes
    a simple rotation of I map and a more complex rotation of QU
    maps to refer to the new axes.

    """
    from healpy.rotator import Rotator
    assert imap.shape[0] == 3 or len(imap) == 3, "Wrong shape!"
    rotator = Rotator(coord=coord)
    I_rot = rotator.rotate_map_pixel(imap[0])
    Q_rot, U_rot = rotate_QU_frame(imap[1], imap[2], rot=rotator)
    if cosmo2iau: U_rot *= -1
    return np.vstack([I_rot, Q_rot, U_rot])
Exemplo n.º 5
0
def precess(ra,dec,equinox,targetEquinox):
    #Function to check that the above one actually works

    vec=dir2vec(ra,dec,lonlat=True)

    R=Rotator(rot=EulerAngles(equinox,targetEquinox))

    vec=R(vec)

    [oRA,oDec]=vec2dir(vec,lonlat=True)

    while oRA<0:
        oRA = oRA+360
    
    
    return (oRA,oDec)
Exemplo n.º 6
0
    def rotate_map(self, old_coord='G', new_coord='E'):
        """
        Rotate Healpix pixel numbering by applying a coordinate system transformation

        Parameters
        ----------
        :param old_coord: str
            One of 'G' (galactic, default), 'C' (celestial), 'E' (ecliptic)
        :param new_coord: str
            One of 'G' (galactic), 'C' (celestial), 'E' (ecliptic, default)
        """
        self.theta, self.phi = hp.pix2ang(self.nside, np.arange(self.npix))
        r = Rotator(coord=[new_coord, old_coord
                           ])  # Transforms galactic to ecliptic coordinates
        theta_rot, phi_rot = r(self.theta, self.phi)  # Apply the conversion
        rot_pixorder = hp.ang2pix(hp.npix2nside(self.npix), theta_rot, phi_rot)
        self.mapdata = self.mapdata[rot_pixorder]
        self.theta = theta_rot
        self.phi = phi_rot
        return
Exemplo n.º 7
0
class WISEMap(HealpixMap):
    """
    Class for working with WISE full sky maps

    Parameters
    ----------
    :param filename: str
        Name of Healpix file, including full path
    :param band: int
        Any of [1,2,3,4]

    Methods
    -------
    ind2wcs :
        Convert pixel index into theta, phi coordinates corresponding to co-latitude and longitude in radians
    wcs2ind :
        Convert RA, Dec values into pixel indices on the Healpix grid.

    Attributes
    ----------
    band : int
        One of [1,2,3,4]
    mapdata : numpy.array
        1-D array containing data for Healpix pixels
    timedata : numpy.array
        1-D array containing timestamps for each pixel
    nside : int
        Healpix NSIDE parameter
    npix : int
        Number of pixels in Healpix map
    theta : numpy.array
        co-latitude (in radians)
    phi : numpy.array
        longitude (in radians)
    """

    r = Rotator(coord=['C', 'G'])

    def __init__(self, filename, band):
        super().__init__(filename)
        self.band = band
        self.nside = 256
        super().set_resolution(self.nside)
        self.timedata = np.zeros_like(self.mapdata)

    def ind2wcs(self, index):
        theta, phi = hp.pixelfunc.pix2ang(self.nside, index)
        return np.degrees(phi), np.degrees(np.pi / 2. - theta)

    def wcs2ind(self, ra_arr, dec_arr, nest=False):
        """
        Define theta and phi in galactic coords, then rotate to celestial coords to determine the correct healpix pixel
        using ang2pix.
        hp.ang2pix only converts from celestial lon lat coords to pixels.
        :param ra_arr: numpy.array
            Right ascension values
        :param dec_arr: numpy.array
            Declination values
        :param nest: bool
            Use NEST pixel ordering instead of RING (default is RING)
        :return: numpy.array
            Array containing the pixel indices for the given coordinates
        """
        theta = [np.radians(-float(dec) + 90.) for dec in dec_arr]
        phi = [np.radians(float(ra)) for ra in ra_arr]
        theta_c, phi_c = self.r(theta, phi)
        return hp.pixelfunc.ang2pix(self.nside, theta_c, phi_c, nest=nest)
Exemplo n.º 8
0
import numpy as np
import matplotlib.pyplot as plt
import healpy as hp
from healpy.rotator import Rotator
from astropy.io import fits

rotE2G = Rotator(coord=['C', 'G'])

map_E_fname = 'sc8_varmap_E.fits'
map_G_fname = 'sc8_varmap_G.fits'

print("reading {}...".format(map_E_fname))
map_E = hp.read_map(map_E_fname, partial=True)
print("rotating...")
map_G = rotE2G.rotate_map_pixel(map_E)
print("fixing unseen...")
map_G[map_G < 0] = hp.UNSEEN
print("writing {}...".format(map_G_fname))
hp.write_map(map_G_fname, map_G, partial=True)

map_E_fname = 'sc8_expmap_E.fits'
map_G_fname = 'sc8_expmap_G.fits'

print("reading {}...".format(map_E_fname))
map_E = hp.read_map(map_E_fname, partial=True)
print("rotating...")
map_G = rotE2G.rotate_map_pixel(map_E)
print("fixing unseen...")
map_G[map_G < 0] = hp.UNSEEN
print("writing {}...".format(map_G_fname))
hp.write_map(map_G_fname, map_G, partial=True)
Exemplo n.º 9
0
reddening = hp.ud_grade(
    hp.read_map(outdir +
                'ExtinctionMaps/HFI_CompMap_ThermalDustModel_2048_R1.20.fits',
                field=2), footprint_res)

# creating boolean healpy map
(theta, phi) = hp.pix2ang(footprint_res,
                          np.arange(hp.nside2npix(footprint_res)))
footprint = np.zeros(hp.nside2npix(footprint_res), dtype=bool)

# square of 10 deg of size on the equator
footprint[(theta >= np.pi / 2. - size / 2.) & (theta < np.pi / 2. + size / 2.)
          & (phi < size)] = True

# rotation to a convenient place in galactic coordinates
rot = Rotator(rot=[-40.0, -40.0, 0.0])
footprint = rot.rotate_map_pixel(footprint) > 0.5

sky_fraction = footprint.sum() / footprint.size

# plot of the location
hp.mollview(footprint.astype(int) + reddening, max=0.2)
plt.savefig(outdir + 'Footprints/100sqdeg.png')

# writes footprint on fits file
print("## writing footprint on file {}".format(footprint_fname))

fg = fits.Column(name='FOOTPRINT_G', array=footprint, format='L')
tb = fits.BinTableHDU.from_columns([fg])
tb.header.append('RES')
tb.header['RES'] = footprint_res