44Providers for the ISIS instruments.
55"""
66
7- from typing import NewType
7+ from dataclasses import dataclass
8+ from typing import Generic , NewType
89
910import sciline
1011import scipp as sc
@@ -45,6 +46,17 @@ class MonitorOffset(sciline.Scope[MonitorType, sc.Variable], sc.Variable):
4546 """
4647
4748
49+ @dataclass
50+ class MonitorSpectrumNumber (Generic [MonitorType ]):
51+ """
52+ Spectrum number for monitor data.
53+
54+ This is used to identify the monitor in ISIS histogram data files.
55+ """
56+
57+ value : int
58+
59+
4860DetectorBankOffset = NewType ('DetectorBankOffset' , sc .Variable )
4961SampleOffset = NewType ('SampleOffset' , sc .Variable )
5062
@@ -61,6 +73,10 @@ def default_parameters() -> dict:
6173 SampleOffset : SampleOffset (sc .vector ([0 , 0 , 0 ], unit = 'm' )),
6274 NonBackgroundWavelengthRange : None ,
6375 Period : None ,
76+ # Not used for event files, setting default so the workflow works. If histogram
77+ # data is used, the user should set this to the correct value.
78+ MonitorSpectrumNumber [Incident ]: MonitorSpectrumNumber [Incident ](- 1 ),
79+ MonitorSpectrumNumber [Transmission ]: MonitorSpectrumNumber [Transmission ](- 1 ),
6480 }
6581
6682
@@ -132,12 +148,43 @@ def get_calibrated_isis_detector(
132148
133149
134150def get_monitor_data (
135- dg : LoadedFileContents [RunType ], nexus_name : NeXusMonitorName [MonitorType ]
151+ dg : LoadedFileContents [RunType ],
152+ nexus_name : NeXusMonitorName [MonitorType ],
153+ spectrum_number : MonitorSpectrumNumber [MonitorType ],
136154) -> NeXusComponent [MonitorType , RunType ]:
155+ """
156+ Extract monitor data that was loaded together with detector data.
157+
158+ If the raw file is histogram data, Mantid stores this as a Workspace2D, where some
159+ or all spectra correspond to monitors.
160+
161+ Parameters
162+ ----------
163+ dg:
164+ Data loaded with Mantid and converted to Scipp.
165+ nexus_name:
166+ Name of the monitor in the NeXus file, e.g. 'incident_monitor' or
167+ 'transmission_monitor'. Used when raw data is an EventWorkspace, where
168+ monitors are stored in a group with this name.
169+ spectrum_number:
170+ Spectrum number of the monitor in the NeXus file, e.g., 3 for incident monitor
171+ or 4 for transmission monitor. This is used when the raw data is a
172+ Workspace2D, where the monitor data is stored in a spectrum with this number.
173+
174+ Returns
175+ -------
176+ :
177+ Monitor data extracted from the loaded file.
178+ """
137179 # The generic NeXus workflow will try to extract 'data' from this, which is exactly
138180 # what we also have in the Mantid data. We use the generic workflow since it also
139181 # applies offsets, etc.
140- monitor = dg ['monitors' ][nexus_name ]['data' ]
182+ if 'monitors' in dg :
183+ # From EventWorkspace
184+ monitor = dg ['monitors' ][nexus_name ]['data' ]
185+ else :
186+ # From Workspace2D
187+ monitor = sc .values (dg ["data" ]["spectrum" , sc .index (spectrum_number .value )])
141188 return NeXusComponent [MonitorType , RunType ](
142189 sc .DataGroup (data = monitor , position = monitor .coords ['position' ])
143190 )
0 commit comments