HDF5 can write a 3D matrix as a whole into .h5 file, or write only a defined portion(hyperslab) of matrix into the specific position of the whole Data Matrix created in h5 file.
However, when the Data Matrix was created in chunk mode(with deflated turn on) instead of H5P_DEFAULT, “write as a whole” still works, but “write a portion(hyperslab)” no longer works.
I have tried four mode of writing matrix into h5 file:
write as a whole into data created by H5P_DEFAULT in .h5 file: works OK.
write as a whole into data created by Chunk&Deflate in .h5 file: works OK.
write as a portion(hyperslab) into data created by H5P_DEFAULT in .h5 file: works OK.
write as a portion(hyperslab) into data created by Chunk&Deflate in .h5 file: not work.
Below is the matlab code:
turn_on_chunk_deflate = 0 % works
turn_on_chunk_deflate = 1 % does not work
%% ============write as portion============
turn_on_chunk_deflate = 0; % 0 is off and works, 1 is on and not work
% create a h5 file and data container 'DataSet''
outFile = 'D:\example.h5';
fid = H5F.create(outFile);
gid0 = H5G.open(fid,'/');
plist = 'H5P_DEFAULT';
H5G.create(gid0,'DataSet',plist,plist,plist);
% create data body "Data'
dims = [100,100,100];
h5_dims = fliplr(dims);% matlab and hdf5.jar's dim are inverted
h5_maxdims = h5_dims;% max dimension
space_id = H5S.create_simple(length(dims),h5_dims,h5_maxdims);
gid = H5G.open(fid,'/DataSet');
type_id = H5T.copy('H5T_NATIVE_UINT8');
% plist: this is the crucial part that cause the error
if ~turn_on_chunk_deflate
% without deflate : runs OK!
plist = 'H5P_DEFAULT';
else
% with deflate 0~9 & chunk[20,20,20]; Not work in final H5D.write!
plist = H5P.create('H5P_DATASET_CREATE');
h5_chunk = [20,20,20];
H5P.set_chunk(plist,h5_chunk);
deflate = 1; %turn on deflate
H5P.set_deflate(plist,deflate);
end
H5D.create(gid,'Data',type_id,space_id,plist);
% write data
start = [2,3,4]; % Note! location is 0-based, not 1-based!
h5_start = fliplr(start);
block = [11,23,25];% block size along xyz
h5_block = fliplr(block);
mem_space_id = H5S.create_simple(length(dims),h5_block,[]);
gid = H5D.open(fid,['/DataSet/Data']);
space_id = H5D.get_space(gid);
H5S.select_hyperslab(space_id,'H5S_SELECT_SET',h5_start,[],[],h5_block);
data = ones(block);
H5D.write(gid,'H5ML_DEFAULT',mem_space_id,space_id,plist,uint8(data));
H5D.close(gid);
H5F.close(fid);
The error:
Error using hdf5lib2
The HDF5 library encountered an unknown error.
Error in H5D.write (line 71)
H5ML.hdf5lib2('H5Dwrite', varargin{:});
Error in HDF5_test (line 56)
H5D.write(gid,'H5ML_DEFAULT',mem_space_id,space_id,plist,uint8(data));