4#include <tclap/CmdLine.h>
28using namespace netCDF;
43 if (str ==
"x" || str ==
"exit")
48 std::size_t
const max = 0)
52 ERR(
"Input not valid.");
54 else if (error_id == 1)
56 ERR(
"Index not valid. Valid indices are in [0,{:d}].", max);
58 else if (error_id == 2)
60 ERR(
"Input not valid.");
61 std::cout <<
"Type \"info\" to display the available options again. "
62 "\"exit\" will exit the programme.\n";
66static std::size_t
parseInput(std::string
const& request_str,
67 std::size_t
const max_val,
68 bool const has_info =
false)
72 std::cout << request_str;
75 std::getline(std::cin, str);
77 if (has_info && str ==
"info")
79 std::stringstream str_stream(str);
80 if (!(str_stream >> val))
82 std::size_t
const error_val = (has_info) ? 2 : 0;
86 if (val > max_val - 1)
93 return std::numeric_limits<std::size_t>::max();
96static NcVar
getDimVar(NcFile
const& dataset, NcVar
const& var,
97 std::size_t
const dim)
99 NcDim
const& dim_obj = var.getDim(dim);
100 return dataset.getVar(dim_obj.getName());
104 std::size_t
const dim)
106 return std::make_pair(0.0,
static_cast<double>(var.getDim(dim).getSize()));
109static std::vector<std::string>
getArrays(NcFile
const& dataset)
111 auto const& names = dataset.getVars();
112 std::vector<std::string> var_names;
113 for (
auto [name, var] : names)
116 var_names.push_back(name);
123 std::size_t
const n_vars(dataset.getDimCount());
124 std::cout <<
"The NetCDF file contains the following " << n_vars
126 std::cout <<
"\tIndex\tArray Name\t#Dimensions\n";
127 std::cout <<
"-------------------------------------------\n";
128 auto const& names = dataset.getVars();
129 std::size_t count = 0;
130 for (
auto [name, var] : names)
132 std::cout <<
"\t" << count++ <<
"\t" << name <<
"\t("
133 << var.getDimCount() <<
"D array)\n";
140 std::cout <<
"Data array \"" << var.getName()
141 <<
"\" contains the following dimensions:\n";
142 std::size_t
const n_dims(var.getDimCount());
143 for (std::size_t i = 0; i < n_dims; ++i)
144 std::cout <<
"\t" << i <<
"\t" << var.getDim(i).getName() <<
"\t("
145 << var.getDim(i).getSize() <<
" values)\n";
151 if (var.getDimCount() == 1)
154 std::size_t
const size = var.getDim(0).getSize();
155 var.getVar({0}, {1}, &start);
156 var.getVar({size - 1}, {1}, &end);
157 return std::make_pair(start, end);
159 return std::make_pair(0, 0);
163 std::vector<std::size_t>
const& dim_idx_map,
164 std::size_t
const time_offset)
167 std::size_t
const n_dims = var.getDimCount();
168 for (std::size_t i = time_offset; i < n_dims; ++i)
170 NcVar
const& dim =
getDimVar(dataset, var, dim_idx_map[i]);
171 auto const bounds = (dim.isNull()) ?
getDimLength(var, dim_idx_map[i])
173 origin[i - time_offset] =
174 (bounds.first < bounds.second) ? bounds.first : bounds.second;
179static void flipRaster(std::vector<double>& data, std::size_t
const layers,
180 std::size_t
const width, std::size_t
const height)
182 std::size_t
const length(data.size());
183 std::vector<double> tmp_vec;
184 tmp_vec.reserve(length);
185 for (std::size_t k = 0; k < layers; k++)
187 std::size_t
const layer_end = (k + 1) * height * width;
188 for (std::size_t i = 0; i < height; i++)
190 std::size_t
const line_idx(layer_end - (width * (i + 1)));
191 for (std::size_t j = 0; j < width; j++)
193 tmp_vec.push_back(data[line_idx + j]);
197 std::copy(tmp_vec.cbegin(), tmp_vec.cend(), data.begin());
202 bool ret(var.getDimCount() < 2);
204 ERR(
"Only 2+ dimensional variables can be converted into OGS "
211 std::vector<std::string>
const& names =
getArrays(dataset);
213 std::size_t
const idx =
214 parseInput(
"Enter data array index: ", dataset.getVarCount(),
true);
216 if (
static_cast<int>(idx) == dataset.getVarCount() ||
224 std::vector<std::size_t>& dim_idx_map)
227 std::size_t
const n_dims(var.getDimCount());
228 dim_idx_map[0] = std::numeric_limits<std::size_t>::max();
229 bool is_time_dep(
true);
234 std::string temp_str(
"");
235 cout <<
"Is the parameter time-dependent?\n";
236 while (dim_idx_map[0] == std::numeric_limits<std::size_t>::max() &&
239 cout <<
"Enter ID for temporal dimension or \"c\" to continue: ";
240 std::getline(std::cin, temp_str);
241 std::stringstream str_stream(temp_str);
242 if (str_stream.str() ==
"c" || str_stream.str() ==
"continue")
246 if (!(str_stream >> dim_idx_map[0]))
249 dim_idx_map[0] = std::numeric_limits<std::size_t>::max();
252 if (dim_idx_map[0] > n_dims - 1)
255 dim_idx_map[0] = std::numeric_limits<std::size_t>::max();
264 std::size_t
const start_idx = (is_time_dep) ? 1 : 0;
265 std::array<std::string, 4>
const dim_comment{
266 "(x / longitude)",
"(y / latitude)",
"(z / height / depth)",
267 "[Error: 4-dimensional non-temporal arrays are not supported]"};
268 for (std::size_t i = start_idx; i < n_dims; ++i)
270 dim_idx_map[i] = std::numeric_limits<std::size_t>::max();
272 std::string
const request_str(
"Enter ID for dimension " +
273 std::to_string(i) +
" " +
274 dim_comment[i - start_idx] +
": ");
275 std::size_t
const idx =
276 parseInput(request_str, var.getDimCount(),
true);
278 if (
static_cast<int>(idx) == var.getDimCount())
284 dim_idx_map[i] = idx;
291 NcVar
const& var, std::size_t
const dim_idx)
293 std::size_t
const n_time_steps = var.getDim(dim_idx).getSize();
294 std::size_t
const max_val = std::numeric_limits<std::size_t>::max();
295 std::pair<std::size_t, std::size_t> bounds(max_val, max_val);
296 std::cout <<
"\nThe dataset contains " << n_time_steps <<
" time steps.\n";
297 while (bounds.first == max_val)
300 "Specify first time step to export: ", n_time_steps,
false);
302 while (bounds.first > bounds.second || bounds.second > n_time_steps)
305 "Specify last time step to export: ", n_time_steps,
false);
318 std::cout <<
"\nSelect element type for result, choose ";
321 std::cout <<
"(t)riangle or (q)uadliteral: ";
323 std::cout <<
"(p)rism or (h)exahedron: ";
324 std::string type(
"");
325 std::getline(std::cin, type);
329 if (type !=
"t" && type !=
"q" && type !=
"tri" && type !=
"quad" &&
330 type !=
"triangle" && type !=
"quatliteral")
332 if (type ==
"t" || type ==
"tri" || type ==
"triangle")
339 if (type !=
"p" && type !=
"h" && type !=
"prism" &&
340 type !=
"hex" && type !=
"hexahedron")
342 if (type ==
"p" || type ==
"prism")
351 std::pair<std::size_t, std::size_t>
const& time_bounds)
356 std::size_t
const n_time_steps(time_bounds.second - time_bounds.first +
358 std::cout <<
"\nThe selection includes " << n_time_steps
360 std::cout <<
"0. Save data in " << n_time_steps
361 <<
" mesh files with one scalar array each.\n";
362 std::cout <<
"1. Save data in one mesh file with " << n_time_steps
363 <<
" scalar arrays.\n";
364 std::cout <<
"2. Save data as " << n_time_steps <<
" ASC images.\n";
366 std::size_t
const ret =
367 parseInput(
"Select preferred method: ", 3,
false);
381 std::size_t
const max_length(std::to_string(max).length());
382 std::string
const current_str(std::to_string(i));
383 return std::string(max_length - current_str.length(),
'0') + current_str;
388 std::size_t
const dim_idx = var.getDimCount() - 1;
389 NcVar
const dim_var(
getDimVar(dataset, var, dim_idx));
390 auto const bounds = (dim_var.isNull()) ?
getDimLength(var, dim_idx)
392 std::size_t
const dim_size = var.getDim(dim_idx).getSize();
395 OGS_FATAL(
"Dimension '{:s}' has size 0. Aborting...",
396 var.getDim(dim_idx).getName());
398 return std::fabs(bounds.second - bounds.first) /
399 static_cast<double>(dim_size);
403 NcFile
const& dataset, NcVar
const& var,
404 std::vector<std::size_t>
const& dim_idx_map,
405 std::vector<std::size_t>
const& length, std::size_t
const time_offset)
408 getOrigin(dataset, var, dim_idx_map, time_offset);
410 std::size_t n_dims = var.getDimCount();
411 std::size_t z_length =
412 (n_dims - time_offset == 3) ? length[dim_idx_map.back()] : 1;
413 return {length[dim_idx_map[0 + time_offset]],
414 length[dim_idx_map[1 + time_offset]],
421static std::vector<std::size_t>
getLength(NcVar
const& var,
422 std::size_t
const time_offset)
424 std::size_t
const n_dims = (var.getDimCount());
425 std::vector<std::size_t> length(n_dims, 1);
426 for (std::size_t i = time_offset; i < n_dims; ++i)
428 length[i] = var.getDim(i).getSize();
433static std::vector<double>
getData(NcVar
const& var,
434 std::size_t
const total_length,
435 std::size_t
const time_step,
436 std::vector<std::size_t>
const& length)
438 std::size_t
const n_dims(var.getDimCount());
439 std::vector<std::size_t> offset(n_dims, 0);
440 offset[0] = time_step;
441 std::vector<double> data_vec(total_length, 0);
442 var.getVar(offset, length, data_vec.data());
445 data_vec.begin(), data_vec.end(),
446 [](
double const& x) { return x == no_data_input; },
no_data_output);
452 std::vector<std::size_t>& dim_idx_map,
453 TCLAP::ValueArg<std::size_t>& arg_dim_time,
454 TCLAP::ValueArg<std::size_t>& arg_dim1,
455 TCLAP::ValueArg<std::size_t>& arg_dim2,
456 TCLAP::ValueArg<std::size_t>& arg_dim3)
458 std::size_t dim_param_count = 0;
459 if (arg_dim_time.isSet())
461 if (arg_dim1.isSet())
463 if (arg_dim2.isSet())
465 if (arg_dim3.isSet())
468 std::size_t
const n_dims = var.getDimCount();
469 if (dim_param_count != n_dims)
471 ERR(
"Number of parameters set does not fit number of parameters for "
472 "specified variable.");
476 if (arg_dim_time.getValue() >= n_dims || arg_dim1.getValue() >= n_dims ||
477 arg_dim2.getValue() >= n_dims || arg_dim3.getValue() >= n_dims)
479 ERR(
"Maximum allowed dimension for variable \"{:s}\" is {:d}.",
480 var.getName(), n_dims - 1);
484 if (arg_dim_time.isSet())
485 dim_idx_map[0] = arg_dim_time.getValue();
486 std::size_t
const temp_offset = (arg_dim_time.isSet()) ? 1 : 0;
487 dim_idx_map[0 + temp_offset] = arg_dim1.getValue();
488 dim_idx_map[1 + temp_offset] = arg_dim2.getValue();
489 if (n_dims == (3 + temp_offset))
490 dim_idx_map[2 + temp_offset] = arg_dim3.getValue();
497 TCLAP::ValueArg<std::size_t>& arg_time_start,
498 TCLAP::ValueArg<std::size_t>& arg_time_end)
501 if (arg_time_start.getValue() > bounds.second)
503 ERR(
"Start time step larger than total number of time steps. Resetting "
505 arg_time_start.reset();
508 if (!arg_time_end.isSet())
509 return {arg_time_start.getValue(), arg_time_start.getValue()};
511 if (arg_time_end.getValue() > bounds.second)
513 ERR(
"End time step larger than total number of time steps. Resetting "
514 "to starting time step");
515 return {arg_time_start.getValue(), arg_time_start.getValue()};
518 if (arg_time_end.getValue() < arg_time_start.getValue())
520 ERR(
"End time step larger than starting time step. Swapping values");
521 return {arg_time_end.getValue(), arg_time_start.getValue()};
524 return {arg_time_start.getValue(), arg_time_end.getValue()};
528 TCLAP::ValueArg<std::string>& arg_elem_type)
530 if (arg_elem_type.getValue() ==
"tri")
532 if (arg_elem_type.getValue() ==
"quad")
534 if (arg_elem_type.getValue() ==
"prism")
536 if (arg_elem_type.getValue() ==
"hex")
542static bool convert(NcFile
const& dataset, NcVar
const& var,
543 std::string
const& output_name,
544 std::vector<std::size_t>
const& dim_idx_map,
545 std::size_t
const time_offset,
546 std::pair<std::size_t, std::size_t>
const& time_bounds,
550 std::unique_ptr<MeshLib::Mesh> mesh;
551 std::vector<std::size_t>
const length =
getLength(var, time_offset);
552 std::size_t
const array_length = std::accumulate(
553 length.cbegin(), length.cend(), 1, std::multiplies<std::size_t>());
554 for (std::size_t i = time_bounds.first; i <= time_bounds.second; ++i)
556 std::string
const step_str =
557 (time_bounds.first != time_bounds.second)
558 ? std::string(
" time step " + std::to_string(i))
560 std::cout <<
"Converting" << step_str <<
"...\n";
561 std::vector<double> data_vec =
getData(var, array_length, i, length);
565 std::size_t
const n_dims = length.size();
566 NcVar
const dim_var(
getDimVar(dataset, var, n_dims - 2));
567 auto const bounds = (dim_var.isNull()) ?
getDimLength(var, n_dims - 2)
569 if (bounds.first > bounds.second)
571 std::size_t n_layers =
572 (length.size() - time_offset == 3) ? length[n_dims - 3] : 1;
573 flipRaster(data_vec, n_layers, length[n_dims - 1],
584 elem_type, useIntensity,
586 std::string
const output_file_name(
594 std::string array_name(var.getName());
595 if (time_bounds.first != time_bounds.second)
597 if (i == time_bounds.first)
599 data_vec.data(), header, elem_type, useIntensity,
603 std::unique_ptr<MeshLib::Mesh>
const temp(
605 elem_type, useIntensity,
608 temp->getProperties().getPropertyVector<
double>(array_name);
616 if (i == time_bounds.second)
619 std::string
const output_file_name =
622 : output_name +
".vtu";
629 data_vec.data() + header.
n_cols *
632 std::string
const output_file_name(
642int main(
int argc,
char* argv[])
645 "Converts NetCDF data into mesh file(s).\n\n "
646 "OpenGeoSys-6 software, version " +
649 "Copyright (c) 2012-2026, OpenGeoSys Community "
650 "(http://www.opengeosys.org)",
653 TCLAP::ValueArg<int> arg_nodata(
655 "explicitly specifies the no data value used in the dataset (usually "
656 "it is not necessary to set this)",
660 std::vector<std::string> allowed_elems{
"tri",
"quad",
"prism",
"hex"};
661 TCLAP::ValuesConstraint<std::string> allowed_elem_vals(allowed_elems);
662 TCLAP::ValueArg<std::string> arg_elem_type(
663 "e",
"elem-type",
"the element type used in the resulting OGS mesh, ",
664 false,
"", &allowed_elem_vals);
665 cmd.add(arg_elem_type);
667 TCLAP::SwitchArg arg_images(
669 "if set, all time steps will be written as ESRI image files (*.asc)");
672 TCLAP::SwitchArg arg_multi_files(
674 "if set, each time step will be written to a separate mesh file");
675 cmd.add(arg_multi_files);
677 TCLAP::SwitchArg arg_single_file(
679 "if set, all time steps will be written to a single mesh file (with "
680 "one scalar array per time step)");
681 cmd.add(arg_single_file);
683 TCLAP::ValueArg<std::size_t> arg_time_end(
685 "last time step to be extracted (only for time-dependent variables!)",
686 false, 0,
"TIME_END");
687 cmd.add(arg_time_end);
689 TCLAP::ValueArg<std::size_t> arg_time_start(
690 "",
"timestep-first",
691 "first time step to be extracted (only for time-dependent variables!)",
692 false, 0,
"TIMESTEP_FIRST");
693 cmd.add(arg_time_start);
695 std::vector<std::size_t> allowed_dims{0, 1, 2, 3};
696 TCLAP::ValuesConstraint<std::size_t> allowed_dim_vals(allowed_dims);
697 TCLAP::ValueArg<std::size_t> arg_dim3(
699 "index of third dimension (z/height/depth) for the selected variable",
700 false, 0, &allowed_dim_vals);
703 TCLAP::ValueArg<std::size_t> arg_dim2(
705 "index of second dimension (y/latitude) for the selected variable",
706 false, 0, &allowed_dim_vals);
709 TCLAP::ValueArg<std::size_t> arg_dim1(
711 "index of first dimension (x/longitude) for the selected variable",
712 false, 0, &allowed_dim_vals);
715 TCLAP::ValueArg<std::size_t> arg_dim_time(
717 "index of the time-dependent dimension for the selected variable",
718 false, 0, &allowed_dim_vals);
719 cmd.add(arg_dim_time);
721 TCLAP::ValueArg<std::string> arg_varname(
722 "v",
"var",
"variable included in the the netCDF file",
false,
"",
724 cmd.add(arg_varname);
726 TCLAP::ValueArg<std::string> arg_output(
727 "o",
"output",
"Output (.vtu). The OGS mesh output file",
true,
"",
730 TCLAP::ValueArg<std::string> arg_input(
"i",
"input",
731 "Input (.nc). The netCDF input file",
732 true,
"",
"INPUT_FILE");
735 cmd.add(log_level_arg);
736 cmd.parse(argc, argv);
741 NcFile dataset(arg_input.getValue().c_str(), NcFile::read);
743 if (dataset.isNull())
745 ERR(
"Error opening file.");
749 std::size_t
const mutex =
750 arg_single_file.isSet() + arg_multi_files.isSet() + arg_images.isSet();
753 ERR(
"Only one output format can be specified (single-file, multi-file, "
758 std::cout <<
"OpenGeoSys NetCDF Converter\n";
759 if (!arg_varname.isSet())
761 std::cout <<
"File " << arg_input.getValue()
762 <<
" loaded. Press ENTER to display available data arrays.\n";
766 std::string
const& output_name(arg_output.getValue());
767 std::string
const& var_name = (arg_varname.isSet())
768 ? arg_varname.getValue()
770 NcVar
const& var = dataset.getVar(var_name);
773 ERR(
"Variable \"{:s}\" not found in file.", arg_varname.getValue());
777 std::vector<std::size_t> dim_idx_map(var.getDimCount(), 0);
778 bool is_time_dep(
false);
779 if (arg_dim1.isSet() && arg_dim2.isSet())
781 is_time_dep = arg_dim_time.isSet();
782 if (!
assignDimParams(var, dim_idx_map, arg_dim_time, arg_dim1, arg_dim2,
793 std::pair<std::size_t, std::size_t> time_bounds(0, 0);
796 (arg_time_start.isSet())
798 arg_time_start, arg_time_end)
802 if (arg_images.isSet())
806 else if (arg_multi_files.isSet())
810 else if (arg_single_file.isSet() || !is_time_dep ||
811 time_bounds.first == time_bounds.second)
820 std::size_t
const temp_offset = (is_time_dep) ? 1 : 0;
821 std::size_t
const n_dims = (var.getDimCount());
826 elem_type = (arg_elem_type.isSet())
833 if (arg_nodata.isSet())
838 if (!
convert(dataset, var, output_name, dim_idx_map, is_time_dep,
839 time_bounds, output, elem_type))
844 std::cout <<
"Conversion finished successfully.\n";
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
static void showArrays(NcFile const &dataset)
static double no_data_input
static OutputType multFilesSelectionLoop(std::pair< std::size_t, std::size_t > const &time_bounds)
int main(int argc, char *argv[])
static std::string arraySelectionLoop(NcFile const &dataset)
static MathLib::Point3d getOrigin(NcFile const &dataset, NcVar const &var, std::vector< std::size_t > const &dim_idx_map, std::size_t const time_offset)
static GeoLib::RasterHeader createRasterHeader(NcFile const &dataset, NcVar const &var, std::vector< std::size_t > const &dim_idx_map, std::vector< std::size_t > const &length, std::size_t const time_offset)
static void showErrorMessage(std::size_t const error_id, std::size_t const max=0)
static std::pair< double, double > getDimLength(NcVar const &var, std::size_t const dim)
static std::pair< std::size_t, std::size_t > assignTimeBounds(NcVar const &var, TCLAP::ValueArg< std::size_t > &arg_time_start, TCLAP::ValueArg< std::size_t > &arg_time_end)
static std::pair< double, double > getBoundaries(NcVar const &var)
static std::size_t parseInput(std::string const &request_str, std::size_t const max_val, bool const has_info=false)
static std::string getIterationString(std::size_t i, std::size_t max)
static MeshLib::MeshElemType assignElemType(TCLAP::ValueArg< std::string > &arg_elem_type)
static MeshLib::MeshElemType elemSelectionLoop(std::size_t const dim)
static bool assignDimParams(NcVar const &var, std::vector< std::size_t > &dim_idx_map, TCLAP::ValueArg< std::size_t > &arg_dim_time, TCLAP::ValueArg< std::size_t > &arg_dim1, TCLAP::ValueArg< std::size_t > &arg_dim2, TCLAP::ValueArg< std::size_t > &arg_dim3)
static std::pair< std::size_t, std::size_t > timestepSelectionLoop(NcVar const &var, std::size_t const dim_idx)
static const double no_data_output
static void checkExit(std::string const &str)
static void flipRaster(std::vector< double > &data, std::size_t const layers, std::size_t const width, std::size_t const height)
static std::vector< std::size_t > getLength(NcVar const &var, std::size_t const time_offset)
static std::vector< std::string > getArrays(NcFile const &dataset)
static NcVar getDimVar(NcFile const &dataset, NcVar const &var, std::size_t const dim)
static bool dimensionSelectionLoop(NcVar const &var, std::vector< std::size_t > &dim_idx_map)
static double getResolution(NcFile const &dataset, NcVar const &var)
static std::vector< double > getData(NcVar const &var, std::size_t const total_length, std::size_t const time_step, std::vector< std::size_t > const &length)
static bool convert(NcFile const &dataset, NcVar const &var, std::string const &output_name, std::vector< std::size_t > const &dim_idx_map, std::size_t const time_offset, std::pair< std::size_t, std::size_t > const &time_bounds, OutputType const output, MeshLib::MeshElemType const elem_type)
static bool canConvert(NcVar const &var)
static void showArraysDims(NcVar const &var)
static void writeRasterAsASC(GeoLib::Raster const &raster, std::string const &file_name)
Writes an Esri asc-file.
Class Raster is used for managing raster data.
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
std::string getFileExtension(const std::string &path)
std::string dropFileExtension(std::string const &filename)
GITINFOLIB_EXPORT const std::string ogs_version
void addPropertyToMesh(Mesh &mesh, std::string_view name, MeshItemType item_type, std::size_t number_of_components, std::span< T const > values)
UseIntensityAs
Selection of possible interpretations for intensities.
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.