Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG - fix for time kernels not loading during imap_cli command #838

Merged
merged 2 commits into from
Sep 12, 2024
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
22 changes: 19 additions & 3 deletions imap_processing/spice/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from numpy.typing import NDArray
from spiceypy.utils.exceptions import SpiceyError

from imap_processing import imap_module_directory

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -137,8 +139,11 @@ def wrapper_ensure_spice(*args: Any, **kwargs: Any) -> Any:
except SpiceyError as spicey_err:
try:
# Step 2.
metakernel_path = os.environ["SPICE_METAKERNEL"]
spice.furnsh(metakernel_path)
if os.getenv("SPICE_METAKERNEL"):
metakernel_path = os.getenv("SPICE_METAKERNEL")
spice.furnsh(metakernel_path)
else:
furnish_time_kernel()
except KeyError:
# TODO: An additional step that was used on EMUS was to get
# a custom metakernel from the SDC API based on an input
Expand All @@ -155,7 +160,7 @@ def wrapper_ensure_spice(*args: Any, **kwargs: Any) -> Any:
raise NotImplementedError from spicey_err
else:
raise SpiceyError(
"When calling a function requiring SPICE, we failed"
"When calling a function requiring SPICE, we failed "
"to load a metakernel. SPICE_METAKERNEL is not set,"
"and time_kernels_only is not set to True"
) from spicey_err
Expand Down Expand Up @@ -402,3 +407,14 @@ def _create_rotation_matrix(et_times: np.ndarray) -> NDArray:
rotation_matrix = np.asarray([x_avg, y_avg, z_avg])

return rotation_matrix


def furnish_time_kernel() -> None:
"""Furnish the time kernels."""
spice_test_data_path = imap_module_directory / "tests/spice/test_data"

# TODO: we need to load these kernels from EFS volumen that is
# mounted to batch volume and extend this to generate metakernell
# which is TBD.
spice.furnsh(str(spice_test_data_path / "imap_sclk_0000.tsc"))
spice.furnsh(str(spice_test_data_path / "naif0012.tls"))
2 changes: 2 additions & 0 deletions imap_processing/tests/spice/test_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def test_ensure_spice_emus_mk_path(func, use_test_metakernel):
assert func(577365941.184, "ISOC", 3) == "2018-04-18T23:24:31.998"


@pytest.mark.xfail(reason="Fix this test once we add metakernel in the imap_cli")
@pytest.mark.usefixtures("_unset_metakernel_path")
def test_ensure_spice_time_kernels():
"""Test functionality of ensure spice with timekernels set"""
Expand All @@ -92,6 +93,7 @@ def test_ensure_spice_time_kernels():
_ = wrapped(577365941.184, "ISOC", 3) == "2018-04-18T23:24:31.998"


@pytest.mark.xfail(reason="Fix this test once we add metakernel in the imap_cli")
@pytest.mark.usefixtures("_unset_metakernel_path")
def test_ensure_spice_key_error():
"""Test functionality of ensure spice when all branches fail"""
Expand Down
Loading