Skip to content

Commit dda08a9

Browse files
committed
added get_count_rate() to cfel
1 parent ef3dcda commit dda08a9

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

src/sed/loader/cfel/loader.py

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
from pathlib import Path
1616

1717
import dask.dataframe as dd
18+
import h5py
19+
import numpy as np
20+
import scipy.interpolate as sint
1821
from natsort import natsorted
1922

2023
from sed.core.logging import set_verbosity
@@ -27,6 +30,30 @@
2730
logger = setup_logging("flash_loader")
2831

2932

33+
def get_count_rate(
34+
h5file: h5py.File,
35+
ms_markers_key: str = "msMarkers",
36+
) -> tuple[np.ndarray, np.ndarray]:
37+
"""Create count rate in the file from the msMarker column.
38+
39+
Args:
40+
h5file (h5py.File): The h5file from which to get the count rate.
41+
ms_markers_key (str, optional): The hdf5 path where the millisecond markers
42+
are stored. Defaults to "msMarkers".
43+
44+
Returns:
45+
tuple[np.ndarray, np.ndarray]: The count rate in Hz and the seconds into the
46+
scan.
47+
"""
48+
ms_markers = np.asarray(h5file[ms_markers_key])
49+
secs = np.arange(0, len(ms_markers)) / 1000
50+
msmarker_spline = sint.InterpolatedUnivariateSpline(secs, ms_markers, k=1)
51+
rate_spline = msmarker_spline.derivative()
52+
count_rate = rate_spline(secs)
53+
54+
return (count_rate, secs)
55+
56+
3057
class CFELLoader(BaseLoader):
3158
"""
3259
The class generates multiindexed multidimensional pandas dataframes from the new FLASH
@@ -137,7 +164,7 @@ def _initialize_dirs(self) -> None:
137164
raw_dir = raw_paths[0].resolve()
138165

139166
processed_dir = beamtime_dir.joinpath("processed")
140-
meta_dir = beamtime_dir.joinpath("meta/fabtrack/")
167+
meta_dir = beamtime_dir.joinpath("meta/fabtrack/") # cspell:ignore fabtrack
141168

142169
processed_dir.mkdir(parents=True, exist_ok=True)
143170

@@ -254,10 +281,61 @@ def parse_local_metadata(self) -> dict:
254281

255282
def get_count_rate(
256283
self,
257-
fids: Sequence[int] = None, # noqa: ARG002
258-
**kwds, # noqa: ARG002
259-
):
260-
return None, None
284+
fids: Sequence[int] = None,
285+
**kwds,
286+
) -> tuple[np.ndarray, np.ndarray]:
287+
"""Create count rate from the msMarker column for the files specified in
288+
``fids``.
289+
290+
Args:
291+
fids (Sequence[int], optional): fids (Sequence[int]): the file ids to
292+
include. Defaults to list of all file ids.
293+
kwds: Keyword arguments:
294+
295+
- **ms_markers_key**: HDF5 path of the ms-markers
296+
297+
Returns:
298+
tuple[np.ndarray, np.ndarray]: Arrays containing countrate and seconds
299+
into the scan.
300+
"""
301+
if fids is None:
302+
fids = range(0, len(self.files))
303+
304+
ms_markers_key = kwds.pop(
305+
"ms_markers_key",
306+
self._config.get("dataframe", {}).get(
307+
"ms_markers_key",
308+
"msMarkers",
309+
),
310+
)
311+
312+
if len(kwds) > 0:
313+
raise TypeError(f"get_count_rate() got unexpected keyword arguments {kwds.keys()}.")
314+
315+
secs_list = []
316+
count_rate_list = []
317+
accumulated_time = 0
318+
for fid in fids:
319+
try:
320+
count_rate_, secs_ = get_count_rate(
321+
h5py.File(self.files[fid]),
322+
ms_markers_key=ms_markers_key,
323+
)
324+
secs_list.append((accumulated_time + secs_).T)
325+
count_rate_list.append(count_rate_.T)
326+
accumulated_time += secs_[-1]
327+
except OSError as exc:
328+
if "Unable to synchronously open file" in str(exc):
329+
logger.warning(
330+
f"Unable to open file {fid}: {str(exc)}. "
331+
"Most likely the file is incomplete.",
332+
)
333+
pass
334+
335+
count_rate = np.concatenate(count_rate_list)
336+
secs = np.concatenate(secs_list)
337+
338+
return count_rate, secs
261339

262340
def get_elapsed_time(self, fids: Sequence[int] = None, **kwds) -> float | list[float]: # type: ignore[override]
263341
"""

0 commit comments

Comments
 (0)