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

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

import Roms_Agrif

File size: 13.2 KB
Line 
1function test_varput_1g ( ncfile )
2% TEST_VARPUT_1G
3%
4% This routine tests VARPUT1, VARPUT, and VARPUTG routines with both scaling
5% and no scaling.  MEXCDF should never have been written with scaling built in.
6%
7% Test 1:  VARPUT1/VARGET1
8% Test 2:  varput1/varget1 with scaling
9% Test 3:  varput/varget
10% Test 4:  varput/varget with scaling
11% Test 4.5:  varput/varget with scaling, negative integers
12% Test 5:  varputg/vargetg
13% Test 6:  varputg/vargetg  with scaling
14% Test 7:  VARPUT1 with a bad ncid
15% Test 8:  VARGET1 with a bad ncid
16
17mexnc ( 'setopts', 0 );
18
19%
20% ok, first create this baby.
21[ncid, status] = mexnc ( 'create', ncfile, nc_clobber_mode );
22if ( status ~= 0 )
23        ncerr_msg = mexnc ( 'strerror', status );
24        msg = sprintf ( '%s:  ''create'' failed on %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
25        error ( msg );
26end
27
28
29
30%
31% Create the fixed dimension. 
32len_x = 100;
33[xdimid, status] = mexnc ( 'def_dim', ncid, 'x', len_x );
34if ( status ~= 0 )
35        ncerr_msg = mexnc ( 'strerror', status );
36        msg = sprintf ( '%s:  ''def_dim'' failed on dim x, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
37        error ( msg );
38end
39
40
41[z_double_varid, status] = mexnc ( 'def_var', ncid, 'z_double', nc_double, 1, [xdimid] );
42if ( status ~= 0 )
43        ncerr_msg = mexnc ( 'strerror', status );
44        msg = sprintf ( '%s:  ''def_var'' failed on var x_short, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
45        error ( msg );
46end
47
48
49
50[z_short_varid, status] = mexnc ( 'def_var', ncid, 'z_short', nc_short, 1, [xdimid] );
51if ( status ~= 0 )
52        ncerr_msg = mexnc ( 'strerror', status );
53        msg = sprintf ( '%s:  ''def_var'' failed on var z_short, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
54        error ( msg );
55end
56
57
58eps = 0.01;
59status = mexnc ( 'put_att_double', ncid, z_short_varid, 'scale_factor', nc_double, 1, eps );
60if ( status ~= 0 )
61        ncerr_msg = mexnc ( 'strerror', status );
62        msg = sprintf ( '%s:  ''%s''\n', mfilename, ncerr_msg );
63        error ( msg );
64end
65
66
67status = mexnc ( 'put_att_double', ncid, z_short_varid, 'add_offset', nc_double, 1, 0.00 );
68if ( status ~= 0 )
69        ncerr_msg = mexnc ( 'strerror', status );
70        msg = sprintf ( '%s:  ''%s''\n', mfilename, ncerr_msg );
71        error ( msg );
72end
73
74
75
76
77
78[status] = mexnc ( 'enddef', ncid );
79if ( status ~= 0 )
80        ncerr_msg = mexnc ( 'strerror', status );
81        msg = sprintf ( '%s:  ''enddef'' failed, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
82        error ( msg );
83end
84
85
86%
87% CLOSE
88status = mexnc ( 'close', ncid );
89if ( status ~= 0 )
90        error ( 'CLOSE failed' );
91end
92
93
94
95[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
96if ( status ~= 0 )
97        ncerr_msg = mexnc ( 'strerror', status );
98        msg = sprintf ( '%s:  ''%s''\n', mfilename, ncerr_msg );
99        error ( msg );
100end
101
102
103
104[z_double_varid, status] = mexnc('INQ_VARID', ncid, 'z_double');
105if ( status ~= 0 )
106        msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
107        error ( msg );
108end
109
110
111%
112% Test 1:  varput1/varget1
113testid = 'Test 1';
114input_data = 3.14159;
115status = mexnc ( 'VARPUT1', ncid, z_double_varid, [0], input_data );
116if ( status ~= 0 )
117        ncerr_msg = mexnc ( 'strerror', status );
118        msg = sprintf ( '%s:  VARPUT1 failed, (%s)\n', mfilename, ncerr_msg );
119        error ( msg );
120end
121
122
123[output_data, status] = mexnc ( 'VARGET1', ncid, z_double_varid, [0] );
124if ( status ~= 0 )
125        msg = sprintf ( '%s:  GET_VAR_DOUBLE failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
126        error ( msg );
127end
128
129output_data = output_data';
130
131d = max(abs(output_data-input_data))';
132if (any(d))
133        msg = sprintf ( '%s:  %s:  values written by PUT_VAR_DOUBLE do not match what was retrieved by GET_VAR_DOUBLE\n', mfilename, testid  );
134        error ( msg );
135end
136
137fprintf ( 1, 'VARPUT1 succeeded\n' );
138fprintf ( 1, 'VARGET1 succeeded\n' );
139
140
141
142%
143% Test 2:  varput1/varget1 with scaling
144testid = 'Test 2';
145input_data = 3.14159;
146status = mexnc ( 'VARPUT1', ncid, z_short_varid, [0], input_data,1 );
147if ( status ~= 0 )
148        ncerr_msg = mexnc ( 'strerror', status );
149        msg = sprintf ( '%s:  VARPUT1 failed, (%s)\n', mfilename, ncerr_msg );
150        error ( msg );
151end
152
153
154[output_data, status] = mexnc ( 'VARGET1', ncid, z_short_varid, [0], 1 );
155if ( status ~= 0 )
156        msg = sprintf ( '%s:  GET_VAR_DOUBLE failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
157        error ( msg );
158end
159
160output_data = output_data';
161
162d = max(abs(output_data-input_data))';
163ind = find ( d > eps );
164if (any(ind))
165        msg = sprintf ( '%s:  %s:  values written by VARPUT1 do not match what was retrieved by VARGET1\n', mfilename  , testid );
166        error ( msg );
167end
168
169fprintf ( 1, 'VARPUT1 with scaling (please don''t do this, it''s bad) succeeded\n' );
170fprintf ( 1, 'VARGET1 with scaling (please don''t do this, it''s bad) succeeded\n' );
171
172
173%
174% Test 3:  varput/varget
175testid = 'Test 3';
176input_data = rand(100,1);
177status = mexnc ( 'VARPUT', ncid, z_double_varid, [0], [len_x], input_data' );
178if ( status ~= 0 )
179        ncerr_msg = mexnc ( 'strerror', status );
180        msg = sprintf ( '%s:  VARPUT failed, (%s)\n', mfilename, ncerr_msg );
181        error ( msg );
182end
183
184
185[output_data, status] = mexnc ( 'VARGET', ncid, z_double_varid, [0], [len_x] );
186if ( status ~= 0 )
187        msg = sprintf ( '%s:  VARGET failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
188        error ( msg );
189end
190
191output_data = output_data';
192
193d = max(abs(output_data-input_data))';
194if (any(d))
195        msg = sprintf ( '%s:  %s:  values written by VARPUT do not match what was retrieved by VARGET\n', mfilename, testid  );
196        error ( msg );
197end
198
199fprintf ( 1, 'VARPUT succeeded\n' );
200fprintf ( 1, 'VARGET succeeded\n' );
201
202
203%
204% Test 4:  varput/varget with scaling
205testid = 'Test 4';
206input_data = rand(100,1);
207status = mexnc ( 'VARPUT', ncid, z_short_varid, [0], [len_x], input_data', 1 );
208if ( status ~= 0 )
209        ncerr_msg = mexnc ( 'strerror', status );
210        msg = sprintf ( '%s:  VARPUT failed, (%s)\n', mfilename, ncerr_msg );
211        error ( msg );
212end
213
214
215[output_data, status] = mexnc ( 'VARGET', ncid, z_short_varid, [0], [len_x], 1 );
216if ( status ~= 0 )
217        msg = sprintf ( '%s:  VARGET failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
218        error ( msg );
219end
220
221output_data = output_data';
222
223d = max(abs(output_data-input_data))';
224ind = find ( d > eps/2 );
225if (any(ind))
226        msg = sprintf ( '%s:  %s:  values written by VARPUT do not match what was retrieved by VARGET\n', mfilename , testid );
227        error ( msg );
228end
229
230
231fprintf ( 1, 'VARPUT with scaling (please don''t do this, it''s bad) succeeded\n' );
232fprintf ( 1, 'VARGET with scaling (please don''t do this, it''s bad) succeeded\n' );
233
234
235
236
237%
238% Test 4.5:  varput/varget with scaling, negative integers being the target.
239testid = 'Test 4.5';
240input_data = rand(100,1) - 1;
241status = mexnc ( 'VARPUT', ncid, z_short_varid, [0], [len_x], input_data', 1 );
242if ( status ~= 0 )
243        ncerr_msg = mexnc ( 'strerror', status );
244        msg = sprintf ( '%s:  VARPUT failed, (%s)\n', mfilename, ncerr_msg );
245        error ( msg );
246end
247
248
249[output_data, status] = mexnc ( 'VARGET', ncid, z_short_varid, [0], [len_x], 1 );
250if ( status ~= 0 )
251        msg = sprintf ( '%s:  VARGET failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
252        error ( msg );
253end
254
255output_data = output_data';
256
257d = max(abs(output_data-input_data))';
258ind = find ( d > eps/2 );
259if (any(ind))
260        msg = sprintf ( '%s:  %s:  values written by VARPUT do not match what was retrieved by VARGET\n', mfilename , testid );
261        error ( msg );
262end
263
264
265fprintf ( 1, 'VARPUT with scaling, negative integers\n' );
266fprintf ( 1, 'VARGET with scaling, negative integers succeeded\n' );
267
268
269
270
271%
272% Test 4.6:  varput/varget with negative integers
273testid = 'Test 4.6';
274input_data = round(rand(100,1)*100 - 200);
275status = mexnc ( 'VARPUT', ncid, z_short_varid, [0], [len_x], input_data', 1 );
276if ( status ~= 0 )
277        ncerr_msg = mexnc ( 'strerror', status );
278        msg = sprintf ( '%s:  VARPUT failed, (%s)\n', mfilename, ncerr_msg );
279        error ( msg );
280end
281
282
283[output_data, status] = mexnc ( 'VARGET', ncid, z_short_varid, [0], [len_x], 1 );
284if ( status ~= 0 )
285        msg = sprintf ( '%s:  VARGET failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
286        error ( msg );
287end
288
289output_data = output_data';
290
291d = max(abs(output_data-input_data))';
292ind = find ( d > eps/2 );
293if (any(ind))
294        msg = sprintf ( '%s:  %s:  values written by VARPUT do not match what was retrieved by VARGET\n', mfilename , testid );
295        error ( msg );
296end
297
298
299fprintf ( 1, 'VARPUT with negative integers\n' );
300fprintf ( 1, 'VARGET with negative integers succeeded\n' );
301
302
303
304
305%
306% VARPUTG
307% Test 5:  varputg/vargetg
308testid = 'Test 5';
309input_data = rand(100,1);
310input_data = input_data(1:2:end,1:2:end);
311[r,c] = size(input_data);
312status = mexnc ( 'VARPUTG', ncid, z_double_varid, [0 0], [r c], [2 2], [], input_data' );
313if ( status ~= 0 )
314        ncerr_msg = mexnc ( 'strerror', status );
315        msg = sprintf ( '%s:  VARPUTG failed, (%s)\n', mfilename, ncerr_msg );
316        error ( msg );
317end
318
319
320[output_data, status] = mexnc ( 'VARGETG', ncid, z_double_varid, [0 0], [r c], [2 2], [] );
321if ( status ~= 0 )
322        msg = sprintf ( '%s:  VARGETG failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
323        error ( msg );
324end
325
326output_data = output_data';
327
328d = max(abs(output_data-input_data))';
329if (any(d))
330        msg = sprintf ( '%s:  %s:  values written by VARPUTG do not match what was retrieved by VARGETG\n', mfilename, testid  );
331        error ( msg );
332end
333
334fprintf ( 1, 'VARPUTG succeeded\n' );
335fprintf ( 1, 'VARGETG succeeded\n' );
336
337
338%
339%
340% VARPUTG
341% Test 6:  varputg/vargetg  with scaling
342testid = 'Test 6';
343input_data = rand(100,1);
344input_data = input_data(1:2:end);
345[r,c] = size(input_data);
346status = mexnc ( 'VARPUTG', ncid, z_short_varid, [0 0], [r], [2], [], input_data', 1 );
347if ( status ~= 0 )
348        ncerr_msg = mexnc ( 'strerror', status );
349        msg = sprintf ( '%s:  VARPUTG failed, (%s)\n', mfilename, ncerr_msg );
350        error ( msg );
351end
352
353
354[output_data, status] = mexnc ( 'VARGETG', ncid, z_short_varid, [0], [r], [2], [], 1 );
355if ( status ~= 0 )
356        msg = sprintf ( '%s:  ''%s''\n', mfilename,  mexnc ( 'strerror', status ) );
357        error ( msg );
358end
359
360output_data = output_data';
361
362d = max(abs(output_data-input_data))';
363ind = find ( d > eps/2 );
364if (any(ind))
365        msg = sprintf ( '%s:  %s:  values written by VARPUTG do not match what was retrieved by VARGETG\n', mfilename, testid  );
366        error ( msg );
367end
368
369
370fprintf ( 1, 'VARPUTG with scaling (please don''t do this, it''s bad) succeeded\n' );
371fprintf ( 1, 'VARGETG with scaling (please don''t do this, it''s bad) succeeded\n' );
372
373
374
375
376
377%
378%
379% Test 7:  VARPUT1 with a bad ncid
380testid = 'Test 7';
381input_data = 3.14159;
382status = mexnc ( 'VARPUT1', -100, z_double_varid, [0], input_data );
383if ( status >= 0 )
384        msg = sprintf ( '%s:  %s:  VARPUT1 succeeded with a bad ncid\n', mfilename, testid );
385        error ( msg );
386end
387
388
389%
390% Test 8:  VARGET1 with a bad ncid
391testid = 'Test 8';
392[output_data, status] = mexnc ( 'VARGET1', -100, z_double_varid, [0] );
393if ( status == 0 )
394        msg = sprintf ( '%s:  %s:  VARGET1 succeeded with a bad ncid\n', mfilename, testid );
395        error ( msg );
396end
397
398
399
400%
401% Test 9:  VARPUT1 with a bad varid
402testid = 'Test 9';
403input_data = 3.14159;
404status = mexnc ( 'VARPUT1', ncid, -500, [0], input_data );
405if ( status == 0 )
406        msg = sprintf ( '%s:  %s:  VARPUT1 succeeded with a bad varid\n', mfilename, testid );
407        error ( msg );
408end
409
410
411%
412% VARGET1 with a bad varid
413[output_data, status] = mexnc ( 'VARGET1', ncid, -500, [0] );
414if ( status >= 0 )
415        msg = sprintf ( '%s:  VARGET1 succeeded with a bad varid\n', mfilename );
416        error ( msg );
417end
418
419
420
421%
422% VARPUT with a bad ncid
423%input_data = [1:1:len_y*len_x] + 3.14159;
424%input_data = reshape ( input_data, len_y, len_x );
425input_data = rand(100,1);
426status = mexnc ( 'VARPUT', -500, z_short_varid, [0], size(input_data), input_data' );
427if ( status >= 0 )
428        msg = sprintf ( '%s:  VARPUT succeeded with a bad varid\n', mfilename );
429        error ( msg );
430end
431
432
433%
434% VARGET with a bad ncid
435[output_data, status] = mexnc ( 'VARGET', -500, z_short_varid, [0], size(input_data) );
436if ( status >= 0 )
437        msg = sprintf ( '%s:  VARGET succeeded with a bad varid\n', mfilename );
438        error ( msg );
439end
440
441
442
443
444%
445% VARPUT with a bad varid
446input_data = rand(100,1);
447status = mexnc ( 'VARPUT', ncid, -500, [0], size(input_data), input_data' );
448if ( status >= 0 )
449        msg = sprintf ( '%s:  VARPUT succeeded with a bad varid\n', mfilename );
450        error ( msg );
451end
452
453
454%
455% VARGET with a bad varid
456[output_data, status] = mexnc ( 'VARGET', ncid, -500, [0], size(input_data) );
457if ( status >= 0 )
458        msg = sprintf ( '%s:  VARGET succeeded with a bad varid\n', mfilename );
459        error ( msg );
460end
461
462
463
464
465%
466% VARPUTG with a bad ncid
467%Tinput_data = input_data(1:2:end,1:2:end);
468input_data = rand(100,1);
469[r,c] = size(input_data);
470status = mexnc ( 'VARPUTG', -5, z_short_varid, [0], [r c], [2 2], [], input_data', 1 );
471if ( status >= 0 )
472        msg = sprintf ( '%s:  VARPUTG succeeded with a bad ncid\n', mfilename );
473        error ( msg );
474end
475
476
477[output_data, status] = mexnc ( 'VARGETG', -5, z_short_varid, [0 0], [r c], [2 2], [], 1 );
478if ( status >= 0 )
479        msg = sprintf ( '%s:  VARGETG succeeded with a bad ncid\n', mfilename );
480        error ( msg );
481end
482
483
484status = mexnc ( 'VARPUTG', ncid, -5, [0 0], [r c], [2 2], [], input_data', 1 );
485if ( status >= 0 )
486        msg = sprintf ( '%s:  VARPUTG succeeded with a bad varid\n', mfilename );
487        error ( msg );
488end
489
490
491[output_data, status] = mexnc ( 'VARGETG', ncid, -5, [0 0], [r c], [2 2], [], 1 );
492if ( status >= 0 )
493        msg = sprintf ( '%s:  VARGETG succeeded with a bad varid\n', mfilename );
494        error ( msg );
495end
496
497
498
499
500%
501% CLOSE
502status = mexnc ( 'close', ncid );
503if ( status ~= 0 )
504        error ( 'CLOSE failed' );
505end
506
507
508return
509
510
511
512
513
514
515
Note: See TracBrowser for help on using the repository browser.