Skip to content

Commit

Permalink
WIP: can save enc1 1-D data
Browse files Browse the repository at this point in the history
  • Loading branch information
mrakitin committed May 14, 2024
1 parent af0f986 commit 0afd370
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/srx_caproto_iocs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ophyd import Device, EpicsSignal, EpicsSignalRO
from ophyd.status import SubscriptionStatus

from .utils import now, save_hdf5
from .utils import now, save_hdf5_1d


class AcqStatuses(Enum):
Expand Down Expand Up @@ -153,7 +153,7 @@ async def _stage(self, instance, value):
async def stage(self, *args, **kwargs):
return await self._stage(*args, **kwargs)

def _get_current_dataset(self, frame):
async def _get_current_dataset(self, frame):
"""The method to return a desired dataset.
See https://scikit-image.org/docs/stable/auto_examples/data/plot_3d.html
Expand Down Expand Up @@ -187,7 +187,7 @@ async def acquire(self, instance, value):
# Delegate saving the resulting data to a blocking callback in a thread.
payload = {
"filename": self.full_file_path.value,
"data": self._get_current_dataset(frame=self.frame_num.value),
"data": await self._get_current_dataset(frame=self.frame_num.value),
"uid": str(uuid.uuid4()),
"timestamp": ttime.time(),
"frame_number": self.frame_num.value,
Expand All @@ -213,7 +213,7 @@ def saver(request_queue, response_queue):
data = received["data"]
frame_number = received["frame_number"]
try:
save_hdf5(fname=filename, data=data, mode="a")
save_hdf5_1d(fname=filename, data=data, mode="x", group_path="enc1")
print(
f"{now()}: saved {frame_number=} {data.shape} data into:\n {filename}"
)
Expand Down
32 changes: 29 additions & 3 deletions src/srx_caproto_iocs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,41 @@ def now(as_object=False):
return _now.isoformat()


def save_hdf5(
def save_hdf5_1d(
fname,
data,
group_path="data",
dtype="float32",
mode="x",
):
"""The function to export the 1-D data to an HDF5 file.
Check https://docs.h5py.org/en/stable/high/file.html#opening-creating-files for modes:
r Readonly, file must exist (default)
r+ Read/write, file must exist
w Create file, truncate if exists
w- or x Create file, fail if exists
a Read/write if exists, create otherwise
"""
with h5py.File(fname, mode, libver="latest") as h5file_desc:
dataset = h5file_desc.create_dataset(
group_path,
data=data,
dtype=dtype,
)
dataset.flush()


def save_hdf5_nd(
fname,
data,
group_name="/entry",
group_path="data/data",
dtype="float32",
mode="x",
):
"""The function to export the data to an HDF5 file.
"""The function to export the N-D data to an HDF5 file (N>1).
Check https://docs.h5py.org/en/stable/high/file.html#opening-creating-files for modes:
Expand Down Expand Up @@ -54,5 +80,5 @@ def save_hdf5(
h5file_desc.swmr_mode = True

dataset.resize((frame_num + 1, *frame_shape))
dataset[frame_num, :, :] = data
dataset[frame_num, ...] = data
dataset.flush()
29 changes: 15 additions & 14 deletions src/srx_caproto_iocs/zebra/caproto_ioc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import textwrap
from pprint import pformat

from caproto.asyncio.client import Context
from caproto.server import run, template_arg_parser
Expand Down Expand Up @@ -102,20 +101,22 @@ def __init__(self, *args, external_pvs=None, **kwargs):
super().__init__(*args, **kwargs)
self._external_pvs = external_pvs

async def _stage(self, *args, **kwargs):
await super()._stage(*args, **kwargs)
# async def _stage(self, *args, **kwargs):
# ret = await super()._stage(*args, **kwargs)
# return ret

async def _get_current_dataset(self, frame, external_pv="enc1"):
client_context = Context()
res = {}
if self._external_pvs is not None:
pvobjects = await client_context.get_pvs(*self._external_pvs.values())
for i, (name, pv) in enumerate(self._external_pvs.items()): # noqa: B007
pvobject = pvobjects[i]
# print(f"{now()}: {pvobject = }")
ret = await pvobject.read()
# print(f"{now()}: {val.data}")
res[name] = {"data": ret.data, "shape": ret.data.shape}
print(f"{now()}:\n{pformat(res)}")
return True
(pvobject,) = await client_context.get_pvs(self._external_pvs[external_pv])
print(f"{pvobject = }")
# pvobject = pvobjects[0]
ret = await pvobject.read()

dataset = ret.data

print(f"{now()}:\n{dataset} {dataset.shape}")

return dataset


if __name__ == "__main__":
Expand Down

0 comments on commit 0afd370

Please sign in to comment.