diff --git a/h5read.cc b/h5read.cc index f346fd5..e168088 100644 --- a/h5read.cc +++ b/h5read.cc @@ -41,6 +41,16 @@ #include "gripes.h" #include "file-stat.h" +//FIXME: A proper check for those development libraries to exist should not +// happen by querying Octave build info (which is since 4.4 no longer +// exposed). One cannot assume that the system Octave was built is the +// same this package is built. Anyway the info if Octave was build with +// HDF5 does not matter for this package. +// +// The runtime information is available with +#define HAVE_HDF5 1 +#define HAVE_HDF5_18 1 + using namespace std; #if ((H5_VERS_MAJOR > 1) || (H5_VERS_MINOR >= 8)) @@ -58,7 +68,7 @@ using namespace std; bool any_int_leq_zero (const Matrix& mat) { - for (int i = 0; i < mat.length (); i++) + for (int i = 0; i < mat.numel (); i++) { if (mat(i) < 0.5) return true; @@ -76,7 +86,7 @@ check_vec (const octave_value& val, Matrix& mat/*out*/, if (error_state) return 0; - if (! mat.is_vector ()) + if (! mat.isvector ()) { error ("%s must be a vector", name); return 0; @@ -85,7 +95,7 @@ check_vec (const octave_value& val, Matrix& mat/*out*/, double mind, maxd; if (allow_zeros) { - for (int i = 0; i < mat.nelem (); i++) + for (int i = 0; i < mat.numel (); i++) { if (mat(i) == octave_Inf) mat(i) = 0; @@ -100,7 +110,7 @@ check_vec (const octave_value& val, Matrix& mat/*out*/, { error ("%s can only contain positive integers", name); return 0; - } + } return 1; } @@ -149,30 +159,30 @@ the appropriate size for the given HDF5 type.\n\ { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) gripe_disabled_feature ("h5read", "HDF5 IO"); - return octave_value_list (); + return octave_value (); #else int nargin = args.length (); if (nargin < 2 || nargin == 3 || nargin > 6 || nargout > 1) { print_usage (); - return octave_value_list (); + return octave_value (); } if (! (args(0).is_string () && args (1).is_string ())) { print_usage (); - return octave_value_list (); + return octave_value (); } string filename = args(0).string_value (); string dsetname = args(1).string_value (); if (error_state) - return octave_value_list (); + return octave_value (); //open the hdf5 file H5File file (filename.c_str (), false); if (error_state) - return octave_value_list (); + return octave_value (); if (nargin < 4) { @@ -183,7 +193,7 @@ the appropriate size for the given HDF5 type.\n\ { Matrix start, count, stride, block; int err = 0; - + err = err || ! check_vec (args(2), start, "START", false); start -= 1; @@ -200,7 +210,7 @@ the appropriate size for the given HDF5 type.\n\ err = err || ! check_vec (args(5), block, "BLOCK", false); if (err) - return octave_value_list (); + return octave_value (); return file.read_dset_hyperslab (dsetname.c_str (), start, count, stride, block, nargin-2); @@ -223,7 +233,7 @@ is to read.\n\ octave_value retval; #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) gripe_disabled_feature ("h5readatt", "HDF5 IO"); - return octave_value_list (); + return octave_value (); #else int nargin = args.length (); if (nargin != 3) @@ -241,13 +251,13 @@ is to read.\n\ string objname = args(1).string_value (); string attname = args(2).string_value (); if (error_state) - return octave_value_list (); - + return octave_value (); + //open the hdf5 file H5File file (filename.c_str (), false); if (error_state) - return octave_value_list (); - + return octave_value (); + retval = file.read_att (objname.c_str (), attname.c_str ()); return retval; @@ -294,42 +304,42 @@ the appropriate size for the given Octave type.\n\ { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) gripe_disabled_feature ("h5write", "HDF5 IO"); - return octave_value_list (); + return octave_value (); #else int nargin = args.length (); if (! (nargin == 3 || nargin == 5 || nargin == 6 || nargin == 7) || nargout != 0) { print_usage (); - return octave_value_list (); + return octave_value (); } if (! (args(0).is_string () && args(1).is_string ())) { print_usage (); - return octave_value_list (); + return octave_value (); } string filename = args(0).string_value (); string location = args(1).string_value (); - + if (error_state) - return octave_value_list (); - + return octave_value (); + if (nargin == 3) { //open the hdf5 file, create it if it does not exist H5File file (filename.c_str (), true); if (error_state) - return octave_value_list (); + return octave_value (); file.write_dset (location.c_str (), args(2)); } - else + else { //open the hdf5 file, complain if it does not exist H5File file (filename.c_str (), false); if (error_state) - return octave_value_list (); + return octave_value (); Matrix start, count, stride, block; int err = 0; @@ -350,14 +360,14 @@ the appropriate size for the given Octave type.\n\ err = err || ! check_vec (args(6), block, "BLOCK", false); if (err) - return octave_value_list (); + return octave_value (); file.write_dset_hyperslab (location.c_str (), args(2), start, count, stride, block, nargin-3); } - return octave_value_list (); + return octave_value (); #endif } @@ -374,37 +384,37 @@ the object named @var{objectname} in the HDF5 file specified by @var{filename}.\ { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) gripe_disabled_feature ("h5writeatt", "HDF5 IO"); - return octave_value_list (); + return octave_value (); #else int nargin = args.length (); if (nargin != 4 || nargout != 0) { print_usage (); - return octave_value_list (); + return octave_value (); } if (! (args(0).is_string () && args(1).is_string () && args(2).is_string ())) { print_usage (); - return octave_value_list (); + return octave_value (); } string filename = args(0).string_value (); string location = args(1).string_value (); string attname = args(2).string_value (); - + if (error_state) - return octave_value_list (); - + return octave_value (); + //open the hdf5 file H5File file (filename.c_str (), false); if (error_state) - return octave_value_list (); + return octave_value (); file.write_att (location.c_str (), attname.c_str (), args(3)); - return octave_value_list (); + return octave_value (); #endif } @@ -443,34 +453,34 @@ setting is not @sc{matlab} compatible.\n\ { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) gripe_disabled_feature("h5create", "HDF5 IO"); - return octave_value_list (); + return octave_value (); #else int nargin = args.length (); if (! (nargin == 3 || nargin == 5 || nargin == 7) || nargout != 0) { print_usage (); - return octave_value_list (); + return octave_value (); } if (! (args(0).is_string () && args(1).is_string ())) { print_usage (); - return octave_value_list (); + return octave_value (); } if ((nargin == 5 && ! args(3).is_string ()) || (nargin == 7 && ! args(5).is_string ())) { print_usage (); - return octave_value_list (); + return octave_value (); } string filename = args(0).string_value (); string location = args(1).string_value (); if (error_state) - return octave_value_list (); - + return octave_value (); + Matrix size; if (! check_vec (args(2), size, "SIZE", true)) - return octave_value_list (); + return octave_value (); // loop over the key-value pairs and see what is given @@ -484,7 +494,7 @@ setting is not @sc{matlab} compatible.\n\ if (error_state) { error ("Datatype argument must be a string"); - return octave_value_list (); + return octave_value (); } } else if (args(i).string_value () == "ChunkSize") @@ -494,31 +504,31 @@ setting is not @sc{matlab} compatible.\n\ if(args(i+1).string_value () != "auto") { error ("ChunkSize argument must be either a vector, or the string 'auto'."); - return octave_value_list (); + return octave_value (); } chunksize = args(2).matrix_value (); chunksize(0) = 0; } else if (! check_vec (args(i+1), chunksize, "ChunkSize", false)) - return octave_value_list (); + return octave_value (); } else { error ("unknown parameter name %s", args(i).string_value ().c_str ()); - return octave_value_list (); + return octave_value (); } } - - + + //open the hdf5 file H5File file (filename.c_str (), true); if (error_state) - return octave_value_list (); + return octave_value (); file.create_dset (location.c_str (), size, datatype.c_str (), chunksize); - - return octave_value_list (); + + return octave_value (); #endif } @@ -542,35 +552,35 @@ Note that this function is not @sc{matlab} compliant.\n\ { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) gripe_disabled_feature("h5delete", "HDF5 IO"); - return octave_value_list (); + return octave_value (); #else int nargin = args.length (); if (! (nargin == 2 || nargin == 3) || nargout != 0) { print_usage (); - return octave_value_list (); + return octave_value (); } if (! (args(0).is_string () && args(1).is_string ())) { print_usage (); - return octave_value_list (); + return octave_value (); } if (nargin == 3 && ! args(2).is_string ()) { print_usage (); - return octave_value_list (); + return octave_value (); } - + string filename = args(0).string_value (); string location = args(1).string_value (); if (error_state) - return octave_value_list (); + return octave_value (); //open the hdf5 file H5File file (filename.c_str (), true); if (error_state) - return octave_value_list (); + return octave_value (); if (nargin == 2) file.delete_link (location.c_str ()); else if (nargin == 3) @@ -579,8 +589,8 @@ Note that this function is not @sc{matlab} compliant.\n\ if(!error_state) file.delete_att (location.c_str (), attname.c_str ()); } - - return octave_value_list (); + + return octave_value (); #endif } @@ -594,7 +604,7 @@ H5File::H5File (const char *filename, const bool create_if_nonexisting) //suppress hdf5 error output H5Eset_auto (H5E_DEFAULT,0,0); - file_stat fs (filename); + octave::sys::file_stat fs (filename); if (! fs.exists () && create_if_nonexisting) file = H5Fcreate (filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); else if (! fs.exists () && ! create_if_nonexisting) @@ -625,7 +635,7 @@ H5File::~H5File () if (H5Iis_valid (dset_id)) H5Dclose (dset_id); - + if (H5Iis_valid (att_id)) H5Aclose (att_id); @@ -634,7 +644,7 @@ H5File::~H5File () if (H5Iis_valid (type_id)) H5Tclose (type_id); - + if (H5Iis_valid (mem_type_id)) H5Tclose (mem_type_id); @@ -659,7 +669,7 @@ template hsize_t* H5File::alloc_hsize (const T& dim, const int mode, const bool reverse) { - int rank = dim.length (); + int rank = dim.numel (); hsize_t *hsize = (hsize_t*)malloc (rank * sizeof (hsize_t)); for (int i = 0; i < rank; i++) { @@ -711,7 +721,7 @@ H5File::open_dset (const char *dsetname) error ("Error determining current dimensions and maximum size of dataset %s", dsetname); return -1; } - + return 0; } @@ -719,10 +729,10 @@ octave_value H5File::read_dset_complete (const char *dsetname) { if (open_dset (dsetname) < 0) - return octave_value_list (); + return octave_value (); mat_dims.resize (max (rank, 2)); - // .resize(1) still leaves mat_dims with a length of 2 for some reason, so + // .resize(1) still leaves mat_dims with a.numel of 2 for some reason, so // we need at least 2 filled mat_dims(0) = mat_dims(1) = 1; for (int i = 0; i < rank; i++) @@ -732,7 +742,7 @@ H5File::read_dset_complete (const char *dsetname) if (H5Sselect_all (dspace_id) < 0) { error ("Error selecting complete dataset %s", dsetname); - return octave_value_list (); + return octave_value (); } octave_value retval = read_dset (); @@ -746,44 +756,44 @@ H5File::read_dset_hyperslab (const char *dsetname, int nargin) { if (open_dset (dsetname) < 0) - return octave_value_list (); + return octave_value (); - if (rank == 0 && ! (start.is_empty () && count.is_empty () - && stride.is_empty () && block.is_empty ())) + if (rank == 0 && ! (start.isempty () && count.isempty () + && stride.isempty () && block.isempty ())) { error ("Cannot specify hyperslab for scalar datasets (rank 0)"); - return octave_value_list (); + return octave_value (); } - if (start.nelem () != rank) + if (start.numel () != rank) { - error ("start must be a vector of length %d, the dataset rank", rank); - return octave_value_list (); + error ("start must be a vector of.numel %d, the dataset rank", rank); + return octave_value (); } - if (count.nelem () != rank) + if (count.numel () != rank) { - error ("count must be a vector of length %d, the dataset rank", rank); - return octave_value_list (); + error ("count must be a vector of.numel %d, the dataset rank", rank); + return octave_value (); } - + Matrix _stride = stride; if (nargin < 3) _stride = Matrix (dim_vector(1, rank), 1); - if (_stride.nelem () != rank) + if (_stride.numel () != rank) { - error ("stride must be a vector of length %d, the dataset rank", rank); - return octave_value_list (); + error ("stride must be a vector of.numel %d, the dataset rank", rank); + return octave_value (); } Matrix _block = block; if (nargin < 4) _block = Matrix (dim_vector(1, rank), 1); - if (_block.nelem () != rank) + if (_block.numel () != rank) { - error ("block must be a vector of length %d, the dataset rank", rank); - return octave_value_list (); + error ("block must be a vector of.numel %d, the dataset rank", rank); + return octave_value (); } - - // .resize(1) still leaves mat_dims with a length of 2 for some reason, so + + // .resize(1) still leaves mat_dims with a.numel of 2 for some reason, so // we need at least 2 filled mat_dims.resize (max (rank, 2)); mat_dims(0) = mat_dims(1) = 1; @@ -795,7 +805,7 @@ H5File::read_dset_hyperslab (const char *dsetname, { error ("In dimension %d, requested stride %d smaller than block size %d", i+1, (int)_stride(i), (int)_block(i)); - return octave_value_list (); + return octave_value (); } if (_count(i) == 0) { @@ -804,13 +814,13 @@ H5File::read_dset_hyperslab (const char *dsetname, _count(i) = (h5_dims[rank-i-1] - start(i) - _block(i)) / _stride(i) + 1; } mat_dims(i) = _count(i)*_block(i); - int end = start(i) + _stride(i)*(_count(i)-1) + _block(i); // exclusive + unsigned int end = start(i) + _stride(i)*(_count(i)-1) + _block(i); // exclusive if (h5_dims[rank-i-1] < end) { error ("In dimension %d, dataset only has %d elements, but at least %d" " are required for requested hyperslab", i+1, (int)h5_dims[rank-i-1], end); - return octave_value_list (); + return octave_value (); } } @@ -829,7 +839,7 @@ H5File::read_dset_hyperslab (const char *dsetname, free (hblock); if (sel_result < 0) - return octave_value_list (); + return octave_value (); octave_value retval = read_dset (); return retval; @@ -838,7 +848,6 @@ H5File::read_dset_hyperslab (const char *dsetname, octave_value H5File::read_dset () { - bool is_cmplx = false; type_id = H5Dget_type (dset_id); hid_t complex_type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); @@ -857,7 +866,7 @@ H5File::read_dset () if (H5Sselect_valid (dspace_id) <= 0) \ { \ error ("selected dataspace is not valid"); \ - return octave_value_list (); \ + return octave_value (); \ } \ \ int mdc_nelem = -1; \ @@ -868,7 +877,7 @@ H5File::read_dset () &rdcc_nelem, &rdcc_nbytes, &rdcc_w0 ) < 0) \ { \ error ("could not determine raw data chunk cache parameters."); \ - return octave_value_list (); \ + return octave_value (); \ } \ /*cout << "cache params:" << rdcc_nelem << "," << rdcc_nbytes << endl;*/ \ herr_t read_result = H5Dread (dset_id, \ @@ -878,11 +887,11 @@ H5File::read_dset () if (read_result < 0) \ { \ error ("error when reading dataset"); \ - return octave_value_list (); \ + return octave_value (); \ } \ retval = octave_value (ret) // macro end - + HDF5_READ_DATA (type_id); } else if (H5Tget_class (type_id) == H5T_INTEGER) @@ -959,7 +968,7 @@ H5File::read_dset () HDF5_READ_DATA (H5T_NATIVE_DOUBLE); } H5Tclose (complex_type_id); - + return retval; } @@ -967,7 +976,7 @@ void H5File::write_dset (const char *dsetname, const octave_value ov_data) { - int rank = ov_data.dims ().length (); + int rank = ov_data.dims ().numel (); hsize_t *dims = alloc_hsize (ov_data.dims(), ALLOC_HSIZE_DEFAULT, true); dspace_id = H5Screate_simple (rank, dims, NULL); @@ -983,7 +992,7 @@ H5File::write_dset (const char *dsetname, //check if all groups in the path dsetname exist. if not, create them string path (dsetname); - for (int i=1; i < path.length (); i++) + for (unsigned int i=1; i < path.length (); i++) { if (path[i] == '/') { @@ -997,12 +1006,12 @@ H5File::write_dset (const char *dsetname, herr_t status; // find the right type - if (ov_data.is_complex_type ()) + if (ov_data.iscomplex ()) { //check if the data set already exists. if it does, open it, //otherwise, create it. Furthermore check if the datatype is //compliant with given octave data. - + #define OPEN_AND_WRITE if (H5Lexists (file,dsetname,H5P_DEFAULT)) \ { \ if (open_dset (dsetname) < 0) \ @@ -1018,12 +1027,12 @@ H5File::write_dset (const char *dsetname, status = H5Dwrite (dset_id, type_id, \ H5S_ALL, H5S_ALL, H5P_DEFAULT, \ data.fortran_vec ()) - + type_id = hdf5_make_complex_type (H5T_NATIVE_DOUBLE); ComplexNDArray data = ov_data.complex_array_value (); OPEN_AND_WRITE; } - else if (ov_data.is_integer_type ()) + else if (ov_data.isinteger ()) { if (ov_data.is_uint64_type ()) { @@ -1079,7 +1088,7 @@ H5File::write_dset (const char *dsetname, type_id = H5Tcopy (H5T_NATIVE_INT); OPEN_AND_WRITE; } - + } else if (ov_data.is_single_type ()) { @@ -1115,37 +1124,37 @@ H5File::write_dset_hyperslab (const char *dsetname, return; // check if the given hyperslab settings are reasonable - if (rank == 0 && ! (start.is_empty () && count.is_empty () - && stride.is_empty () && block.is_empty ())) + if (rank == 0 && ! (start.isempty () && count.isempty () + && stride.isempty () && block.isempty ())) { error ("Cannot specify hyperslab for scalar datasets (rank 0)"); return; } - if (start.nelem () != rank) + if (start.numel () != rank) { - error ("start must be a vector of length %d, the dataset rank", rank); + error ("start must be a vector of.numel %d, the dataset rank", rank); return; } - if (count.nelem () != rank) + if (count.numel () != rank) { - error ("count must be a vector of length %d, the dataset rank", rank); + error ("count must be a vector of.numel %d, the dataset rank", rank); return; } Matrix _stride = stride; if (nargin < 3) _stride = Matrix (dim_vector(1, rank), 1); - if (_stride.nelem () != rank) + if (_stride.numel () != rank) { - error ("stride must be a vector of length %d, the dataset rank", rank); + error ("stride must be a vector of.numel %d, the dataset rank", rank); return; } Matrix _block = block; if (nargin < 4) _block = Matrix (dim_vector(1, rank), 1); - if (_block.nelem () != rank) + if (_block.numel () != rank) { - error ("block must be a vector of length %d, the dataset rank", rank); + error ("block must be a vector of.numel %d, the dataset rank", rank); return; } @@ -1162,7 +1171,7 @@ H5File::write_dset_hyperslab (const char *dsetname, // A count value 0 is not allowed when writing data. - int end = start(i) + _stride(i)*(count(i)-1) + _block(i); // exclusive + unsigned int end = start(i) + _stride(i)*(count(i)-1) + _block(i); // exclusive if (h5_maxdims[rank-i-1] < end) { error ("In dimension %d, the dataset %s may have at max. only %d elements," @@ -1182,7 +1191,7 @@ H5File::write_dset_hyperslab (const char *dsetname, hsize_t *hcount = alloc_hsize (count, ALLOC_HSIZE_DEFAULT, true); hsize_t *hblock = alloc_hsize (_block, ALLOC_HSIZE_DEFAULT, true); // TODO check these (and hmem) for NULLs - + // make the current size of the dataset bigger H5Sclose (dspace_id); if (H5Dset_extent (dset_id, h5_dims) < 0) @@ -1209,7 +1218,7 @@ H5File::write_dset_hyperslab (const char *dsetname, error ("error when selecting the hyperslab of dataset %s to write to", dsetname); return; } - + hsize_t *hmem = alloc_hsize (data.dims (), ALLOC_HSIZE_DEFAULT, false); hid_t memspace_id = H5Screate_simple (rank, hmem, hmem); if (memspace_id < 0) @@ -1218,7 +1227,7 @@ H5File::write_dset_hyperslab (const char *dsetname, return; } free (hmem); - + herr_t status = H5Dwrite (dset_id, H5T_NATIVE_DOUBLE, memspace_id, dspace_id, H5P_DEFAULT, data.fortran_vec ()); @@ -1227,7 +1236,7 @@ H5File::write_dset_hyperslab (const char *dsetname, error ("error when writing the dataset %s", dsetname); return; } - + } @@ -1268,7 +1277,7 @@ H5File::read_att (const char *objname, const char *attname) { // Size of each string: size_t size = H5Tget_size (type); - // to read an array of strings (for future work): + // to read an array of strings (for future work): //totsize = size*sdim[0]*sdim[1]; // to read a single string: size_t totsize = size; @@ -1284,7 +1293,7 @@ H5File::read_att (const char *objname, const char *attname) else if (H5Tget_class (type)==H5T_INTEGER) { // Integer attributes are casted to floating point octave values - + double value[numVal]; if (H5Tget_size (type)==sizeof (int)) { @@ -1307,7 +1316,7 @@ H5File::read_att (const char *objname, const char *attname) for (size_t n=0;n