10 #include <tclap/CmdLine.h>
15 #include <string_view>
28 std::string
const& val_name,
32 std::size_t start(line.find(val_name));
33 std::size_t end(std::string::npos);
36 ERR(
"Value not found.");
39 value = line.substr(start + 1, std::string::npos);
42 start = value.find(
"\"");
43 value = value.substr(start + 1, std::string::npos);
44 end = value.find(
"\"");
48 start = value.find_first_not_of(
" ");
49 value = value.substr(start + 1, std::string::npos);
51 end = value.find_first_of(
" ");
53 value = value.substr(0, end);
59 std::string
getName(std::string
const& line)
66 auto start = line.find(
"ZONETYPE=");
67 if (start == std::string::npos)
70 "A required 'ZONETYPE=' substring is not available in the ZONE "
71 "description: '{:s}'.",
74 start += std::size(
"ZONETYPE=") - 1;
76 auto end = line.find(
',', start);
77 if (end == std::string::npos)
80 "Expected the 'ZONETYPE=type' to be followed by a comma in the "
81 "ZONE description '{:s}'. The zone type starts at position {:d}.",
86 ERR(
"ZONETYPE string is empty in ZONE description '{:s}'.", line);
88 return std::string_view(&line[start], end - start);
92 std::pair<std::size_t, std::size_t>
getDimensions(std::string
const& line)
94 std::pair<std::size_t, std::size_t> dims;
95 std::stringstream start(
getValue(line,
"I=",
false));
97 std::stringstream end(
getValue(line,
"J=",
false));
105 std::size_t
const start = var.find_first_not_of(
"\"");
106 var = var.substr(start, std::string::npos);
107 std::size_t
const end = var.find_first_of(
"\"");
108 return var.substr(0, end);
114 std::string
const var_str(
"VARIABLES");
115 std::size_t start(line.find(var_str));
116 std::string all_vars =
117 line.substr(start + var_str.length(), std::string::npos);
118 start = all_vars.find(
"=");
119 all_vars = all_vars.substr(start + 1, std::string::npos);
122 std::vector<std::string> variables;
124 while (end != std::string::npos)
126 end = all_vars.find_first_of(
" ");
127 std::string var = all_vars.substr(0, end);
129 all_vars = all_vars.substr(end + 1, std::string::npos);
138 std::size_t
const& current,
139 std::size_t
const& total)
141 if (current != total)
143 ERR(
"Data rows found do not fit specified dimensions for section "
154 std::string
const&
name,
155 std::size_t
const& current,
156 std::size_t
const& total)
168 std::vector<std::vector<double>>& scalars,
169 std::size_t& val_count)
172 scalars.reserve(n_scalars);
173 for (std::size_t i = 0; i < n_scalars; ++i)
175 scalars.emplace_back(0);
182 std::string
const& file_name,
183 std::size_t& write_count,
184 std::size_t& val_count,
185 std::size_t& val_total)
187 if (write_count == 0 || val_total != 0)
189 std::size_t
const delim_pos(file_name.find_last_of(
"."));
190 std::string
const base_name(file_name.substr(0, delim_pos + 1));
191 std::string
const extension(
192 file_name.substr(delim_pos, std::string::npos));
196 INFO(
"Writing section #{:i}", write_count);
198 out.open(base_name + std::to_string(write_count++) + extension);
204 std::size_t& write_count,
205 std::vector<std::string>
const& vec_names,
206 std::vector<std::vector<double>>
const& scalars,
207 std::pair<std::size_t, std::size_t>
const& dims)
210 for (std::size_t i = 0; i < vec_names.size(); ++i)
212 if (vec_names[i] ==
"x" || vec_names[i] ==
"X")
214 cellsize = scalars[i][1] - scalars[i][0];
221 ERR(
"Cell size not found. Aborting...");
227 origin, cellsize, -9999};
229 std::unique_ptr<MeshLib::Mesh> mesh(
236 for (std::size_t i = 1; i < vec_names.size(); ++i)
242 ERR(
"Error creating array '{:s}'.", vec_names[i]);
245 prop->reserve(scalars[i].size());
246 std::copy(scalars[i].cbegin(), scalars[i].cend(),
247 std::back_inserter(*prop));
250 std::size_t
const delim_pos(file_name.find_last_of(
"."));
251 std::string
const base_name(file_name.substr(0, delim_pos + 1));
252 std::string
const extension(file_name.substr(delim_pos, std::string::npos));
254 INFO(
"Writing section #{:i}", write_count);
256 vtu.
writeToFile(base_name + std::to_string(write_count++) + extension);
263 while (std::getline(in, line))
265 if ((line.find(
"TITLE") != std::string::npos) ||
266 (line.find(
"VARIABLES") != std::string::npos) ||
267 (line.find(
"ZONE") != std::string::npos))
275 int splitFile(std::ifstream& in, std::string
const& file_name)
280 std::size_t val_count(0);
281 std::size_t val_total(0);
282 std::size_t write_count(0);
283 while (std::getline(in, line))
285 if (line.find(
"TITLE") != std::string::npos)
296 if (line.find(
"VARIABLES") != std::string::npos)
307 if (line.find(
"ZONE") != std::string::npos)
317 std::pair<std::size_t, std::size_t> dims =
getDimensions(line);
318 val_total = dims.first * dims.second;
330 INFO(
"Writing time step #{:i}", write_count);
332 INFO(
"Finished split.");
342 std::pair<std::size_t, std::size_t> dims(0, 0);
343 std::vector<std::string> var_names;
344 std::vector<std::vector<double>> scalars;
345 std::size_t val_count(0);
346 std::size_t val_total(0);
347 std::size_t write_count(0);
348 while (std::getline(in, line))
350 if (line.find(
"GEOMETRY") != std::string::npos)
359 if (line.find(
"TITLE") != std::string::npos)
373 if (line.find(
"VARIABLES") != std::string::npos)
389 if (line.find(
"ZONE") != std::string::npos)
402 if (
auto const zonetype =
getZonetype(line); zonetype !=
"ORDERED")
404 ERR(
"Given zonetype '{:s}' is not supported. Only 'ORDERED' "
405 "zonetype data can be converted.",
410 val_total = dims.first * dims.second;
416 std::stringstream iss(line);
418 std::size_t
const n_scalars(scalars.size());
421 if (i > n_scalars - 1)
423 ERR(
"Too much data for existing scalar arrays");
426 scalars[i++].push_back(x);
430 ERR(
"Not enough data for existing scalar arrays");
440 INFO(
"Finished conversion.");
455 int main(
int argc,
char* argv[])
459 "OpenGeoSys-6 software, version " +
462 "Copyright (c) 2012-2021, OpenGeoSys Community "
463 "(http://www.opengeosys.org)",
465 TCLAP::SwitchArg split_arg(
"s",
"split",
466 "split time steps into separate files");
468 TCLAP::SwitchArg convert_arg(
"c",
"convert",
469 "convert TecPlot data into OGS meshes");
470 cmd.add(convert_arg);
471 TCLAP::ValueArg<std::string> output_arg(
472 "o",
"output-file",
"output mesh file",
false,
"",
"string");
474 TCLAP::ValueArg<std::string> input_arg(
475 "i",
"input-file",
"TecPlot input file",
true,
"",
"string");
477 cmd.parse(argc, argv);
479 if (!input_arg.isSet())
481 ERR(
"No input file given. Please specify TecPlot (*.plt) file");
485 if (convert_arg.getValue() && !output_arg.isSet())
487 ERR(
"No output file given. Please specify OGS mesh (*.vtu) file");
491 std::ifstream in(input_arg.getValue().c_str());
494 ERR(
"Could not open file {:s}.", input_arg.getValue());
498 if (!convert_arg.isSet() && !split_arg.isSet())
500 INFO(
"Nothing to do. Use -s to split or -c to convert.");
504 std::string
const filename =
505 (output_arg.isSet()) ? output_arg.getValue() : input_arg.getValue();
507 if (split_arg.getValue())
511 else if (convert_arg.getValue())
Definition of the Point class.
void INFO(char const *fmt, Args const &... args)
void ERR(char const *fmt, Args const &... args)
Definition of the Mesh class.
Implementation of the VtuInterface class.
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
PropertyVector< T > * createNewPropertyVector(std::string const &name, MeshItemType mesh_item_type, std::size_t n_components=1)
static std::unique_ptr< MeshLib::Mesh > convert(GeoLib::Raster const &raster, MeshElemType elem_type, UseIntensityAs intensity_type, std::string const &array_name="Colour")
void trim(std::string &str, char ch)
GITINFOLIB_EXPORT const std::string ogs_version
void copy(PETScVector const &x, PETScVector &y)