[cig-commits] r13573 - cs/cigma/trunk/src

luis at geodynamics.org luis at geodynamics.org
Tue Dec 9 18:14:48 PST 2008


Author: luis
Date: 2008-12-09 18:14:48 -0800 (Tue, 09 Dec 2008)
New Revision: 13573

Modified:
   cs/cigma/trunk/src/io_hdf5_reader.cpp
   cs/cigma/trunk/src/io_hdf5_reader.h
Log:
Methods for reading attributes from file

Modified: cs/cigma/trunk/src/io_hdf5_reader.cpp
===================================================================
--- cs/cigma/trunk/src/io_hdf5_reader.cpp	2008-12-10 02:14:45 UTC (rev 13572)
+++ cs/cigma/trunk/src/io_hdf5_reader.cpp	2008-12-10 02:14:48 UTC (rev 13573)
@@ -52,9 +52,10 @@
 // ----------------------------------------------------------------------------
 
 template <typename T> static H5::PredType predtype_from();
+template <> H5::PredType predtype_from<int>()   { return H5::PredType::NATIVE_INT; }
+template <> H5::PredType predtype_from<long>()  { return H5::PredType::NATIVE_LONG; }
 template <> H5::PredType predtype_from<float>() { return H5::PredType::NATIVE_FLOAT; }
 template <> H5::PredType predtype_from<double>(){ return H5::PredType::NATIVE_DOUBLE; }
-template <> H5::PredType predtype_from<int>()   { return H5::PredType::NATIVE_INT; }
 
 template <typename T> static H5T_class_t typeclass_from();
 template <> H5T_class_t typeclass_from<float>()  { return H5T_FLOAT; }
@@ -150,3 +151,121 @@
 }
 
 // ----------------------------------------------------------------------------
+
+static H5::H5Object* load_h5obj(H5::H5File *file, const char *loc)
+{
+    H5::H5Object* obj = 0;
+
+    if (file)
+    {
+        H5::Exception::dontPrint();
+
+        try
+        {
+            obj = new H5::Group(file->openGroup(loc));
+        }
+        catch (H5::Exception error)
+        {
+        }
+
+        if (obj == 0)
+        {
+            try
+            {
+                obj = new H5::DataSet(file->openDataSet(loc));
+            }
+            catch (H5::Exception error)
+            {
+            }
+        }
+    }
+
+    return obj;
+}
+
+int HDF5_Reader::readAttrString(const char *loc, const char *attr_name, std::string& val)
+{
+    H5::H5Object* obj = 0;
+    H5::Attribute* attr = 0;
+
+    if (file)
+    {
+        try
+        {
+            obj = load_h5obj(file, loc);
+            if (obj == 0) { return -2; }
+
+            H5::StrType tid(0, H5T_VARIABLE);
+            attr = new H5::Attribute(obj->openAttribute(attr_name));
+            attr->read(tid, val);
+        }
+        catch (H5::Exception error)
+        {
+            if (obj) { delete obj; }
+            if (attr) { delete attr; }
+            return -1;
+        }
+
+        if (obj) { delete obj; }
+        if (attr) { delete attr; }
+    }
+
+    return 0;
+}
+
+template <typename T>
+static int read_attribute(H5::H5File *file, const char *loc, const char *attr_name, T* val)
+{
+    H5::H5Object* obj = 0;
+    H5::Attribute* attr = 0;
+
+    if (file)
+    {
+        try
+        {
+            obj = load_h5obj(file, loc);
+            if (obj == 0) { return -2; }
+
+            if (obj == 0)
+            {
+                return -2;
+            }
+
+            attr = new H5::Attribute(obj->openAttribute(attr_name));
+            attr->read(predtype_from<T>(), val);
+        }
+        catch (H5::Exception error)
+        {
+            if (obj) { delete obj; }
+            if (attr) { delete attr; }
+            return -1;
+        }
+
+        if (obj) { delete obj; }
+        if (attr) { delete attr; }
+    }
+
+    return 0;
+}
+
+int HDF5_Reader::readAttrInteger(const char *loc, const char *attr_name, int& val)
+{
+    return read_attribute<int>(file, loc, attr_name, &val);
+}
+
+int HDF5_Reader::readAttrLong(const char *loc, const char *attr_name, long& val)
+{
+    return read_attribute<long>(file, loc, attr_name, &val);
+}
+
+int HDF5_Reader::readAttrFloat(const char *loc, const char *attr_name, float& val)
+{
+    return read_attribute<float>(file, loc, attr_name, &val);
+}
+
+int HDF5_Reader::readAttrDouble(const char *loc, const char *attr_name, double& val)
+{
+    return read_attribute<double>(file, loc, attr_name, &val);
+}
+
+// ----------------------------------------------------------------------------

Modified: cs/cigma/trunk/src/io_hdf5_reader.h
===================================================================
--- cs/cigma/trunk/src/io_hdf5_reader.h	2008-12-10 02:14:45 UTC (rev 13572)
+++ cs/cigma/trunk/src/io_hdf5_reader.h	2008-12-10 02:14:48 UTC (rev 13573)
@@ -24,6 +24,12 @@
     int getCoordinates(const char *loc, double **coordinates, int *nno, int *nsd);
     int getConnectivity(const char *loc, int **connectivity, int *nel, int *ndofs);
     
+    int readAttrString(const char *loc, const char *attr_name, std::string& val);
+    int readAttrInteger(const char *loc, const char *attr_name, int& val);
+    int readAttrLong(const char *loc, const char *attr_name, long& val);
+    int readAttrFloat(const char *loc, const char *attr_name, float& val);
+    int readAttrDouble(const char *loc, const char *attr_name, double& val);
+
 public:
     H5::H5File *file;
 };



More information about the CIG-COMMITS mailing list