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
7 changes: 5 additions & 2 deletions imap_processing/tests/ultra/unit/test_ultra_l1b_culling.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
get_energy_histogram,
get_n_sigma,
get_pulses_per_spin,
get_spin_and_duration,
get_spin_data,
)
from imap_processing.ultra.l1b.ultra_l1b_extended import get_spin_info

TEST_PATH = imap_module_directory / "tests" / "ultra" / "data" / "l1"

Expand Down Expand Up @@ -185,6 +185,7 @@ def test_get_duration(rates_l1_test_path, use_fake_spin_data_for_time):
aux_ds = xr.Dataset(
data_vars={
"timespinstart": ("epoch", spin_start_times),
"timespinstartsub": ("epoch", np.ones_like(spin_start_times)),
"duration": ("epoch", np.full(num_spins, 15)),
"spinnumber": ("epoch", spin_numbers),
},
Expand All @@ -193,7 +194,9 @@ def test_get_duration(rates_l1_test_path, use_fake_spin_data_for_time):

met = df["TimeTag"] - df["TimeTag"].values[0]
spin = df["Spin"]
spin_number, duration = get_spin_and_duration(aux_ds, met)
spin_ds = get_spin_info(aux_ds, met)
spin_number = spin_ds["spin_number"].values
duration = spin_ds["spin_duration"].values
assert np.array_equal(spin, spin_number)
assert np.all(duration == 15)

Expand Down
44 changes: 25 additions & 19 deletions imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
get_path_length,
get_ph_tof_and_back_positions,
get_phi_theta,
get_spin_and_duration,
get_spin_info,
get_ssd_back_position_and_tof_offset,
get_ssd_tof,
interpolate_fwhm,
Expand Down Expand Up @@ -524,8 +524,8 @@ def test_get_eventtimes(test_fixture, aux_dataset):

event_times, spin_start_times = get_event_times(
aux_dataset,
de_dataset["phase_angle"].values,
de_dataset["shcoarse"].values,
de_dataset["phase_angle"].values,
)

# Check shapes
Expand Down Expand Up @@ -561,44 +561,50 @@ def test_get_eventtimes(test_fixture, aux_dataset):

@pytest.mark.external_test_data
def test_get_spin_and_duration(test_fixture, aux_dataset):
"""Tests get_spin_and_duration function."""
"""Tests get_spin_info function."""
df_filt, _, _, de_dataset = test_fixture

spin_number, spin_duration = get_spin_and_duration(
spin_ds = get_spin_info(
aux_dataset,
de_dataset["shcoarse"].values,
)

# Check shapes
assert spin_number.shape == spin_duration.shape == de_dataset["shcoarse"].shape
assert (
spin_ds.spin_number.shape
== spin_ds.spin_duration.shape
== de_dataset["shcoarse"].shape
)

t1_spin_number = aux_dataset["spinnumber"].values[0]
t1_start_dur = aux_dataset["duration"].values[0]
# Check the first event spin number and duration
assert spin_number[0] == t1_spin_number
assert spin_duration[0] == t1_start_dur
assert spin_ds.spin_number[0] == t1_spin_number
assert spin_ds.spin_duration[0] == t1_start_dur


@pytest.mark.external_test_data
def test_get_event_times_out_of_range(test_fixture, aux_dataset):
def test_get_event_times_out_of_range(
test_fixture, aux_dataset, use_fake_spin_data_for_time
):
"""Tests get_event_times with out of range values."""
df_filt, _, _, de_dataset = test_fixture
# Get min time from aux_dataset
min_time = aux_dataset["timespinstart"].values.min()
# Set some coarse times to be out of range (less than min_time)
coarse_times = de_dataset["shcoarse"].values.copy()
# Set first coarse time to be out of range
# Set first coarse time to be out of range of the aux data
coarse_times[0] = min_time - 1000

with pytest.raises(
ValueError,
match="Coarse MET time contains events outside aux_dataset time range",
):
get_event_times(
aux_dataset,
de_dataset["phase_angle"].values,
coarse_times,
)
# set spin data that DOES cover the range of coarse_times
use_fake_spin_data_for_time(min_time - 1000, min_time + 10000)
# This should not raise an error.
event_times, spin_starts = get_event_times(
aux_dataset,
coarse_times,
de_dataset["phase_angle"].values,
)
assert event_times.shape == coarse_times.shape
assert spin_starts.shape == coarse_times.shape


@pytest.mark.external_test_data
Expand Down
12 changes: 8 additions & 4 deletions imap_processing/tests/ultra/unit/test_ultra_l1c_pset_bins.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_get_deadtime_ratios():
assert np.all(deadtime_correction_factors >= 0)


def test_get_deadtime_interpolator(use_fake_spin_data_for_time):
def test_get_deadtime_interpolator(use_fake_spin_data_for_time, aux_dataset):
"""Tests get_deadtime_correction_factors function."""
use_fake_spin_data_for_time(1, 10)
sector_rate_seconds = 20 * 60 # 20 minutes in seconds
Expand All @@ -223,7 +223,7 @@ def test_get_deadtime_interpolator(use_fake_spin_data_for_time):
return_value=deadtime_ratios,
):
deadtime_ratios = get_deadtime_ratios_by_spin_phase(
sectored_rates_ds, spin_steps=num_deadtimes
sectored_rates_ds, aux_dataset, spin_steps=num_deadtimes
)
np.testing.assert_array_equal(deadtime_ratios.shape, (num_deadtimes))

Expand All @@ -237,12 +237,12 @@ def test_get_deadtime_interpolator(use_fake_spin_data_for_time):
match="All dead time ratios are NaN, cannot interpolate",
):
get_deadtime_ratios_by_spin_phase(
sectored_rates_ds, spin_steps=num_deadtimes
sectored_rates_ds, aux_dataset, spin_steps=num_deadtimes
)


@pytest.mark.external_test_data
def test_get_deadtime_interpolator_no_sectored_rates(ancillary_files):
def test_get_deadtime_interpolator_no_sectored_rates(ancillary_files, aux_dataset):
"""Tests get_deadtime_correction_factors function."""

num_deadtimes = 15000 # Standard number of spin phases
Expand All @@ -251,6 +251,7 @@ def test_get_deadtime_interpolator_no_sectored_rates(ancillary_files):
# static deadtime ratios lookup.
dt_ratios = get_deadtime_ratios_by_spin_phase(
sectored_rates=None,
aux_dataset=aux_dataset,
spin_steps=num_deadtimes,
sensor_id=sensor,
ancillary_files=ancillary_files,
Expand Down Expand Up @@ -393,6 +394,7 @@ def test_get_spacecraft_exposure_times(
imap_ena_sim_metakernel,
ancillary_files,
use_fake_spin_data_for_time,
aux_dataset,
):
"""Test get_spacecraft_exposure_times function."""
data_start_time = 445015665.0
Expand All @@ -419,6 +421,7 @@ def test_get_spacecraft_exposure_times(
rates_dataset,
pixels_below_threshold,
boundary_sf,
aux_dataset,
(
data_start_time,
data_start_time,
Expand Down Expand Up @@ -446,6 +449,7 @@ def test_get_spacecraft_background_rates(
aux_ds = xr.Dataset(
data_vars={
"timespinstart": ("epoch", spin_start_times),
"timespinstartsub": ("epoch", np.ones_like(spin_start_times)),
"duration": ("epoch", np.full(num_spins, 15)),
"spinnumber": ("epoch", spin_numbers),
},
Expand Down
3 changes: 2 additions & 1 deletion imap_processing/ultra/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,5 @@ class UltraConstants:

# For spatiotemporal culling
EARTH_RADIUS_KM: float = 6378.1
DEFAULT_EARTH_CULLING_RADIUS = EARTH_RADIUS_KM * 30
N_RE = 50
DEFAULT_EARTH_CULLING_RADIUS = EARTH_RADIUS_KM * N_RE
11 changes: 7 additions & 4 deletions imap_processing/ultra/l1b/de.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
get_path_length,
get_ph_tof_and_back_positions,
get_phi_theta,
get_spin_and_duration,
get_spin_info,
get_ssd_back_position_and_tof_offset,
get_ssd_tof,
is_back_tof_valid,
Expand Down Expand Up @@ -158,15 +158,18 @@ def calculate_de(
f"ultra{sensor}",
ancillary_files,
)

start_type[valid_indices] = de_dataset["start_type"].data[valid_indices]
spin_ds = get_spin_info(aux_dataset, de_dataset["shcoarse"].data)

(event_times, spin_starts) = get_event_times(
aux_dataset,
de_dataset["phase_angle"].data,
de_dataset["shcoarse"].data,
de_dataset["phase_angle"].data,
spin_ds,
)
spin_number, _ = get_spin_and_duration(aux_dataset, de_dataset["shcoarse"].data)

de_dict["spin"] = spin_number
de_dict["spin"] = spin_ds.spin_number.data
de_dict["event_times"] = event_times.astype(np.float64)
# Pulse height
ph_result = get_ph_tof_and_back_positions(
Expand Down
5 changes: 3 additions & 2 deletions imap_processing/ultra/l1b/ultra_l1b_culling.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
get_scattering_thresholds,
)
from imap_processing.ultra.l1b.quality_flag_filters import DE_QUALITY_FLAG_FILTERS
from imap_processing.ultra.l1b.ultra_l1b_extended import get_spin_and_duration
from imap_processing.ultra.l1b.ultra_l1b_extended import get_spin_info
from imap_processing.ultra.l1c.l1c_lookup_utils import build_energy_bins

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -368,7 +368,8 @@ def get_pulses_per_spin(aux: xr.Dataset, rates: xr.Dataset) -> RateResult:
coin_pulses : NDArray
Total coincidence pulses.
"""
spin_number, _duration = get_spin_and_duration(aux, rates["shcoarse"].values)
spin_ds = get_spin_info(aux, rates["shcoarse"].values)
spin_number = spin_ds["spin_number"].values

# Top coin pulses
top_coin_pulses = np.stack(
Expand Down
Loading