42 #include "ColumnDataReader.hpp"
51 #include "OutputFileHandler.hpp"
61 const std::string& rBaseName,
65 std::string directory;
74 if ( !(*(rDirectory.end()-1) ==
'/'))
76 directory = rDirectory +
"/";
80 directory = rDirectory;
87 const std::string& rBaseName)
103 if (!infofile.is_open())
120 EXCEPTION(
"Couldn't read info file correctly");
132 std::stringstream suffix;
135 mDataFilename = rDirectory + rBaseName +
"_" + suffix.str() +
".dat";
147 if (!ancillaryfile.is_open())
149 EXCEPTION(
"Couldn't open ancillary data file");
151 std::string dimension;
152 std::getline(ancillaryfile, dimension);
153 std::stringstream dimension_stream(dimension);
154 std::string dimension_unit, dimension_name, header;
155 dimension_stream >> header;
158 int unitpos = header.find(
"(") + 1;
160 dimension_name = header.substr(0, unitpos - 1);
161 dimension_unit = header.substr(unitpos, header.length() - unitpos - 1);
164 ancillaryfile.close();
174 if (!datafile.is_open())
179 std::string variables;
180 std::getline(datafile, variables);
181 std::stringstream variable_stream(variables);
182 std::string header, variable, unit;
186 while (variable_stream >> header)
189 int unitpos = header.find(
"(") + 1;
191 variable = header.substr(0, unitpos - 1);
192 unit = header.substr(unitpos, header.length() - unitpos - 1);
215 std::string first_line;
216 std::string first_entry;
217 unsigned last_pos=0u;
219 while (first_entry.length()==0 && !datafile.eof())
221 std::getline(datafile, first_line);
222 std::stringstream stream(first_line);
223 stream >> first_entry;
224 last_pos = stream.tellg();
225 while (stream.good() && last_pos <170)
227 std::string last_entry;
228 stream >> last_entry;
229 if (stream.tellg() > 0)
231 last_pos = stream.tellg();
236 if (datafile.eof() && first_entry.length()==0)
238 EXCEPTION(
"Unable to determine field width from file as cannot find any data entries");
240 assert (last_pos > 0u);
242 size_t dot_pos = first_entry.find(
".");
243 size_t e_pos = first_entry.find(
"e");
244 if (dot_pos == std::string::npos || e_pos == std::string::npos)
246 EXCEPTION(
"Badly formatted scientific data field");
249 unsigned est_field_width = e_pos - dot_pos - 1 + 8;
251 if (last_pos % est_field_width == 0)
257 assert ( last_pos % (est_field_width+1) == 0 || (last_pos+1) % (est_field_width+1) == 0 );
268 EXCEPTION(
"Data file has fixed dimension which must be specified");
274 std::stringstream variable_name;
275 variable_name << rVariableName;
276 EXCEPTION(
"'" + variable_name.str() +
"' is an unknown variable.");
279 int column = (*col).second;
290 EXCEPTION(
"Data file has no fixed dimension");
302 int column = (*col).second;
317 std::string::size_type underscore_pos = datafile.rfind(
"_", datafile.length());
318 std::stringstream suffix;
322 if (underscore_pos != std::string::npos)
324 datafile = datafile.substr(0, underscore_pos+1) + suffix.str() +
".dat";
347 EXCEPTION(
"Data file has no unlimited dimension");
364 std::ifstream datafile(rFilename.c_str(), std::ios::in);
366 if (!datafile.is_open())
370 std::string variable_values;
371 for (
int i=0; i<row+1; i++)
373 std::getline(datafile, variable_values);
376 std::getline(datafile, variable_values);
388 std::ifstream datafile(rFilename.c_str(), std::ios::in);
392 assert(datafile.is_open());
395 bool end_of_file_reached =
false;
398 end_of_file_reached = std::getline(datafile, value).eof();
400 while (!end_of_file_reached)
402 end_of_file_reached = std::getline(datafile, value).eof();
412 value = rLine.substr(startpos, mFieldWidth);
414 std::stringstream variable_stream(value);
416 variable_stream >> d_value;
417 if (variable_stream.fail())
419 if (variable_stream.eof())
425 #define COVERAGE_IGNORE
428 #undef COVERAGE_IGNORE
std::string mAncillaryFilename
std::map< std::string, int > mVariablesToColumns
std::string mInfoFilename
bool HasValues(const std::string &rVariableName)
bool mHasUnlimitedDimension
std::string GetAbsolutePath() const
#define EXCEPTION(message)
std::vector< double > GetValues(const std::string &rVariableName)
std::string GetOutputDirectoryFullPath() const
void ReadColumnFromFile(const std::string &rFilename, int col)
std::string mDataFilename
std::map< std::string, std::string > mVariablesToUnits
std::vector< double > GetUnlimitedDimensionValues()
void CheckFiles(const std::string &rDirectory, const std::string &rBaseName)
ColumnDataReader(const std::string &rDirectory, const std::string &rBaseName, bool makeAbsolute=true)
const int FILE_SUFFIX_WIDTH
void ReadValueFromFile(const std::string &rFilename, int col, int row)
std::vector< double > mValues
void PushColumnEntryFromLine(const std::string &rLine, int col)