diff --git a/lib/Hdf5File.cpp b/lib/Hdf5File.cpp index a2c7845..ca2a079 100644 --- a/lib/Hdf5File.cpp +++ b/lib/Hdf5File.cpp @@ -124,8 +124,8 @@ void Hdf5File::save(const RandGenState &state, const string &name) group = h5File_->createGroup(name.c_str() + nameOffset(name)); attr = group.createAttribute("type", PredType::NATIVE_SHORT, attrSpace); attr.write(PredType::NATIVE_SHORT, &rgStateType); - dataset = group.createDataSet("data", PredType::NATIVE_DOUBLE, dataSpace); - dataset.write(state.data(), PredType::NATIVE_DOUBLE); + dataset = group.createDataSet("data", PredType::NATIVE_INT, dataSpace); + dataset.write(state.data(), PredType::NATIVE_INT); } // read first name //////////////////////////////////////////////////////////// @@ -262,7 +262,7 @@ void Hdf5File::load(RandGenState &state, const DataSet &d) dataspace.getSimpleExtentDims(dim); if (dim[0] != RLXG_STATE_SIZE) { - // error here + LATAN_ERROR(Io, "random generator state has a wrong length"); } d.read(state.data(), PredType::NATIVE_INT); } diff --git a/lib/Io.cpp b/lib/Io.cpp index f96601a..0f2bb69 100644 --- a/lib/Io.cpp +++ b/lib/Io.cpp @@ -25,20 +25,9 @@ using namespace Latan; string Io::getFirstName(const string &fileName) { - string ext = extension(fileName); - - if (ext == "h5") - { - return getFirstName(fileName); - } - else if ((ext == "dat")||(ext == "sample")||(ext == "seed")) - { - return getFirstName(fileName); - } - else - { - LATAN_ERROR(Io, "unknown file extension '" + ext + "'"); - } + std::unique_ptr file = open(fileName); + + return file->getFirstName(); } unique_ptr Io::open(const std::string &fileName, const unsigned int mode) diff --git a/lib/Io.hpp b/lib/Io.hpp index 8d5e461..20999d8 100644 --- a/lib/Io.hpp +++ b/lib/Io.hpp @@ -48,7 +48,7 @@ public: static std::string getFirstName(const std::string &fileName); static std::string getFirstName(const std::string &fileName); static std::unique_ptr open(const std::string &fileName, - const unsigned int mode = File::Mode::write); + const unsigned int mode = File::Mode::read); }; // template implementation ///////////////////////////////////////////////////// @@ -63,27 +63,16 @@ IoT Io::load(const std::string &fileName, const std::string &name) template IoT Io::load(const std::string &fileName, const std::string &name) { - std::string ext = extension(fileName); - - if (ext == "h5") - { - return load(fileName, name); - } - else if ((ext == "dat")||(ext == "sample")||(ext == "seed")) - { - return load(fileName, name); - } - else - { - LATAN_ERROR(Io, "unknown file extension '" + ext + "'"); - } + std::unique_ptr file = open(fileName); + + return file->read(name); } template void Io::save(const IoT &data, const std::string &fileName, const unsigned int mode, const std::string &name) { - FileType file(fileName, mode); + FileType file(fileName, mode); std::string realName = (name.empty()) ? fileName : name; file.save(data, realName); @@ -93,20 +82,10 @@ template void Io::save(const IoT &data, const std::string &fileName, const unsigned int mode, const std::string &name) { - std::string ext = extension(fileName); - - if (ext == "h5") - { - save(data, fileName, mode, name); - } - else if ((ext == "dat")||(ext == "sample")||(ext == "seed")) - { - save(data, fileName, mode, name); - } - else - { - LATAN_ERROR(Io, "unknown file extension '" + ext + "'"); - } + std::unique_ptr file = open(fileName, mode); + std::string realName = (name.empty()) ? fileName : name; + + file->save(data, realName); } template diff --git a/utils/Makefile.am b/utils/Makefile.am index 7c846d7..75b2a77 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -15,11 +15,16 @@ endif endif bin_PROGRAMS = \ + latan_create_rg_state \ latan_make_fake_sample\ latan_sample_combine \ latan_sample_read \ latan_resample +latan_create_rg_state_SOURCES = create_rg_state.cpp +latan_create_rg_state_CXXFLAGS = $(COM_CXXFLAGS) +latan_create_rg_state_LDFLAGS = -L../lib/.libs -lLatAnalyze + latan_make_fake_sample_SOURCES = make_fake_sample.cpp latan_make_fake_sample_CXXFLAGS = $(COM_CXXFLAGS) latan_make_fake_sample_LDFLAGS = -L../lib/.libs -lLatAnalyze diff --git a/utils/create_rg_state.cpp b/utils/create_rg_state.cpp new file mode 100644 index 0000000..9ce3c82 --- /dev/null +++ b/utils/create_rg_state.cpp @@ -0,0 +1,44 @@ +/* + * create_rg_state.cpp, part of LatAnalyze 3 + * + * Copyright (C) 2013 - 2015 Antonin Portelli + * + * LatAnalyze 3 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LatAnalyze 3 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LatAnalyze 3. If not, see . + */ + +#include +#include +#include + +using namespace Latan; +using namespace std; + +int main(int argc, char *argv[]) +{ + string outFilename; + + if (argc != 2) + { + cerr << "usage: " << argv[0] << " " << endl; + + return EXIT_FAILURE; + } + outFilename = argv[1]; + + RandGen gen; + + Io::save(gen.getState(), outFilename); + + return EXIT_SUCCESS; +} diff --git a/utils/resample.cpp b/utils/resample.cpp index 7311de9..213fa39 100644 --- a/utils/resample.cpp +++ b/utils/resample.cpp @@ -34,7 +34,7 @@ using namespace Latan; static void usage(const string &cmdName) { cerr << "usage: " << cmdName; - cerr << " [-n -b -r -o ]"; + cerr << " [-n -b -r -o -f {h5|sample}]"; cerr << " "; cerr << endl; exit(EXIT_FAILURE); @@ -45,11 +45,12 @@ int main(int argc, char *argv[]) // argument parsing //////////////////////////////////////////////////////// int c; string manFileName, nameFileName, stateFileName, cmdName, outDirName = "."; + string ext = "h5"; Index binSize = 1, nSample = DEF_NSAMPLE; opterr = 0; cmdName = basename(argv[0]); - while ((c = getopt(argc, argv, "b:n:r:o:")) != -1) + while ((c = getopt(argc, argv, "b:n:r:o:f:")) != -1) { switch (c) { @@ -65,6 +66,9 @@ int main(int argc, char *argv[]) case 'r': stateFileName = optarg; break; + case 'f': + ext = optarg; + break; case '?': cerr << "error parsing option -" << char(optopt) << endl; usage(cmdName); @@ -92,11 +96,12 @@ int main(int argc, char *argv[]) cout << "================================================" << endl; cout << "Data resampler" << endl; cout << "------------------------------------------------" << endl; - cout << " #file= " << dataFileName.size() << endl; - cout << " #name= " << name.size() << endl; - cout << " bin size= " << binSize << endl; - cout << " #sample= " << nSample << endl; - cout << "output dir: " << outDirName << endl; + cout << " #file= " << dataFileName.size() << endl; + cout << " #name= " << name.size() << endl; + cout << " bin size= " << binSize << endl; + cout << " #sample= " << nSample << endl; + cout << " output dir: " << outDirName << endl; + cout << "output format: " << ext << endl; cout << "------------------------------------------------" << endl; // data loading //////////////////////////////////////////////////////////// @@ -109,30 +114,13 @@ int main(int argc, char *argv[]) } for (unsigned int i = 0; i < dataFileName.size(); ++i) { - std::unique_ptr dataFile; + std::unique_ptr dataFile = Io::open(dataFileName[i]); - if (extension(dataFileName[i]) == "dat") - { - dataFile.reset(new AsciiFile); - } - else if (extension(dataFileName[i]) == "h5") - { - dataFile.reset(new Hdf5File); - } - else - { - cerr << "error: '" << dataFileName[i]; - cerr << "' has an unknown extension" << endl; - - return EXIT_FAILURE; - } cout << '\r' << ProgressBar(i + 1, dataFileName.size()); - dataFile->open(dataFileName[i], AsciiFile::Mode::read); for (const string &n: name) { data[n][i] = dataFile->read(n); } - dataFile->close(); } cout << endl; @@ -148,7 +136,7 @@ int main(int argc, char *argv[]) } for (unsigned int i = 0; i < name.size(); ++i) { - const string outFileName = name[i] + "_" + manFileName + ".sample"; + const string outFileName = name[i] + "_" + manFileName + "." + ext; cout << '\r' << ProgressBar(i + 1, name.size()); data[name[i]].bin(binSize);