Modifications in preparation to MAX IV experiment

This commit is contained in:
2024-01-27 21:23:56 +01:00
parent 2446643489
commit f5f86d9ab6
250 changed files with 9363 additions and 3022 deletions
+196 -178
View File
@@ -87,12 +87,13 @@ inline uint64_t GetCBORArrayLen(CborValue &value) {
}
inline uint64_t GetCBORMapLen(CborValue &value) {
size_t array_len;
cborErr(cbor_value_get_map_length(&value, &array_len));
return array_len;
if (!cbor_value_is_map(&value) )
throw JFJochException(JFJochExceptionCategory::CBORError, "Map expected");
size_t map_len;
cborErr(cbor_value_get_map_length(&value, &map_len));
return map_len;
}
inline std::pair<uint64_t, uint64_t> GetRational(CborValue &value) {
std::pair<uint64_t, uint64_t> ret;
if (GetCBORArrayLen(value) != 2)
@@ -159,6 +160,31 @@ inline std::pair<const uint8_t *, size_t> GetCBORByteString(CborValue &value) {
return {ptr, len};
}
inline ROIRectangle GetROIRectangle(CborValue &value) {
ROIRectangle ret{};
CborValue map_value;
if (GetCBORMapLen(value) != 4)
throw JFJochException(JFJochExceptionCategory::CBORError, "ROI Rectangle must be 4 values");
cborErr(cbor_value_enter_container(&value, &map_value));
while (!cbor_value_at_end(&map_value)) {
auto key = GetCBORString(map_value);
if (key == "x_min")
ret.x_min = GetCBORUInt(map_value);
else if (key == "x_max")
ret.x_max = GetCBORUInt(map_value);
else if (key == "y_min")
ret.y_min = GetCBORUInt(map_value);
else if (key == "y_max")
ret.y_max = GetCBORUInt(map_value);
else
throw JFJochException(JFJochExceptionCategory::CBORError, "Unexpected entry in ROI");
}
cborErr(cbor_value_leave_container(&value, &map_value));
return ret;
}
inline void GetCBORFloatArray(CborValue &value, std::vector<float> &v) {
if (GetCBORTag(value) != TagFloatLE)
throw JFJochException(JFJochExceptionCategory::CBORError, "Incorrect array type tag");
@@ -284,6 +310,7 @@ void GetCBORMultidimTypedArray(CompressedImage &v, CborValue &value) {
cborErr(cbor_value_leave_container(&value, &array_value));
}
bool CheckMagicNumber(CborValue &v) {
auto key = GetCBORString(v);
if (key != "magic_number") {
@@ -294,24 +321,36 @@ bool CheckMagicNumber(CborValue &v) {
}
}
inline SpotToSave GetSpot(CborValue& value) {
SpotToSave s;
CborValue map_value;
cborErr(cbor_value_enter_container(&value, &map_value));
while (!cbor_value_at_end(&map_value)) {
auto key = GetCBORString(map_value);
if (key == "x")
s.x = GetCBORFloat(map_value);
else if (key == "y")
s.y = GetCBORFloat(map_value);
else if (key == "I")
s.intensity = GetCBORFloat(map_value);
else if (key == "indexed")
s.indexed = GetCBORBool(map_value);
else
cbor_value_advance(&map_value);
}
cborErr(cbor_value_leave_container(&value, &map_value));
return s;
}
void CBORStream2Deserializer::GetCBORSpots(CborValue &value) {
size_t array_len = GetCBORArrayLen(value);
if (array_len % 4 != 0)
throw JFJochException(JFJochExceptionCategory::CBORError,
"Spot array has to have elements multiple of 4");
CborValue array_value;
cborErr(cbor_value_enter_container(&value, &array_value));
for (int i = 0; i < array_len / 4; i++) {
SpotToSave s{
.x = GetCBORFloat(array_value),
.y = GetCBORFloat(array_value),
.intensity = GetCBORFloat(array_value),
.indexed = GetCBORBool(array_value)
};
data_message.spots.push_back(s);
}
for (int i = 0; i < array_len; i++)
data_message.spots.push_back(GetSpot(array_value));
cborErr(cbor_value_leave_container(&value, &array_value));
}
@@ -348,6 +387,17 @@ void CBORStream2Deserializer::ProcessGoniometerMap(CborValue &value) {
cborErr(cbor_value_leave_container(&value, &map_value));
}
void CBORStream2Deserializer::ProcessAxis(CborValue &value, float v[3]) {
if (GetCBORArrayLen(value) != 3)
throw JFJochException(JFJochExceptionCategory::CBORError, "Array with 3 floats expected");
CborValue array_value;
cborErr(cbor_value_enter_container(&value, &array_value));
for (int i = 0; i < 3; i++)
v[i] = GetCBORFloat(array_value);
cborErr(cbor_value_leave_container(&value, &array_value));
}
GoniometerAxis CBORStream2Deserializer::ProcessGoniometer(CborValue &value) {
CborValue map_value;
GoniometerAxis ret{};
@@ -359,6 +409,8 @@ GoniometerAxis CBORStream2Deserializer::ProcessGoniometer(CborValue &value) {
ret.increment = GetCBORFloat(map_value);
else if (key == "start")
ret.start = GetCBORFloat(map_value);
else if (key == "axis")
ProcessAxis(map_value, ret.axis);
else
cbor_value_advance(&value);
}
@@ -366,53 +418,6 @@ GoniometerAxis CBORStream2Deserializer::ProcessGoniometer(CborValue &value) {
return ret;
}
void CBORStream2Deserializer::ProcessDetTranslation(CborValue &value) {
if (GetCBORArrayLen(value) != 3)
throw JFJochException(JFJochExceptionCategory::CBORError, "Array with 3 floats expected");
CborValue array_value;
cborErr(cbor_value_enter_container(&value, &array_value));
for (int i = 0; i < 3; i++)
start_message.detector_translation[i] = GetCBORFloat(array_value);
cborErr(cbor_value_leave_container(&value, &array_value));
}
void CBORStream2Deserializer::ProcessImageMessageUserDataElement(CborValue &value) {
CborValue map_value;
cborErr(cbor_value_enter_container(&value, &map_value));
if (!CheckMagicNumber(map_value)) {
cborErr(cbor_value_leave_container(&value, &map_value));
return;
}
while (!cbor_value_at_end(&map_value)) {
auto key = GetCBORString(map_value);
if (key == "spots")
GetCBORSpots(map_value);
else if (key == "rad_int_profile")
GetCBORFloatArray(map_value, data_message.rad_int_profile);
else if (key == "indexing_result")
data_message.indexing_result = GetCBORUInt(map_value);
else if (key == "indexing_lattice")
GetCBORFloatArray(map_value, data_message.indexing_lattice);
else if (key == "jf_info")
data_message.jf_info = GetCBORUInt(map_value) & UINT32_MAX;
else if (key == "receiver_available_send_buffers")
data_message.receiver_available_send_buffers = GetCBORFloat(map_value);
else if (key == "receiver_aq_dev_delay")
data_message.receiver_aq_dev_delay = GetCBORInt(map_value);
else if (key == "storage_cell")
data_message.storage_cell = GetCBORUInt(map_value) & UINT32_MAX;
else if (key == "bunch_id")
data_message.bunch_id = GetCBORUInt(map_value);
else
cbor_value_advance(&map_value);
}
cborErr(cbor_value_leave_container(&value, &map_value));
}
bool CBORStream2Deserializer::ProcessImageMessageElement(CborValue &value) {
if (cbor_value_at_end(&value))
return false;
@@ -436,7 +441,47 @@ bool CBORStream2Deserializer::ProcessImageMessageElement(CborValue &value) {
data_message.timestamp = r.first;
data_message.timestamp_base = r.second;
} else if (key == "user_data")
ProcessImageMessageUserDataElement(value);
data_message.user_data = GetCBORString(value);
else if (key == "spots")
GetCBORSpots(value);
else if (key == "az_int_profile")
GetCBORFloatArray(value, data_message.az_int_profile);
else if (key == "indexing_result")
data_message.indexing_result = GetCBORUInt(value);
else if (key == "indexing_lattice")
GetCBORFloatArray(value, data_message.indexing_lattice);
else if (key == "jf_info")
data_message.jf_info = GetCBORUInt(value) & UINT32_MAX;
else if (key == "receiver_aq_dev_delay")
data_message.receiver_aq_dev_delay = GetCBORInt(value);
else if (key == "storage_cell")
data_message.storage_cell = GetCBORUInt(value) & UINT32_MAX;
else if (key == "xfel_bunch_id")
data_message.xfel_bunch_id = GetCBORUInt(value);
else if (key == "xfel_event_code")
data_message.xfel_event_code = GetCBORUInt(value);
else if (key == "saturated_pixel_count")
data_message.saturated_pixel_count = GetCBORUInt(value);
else if (key == "error_pixel_count")
data_message.error_pixel_count = GetCBORUInt(value);
else if (key == "strong_pixel_count")
data_message.strong_pixel_count = GetCBORUInt(value);
else if (key == "data_collection_efficiency")
data_message.image_collection_efficiency = GetCBORFloat(value);
else if (key == "bkg_estimate")
data_message.bkg_estimate = GetCBORFloat(value);
else if (key == "adu_histogram")
GetCBORUInt64Array(value, data_message.adu_histogram);
else if (key == "beam_center_x")
data_message.beam_center_x = GetCBORFloat(value);
else if (key == "beam_center_y")
data_message.beam_center_y = GetCBORFloat(value);
else if (key == "detector_distance")
data_message.detector_distance = GetCBORFloat(value);
else if (key == "incident_energy")
data_message.incident_energy = GetCBORFloat(value);
else if (key == "roi_sum")
data_message.roi_sum = GetCBORInt(value);
else
cbor_value_advance(&value);
return true;
@@ -444,6 +489,8 @@ bool CBORStream2Deserializer::ProcessImageMessageElement(CborValue &value) {
}
void CBORStream2Deserializer::ProcessCalibration(CborValue &value) {
// For calibration and mask - image might be accessed later on - so it is saved in internal storage of the object
// It allows the original start message to be lost
CborValue map_value;
cborErr(cbor_value_enter_container(&value, &map_value));
while (! cbor_value_at_end(&map_value)) {
@@ -451,13 +498,16 @@ void CBORStream2Deserializer::ProcessCalibration(CborValue &value) {
CompressedImage image;
image.channel = key;
GetCBORMultidimTypedArray(image, map_value);
start_message.calibration.push_back(image);
image.Save();
start_message.calibration.emplace_back(std::move(image));
}
cborErr(cbor_value_leave_container(&value, &map_value));
}
void CBORStream2Deserializer::ProcessPixelMaskElement(CborValue &value) {
// For calibration and mask - image might be accessed later on - so it is saved in internal storage of the object
// It allows the original start message to be lost
CborValue map_value;
cborErr(cbor_value_enter_container(&value, &map_value));
while (! cbor_value_at_end(&map_value)) {
@@ -465,7 +515,8 @@ void CBORStream2Deserializer::ProcessPixelMaskElement(CborValue &value) {
CompressedImage image;
image.channel = key;
GetCBORMultidimTypedArray(image, map_value);
start_message.pixel_mask.push_back(image);
image.Save();
start_message.pixel_mask.emplace_back(std::move(image));
}
cborErr(cbor_value_leave_container(&value, &map_value));
}
@@ -477,7 +528,7 @@ void CBORStream2Deserializer::ProcessRadIntResultElement(CborValue &value) {
auto key = GetCBORString(map_value);
std::vector<float> val;
GetCBORFloatArray(map_value, val);
end_message.rad_int_result[key] = val;
end_message.az_int_result[key] = val;
}
cborErr(cbor_value_leave_container(&value, &map_value));
}
@@ -534,80 +585,6 @@ void CBORStream2Deserializer::ProcessUnitCellElement(CborValue &value) {
start_message.unit_cell = unit_cell;
}
void CBORStream2Deserializer::ProcessStartMessageUserDataElement(CborValue &value) {
CborValue map_value;
cborErr(cbor_value_enter_container(&value, &map_value));
if (!CheckMagicNumber(map_value)) {
cborErr(cbor_value_leave_container(&value, &map_value));
return;
}
while (!cbor_value_at_end(&map_value)) {
auto key = GetCBORString(map_value);
if (key == "space_group_number")
start_message.space_group_number = GetCBORUInt(map_value);
else if (key == "unit_cell")
ProcessUnitCellElement(map_value);
else if (key == "sample_name")
start_message.sample_name = GetCBORString(map_value);
else if (key == "source_name")
start_message.source_name = GetCBORString(map_value);
else if (key == "source_name_short")
start_message.source_name_short = GetCBORString(map_value);
else if (key == "instrument_name")
start_message.instrument_name = GetCBORString(map_value);
else if (key == "instrument_name_short")
start_message.instrument_name_short = GetCBORString(map_value);
else if (key == "file_prefix")
start_message.file_prefix = GetCBORString(map_value);
else if (key == "max_spot_count")
start_message.max_spot_count = GetCBORUInt(map_value);
else if (key == "data_file_count")
start_message.data_file_count = GetCBORUInt(map_value);
else if (key == "pixel_bit_depth")
start_message.pixel_bit_depth = GetCBORUInt(map_value);
else if (key == "pixel_signed")
start_message.pixel_signed = GetCBORBool(map_value);
else if (key == "min_value")
start_message.min_value = GetCBORInt(map_value);
else if (key == "rad_int_bin_number")
start_message.rad_int_bin_number = GetCBORInt(map_value);
else if (key == "rad_int_bin_to_q")
GetCBORFloatArray(map_value, start_message.rad_int_bin_to_q);
else if (key == "rad_int_solid_angle_corr")
GetCBORFloatArray(map_value, start_message.rad_int_solid_angle_corr);
else if (key == "summation")
start_message.summation = GetCBORUInt(map_value);
else if (key == "storage_cell_number")
start_message.storage_cell_number = GetCBORUInt(map_value);
else if (key == "storage_cell_delay")
start_message.storage_cell_delay_ns = GetRational(map_value).first;
else if (key == "total_flux")
start_message.total_flux = GetCBORFloat(map_value);
else if (key == "attenuator_transmission")
start_message.attenuator_transmission = GetCBORFloat(map_value);
else if (key == "compression_algorithm") {
auto tmp = GetCBORString(map_value);
if (tmp == "bslz4")
start_message.compression_algorithm = CompressionAlgorithm::BSHUF_LZ4;
else if (tmp == "bszstd")
start_message.compression_algorithm = CompressionAlgorithm::BSHUF_ZSTD;
else if (tmp == "none")
start_message.compression_algorithm = CompressionAlgorithm::NO_COMPRESSION;
else
throw JFJochException(JFJochExceptionCategory::CBORError, "Unsupported compression");
} else if (key == "compression_block_size")
start_message.compression_block_size = GetCBORUInt(map_value);
else if (key == "calibration")
ProcessCalibration(map_value);
else
cbor_value_advance(&map_value);
}
cborErr(cbor_value_leave_container(&value, &map_value));
}
bool CBORStream2Deserializer::ProcessStartMessageElement(CborValue &value) {
if (cbor_value_at_end(&value))
return false;
@@ -636,6 +613,8 @@ bool CBORStream2Deserializer::ProcessStartMessageElement(CborValue &value) {
start_message.count_time = GetCBORFloat(value);
else if (key == "saturation_value")
start_message.saturation_value = GetCBORInt(value);
else if (key == "error_value")
start_message.error_value = GetCBORInt(value);
else if (key == "pixel_size_x")
start_message.pixel_size_x = GetCBORFloat(value);
else if (key == "pixel_size_y")
@@ -657,7 +636,7 @@ bool CBORStream2Deserializer::ProcessStartMessageElement(CborValue &value) {
else if (key == "channels")
ProcessChannels(value);
else if (key == "detector_translation")
ProcessDetTranslation(value);
ProcessAxis(value, start_message.detector_translation);
else if (key == "goniometer")
ProcessGoniometerMap(value);
else if (key == "pixel_mask_enabled")
@@ -665,44 +644,67 @@ bool CBORStream2Deserializer::ProcessStartMessageElement(CborValue &value) {
else if (key == "arm_date")
start_message.arm_date = GetCBORDateTime(value);
else if (key == "user_data")
ProcessStartMessageUserDataElement(value);
start_message.user_data = GetCBORString(value);
else if (key == "space_group_number")
start_message.space_group_number = GetCBORUInt(value);
else if (key == "unit_cell")
ProcessUnitCellElement(value);
else if (key == "sample_name")
start_message.sample_name = GetCBORString(value);
else if (key == "source_name")
start_message.source_name = GetCBORString(value);
else if (key == "source_name_short")
start_message.source_name_short = GetCBORString(value);
else if (key == "instrument_name")
start_message.instrument_name = GetCBORString(value);
else if (key == "instrument_name_short")
start_message.instrument_name_short = GetCBORString(value);
else if (key == "file_prefix")
start_message.file_prefix = GetCBORString(value);
else if (key == "max_spot_count")
start_message.max_spot_count = GetCBORUInt(value);
else if (key == "data_file_count")
start_message.data_file_count = GetCBORUInt(value);
else if (key == "pixel_bit_depth")
start_message.pixel_bit_depth = GetCBORUInt(value);
else if (key == "pixel_signed")
start_message.pixel_signed = GetCBORBool(value);
else if (key == "az_int_bin_number")
start_message.az_int_bin_number = GetCBORInt(value);
else if (key == "az_int_bin_to_q")
GetCBORFloatArray(value, start_message.az_int_bin_to_q);
else if (key == "summation")
start_message.summation = GetCBORUInt(value);
else if (key == "storage_cell_number")
start_message.storage_cell_number = GetCBORUInt(value);
else if (key == "storage_cell_delay")
start_message.storage_cell_delay_ns = GetRational(value).first;
else if (key == "total_flux")
start_message.total_flux = GetCBORFloat(value);
else if (key == "attenuator_transmission")
start_message.attenuator_transmission = GetCBORFloat(value);
else if (key == "roi_sum_area")
start_message.roi_summation_area = GetROIRectangle(value);
else if (key == "compression_algorithm") {
auto tmp = GetCBORString(value);
if (tmp == "bslz4")
start_message.compression_algorithm = CompressionAlgorithm::BSHUF_LZ4;
else if (tmp == "bszstd")
start_message.compression_algorithm = CompressionAlgorithm::BSHUF_ZSTD;
else if (tmp == "none")
start_message.compression_algorithm = CompressionAlgorithm::NO_COMPRESSION;
else
throw JFJochException(JFJochExceptionCategory::CBORError, "Unsupported compression");
} else if (key == "compression_block_size")
start_message.compression_block_size = GetCBORUInt(value);
else if (key == "calibration")
ProcessCalibration(value);
else
cbor_value_advance(&value);
return true;
}
}
void CBORStream2Deserializer::ProcessEndMessageUserDataElement(CborValue &value) {
CborValue map_value;
cborErr(cbor_value_enter_container(&value, &map_value));
if (!CheckMagicNumber(map_value)) {
cborErr(cbor_value_leave_container(&value, &map_value));
return;
}
while (!cbor_value_at_end(&map_value)) {
auto key = GetCBORString(map_value);
if (key == "number_of_images")
end_message.number_of_images = GetCBORUInt(map_value);
else if (key == "max_receiver_delay")
end_message.max_receiver_delay = GetCBORUInt(map_value);
else if (key == "receiver_efficiency")
end_message.efficiency = GetCBORFloat(map_value);
else if (key == "write_master_file")
end_message.write_master_file = GetCBORBool(map_value);
else if (key == "rad_int_result")
ProcessRadIntResultElement(map_value);
else if (key == "adu_histogram")
ProcessADUHistogramElement(map_value);
else if (key == "adu_histogram_bin_width")
end_message.adu_histogram_bin_width = GetCBORUInt(map_value);
else
cbor_value_advance(&map_value);
}
cborErr(cbor_value_leave_container(&value, &map_value));
}
bool CBORStream2Deserializer::ProcessEndMessageElement(CborValue &value) {
if (cbor_value_at_end(&value))
return false;
@@ -714,8 +716,24 @@ bool CBORStream2Deserializer::ProcessEndMessageElement(CborValue &value) {
end_message.series_unique_id = GetCBORString(value);
else if (key == "series_id")
end_message.series_id = GetCBORUInt(value);
else if (key == "user_data")
ProcessEndMessageUserDataElement(value);
else if (key == "max_image_number")
end_message.max_image_number = GetCBORUInt(value);
else if (key == "images_collected")
end_message.images_collected_count = GetCBORUInt(value);
else if (key == "images_sent_to_write")
end_message.images_sent_to_write_count = GetCBORUInt(value);
else if (key == "max_receiver_delay")
end_message.max_receiver_delay = GetCBORUInt(value);
else if (key == "data_collection_efficiency")
end_message.efficiency = GetCBORFloat(value);
else if (key == "write_master_file")
end_message.write_master_file = GetCBORBool(value);
else if (key == "az_int_result")
ProcessRadIntResultElement(value);
else if (key == "adu_histogram")
ProcessADUHistogramElement(value);
else if (key == "adu_histogram_bin_width")
end_message.adu_histogram_bin_width = GetCBORUInt(value);
else
cbor_value_advance(&value);
return true;