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

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

import Roms_Agrif

File size: 15.6 KB
Line 
1function test_put_get_var_double ( ncfile )
2% TEST_GET_PUT_VAR_DOUBLE
3%
4% Tests expected to succeed.
5% Test 001:  write to a singleton value, read them back using [put/get]_var_double
6% Test 002:  write to a singleton value, read them back using [put/get]_var1_double
7% Test 003:  write to an entire value, read them back using [put/get]_var_double
8% Test 004:  write to an single value, read them back using [put/get]_var1_double
9% Test 005:  write to an single value, read them back using [put/get]_vara_double
10% Test 006:  write to an single value, read them back using [put/get]_vars_double
11% Test 007:  write to an single value, read them back using [put/get]_varm_double
12%
13%     [PUT,GET]_VAR_DOUBLE: Write a 6x4 array of monotonically increasing values, read them back.
14%     [PUT,GET]_VAR1_DOUBLE:  write and read 255 to disk
15%     [PUT,GET]_VARA_DOUBLE:  write and read a 3x3 chunk
16%     [PUT,GET]_VARS_DOUBLE:  write and read the 6x4 array, subset by factor of 2, stride of 2
17%     [PUT,GET]_VARM_DOUBLE:  normally we want to transpose the data before writing with
18%                             mexnc.  However, this is makes for a good test of the VARM_XXX
19%                             routine, as that routine can actually handle the transpose for us.
20%                             So this test writes the same array of data as [put,get]_var_xxx,
21%                             but we don't bother to transpose the array.  Yes, these routines
22%                             really are that hard to wrap one's head around.
23%
24
25if ( nargin == 0 )
26        ncfile = 'foo.nc';
27end
28
29create_test_ncfile ( ncfile );
30
31test_001 ( ncfile );
32test_002 ( ncfile );
33test_003 ( ncfile );
34test_004 ( ncfile );
35test_005 ( ncfile );
36test_006 ( ncfile );
37test_007 ( ncfile );
38
39
40
41function test_003 ( ncfile )
42
43[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
44if ( status ~= 0 )
45        msg = sprintf ( '%s:  %s\n', mfilename, mexnc ( 'strerror', status ) );
46        error ( msg );
47end
48
49
50[y_dimid,status] = mexnc ( 'inq_dimid', ncid, 'y' );
51if status, error ( mexnc ( 'strerror', status ) ), end
52
53[len_y,status] = mexnc ( 'inq_dimlen', ncid, y_dimid );
54if status, error ( mexnc ( 'strerror', status ) ), end
55
56[x_dimid,status] = mexnc ( 'inq_dimid', ncid, 'x' );
57if status, error ( mexnc ( 'strerror', status ) ), end
58
59[len_x,status] = mexnc ( 'inq_dimlen', ncid, x_dimid );
60if status, error ( mexnc ( 'strerror', status ) ), end
61
62
63[varid, status] = mexnc('INQ_VARID', ncid, 'z_double');
64if ( status ~= 0 )
65        msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
66        error ( msg );
67end
68
69[varname,xtype,ndims,dimids,natts,status] = mexnc('INQ_VAR',ncid,varid);
70for j = 1:ndims
71        dimlen(j) = mexnc('INQ_DIMLEN',ncid,dimids(j));
72end
73
74
75
76
77input_data = [1:1:prod(dimlen)];
78input_data = reshape ( input_data, dimlen(1), dimlen(2) );
79
80status = mexnc ( 'PUT_VAR_DOUBLE', ncid, varid, input_data' );
81if ( status ~= 0 )
82        ncerr_msg = mexnc ( 'strerror', status );
83        msg = sprintf ( '%s:  PUT_VAR_DOUBLE failed, (%s)\n', mfilename, ncerr_msg );
84        error ( msg );
85end
86
87
88[output_data, status] = mexnc ( 'GET_VAR_DOUBLE', ncid, varid );
89if ( status ~= 0 )
90        msg = sprintf ( '%s:  GET_VAR_DOUBLE failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
91        error ( msg );
92end
93
94
95d = max(abs(output_data-input_data'))';
96if (any(d))
97        msg = sprintf ( '%s:  values written by PUT_VAR_DOUBLE do not match what was retrieved by GET_VAR_DOUBLE\n', mfilename  );
98        error ( msg );
99end
100
101fprintf ( 1, 'PUT_VAR_DOUBLE succeeded\n' );
102fprintf ( 1, 'GET_VAR_DOUBLE succeeded\n' );
103
104status = mexnc ( 'close', ncid );
105if ( status ~= 0 )
106        error ( 'CLOSE failed' );
107end
108
109return
110
111
112
113
114
115
116function test_004 ( ncfile )
117
118
119[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
120if ( status ~= 0 )
121        msg = sprintf ( '%s:  %s\n', mfilename, mexnc ( 'strerror', status ) );
122        error ( msg );
123end
124
125
126[y_dimid,status] = mexnc ( 'inq_dimid', ncid, 'y' );
127if status, error ( mexnc ( 'strerror', status ) ), end
128
129[len_y,status] = mexnc ( 'inq_dimlen', ncid, y_dimid );
130if status, error ( mexnc ( 'strerror', status ) ), end
131
132[x_dimid,status] = mexnc ( 'inq_dimid', ncid, 'x' );
133if status, error ( mexnc ( 'strerror', status ) ), end
134
135[len_x,status] = mexnc ( 'inq_dimlen', ncid, x_dimid );
136if status, error ( mexnc ( 'strerror', status ) ), end
137
138
139[varid, status] = mexnc('INQ_VARID', ncid, 'z_double');
140if ( status ~= 0 )
141        msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
142        error ( msg );
143end
144
145
146%
147% Test PUT/GET_VAR1
148input_data = 255;
149status = mexnc ( 'PUT_VAR1_DOUBLE', ncid, varid, [2 2], input_data );
150if ( status < 0 )
151        msg = sprintf ( '%s:  PUT_VAR1_DOUBLE failed\n', mfilename );
152        error ( msg );
153end
154
155
156status = mexnc ( 'SYNC', ncid );
157if ( status < 0 )
158        msg = sprintf ( '%s:  SYNC failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
159        error ( msg );
160end
161
162
163[output_data, status] = mexnc ( 'GET_VAR1_DOUBLE', ncid, varid, [2 2] );
164if ( status < 0 )
165        msg = sprintf ( '%s:  GET_VAR1_DOUBLE failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
166        error ( msg );
167end
168
169d = max(abs(double(output_data)-double(input_data)));
170if (d > 0)
171        msg = sprintf ( '%s:  values written by PUT_VAR1_DOUBLE do not match what was retrieved by GET_VAR_DOUBLE\n', mfilename  );
172        error ( msg );
173end
174
175status = mexnc ( 'close', ncid );
176if ( status ~= 0 )
177        error ( 'CLOSE failed' );
178end
179
180fprintf ( 1, 'PUT_VAR1_DOUBLE succeeded\n' );
181fprintf ( 1, 'GET_VAR1_DOUBLE succeeded\n' );
182return
183
184
185
186
187
188
189
190
191
192
193function test_005 ( ncfile )
194
195[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
196if ( status ~= 0 )
197        msg = sprintf ( '%s:  %s\n', mfilename, mexnc ( 'strerror', status ) );
198        error ( msg );
199end
200
201
202[y_dimid,status] = mexnc ( 'inq_dimid', ncid, 'y' );
203if status, error ( mexnc ( 'strerror', status ) ), end
204
205[len_y,status] = mexnc ( 'inq_dimlen', ncid, y_dimid );
206if status, error ( mexnc ( 'strerror', status ) ), end
207
208[x_dimid,status] = mexnc ( 'inq_dimid', ncid, 'x' );
209if status, error ( mexnc ( 'strerror', status ) ), end
210
211[len_x,status] = mexnc ( 'inq_dimlen', ncid, x_dimid );
212if status, error ( mexnc ( 'strerror', status ) ), end
213
214
215[varid, status] = mexnc('INQ_VARID', ncid, 'z_double');
216if ( status ~= 0 )
217        msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
218        error ( msg );
219end
220
221[varname,xtype,ndims,dimids,natts,status] = mexnc('INQ_VAR',ncid,varid);
222for j = 1:ndims
223        dimlen(j) = mexnc('INQ_DIMLEN',ncid,dimids(j));
224end
225
226
227
228
229input_data = [1:1:prod(dimlen)];
230input_data = reshape ( input_data, dimlen(1), dimlen(2) );
231
232input_data = input_data(2:4,1:3);
233input_data = input_data';
234start = [1 0]; count = [3 3];
235
236
237status = mexnc ( 'PUT_VARA_DOUBLE', ncid, varid, start, count, input_data );
238if ( status < 0 )
239        msg = sprintf ( '%s:  PUT_VARA_DOUBLE failed\n', mfilename );
240        error ( msg );
241end
242
243
244status = mexnc ( 'SYNC', ncid );
245if ( status < 0 )
246        msg = sprintf ( '%s:  SYNC failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
247        error ( msg );
248end
249
250
251[output_data, status] = mexnc ('GET_VARA_DOUBLE',ncid,varid,start,count);
252if ( status < 0 )
253        msg = sprintf ( '%s:  GET_VARA_DOUBLE failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
254        error ( msg );
255end
256
257d = max(abs(output_data-input_data))';
258if (any(d))
259        msg = sprintf ( '%s:  values written by PUT_VARA_DOUBLE do not match what was retrieved by GET_VARA_DOUBLE\n', mfilename  );
260        error ( msg );
261end
262
263status = mexnc ( 'close', ncid );
264if ( status ~= 0 )
265        error ( 'CLOSE failed' );
266end
267
268fprintf ( 1, 'PUT_VARA_DOUBLE succeeded\n' );
269fprintf ( 1, 'GET_VARA_DOUBLE succeeded\n' );
270
271return
272
273
274
275
276
277
278
279
280
281function test_006 ( ncfile )
282
283[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
284if ( status ~= 0 )
285        msg = sprintf ( '%s:  %s\n', mfilename, mexnc ( 'strerror', status ) );
286        error ( msg );
287end
288
289[varid, status] = mexnc('INQ_VARID', ncid, 'z_double');
290if ( status ~= 0 )
291        msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
292        error ( msg );
293end
294
295[varname,xtype,ndims,dimids,natts,status] = mexnc('INQ_VAR',ncid,varid);
296for j = 1:ndims
297        dimlen(j) = mexnc('INQ_DIMLEN',ncid,dimids(j));
298end
299
300
301
302
303input_data = [1:1:prod(dimlen)];
304input_data = reshape ( input_data, dimlen(1), dimlen(2) );
305input_data = input_data(1:2:end,1:2:end);
306[r,c] = size(input_data);
307
308status = mexnc ( 'PUT_VARS_DOUBLE', ncid, varid, [0 0], [r c], [2 2], input_data' );
309if ( status < 0 )
310        msg = sprintf ( '%s:  PUT_VARS_DOUBLE failed\n', mfilename );
311        error ( msg );
312end
313
314
315status = mexnc ( 'SYNC', ncid );
316if ( status < 0 )
317        msg = sprintf ( '%s:  SYNC failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
318        error ( msg );
319end
320
321
322[output_data, status] = mexnc ( 'GET_VARS_DOUBLE', ncid, varid, [0 0], [r c], [2 2] );
323if ( status < 0 )
324        msg = sprintf ( '%s:  GET_VARS_DOUBLE failed, msg ''%s''\n', mfilename, mexnc ( 'strerror', status ) );
325        error ( msg );
326end
327
328
329d = max(abs(output_data-input_data'))';
330if (any(d))
331        msg = sprintf ( '%s:  values written by PUT_VARS_DOUBLE do not match what was retrieved by GET_VARS_DOUBLE\n', mfilename  );
332        error ( msg );
333end
334
335fprintf ( 1, 'PUT_VARS_DOUBLE succeeded\n' );
336fprintf ( 1, 'GET_VARS_DOUBLE succeeded\n' );
337
338status = mexnc ( 'close', ncid );
339if ( status ~= 0 )
340        error ( 'CLOSE failed' );
341end
342
343return
344
345
346
347
348
349
350
351
352
353function test_007 ( ncfile )
354
355if mexnc_use_tmw
356        warning('GET_VARM and PUT_VARM operations are not supported in the netCDF package');
357        return
358end
359
360[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
361if ( status ~= 0 )
362        msg = sprintf ( '%s:  %s\n', mfilename, mexnc ( 'strerror', status ) );
363        error ( msg );
364end
365
366
367[y_dimid,status] = mexnc ( 'inq_dimid', ncid, 'y' );
368if status, error ( mexnc ( 'strerror', status ) ), end
369
370[len_y,status] = mexnc ( 'inq_dimlen', ncid, y_dimid );
371if status, error ( mexnc ( 'strerror', status ) ), end
372
373[x_dimid,status] = mexnc ( 'inq_dimid', ncid, 'x' );
374if status, error ( mexnc ( 'strerror', status ) ), end
375
376[len_x,status] = mexnc ( 'inq_dimlen', ncid, x_dimid );
377if status, error ( mexnc ( 'strerror', status ) ), end
378
379
380[varid, status] = mexnc('INQ_VARID', ncid, 'z_double');
381if ( status ~= 0 )
382        msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
383        error ( msg );
384end
385
386
387
388%
389% Test PUT/GET_VARM
390input_data = [1:1:len_y*len_x];
391input_data = reshape ( input_data, len_y, len_x );
392
393start_coord = [0 0];
394count_coord = [6 4];
395stride_coord = [1 1];
396
397%
398% This is the tricky part.  When the matrix arrives in the mex file, it
399% arrives essentially transposed, as a 4x6 array, monotonically increasing
400% across the columns instead of rows.  But in the netcdf file, we want the
401% dataset to increase monotonically by rows.  This is an inter-element
402% distance of 1 for the rows.  So the first column should consist of
403% [1 2 3 4 5 6], and the 2nd row starts at 7.  That inter-element distance
404% is then 6.  In the imap vector, you start with the slowest varying FILE
405% dimension and proceed out to the most rapidly varying.  This means imap
406% should be 6 and then 1.
407%
408%     NetCDF dimension         inter-element distance
409%     most rapidly varying     6
410%     most slowly varying      1
411%
412imap_coord = [1 6];
413
414%
415% Write without transposing first.
416status = mexnc ( 'put_varm_double', ncid, varid, start_coord, count_coord, stride_coord, imap_coord, input_data );
417if ( status ~= 0 )
418        ncerr_msg = mexnc ( 'strerror', status );
419        msg = sprintf ( '%s:  ''put_varm_double'' failed on var rh, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
420        error ( msg );
421end
422
423status = mexnc ( 'sync', ncid );
424if ( status ~= 0 )
425        ncerr_msg = mexnc ( 'strerror', status );
426        msg = sprintf ( '%s:  ''sync'' failed on file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
427        error ( msg );
428end
429
430
431%
432% Now read it back.
433%
434% This is the tricky part.  When the matrix arrives from the mexfile,
435% one has to remember that it arrives transposed.  So what we want is
436% to choose imap so that it yields the transpose of what we really want.
437% Then we get the transpose of a transpose, which would be the data arranged
438% in rows and columns in matlab exactly as it is in the netcdf file.
439%
440% The NetCDF file has the data stored as
441%
442%    |  1  7 13 19 |
443%    |  2  8 14 20 |
444%    |  3  9 15 21 |
445%    |  4 10 16 22 |
446%    |  5 11 17 23 |
447%    |  6 12 18 24 |
448%
449% The transpose of what we want would be
450%
451%    |  1  2  3  4  5  6 |
452%    |  7  8  9 10 11 12 |
453%    | 13 14 15 16 17 18 |
454%    | 19 20 21 22 23 24 |
455%
456% Start with the end of imap, which is the most rapidly varying NetCDF
457% dimension.  That's the column.  In the internal array (that's the 2nd
458% array above), how far between elements?  In the NetCDF file file, 1 and 7
459% are next to each other across the row, but in the internal array (which
460% is still inside the mex file and therefore in C, so row major order)
461% they have a distance of 6.  The first element of imap corresponds to
462% the slowest varying NetCDF dimension, the rows.  In the internal array,
463% how far between row elements?  In the NetCDF file, 1 and 2 are next to
464% each other across rows, but in the internal array, they have a distance
465% of 1 across columns.  So imap is the same as above, [1 6].
466[output_data,status] = mexnc ( 'get_varm_double', ncid, varid, start_coord, count_coord, stride_coord, imap_coord );
467if ( status ~= 0 )
468        ncerr_msg = mexnc ( 'strerror', status );
469        msg = sprintf ( '%s:  ''get_varm_double'' failed on var rh, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
470        error ( msg );
471end
472
473d = max(abs(output_data-input_data))';
474if (any(d))
475        msg = sprintf ( '%s:  values written by PUT_VARM_DOUBLE do not match what was retrieved by GET_VARM_DOUBLE\n', mfilename  );
476        error ( msg );
477end
478
479
480
481fprintf ( 1, 'PUT_VARM_DOUBLE succeeded\n' );
482fprintf ( 1, 'GET_VARM_DOUBLE succeeded\n' );
483
484
485
486%
487% CLOSE
488status = mexnc ( 'close', ncid );
489if ( status ~= 0 )
490        error ( 'CLOSE failed' );
491end
492
493
494return
495
496
497
498
499
500
501function test_001 ( ncfile )
502
503[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
504if ( status ~= 0 )
505        msg = sprintf ( '%s:  %s\n', mfilename, mexnc ( 'strerror', status ) );
506        error ( msg );
507end
508
509fprintf ( 1, 'a\n' );
510
511%
512% Test 1: singleton
513[varid, status] = mexnc('INQ_VARID', ncid, 'double_singleton');
514if ( status ~= 0 )
515        msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
516        error ( msg );
517end
518
519fprintf ( 1, 'b\n' );
520
521input_data = 3.14;
522status = mexnc ( 'PUT_VAR_DOUBLE', ncid, varid, input_data );
523if status
524        error ( mexnc('strerror',status) );
525end
526
527fprintf ( 1, 'c\n' );
528
529[output_data, status] = mexnc ( 'GET_VAR_DOUBLE', ncid, varid );
530if status
531        error ( mexnc('strerror',status) );
532end
533
534fprintf ( 1, 'd\n' );
535
536d = max(abs(output_data-input_data))';
537if (any(d))
538        msg = sprintf ( '%s:  values written by PUT_VAR_DOUBLE do not match what was retrieved by GET_VAR_DOUBLE\n', mfilename  );
539        error ( msg );
540end
541
542fprintf ( 1, 'GET_VAR_DOUBLE succeeded on a singleton\n' );
543fprintf ( 1, 'PUT_VAR_DOUBLE succeeded on a singleton\n' );
544
545status = mexnc ( 'close', ncid );
546if status, error ( mexnc('strerror',status) ), end
547
548return
549
550
551
552
553
554
555function test_002 ( ncfile )
556
557[ncid, status] = mexnc ( 'open', ncfile, nc_write_mode );
558if ( status ~= 0 )
559        msg = sprintf ( '%s:  %s\n', mfilename, mexnc ( 'strerror', status ) );
560        error ( msg );
561end
562
563
564%
565% Test 1: singleton
566[varid, status] = mexnc('INQ_VARID', ncid, 'double_singleton');
567if ( status ~= 0 )
568        msg = sprintf ( '%s:  INQ_VARID failed\n', mfilename );
569        error ( msg );
570end
571
572input_data = 3.14;
573status = mexnc ( 'PUT_VAR1_DOUBLE', ncid, varid, 0, input_data );
574if status
575        error ( mexnc('strerror',status) );
576end
577
578[output_data, status] = mexnc ( 'GET_VAR1_DOUBLE', ncid, varid, 0 );
579if status
580        error ( mexnc('strerror',status) );
581end
582
583d = max(abs(output_data-input_data))';
584if (any(d))
585        msg = sprintf ( '%s:  values written by PUT_VAR1_DOUBLE do not match what was retrieved by GET_VAR1_DOUBLE\n', mfilename  );
586        error ( msg );
587end
588
589
590fprintf ( 1, 'GET_VAR1_DOUBLE succeeded on a singleton\n' );
591fprintf ( 1, 'PUT_VAR1_DOUBLE succeeded on a singleton\n' );
592
593status = mexnc ( 'close', ncid );
594if status, error ( mexnc('strerror',status) ), end
595
596return
Note: See TracBrowser for help on using the repository browser.