Skip to content

Commit

Permalink
ncexists: 2-10x speedup, scalar variable
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jun 26, 2024
1 parent b7228ae commit 4540eb1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
16 changes: 12 additions & 4 deletions +stdlib/+hdf5nc/ncexists.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
function exists = ncexists(file, vars)
function exists = ncexists(file, variable)

arguments
file (1,1) string {mustBeFile}
vars string
variable string {mustBeScalarOrEmpty}
end

% NOT contains because we want exact string match
exists = ismember(vars, stdlib.hdf5nc.ncvariables(file));
exists = false;

try
ncinfo(file, variable);
exists = true;
catch e
if ~any(contains(e.identifier, ["MATLAB:imagesci:netcdf:badLocationString", "MATLAB:imagesci:netcdf:unknownLocation"]))
rethrow(e)
end
end

end
8 changes: 4 additions & 4 deletions +stdlib/h5exists.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function exists = h5exists(file, variable)
%% H5EXISTS(file, vars)
% check if object(s) exists in HDF5 file
%% H5EXISTS(file, variable)
% check if object exists in HDF5 file
%
%%% Inputs
% * file: data filename
Expand All @@ -10,8 +10,8 @@
% * exists: boolean

arguments
file (1,1) string {mustBeFile}
variable string {mustBeScalarOrEmpty}
file (1,1) string
variable string {mustBeScalarOrEmpty}
end

exists = stdlib.hdf5nc.h5exists(file, variable);
Expand Down
16 changes: 8 additions & 8 deletions +stdlib/ncexists.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
function exists = ncexists(file, vars)
%% ncexists(file, vars)
% check if variable(s) exists in NetCDF4 file
function exists = ncexists(file, variable)
%% ncexists(file, variable)
% check if variable exists in NetCDF4 file
%
%%% Inputs
% * file: data filename
% * varname: path(s) of variable in file
% * variable: path of variable in file
%
%%% Outputs
% * exists: boolean (scalar or vector)
% * exists: boolean

arguments
file (1,1) string {mustBeFile}
vars string
file (1,1) string
variable string {mustBeScalarOrEmpty}
end

exists = stdlib.hdf5nc.ncexists(file, vars);
exists = stdlib.hdf5nc.ncexists(file, variable);

end
9 changes: 2 additions & 7 deletions test/TestNetCDF.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,14 @@ function test_exists(tc)
import matlab.unittest.constraints.IsScalar
basic = tc.TestData.basic;

tc.verifyEmpty(stdlib.ncexists(basic, string.empty))

e = stdlib.ncexists(basic, "");

tc.verifyThat(e, IsScalar)
tc.verifyFalse(e)

e = stdlib.ncexists(basic, ["A1", "oops"]);
tc.verifyTrue(isvector(e))
tc.verifyEqual(e, [true, false])
tc.verifyTrue(stdlib.ncexists(basic, "A1"))
tc.verifyFalse(stdlib.ncexists(basic, "not-exist"))

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


Expand Down

0 comments on commit 4540eb1

Please sign in to comment.