Exemplo n.º 1
0
    def test_06_rotate(self):
        v1 = coord.rand_vec(stat)
        rot_axis = np.hstack(coord.rand_vec(1))
        angle = 0.25
        v2 = coord.rotate(v1, rot_axis, angle)
        angles = coord.angle(v1, v2)
        self.assertTrue((angles > 0).all() & (angles <= angle).all())
        # rotate back
        v3 = coord.rotate(v2, rot_axis, -angle)
        v4 = coord.rotate(v2, rot_axis, 2 * np.pi - angle)
        self.assertTrue(np.allclose(v1, v3))
        self.assertTrue(np.allclose(v3, v4))

        # when rotating around z-axis and vectors have z=0: all angles have to be 0.25
        rot_axis = np.array([0, 0, 1])
        v1 = coord.ang2vec(coord.rand_phi(stat), np.zeros(stat))
        v2 = coord.rotate(v1, rot_axis, angle)
        angles = coord.angle(v1, v2)
        self.assertTrue((angles > angle - 1e-3).all()
                        & (angles < angle + 1e-3).all())

        # when rotating around z-axis all angles correspond to longitude shift
        angles = 2 * np.pi * np.random.random(stat)
        v1 = coord.rand_vec(stat)
        lon1, lat1 = coord.vec2ang(v1)
        v2 = np.array(
            [coord.rotate(vi, rot_axis, ai) for vi, ai in zip(v1.T, angles)]).T
        lon2, lat2 = coord.vec2ang(v2)
        self.assertTrue(np.allclose(lat1, lat2))
        lon_diff = lon1 - lon2
        lon_diff[lon_diff < 0] += 2 * np.pi
        self.assertTrue(np.allclose(lon_diff, angles))
Exemplo n.º 2
0
 def test_08_rotate_one_vec_multi_angles(self):
     v = coord.rand_vec(1)
     rot = coord.rand_vec(1)
     angles = np.random.random(stat)
     v_rot = coord.rotate(v, rot, angles)
     self.assertTrue(np.shape(v_rot)[1] == stat)
     for i in range(stat):
         vi_rot = coord.rotate(v, rot, angles[i])
         self.assertTrue(np.allclose(np.squeeze(vi_rot), v_rot[:, i]))
Exemplo n.º 3
0
 def test_06a_rotate_multi(self):
     v = coord.rand_vec(stat)
     rot = coord.rand_vec(stat)
     angles = np.random.random(stat)
     v_rot = coord.rotate(v, rot, angles)
     self.assertTrue(np.shape(v_rot) == np.shape(v))
     for i in range(stat):
         vi_rot = coord.rotate(v[:, i], rot[:, i], angles[i])
         self.assertTrue(np.allclose(vi_rot, v_rot[:, i]))
Exemplo n.º 4
0
def plot_tissot(vec_c, r, res=1e-2, **kwargs):
    """
    Plot a circle onto skymap.

    :param vec_c: vector pointing to the center of the circle
    :param r: radius of the circle
    :param res: resolution of the circle approximation (in radian)
    :param kwargs: additional named keyword arguments passed to plt.plot()
    """
    lon, lat = coord.vec2ang(vec_c)
    vec_ref = coord.rotate(vec_c, coord.sph_unit_vectors(lon, lat)[2], r)
    psis = np.arange(0, 2 * np.pi, res)
    lons, lats = coord.vec2ang(coord.rotate(vec_ref, vec_c, psis))
    plt.plot(-lons, lats, **kwargs)
Exemplo n.º 5
0
def rotate_map(healpy_map, rotation_axis, rotation_angle):
    """
    Perform rotation of healpy map for given rotation axis and angle(s).

    :param healpy_map: healpix map to be rotated
    :param rotation_axis: rotation axis, either np.array([x, y, z]) or ndarray with shape (3, n)
    :param rotation_angle: rotation angle in radians, either float or array size n
    :return: rotated healpy map, same shape as input healpy map or shape (n, npix)
    """
    rotation_axis = coord.atleast_kd(rotation_axis, 2)
    nside, npix = hp.get_nside(healpy_map), len(healpy_map)
    n_ang, n_ax = np.size(rotation_angle), np.shape(rotation_axis)[-1]
    assert (n_ang == n_ax) or (np.min([
        n_ang, n_ax
    ]) == 1), "Rotation axes and angles dimensions not compatible."
    n = np.max([n_ang, n_ax])
    rotation_vectors = pix2vec(nside, np.arange(npix))
    if n > 1:
        rotation_vectors = np.tile(rotation_vectors, n)
        rotation_axis = np.repeat(rotation_axis,
                                  npix * ((n_ax == 1) * n + (n_ax > 1)),
                                  axis=1)
        rotation_angle = np.repeat(rotation_angle,
                                   npix * ((n_ang == 1) * n + (n_ang > 1)))
    _vecs = coord.rotate(rotation_vectors, rotation_axis, -rotation_angle)
    _phi, _theta = vec2ang(np.squeeze(_vecs.reshape(3, -1, npix)))
    return hp.get_interp_val(healpy_map, np.pi / 2. - _theta, _phi)
Exemplo n.º 6
0
    def test_08d_rotate_map(self):
        # size(rot_axis) = n and size(rot_angle) = 1
        nside = 64
        npix = hpt.nside2npix(nside)
        for i in range(10):
            ipix = np.random.randint(npix)
            _inmap = np.zeros(npix)
            _inmap[ipix] = 1
            rot_axis = coord.rand_vec(5)
            rot_angle = 4 * np.pi * np.random.random(1) - 2 * np.pi
            _rot_map = hpt.rotate_map(_inmap, rot_axis, rot_angle)

            # compare with well tested coord rotate function
            for j in range(5):
                v_hpt = hpt.pix2vec(nside, np.argmax(_rot_map[j]))
                v_coord = coord.rotate(hpt.pix2vec(nside, ipix),
                                       rot_axis[:, j], rot_angle)
                self.assertTrue(
                    coord.angle(v_hpt, v_coord) < hpt.max_pixrad(nside))
Exemplo n.º 7
0
    def test_08a_rotate_map(self):
        # size(rot_axis) = 1 and size(rot_angle) = 1
        nside = 64
        npix = hpt.nside2npix(nside)
        for i in range(10):
            ipix = np.random.randint(npix)
            _inmap = np.zeros(npix)
            _inmap[ipix] = 1
            rot_axis = np.squeeze(
                coord.rand_vec(1)) if i < 5 else coord.rand_vec(1)
            rot_angle = 4 * np.pi * np.random.random() - 2 * np.pi
            _rot_map = hpt.rotate_map(_inmap, rot_axis, rot_angle)
            v_hpt = hpt.pix2vec(nside, np.argmax(_rot_map))

            # compare with well tested coord rotate function
            v_coord = coord.rotate(hpt.pix2vec(nside, ipix), rot_axis,
                                   rot_angle)
            self.assertTrue(
                coord.angle(v_hpt, v_coord) < hpt.max_pixrad(nside))
Exemplo n.º 8
0
 def test_06b_rotate_multi_shape(self):
     shape = (5, 10)
     v = coord.rand_vec(shape)
     rot = coord.rand_vec(shape)
     angles = np.random.random(shape)
     v_rot = coord.rotate(v, rot, angles)
     self.assertTrue(np.shape(v_rot) == np.shape(v))
     v_rot_flatten = coord.rotate(v.reshape(3, -1), rot.reshape(3, -1),
                                  angles.flatten())
     self.assertTrue(np.allclose(v_rot, v_rot_flatten.reshape(v.shape)))
     v_rot = coord.rotate(v, np.array([1, 0, 0]), angles)
     self.assertTrue(np.shape(v_rot) == np.shape(v))
     v_rot_flatten = coord.rotate(v.reshape(3, -1), np.array([1, 0, 0]),
                                  angles.flatten())
     self.assertTrue(np.allclose(v_rot, v_rot_flatten.reshape(v.shape)))
     v_rot = coord.rotate(v, np.array([1, 0, 0]), np.pi)
     self.assertTrue(np.shape(v_rot) == np.shape(v))
     v_rot_flatten = coord.rotate(v.reshape(3, -1), np.array([1, 0, 0]),
                                  np.pi)
     self.assertTrue(np.allclose(v_rot, v_rot_flatten.reshape(v.shape)))