[cig-commits] r13561 - cs/cigma/trunk/src
luis at geodynamics.org
luis at geodynamics.org
Tue Dec 9 18:14:22 PST 2008
Author: luis
Date: 2008-12-09 18:14:22 -0800 (Tue, 09 Dec 2008)
New Revision: 13561
Added:
cs/cigma/trunk/src/h5attr.cpp
Log:
Copy utility program h5attr from cigma-0.9 branch
Added: cs/cigma/trunk/src/h5attr.cpp
===================================================================
--- cs/cigma/trunk/src/h5attr.cpp (rev 0)
+++ cs/cigma/trunk/src/h5attr.cpp 2008-12-10 02:14:22 UTC (rev 13561)
@@ -0,0 +1,368 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+#include <boost/program_options.hpp>
+#include <boost/filesystem.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/algorithm/string.hpp>
+#include <boost/lexical_cast.hpp>
+
+#include "tri_logger.hpp"
+
+#include "H5Cpp.h"
+
+using namespace std;
+using namespace boost;
+namespace po = boost::program_options;
+namespace fs = boost::filesystem;
+
+
+// ----------------------------------------------------------------------------
+
+void split_on_colon(const string& s, vector<string>& ss)
+{
+ boost::split(ss, s, boost::is_any_of(":"), boost::token_compress_on);
+}
+
+bool split_path(const string& s, std::pair<string,string>& p)
+{
+ p.first = "";
+ p.second = "";
+
+ size_t colon = s.find(':');
+
+ if (colon != string::npos)
+ {
+ vector<string> ss;
+ split_on_colon(s, ss);
+ if (ss.size() == 2)
+ {
+ p.first = ss[0];
+ p.second = ss[1];
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ else
+ {
+ p.first = s;
+ p.second = "/";
+ }
+
+ return true;
+}
+
+
+
+// ----------------------------------------------------------------------------
+
+int main(int argc, char *argv[])
+{
+ string filename, location;
+ string attr_name;
+ string attr_loc;
+ string attr_value;
+ string attr_type = "string";
+ bool verbose = false;
+ bool overwrite = true;
+
+ po::options_description all, opts, other;
+ po::positional_options_description args;
+ po::variables_map vm;
+
+ opts.add_options()
+ ("location,l", po::value<string>(&attr_loc), "Where to store the attribute <file:path>")
+ ("name,n", po::value<string>(&attr_name), "Name of the attribute")
+ ("value,v", po::value<string>(&attr_value), "Value of the attribute")
+ ("value-type,t", po::value<string>(&attr_type), "Type of attribute (default 'string')")
+ ;
+
+ other.add_options()
+ ("help,h", "produce help message")
+ ("debug,D", "print debugging information")
+ ("no-overwrite", "don't overwrite existing values (default off)")
+ ;
+
+ all.add(opts).add(other);
+
+ args.add("location", 1);
+ args.add("name", 1);
+ args.add("value", 1);
+ args.add("value-type", 1);
+
+ po::store(po::command_line_parser(argc, argv).options(all).positional(args).run(), vm);
+
+ po::notify(vm);
+
+
+ if (vm.count("help") || (argc == 1))
+ {
+ cout << "Usage: h5attr <LOCATION> <ATTR-NAME> <VALUE> <TYPE>" << endl;
+ cout << endl;
+
+ if (opts.options().size() > 0)
+ {
+ cout << "Main Options: " << endl;
+ cout << opts << endl;
+ }
+
+ if (other.options().size() > 0)
+ {
+ cout << "Other Options: " << endl;
+ cout << other << endl;
+ }
+
+ cout << endl;
+ cout << "The attribute types can be one of the following" << endl;
+ cout << " - string (default)" << endl;
+ cout << " - integer" << endl;
+ cout << " - float" << endl;
+ cout << " - double" << endl;
+ return 1;
+ }
+
+ if (!vm.count("debug"))
+ {
+ TRI_LOG_OFF();
+ verbose = true;
+ }
+
+ if (vm.count("no-overwrite"))
+ {
+ overwrite = false;
+ }
+
+ TRI_LOG_STR("main()");
+ TRI_LOG(attr_loc);
+ TRI_LOG(attr_name);
+ TRI_LOG(attr_value);
+ TRI_LOG(attr_type);
+ TRI_LOG(overwrite);
+
+ // Check that attribute name is not empty
+ if (attr_name == "")
+ {
+ cout << "Error: Attribute name not specified (use --name option)" << endl;
+ return -1;
+ }
+
+ // Validate location
+ std::pair<string,string> path;
+ if (!split_path(attr_loc, path))
+ {
+ cout << "Error: Invalid location '" << attr_loc << "'" << endl;
+ return -1;
+ }
+
+ filename = path.first;
+ location = path.second;
+
+ TRI_LOG(filename);
+ TRI_LOG(location);
+
+ // Validate filename
+ if (!fs::exists(filename))
+ {
+ cout << "Error: File '" << filename << "' does not exist!" << endl;
+ return -1;
+ }
+
+ // Turn off auto-printing on error
+ H5::Exception::dontPrint();
+
+ // Load file
+ H5::H5File* file = 0;
+ try
+ {
+ file = new H5::H5File(filename, H5F_ACC_RDWR);
+ TRI_LOG(file);
+ }
+ catch (H5::FileIException error)
+ {
+ cout << "Error: File '" << filename << "' not found" << endl;
+ if (verbose) { error.printError(); }
+ return -1;
+ }
+
+ // Load location
+ H5::H5Object* obj = 0;
+ try
+ {
+ obj = new H5::Group(file->openGroup(location));
+ }
+ catch (H5::Exception error)
+ {
+ }
+ if (obj == 0)
+ {
+ try
+ {
+ obj = new H5::DataSet(file->openDataSet(location));
+ }
+ catch (H5::Exception error)
+ {
+ }
+ }
+ if (obj == 0)
+ {
+ cout << "Error: Could not open location '" << location << "'" << endl;
+ return -1;
+ }
+
+ H5::Attribute* attr = 0;
+ if (vm.count("value"))
+ {
+ //
+ // Set attribute value
+ //
+ TRI_LOG_STR("setattr()");
+ try
+ {
+ shared_ptr<H5::DataType> datatype;
+
+ if (attr_type == "string")
+ {
+ datatype = shared_ptr<H5::DataType>(new H5::StrType(0, H5T_VARIABLE));
+ }
+ else if (attr_type == "integer")
+ {
+ datatype = shared_ptr<H5::DataType>(new H5::DataType(H5::PredType::NATIVE_INT));
+ }
+ else if (attr_type == "float")
+ {
+ datatype = shared_ptr<H5::DataType>(new H5::DataType(H5::PredType::NATIVE_FLOAT));
+ }
+ else if (attr_type == "double")
+ {
+ datatype = shared_ptr<H5::DataType>(new H5::DataType(H5::PredType::NATIVE_DOUBLE));
+ }
+ else
+ {
+ cout << "Error: Invalid type '" << attr_type << "'" << endl;
+ return -1;
+ }
+
+ H5::DataSpace dataspace(H5S_SCALAR);
+
+ try
+ {
+ attr = new H5::Attribute(obj->openAttribute(attr_name));
+ }
+ catch (H5::AttributeIException e)
+ {
+ }
+ if (attr != 0)
+ {
+ if (overwrite)
+ {
+ try
+ {
+ delete attr;
+ attr = 0;
+
+ obj->removeAttr(attr_name);
+ }
+ catch (H5::AttributeIException e)
+ {
+ }
+ }
+ else
+ {
+ cout << "Error: Cannot overwrite existing attribute '" << attr_name << "'" << endl;
+ return -1;
+ }
+ }
+
+ attr = new H5::Attribute(obj->createAttribute(attr_name, *datatype, dataspace));
+ TRI_LOG(attr);
+
+ if (attr_type == "string")
+ {
+ attr->write(*datatype, attr_value);
+ TRI_LOG(attr_value);
+ }
+ else if (attr_type == "integer")
+ {
+ int value = lexical_cast<int>(attr_value);
+ attr->write(*datatype, &value);
+ TRI_LOG(value);
+ }
+ else if (attr_type == "float")
+ {
+ float value = lexical_cast<float>(attr_value);
+ attr->write(*datatype, &value);
+ TRI_LOG(value);
+ }
+ else if (attr_type == "double")
+ {
+ double value = lexical_cast<double>(attr_value);
+ attr->write(*datatype, &value);
+ TRI_LOG(value);
+ }
+ }
+ catch (H5::Exception error)
+ {
+ cout << "Error: Could not set attribute '" << attr_name << "'" << endl;
+ if (verbose) { error.printError(); }
+ return -1;
+ }
+ catch (boost::bad_lexical_cast e)
+ {
+ cout << "Error: Could not cast value '" << attr_value << "' to " << attr_type << endl;
+ return -1;
+ }
+
+ }
+ else
+ {
+ //
+ // Get attribute value
+ //
+ TRI_LOG_STR("getattr()");
+ try
+ {
+ attr = new H5::Attribute(obj->openAttribute(attr_name));
+
+ H5T_class_t typeclass = attr->getTypeClass();
+ TRI_LOG(typeclass);
+
+ if (typeclass == H5T_STRING)
+ {
+ string value;
+ H5::StrType tid(0, H5T_VARIABLE);
+ attr->read(tid, value);
+ cout << attr_name << " = " << value << endl;
+ }
+ else if (typeclass == H5T_INTEGER)
+ {
+ long value;
+ attr->read(H5::PredType::NATIVE_LONG, &value);
+ cout << attr_name << " = " << value << endl;
+ }
+ else if (typeclass == H5T_FLOAT)
+ {
+ double value;
+ attr->read(H5::PredType::NATIVE_DOUBLE, &value);
+ cout << attr_name << " = " << value << endl;
+ }
+ }
+ catch (H5::Exception error)
+ {
+ cout << "Error: Could not get attribute '" << attr_name << "'" << endl;
+ if (verbose) { error.printError(); }
+ return -1;
+ }
+
+ }
+
+ // Clean up
+ if (attr) { delete attr; }
+ if (obj) { delete obj; }
+ if (file) { delete file; }
+
+ return 0;
+}
More information about the CIG-COMMITS
mailing list