source: Roms_tools/mexcdf/mexnc/tests/test_inq.m @ 1

Last change on this file since 1 was 1, checked in by cholod, 13 years ago

import Roms_Agrif

File size: 10.7 KB
Line 
1function test_inq ( ncfile )
2% TEST_INQ
3%
4% Tests number of dimensions, variables, global attributes, record dimension for
5% foo.nc.  Also tests helper routines, "nc_inq_ndims", "nc_inq_nvars", "nc_inq_ncatts".
6%
7% Tests bad ncid as well.
8%
9% Test 1:  Normal retrieval
10% Test 2:  Bad ncid.
11% Test 3:  Empty set ncid.
12% Test 4:  Non numeric ncid
13% Test 5:  INQ_NDIMS normal retrieval
14% Test 6:  INQ_NDIMS Bad ncid.
15% Test 7:  INQ_NDIMS Empty set ncid.
16% Test 8:  INQ_NDIMS Non numeric ncid
17% Test 9:  INQ_NVARS normal retrieval
18% Test 10:  INQ_NVARS Bad ncid.
19% Test 11:  INQ_NVARS Empty set ncid.
20% Test 12:  INQ_NVARS Non numeric ncid
21% Test 13:  INQ_NATTS normal retrieval
22% Test 14:  INQ_NATTS Bad ncid.
23% Test 15:  INQ_NATTS Empty set ncid.
24% Test 16:  INQ_NATTS Non numeric ncid
25
26if nargin == 0
27        ncfile = 'foo.nc';
28end
29create_testfile ( ncfile );
30
31test_001 ( ncfile );
32test_002 ( ncfile );
33test_003 ( ncfile );
34test_004 ( ncfile );
35test_005 ( ncfile );
36test_006 ( ncfile );
37test_007 ( ncfile );
38test_008 ( ncfile );
39test_009 ( ncfile );
40test_010 ( ncfile );
41test_011 ( ncfile );
42test_012 ( ncfile );
43test_013 ( ncfile );
44test_014 ( ncfile );
45test_015 ( ncfile );
46test_016 ( ncfile );
47
48fprintf ( 1, 'INQ succeeded\n' );
49fprintf ( 1, 'INQ_NDIMS succeeded\n' );
50fprintf ( 1, 'INQ_NVARS succeeded\n' );
51fprintf ( 1, 'INQ_NATTS succeeded\n' );
52
53
54function create_testfile ( ncfile )
55
56%
57% Create a netcdf file with
58[ncid, status] = mexnc ( 'create', ncfile, nc_clobber_mode );
59if status, error ( mexnc(strerror,status) ), end
60
61
62%
63% DIMDEF
64[xdimid, status] = mexnc ( 'def_dim', ncid, 'x', 20 );
65if status, error ( mexnc(strerror,status) ), end
66
67[ydimid, status] = mexnc ( 'def_dim', ncid, 'y', 24 );
68if status, error ( mexnc(strerror,status) ), end
69
70[zdimid, status] = mexnc ( 'def_dim', ncid, 'z', 32 );
71if status, error ( mexnc(strerror,status) ), end
72
73
74%
75% VARDEF
76[xdvarid, status] = mexnc ( 'def_var', ncid, 'x_double', 'double', 1, xdimid );
77if status, error ( mexnc(strerror,status) ), end
78
79[xmvarid, status] = mexnc ( 'def_var', ncid, 'xm_double', 'double', 3, [zdimid ydimid xdimid] );
80if status, error ( mexnc(strerror,status) ), end
81
82[xfvarid, status] = mexnc ( 'def_var', ncid, 'x_float', 'float', 1, xdimid );
83if status, error ( mexnc(strerror,status) ), end
84
85[xmvarid, status] = mexnc ( 'def_var', ncid, 'xm_float', 'float', 3, [zdimid ydimid xdimid] );
86if status, error ( mexnc(strerror,status) ), end
87
88[xlvarid, status] = mexnc ( 'def_var', ncid, 'x_long', 'long', 1, xdimid );
89if status, error ( mexnc(strerror,status) ), end
90
91[xmvarid, status] = mexnc ( 'def_var', ncid, 'xm_int', 'long', 3, [zdimid ydimid xdimid] );
92if status, error ( mexnc(strerror,status) ), end
93
94[x_short_varid, status] = mexnc ( 'def_var', ncid, 'x_short', 'short', 1, xdimid );
95if status, error ( mexnc(strerror,status) ), end
96
97[xmvarid, status] = mexnc ( 'def_var', ncid, 'xm_short', 'short', 3, [zdimid ydimid xdimid] );
98if status, error ( mexnc(strerror,status) ), end
99
100[x_byte_varid, status] = mexnc ( 'def_var', ncid, 'x_byte', 'byte', 1, xdimid );
101if status, error ( mexnc(strerror,status) ), end
102
103[xmvarid, status] = mexnc ( 'def_var', ncid, 'xm_byte', 'byte', 3, [zdimid ydimid xdimid] );
104if status, error ( mexnc(strerror,status) ), end
105
106[x_char_varid, status] = mexnc ( 'def_var', ncid, 'x_char', 'char', 1, xdimid );
107if status, error ( mexnc(strerror,status) ), end
108
109[xmvarid, status] = mexnc ( 'def_var', ncid, 'xm_char', 'char', 3, [zdimid ydimid xdimid] );
110if status, error ( mexnc(strerror,status) ), end
111
112
113
114%
115% Define some attributes
116attvalue = 'this is a test';
117attlen = length(attvalue);
118status = mexnc ( 'put_att_text', ncid, -1, 'test_global_attributes', 'char', attlen, attvalue );
119if status, error ( mexnc(strerror,status) ), end
120
121
122[status] = mexnc ( 'enddef', ncid );
123if status, error ( mexnc(strerror,status) ), end
124
125return
126
127
128
129
130
131
132
133
134
135function test_001 ( ncfile )
136
137[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
138if status, error ( mexnc(strerror,status) ), end
139
140[ndims, nvars, natts, recdim, status] = mexnc('INQ', ncid);
141if status, error ( mexnc(strerror,status) ), end
142
143if ndims ~= 3
144        msg = sprintf ( 'INQ returned %d dimensions when there should only have been 1\n', ndims );
145        error ( msg );
146end
147
148if nvars ~= 12
149        msg = sprintf ( 'INQ returned %d variables when there should have been 12\n', nvars );
150        error ( msg );
151end
152
153if natts ~= 1
154        msg = sprintf ( 'INQ returned %d attributes when there should have been 1\n', natts );
155        error ( msg );
156end
157
158if recdim ~= -1
159        msg = sprintf ( 'INQ returned an unlimited dimension when there should not have been one\n' );
160        error ( msg );
161end
162
163mexnc ( 'close', ncid );
164
165return
166
167
168
169
170
171
172
173function test_002 ( ncfile )
174
175[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
176if status, error ( mexnc(strerror,status) ), end
177
178
179% Test 2:  Bad ncid.
180testid = 'Test 2';
181[ndims, nvars, natts, recdim, status] = mexnc('INQ', -2000);
182if ( status == 0 )
183        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
184        error ( err_msg );
185end
186
187mexnc ( 'close', ncid );
188
189return
190
191
192
193
194
195
196
197
198function test_003 ( ncfile )
199
200[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
201if status, error ( mexnc(strerror,status) ), end
202
203% Test 3:  Empty set ncid.
204testid = 'Test 3';
205try
206        [ndims, nvars, natts, recdim, status] = mexnc('INQ', [] );
207        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
208        error ( err_msg );
209end
210
211mexnc ( 'close', ncid );
212
213return
214
215
216
217
218
219function test_004 ( ncfile )
220
221[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
222if status, error ( mexnc(strerror,status) ), end
223
224% Test 4:  Non numeric ncid
225testid = 'Test 4';
226try
227        [ndims, nvars, natts, recdim, status] = mexnc('INQ', 'blah' );
228        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
229        error ( err_msg );
230end
231
232mexnc ( 'close', ncid );
233
234return
235
236
237
238
239
240
241function test_005 ( ncfile )
242
243[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
244if status, error ( mexnc(strerror,status) ), end
245
246[ndims, status] = mexnc('INQ_NDIMS', ncid);
247if ( status ~= 0 )
248        msg = sprintf ( '%s:  %s:  ''%s''', mfilename, testid, mexnc ( 'strerror', status ) );
249        error ( msg );
250end
251
252if ndims ~= 3
253        msg = sprintf ( 'INQ_NDIMS returned %d dimensions when there should only have been 1\n', ndims );
254        error ( msg );
255end
256
257mexnc ( 'close', ncid );
258
259return
260
261
262
263
264
265
266
267function test_006 ( ncfile )
268
269[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
270if status, error ( mexnc(strerror,status) ), end
271
272
273testid = 'Test 6';
274[ndims, status] = mexnc('INQ_NDIMS', -2000);
275if ( status == 0 )
276        msg = sprintf ( '%s:  %s:  ''%s''', mfilename, testid, mexnc ( 'strerror', status ) );
277        error ( msg );
278end
279
280mexnc ( 'close', ncid );
281
282return
283
284
285
286
287
288
289
290
291
292
293
294
295
296function test_007 ( ncfile )
297
298[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
299if status, error ( mexnc(strerror,status) ), end
300
301testid = 'Test 7';
302try
303        [ndims, status] = mexnc('INQ_NDIMS', [] );
304        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
305        error ( err_msg );
306end
307
308mexnc ( 'close', ncid );
309
310return
311
312
313
314
315
316
317
318
319
320
321function test_008 ( ncfile )
322
323[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
324if status, error ( mexnc(strerror,status) ), end
325
326testid = 'Test 8';
327try
328        [ndims, status] = mexnc('INQ_NDIMS', 'blah' );
329        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
330        error ( err_msg );
331end
332
333mexnc ( 'close', ncid );
334
335return
336
337
338
339
340
341
342
343
344
345function test_009 ( ncfile )
346
347[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
348if status, error ( mexnc(strerror,status) ), end
349
350% Test 9:  INQ_NVARS Normal retrieval
351testid = 'Test 9';
352[nvars, status] = mexnc('INQ_NVARS', ncid);
353if ( status ~= 0 )
354        msg = sprintf ( '%s:  %s:  ''%s''', mfilename, testid, mexnc ( 'strerror', status ) );
355        error ( msg );
356end
357
358if nvars ~= 12
359        msg = sprintf ( 'INQ_NVARS returned %d dimensions when there should have been 12\n', nvars );
360        error ( msg );
361end
362
363mexnc ( 'close', ncid );
364
365return
366
367
368
369
370
371
372
373function test_010 ( ncfile )
374
375[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
376if status, error ( mexnc(strerror,status) ), end
377
378% Test 10:  INQ_NVARS Bad ncid.
379testid = 'Test 10';
380[nvars, status] = mexnc('INQ_NVARS', -2000);
381if ( status == 0 )
382        msg = sprintf ( '%s:  %s:  ''%s''', mfilename, testid, mexnc ( 'strerror', status ) );
383        error ( msg );
384end
385
386mexnc ( 'close', ncid );
387
388return
389
390
391
392
393
394
395
396
397
398
399
400
401
402function test_011 ( ncfile )
403
404[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
405if status, error ( mexnc(strerror,status) ), end
406
407% Test 11:  INQ_NVARS Empty set ncid.
408testid = 'Test 11';
409try
410        [nvars, status] = mexnc('INQ_NVARS', [] );
411        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
412        error ( err_msg );
413end
414
415mexnc ( 'close', ncid );
416
417return
418
419
420
421
422
423function test_012 ( ncfile )
424
425[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
426if status, error ( mexnc(strerror,status) ), end
427
428% Test 12:  Non numeric ncid
429testid = 'Test 12';
430try
431        [nvars, status] = mexnc('INQ_NVARS', 'blah' );
432        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
433        error ( err_msg );
434end
435
436mexnc ( 'close', ncid );
437
438return
439
440
441
442
443
444
445
446
447
448
449
450function test_013 ( ncfile )
451
452[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
453if status, error ( mexnc(strerror,status) ), end
454
455% Test 13:  INQ_NATTS Normal retrieval
456testid = 'Test 13';
457[natts, status] = mexnc('INQ_NATTS', ncid);
458if ( status ~= 0 )
459        msg = sprintf ( '%s:  %s:  ''%s''', mfilename, testid, mexnc ( 'strerror', status ) );
460        error ( msg );
461end
462
463if natts ~= 1
464        msg = sprintf ( 'INQ_NATTS returned %d attributes when there should have been 1\n', natts );
465        error ( msg );
466end
467
468mexnc ( 'close', ncid );
469
470return
471
472
473
474
475
476
477
478
479function test_014 ( ncfile )
480
481[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
482if status, error ( mexnc(strerror,status) ), end
483
484% Test 14:  INQ_NATTS Bad ncid.
485testid = 'Test 14';
486[natts, status] = mexnc('INQ_NATTS', -2000);
487if ( status == 0 )
488        msg = sprintf ( '%s:  %s:  ''%s''', mfilename, testid, mexnc ( 'strerror', status ) );
489        error ( msg );
490end
491
492mexnc ( 'close', ncid );
493
494return
495
496
497
498
499
500
501
502
503
504function test_015 ( ncfile )
505
506[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
507if status, error ( mexnc(strerror,status) ), end
508
509% Test 15:  INQ_NATTS Empty set ncid.
510testid = 'Test 15';
511try
512        [natts, status] = mexnc('INQ_NATTS', [] );
513        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
514        error ( err_msg );
515end
516
517mexnc ( 'close', ncid );
518
519return
520
521
522
523
524
525
526function test_016 ( ncfile )
527
528[ncid, status] = mexnc('open', ncfile, nc_nowrite_mode );
529if status, error ( mexnc(strerror,status) ), end
530
531
532testid = 'Test 16';
533try
534        [natts, status] = mexnc('INQ_NATTS', 'blah' );
535        err_msg = sprintf ( '%s:  %s:  Succeeded when it should have failed\n', mfilename, testid );
536        error ( err_msg );
537end
538
539mexnc ( 'close', ncid );
540
541return
542
543
544
545
546
547
Note: See TracBrowser for help on using the repository browser.