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/error.hpp>
22 #include <icedb/fs_backend.hpp>
23 #include "shape.hpp"
24 #include "shapeIOtext.hpp"
25 
26 // A list of valid shapefile output formats
27 const std::map<std::string, std::set<sfs::path> > file_formats = {
28  {"text", {".dat", ".shp", ".txt", ".shape"} },
29  {"icedb", {".hdf5", ".nc", ".h5", ".cdf", ".hdf"} },
30  {"psu", {".nc"}}
31 };
32 
33 // These get set in main(int,char**).
34 float resolution_um = 0;
35 
37 
38 int main(int argc, char** argv) {
39  try {
40  using namespace std;
41  // Read program options
42 
43  namespace po = boost::program_options;
44  po::options_description desc("Allowed options");
45  desc.add_options()
46  ("help,h", "produce help message")
47  ("from", po::value<vector<string> >()->multitoken(), "The paths where shapes are read from")
48  ("to", po::value<string>(), "The path where the shape is written to")
49  ("db-path", po::value<string>()->default_value("shape"), "The path within the database to write to")
50  ("create", "Create the output database if it does not exist")
51  ("resolution", po::value<float>(), "Lattice spacing for the shape, in um")
52  ("truncate", "Instead of opening existing output files in read-write mode, truncate them.")
53  ("from-format", po::value<string>()->default_value("text"), "The format of the input files. Options: text, psu.")
54  ;
55  po::variables_map vm;
56  po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
57  po::notify(vm);
58 
59  auto doHelp = [&](const string& s)->void
60  {
61  cout << s << endl;
62  cout << desc << endl;
63  exit(1);
64  };
65  if (vm.count("help")) doHelp("");
66  if (!vm.count("from") || !vm.count("to")) doHelp("Need to specify to/from locations.");
67 
68  using namespace icedb;
69 
70  // namespace sfs defined for compatability. See <icedb/fs_backend.hpp>
71  vector<string> vsFromRaw = vm["from"].as<vector<string> >();
72  string sToRaw = vm["to"].as<string>();
73 
74  sfs::path pToRaw(sToRaw);
75  string dbpath = vm["db-path"].as<string>();
76  if (vm.count("resolution")) resolution_um = vm["resolution"].as<float>();
77  string informat = vm["from-format"].as<string>();
78 
79  // Create the output database if it does not exist
80  auto iof = fs::IOopenFlags::READ_WRITE;
81  if (vm.count("create")) iof = fs::IOopenFlags::CREATE;
82  if (vm.count("truncate")) iof = fs::IOopenFlags::TRUNCATE;
83  if (!sfs::exists(pToRaw)) iof = fs::IOopenFlags::CREATE;
84  Databases::Database::Database_ptr db = Databases::Database::openDatabase(pToRaw.string(), iof);
85 
86  // Changes start here
87  for (const auto &sFromRaw : vsFromRaw)
88  {
89  sfs::path pFromRaw(sFromRaw);
90  auto files = icedb::fs::impl::collectDatasetFiles(pFromRaw, file_formats.at(informat));
91  for (const auto &f : files)
92  {
93  // Reading the shape from the text file
95  if (informat == "text")
96  data = icedb::Examples::Shapes::readTextFile(f.first.string());
97  else if (informat == "psu")
98  data = icedb::Examples::Shapes::readPSUfile(f.first.string());
100  .add("Description", "Unknown input file format. See program help for a list of valid formats.")
101  .add("Current-format", informat);
102 
103  // Set a basic particle id. This id is used when writing the shape to the output file.
104  // In this example, objects in the output file are named according to their ids.
105  data.required.particle_id = pFromRaw.filename().string();
106  if (resolution_um)
108 
109  // Writing the shape to the HDF5/netCDF file
110 
111  basegrp = db->createGroupStructure(dbpath);
112  auto shpgrp = basegrp->createGroup(data.required.particle_id);
113  auto shp = data.toShape(data.required.particle_id, shpgrp->getHDF5Group());
114  }
115  }
116  }
117  // Ensure that unhandled errors are displayed before the application terminates.
118  catch (const std::exception &e) {
119  std::cerr << e.what() << std::endl;
120  return 1;
121  }
122  catch (...) {
123  std::cerr << "Unknown exception caught." << std::endl;
124  return 1;
125  }
126  return 0;
127 }
CollectedFilesRet_Type collectDatasetFiles(const sfs::path &base, const ExtensionsMatching_Type &valid_extensions)
Definition: fs_backend.cpp:26
STL namespace.
#define ICEDB_throw(x)
Definition: error.hpp:88
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
icedb::Shapes::Shape::Shape_Type toShape(const std::string &name, std::shared_ptr< H5::Group >) const
Make a new shape under the group.
Definition: shapeIOtext.cpp:55
std::unique_ptr< Groups::Group > Group_ptr
Definition: Group.hpp:30
ShapeDataBasic readPSUfile(const std::string &filename)
Reads a Penn State-style geometry file.
Definition: shapeIOpsu.cpp:137
ShapeCommonOptionalData optional
Definition: shape.hpp:44
int main(int argc, char **argv)
The main body of the program.