diff --git a/README b/README index 4ab73f5..0675911 100644 --- a/README +++ b/README @@ -39,7 +39,7 @@ integer and string. To install, just use make - + This will produce a package file named "hdf5oct-*.tar.gz" . Then you may either install the package with @@ -48,7 +48,7 @@ you may either install the package with or you may start GNU Octave and install the package manually (using the correct file name) with the command - pkg install hdf5oct-0.2.0.tar.gz + pkg install hdf5oct-0.4.0.tar.gz This will put the *.oct files somewhere where Octave will find them. You can try running @@ -73,4 +73,4 @@ To uninstall the package you may want to use - read string-array typed attributes - write more comprehensive tests instead of a few random choices. Also - test for error conditions. \ No newline at end of file + test for error conditions. diff --git a/h5read.cc b/h5read.cc index f346fd5..bd7ccce 100644 --- a/h5read.cc +++ b/h5read.cc @@ -38,9 +38,12 @@ #include #include #include -#include "gripes.h" +#include "errwarn.h" #include "file-stat.h" +#define HAVE_HDF5 1 +#define HAVE_HDF5_18 1 + using namespace std; #if ((H5_VERS_MAJOR > 1) || (H5_VERS_MINOR >= 8)) @@ -76,7 +79,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 +88,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.length (); i++) { if (mat(i) == octave_Inf) mat(i) = 0; @@ -100,7 +103,7 @@ check_vec (const octave_value& val, Matrix& mat/*out*/, { error ("%s can only contain positive integers", name); return 0; - } + } return 1; } @@ -148,31 +151,31 @@ the appropriate size for the given HDF5 type.\n\ @end deftypefn") { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) - gripe_disabled_feature ("h5read", "HDF5 IO"); - return octave_value_list (); + warn_disabled_feature ("h5read", "HDF5 IO"); + 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 +186,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 +203,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); @@ -222,8 +225,8 @@ 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 (); + warn_disabled_feature ("h5readatt", "HDF5 IO"); + return octave_value (); #else int nargin = args.length (); if (nargin != 3) @@ -241,13 +244,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; @@ -293,43 +296,43 @@ the appropriate size for the given Octave type.\n\ @end deftypefn") { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) - gripe_disabled_feature ("h5write", "HDF5 IO"); - return octave_value_list (); + warn_disabled_feature ("h5write", "HDF5 IO"); + 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 +353,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 } @@ -373,38 +376,38 @@ the object named @var{objectname} in the HDF5 file specified by @var{filename}.\ @end deftypefn") { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) - gripe_disabled_feature ("h5writeatt", "HDF5 IO"); - return octave_value_list (); + warn_disabled_feature ("h5writeatt", "HDF5 IO"); + 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 } @@ -442,35 +445,35 @@ setting is not @sc{matlab} compatible.\n\ @end deftypefn") { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) - gripe_disabled_feature("h5create", "HDF5 IO"); - return octave_value_list (); + warn_disabled_feature("h5create", "HDF5 IO"); + 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 +487,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 +497,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 } @@ -541,36 +544,36 @@ Note that this function is not @sc{matlab} compliant.\n\ @end deftypefn") { #if ! (defined (HAVE_HDF5) && defined (HAVE_HDF5_18)) - gripe_disabled_feature("h5delete", "HDF5 IO"); - return octave_value_list (); + warn_disabled_feature("h5delete", "HDF5 IO"); + 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 +582,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 +597,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 +628,7 @@ H5File::~H5File () if (H5Iis_valid (dset_id)) H5Dclose (dset_id); - + if (H5Iis_valid (att_id)) H5Aclose (att_id); @@ -634,7 +637,7 @@ H5File::~H5File () if (H5Iis_valid (type_id)) H5Tclose (type_id); - + if (H5Iis_valid (mem_type_id)) H5Tclose (mem_type_id); @@ -711,7 +714,7 @@ H5File::open_dset (const char *dsetname) error ("Error determining current dimensions and maximum size of dataset %s", dsetname); return -1; } - + return 0; } @@ -719,7 +722,7 @@ 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 @@ -732,7 +735,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,43 +749,43 @@ 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.length () != rank) { error ("start must be a vector of length %d, the dataset rank", rank); - return octave_value_list (); + return octave_value (); } - if (count.nelem () != rank) + if (count.length () != rank) { error ("count must be a vector of length %d, the dataset rank", rank); - return octave_value_list (); + return octave_value (); } - + Matrix _stride = stride; if (nargin < 3) _stride = Matrix (dim_vector(1, rank), 1); - if (_stride.nelem () != rank) + if (_stride.length () != rank) { error ("stride must be a vector of length %d, the dataset rank", rank); - return octave_value_list (); + return octave_value (); } Matrix _block = block; if (nargin < 4) _block = Matrix (dim_vector(1, rank), 1); - if (_block.nelem () != rank) + if (_block.length () != rank) { error ("block must be a vector of length %d, the dataset rank", rank); - return octave_value_list (); + return octave_value (); } - + // .resize(1) still leaves mat_dims with a length of 2 for some reason, so // we need at least 2 filled mat_dims.resize (max (rank, 2)); @@ -795,7 +798,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 +807,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 +832,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; @@ -857,20 +860,20 @@ 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; \ - size_t rdcc_nelem = -1; \ + int mdc_length = -1; \ + size_t rdcc_length = -1; \ size_t rdcc_nbytes = -1; \ double rdcc_w0 = -1; \ - if (H5Pget_cache (H5Fget_access_plist (file), &mdc_nelem, \ - &rdcc_nelem, &rdcc_nbytes, &rdcc_w0 ) < 0) \ + if (H5Pget_cache (H5Fget_access_plist (file), &mdc_length, \ + &rdcc_length, &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;*/ \ + /*cout << "cache params:" << rdcc_length << "," << rdcc_nbytes << endl;*/ \ herr_t read_result = H5Dread (dset_id, \ type, \ H5S_ALL, dspace_id, \ @@ -878,11 +881,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 +962,7 @@ H5File::read_dset () HDF5_READ_DATA (H5T_NATIVE_DOUBLE); } H5Tclose (complex_type_id); - + return retval; } @@ -997,12 +1000,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 +1021,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 +1082,7 @@ H5File::write_dset (const char *dsetname, type_id = H5Tcopy (H5T_NATIVE_INT); OPEN_AND_WRITE; } - + } else if (ov_data.is_single_type ()) { @@ -1115,19 +1118,19 @@ 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.length () != rank) { error ("start must be a vector of length %d, the dataset rank", rank); return; } - if (count.nelem () != rank) + if (count.length () != rank) { error ("count must be a vector of length %d, the dataset rank", rank); return; @@ -1135,7 +1138,7 @@ H5File::write_dset_hyperslab (const char *dsetname, Matrix _stride = stride; if (nargin < 3) _stride = Matrix (dim_vector(1, rank), 1); - if (_stride.nelem () != rank) + if (_stride.length () != rank) { error ("stride must be a vector of length %d, the dataset rank", rank); return; @@ -1143,7 +1146,7 @@ H5File::write_dset_hyperslab (const char *dsetname, Matrix _block = block; if (nargin < 4) _block = Matrix (dim_vector(1, rank), 1); - if (_block.nelem () != rank) + if (_block.length () != rank) { error ("block must be a vector of length %d, the dataset rank", rank); return; @@ -1162,7 +1165,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 +1185,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 +1212,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 +1221,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 +1230,7 @@ H5File::write_dset_hyperslab (const char *dsetname, error ("error when writing the dataset %s", dsetname); return; } - + } @@ -1268,7 +1271,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 +1287,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 +1310,7 @@ H5File::read_att (const char *objname, const char *attname) for (size_t n=0;n @@ -8,4 +8,4 @@ Title: a HDF5 wrapper for GNU Octave Url: https://github.com/stegro/hdf5oct Description: Currently this package provides a rather small number of function, in order to access datasets and attributes of HDF5 files. -License: LGPLv3+ \ No newline at end of file +License: LGPLv3+