Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
hooks:
- id: ruff
name: Run ruff
stages: [commit]
stages: [pre-commit]
language: system
entry: ruff check
types: [python]
Expand Down
13 changes: 12 additions & 1 deletion src/nexgen/beamlines/I19_2_nxs.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def eiger_writer(
n_frames: int | None = None,
vds_offset: int = 0,
notes: dict[str, Any] | None = None,
data_entry_key: str = "data",
):
"""
A function to call the NXmx nexus file writer for Eiger 2X 4M detector.
Expand All @@ -248,6 +249,8 @@ def eiger_writer(
vds_offset (int, optional): Start index for the vds writer. Defaults to 0.
notes (dict[str, Any], optional): Dictionary of (key, value) pairs where key represents the \
dataset name and value its data. Defaults to None.
data_entry_key (str, optional): Dataset entry key in datafiles. eg. for gating mode it's data1.\
Defaults to data.

Raises:
ValueError: If use_meta is set to False but axes_pos and det_pos haven't been passed.
Expand Down Expand Up @@ -424,7 +427,11 @@ def eiger_writer(
TR.tot_num_images,
sample,
)
NXmx_writer.write(image_filename=image_filename, start_time=timestamps[0])
NXmx_writer.write(
image_filename=image_filename,
start_time=timestamps[0],
data_entry_key=data_entry_key,
)
NXmx_writer.write_vds(
vds_offset=vds_offset,
vds_shape=(n_frames, *detector.detector_params.image_size),
Expand Down Expand Up @@ -515,6 +522,7 @@ def nexus_writer(
master_file: Path,
timestamps: tuple[datetime, datetime] = (None, None),
use_meta: bool = False,
data_entry_key: str = "data",
):
"""Wrapper function to gather all parameters from the beamline and kick off the nexus writer for a \
standard experiment on I19-2.
Expand All @@ -526,6 +534,8 @@ def nexus_writer(
Defaults to (None, None).
use_meta (bool, optional): Eiger option only, if True use metadata from meta.h5 file. Otherwise \
all parameters will need to be passed manually. Defaults to False.
data_entry_key (str, optional): Dataset entry key in datafiles. eg. for gating mode it's data1.\
Defaults to data.
"""
collection_params = CollectionParams(**params)
wdir = master_file.parent
Expand Down Expand Up @@ -592,6 +602,7 @@ def nexus_writer(
collection_params,
timestamps,
use_meta,
data_entry_key=data_entry_key,
)
case DetectorName.TRISTAN:
tristan_writer(
Expand Down
11 changes: 9 additions & 2 deletions src/nexgen/command_line/I19_2_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def nexgen_writer(args):
master_file,
(_start, _stop),
args.use_meta,
data_entry_key=args.data_key,
)


Expand Down Expand Up @@ -236,12 +237,12 @@ def nexgen_writer(args):
)
parser_nex.add_argument("meta_file", type=str, help="Path to _meta.h5 file.")
parser_nex.add_argument(
"detector-name",
"detector_name",
type=str,
choices=["eiger", "tristan"],
help="Detector currently in use on beamline.",
)
parser_nex.add_argument("exp-time", type=float, help="Exposure time, in s.")
parser_nex.add_argument("exp_time", type=float, help="Exposure time, in s.")
parser_nex.add_argument(
"-n",
"--num-imgs",
Expand Down Expand Up @@ -297,6 +298,12 @@ def nexgen_writer(args):
help="Number of frames in the nexus and vds file. Only passed if different from total number \
of images collected.",
)
parser_nex.add_argument(
"--data-key",
type=str,
default="data",
help="Data entry key of dataset in raw .h5 file. Defaults to data.",
)
parser_nex.set_defaults(func=nexgen_writer)


Expand Down
10 changes: 9 additions & 1 deletion src/nexgen/command_line/nexus_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def _get_datafiles(filepath: Path, filename_root: str) -> list[Path]:
for f in glob.glob(_file_template.as_posix())
if "meta.h5" not in f
]
datafiles.sort()
return datafiles


Expand Down Expand Up @@ -141,6 +142,7 @@ def write_nxmx_cli(args):
logger.debug(f"Recorded exposure time: {detector.exp_time} s.")

try:
entry_key = args.data_key if args.data_key else "data"
# Aaaaaaaaaaaand write
if params.det.mode == "images":
writer = NXmxFileWriter(
Expand All @@ -152,7 +154,7 @@ def write_nxmx_cli(args):
params.instrument.attenuator,
num_images,
)
writer.write(image_datafiles=datafiles)
writer.write(image_datafiles=datafiles, data_entry_key=entry_key)
if not args.no_vds:
writer.write_vds(args.vds_offset)
else:
Expand Down Expand Up @@ -382,6 +384,12 @@ def _parse_cli() -> argparse.ArgumentParser:
type=str,
help="New nexus filename if stem needs to be different from data files.",
)
nxmx_parser.add_argument(
"--data-key",
type=str,
default="data",
help="Data entry key of dataset in raw .h5 file. Defaults to data.",
)
nxmx_parser.set_defaults(func=write_nxmx_cli)
demo_parser = subparsers.add_parser(
"2",
Expand Down
10 changes: 6 additions & 4 deletions src/nexgen/nxs_write/nxmx_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def write(
est_end_time: datetime | str | None = None,
write_mode: str = "x",
add_non_standard: bool = True,
data_entry_key: str = "data",
):
"""Write the NXmx format NeXus file.

Expand All @@ -162,6 +163,7 @@ def write(
Accepts any valid h5py file opening mode. Defaults to "x".
add_non_standard (bool, optional): Flag if non-standard NXsample fields should be added \
for processing to work. Defaults to True, will change in the future.
data_entry_key (str, optional): Dataset entry key in datafiles. Defaults to data.
"""
metafile = self._get_meta_file(image_filename)
if metafile:
Expand Down Expand Up @@ -193,10 +195,7 @@ def write(

# NXdata: entry/data
write_NXdata(
nxs,
datafiles,
"images",
list(osc.keys())[0],
nxs, datafiles, "images", list(osc.keys())[0], entry_key=data_entry_key
)

# NXinstrument: entry/instrument
Expand Down Expand Up @@ -341,6 +340,7 @@ def write(
start_time: datetime | str | None = None,
write_mode: str = "x",
add_non_standard: bool = False,
data_entry_key: str = "data",
):
"""Write a NXmx-like NeXus file for event mode data collections.

Expand All @@ -356,6 +356,7 @@ def write(
Accepts any valid h5py file opening mode. Defaults to "x".
add_non_standard (bool, optional): Flag if non-standard NXsample fields should be added \
for processing to work. Defaults to False.
data_entry_key (str, optional): Dataset entry key in datafiles. Defaults to data.
"""
# Get metafile
# No data files, just link to meta
Expand All @@ -381,6 +382,7 @@ def write(
[metafile],
"events",
list(osc.keys())[0],
entry_key=data_entry_key,
)

# NXinstrument: entry/instrument
Expand Down
Loading