From 3d38e6f416901e74f6b9092ac415d75f221c56d6 Mon Sep 17 00:00:00 2001 From: Laura Sandoval Date: Thu, 22 Aug 2024 15:19:17 -0600 Subject: [PATCH] update test --- imap_processing/spice/kernels.py | 135 ++++++++++---------- imap_processing/tests/spice/test_kernels.py | 89 ++++++------- 2 files changed, 101 insertions(+), 123 deletions(-) diff --git a/imap_processing/spice/kernels.py b/imap_processing/spice/kernels.py index aca6cce21..5e906df9e 100644 --- a/imap_processing/spice/kernels.py +++ b/imap_processing/spice/kernels.py @@ -169,79 +169,72 @@ def create_pointing_frame() -> Path: Returns ------- path_to_pointing_frame : Path - Path to dps frame. + Path to pointing frame. """ - # TODO: this part will change with ensure_spice decorator. - # Mount path to EFS. - mount_path = Path(os.getenv("EFS_MOUNT_PATH", "")) - - # TODO: this part will change with ensure_spice decorator. - kernels = [str(file) for file in mount_path.iterdir()] - ck_kernel = [str(file) for file in mount_path.iterdir() if file.suffix == ".bc"] - - # Furnish the kernels. - with spice.KernelPool(kernels): - # Get timerange for the pointing frame kernel. - et_start, et_end, et_times = _get_et_times(str(ck_kernel[0])) - # Create a rotation matrix - rotation_matrix = _create_rotation_matrix(et_times) - - # Convert the rotation matrix to a quaternion. - # https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.m2q - q_avg = spice.m2q(rotation_matrix) - - # TODO: come up with naming convention. - path_to_pointing_frame = mount_path / "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) - # 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) - # https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.sce2c - # Convert start and end times to SCLK. - sclk_begtim = spice.sce2c(int(id_imap_sclk), et_start) - sclk_endtim = spice.sce2c(int(id_imap_sclk), et_end) - - # Get the pointing frame ID. - # https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.gipool - id_imap_dps = spice.gipool("FRAME_IMAP_DPS", 0, 1) - - # Create the pointing frame kernel. - # https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.ckw02 - spice.ckw02( - # Handle of an open CK file. - handle, - # Start time of the segment. - sclk_begtim, - # End time of the segment. - sclk_endtim, - # Pointing frame ID. - int(id_imap_dps), - # Reference frame. - "ECLIPJ2000", # Reference frame - # Identifier. - "IMAP_DPS", - # Number of pointing intervals. - 1, - # Start times of individual pointing records within segment. - # Since there is only a single record this is equal to sclk_begtim. - np.array([sclk_begtim]), - # End times of individual pointing records within segment. - # Since there is only a single record this is equal to sclk_endtim. - np.array([sclk_endtim]), # Single stop time - # Average quaternion. - q_avg, - # 0.0 Angular rotation terms. - np.array([0.0, 0.0, 0.0]), - # Rates (seconds per tick) at which the quaternion and - # angular velocity change. - np.array([1.0]), - ) + 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) + # Create a rotation matrix + rotation_matrix = _create_rotation_matrix(et_times) + + # Convert the rotation matrix to a quaternion. + # https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.m2q + q_avg = spice.m2q(rotation_matrix) - # Close CK file. - spice.ckcls(handle) + # TODO: come up with naming convention. + path_to_pointing_frame = directory / "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) + # 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) + # https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.sce2c + # Convert start and end times to SCLK. + sclk_begtim = spice.sce2c(int(id_imap_sclk), et_start) + sclk_endtim = spice.sce2c(int(id_imap_sclk), et_end) + + # Get the pointing frame ID. + # https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.gipool + id_imap_dps = spice.gipool("FRAME_IMAP_DPS", 0, 1) + + # Create the pointing frame kernel. + # https://spiceypy.readthedocs.io/en/main/documentation.html#spiceypy.spiceypy.ckw02 + spice.ckw02( + # Handle of an open CK file. + handle, + # Start time of the segment. + sclk_begtim, + # End time of the segment. + sclk_endtim, + # Pointing frame ID. + int(id_imap_dps), + # Reference frame. + "ECLIPJ2000", # Reference frame + # Identifier. + "IMAP_DPS", + # Number of pointing intervals. + 1, + # Start times of individual pointing records within segment. + # Since there is only a single record this is equal to sclk_begtim. + np.array([sclk_begtim]), + # End times of individual pointing records within segment. + # Since there is only a single record this is equal to sclk_endtim. + np.array([sclk_endtim]), # Single stop time + # Average quaternion. + q_avg, + # 0.0 Angular rotation terms. + np.array([0.0, 0.0, 0.0]), + # Rates (seconds per tick) at which the quaternion and + # angular velocity change. + np.array([1.0]), + ) + + # Close CK file. + spice.ckcls(handle) return path_to_pointing_frame diff --git a/imap_processing/tests/spice/test_kernels.py b/imap_processing/tests/spice/test_kernels.py index 9f72271b2..63155f875 100644 --- a/imap_processing/tests/spice/test_kernels.py +++ b/imap_processing/tests/spice/test_kernels.py @@ -16,6 +16,29 @@ ) +@pytest.fixture() +def kernels(spice_test_data_path): + """Furnish SPICE kernels for the entire test module.""" + kernels = [str(file) for file in spice_test_data_path.iterdir()] + return kernels + + +@pytest.fixture() +def et_times(kernels): + """Tests get_et_times function.""" + spice.furnsh(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 + + @ensure_spice def single_wrap_et2utc(et, fmt, prec): """Directly decorate a spice function with ensure_spice for use in tests""" @@ -78,47 +101,10 @@ def test_ensure_spice_key_error(): _ = wrapped(577365941.184, "ISOC", 3) == "2018-04-18T23:24:31.998" -@pytest.fixture() -def kernels(spice_test_data_path): - """Create kernel list.""" - kernels = [str(file) for file in spice_test_data_path.iterdir()] - - return kernels - - -@pytest.fixture() -def ck_kernel(spice_test_data_path): - """Location of ck kernel to create pointing kernel from.""" - return spice_test_data_path / "imap_spin.bc" - - -@pytest.fixture() -def et_times(ck_kernel, kernels): - """Tests get_et_times function.""" - - with spice.KernelPool(kernels): - et_start, et_end, et_times = _get_et_times(str(ck_kernel)) - - return et_times - - -def test_get_et_times(kernels, ck_kernel): - """Tests get_et_times function.""" - - with spice.KernelPool(kernels): - et_start, et_end, et_times = _get_et_times(str(ck_kernel)) - - assert et_start == 802008069.184905 - assert et_end == 802015267.184906 - assert et_times[0] == et_start - assert et_times[-1] == et_end - - def test_average_quaternions(et_times, kernels): """Tests average_quaternions function.""" - - with spice.KernelPool(kernels): - q_avg = _average_quaternions(et_times) + spice.furnsh(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]) @@ -127,11 +113,10 @@ def test_average_quaternions(et_times, kernels): def test_create_rotation_matrix(et_times, kernels): """Tests create_rotation_matrix function.""" - - with spice.KernelPool(kernels): - rotation_matrix = _create_rotation_matrix(et_times) - q_avg = _average_quaternions(et_times) - z_avg = spice.q2m(list(q_avg))[:, 2] + spice.furnsh(kernels) + rotation_matrix = _create_rotation_matrix(et_times) + q_avg = _average_quaternions(et_times) + z_avg = spice.q2m(list(q_avg))[:, 2] rotation_matrix_expected = np.array( [[0.0000, 0.0000, 1.0000], [0.9104, -0.4136, 0.0000], [0.4136, 0.9104, 0.0000]] @@ -142,19 +127,19 @@ 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(monkeypatch, spice_test_data_path, ck_kernel, tmp_path): +def test_create_pointing_frame(spice_test_data_path, kernels): """Tests create_pointing_frame function.""" - monkeypatch.setenv("EFS_MOUNT_PATH", str(spice_test_data_path)) + spice.furnsh(kernels) + ck_kernel, _, _, _ = spice.kdata(0, "ck") + et_start, et_end, et_times = _get_et_times(ck_kernel) create_pointing_frame() # After imap_dps.bc has been created. - kernels = [str(file) for file in spice_test_data_path.iterdir()] - - with spice.KernelPool(kernels): - et_start, et_end, et_times = _get_et_times(str(ck_kernel)) + dps_kernel = str(spice_test_data_path / "imap_dps.bc") - rotation_matrix_1 = spice.pxform("ECLIPJ2000", "IMAP_DPS", et_start + 100) - rotation_matrix_2 = spice.pxform("ECLIPJ2000", "IMAP_DPS", et_start + 1000) + spice.furnsh(dps_kernel) + rotation_matrix_1 = spice.pxform("ECLIPJ2000", "IMAP_DPS", et_start + 100) + rotation_matrix_2 = spice.pxform("ECLIPJ2000", "IMAP_DPS", et_start + 1000) # All the rotation matrices should be the same. assert np.array_equal(rotation_matrix_1, rotation_matrix_2)