PNeXus now handles std::vector<> attributes new correctly. HDF4/5 now is using int.

This commit is contained in:
2026-02-07 20:00:27 +01:00
parent 49d42c93ba
commit 8f3ad0c61d

View File

@@ -1832,7 +1832,7 @@ void nxH4::PNeXus::Dump()
if (counts_data.HasAttribute("t0_bin")) {
try {
auto t0_bin = std::any_cast<int32_t>(counts_data.GetAttribute("t0_bin"));
auto t0_bin = std::any_cast<int>(counts_data.GetAttribute("t0_bin"));
std::cout << " t0_bin : " << t0_bin << std::endl;
} catch (const std::bad_any_cast& e) {
std::cerr << "Error: Failed to cast t0_bin attribute" << std::endl;
@@ -1843,7 +1843,7 @@ void nxH4::PNeXus::Dump()
if (counts_data.HasAttribute("first_good_bin")) {
try {
first_good_bin = std::any_cast<int32_t>(counts_data.GetAttribute("first_good_bin"));
first_good_bin = std::any_cast<int>(counts_data.GetAttribute("first_good_bin"));
std::cout << " first_good_bin : " << first_good_bin << std::endl;
} catch (const std::bad_any_cast& e) {
std::cerr << "Error: Failed to cast first_good_bin attribute" << std::endl;
@@ -1854,7 +1854,7 @@ void nxH4::PNeXus::Dump()
if (counts_data.HasAttribute("last_good_bin")) {
try {
auto last_good_bin = std::any_cast<int32_t>(counts_data.GetAttribute("last_good_bin"));
auto last_good_bin = std::any_cast<int>(counts_data.GetAttribute("last_good_bin"));
std::cout << " last_good_bin : " << last_good_bin << std::endl;
} catch (const std::bad_any_cast& e) {
std::cerr << "Error: Failed to cast last_good_bin attribute" << std::endl;
@@ -1865,7 +1865,7 @@ void nxH4::PNeXus::Dump()
if (fDataMap.find("/raw_data_1/instrument/detector_1/resolution") != fDataMap.end()) {
try {
auto ivalData = std::any_cast<PNXdata<int32_t>>(fDataMap["/raw_data_1/instrument/detector_1/resolution"]);
auto ivalData = std::any_cast<PNXdata<int>>(fDataMap["/raw_data_1/instrument/detector_1/resolution"]);
std::cout << " resolution : " << ivalData.GetData()[0];
if (ivalData.HasAttribute("units")) {
try {
@@ -2637,7 +2637,7 @@ nxH5::PNeXusDeadTime::PNeXusDeadTime(const PNeXus *nxs, bool debug) : fDebug(deb
// get t0
if (counts_data.HasAttribute("t0_bin")) {
try {
auto t0_bin = std::any_cast<int32_t>(counts_data.GetAttribute("t0_bin"));
auto t0_bin = std::any_cast<int>(counts_data.GetAttribute("t0_bin"));
fT0Bin = t0_bin;
if (fDebug)
std::cout << " fT0Bin: " << fT0Bin << std::endl;
@@ -2648,7 +2648,7 @@ nxH5::PNeXusDeadTime::PNeXusDeadTime(const PNeXus *nxs, bool debug) : fDebug(deb
// get fgb
if (counts_data.HasAttribute("first_good_bin")) {
try {
auto first_good_bin = std::any_cast<int32_t>(counts_data.GetAttribute("first_good_bin"));
auto first_good_bin = std::any_cast<int>(counts_data.GetAttribute("first_good_bin"));
fFgbBin = first_good_bin;
if (fDebug)
std::cout << " fFgbBin: " << fFgbBin << std::endl;
@@ -2659,7 +2659,7 @@ nxH5::PNeXusDeadTime::PNeXusDeadTime(const PNeXus *nxs, bool debug) : fDebug(deb
// get lgb
if (counts_data.HasAttribute("last_good_bin")) {
try {
auto last_good_bin = std::any_cast<int32_t>(counts_data.GetAttribute("last_good_bin"));
auto last_good_bin = std::any_cast<int>(counts_data.GetAttribute("last_good_bin"));
fLgbBin = last_good_bin;
if (fDebug)
std::cout << " fLgbBin: " << fLgbBin << std::endl;
@@ -3576,7 +3576,7 @@ void nxH5::PNeXus::HandleIdfV2(H5::H5File &file)
//=============================================================================
void nxH5::PNeXus::Dump()
{
int32_t first_good_bin{0};
int first_good_bin{0};
if (fIdfVersion == 1) {
std::cout << std::endl;
@@ -4441,7 +4441,7 @@ void nxH5::PNeXus::Dump()
if (counts_data.HasAttribute("t0_bin")) {
try {
auto t0_bin = std::any_cast<int32_t>(counts_data.GetAttribute("t0_bin"));
auto t0_bin = std::any_cast<int>(counts_data.GetAttribute("t0_bin"));
std::cout << " t0_bin : " << t0_bin << std::endl;
} catch (const std::bad_any_cast& e) {
std::cerr << "Error: Failed to cast t0_bin attribute" << std::endl;
@@ -4452,7 +4452,7 @@ void nxH5::PNeXus::Dump()
if (counts_data.HasAttribute("first_good_bin")) {
try {
first_good_bin = std::any_cast<int32_t>(counts_data.GetAttribute("first_good_bin"));
first_good_bin = std::any_cast<int>(counts_data.GetAttribute("first_good_bin"));
std::cout << " first_good_bin : " << first_good_bin << std::endl;
} catch (const std::bad_any_cast& e) {
std::cerr << "Error: Failed to cast first_good_bin attribute" << std::endl;
@@ -4463,7 +4463,7 @@ void nxH5::PNeXus::Dump()
if (counts_data.HasAttribute("last_good_bin")) {
try {
auto last_good_bin = std::any_cast<int32_t>(counts_data.GetAttribute("last_good_bin"));
auto last_good_bin = std::any_cast<int>(counts_data.GetAttribute("last_good_bin"));
std::cout << " last_good_bin : " << last_good_bin << std::endl;
} catch (const std::bad_any_cast& e) {
std::cerr << "Error: Failed to cast last_good_bin attribute" << std::endl;
@@ -4474,7 +4474,7 @@ void nxH5::PNeXus::Dump()
if (fDataMap.find("/raw_data_1/instrument/detector_1/resolution") != fDataMap.end()) {
try {
auto ivalData = std::any_cast<PNXdata<int32_t>>(fDataMap["/raw_data_1/instrument/detector_1/resolution"]);
auto ivalData = std::any_cast<PNXdata<int>>(fDataMap["/raw_data_1/instrument/detector_1/resolution"]);
std::cout << " resolution : " << ivalData.GetData()[0];
if (ivalData.HasAttribute("units")) {
try {
@@ -4796,8 +4796,8 @@ void nxH5::PNeXus::ReadDatasetAttributes(H5::DataSet& dataset, PNXdata<T>& data)
try {
if (typeClass == H5T_INTEGER) {
int32_t value;
attr.read(H5::PredType::NATIVE_INT32, &value);
int value;
attr.read(H5::PredType::NATIVE_INT, &value);
data.AddAttribute(attrName, value);
} else if (typeClass == H5T_FLOAT) {
float value;
@@ -4895,7 +4895,7 @@ H5::Group nxH5::PNeXus::CreateGroupHierarchy(H5::H5File& file, const std::string
//=============================================================================
/**
* Writes all attributes from a PNXdata object to an HDF5 dataset.
* Supports int32_t, float, and string attribute types.
* Supports int, float, and string attribute types.
*
* @tparam T The data type of the PNXdata object
* @param dataset HDF5 dataset object to write attributes to
@@ -4908,8 +4908,8 @@ void nxH5::PNeXus::WriteDatasetAttributes(H5::DataSet& dataset, const PNXdata<T>
for (const auto& [attrName, attrValue] : attributes) {
try {
// Try int32_t
if (auto* intVal = std::any_cast<int32_t>(&attrValue)) {
// Try int
if (auto* intVal = std::any_cast<int>(&attrValue)) {
H5::DataSpace attrSpace(H5S_SCALAR);
H5::Attribute attr = dataset.createAttribute(
attrName, H5::PredType::NATIVE_INT32, attrSpace
@@ -4939,6 +4939,28 @@ void nxH5::PNeXus::WriteDatasetAttributes(H5::DataSet& dataset, const PNXdata<T>
attr.close();
attrSpace.close();
}
// Try vector<int> for multi-element attributes
else if (auto* intVec = std::any_cast<std::vector<int>>(&attrValue)) {
hsize_t dims[1] = {intVec->size()};
H5::DataSpace attrSpace(1, dims);
H5::Attribute attr = dataset.createAttribute(
attrName, H5::PredType::NATIVE_INT32, attrSpace
);
attr.write(H5::PredType::NATIVE_INT32, intVec->data());
attr.close();
attrSpace.close();
}
// Try vector<float> for multi-element attributes
else if (auto* floatVec = std::any_cast<std::vector<float>>(&attrValue)) {
hsize_t dims[1] = {floatVec->size()};
H5::DataSpace attrSpace(1, dims);
H5::Attribute attr = dataset.createAttribute(
attrName, H5::PredType::NATIVE_FLOAT, attrSpace
);
attr.write(H5::PredType::NATIVE_FLOAT, floatVec->data());
attr.close();
attrSpace.close();
}
else {
if (fPrintDebug) {
std::cerr << "Warning: Unsupported attribute type for "
@@ -5181,7 +5203,7 @@ void nxH5::PNeXus::WriteStringDataset(H5::H5File& file, const std::string& path,
// Write group attributes
//=============================================================================
/**
* Writes attributes to an HDF5 group object. Supports int32_t, float, and
* Writes attributes to an HDF5 group object. Supports int, float, and
* string attribute types.
*
* @param group HDF5 group object to write attributes to
@@ -5191,8 +5213,8 @@ void nxH5::PNeXus::WriteGroupAttributes(H5::Group& group, const std::map<std::st
{
for (const auto& [attrName, attrValue] : attributes) {
try {
// Try int32_t
if (auto* intVal = std::any_cast<int32_t>(&attrValue)) {
// Try int
if (auto* intVal = std::any_cast<int>(&attrValue)) {
H5::DataSpace attrSpace(H5S_SCALAR);
H5::Attribute attr = group.createAttribute(
attrName, H5::PredType::NATIVE_INT32, attrSpace
@@ -5222,6 +5244,28 @@ void nxH5::PNeXus::WriteGroupAttributes(H5::Group& group, const std::map<std::st
attr.close();
attrSpace.close();
}
// Try vector<int> for multi-element attributes
else if (auto* intVec = std::any_cast<std::vector<int>>(&attrValue)) {
hsize_t dims[1] = {intVec->size()};
H5::DataSpace attrSpace(1, dims);
H5::Attribute attr = group.createAttribute(
attrName, H5::PredType::NATIVE_INT32, attrSpace
);
attr.write(H5::PredType::NATIVE_INT32, intVec->data());
attr.close();
attrSpace.close();
}
// Try vector<float> for multi-element attributes
else if (auto* floatVec = std::any_cast<std::vector<float>>(&attrValue)) {
hsize_t dims[1] = {floatVec->size()};
H5::DataSpace attrSpace(1, dims);
H5::Attribute attr = group.createAttribute(
attrName, H5::PredType::NATIVE_FLOAT, attrSpace
);
attr.write(H5::PredType::NATIVE_FLOAT, floatVec->data());
attr.close();
attrSpace.close();
}
else {
if (fPrintDebug) {
std::cerr << "Warning: Unsupported attribute type for "
@@ -5309,8 +5353,8 @@ void nxH5::PNeXus::WriteFileAttributes(H5::H5File& file)
for (const auto& [attrName, attrValue] : rootAttrIt->second) {
try {
// Try int32_t
if (auto* intVal = std::any_cast<int32_t>(&attrValue)) {
// Try int
if (auto* intVal = std::any_cast<int>(&attrValue)) {
H5::DataSpace scalarSpace(H5S_SCALAR);
H5::Attribute attr = file.createAttribute(
attrName, H5::PredType::NATIVE_INT32, scalarSpace
@@ -5535,29 +5579,6 @@ int nxH5::PNeXus::WriteNexusFile(const std::string& filename, int idfVersion)
// Try PNXdata<int>
try {
auto intData = std::any_cast<PNXdata<int>>(anyData);
WriteIntDataset(file, path, intData);
continue;
} catch (const std::bad_any_cast&) {}
// Try PNXdata<int32_t> - convert to int
try {
auto int32Data = std::any_cast<PNXdata<int32_t>>(anyData);
// Convert PNXdata<int32_t> to PNXdata<int>
PNXdata<int> intData(int32Data.GetDataType());
intData.SetDimensions(int32Data.GetDimensions());
std::vector<int> convertedData;
for (auto val : int32Data.GetData()) {
convertedData.push_back(static_cast<int>(val));
}
intData.SetData(convertedData);
// Copy attributes
for (const auto& [name, value] : int32Data.GetAttributes()) {
intData.AddAttribute(name, value);
}
WriteIntDataset(file, path, intData);
continue;
} catch (const std::bad_any_cast&) {}