diff --git a/slsReceiverSoftware/CMakeLists.txt b/slsReceiverSoftware/CMakeLists.txt index 8589e0314..a72da71b7 100755 --- a/slsReceiverSoftware/CMakeLists.txt +++ b/slsReceiverSoftware/CMakeLists.txt @@ -6,7 +6,6 @@ set(SOURCES src/Receiver.cpp src/File.cpp src/BinaryDataFile.cpp - src/BinaryMasterFile.cpp src/ThreadObject.cpp src/Listener.cpp src/DataProcessor.cpp @@ -14,6 +13,7 @@ set(SOURCES src/Fifo.cpp src/Arping.cpp src/MasterAttributes.cpp + src/MasterFileUtility.cpp ) set(PUBLICHEADERS @@ -28,7 +28,6 @@ if (SLS_USE_HDF5) ) list (APPEND SOURCES src/HDF5DataFile.cpp - src/HDF5Utility.cpp ) endif (SLS_USE_HDF5) diff --git a/slsReceiverSoftware/src/BinaryMasterFile.cpp b/slsReceiverSoftware/src/BinaryMasterFile.cpp deleted file mode 100644 index 04fc0f2d7..000000000 --- a/slsReceiverSoftware/src/BinaryMasterFile.cpp +++ /dev/null @@ -1,52 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-other -// Copyright (C) 2021 Contributors to the SLS Detector Package -#include "BinaryMasterFile.h" -#include "MasterAttributes.h" - -std::string BinaryMasterFile::CreateMasterFile(const std::string filePath, - const std::string fileNamePrefix, - const uint64_t fileIndex, - const bool overWriteEnable, - const bool silentMode, - MasterAttributes *attr) { - // create file name - std::ostringstream os; - os << filePath << "/" << fileNamePrefix << "_master" - << "_" << fileIndex << ".json"; - std::string fileName = os.str(); - - // create file - FILE *fd{nullptr}; - if (!overWriteEnable) { - if (nullptr == (fd = fopen((const char *)fileName.c_str(), "wx"))) { - fd = nullptr; - throw sls::RuntimeError("Could not create binary master file " + - fileName); - } - } else if (nullptr == (fd = fopen((const char *)fileName.c_str(), "w"))) { - fd = nullptr; - throw sls::RuntimeError( - "Could not create/overwrite binary master file " + fileName); - } - - std::string message = BinaryMasterFile::GetMasterAttributes(attr); - if (fwrite((void *)message.c_str(), 1, message.length(), fd) != - message.length()) { - throw sls::RuntimeError( - "Master binary file incorrect number of bytes written to file"); - } - if (fd) { - fclose(fd); - } - if (!silentMode) { - LOG(logINFO) << "Master File: " << fileName; - } - return fileName; -} - -std::string BinaryMasterFile::GetMasterAttributes(MasterAttributes *attr) { - rapidjson::StringBuffer s; - rapidjson::Writer writer(s); - attr->GetBinaryAttributes(&writer); - return s.GetString(); -} diff --git a/slsReceiverSoftware/src/BinaryMasterFile.h b/slsReceiverSoftware/src/BinaryMasterFile.h deleted file mode 100644 index 9bfb4504a..000000000 --- a/slsReceiverSoftware/src/BinaryMasterFile.h +++ /dev/null @@ -1,19 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-other -// Copyright (C) 2021 Contributors to the SLS Detector Package -#pragma once - -#include "MasterAttributes.h" - -class BinaryMasterFile : private virtual slsDetectorDefs { - - public: - static std::string CreateMasterFile(const std::string filePath, - const std::string fileNamePrefix, - const uint64_t fileIndex, - const bool overWriteEnable, - const bool silentMode, - MasterAttributes *attr); - - private: - static std::string GetMasterAttributes(MasterAttributes *attr); -}; \ No newline at end of file diff --git a/slsReceiverSoftware/src/DataProcessor.cpp b/slsReceiverSoftware/src/DataProcessor.cpp index 927213509..4597d80ec 100644 --- a/slsReceiverSoftware/src/DataProcessor.cpp +++ b/slsReceiverSoftware/src/DataProcessor.cpp @@ -13,9 +13,9 @@ #include "Fifo.h" #include "GeneralData.h" #include "MasterAttributes.h" +#include "MasterFileUtility.h" #ifdef HDF5C #include "HDF5DataFile.h" -#include "HDF5Utility.h" #endif #include "DataStreamer.h" #include "sls/container_utils.h" @@ -177,7 +177,7 @@ std::array DataProcessor::CreateVirtualFile( // files (they exist anyway) assumption2: virtual file max frame index // is from R0 P0 (difference from others when missing frames or for a // stop acquisition) - return hdf5Utility::CreateVirtualFile( + return masterFileUtility::CreateVirtualHDF5File( filePath, fileNamePrefix, fileIndex, overWriteEnable, silentMode, modulePos, numUnitsPerReadout, framesPerFile, numImages, generalData_->nPixelsX, generalData_->nPixelsY, dynamicRange, @@ -198,9 +198,9 @@ void DataProcessor::LinkFileInMaster(const std::string &masterFileName, fname = res[0]; datasetName = res[1]; } - hdf5Utility::LinkFileInMaster(masterFileName, fname, datasetName, - dataFile_->GetParameterNames(), silentMode, - hdf5LibMutex); + masterFileUtility::LinkHDF5FileInMaster(masterFileName, fname, datasetName, + dataFile_->GetParameterNames(), + silentMode, hdf5LibMutex); } #endif @@ -216,14 +216,14 @@ std::string DataProcessor::CreateMasterFile( switch (fileFormatType) { #ifdef HDF5C case HDF5: - return hdf5Utility::CreateMasterFile(filePath, fileNamePrefix, - fileIndex, overWriteEnable, - silentMode, attr, hdf5LibMutex); + return masterFileUtility::CreateMasterHDF5File( + filePath, fileNamePrefix, fileIndex, overWriteEnable, silentMode, + attr, hdf5LibMutex); #endif case BINARY: - return BinaryMasterFile::CreateMasterFile(filePath, fileNamePrefix, - fileIndex, overWriteEnable, - silentMode, attr); + return masterFileUtility::CreateMasterBinaryFile( + filePath, fileNamePrefix, fileIndex, overWriteEnable, silentMode, + attr); default: throw sls::RuntimeError("Unknown file format (compile with hdf5 flags"); } diff --git a/slsReceiverSoftware/src/HDF5Utility.h b/slsReceiverSoftware/src/HDF5Utility.h deleted file mode 100644 index c50e3129f..000000000 --- a/slsReceiverSoftware/src/HDF5Utility.h +++ /dev/null @@ -1,43 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-other -// Copyright (C) 2021 Contributors to the SLS Detector Package -#pragma once - -#include "MasterAttributes.h" - -#ifdef HDF5C -#include "H5Cpp.h" -#ifndef H5_NO_NAMESPACE -using namespace H5; -#endif -#endif - -#include - -namespace hdf5Utility { - -void LinkFileInMaster(const std::string &masterFileName, - const std::string &dataFilename, - const std::string &dataSetname, - const std::vector parameterNames, - const bool silentMode, std::mutex *hdf5LibMutex); - -std::string CreateMasterFile(const std::string &filePath, - const std::string &fileNamePrefix, - const uint64_t fileIndex, - const bool overWriteEnable, const bool silentMode, - MasterAttributes *attr, std::mutex *hdf5LibMutex); - -std::array -CreateVirtualFile(const std::string &filePath, - const std::string &fileNamePrefix, const uint64_t fileIndex, - const bool overWriteEnable, const bool silentMode, - const int modulePos, const int numUnitsPerReadout, - const uint32_t maxFramesPerFile, const uint64_t numImages, - const uint32_t nPixelsX, const uint32_t nPixelsY, - const uint32_t dynamicRange, const uint64_t numImagesCaught, - const int numModX, const int numModY, const DataType dataType, - const std::vector parameterNames, - const std::vector parameterDataTypes, - std::mutex *hdf5LibMutex, bool gotthard25um); - -} // namespace hdf5Utility \ No newline at end of file diff --git a/slsReceiverSoftware/src/HDF5Utility.cpp b/slsReceiverSoftware/src/MasterFileUtility.cpp similarity index 77% rename from slsReceiverSoftware/src/HDF5Utility.cpp rename to slsReceiverSoftware/src/MasterFileUtility.cpp index 21891552b..0ae1dc57d 100644 --- a/slsReceiverSoftware/src/HDF5Utility.cpp +++ b/slsReceiverSoftware/src/MasterFileUtility.cpp @@ -1,17 +1,62 @@ // SPDX-License-Identifier: LGPL-3.0-or-other // Copyright (C) 2021 Contributors to the SLS Detector Package -#include "HDF5Utility.h" +#include "MasterFileUtility.h" #include "sls/container_utils.h" #include -namespace hdf5Utility { +namespace masterFileUtility { -void LinkFileInMaster(const std::string &masterFileName, - const std::string &dataFilename, - const std::string &dataSetname, - const std::vector parameterNames, - const bool silentMode, std::mutex *hdf5LibMutex) { +std::string CreateMasterBinaryFile(const std::string filePath, + const std::string fileNamePrefix, + const uint64_t fileIndex, + const bool overWriteEnable, + const bool silentMode, + MasterAttributes *attr) { + // create file name + std::ostringstream os; + os << filePath << "/" << fileNamePrefix << "_master" + << "_" << fileIndex << ".json"; + std::string fileName = os.str(); + + // create file + FILE *fd{nullptr}; + if (!overWriteEnable) { + if (nullptr == (fd = fopen((const char *)fileName.c_str(), "wx"))) { + fd = nullptr; + throw sls::RuntimeError("Could not create binary master file " + + fileName); + } + } else if (nullptr == (fd = fopen((const char *)fileName.c_str(), "w"))) { + fd = nullptr; + throw sls::RuntimeError( + "Could not create/overwrite binary master file " + fileName); + } + + rapidjson::StringBuffer s; + rapidjson::Writer writer(s); + attr->GetBinaryAttributes(&writer); + std::string message = s.GetString(); + if (fwrite((void *)message.c_str(), 1, message.length(), fd) != + message.length()) { + throw sls::RuntimeError( + "Master binary file incorrect number of bytes written to file"); + } + if (fd) { + fclose(fd); + } + if (!silentMode) { + LOG(logINFO) << "Master File: " << fileName; + } + return fileName; +} + +#ifdef HDF5C +void LinkHDF5FileInMaster(const std::string &masterFileName, + const std::string &dataFilename, + const std::string &dataSetname, + const std::vector parameterNames, + const bool silentMode, std::mutex *hdf5LibMutex) { std::lock_guard lock(*hdf5LibMutex); std::unique_ptr fd{nullptr}; @@ -64,11 +109,12 @@ void LinkFileInMaster(const std::string &masterFileName, } } -std::string CreateMasterFile(const std::string &filePath, - const std::string &fileNamePrefix, - const uint64_t fileIndex, - const bool overWriteEnable, const bool silentMode, - MasterAttributes *attr, std::mutex *hdf5LibMutex) { +std::string CreateMasterHDF5File(const std::string &filePath, + const std::string &fileNamePrefix, + const uint64_t fileIndex, + const bool overWriteEnable, + const bool silentMode, MasterAttributes *attr, + std::mutex *hdf5LibMutex) { std::ostringstream os; os << filePath << "/" << fileNamePrefix << "_master" @@ -121,18 +167,17 @@ std::string CreateMasterFile(const std::string &filePath, return fileName; } -std::array -CreateVirtualFile(const std::string &filePath, - const std::string &fileNamePrefix, const uint64_t fileIndex, - const bool overWriteEnable, const bool silentMode, - const int modulePos, const int numUnitsPerReadout, - const uint32_t maxFramesPerFile, const uint64_t numImages, - const uint32_t nPixelsX, const uint32_t nPixelsY, - const uint32_t dynamicRange, const uint64_t numImagesCaught, - const int numModX, const int numModY, const DataType dataType, - const std::vector parameterNames, - const std::vector parameterDataTypes, - std::mutex *hdf5LibMutex, bool gotthard25um) { +std::array CreateVirtualHDF5File( + const std::string &filePath, const std::string &fileNamePrefix, + const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, + const int modulePos, const int numUnitsPerReadout, + const uint32_t maxFramesPerFile, const uint64_t numImages, + const uint32_t nPixelsX, const uint32_t nPixelsY, + const uint32_t dynamicRange, const uint64_t numImagesCaught, + const int numModX, const int numModY, const DataType dataType, + const std::vector parameterNames, + const std::vector parameterDataTypes, std::mutex *hdf5LibMutex, + bool gotthard25um) { // virtual file name std::ostringstream osfn; @@ -308,5 +353,6 @@ CreateVirtualFile(const std::string &filePath, } return std::array{fileName, dataSetName}; } +#endif -} // namespace hdf5Utility +} // namespace masterFileUtility diff --git a/slsReceiverSoftware/src/MasterFileUtility.h b/slsReceiverSoftware/src/MasterFileUtility.h new file mode 100644 index 000000000..d0a74b04f --- /dev/null +++ b/slsReceiverSoftware/src/MasterFileUtility.h @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: LGPL-3.0-or-other +// Copyright (C) 2021 Contributors to the SLS Detector Package +#pragma once + +#include "MasterAttributes.h" + +#ifdef HDF5C +#include "H5Cpp.h" +#include +#ifndef H5_NO_NAMESPACE +using namespace H5; +#endif +#endif + +namespace masterFileUtility { + +std::string CreateMasterBinaryFile(const std::string filePath, + const std::string fileNamePrefix, + const uint64_t fileIndex, + const bool overWriteEnable, + const bool silentMode, + MasterAttributes *attr); + +#ifdef HDF5C +void LinkHDF5FileInMaster(const std::string &masterFileName, + const std::string &dataFilename, + const std::string &dataSetname, + const std::vector parameterNames, + const bool silentMode, std::mutex *hdf5LibMutex); + +std::string CreateMasterHDF5File(const std::string &filePath, + const std::string &fileNamePrefix, + const uint64_t fileIndex, + const bool overWriteEnable, + const bool silentMode, MasterAttributes *attr, + std::mutex *hdf5LibMutex); + +std::array CreateVirtualHDF5File( + const std::string &filePath, const std::string &fileNamePrefix, + const uint64_t fileIndex, const bool overWriteEnable, const bool silentMode, + const int modulePos, const int numUnitsPerReadout, + const uint32_t maxFramesPerFile, const uint64_t numImages, + const uint32_t nPixelsX, const uint32_t nPixelsY, + const uint32_t dynamicRange, const uint64_t numImagesCaught, + const int numModX, const int numModY, const DataType dataType, + const std::vector parameterNames, + const std::vector parameterDataTypes, std::mutex *hdf5LibMutex, + bool gotthard25um); +#endif +} // namespace masterFileUtility \ No newline at end of file