Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Nektar
Nektar
Commits
41c827b2
Commit
41c827b2
authored
Apr 22, 2017
by
David Moxey
Committed by
Spencer Sherwin
Jun 07, 2017
Browse files
Add ability to guess input format from brief inspection of file
parent
5c3b9808
Changes
5
Hide whitespace changes
Inline
Side-by-side
library/FieldUtils/InputModules/InputNek5000.cpp
View file @
41c827b2
...
...
@@ -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.
*
...
...
library/FieldUtils/InputModules/InputSemtex.cpp
View file @
41c827b2
...
...
@@ -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
);
}
}
}
library/FieldUtils/Module.cpp
View file @
41c827b2
...
...
@@ -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.
*/
...
...
library/FieldUtils/Module.h
View file @
41c827b2
...
...
@@ -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.
...
...
utilities/FieldConvert/FieldConvert.cpp
View file @
41c827b2
...
...
@@ -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
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment