Skip to content

Commit 440e6b0

Browse files
committed
ncexists: 2-10x speedup, scalar variable
1 parent 3d1f9a6 commit 440e6b0

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

+stdlib/+hdf5nc/ncexists.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
function exists = ncexists(file, vars)
1+
function exists = ncexists(file, variable)
22

33
arguments
44
file (1,1) string {mustBeFile}
5-
vars string
5+
variable string {mustBeScalarOrEmpty}
66
end
77

8-
% NOT contains because we want exact string match
9-
exists = ismember(vars, stdlib.hdf5nc.ncvariables(file));
8+
exists = false;
9+
10+
try
11+
ncinfo(file, variable);
12+
exists = true;
13+
catch e
14+
if ~any(contains(e.identifier, ["MATLAB:imagesci:netcdf:badLocationString", "MATLAB:imagesci:netcdf:unknownLocation"]))
15+
rethrow(e)
16+
end
17+
end
1018

1119
end

+stdlib/h5exists.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function exists = h5exists(file, variable)
2-
%% H5EXISTS(file, vars)
3-
% check if object(s) exists in HDF5 file
2+
%% H5EXISTS(file, variable)
3+
% check if object exists in HDF5 file
44
%
55
%%% Inputs
66
% * file: data filename
@@ -10,8 +10,8 @@
1010
% * exists: boolean
1111

1212
arguments
13-
file (1,1) string {mustBeFile}
14-
variable string {mustBeScalarOrEmpty}
13+
file (1,1) string
14+
variable string {mustBeScalarOrEmpty}
1515
end
1616

1717
exists = stdlib.hdf5nc.h5exists(file, variable);

+stdlib/ncexists.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
function exists = ncexists(file, vars)
2-
%% ncexists(file, vars)
3-
% check if variable(s) exists in NetCDF4 file
1+
function exists = ncexists(file, variable)
2+
%% ncexists(file, variable)
3+
% check if variable exists in NetCDF4 file
44
%
55
%%% Inputs
66
% * file: data filename
7-
% * varname: path(s) of variable in file
7+
% * variable: path of variable in file
88
%
99
%%% Outputs
10-
% * exists: boolean (scalar or vector)
10+
% * exists: boolean
1111

1212
arguments
13-
file (1,1) string {mustBeFile}
14-
vars string
13+
file (1,1) string
14+
variable string {mustBeScalarOrEmpty}
1515
end
1616

17-
exists = stdlib.hdf5nc.ncexists(file, vars);
17+
exists = stdlib.hdf5nc.ncexists(file, variable);
1818

1919
end

test/TestNetCDF.m

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,14 @@ function test_exists(tc)
9090
import matlab.unittest.constraints.IsScalar
9191
basic = tc.TestData.basic;
9292

93-
tc.verifyEmpty(stdlib.ncexists(basic, string.empty))
94-
9593
e = stdlib.ncexists(basic, "");
9694

9795
tc.verifyThat(e, IsScalar)
9896
tc.verifyFalse(e)
9997

100-
e = stdlib.ncexists(basic, ["A1", "oops"]);
101-
tc.verifyTrue(isvector(e))
102-
tc.verifyEqual(e, [true, false])
98+
tc.verifyTrue(stdlib.ncexists(basic, "A1"))
99+
tc.verifyFalse(stdlib.ncexists(basic, "not-exist"))
103100

104-
e = stdlib.ncexists(basic, {'A0', 'A1', 'A2', 'A3', 'A4'});
105-
tc.verifyTrue(all(e))
106101
end
107102

108103

0 commit comments

Comments
 (0)