66
77from imap_processing import imap_module_directory
88from imap_processing .cdf .utils import load_cdf , write_cdf
9+ from imap_processing .quality_flags import ImapDEOutliersUltraFlags
10+ from imap_processing .ultra .l1b .de import FILLVAL_FLOAT32
911from imap_processing .ultra .l1b .ultra_l1b import ultra_l1b
1012from imap_processing .ultra .utils .ultra_l1_utils import create_dataset
1113
@@ -71,6 +73,29 @@ def mock_data_l1b_extendedspin_dict():
7173 return data_dict
7274
7375
76+ @pytest .fixture
77+ def mock_get_annotated_particle_velocity ():
78+ """
79+ Mock behavior of get_annotated_particle_velocity.
80+
81+ Returns NaN-filled arrays matching the expected output shape.
82+ """
83+
84+ def side_effect_func (event_times , position , ultra_frame , dps_frame , sc_frame ):
85+ num_events = event_times .size
86+ return (
87+ np .full ((num_events , 3 ), np .nan ), # sc_velocity
88+ np .full ((num_events , 3 ), np .nan ), # sc_dps_velocity
89+ np .full ((num_events , 3 ), np .nan ), # helio_velocity
90+ )
91+
92+ with mock .patch (
93+ "imap_processing.ultra.l1b.de.get_annotated_particle_velocity"
94+ ) as mocked_func :
95+ mocked_func .side_effect = side_effect_func
96+ yield mocked_func
97+
98+
7499def test_create_extendedspin_dataset (mock_data_l1b_extendedspin_dict ):
75100 """Tests that dataset is created as expected."""
76101 dataset = create_dataset (
@@ -101,13 +126,12 @@ def test_create_de_dataset(mock_data_l1b_de_dict):
101126
102127
103128@pytest .mark .external_test_data
104- @mock .patch ("imap_processing.ultra.l1b.de.get_annotated_particle_velocity" )
105129def test_cdf_de (
106- mock_get_annotated_particle_velocity ,
107130 de_dataset ,
108131 use_fake_spin_data_for_time ,
109132 ancillary_files ,
110133 use_fake_repoint_data_for_time ,
134+ mock_get_annotated_particle_velocity ,
111135):
112136 """Tests that CDF file is created and contains same attributes as xarray."""
113137
@@ -118,22 +142,6 @@ def test_cdf_de(
118142 use_fake_spin_data_for_time (511000000 , 511000000 + 86400 * 5 )
119143 use_fake_repoint_data_for_time (np .arange (511000000 , 511000000 + 86400 * 5 , 86400 ))
120144
121- # Mock get_annotated_particle_velocity to avoid needing kernels
122- def side_effect_func (event_times , position , ultra_frame , dps_frame , sc_frame ):
123- """
124- Mock behavior of get_annotated_particle_velocity.
125-
126- Returns NaN-filled arrays matching the expected output shape.
127- """
128- num_events = event_times .size
129- return (
130- np .full ((num_events , 3 ), np .nan ), # sc_velocity
131- np .full ((num_events , 3 ), np .nan ), # sc_dps_velocity
132- np .full ((num_events , 3 ), np .nan ), # helio_velocity
133- )
134-
135- mock_get_annotated_particle_velocity .side_effect = side_effect_func
136-
137145 l1b_de_dataset = ultra_l1b (data_dict , ancillary_files )
138146
139147 assert (
@@ -151,6 +159,31 @@ def side_effect_func(event_times, position, ultra_frame, dps_frame, sc_frame):
151159 )
152160
153161
162+ @pytest .mark .external_test_data
163+ def test_cdf_de_flags (
164+ mock_get_annotated_particle_velocity ,
165+ de_dataset ,
166+ use_fake_spin_data_for_time ,
167+ ancillary_files ,
168+ use_fake_repoint_data_for_time ,
169+ ):
170+ """Tests that the de code flags events not in a repointing."""
171+ data_dict = {}
172+ de_dataset .attrs ["Repointing" ] = "repoint00000"
173+ data_dict [de_dataset .attrs ["Logical_source" ]] = de_dataset
174+ # Create a spin table that cover spin 0-141
175+ use_fake_spin_data_for_time (511000000 , 511000000 + 86400 * 5 )
176+ # Use repoint data that will NOT cover the event times to test flag setting
177+ use_fake_repoint_data_for_time (np .arange (0 , + 86400 * 5 , 86400 ))
178+
179+ l1b_de_dataset = ultra_l1b (data_dict , ancillary_files )
180+ # All valid events should be flagged as DURINGREPOINT since the repoint data does
181+ # not cover any of the event times
182+ valid_events = l1b_de_dataset [0 ]["event_times" ] != FILLVAL_FLOAT32
183+ flags = l1b_de_dataset [0 ]["quality_outliers" ].values [valid_events ]
184+ assert np .all ((flags & ImapDEOutliersUltraFlags .DURINGREPOINT .value ) != 0 )
185+
186+
154187@pytest .mark .external_test_data
155188def test_ultra_l1b_extendedspin (
156189 use_fake_spin_data_for_time , faux_aux_dataset , rates_dataset
0 commit comments