Skip to content

Commit

Permalink
pr response
Browse files Browse the repository at this point in the history
  • Loading branch information
laspsandoval committed Aug 26, 2024
1 parent bcb2d9c commit aa8feac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
23 changes: 12 additions & 11 deletions imap_processing/spice/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,25 @@ def wrapper_ensure_spice(*args: Any, **kwargs: Any) -> Any:


@ensure_spice
def create_pointing_frame(pointing_frame_dir: Optional[Path] = None) -> Path:
def create_pointing_frame(pointing_frame_path: Optional[Path] = None) -> Path:
"""
Create the pointing frame.
Parameters
----------
pointing_frame_dir : Path
pointing_frame_path : Path
Directory of where pointing frame will be saved.
Returns
-------
path_to_pointing_frame : Path
pointing_frame_path : Path
Path to pointing frame.
References
----------
https://numpydoc.readthedocs.io/en/latest/format.html#references
"""
ck_kernel, _, _, _ = spice.kdata(0, "ck")
directory = Path(ck_kernel).parent

# Get timerange for the pointing frame kernel.
et_start, et_end, et_times = _get_et_times(ck_kernel)
Expand All @@ -189,14 +192,12 @@ def create_pointing_frame(pointing_frame_dir: Optional[Path] = None) -> Path:
q_avg = spice.m2q(rotation_matrix)

# TODO: come up with naming convention.
if pointing_frame_dir is None:
path_to_pointing_frame = directory / "imap_dps.bc"
else:
path_to_pointing_frame = pointing_frame_dir / "imap_dps.bc"
if pointing_frame_path is None:
pointing_frame_path = Path(ck_kernel).parent / "imap_dps.bc"

# Open a new CK file, returning the handle of the opened file.
# https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.ckopn
handle = spice.ckopn(str(path_to_pointing_frame), "CK", 0)
handle = spice.ckopn(str(pointing_frame_path), "CK", 0)
# Get the SCLK ID.
# https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.gipool
id_imap_sclk = spice.gipool("CK_-43000_SCLK", 0, 1)
Expand Down Expand Up @@ -244,7 +245,7 @@ def create_pointing_frame(pointing_frame_dir: Optional[Path] = None) -> Path:
# Close CK file.
spice.ckcls(handle)

return path_to_pointing_frame
return pointing_frame_path


@ensure_spice
Expand All @@ -271,6 +272,7 @@ def _get_et_times(ck_kernel: str) -> tuple[float, float, np.ndarray]:
id_imap_spacecraft = spice.gipool("FRAME_IMAP_SPACECRAFT", 0, 1)

# TODO: Queried pointing start and stop times here.
# TODO removing the @ensure_spice decorator when using the repointing table.

# Get the coverage window
# https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.ckcov
Expand Down Expand Up @@ -340,7 +342,6 @@ def _average_quaternions(et_times: np.ndarray) -> NDArray:
return q_avg


@ensure_spice
def _create_rotation_matrix(et_times: np.ndarray) -> NDArray:
"""
Create a rotation matrix.
Expand Down
55 changes: 34 additions & 21 deletions imap_processing/tests/spice/test_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@


@pytest.fixture()
def kernels(spice_test_data_path):
def pointing_frame_kernels(spice_test_data_path):
"""List SPICE kernels."""
# TODO: remove this fixture after ensure_spice update.
kernels = [str(file) for file in spice_test_data_path.iterdir()]
required_kernels = [
"imap_science_0001.tf",
"imap_sclk_0000.tsc",
"imap_sim_ck_2hr_2secsampling_with_nutation.bc",
"imap_wkcp.tf",
"naif0012.tls",
]
kernels = [str(spice_test_data_path / kernel) for kernel in required_kernels]
return kernels


@pytest.fixture()
def et_times(kernels):
def et_times(pointing_frame_kernels):
"""Tests get_et_times function."""
# TODO: remove spice.furnsh(kernels) after ensure_spice update.
spice.furnsh(kernels)
spice.furnsh(pointing_frame_kernels)

file, _, _, _ = spice.kdata(0, "ck")
et_start, et_end, et_times = _get_et_times(file)

assert et_start == 802008069.184905
assert et_end == 802015267.184906
assert et_times[0] == et_start
assert et_times[-1] == et_end

return et_times


Expand Down Expand Up @@ -101,21 +101,19 @@ def test_ensure_spice_key_error():
_ = wrapped(577365941.184, "ISOC", 3) == "2018-04-18T23:24:31.998"


def test_average_quaternions(et_times, kernels):
def test_average_quaternions(et_times, pointing_frame_kernels):
"""Tests average_quaternions function."""
# TODO: remove spice.furnsh(kernels) after ensure_spice update.
spice.furnsh(kernels)
spice.furnsh(pointing_frame_kernels)
q_avg = _average_quaternions(et_times)

# Generated from MATLAB code results
q_avg_expected = np.array([-0.6611, 0.4981, -0.5019, -0.2509])
np.testing.assert_allclose(q_avg, q_avg_expected, atol=1e-4)


def test_create_rotation_matrix(et_times, kernels):
def test_create_rotation_matrix(et_times, pointing_frame_kernels):
"""Tests create_rotation_matrix function."""
# TODO: remove spice.furnsh(kernels) after ensure_spice update.
spice.furnsh(kernels)
spice.furnsh(pointing_frame_kernels)
rotation_matrix = _create_rotation_matrix(et_times)
q_avg = _average_quaternions(et_times)
z_avg = spice.q2m(list(q_avg))[:, 2]
Expand All @@ -129,13 +127,12 @@ def test_create_rotation_matrix(et_times, kernels):
np.testing.assert_allclose(rotation_matrix, rotation_matrix_expected, atol=1e-4)


def test_create_pointing_frame(spice_test_data_path, kernels, tmp_path):
def test_create_pointing_frame(spice_test_data_path, pointing_frame_kernels, tmp_path):
"""Tests create_pointing_frame function."""
# TODO: remove spice.furnsh(kernels) after ensure_spice update.
spice.furnsh(kernels)
spice.furnsh(pointing_frame_kernels)
ck_kernel, _, _, _ = spice.kdata(0, "ck")
et_start, et_end, et_times = _get_et_times(ck_kernel)
create_pointing_frame(pointing_frame_dir=tmp_path)
create_pointing_frame(pointing_frame_path=tmp_path / "imap_dps.bc")

# After imap_dps.bc has been created.
dps_kernel = str(tmp_path / "imap_dps.bc")
Expand All @@ -155,3 +152,19 @@ def test_create_pointing_frame(spice_test_data_path, kernels, tmp_path):

# Verify imap_dps.bc has been created.
assert (tmp_path / "imap_dps.bc").exists()


@ensure_spice
def test_et_times(pointing_frame_kernels):
"""Tests get_et_times function."""
spice.furnsh(pointing_frame_kernels)

file, _, _, _ = spice.kdata(0, "ck")
et_start, et_end, et_times = _get_et_times(file)

assert et_start == 802008069.184905
assert et_end == 802015267.184906
assert et_times[0] == et_start
assert et_times[-1] == et_end

return et_times

0 comments on commit aa8feac

Please sign in to comment.