def test_to_string_padding(): a = Angle(0.5653, unit=u.deg) assert a.to_string(unit='deg', sep=':', pad=True) == r'00:33:55.08' # Test to make sure negative angles are padded correctly a = Angle(-0.5653, unit=u.deg) assert a.to_string(unit='deg', sep=':', pad=True) == r'-00:33:55.08'
def test_to_string_radian_with_precision(): """ Regression test for a bug that caused ``to_string`` to crash for angles in radians when specifying the precision. """ # Check that specifying the precision works a = Angle(3., unit=u.rad) assert a.to_string(precision=3, sep='fromunit') == '3.000rad'
def test_to_string_formats(): a = Angle(1.113355, unit=u.deg) assert a.to_string(format='latex') == r'$1^\circ06{}^\prime48.078{}^{\prime\prime}$' assert a.to_string(format='unicode') == '1°06′48.078″' a = Angle(1.113355, unit=u.hour) assert a.to_string(format='latex') == r'$1^\mathrm{h}06^\mathrm{m}48.078^\mathrm{s}$' assert a.to_string(format='unicode') == '1ʰ06ᵐ48.078ˢ' a = Angle(1.113355, unit=u.radian) assert a.to_string(format='latex') == r'$1.11336\mathrm{rad}$' assert a.to_string(format='unicode') == '1.11336rad'
def test_large_angle_representation(): """Test that angles above 360 degrees can be output as strings, in repr, str, and to_string. (regression test for #1413)""" a = Angle(350, u.deg) + Angle(350, u.deg) a.to_string() a.to_string(u.hourangle) repr(a) repr(a.to(u.hourangle)) str(a) str(a.to(u.hourangle))
def test_wrap_at(): a = Angle([-20, 150, 350, 360] * u.deg) assert np.all(a.wrap_at(360 * u.deg).degree == np.array([340., 150., 350., 0.])) assert np.all(a.wrap_at(Angle(360, unit=u.deg)).degree == np.array([340., 150., 350., 0.])) assert np.all(a.wrap_at('360d').degree == np.array([340., 150., 350., 0.])) assert np.all(a.wrap_at('180d').degree == np.array([-20., 150., -10., 0.])) assert np.all(a.wrap_at(np.pi * u.rad).degree == np.array([-20., 150., -10., 0.])) # Test wrapping a scalar Angle a = Angle('190d') assert a.wrap_at('180d') == Angle('-170d') a = Angle(np.arange(-1000.0, 1000.0, 0.125), unit=u.deg) for wrap_angle in (270, 0.2, 0.0, 360.0, 500, -2000.125): aw = a.wrap_at(wrap_angle * u.deg) assert np.all(aw.degree >= wrap_angle - 360.0) assert np.all(aw.degree < wrap_angle) aw = a.to(u.rad).wrap_at(wrap_angle * u.deg) assert np.all(aw.degree >= wrap_angle - 360.0) assert np.all(aw.degree < wrap_angle)
def test_sexagesimal_rounding_up(): a = Angle(359.9999999999, unit=u.deg) assert a.to_string(precision=None) == '360d00m00s' assert a.to_string(precision=4) == '360d00m00.0000s' assert a.to_string(precision=5) == '360d00m00.00000s' assert a.to_string(precision=6) == '360d00m00.000000s' assert a.to_string(precision=7) == '359d59m59.9999996s' a = Angle(3.999999, unit=u.deg) assert a.to_string(fields=2, precision=None) == '4d00m' assert a.to_string(fields=2, precision=1) == '4d00m' assert a.to_string(fields=2, precision=5) == '4d00m' assert a.to_string(fields=1, precision=1) == '4d' assert a.to_string(fields=1, precision=5) == '4d'
def test_is_within_bounds(): a = Angle([-20, 150, 350] * u.deg) assert a.is_within_bounds('0d', '360d') is False assert a.is_within_bounds(None, '360d') is True assert a.is_within_bounds(-30 * u.deg, None) is True a = Angle('-20d') assert a.is_within_bounds('0d', '360d') is False assert a.is_within_bounds(None, '360d') is True assert a.is_within_bounds(-30 * u.deg, None) is True
def test_to_string_precision(): # There are already some tests in test_api.py, but this is a regression # test for the bug in issue #1319 which caused incorrect formatting of the # seconds for precision=0 angle = Angle(-1.23456789, unit=u.degree) assert angle.to_string(precision=3) == '-1d14m04.444s' assert angle.to_string(precision=1) == '-1d14m04.4s' assert angle.to_string(precision=0) == '-1d14m04s' angle2 = Angle(-1.23456789, unit=u.hourangle) assert angle2.to_string(precision=3, unit=u.hour) == '-1h14m04.444s' assert angle2.to_string(precision=1, unit=u.hour) == '-1h14m04.4s' assert angle2.to_string(precision=0, unit=u.hour) == '-1h14m04s' # Regression test for #7141 angle3 = Angle(-0.5, unit=u.degree) assert angle3.to_string(precision=0, fields=3) == '-0d30m00s' assert angle3.to_string(precision=0, fields=2) == '-0d30m' assert angle3.to_string(precision=0, fields=1) == '-1d'
def test_to_string_decimal(): # There are already some tests in test_api.py, but this is a regression # test for the bug in issue #1323 which caused decimal formatting to not # work angle1 = Angle(2., unit=u.degree) assert angle1.to_string(decimal=True, precision=3) == '2.000' assert angle1.to_string(decimal=True, precision=1) == '2.0' assert angle1.to_string(decimal=True, precision=0) == '2' angle2 = Angle(3., unit=u.hourangle) assert angle2.to_string(decimal=True, precision=3) == '3.000' assert angle2.to_string(decimal=True, precision=1) == '3.0' assert angle2.to_string(decimal=True, precision=0) == '3' angle3 = Angle(4., unit=u.radian) assert angle3.to_string(decimal=True, precision=3) == '4.000' assert angle3.to_string(decimal=True, precision=1) == '4.0' assert angle3.to_string(decimal=True, precision=0) == '4'
def test_repr_latex(): """ Check the _repr_latex_ method, used primarily by IPython notebooks """ # try with both scalar scangle = Angle(2.1, u.deg) rlscangle = scangle._repr_latex_() # and array angles arrangle = Angle([1, 2.1], u.deg) rlarrangle = arrangle._repr_latex_() assert rlscangle == r'$2^\circ06{}^\prime00{}^{\prime\prime}$' assert rlscangle.split('$')[1] in rlarrangle # make sure the ... appears for large arrays bigarrangle = Angle(np.ones(50000)/50000., u.deg) assert '...' in bigarrangle._repr_latex_()
def test__str__(): """ Check the __str__ method used in printing the Angle """ # scalar angle scangle = Angle('10.2345d') strscangle = scangle.__str__() assert strscangle == '10d14m04.2s' # non-scalar array angles arrangle = Angle(['10.2345d', '-20d']) strarrangle = arrangle.__str__() assert strarrangle == '[10d14m04.2s -20d00m00s]' # summarizing for large arrays, ... should appear bigarrangle = Angle(np.ones(10000), u.deg) assert '...' in bigarrangle.__str__()
def test_angle_formatting(): """ Tests string formatting for Angle objects """ ''' The string method of Angle has this signature: def string(self, unit=DEGREE, decimal=False, sep=" ", precision=5, pad=False): The "decimal" parameter defaults to False since if you need to print the Angle as a decimal, there's no need to use the "format" method (see above). ''' angle = Angle("54.12412", unit=u.degree) # __str__ is the default `format` assert str(angle) == angle.to_string() res = 'Angle as HMS: 3h36m29.7888s' assert "Angle as HMS: {0}".format(angle.to_string(unit=u.hour)) == res res = 'Angle as HMS: 3:36:29.7888' assert "Angle as HMS: {0}".format(angle.to_string(unit=u.hour, sep=":")) == res res = 'Angle as HMS: 3:36:29.79' assert "Angle as HMS: {0}".format(angle.to_string(unit=u.hour, sep=":", precision=2)) == res # Note that you can provide one, two, or three separators passed as a # tuple or list res = 'Angle as HMS: 3h36m29.7888s' assert "Angle as HMS: {0}".format(angle.to_string(unit=u.hour, sep=("h", "m", "s"), precision=4)) == res res = 'Angle as HMS: 3-36|29.7888' assert "Angle as HMS: {0}".format(angle.to_string(unit=u.hour, sep=["-", "|"], precision=4)) == res res = 'Angle as HMS: 3-36-29.7888' assert "Angle as HMS: {0}".format(angle.to_string(unit=u.hour, sep="-", precision=4)) == res res = 'Angle as HMS: 03h36m29.7888s' assert "Angle as HMS: {0}".format(angle.to_string(unit=u.hour, precision=4, pad=True)) == res # Same as above, in degrees angle = Angle("3 36 29.78880", unit=u.degree) res = 'Angle as DMS: 3d36m29.7888s' assert "Angle as DMS: {0}".format(angle.to_string(unit=u.degree)) == res res = 'Angle as DMS: 3:36:29.7888' assert "Angle as DMS: {0}".format(angle.to_string(unit=u.degree, sep=":")) == res res = 'Angle as DMS: 3:36:29.79' assert "Angle as DMS: {0}".format(angle.to_string(unit=u.degree, sep=":", precision=2)) == res # Note that you can provide one, two, or three separators passed as a # tuple or list res = 'Angle as DMS: 3d36m29.7888s' assert "Angle as DMS: {0}".format(angle.to_string(unit=u.degree, sep=("d", "m", "s"), precision=4)) == res res = 'Angle as DMS: 3-36|29.7888' assert "Angle as DMS: {0}".format(angle.to_string(unit=u.degree, sep=["-", "|"], precision=4)) == res res = 'Angle as DMS: 3-36-29.7888' assert "Angle as DMS: {0}".format(angle.to_string(unit=u.degree, sep="-", precision=4)) == res res = 'Angle as DMS: 03d36m29.7888s' assert "Angle as DMS: {0}".format(angle.to_string(unit=u.degree, precision=4, pad=True)) == res res = 'Angle as rad: 0.0629763rad' assert "Angle as rad: {0}".format(angle.to_string(unit=u.radian)) == res res = 'Angle as rad decimal: 0.0629763' assert "Angle as rad decimal: {0}".format(angle.to_string(unit=u.radian, decimal=True)) == res # check negative angles angle = Angle(-1.23456789, unit=u.degree) angle2 = Angle(-1.23456789, unit=u.hour) assert angle.to_string() == '-1d14m04.4444s' assert angle.to_string(pad=True) == '-01d14m04.4444s' assert angle.to_string(unit=u.hour) == '-0h04m56.2963s' assert angle2.to_string(unit=u.hour, pad=True) == '-01h14m04.4444s' assert angle.to_string(unit=u.radian, decimal=True) == '-0.0215473'
def test_plus_sixty_hm(): # Test for HM parser with pytest.warns(IllegalMinuteWarning): a = Angle('00:60', u.hour) assert_allclose(a.hour, 1.)
def test_negative_zero_dm(): # Test for DM parser a = Angle('-00:10', u.deg) assert_allclose(a.degree, -10. / 60.)
def test_angle_formatting(): """ Tests string formatting for Angle objects """ ''' The string method of Angle has this signature: def string(self, unit=DEGREE, decimal=False, sep=" ", precision=5, pad=False): The "decimal" parameter defaults to False since if you need to print the Angle as a decimal, there's no need to use the "format" method (see above). ''' angle = Angle("54.12412", unit=u.degree) # __str__ is the default `format` assert str(angle) == angle.to_string() res = 'Angle as HMS: 3h36m29.7888s' assert f"Angle as HMS: {angle.to_string(unit=u.hour)}" == res res = 'Angle as HMS: 3:36:29.7888' assert f"Angle as HMS: {angle.to_string(unit=u.hour, sep=':')}" == res res = 'Angle as HMS: 3:36:29.79' assert f"Angle as HMS: {angle.to_string(unit=u.hour, sep=':', precision=2)}" == res # Note that you can provide one, two, or three separators passed as a # tuple or list res = 'Angle as HMS: 3h36m29.7888s' assert "Angle as HMS: {}".format( angle.to_string(unit=u.hour, sep=("h", "m", "s"), precision=4)) == res res = 'Angle as HMS: 3-36|29.7888' assert "Angle as HMS: {}".format( angle.to_string(unit=u.hour, sep=["-", "|"], precision=4)) == res res = 'Angle as HMS: 3-36-29.7888' assert f"Angle as HMS: {angle.to_string(unit=u.hour, sep='-', precision=4)}" == res res = 'Angle as HMS: 03h36m29.7888s' assert f"Angle as HMS: {angle.to_string(unit=u.hour, precision=4, pad=True)}" == res # Same as above, in degrees angle = Angle("3 36 29.78880", unit=u.degree) res = 'Angle as DMS: 3d36m29.7888s' assert f"Angle as DMS: {angle.to_string(unit=u.degree)}" == res res = 'Angle as DMS: 3:36:29.7888' assert f"Angle as DMS: {angle.to_string(unit=u.degree, sep=':')}" == res res = 'Angle as DMS: 3:36:29.79' assert "Angle as DMS: {}".format( angle.to_string(unit=u.degree, sep=":", precision=2)) == res # Note that you can provide one, two, or three separators passed as a # tuple or list res = 'Angle as DMS: 3d36m29.7888s' assert "Angle as DMS: {}".format( angle.to_string(unit=u.degree, sep=("d", "m", "s"), precision=4)) == res res = 'Angle as DMS: 3-36|29.7888' assert "Angle as DMS: {}".format( angle.to_string(unit=u.degree, sep=["-", "|"], precision=4)) == res res = 'Angle as DMS: 3-36-29.7888' assert "Angle as DMS: {}".format( angle.to_string(unit=u.degree, sep="-", precision=4)) == res res = 'Angle as DMS: 03d36m29.7888s' assert "Angle as DMS: {}".format( angle.to_string(unit=u.degree, precision=4, pad=True)) == res res = 'Angle as rad: 0.0629763rad' assert f"Angle as rad: {angle.to_string(unit=u.radian)}" == res res = 'Angle as rad decimal: 0.0629763' assert f"Angle as rad decimal: {angle.to_string(unit=u.radian, decimal=True)}" == res # check negative angles angle = Angle(-1.23456789, unit=u.degree) angle2 = Angle(-1.23456789, unit=u.hour) assert angle.to_string() == '-1d14m04.444404s' assert angle.to_string(pad=True) == '-01d14m04.444404s' assert angle.to_string(unit=u.hour) == '-0h04m56.2962936s' assert angle2.to_string(unit=u.hour, pad=True) == '-01h14m04.444404s' assert angle.to_string(unit=u.radian, decimal=True) == '-0.0215473'
def parse_test(i=0): Angle(angles, unit='hour')
def dec_dms(self): try: a = Angle(float(self.dec), unit='degree').dms except Exception as e: return self.dec return "{:+03.0f}:{:02.0f}:{:05.2f}".format(a[0], abs(a[1]), abs(a[2]))
def test_to_string_precision(): # There are already some tests in test_api.py, but this is a regression # test for the bug in issue #1319 which caused incorrect formatting of the # seconds for precision=0 angle = Angle(-1.23456789, unit=u.degree) assert angle.to_string(precision=3) == '-1d14m04.444s' assert angle.to_string(precision=1) == '-1d14m04.4s' assert angle.to_string(precision=0) == '-1d14m04s' angle2 = Angle(-1.23456789, unit=u.hourangle) assert angle2.to_string(precision=3, unit=u.hour) == '-1h14m04.444s' assert angle2.to_string(precision=1, unit=u.hour) == '-1h14m04.4s' assert angle2.to_string(precision=0, unit=u.hour) == '-1h14m04s' # Regression test for #7141 angle3 = Angle(-0.5, unit=u.degree) assert angle3.to_string(precision=0, fields=3) == '-0d30m00s' assert angle3.to_string(precision=0, fields=2) == '-0d30m' assert angle3.to_string(precision=0, fields=1) == '-1d'
def test_plus_sixty_dms(): # Test for DMS parser with pytest.warns(IllegalSecondWarning): a = Angle('+00:00:60', u.deg) assert_allclose(a.degree, 1. / 60.)
def test_sexagesimal_rounding_up(): a = Angle(359.999999999999, unit=u.deg) assert a.to_string(precision=None) == '360d00m00s' assert a.to_string(precision=4) == '360d00m00.0000s' assert a.to_string(precision=5) == '360d00m00.00000s' assert a.to_string(precision=6) == '360d00m00.000000s' assert a.to_string(precision=7) == '360d00m00.0000000s' assert a.to_string(precision=8) == '360d00m00.00000000s' assert a.to_string(precision=9) == '359d59m59.999999996s' a = Angle(3.999999, unit=u.deg) assert a.to_string(fields=2, precision=None) == '4d00m' assert a.to_string(fields=2, precision=1) == '4d00m' assert a.to_string(fields=2, precision=5) == '4d00m' assert a.to_string(fields=1, precision=1) == '4d' assert a.to_string(fields=1, precision=5) == '4d'
def test_to_string_fields(): a = Angle(1.113355, unit=u.deg) assert a.to_string(fields=1) == r'1d' assert a.to_string(fields=2) == r'1d07m' assert a.to_string(fields=3) == r'1d06m48.078s'
def test_to_string_formats(): a = Angle(1.113355, unit=u.deg) assert a.to_string(format='latex') == r'$1^\circ06{}^\prime48.078{}^{\prime\prime}$' assert a.to_string(format='unicode') == '1°06′48.078″' a = Angle(1.113355, unit=u.hour) assert a.to_string(format='latex') == r'$1^{\mathrm{h}}06^{\mathrm{m}}48.078^{\mathrm{s}}$' assert a.to_string(format='unicode') == '1ʰ06ᵐ48.078ˢ' a = Angle(1.113355, unit=u.radian) assert a.to_string(format='latex') == r'$1.11336\mathrm{rad}$' assert a.to_string(format='unicode') == '1.11336rad'
def test_to_string_decimal(): # There are already some tests in test_api.py, but this is a regression # test for the bug in issue #1323 which caused decimal formatting to not # work angle1 = Angle(2., unit=u.degree) assert angle1.to_string(decimal=True, precision=3) == '2.000' assert angle1.to_string(decimal=True, precision=1) == '2.0' assert angle1.to_string(decimal=True, precision=0) == '2' angle2 = Angle(3., unit=u.hourangle) assert angle2.to_string(decimal=True, precision=3) == '3.000' assert angle2.to_string(decimal=True, precision=1) == '3.0' assert angle2.to_string(decimal=True, precision=0) == '3' angle3 = Angle(4., unit=u.radian) assert angle3.to_string(decimal=True, precision=3) == '4.000' assert angle3.to_string(decimal=True, precision=1) == '4.0' assert angle3.to_string(decimal=True, precision=0) == '4'
def test_to_string_fields_colon(): a = Angle(1.113355, unit=u.deg) assert a.to_string(fields=2, sep=':') == '1:07' assert a.to_string(fields=3, sep=':') == '1:06:48.078' assert a.to_string(fields=1, sep=':') == '1'
def test_sexagesimal_round_down(): a1 = Angle(1, u.deg).to(u.hourangle) a2 = Angle(2, u.deg) assert a1.to_string() == '0h04m00s' assert a2.to_string() == '2d00m00s'
def test_to_string_fields_colon(): a = Angle(1.113355, unit=u.deg) assert a.to_string(fields=2, sep=':') == '1:07' assert a.to_string(fields=3, sep=':') == '1:06:48.078' assert a.to_string(fields=1, sep=':') == '1'
def test_sexagesimal_round_down(): a1 = Angle(1, u.deg).to(u.hourangle) a2 = Angle(2, u.deg) assert a1.to_string() == '0h04m00s' assert a2.to_string() == '2d00m00s'
def test_angle_to_is_angle(): with pytest.warns(IllegalSecondWarning): a = Angle('00:00:60', u.deg) assert isinstance(a, Angle) assert isinstance(a.to(u.rad), Angle)
def ra_hms(self): try: a = Angle(float(self.ra), unit='degree').hms except Exception as e: return self.ra return "{:02.0f}:{:02.0f}:{:05.2f}".format(*a)
def test_angle_to_quantity(): with pytest.warns(IllegalSecondWarning): a = Angle('00:00:60', u.deg) q = u.Quantity(a) assert isinstance(q, u.Quantity) assert q.unit is u.deg
def test_angle_wrap_at_nan(): # Check that no attempt is made to wrap a NaN angle angle = Angle([0, np.nan, 1] * u.deg) angle.flags.writeable = False # to force an error if a write is attempted angle.wrap_at(180 * u.deg, inplace=True)
def test_angle_string(): with pytest.warns(IllegalSecondWarning): a = Angle('00:00:60', u.deg) assert str(a) == '0d01m00s' a = Angle('00:00:59S', u.deg) assert str(a) == '-0d00m59s' a = Angle('00:00:59N', u.deg) assert str(a) == '0d00m59s' a = Angle('00:00:59E', u.deg) assert str(a) == '0d00m59s' a = Angle('00:00:59W', u.deg) assert str(a) == '-0d00m59s' a = Angle('-00:00:10', u.hour) assert str(a) == '-0h00m10s' a = Angle('00:00:59E', u.hour) assert str(a) == '0h00m59s' a = Angle('00:00:59W', u.hour) assert str(a) == '-0h00m59s' a = Angle(3.2, u.radian) assert str(a) == '3.2rad' a = Angle(4.2, u.microarcsecond) assert str(a) == '4.2uarcsec' a = Angle('1.0uarcsec') assert a.value == 1.0 assert a.unit == u.microarcsecond a = Angle('1.0uarcsecN') assert a.value == 1.0 assert a.unit == u.microarcsecond a = Angle('1.0uarcsecS') assert a.value == -1.0 assert a.unit == u.microarcsecond a = Angle('1.0uarcsecE') assert a.value == 1.0 assert a.unit == u.microarcsecond a = Angle('1.0uarcsecW') assert a.value == -1.0 assert a.unit == u.microarcsecond a = Angle("3d") assert_allclose(a.value, 3.0) assert a.unit == u.degree a = Angle("3dN") assert str(a) == "3d00m00s" assert a.unit == u.degree a = Angle("3dS") assert str(a) == "-3d00m00s" assert a.unit == u.degree a = Angle("3dE") assert str(a) == "3d00m00s" assert a.unit == u.degree a = Angle("3dW") assert str(a) == "-3d00m00s" assert a.unit == u.degree a = Angle('10"') assert_allclose(a.value, 10.0) assert a.unit == u.arcsecond a = Angle("10'N") assert_allclose(a.value, 10.0) assert a.unit == u.arcminute a = Angle("10'S") assert_allclose(a.value, -10.0) assert a.unit == u.arcminute a = Angle("10'E") assert_allclose(a.value, 10.0) assert a.unit == u.arcminute a = Angle("10'W") assert_allclose(a.value, -10.0) assert a.unit == u.arcminute a = Angle('45°55′12″N') assert str(a) == '45d55m12s' assert_allclose(a.value, 45.92) assert a.unit == u.deg a = Angle('45°55′12″S') assert str(a) == '-45d55m12s' assert_allclose(a.value, -45.92) assert a.unit == u.deg a = Angle('45°55′12″E') assert str(a) == '45d55m12s' assert_allclose(a.value, 45.92) assert a.unit == u.deg a = Angle('45°55′12″W') assert str(a) == '-45d55m12s' assert_allclose(a.value, -45.92) assert a.unit == u.deg with pytest.raises(ValueError): Angle('00h00m10sN') with pytest.raises(ValueError): Angle('45°55′12″NS')
def test_create_angles(): """ Tests creating and accessing Angle objects """ ''' The "angle" is a fundamental object. The internal representation is stored in radians, but this is transparent to the user. Units *must* be specified rather than a default value be assumed. This is as much for self-documenting code as anything else. Angle objects simply represent a single angular coordinate. More specific angular coordinates (e.g. Longitude, Latitude) are subclasses of Angle.''' a1 = Angle(54.12412, unit=u.degree) a2 = Angle("54.12412", unit=u.degree) a3 = Angle("54:07:26.832", unit=u.degree) a4 = Angle("54.12412 deg") a5 = Angle("54.12412 degrees") a6 = Angle("54.12412°") # because we like Unicode a8 = Angle("54°07'26.832\"") a9 = Angle([54, 7, 26.832], unit=u.degree) assert_allclose(a9.value, [54, 7, 26.832]) assert a9.unit is u.degree a10 = Angle(3.60827466667, unit=u.hour) a11 = Angle("3:36:29.7888000120", unit=u.hour) with pytest.warns(AstropyDeprecationWarning, match='hms_to_hour'): a12 = Angle((3, 36, 29.7888000120), unit=u.hour) # *must* be a tuple with pytest.warns(AstropyDeprecationWarning, match='hms_to_hour'): # Regression test for #5001 a13 = Angle((3, 36, 29.7888000120), unit='hour') Angle(0.944644098745, unit=u.radian) with pytest.raises(u.UnitsError): Angle(54.12412) # raises an exception because this is ambiguous with pytest.raises(u.UnitsError): Angle(54.12412, unit=u.m) with pytest.raises(ValueError): Angle(12.34, unit="not a unit") a14 = Angle("03h36m29.7888000120") # no trailing 's', but unambiguous a15 = Angle("5h4m3s") # single digits, no decimal assert a15.unit == u.hourangle a16 = Angle("1 d") a17 = Angle("1 degree") assert a16.degree == 1 assert a17.degree == 1 a18 = Angle("54 07.4472", unit=u.degree) a19 = Angle("54:07.4472", unit=u.degree) a20 = Angle("54d07.4472m", unit=u.degree) a21 = Angle("3h36m", unit=u.hour) a22 = Angle("3.6h", unit=u.hour) a23 = Angle("- 3h", unit=u.hour) a24 = Angle("+ 3h", unit=u.hour) # ensure the above angles that should match do assert a1 == a2 == a3 == a4 == a5 == a6 == a8 == a18 == a19 == a20 assert_allclose(a1.radian, a2.radian) assert_allclose(a2.degree, a3.degree) assert_allclose(a3.radian, a4.radian) assert_allclose(a4.radian, a5.radian) assert_allclose(a5.radian, a6.radian) assert_allclose(a10.degree, a11.degree) assert a11 == a12 == a13 == a14 assert a21 == a22 assert a23 == -a24 # check for illegal ranges / values with pytest.raises(IllegalSecondError): a = Angle("12 32 99", unit=u.degree) with pytest.raises(IllegalMinuteError): a = Angle("12 99 23", unit=u.degree) with pytest.raises(IllegalSecondError): a = Angle("12 32 99", unit=u.hour) with pytest.raises(IllegalMinuteError): a = Angle("12 99 23", unit=u.hour) with pytest.raises(IllegalHourError): a = Angle("99 25 51.0", unit=u.hour) with pytest.raises(ValueError): a = Angle("12 25 51.0xxx", unit=u.hour) with pytest.raises(ValueError): a = Angle("12h34321m32.2s") assert a1 is not None
def test_wrap_at_inplace(): a = Angle([-20, 150, 350, 360] * u.deg) out = a.wrap_at('180d', inplace=True) assert out is None assert np.all(a.degree == np.array([-20., 150., -10., 0.]))
def test_to_string_vector(): # Regression test for the fact that vectorize doesn't work with Numpy 1.6 assert Angle([1. / 7., 1. / 7.], unit='deg').to_string()[0] == "0d08m34.28571429s" assert Angle([1. / 7.], unit='deg').to_string()[0] == "0d08m34.28571429s" assert Angle(1. / 7., unit='deg').to_string() == "0d08m34.28571429s"
def test_empty_sep(): a = Angle('05h04m31.93830s') assert a.to_string(sep='', precision=2, pad=True) == '050431.94'
def test_negative_zero_hm(): # Test for HM parser a = Angle('-00:10', u.hour) assert_allclose(a.hour, -10. / 60.)
def test_to_string_fields(): a = Angle(1.113355, unit=u.deg) assert a.to_string(fields=1) == r'1d' assert a.to_string(fields=2) == r'1d07m' assert a.to_string(fields=3) == r'1d06m48.078s'
def test_negative_fifty_nine_sixty_dms(): # Test for DMS parser with pytest.warns(IllegalSecondWarning): a = Angle('-00:59:60', u.deg) assert_allclose(a.degree, -1.)
def test_angle_mismatched_unit(): a = Angle('+6h7m8s', unit=u.degree) assert_allclose(a.value, 91.78333333333332)
def test_angle_to_is_angle(): with pytest.warns(IllegalSecondWarning): a = Angle('00:00:60', u.deg) assert isinstance(a, Angle) assert isinstance(a.to(u.rad), Angle)
def test_empty_sep(): a = Angle('05h04m31.93830s') assert a.to_string(sep='', precision=2, pad=True) == '050431.94'
def test_wrap_at_inplace(): a = Angle([-20, 150, 350, 360] * u.deg) out = a.wrap_at('180d', inplace=True) assert out is None assert np.all(a.degree == np.array([-20., 150., -10., 0.]))
def test_array_angle_tostring(): aobj = Angle([1, 2], u.deg) assert aobj.to_string().dtype.kind == 'U' assert np.all(aobj.to_string() == ['1d00m00s', '2d00m00s'])
def test_latitude(): with pytest.raises(ValueError): lat = Latitude(['91d', '89d']) with pytest.raises(ValueError): lat = Latitude('-91d') lat = Latitude(['90d', '89d']) # check that one can get items assert lat[0] == 90 * u.deg assert lat[1] == 89 * u.deg # and that comparison with angles works assert np.all(lat == Angle(['90d', '89d'])) # check setitem works lat[1] = 45. * u.deg assert np.all(lat == Angle(['90d', '45d'])) # but not with values out of range with pytest.raises(ValueError): lat[0] = 90.001 * u.deg with pytest.raises(ValueError): lat[0] = -90.001 * u.deg # these should also not destroy input (#1851) assert np.all(lat == Angle(['90d', '45d'])) # conserve type on unit change (closes #1423) angle = lat.to('radian') assert type(angle) is Latitude # but not on calculations angle = lat - 190 * u.deg assert type(angle) is Angle assert angle[0] == -100 * u.deg lat = Latitude('80d') angle = lat / 2. assert type(angle) is Angle assert angle == 40 * u.deg angle = lat * 2. assert type(angle) is Angle assert angle == 160 * u.deg angle = -lat assert type(angle) is Angle assert angle == -80 * u.deg # Test errors when trying to interoperate with longitudes. with pytest.raises(TypeError) as excinfo: lon = Longitude(10, 'deg') lat = Latitude(lon) assert "A Latitude angle cannot be created from a Longitude angle" in str( excinfo.value) with pytest.raises(TypeError) as excinfo: lon = Longitude(10, 'deg') lat = Latitude([20], 'deg') lat[0] = lon assert "A Longitude angle cannot be assigned to a Latitude angle" in str( excinfo.value) # Check we can work around the Lat vs Long checks by casting explicitly to Angle. lon = Longitude(10, 'deg') lat = Latitude(Angle(lon)) assert lat.value == 10.0 # Check setitem. lon = Longitude(10, 'deg') lat = Latitude([20], 'deg') lat[0] = Angle(lon) assert lat.value[0] == 10.0
def test_to_string_scalar(): a = Angle(1.113355, unit=u.deg) assert isinstance(a.to_string(), str)
def test_array_angle_tostring(): aobj = Angle([1, 2], u.deg) assert aobj.to_string().dtype.kind == 'U' assert np.all(aobj.to_string() == ['1d00m00s', '2d00m00s'])
def test_longitude(): # Default wrapping at 360d with an array input lon = Longitude(['370d', '88d']) assert np.all(lon == Longitude(['10d', '88d'])) assert np.all(lon == Angle(['10d', '88d'])) # conserve type on unit change and keep wrap_angle (closes #1423) angle = lon.to('hourangle') assert type(angle) is Longitude assert angle.wrap_angle == lon.wrap_angle angle = lon[0] assert type(angle) is Longitude assert angle.wrap_angle == lon.wrap_angle angle = lon[1:] assert type(angle) is Longitude assert angle.wrap_angle == lon.wrap_angle # but not on calculations angle = lon / 2. assert np.all(angle == Angle(['5d', '44d'])) assert type(angle) is Angle assert not hasattr(angle, 'wrap_angle') angle = lon * 2. + 400 * u.deg assert np.all(angle == Angle(['420d', '576d'])) assert type(angle) is Angle # Test setting a mutable value and having it wrap lon[1] = -10 * u.deg assert np.all(lon == Angle(['10d', '350d'])) # Test wrapping and try hitting some edge cases lon = Longitude(np.array([0, 0.5, 1.0, 1.5, 2.0]) * np.pi, unit=u.radian) assert np.all(lon.degree == np.array([0., 90, 180, 270, 0])) lon = Longitude(np.array([0, 0.5, 1.0, 1.5, 2.0]) * np.pi, unit=u.radian, wrap_angle='180d') assert np.all(lon.degree == np.array([0., 90, -180, -90, 0])) # Wrap on setting wrap_angle property (also test auto-conversion of wrap_angle to an Angle) lon = Longitude(np.array([0, 0.5, 1.0, 1.5, 2.0]) * np.pi, unit=u.radian) lon.wrap_angle = '180d' assert np.all(lon.degree == np.array([0., 90, -180, -90, 0])) lon = Longitude('460d') assert lon == Angle('100d') lon.wrap_angle = '90d' assert lon == Angle('-260d') # check that if we initialize a longitude with another longitude, # wrap_angle is kept by default lon2 = Longitude(lon) assert lon2.wrap_angle == lon.wrap_angle # but not if we explicitly set it lon3 = Longitude(lon, wrap_angle='180d') assert lon3.wrap_angle == 180 * u.deg # check for problem reported in #2037 about Longitude initializing to -0 lon = Longitude(0, u.deg) lonstr = lon.to_string() assert not lonstr.startswith('-') # also make sure dtype is correctly conserved assert Longitude(0, u.deg, dtype=float).dtype == np.dtype(float) assert Longitude(0, u.deg, dtype=int).dtype == np.dtype(int) # Test errors when trying to interoperate with latitudes. with pytest.raises(TypeError) as excinfo: lat = Latitude(10, 'deg') lon = Longitude(lat) assert "A Longitude angle cannot be created from a Latitude angle" in str( excinfo.value) with pytest.raises(TypeError) as excinfo: lat = Latitude(10, 'deg') lon = Longitude([20], 'deg') lon[0] = lat assert "A Latitude angle cannot be assigned to a Longitude angle" in str( excinfo.value) # Check we can work around the Lat vs Long checks by casting explicitly to Angle. lat = Latitude(10, 'deg') lon = Longitude(Angle(lat)) assert lon.value == 10.0 # Check setitem. lat = Latitude(10, 'deg') lon = Longitude([20], 'deg') lon[0] = Angle(lat) assert lon.value[0] == 10.0
def test_wrap_at(): a = Angle([-20, 150, 350, 360] * u.deg) assert np.all( a.wrap_at(360 * u.deg).degree == np.array([340., 150., 350., 0.])) assert np.all( a.wrap_at(Angle(360, unit=u.deg)).degree == np.array( [340., 150., 350., 0.])) assert np.all(a.wrap_at('360d').degree == np.array([340., 150., 350., 0.])) assert np.all(a.wrap_at('180d').degree == np.array([-20., 150., -10., 0.])) assert np.all( a.wrap_at(np.pi * u.rad).degree == np.array([-20., 150., -10., 0.])) # Test wrapping a scalar Angle a = Angle('190d') assert a.wrap_at('180d') == Angle('-170d') a = Angle(np.arange(-1000.0, 1000.0, 0.125), unit=u.deg) for wrap_angle in (270, 0.2, 0.0, 360.0, 500, -2000.125): aw = a.wrap_at(wrap_angle * u.deg) assert np.all(aw.degree >= wrap_angle - 360.0) assert np.all(aw.degree < wrap_angle) aw = a.to(u.rad).wrap_at(wrap_angle * u.deg) assert np.all(aw.degree >= wrap_angle - 360.0) assert np.all(aw.degree < wrap_angle)
def test_to_string_scalar(): a = Angle(1.113355, unit=u.deg) assert isinstance(a.to_string(), str)