icedb  version 0.5.1
Snow particle scattering database API
shapes-main.cpp
Go to the documentation of this file.
1 
13 #include <icedb/defs.h>
14 #include <boost/program_options.hpp>
15 #include <iostream>
16 #include <memory>
17 #include <string>
18 #include <vector>
19 #include <icedb/shape.hpp>
20 #include <icedb/Database.hpp>
21 #include <icedb/fs_backend.hpp>
22 #include "shape.hpp"
23 #include "shapeIOtext.hpp"
24 
25 // A list of valid shapefile output formats
26 const std::map<std::string, std::set<sfs::path> > file_formats = {
27  {"text", {".dat", ".shp", ".txt", ".shape"} },
28  {"icedb", {".hdf5", ".nc", ".h5", ".cdf", ".hdf"} }
29 };
30 
31 // These get set in main(int,char**).
32 float resolution_um = 0;
33 
35 
36 int main(int argc, char** argv) {
37  try {
38  using namespace std;
39  // Read program options
40 
41  namespace po = boost::program_options;
42  po::options_description desc("Allowed options");
43  desc.add_options()
44  ("help,h", "produce help message")
45  ("from", po::value<string>(), "The path where a shape is read from")
46  ("to", po::value<string>(), "The path where the shape is written to")
47  ("db-path", po::value<string>()->default_value("shape"), "The path within the database to write to")
48  ("create", "Create the output database if it does not exist")
49  ("resolution", po::value<float>(), "Lattice spacing for the shape, in um")
50  ("truncate", "Instead of opening existing output files in read-write mode, truncate them.")
51  ;
52  po::variables_map vm;
53  po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
54  po::notify(vm);
55 
56  auto doHelp = [&](const string& s)->void
57  {
58  cout << s << endl;
59  cout << desc << endl;
60  exit(1);
61  };
62  if (vm.count("help")) doHelp("");
63  if (!vm.count("from") || !vm.count("to")) doHelp("Need to specify to/from locations.");
64 
65  using namespace icedb;
66 
67  // namespace sfs defined for compatability. See <icedb/fs_backend.hpp>
68  string sFromRaw = vm["from"].as<string>();
69  string sToRaw = vm["to"].as<string>();
70  sfs::path pFromRaw(sFromRaw);
71  sfs::path pToRaw(sToRaw);
72  string dbpath = vm["db-path"].as<string>();
73  if (vm.count("resolution")) resolution_um = vm["resolution"].as<float>();
74 
75  // Create the output database if it does not exist
76  auto iof = fs::IOopenFlags::READ_WRITE;
77  if (vm.count("create")) iof = fs::IOopenFlags::CREATE;
78  if (vm.count("truncate")) iof = fs::IOopenFlags::TRUNCATE;
79  if (!sfs::exists(pToRaw)) iof = fs::IOopenFlags::CREATE;
80  Databases::Database::Database_ptr db = Databases::Database::openDatabase(pToRaw.string(), iof);
81 
82  // Reading the shape from the text file
83  auto data = icedb::Examples::Shapes::readTextFile(pFromRaw.string());
84  // Set a basic particle id. This id is used when writing the shape to the output file.
85  // In this example, objects in the output file are named according to their ids.
86  data.required.particle_id = pFromRaw.filename().string();
87  if (resolution_um)
88  data.optional.particle_scattering_element_spacing = resolution_um / 1.e6f;
89 
90  // Writing the shape to the HDF5/netCDF file
91 
92  basegrp = db->createGroupStructure(dbpath);
93  auto shp = data.toShape(data.required.particle_id, basegrp->getHDF5Group());
94  }
95  // Ensure that unhandled errors are displayed before the application terminates.
96  catch (const std::exception &e) {
97  std::cerr << e.what() << std::endl;
98  return 1;
99  }
100  catch (...) {
101  std::cerr << "Unknown exception caught." << std::endl;
102  return 1;
103  }
104  return 0;
105 }
STL namespace.
float resolution_um
The resolution of each shape lattice, in micrometers.
Definition: shapes-main.cpp:76
const std::map< std::string, std::set< sfs::path > > file_formats
Definition: shapes-main.cpp:70
ShapeDataBasic readTextFile(const std::string &filename)
std::unique_ptr< Database > Database_ptr
Definition: Database.hpp:20
icedb::Groups::Group::Group_ptr basegrp
Shapes get written to this location in the output database.
Definition: shapes-main.cpp:79
std::unique_ptr< Groups::Group > Group_ptr
Definition: Group.hpp:30
int main(int argc, char **argv)
The main body of the program.