2012-01-16 12:30:50 +00:00
|
|
|
#include <octave/oct.h>
|
|
|
|
#include <octave/ov-struct.h>
|
|
|
|
#include "hdf5.h"
|
|
|
|
|
2013-09-30 12:18:01 +00:00
|
|
|
// this special treatment is necessary because Win32-Octave ships with a very old hdf5 version (1.6.10)
|
|
|
|
void CloseH5Object(hid_t obj)
|
|
|
|
{
|
|
|
|
H5Oclose(obj);
|
|
|
|
}
|
|
|
|
|
2012-01-16 12:30:50 +00:00
|
|
|
DEFUN_DLD (h5readatt_octave, args, nargout, "h5readatt_octave(<File_Name>,<DataSet_Name>,<Attribute_Name>)")
|
|
|
|
{
|
|
|
|
octave_value retval;
|
|
|
|
int nargin = args.length();
|
|
|
|
if (nargin != 3)
|
|
|
|
{
|
|
|
|
print_usage();
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
if ((args(0).is_string()==false) || (args(1).is_string()==false) || (args(2).is_string()==false))
|
|
|
|
{
|
|
|
|
print_usage();
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
//suppress hdf5 error output
|
2016-09-26 12:35:33 +00:00
|
|
|
H5Eset_auto2(H5E_DEFAULT, NULL, NULL);
|
2012-01-16 12:30:50 +00:00
|
|
|
|
|
|
|
hid_t file = H5Fopen( args(0).string_value().c_str(), H5F_ACC_RDONLY, H5P_DEFAULT );
|
|
|
|
if (file==-1)
|
|
|
|
{
|
|
|
|
error("h5readatt_octave: opening the given File failed");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-02-06 13:26:06 +00:00
|
|
|
hid_t obj = H5Oopen(file, args(1).string_value().c_str(), H5P_DEFAULT);
|
|
|
|
|
|
|
|
if (obj==-1)
|
2012-01-16 12:30:50 +00:00
|
|
|
{
|
2013-09-30 12:18:01 +00:00
|
|
|
CloseH5Object(obj);
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Fclose(file);
|
2012-02-02 10:39:35 +00:00
|
|
|
error("h5readatt_octave: opening the given Object failed");
|
2012-01-16 12:30:50 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-09-26 12:35:33 +00:00
|
|
|
hid_t attr = H5Aopen_by_name(obj, ".", args(2).string_value().c_str(), H5P_DEFAULT, H5P_DEFAULT);
|
|
|
|
|
2012-01-16 12:30:50 +00:00
|
|
|
if (attr==-1)
|
|
|
|
{
|
2013-09-30 12:18:01 +00:00
|
|
|
CloseH5Object(obj);
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Fclose(file);
|
2012-01-16 12:30:50 +00:00
|
|
|
error("h5readatt_octave: opening the given Attribute failed");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-02-02 10:39:35 +00:00
|
|
|
hid_t type = H5Aget_type(attr);
|
|
|
|
if (type<0)
|
|
|
|
{
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Aclose(attr);
|
2013-09-30 12:18:01 +00:00
|
|
|
CloseH5Object(obj);
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Fclose(file);
|
2012-02-02 10:39:35 +00:00
|
|
|
error("h5readatt_octave: dataset type error");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (H5Tget_class(type)!=H5T_FLOAT)
|
|
|
|
{
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Aclose(attr);
|
2013-09-30 12:18:01 +00:00
|
|
|
CloseH5Object(obj);
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Fclose(file);
|
2012-02-02 10:39:35 +00:00
|
|
|
error("h5readatt_octave: attribute type not supported");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-09-21 12:12:16 +00:00
|
|
|
size_t numVal = H5Aget_storage_size(attr)/H5Tget_size(type);
|
|
|
|
double value[numVal];
|
|
|
|
if (H5Tget_size(type)==sizeof(float))
|
2012-01-16 12:30:50 +00:00
|
|
|
{
|
2012-09-21 12:12:16 +00:00
|
|
|
float f_value[numVal];
|
|
|
|
if (H5Aread(attr, H5T_NATIVE_FLOAT, f_value)<0)
|
|
|
|
{
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Aclose(attr);
|
2013-09-30 12:18:01 +00:00
|
|
|
CloseH5Object(obj);
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Fclose(file);
|
2012-09-21 12:12:16 +00:00
|
|
|
error("h5readatt_octave: reading the given Attribute failed");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
for (size_t n=0;n<numVal;++n)
|
|
|
|
value[n] = f_value[n];
|
|
|
|
}
|
|
|
|
else if (H5Tget_size(type)==sizeof(double))
|
|
|
|
{
|
|
|
|
if (H5Aread(attr, H5T_NATIVE_DOUBLE, value)<0)
|
|
|
|
{
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Aclose(attr);
|
2013-09-30 12:18:01 +00:00
|
|
|
CloseH5Object(obj);
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Fclose(file);
|
2012-09-21 12:12:16 +00:00
|
|
|
error("h5readatt_octave: reading the given Attribute failed");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Aclose(attr);
|
2013-09-30 12:18:01 +00:00
|
|
|
CloseH5Object(obj);
|
2013-06-21 11:19:00 +00:00
|
|
|
H5Fclose(file);
|
2012-09-21 12:12:16 +00:00
|
|
|
error("h5readatt_octave: reading the given Attribute failed: unknown type");
|
|
|
|
return retval;
|
2012-01-16 12:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
H5Aclose(attr);
|
2013-09-30 12:18:01 +00:00
|
|
|
CloseH5Object(obj);
|
2012-01-16 12:30:50 +00:00
|
|
|
H5Fclose(file);
|
2012-02-02 10:39:35 +00:00
|
|
|
Matrix mat(numVal,1);
|
|
|
|
for (size_t n=0;n<numVal;++n)
|
|
|
|
mat(n)=value[n];
|
|
|
|
retval = octave_value(mat);
|
2012-01-16 12:30:50 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|