示例#1
0
def test_galactocentric_references(reset_galactocentric_defaults):
    # references in the "scientific paper"-sense

    from astropy.coordinates import (Galactocentric,
                                     galactocentric_frame_defaults)

    with galactocentric_frame_defaults.set('pre-v4.0'):
        galcen_pre40 = Galactocentric()

        for k in galcen_pre40.get_frame_attr_names():
            if k == 'roll':  # no reference for this parameter
                continue

            assert k in galcen_pre40.frame_attribute_references

    with galactocentric_frame_defaults.set('v4.0'):
        galcen_40 = Galactocentric()

        for k in galcen_40.get_frame_attr_names():
            if k == 'roll':  # no reference for this parameter
                continue

            assert k in galcen_40.frame_attribute_references

    with galactocentric_frame_defaults.set('v4.0'):
        galcen_custom = Galactocentric(z_sun=15*u.pc)

        for k in galcen_custom.get_frame_attr_names():
            if k == 'roll':  # no reference for this parameter
                continue

            if k == 'z_sun':
                assert k not in galcen_custom.frame_attribute_references
            else:
                assert k in galcen_custom.frame_attribute_references
示例#2
0
def test_galactocentric():
    # when z_sun=0, transformation should be very similar to Galactic
    icrs_coord = ICRS(ra=np.linspace(0, 360, 10) * u.deg,
                      dec=np.linspace(-90, 90, 10) * u.deg,
                      distance=1. * u.kpc)

    g_xyz = icrs_coord.transform_to(Galactic).cartesian.xyz
    with galactocentric_frame_defaults.set('pre-v4.0'):
        gc_xyz = icrs_coord.transform_to(Galactocentric(z_sun=0 *
                                                        u.kpc)).cartesian.xyz
    diff = np.abs(g_xyz - gc_xyz)

    assert allclose(diff[0], 8.3 * u.kpc, atol=1E-5 * u.kpc)
    assert allclose(diff[1:], 0 * u.kpc, atol=1E-5 * u.kpc)

    # generate some test coordinates
    g = Galactic(l=[0, 0, 45, 315] * u.deg,
                 b=[-45, 45, 0, 0] * u.deg,
                 distance=[np.sqrt(2)] * 4 * u.kpc)
    with galactocentric_frame_defaults.set('pre-v4.0'):
        xyz = g.transform_to(
            Galactocentric(galcen_distance=1. * u.kpc,
                           z_sun=0. * u.pc)).cartesian.xyz
    true_xyz = np.array([[0, 0, -1.], [0, 0, 1], [0, 1, 0], [0, -1, 0]
                         ]).T * u.kpc
    assert allclose(xyz.to(u.kpc), true_xyz.to(u.kpc), atol=1E-5 * u.kpc)

    # check that ND arrays work

    # from Galactocentric to Galactic
    x = np.linspace(-10., 10., 100) * u.kpc
    y = np.linspace(-10., 10., 100) * u.kpc
    z = np.zeros_like(x)

    # from Galactic to Galactocentric
    l = np.linspace(15, 30., 100) * u.deg
    b = np.linspace(-10., 10., 100) * u.deg
    d = np.ones_like(l.value) * u.kpc

    with galactocentric_frame_defaults.set('latest'):
        g1 = Galactocentric(x=x, y=y, z=z)
        g2 = Galactocentric(x=x.reshape(100, 1, 1),
                            y=y.reshape(100, 1, 1),
                            z=z.reshape(100, 1, 1))

        g1t = g1.transform_to(Galactic)
        g2t = g2.transform_to(Galactic)

        assert_allclose(g1t.cartesian.xyz, g2t.cartesian.xyz[:, :, 0, 0])

        g1 = Galactic(l=l, b=b, distance=d)
        g2 = Galactic(l=l.reshape(100, 1, 1),
                      b=b.reshape(100, 1, 1),
                      distance=d.reshape(100, 1, 1))

        g1t = g1.transform_to(Galactocentric)
        g2t = g2.transform_to(Galactocentric)

        np.testing.assert_almost_equal(g1t.cartesian.xyz.value,
                                       g2t.cartesian.xyz.value[:, :, 0, 0])
示例#3
0
def test_galactocentric_defaults(reset_galactocentric_defaults):
    from astropy.coordinates import (Galactocentric,
                                     galactocentric_frame_defaults,
                                     BaseCoordinateFrame,
                                     CartesianDifferential)

    with galactocentric_frame_defaults.set('pre-v4.0'):
        galcen_pre40 = Galactocentric()

    with galactocentric_frame_defaults.set('v4.0'):
        galcen_40 = Galactocentric()

    with galactocentric_frame_defaults.set('latest'):
        galcen_latest = Galactocentric()

    # parameters that changed
    assert not u.allclose(galcen_pre40.galcen_distance,
                          galcen_40.galcen_distance)
    assert not u.allclose(galcen_pre40.z_sun, galcen_40.z_sun)

    for k in galcen_40.get_frame_attr_names():
        if isinstance(getattr(galcen_40, k), BaseCoordinateFrame):
            continue  # skip coordinate comparison...

        elif isinstance(getattr(galcen_40, k), CartesianDifferential):
            assert u.allclose(getattr(galcen_40, k).d_xyz,
                              getattr(galcen_latest, k).d_xyz)
        else:
            assert getattr(galcen_40, k) == getattr(galcen_latest, k)
def test_api():
    # transform observed Barycentric velocities to full-space Galactocentric
    with galactocentric_frame_defaults.set('latest'):
        gc_frame = Galactocentric()
        icrs = ICRS(ra=151. * u.deg,
                    dec=-16 * u.deg,
                    distance=101 * u.pc,
                    pm_ra_cosdec=21 * u.mas / u.yr,
                    pm_dec=-71 * u.mas / u.yr,
                    radial_velocity=71 * u.km / u.s)
        icrs.transform_to(gc_frame)

        # transform a set of ICRS proper motions to Galactic
        icrs = ICRS(ra=151. * u.deg,
                    dec=-16 * u.deg,
                    pm_ra_cosdec=21 * u.mas / u.yr,
                    pm_dec=-71 * u.mas / u.yr)
        icrs.transform_to(Galactic)

        # transform a Barycentric RV to a GSR RV
        icrs = ICRS(ra=151. * u.deg,
                    dec=-16 * u.deg,
                    distance=1. * u.pc,
                    pm_ra_cosdec=0 * u.mas / u.yr,
                    pm_dec=0 * u.mas / u.yr,
                    radial_velocity=71 * u.km / u.s)
        icrs.transform_to(Galactocentric)
示例#5
0
 def backwards(self, l_deg, b_deg, d_kpc):
     with galactocentric_frame_defaults.set('pre-v4.0'):
         gal = Galactic(l=l_deg * u.deg,
                        b=b_deg * u.deg,
                        distance=d_kpc * u.kpc).transform_to(Galactocentric)
     return gal.x.to(u.kpc).value, gal.y.to(u.kpc).value, gal.z.to(
         u.kpc).value
示例#6
0
    def test_plot_coord_3d_transform(self):
        wcs = WCS(self.msx_header)

        with galactocentric_frame_defaults.set('latest'):
            coord = SkyCoord(0 * u.kpc, 0 * u.kpc, 0 * u.kpc, frame='galactocentric')

        fig = plt.figure()
        ax = fig.add_subplot(1, 1, 1, projection=wcs)
        point, = ax.plot_coord(coord, 'ro')

        np.testing.assert_allclose(point.get_xydata()[0], [0, 0], atol=1e-4)
def test_frame_affinetransform(kwargs, expect_success):
    """There are already tests in test_transformations.py that check that
    an AffineTransform fails without full-space data, but this just checks that
    things work as expected at the frame level as well.
    """
    with galactocentric_frame_defaults.set('latest'):
        icrs = ICRS(**kwargs)

        if expect_success:
            _ = icrs.transform_to(Galactocentric)

        else:
            with pytest.raises(ConvertError):
                icrs.transform_to(Galactocentric)
示例#8
0
def test_galactocentric_default_warning(reset_galactocentric_defaults):
    from astropy.coordinates import (Galactocentric,
                                     galactocentric_frame_defaults)

    # Make sure a warning is thrown if the frame is created with no args
    with pytest.warns(AstropyDeprecationWarning,
                      match=r"In v4\.1 and later versions"):
        Galactocentric()

    # Throw a warning even if only a subset of args are specified
    with pytest.warns(AstropyDeprecationWarning,
                      match=r"In v4\.1 and later versions"):
        Galactocentric(galcen_distance=8.2*u.kpc)

    # No warning if using the latest parameter set:
    with galactocentric_frame_defaults.set('latest'):
        Galactocentric()
示例#9
0
def test_galactocentric_default_warning(reset_galactocentric_defaults):
    from astropy.coordinates import (Galactocentric,
                                     galactocentric_frame_defaults)

    # Note: this is necessary because the doctests may alter the state of the
    # class globally and simultaneously when the tests are run in parallel
    # reset_galactocentric_defaults()

    # Make sure a warning is thrown if the frame is created with no args
    with pytest.warns(AstropyDeprecationWarning,
                      match=r"In v4\.1 and later versions"):
        Galactocentric()

    # Throw a warning even if only a subset of args are specified
    with pytest.warns(AstropyDeprecationWarning,
                      match=r"In v4\.1 and later versions"):
        Galactocentric(galcen_distance=8.2 * u.kpc)

    # No warning if using the latest parameter set:
    with galactocentric_frame_defaults.set('latest'):
        Galactocentric()
示例#10
0
def test_xyz_to_lbr():
    x,y,z = pygedm.convert_lbr_to_xyz(0, 0, 0, method='ymw16')
    assert x == 0
    assert y == 8300 * u.pc
    assert z == 6 * u.pc

    x,y,z = pygedm.convert_lbr_to_xyz(0, 0, 0, method='ne2001')
    assert x == 0
    assert y == 8500 * u.pc
    assert z == 0

    # Who even knows where the centre of the Galaxy is? 
    # https://docs.astropy.org/en/stable/api/astropy.coordinates.galactocentric_frame_defaults.html
    from astropy.coordinates import galactocentric_frame_defaults
    _ = galactocentric_frame_defaults.set('v4.0') 
    x,y,z = pygedm.convert_lbr_to_xyz(0, 0, 0, method='astropy')
    assert np.isclose(x.to('pc').value, -1 * Galactocentric().galcen_distance.to('pc').value)
    assert y == 0
    assert np.isclose(z.to('pc').value, Galactocentric().z_sun.to('pc').value)

    x,y,z = pygedm.convert_lbr_to_xyz(Angle(0, unit='degree'), Angle(0, unit='rad'), 0 * u.pc, method='ymw16')
    assert x == 0
    assert y == 8300 * u.pc
    assert z == 6 * u.pc
示例#11
0
 def forwards(self, x_kpc, y_kpc, z_kpc):
     with galactocentric_frame_defaults.set('pre-v4.0'):
         gal = Galactocentric(x=x_kpc * u.kpc,
                              y=y_kpc * u.kpc,
                              z=z_kpc * u.kpc).transform_to(Galactic)
     return gal.l.degree, gal.b.degree, gal.distance.to(u.kpc).value