1 ## Copyright (C) 2005 Alexander Barth <barth.alexander@gmail.com>
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.
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.
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/>.
17 ## @deftypefn {Loadable Function} nctest
18 ## Tests the octcdf toolbox. Tests results are written to nctest.log. All tests
22 ## Author: Alexander Barth <barth.alexander@gmail.com>
26 function [passes,tests] = nctest
27 disp('writing test output to nctest.log');
28 test('nctest','normal','nctest.log');
33 %! fname = [tmpnam '-octcdf.nc'];
34 %! nc = netcdf(fname,'c');
36 %!# Test reading/writing dimension
41 %! assert(length(nc('time')),5);
42 %! assert(length(nc('space')),3);
44 %!# Test reading/writing global attributes
46 %! nc.byte_att = ncbyte(123);
47 %! nc.short_att = ncshort(123);
48 %! nc.int_att = ncint(123);
49 %! nc.long_att = nclong(123);
50 %! nc.float_att = ncfloat(123);
51 %! nc.double_att = ncdouble(123.4);
52 %! nc.string_att = "test string";
53 %! assert(nc.byte_att,123)
54 %! assert(nc.short_att,123)
55 %! assert(nc.int_att,123)
56 %! assert(nc.long_att,123)
57 %! assert(nc.float_att,single(123))
58 %! assert(nc.double_att,123.4)
59 %! assert(nc.string_att,"test string")
61 %!# Test reading/writing varaible of type byte
63 %! byte_var = reshape(1:15,5,3);
64 %! nc{'byte_var'} = ncbyte('time','space');
65 %! nc{'byte_var'}(:) = byte_var;
66 %! assert(nc{'byte_var'}(:),byte_var);
68 %!# Test reading/writing varaible of type short
70 %! short_var = reshape(1:15,5,3);
71 %! nc{'short_var'} = ncshort('time','space');
72 %! nc{'short_var'}(:) = short_var;
73 %! assert(nc{'short_var'}(:),short_var);
75 %!# Test reading/writing varaible of type int
77 %! int_var = reshape(1:15,5,3);
78 %! nc{'int_var'} = ncint('time','space');
79 %! nc{'int_var'}(:) = int_var;
80 %! assert(nc{'int_var'}(:),int_var);
82 %!# Test reading/writing varaible of type long
84 %! long_var = reshape(1:15,5,3);
85 %! nc{'long_var'} = nclong('time','space');
86 %! nc{'long_var'}(:) = long_var;
87 %! assert(nc{'long_var'}(:),long_var);
89 %!# Test reading/writing varaible of type float
91 %! float_var = reshape(1:15,5,3)/10;
92 %! nc{'float_var'} = ncfloat('time','space');
93 %! nc{'float_var'}(:) = float_var;
94 %! assert(nc{'float_var'}(:),float_var,1e-5);
96 %!# Test reading/writing varaible of type double
98 %! double_var = reshape(1:15,5,3)/10;
99 %! nc{'double_var'} = ncdouble('time','space');
100 %! nc{'double_var'}(:) = double_var;
101 %! assert(nc{'double_var'}(:),double_var);
103 %!# Test reading/writing varaible of type char
105 %! char_var = reshape('ajhkjhgkjhfdgkh',5,3);
106 %! nc{'char_var'} = ncchar('time','space');
107 %! nc{'char_var'}(:) = char_var;
108 %! assert(nc{'char_var'}(:),char_var);
110 %!# Test reading/writing variable attributes
112 %! nv = nc{'int_var'};
113 %! nv.byte_att = ncbyte(123);
114 %! nv.short_att = ncshort(123);
115 %! nv.int_att = ncint(123);
116 %! nv.long_att = nclong(123);
117 %! nv.float_att = ncfloat(123);
118 %! nv.double_att = ncdouble(123.4);
119 %! nv.string_att = "test string";
120 %! assert(nv.byte_att,123)
121 %! assert(nv.short_att,123)
122 %! assert(nv.int_att,123)
123 %! assert(nv.long_att,123)
124 %! assert(nv.float_att,single(123))
125 %! assert(nv.double_att,123.4)
126 %! assert(nv.string_att,"test string")
130 %! dimlist = dim(nc);
131 %! assert(length(dimlist),3);
132 %! assert(dimlist{1}(:),5);
133 %! assert(dimlist{2}(:),3);
137 %! assert(isrecord(nc('record')) == 1);
139 %!# Test name on file
141 %! assert(name(nc),fname);
145 %! attlist = att(nc);
146 %! assert(length(attlist),7);
147 %! assert(name(attlist{1}),'byte_att');
148 %! assert(datatype(attlist{1}),'byte');
149 %! assert(attlist{1}(:),123);
153 %! varlist = var(nc);
154 %! assert(length(varlist),7);
155 %! assert(name(varlist{1}),'byte_var');
156 %! assert(datatype(varlist{1}),'byte');
158 %!# Test to write a variable by slices
163 %! nc('imax') = imax;
164 %! nc('jmax') = jmax;
165 %! nc('kmax') = kmax;
166 %! sliced_var = reshape(1:imax*jmax*kmax,imax,jmax,kmax);
167 %! nc{'sliced_var'} = ncdouble('imax','jmax','kmax');
170 %! nc{'sliced_var'}(i,:,:) = squeeze(sliced_var(i,:,:));
173 %! sliced_var2 = nc{'sliced_var'}(:);
175 %! assert(sliced_var2 == sliced_var)
181 %! scale_factor = .1;
183 %! nc{'autoscale_var'} = ncdouble('time','space');
184 %! nc{'autoscale_var'}.add_offset = add_offset;
185 %! nc{'autoscale_var'}.scale_factor = scale_factor;
186 %! nc{'autoscale_var',1}(:) = var;
188 %! unscaled = nc{'autoscale_var'}(:);
189 %! assert(var,scale_factor*unscaled+add_offset,1e-6);
191 %! scaled = nc{'autoscale_var',1}(:);
192 %! assert(var,scaled,1e-6);
194 %! nf = autoscale(nc{'autoscale_var'},1);
195 %! assert(nf(:),scaled,1e-6);
199 %! nc{'variable_with_gaps'}=ncdouble('time');
200 %! nv = nc{'variable_with_gaps'};
201 %! nv = autonan(nv,1);
202 %! nv.FillValue_ = 99;
205 %! nv = autonan(nv,0);
206 %! assert(all(nv(:) == 99))
208 %! nv = autonan(nv,1);
209 %! assert(all(isnan(nv(:))))
211 %!# Test size of vector
213 %! nc{'vector'} = ncfloat('time');
214 %! nc{'vector'}(:) = 0;
215 %! v = nc{'vector'}(:);
216 %! assert(size(v),[5 1])
220 %! nc{'scalar'} = ncint();
221 %! nc{'scalar'}(:) = 10;
222 %! v = nc{'scalar'}(:);
231 %!# Test rename function
233 %! filename = [tmpnam '-octcdf.nc'];
234 %! nf = netcdf(filename,'c');
236 %! nf('longitude') = 5;
237 %! nf('latitude') = 5;
239 %! nf.old_name = 'example attribute';
242 %! name(d,'new_name');
243 %! assert(name(d),'new_name')
246 %! name(a,'new_name');
247 %! assert(name(a),'new_name');
249 %! nf{'old_name'} = ncdouble('new_name','latitude');
250 %! v = nf{'old_name'};
251 %! name(v,'new_name')
252 %! assert(name(v),'new_name');
256 %!# Test 64bit-offset function
258 %! filename = [tmpnam '-octcdf.nc'];
259 %! nf = netcdf(filename,'c','64bit-offset');