27 #include <boost/program_options.hpp> 45 #include "../../lib/private/hdf5_supplemental.hpp" 54 std::vector<std::unique_ptr<std::thread> >
pool;
67 std::deque<std::tuple<icedb::Examples::Shapes::ShapeDataBasic, sfs::path, std::string> >
mywritestack;
71 {
"text", {
".dat",
".shp",
".txt",
".shape"} },
72 {
"icedb", {
".hdf5",
".nc",
".h5",
".cdf",
".hdf"} }
96 decltype(myreadstack)::value_type cur;
100 std::lock_guard<std::mutex> lStack(
mStack);
101 if (myreadstack.empty())
return;
102 cur = myreadstack.front();
104 myreadstack.pop_front();
114 data.required.NC4_compat =
nccompat;
116 data.optional.particle_scattering_element_spacing =
resolution_um / 1.e6f;
121 std::lock_guard<std::mutex> lOutStack(
mOutStack);
123 mywritestack.push_back(
124 std::tuple<icedb::Examples::Shapes::ShapeDataBasic, sfs::path, std::string>
125 (std::move(data), cur.first, cur.second));
133 if (numThreads > 1) numThreads-= 1;
134 for (
int i = 0; i < numThreads; ++i)
135 pool.push_back(std::make_unique<std::thread>(
readtask));
149 int main(
int argc,
char** argv) {
154 namespace po = boost::program_options;
155 po::options_description desc(
"Allowed options");
157 (
"help,h",
"produce help message")
158 (
"from", po::value<string>(),
"The path where shapes are read from")
159 (
"to", po::value<string>(),
"The path where shapes are written to")
160 (
"db-folder", po::value<string>()->default_value(
"shapes"),
"The path within the database to write to")
161 (
"create",
"Create the output database if it does not exist")
162 (
"resolution", po::value<float>(),
"Lattice spacing for the shape, in um")
163 (
"compression-level", po::value<int>()->default_value(6),
"Compression level (0-9). 0 is no compression, 9 is max compression.")
164 (
"nc4-compat", po::value<bool>()->default_value(
true),
"Generate a NetCDF4-compatible file")
165 (
"truncate",
"Instead of opening existing output files in read-write mode, truncate them.")
167 po::variables_map vm;
168 po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
171 auto doHelp = [&](
const string& s)->
void 174 cout << desc << endl;
177 if (vm.count(
"help")) doHelp(
"");
178 if (!vm.count(
"from") || !vm.count(
"to")) doHelp(
"Need to specify to/from locations.");
181 using namespace icedb;
182 int clev = vm[
"compression-level"].as<
int>();
183 nccompat = vm[
"nc4-compat"].as<
bool>();
189 string sFromRaw = vm[
"from"].as<
string>();
190 string sToRaw = vm[
"to"].as<
string>();
191 sfs::path pFromRaw(sFromRaw);
192 sfs::path pToRaw(sToRaw);
193 string dbfolder = vm[
"db-folder"].as<
string>();
194 if (vm.count(
"resolution"))
resolution_um = vm[
"resolution"].as<float>();
198 const int Num_Threads = thread::hardware_concurrency();
201 auto iof = fs::IOopenFlags::READ_WRITE;
202 if (vm.count(
"create")) iof = fs::IOopenFlags::CREATE;
203 if (vm.count(
"truncate")) iof = fs::IOopenFlags::TRUNCATE;
204 if (!sfs::exists(pToRaw)) iof = fs::IOopenFlags::CREATE;
206 basegrp = db->createGroupStructure(dbfolder);
210 for (
const auto &f : files) myreadstack.push_back(f);
217 decltype(mywritestack)::value_type cur;
221 std::unique_ptr<std::lock_guard<std::mutex> > lOutStack
222 = make_unique<std::lock_guard<std::mutex>>(
mOutStack);
228 if (mywritestack.empty()) {
232 std::this_thread::sleep_for(std::chrono::milliseconds(100));
237 cur = mywritestack.front();
238 mywritestack.pop_front();
247 std::string groupName = std::get<2>(cur);
248 if (groupName ==
"/") groupName =
"";
249 const std::string particleId = std::get<0>(cur).required.particle_id;
250 if (!groupName.size() && particleId.size()) groupName = particleId;
251 if (!groupName.size()) groupName =
"shape";
254 auto sgrp = basegrp->createGroup(groupName);
255 auto shp = std::get<0>(cur).toShape(
256 std::get<1>(cur).filename().string(), sgrp->getHDF5Group());
261 for (
auto &t : pool) t->join();
264 catch (
const std::exception &e) {
265 std::cerr << e.what() << std::endl;
269 std::cerr <<
"Unknown exception caught." << std::endl;
CollectedFilesRet_Type collectDatasetFiles(const sfs::path &base, const ExtensionsMatching_Type &valid_extensions)
std::vector< std::unique_ptr< std::thread > > pool
3d_structures program - An example program to read and write shape files
bool construct_thread_pool(int numThreads)
Convenience function to build a pool of threads for parsing shapes and writing to the hdf5 file...
bool nccompat
Are we forcing NetCDF-4 compatability.
std::deque< std::tuple< icedb::Examples::Shapes::ShapeDataBasic, sfs::path, std::string > > mywritestack
float resolution_um
The resolution of each shape lattice, in micrometers.
const std::map< std::string, std::set< sfs::path > > file_formats
ShapeDataBasic readTextFile(const std::string &filename)
void readtask()
This is the primary function in each thread devoted to reading shapes. Usually, about three of these ...
std::unique_ptr< Database > Database_ptr
icedb::Groups::Group::Group_ptr basegrp
Shapes get written to this location in the output database.
std::atomic< int > countCurrentReading
std::unique_ptr< Groups::Group > Group_ptr
int main(int argc, char **argv)
The main body of the program.
std::deque< std::pair< sfs::path, std::string > > myreadstack
ShapeRequiredData required