Skip to content

MAG L1D index error fix#3159

Open
maxinelasp wants to merge 3 commits intoIMAP-Science-Operations-Center:devfrom
maxinelasp:mag2832-index-error
Open

MAG L1D index error fix#3159
maxinelasp wants to merge 3 commits intoIMAP-Science-Operations-Center:devfrom
maxinelasp:mag2832-index-error

Conversation

@maxinelasp
Copy link
Copy Markdown
Contributor

Change Summary

Addresses #2832. Just adds a check in the chunk generation to see if it's empty before continuing. I think the chunk can very rarely be empty in some cases if the spin inputs aren't what we expected. If the chunk is empty, it should be fine to skip past it and continue on. We already skip past chunks with too many NaNs, so it's an expected use case.

Overview

Just added a simple if and continue flow if the expected epoch list is empty.

File changes

@maxinelasp maxinelasp added this to the May 2026 milestone May 7, 2026
@maxinelasp maxinelasp self-assigned this May 7, 2026
@maxinelasp maxinelasp added the Ins: MAG Related to the MAG instrument label May 7, 2026
@maxinelasp maxinelasp requested a review from Copilot May 7, 2026 20:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a rare IndexError in MAG L1D spin-offset computation by skipping over chunk windows that unexpectedly contain no epochs, preventing downstream indexing into an empty chunk_epoch.

Changes:

  • Add an empty-chunk guard in calculate_spin_offsets() to skip processing when chunk_epoch is empty.
  • Emit a warning when an empty chunk is encountered to aid debugging/traceability.
Comments suppressed due to low confidence (1)

imap_processing/mag/l1d/mag_l1d_data.py:542

  • np.nanmean is computed before the validity checks; when a chunk is all-NaN this still triggers RuntimeWarnings ("Mean of empty slice") even though the values are later discarded/replaced. Consider moving the nanmean calls to after the valid-count/size checks (or guarding them) to avoid noisy warnings and unnecessary work.
            # Check if more than half of the chunk data is NaN before processing
            x_valid_count: int = int(np.sum(~np.isnan(chunk_vectors[:, 0])))
            y_valid_count: int = int(np.sum(~np.isnan(chunk_vectors[:, 1])))
            total_points = len(chunk_vectors)

            # average the x and y axes (z is fixed, as the spin axis)
            avg_x = np.nanmean(chunk_vectors[:, 0])
            avg_y = np.nanmean(chunk_vectors[:, 1])


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread imap_processing/mag/l1d/mag_l1d_data.py Outdated
Comment thread imap_processing/mag/l1d/mag_l1d_data.py
Comment thread imap_processing/mag/l1d/mag_l1d_data.py
@maxinelasp maxinelasp requested a review from sapols May 7, 2026 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ins: MAG Related to the MAG instrument

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants