46 #include "ColumnDataWriter.hpp" 49 #include "Version.hpp" 53 const std::string& rBaseName,
56 : mOutputFileHandler(rDirectory, cleanDirectory),
57 mDirectory(rDirectory),
59 mIsInDefineMode(true),
60 mIsFixedDimensionSet(false),
61 mIsUnlimitedDimensionSet(false),
62 mUnlimitedDimensionPosition(0),
63 mFixedDimensionSize(-1),
64 mpCurrentOutputFile(nullptr),
65 mpCurrentAncillaryFile(nullptr),
66 mpUnlimitedDimensionVariable(nullptr),
67 mpFixedDimensionVariable(nullptr),
68 mFieldWidth(precision+8),
69 mPrecision(precision),
70 mHasPutVariable(false),
71 mNeedAdvanceAlongUnlimitedDimension(false),
72 mCommentForInfoFile(
"")
74 if (mPrecision<2 || mPrecision>20)
76 EXCEPTION(
"Precision must be between 2 and 20 (inclusive)");
118 if (rName.length() == 0)
120 EXCEPTION(
"Variable name not allowed: may not be blank.");
127 for (
unsigned i=0; i<rName.length(); i++)
129 if (!isalnum(rName[i]) && !(rName[i]==
'_'))
131 std::string error =
"Variable name/units '" + rName +
"' not allowed: may only contain alphanumeric characters or '_'.";
138 const std::string& rDimensionUnits)
142 EXCEPTION(
"Unlimited dimension already set. Cannot be defined twice");
147 EXCEPTION(
"Cannot define variables when not in Define mode");
166 const std::string& rDimensionUnits,
171 EXCEPTION(
"Cannot define variables when not in Define mode");
173 if (dimensionSize < 1)
175 EXCEPTION(
"Fixed dimension must be at least 1 long");
194 const std::string& rVariableUnits)
198 EXCEPTION(
"Cannot define variables when not in Define mode");
208 EXCEPTION(
"Variable name: " + rVariableName +
" already in use as unlimited dimension");
212 EXCEPTION(
"Variable name: " + rVariableName +
" already in use as fixed dimension");
235 EXCEPTION(
"Cannot end define mode. No dimensions have been defined.");
240 EXCEPTION(
"Cannot end define mode. No variables have been defined.");
253 std::stringstream suffix;
258 std::string ancillary_filename =
mBaseName +
"_unlimited.dat";
260 (*mpCurrentAncillaryFile) << std::setiosflags(std::ios::scientific);
261 (*mpCurrentAncillaryFile) << std::setprecision(
mPrecision);
269 std::string filename =
mBaseName +
"_" + suffix.str() +
".dat";
277 std::string filename =
mBaseName +
".dat";
279 (*mpCurrentOutputFile) << std::setiosflags(std::ios::scientific);
280 (*mpCurrentOutputFile) << std::setprecision(
mPrecision);
293 (*mpCurrentOutputFile) <<
mVariables[i].mVariableName <<
"(" <<
mVariables[i].mVariableUnits <<
")";
296 (*mpCurrentOutputFile) <<
" ";
299 (*mpCurrentOutputFile) << std::endl;
304 (*mpCurrentOutputFile) << blank_line;
311 std::string filename =
mBaseName +
".dat";
316 std::string infoname =
mBaseName +
".info";
326 (*mpCurrentOutputFile) << std::setiosflags(std::ios::scientific);
327 (*mpCurrentOutputFile) << std::setprecision(
mPrecision);
334 for (
unsigned i = 0; i <
mVariables.size(); i++)
336 (*mpCurrentOutputFile) <<
mVariables[i].mVariableName <<
"(" <<
mVariables[i].mVariableUnits <<
")";
339 (*mpCurrentOutputFile) <<
" ";
342 (*mpCurrentOutputFile) << std::endl;
347 (*mpCurrentOutputFile) << blank_line << std::endl;
357 (*p_info_file) <<
"VARIABLES " <<
mVariables.size() << std::endl;
363 p_info_file->close();
377 std::stringstream suffix;
380 std::string filename =
mBaseName +
"_" + suffix.str() +
".dat";
387 (*mpCurrentOutputFile) << std::endl;
390 (*mpCurrentOutputFile) << blank_line;
395 EXCEPTION(
"Cannot advance along unlimited dimension if it is not defined");
419 EXCEPTION(
"Cannot put variables when in Define mode");
434 EXCEPTION(
"Dimension position not supplied");
438 EXCEPTION(
"Dimension position out of range");
442 EXCEPTION(
"Dimension position supplied, but not required");
453 (*mpCurrentAncillaryFile) << std::endl <<
" ";
455 (*mpCurrentAncillaryFile) << variableValue;
473 (*mpCurrentOutputFile) << variableValue;
492 (*mpCurrentOutputFile) << variableValue;
511 (*mpCurrentOutputFile) << variableValue;
void CheckVariableName(const std::string &rName)
out_stream mpCurrentOutputFile
bool mIsFixedDimensionSet
virtual void PutVariable(int variableID, double variableValue, long dimensionPosition=-1)
int DefineUnlimitedDimension(const std::string &rDimensionName, const std::string &rDimensionUnits)
#define EXCEPTION(message)
virtual void AdvanceAlongUnlimitedDimension()
const unsigned mPrecision
void DoAdvanceAlongUnlimitedDimension()
std::vector< DataWriterVariable > mVariables
virtual void EndDefineMode()
static const int UNLIMITED_DIMENSION_VAR_ID
std::string GetOutputDirectory()
std::string GetOutputDirectoryFullPath() const
out_stream mpCurrentAncillaryFile
const unsigned mFieldWidth
DataWriterVariable * mpFixedDimensionVariable
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
virtual ~ColumnDataWriter()
ColumnDataWriter(const std::string &rDirectory, const std::string &rBaseName, bool cleanDirectory=true, unsigned precision=8)
void CreateFixedDimensionFile(const std::string &rFileName)
long mUnlimitedDimensionPosition
int DefineVariable(const std::string &rVariableName, const std::string &rVariableUnits)
int mAncillaryRowStartPosition
std::string mVariableName
int DefineFixedDimension(const std::string &rDimensionName, const std::string &rDimensionUnits, long dimensionSize)
std::string mCommentForInfoFile
std::string mFixedDimensionName
const int FILE_SUFFIX_WIDTH
std::string mFixedDimensionUnits
static std::string GetProvenanceString()
std::string mUnlimitedDimensionName
std::string mVariableUnits
void CheckUnitsName(const std::string &rName)
std::string mUnlimitedDimensionUnits
static const int FIXED_DIMENSION_VAR_ID
void CreateInfoFile(const std::string &rFileName)
DataWriterVariable * mpUnlimitedDimensionVariable
bool mNeedAdvanceAlongUnlimitedDimension
OutputFileHandler mOutputFileHandler
bool mIsUnlimitedDimensionSet