def test_from_hcrs(self): # test scalar transform transformed = self.sun_hcrs_t1.transform_to(ICRS()) separation = transformed.separation_3d(self.sun_icrs_scalar) assert_allclose(separation, 0 * u.km, atol=self.tolerance) # test non-scalar positions and times transformed = self.sun_hcrs_tarr.transform_to(ICRS()) separation = transformed.separation_3d(self.sun_icrs_arr) assert_allclose(separation, 0 * u.km, atol=self.tolerance)
def test_3d_separations(): """ Test 3D separation functionality """ c1 = ICRS(ra=1 * u.deg, dec=1 * u.deg, distance=9 * u.kpc) c2 = ICRS(ra=1 * u.deg, dec=1 * u.deg, distance=10 * u.kpc) sep3d = c2.separation_3d(c1) assert isinstance(sep3d, Distance) assert_allclose(sep3d - 1 * u.kpc, 0 * u.kpc, atol=1e-12 * u.kpc)
def test_skyoffset_functional_ra_dec(): # we do the 12)[1:-1] business because sometimes machine precision issues # lead to results that are either ~0 or ~360, which mucks up the final # comparison and leads to spurious failures. So this just avoids that by # staying away from the edges input_ra = np.linspace(0, 360, 12)[1:-1] input_dec = np.linspace(-90, 90, 12)[1:-1] input_ra_rad = np.deg2rad(input_ra) input_dec_rad = np.deg2rad(input_dec) icrs_coord = ICRS(ra=input_ra * u.deg, dec=input_dec * u.deg, distance=1. * u.kpc) for ra in np.linspace(0, 360, 10): for dec in np.linspace(-90, 90, 5): # expected rotation dec_rad = -np.deg2rad(dec) ra_rad = np.deg2rad(ra) expected_x = (-np.sin(input_dec_rad) * np.sin(dec_rad) + np.cos(input_ra_rad) * np.cos(input_dec_rad) * np.cos(dec_rad) * np.cos(ra_rad) + np.sin(input_ra_rad) * np.cos(input_dec_rad) * np.cos(dec_rad) * np.sin(ra_rad)) expected_y = ( np.sin(input_ra_rad) * np.cos(input_dec_rad) * np.cos(ra_rad) - np.cos(input_ra_rad) * np.cos(input_dec_rad) * np.sin(ra_rad)) expected_z = (np.sin(input_dec_rad) * np.cos(dec_rad) + np.sin(dec_rad) * np.cos(ra_rad) * np.cos(input_ra_rad) * np.cos(input_dec_rad) + np.sin(dec_rad) * np.sin(ra_rad) * np.sin(input_ra_rad) * np.cos(input_dec_rad)) expected = SkyCoord(x=expected_x, y=expected_y, z=expected_z, unit='kpc', representation_type='cartesian') expected_xyz = expected.cartesian.xyz # actual transformation to the frame skyoffset_frame = SkyOffsetFrame( origin=ICRS(ra * u.deg, dec * u.deg)) actual = icrs_coord.transform_to(skyoffset_frame) actual_xyz = actual.cartesian.xyz # back to ICRS roundtrip = actual.transform_to(ICRS()) # Verify assert_allclose(actual_xyz, expected_xyz, atol=1E-5 * u.kpc) assert_allclose(icrs_coord.ra, roundtrip.ra, atol=1E-4 * u.deg) assert_allclose(icrs_coord.dec, roundtrip.dec, atol=1E-5 * u.deg) assert_allclose(icrs_coord.distance, roundtrip.distance, atol=1E-5 * u.kpc)
def test_faux_lsr(dt, symmetric): class LSR2(LSR): obstime = TimeAttribute(default=J2000) @frame_transform_graph.transform(FunctionTransformWithFiniteDifference, ICRS, LSR2, finite_difference_dt=dt, symmetric_finite_difference=symmetric) def icrs_to_lsr(icrs_coo, lsr_frame): dt = lsr_frame.obstime - J2000 offset = lsr_frame.v_bary * dt.to(u.second) return lsr_frame.realize_frame(icrs_coo.data.without_differentials() + offset) @frame_transform_graph.transform(FunctionTransformWithFiniteDifference, LSR2, ICRS, finite_difference_dt=dt, symmetric_finite_difference=symmetric) def lsr_to_icrs(lsr_coo, icrs_frame): dt = lsr_coo.obstime - J2000 offset = lsr_coo.v_bary * dt.to(u.second) return icrs_frame.realize_frame(lsr_coo.data - offset) ic = ICRS(ra=12.3 * u.deg, dec=45.6 * u.deg, distance=7.8 * u.au, pm_ra_cosdec=0 * u.marcsec / u.yr, pm_dec=0 * u.marcsec / u.yr, radial_velocity=0 * u.km / u.s) lsrc = ic.transform_to(LSR2()) assert quantity_allclose(ic.cartesian.xyz, lsrc.cartesian.xyz) idiff = ic.cartesian.differentials['s'] ldiff = lsrc.cartesian.differentials['s'] change = (ldiff.d_xyz - idiff.d_xyz).to(u.km / u.s) totchange = np.sum(change**2)**0.5 assert quantity_allclose(totchange, np.sum(lsrc.v_bary.d_xyz**2)**0.5) ic2 = ICRS(ra=120.3 * u.deg, dec=45.6 * u.deg, distance=7.8 * u.au, pm_ra_cosdec=0 * u.marcsec / u.yr, pm_dec=10 * u.marcsec / u.yr, radial_velocity=1000 * u.km / u.s) lsrc2 = ic2.transform_to(LSR2()) ic2_roundtrip = lsrc2.transform_to(ICRS) tot = np.sum(lsrc2.cartesian.differentials['s'].d_xyz**2)**0.5 assert np.abs(tot.to('km/s') - 1000 * u.km / u.s) < 20 * u.km / u.s assert quantity_allclose(ic2.cartesian.xyz, ic2_roundtrip.cartesian.xyz)
def test_shorthand_attributes(): # Check that attribute access works # for array data: n = 4 icrs1 = ICRS(ra=np.random.uniform(0, 360, n) * u.deg, dec=np.random.uniform(-90, 90, n) * u.deg, distance=100 * u.pc, pm_ra_cosdec=np.random.normal(0, 100, n) * u.mas / u.yr, pm_dec=np.random.normal(0, 100, n) * u.mas / u.yr, radial_velocity=np.random.normal(0, 100, n) * u.km / u.s) v = icrs1.velocity pm = icrs1.proper_motion assert quantity_allclose(pm[0], icrs1.pm_ra_cosdec) assert quantity_allclose(pm[1], icrs1.pm_dec) # for scalar data: icrs2 = ICRS(ra=37.4 * u.deg, dec=-55.8 * u.deg, distance=150 * u.pc, pm_ra_cosdec=-21.2 * u.mas / u.yr, pm_dec=17.1 * u.mas / u.yr, radial_velocity=105.7 * u.km / u.s) v = icrs2.velocity pm = icrs2.proper_motion assert quantity_allclose(pm[0], icrs2.pm_ra_cosdec) assert quantity_allclose(pm[1], icrs2.pm_dec) # check that it fails where we expect: # no distance rv = 105.7 * u.km / u.s icrs3 = ICRS(ra=37.4 * u.deg, dec=-55.8 * u.deg, pm_ra_cosdec=-21.2 * u.mas / u.yr, pm_dec=17.1 * u.mas / u.yr, radial_velocity=rv) with pytest.raises(ValueError): icrs3.velocity icrs3.set_representation_cls('cartesian') assert hasattr(icrs3, 'radial_velocity') assert quantity_allclose(icrs3.radial_velocity, rv) icrs4 = ICRS(x=30 * u.pc, y=20 * u.pc, z=11 * u.pc, v_x=10 * u.km / u.s, v_y=10 * u.km / u.s, v_z=10 * u.km / u.s, representation_type=r.CartesianRepresentation, differential_type=r.CartesianDifferential) icrs4.radial_velocity
def test_transform(): """ This test just makes sure the transform architecture works, but does *not* actually test all the builtin transforms themselves are accurate """ from astropy.coordinates.builtin_frames import ICRS, FK4, FK5, Galactic from astropy.time import Time i = ICRS(ra=[1, 2] * u.deg, dec=[3, 4] * u.deg) f = i.transform_to(FK5) i2 = f.transform_to(ICRS) assert i2.data.__class__ == r.UnitSphericalRepresentation assert_allclose(i.ra, i2.ra) assert_allclose(i.dec, i2.dec) i = ICRS(ra=[1, 2] * u.deg, dec=[3, 4] * u.deg, distance=[5, 6] * u.kpc) f = i.transform_to(FK5) i2 = f.transform_to(ICRS) assert i2.data.__class__ != r.UnitSphericalRepresentation f = FK5(ra=1 * u.deg, dec=2 * u.deg, equinox=Time('J2001')) f4 = f.transform_to(FK4) f4_2 = f.transform_to(FK4(equinox=f.equinox)) # make sure attributes are copied over correctly assert f4.equinox == FK4.get_frame_attr_names()['equinox'] assert f4_2.equinox == f.equinox # make sure self-transforms also work i = ICRS(ra=[1, 2] * u.deg, dec=[3, 4] * u.deg) i2 = i.transform_to(ICRS) assert_allclose(i.ra, i2.ra) assert_allclose(i.dec, i2.dec) f = FK5(ra=1 * u.deg, dec=2 * u.deg, equinox=Time('J2001')) f2 = f.transform_to(FK5) # default equinox, so should be *different* assert f2.equinox == FK5().equinox with pytest.raises(AssertionError): assert_allclose(f.ra, f2.ra) with pytest.raises(AssertionError): assert_allclose(f.dec, f2.dec) # finally, check Galactic round-tripping i1 = ICRS(ra=[1, 2] * u.deg, dec=[3, 4] * u.deg) i2 = i1.transform_to(Galactic).transform_to(ICRS) assert_allclose(i1.ra, i2.ra) assert_allclose(i1.dec, i2.dec)
def test_differential_type_arg(): """ Test passing in an explicit differential class to the initializer or changing the differential class via set_representation_cls """ from astropy.coordinates.builtin_frames import ICRS icrs = ICRS(ra=1 * u.deg, dec=60 * u.deg, pm_ra=10 * u.mas / u.yr, pm_dec=-11 * u.mas / u.yr, differential_type=r.UnitSphericalDifferential) assert icrs.pm_ra == 10 * u.mas / u.yr icrs = ICRS(ra=1 * u.deg, dec=60 * u.deg, pm_ra=10 * u.mas / u.yr, pm_dec=-11 * u.mas / u.yr, differential_type={'s': r.UnitSphericalDifferential}) assert icrs.pm_ra == 10 * u.mas / u.yr icrs = ICRS(ra=1 * u.deg, dec=60 * u.deg, pm_ra_cosdec=10 * u.mas / u.yr, pm_dec=-11 * u.mas / u.yr) icrs.set_representation_cls(s=r.UnitSphericalDifferential) assert quantity_allclose(icrs.pm_ra, 20 * u.mas / u.yr) # incompatible representation and differential with pytest.raises(TypeError): ICRS(ra=1 * u.deg, dec=60 * u.deg, v_x=1 * u.km / u.s, v_y=-2 * u.km / u.s, v_z=-2 * u.km / u.s, differential_type=r.CartesianDifferential) # specify both icrs = ICRS(x=1 * u.pc, y=2 * u.pc, z=3 * u.pc, v_x=1 * u.km / u.s, v_y=2 * u.km / u.s, v_z=3 * u.km / u.s, representation_type=r.CartesianRepresentation, differential_type=r.CartesianDifferential) assert icrs.x == 1 * u.pc assert icrs.y == 2 * u.pc assert icrs.z == 3 * u.pc assert icrs.v_x == 1 * u.km / u.s assert icrs.v_y == 2 * u.km / u.s assert icrs.v_z == 3 * u.km / u.s
def test_converting_units(): import re from astropy.coordinates.baseframe import RepresentationMapping from astropy.coordinates.builtin_frames import ICRS, FK5 # this is a regular expression that with split (see below) removes what's # the decimal point to fix rounding problems rexrepr = re.compile(r'(.*?=\d\.).*?( .*?=\d\.).*?( .*)') # Use values that aren't subject to rounding down to X.9999... i2 = ICRS(ra=2. * u.deg, dec=2. * u.deg) i2_many = ICRS(ra=[2., 4.] * u.deg, dec=[2., -8.1] * u.deg) # converting from FK5 to ICRS and back changes the *internal* representation, # but it should still come out in the preferred form i4 = i2.transform_to(FK5).transform_to(ICRS) i4_many = i2_many.transform_to(FK5).transform_to(ICRS) ri2 = ''.join(rexrepr.split(repr(i2))) ri4 = ''.join(rexrepr.split(repr(i4))) assert ri2 == ri4 assert i2.data.lon.unit != i4.data.lon.unit # Internal repr changed ri2_many = ''.join(rexrepr.split(repr(i2_many))) ri4_many = ''.join(rexrepr.split(repr(i4_many))) assert ri2_many == ri4_many assert i2_many.data.lon.unit != i4_many.data.lon.unit # Internal repr changed # but that *shouldn't* hold if we turn off units for the representation class FakeICRS(ICRS): frame_specific_representation_info = { 'spherical': [ RepresentationMapping('lon', 'ra', u.hourangle), RepresentationMapping('lat', 'dec', None), RepresentationMapping('distance', 'distance') ] # should fall back to default of None unit } fi = FakeICRS(i4.data) ri2 = ''.join(rexrepr.split(repr(i2))) rfi = ''.join(rexrepr.split(repr(fi))) rfi = re.sub('FakeICRS', 'ICRS', rfi) # Force frame name to match assert ri2 != rfi # the attributes should also get the right units assert i2.dec.unit == i4.dec.unit # unless no/explicitly given units assert i2.dec.unit != fi.dec.unit assert i2.ra.unit != fi.ra.unit assert fi.ra.unit == u.hourangle
def test_sep(): from astropy.coordinates.builtin_frames import ICRS i1 = ICRS(ra=0*u.deg, dec=1*u.deg) i2 = ICRS(ra=0*u.deg, dec=2*u.deg) sep = i1.separation(i2) assert sep.deg == 1 i3 = ICRS(ra=[1, 2]*u.deg, dec=[3, 4]*u.deg, distance=[5, 6]*u.kpc) i4 = ICRS(ra=[1, 2]*u.deg, dec=[3, 4]*u.deg, distance=[4, 5]*u.kpc) sep3d = i3.separation_3d(i4) assert_allclose(sep3d.to(u.kpc), np.array([1, 1])*u.kpc) # check that it works even with velocities i5 = ICRS(ra=[1, 2]*u.deg, dec=[3, 4]*u.deg, distance=[5, 6]*u.kpc, pm_ra_cosdec=[1, 2]*u.mas/u.yr, pm_dec=[3, 4]*u.mas/u.yr, radial_velocity=[5, 6]*u.km/u.s) i6 = ICRS(ra=[1, 2]*u.deg, dec=[3, 4]*u.deg, distance=[7, 8]*u.kpc, pm_ra_cosdec=[1, 2]*u.mas/u.yr, pm_dec=[3, 4]*u.mas/u.yr, radial_velocity=[5, 6]*u.km/u.s) sep3d = i5.separation_3d(i6) assert_allclose(sep3d.to(u.kpc), np.array([2, 2])*u.kpc) # 3d separations of dimensionless distances should still work i7 = ICRS(ra=1*u.deg, dec=2*u.deg, distance=3*u.one) i8 = ICRS(ra=1*u.deg, dec=2*u.deg, distance=4*u.one) sep3d = i7.separation_3d(i8) assert_allclose(sep3d, 1*u.one) # but should fail with non-dimensionless with pytest.raises(ValueError): i7.separation_3d(i3)
def test_skyoffset(inradec, expectedlatlon, tolsep, originradec=(45, 45)*u.deg): origin = ICRS(*originradec) skyoffset_frame = SkyOffsetFrame(origin=origin) skycoord = SkyCoord(*inradec, frame=ICRS) skycoord_inaf = skycoord.transform_to(skyoffset_frame) assert hasattr(skycoord_inaf, 'lon') assert hasattr(skycoord_inaf, 'lat') expected = SkyCoord(*expectedlatlon, frame=skyoffset_frame) assert skycoord_inaf.separation(expected) < tolsep # Check we can also transform back (regression test for gh-11254). roundtrip = skycoord_inaf.transform_to(ICRS()) assert roundtrip.separation(skycoord) < 1*u.uas
def test_roundtrip_scalar(): icrs = ICRS(ra=1 * u.deg, dec=2 * u.deg, distance=3 * u.au) gcrs = GCRS(icrs.cartesian) bary = icrs.transform_to(BarycentricMeanEcliptic()) helio = icrs.transform_to(HeliocentricMeanEcliptic()) geo = gcrs.transform_to(GeocentricMeanEcliptic()) bary_icrs = bary.transform_to(ICRS()) helio_icrs = helio.transform_to(ICRS()) geo_gcrs = geo.transform_to(GCRS()) assert quantity_allclose(bary_icrs.cartesian.xyz, icrs.cartesian.xyz) assert quantity_allclose(helio_icrs.cartesian.xyz, icrs.cartesian.xyz) assert quantity_allclose(geo_gcrs.cartesian.xyz, gcrs.cartesian.xyz)
def test_altaz_diffs(): time = Time('J2015') + np.linspace(-1, 1, 1000) * u.day loc = get_builtin_sites()['greenwich'] aa = AltAz(obstime=time, location=loc) icoo = ICRS(np.zeros_like(time) * u.deg, 10 * u.deg, 100 * u.au, pm_ra_cosdec=np.zeros_like(time) * u.marcsec / u.yr, pm_dec=0 * u.marcsec / u.yr, radial_velocity=0 * u.km / u.s) acoo = icoo.transform_to(aa) # Make sure the change in radial velocity over ~2 days isn't too much # more than the rotation speed of the Earth - some excess is expected # because the orbit also shifts the RV, but it should be pretty small # over this short a time. assert np.ptp(acoo.radial_velocity) / 2 < (2 * np.pi * constants.R_earth / u.day) * 1.2 # MAGIC NUMBER cdiff = acoo.data.differentials['s'].represent_as(CartesianDifferential, acoo.data) # The "total" velocity should be > c, because the *tangential* velocity # isn't a True velocity, but rather an induced velocity due to the Earth's # rotation at a distance of 100 AU assert np.all(np.sum(cdiff.d_xyz**2, axis=0)**0.5 > constants.c)
def test_vel_transformation_obstime_err(): # TODO: replace after a final decision on PR #6280 from astropy.coordinates.sites import get_builtin_sites diff = r.CartesianDifferential([.1, .2, .3]*u.km/u.s) rep = r.CartesianRepresentation([1, 2, 3]*u.au, differentials=diff) loc = get_builtin_sites()['example_site'] aaf = AltAz(obstime='J2010', location=loc) aaf2 = AltAz(obstime=aaf.obstime + 3*u.day, location=loc) aaf3 = AltAz(obstime=aaf.obstime + np.arange(3)*u.day, location=loc) aaf4 = AltAz(obstime=aaf.obstime, location=loc) aa = aaf.realize_frame(rep) with pytest.raises(NotImplementedError) as exc: aa.transform_to(aaf2) assert 'cannot transform' in exc.value.args[0] with pytest.raises(NotImplementedError) as exc: aa.transform_to(aaf3) assert 'cannot transform' in exc.value.args[0] aa.transform_to(aaf4) aa.transform_to(ICRS())
def test_realizing(): from astropy.coordinates.builtin_frames import ICRS, FK5 from astropy.time import Time rep = r.SphericalRepresentation(1 * u.deg, 2 * u.deg, 3 * u.kpc) i = ICRS() i2 = i.realize_frame(rep) assert not i.has_data assert i2.has_data f = FK5(equinox=Time('J2001')) f2 = f.realize_frame(rep) assert not f.has_data assert f2.has_data assert f2.equinox == f.equinox assert f2.equinox != FK5.get_frame_attr_names()['equinox'] # Check that a nicer error message is returned: with pytest.raises(TypeError) as excinfo: f.realize_frame(f.representation_type) assert ('Class passed as data instead of a representation' in excinfo.value.args[0])
def test_frame_with_velocity_without_distance_can_be_transformed(): frame = CIRS(1 * u.deg, 2 * u.deg, pm_dec=1 * u.mas / u.yr, pm_ra_cosdec=2 * u.mas / u.yr) rep = frame.transform_to(ICRS()) assert "<ICRS Coordinate: (ra, dec, distance) in" in repr(rep)
def test_arraytransforms(): """ Test that transforms to/from ecliptic coordinates work on array coordinates (not testing for accuracy.) """ ra = np.ones((4, ), dtype=float) * u.deg dec = 2 * np.ones((4, ), dtype=float) * u.deg distance = np.ones((4, ), dtype=float) * u.au test_icrs = ICRS(ra=ra, dec=dec, distance=distance) test_gcrs = GCRS(test_icrs.data) bary_arr = test_icrs.transform_to(BarycentricTrueEcliptic) assert bary_arr.shape == ra.shape helio_arr = test_icrs.transform_to(HeliocentricTrueEcliptic) assert helio_arr.shape == ra.shape geo_arr = test_gcrs.transform_to(GeocentricTrueEcliptic) assert geo_arr.shape == ra.shape # now check that we also can go back the other way without shape problems bary_icrs = bary_arr.transform_to(ICRS) assert bary_icrs.shape == test_icrs.shape helio_icrs = helio_arr.transform_to(ICRS) assert helio_icrs.shape == test_icrs.shape geo_gcrs = geo_arr.transform_to(GCRS) assert geo_gcrs.shape == test_gcrs.shape
def test_cirs_icrs(): """ Test CIRS<->ICRS transformations, including self transform """ t = Time("J2010") MOONDIST = 385000 * u.km # approximate moon semi-major orbit axis of moon MOONDIST_CART = CartesianRepresentation(3**-0.5 * MOONDIST, 3**-0.5 * MOONDIST, 3**-0.5 * MOONDIST) loc = EarthLocation(lat=0 * u.deg, lon=0 * u.deg) cirs_geo_frame = CIRS(obstime=t) cirs_topo_frame = CIRS(obstime=t, location=loc) moon_geo = cirs_geo_frame.realize_frame(MOONDIST_CART) moon_topo = moon_geo.transform_to(cirs_topo_frame) # now check that the distance change is similar to earth radius assert 1000 * u.km < np.abs(moon_topo.distance - moon_geo.distance).to( u.au) < 7000 * u.km # now check that it round-trips moon2 = moon_topo.transform_to(moon_geo) assert_allclose(moon_geo.cartesian.xyz, moon2.cartesian.xyz) # now check ICRS transform gives a decent distance from Barycentre moon_icrs = moon_geo.transform_to(ICRS()) assert_allclose(moon_icrs.distance - 1 * u.au, 0.0 * u.R_sun, atol=3 * u.R_sun)
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])
def test_distance_change(): ra = Longitude("4:08:15.162342", unit=u.hour) dec = Latitude("-41:08:15.162342", unit=u.degree) c1 = ICRS(ra, dec, Distance(1, unit=u.kpc)) oldx = c1.cartesian.x.value assert (oldx - 0.35284083171901953) < 1e-10 # first make sure distances are immutible with pytest.raises(AttributeError): c1.distance = Distance(2, unit=u.kpc) # now x should increase with a bigger distance increases c2 = ICRS(ra, dec, Distance(2, unit=u.kpc)) assert c2.cartesian.x.value == oldx * 2
def test_lsr_loopback(frame): xyz = CartesianRepresentation(1, 2, 3) * u.AU xyz = xyz.with_differentials(CartesianDifferential(4, 5, 6) * u.km / u.s) v_bary = CartesianDifferential(5, 10, 15) * u.km / u.s # Test that the loopback properly handles a change in v_bary from_coo = frame(xyz) # default v_bary to_frame = frame(v_bary=v_bary) explicit_coo = from_coo.transform_to(ICRS()).transform_to(to_frame) implicit_coo = from_coo.transform_to(to_frame) # Confirm that the explicit transformation changes the velocity but not the position assert allclose(explicit_coo.cartesian.xyz, from_coo.cartesian.xyz, rtol=1e-10) assert not allclose( explicit_coo.velocity.d_xyz, from_coo.velocity.d_xyz, rtol=1e-10) # Confirm that the loopback matches the explicit transformation assert allclose(explicit_coo.cartesian.xyz, implicit_coo.cartesian.xyz, rtol=1e-10) assert allclose(explicit_coo.velocity.d_xyz, implicit_coo.velocity.d_xyz, rtol=1e-10)
def test_all_arg_options(kwargs): # Above is a list of all possible valid combinations of arguments. # Here we do a simple thing and just verify that passing them in, we have # access to the relevant attributes from the resulting object icrs = ICRS(**kwargs) gal = icrs.transform_to(Galactic) repr_gal = repr(gal) for k in kwargs: if k == 'differential_type': continue getattr(icrs, k) if 'pm_ra_cosdec' in kwargs: # should have both assert 'pm_l_cosb' in repr_gal assert 'pm_b' in repr_gal assert 'mas / yr' in repr_gal if 'radial_velocity' not in kwargs: assert 'radial_velocity' not in repr_gal if 'radial_velocity' in kwargs: assert 'radial_velocity' in repr_gal assert 'km / s' in repr_gal if 'pm_ra_cosdec' not in kwargs: assert 'pm_l_cosb' not in repr_gal assert 'pm_b' not in repr_gal
def test_skyoffset_names(): origin1 = ICRS(45 * u.deg, 45 * u.deg) aframe1 = SkyOffsetFrame(origin=origin1) assert type(aframe1).__name__ == 'SkyOffsetICRS' origin2 = Galactic(45 * u.deg, 45 * u.deg) aframe2 = SkyOffsetFrame(origin=origin2) assert type(aframe2).__name__ == 'SkyOffsetGalactic'
def test_nodata_error(): from astropy.coordinates.builtin_frames import ICRS i = ICRS() with pytest.raises(ValueError) as excinfo: i.data assert 'does not have associated data' in str(excinfo.value)
def test_getitem_representation(): """ Make sure current representation survives __getitem__ even if different from data representation. """ from astropy.coordinates.builtin_frames import ICRS c = ICRS([1, 1] * u.deg, [2, 2] * u.deg) c.representation_type = 'cartesian' assert c[0].representation_type is r.CartesianRepresentation
def test_negative_distance(): """ Regression test: #7408 Make sure that negative parallaxes turned into distances are handled right """ RA = 150 * u.deg DEC = -11 * u.deg c = ICRS(ra=RA, dec=DEC, distance=(-10 * u.mas).to(u.pc, u.parallax()), pm_ra_cosdec=10 * u.mas / u.yr, pm_dec=10 * u.mas / u.yr) assert quantity_allclose(c.ra, RA) assert quantity_allclose(c.dec, DEC) c = ICRS(ra=RA, dec=DEC, distance=(-10 * u.mas).to(u.pc, u.parallax())) assert quantity_allclose(c.ra, RA) assert quantity_allclose(c.dec, DEC)
def test_api(): # transform observed Barycentric velocities to full-space Galactocentric 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)
def test_transform_to_nonscalar_nodata_frame(): # https://github.com/astropy/astropy/pull/5254#issuecomment-241592353 from astropy.coordinates.builtin_frames import ICRS, FK5 from astropy.time import Time times = Time('2016-08-23') + np.linspace(0, 10, 12) * u.day coo1 = ICRS(ra=[[0.], [10.], [20.]] * u.deg, dec=[[-30.], [30.], [60.]] * u.deg) coo2 = coo1.transform_to(FK5(equinox=times)) assert coo2.shape == (3, 12)
def test_obstime(): """ Checks to make sure observation time is accounted for at least in FK4 <-> ICRS transformations """ b1950 = Time('B1950') j1975 = Time('J1975') fk4_50 = FK4(ra=1*u.deg, dec=2*u.deg, obstime=b1950) fk4_75 = FK4(ra=1*u.deg, dec=2*u.deg, obstime=j1975) icrs_50 = fk4_50.transform_to(ICRS()) icrs_75 = fk4_75.transform_to(ICRS()) # now check that the resulting coordinates are *different* - they should be, # because the obstime is different assert icrs_50.ra.degree != icrs_75.ra.degree assert icrs_50.dec.degree != icrs_75.dec.degree
def test_skyoffset_velocity(): c = ICRS(ra=170.9*u.deg, dec=-78.4*u.deg, pm_ra_cosdec=74.4134*u.mas/u.yr, pm_dec=-93.2342*u.mas/u.yr) skyoffset_frame = SkyOffsetFrame(origin=c) c_skyoffset = c.transform_to(skyoffset_frame) assert_allclose(c_skyoffset.pm_lon_coslat, c.pm_ra_cosdec) assert_allclose(c_skyoffset.pm_lat, c.pm_dec)
def test_gcrs_diffs(): time = Time('2017-01-01') gf = GCRS(obstime=time) sung = get_sun(time) # should have very little vhelio # qtr-year off sun location should be the direction of ~ maximal vhelio qtrsung = get_sun(time - .25 * u.year) # now we use those essentially as directions where the velocities should # be either maximal or minimal - with or perpendiculat to Earh's orbit msungr = CartesianRepresentation(-sung.cartesian.xyz).represent_as( SphericalRepresentation) suni = ICRS(ra=msungr.lon, dec=msungr.lat, distance=100 * u.au, pm_ra_cosdec=0 * u.marcsec / u.yr, pm_dec=0 * u.marcsec / u.yr, radial_velocity=0 * u.km / u.s) qtrsuni = ICRS(ra=qtrsung.ra, dec=qtrsung.dec, distance=100 * u.au, pm_ra_cosdec=0 * u.marcsec / u.yr, pm_dec=0 * u.marcsec / u.yr, radial_velocity=0 * u.km / u.s) # Now we transform those parallel- and perpendicular-to Earth's orbit # directions to GCRS, which should shift the velocity to either include # the Earth's velocity vector, or not (for parallel and perpendicular, # respectively). sung = suni.transform_to(gf) qtrsung = qtrsuni.transform_to(gf) # should be high along the ecliptic-not-sun sun axis and # low along the sun axis assert np.abs(qtrsung.radial_velocity) > 30 * u.km / u.s assert np.abs(qtrsung.radial_velocity) < 40 * u.km / u.s assert np.abs(sung.radial_velocity) < 1 * u.km / u.s suni2 = sung.transform_to(ICRS) assert np.all( np.abs(suni2.data.differentials['s'].d_xyz) < 3e-5 * u.km / u.s) qtrisun2 = qtrsung.transform_to(ICRS) assert np.all( np.abs(qtrisun2.data.differentials['s'].d_xyz) < 3e-5 * u.km / u.s)