Skip to content
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
10 changes: 10 additions & 0 deletions imap_processing/spice/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import spiceypy
from numpy.typing import NDArray

from imap_processing.mag import constants

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -317,6 +319,14 @@ def frame_transform(
# Multiple et/positions : (n, 3, 3),(n, 3, 1) -> (n, 3, 1)
result = np.squeeze(rotate @ position[..., np.newaxis])

# For every FILLVAL in the input position, ensure the output is also NaN or FILLVAL
if np.isnan(position).any() or (position == constants.FILLVAL).any():
result = np.where(
np.isnan(position) | (position == constants.FILLVAL),
constants.FILLVAL,
result,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, no! This is MAG specific code in the common geometry function. If the input is a NaN, this function already behaves as expected. If the input is a MAG specific FILLVAL, then that case should be handled explicitly in the MAG code, not the general geometry module. @maxinelasp please revert this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoot, good catch Tim! I assumed this was in the rotate frames function in the mag code and totally missed the file. Sorry! I will revert and put up a proper fix.

return result


Expand Down
20 changes: 20 additions & 0 deletions imap_processing/tests/spice/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
import spiceypy

from imap_processing.mag import constants
from imap_processing.spice.geometry import (
SpiceBody,
SpiceFrame,
Expand Down Expand Up @@ -159,6 +160,19 @@ def test_get_spacecraft_to_instrument_spin_phase_offset(
SpiceFrame.IMAP_SPACECRAFT,
SpiceFrame.IMAP_DPS,
),
# single et, single NaN/FILL_VAL vector
(
["2025-04-30T12:00:00.000"],
np.array(
[
[0, 0, 0],
[constants.FILLVAL, constants.FILLVAL, constants.FILLVAL],
[np.nan, np.nan, np.nan],
]
),
SpiceFrame.IMAP_SPACECRAFT,
SpiceFrame.IMAP_DPS,
),
],
)
def test_frame_transform(et_strings, position, from_frame, to_frame, furnish_kernels):
Expand Down Expand Up @@ -203,6 +217,12 @@ def test_frame_transform(et_strings, position, from_frame, to_frame, furnish_ker
spice_result = spiceypy.mxv(rotation_matrix, spice_position)
np.testing.assert_allclose(test_result, spice_result, atol=1e-12)

# Ensure that NaN/FILL_VAL inputs are preserved exactly as FILL_VAL outputs
# and not just really close to but not quite FILL_VAL
for input_vec, output_vec in zip(position, result, strict=False):
if np.isnan(input_vec).all() or (input_vec == constants.FILLVAL).all():
assert (output_vec == constants.FILLVAL).all()


@pytest.mark.parametrize(
"spice_frame",
Expand Down
Loading