Skip to content

Commit

Permalink
Pointing table
Browse files Browse the repository at this point in the history
  • Loading branch information
nabobalis committed Dec 11, 2024
1 parent 68b6ce7 commit 13236dc
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 159 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
envs: |
- windows: py310
- macos: py311
- linux: py312-devdeps
- linux: py313-devdeps
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

Expand Down
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
repos:
- repo: https://github.com/myint/docformatter
rev: v1.7.5
- repo: https://github.com/PyCQA/docformatter
rev: master
hooks:
- id: docformatter
args: ["--in-place", "--pre-summary-newline", "--make-summary-multi"]
- repo: https://github.com/myint/autoflake
- repo: https://github.com/PyCQA/autoflake
rev: v2.3.1
hooks:
- id: autoflake
Expand All @@ -16,13 +16,13 @@ repos:
]
exclude: ".*(.fits|.fts|.fit|.txt|tca.*|extern.*|.rst|.md|docs/conf.py)$"
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.5.4"
rev: "v0.8.2"
hooks:
- id: ruff
args: ["--fix", "--unsafe-fixes"]
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: check-ast
- id: check-case-conflict
Expand All @@ -40,7 +40,7 @@ repos:
- id: codespell
additional_dependencies:
- tomli
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
- repo: https://github.com/rbubley/mirrors-prettier.git
rev: v3.4.2
hooks:
- id: prettier
2 changes: 1 addition & 1 deletion aiapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def _get_bibtex():


__citation__ = __bibtex__ = _get_bibtex()
__all__ = ["__version__", "__citation__", "_SSW_MIRRORS"]
__all__ = ["_SSW_MIRRORS", "__citation__", "__version__"]
4 changes: 2 additions & 2 deletions aiapy/calibrate/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from aiapy.util import AiapyUserWarning
from aiapy.util.decorators import validate_channel

__all__ = ["register", "correct_degradation", "degradation"]
__all__ = ["correct_degradation", "degradation", "register"]


@add_common_docstring(rotation_function_names=_rotation_function_names)
Expand Down Expand Up @@ -218,7 +218,7 @@ def degradation(
ratio = np.zeros(obstime.shape)
poly = np.zeros(obstime.shape)
# Do this outside of the loop to avoid repeated queries
correction_table = get_correction_table(correction_table=correction_table)
correction_table = get_correction_table(correction_table=correction_table, calibration_version=calibration_version)
for i, t in enumerate(obstime):
table = _select_epoch_from_correction_table(channel, t, correction_table, version=calibration_version)

Expand Down
14 changes: 9 additions & 5 deletions aiapy/calibrate/spikes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from aiapy.util import AiapyUserWarning

__all__ = ["respike", "fetch_spikes"]
__all__ = ["fetch_spikes", "respike"]


def respike(smap, *, spikes=None):
Expand Down Expand Up @@ -142,10 +142,14 @@ def fetch_spikes(smap, *, as_coords=False):
series = r"aia.lev1_euv_12s"
if smap.wavelength in (1600, 1700, 4500) * u.angstrom:
series = r"aia.lev1_uv_24s"
file = drms.Client().query(
f'{series}[{smap.date}/12s][WAVELNTH={smap.meta["wavelnth"]}]',
seg="spikes",
)
try:
file = drms.Client().query(

Check warning on line 146 in aiapy/calibrate/spikes.py

View check run for this annotation

Codecov / codecov/patch

aiapy/calibrate/spikes.py#L145-L146

Added lines #L145 - L146 were not covered by tests
f'{series}[{smap.date}/12s][WAVELNTH={smap.meta["wavelnth"]}]',
seg="spikes",
)
except Exception as e:
msg = f"Could not retrieve spikes for {smap.date} at {smap.wavelength}.\n" f"Error message: {e}"
raise OSError(msg) from e

Check warning on line 152 in aiapy/calibrate/spikes.py

View check run for this annotation

Codecov / codecov/patch

aiapy/calibrate/spikes.py#L150-L152

Added lines #L150 - L152 were not covered by tests
_, spikes = fits.open(f'http://jsoc.stanford.edu{file["spikes"][0]}')
# Loaded as floats, but they are actually integers
spikes = spikes.data.astype(np.int32)
Expand Down
16 changes: 8 additions & 8 deletions aiapy/calibrate/tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def test_fix_observer_location(aia_171_map):
assert smap_fixed.meta["dsun_obs"] == smap_fixed.observer_coordinate.radius.value


@pytest.fixture()
@pytest.fixture
def pointing_table(aia_171_map):
return get_pointing_table(aia_171_map.date - 6 * u.h, aia_171_map.date + 6 * u.h)


@pytest.fixture()
@pytest.fixture
def mock_pointing_table():
return QTable(
[
Expand All @@ -45,7 +45,7 @@ def mock_pointing_table():
)


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_fix_pointing(aia_171_map, pointing_table):
keys = ["CRPIX1", "CRPIX2", "CDELT1", "CDELT2", "CROTA2"]
# Remove keys to at least test that they get set
Expand All @@ -61,7 +61,7 @@ def test_fix_pointing(aia_171_map, pointing_table):
assert aia_map_updated.meta[k] == aia_map_updated2.meta[k]


@pytest.mark.remote_data()
@pytest.mark.remote_data
@pytest.mark.parametrize(
("t_delt_factor", "expected_entry"),
[
Expand All @@ -86,7 +86,7 @@ def test_update_pointing_accuracy(aia_171_map, pointing_table, t_delt_factor, ex
assert aia_map_updated.reference_pixel.y == pointing_table[expected_entry]["A_171_Y0"]


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_update_pointing_submap_raises_exception(aia_171_map, pointing_table):
m = aia_171_map.submap(
SkyCoord(0, 0, unit="arcsec", frame=aia_171_map.coordinate_frame),
Expand All @@ -96,19 +96,19 @@ def test_update_pointing_submap_raises_exception(aia_171_map, pointing_table):
update_pointing(m, pointing_table=pointing_table)


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_update_pointing_resampled_raises_exception(aia_171_map, pointing_table):
m = aia_171_map.resample((512, 512) * u.pixel)
with pytest.raises(ValueError, match="Input must be at the full resolution"):
update_pointing(m, pointing_table=pointing_table)


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_update_pointing_no_entry_raises_exception(aia_171_map, pointing_table):
# This tests that an exception is thrown when entry corresponding to
# T_START <= T_OBS < T_END cannot be found in the pointing table.
# We explicitly set the T_OBS key
aia_171_map.meta["T_OBS"] = (aia_171_map.date + 1 * u.day).isot
aia_171_map.meta["T_OBS"] = (aia_171_map.date - 1000 * u.day).isot

Check warning on line 111 in aiapy/calibrate/tests/test_meta.py

View check run for this annotation

Codecov / codecov/patch

aiapy/calibrate/tests/test_meta.py#L111

Added line #L111 was not covered by tests
with pytest.raises(IndexError, match="No valid entries for"):
update_pointing(aia_171_map, pointing_table=pointing_table)

Expand Down
30 changes: 16 additions & 14 deletions aiapy/calibrate/tests/test_prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
from aiapy.util import AiapyUserWarning


@pytest.fixture()
@pytest.fixture
def lvl_15_map(aia_171_map):
return register(aia_171_map)


@pytest.fixture()
@pytest.fixture
def non_sdo_map():
return Map(sunpy.data.test.get_test_filepath("hsi_image_20101016_191218.fits"))

Expand Down Expand Up @@ -53,10 +53,11 @@ def test_register_filesave(lvl_15_map):
Test that adjusted header values are still correct after saving the map and
reloading it.
"""
afilename = tempfile.NamedTemporaryFile(suffix=".fits").name
with pytest.warns(VerifyWarning, match="The 'BLANK' keyword is only applicable to integer data"):
lvl_15_map.save(afilename, overwrite=True)
load_map = Map(afilename)
with tempfile.NamedTemporaryFile(suffix=".fits") as af:
afilename = af.name
with pytest.warns(VerifyWarning, match="The 'BLANK' keyword is only applicable to integer data"):
lvl_15_map.save(afilename, overwrite=True)
load_map = Map(afilename)
# Check crpix values
assert load_map.meta["crpix1"] == lvl_15_map.data.shape[1] / 2.0 + 0.5
assert load_map.meta["crpix2"] == lvl_15_map.data.shape[0] / 2.0 + 0.5
Expand Down Expand Up @@ -133,30 +134,30 @@ def test_correct_degradation(aia_171_map, correction_table, version):
pytest.param(
None,
10,
0.9031773242843387 * u.dimensionless_unscaled,
0.903 * u.dimensionless_unscaled,
marks=pytest.mark.remote_data,
),
pytest.param(
None,
9,
0.8658650561969473 * u.dimensionless_unscaled,
0.865 * u.dimensionless_unscaled,
marks=pytest.mark.remote_data,
),
pytest.param(
None,
8,
0.7667012041798814 * u.dimensionless_unscaled,
0.766 * u.dimensionless_unscaled,
marks=pytest.mark.remote_data,
),
(
get_test_filepath("aia_V8_20171210_050627_response_table.txt"),
8,
0.7667108920899671 * u.dimensionless_unscaled,
0.766 * u.dimensionless_unscaled,
),
(
get_correction_table(correction_table=get_test_filepath("aia_V8_20171210_050627_response_table.txt")),
8,
0.7667108920899671 * u.dimensionless_unscaled,
0.766 * u.dimensionless_unscaled,
),
],
)
Expand All @@ -174,7 +175,7 @@ def test_degradation(correction_table, version, time_correction_truth):
calibration_version=version,
correction_table=correction_table,
)
assert u.allclose(time_correction, time_correction_truth, rtol=1e-10, atol=0.0)
assert u.allclose(time_correction, time_correction_truth, atol=1e-3)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -231,13 +232,14 @@ def test_degradation_all_wavelengths(wavelength, result):
assert u.allclose(time_correction, result)


@pytest.mark.remote_data()
@pytest.mark.xfail(reason="JSOC is down plus the static files all lack 4500")
@pytest.mark.remote_data
def test_degradation_4500():
# 4500 has a max version of 3, so by default it will error
obstime = astropy.time.Time("2015-01-01T00:00:00", scale="utc")
with pytest.raises(
ValueError,
match="Correction table does not contain calibration for version 10 for 4500.0 Angstrom. Max version is 3",
match="Correction table does not contain calibration for version 10 for 4500.0 Angstrom. Max version is 3.",
):
degradation(4500 * u.angstrom, obstime)

Expand Down
22 changes: 12 additions & 10 deletions aiapy/calibrate/tests/test_spikes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,47 @@
from aiapy.calibrate import fetch_spikes, respike
from aiapy.util import AiapyUserWarning

pytestmark = pytest.mark.xfail(reason="JSOC is down")

@pytest.fixture()

@pytest.fixture
def despiked_map():
# Need an actual 4K-by-4K map to do the spike replacement
return sunpy.map.Map(
"https://github.com/sunpy/data/blob/main/aiapy/aia_lev1_193a_2013_03_15t12_01_06_84z_image_lev1.fits?raw=true",
)


@pytest.fixture()
@pytest.fixture
def respiked_map(despiked_map):
return respike(despiked_map)


@pytest.fixture()
@pytest.fixture
def spikes(despiked_map):
return fetch_spikes(despiked_map)


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_respike(respiked_map, spikes):
coords, values = spikes
for x, y, v in zip(coords.x.value, coords.y.value, values, strict=True):
assert v == respiked_map.data[int(y), int(x)]


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_respike_meta(respiked_map):
assert respiked_map.meta["lvl_num"] == 0.5
assert respiked_map.meta["nspikes"] == 0


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_fetch_with_prefetched_spikes(despiked_map, respiked_map, spikes):
respiked_map_prefetched = respike(despiked_map, spikes=spikes)
assert np.allclose(respiked_map.data, respiked_map_prefetched.data)


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_cutout(respiked_map, despiked_map):
blc = (-500, -500) * u.arcsec
trc = (500, 500) * u.arcsec
Expand All @@ -65,7 +67,7 @@ def test_cutout(respiked_map, despiked_map):
assert np.allclose(respiked_map_cutout.data, cutout_map_respiked.data)


@pytest.mark.remote_data()
@pytest.mark.remote_data
@pytest.mark.parametrize(
("key", "value", "error", "match"),
[
Expand All @@ -81,7 +83,7 @@ def test_exceptions(despiked_map, key, value, error, match):
respike(sunpy.map.Map(despiked_map.data, new_meta))


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_resample_warning(despiked_map):
despiked_map_resample = despiked_map.resample((512, 512) * u.pixel)
with (
Expand All @@ -91,7 +93,7 @@ def test_resample_warning(despiked_map):
respike(despiked_map_resample)


@pytest.mark.remote_data()
@pytest.mark.remote_data
@pytest.mark.parametrize(("as_coords", "kind"), [(True, SkyCoord), (False, PixelPair)])
def test_fetch_spikes(despiked_map, as_coords, kind):
n_spikes = despiked_map.meta["nspikes"]
Expand Down
4 changes: 2 additions & 2 deletions aiapy/calibrate/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_obstime_out_of_range():
_select_epoch_from_correction_table(94 * u.angstrom, obstime_out_of_range, correction_table_local, version=8)


@pytest.mark.remote_data()
@pytest.mark.remote_data
def test_pointing_table():
expected_columns = ["T_START", "T_STOP"]
for c in ["094", "171", "193", "211", "304", "335", "1600", "1700", "4500"]:
Expand All @@ -111,7 +111,7 @@ def test_pointing_table():
assert not hasattr(table[c], "mask")


@pytest.mark.remote_data()
@pytest.mark.xfail(reason="JSOC is down")
def test_pointing_table_unavailable():
# Check that missing pointing data raises a nice error
t = astropy.time.Time("1990-01-01")
Expand Down
Loading

0 comments on commit 13236dc

Please sign in to comment.