ENH: Updated CSV DataEntry

This commit is contained in:
andy
2013-01-28 16:46:59 +00:00
parent 0c6a9870f0
commit 3753faf147
3 changed files with 109 additions and 28 deletions

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2012-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -87,36 +87,83 @@ void Foam::CSV<Type>::read()
DynamicList<Tuple2<scalar, Type> > values;
// skip header
if (headerLine_)
for (label i = 0; i < nHeaderLine_; i++)
{
string line;
is.getLine(line);
}
label nEntries = max(componentColumns_);
// read data
while (is.good())
{
string line;
is.getLine(line);
label n = 0;
std::size_t pos = 0;
DynamicList<string> splitted;
std::size_t pos = 0;
while (pos != std::string::npos)
if (mergeSeparators_)
{
std::size_t nPos = line.find(separator_, pos);
std::size_t nPos = 0;
if (nPos == std::string::npos)
while ((pos != std::string::npos) && (n <= nEntries))
{
splitted.append(line.substr(pos));
pos = nPos;
}
else
{
splitted.append(line.substr(pos, nPos - pos));
pos = nPos + 1;
bool found = false;
while (!found)
{
nPos = line.find(separator_, pos);
if ((nPos != std::string::npos) && (nPos - pos == 0))
{
pos = nPos + 1;
}
else
{
found = true;
}
}
nPos = line.find(separator_, pos);
if (nPos == std::string::npos)
{
splitted.append(line.substr(pos));
pos = nPos;
n++;
}
else
{
splitted.append(line.substr(pos, nPos - pos));
pos = nPos + 1;
n++;
}
}
}
else
{
while ((pos != std::string::npos) && (n <= nEntries))
{
std::size_t nPos = line.find(separator_, pos);
if (nPos == std::string::npos)
{
splitted.append(line.substr(pos));
pos = nPos;
n++;
}
else
{
splitted.append(line.substr(pos, nPos - pos));
pos = nPos + 1;
n++;
}
}
}
if (splitted.size() <= 1)
{
@ -136,15 +183,21 @@ void Foam::CSV<Type>::read()
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::CSV<Type>::CSV(const word& entryName, const dictionary& dict)
Foam::CSV<Type>::CSV
(
const word& entryName,
const dictionary& dict,
const word& ext
)
:
DataEntry<Type>(entryName),
TableBase<Type>(entryName, dict.subDict(type() + "Coeffs")),
coeffs_(dict.subDict(type() + "Coeffs")),
headerLine_(readBool(coeffs_.lookup("hasHeaderLine"))),
TableBase<Type>(entryName, dict.subDict(type() + ext)),
coeffs_(dict.subDict(type() + ext)),
nHeaderLine_(readLabel(coeffs_.lookup("nHeaderLine"))),
refColumn_(readLabel(coeffs_.lookup("refColumn"))),
componentColumns_(coeffs_.lookup("componentColumns")),
separator_(coeffs_.lookupOrDefault<string>("separator", string(","))[0]),
mergeSeparators_(readBool(coeffs_.lookup("mergeSeparators"))),
fName_(coeffs_.lookup("fileName"))
{
if (componentColumns_.size() != pTraits<Type>::nComponents)
@ -166,10 +219,11 @@ Foam::CSV<Type>::CSV(const CSV<Type>& tbl)
:
DataEntry<Type>(tbl),
TableBase<Type>(tbl),
headerLine_(tbl.headerLine_),
nHeaderLine_(tbl.nHeaderLine_),
refColumn_(tbl.refColumn_),
componentColumns_(tbl.componentColumns_),
separator_(tbl.separator_),
mergeSeparators_(tbl.mergeSeparators_),
fName_(tbl.fName_)
{}
@ -181,6 +235,15 @@ Foam::CSV<Type>::~CSV()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
const Foam::fileName& Foam::CSV<Type>::fName() const
{
return fName_;
}
// * * * * * * * * * * * * * * IOStream operators * * * * * * * * * * * * * //
#include "CSVIO.C"

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -32,10 +32,11 @@ Description
<entryName> csvFile;
csvFileCoeffs
{
hasHeaderLine true;
nHeaderLines 4;
refColumn 0; // reference column index
componentColumns (1 2 3); // component column indices
separator ","; // optional (defaults to ",")
mergeSeparators no; // merge multiple separators
fileName "fileXYZ"; // name of csv data file
outOfBounds clamp; // optional out-of-bounds handling
interpolationScheme linear; // optional interpolation scheme
@ -86,8 +87,8 @@ class CSV
//- Coefficients dictionary (for convenience on reading)
dictionary coeffs_;
//- Does the file have a header line?
bool headerLine_;
//- Number header lines
label nHeaderLine_;
//- Column of the time
label refColumn_;
@ -98,7 +99,10 @@ class CSV
//- Separator character
char separator_;
//- File name for csv table (optional)
//- Merge separators flag, e.g. ',,,' becomes ','
bool mergeSeparators_;
//- File name for csv table
fileName fName_;
@ -122,8 +126,13 @@ public:
// Constructors
//- Construct from entry name and Istream
CSV(const word& entryName, const dictionary& dict);
//- Construct from entry name and dictionary
CSV
(
const word& entryName,
const dictionary& dict,
const word& ext = "Ceoffs"
);
//- Copy constructor
CSV(const CSV<Type>& tbl);
@ -150,6 +159,12 @@ public:
}
// Access
//- Return const access to the file name
virtual const fileName& fName() const;
// Evaluation
//- Return Table value

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2012-2013 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -37,10 +37,11 @@ Foam::Ostream& Foam::operator<<
if (os.format() == IOstream::ASCII)
{
os << static_cast<const DataEntry<Type>& >(tbl)
<< token::SPACE << tbl.headerLine_
<< token::SPACE << tbl.nHeaderLine_
<< token::SPACE << tbl.timeColumn_
<< token::SPACE << tbl.componentColumns_
<< token::SPACE << tbl.separator_
<< token::SPACE << tbl.mergeSeparators_
<< token::SPACE << tbl.fileName_;
}
else
@ -70,13 +71,15 @@ void Foam::CSV<Type>::writeData(Ostream& os) const
// the values themselves
TableBase<Type>::writeEntries(os);
os.writeKeyword("hasHeaderLine") << headerLine_ << token::END_STATEMENT
os.writeKeyword("nHeaderLine") << nHeaderLine_ << token::END_STATEMENT
<< nl;
os.writeKeyword("refColumn") << refColumn_ << token::END_STATEMENT << nl;
os.writeKeyword("componentColumns") << componentColumns_
<< token::END_STATEMENT << nl;
os.writeKeyword("separator") << string(separator_)
<< token::END_STATEMENT << nl;
os.writeKeyword("mergeSeparators") << string(mergeSeparators_)
<< token::END_STATEMENT << nl;
os.writeKeyword("fileName") << fName_ << token::END_STATEMENT << nl;
os << decrIndent << indent << token::END_BLOCK << endl;
}