Commit 41c827b2 authored by David Moxey's avatar David Moxey Committed by Spencer Sherwin
Browse files

Add ability to guess input format from brief inspection of file

parent 5c3b9808
......@@ -54,28 +54,6 @@ ModuleKey InputNek5000::m_className[1] = {
"Reads Nek5000 field file.")
};
/**
* @brief Swap endian ordering of the input variable.
*/
template <typename T>
void swap_endian(T &u)
{
union
{
T u;
unsigned char u8[sizeof(T)];
} source, dest;
source.u = u;
for (size_t k = 0; k < sizeof(T); k++)
{
dest.u8[k] = source.u8[sizeof(T) - k - 1];
}
u = dest.u;
}
/**
* @brief Set up InputNek5000 object.
*
......
......@@ -39,6 +39,7 @@
#include <vector>
using namespace std;
#include <LibUtilities/BasicUtils/CompressData.h>
#include <boost/algorithm/string.hpp>
#include "InputSemtex.h"
......@@ -54,28 +55,6 @@ ModuleKey InputSemtex::m_className[1] = {
"Reads Semtex field file.")
};
/**
* @brief Swap endian ordering of the input variable.
*/
template <typename T>
void swap_endian(T &u)
{
union
{
T u;
unsigned char u8[sizeof(T)];
} source, dest;
source.u = u;
for (size_t k = 0; k < sizeof(T); k++)
{
dest.u8[k] = source.u8[sizeof(T) - k - 1];
}
u = dest.u;
}
/**
* @brief Set up InputSemtex object.
*
......@@ -113,7 +92,7 @@ void InputSemtex::Process(po::variables_map &vm)
}
}
string sessionName, date, fields;
string sessionName, date, fields, endian;
int nr, ns, nz, nelmt, step;
NekDouble time, dt, kinvis, beta;
......@@ -167,8 +146,24 @@ void InputSemtex::Process(po::variables_map &vm)
getline(file, line);
// TODO Endian-ness
LibUtilities::EndianType systemEndian = LibUtilities::Endianness();
std::string endianSearch;
if (systemEndian == LibUtilities::eEndianBig)
{
endianSearch = "big";
}
else if (systemEndian == LibUtilities::eEndianLittle)
{
endianSearch = "little";
}
else
{
ASSERTL0(false, "Only little- or big-endian systems are supported");
}
file.read(buf, 25);
bool byteSwap = false;
endian = string(buf, 25);
bool byteSwap = endian.find(endianSearch) == string::npos;
getline(file, line);
// Print some basic information for input if in verbose mode.
......@@ -266,15 +261,22 @@ void InputSemtex::Process(po::variables_map &vm)
size_t elSizeJ = j * elmtSize;
file.read((char *)&tmp[0], planeSize * sizeof(NekDouble));
if (byteSwap)
{
swap_endian(tmp);
}
for (int k = 0; k < nelmt; ++k)
{
std::copy(&tmp[k * elmtSize], &tmp[(k+1) * elmtSize],
data + k * offset + elSizeJ);
}
}
}
m_f->m_fielddef.push_back(fielddef);
}
}
}
......@@ -157,6 +157,37 @@ void Module::SetDefaults()
}
}
/**
* @brief Tries to guess the format of the input file.
*/
string InputModule::GuessFormat(string filename)
{
// Read first 64 bytes of data, assuming input is this long.
ifstream inFile(filename.c_str(), ios::binary);
vector<char> data(64, 0);
inFile.read(&data[0], 64);
string check1(&data[0], 64);
// Nek5000 format: first four characters are: #std
if (data[0] == '#' && data[1] == 's' && data[2] == 't' && data[3] == 'd')
{
inFile.close();
return "fld5000";
}
// Semtex format: first line should contain the string "Session"
if (check.find("Session") != string::npos)
{
inFile.close();
return "fldsem";
}
// Otherwise don't really know -- try to guess from file extension.
inFile.close();
return "";
}
/**
* @brief Print a brief summary of information.
*/
......
......@@ -75,6 +75,38 @@ enum ModuleType
const char *const ModuleTypeMap[] = {"Input", "Process", "Output"};
/**
* @brief Swap endian ordering of the input variable.
*/
template <typename T>
void swap_endian(T &u)
{
union
{
T u;
unsigned char u8[sizeof(T)];
} source, dest;
source.u = u;
for (size_t k = 0; k < sizeof(T); k++)
{
dest.u8[k] = source.u8[sizeof(T) - k - 1];
}
u = dest.u;
}
template <typename T>
void swap_endian(vector<T> &u)
{
size_t vecSize = u.size();
for (int i = 0; i < vecSize; ++i)
{
swap_endian(u[i]);
}
}
/**
* @brief Represents a command-line configuration option.
*/
......@@ -187,6 +219,7 @@ class InputModule : public Module
public:
InputModule(FieldSharedPtr p_m);
FIELD_UTILS_EXPORT void AddFile(string fileType, string fileName);
FIELD_UTILS_EXPORT static string GuessFormat(string fileName);
protected:
/// Print summary of elements.
......
......@@ -284,19 +284,43 @@ int main(int argc, char* argv[])
// filename.xml:vtk:opt1=arg1:opt2=arg2
if (tmp1.size() == 1)
{
int dot = tmp1[0].find_last_of('.') + 1;
string ext = tmp1[0].substr(dot, tmp1[0].length() - dot);
// First, let's try to guess the input format if we're dealing
// with input files.
string guess;
if(ext == "gz")
if (i < nInput)
{
string tmp2 = tmp1[0].substr(0,dot-1);
dot = tmp2.find_last_of('.') + 1;
ext = tmp1[0].substr(dot,tmp1[0].length()-dot);
guess = InputModule::GuessFormat(tmp1[0]);
}
module.second = ext;
tmp1.push_back(string(i < nInput ? "infile=" : "outfile=")
+tmp1[0]);
// Found file type.
if (guess != "")
{
if (f->m_verbose)
{
cout << "Using input module " << guess << " for: "
<< tmp1[0] << endl;
}
module.second = guess;
tmp1.push_back(string("infile="+tmp1[0]));
}
else
{
int dot = tmp1[0].find_last_of('.') + 1;
string ext = tmp1[0].substr(dot, tmp1[0].length() - dot);
if(ext == "gz")
{
string tmp2 = tmp1[0].substr(0,dot-1);
dot = tmp2.find_last_of('.') + 1;
ext = tmp1[0].substr(dot,tmp1[0].length()-dot);
}
module.second = ext;
tmp1.push_back(string(i < nInput ? "infile=" : "outfile=")
+tmp1[0]);
}
}
else
{
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment