Mercurial > forge
annotate main/netcdf/inst/private/test_netcdf_low_level_interface.m @ 12711:a4de85b2e6a0 octave-forge
fix bug 47014 and make test code work in matlab
author | abarth93 |
---|---|
date | Fri, 19 Feb 2016 13:45:14 +0000 |
parents | bd0a4b380d35 |
children |
rev | line source |
---|---|
11992 | 1 %% Copyright (C) 2013 Alexander Barth |
2 %% | |
3 %% This program is free software; you can redistribute it and/or modify | |
4 %% it under the terms of the GNU General Public License as published by | |
5 %% the Free Software Foundation; either version 2 of the License, or | |
6 %% (at your option) any later version. | |
7 %% | |
8 %% This program is distributed in the hope that it will be useful, | |
9 %% but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 %% GNU General Public License for more details. | |
12 %% | |
13 %% You should have received a copy of the GNU General Public License | |
14 %% along with this program; If not, see <http://www.gnu.org/licenses/>. | |
15 | |
11988 | 16 function test_netcdf_low_level_interface |
17 | |
18 import_netcdf | |
19 | |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
20 % 2 dimensions |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
21 |
11988 | 22 fname = [tempname '-octave-netcdf.nc']; |
23 | |
24 ncid = netcdf.create(fname,'NC_CLOBBER'); | |
25 assert(strcmp(netcdf.inqFormat(ncid),'FORMAT_CLASSIC')); | |
26 | |
27 n = 10; | |
28 m = 5; | |
29 | |
30 dimid_lon = netcdf.defDim(ncid,'lon',m); | |
31 dimid = netcdf.defDim(ncid,'time',n); | |
32 | |
33 varidd = netcdf.defVar(ncid,'double_var','double',[dimid_lon,dimid]); | |
34 | |
35 varid = netcdf.defVar(ncid,'byte_var','byte',[dimid]); | |
36 | |
37 varidf = netcdf.defVar(ncid,'float_var','float',[dimid]); | |
38 | |
39 varidi = netcdf.defVar(ncid,'int_var','int',[dimid]); | |
40 | |
41 varids = netcdf.defVar(ncid,'short_var','short',[dimid]); | |
42 assert(varidd == netcdf.inqVarID(ncid,'double_var')) | |
43 | |
44 [numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(ncid); | |
45 assert(numvars == 5) | |
46 | |
47 [varname,xtype,dimids,natts] = netcdf.inqVar(ncid,varidd); | |
48 assert(strcmp(varname,'double_var')); | |
49 | |
50 [dimname,len] = netcdf.inqDim(ncid,dimid); | |
51 assert(len == n); | |
52 assert(strcmp(dimname,'time')); | |
53 | |
54 | |
55 types = {'double','float','byte','short','int'}; | |
56 | |
57 | |
58 for i=1:length(types) | |
59 vid{i} = netcdf.defVar(ncid,[types{i} '_variable'],types{i},[dimid_lon,dimid]); | |
60 end | |
61 | |
62 | |
63 netcdf.endDef(ncid) | |
64 | |
65 | |
66 z = randn(m,n); | |
67 netcdf.putVar(ncid,varidd,z); | |
68 | |
69 varf = randn(n,1); | |
70 netcdf.putVar(ncid,varidf,varf); | |
71 | |
72 vari = floor(randn(n,1)); | |
73 netcdf.putVar(ncid,varidi,vari); | |
74 | |
75 netcdf.putVar(ncid,varids,[1:n]) | |
76 | |
77 z2 = netcdf.getVar(ncid,varidd); | |
78 assert(all(all(abs(z2 - z) < 1e-5))) | |
79 | |
80 z2 = netcdf.getVar(ncid,varidd,[0 0]); | |
81 assert(z2 == z(1,1)) | |
82 | |
83 z2 = netcdf.getVar(ncid,varidd,[2 2],[3 5]); | |
84 assert(isequal(z2,z(3:5,3:7))) | |
85 | |
86 z2 = netcdf.getVar(ncid,varidd,[2 2],[3 4],[1 2]); | |
87 assert(isequal(z2,z(3:5,3:2:9))) | |
88 | |
89 | |
90 netcdf.putVar(ncid,varidd,[0 0],123.); | |
91 z(1,1) = 123; | |
92 z2 = netcdf.getVar(ncid,varidd); | |
93 assert(isequal(z,z2)) | |
94 | |
95 netcdf.putVar(ncid,varidd,[2 2],[3 3],ones(3,3)); | |
96 z(3:5,3:5) = 1; | |
97 z2 = netcdf.getVar(ncid,varidd); | |
98 assert(isequal(z,z2)) | |
99 | |
100 | |
101 netcdf.putVar(ncid,varidd,[0 0],[3 5],[2 2],zeros(3,5)); | |
102 z(1:2:5,1:2:9) = 0; | |
103 z2 = netcdf.getVar(ncid,varidd); | |
104 assert(isequal(z,z2)) | |
105 | |
106 | |
107 z2 = netcdf.getVar(ncid,varidf); | |
108 assert(all(z2 - varf < 1e-5)) | |
109 | |
110 | |
111 vari2 = netcdf.getVar(ncid,varidi); | |
112 assert(all(vari2 == vari)) | |
113 | |
114 | |
115 netcdf.close(ncid); | |
116 delete(fname); | |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
117 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
118 % test with different dimensions |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
119 |
12030 | 120 for i = 1:5 |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
121 nc_test_ndims(i); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
122 end |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
123 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
124 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
125 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
126 function nc_test_ndims(ndims) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
127 import_netcdf |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
128 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
129 fname = [tempname '-octave-netcdf.nc']; |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
130 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
131 ncid = netcdf.create(fname,'NC_CLOBBER'); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
132 |
12711 | 133 sz = ones(1,ndims); |
134 dimids = ones(1,ndims); | |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
135 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
136 for i = 1:ndims |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
137 sz(i) = 10+i; |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
138 dimids(i) = netcdf.defDim(ncid,sprintf('dim%g',i),sz(i)); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
139 end |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
140 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
141 varid = netcdf.defVar(ncid,'double_var','double',dimids); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
142 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
143 [varname,xtype,dimids,natts] = netcdf.inqVar(ncid,varid); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
144 assert(strcmp(varname,'double_var')); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
145 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
146 for i = 1:ndims |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
147 [dimname,len] = netcdf.inqDim(ncid,dimids(i)); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
148 assert(len == sz(i)); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
149 assert(strcmp(dimname,sprintf('dim%g',i))); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
150 end |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
151 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
152 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
153 netcdf.endDef(ncid) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
154 |
12030 | 155 if ndims == 1 |
156 z = randn(sz,1); | |
157 else | |
158 z = randn(sz); | |
159 end | |
160 | |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
161 netcdf.putVar(ncid,varid,z); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
162 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
163 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
164 z2 = netcdf.getVar(ncid,varid); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
165 assert(isequal(z,z2)) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
166 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
167 z2 = netcdf.getVar(ncid,varid,zeros(ndims,1)); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
168 assert(z2 == z(1)) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
169 |
12711 | 170 start = 2 * ones(1,ndims); |
171 count = 5 * ones(1,ndims); | |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
172 z2 = netcdf.getVar(ncid,varid,start,count); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
173 idx = scs(start,count); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
174 assert(isequal(z2,z(idx{:}))) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
175 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
176 |
12711 | 177 start = 2 * ones(1,ndims); |
178 count = 5 * ones(1,ndims); | |
179 stride = 2 * ones(1,ndims); | |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
180 z2 = netcdf.getVar(ncid,varid,start,count,stride); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
181 idx = scs(start,count,stride); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
182 assert(isequal(z2,z(idx{:}))) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
183 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
184 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
185 % put with start |
12711 | 186 start = zeros(1,ndims); |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
187 netcdf.putVar(ncid,varid,start,123.); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
188 z(1) = 123; |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
189 z2 = netcdf.getVar(ncid,varid); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
190 assert(isequal(z,z2)) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
191 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
192 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
193 % put with start and count |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
194 |
12711 | 195 start = 2 * ones(1,ndims); |
196 count = 5 * ones(1,ndims); | |
197 if ndims == 1 | |
198 data = ones(count,1); | |
199 else | |
200 data = ones(count); | |
201 end | |
202 netcdf.putVar(ncid,varid,start,count,data); | |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
203 idx = scs(start,count); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
204 z(idx{:}) = 1; |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
205 z2 = netcdf.getVar(ncid,varid); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
206 assert(isequal(z,z2)) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
207 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
208 % put with start, count and stride |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
209 |
12711 | 210 start = 2 * ones(1,ndims); |
211 count = 5 * ones(1,ndims); | |
212 stride = 2 * ones(1,ndims); | |
213 if ndims == 1 | |
214 data = zeros(count,1); | |
215 else | |
216 data = zeros(count); | |
217 end | |
218 netcdf.putVar(ncid,varid,start,count,stride,data); | |
12029
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
219 idx = scs(start,count,stride); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
220 z(idx{:}) = 0; |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
221 z2 = netcdf.getVar(ncid,varid); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
222 assert(isequal(z,z2)) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
223 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
224 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
225 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
226 netcdf.close(ncid); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
227 delete(fname); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
228 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
229 function idx = scs(start,count,stride) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
230 idx = cell(length(start),1); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
231 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
232 if nargin == 2 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
233 stride = ones(length(start),1); |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
234 end |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
235 |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
236 for i = 1:length(start) |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
237 idx{i} = start(i) + 1 + stride(i) * [0:count(i)-1]; |
21b530fbeee2
make size_t and ptrdiff_t convertion work with 32-bit compilers
abarth93
parents:
11992
diff
changeset
|
238 end |