@@ -385,7 +385,11 @@ def get_calibrated_detector(
385385 # If the NXdetector in the file is not 1-D, we want to match the order of dims.
386386 # zip_pixel_offsets otherwise yields a vector with dimensions in the order given
387387 # by the x/y/z offsets.
388- offsets = snx .zip_pixel_offsets (da .coords ).transpose (da .dims ).copy ()
388+ offsets = snx .zip_pixel_offsets (da .coords )
389+ # Get the dims in the order of the detector data array, but filter out dims that
390+ # don't exist in the offsets (e.g. the detector data may have a 'time' dimension).
391+ dims = [dim for dim in da .dims if dim in offsets .dims ]
392+ offsets = offsets .transpose (dims ).copy ()
389393 # We use the unit of the offsets as this is likely what the user expects.
390394 if transform .value .unit is not None and transform .value .unit != '' :
391395 transform_value = transform .value .to (unit = offsets .unit )
@@ -399,7 +403,7 @@ def get_calibrated_detector(
399403
400404def assemble_detector_data (
401405 detector : EmptyDetector [RunType ],
402- event_data : NeXusData [snx .NXdetector , RunType ],
406+ neutron_data : NeXusData [snx .NXdetector , RunType ],
403407) -> RawDetector [RunType ]:
404408 """
405409 Assemble a detector data array with event data.
@@ -410,14 +414,15 @@ def assemble_detector_data(
410414 ----------
411415 detector:
412416 Calibrated detector data array.
413- event_data :
414- Event data array.
417+ neutron_data :
418+ Neutron data array (events or histogram) .
415419 """
416- grouped = nexus .group_event_data (
417- event_data = event_data , detector_number = detector .coords ['detector_number' ]
418- )
420+ if neutron_data .bins is not None :
421+ neutron_data = nexus .group_event_data (
422+ event_data = neutron_data , detector_number = detector .coords ['detector_number' ]
423+ )
419424 return RawDetector [RunType ](
420- _add_variances (grouped )
425+ _add_variances (neutron_data )
421426 .assign_coords (detector .coords )
422427 .assign_masks (detector .masks )
423428 )
@@ -504,6 +509,19 @@ def _drop(
504509 }
505510
506511
512+ class _EmptyField :
513+ """Empty field that can replace a missing detector_number in NXdetector."""
514+
515+ def __init__ (self , sizes : dict [str , int ]):
516+ self .attrs = {}
517+ self .sizes = sizes .copy ()
518+ self .dims = tuple (sizes .keys ())
519+ self .shape = tuple (sizes .values ())
520+
521+ def __getitem__ (self , key : Any ) -> sc .Variable :
522+ return sc .zeros (dims = self .dims , shape = self .shape , unit = None , dtype = 'int32' )
523+
524+
507525class _StrippedDetector (snx .NXdetector ):
508526 """Detector definition without large geometry or event data for ScippNexus.
509527
@@ -513,8 +531,36 @@ class _StrippedDetector(snx.NXdetector):
513531 def __init__ (
514532 self , attrs : dict [str , Any ], children : dict [str , snx .Field | snx .Group ]
515533 ):
516- children = _drop (children , (snx .NXoff_geometry , snx .NXevent_data ))
517- children ['data' ] = children ['detector_number' ]
534+ if 'detector_number' in children :
535+ data = children ['detector_number' ]
536+ else :
537+ # We get the 'data' sizes before the NXdata is dropped
538+ if 'data' not in children :
539+ raise KeyError (
540+ "StrippedDetector: Cannot determine shape of the detector. "
541+ "No 'detector_number' was found, and the 'data' entry is missing."
542+ )
543+ if 'value' not in children ['data' ]:
544+ raise KeyError (
545+ "StrippedDetector: Cannot determine shape of the detector. "
546+ "The 'data' entry has no 'value'."
547+ )
548+ # We drop any time-related dimension from the data sizes, as they are not
549+ # relevant for the detector geometry/shape.
550+ data = _EmptyField (
551+ sizes = {
552+ dim : size
553+ for dim , size in children ['data' ]['value' ].sizes .items ()
554+ if dim not in ('time' , 'frame_time' )
555+ }
556+ )
557+
558+ children = _drop (
559+ children , (snx .NXoff_geometry , snx .NXevent_data , snx .NXdata , snx .NXlog )
560+ )
561+
562+ children ['data' ] = data
563+
518564 super ().__init__ (attrs = attrs , children = children )
519565
520566
@@ -528,7 +574,7 @@ def __init__(self, dim: str):
528574 self .shape = (0 ,)
529575
530576 def __getitem__ (self , key : Any ) -> sc .Variable :
531- return sc .empty (dims = self .dims , shape = self .shape , unit = None )
577+ return sc .zeros (dims = self .dims , shape = self .shape , unit = None , dtype = 'int32' )
532578
533579
534580class _StrippedMonitor (snx .NXmonitor ):
0 commit comments