OGS
TecPlotTools.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#include <iostream>
13#include <memory>
14#include <string>
15#include <string_view>
16#include <vector>
17
18#include "BaseLib/MPI.h"
19#include "BaseLib/StringTools.h"
20#include "GeoLib/Point.h"
21#include "InfoLib/GitInfo.h"
23#include "MeshLib/Mesh.h"
25
28std::string getValue(std::string const& line,
29 std::string const& val_name,
30 bool is_string)
31{
32 std::string value;
33 std::size_t start(line.find(val_name));
34 std::size_t end(std::string::npos);
35 if (start == end)
36 {
37 ERR("Value not found.");
38 return "";
39 }
40 value = line.substr(start + 1, std::string::npos);
41 if (is_string)
42 {
43 start = value.find("\"");
44 value = value.substr(start + 1, std::string::npos);
45 end = value.find("\"");
46 }
47 else
48 {
49 start = value.find_first_not_of(" ");
50 value = value.substr(start + 1, std::string::npos);
51 BaseLib::trim(value);
52 end = value.find_first_of(" ");
53 }
54 value = value.substr(0, end);
55 BaseLib::trim(value);
56 return value;
57}
58
60std::string getName(std::string const& line)
61{
62 return getValue(line, "T=", true);
63}
64
65std::string_view getZonetype(std::string const& line)
66{
67 auto start = line.find("ZONETYPE=");
68 if (start == std::string::npos)
69 {
71 "A required 'ZONETYPE=' substring is not available in the ZONE "
72 "description: '{:s}'.",
73 line);
74 }
75 start += std::size("ZONETYPE=") - 1;
76
77 auto end = line.find(',', start);
78 if (end == std::string::npos)
79 {
81 "Expected the 'ZONETYPE=type' to be followed by a comma in the "
82 "ZONE description '{:s}'. The zone type starts at position {:d}.",
83 line, start);
84 }
85 if (start == end)
86 {
87 ERR("ZONETYPE string is empty in ZONE description '{:s}'.", line);
88 }
89 return std::string_view(&line[start], end - start);
90}
91
93std::pair<std::size_t, std::size_t> getDimensions(std::string const& line)
94{
95 std::pair<std::size_t, std::size_t> dims;
96 std::stringstream start(getValue(line, "I=", false));
97 start >> dims.first;
98 std::stringstream end(getValue(line, "J=", false));
99 end >> dims.second;
100 return dims;
101}
102
104std::string trimVariable(std::string& var)
105{
106 std::size_t const start = var.find_first_not_of("\"");
107 var = var.substr(start, std::string::npos);
108 std::size_t const end = var.find_first_of("\"");
109 return var.substr(0, end);
110}
111
113std::vector<std::string> getVariables(std::string const& line)
114{
115 std::string const var_str("VARIABLES");
116 std::size_t start(line.find(var_str));
117 std::string all_vars =
118 line.substr(start + var_str.length(), std::string::npos);
119 start = all_vars.find("=");
120 all_vars = all_vars.substr(start + 1, std::string::npos);
121 BaseLib::trim(all_vars);
122
123 std::vector<std::string> variables;
124 std::size_t end = 0;
125 while (end != std::string::npos)
126 {
127 end = all_vars.find_first_of(" ");
128 std::string var = all_vars.substr(0, end);
129 variables.push_back(trimVariable(var));
130 all_vars = all_vars.substr(end + 1, std::string::npos);
131 BaseLib::trim(all_vars);
132 }
133
134 return variables;
135}
136
138bool dataCountError(std::string const& name,
139 std::size_t const& current,
140 std::size_t const& total)
141{
142 if (current != total)
143 {
144 ERR("Data rows found do not fit specified dimensions for section "
145 "'{:s}'.",
146 name);
147 return true;
148 }
149 return false;
150}
151
154bool dataCountError(std::ofstream& out,
155 std::string const& name,
156 std::size_t const& current,
157 std::size_t const& total)
158{
159 if (dataCountError(name, current, total))
160 {
161 out.close();
162 return true;
163 }
164 return false;
165}
166
168void resetDataStructures(std::size_t const& n_scalars,
169 std::vector<std::vector<double>>& scalars,
170 std::size_t& val_count)
171{
172 scalars.clear();
173 scalars.reserve(n_scalars);
174 for (std::size_t i = 0; i < n_scalars; ++i)
175 {
176 scalars.emplace_back(0);
177 }
178 val_count = 0;
179}
180
182void writeTecPlotSection(std::ofstream& out,
183 std::string const& file_name,
184 std::size_t& write_count,
185 std::size_t& val_count,
186 std::size_t& val_total)
187{
188 if (write_count == 0 || val_total != 0)
189 {
190 std::size_t const delim_pos(file_name.find_last_of("."));
191 std::string const base_name(file_name.substr(0, delim_pos + 1));
192 std::string const extension(
193 file_name.substr(delim_pos, std::string::npos));
194
195 val_count = 0;
196 val_total = 0;
197 INFO("Writing section #{}", write_count);
198 out.close();
199 out.open(base_name + std::to_string(write_count++) + extension);
200 }
201}
202
204int writeDataToMesh(std::string const& file_name,
205 std::size_t& write_count,
206 std::vector<std::string> const& vec_names,
207 std::vector<std::vector<double>> const& scalars,
208 std::pair<std::size_t, std::size_t> const& dims)
209{
210 double cellsize = 0;
211 for (std::size_t i = 0; i < vec_names.size(); ++i)
212 {
213 if (vec_names[i] == "x" || vec_names[i] == "X")
214 {
215 cellsize = scalars[i][1] - scalars[i][0];
216 break;
217 }
218 }
219
220 if (cellsize == 0)
221 {
222 ERR("Cell size not found. Aborting...");
223 return -4;
224 }
225
226 GeoLib::Point origin(0, 0, 0);
227 GeoLib::RasterHeader header{dims.first, dims.second, 1,
228 origin, cellsize, -9999};
229
230 std::unique_ptr<MeshLib::Mesh> mesh(
231 MeshToolsLib::RasterToMesh::convert(scalars[0].data(),
232 header,
235 vec_names[0]));
236 MeshLib::Properties& properties = mesh->getProperties();
237 for (std::size_t i = 1; i < vec_names.size(); ++i)
238 {
239 auto* const prop = properties.createNewPropertyVector<double>(
240 vec_names[i], MeshLib::MeshItemType::Cell, 1);
241 if (!prop)
242 {
243 ERR("Error creating array '{:s}'.", vec_names[i]);
244 return -5;
245 }
246 prop->reserve(scalars[i].size());
247 std::copy(scalars[i].cbegin(), scalars[i].cend(),
248 std::back_inserter(*prop));
249 }
250
251 std::size_t const delim_pos(file_name.find_last_of("."));
252 std::string const base_name(file_name.substr(0, delim_pos + 1));
253 std::string const extension(file_name.substr(delim_pos, std::string::npos));
254
255 INFO("Writing section #{}", write_count);
256 MeshLib::IO::VtuInterface vtu(mesh.get());
257 vtu.writeToFile(base_name + std::to_string(write_count++) + extension);
258 return 0;
259}
260
262void skipGeometrySection(std::ifstream& in, std::string& line)
263{
264 while (std::getline(in, line))
265 {
266 if ((line.find("TITLE") != std::string::npos) ||
267 (line.find("VARIABLES") != std::string::npos) ||
268 (line.find("ZONE") != std::string::npos))
269 {
270 return;
271 }
272 }
273}
274
276int splitFile(std::ifstream& in, std::string const& file_name)
277{
278 std::ofstream out;
279 std::string line;
280 std::string name;
281 std::size_t val_count(0);
282 std::size_t val_total(0);
283 std::size_t write_count(0);
284 while (std::getline(in, line))
285 {
286 if (line.find("TITLE") != std::string::npos)
287 {
288 if (dataCountError(out, name, val_count, val_total))
289 {
290 return -3;
291 }
292 writeTecPlotSection(out, file_name, write_count, val_count,
293 val_total);
294 out << line << "\n";
295 continue;
296 }
297 if (line.find("VARIABLES") != std::string::npos)
298 {
299 if (dataCountError(out, name, val_count, val_total))
300 {
301 return -3;
302 }
303 writeTecPlotSection(out, file_name, write_count, val_count,
304 val_total);
305 out << line << "\n";
306 continue;
307 }
308 if (line.find("ZONE") != std::string::npos)
309 {
310 if (dataCountError(out, name, val_count, val_total))
311 {
312 return -3;
313 }
314 writeTecPlotSection(out, file_name, write_count, val_count,
315 val_total);
316 out << line << "\n";
317 name = getName(line);
318 std::pair<std::size_t, std::size_t> dims = getDimensions(line);
319 val_total = dims.first * dims.second;
320 val_count = 0;
321 continue;
322 }
323
324 out << line << "\n";
325 val_count++;
326 }
327 if (dataCountError(out, name, val_count, val_total))
328 {
329 return -3;
330 }
331 INFO("Writing time step #{}", write_count);
332 out.close();
333 INFO("Finished split.");
334 return 0;
335}
336
339int convertFile(std::ifstream& in, std::string const& file_name)
340{
341 std::string line;
342 std::string name;
343 std::pair<std::size_t, std::size_t> dims(0, 0);
344 std::vector<std::string> var_names;
345 std::vector<std::vector<double>> scalars;
346 std::size_t val_count(0);
347 std::size_t val_total(0);
348 std::size_t write_count(0);
349 while (std::getline(in, line))
350 {
351 if (line.find("GEOMETRY") != std::string::npos)
352 {
353 skipGeometrySection(in, line);
354 }
355
356 if (line.empty())
357 {
358 continue;
359 }
360 if (line.find("TITLE") != std::string::npos)
361 {
362 if (dataCountError(name, val_count, val_total))
363 {
364 return -3;
365 }
366 if (val_count != 0)
367 {
368 writeDataToMesh(file_name, write_count, var_names, scalars,
369 dims);
370 resetDataStructures(var_names.size(), scalars, val_count);
371 }
372 continue;
373 }
374 if (line.find("VARIABLES") != std::string::npos)
375 {
376 if (val_count != 0)
377 {
378 if (dataCountError(name, val_count, val_total))
379 {
380 return -3;
381 }
382 writeDataToMesh(file_name, write_count, var_names, scalars,
383 dims);
384 }
385 var_names.clear();
386 var_names = getVariables(line);
387 resetDataStructures(var_names.size(), scalars, val_count);
388 continue;
389 }
390 if (line.find("ZONE") != std::string::npos)
391 {
392 if (val_count != 0)
393 {
394 if (dataCountError(name, val_count, val_total))
395 {
396 return -3;
397 }
398 writeDataToMesh(file_name, write_count, var_names, scalars,
399 dims);
400 resetDataStructures(var_names.size(), scalars, val_count);
401 }
402 name = getName(line);
403 if (auto const zonetype = getZonetype(line); zonetype != "ORDERED")
404 {
405 ERR("Given zonetype '{:s}' is not supported. Only 'ORDERED' "
406 "zonetype data can be converted.",
407 zonetype);
408 return -4;
409 }
410 dims = getDimensions(line);
411 val_total = dims.first * dims.second;
412 val_count = 0;
413 continue;
414 }
415
416 double x;
417 std::stringstream iss(line);
418 std::size_t i(0);
419 std::size_t const n_scalars(scalars.size());
420 while (iss >> x)
421 {
422 if (i > n_scalars - 1)
423 {
424 ERR("Too much data for existing scalar arrays");
425 return -3;
426 }
427 scalars[i++].push_back(x);
428 }
429 if (i < n_scalars)
430 {
431 ERR("Not enough data for existing scalar arrays");
432 return -3;
433 }
434 val_count++;
435 }
436 if (dataCountError(name, val_count, val_total))
437 {
438 return -3;
439 }
440 writeDataToMesh(file_name, write_count, var_names, scalars, dims);
441 INFO("Finished conversion.");
442 return 0;
443}
444
456int main(int argc, char* argv[])
457{
458 TCLAP::CmdLine cmd(
459 "TecPlot Parser\n\n"
460 "OpenGeoSys-6 software, version " +
462 ".\n"
463 "Copyright (c) 2012-2024, OpenGeoSys Community "
464 "(http://www.opengeosys.org)",
466 TCLAP::SwitchArg split_arg("s", "split",
467 "split time steps into separate files");
468 cmd.add(split_arg);
469 TCLAP::SwitchArg convert_arg("c", "convert",
470 "convert TecPlot data into OGS meshes");
471 cmd.add(convert_arg);
472 TCLAP::ValueArg<std::string> output_arg(
473 "o", "output-file", "output mesh file", false, "", "string");
474 cmd.add(output_arg);
475 TCLAP::ValueArg<std::string> input_arg(
476 "i", "input-file", "TecPlot input file", true, "", "string");
477 cmd.add(input_arg);
478 cmd.parse(argc, argv);
479
480 BaseLib::MPI::Setup mpi_setup(argc, argv);
481
482 if (!input_arg.isSet())
483 {
484 ERR("No input file given. Please specify TecPlot (*.plt) file");
485 return -1;
486 }
487
488 if (convert_arg.getValue() && !output_arg.isSet())
489 {
490 ERR("No output file given. Please specify OGS mesh (*.vtu) file");
491 return -1;
492 }
493
494 std::ifstream in(input_arg.getValue().c_str());
495 if (!in.is_open())
496 {
497 ERR("Could not open file {:s}.", input_arg.getValue());
498 return -2;
499 }
500
501 if (!convert_arg.isSet() && !split_arg.isSet())
502 {
503 INFO("Nothing to do. Use -s to split or -c to convert.");
504 return 0;
505 }
506
507 std::string const filename =
508 (output_arg.isSet()) ? output_arg.getValue() : input_arg.getValue();
509 int return_val(0);
510 if (split_arg.getValue())
511 {
512 return_val = splitFile(in, filename);
513 }
514 else if (convert_arg.getValue())
515 {
516 return_val = convertFile(in, filename);
517 }
518
519 in.close();
520 return return_val;
521}
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the Point class.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the Mesh class.
Definition of string helper functions.
int convertFile(std::ifstream &in, std::string const &file_name)
std::string getName(std::string const &line)
Returns the name/title from the "Zone"-description.
int main(int argc, char *argv[])
std::string_view getZonetype(std::string const &line)
int splitFile(std::ifstream &in, std::string const &file_name)
Splits a TecPlot file containing multiple sections/zones into separate files.
bool dataCountError(std::string const &name, std::size_t const &current, std::size_t const &total)
Tests if the number of read values equals the number of expected values.
std::string getValue(std::string const &line, std::string const &val_name, bool is_string)
std::string trimVariable(std::string &var)
Trims a substring containing a variable-name.
void skipGeometrySection(std::ifstream &in, std::string &line)
If a geometry-section is encountered, it is currently ignored.
void writeTecPlotSection(std::ofstream &out, std::string const &file_name, std::size_t &write_count, std::size_t &val_count, std::size_t &val_total)
Writes one section/zone into a separate TecPlot file.
void resetDataStructures(std::size_t const &n_scalars, std::vector< std::vector< double > > &scalars, std::size_t &val_count)
Resets all data structures after a new "Variables"-description is found.
std::pair< std::size_t, std::size_t > getDimensions(std::string const &line)
Returns raster dimensions from the "Zone"-description.
int writeDataToMesh(std::string const &file_name, std::size_t &write_count, std::vector< std::string > const &vec_names, std::vector< std::vector< double > > const &scalars, std::pair< std::size_t, std::size_t > const &dims)
Writes one section/zone into a separate OGS-mesh.
std::vector< std::string > getVariables(std::string const &line)
Returns an vector of variable names from the "Variables"-description.
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....
Definition Properties.h:36
PropertyVector< T > * createNewPropertyVector(std::string_view name, MeshItemType mesh_item_type, std::size_t n_components=1)
Definition Properties.h:17
static std::unique_ptr< MeshLib::Mesh > convert(GeoLib::Raster const &raster, MeshLib::MeshElemType elem_type, MeshLib::UseIntensityAs intensity_type, std::string const &array_name="Colour")
void trim(std::string &str, char ch)
GITINFOLIB_EXPORT const std::string ogs_version
Contains the relevant information when storing a geoscientific raster data.
Definition Raster.h:28