H5CREATE_GROUP create HDF5 group

Contents

Inputs

Outputs

function fid = h5create_group(file, hpath)
arguments
  file
  hpath (1,1) string {mustBeNonzeroLengthText}
end

% polymorphic fid/filename
if isa(file, 'H5ML.id')
  fid = file;
else
  % avoid confusing creating file ./~/foo.h5
  file = stdlib.expanduser(file);
  dcpl = 'H5P_DEFAULT';
  if isfile(file)
    fid = H5F.open(file, 'H5F_ACC_RDWR', dcpl);
  else
    fid = H5F.create(file);
  end
end

% are there any groups
grps = split(hpath, "/");
if length(grps) < 3
  return
end

% recursively create groups as needed
plist = 'H5P_DEFAULT';
groot = H5G.open(fid, "/");

for i = 0:length(grps) - 3
  n = join(grps(1:i+2), "/");

  if ~H5L.exists(groot, n, plist)
    gid = H5G.create(fid, n, plist, plist, plist);
    H5G.close(gid)
  end
end % for

H5G.close(groot)

if nargout == 0
  H5F.close(fid);
  clear('fid')
end

end