Skip to content

Commit b3c6b7f

Browse files
committed
Expose more coordinates to be exported.
1 parent 7693713 commit b3c6b7f

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/ess/nmx/mcstas/xml.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ def num_fast_pixels_per_row(self) -> int:
212212
"""Number of pixels in each row of the detector along the fast axis."""
213213
return self.num_x if self.fast_axis_name == 'x' else self.num_y
214214

215+
@property
216+
def detector_shape(self) -> tuple:
217+
"""Shape of the detector panel. (num_x, num_y)"""
218+
return (self.num_x, self.num_y)
219+
215220

216221
def _collect_detector_descriptions(tree: _XML) -> tuple[DetectorDesc, ...]:
217222
"""Retrieve detector geometry descriptions from mcstas file."""
@@ -392,6 +397,9 @@ def to_coords(self, *det_names: str) -> dict[str, sc.Variable]:
392397
slow_axes = [det.slow_axis for det in detectors]
393398
fast_axes = [det.fast_axis for det in detectors]
394399
origins = [self.sample.position_from_sample(det.position) for det in detectors]
400+
detector_shapes = [sc.scalar(det.detector_shape) for det in detectors]
401+
x_pixel_sizes = [det.step_x for det in detectors]
402+
y_pixel_sizes = [det.step_y for det in detectors]
395403
return {
396404
'pixel_id': _construct_pixel_ids(detectors),
397405
'fast_axis': sc.concat(fast_axes, 'panel'),
@@ -401,6 +409,9 @@ def to_coords(self, *det_names: str) -> dict[str, sc.Variable]:
401409
'source_position': self.sample.position_from_sample(self.source.position),
402410
'sample_name': sc.scalar(self.sample.name),
403411
'position': _detector_pixel_positions(detectors, self.sample),
412+
'detector_shape': sc.concat(detector_shapes, 'panel'),
413+
'x_pixel_size': sc.concat(x_pixel_sizes, 'panel'),
414+
'y_pixel_size': sc.concat(y_pixel_sizes, 'panel'),
404415
}
405416

406417

src/ess/nmx/nexus.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _add_lauetof_instrument(nx_entry: h5py.Group):
164164

165165

166166
def _add_lauetof_detector_group(dg: sc.DataGroup, nx_instrument: h5py.Group) -> None:
167-
nx_detector = nx_instrument.create_group(dg["name"]) # Detector name
167+
nx_detector = nx_instrument.create_group(dg["name"].value) # Detector name
168168
nx_detector.attrs["NX_class"] = "NXdetector"
169169
# Polar angle
170170
_create_dataset_from_var(
@@ -181,22 +181,19 @@ def _add_lauetof_detector_group(dg: sc.DataGroup, nx_instrument: h5py.Group) ->
181181
# Data - shape: [n_x_pixels, n_y_pixels, n_tof_bins]
182182
# The actual application definition defines it as integer,
183183
# but we keep the original data type for now
184+
num_x, num_y = dg["detector_shape"].values[0] # Probably better way to do this...
184185
_create_dataset_from_var(
185186
name="data",
186187
root_entry=nx_detector,
187-
var=sc.scalar(0, unit=''), # TODO: Add real data
188+
var=sc.fold(dg["counts"].data, dim='id', sizes={'x': num_x, 'y': num_y}),
188189
)
189190
# x_pixel_size
190191
_create_dataset_from_var(
191-
name="x_pixel_size",
192-
root_entry=nx_detector,
193-
var=sc.scalar(0, unit='mm'), # TODO: Add real data
192+
name="x_pixel_size", root_entry=nx_detector, var=dg["x_pixel_size"]
194193
)
195194
# y_pixel_size
196195
_create_dataset_from_var(
197-
name="y_pixel_size",
198-
root_entry=nx_detector,
199-
var=sc.scalar(0, unit='mm'), # TODO: Add real data
196+
name="y_pixel_size", root_entry=nx_detector, var=dg["y_pixel_size"]
200197
)
201198
# distance
202199
_create_dataset_from_var(
@@ -208,7 +205,7 @@ def _add_lauetof_detector_group(dg: sc.DataGroup, nx_instrument: h5py.Group) ->
208205
_create_dataset_from_var(
209206
name="time_of_flight",
210207
root_entry=nx_detector,
211-
var=sc.scalar(0, unit='s'), # TODO: Add real data
208+
var=sc.midpoints(dg["counts"].coords['t']),
212209
# It should be actual time of flight values of each bin
213210
# Not sure if it should be median/mean of the bin or bin edges
214211
)
@@ -274,6 +271,7 @@ def export_panel_independent_data_as_nxlauetof(
274271
_add_lauetof_definition(nx_entry)
275272
_add_lauetof_instrument(nx_entry)
276273
_add_lauetof_sample_group(data, nx_entry)
274+
# Placeholder for ``monitor`` group
277275
_add_lauetof_monitor_group(data, nx_entry)
278276
# Skipping ``name`` field
279277

@@ -288,10 +286,12 @@ def export_panel_dependent_data_as_nxlauetof(
288286

289287

290288
def export_as_nxlauetof(
291-
data: sc.DataGroup, output_file: str | pathlib.Path | io.BytesIO
289+
dg: sc.DataGroup, *dgs: sc.DataGroup, output_file: str | pathlib.Path | io.BytesIO
292290
) -> None:
293291
"""Export the reduced data into a nxlauetof format.
294292
295293
Exporting step is not expected to be part of sciline pipelines.
296294
"""
297-
export_panel_independent_data_as_nxlauetof(data, output_file)
295+
296+
export_panel_independent_data_as_nxlauetof(dg, output_file)
297+
export_panel_dependent_data_as_nxlauetof(dg, *dgs, output_file=output_file)

0 commit comments

Comments
 (0)