Skip to content

Implement fallback to linear interpolation in interpolate_map_flux_to…#3150

Merged
tmplummer merged 1 commit into
IMAP-Science-Operations-Center:devfrom
tmplummer:3149-hi-l2---update-interpolation-to-esa-energy-in-cg-correction
May 7, 2026
Merged

Implement fallback to linear interpolation in interpolate_map_flux_to…#3150
tmplummer merged 1 commit into
IMAP-Science-Operations-Center:devfrom
tmplummer:3149-hi-l2---update-interpolation-to-esa-energy-in-cg-correction

Conversation

@tmplummer
Copy link
Copy Markdown
Contributor

…_helio_frame method

Change Summary

Overview

Fixes #3149 - Adds linear interpolation fallback for zero-flux cases in the Compton-Getting correction.

When one or more fluxes are zero, the power-law interpolation in interpolate_map_flux_to_helio_frame produces NaN because log(0) is undefined. This PR detects these conditions and falls back to linear interpolation.

Changes

imap_processing/ena_maps/utils/corrections.py

Modified interpolate_map_flux_to_helio_frame to:

  • Detect when either bounding flux is zero or negative (use_linear = (flux_left <= 0) | (flux_right <= 0))
  • Compute linear interpolation as a fallback:
    • Flux: flux_left + (flux_right - flux_left) * f
    • Statistical uncertainty: sqrt((1-f)² * σ_left² + f² * σ_right²)
    • Systematic error: sys_err_left + (sys_err_right - sys_err_left) * f
  • Use xr.where() to select linear interpolation where needed, power-law otherwise
  • Clamp negative flux values to zero (can occur from linear extrapolation)

imap_processing/tests/ena_maps/test_corrections.py

Added 5 new test cases:

  • test_linear_fallback_when_flux_left_zero - Verifies correct interpolation when left bounding flux is zero
  • test_linear_fallback_when_flux_right_zero - Verifies correct interpolation when right bounding flux is zero
  • test_linear_fallback_when_both_fluxes_zero - Verifies result is zero (not NaN) when both bounds are zero
  • test_negative_interpolated_flux_clamped_to_zero - Verifies negative fluxes are clamped to zero
  • test_linear_and_powerlaw_mixed - Verifies correct method selection in mixed scenarios

Closes: #3149

Copy link
Copy Markdown
Contributor

@laspsandoval laspsandoval left a comment

Choose a reason for hiding this comment

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

Nice job. Thanks Tim!

# Allow for some numerical variation due to interpolation
np.testing.assert_allclose(ratio, 0.2, rtol=0.01)

def test_linear_fallback_when_flux_left_zero(self):
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.

Double check that clamping happens in this test. Claude says:

interp_fraction < 0 and flux_sc_linear stays positive. The assertions only confirm non-negativity of already-positive outputs, not that the clamp fires.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test is not meant to test the clamping. It tests that the linear interpolation fallback is used when the left flux value used for interpolation is zero. The clamping is tested below in the test_negative_interpolated_flux_clamped_to_zero test.

@tmplummer tmplummer merged commit b1dee10 into IMAP-Science-Operations-Center:dev May 7, 2026
18 checks passed
@tmplummer tmplummer deleted the 3149-hi-l2---update-interpolation-to-esa-energy-in-cg-correction branch May 7, 2026 16:36
@github-project-automation github-project-automation Bot moved this to Done in IMAP May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Hi L2 - update interpolation to ESA energy in CG correction

2 participants