% Simple HDF5 example using Matlab's HDF5 high level API
%
% Reads an HDF5 file, exported from SIMA
% Performs simple post processing
% Writes the result to HDF5
%

% Filename as exported from SIMA
fin = 'flexout.h5';

% Get info about hdf5-file
fileInfo = hdf5info(fin);

% View the file hierarchy
h5disp(fin);

% Find the name of the data set this using h5disp
dsetName = '/Flex/HDF5Example/Dynamic/RiserLine/segment_1/element_1/Axial force';

% Read the data set and the corresponding attributes
[dset, attr] = hdf5read(fin,dsetName,'ReadAttributes',true);

% Example of simple post processing
dsetOut = dset - mean(dset);

% Use whatever structure you like
dsetOutName = '/MatlabResults/Axial force';

% Name of new HDF5-file
fout = 'flexinMat.h5';

% Delete the file if it already exists
try 
    delete(fout);
catch err
    print err
end

% Create an empty data set in the HDF5-file
h5create(fout,dsetOutName,[length(dsetOut)])

% Write the processed data set to the HDF5-file
h5write(fout,dsetOutName,dsetOut);

% Write attributes to file
for i=1:length(attr)
	% HDF5 strings needs to be treated separately
    if strcmp(class(attr(i).Value),'hdf5.h5string')
        h5writeatt(fout,dsetOutName,attr(i).Shortname,attr(i).Value.Data);
    else
        h5writeatt(fout,dsetOutName,attr(i).Shortname,attr(i).Value);
    end
end


