File tree Expand file tree Collapse file tree 2 files changed +39
-4
lines changed Expand file tree Collapse file tree 2 files changed +39
-4
lines changed Original file line number Diff line number Diff line change @@ -67,12 +67,27 @@ def load_raw_event_data(
67
67
def load_crystal_rotation (
68
68
file_path : FilePath , instrument : McStasInstrument
69
69
) -> CrystalRotation :
70
- """Retrieve crystal rotation from the file."""
70
+ """Retrieve crystal rotation from the file.
71
+
72
+ Raises
73
+ ------
74
+ KeyError
75
+ If the crystal rotation is not found in the file.
76
+
77
+ """
71
78
with snx .File (file_path , 'r' ) as file :
72
- return sc .vector (
73
- value = [file [f"entry1/simulation/Param/XtalPhi{ key } " ][...] for key in "XYZ" ],
79
+ param_keys = tuple (f"entry1/simulation/Param/XtalPhi{ key } " for key in "XYZ" )
80
+ if not all (key in file for key in param_keys ):
81
+ raise KeyError (
82
+ f"Crystal rotations [{ ', ' .join (param_keys )} ] not found in file."
83
+ )
84
+
85
+ return CrystalRotation (
86
+ sc .vector (
87
+ value = [file [param_key ][...] for param_key in param_keys ],
74
88
unit = instrument .simulation_settings .angle_unit ,
75
89
)
90
+ )
76
91
77
92
78
93
def event_weights_from_probability (
Original file line number Diff line number Diff line change 10
10
import scippnexus as snx
11
11
from ess .nmx import default_parameters
12
12
from ess .nmx .data import small_mcstas_2_sample , small_mcstas_3_sample
13
- from ess .nmx .mcstas .load import bank_names_to_detector_names
13
+ from ess .nmx .mcstas .load import bank_names_to_detector_names , load_crystal_rotation
14
14
from ess .nmx .mcstas .load import providers as loader_providers
15
15
from ess .nmx .reduction import NMXData
16
16
from ess .nmx .types import (
@@ -192,6 +192,26 @@ def test_file_reader_mcstas_additional_fields(tmp_mcstas_file: pathlib.Path) ->
192
192
assert isinstance (dg , sc .DataGroup )
193
193
194
194
195
+ @pytest .fixture ()
196
+ def rotation_mission_tmp_file (tmp_mcstas_file : pathlib .Path ) -> pathlib .Path :
197
+ import h5py
198
+
199
+ param_keys = tuple (f"entry1/simulation/Param/XtalPhi{ key } " for key in "XYZ" )
200
+
201
+ # Remove the rotation parameters from the file.
202
+ with h5py .File (tmp_mcstas_file , 'a' ) as file :
203
+ for key in param_keys :
204
+ del file [key ]
205
+
206
+ return tmp_mcstas_file
207
+
208
+
209
+ def test_missing_rotation (rotation_mission_tmp_file : FilePath ) -> None :
210
+ with pytest .raises (KeyError , match = "XtalPhiX" ):
211
+ load_crystal_rotation (rotation_mission_tmp_file , None )
212
+ # McStasInstrument is not used due to error in the file.
213
+
214
+
195
215
def test_bank_names_to_detector_names_two_detectors ():
196
216
res = bank_names_to_detector_names (two_detectors_two_filenames )
197
217
assert len (res ) == 2
You can’t perform that action at this time.
0 commit comments