Skip to content

Commit 9e5c39b

Browse files
committed
MAG L1C: harden previous-day handling per independent review
- Ignore (with a warning) a previous day dataset that has no epoch variable instead of raising KeyError. - Log skipped inheritance (too few pre-midnight samples, unknown cadence) at warning level to match the other unusable-neighbor paths. - Select the previous-day file by the job descriptor (norm + sensor) rather than "norm" alone, so a wrong-sensor file can never shadow the right one. - Drop the "exactly as before" claim from the mag_l1c docstring: the generate_missing_timestamps precision fix means gap fills at 8+ vec/s can differ from previously generated products by up to 128 ns even without a previous-day file.
1 parent ac9fdb5 commit 9e5c39b

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

imap_processing/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ def do_processing( # noqa: PLR0912
13601360
for path in dependencies.get_valid_inputs_for_start_date(
13611361
start_datetime - timedelta(days=1)
13621362
).get_file_paths(source="mag", data_type="l1b")
1363-
if "norm" in path.name
1363+
if self.descriptor in path.name
13641364
]
13651365
previous_day_dataset = (
13661366
load_cdf(previous_day_files[0]) if previous_day_files else None

imap_processing/mag/l1c/mag_l1c.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def mag_l1c(
4646
current day opens with a gap, timestamps generated for that gap continue the
4747
previous day's cadence and phase so the L1C timeline stays regular across the
4848
day boundary. If not provided (or not usable), gaps at the start of the day
49-
are filled on the current day's own grid, exactly as before.
49+
are filled on the current day's own grid.
5050
5151
Returns
5252
-------
@@ -321,7 +321,10 @@ def _validated_previous_day(
321321
f"expected normal mode L1B data for sensor mag{sensor}."
322322
)
323323
return None
324-
if previous_day_dataset["epoch"].data.size == 0:
324+
if (
325+
"epoch" not in previous_day_dataset
326+
or previous_day_dataset["epoch"].data.size == 0
327+
):
325328
logger.warning("Ignoring previous day dataset with no epochs.")
326329
return None
327330
return previous_day_dataset
@@ -390,7 +393,7 @@ def _previous_day_grid(
390393
previous_epochs = previous_day_dataset["epoch"].data
391394
anchor_index = int(np.searchsorted(previous_epochs, midnight_ns, side="left")) - 1
392395
if anchor_index < 1:
393-
logger.info(
396+
logger.warning(
394397
"Previous day dataset has fewer than two samples before the current day; "
395398
"not inheriting its timeline."
396399
)
@@ -407,7 +410,7 @@ def _previous_day_grid(
407410
rate = vecsec.value
408411
break
409412
if rate is None:
410-
logger.info(
413+
logger.warning(
411414
f"Previous day dataset ends with sample spacing {anchor_spacing} ns, "
412415
f"which matches no known MAG rate; not inheriting its timeline."
413416
)

imap_processing/tests/mag/test_mag_l1c.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,3 +1377,16 @@ def test_process_mag_l1c_previous_day_anchor_ignores_buffer_samples():
13771377
assert leading.size > 0
13781378
assert leading[0] == anchor + cadence
13791379
assert np.all((leading - anchor) % cadence == 0)
1380+
1381+
1382+
def test_mag_l1c_ignores_previous_day_without_epochs():
1383+
"""A previous day dataset with no epoch variable is ignored, not raised."""
1384+
day1 = np.datetime64("2025-01-01")
1385+
day2 = np.datetime64("2025-01-02")
1386+
_, norm_day2, burst_day2, _ = _build_cross_day_l1b(day1, day2)
1387+
no_epochs = xr.Dataset(attrs={"Logical_source": "imap_mag_l1b_norm-mago"})
1388+
1389+
baseline = mag_l1c(norm_day2, day2, burst_day2)
1390+
output = mag_l1c(norm_day2, day2, burst_day2, previous_day_dataset=no_epochs)
1391+
1392+
assert np.array_equal(output["epoch"].data, baseline["epoch"].data)

0 commit comments

Comments
 (0)