1# Licensed under a 3-clause BSD style license - see LICENSE.rst
2
3from unittest import mock
4
5import pytest
6
7from astropy.io.fits import HDUList, Header, PrimaryHDU, BinTableHDU
8from astropy.utils.data import get_pkg_data_filename
9
10from astropy.timeseries.io.kepler import kepler_fits_reader
11
12
13def fake_header(extver, version, timesys, telescop):
14    return Header({"SIMPLE": "T",
15                   "BITPIX": 8,
16                   "NAXIS": 0,
17                   "EXTVER": extver,
18                   "VERSION": version,
19                   'TIMESYS': f"{timesys}",
20                   "TELESCOP": f"{telescop}"})
21
22
23def fake_hdulist(extver=1, version=2, timesys="TDB", telescop="KEPLER"):
24    new_header = fake_header(extver, version, timesys, telescop)
25    return [HDUList(hdus=[PrimaryHDU(header=new_header),
26                    BinTableHDU(header=new_header, name="LIGHTCURVE")])]
27
28
29@mock.patch("astropy.io.fits.open", side_effect=fake_hdulist(telescop="MadeUp"))
30def test_raise_telescop_wrong(mock_file):
31    with pytest.raises(NotImplementedError) as exc:
32        kepler_fits_reader(None)
33    assert exc.value.args[0] == ("MadeUp is not implemented, only KEPLER or TESS are "
34                                 "supported through this reader")
35
36
37@mock.patch("astropy.io.fits.open", side_effect=fake_hdulist(extver=2))
38def test_raise_extversion_kepler(mock_file):
39    with pytest.raises(NotImplementedError) as exc:
40        kepler_fits_reader(None)
41    assert exc.value.args[0] == ("Support for KEPLER v2 files not yet "
42                                 "implemented")
43
44
45@mock.patch("astropy.io.fits.open", side_effect=fake_hdulist(extver=2, telescop="TESS"))
46def test_raise_extversion_tess(mock_file):
47    with pytest.raises(NotImplementedError) as exc:
48        kepler_fits_reader(None)
49    assert exc.value.args[0] == ("Support for TESS v2 files not yet "
50                                 "implemented")
51
52
53@mock.patch("astropy.io.fits.open", side_effect=fake_hdulist(timesys="TCB"))
54def test_raise_timesys_kepler(mock_file):
55    with pytest.raises(NotImplementedError) as exc:
56        kepler_fits_reader(None)
57    assert exc.value.args[0] == ("Support for TCB time scale not yet "
58                                 "implemented in KEPLER reader")
59
60
61@mock.patch("astropy.io.fits.open", side_effect=fake_hdulist(timesys="TCB", telescop="TESS"))
62def test_raise_timesys_tess(mock_file):
63    with pytest.raises(NotImplementedError) as exc:
64        kepler_fits_reader(None)
65    assert exc.value.args[0] == ("Support for TCB time scale not yet "
66                                 "implemented in TESS reader")
67
68
69@pytest.mark.remote_data(source='astropy')
70def test_kepler_astropy():
71    filename = get_pkg_data_filename('timeseries/kplr010666592-2009131110544_slc.fits')
72    timeseries = kepler_fits_reader(filename)
73    assert timeseries["time"].format == 'isot'
74    assert timeseries["time"].scale == 'tdb'
75    assert timeseries["sap_flux"].unit.to_string() == 'electron / s'
76    assert len(timeseries) == 14280
77    assert len(timeseries.columns) == 20
78
79
80@pytest.mark.remote_data(source='astropy')
81def test_tess_astropy():
82    filename = get_pkg_data_filename('timeseries/hlsp_tess-data-alerts_tess_phot_00025155310-s01_tess_v1_lc.fits')
83    with pytest.warns(UserWarning, match='Ignoring 815 rows with NaN times'):
84        timeseries = kepler_fits_reader(filename)
85    assert timeseries["time"].format == 'isot'
86    assert timeseries["time"].scale == 'tdb'
87    assert timeseries["sap_flux"].unit.to_string() == 'electron / s'
88    assert len(timeseries) == 19261
89    assert len(timeseries.columns) == 20
90