Skip to content

Commit

Permalink
Add ansto ev44 alternative (#105)
Browse files Browse the repository at this point in the history
* added an44

* updated readme
  • Loading branch information
mattclarke authored Jan 7, 2025
1 parent 598f7d8 commit bc83325
Show file tree
Hide file tree
Showing 7 changed files with 544 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ https://github.com/ess-dmsc/streaming-data-types
| ev42 | Event data (deprecated in favour of ev44) |
| ev43 | Event data from multiple pulses |
| ev44 | Event data with signed data types |
| an44 | ev44 with ANSTO specific changes |
| x5f2 | Status messages |
| tdct | Timestamps |
| ep00 | EPICS connection info (deprecated in favour of ep01) |
Expand Down
11 changes: 7 additions & 4 deletions streaming_data_types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
deserialise_ep00,
serialise_ep00,
)
from streaming_data_types.eventdata_an44 import deserialise_an44, serialise_an44
from streaming_data_types.eventdata_ev42 import deserialise_ev42, serialise_ev42
from streaming_data_types.eventdata_ev43 import deserialise_ev43, serialise_ev43
from streaming_data_types.eventdata_ev44 import deserialise_ev44, serialise_ev44
from streaming_data_types.finished_writing_wrdn import deserialise_wrdn, serialise_wrdn
from streaming_data_types.forwarder_config_update_rf5k import (
deserialise_rf5k,
serialise_rf5k,
)
from streaming_data_types.forwarder_config_update_fc00 import (
deserialise_fc00,
serialise_fc00,
)
from streaming_data_types.forwarder_config_update_rf5k import (
deserialise_rf5k,
serialise_rf5k,
)
from streaming_data_types.histogram_hs00 import deserialise_hs00, serialise_hs00
from streaming_data_types.histogram_hs01 import deserialise_hs01, serialise_hs01
from streaming_data_types.json_json import deserialise_json, serialise_json
Expand All @@ -41,6 +42,7 @@
__version__ = version

SERIALISERS = {
"an44": serialise_an44,
"ev42": serialise_ev42,
"ev43": serialise_ev43,
"ev44": serialise_ev44,
Expand Down Expand Up @@ -71,6 +73,7 @@


DESERIALISERS = {
"an44": deserialise_an44,
"ev42": deserialise_ev42,
"ev43": deserialise_ev43,
"ev44": deserialise_ev44,
Expand Down
2 changes: 1 addition & 1 deletion streaming_data_types/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Version is not directly defined in __init__ because that causes all
# run time dependencies to become build-time dependencies when it is
# imported in setup.py
version = "0.26.1"
version = "0.27.0"
95 changes: 95 additions & 0 deletions streaming_data_types/eventdata_an44.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from collections import namedtuple

import flatbuffers
import numpy as np

import streaming_data_types.fbschemas.eventdata_an44.AN44EventMessage as AN44EventMessage
from streaming_data_types.utils import check_schema_identifier

FILE_IDENTIFIER = b"an44"


EventData = namedtuple(
"EventData",
(
"source_name",
"message_id",
"reference_time",
"reference_time_index",
"time_of_flight",
"pixel_id",
"weight",
),
)


def deserialise_an44(buffer):
"""
Deserialise FlatBuffer an44.
:param buffer: The FlatBuffers buffer.
:return: The deserialised data.
"""
check_schema_identifier(buffer, FILE_IDENTIFIER)

event = AN44EventMessage.AN44EventMessage.GetRootAs(buffer, 0)

return EventData(
event.SourceName().decode("utf-8"),
event.MessageId(),
event.ReferenceTimeAsNumpy(),
event.ReferenceTimeIndexAsNumpy(),
event.TimeOfFlightAsNumpy(),
event.PixelIdAsNumpy(),
event.WeightAsNumpy(),
)


def serialise_an44(
source_name,
message_id,
reference_time,
reference_time_index,
time_of_flight,
pixel_id,
weight,
):
"""
Serialise event data as an an44 FlatBuffers message.
:param source_name:
:param message_id:
:param reference_time:
:param reference_time_index:
:param time_of_flight:
:param pixel_id:
:param weight:
:return:
"""
builder = flatbuffers.Builder(1024)
builder.ForceDefaults(True)

source = builder.CreateString(source_name)
ref_time_data = builder.CreateNumpyVector(
np.asarray(reference_time).astype(np.int64)
)
ref_time_index_data = builder.CreateNumpyVector(
np.asarray(reference_time_index).astype(np.int32)
)
tof_data = builder.CreateNumpyVector(np.asarray(time_of_flight).astype(np.int32))
pixel_id_data = builder.CreateNumpyVector(np.asarray(pixel_id).astype(np.int32))
weight_data = builder.CreateNumpyVector(np.asarray(weight).astype(np.int16))

AN44EventMessage.AN44EventMessageStart(builder)
AN44EventMessage.AN44EventMessageAddReferenceTime(builder, ref_time_data)
AN44EventMessage.AN44EventMessageAddReferenceTimeIndex(builder, ref_time_index_data)
AN44EventMessage.AN44EventMessageAddTimeOfFlight(builder, tof_data)
AN44EventMessage.AN44EventMessageAddPixelId(builder, pixel_id_data)
AN44EventMessage.AN44EventMessageAddWeight(builder, weight_data)
AN44EventMessage.AN44EventMessageAddMessageId(builder, message_id)
AN44EventMessage.AN44EventMessageAddSourceName(builder, source)

data = AN44EventMessage.AN44EventMessageEnd(builder)
builder.Finish(data, file_identifier=FILE_IDENTIFIER)

return bytes(builder.Output())
Loading

0 comments on commit bc83325

Please sign in to comment.