Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions astropy/time/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst


import copy
import functools
import datetime
Expand All @@ -10,9 +9,10 @@
from numpy.testing import assert_allclose

from astropy.tests.helper import catch_warnings, pytest
from astropy.utils.exceptions import AstropyDeprecationWarning
from astropy.utils import isiterable
from astropy.time import Time, ScaleValueError, STANDARD_TIME_SCALES, TimeString, TimezoneInfo
from astropy.utils.exceptions import AstropyDeprecationWarning, ErfaWarning
from astropy.utils import isiterable, iers
from astropy.time import (Time, ScaleValueError, STANDARD_TIME_SCALES,
TimeString, TimezoneInfo)
from astropy.coordinates import EarthLocation
from astropy import units as u
from astropy import _erfa as erfa
Expand Down Expand Up @@ -41,7 +41,7 @@ def teardown_function(func):
Time.FORMATS.update(func.FORMATS_ORIG)


class TestBasic():

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is leftover from incomplete PY2 removal? Sorry, I can't help myself and had to clean it up. 😂

class TestBasic:
"""Basic tests stemming from initial example and API reference"""

def test_simple(self):
Expand Down Expand Up @@ -319,18 +319,19 @@ def test_all_scale_transforms(self):
converted to local scales"""
lat = 19.48125
lon = -155.933222
for scale1 in STANDARD_TIME_SCALES:
t1 = Time('2006-01-15 21:24:37.5', format='iso', scale=scale1,
location=(lon, lat))
for scale2 in STANDARD_TIME_SCALES:
t2 = getattr(t1, scale2)
t21 = getattr(t2, scale1)
assert allclose_jd(t21.jd, t1.jd)

# test for conversion to local scale
scale3 = 'local'
with pytest.raises(ScaleValueError):
t2 = getattr(t1, scale3)
with iers.conf.set_temp('auto_download', False):
for scale1 in STANDARD_TIME_SCALES:
t1 = Time('2006-01-15 21:24:37.5', format='iso', scale=scale1,
location=(lon, lat))
for scale2 in STANDARD_TIME_SCALES:
t2 = getattr(t1, scale2)
t21 = getattr(t2, scale1)
assert allclose_jd(t21.jd, t1.jd)

# test for conversion to local scale
scale3 = 'local'
with pytest.raises(ScaleValueError):
t2 = getattr(t1, scale3)

def test_creating_all_formats(self):
"""Create a time object using each defined format"""
Expand Down Expand Up @@ -487,7 +488,8 @@ def test_input_validation(self):
with pytest.raises(ValueError):
Time(np.nan, format='jd', scale='utc')
with pytest.raises(ValueError):
Time('2000-01-02T03:04:05(TAI)', scale='utc')
with pytest.warns(AstropyDeprecationWarning):
Time('2000-01-02T03:04:05(TAI)', scale='utc')
with pytest.raises(ValueError):
Time('2000-01-02T03:04:05(TAI')
with pytest.raises(ValueError):
Expand All @@ -500,7 +502,8 @@ def test_utc_leap_sec(self):
# Start with a day without a leap second and note rollover
yyyy_mm = '{:04d}-{:02d}'.format(year, month)
yyyy_mm_dd = '{:04d}-{:02d}-{:02d}'.format(year, month, day)
t1 = Time(yyyy_mm + '-01 23:59:60.0', scale='utc')
with pytest.warns(ErfaWarning):
t1 = Time(yyyy_mm + '-01 23:59:60.0', scale='utc')
assert t1.iso == yyyy_mm + '-02 00:00:00.000'

# Leap second is different
Expand All @@ -518,7 +521,8 @@ def test_utc_leap_sec(self):
else:
yyyy_mm_dd_plus1 = '{:04d}-01-01'.format(year+1)

t1 = Time(yyyy_mm_dd + ' 23:59:61.0', scale='utc')
with pytest.warns(ErfaWarning):
t1 = Time(yyyy_mm_dd + ' 23:59:61.0', scale='utc')
assert t1.iso == yyyy_mm_dd_plus1 + ' 00:00:00.000'

# Delta time gives 2 seconds here as expected
Expand Down Expand Up @@ -570,7 +574,7 @@ def test_init_from_time_objects(self):
t6 = Time(t1, scale='local')


class TestVal2():
class TestVal2:
"""Tests related to val2"""

def test_val2_ignored(self):
Expand All @@ -593,7 +597,7 @@ def test_val_broadcasts_against_val2(self):
Time([0.0, 50000.0], [0.0, 1.0, 2.0], format='mjd', scale='tai')


class TestSubFormat():
class TestSubFormat:
"""Test input and output subformat functionality"""

def test_input_subformat(self):
Expand Down Expand Up @@ -795,7 +799,7 @@ def test_epoch_times(self):
assert allclose_sec(t.unix, 1095379199.0)


class TestSofaErrors():
class TestSofaErrors:
"""Test that erfa status return values are handled correctly"""

def test_bad_time(self):
Expand All @@ -820,7 +824,7 @@ def test_bad_time(self):
assert allclose_jd(djm, [53574.])


class TestCopyReplicate():
class TestCopyReplicate:
"""Test issues related to copying and replicating data"""

def test_immutable_input(self):
Expand Down Expand Up @@ -936,11 +940,11 @@ def test_decimalyear():


def test_fits_year0():
t = Time(1721425.5, format='jd')
t = Time(1721425.5, format='jd', scale='tai')
assert t.fits == '0001-01-01T00:00:00.000'
t = Time(1721425.5 - 366., format='jd')
t = Time(1721425.5 - 366., format='jd', scale='tai')
assert t.fits == '+00000-01-01T00:00:00.000'
t = Time(1721425.5 - 366. - 365., format='jd')
t = Time(1721425.5 - 366. - 365., format='jd', scale='tai')
assert t.fits == '-00001-01-01T00:00:00.000'


Expand Down Expand Up @@ -991,8 +995,9 @@ def test_TimeFormat_scale():
attributes (delta_ut1_utc here), preventing conversion to unix, cxc"""
t = Time('1900-01-01', scale='ut1')
t.delta_ut1_utc = 0.0
t.unix
assert t.unix == t.utc.unix
with pytest.warns(ErfaWarning):
t.unix
assert t.unix == t.utc.unix


@pytest.mark.remote_data
Expand Down Expand Up @@ -1516,8 +1521,9 @@ def test_strptime_leapsecond():


def test_strptime_3_digit_year():
time_obj1 = Time('0995-12-31T00:00:00', format='isot')
time_obj2 = Time.strptime('0995-Dec-31 00:00:00', '%Y-%b-%d %H:%M:%S')
time_obj1 = Time('0995-12-31T00:00:00', format='isot', scale='tai')
time_obj2 = Time.strptime('0995-Dec-31 00:00:00', '%Y-%b-%d %H:%M:%S',
scale='tai')

assert time_obj1 == time_obj2

Expand Down
2 changes: 1 addition & 1 deletion astropy/time/tests/test_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from astropy.time import Time, TimeDelta


class TestTimeComparisons():
class TestTimeComparisons:
"""Test Comparisons of Time and TimeDelta classes"""

def setup(self):
Expand Down
20 changes: 16 additions & 4 deletions astropy/time/tests/test_corrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
from astropy import units as u
from astropy.coordinates import EarthLocation, SkyCoord, solar_system_ephemeris
from astropy.time import Time, TimeDelta
from astropy.utils import iers

try:
import jplephem # pylint: disable=W0611
import jplephem # pylint: disable=W0611 # noqa
except ImportError:
HAS_JPLEPHEM = False
else:
HAS_JPLEPHEM = True


class TestHelioBaryCentric():
class TestHelioBaryCentric:
"""
Verify time offsets to the solar system barycentre and the heliocentre.
Uses the WHT observing site.
Expand All @@ -22,11 +23,21 @@ class TestHelioBaryCentric():
routines. They agree to an independent SLALIB based implementation
to 20 microseconds.
"""
@classmethod
def setup_class(cls):
cls.orig_auto_download = iers.conf.auto_download
iers.conf.auto_download = False

@classmethod
def teardown_class(cls):
iers.conf.auto_download = cls.orig_auto_download

def setup(self):
wht = EarthLocation(342.12*u.deg, 28.758333333333333*u.deg, 2327*u.m)
self.obstime = Time("2013-02-02T23:00", location=wht)
self.obstime2 = Time("2013-08-02T23:00", location=wht)
self.obstimeArr = Time(["2013-02-02T23:00", "2013-08-02T23:00"], location=wht)
self.obstimeArr = Time(["2013-02-02T23:00", "2013-08-02T23:00"],
location=wht)
self.star = SkyCoord("08:08:08 +32:00:00", unit=(u.hour, u.degree),
frame='icrs')

Expand Down Expand Up @@ -59,7 +70,8 @@ def test_arrays(self):
def test_ephemerides(self):
bval1 = self.obstime.light_travel_time(self.star, 'barycentric')
with solar_system_ephemeris.set('jpl'):
bval2 = self.obstime.light_travel_time(self.star, 'barycentric', ephemeris='jpl')
bval2 = self.obstime.light_travel_time(self.star, 'barycentric',
ephemeris='jpl')
# should differ by less than 0.1 ms, but not be the same
assert abs(bval1 - bval2) < 1. * u.ms
assert abs(bval1 - bval2) > 1. * u.us
26 changes: 19 additions & 7 deletions astropy/time/tests/test_delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@
from datetime import timedelta

from astropy.time import (Time, TimeDelta, OperandTypeError, ScaleValueError,
TIME_SCALES, STANDARD_TIME_SCALES, TIME_DELTA_SCALES)
TIME_SCALES, STANDARD_TIME_SCALES, TIME_DELTA_SCALES)
from astropy.utils import iers
from astropy import units as u

allclose_jd = functools.partial(np.allclose, rtol=2. ** -52, atol=0)
allclose_jd2 = functools.partial(np.allclose, rtol=2. ** -52,
atol=2. ** -52) # 20 ps atol
allclose_sec = functools.partial(np.allclose, rtol=2. ** -52,
atol=2. ** -52 * 24 * 3600) # 20 ps atol
orig_auto_download = iers.conf.auto_download


class TestTimeDelta():
def setup_module(module):
"""Use offline IERS table only."""
iers.conf.auto_download = False


def teardown_module(module):
"""Restore original setting."""
iers.conf.auto_download = orig_auto_download


class TestTimeDelta:
"""Test TimeDelta class"""

def setup(self):
Expand Down Expand Up @@ -74,8 +86,8 @@ def test_add(self):
def test_add_vector(self):
"""Check time arithmetic as well as properly keeping track of whether
a time is a scalar or a vector"""
t = Time(0.0, format='mjd', scale='utc')
t2 = Time([0.0, 1.0], format='mjd', scale='utc')
t = Time(0.0, format='mjd', scale='tai')
t2 = Time([0.0, 1.0], format='mjd', scale='tai')
dt = TimeDelta(100.0, format='jd')
dt2 = TimeDelta([100.0, 200.0], format='jd')

Expand Down Expand Up @@ -119,8 +131,8 @@ def test_add_vector(self):
def test_sub_vector(self):
"""Check time arithmetic as well as properly keeping track of whether
a time is a scalar or a vector"""
t = Time(0.0, format='mjd', scale='utc')
t2 = Time([0.0, 1.0], format='mjd', scale='utc')
t = Time(0.0, format='mjd', scale='tai')
t2 = Time([0.0, 1.0], format='mjd', scale='tai')
dt = TimeDelta(100.0, format='jd')
dt2 = TimeDelta([100.0, 200.0], format='jd')

Expand Down Expand Up @@ -256,7 +268,7 @@ def test_set_format(self):
assert dt.format == 'datetime'


class TestTimeDeltaScales():
class TestTimeDeltaScales:
"""Test scale conversion for Time Delta.
Go through @taldcroft's list of expected behavior from #1932"""

Expand Down
2 changes: 1 addition & 1 deletion astropy/time/tests/test_guess.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from astropy.time import Time


class TestGuess():
class TestGuess:
"""Test guessing the input value format"""

def test_guess1(self):
Expand Down
42 changes: 25 additions & 17 deletions astropy/time/tests/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
import functools
import numpy as np

from astropy.io.fits.verify import VerifyWarning
from astropy.utils.compat import NUMPY_LT_1_14
from astropy.utils import iers
from astropy.tests.helper import pytest
from astropy.time import Time
from astropy.table import Table

try:
import h5py # pylint: disable=W0611
import h5py # pylint: disable=W0611 # noqa
except ImportError:
HAS_H5PY = False
else:
HAS_H5PY = True

try:
import yaml # pylint: disable=W0611
import yaml # pylint: disable=W0611 # noqa
HAS_YAML = True
except ImportError:
HAS_YAML = False
Expand Down Expand Up @@ -95,26 +97,29 @@ def test_str():


def test_transform():
t = Time(['2000:001', '2000:002'])
t[1] = np.ma.masked
with iers.conf.set_temp('auto_download', False):
t = Time(['2000:001', '2000:002'])
t[1] = np.ma.masked

# Change scale (this tests the ERFA machinery with masking as well)
t_ut1 = t.ut1
assert is_masked(t_ut1.value[1])
assert not is_masked(t_ut1.value[0])
assert np.all(t_ut1.mask == [False, True])
# Change scale (this tests the ERFA machinery with masking as well)
t_ut1 = t.ut1
assert is_masked(t_ut1.value[1])
assert not is_masked(t_ut1.value[0])
assert np.all(t_ut1.mask == [False, True])

# Change format
t_unix = t.unix
assert is_masked(t_unix[1])
assert not is_masked(t_unix[0])
assert np.all(t_unix.mask == [False, True])
# Change format
t_unix = t.unix
assert is_masked(t_unix[1])
assert not is_masked(t_unix[0])
assert np.all(t_unix.mask == [False, True])


def test_masked_input():
v0 = np.ma.MaskedArray([[1, 2], [3, 4]]) # No masked elements
v1 = np.ma.MaskedArray([[1, 2], [3, 4]], mask=[[True, False], [False, False]])
v2 = np.ma.MaskedArray([[10, 20], [30, 40]], mask=[[False, False], [False, True]])
v1 = np.ma.MaskedArray([[1, 2], [3, 4]],
mask=[[True, False], [False, False]])
v2 = np.ma.MaskedArray([[10, 20], [30, 40]],
mask=[[False, False], [False, True]])

# Init from various combinations of masked arrays
t = Time(v0, format='cxcsec')
Expand Down Expand Up @@ -158,7 +163,10 @@ def test_serialize_fits_masked(tmpdir):
fn = str(tmpdir.join('tempfile.fits'))
t = Table([tm])
t.write(fn)
t2 = Table.read(fn, astropy_native=True)

# Warning is a bug; https://github.com/astropy/astropy/issues/8773

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment here about #8773, if that helps.

with pytest.warns(VerifyWarning):
t2 = Table.read(fn, astropy_native=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taldcroft - is the VerifyWarning expected?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks wrong to me, see #8773

@pllim pllim May 29, 2019 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While having the warning is a bug, I think we should still catch it for now until the bug is fixed. So that when it is fixed, this will cause the test to fail, and then we can remove the pytest.warns line?


# Time FITS handling does not current round-trip format in FITS
t2['col0'].format = tm.format
Expand Down
Loading