2222 get_scattering_thresholds ,
2323)
2424from imap_processing .ultra .l1b .quality_flag_filters import DE_QUALITY_FLAG_FILTERS
25+ from imap_processing .ultra .l1c .l1c_lookup_utils import build_energy_bins
2526
2627logging .basicConfig (level = logging .INFO )
2728logger = logging .getLogger (__name__ )
@@ -514,7 +515,13 @@ def flag_scattering(
514515 Quality flags.
515516 """
516517 scattering_thresholds = get_scattering_thresholds (ancillary_files )
517-
518+ _ , _ , energy_bin_geometric_means = build_energy_bins ()
519+ energy_bin_inds = np .digitize (tof_energy , UltraConstants .PSET_ENERGY_BIN_EDGES )
520+ # Clip indices to valid range (events outside the energy bins get assigned
521+ # to the nearest bin. These events have already been flagged and
522+ # will be ignored in l1c)
523+ energy_bin_inds = np .clip (energy_bin_inds , 1 , len (energy_bin_geometric_means ))
524+ energy_geom_means = energy_bin_geometric_means [energy_bin_inds - 1 ]
518525 for (e_min , e_max ), threshold in scattering_thresholds .items ():
519526 event_mask = (tof_energy >= e_min ) & (tof_energy < e_max )
520527 # Input the theta and phi values for the current energy range.
@@ -528,8 +535,11 @@ def flag_scattering(
528535 )
529536 # FWHM_PHI = A_PHI * E^G_PHI
530537 # FWHM_THETA = A_THETA * E^G_THETA
531- fwhm_theta = theta_coeffs [:, 0 ] * tof_energy [event_mask ] ** theta_coeffs [:, 1 ]
532- fwhm_phi = phi_coeffs [:, 0 ] * tof_energy [event_mask ] ** phi_coeffs [:, 1 ]
538+ # Use the geometric mean of the energy bin for the scattering check
539+ fwhm_theta = (
540+ theta_coeffs [:, 0 ] * energy_geom_means [event_mask ] ** theta_coeffs [:, 1 ]
541+ )
542+ fwhm_phi = phi_coeffs [:, 0 ] * energy_geom_means [event_mask ] ** phi_coeffs [:, 1 ]
533543 is_nan = np .isnan (fwhm_theta ) | np .isnan (fwhm_phi )
534544 quality_flags [np .where (event_mask )[0 ][is_nan ]] |= (
535545 ImapDEScatteringUltraFlags .NAN_PHI_OR_THETA .value
0 commit comments