source: Roms_tools/mexcdf/snctools/tests/test_nc_varput.m @ 1

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

import Roms_Agrif

File size: 21.0 KB
Line 
1function test_nc_varput ( ncfile )
2% TEST_NC_VARPUT:
3%
4%
5% Generic Tests, should all fail gracefully.
6% Test 001:  pass 0 arguments into nc_varput.
7% Test 002:  pass 1 arguments into nc_varput.
8% Test 003:  pass 2 arguments into nc_varput.
9% Test 004:  bad filename into nc_varput.
10% Test 005:  bad varname into nc_varput.
11% Test 006:  try to write a 2D matrix to a singleton
12% Test 007:  try to write a 2D matrix to a 2D var using 'put_var',
13%            but having the wrong size
14% Test 008:  try to write a 2D matrix to a 2D var using 'put_vara',
15%            but having the wrong size
16% Test 009:  try to write a 2D matrix to a 2D var using 'put_vars',
17%            but having the wrong size
18% Test 010:  try to write a 2D matrix to a 2D var using 'put_vara',
19%            but with too long of a count argument
20% Test 011:  try to write a 2D matrix to a 2D var using 'put_vars',
21%            but with too long of a stride argument
22% Test 012:  try to write a 2D matrix to a 2D var using 'put_vars',
23%            but with too long of a start, count, stride argument
24% Test 013:  try a bad start index
25%           
26%           
27%
28% put_var1
29% Test 100:  write to a singleton variable and read it back.
30% Test 101:  write to a 1D variable with just a count
31% Test 102:  write to a 1D variable with a bad count
32% Test 103:  write to a 1D variable with a good count
33% Test 104:  write to a 1D variable with a bad stride
34% Test 105:  write to a 1D variable with a good stride.
35% Test 106:  write more than 1 datum to a singleton variable.  This should fail.
36% Test 107:  write 1 datum to a singleton variable, bad start.  Should fail.
37% Test 108:  write 1 datum to a singleton variable, bad count.  Should fail.
38% Test 109:  write 1 datum to a singleton variable, give a stride.  Should fail.
39%
40% put_var
41% Test 200:  using put_var, write all the data to a 2D dataset.
42% Test 201:  using put_vara, write a chunk of the data to a 2D dataset.
43% Test 202:  using put_vara, write a chunk of data to a 2D dataset.
44% Test 203:  using put_vars, write a chunk of data to a 2D dataset.
45% Test 204:  write too much to a 2D dataset (using put_var).  Should fail.
46% Test 205:  write too little to a 2D dataset (using put_var).  Should fail.
47% Test 206:  use put_vara, write with a bad offset.  Should fail.
48% Test 207:  use put_vars, write with a bad start.  Should fail.
49% Test 208:  use put_vara, write with a bad count.  Should fail.
50% Test 209:  use put_vars, write with a bad stride.  Should fail.
51%
52% Test 301:  test reading with scale factors, add offsets.
53% Test 302:  test writing with scale factors, add offsets.
54% Test 303:  test reading with scale factor, no add offset.
55% Test 304:  test writing/reading with _FillValue
56% Test 305:  test reading with missing_value
57% Test 306:  test reading with floating point scale factor
58% Test 307:  test with _FillValue and missing_value
59%
60%
61
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63%
64% $Id: test_nc_varput.m 2559 2008-11-28 21:53:27Z johnevans007 $
65% $LastChangedDate: 2008-11-28 16:53:27 -0500 (Fri, 28 Nov 2008) $
66% $LastChangedRevision: 2559 $
67% $LastChangedBy: johnevans007 $
68%
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70
71fprintf ( 1, 'NC_VARGET, NC_VARPUT:  starting test suite...\n' );
72if nargin == 0
73        ncfile = 'foo.nc';
74end
75
76
77create_test_file ( ncfile );
78
79test_001 ( 'testdata/empty.nc' );
80test_002 ( 'testdata/empty.nc' );
81test_003 ( 'testdata/empty.nc' );
82test_004 ( 'i_do_not_exist.nc' );
83test_005 ( ncfile );
84test_006 ( ncfile );
85test_007 ( ncfile );
86test_008 ( ncfile );
87test_009 ( ncfile );
88test_010 ( ncfile );
89test_011 ( ncfile );
90
91test_100 ( ncfile );
92test_101 ( ncfile );
93test_102 ( ncfile );
94test_103 ( ncfile );
95test_104 ( ncfile );
96test_105 ( ncfile );
97
98test_106 ( ncfile );
99test_107 ( ncfile );
100test_108 ( ncfile );
101test_109 ( ncfile );
102
103
104test_200 ( ncfile );
105test_201 ( ncfile );
106test_202 ( ncfile );
107test_203 ( ncfile );
108
109test_204 ( ncfile );
110test_205 ( ncfile );
111test_206 ( ncfile );
112
113test_207 ( ncfile );
114test_208 ( ncfile );
115test_209 ( ncfile );
116
117
118test_301 ( ncfile );
119test_302 ( ncfile );
120test_303 ( ncfile );
121test_304 ( ncfile );
122test_305 ( ncfile );
123test_306 ( ncfile );
124test_307 ( ncfile );
125
126return
127
128
129
130
131
132function create_test_file ( ncfile, arg2 )
133
134if snctools_use_tmw
135    %
136    % ok, first create the first file
137    ncid_1 = netcdf.create(ncfile, nc_clobber_mode );
138   
139   
140    %
141    % Create a fixed dimension. 
142    len_x = 4;
143    xdimid = netcdf.defDim(ncid_1, 'x', len_x );
144   
145    %
146    % Create a fixed dimension. 
147    len_y = 6;
148    ydimid = netcdf.defDim(ncid_1, 'y', len_y );
149   
150    netcdf.close(ncid_1);
151elseif snctools_use_mexnc
152    %
153    % ok, first create the first file
154    [ncid_1, status] = mexnc ( 'create', ncfile, nc_clobber_mode );
155    if ( status ~= 0 )
156        ncerr_msg = mexnc ( 'strerror', status );
157        msg = sprintf ( '%s:  ''create'' failed, error message '' %s ''\n', mfilename, ncerr_msg );
158        error ( msg );
159    end
160   
161   
162    %
163    % Create a fixed dimension. 
164    len_x = 4;
165    [xdimid, status] = mexnc ( 'def_dim', ncid_1, 'x', len_x );
166    if ( status ~= 0 )
167        ncerr_msg = mexnc ( 'strerror', status );
168        msg = sprintf ( '%s:  ''def_dim'' failed on dim x, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
169        error ( msg );
170    end
171   
172    %
173    % Create a fixed dimension. 
174    len_y = 6;
175    [ydimid, status] = mexnc ( 'def_dim', ncid_1, 'y', len_y );
176    if ( status ~= 0 )
177        ncerr_msg = mexnc ( 'strerror', status );
178        msg = sprintf ( '%s:  ''def_dim'' failed on dim y, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
179        error ( msg );
180    end
181   
182   
183    %
184    % CLOSE
185    status = mexnc ( 'close', ncid_1 );
186    if ( status ~= 0 )
187        error ( 'CLOSE failed' );
188    end
189else
190        error('No mexnc or native matlab support, this test cannot be run.');
191end
192
193%
194% Add a singleton
195varstruct.Name = 'test_singleton';
196varstruct.Nctype = 'double';
197varstruct.Dimension = [];
198
199nc_addvar ( ncfile, varstruct );
200
201
202clear varstruct;
203varstruct.Name = 'test_1D';
204varstruct.Nctype = 'double';
205varstruct.Dimension = { 'y' };
206
207nc_addvar ( ncfile, varstruct );
208
209
210clear varstruct;
211varstruct.Name = 'test_2D';
212varstruct.Nctype = 'double';
213if getpref('SNCTOOLS','PRESERVE_FVD',false)
214    varstruct.Dimension = { 'x', 'y' };
215else
216    varstruct.Dimension = { 'y', 'x' };
217end
218
219nc_addvar ( ncfile, varstruct );
220
221
222clear varstruct;
223varstruct.Name = 'test_2D_float';
224varstruct.Nctype = 'float';
225if getpref('SNCTOOLS','PRESERVE_FVD',false)
226    varstruct.Dimension = { 'x', 'y' };
227else
228    varstruct.Dimension = { 'y', 'x' };
229end
230
231nc_addvar ( ncfile, varstruct );
232
233
234clear varstruct;
235varstruct.Name = 'test_var3';
236varstruct.Nctype = 'double';
237varstruct.Dimension = { 'x' };
238
239nc_addvar ( ncfile, varstruct );
240return
241
242
243
244
245
246
247
248
249
250
251
252
253function test_001 ( ncfile )
254
255try
256    nc_varput;
257    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
258    error ( msg );
259end
260
261return
262
263
264
265function test_002 ( ncfile )
266try
267    nc_varput ( ncfile );
268    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
269    error ( msg );
270end
271return
272
273
274function test_003 ( ncfile )
275
276try
277    nc_varput ( ncfile, 'test_2d' );
278    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
279    error ( msg );
280end
281
282return
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302function test_106 ( ncfile )
303
304input_data = [3.14159; 2];
305nc_varput ( ncfile, 'test_1D', input_data, 0, 2, 2 );
306output_data = nc_varget ( ncfile, 'test_1D', 0, 2, 2 );
307
308ddiff = abs(input_data - output_data);
309if any( find(ddiff > eps) )
310    msg = sprintf ( '%s:  input data ~= output data.\n', mfilename );
311    error ( msg );
312end
313
314return
315
316
317
318
319function test_004 ( ncfile )
320
321try
322    nc_varput ( ncfile, 'test_2d' );
323    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
324    error ( msg );
325end
326
327return
328
329
330
331
332
333
334function test_005 ( ncfile )
335
336try
337    nc_varput ( ncfile, 'bad', 5 );
338    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
339    error ( msg );
340end
341
342return
343
344
345
346
347
348
349
350function test_006 ( ncfile )
351
352try
353    nc_varput ( ncfile, 'test_singleton', [2 1] );
354    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
355    error ( msg );
356end
357
358return
359
360
361
362
363
364
365
366function test_007 ( ncfile )
367
368try
369    nc_varput ( ncfile, 'test_2D', ones(7,4) );
370    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
371    error ( msg );
372end
373
374return
375
376
377
378
379
380
381function test_008 ( ncfile )
382
383try
384    nc_varput ( ncfile, 'test_2D', ones(3,4), [0 0], [3 3] );
385    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
386    error ( msg );
387end
388
389return
390
391
392
393
394
395
396function test_009 ( ncfile )
397
398try
399    nc_varput ( ncfile, 'test_2D', ones(3,2), [0 0], [3 2], [2 2] );
400    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
401    error ( msg );
402end
403
404return
405
406
407
408
409
410
411function test_010 ( ncfile )
412
413try
414    nc_varput ( ncfile, 'test_2D', ones(6,4), [0 0], [6 4 1] );
415    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
416    error ( msg );
417end
418
419return
420
421
422
423
424
425
426function test_011 ( ncfile )
427
428try
429    nc_varput ( ncfile, 'test_2D', ones(3,2), [0 0], [3 2], [2 2 1] );
430    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
431    error ( msg );
432end
433
434return
435
436
437
438
439
440
441function test_012 ( ncfile )
442
443try
444    nc_varput ( ncfile, 'test_2D', ones(3,2), [0 0 0], [3 2 1], [2 2 1] );
445    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
446    error ( msg );
447end
448
449return
450
451
452
453
454
455function test_013 ( ncfile )
456
457try
458    nc_varput ( ncfile, 'test_2D', ones(6,4), [1 0], [6 4] );
459    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
460    error ( msg );
461end
462
463return
464
465
466
467
468
469
470function test_021 ( ncfile )
471
472try
473    nc_varput ( ncfile, 'test_2D', ones(3,2), [0 0 0], [3 2], [2 2 1] );
474    msg = sprintf ( '%s:  test failed, ''%s''.\n', mfilename, lasterr );
475    error ( msg );
476end
477
478return
479
480
481
482
483
484
485
486function test_100 ( ncfile )
487
488
489input_data = 3.14159;
490nc_varput ( ncfile, 'test_singleton', input_data );
491output_data = nc_varget ( ncfile, 'test_singleton' );
492
493ddiff = abs(input_data - output_data);
494if any( find(ddiff > eps) )
495    msg = sprintf ( '%s:  input data ~= output data.\n', mfilename );
496    error ( msg );
497end
498
499return
500
501
502
503
504function test_101 ( ncfile )
505
506input_data = 3.14159;
507try
508    nc_varput ( ncfile, 'test_1D', input_data, 8 );
509    msg = sprintf ( '%s:  nc_varput succeeded when it should have failed.\n', mfilename );
510    error ( msg );
511end
512
513return
514
515
516
517
518function test_102 ( ncfile )
519
520input_data = 3.14159;
521try
522    nc_varput ( ncfile, 'test_1D', input_data, 4, 2 );
523    msg = sprintf ( '%s:  nc_varput succeeded in when it should have failed.\n', mfilename );
524    error ( msg );
525end
526
527return
528
529
530
531
532
533
534
535function test_103 ( ncfile )
536
537input_data = 3.14159;
538nc_varput ( ncfile, 'test_1D', input_data, 0, 1 );
539output_data = nc_varget ( ncfile, 'test_1D', 0, 1 );
540
541ddiff = abs(input_data - output_data);
542if any( find(ddiff > eps) )
543    msg = sprintf ( '%s:  input data ~= output data.\n', mfilename );
544    error ( msg );
545end
546
547return
548
549
550
551
552function test_104 ( ncfile )
553
554input_data = [3.14159; 2];
555try
556    nc_varput ( ncfile, 'test_1D', input_data, 0, 2, 8 );
557    msg = sprintf ( '%s:  nc_varput succeeded when it should have failed.\n', mfilename );
558    error ( msg );
559end
560
561
562return
563
564
565
566
567
568function test_105 ( ncfile )
569
570input_data = [3.14159 2];
571try
572    nc_varput ( ncfile, 'test_singleton', input_data );
573    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
574    error ( msg );
575end
576
577return
578
579
580
581
582
583
584
585function test_107 ( ncfile )
586
587input_data = 3.14159;
588try
589    nc_varput ( ncfile, 'test_singleton', input_data, 4, 1 );
590    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
591    error ( msg );
592end
593
594return
595
596
597
598
599
600function test_108 ( ncfile )
601
602input_data = 3.14159;
603try
604    nc_varput ( ncfile, 'test_singleton', input_data, 0, 2 );
605    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
606    error ( msg );
607end
608
609return
610
611
612
613
614
615
616
617
618function test_109 ( ncfile )
619
620input_data = 3.14159;
621try
622    nc_varput ( ncfile, 'test_singleton', input_data, 0, 1, 1 );
623    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
624    error ( msg );
625end
626
627return
628
629
630
631
632
633
634
635function test_200 ( ncfile )
636
637input_data = [1:24];
638
639count = nc_varsize(ncfile,'test_2D');
640input_data = reshape(input_data,count);
641nc_varput ( ncfile, 'test_2D', input_data );
642output_data = nc_varget ( ncfile, 'test_2D' );
643
644ddiff = abs(input_data - output_data);
645if any( find(ddiff > eps) )
646    msg = sprintf ( '%s:  input data ~= output data \n', mfilename );
647    error ( msg );
648end
649
650return
651
652
653
654
655
656
657
658function test_201 ( ncfile )
659
660sz = nc_varsize(ncfile,'test_2D');
661start = [0 0];
662count = sz-1;
663
664input_data = [1:prod(count)];
665
666input_data = reshape(input_data,count);
667nc_varput ( ncfile, 'test_2D', input_data, start, count );
668output_data = nc_varget ( ncfile, 'test_2D', start, count );
669
670ddiff = abs(input_data - output_data);
671if any( find(ddiff > eps) )
672    msg = sprintf ( '%s:  input data ~= output data .\n', mfilename );
673    error ( msg );
674end
675
676return
677
678
679
680
681
682function test_202 ( ncfile )
683
684sz = nc_varsize(ncfile,'test_2D');
685start = [1 1];
686count = sz-1;
687
688input_data = [1:prod(count)] - 5;
689input_data = reshape(input_data,count);
690
691nc_varput ( ncfile, 'test_2D', input_data, start, count );
692output_data = nc_varget ( ncfile, 'test_2D', start, count );
693
694ddiff = abs(input_data - output_data);
695if any( find(ddiff > eps) )
696    msg = sprintf ( '%s:  input data ~= output data .\n', mfilename );
697    error ( msg );
698end
699
700
701return
702
703
704
705
706
707
708
709
710
711
712
713
714function test_203 ( ncfile )
715
716sz = nc_varsize(ncfile,'test_2D');
717start = [0 0];
718count = sz/2;
719stride = [2 2];
720
721input_data = [1:prod(count)];
722
723input_data = reshape(input_data,count);
724nc_varput ( ncfile, 'test_2D', input_data, start, count, stride );
725output_data = nc_varget ( ncfile, 'test_2D', start, count, stride );
726
727ddiff = abs(input_data - output_data);
728if any( find(ddiff > eps) )
729    msg = sprintf ( '%s:  input data ~= output data.\n', mfilename );
730    error ( msg );
731end
732
733return
734
735
736
737
738
739
740
741
742function test_204 ( ncfile )
743
744input_data = [1:49];
745input_data = reshape(input_data,7,7);
746try
747    nc_varput ( ncfile, 'test_2D', input_data );
748    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
749    error ( msg );
750end
751
752
753return
754
755
756
757
758
759
760function test_205 ( ncfile )
761
762sz = nc_varsize(ncfile,'test_2D');
763start = [0 0];
764count = sz-1;
765
766input_data = [1:prod(count)];
767input_data = reshape(input_data,count);
768try
769    nc_varput ( ncfile, 'test_2D', input_data );
770    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
771    error ( msg );
772end
773
774return
775
776
777
778
779
780
781function test_206 ( ncfile )
782% write with a bad offset
783
784sz = nc_varsize(ncfile,'test_2D');
785start = [1 1];
786count = sz;
787
788input_data = [1:prod(count)];
789input_data = reshape(input_data,count);
790try
791    nc_varput ( ncfile, 'test_2D', input_data, start, count );
792    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
793    error ( msg );
794end
795return
796
797
798
799
800
801
802
803
804function test_207 ( ncfile )
805% write using put_vars with a bad offset
806
807sz = nc_varsize(ncfile,'test_2D');
808start = [1 1];
809count = sz/2;
810stride = [2 2];
811
812input_data = [1:prod(count)] + 3.14159;
813input_data = reshape(input_data,count);
814
815try
816    nc_varput ( ncfile, 'test_2D', input_data, start, count, stride);
817    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
818    error ( msg );
819end
820return
821
822
823
824
825
826
827function test_208 ( ncfile )
828% vara with bad count
829sz = nc_varsize(ncfile,'test_2D');
830start = [0 0];
831count = sz+1;
832
833input_data = [1:prod(count)] + 3.14159;
834input_data = reshape(input_data,count);
835try
836    nc_varput ( ncfile, 'test_2D', input_data, start, count );
837    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
838    error ( msg );
839end
840return
841
842
843
844
845
846
847function test_209 ( ncfile )
848
849sz = nc_varsize(ncfile,'test_2D');
850start = [0 0];
851count = sz/2;
852stride = [3 3];
853
854input_data = [1:prod(count)] + 3.14159;
855input_data = reshape(input_data,count);
856try
857    nc_varput ( ncfile, 'test_2D', input_data, start, count, stride);
858    msg = sprintf ( '%s:  nc_varput succeeded when it should not have.\n', mfilename );
859    error ( msg );
860end
861return
862
863
864
865
866
867function test_301 ( ncfile )
868
869%
870% Write some data, then put a scale factor of 2 and add offset of 1.  The
871% data read back should be twice as large plus 1.
872create_test_file ( ncfile );
873
874sz = nc_varsize(ncfile,'test_2D');
875start = [0 0];
876count = sz;
877input_data = [1:prod(count)];
878input_data = reshape(input_data,count);
879
880nc_varput ( ncfile, 'test_2D', input_data );
881nc_attput ( ncfile, 'test_2D', 'scale_factor', 2.0 );
882nc_attput ( ncfile, 'test_2D', 'add_offset', 1.0 );
883output_data = nc_varget ( ncfile, 'test_2D' );
884
885ddiff = abs(input_data - (output_data-1)/2);
886if any( find(ddiff > eps) )
887    msg = sprintf ( '%s:  input data ~= output data in Test 13.\n', mfilename );
888    error ( msg );
889end
890return
891
892
893
894
895
896function test_302 ( ncfile )
897%
898% Put a scale factor of 2 and add offset of 1.
899% Write some data,
900% Put a scale factor of 4 and add offset of 2.
901% data read back should be twice as large
902create_test_file ( ncfile );
903
904sz = nc_varsize(ncfile,'test_2D');
905start = [0 0];
906count = sz;
907input_data = [1:prod(count)];
908input_data = reshape(input_data,count);
909
910
911nc_attput ( ncfile, 'test_2D', 'scale_factor', 2.0 );
912nc_attput ( ncfile, 'test_2D', 'add_offset', 1.0 );
913nc_varput ( ncfile, 'test_2D', input_data );
914nc_attput ( ncfile, 'test_2D', 'scale_factor', 4.0 );
915nc_attput ( ncfile, 'test_2D', 'add_offset', 2.0 );
916output_data = nc_varget ( ncfile, 'test_2D' );
917ddiff = abs(input_data - (output_data)/2);
918if any( find(ddiff > eps) )
919    msg = sprintf ( '%s:  input data ~= output data .\n', mfilename );
920    error ( msg );
921end
922return
923
924
925
926
927
928
929
930
931
932function test_303 ( ncfile )
933%
934% Put a scale factor of 2 and no add offset.
935% Write some data. 
936create_test_file ( ncfile );
937
938sz = nc_varsize(ncfile,'test_2D');
939start = [0 0];
940count = sz;
941input_data = [1:prod(count)];
942input_data = reshape(input_data,count);
943
944
945nc_attput ( ncfile, 'test_2D', 'scale_factor', 2.0 );
946nc_varput ( ncfile, 'test_2D', input_data );
947
948%
949% Now change the scale_factor, doubling it.
950nc_attput ( ncfile, 'test_2D', 'scale_factor', 4.0 );
951output_data = nc_varget ( ncfile, 'test_2D' );
952
953if output_data(1) ~= 2
954    msg = sprintf ( '%s:  input data ~= output data .\n', mfilename );
955    error ( msg );
956end
957return
958
959
960
961
962
963
964
965
966function test_304 ( ncfile )
967
968create_test_file ( ncfile );
969
970sz = nc_varsize(ncfile,'test_2D');
971start = [0 0];
972count = sz;
973input_data = [1:prod(count)];
974input_data = reshape(input_data,count);
975
976
977input_data(1,1) = NaN;
978
979nc_attput ( ncfile, 'test_2D', '_FillValue', -1 );
980nc_varput ( ncfile, 'test_2D', input_data );
981
982%
983% Now change the _FillValue, to -2. 
984nc_attput ( ncfile, 'test_2D', '_FillValue', -2 );
985
986%
987% Now read the data back.  Should have a -1 in position (1,1).
988output_data = nc_varget ( ncfile, 'test_2D' );
989
990if output_data(1) ~= -1
991    msg = sprintf ( '%s:  input data ~= output data .\n', mfilename );
992    error ( msg );
993end
994return
995
996
997
998
999
1000
1001
1002
1003
1004function test_305 ( ncfile )
1005
1006create_test_file ( ncfile );
1007
1008sz = nc_varsize(ncfile,'test_2D');
1009start = [0 0];
1010count = sz;
1011input_data = [1:prod(count)];
1012input_data = reshape(input_data,count);
1013
1014
1015input_data(1,1) = NaN;
1016
1017nc_attput ( ncfile, 'test_2D', 'missing_value', -1 );
1018nc_varput ( ncfile, 'test_2D', input_data );
1019
1020%
1021% Now change the _FillValue, to -2. 
1022nc_attput ( ncfile, 'test_2D', '_FillValue', -2 );
1023
1024%
1025% Now read the data back.  Should have a NaN in position (1,1).
1026output_data = nc_varget ( ncfile, 'test_2D' );
1027
1028if ~isnan(output_data(1,1))
1029    msg = sprintf ( '%s:  output data is not correct.\n', mfilename );
1030    error ( msg );
1031end
1032return
1033
1034
1035
1036
1037
1038
1039% Read from a single precision dataset with a single precision scale factor.
1040% Should still produce single precision.
1041function test_306 ( ncfile )
1042
1043%
1044% Write some data, then put a scale factor of 2 and add offset of 1.  The
1045% data read back should be twice as large plus 1.
1046create_test_file ( ncfile );
1047
1048sz = nc_varsize(ncfile,'test_2D');
1049start = [0 0];
1050count = sz;
1051input_data = rand(1,prod(count));
1052input_data = reshape(input_data,count);
1053
1054
1055scale_factor = single(0.5);
1056add_offset = single(1.0);
1057nc_attput ( ncfile, 'test_2D_float', 'scale_factor', scale_factor );
1058nc_attput ( ncfile, 'test_2D_float', 'add_offset', add_offset );
1059nc_varput ( ncfile, 'test_2D_float', input_data );
1060output_data = nc_varget ( ncfile, 'test_2D_float' );
1061
1062ddiff = abs(input_data - output_data);
1063if any( find(ddiff > 1e-6) )
1064    msg = sprintf ( 'input data ~= output data.\n' );
1065    error ( msg );
1066end
1067
1068return
1069
1070
1071%
1072% Test a fill value / missing value conflict.  The fill value should take precedence.
1073function test_307 ( ncfile )
1074
1075create_test_file ( ncfile );
1076
1077sz = nc_varsize(ncfile,'test_2D');
1078start = [0 0];
1079count = sz;
1080input_data = [1:prod(count)];
1081input_data = reshape(input_data,count);
1082
1083
1084input_data(1,1) = NaN;
1085
1086nc_attput ( ncfile, 'test_2D', '_FillValue', -1 );
1087nc_attput ( ncfile, 'test_2D', 'missing_value', -1 );
1088nc_varput ( ncfile, 'test_2D', input_data );
1089
1090
1091%
1092% Now read the data back.  Should have a NaN in position (1,1).
1093output_data = nc_varget ( ncfile, 'test_2D' );
1094
1095if ~isnan(output_data(1,1))
1096    msg = sprintf ( '%s:  output data is not correct.\n', mfilename );
1097    error ( msg );
1098end
1099return
1100
1101
1102
1103
1104
1105
Note: See TracBrowser for help on using the repository browser.