]> Creatis software - CreaPhase.git/blob - octave_packages/octcdf-1.1.4/nctest.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / octcdf-1.1.4 / nctest.m
1 ## Copyright (C) 2005 Alexander Barth <barth.alexander@gmail.com>
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
16 ## -*- texinfo -*-
17 ## @deftypefn {Loadable Function} nctest
18 ## Tests the octcdf toolbox. Tests results are written to nctest.log. All tests
19 ## should pass.
20 ## @end deftypefn
21
22 ## Author: Alexander Barth <barth.alexander@gmail.com>
23
24
25
26 function [passes,tests] = nctest
27    disp('writing test output to nctest.log');
28    test('nctest','normal','nctest.log');
29 endfunction
30
31
32 %!shared fname, nc
33 %! fname = [tmpnam '-octcdf.nc'];
34 %! nc = netcdf(fname,'c');
35
36 %!# Test reading/writing dimension
37 %!test
38 %! nc('time') = 5;
39 %! nc('space') = 3;
40 %! nc('record') = 0;
41 %! assert(length(nc('time')),5);
42 %! assert(length(nc('space')),3);
43
44 %!# Test reading/writing global attributes
45 %!test
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")
60
61 %!# Test reading/writing varaible of type byte
62 %!test
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);
67
68 %!# Test reading/writing varaible of type short
69 %!test
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);
74
75 %!# Test reading/writing varaible of type int
76 %!test
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);
81
82 %!# Test reading/writing varaible of type long
83 %!test
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);
88
89 %!# Test reading/writing varaible of type float
90 %!test
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);
95
96 %!# Test reading/writing varaible of type double
97 %!test
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);
102
103 %!# Test reading/writing varaible of type char
104 %!test
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);
109
110 %!# Test reading/writing variable attributes
111 %!test
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")
127
128 %!# Test ncdim
129 %!test
130 %! dimlist = dim(nc);
131 %! assert(length(dimlist),3);
132 %! assert(dimlist{1}(:),5);
133 %! assert(dimlist{2}(:),3);
134
135 %!# Test isrecord
136 %!test
137 %! assert(isrecord(nc('record')) == 1);
138
139 %!# Test name on file
140 %!test
141 %! assert(name(nc),fname);
142
143 %!# Test ncatt
144 %!test
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);
150
151 %!# Test ncvar
152 %!test
153 %! varlist = var(nc);
154 %! assert(length(varlist),7);
155 %! assert(name(varlist{1}),'byte_var');
156 %! assert(datatype(varlist{1}),'byte');
157
158 %!# Test to write a variable by slices
159 %!test
160 %! imax = 10;
161 %! jmax = 11;
162 %! kmax = 12;
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');
168 %! 
169 %! for i=1:imax
170 %!   nc{'sliced_var'}(i,:,:) = squeeze(sliced_var(i,:,:));
171 %! end
172 %! 
173 %! sliced_var2 = nc{'sliced_var'}(:);
174 %! 
175 %! assert(sliced_var2 == sliced_var)
176
177 %!# Test autoscale
178 %!test
179 %! var = rand(5,3);
180 %! add_offset = 1; 
181 %! scale_factor = .1;
182 %!
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;     
187 %!
188 %! unscaled = nc{'autoscale_var'}(:);
189 %! assert(var,scale_factor*unscaled+add_offset,1e-6);
190 %!
191 %! scaled = nc{'autoscale_var',1}(:);                          
192 %! assert(var,scaled,1e-6);
193 %!
194 %! nf = autoscale(nc{'autoscale_var'},1);
195 %! assert(nf(:),scaled,1e-6);
196
197 %!# Test autonan
198 %!test
199 %! nc{'variable_with_gaps'}=ncdouble('time'); 
200 %! nv = nc{'variable_with_gaps'}; 
201 %! nv = autonan(nv,1);
202 %! nv.FillValue_ = 99; 
203 %! nv(:) = NaN;
204 %! 
205 %! nv = autonan(nv,0);
206 %! assert(all(nv(:) == 99))
207 %! 
208 %! nv = autonan(nv,1);
209 %! assert(all(isnan(nv(:))))
210
211 %!# Test size of vector
212 %!test
213 %! nc{'vector'} = ncfloat('time'); 
214 %! nc{'vector'}(:) = 0;  
215 %! v = nc{'vector'}(:); 
216 %! assert(size(v),[5 1])
217
218 %!# Test scalar
219 %!test
220 %! nc{'scalar'} = ncint();
221 %! nc{'scalar'}(:) = 10;
222 %! v = nc{'scalar'}(:);
223 %! assert(v,10)
224  
225 %!# Close file
226 %!test
227 %! close(nc);
228 %! delete(fname);
229
230
231 %!# Test rename function
232 %!test
233 %! filename = [tmpnam '-octcdf.nc'];
234 %! nf = netcdf(filename,'c');
235 %! 
236 %! nf('longitude') = 5;
237 %! nf('latitude') = 5;
238 %! 
239 %! nf.old_name = 'example attribute';
240 %! 
241 %! d = dim(nf){1};
242 %! name(d,'new_name');
243 %! assert(name(d),'new_name')
244 %! 
245 %! a = att(nf){1};
246 %! name(a,'new_name');
247 %! assert(name(a),'new_name');
248 %! 
249 %! nf{'old_name'} = ncdouble('new_name','latitude');
250 %! v = nf{'old_name'};
251 %! name(v,'new_name')
252 %! assert(name(v),'new_name');
253 %! close(nf);
254 %! delete(filename);
255
256 %!# Test 64bit-offset function
257 %!test
258 %! filename = [tmpnam '-octcdf.nc'];
259 %! nf = netcdf(filename,'c','64bit-offset');
260 %! close(nf);
261 %! delete(filename);
262