wip rewrite

This commit is contained in:
maliakal_d 2021-06-29 20:45:52 +02:00
parent ee1a1563ba
commit d7dc1912ac
12 changed files with 481 additions and 0 deletions

View File

@ -0,0 +1,13 @@
#include "BinaryDataFile.h"
#include "sls/logger.h"
BinaryDataFile::BinaryDataFile(int index) : File(index, BINARY) {}
BinaryDataFile::~BinaryDataFile() { CloseFile(); }
void BinaryDataFile::CloseFile() {
if (fd_) {
fclose(fd_);
}
fd_ = nullptr;
}

View File

@ -0,0 +1,15 @@
#pragma once
#include "File.h"
class BinaryDataFile : private virtual slsDetectorDefs, public File {
public:
BinaryDataFile(int index);
~BinaryDataFile();
void CloseFile() override;
private:
FILE *fd_{nullptr};
};

View File

@ -0,0 +1,46 @@
#include "BinaryMasterFile.h"
#include "MasterAttributes.h"
#include "sls/logger.h"
BinaryMasterFile::BinaryMasterFile(int index) : File(index, BINARY) {}
BinaryMasterFile::~BinaryMasterFile() { CloseFile(); }
void BinaryMasterFile::CloseFile() {
if (fd_) {
fclose(fd_);
}
fd_ = nullptr;
}
void BinaryMasterFile::CreateMasterFile(MasterAttributes *attr,
std::string filePath,
std::string fileNamePrefix,
uint64_t fileIndex,
bool overWriteEnable, bool silentMode) {
// create file name
std::ostringstream os;
os << filePath << "/" << fileNamePrefix << "_master"
<< "_" << fileIndex << ".raw";
fileName_ = os.str();
if (!(silentMode)) {
LOG(logINFO) << "Master File: " << fileName_;
}
// create file
if (!(overWriteEnable)) {
if (nullptr == (fd_ = fopen((const char *)fileName_.c_str(), "wx"))) {
fd_ = nullptr;
throw sls::RuntimeError(
"Could not create binary master file (no overwrite) " +
fileName_);
}
} else if (nullptr == (fd_ = fopen((const char *)fileName_.c_str(), "w"))) {
fd_ = nullptr;
throw sls::RuntimeError(
"Could not create binary master file (overwrite) " + fileName_);
}
attr->WriteMasterBinaryAttributes(fd_);
CloseFile();
}

View File

@ -0,0 +1,20 @@
#pragma once
#include "File.h"
#include "MasterAttributes.h"
class BinaryMasterFile : private virtual slsDetectorDefs, public File {
public:
BinaryMasterFile(int index);
~BinaryMasterFile();
void CloseFile() override;
void CreateMasterFile(MasterAttributes *attr, std::string filePath,
std::string fileNamePrefix, uint64_t fileIndex,
bool overWriteEnable, bool silentMode) override;
private:
FILE *fd_{nullptr};
std::string fileName_;
};

View File

@ -0,0 +1,71 @@
/************************************************
* @file File.cpp
* @short sets/gets properties for the file,
* creates/closes the file and writes data to it
***********************************************/
#include "File.h"
#include <iostream>
File::File(int ind, slsDetectorDefs::fileFormat type, uint32_t *maxf, int *nd,
std::string *fname, std::string *fpath, uint64_t *findex,
bool *owenable, int *dindex, int *nunits, uint64_t *nf, uint32_t *dr,
uint32_t *portno, bool *smode)
: index(ind), formatType(type), maxFramesPerFile(maxf), numDetX(nd[0]),
numDetY(nd[1]), fileNamePrefix(fname), filePath(fpath), fileIndex(findex),
overWriteEnable(owenable), detIndex(dindex), numUnitsPerDetector(nunits),
numImages(nf), dynamicRange(dr), udpPortNumber(portno), silentMode(smode)
{
master = ((index == 0) && (*detIndex == 0)) ? true : false;
}
File::~File() {}
slsDetectorDefs::fileFormat File::GetFileType() { return formatType; }
std::string File::GetCurrentFileName() { return currentFileName; }
void File::resetSubFileIndex() { subFileIndex = 0u; }
void File::PrintMembers(TLogLevel level) {
LOG(level) << "\nGeneral Writer Variables:" << std::endl
<< "Index: " << index << std::endl
<< "Max Frames Per File: " << *maxFramesPerFile << std::endl
<< "Number of Detectors in x dir: " << numDetX << std::endl
<< "Number of Detectors in y dir: " << numDetY << std::endl
<< "File Name Prefix: " << fileNamePrefix << std::endl
<< "File Path: " << filePath << std::endl
<< "File Index: " << *fileIndex << std::endl
<< "Over Write Enable: " << *overWriteEnable << std::endl
<< "Detector Index: " << *detIndex << std::endl
<< "Number of Units Per Detector: " << *numUnitsPerDetector
<< std::endl
<< "Number of Images in Acquisition: " << *numImages << std::endl
<< "Dynamic Range: " << *dynamicRange << std::endl
<< "UDP Port number: " << *udpPortNumber << std::endl
<< "Master File Name: " << masterFileName << std::endl
<< "Current File Name: " << currentFileName << std::endl
<< "Silent Mode: " << *silentMode;
}
void File::GetMemberPointerValues(int *nd, uint32_t *&maxf, std::string *&fname,
std::string *&fpath, uint64_t *&findex,
bool *&owenable, int *&dindex, int *&nunits,
uint64_t *&nf, uint32_t *&dr,
uint32_t *&portno) {
nd[0] = numDetX;
nd[1] = numDetY;
maxf = maxFramesPerFile;
fname = fileNamePrefix;
fpath = filePath;
findex = fileIndex;
owenable = overWriteEnable;
dindex = detIndex;
nunits = numUnitsPerDetector;
nf = numImages;
dr = dynamicRange;
portno = udpPortNumber;
}

View File

@ -0,0 +1,142 @@
#pragma once
/************************************************
* @file File.h
* @short sets/gets properties for the file,
* creates/closes the file and writes data to it
***********************************************/
/**
*@short sets/gets properties for the file, creates/closes the file and writes
*data to it
*/
#include "receiver_defs.h"
#include "sls/logger.h"
#include "sls/sls_detector_defs.h"
#include <string>
struct MasterAttributes;
class File : private virtual slsDetectorDefs {
public:
/**
* Constructor
* creates the File Writer
* @param ind self index
* @param type file format type
* @param maxf pointer to max frames per file
* @param nd pointer to number of detectors in each dimension
* @param fname pointer to file name prefix
* @param fpath pointer to file path
* @param findex pointer to file index
* @param owenable pointer to over write enable
* @param dindex pointer to detector index
* @param nunits pointer to number of theads/ units per detector
* @param nf pointer to number of images in acquisition
* @param dr pointer to dynamic range
* @param portno pointer to udp port number for logging
* @param smode pointer to silent mode
*/
File(int ind, slsDetectorDefs::fileFormat type, uint32_t *maxf, int *nd,
std::string *fname, std::string *fpath, uint64_t *findex,
bool *owenable, int *dindex, int *nunits, uint64_t *nf, uint32_t *dr,
uint32_t *portno, bool *smode);
virtual ~File();
fileFormat GetFileType();
std::string GetCurrentFileName();
void resetSubFileIndex();
virtual void PrintMembers(TLogLevel level = logDEBUG1);
/**
* Get Member Pointer Values before the object is destroyed
* @param nd pointer to number of detectors in each dimension
* @param maxf pointer to max frames per file
* @param fname pointer to file name prefix
* @param fpath pointer to file path
* @param findex pointer to file index
* @param owenable pointer to over write enable
* @param dindex pointer to detector index
* @param nunits pointer to number of theads/ units per detector
* @param nf pointer to number of images in acquisition
* @param dr pointer to dynamic range
* @param portno pointer to dynamic range
*/
void GetMemberPointerValues(int *nd, uint32_t *&maxf, std::string *&fname,
std::string *&fpath, uint64_t *&findex,
bool *&owenable, int *&dindex, int *&nunits,
uint64_t *&nf, uint32_t *&dr,
uint32_t *&portno);
virtual void CreateFile() = 0;
virtual void CloseCurrentDataFile() = 0;
virtual void CloseAllFiles() = 0;
virtual void CloseMasterFile() = 0;
/**
* Write data to file
* @param buffer buffer to write from
* @param buffersize size of buffer
* @param fnum current image number
* @param nump number of packets caught
*/
virtual void WriteToFile(char *buffer, int buffersize, uint64_t fnum,
uint32_t nump) = 0;
/**
* Create master file
* @param attr master file attributes
*/
virtual void CreateMasterFile(MasterAttributes *attr) = 0;
// HDf5 specific
/**
* Set Number of pixels
* @param nx number of pixels in x direction
* @param ny number of pixels in y direction
*/
virtual void SetNumberofPixels(uint32_t nx, uint32_t ny) {
LOG(logERROR) << "This is a generic function SetNumberofPixels that "
"should be overloaded by a derived class";
}
/**
* Start of Acquisition
*/
virtual void StartofAcquisition() {
LOG(logERROR) << "This is a generic function StartofAcquisition that "
"should be overloaded by a derived class";
}
/**
* End of Acquisition
* @param anyPacketsCaught true if any packets are caught, else false
* @param numf number of images caught
*/
virtual void EndofAcquisition(bool anyPacketsCaught, uint64_t numf) {
LOG(logERROR) << "This is a generic function EndofAcquisition that "
"should be overloaded by a derived class";
}
protected:
bool master;
int index;
slsDetectorDefs::fileFormat formatType;
uint32_t *maxFramesPerFile;
std::string masterFileName;
std::string currentFileName;
int numDetX;
int numDetY;
std::string *fileNamePrefix;
std::string *filePath;
uint64_t *fileIndex;
uint64_t subFileIndex{0};
bool *overWriteEnable;
int *detIndex;
int *numUnitsPerDetector;
uint64_t *numImages;
uint32_t *dynamicRange;
uint32_t *udpPortNumber;
bool *silentMode;
};

View File

@ -0,0 +1,21 @@
#include "HDF5DataFile.h"
#include "sls/logger.h"
HDF5DataFile::HDF5DataFile(int index) : File(index, HDF5) {}
HDF5DataFile::~HDF5DataFile() { CloseFile(); }
void HDF5DataFile::CloseFile() {
try {
Exception::dontPrint(); // to handle errors
if (fd_) {
fd_->close();
delete fd_;
fd_ = nullptr;
}
} catch (const Exception &error) {
LOG(logERROR) << "Could not close data HDF5 handles of index "
<< index_;
error.printErrorStack();
}
}

View File

@ -0,0 +1,20 @@
#pragma once
#include "File.h"
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
class HDF5DataFile : private virtual slsDetectorDefs, public File {
public:
HDF5DataFile(int index);
~HDF5DataFile();
void CloseFile() override;
private:
H5File *fd_{nullptr};
};

View File

@ -0,0 +1,71 @@
#include "HDF5MasterFile.h"
#include "MasterAttributes.h"
#include "sls/logger.h"
HDF5MasterFile::HDF5MasterFile(int index, std::mutex *hdf5Lib)
: File(index, HDF5), hdf5Lib_(hdf5Lib) {}
HDF5MasterFile::~HDF5MasterFile() { CloseFile(); }
void HDF5MasterFile::CloseFile() {
try {
Exception::dontPrint(); // to handle errors
if (fd_) {
fd_->close();
delete fd_;
fd_ = nullptr;
}
} catch (const Exception &error) {
LOG(logERROR) << "Could not close data HDF5 handles of index "
<< index_;
error.printErrorStack();
}
}
void HDF5MasterFile::CreateMasterFile(MasterAttributes *attr,
std::string filePath,
std::string fileNamePrefix,
uint64_t fileIndex, bool overWriteEnable,
bool silentMode) {
std::ostringstream os;
os << filePath << "/" << fileNamePrefix << "_master"
<< "_" << fileIndex << ".h5";
fileName_ = os.str();
if (!(silentMode)) {
LOG(logINFO) << "Master File: " << fileName_;
}
std::lock_guard<std::mutex> lock(*hdf5Lib_);
try {
Exception::dontPrint(); // to handle errors
FileAccPropList flist;
flist.setFcloseDegree(H5F_CLOSE_STRONG);
fd_ = nullptr;
if (!(overWriteEnable))
fd_ = new H5File(fileName_.c_str(), H5F_ACC_EXCL,
FileCreatPropList::DEFAULT, flist);
else
fd_ = new H5File(fileName_.c_str(), H5F_ACC_TRUNC,
FileCreatPropList::DEFAULT, flist);
// Create a group in the file
Group group1(fd_->createGroup("entry"));
Group group2(group1.createGroup("data"));
Group group3(group1.createGroup("instrument"));
Group group4(group3.createGroup("beam"));
Group group5(group3.createGroup("detector"));
Group group6(group1.createGroup("sample"));
attr->WriteMasterHDF5Attributes(fd_, &group5);
fd_->close();
} catch (const Exception &error) {
error.printErrorStack();
CloseFile();
throw sls::RuntimeError("Could not create master HDF5 handles");
}
}

View File

@ -0,0 +1,27 @@
#pragma once
#include "File.h"
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
#include <mutex>
class HDF5MasterFile : private virtual slsDetectorDefs, public File {
public:
HDF5MasterFile(int index, std::mutex *hdf5Lib);
~HDF5MasterFile();
void CloseFile() override;
void CreateMasterFile(MasterAttributes *attr, std::string filePath,
std::string fileNamePrefix, uint64_t fileIndex,
bool overWriteEnable, bool silentMode) override;
private:
std::mutex *hdf5Lib_;
H5File *fd_{nullptr};
std::string fileName_;
};

View File

@ -0,0 +1,15 @@
#include "HDF5VirtualFile.h"
#include "sls/logger.h"
HDF5VirtualFile::HDF5VirtualFile(int index) : File(index, HDF5), fd_(0) {}
HDF5VirtualFile::~HDF5VirtualFile() { CloseFile(); }
void HDF5VirtualFile::CloseFile() {
if (fd_ != 0) {
if (H5Fclose(fd_) < 0) {
LOG(logERROR) << "Could not close virtual HDF5 handles";
}
fd_ = 0;
}
}

View File

@ -0,0 +1,20 @@
#pragma once
#include "File.h"
#include "H5Cpp.h"
#ifndef H5_NO_NAMESPACE
using namespace H5;
#endif
class HDF5VirtualFile : private virtual slsDetectorDefs, public File {
public:
HDF5VirtualFile(int index);
~HDF5VirtualFile();
void CloseFile() override;
private:
hid_t fd_;
};