Canoe
Comprehensive Atmosphere N' Ocean Engine
combine_blocks.cpp
Go to the documentation of this file.
1 // C/C++ headers
2 #include <glob.h>
3 
4 #include <cstdio>
5 #include <cstring>
6 #include <iostream>
7 #include <sstream> // stringstream
8 #include <stdexcept>
9 
10 // athena
11 #include <athena/globals.hpp>
12 #include <athena/outputs/user_outputs.hpp>
13 
14 // canoe
15 #include <configure.hpp>
16 
17 int mppnccombine(int argc, char *argv[]);
18 
19 void NetcdfOutput::CombineBlocks() {
20 // Only proceed if NETCDF output enabled
21 #ifdef NETCDFOUTPUT
22 
23  std::stringstream msg;
24 #ifdef MPI_PARALLEL
25  MPI_Barrier(MPI_COMM_WORLD);
26 #endif
27  if (Globals::my_rank == 0) {
28  char number[64];
29  snprintf(number, sizeof(number), "%05d", output_params.file_number - 1);
30 
31  std::string infile;
32  infile.assign(output_params.file_basename);
33  infile.append(".block*.");
34  infile.append(output_params.file_id);
35  infile.append(".");
36  infile.append(number);
37  infile.append(".nc");
38 
39  glob_t glob_result;
40  int err = glob(infile.c_str(), GLOB_TILDE, NULL, &glob_result);
41  if (err != 0) {
42  globfree(&glob_result);
43  msg << "### FATAL ERROR in function [NetcdfOutput::CombineBlocks]"
44  << std::endl
45  << "glob() failed with error " << err << std::endl;
46  throw std::runtime_error(msg.str().c_str());
47  }
48 
49  std::string outfile;
50  outfile.assign(output_params.file_basename);
51  outfile.append(".");
52  outfile.append(output_params.file_id);
53  outfile.append(".");
54  outfile.append(number);
55  outfile.append(".nc");
56 
57  int argc = 3 + glob_result.gl_pathc;
58  // char argv[][2048] = {"CombineBlocks", "-r", outfile.c_str(),
59  // infile.c_str()};
60  char **argv = new char *[argc];
61  for (int i = 0; i < argc; ++i) argv[i] = new char[2048];
62  snprintf(argv[0], 2048, "%s", "CombineBlocks");
63  snprintf(argv[1], 2048, "%s", "-r");
64  snprintf(argv[2], 2048, "%s", outfile.c_str());
65  for (int i = 3; i < argc; ++i)
66  snprintf(argv[i], 2048, "%s", glob_result.gl_pathv[i - 3]);
67 
68  remove(outfile.c_str());
69  err = mppnccombine(argc, argv);
70  if (err) {
71  std::cerr << "### WARNING in function [NetcdfOutput::CombineBlocks]"
72  << std::endl
73  << "mppnccombine returns none zero.";
74  // throw std::runtime_error(msg.str().c_str());
75  }
76 
77  globfree(&glob_result);
78  for (int i = 0; i < argc; ++i) delete[] argv[i];
79  delete[] argv;
80  }
81 
82 #endif // NETCDFOUTPUT
83 }
int mppnccombine(int argc, char *argv[])