PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/other/netcdf_write_matrix/src/nc_test/test_put.c

https://github.com/jbeezley/wrf-fire
C | 7354 lines | 6573 code | 423 blank | 358 comment | 1804 complexity | aac14ed06b320f51a19bc66dea284c63 MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /* Do not edit this file. It is produced from the corresponding .m4 source */
  2. /*********************************************************************
  3. * Copyright 1996, UCAR/Unidata
  4. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  5. * $Id: test_put.m4,v 1.25 2005/03/08 03:04:19 ed Exp $
  6. *********************************************************************/
  7. #include "tests.h"
  8. /*
  9. * ensure hash value within range for internal TYPE
  10. */
  11. static
  12. double
  13. hash_text(
  14. const nc_type type,
  15. const int rank,
  16. const size_t *index,
  17. const nct_itype itype)
  18. {
  19. const double min = text_min;
  20. const double max = text_max;
  21. return MAX(min, MIN(max, hash4( type, rank, index, itype)));
  22. }
  23. /*
  24. * ensure hash value within range for internal TYPE
  25. */
  26. static
  27. double
  28. hash_uchar(
  29. const nc_type type,
  30. const int rank,
  31. const size_t *index,
  32. const nct_itype itype)
  33. {
  34. const double min = uchar_min;
  35. const double max = uchar_max;
  36. return MAX(min, MIN(max, hash4( type, rank, index, itype)));
  37. }
  38. /*
  39. * ensure hash value within range for internal TYPE
  40. */
  41. static
  42. double
  43. hash_schar(
  44. const nc_type type,
  45. const int rank,
  46. const size_t *index,
  47. const nct_itype itype)
  48. {
  49. const double min = schar_min;
  50. const double max = schar_max;
  51. return MAX(min, MIN(max, hash4( type, rank, index, itype)));
  52. }
  53. /*
  54. * ensure hash value within range for internal TYPE
  55. */
  56. static
  57. double
  58. hash_short(
  59. const nc_type type,
  60. const int rank,
  61. const size_t *index,
  62. const nct_itype itype)
  63. {
  64. const double min = short_min;
  65. const double max = short_max;
  66. return MAX(min, MIN(max, hash4( type, rank, index, itype)));
  67. }
  68. /*
  69. * ensure hash value within range for internal TYPE
  70. */
  71. static
  72. double
  73. hash_int(
  74. const nc_type type,
  75. const int rank,
  76. const size_t *index,
  77. const nct_itype itype)
  78. {
  79. const double min = int_min;
  80. const double max = int_max;
  81. return MAX(min, MIN(max, hash4( type, rank, index, itype)));
  82. }
  83. /*
  84. * ensure hash value within range for internal TYPE
  85. */
  86. static
  87. double
  88. hash_long(
  89. const nc_type type,
  90. const int rank,
  91. const size_t *index,
  92. const nct_itype itype)
  93. {
  94. const double min = long_min;
  95. const double max = long_max;
  96. return MAX(min, MIN(max, hash4( type, rank, index, itype)));
  97. }
  98. /*
  99. * ensure hash value within range for internal TYPE
  100. */
  101. static
  102. double
  103. hash_float(
  104. const nc_type type,
  105. const int rank,
  106. const size_t *index,
  107. const nct_itype itype)
  108. {
  109. const double min = float_min;
  110. const double max = float_max;
  111. return MAX(min, MIN(max, hash4( type, rank, index, itype)));
  112. }
  113. /*
  114. * ensure hash value within range for internal TYPE
  115. */
  116. static
  117. double
  118. hash_double(
  119. const nc_type type,
  120. const int rank,
  121. const size_t *index,
  122. const nct_itype itype)
  123. {
  124. const double min = double_min;
  125. const double max = double_max;
  126. return MAX(min, MIN(max, hash4( type, rank, index, itype)));
  127. }
  128. /*
  129. * check all vars in file which are (text/numeric) compatible with TYPE
  130. */
  131. static
  132. void
  133. check_vars_text(const char *filename)
  134. {
  135. int ncid; /* netCDF id */
  136. size_t index[MAX_RANK];
  137. int err; /* status */
  138. int d;
  139. int i;
  140. size_t j;
  141. text value;
  142. nc_type datatype;
  143. int ndims;
  144. int dimids[MAX_RANK];
  145. double expect;
  146. char name[NC_MAX_NAME];
  147. size_t length;
  148. int canConvert; /* Both text or both numeric */
  149. int nok = 0; /* count of valid comparisons */
  150. err = nc_open(filename, NC_NOWRITE, &ncid);
  151. IF (err)
  152. error("nc_open: %s", nc_strerror(err));
  153. for (i = 0; i < NVARS; i++) {
  154. canConvert = (var_type[i] == NC_CHAR) == (NCT_TEXT == NCT_TEXT);
  155. if (canConvert) {
  156. err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, NULL);
  157. IF (err)
  158. error("nc_inq_var: %s", nc_strerror(err));
  159. IF (strcmp(name, var_name[i]) != 0)
  160. error("Unexpected var_name");
  161. IF (datatype != var_type[i])
  162. error("Unexpected type");
  163. IF (ndims != var_rank[i])
  164. error("Unexpected rank");
  165. for (j = 0; j < ndims; j++) {
  166. err = nc_inq_dim(ncid, dimids[j], 0, &length);
  167. IF (err)
  168. error("nc_inq_dim: %s", nc_strerror(err));
  169. IF (length != var_shape[i][j])
  170. error("Unexpected shape");
  171. }
  172. for (j = 0; j < var_nels[i]; j++) {
  173. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  174. IF (err)
  175. error("error in toMixedBase 2");
  176. expect = hash4( var_type[i], var_rank[i], index, NCT_TEXT);
  177. err = nc_get_var1_text(ncid, i, index, &value);
  178. if (inRange3(expect,datatype,NCT_TEXT)) {
  179. if (expect >= text_min && expect <= text_max) {
  180. IF (err) {
  181. error("nc_get_var1_text: %s", nc_strerror(err));
  182. } else {
  183. IF (!equal(value,expect,var_type[i],NCT_TEXT)) {
  184. error("Var value read not that expected");
  185. if (verbose) {
  186. error("\n");
  187. error("varid: %d, ", i);
  188. error("var_name: %s, ", var_name[i]);
  189. error("index:");
  190. for (d = 0; d < var_rank[i]; d++)
  191. error(" %d", index[d]);
  192. error(", expect: %g, ", expect);
  193. error("got: %g", (double) value);
  194. }
  195. } else {
  196. ++nok;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. err = nc_close (ncid);
  205. IF (err)
  206. error("nc_close: %s", nc_strerror(err));
  207. print_nok(nok);
  208. }
  209. /*
  210. * check all vars in file which are (text/numeric) compatible with TYPE
  211. */
  212. static
  213. void
  214. check_vars_uchar(const char *filename)
  215. {
  216. int ncid; /* netCDF id */
  217. size_t index[MAX_RANK];
  218. int err; /* status */
  219. int d;
  220. int i;
  221. size_t j;
  222. uchar value;
  223. nc_type datatype;
  224. int ndims;
  225. int dimids[MAX_RANK];
  226. double expect;
  227. char name[NC_MAX_NAME];
  228. size_t length;
  229. int canConvert; /* Both text or both numeric */
  230. int nok = 0; /* count of valid comparisons */
  231. err = nc_open(filename, NC_NOWRITE, &ncid);
  232. IF (err)
  233. error("nc_open: %s", nc_strerror(err));
  234. for (i = 0; i < NVARS; i++) {
  235. canConvert = (var_type[i] == NC_CHAR) == (NCT_UCHAR == NCT_TEXT);
  236. if (canConvert) {
  237. err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, NULL);
  238. IF (err)
  239. error("nc_inq_var: %s", nc_strerror(err));
  240. IF (strcmp(name, var_name[i]) != 0)
  241. error("Unexpected var_name");
  242. IF (datatype != var_type[i])
  243. error("Unexpected type");
  244. IF (ndims != var_rank[i])
  245. error("Unexpected rank");
  246. for (j = 0; j < ndims; j++) {
  247. err = nc_inq_dim(ncid, dimids[j], 0, &length);
  248. IF (err)
  249. error("nc_inq_dim: %s", nc_strerror(err));
  250. IF (length != var_shape[i][j])
  251. error("Unexpected shape");
  252. }
  253. for (j = 0; j < var_nels[i]; j++) {
  254. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  255. IF (err)
  256. error("error in toMixedBase 2");
  257. expect = hash4( var_type[i], var_rank[i], index, NCT_UCHAR);
  258. err = nc_get_var1_uchar(ncid, i, index, &value);
  259. if (inRange3(expect,datatype,NCT_UCHAR)) {
  260. if (expect >= uchar_min && expect <= uchar_max) {
  261. IF (err) {
  262. error("nc_get_var1_uchar: %s", nc_strerror(err));
  263. } else {
  264. IF (!equal(value,expect,var_type[i],NCT_UCHAR)) {
  265. error("Var value read not that expected");
  266. if (verbose) {
  267. error("\n");
  268. error("varid: %d, ", i);
  269. error("var_name: %s, ", var_name[i]);
  270. error("index:");
  271. for (d = 0; d < var_rank[i]; d++)
  272. error(" %d", index[d]);
  273. error(", expect: %g, ", expect);
  274. error("got: %g", (double) value);
  275. }
  276. } else {
  277. ++nok;
  278. }
  279. }
  280. }
  281. }
  282. }
  283. }
  284. }
  285. err = nc_close (ncid);
  286. IF (err)
  287. error("nc_close: %s", nc_strerror(err));
  288. print_nok(nok);
  289. }
  290. /*
  291. * check all vars in file which are (text/numeric) compatible with TYPE
  292. */
  293. static
  294. void
  295. check_vars_schar(const char *filename)
  296. {
  297. int ncid; /* netCDF id */
  298. size_t index[MAX_RANK];
  299. int err; /* status */
  300. int d;
  301. int i;
  302. size_t j;
  303. schar value;
  304. nc_type datatype;
  305. int ndims;
  306. int dimids[MAX_RANK];
  307. double expect;
  308. char name[NC_MAX_NAME];
  309. size_t length;
  310. int canConvert; /* Both text or both numeric */
  311. int nok = 0; /* count of valid comparisons */
  312. err = nc_open(filename, NC_NOWRITE, &ncid);
  313. IF (err)
  314. error("nc_open: %s", nc_strerror(err));
  315. for (i = 0; i < NVARS; i++) {
  316. canConvert = (var_type[i] == NC_CHAR) == (NCT_SCHAR == NCT_TEXT);
  317. if (canConvert) {
  318. err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, NULL);
  319. IF (err)
  320. error("nc_inq_var: %s", nc_strerror(err));
  321. IF (strcmp(name, var_name[i]) != 0)
  322. error("Unexpected var_name");
  323. IF (datatype != var_type[i])
  324. error("Unexpected type");
  325. IF (ndims != var_rank[i])
  326. error("Unexpected rank");
  327. for (j = 0; j < ndims; j++) {
  328. err = nc_inq_dim(ncid, dimids[j], 0, &length);
  329. IF (err)
  330. error("nc_inq_dim: %s", nc_strerror(err));
  331. IF (length != var_shape[i][j])
  332. error("Unexpected shape");
  333. }
  334. for (j = 0; j < var_nels[i]; j++) {
  335. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  336. IF (err)
  337. error("error in toMixedBase 2");
  338. expect = hash4( var_type[i], var_rank[i], index, NCT_SCHAR);
  339. err = nc_get_var1_schar(ncid, i, index, &value);
  340. if (inRange3(expect,datatype,NCT_SCHAR)) {
  341. if (expect >= schar_min && expect <= schar_max) {
  342. IF (err) {
  343. error("nc_get_var1_schar: %s", nc_strerror(err));
  344. } else {
  345. IF (!equal(value,expect,var_type[i],NCT_SCHAR)) {
  346. error("Var value read not that expected");
  347. if (verbose) {
  348. error("\n");
  349. error("varid: %d, ", i);
  350. error("var_name: %s, ", var_name[i]);
  351. error("index:");
  352. for (d = 0; d < var_rank[i]; d++)
  353. error(" %d", index[d]);
  354. error(", expect: %g, ", expect);
  355. error("got: %g", (double) value);
  356. }
  357. } else {
  358. ++nok;
  359. }
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }
  366. err = nc_close (ncid);
  367. IF (err)
  368. error("nc_close: %s", nc_strerror(err));
  369. print_nok(nok);
  370. }
  371. /*
  372. * check all vars in file which are (text/numeric) compatible with TYPE
  373. */
  374. static
  375. void
  376. check_vars_short(const char *filename)
  377. {
  378. int ncid; /* netCDF id */
  379. size_t index[MAX_RANK];
  380. int err; /* status */
  381. int d;
  382. int i;
  383. size_t j;
  384. short value;
  385. nc_type datatype;
  386. int ndims;
  387. int dimids[MAX_RANK];
  388. double expect;
  389. char name[NC_MAX_NAME];
  390. size_t length;
  391. int canConvert; /* Both text or both numeric */
  392. int nok = 0; /* count of valid comparisons */
  393. err = nc_open(filename, NC_NOWRITE, &ncid);
  394. IF (err)
  395. error("nc_open: %s", nc_strerror(err));
  396. for (i = 0; i < NVARS; i++) {
  397. canConvert = (var_type[i] == NC_CHAR) == (NCT_SHORT == NCT_TEXT);
  398. if (canConvert) {
  399. err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, NULL);
  400. IF (err)
  401. error("nc_inq_var: %s", nc_strerror(err));
  402. IF (strcmp(name, var_name[i]) != 0)
  403. error("Unexpected var_name");
  404. IF (datatype != var_type[i])
  405. error("Unexpected type");
  406. IF (ndims != var_rank[i])
  407. error("Unexpected rank");
  408. for (j = 0; j < ndims; j++) {
  409. err = nc_inq_dim(ncid, dimids[j], 0, &length);
  410. IF (err)
  411. error("nc_inq_dim: %s", nc_strerror(err));
  412. IF (length != var_shape[i][j])
  413. error("Unexpected shape");
  414. }
  415. for (j = 0; j < var_nels[i]; j++) {
  416. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  417. IF (err)
  418. error("error in toMixedBase 2");
  419. expect = hash4( var_type[i], var_rank[i], index, NCT_SHORT);
  420. err = nc_get_var1_short(ncid, i, index, &value);
  421. if (inRange3(expect,datatype,NCT_SHORT)) {
  422. if (expect >= short_min && expect <= short_max) {
  423. IF (err) {
  424. error("nc_get_var1_short: %s", nc_strerror(err));
  425. } else {
  426. IF (!equal(value,expect,var_type[i],NCT_SHORT)) {
  427. error("Var value read not that expected");
  428. if (verbose) {
  429. error("\n");
  430. error("varid: %d, ", i);
  431. error("var_name: %s, ", var_name[i]);
  432. error("index:");
  433. for (d = 0; d < var_rank[i]; d++)
  434. error(" %d", index[d]);
  435. error(", expect: %g, ", expect);
  436. error("got: %g", (double) value);
  437. }
  438. } else {
  439. ++nok;
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. }
  447. err = nc_close (ncid);
  448. IF (err)
  449. error("nc_close: %s", nc_strerror(err));
  450. print_nok(nok);
  451. }
  452. /*
  453. * check all vars in file which are (text/numeric) compatible with TYPE
  454. */
  455. static
  456. void
  457. check_vars_int(const char *filename)
  458. {
  459. int ncid; /* netCDF id */
  460. size_t index[MAX_RANK];
  461. int err; /* status */
  462. int d;
  463. int i;
  464. size_t j;
  465. int value;
  466. nc_type datatype;
  467. int ndims;
  468. int dimids[MAX_RANK];
  469. double expect;
  470. char name[NC_MAX_NAME];
  471. size_t length;
  472. int canConvert; /* Both text or both numeric */
  473. int nok = 0; /* count of valid comparisons */
  474. err = nc_open(filename, NC_NOWRITE, &ncid);
  475. IF (err)
  476. error("nc_open: %s", nc_strerror(err));
  477. for (i = 0; i < NVARS; i++) {
  478. canConvert = (var_type[i] == NC_CHAR) == (NCT_INT == NCT_TEXT);
  479. if (canConvert) {
  480. err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, NULL);
  481. IF (err)
  482. error("nc_inq_var: %s", nc_strerror(err));
  483. IF (strcmp(name, var_name[i]) != 0)
  484. error("Unexpected var_name");
  485. IF (datatype != var_type[i])
  486. error("Unexpected type");
  487. IF (ndims != var_rank[i])
  488. error("Unexpected rank");
  489. for (j = 0; j < ndims; j++) {
  490. err = nc_inq_dim(ncid, dimids[j], 0, &length);
  491. IF (err)
  492. error("nc_inq_dim: %s", nc_strerror(err));
  493. IF (length != var_shape[i][j])
  494. error("Unexpected shape");
  495. }
  496. for (j = 0; j < var_nels[i]; j++) {
  497. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  498. IF (err)
  499. error("error in toMixedBase 2");
  500. expect = hash4( var_type[i], var_rank[i], index, NCT_INT);
  501. err = nc_get_var1_int(ncid, i, index, &value);
  502. if (inRange3(expect,datatype,NCT_INT)) {
  503. if (expect >= int_min && expect <= int_max) {
  504. IF (err) {
  505. error("nc_get_var1_int: %s", nc_strerror(err));
  506. } else {
  507. IF (!equal(value,expect,var_type[i],NCT_INT)) {
  508. error("Var value read not that expected");
  509. if (verbose) {
  510. error("\n");
  511. error("varid: %d, ", i);
  512. error("var_name: %s, ", var_name[i]);
  513. error("index:");
  514. for (d = 0; d < var_rank[i]; d++)
  515. error(" %d", index[d]);
  516. error(", expect: %g, ", expect);
  517. error("got: %g", (double) value);
  518. }
  519. } else {
  520. ++nok;
  521. }
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. err = nc_close (ncid);
  529. IF (err)
  530. error("nc_close: %s", nc_strerror(err));
  531. print_nok(nok);
  532. }
  533. /*
  534. * check all vars in file which are (text/numeric) compatible with TYPE
  535. */
  536. static
  537. void
  538. check_vars_long(const char *filename)
  539. {
  540. int ncid; /* netCDF id */
  541. size_t index[MAX_RANK];
  542. int err; /* status */
  543. int d;
  544. int i;
  545. size_t j;
  546. long value;
  547. nc_type datatype;
  548. int ndims;
  549. int dimids[MAX_RANK];
  550. double expect;
  551. char name[NC_MAX_NAME];
  552. size_t length;
  553. int canConvert; /* Both text or both numeric */
  554. int nok = 0; /* count of valid comparisons */
  555. err = nc_open(filename, NC_NOWRITE, &ncid);
  556. IF (err)
  557. error("nc_open: %s", nc_strerror(err));
  558. for (i = 0; i < NVARS; i++) {
  559. canConvert = (var_type[i] == NC_CHAR) == (NCT_LONG == NCT_TEXT);
  560. if (canConvert) {
  561. err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, NULL);
  562. IF (err)
  563. error("nc_inq_var: %s", nc_strerror(err));
  564. IF (strcmp(name, var_name[i]) != 0)
  565. error("Unexpected var_name");
  566. IF (datatype != var_type[i])
  567. error("Unexpected type");
  568. IF (ndims != var_rank[i])
  569. error("Unexpected rank");
  570. for (j = 0; j < ndims; j++) {
  571. err = nc_inq_dim(ncid, dimids[j], 0, &length);
  572. IF (err)
  573. error("nc_inq_dim: %s", nc_strerror(err));
  574. IF (length != var_shape[i][j])
  575. error("Unexpected shape");
  576. }
  577. for (j = 0; j < var_nels[i]; j++) {
  578. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  579. IF (err)
  580. error("error in toMixedBase 2");
  581. expect = hash4( var_type[i], var_rank[i], index, NCT_LONG);
  582. err = nc_get_var1_long(ncid, i, index, &value);
  583. if (inRange3(expect,datatype,NCT_LONG)) {
  584. if (expect >= long_min && expect <= long_max) {
  585. IF (err) {
  586. error("nc_get_var1_long: %s", nc_strerror(err));
  587. } else {
  588. IF (!equal(value,expect,var_type[i],NCT_LONG)) {
  589. error("Var value read not that expected");
  590. if (verbose) {
  591. error("\n");
  592. error("varid: %d, ", i);
  593. error("var_name: %s, ", var_name[i]);
  594. error("index:");
  595. for (d = 0; d < var_rank[i]; d++)
  596. error(" %d", index[d]);
  597. error(", expect: %g, ", expect);
  598. error("got: %g", (double) value);
  599. }
  600. } else {
  601. ++nok;
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. }
  609. err = nc_close (ncid);
  610. IF (err)
  611. error("nc_close: %s", nc_strerror(err));
  612. print_nok(nok);
  613. }
  614. /*
  615. * check all vars in file which are (text/numeric) compatible with TYPE
  616. */
  617. static
  618. void
  619. check_vars_float(const char *filename)
  620. {
  621. int ncid; /* netCDF id */
  622. size_t index[MAX_RANK];
  623. int err; /* status */
  624. int d;
  625. int i;
  626. size_t j;
  627. float value;
  628. nc_type datatype;
  629. int ndims;
  630. int dimids[MAX_RANK];
  631. double expect;
  632. char name[NC_MAX_NAME];
  633. size_t length;
  634. int canConvert; /* Both text or both numeric */
  635. int nok = 0; /* count of valid comparisons */
  636. err = nc_open(filename, NC_NOWRITE, &ncid);
  637. IF (err)
  638. error("nc_open: %s", nc_strerror(err));
  639. for (i = 0; i < NVARS; i++) {
  640. canConvert = (var_type[i] == NC_CHAR) == (NCT_FLOAT == NCT_TEXT);
  641. if (canConvert) {
  642. err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, NULL);
  643. IF (err)
  644. error("nc_inq_var: %s", nc_strerror(err));
  645. IF (strcmp(name, var_name[i]) != 0)
  646. error("Unexpected var_name");
  647. IF (datatype != var_type[i])
  648. error("Unexpected type");
  649. IF (ndims != var_rank[i])
  650. error("Unexpected rank");
  651. for (j = 0; j < ndims; j++) {
  652. err = nc_inq_dim(ncid, dimids[j], 0, &length);
  653. IF (err)
  654. error("nc_inq_dim: %s", nc_strerror(err));
  655. IF (length != var_shape[i][j])
  656. error("Unexpected shape");
  657. }
  658. for (j = 0; j < var_nels[i]; j++) {
  659. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  660. IF (err)
  661. error("error in toMixedBase 2");
  662. expect = hash4( var_type[i], var_rank[i], index, NCT_FLOAT);
  663. err = nc_get_var1_float(ncid, i, index, &value);
  664. if (inRange3(expect,datatype,NCT_FLOAT)) {
  665. if (expect >= float_min && expect <= float_max) {
  666. IF (err) {
  667. error("nc_get_var1_float: %s", nc_strerror(err));
  668. } else {
  669. IF (!equal(value,expect,var_type[i],NCT_FLOAT)) {
  670. error("Var value read not that expected");
  671. if (verbose) {
  672. error("\n");
  673. error("varid: %d, ", i);
  674. error("var_name: %s, ", var_name[i]);
  675. error("index:");
  676. for (d = 0; d < var_rank[i]; d++)
  677. error(" %d", index[d]);
  678. error(", expect: %g, ", expect);
  679. error("got: %g", (double) value);
  680. }
  681. } else {
  682. ++nok;
  683. }
  684. }
  685. }
  686. }
  687. }
  688. }
  689. }
  690. err = nc_close (ncid);
  691. IF (err)
  692. error("nc_close: %s", nc_strerror(err));
  693. print_nok(nok);
  694. }
  695. /*
  696. * check all vars in file which are (text/numeric) compatible with TYPE
  697. */
  698. static
  699. void
  700. check_vars_double(const char *filename)
  701. {
  702. int ncid; /* netCDF id */
  703. size_t index[MAX_RANK];
  704. int err; /* status */
  705. int d;
  706. int i;
  707. size_t j;
  708. double value;
  709. nc_type datatype;
  710. int ndims;
  711. int dimids[MAX_RANK];
  712. double expect;
  713. char name[NC_MAX_NAME];
  714. size_t length;
  715. int canConvert; /* Both text or both numeric */
  716. int nok = 0; /* count of valid comparisons */
  717. err = nc_open(filename, NC_NOWRITE, &ncid);
  718. IF (err)
  719. error("nc_open: %s", nc_strerror(err));
  720. for (i = 0; i < NVARS; i++) {
  721. canConvert = (var_type[i] == NC_CHAR) == (NCT_DOUBLE == NCT_TEXT);
  722. if (canConvert) {
  723. err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, NULL);
  724. IF (err)
  725. error("nc_inq_var: %s", nc_strerror(err));
  726. IF (strcmp(name, var_name[i]) != 0)
  727. error("Unexpected var_name");
  728. IF (datatype != var_type[i])
  729. error("Unexpected type");
  730. IF (ndims != var_rank[i])
  731. error("Unexpected rank");
  732. for (j = 0; j < ndims; j++) {
  733. err = nc_inq_dim(ncid, dimids[j], 0, &length);
  734. IF (err)
  735. error("nc_inq_dim: %s", nc_strerror(err));
  736. IF (length != var_shape[i][j])
  737. error("Unexpected shape");
  738. }
  739. for (j = 0; j < var_nels[i]; j++) {
  740. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  741. IF (err)
  742. error("error in toMixedBase 2");
  743. expect = hash4( var_type[i], var_rank[i], index, NCT_DOUBLE);
  744. err = nc_get_var1_double(ncid, i, index, &value);
  745. if (inRange3(expect,datatype,NCT_DOUBLE)) {
  746. if (expect >= double_min && expect <= double_max) {
  747. IF (err) {
  748. error("nc_get_var1_double: %s", nc_strerror(err));
  749. } else {
  750. IF (!equal(value,expect,var_type[i],NCT_DOUBLE)) {
  751. error("Var value read not that expected");
  752. if (verbose) {
  753. error("\n");
  754. error("varid: %d, ", i);
  755. error("var_name: %s, ", var_name[i]);
  756. error("index:");
  757. for (d = 0; d < var_rank[i]; d++)
  758. error(" %d", index[d]);
  759. error(", expect: %g, ", expect);
  760. error("got: %g", (double) value);
  761. }
  762. } else {
  763. ++nok;
  764. }
  765. }
  766. }
  767. }
  768. }
  769. }
  770. }
  771. err = nc_close (ncid);
  772. IF (err)
  773. error("nc_close: %s", nc_strerror(err));
  774. print_nok(nok);
  775. }
  776. /*
  777. * check all attributes in file which are (text/numeric) compatible with TYPE
  778. * ignore any attributes containing values outside range of TYPE
  779. */
  780. static
  781. void
  782. check_atts_text(int ncid)
  783. {
  784. int err; /* status */
  785. int i;
  786. int j;
  787. size_t k;
  788. text value[MAX_NELS];
  789. nc_type datatype;
  790. double expect[MAX_NELS];
  791. size_t length;
  792. size_t nInExtRange; /* number values within external range */
  793. size_t nInIntRange; /* number values within internal range */
  794. int canConvert; /* Both text or both numeric */
  795. int nok = 0; /* count of valid comparisons */
  796. for (i = -1; i < NVARS; i++) {
  797. for (j = 0; j < NATTS(i); j++) {
  798. canConvert = (ATT_TYPE(i,j) == NC_CHAR) == (NCT_TEXT == NCT_TEXT);
  799. if (canConvert) {
  800. err = nc_inq_att(ncid, i, ATT_NAME(i,j), &datatype, &length);
  801. IF (err)
  802. error("nc_inq_att: %s", nc_strerror(err));
  803. IF (datatype != ATT_TYPE(i,j))
  804. error("nc_inq_att: unexpected type");
  805. IF (length != ATT_LEN(i,j))
  806. error("nc_inq_att: unexpected length");
  807. assert(length <= MAX_NELS);
  808. nInIntRange = nInExtRange = 0;
  809. for (k = 0; k < length; k++) {
  810. expect[k] = hash4( datatype, -1, &k, NCT_TEXT);
  811. if (inRange3(expect[k], datatype, NCT_TEXT)) {
  812. ++nInExtRange;
  813. if (expect[k] >= text_min && expect[k] <= text_max)
  814. ++nInIntRange;
  815. }
  816. }
  817. err = nc_get_att_text(ncid, i, ATT_NAME(i,j), value);
  818. if (nInExtRange == length && nInIntRange == length) {
  819. IF (err)
  820. error("%s", nc_strerror(err));
  821. } else {
  822. IF (err != 0 && err != NC_ERANGE)
  823. error("OK or Range error: status = %d", err);
  824. }
  825. for (k = 0; k < length; k++) {
  826. if (inRange3(expect[k],datatype,NCT_TEXT)
  827. && expect[k] >= text_min && expect[k] <= text_max) {
  828. IF (!equal(value[k],expect[k],datatype,NCT_TEXT)) {
  829. error("att. value read not that expected");
  830. if (verbose) {
  831. error("\n");
  832. error("varid: %d, ", i);
  833. error("att_name: %s, ", ATT_NAME(i,j));
  834. error("element number: %d ", k);
  835. error("expect: %g, ", expect[k]);
  836. error("got: %g", (double) value[k]);
  837. }
  838. } else {
  839. nok++;
  840. }
  841. }
  842. }
  843. }
  844. }
  845. }
  846. print_nok(nok);
  847. }
  848. /*
  849. * check all attributes in file which are (text/numeric) compatible with TYPE
  850. * ignore any attributes containing values outside range of TYPE
  851. */
  852. static
  853. void
  854. check_atts_uchar(int ncid)
  855. {
  856. int err; /* status */
  857. int i;
  858. int j;
  859. size_t k;
  860. uchar value[MAX_NELS];
  861. nc_type datatype;
  862. double expect[MAX_NELS];
  863. size_t length;
  864. size_t nInExtRange; /* number values within external range */
  865. size_t nInIntRange; /* number values within internal range */
  866. int canConvert; /* Both text or both numeric */
  867. int nok = 0; /* count of valid comparisons */
  868. for (i = -1; i < NVARS; i++) {
  869. for (j = 0; j < NATTS(i); j++) {
  870. canConvert = (ATT_TYPE(i,j) == NC_CHAR) == (NCT_UCHAR == NCT_TEXT);
  871. if (canConvert) {
  872. err = nc_inq_att(ncid, i, ATT_NAME(i,j), &datatype, &length);
  873. IF (err)
  874. error("nc_inq_att: %s", nc_strerror(err));
  875. IF (datatype != ATT_TYPE(i,j))
  876. error("nc_inq_att: unexpected type");
  877. IF (length != ATT_LEN(i,j))
  878. error("nc_inq_att: unexpected length");
  879. assert(length <= MAX_NELS);
  880. nInIntRange = nInExtRange = 0;
  881. for (k = 0; k < length; k++) {
  882. expect[k] = hash4( datatype, -1, &k, NCT_UCHAR);
  883. if (inRange3(expect[k], datatype, NCT_UCHAR)) {
  884. ++nInExtRange;
  885. if (expect[k] >= uchar_min && expect[k] <= uchar_max)
  886. ++nInIntRange;
  887. }
  888. }
  889. err = nc_get_att_uchar(ncid, i, ATT_NAME(i,j), value);
  890. if (nInExtRange == length && nInIntRange == length) {
  891. IF (err)
  892. error("%s", nc_strerror(err));
  893. } else {
  894. IF (err != 0 && err != NC_ERANGE)
  895. error("OK or Range error: status = %d", err);
  896. }
  897. for (k = 0; k < length; k++) {
  898. if (inRange3(expect[k],datatype,NCT_UCHAR)
  899. && expect[k] >= uchar_min && expect[k] <= uchar_max) {
  900. IF (!equal(value[k],expect[k],datatype,NCT_UCHAR)) {
  901. error("att. value read not that expected");
  902. if (verbose) {
  903. error("\n");
  904. error("varid: %d, ", i);
  905. error("att_name: %s, ", ATT_NAME(i,j));
  906. error("element number: %d ", k);
  907. error("expect: %g, ", expect[k]);
  908. error("got: %g", (double) value[k]);
  909. }
  910. } else {
  911. nok++;
  912. }
  913. }
  914. }
  915. }
  916. }
  917. }
  918. print_nok(nok);
  919. }
  920. /*
  921. * check all attributes in file which are (text/numeric) compatible with TYPE
  922. * ignore any attributes containing values outside range of TYPE
  923. */
  924. static
  925. void
  926. check_atts_schar(int ncid)
  927. {
  928. int err; /* status */
  929. int i;
  930. int j;
  931. size_t k;
  932. schar value[MAX_NELS];
  933. nc_type datatype;
  934. double expect[MAX_NELS];
  935. size_t length;
  936. size_t nInExtRange; /* number values within external range */
  937. size_t nInIntRange; /* number values within internal range */
  938. int canConvert; /* Both text or both numeric */
  939. int nok = 0; /* count of valid comparisons */
  940. for (i = -1; i < NVARS; i++) {
  941. for (j = 0; j < NATTS(i); j++) {
  942. canConvert = (ATT_TYPE(i,j) == NC_CHAR) == (NCT_SCHAR == NCT_TEXT);
  943. if (canConvert) {
  944. err = nc_inq_att(ncid, i, ATT_NAME(i,j), &datatype, &length);
  945. IF (err)
  946. error("nc_inq_att: %s", nc_strerror(err));
  947. IF (datatype != ATT_TYPE(i,j))
  948. error("nc_inq_att: unexpected type");
  949. IF (length != ATT_LEN(i,j))
  950. error("nc_inq_att: unexpected length");
  951. assert(length <= MAX_NELS);
  952. nInIntRange = nInExtRange = 0;
  953. for (k = 0; k < length; k++) {
  954. expect[k] = hash4( datatype, -1, &k, NCT_SCHAR);
  955. if (inRange3(expect[k], datatype, NCT_SCHAR)) {
  956. ++nInExtRange;
  957. if (expect[k] >= schar_min && expect[k] <= schar_max)
  958. ++nInIntRange;
  959. }
  960. }
  961. err = nc_get_att_schar(ncid, i, ATT_NAME(i,j), value);
  962. if (nInExtRange == length && nInIntRange == length) {
  963. IF (err)
  964. error("%s", nc_strerror(err));
  965. } else {
  966. IF (err != 0 && err != NC_ERANGE)
  967. error("OK or Range error: status = %d", err);
  968. }
  969. for (k = 0; k < length; k++) {
  970. if (inRange3(expect[k],datatype,NCT_SCHAR)
  971. && expect[k] >= schar_min && expect[k] <= schar_max) {
  972. IF (!equal(value[k],expect[k],datatype,NCT_SCHAR)) {
  973. error("att. value read not that expected");
  974. if (verbose) {
  975. error("\n");
  976. error("varid: %d, ", i);
  977. error("att_name: %s, ", ATT_NAME(i,j));
  978. error("element number: %d ", k);
  979. error("expect: %g, ", expect[k]);
  980. error("got: %g", (double) value[k]);
  981. }
  982. } else {
  983. nok++;
  984. }
  985. }
  986. }
  987. }
  988. }
  989. }
  990. print_nok(nok);
  991. }
  992. /*
  993. * check all attributes in file which are (text/numeric) compatible with TYPE
  994. * ignore any attributes containing values outside range of TYPE
  995. */
  996. static
  997. void
  998. check_atts_short(int ncid)
  999. {
  1000. int err; /* status */
  1001. int i;
  1002. int j;
  1003. size_t k;
  1004. short value[MAX_NELS];
  1005. nc_type datatype;
  1006. double expect[MAX_NELS];
  1007. size_t length;
  1008. size_t nInExtRange; /* number values within external range */
  1009. size_t nInIntRange; /* number values within internal range */
  1010. int canConvert; /* Both text or both numeric */
  1011. int nok = 0; /* count of valid comparisons */
  1012. for (i = -1; i < NVARS; i++) {
  1013. for (j = 0; j < NATTS(i); j++) {
  1014. canConvert = (ATT_TYPE(i,j) == NC_CHAR) == (NCT_SHORT == NCT_TEXT);
  1015. if (canConvert) {
  1016. err = nc_inq_att(ncid, i, ATT_NAME(i,j), &datatype, &length);
  1017. IF (err)
  1018. error("nc_inq_att: %s", nc_strerror(err));
  1019. IF (datatype != ATT_TYPE(i,j))
  1020. error("nc_inq_att: unexpected type");
  1021. IF (length != ATT_LEN(i,j))
  1022. error("nc_inq_att: unexpected length");
  1023. assert(length <= MAX_NELS);
  1024. nInIntRange = nInExtRange = 0;
  1025. for (k = 0; k < length; k++) {
  1026. expect[k] = hash4( datatype, -1, &k, NCT_SHORT);
  1027. if (inRange3(expect[k], datatype, NCT_SHORT)) {
  1028. ++nInExtRange;
  1029. if (expect[k] >= short_min && expect[k] <= short_max)
  1030. ++nInIntRange;
  1031. }
  1032. }
  1033. err = nc_get_att_short(ncid, i, ATT_NAME(i,j), value);
  1034. if (nInExtRange == length && nInIntRange == length) {
  1035. IF (err)
  1036. error("%s", nc_strerror(err));
  1037. } else {
  1038. IF (err != 0 && err != NC_ERANGE)
  1039. error("OK or Range error: status = %d", err);
  1040. }
  1041. for (k = 0; k < length; k++) {
  1042. if (inRange3(expect[k],datatype,NCT_SHORT)
  1043. && expect[k] >= short_min && expect[k] <= short_max) {
  1044. IF (!equal(value[k],expect[k],datatype,NCT_SHORT)) {
  1045. error("att. value read not that expected");
  1046. if (verbose) {
  1047. error("\n");
  1048. error("varid: %d, ", i);
  1049. error("att_name: %s, ", ATT_NAME(i,j));
  1050. error("element number: %d ", k);
  1051. error("expect: %g, ", expect[k]);
  1052. error("got: %g", (double) value[k]);
  1053. }
  1054. } else {
  1055. nok++;
  1056. }
  1057. }
  1058. }
  1059. }
  1060. }
  1061. }
  1062. print_nok(nok);
  1063. }
  1064. /*
  1065. * check all attributes in file which are (text/numeric) compatible with TYPE
  1066. * ignore any attributes containing values outside range of TYPE
  1067. */
  1068. static
  1069. void
  1070. check_atts_int(int ncid)
  1071. {
  1072. int err; /* status */
  1073. int i;
  1074. int j;
  1075. size_t k;
  1076. int value[MAX_NELS];
  1077. nc_type datatype;
  1078. double expect[MAX_NELS];
  1079. size_t length;
  1080. size_t nInExtRange; /* number values within external range */
  1081. size_t nInIntRange; /* number values within internal range */
  1082. int canConvert; /* Both text or both numeric */
  1083. int nok = 0; /* count of valid comparisons */
  1084. for (i = -1; i < NVARS; i++) {
  1085. for (j = 0; j < NATTS(i); j++) {
  1086. canConvert = (ATT_TYPE(i,j) == NC_CHAR) == (NCT_INT == NCT_TEXT);
  1087. if (canConvert) {
  1088. err = nc_inq_att(ncid, i, ATT_NAME(i,j), &datatype, &length);
  1089. IF (err)
  1090. error("nc_inq_att: %s", nc_strerror(err));
  1091. IF (datatype != ATT_TYPE(i,j))
  1092. error("nc_inq_att: unexpected type");
  1093. IF (length != ATT_LEN(i,j))
  1094. error("nc_inq_att: unexpected length");
  1095. assert(length <= MAX_NELS);
  1096. nInIntRange = nInExtRange = 0;
  1097. for (k = 0; k < length; k++) {
  1098. expect[k] = hash4( datatype, -1, &k, NCT_INT);
  1099. if (inRange3(expect[k], datatype, NCT_INT)) {
  1100. ++nInExtRange;
  1101. if (expect[k] >= int_min && expect[k] <= int_max)
  1102. ++nInIntRange;
  1103. }
  1104. }
  1105. err = nc_get_att_int(ncid, i, ATT_NAME(i,j), value);
  1106. if (nInExtRange == length && nInIntRange == length) {
  1107. IF (err)
  1108. error("%s", nc_strerror(err));
  1109. } else {
  1110. IF (err != 0 && err != NC_ERANGE)
  1111. error("OK or Range error: status = %d", err);
  1112. }
  1113. for (k = 0; k < length; k++) {
  1114. if (inRange3(expect[k],datatype,NCT_INT)
  1115. && expect[k] >= int_min && expect[k] <= int_max) {
  1116. IF (!equal(value[k],expect[k],datatype,NCT_INT)) {
  1117. error("att. value read not that expected");
  1118. if (verbose) {
  1119. error("\n");
  1120. error("varid: %d, ", i);
  1121. error("att_name: %s, ", ATT_NAME(i,j));
  1122. error("element number: %d ", k);
  1123. error("expect: %g, ", expect[k]);
  1124. error("got: %g", (double) value[k]);
  1125. }
  1126. } else {
  1127. nok++;
  1128. }
  1129. }
  1130. }
  1131. }
  1132. }
  1133. }
  1134. print_nok(nok);
  1135. }
  1136. /*
  1137. * check all attributes in file which are (text/numeric) compatible with TYPE
  1138. * ignore any attributes containing values outside range of TYPE
  1139. */
  1140. static
  1141. void
  1142. check_atts_long(int ncid)
  1143. {
  1144. int err; /* status */
  1145. int i;
  1146. int j;
  1147. size_t k;
  1148. long value[MAX_NELS];
  1149. nc_type datatype;
  1150. double expect[MAX_NELS];
  1151. size_t length;
  1152. size_t nInExtRange; /* number values within external range */
  1153. size_t nInIntRange; /* number values within internal range */
  1154. int canConvert; /* Both text or both numeric */
  1155. int nok = 0; /* count of valid comparisons */
  1156. for (i = -1; i < NVARS; i++) {
  1157. for (j = 0; j < NATTS(i); j++) {
  1158. canConvert = (ATT_TYPE(i,j) == NC_CHAR) == (NCT_LONG == NCT_TEXT);
  1159. if (canConvert) {
  1160. err = nc_inq_att(ncid, i, ATT_NAME(i,j), &datatype, &length);
  1161. IF (err)
  1162. error("nc_inq_att: %s", nc_strerror(err));
  1163. IF (datatype != ATT_TYPE(i,j))
  1164. error("nc_inq_att: unexpected type");
  1165. IF (length != ATT_LEN(i,j))
  1166. error("nc_inq_att: unexpected length");
  1167. assert(length <= MAX_NELS);
  1168. nInIntRange = nInExtRange = 0;
  1169. for (k = 0; k < length; k++) {
  1170. expect[k] = hash4( datatype, -1, &k, NCT_LONG);
  1171. if (inRange3(expect[k], datatype, NCT_LONG)) {
  1172. ++nInExtRange;
  1173. if (expect[k] >= long_min && expect[k] <= long_max)
  1174. ++nInIntRange;
  1175. }
  1176. }
  1177. err = nc_get_att_long(ncid, i, ATT_NAME(i,j), value);
  1178. if (nInExtRange == length && nInIntRange == length) {
  1179. IF (err)
  1180. error("%s", nc_strerror(err));
  1181. } else {
  1182. IF (err != 0 && err != NC_ERANGE)
  1183. error("OK or Range error: status = %d", err);
  1184. }
  1185. for (k = 0; k < length; k++) {
  1186. if (inRange3(expect[k],datatype,NCT_LONG)
  1187. && expect[k] >= long_min && expect[k] <= long_max) {
  1188. IF (!equal(value[k],expect[k],datatype,NCT_LONG)) {
  1189. error("att. value read not that expected");
  1190. if (verbose) {
  1191. error("\n");
  1192. error("varid: %d, ", i);
  1193. error("att_name: %s, ", ATT_NAME(i,j));
  1194. error("element number: %d ", k);
  1195. error("expect: %g, ", expect[k]);
  1196. error("got: %g", (double) value[k]);
  1197. }
  1198. } else {
  1199. nok++;
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. }
  1206. print_nok(nok);
  1207. }
  1208. /*
  1209. * check all attributes in file which are (text/numeric) compatible with TYPE
  1210. * ignore any attributes containing values outside range of TYPE
  1211. */
  1212. static
  1213. void
  1214. check_atts_float(int ncid)
  1215. {
  1216. int err; /* status */
  1217. int i;
  1218. int j;
  1219. size_t k;
  1220. float value[MAX_NELS];
  1221. nc_type datatype;
  1222. double expect[MAX_NELS];
  1223. size_t length;
  1224. size_t nInExtRange; /* number values within external range */
  1225. size_t nInIntRange; /* number values within internal range */
  1226. int canConvert; /* Both text or both numeric */
  1227. int nok = 0; /* count of valid comparisons */
  1228. for (i = -1; i < NVARS; i++) {
  1229. for (j = 0; j < NATTS(i); j++) {
  1230. canConvert = (ATT_TYPE(i,j) == NC_CHAR) == (NCT_FLOAT == NCT_TEXT);
  1231. if (canConvert) {
  1232. err = nc_inq_att(ncid, i, ATT_NAME(i,j), &datatype, &length);
  1233. IF (err)
  1234. error("nc_inq_att: %s", nc_strerror(err));
  1235. IF (datatype != ATT_TYPE(i,j))
  1236. error("nc_inq_att: unexpected type");
  1237. IF (length != ATT_LEN(i,j))
  1238. error("nc_inq_att: unexpected length");
  1239. assert(length <= MAX_NELS);
  1240. nInIntRange = nInExtRange = 0;
  1241. for (k = 0; k < length; k++) {
  1242. expect[k] = hash4( datatype, -1, &k, NCT_FLOAT);
  1243. if (inRange3(expect[k], datatype, NCT_FLOAT)) {
  1244. ++nInExtRange;
  1245. if (expect[k] >= float_min && expect[k] <= float_max)
  1246. ++nInIntRange;
  1247. }
  1248. }
  1249. err = nc_get_att_float(ncid, i, ATT_NAME(i,j), value);
  1250. if (nInExtRange == length && nInIntRange == length) {
  1251. IF (err)
  1252. error("%s", nc_strerror(err));
  1253. } else {
  1254. IF (err != 0 && err != NC_ERANGE)
  1255. error("OK or Range error: status = %d", err);
  1256. }
  1257. for (k = 0; k < length; k++) {
  1258. if (inRange3(expect[k],datatype,NCT_FLOAT)
  1259. && expect[k] >= float_min && expect[k] <= float_max) {
  1260. IF (!equal(value[k],expect[k],datatype,NCT_FLOAT)) {
  1261. error("att. value read not that expected");
  1262. if (verbose) {
  1263. error("\n");
  1264. error("varid: %d, ", i);
  1265. error("att_name: %s, ", ATT_NAME(i,j));
  1266. error("element number: %d ", k);
  1267. error("expect: %g, ", expect[k]);
  1268. error("got: %g", (double) value[k]);
  1269. }
  1270. } else {
  1271. nok++;
  1272. }
  1273. }
  1274. }
  1275. }
  1276. }
  1277. }
  1278. print_nok(nok);
  1279. }
  1280. /*
  1281. * check all attributes in file which are (text/numeric) compatible with TYPE
  1282. * ignore any attributes containing values outside range of TYPE
  1283. */
  1284. static
  1285. void
  1286. check_atts_double(int ncid)
  1287. {
  1288. int err; /* status */
  1289. int i;
  1290. int j;
  1291. size_t k;
  1292. double value[MAX_NELS];
  1293. nc_type datatype;
  1294. double expect[MAX_NELS];
  1295. size_t length;
  1296. size_t nInExtRange; /* number values within external range */
  1297. size_t nInIntRange; /* number values within internal range */
  1298. int canConvert; /* Both text or both numeric */
  1299. int nok = 0; /* count of valid comparisons */
  1300. for (i = -1; i < NVARS; i++) {
  1301. for (j = 0; j < NATTS(i); j++) {
  1302. canConvert = (ATT_TYPE(i,j) == NC_CHAR) == (NCT_DOUBLE == NCT_TEXT);
  1303. if (canConvert) {
  1304. err = nc_inq_att(ncid, i, ATT_NAME(i,j), &datatype, &length);
  1305. IF (err)
  1306. error("nc_inq_att: %s", nc_strerror(err));
  1307. IF (datatype != ATT_TYPE(i,j))
  1308. error("nc_inq_att: unexpected type");
  1309. IF (length != ATT_LEN(i,j))
  1310. error("nc_inq_att: unexpected length");
  1311. assert(length <= MAX_NELS);
  1312. nInIntRange = nInExtRange = 0;
  1313. for (k = 0; k < length; k++) {
  1314. expect[k] = hash4( datatype, -1, &k, NCT_DOUBLE);
  1315. if (inRange3(expect[k], datatype, NCT_DOUBLE)) {
  1316. ++nInExtRange;
  1317. if (expect[k] >= double_min && expect[k] <= double_max)
  1318. ++nInIntRange;
  1319. }
  1320. }
  1321. err = nc_get_att_double(ncid, i, ATT_NAME(i,j), value);
  1322. if (nInExtRange == length && nInIntRange == length) {
  1323. IF (err)
  1324. error("%s", nc_strerror(err));
  1325. } else {
  1326. IF (err != 0 && err != NC_ERANGE)
  1327. error("OK or Range error: status = %d", err);
  1328. }
  1329. for (k = 0; k < length; k++) {
  1330. if (inRange3(expect[k],datatype,NCT_DOUBLE)
  1331. && expect[k] >= double_min && expect[k] <= double_max) {
  1332. IF (!equal(value[k],expect[k],datatype,NCT_DOUBLE)) {
  1333. error("att. value read not that expected");
  1334. if (verbose) {
  1335. error("\n");
  1336. error("varid: %d, ", i);
  1337. error("att_name: %s, ", ATT_NAME(i,j));
  1338. error("element number: %d ", k);
  1339. error("expect: %g, ", expect[k]);
  1340. error("got: %g", (double) value[k]);
  1341. }
  1342. } else {
  1343. nok++;
  1344. }
  1345. }
  1346. }
  1347. }
  1348. }
  1349. }
  1350. print_nok(nok);
  1351. }
  1352. void
  1353. test_nc_put_var1_text(void)
  1354. {
  1355. int ncid;
  1356. int i;
  1357. int j;
  1358. int err;
  1359. size_t index[MAX_RANK];
  1360. int canConvert; /* Both text or both numeric */
  1361. text value = 5; /* any value would do - only for error cases */
  1362. err = nc_create(scratch, NC_CLOBBER, &ncid);
  1363. IF (err) {
  1364. error("nc_create: %s", nc_strerror(err));
  1365. return;
  1366. }
  1367. def_dims(ncid);
  1368. def_vars(ncid);
  1369. err = nc_enddef(ncid);
  1370. IF (err)
  1371. error("nc_enddef: %s", nc_strerror(err));
  1372. for (i = 0; i < NVARS; i++) {
  1373. canConvert = (var_type[i] == NC_CHAR) == (NCT_TEXT == NCT_TEXT);
  1374. for (j = 0; j < var_rank[i]; j++)
  1375. index[j] = 0;
  1376. err = nc_put_var1_text(BAD_ID, i, index, &value);
  1377. IF (err != NC_EBADID)
  1378. error("bad ncid: status = %d", err);
  1379. err = nc_put_var1_text(ncid, BAD_VARID, index, &value);
  1380. IF (err != NC_ENOTVAR)
  1381. error("bad var id: status = %d", err);
  1382. for (j = 0; j < var_rank[i]; j++) {
  1383. if (var_dimid[i][j] > 0) { /* skip record dim */
  1384. index[j] = var_shape[i][j];
  1385. err = nc_put_var1_text(ncid, i, index, &value);
  1386. IF (canConvert && err != NC_EINVALCOORDS)
  1387. error("bad index: status = %d", err);
  1388. index[j] = 0;
  1389. }
  1390. }
  1391. for (j = 0; j < var_nels[i]; j++) {
  1392. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  1393. IF (err)
  1394. error("error in toMixedBase 1");
  1395. value = hash_text( var_type[i], var_rank[i], index, NCT_TEXT);
  1396. if (var_rank[i] == 0 && i%2 == 0)
  1397. err = nc_put_var1_text(ncid, i, NULL, &value);
  1398. else
  1399. err = nc_put_var1_text(ncid, i, index, &value);
  1400. if (canConvert) {
  1401. if (inRange3(value, var_type[i],NCT_TEXT)) {
  1402. IF (err)
  1403. error("%s", nc_strerror(err));
  1404. } else {
  1405. IF (err != NC_ERANGE) {
  1406. error("Range error: status = %d", err);
  1407. error("\n\t\tfor type %s value %.17e %ld",
  1408. s_nc_type(var_type[i]),
  1409. (double)value, (long)value);
  1410. }
  1411. }
  1412. } else {
  1413. IF (err != NC_ECHAR)
  1414. error("wrong type: status = %d", err);
  1415. }
  1416. }
  1417. }
  1418. err = nc_close(ncid);
  1419. IF (err)
  1420. error("nc_close: %s", nc_strerror(err));
  1421. check_vars_text(scratch);
  1422. err = remove(scratch);
  1423. IF (err)
  1424. error("remove of %s failed", scratch);
  1425. }
  1426. void
  1427. test_nc_put_var1_uchar(void)
  1428. {
  1429. int ncid;
  1430. int i;
  1431. int j;
  1432. int err;
  1433. size_t index[MAX_RANK];
  1434. int canConvert; /* Both text or both numeric */
  1435. uchar value = 5; /* any value would do - only for error cases */
  1436. err = nc_create(scratch, NC_CLOBBER, &ncid);
  1437. IF (err) {
  1438. error("nc_create: %s", nc_strerror(err));
  1439. return;
  1440. }
  1441. def_dims(ncid);
  1442. def_vars(ncid);
  1443. err = nc_enddef(ncid);
  1444. IF (err)
  1445. error("nc_enddef: %s", nc_strerror(err));
  1446. for (i = 0; i < NVARS; i++) {
  1447. canConvert = (var_type[i] == NC_CHAR) == (NCT_UCHAR == NCT_TEXT);
  1448. for (j = 0; j < var_rank[i]; j++)
  1449. index[j] = 0;
  1450. err = nc_put_var1_uchar(BAD_ID, i, index, &value);
  1451. IF (err != NC_EBADID)
  1452. error("bad ncid: status = %d", err);
  1453. err = nc_put_var1_uchar(ncid, BAD_VARID, index, &value);
  1454. IF (err != NC_ENOTVAR)
  1455. error("bad var id: status = %d", err);
  1456. for (j = 0; j < var_rank[i]; j++) {
  1457. if (var_dimid[i][j] > 0) { /* skip record dim */
  1458. index[j] = var_shape[i][j];
  1459. err = nc_put_var1_uchar(ncid, i, index, &value);
  1460. IF (canConvert && err != NC_EINVALCOORDS)
  1461. error("bad index: status = %d", err);
  1462. index[j] = 0;
  1463. }
  1464. }
  1465. for (j = 0; j < var_nels[i]; j++) {
  1466. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  1467. IF (err)
  1468. error("error in toMixedBase 1");
  1469. value = hash_uchar( var_type[i], var_rank[i], index, NCT_UCHAR);
  1470. if (var_rank[i] == 0 && i%2 == 0)
  1471. err = nc_put_var1_uchar(ncid, i, NULL, &value);
  1472. else
  1473. err = nc_put_var1_uchar(ncid, i, index, &value);
  1474. if (canConvert) {
  1475. if (inRange3(value, var_type[i],NCT_UCHAR)) {
  1476. IF (err)
  1477. error("%s", nc_strerror(err));
  1478. } else {
  1479. IF (err != NC_ERANGE) {
  1480. error("Range error: status = %d", err);
  1481. error("\n\t\tfor type %s value %.17e %ld",
  1482. s_nc_type(var_type[i]),
  1483. (double)value, (long)value);
  1484. }
  1485. }
  1486. } else {
  1487. IF (err != NC_ECHAR)
  1488. error("wrong type: status = %d", err);
  1489. }
  1490. }
  1491. }
  1492. err = nc_close(ncid);
  1493. IF (err)
  1494. error("nc_close: %s", nc_strerror(err));
  1495. check_vars_uchar(scratch);
  1496. err = remove(scratch);
  1497. IF (err)
  1498. error("remove of %s failed", scratch);
  1499. }
  1500. void
  1501. test_nc_put_var1_schar(void)
  1502. {
  1503. int ncid;
  1504. int i;
  1505. int j;
  1506. int err;
  1507. size_t index[MAX_RANK];
  1508. int canConvert; /* Both text or both numeric */
  1509. schar value = 5; /* any value would do - only for error cases */
  1510. err = nc_create(scratch, NC_CLOBBER, &ncid);
  1511. IF (err) {
  1512. error("nc_create: %s", nc_strerror(err));
  1513. return;
  1514. }
  1515. def_dims(ncid);
  1516. def_vars(ncid);
  1517. err = nc_enddef(ncid);
  1518. IF (err)
  1519. error("nc_enddef: %s", nc_strerror(err));
  1520. for (i = 0; i < NVARS; i++) {
  1521. canConvert = (var_type[i] == NC_CHAR) == (NCT_SCHAR == NCT_TEXT);
  1522. for (j = 0; j < var_rank[i]; j++)
  1523. index[j] = 0;
  1524. err = nc_put_var1_schar(BAD_ID, i, index, &value);
  1525. IF (err != NC_EBADID)
  1526. error("bad ncid: status = %d", err);
  1527. err = nc_put_var1_schar(ncid, BAD_VARID, index, &value);
  1528. IF (err != NC_ENOTVAR)
  1529. error("bad var id: status = %d", err);
  1530. for (j = 0; j < var_rank[i]; j++) {
  1531. if (var_dimid[i][j] > 0) { /* skip record dim */
  1532. index[j] = var_shape[i][j];
  1533. err = nc_put_var1_schar(ncid, i, index, &value);
  1534. IF (canConvert && err != NC_EINVALCOORDS)
  1535. error("bad index: status = %d", err);
  1536. index[j] = 0;
  1537. }
  1538. }
  1539. for (j = 0; j < var_nels[i]; j++) {
  1540. err = toMixedBase(j, var_rank[i], var_shape[i], index);
  1541. IF (err)
  1542. error("error in toMixedBase 1");
  1543. value = hash_schar( var_type[i], var_rank[i], index, NCT_SCHAR);
  1544. if (var_rank[i] == 0 && i%2 == 0)
  1545. err = nc_put_var1_schar(ncid, i, NULL, &value);
  1546. else
  1547. err = nc_put_var1_schar(ncid, i, index, &value);

Large files files are truncated, but you can click here to view the full file