Skip to content

Commit 54f9b77

Browse files
committed
NetCDF broader
1 parent a2c4e05 commit 54f9b77

File tree

4 files changed

+62
-52
lines changed

4 files changed

+62
-52
lines changed

+stdlib/ncsave.m

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@
1010
% * opts.dims: name and size of dimensions
1111
% * opts.type: class of variable e.g. int32, single
1212

13-
function ncsave(filename, varname, A, opts)
14-
arguments
15-
filename (1,1) string
16-
varname (1,1) string
17-
A {mustBeNonempty}
18-
opts.dims cell = {}
19-
opts.type (1,1) string = ""
20-
opts.compressLevel (1,1) double {mustBeInteger,mustBeNonnegative} = 0
21-
end
13+
function ncsave(filename, varname, A, varargin)
14+
% arguments
15+
% filename (1,1) string
16+
% varname (1,1) string
17+
% A {mustBeNonempty}
18+
% opts.dims cell = {}
19+
% opts.type (1,1) string = ""
20+
% opts.compressLevel (1,1) double {mustBeInteger,mustBeNonnegative} = 0
21+
% end
22+
23+
p = inputParser;
24+
addParameter(p, 'dims', {});
25+
addParameter(p, 'type', '');
26+
addParameter(p, 'compressLevel', 0, @(x) mustBeInteger(x) && mustBeNonnegative(x));
27+
parse(p, varargin{:});
28+
29+
opts = p.Results;
2230

2331
if isnumeric(A)
2432
mustBeReal(A)

+stdlib/private/ncsave_exist.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
% normally users will use ncsave() instead of this function
33

44
function ncsave_exist(filename, varname, A, sizeA)
5-
arguments
6-
filename
7-
varname
8-
A
9-
sizeA (1,:) {mustBePositive, mustBeInteger}
10-
end
5+
% arguments
6+
% filename
7+
% varname
8+
% A
9+
% sizeA (1,:) {mustBePositive, mustBeInteger}
10+
% end
1111

1212
diskshape = stdlib.ncsize(filename, varname);
1313

+stdlib/private/ncsave_new.m

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
% normally users will use ncsave() instead of this function
33

44
function ncsave_new(file, varname, A, sizeA, ncdims, compressLevel)
5-
arguments
6-
file
7-
varname
8-
A
9-
sizeA (1,:) double {mustBeInteger,mustBeNonnegative} = []
10-
ncdims (1,:) cell = {}
11-
compressLevel (1,1) double {mustBeInteger,mustBeNonnegative} = 0
5+
% arguments
6+
% file
7+
% varname
8+
% A
9+
% sizeA (1,:) double {mustBeInteger,mustBeNonnegative} = []
10+
% ncdims (1,:) cell = {}
11+
% compressLevel (1,1) double {mustBeInteger,mustBeNonnegative} = 0
12+
% end
13+
if nargin < 4
14+
sizeA = [];
15+
end
16+
if nargin < 5
17+
ncdims = {};
18+
end
19+
if nargin < 6
20+
compressLevel = 0;
1221
end
1322

1423
if isscalar(A)

test/TestNetCDF.m

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
utf2
1515
end
1616

17+
properties (TestParameter)
18+
type = {'single', 'double', 'int8', 'int16', 'int32', 'int64', ...
19+
'uint8', 'uint16', 'uint32', 'uint64'}
20+
end
21+
1722
methods (TestClassSetup)
1823
function setup_file(tc)
19-
tc.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture())
24+
tc.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture());
2025

2126
tc.A0 = 42.;
2227
tc.A1 = [42.; 43.];
@@ -28,7 +33,7 @@ function setup_file(tc)
2833
tc.utf1 = [tc.utf0; ""];
2934
tc.utf2 = [tc.utf0, ""; "📞", "👋"];
3035

31-
tc.file = fullfile(pwd(), class(tc) + ".nc");
36+
tc.file = fullfile(pwd(), [class(tc), '.nc']);
3237

3338
% create test data first, so that parallel tests works
3439
stdlib.ncsave(tc.file, 'A0', tc.A0)
@@ -38,21 +43,20 @@ function setup_file(tc)
3843
stdlib.ncsave(tc.file, 'A4', tc.A4, "dims", {'x4', size(tc.A4,1), 'y4', size(tc.A4,2), 'z4', size(tc.A4,3), 'w4', size(tc.A4,4)})
3944

4045
if ~stdlib.matlabOlderThan('R2021b')
41-
stdlib.ncsave(tc.file, "utf0", tc.utf0)
42-
stdlib.ncsave(tc.file, "utf1", tc.utf1, "dims", {'s1', size(tc.utf1, 1)})
43-
stdlib.ncsave(tc.file, "utf2", tc.utf2, "dims", {'s1', size(tc.utf2, 1), 't1', size(tc.utf2, 2)})
46+
stdlib.ncsave(tc.file, 'utf0', tc.utf0)
47+
stdlib.ncsave(tc.file, 'utf1', tc.utf1, "dims", {'s1', size(tc.utf1, 1)})
48+
stdlib.ncsave(tc.file, 'utf2', tc.utf2, "dims", {'s1', size(tc.utf2, 1), 't1', size(tc.utf2, 2)})
4449
end
4550

4651
stdlib.ncsave(tc.file, '/t/x', 12)
4752
stdlib.ncsave(tc.file, '/t/y', 13)
4853
stdlib.ncsave(tc.file, '/j/a/b', 6)
4954

50-
tc.assertThat(tc.file, matlab.unittest.constraints.IsFile)
5155
end
5256
end
5357

5458

55-
methods (Test, TestTags={'R2019b'})
59+
methods (Test, TestTags={'R2017b'})
5660

5761
function test_netcdf_version(tc)
5862
tc.verifyTrue(stdlib.version_atleast(stdlib.nc_get_version(), "4.6"), "version unexpected")
@@ -68,33 +72,31 @@ function test_get_variables(tc)
6872
tc.verifyEqual(sort(stdlib.ncvariables(tc.file)), k)
6973

7074
% 1-level group
71-
v = stdlib.ncvariables(tc.file, "/t");
75+
v = stdlib.ncvariables(tc.file, '/t');
7276
tc.verifyEqual(sort(v), ["x", "y"])
7377

7478
% traversal
75-
tc.verifyEmpty(stdlib.ncvariables(tc.file, "/j") )
79+
tc.verifyEmpty(stdlib.ncvariables(tc.file, '/j') )
7680

77-
tc.verifyEqual(stdlib.ncvariables(tc.file, "/j/a") , "b")
81+
tc.verifyEqual(stdlib.ncvariables(tc.file, '/j/a') , "b")
7882
end
7983

8084

8185
function test_exists(tc)
82-
import matlab.unittest.constraints.IsScalar
8386

84-
tc.verifyTrue(stdlib.ncexists(tc.file, "A1"))
85-
tc.verifyFalse(stdlib.ncexists(tc.file, "not-exist"))
87+
tc.verifyTrue(stdlib.ncexists(tc.file, 'A1'))
88+
tc.verifyFalse(stdlib.ncexists(tc.file, 'not-exist'))
8689

8790
end
8891

8992

9093
function test_size(tc)
91-
import matlab.unittest.constraints.IsScalar
9294

9395
s = stdlib.ncsize(tc.file, 'A0');
9496
tc.verifyEmpty(s)
9597

9698
s = stdlib.ncsize(tc.file, 'A1');
97-
tc.verifyThat(s, IsScalar)
99+
tc.verifyThat(s, matlab.unittest.constraints.IsScalar)
98100
tc.verifyEqual(s, 2)
99101

100102
s = stdlib.ncsize(tc.file, 'A2');
@@ -113,11 +115,9 @@ function test_size(tc)
113115

114116

115117
function test_read(tc)
116-
import matlab.unittest.constraints.IsScalar
117-
118118

119119
s = ncread(tc.file, 'A0');
120-
tc.verifyThat(s, IsScalar)
120+
tc.verifyThat(s, matlab.unittest.constraints.IsScalar)
121121
tc.verifyEqual(s, 42)
122122

123123
s = ncread(tc.file, 'A1');
@@ -138,30 +138,23 @@ function test_read(tc)
138138
end
139139

140140

141-
function test_coerce(tc)
142-
143-
for type = ["single", "double", ...
144-
"int8", "int16", "int32", "int64", ...
145-
"uint8", "uint16", "uint32", "uint64"]
146-
147-
stdlib.ncsave(tc.file, type, 0, "type", type)
148-
149-
tc.verifyClass(ncread(tc.file, type), type)
150-
end
141+
function test_coerce(tc, type)
142+
stdlib.ncsave(tc.file, type, 0, "type", type)
151143

144+
tc.verifyClass(ncread(tc.file, type), type)
152145
end
153146

154147

155148
function test_rewrite(tc)
156149
tc.A2 = 3*magic(4);
157-
stdlib.ncsave(tc.file, "A2", tc.A2, "dims", {'x2', size(tc.A2,1), 'y2', size(tc.A2,2)})
150+
stdlib.ncsave(tc.file, 'A2', tc.A2, "dims", {'x2', size(tc.A2,1), 'y2', size(tc.A2,2)})
158151

159152
tc.verifyEqual(ncread(tc.file, 'A2'), 3*magic(4))
160153
end
161154

162155

163156
function test_real_only(tc)
164-
tc.verifyError(@() stdlib.ncsave(tc.file, "bad_imag", 1j), 'MATLAB:validators:mustBeReal')
157+
tc.verifyError(@() stdlib.ncsave(tc.file, 'bad_imag', 1j), 'MATLAB:validators:mustBeReal')
165158
end
166159

167160
end

0 commit comments

Comments
 (0)