1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
|
/*********************************************************************
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/README file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/ncdump/dumplib.c,v 1.85 2010/05/05 22:15:39 dmh Exp $
*********************************************************************/
/*
* We potentially include <stdarg.h> before <stdio.h> in order to obtain a
* definition for va_list from the GNU C compiler.
*/
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#ifndef NO_FLOAT_H
#include <float.h> /* for FLT_EPSILON, DBL_EPSILON */
#endif /* NO_FLOAT_H */
#include <math.h>
#include <netcdf.h>
#include "utils.h"
#include "nccomps.h"
#include "dumplib.h"
#include "ncdump.h"
#include "isnan.h"
#include "nctime0.h"
static float float_eps;
static double double_eps;
extern fspec_t formatting_specs; /* set from command-line options */
static float
float_epsilon(void)
{
float float_eps;
#ifndef NO_FLOAT_H
float_eps = FLT_EPSILON;
#else /* NO_FLOAT_H */
{
float etop, ebot, eps;
float one = 1.0;
float two = 2.0;
etop = 1.0;
ebot = 0.0;
eps = ebot + (etop - ebot)/two;
while (eps != ebot && eps != etop) {
float epsp1;
epsp1 = one + eps;
if (epsp1 > one)
etop = eps;
else
ebot = eps;
eps = ebot + (etop - ebot)/two;
}
float_eps = two * etop;
}
#endif /* NO_FLOAT_H */
return float_eps;
}
static double
double_epsilon(void)
{
double double_eps;
#ifndef NO_FLOAT_H
double_eps = DBL_EPSILON;
#else /* NO_FLOAT_H */
{
double etop, ebot, eps;
double one = 1.0;
double two = 2.0;
etop = 1.0;
ebot = 0.0;
eps = ebot + (etop - ebot)/two;
while (eps != ebot && eps != etop) {
double epsp1;
epsp1 = one + eps;
if (epsp1 > one)
etop = eps;
else
ebot = eps;
eps = ebot + (etop - ebot)/two;
}
double_eps = two * etop;
}
#endif /* NO_FLOAT_H */
return double_eps;
}
void
init_epsilons(void)
{
float_eps = float_epsilon();
double_eps = double_epsilon();
}
static char* has_c_format_att(int ncid, int varid);
int float_precision_specified = 0; /* -p option specified float precision */
int double_precision_specified = 0; /* -p option specified double precision */
char float_var_fmt[] = "%.NNg";
char double_var_fmt[] = "%.NNg";
char float_att_fmt[] = "%#.NNgf";
char float_attx_fmt[] = "%#.NNg";
char double_att_fmt[] = "%#.NNg";
/* magic number stored in a safebuf and checked, hoping it will be
* changed if buffer was overwritten inadvertently */
#define SAFEBUF_CERT 2147114711
/* expression for where SAFEBUF_CERT is stored within safebuf (at end
* of buffer, after data) */
#define SAFEBUF_EXPR(sbuf) (*(int *)((sbuf)->buf + (sbuf)->len))
/* expression to be checked whenever a safebuf is used */
#define SAFEBUF_CHECK(sbuf) (SAFEBUF_EXPR(sbuf) == SAFEBUF_CERT)
/* somewhat arbitrary initial size of safebufs, grow as needed */
#define SAFEBUF_INIT_LEN 128
/* initialize safe buffer */
safebuf_t *
sbuf_new() {
size_t len = SAFEBUF_INIT_LEN;
safebuf_t *sb;
sb = (safebuf_t *) emalloc(sizeof(safebuf_t));
sb->buf = (char *)emalloc(len + sizeof(int));
sb->len = len;
/* write a "stamp" in last 4 bytes of buffer for id and to check for overflow */
SAFEBUF_EXPR(sb) = SAFEBUF_CERT;
sb->buf[0] = 0;
sb->cl = strlen(sb->buf);
assert(SAFEBUF_CHECK(sb));
return sb;
}
/* grow buffer to at least len bytes, copying previous contents if
* necessary */
void
sbuf_grow(safebuf_t *sb, size_t len) {
size_t m = sb->len;
void *tmp;
assert(SAFEBUF_CHECK(sb));
if (len <= m)
return;
/* Make sure we at least double size of buffer to get what's
* needed. If we just used realloc(), no guarantee that length
* would be expanded by a multiple, which we want. */
while(len > m) {
m *= 2;
}
tmp = emalloc(m + sizeof(int));
memcpy(tmp, sb->buf, sb->len);
sb->len = m;
free(sb->buf);
sb->buf = tmp;
SAFEBUF_EXPR(sb) = SAFEBUF_CERT;
assert(SAFEBUF_CHECK(sb));
}
/* Copy string s2 to safe buffer, growing if necessary */
void
sbuf_cpy(safebuf_t *sb, const char *s2) {
size_t s2len;
assert(SAFEBUF_CHECK(sb));
s2len = strlen(s2);
sbuf_grow(sb, 1 + s2len);
strncpy(sb->buf, s2, sb->len);
sb->cl = s2len;
assert(SAFEBUF_CHECK(sb));
}
/* Concatenate string s2 to end of string in safe buffer, growing if necessary */
void
sbuf_cat(safebuf_t *sb, const char *s2) {
size_t s2len;
size_t res;
assert(SAFEBUF_CHECK(sb));
s2len = strlen(s2);
sbuf_grow(sb, 1 + sb->cl + s2len);
res = strlcat(sb->buf + sb->cl, s2, sb->len);
assert( res < sb->len );
sb->cl += s2len;
assert(SAFEBUF_CHECK(sb));
}
/* Concatenate string in safebuf s2 to end of string in safebuf s1,
* growing if necessary */
void
sbuf_catb(safebuf_t *s1, const safebuf_t *s2) {
size_t s2len;
size_t res;
assert(SAFEBUF_CHECK(s1));
assert(SAFEBUF_CHECK(s2));
s2len = sbuf_len(s2);
sbuf_grow(s1, 1 + s1->cl + s2len);
res = strlcat(s1->buf + s1->cl, s2->buf, s1->len);
assert( res < s1->len );
s1->cl += s2len;
assert(SAFEBUF_CHECK(s1));
}
/* Return length of string in sbuf */
size_t
sbuf_len(const safebuf_t *sb) {
assert(SAFEBUF_CHECK(sb));
return sb->cl;
}
/* Return C string in an sbuf */
char *
sbuf_str(const safebuf_t *sb) {
assert(SAFEBUF_CHECK(sb));
return sb->buf;
}
/* free safe buffer */
void
sbuf_free(safebuf_t *sb) {
assert(SAFEBUF_CHECK(sb));
free(sb->buf);
free(sb);
}
/* In case different formats specified with -d option, set them here. */
void
set_formats(int float_digits, int double_digits)
{
int res;
res = snprintf(float_var_fmt, strlen(float_var_fmt) + 1, "%%.%dg",
float_digits) + 1;
assert(res <= sizeof(float_var_fmt));
res = snprintf(double_var_fmt, strlen(double_var_fmt) + 1, "%%.%dg",
double_digits) + 1;
assert(res <= sizeof(double_var_fmt));
res = snprintf(float_att_fmt, strlen(float_att_fmt) + 1, "%%#.%dgf",
float_digits) + 1;
assert(res <= sizeof(float_att_fmt));
res = snprintf(float_attx_fmt, strlen(float_attx_fmt) + 1, "%%#.%dg",
float_digits) + 1;
assert(res <= sizeof(float_attx_fmt));
res = snprintf(double_att_fmt, strlen(double_att_fmt) + 1, "%%#.%dg",
double_digits) + 1;
assert(res <= sizeof(double_att_fmt));
}
static char *
has_c_format_att(
int ncid, /* netcdf id */
int varid /* variable id */
)
{
nc_type cfmt_type;
size_t cfmt_len;
#define C_FMT_NAME "C_format" /* name of C format attribute */
#define MAX_CFMT_LEN 100 /* max length of C format attribute */
static char cfmt[MAX_CFMT_LEN];
/* we expect nc_inq_att to fail if there is no "C_format" attribute */
int nc_stat = nc_inq_att(ncid, varid, "C_format", &cfmt_type, &cfmt_len);
switch(nc_stat) {
case NC_NOERR:
if (cfmt_type == NC_CHAR && cfmt_len != 0 && cfmt_len < MAX_CFMT_LEN) {
int nc_stat = nc_get_att_text(ncid, varid, "C_format", cfmt);
if(nc_stat != NC_NOERR) {
fprintf(stderr, "Getting 'C_format' attribute %s\n",
nc_strerror(nc_stat));
(void) fflush(stderr);
}
cfmt[cfmt_len] = '\0';
return &cfmt[0];
}
break;
case NC_ENOTATT:
break;
default:
fprintf(stderr, "Inquiring about 'C_format' attribute %s\n",
nc_strerror(nc_stat));
(void) fflush(stderr);
break;
}
return 0;
}
/* Return default format to use for a primitive type */
const char *
get_default_fmt(nc_type typeid) {
/* Otherwise return sensible default. */
switch (typeid) {
case NC_BYTE:
return "%d";
case NC_CHAR:
return "%s";
case NC_SHORT:
return "%d";
case NC_INT:
return "%d";
case NC_FLOAT:
return float_var_fmt;
case NC_DOUBLE:
return double_var_fmt;
case NC_UBYTE:
return "%u";
case NC_USHORT:
return "%u";
case NC_UINT:
return "%u";
case NC_INT64:
return "%lld";
case NC_UINT64:
return "%llu";
case NC_STRING:
return "\"%s\"";
default:
break;
}
return ""; /* user-defined types don't use fmt member */
}
/*
* Determine print format to use for each primitive value for this
* variable. Use value of attribute C_format if it exists, otherwise
* a sensible default.
*/
const char *
get_fmt(
int ncid,
int varid,
nc_type typeid
)
{
char *c_format_att;
/* float or double precision specified with -p option overrides any
C_format attribute value, so check for that first. */
if (float_precision_specified && typeid == NC_FLOAT)
return float_var_fmt;
if (double_precision_specified && typeid == NC_DOUBLE)
return double_var_fmt;
/* If C_format attribute exists, return it */
c_format_att = has_c_format_att(ncid, varid);
if (c_format_att)
return c_format_att;
return get_default_fmt(typeid);
}
/* Return primitive type name */
static const char *
prim_type_name(nc_type type)
{
switch (type) {
case NC_BYTE:
return "byte";
case NC_CHAR:
return "char";
case NC_SHORT:
return "short";
case NC_INT:
return "int";
case NC_FLOAT:
return "float";
case NC_DOUBLE:
return "double";
case NC_UBYTE:
return "ubyte";
case NC_USHORT:
return "ushort";
case NC_UINT:
return "uint";
case NC_INT64:
return "int64";
case NC_UINT64:
return "uint64";
case NC_STRING:
return "string";
default:
error("prim_type_name: bad type %d", type);
return "bogus";
}
}
static int max_type = 0;
static int max_atomic_type = 0;
static nctype_t **nctypes = 0; /* holds all types in a netCDF dataset */
#ifdef USE_NETCDF4
/* return number of user-defined types in a group and all its subgroups */
static int
count_udtypes(int ncid) {
int ntypes = 0;
int numgrps;
int *ncids;
int i;
int format;
NC_CHECK( nc_inq_format(ncid, &format) );
if (format == NC_FORMAT_NETCDF4) {
/* Get number of types in this group */
NC_CHECK( nc_inq_typeids(ncid, &ntypes, NULL) ) ;
NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) ) ;
ncids = (int *) emalloc(sizeof(int) * (numgrps + 1));
NC_CHECK( nc_inq_grps(ncid, NULL, ncids) ) ;
/* Add number of types in each subgroup, if any */
for (i=0; i < numgrps; i++) {
ntypes += count_udtypes(ncids[i]);
}
free(ncids);
}
return ntypes;
}
#endif /*USE_NETCDF4*/
/* This routine really is intended to return the max atomic typeid */
static int
max_typeid(int ncid) {
int maxtypes = NC_NAT;
int maxatomictypes = NC_NAT;
int format = 0;
int err = NC_NOERR;
/* get the file type */
err = nc_inq_format(ncid,&format);
if(err) {
fprintf(stderr,"%s: Cannot get file format.\n",nc_strerror(err));
return 0;
}
switch (format) {
case NC_FORMAT_CLASSIC:
case NC_FORMAT_NETCDF4_CLASSIC:
case NC_FORMAT_64BIT_OFFSET:
maxatomictypes = (maxtypes = NC_DOUBLE); /*ignore NC_NAT?*/
break;
case NC_FORMAT_64BIT_DATA:
maxatomictypes = (maxtypes = NC_UINT64);
break;
case NC_FORMAT_NETCDF4:
#ifdef USE_NETCDF4
{
int nuser = 0;
maxatomictypes = (maxtypes = NC_STRING); /* extra netCDF-4 primitive types */
maxtypes += 4; /* user-defined classes */
nuser = count_udtypes(ncid);
if(nuser > 0)
maxtypes = NC_FIRSTUSERTYPEID + (nuser - 1);
} break;
#else
/* fallthru */
#endif
default:
fprintf(stderr,"Unexpected file format: %d\n",format);
return 0;
}
max_type = maxtypes;
max_atomic_type = maxatomictypes;
return maxtypes;
}
void typeadd(nctype_t *typep) {
nctypes[typep->tid] = typep;
}
/* From type id, get full type info */
nctype_t *
get_typeinfo ( int typeid ) {
if(typeid < 0 || typeid > max_type)
error("ncdump: %d is an invalid type id", typeid);
return nctypes[typeid];
}
/* void */
/* xfree_typeinfo(int ncid) { */
/* int i; */
/* for (i = 0; i < number_of_types; i++) { */
/* nctype_t *tinfop = nctypes[i]; */
/* if (tinfop) { */
/* if(tinfop->name) */
/* free(tinfop->name); */
/* if(tinfop->grps) */
/* free(tinfop->grps); */
/* free(tinfop); */
/* } */
/* } */
/* } */
bool_t
ncbyte_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(signed char* )v1p == *(signed char* )v2p);
}
bool_t
ncchar_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(char* )v1p == *(char* )v2p);
}
bool_t
ncshort_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(short* )v1p == *(short* )v2p);
}
bool_t
ncint_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(int* )v1p == *(int* )v2p);
}
#define absval(x) ( (x) < 0 ? -(x) : (x) )
/*
* Return ( *(float* )v1p == *(float* )v2p);
* except use floating epsilon to compare very close vals as equal
* and handle IEEE NaNs and infinities.
*/
bool_t
ncfloat_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
float v1 = *(float* )v1p;
float v2 = *(float* )v2p;
if((v1 > 0.0f) != (v2 > 0.0f)) /* avoid overflow */
return false;
if(isfinite(v1) && isfinite(v2))
return (absval(v1 - v2) <= absval(float_eps * v2)) ;
if(isnan(v1) && isnan(v2))
return true;
if(isinf(v1) && isinf(v2))
return true;
return false;
}
/*
* Return ( *(double* )v1p == *(double* )v2p);
* except use floating epsilon to compare very close vals as equal
* and handle IEEE NaNs and infinities.
*/
bool_t
ncdouble_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
double v1 = *(double* )v1p;
double v2 = *(double* )v2p;
if((v1 > 0.0) != (v2 > 0.0)) /* avoid overflow */
return false;
if(isfinite(v1) && isfinite(v2))
return (absval(v1 - v2) <= absval(double_eps * v2)) ;
if(isnan(v1) && isnan(v2))
return true;
if(isinf(v1) && isinf(v2))
return true;
return false;
}
bool_t
ncubyte_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(unsigned char* )v1p == *(unsigned char* )v2p);
}
bool_t
ncushort_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(unsigned short* )v1p == *(unsigned short* )v2p);
}
bool_t
ncuint_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(unsigned int* )v1p == *(unsigned int* )v2p);
}
bool_t
ncint64_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(long long* )v1p == *(long long* )v2p);
}
bool_t
ncuint64_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
return ( *(unsigned long long* )v1p == *(unsigned long long* )v2p);
}
bool_t
ncstring_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
if (NULL == *((char **)v1p) && NULL == *((char **)v2p))
return(1);
else if (NULL != *((char **)v1p) && NULL == *((char **)v2p))
return(0);
else if (NULL == *((char **)v1p) && NULL != *((char **)v2p))
return(0);
return (strcmp(*((char **)v1p), *((char **)v2p)) == 0);
}
#ifdef USE_NETCDF4
bool_t
ncopaque_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
size_t nbytes = this->size;
const char *c1p = (const char *) v1p;
const char *c2p = (const char *) v2p;
int i;
for (i=0; i < nbytes; i++) {
if (*c1p++ != *c2p++)
return false;
}
return true;
}
bool_t
ncvlen_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
size_t v1len = ((nc_vlen_t *)v1p)->len;
size_t v2len = ((nc_vlen_t *)v2p)->len;
if (v1len != v2len)
return false;
{
size_t base_size = this->size;
nc_type base_type = this->base_tid;
nctype_t *base_info = get_typeinfo(base_type);
val_equals_func base_val_equals = base_info->val_equals;
const char *v1dat = ((nc_vlen_t *)v1p)->p;
const char *v2dat = ((nc_vlen_t *)v2p)->p;
size_t i;
for(i = 0; i < v1len; i++) {
if (base_val_equals(base_info, (const void *)v1dat,
(const void *)v2dat) != true)
return false;
v1dat += base_size;
v2dat += base_size;
}
}
return true;
}
/* Determine if two compound values are equal, by testing equality of
* each member field. */
bool_t
nccomp_val_equals(const nctype_t *this,
const void *v1p, const void *v2p) {
int nfields = this->nfields;
int fidx; /* field id */
for (fidx = 0; fidx < nfields; fidx++) {
size_t offset = this->offsets[fidx];
nc_type fid = this->fids[fidx]; /* field type id */
nctype_t *finfo = get_typeinfo(fid);
if(finfo->ranks == 0 || finfo->ranks[fidx] == 0) {
if(! finfo->val_equals(finfo,
(char *)v1p + offset, (char *)v2p + offset))
return false;
} else { /* this field is an array */
int i; /* array element counter when rank > 0 */
void *v1elem = (char *)v1p + offset;
void *v2elem = (char *)v2p + offset;
for(i = 0; i < finfo->nvals[fidx]; i++) {
if(! finfo->val_equals(finfo, v1elem, v2elem))
return false;
v1elem = (char *)v1elem + finfo->size;
v2elem = (char *)v1elem + finfo->size;
}
}
}
return true;
}
#endif /* USE_NETCDF4 */
int
ncbyte_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(signed char *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncchar_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(char *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncshort_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(short *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncint_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(int *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
/* CDL canonical representations of some special floating point values */
#define NCDL_NANF "NaNf"
#define NCDL_NAN "NaN"
#define NCDL_INFF "Infinityf"
#define NCDL_INF "Infinity"
/* Convert a float NaN or Infinity to an allocated string large enough
* to hold it (at least PRIM_LEN chars) */
static void
float_special_tostring(float vv, char *sout) {
if(isnan(vv)) {
snprintf(sout, PRIM_LEN, "%s", NCDL_NANF);
} else if(isinf(vv)) {
if(vv < 0.0) {
snprintf(sout, PRIM_LEN, "-%s", NCDL_INFF);
} else {
snprintf(sout, PRIM_LEN, "%s", NCDL_INFF);
}
} else
assert(false); /* vv was finite */
}
/* Convert a double NaN or Infinity to an allocated string large enough
* to hold it (at least PRIM_LEN chars) */
static void
double_special_tostring(double vv, char *sout) {
if(isnan(vv)) {
snprintf(sout, PRIM_LEN, "%s", NCDL_NAN);
} else if(isinf(vv)) {
if(vv < 0.0) {
snprintf(sout, PRIM_LEN, "-%s", NCDL_INF);
} else {
snprintf(sout, PRIM_LEN, "%s", NCDL_INF);
}
} else
assert(false); /* vv was finite */
}
int
ncfloat_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
float vv = *(float *)valp;
if(isfinite(vv)) {
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, vv);
assert(res < PRIM_LEN);
} else {
float_special_tostring(vv, sout);
}
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncdouble_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
double vv = *(double *)valp;
if(isfinite(vv)) {
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, vv);
assert(res < PRIM_LEN);
} else {
double_special_tostring(vv, sout);
}
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncubyte_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(unsigned char *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncushort_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(unsigned short *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncuint_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(unsigned int *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncint64_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(long long *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncuint64_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, typ->fmt, *(unsigned long long *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int ncstring_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp)
{
const char *cp;
cp = ((char **)valp)[0];
if(cp) {
size_t slen;
char *sout;
char *sp;
unsigned char uc;
slen = 4 + 5 * strlen(cp); /* need "'s around string, and extra space to escape control characters */
slen++; /* nul term */
sout = emalloc(slen);
sp = sout;
*sp++ = '"' ;
while(*cp) {
switch (uc = *cp++ & 0377) {
case '\b':
*sp++ = '\\';
*sp++ = 'b' ;
break;
case '\f':
*sp++ = '\\';
*sp++ = 'f';
break;
case '\n':
*sp++ = '\\';
*sp++ = 'n';
break;
case '\r':
*sp++ = '\\';
*sp++ = 'r';
break;
case '\t':
*sp++ = '\\';
*sp++ = 't';
break;
case '\v':
*sp++ = '\\';
*sp++ = 'n';
break;
case '\\':
*sp++ = '\\';
*sp++ = '\\';
break;
case '\'':
*sp++ = '\\';
*sp++ = '\'';
break;
case '\"':
*sp++ = '\\';
*sp++ = '\"';
break;
default:
if (iscntrl(uc)) {
snprintf(sp,4+1,"\\%03o",uc); /* +1 for nul */
sp += 4;
}
else
*sp++ = uc;
break;
}
}
*sp++ = '"' ;
*sp = '\0' ;
sbuf_cpy(sfbf, sout);
free(sout);
}
else {
sbuf_cpy(sfbf, "NIL");
}
return sbuf_len(sfbf);
}
#ifdef USE_NETCDF4
int
ncenum_typ_tostring(const nctype_t *typ, safebuf_t *sfbf, const void *valp) {
char symbol[NC_MAX_NAME + 1];
long long val = 0;
switch (typ->base_tid) {
case NC_BYTE:
val = *(signed char *)valp;
break;
case NC_UBYTE:
val = *(unsigned char *)valp;
break;
case NC_SHORT:
val = *(short *)valp;
break;
case NC_USHORT:
val = *(unsigned short *)valp;
break;
case NC_INT:
val = *(int *)valp;
break;
case NC_UINT:
val = *(unsigned int *)valp;
break;
case NC_INT64:
val = *(long long *)valp;
break;
case NC_UINT64:
val = *(long long *)valp;
break;
default:
error("bad base type for enum");
break;
}
NC_CHECK( nc_inq_enum_ident(typ->ncid, typ->tid, val, symbol));
sbuf_cpy(sfbf, symbol);
return sbuf_len(sfbf);
}
/* Given an opaque type size and opaque value, convert to a string,
* represented as hexadecimal characters, returning number of chars in
* output string */
int
ncopaque_val_as_hex(size_t size, char *sout, const void *valp) {
const unsigned char *cp = valp;
char *sp = sout;
int i;
char *prefix = "0X";
int prelen = strlen(prefix);
snprintf(sp, prelen + 1, "%s", prefix);
sp += prelen;
for(i = 0; i < size; i++) {
int res;
res = snprintf(sp, prelen + 1, "%.2X", *cp++);
assert (res == 2);
sp += 2;
}
*sp = '\0';
return 2*size + prelen;
}
/* Convert an opaque value to a string, represented as hexadecimal
* characters */
int
ncopaque_typ_tostring(const nctype_t *typ, safebuf_t *sfbf,
const void *valp) {
char* sout = (char *) emalloc(2 * typ->size + strlen("0X") + 1);
(void) ncopaque_val_as_hex(typ->size, sout, valp);
sbuf_cpy(sfbf, sout);
free(sout);
return sbuf_len(sfbf);
}
/* Convert a vlen value to a string, by using tostring function for base type */
int
ncvlen_typ_tostring(const nctype_t *tinfo, safebuf_t *sfbf, const void *valp) {
nc_type base_type = tinfo->base_tid;
nctype_t *base_info = get_typeinfo(base_type);
size_t base_size = base_info->size;
size_t len = ((nc_vlen_t *)valp)->len;
typ_tostring_func base_typ_tostring = base_info->typ_tostring;
size_t i;
const char *vp; /* instead of void* so can increment to next */
safebuf_t* sout2 = sbuf_new();
sbuf_cpy(sfbf, "{");
/* put each val in sout2, then append sout2 to sfbf */
vp = ((nc_vlen_t *)valp)->p;
for(i = 0; i < len; i++) {
(void) base_typ_tostring(base_info, sout2, vp);
sbuf_catb(sfbf, sout2);
if(i < len - 1) {
sbuf_cat(sfbf, ", ");
}
vp += base_size;
}
sbuf_cat(sfbf, "}");
sbuf_free(sout2);
return sbuf_len(sfbf);
}
/*
* Print a number of char values as a text string.
*/
static int
chars_tostring(
safebuf_t *sbuf, /* for output */
size_t len, /* number of characters */
const char *vals /* pointer to block of values */
)
{
long iel;
const char *sp;
char *sout = (char *)emalloc(4*len + 5); /* max len of string */
char *cp = sout;
*cp++ = '"';
/* adjust len so trailing nulls don't get printed */
sp = vals + len;
while (len != 0 && *--sp == '\0')
len--;
for (iel = 0; iel < len; iel++) {
unsigned char uc;
switch (uc = *vals++ & 0377) {
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
case '\\':
case '\'':
case '\"':
*cp++ = '\\';
*cp++ = *(char *)&uc; /* just copy, even if char is signed */
break;
default:
if (isprint(uc))
*cp++ = *(char *)&uc; /* just copy, even if char is signed */
else {
sprintf(cp,"\\%.3o",uc);
cp += 4;
}
break;
}
}
*cp++ = '"';
*cp = '\0';
sbuf_cpy(sbuf, sout);
free(sout);
return sbuf_len(sbuf);
}
/* Convert a compound value to a string, by using tostring function for
each member field */
int
nccomp_typ_tostring(const nctype_t *tinfo, safebuf_t *sfbf, const void *valp) {
int nfields = tinfo->nfields;
int fidx; /* field id */
safebuf_t* sout2 = sbuf_new();
sbuf_cpy(sfbf, "{");
/* put each val in sout2, then append sout2 to sfbf if enough room */
for (fidx = 0; fidx < nfields; fidx++) {
size_t offset = tinfo->offsets[fidx];
nc_type fid = tinfo->fids[fidx]; /* field type id */
nctype_t *finfo = get_typeinfo(fid);
if(tinfo->ranks[fidx] == 0) {
if(finfo->tid == NC_CHAR) { /* aggregate char rows into strings */
chars_tostring(sout2, 1, ((char *)valp + offset));
} else {
finfo->typ_tostring(finfo, sout2, ((char *)valp + offset));
}
} else { /* this field is an array */
int i; /* array element counter when rank > 0 */
void *vp = (char *)valp + offset;
safebuf_t *sout3 = sbuf_new();
sbuf_cpy(sout2, "{");
if(finfo->tid == NC_CHAR) { /* aggregate char rows into strings */
int rank = tinfo->ranks[fidx];
size_t nstrings;
size_t slen;
int j;
slen = tinfo->sides[fidx][rank-1];
nstrings = 1; /* product of all but last array dimension */
for(j=0; j < rank-1; j++) {
nstrings *= tinfo->sides[fidx][j];
}
for(i=0; i < nstrings; i++) { /* loop on product of all but
last index of array */
chars_tostring(sout3, slen, (char *)vp);
vp = (char *)vp + slen;
if(i < nstrings - 1) {
sbuf_cat(sout3, ", ");
}
sbuf_catb(sout2, sout3);
}
} else {
for(i = 0; i < tinfo->nvals[fidx]; i++) {
(void) finfo->typ_tostring(finfo, sout3, vp);
vp = (char *)vp + finfo->size;
if(i < tinfo->nvals[fidx] - 1) {
sbuf_cat(sout3, ", ");
}
sbuf_catb(sout2, sout3);
}
}
sbuf_cat(sout2, "}");
sbuf_free(sout3);
}
sbuf_catb(sfbf, sout2);
if(fidx < nfields - 1) {
sbuf_cat(sfbf, ", ");
}
}
sbuf_cat(sfbf, "}");
sbuf_free(sout2);
return sbuf_len(sfbf);
}
#endif /* USE_NETCDF4 */
int
ncbyte_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(signed char *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncchar_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(char *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncshort_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(short *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncint_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(int *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncfloat_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
float vv = *(float *)valp;
if(isfinite(vv)) {
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, vv);
assert(res < PRIM_LEN);
} else {
float_special_tostring(vv, sout);
}
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncdouble_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
double vv = *(double *)valp;
if(isfinite(vv)) {
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, vv);
assert(res < PRIM_LEN);
} else {
double_special_tostring(vv, sout);
}
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
/* Convert value of any numeric type to a double. Beware, this may
* lose precision for values of type NC_INT64 or NC_UINT64 */
static
double to_double(const ncvar_t *varp, const void *valp) {
double dd = 0.0;
switch (varp->type) {
case NC_BYTE:
dd = *(signed char *)valp;
break;
case NC_SHORT:
dd = *(short *)valp;
break;
case NC_INT:
dd = *(int *)valp;
break;
case NC_FLOAT:
dd = *(float *)valp;
break;
case NC_DOUBLE:
dd = *(double *)valp;
break;
case NC_UBYTE:
dd = *(unsigned char *)valp;
break;
case NC_USHORT:
dd = *(unsigned short *)valp;
break;
case NC_UINT:
dd = *(unsigned int *)valp;
break;
case NC_INT64:
dd = *(long long *)valp;
break;
case NC_UINT64:
dd = *(unsigned long long *)valp;
break;
default:
error("to_double: type not numeric primitive");
}
return dd;
}
int
nctime_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
double vv = to_double(varp, valp);
int separator = formatting_specs.iso_separator ? 'T' : ' ';
if(isfinite(vv)) {
int oldopts = 0;
int newopts = 0;
int res;
sout[0]='"';
/* Make nctime dump error messages */
oldopts = cdSetErrOpts(0);
newopts = oldopts | CU_VERBOSE;
cdSetErrOpts(newopts);
cdRel2Iso(varp->timeinfo->calendar, varp->timeinfo->units, separator, vv, &sout[1]);
cdSetErrOpts(oldopts);
res = strlen(sout);
sout[res++] = '"';
sout[res] = '\0';
assert(res < PRIM_LEN);
} else {
double_special_tostring(vv, sout);
}
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncubyte_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(unsigned char *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncushort_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(unsigned short *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncuint_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(unsigned int *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncint64_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(long long *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncuint64_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
char sout[PRIM_LEN];
int res;
res = snprintf(sout, PRIM_LEN, varp->fmt, *(unsigned long long *)valp);
assert(res < PRIM_LEN);
sbuf_cpy(sfbf, sout);
return sbuf_len(sfbf);
}
int
ncstring_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
return ncstring_typ_tostring(varp->tinfo, sfbf, valp);
}
#ifdef USE_NETCDF4
int
ncenum_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
return ncenum_typ_tostring(varp->tinfo, sfbf, valp);
}
/* Convert an opaque value to a string, represented as hexadecimal
* characters */
int
ncopaque_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
return ncopaque_typ_tostring(varp->tinfo, sfbf, valp);
}
/* Convert a vlen value to a string, by using tostring function for base type */
int
ncvlen_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
return ncvlen_typ_tostring(varp->tinfo, sfbf, valp);
}
int
nccomp_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
return nccomp_typ_tostring(varp->tinfo, sfbf, valp);
}
#endif /*USE_NETCDF4*/
static val_equals_func eq_funcs[] = {
ncbyte_val_equals,
ncchar_val_equals,
ncshort_val_equals,
ncint_val_equals,
ncfloat_val_equals,
ncdouble_val_equals,
ncubyte_val_equals,
ncushort_val_equals,
ncuint_val_equals,
ncint64_val_equals,
ncuint64_val_equals,
ncstring_val_equals
};
static typ_tostring_func ts_funcs[] = {
ncbyte_typ_tostring,
ncchar_typ_tostring,
ncshort_typ_tostring,
ncint_typ_tostring,
ncfloat_typ_tostring,
ncdouble_typ_tostring,
ncubyte_typ_tostring,
ncushort_typ_tostring,
ncuint_typ_tostring,
ncint64_typ_tostring,
ncuint64_typ_tostring,
ncstring_typ_tostring
};
/* Set function pointer of function to convert a value to a string for
* the variable pointed to by varp. */
void
set_tostring_func(ncvar_t *varp) {
val_tostring_func tostring_funcs[] = {
ncbyte_val_tostring,
ncchar_val_tostring,
ncshort_val_tostring,
ncint_val_tostring,
ncfloat_val_tostring,
ncdouble_val_tostring,
ncubyte_val_tostring,
ncushort_val_tostring,
ncuint_val_tostring,
ncint64_val_tostring,
ncuint64_val_tostring,
ncstring_val_tostring
};
if(varp->has_timeval && formatting_specs.string_times) {
varp->val_tostring = (val_tostring_func) nctime_val_tostring;
return;
}
if( !is_user_defined_type(varp->type) ) {
varp->val_tostring = tostring_funcs[varp->type - 1];
return;
}
#ifdef USE_NETCDF4
switch(varp->tinfo->class) {
case NC_VLEN:
varp->val_tostring = (val_tostring_func) ncvlen_val_tostring;
break;
case NC_OPAQUE:
varp->val_tostring = (val_tostring_func) ncopaque_val_tostring;
break;
case NC_ENUM:
varp->val_tostring = (val_tostring_func) ncenum_val_tostring;
break;
case NC_COMPOUND:
varp->val_tostring = (val_tostring_func) nccomp_val_tostring;
break;
default:
error("unrecognized class of user defined type: %d",
varp->tinfo->class);
}
#endif /* USE_NETCDF4 */
return;
}
/* Initialize typelist with primitive types. For netCDF-3 only need primitive
types. */
static void
init_prim_types(int ncid) {
nctype_t *tp;
int i;
int types[] =
{
NC_BYTE,
NC_CHAR,
NC_SHORT,
NC_INT,
NC_FLOAT,
NC_DOUBLE,
NC_UBYTE,
NC_USHORT,
NC_UINT,
NC_INT64,
NC_UINT64,
NC_STRING
};
size_t sizes[] = {
sizeof(char),
sizeof(char),
sizeof(short),
sizeof(int),
sizeof(float),
sizeof(double),
sizeof(unsigned char),
sizeof(unsigned short),
sizeof(unsigned int),
sizeof(long long),
sizeof(unsigned long long),
sizeof(char **)
};
#if 0
for(i=0; i < sizeof(types)/sizeof(int); i++) {
#else
for(i=0; i < max_atomic_type; i++) {
#endif
tp = (nctype_t *)emalloc(sizeof(nctype_t));
tp->ncid = ncid;
tp->tid = types[i];
tp->name = strdup(prim_type_name(tp->tid));
tp->grps = 0;
tp->class = 0; /* primitive type */
tp->size = sizes[i];
tp->base_tid = NC_NAT; /* not used for primitive types */
tp->nfields = 0; /* not used for primitive types */
tp->fmt = get_default_fmt(types[i]);
tp->fids = 0; /* not used for primitive types */
tp->offsets = 0; /* not used for primitive types */
tp->ranks = 0; /* not used for primitive types */
tp->sides = 0; /* not used for primitive types */
tp->nvals = 0; /* not used for primitive types */
tp->val_equals = (val_equals_func) eq_funcs[i];
tp->typ_tostring = (typ_tostring_func) ts_funcs[i];
typeadd(tp);
}
}
/* Initialize typelist.
*
* This must be done over all groups in netCDF-4, because
* variables in one group may be declared using types in a
* different group. For netCDF-3, this is just the info about
* primitive types.
*/
void
init_types(int ncid) {
#ifdef USE_NETCDF4
int ntypes;
#endif
if (max_type == 0) { /* if called for first time */
int maxtype = max_typeid(ncid);
int i;
nctypes = (nctype_t **) emalloc((maxtype + 2) * sizeof(nctype_t *));
for(i=0; i < maxtype+1; i++)
nctypes[i] = NULL; /* so can later skip over unused type slots */
init_prim_types(ncid);
}
#ifdef USE_NETCDF4
/* Are there any user defined types in this group? */
NC_CHECK( nc_inq_typeids(ncid, &ntypes, NULL) );
if (ntypes)
{
int t;
int *typeids = emalloc((ntypes + 1) * sizeof(int));
NC_CHECK( nc_inq_typeids(ncid, NULL, typeids) );
for (t = 0; t < ntypes; t++) {
nctype_t *tinfo; /* details about the type */
char type_name[NC_MAX_NAME + 1];
size_t group_name_len;
char* group_name;
int fidx; /* for compound type, field index */
tinfo = (nctype_t *) emalloc(sizeof(nctype_t));
NC_CHECK( nc_inq_user_type(ncid, typeids[t], type_name, &tinfo->size,
&tinfo->base_tid, &tinfo->nfields,
&tinfo->class) );
tinfo->tid = typeids[t];
tinfo->ncid = ncid;
tinfo->name = strdup(type_name);
tinfo->grps = 0;
if(tinfo->class == NC_VLEN) {
tinfo->size = sizeof(nc_vlen_t); /* not size of base type */
}
NC_CHECK( nc_inq_grpname_full(ncid, &group_name_len, NULL) );
group_name = (char *) emalloc(group_name_len + 1);
NC_CHECK( nc_inq_grpname_full(ncid, &group_name_len, group_name) );
tinfo->grps = strdup(group_name);
free(group_name);
switch(tinfo->class) {
case NC_ENUM:
tinfo->val_equals = eq_funcs[tinfo->base_tid-1];
tinfo->typ_tostring = (typ_tostring_func) ncenum_typ_tostring;
break;
case NC_COMPOUND:
tinfo->val_equals = (val_equals_func) nccomp_val_equals;
tinfo->typ_tostring = (typ_tostring_func) nccomp_typ_tostring;
tinfo->fids = (nc_type *) emalloc((tinfo->nfields + 1)
* sizeof(nc_type));
tinfo->offsets = (size_t *) emalloc((tinfo->nfields + 1)
* sizeof(size_t));
tinfo->ranks = (int *) emalloc((tinfo->nfields + 1)
* sizeof(int));
tinfo->sides = (int **) emalloc((tinfo->nfields + 1)
* sizeof(int *));
tinfo->nvals = (int *) emalloc((tinfo->nfields + 1)
* sizeof(int));
for (fidx = 0; fidx < tinfo->nfields; fidx++) {
size_t offset;
nc_type ftype;
int rank;
int *sides;
int i;
sides = NULL;
NC_CHECK( nc_inq_compound_field(ncid, tinfo->tid, fidx, NULL,
&offset, &ftype, &rank,
sides) );
if(rank > 0) sides = (int *) emalloc(rank * sizeof(int));
NC_CHECK( nc_inq_compound_field(ncid, tinfo->tid, fidx, NULL,
NULL, NULL, NULL, sides) );
tinfo->fids[fidx] = ftype;
tinfo->offsets[fidx] = offset;
tinfo->ranks[fidx] = rank;
if (rank > 0)
tinfo->sides[fidx] = (int *) emalloc(rank * sizeof(int));
tinfo->nvals[fidx] = 1;
for(i = 0; i < rank; i++) {
tinfo->sides[fidx][i] = sides[i];
tinfo->nvals[fidx] *= sides[i];
}
if (rank > 0)
free(sides);
}
break;
case NC_VLEN:
tinfo->val_equals = (val_equals_func) ncvlen_val_equals;
tinfo->typ_tostring = (typ_tostring_func) ncvlen_typ_tostring;
break;
case NC_OPAQUE:
tinfo->val_equals = (val_equals_func) ncopaque_val_equals;
tinfo->typ_tostring = (typ_tostring_func) ncopaque_typ_tostring;
break;
default:
error("bad class: %d", tinfo->class);
break;
}
typeadd(tinfo);
}
free(typeids);
}
/* For netCDF-4, check to see if this group has any subgroups and call
* recursively on each of them. */
{
int g, numgrps, *ncids;
/* See how many groups there are. */
NC_CHECK( nc_inq_grps(ncid, &numgrps, NULL) );
if (numgrps > 0) {
ncids = (int *) emalloc(numgrps * sizeof(int));
/* Get the list of group ids. */
NC_CHECK( nc_inq_grps(ncid, NULL, ncids) );
/* Call this function for each group. */
for (g = 0; g < numgrps; g++) {
init_types(ncids[g]);
}
free(ncids);
}
}
#endif /* USE_NETCDF4 */
}
/*
* return 1 if varid identifies a coordinate variable
* else return 0
*/
int
iscoordvar(int ncid, int varid)
{
int ndims, ndims1;
int dimid;
int* dimids = 0;
ncdim_t *dims = 0;
#ifdef USE_NETCDF4
int include_parents = 1;
#endif
int is_coord = 0; /* true if variable is a coordinate variable */
char varname[NC_MAX_NAME];
int varndims;
do { /* be safe in case someone is currently adding
* dimensions */
#ifdef USE_NETCDF4
NC_CHECK( nc_inq_dimids(ncid, &ndims, NULL, include_parents ) );
#else
NC_CHECK( nc_inq_ndims(ncid, &ndims) );
#endif
if (dims)
free(dims);
dims = (ncdim_t *) emalloc((ndims + 1) * sizeof(ncdim_t));
if (dimids)
free(dimids);
dimids = (int *) emalloc((ndims + 1) * sizeof(int));
#ifdef USE_NETCDF4
NC_CHECK( nc_inq_dimids(ncid, &ndims1, dimids, include_parents ) );
#else
{
int i;
for(i = 0; i < ndims; i++) {
dimids[i] = i; /* for netCDF-3, dimids are 0, 1, ..., ndims-1 */
}
NC_CHECK( nc_inq_ndims(ncid, &ndims1) );
}
#endif /* USE_NETCDF4 */
} while (ndims != ndims1);
for (dimid = 0; dimid < ndims; dimid++) {
NC_CHECK( nc_inq_dimname(ncid, dimids[dimid], dims[dimid].name) );
}
NC_CHECK( nc_inq_varname(ncid, varid, varname) );
NC_CHECK( nc_inq_varndims(ncid, varid, &varndims) );
for (dimid = 0; dimid < ndims; dimid++) {
if (strcmp(dims[dimid].name, varname) == 0 && varndims == 1) {
is_coord = 1;
break;
}
}
if(dims)
free(dims);
if(dimids)
free(dimids);
return is_coord;
}
/* Return true if user-defined type */
int
is_user_defined_type(nc_type type) {
nctype_t *typeinfop = get_typeinfo(type);
return (typeinfop->class > 0);
}
/*
* Return name of type in user-allocated space, whether built-in
* primitive type or user-defined type. Note: name must have enough
* space allocated to hold type name.
*/
void
get_type_name(int ncid, nc_type type, char *name)
{
#ifdef USE_NETCDF4
if (is_user_defined_type(type)) {
NC_CHECK(nc_inq_user_type(ncid, type, name, NULL, NULL, NULL, NULL));
} else {
strncpy(name, prim_type_name(type), NC_MAX_NAME + 1);
}
#else
strncpy(name, prim_type_name(type), NC_MAX_NAME + 1);
#endif /* USE_NETCDF4 */
}
/*
* Print type name with CDL escapes for special characters. locid is
* the id of the group in which the type is referenced, which is
* needed to determine whether an absolute type name must be printed.
* If the type is defined in the referenced group or in some ancestor
* group, only the simple type name is printed. If the type is
* defined in some other non-ancestor group, an absolute path for the
* typename is printed instead.
*/
void
print_type_name(int locid, int typeid) {
char *ename;
#ifdef USE_NETCDF4
char name[NC_MAX_NAME+1];
int type_inherited = 0;
int curlocid; /* group we are searching in */
int parent_groupid = locid;
int ntypes;
int stat;
#endif
assert(typeid > 0 && typeid <= max_type);
ename = escaped_name(nctypes[typeid]->name);
#ifdef USE_NETCDF4
if(is_user_defined_type(typeid)) {
/* determine if type is inherited, that is if defined in this
* group or any ancestor group */
name[NC_MAX_NAME] = '\0';
strncpy(name,nctypes[typeid]->name,NC_MAX_NAME);
do {
curlocid = parent_groupid;
NC_CHECK( nc_inq_typeids(curlocid, &ntypes, NULL) );
if(ntypes > 0) {
int *typeids = (int *) emalloc((ntypes + 1) * sizeof(int));
int i;
NC_CHECK( nc_inq_typeids(curlocid, &ntypes, typeids) );
for(i = 0; i < ntypes; i++) {
char curname[NC_MAX_NAME];
NC_CHECK( nc_inq_type(curlocid, typeids[i], curname, NULL) );
if(strncmp(name, curname, NC_MAX_NAME) == 0) {
type_inherited = 1;
break;
}
}
free(typeids);
if(type_inherited)
break;
}
stat = nc_inq_grp_parent(curlocid, &parent_groupid);
} while (stat != NC_ENOGRP && stat != NC_ENOTNC4);
if (type_inherited == 0) {
char *gname = nctypes[typeid]->grps;
print_name(gname);
fputs("/", stdout);
}
}
#endif /* USE_NETCDF4 */
fputs(ename, stdout);
free(ename);
}
/* Allocate and initialize table of unlimited dimensions for ncid, for
* use by is_unlim_dim() function. If ncid is a subgroup of a netCDF
* dataset, the table will still be initialized for the whole dataset
* in which the subgroup resides. */
#ifdef USE_NETCDF4
static int
init_is_unlim(int ncid, int **is_unlim_p)
{
int num_grps; /* total number of groups */
int num_dims = 0; /* total number of dimensions in all groups */
int num_undims = 0; /* total number of unlimited dimensions in all groups */
int *grpids = NULL; /* temporary list of all grpids */
int igrp;
int grpid;
/* if ncid is not root group, find its ancestor root group id */
int status = nc_inq_grp_parent(ncid, &grpid);
while(status == NC_NOERR && grpid != ncid) {
ncid = grpid;
status = nc_inq_grp_parent(ncid, &grpid);
}
if (status != NC_ENOGRP)
return NC_EBADGRPID;
/* Now ncid is root group. Get total number of groups and their ids */
NC_CHECK( nc_inq_grps_full(ncid, &num_grps, NULL) );
grpids = emalloc((num_grps + 1) * sizeof(int));
NC_CHECK( nc_inq_grps_full(ncid, &num_grps, grpids) );
#define DONT_INCLUDE_PARENTS 0
/* Get all dimensions in groups and info about which ones are unlimited */
for(igrp = 0; igrp < num_grps; igrp++) {
int ndims;
grpid = grpids[igrp];
NC_CHECK( nc_inq_dimids(grpid, &ndims, NULL, DONT_INCLUDE_PARENTS) );
num_dims += ndims;
}
*is_unlim_p = emalloc((num_dims + 1) * sizeof(int));
for(igrp = 0; igrp < num_grps; igrp++) {
int ndims, idim, *dimids, nundims;
grpid = grpids[igrp];
NC_CHECK( nc_inq_dimids(grpid, &ndims, NULL, DONT_INCLUDE_PARENTS) );
dimids = emalloc((ndims + 1) * sizeof(int));
NC_CHECK( nc_inq_dimids(grpid, &ndims, dimids, DONT_INCLUDE_PARENTS) );
/* mark all dims in this group as fixed-size */
for(idim = 0; idim < ndims; idim++) {
(*is_unlim_p)[dimids[idim]] = 0;
}
NC_CHECK( nc_inq_unlimdims(grpid, &nundims, dimids) );
assert(nundims <= ndims);
/* mark the subset of dims in this group that are unlimited */
for(idim = 0; idim < nundims; idim++) {
(*is_unlim_p)[dimids[idim]] = 1;
num_undims++;
}
if(dimids)
free(dimids);
}
free(grpids);
return NC_NOERR;
}
#endif /* USE_NETCDF4 */
/* TODO: make list of these arrays for multiple open datasets, such as
* the idnode_t lists above. For now, we just have one of these, for
* the unique input dataset for this invocation of ncdump. */
#define UNLIM_NOT_INITIALIZED (-1)
/* Is dimid the dimension ID of an unlimited dimension? */
bool_t
is_unlim_dim(int ncid, int dimid) {
bool_t result; /* 0 if fixed, 1 if unlimited size */
static int for_ncid = UNLIM_NOT_INITIALIZED; /* ensure only ever called for one ncid */
#ifdef USE_NETCDF4
static int *is_unlim = NULL; /* gets allocated by init_is_unlim() */
if(for_ncid == UNLIM_NOT_INITIALIZED) {
NC_CHECK(init_is_unlim(ncid, &is_unlim));
for_ncid = ncid;
}
assert(is_unlim);
result = is_unlim[dimid]; /* 0 if fixed, 1 if unlimited size */
#else
static int unlimdimid;
if(for_ncid == UNLIM_NOT_INITIALIZED) {
NC_CHECK( nc_inq_unlimdim(ncid, &unlimdimid) );
for_ncid = ncid;
}
result = (dimid == unlimdimid) ;
#endif /* USE_NETCDF4 */
return result;
}
|