Skip to content

Commit 4597868

Browse files
committed
add h5create_group()
1 parent d2e1082 commit 4597868

File tree

11 files changed

+72
-65
lines changed

11 files changed

+72
-65
lines changed

+stdlib/+hdf5nc/h5create_group.m

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function fid = h5create_group(file, hpath)
2+
%% create HDF5 group
3+
%
4+
% file: HDF5 file name or handle
5+
% hpath: HDF5 group/dataset -- ensure final character is "/" if hpath is only a group
6+
7+
arguments
8+
file
9+
hpath (1,1) string {mustBeNonzeroLengthText}
10+
end
11+
12+
import stdlib.fileio.expanduser
13+
14+
%% polymorphic fid/filename
15+
if isa(file, 'H5ML.id')
16+
fid = file;
17+
else
18+
file = expanduser(file);
19+
if isfile(file)
20+
dcpl = 'H5P_DEFAULT';
21+
fid = H5F.open(file, 'H5F_ACC_RDWR', dcpl);
22+
else
23+
fid = H5F.create(file);
24+
end
25+
end
26+
27+
%% are there any groups
28+
grps = split(hpath, "/");
29+
if length(grps) < 3
30+
return
31+
end
32+
33+
%% recursively create groups as needed
34+
plist = 'H5P_DEFAULT';
35+
groot = H5G.open(fid, "/");
36+
37+
for i = 0:length(grps) - 3
38+
n = join(grps(1:i+2), "/");
39+
40+
if ~H5L.exists(groot, n, plist)
41+
gid = H5G.create(fid, n, plist, plist, plist);
42+
H5G.close(gid)
43+
end
44+
end % for
45+
46+
H5G.close(groot)
47+
48+
if nargout == 0
49+
H5F.close(fid);
50+
clear('fid')
51+
end
52+
53+
end

+stdlib/+hdf5nc/h5exists.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
function exists = h5exists(file, varnames)
2-
% check if variable(s) exists in HDF5 file
2+
% check if object(s) exists in HDF5 file
33
%
44
% parameters
55
% ----------
66
% file: HDF5 filename
7-
% varname: name of variable inside HDF5 file
7+
% varname: name of variable in file
88
%
99
% returns
1010
% -------

+stdlib/+hdf5nc/h5ndims.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
file = expanduser(file);
1515

16-
assert(isfile(file), "%s not found", file)
17-
1816
dsi = h5info(file, variable).Dataspace;
1917
if dsi.Type == "scalar"
2018
frank = 0;

+stdlib/+hdf5nc/h5size.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
file = expanduser(file);
1616

17-
assert(isfile(file), "%s not found", file)
18-
1917
dsi = h5info(file, variable).Dataspace;
2018
if dsi.Type == "scalar"
2119
fsize = [];

+stdlib/+hdf5nc/h5variables.m

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
function [names, groups] = h5variables(file, group)
2-
% get dataset names in a file
3-
% Optionally, get first level of groups in a file.
1+
function names = h5variables(file, group)
2+
%% get dataset names in a file under group
3+
% default is datasets under "/", optionally under "/group"
44
%
55
% parameters
66
% ----------
@@ -10,7 +10,6 @@
1010
% returns
1111
% -------
1212
% names: variable names
13-
% groups: file groups
1413

1514
arguments
1615
file (1,1) string {mustBeNonzeroLengthText}
@@ -22,9 +21,6 @@
2221
file = expanduser(file);
2322

2423
names = string.empty;
25-
groups = string.empty;
26-
27-
assert(isfile(file), "%s not found", file)
2824

2925
if isempty(group)
3026
finf = h5info(file);
@@ -33,14 +29,11 @@
3329
end
3430

3531
ds = finf.Datasets;
36-
gs = finf.Groups;
3732

38-
if ~isempty(ds)
39-
names = string({ds.Name});
33+
if isempty(ds)
34+
return
4035
end
4136

42-
if nargout > 1 && ~isempty(gs)
43-
groups = string({gs.Name});
44-
end
37+
names = string({ds.Name});
4538

4639
end % function

+stdlib/+hdf5nc/ncndims.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
file = expanduser(file);
1515

16-
assert(isfile(file), "%s not found", file)
17-
1816
dsi = ncinfo(file, variable);
1917
if isempty(dsi.Dimensions)
2018
frank = 0;

+stdlib/+hdf5nc/ncsize.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
file = expanduser(file);
1616

17-
assert(isfile(file), "%s not found", file)
18-
1917
dsi = ncinfo(file, variable);
2018
if isempty(dsi.Dimensions)
2119
fsize = [];

+stdlib/+hdf5nc/ncvariables.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
file = expanduser(file);
1919

20-
assert(isfile(file), "%s not found", file)
21-
2220
finf = ncinfo(file);
2321
ds = finf.Variables(:);
2422
names = string({ds(:).Name});

+stdlib/+hdf5nc/private/create_hdf5_group.m

Lines changed: 0 additions & 21 deletions
This file was deleted.

+stdlib/+hdf5nc/private/h5_write_scalar.m

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
function h5_write_scalar(filename, varname, A)
1+
function h5_write_scalar(file, hpath, A)
22
%% write HDF5 scalar as a scalar
33
% h5create doesn't support scalars
44
arguments
5-
filename (1,1) string {mustBeNonzeroLengthText}
6-
varname (1,1) string {mustBeNonzeroLengthText}
5+
file (1,1)
6+
hpath (1,1) string {mustBeNonzeroLengthText}
77
A (1,1)
88
end
99

10-
dcpl = 'H5P_DEFAULT';
10+
import stdlib.hdf5nc.h5create_group
1111

12-
if isfile(filename)
13-
fid = H5F.open(filename, 'H5F_ACC_RDWR', dcpl);
14-
else
15-
fid = H5F.create(filename);
16-
end
12+
dcpl = 'H5P_DEFAULT';
1713

18-
create_hdf5_group(fid, varname);
14+
fid = h5create_group(file, hpath);
1915

2016
space_id = H5S.create('H5S_SCALAR');
2117
if isstring(A)
@@ -30,7 +26,7 @@ function h5_write_scalar(filename, varname, A)
3026
type_id = H5T.copy(class2h5t(A));
3127
end
3228

33-
dset_id = H5D.create(fid, varname, type_id, space_id, dcpl);
29+
dset_id = H5D.create(fid, hpath, type_id, space_id, dcpl);
3430

3531
H5D.write(dset_id,'H5ML_DEFAULT','H5S_ALL','H5S_ALL', dcpl, A);
3632

0 commit comments

Comments
 (0)