Skip to content

Commit 0130bd4

Browse files
authored
ULTRA l1b coinph valid and backtof valid flags (#2467)
* add more flags l1b
1 parent 10e19f4 commit 0130bd4

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

imap_processing/quality_flags.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ImapDEOutliersUltraFlags(FlagNameMixin):
4646
COINPH = 2**2 # bit 2 # Event validity
4747
INVALID_ENERGY = 2**3 # bit 3
4848
DURINGREPOINT = 2**4 # bit 4 # event during a repointing
49+
BACKTOF = 2**5 # bit 5 # Back TOF outlier
4950

5051

5152
class ImapHkUltraFlags(FlagNameMixin):

imap_processing/tests/ultra/unit/test_ultra_l1b_extended.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,15 +734,24 @@ def test_is_back_tof_valid(test_fixture, ancillary_files):
734734
df_filt, _, _, de_dataset = test_fixture
735735
df_ph = df_filt[np.isin(df_filt["StopType"], [StopType.PH.value])]
736736

737-
valid = is_back_tof_valid(
738-
de_dataset,
739-
df_filt.Xf.astype("float").values,
737+
# Get the ph inds
738+
ph_mask = (
739+
(de_dataset["stop_type"] == StopType.Top.value)
740+
| (de_dataset["stop_type"] == StopType.Bottom.value)
741+
).values
742+
quality_flags = np.zeros(len(np.nonzero(ph_mask)[0]), dtype=np.uint16)
743+
valid, quality_flags = is_back_tof_valid(
744+
de_dataset.isel(epoch=np.nonzero(ph_mask)[0]),
745+
df_filt.Xf.astype("float").values[ph_mask],
740746
"ultra45",
741747
ancillary_files,
748+
quality_flags,
742749
)
750+
743751
back_tof_valid_bool = df_ph["BackTOFValid"].astype(int).astype(bool).values
744752

745753
np.testing.assert_equal(back_tof_valid_bool, valid)
754+
assert np.any(quality_flags)
746755

747756

748757
@pytest.mark.external_test_data
@@ -764,7 +773,7 @@ def test_is_coin_ph_valid(test_fixture, ancillary_files):
764773
len(ctof), ImapDEOutliersUltraFlags.NONE.value, dtype=np.uint16
765774
)
766775

767-
combined_mask = is_coin_ph_valid(
776+
combined_mask, quality_flags = is_coin_ph_valid(
768777
etof,
769778
xc,
770779
xb,

imap_processing/ultra/l1b/de.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,17 @@ def calculate_de(
209209
f"ultra{sensor}",
210210
ancillary_files,
211211
)
212-
backtofvalid = is_back_tof_valid(
213-
de_dataset,
214-
xf,
212+
backtofvalid_quality_flags = np.zeros(len(ph_indices), dtype=quality_flags.dtype)
213+
backtofvalid, backtofvalid_quality_flags = is_back_tof_valid(
214+
de_dataset.isel(epoch=ph_indices),
215+
xf[ph_indices],
215216
f"ultra{sensor}",
216217
ancillary_files,
218+
backtofvalid_quality_flags,
217219
)
218-
coinphvalid = is_coin_ph_valid(
220+
221+
coinphvalid_quality_flags = np.zeros(len(ph_indices), dtype=quality_flags.dtype)
222+
coinphvalid, coinphvalid_quality_flags = is_coin_ph_valid(
219223
etof[ph_indices],
220224
xc[ph_indices],
221225
xb[ph_indices],
@@ -225,8 +229,11 @@ def calculate_de(
225229
de_dataset["stop_west_tdc"][ph_indices].values,
226230
f"ultra{sensor}",
227231
ancillary_files,
228-
quality_flags[ph_indices],
232+
coinphvalid_quality_flags,
229233
)
234+
quality_flags[ph_indices] |= coinphvalid_quality_flags
235+
quality_flags[ph_indices] |= backtofvalid_quality_flags
236+
230237
e_bin[ph_indices] = determine_ebin_pulse_height(
231238
energy[ph_indices],
232239
tof[ph_indices],

imap_processing/ultra/l1b/quality_flag_filters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"quality_outliers": [
2020
ImapDEOutliersUltraFlags.FOV,
2121
ImapDEOutliersUltraFlags.DURINGREPOINT,
22+
ImapDEOutliersUltraFlags.COINPH,
23+
ImapDEOutliersUltraFlags.BACKTOF,
2224
],
2325
"quality_scattering": [
2426
ImapDEScatteringUltraFlags.ABOVE_THRESHOLD,

imap_processing/ultra/l1b/ultra_l1b_extended.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,8 @@ def is_back_tof_valid(
13421342
xf: NDArray,
13431343
sensor: str,
13441344
ancillary_files: dict,
1345-
) -> NDArray:
1345+
quality_flags: NDArray,
1346+
) -> tuple[NDArray, NDArray]:
13461347
"""
13471348
Determine whether back TOF is valid based on stop type.
13481349
@@ -1357,11 +1358,15 @@ def is_back_tof_valid(
13571358
Sensor name: "ultra45" or "ultra90".
13581359
ancillary_files : dict
13591360
Ancillary files for lookup.
1361+
quality_flags : NDArray
1362+
Quality flag to set when there is an outlier.
13601363
13611364
Returns
13621365
-------
13631366
valid_mask : NDArray
13641367
Boolean array indicating whether back TOF is valid.
1368+
quality_flags : NDArray
1369+
Updated quality flags.
13651370
13661371
Notes
13671372
-----
@@ -1372,15 +1377,10 @@ def is_back_tof_valid(
13721377
)
13731378
diff = tofy - tofx
13741379

1375-
indices = np.nonzero(
1376-
np.isin(de_dataset["stop_type"], [StopType.Top.value, StopType.Bottom.value])
1377-
)[0]
1378-
de_ph = de_dataset.isel(epoch=indices)
1379-
1380-
top_mask = de_ph["stop_type"] == StopType.Top.value
1381-
bottom_mask = de_ph["stop_type"] == StopType.Bottom.value
1380+
top_mask = de_dataset["stop_type"] == StopType.Top.value
1381+
bottom_mask = de_dataset["stop_type"] == StopType.Bottom.value
13821382

1383-
valid = np.zeros_like(diff, dtype=bool)
1383+
valid = np.zeros(len(top_mask), dtype=bool)
13841384

13851385
diff_tp_min = get_image_params("TOFDiffTpMin", sensor, ancillary_files)
13861386
diff_tp_max = get_image_params("TOFDiffTpMax", sensor, ancillary_files)
@@ -1391,8 +1391,8 @@ def is_back_tof_valid(
13911391
valid[bottom_mask] = (diff[bottom_mask] >= diff_bt_min) & (
13921392
diff[bottom_mask] <= diff_bt_max
13931393
)
1394-
1395-
return valid
1394+
quality_flags[~valid] |= ImapDEOutliersUltraFlags.BACKTOF.value
1395+
return valid, quality_flags
13961396

13971397

13981398
def is_coin_ph_valid(
@@ -1406,7 +1406,7 @@ def is_coin_ph_valid(
14061406
sensor: str,
14071407
ancillary_files: dict,
14081408
quality_flags: NDArray,
1409-
) -> NDArray:
1409+
) -> tuple[NDArray, NDArray]:
14101410
"""
14111411
Determine event validity.
14121412
@@ -1438,6 +1438,8 @@ def is_coin_ph_valid(
14381438
-------
14391439
combined_mask : NDArray
14401440
Boolean array indicating whether back TOF is valid.
1441+
quality_flags : NDArray
1442+
Updated quality flags.
14411443
14421444
Notes
14431445
-----
@@ -1486,4 +1488,4 @@ def is_coin_ph_valid(
14861488

14871489
quality_flags[~combined_mask] |= ImapDEOutliersUltraFlags.COINPH.value
14881490

1489-
return combined_mask
1491+
return combined_mask, quality_flags

0 commit comments

Comments
 (0)