Skip to content

Commit a9a5641

Browse files
committed
add HDF4 h4{size,exists,variables}
1 parent 7355fa8 commit a9a5641

File tree

10 files changed

+146
-14
lines changed

10 files changed

+146
-14
lines changed

+stdlib/+hdf5nc/h4exists.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function exists = h4exists(file, variable)
2+
3+
arguments
4+
file (1,1) string {mustBeFile}
5+
variable (1,1) string {mustBeNonzeroLengthText}
6+
end
7+
8+
exists = false;
9+
10+
try
11+
sds = hdfinfo(file).SDS;
12+
i = string(sds.Name) == variable;
13+
exists = any(i);
14+
catch e
15+
if e.identifier ~= "MATLAB:imagesci:h5info:unableToFind"
16+
rethrow(e)
17+
end
18+
end

+stdlib/+hdf5nc/h4size.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
sds = hdfinfo(file).SDS;
99

10-
i = string(hdfinfo(file).SDS.Name) == variable;
10+
i = string(sds.Name) == variable;
1111
if ~all(i)
1212
error(variable + " is not an SDS in " + file)
1313
end

+stdlib/+hdf5nc/h4variables.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function names = h4variables(file)
2+
3+
arguments
4+
file (1,1) string {mustBeFile}
5+
end
6+
7+
finf = hdfinfo(file);
8+
9+
ds = finf.SDS;
10+
11+
names = string({ds.Name});
12+
13+
end

+stdlib/+hdf5nc/h5variables.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
group string {mustBeScalarOrEmpty} = string.empty
66
end
77

8-
names = string.empty;
9-
108
if isempty(group) || strlength(group) == 0
119
finf = h5info(file);
1210
else
@@ -16,9 +14,9 @@
1614
ds = finf.Datasets;
1715

1816
if isempty(ds)
19-
return
17+
names = string.empty;
18+
else
19+
names = string({ds.Name});
2020
end
2121

22-
names = string({ds.Name});
23-
24-
end % function
22+
end

+stdlib/+hdf5nc/ncvariables.m

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
group string {mustBeScalarOrEmpty} = string.empty
66
end
77

8-
names = string.empty;
9-
108
if isempty(group) || strlength(group) == 0
119
finf = ncinfo(file);
1210
else
1311
finf = ncinfo(file, group);
1412
end
1513

1614
ds = finf.Variables(:);
15+
1716
if isempty(ds)
18-
return
17+
names = string.empty;
18+
else
19+
names = string({ds.Name});
1920
end
2021

21-
names = string({ds.Name});
22-
23-
end % function
22+
end

+stdlib/h4exists.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function exists = h4exists(file, variable)
2+
%% H5EXISTS(file, variable)
3+
% check if object exists in HDF4 file
4+
%
5+
%%% Inputs
6+
% * file: data filename
7+
% * variable: path of variable in file
8+
%
9+
%%% Outputs
10+
% * exists: boolean
11+
12+
arguments
13+
file (1,1) string
14+
variable (1,1) string
15+
end
16+
17+
exists = stdlib.hdf5nc.h4exists(file, variable);
18+
19+
end

+stdlib/h4size.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function fsize = h4size(file, variable)
2+
%% h5size(file, variable)
3+
% get size (shape) of a data file variable
4+
%
5+
%%% Inputs
6+
% filename: data filename
7+
% variable: name of variable inside file
8+
%
9+
%%% Outputs
10+
% fsize: vector of variable size per dimension. Empty if scalar variable.
11+
12+
arguments
13+
file (1,1) string
14+
variable (1,1) string
15+
end
16+
17+
fsize = stdlib.hdf5nc.h4size(file, variable);
18+
19+
end

+stdlib/h4variables.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function names = h4variables(file)
2+
%% h5variables(file, group)
3+
% get dataset names in a file under group
4+
% default is datasets under "/", optionally under "/group"
5+
%
6+
%%% Inputs
7+
% * file: filename
8+
% * group: group name (optional)
9+
%
10+
%%% Outputs
11+
% * names: variable names
12+
13+
arguments
14+
file (1,1) string
15+
end
16+
17+
names = stdlib.hdf5nc.h4variables(file);
18+
19+
end

+stdlib/h5variables.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
% * names: variable names
1212

1313
arguments
14-
file (1,1) string {mustBeFile}
14+
file (1,1) string
1515
group string {mustBeScalarOrEmpty} = string.empty
1616
end
1717

test/TestHDF4.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
classdef TestHDF4 < matlab.unittest.TestCase
2+
3+
properties
4+
TestData
5+
end
6+
7+
methods (TestClassSetup)
8+
function setup_path(tc)
9+
tc.TestData.basic = fullfile(matlabroot, "toolbox/matlab/demos/example.hdf");
10+
end
11+
end
12+
13+
methods (Test)
14+
15+
function test_exists(tc)
16+
import matlab.unittest.constraints.IsScalar
17+
basic = tc.TestData.basic;
18+
19+
e = stdlib.h4exists(basic, "Example SDS");
20+
21+
tc.verifyThat(e, IsScalar)
22+
tc.verifyTrue(e);
23+
24+
tc.verifyFalse(stdlib.h4exists(basic, "/j"))
25+
26+
end
27+
28+
29+
function test_size(tc)
30+
basic = tc.TestData.basic;
31+
32+
s = stdlib.h4size(basic, "Example SDS");
33+
tc.verifyEqual(s, [16, 5])
34+
35+
end
36+
37+
38+
function test_vars(tc)
39+
basic = tc.TestData.basic;
40+
41+
v = stdlib.h4variables(basic);
42+
tc.verifyTrue(any(contains(v, "Example SDS")))
43+
end
44+
45+
46+
end
47+
end

0 commit comments

Comments
 (0)