Skip to content
Snippets Groups Projects
Commit 7bd7dddf authored by Juan Manuel Carmona Loaiza's avatar Juan Manuel Carmona Loaiza :ghost:
Browse files

Default Read/Write to ASCII matrices of numbers.

parent c8473a18
No related branches found
No related tags found
No related merge requests found
......@@ -41,7 +41,6 @@ const std::vector<std::pair<std::string, createAxisFun>> type_map = {
const std::string GzipExtension = ".gz";
const std::string BzipExtension = ".bz2";
const std::string IntExtension = ".int";
const std::string TxtExtension = ".txt";
const std::string TiffExtension = ".tif";
const std::string TiffExtension2 = ".tiff";
}
......@@ -77,27 +76,11 @@ std::string DataFormatUtils::GetFileMainExtension(const std::string& name)
return FileSystemUtils::extension(stripped_name);
}
bool DataFormatUtils::isBinaryFile(const std::string& file_name)
{
// all compressed files are always binary.
if(isCompressed(file_name)) return true;
// uncompressed "int" or "txt" files are ascii
if(isIntFile(file_name)) return false;
if(isTxtFile(file_name)) return false;
// the rest (e.g. tif) is also binary
return true;
}
bool DataFormatUtils::isIntFile(const std::string& file_name)
{
return GetFileMainExtension(file_name) == IntExtension;
}
bool DataFormatUtils::isTxtFile(const std::string& file_name)
{
return GetFileMainExtension(file_name) == TxtExtension;
}
bool DataFormatUtils::isTiffFile(const std::string& file_name)
{
return (GetFileMainExtension(file_name) == TiffExtension ||
......
......@@ -38,15 +38,9 @@ BA_CORE_API_ bool isBZipped(const std::string& name);
//! Returns file extension after stripping '.gz' if any
BA_CORE_API_ std::string GetFileMainExtension(const std::string& name);
//! returns true if file name corresponds to a binary file
BA_CORE_API_ bool isBinaryFile(const std::string& file_name);
//! returns true if file name corresponds to BornAgain native format (compressed or not)
BA_CORE_API_ bool isIntFile(const std::string& file_name);
//! returns true if file name corresponds to simple numpy-style ASCII file
BA_CORE_API_ bool isTxtFile(const std::string& file_name);
//! returns true if file name corresponds to tiff file (can be also compressed)
BA_CORE_API_ bool isTiffFile(const std::string& file_name);
......
......@@ -31,13 +31,14 @@ OutputDataReader::OutputDataReader(const std::string& file_name)
OutputData<double>* OutputDataReader::getOutputData()
{
using namespace DataFormatUtils;
if(!m_read_strategy)
throw Exceptions::NullPointerException(
"OutputDataReader::getOutputData() -> Error! No read strategy defined");
std::ifstream fin;
std::ios_base::openmode openmode = std::ios::in;
if (DataFormatUtils::isBinaryFile(m_file_name))
if(isTiffFile(m_file_name) || isCompressed(m_file_name))
openmode = std::ios::in | std::ios_base::binary;
fin.open(m_file_name.c_str(), openmode );
......
......@@ -25,14 +25,11 @@ OutputDataWriter *OutputDataWriteFactory::getWriter(const std::string &file_name
IOutputDataWriteStrategy *OutputDataWriteFactory::getWriteStrategy(const std::string &file_name)
{
IOutputDataWriteStrategy *result(0);
IOutputDataWriteStrategy *result(nullptr);
if(DataFormatUtils::isIntFile(file_name)) {
result = new OutputDataWriteINTStrategy();
}
else if(DataFormatUtils::isTxtFile(file_name)) {
result = new OutputDataWriteNumpyTXTStrategy();
}
#ifdef BORNAGAIN_TIFF_SUPPORT
else if(DataFormatUtils::isTiffFile(file_name)) {
......@@ -40,10 +37,10 @@ IOutputDataWriteStrategy *OutputDataWriteFactory::getWriteStrategy(const std::st
}
#endif // BORNAGAIN_TIFF_SUPPORT
else {
throw Exceptions::LogicErrorException("OutputDataWriteFactory::getWriter() -> Error. "
"Don't know how to write file '" + file_name+std::string("'"));
else{
result = new OutputDataWriteNumpyTXTStrategy();
}
return result;
}
......@@ -32,14 +32,16 @@ OutputDataWriter::OutputDataWriter(const std::string& file_name)
void OutputDataWriter::writeOutputData(const OutputData<double>& data)
{
using namespace DataFormatUtils;
if(!m_write_strategy)
throw Exceptions::NullPointerException("OutputDataWriter::getOutputData() ->"
" Error! No read strategy defined");
std::ofstream fout;
std::ios_base::openmode openmode = std::ios::out;
if(DataFormatUtils::isBinaryFile(m_file_name))
if(isTiffFile(m_file_name) || isCompressed(m_file_name))
openmode = std::ios::out | std::ios_base::binary;
fout.open(m_file_name.c_str(), openmode );
if(!fout.is_open())
throw Exceptions::FileNotIsOpenException("OutputDataWriter::writeOutputData() -> Error. "
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment