PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/other_authors/netcdf/snctools/tests/test_nc_varput.m

http://m--pack.googlecode.com/
MATLAB | 634 lines | 402 code | 111 blank | 121 comment | 56 complexity | 7c80fb084954baa527369c0c4739e685 MD5 | raw file
  1. function test_nc_varput ( ncfile )
  2. % TEST_NC_VARPUT:
  3. %
  4. % Relies upon nc_attput
  5. %
  6. % If all works as expected, you should NOT see any message like
  7. %
  8. % ??? Error using ==> test_nc_varput
  9. %
  10. % If you do, because an exception was thrown, it will be at the
  11. % end of the output, so you don't have to go scrolling back thru
  12. % all of it.
  13. %
  14. % Generic Tests
  15. % Test a: pass 0 arguments.
  16. % Test b: pass 1 arguments.
  17. % Test c: pass 2 arguments.
  18. % Test d: bad filename
  19. % Test e: bad varname
  20. %
  21. % put_var1
  22. % Test 1: write to a singleton variable and read it back.
  23. % Test 1a: write to a 1D variable with 3 input args.
  24. % Test 1b: write to a 1D variable with a bad start
  25. % Test 1c: write to a 1D variable with a bad count
  26. % Test 1d: write to a 1D variable with a good count
  27. % Test 1e: write to a 1D variable with a bad stride
  28. % Test 1f: write to a 1D variable with a good stride.
  29. % Test 2: write more than 1 datum to a singleton variable. This should fail.
  30. % Test 3: write 1 datum to a singleton variable, bad start. Should fail.
  31. % Test 4: write 1 datum to a singleton variable, bad count. Should fail.
  32. % Test 5: write 1 datum to a singleton variable, give a stride. Should fail.
  33. %
  34. % put_var
  35. % Test 6: using put_var, write all the data to a 2D dataset.
  36. % Test 6a: using put_vara, write a chunk of the data to a 2D dataset.
  37. % Test 6b: using put_vara, write a chunk of data to a 2D dataset.
  38. % Test 6c: using put_vars, write a chunk of data to a 2D dataset.
  39. % Test 7: write too much to a 2D dataset (using put_var). Should fail.
  40. % Test 8: write too little to a 2D dataset (using put_var). Should fail.
  41. % Test 9: use put_vara, write with a bad offset. Should fail.
  42. % Test 10: use put_vars, write with a bad start. Should fail.
  43. % Test 11: use put_vara, write with a bad count. Should fail.
  44. % Test 12: use put_vars, write with a bad stride. Should fail.
  45. %
  46. % Test 13: test reading with scale factors, add offsets.
  47. % Test 14: test writing with scale factors, add offsets.
  48. %
  49. %
  50. %!ncgen -o test.nc test.cdl
  51. create_test_file ( ncfile );
  52. %
  53. % Test a
  54. disp ( 'Test a' );
  55. status = nc_varput;
  56. if ( status >= 0 )
  57. msg = sprintf ( '%s: nc_varput succeeded in Test a when it should not have.\n', mfilename );
  58. error ( msg );
  59. end
  60. disp ( 'Test a succeeded' );
  61. %
  62. % Test b
  63. disp ( 'Test b' );
  64. status = nc_varput ( ncfile );
  65. if ( status >= 0 )
  66. msg = sprintf ( '%s: nc_varput succeeded in Test b when it should not have.\n', mfilename );
  67. error ( msg );
  68. end
  69. disp ( 'Test b succeeded' );
  70. %
  71. % Test c
  72. disp ( 'Test c' );
  73. status = nc_varput ( ncfile, 'test_2d' );
  74. if ( status >= 0 )
  75. msg = sprintf ( '%s: nc_varput succeeded in Test c when it should not have.\n', mfilename );
  76. error ( msg );
  77. end
  78. disp ( 'Test c succeeded' );
  79. %
  80. % Test d
  81. disp ( 'Test d' );
  82. status = nc_varput ( 'bad.nc', 'test_2d' );
  83. if ( status >= 0 )
  84. msg = sprintf ( '%s: nc_varput succeeded in Test d when it should not have.\n', mfilename );
  85. error ( msg );
  86. end
  87. disp ( 'Test d succeeded' );
  88. %
  89. % Test e
  90. disp ( 'Test e' );
  91. status = nc_varput ( ncfile, 'bad', 5 );
  92. if ( status >= 0 )
  93. msg = sprintf ( '%s: nc_varput succeeded in Test d when it should not have.\n', mfilename );
  94. error ( msg );
  95. end
  96. disp ( 'Test e succeeded' );
  97. %
  98. % Test1
  99. disp ( 'Test 1' );
  100. input_data = 3.14159;
  101. status = nc_varput ( ncfile, 'test_singleton', input_data );
  102. if ( status < 0 )
  103. msg = sprintf ( '%s: nc_varput failed in Test 1.\n', mfilename );
  104. error ( msg );
  105. end
  106. [output_data, status] = nc_varget ( ncfile, 'test_singleton' );
  107. if ( status < 0 )
  108. msg = sprintf ( '%s: nc_varput failed in Test 1.\n', mfilename );
  109. error ( msg );
  110. end
  111. ddiff = abs(input_data - output_data);
  112. if any( find(ddiff > eps) )
  113. msg = sprintf ( '%s: input data ~= output data in Test 1.\n', mfilename );
  114. error ( msg );
  115. end
  116. disp ( 'Test 1 succeeded' );
  117. %
  118. % Test1a
  119. disp ( 'Test 1a' );
  120. input_data = 3.14159;
  121. status = nc_varput ( ncfile, 'test_1D', input_data );
  122. if ( status >= 0 )
  123. msg = sprintf ( '%s: nc_varput passed Test 1a when it should have failed.\n', mfilename );
  124. error ( msg );
  125. end
  126. disp ( 'Test 1a succeeded' );
  127. %
  128. % Test1b
  129. disp ( 'Test 1b' );
  130. input_data = 3.14159;
  131. status = nc_varput ( ncfile, 'test_1D', input_data, 8 );
  132. if ( status >= 0 )
  133. msg = sprintf ( '%s: nc_varput succeeded in Test 1b when it should have failed.\n', mfilename );
  134. error ( msg );
  135. end
  136. disp ( 'Test 1b succeeded' );
  137. %
  138. % Test1c
  139. disp ( 'Test 1c' );
  140. input_data = 3.14159;
  141. status = nc_varput ( ncfile, 'test_1D', input_data, 4, 2 );
  142. if ( status >= 0 )
  143. msg = sprintf ( '%s: nc_varput succeeded in Test 1c when it should have failed.\n', mfilename );
  144. error ( msg );
  145. end
  146. disp ( 'Test 1c succeeded' );
  147. %
  148. % Test1d
  149. disp ( 'Test 1d' );
  150. input_data = 3.14159;
  151. status = nc_varput ( ncfile, 'test_1D', input_data, 0, 1 );
  152. if ( status < 0 )
  153. msg = sprintf ( '%s: nc_varput failed in Test 1d.\n', mfilename );
  154. error ( msg );
  155. end
  156. [output_data, status] = nc_varget ( ncfile, 'test_1D', 0, 1 );
  157. if ( status < 0 )
  158. msg = sprintf ( '%s: nc_varput failed in Test 1a.\n', mfilename );
  159. error ( msg );
  160. end
  161. ddiff = abs(input_data - output_data);
  162. if any( find(ddiff > eps) )
  163. msg = sprintf ( '%s: input data ~= output data in Test 1d.\n', mfilename );
  164. error ( msg );
  165. end
  166. disp ( 'Test 1d succeeded' );
  167. %
  168. % Test1e
  169. disp ( 'Test 1e' );
  170. input_data = [3.14159; 2];
  171. status = nc_varput ( ncfile, 'test_1D', input_data, 0, 2, 8 );
  172. if ( status >= 0 )
  173. msg = sprintf ( '%s: nc_varput succeeded in Test 1e when it should have failed.\n', mfilename );
  174. error ( msg );
  175. end
  176. disp ( 'Test 1e succeeded' );
  177. %
  178. % Test1f
  179. disp ( 'Test 1f' );
  180. input_data = [3.14159; 2];
  181. status = nc_varput ( ncfile, 'test_1D', input_data, 0, 2, 2 );
  182. if ( status < 0 )
  183. msg = sprintf ( '%s: nc_varput failed in Test 1f.\n', mfilename );
  184. error ( msg );
  185. end
  186. [output_data, status] = nc_varget ( ncfile, 'test_1D', 0, 2, 2 );
  187. if ( status < 0 )
  188. msg = sprintf ( '%s: nc_varput failed in Test 1f.\n', mfilename );
  189. error ( msg );
  190. end
  191. ddiff = abs(input_data - output_data);
  192. if any( find(ddiff > eps) )
  193. msg = sprintf ( '%s: input data ~= output data in Test 1f.\n', mfilename );
  194. error ( msg );
  195. end
  196. disp ( 'Test 1f succeeded' );
  197. %
  198. % Test2
  199. disp ( 'Test 2' );
  200. input_data = [3.14159 2];
  201. status = nc_varput ( ncfile, 'test_singleton', input_data );
  202. if ( status >= 0 )
  203. msg = sprintf ( '%s: nc_varput succeeded in Test 2 when it should not have.\n', mfilename );
  204. error ( msg );
  205. end
  206. disp ( 'Test 2 succeeded' );
  207. %
  208. % Test3
  209. disp ( 'Test 3' );
  210. input_data = 3.14159;
  211. status = nc_varput ( ncfile, 'test_singleton', input_data, 4, 1 );
  212. if ( status >= 0 )
  213. msg = sprintf ( '%s: nc_varput succeeded in Test 3 when it should not have.\n', mfilename );
  214. error ( msg );
  215. end
  216. disp ( 'Test 3 succeeded' );
  217. %
  218. % Test4
  219. disp ( 'Test 4' );
  220. input_data = 3.14159;
  221. status = nc_varput ( ncfile, 'test_singleton', input_data, 0, 2 );
  222. if ( status >= 0 )
  223. msg = sprintf ( '%s: nc_varput succeeded in Test 4 when it should not have.\n', mfilename );
  224. error ( msg );
  225. end
  226. disp ( 'Test 4 succeeded' );
  227. %
  228. % Test5
  229. disp ( 'Test 5' );
  230. input_data = 3.14159;
  231. status = nc_varput ( ncfile, 'test_singleton', input_data, 0, 1, 1 );
  232. if ( status >= 0 )
  233. msg = sprintf ( '%s: nc_varput succeeded in Test 5 when it should not have.\n', mfilename );
  234. error ( msg );
  235. end
  236. disp ( 'Test 5 succeeded' );
  237. %
  238. % Test6
  239. disp ( 'Test 6' );
  240. input_data = [1:24];
  241. input_data = reshape(input_data,6,4);
  242. status = nc_varput ( ncfile, 'test_2D', input_data );
  243. if ( status < 0 )
  244. msg = sprintf ( '%s: nc_varput failed in Test 6.\n', mfilename );
  245. error ( msg );
  246. end
  247. [output_data, status] = nc_varget ( ncfile, 'test_2D' );
  248. if ( status < 0 )
  249. msg = sprintf ( '%s: nc_varput failed in Test 6.\n', mfilename );
  250. error ( msg );
  251. end
  252. ddiff = abs(input_data - output_data);
  253. if any( find(ddiff > eps) )
  254. msg = sprintf ( '%s: input data ~= output data in Test 6.\n', mfilename );
  255. error ( msg );
  256. end
  257. disp ( 'Test 6 succeeded' );
  258. %
  259. % Test6a
  260. disp ( 'Test 6a' );
  261. input_data = [1:20];
  262. input_data = reshape(input_data,5,4);
  263. status = nc_varput ( ncfile, 'test_2D', input_data, [0 0], [5 4] );
  264. if ( status < 0 )
  265. msg = sprintf ( '%s: nc_varput failed in Test 6a.\n', mfilename );
  266. error ( msg );
  267. end
  268. [output_data, status] = nc_varget ( ncfile, 'test_2D', [0 0], [5 4] );
  269. if ( status < 0 )
  270. msg = sprintf ( '%s: nc_varput failed in Test 6a.\n', mfilename );
  271. error ( msg );
  272. end
  273. ddiff = abs(input_data - output_data);
  274. if any( find(ddiff > eps) )
  275. msg = sprintf ( '%s: input data ~= output data in Test 6a.\n', mfilename );
  276. error ( msg );
  277. end
  278. disp ( 'Test 6a succeeded' );
  279. %
  280. % Test6b
  281. disp ( 'Test 6b' );
  282. input_data = [1:20] - 5;
  283. input_data = reshape(input_data,5,4);
  284. status = nc_varput ( ncfile, 'test_2D', input_data, [1 0], [5 4] );
  285. if ( status < 0 )
  286. msg = sprintf ( '%s: nc_varput failed in Test 6b.\n', mfilename );
  287. error ( msg );
  288. end
  289. [output_data, status] = nc_varget ( ncfile, 'test_2D', [1 0], [5 4] );
  290. if ( status < 0 )
  291. msg = sprintf ( '%s: nc_varput failed in Test 6b.\n', mfilename );
  292. error ( msg );
  293. end
  294. ddiff = abs(input_data - output_data);
  295. if any( find(ddiff > eps) )
  296. msg = sprintf ( '%s: input data ~= output data in Test 6b.\n', mfilename );
  297. error ( msg );
  298. end
  299. disp ( 'Test 6b succeeded' );
  300. %
  301. % Test6c
  302. disp ( 'Test 6c' );
  303. input_data = [1:6];
  304. input_data = reshape(input_data,3,2);
  305. status = nc_varput ( ncfile, 'test_2D', input_data, [0 0], [3 2], [2 2] );
  306. if ( status < 0 )
  307. msg = sprintf ( '%s: nc_varput failed in Test 6c.\n', mfilename );
  308. error ( msg );
  309. end
  310. [output_data, status] = nc_varget ( ncfile, 'test_2D', [0 0], [3 2], [2 2] );
  311. if ( status < 0 )
  312. msg = sprintf ( '%s: nc_varput failed in Test 6c.\n', mfilename );
  313. error ( msg );
  314. end
  315. ddiff = abs(input_data - output_data);
  316. if any( find(ddiff > eps) )
  317. msg = sprintf ( '%s: input data ~= output data in Test 6c.\n', mfilename );
  318. error ( msg );
  319. end
  320. disp ( 'Test 6c succeeded' );
  321. %
  322. % Test7
  323. disp ( 'Test 7' );
  324. input_data = [1:28];
  325. input_data = reshape(input_data,7,4);
  326. status = nc_varput ( ncfile, 'test_2D', input_data );
  327. if ( status >= 0 )
  328. msg = sprintf ( '%s: nc_varput succeeded in Test 7 when it should not have.\n', mfilename );
  329. error ( msg );
  330. end
  331. disp ( 'Test 7 succeeded' );
  332. %
  333. % Test8
  334. disp ( 'Test 8' );
  335. input_data = [1:20];
  336. input_data = reshape(input_data,5,4);
  337. status = nc_varput ( ncfile, 'test_2D', input_data );
  338. if ( status >= 0 )
  339. msg = sprintf ( '%s: nc_varput succeeded in Test 8 when it should not have.\n', mfilename );
  340. error ( msg );
  341. end
  342. disp ( 'Test 8 succeeded' );
  343. %
  344. % Test9
  345. disp ( 'Test 9' );
  346. input_data = [1:24];
  347. input_data = reshape(input_data,6,4);
  348. status = nc_varput ( ncfile, 'test_2D', input_data, [1 1], [6 4] );
  349. if ( status >= 0 )
  350. msg = sprintf ( '%s: nc_varput succeeded in Test 9 when it should not have.\n', mfilename );
  351. error ( msg );
  352. end
  353. disp ( 'Test 9 succeeded' );
  354. %
  355. % Test10
  356. disp ( 'Test 10' );
  357. input_data = [1:24] + 3.14159;
  358. input_data = reshape(input_data,6,4);
  359. input_data = input_data(1:2:end,1:2:end);
  360. status = nc_varput ( ncfile, 'test_2D', input_data, [2 2], [3 2], [2 2] );
  361. if ( status >= 0 )
  362. msg = sprintf ( '%s: nc_varput succeeded in Test 10 when it should not have.\n', mfilename );
  363. error ( msg );
  364. end
  365. disp ( 'Test 10 succeeded' );
  366. %
  367. % Test11
  368. disp ( 'Test 11' );
  369. input_data = [1:28] + 3.14159;
  370. input_data = reshape(input_data,7,4);
  371. status = nc_varput ( ncfile, 'test_2D', input_data, [0 0], [7 4] );
  372. if ( status >= 0 )
  373. msg = sprintf ( '%s: nc_varput succeeded in Test 11 when it should not have.\n', mfilename );
  374. error ( msg );
  375. end
  376. disp ( 'Test 11 succeeded' );
  377. %
  378. % Test12
  379. disp ( 'Test 12' );
  380. input_data = [1:24] + 3.14159;
  381. input_data = reshape(input_data,6,4);
  382. input_data = input_data(1:2:end,1:2:end);
  383. status = nc_varput ( ncfile, 'test_2D', input_data, [0 0], [3 2], [3 3] );
  384. if ( status >= 0 )
  385. msg = sprintf ( '%s: nc_varput succeeded in Test 12 when it should not have.\n', mfilename );
  386. error ( msg );
  387. end
  388. disp ( 'Test 12 succeeded' );
  389. %
  390. % Test13
  391. % Write some data, then put a scale factor of 2 and add offset of 1. The
  392. % data read back should be twice as large plus 1.
  393. create_test_file ( ncfile );
  394. disp ( 'Test 13' );
  395. input_data = [1:24];
  396. input_data = reshape(input_data,6,4);
  397. status = nc_varput ( ncfile, 'test_2D', input_data );
  398. if ( status < 0 )
  399. msg = sprintf ( '%s: nc_varput failed in Test 13.\n', mfilename );
  400. error ( msg );
  401. end
  402. status = nc_attput ( ncfile, 'test_2D', 'scale_factor', 2.0 );
  403. if ( status < 0 )
  404. msg = sprintf ( '%s: nc_attput failed in Test 13.\n', mfilename );
  405. error ( msg );
  406. end
  407. status = nc_attput ( ncfile, 'test_2D', 'add_offset', 1.0 );
  408. if ( status < 0 )
  409. msg = sprintf ( '%s: nc_attput failed in Test 13.\n', mfilename );
  410. error ( msg );
  411. end
  412. [output_data, status] = nc_varget ( ncfile, 'test_2D' );
  413. if ( status < 0 )
  414. msg = sprintf ( '%s: nc_varget failed in Test 13.\n', mfilename );
  415. error ( msg );
  416. end
  417. ddiff = abs(input_data - (output_data-1)/2);
  418. if any( find(ddiff > eps) )
  419. msg = sprintf ( '%s: input data ~= output data in Test 13.\n', mfilename );
  420. error ( msg );
  421. end
  422. disp ( 'Test 13 succeeded' );
  423. %
  424. % Test14
  425. % Put a scale factor of 2 and add offset of 1.
  426. % Write some data,
  427. % Put a scale factor of 4 and add offset of 2.
  428. % data read back should be twice as large
  429. create_test_file ( ncfile );
  430. disp ( 'Test 14' );
  431. input_data = [1:24];
  432. input_data = reshape(input_data,6,4);
  433. status = nc_attput ( ncfile, 'test_2D', 'scale_factor', 2.0 );
  434. if ( status < 0 )
  435. msg = sprintf ( '%s: nc_attput failed in Test 14.\n', mfilename );
  436. error ( msg );
  437. end
  438. status = nc_attput ( ncfile, 'test_2D', 'add_offset', 1.0 );
  439. if ( status < 0 )
  440. msg = sprintf ( '%s: nc_attput failed in Test 14.\n', mfilename );
  441. error ( msg );
  442. end
  443. status = nc_varput ( ncfile, 'test_2D', input_data );
  444. if ( status < 0 )
  445. msg = sprintf ( '%s: nc_varput failed in Test 14.\n', mfilename );
  446. error ( msg );
  447. end
  448. status = nc_attput ( ncfile, 'test_2D', 'scale_factor', 4.0 );
  449. if ( status < 0 )
  450. msg = sprintf ( '%s: nc_attput failed in Test l4.\n', mfilename );
  451. error ( msg );
  452. end
  453. status = nc_attput ( ncfile, 'test_2D', 'add_offset', 2.0 );
  454. if ( status < 0 )
  455. msg = sprintf ( '%s: nc_attput failed in Test 14.\n', mfilename );
  456. error ( msg );
  457. end
  458. [output_data, status] = nc_varget ( ncfile, 'test_2D' );
  459. if ( status < 0 )
  460. msg = sprintf ( '%s: nc_varget failed in Test 14.\n', mfilename );
  461. error ( msg );
  462. end
  463. ddiff = abs(input_data - (output_data)/2);
  464. if any( find(ddiff > eps) )
  465. msg = sprintf ( '%s: input data ~= output data in Test 14.\n', mfilename );
  466. error ( msg );
  467. end
  468. disp ( 'Test 14 succeeded' );
  469. function create_test_file ( ncfile, arg2 )
  470. %
  471. % ok, first create the first file
  472. [ncid_1, status] = mexnc ( 'create', ncfile, nc_clobber_mode );
  473. if ( status ~= 0 )
  474. ncerr_msg = mexnc ( 'strerror', status );
  475. msg = sprintf ( '%s: ''create'' failed, error message '' %s ''\n', mfilename, ncerr_msg );
  476. error ( msg );
  477. end
  478. %
  479. % Create a fixed dimension.
  480. len_x = 4;
  481. [xdimid, status] = mexnc ( 'def_dim', ncid_1, 'x', len_x );
  482. if ( status ~= 0 )
  483. ncerr_msg = mexnc ( 'strerror', status );
  484. msg = sprintf ( '%s: ''def_dim'' failed on dim x, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
  485. error ( msg );
  486. end
  487. %
  488. % Create a fixed dimension.
  489. len_y = 6;
  490. [ydimid, status] = mexnc ( 'def_dim', ncid_1, 'y', len_y );
  491. if ( status ~= 0 )
  492. ncerr_msg = mexnc ( 'strerror', status );
  493. msg = sprintf ( '%s: ''def_dim'' failed on dim y, file %s, error message '' %s ''\n', mfilename, ncfile, ncerr_msg );
  494. error ( msg );
  495. end
  496. %
  497. % CLOSE
  498. status = mexnc ( 'close', ncid_1 );
  499. if ( status ~= 0 )
  500. error ( 'CLOSE failed' );
  501. end
  502. %
  503. % Add a singleton
  504. varstruct.Name = 'test_singleton';
  505. varstruct.Nctype = 'double';
  506. varstruct.Dimension = [];
  507. nc_addvar ( ncfile, varstruct );
  508. clear varstruct;
  509. varstruct.Name = 'test_1D';
  510. varstruct.Nctype = 'double';
  511. varstruct.Dimension = { 'y' };
  512. nc_addvar ( ncfile, varstruct );
  513. clear varstruct;
  514. varstruct.Name = 'test_2D';
  515. varstruct.Nctype = 'double';
  516. varstruct.Dimension = { 'y', 'x' };
  517. nc_addvar ( ncfile, varstruct );
  518. clear varstruct;
  519. varstruct.Name = 'test_var3';
  520. varstruct.Nctype = 'double';
  521. varstruct.Dimension = { 'x' };
  522. nc_addvar ( ncfile, varstruct );
  523. return