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 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083 11084 11085 11086 11087 11088 11089 11090 11091 11092 11093 11094 11095 11096 11097 11098 11099 11100 11101 11102 11103 11104 11105 11106 11107 11108 11109 11110 11111 11112 11113 11114 11115 11116 11117 11118 11119 11120 11121 11122 11123 11124 11125 11126 11127 11128 11129 11130 11131 11132 11133 11134 11135 11136 11137 11138 11139 11140 11141 11142 11143 11144 11145 11146 11147 11148 11149 11150 11151 11152 11153 11154 11155 11156 11157 11158 11159 11160 11161 11162 11163 11164 11165 11166 11167 11168 11169 11170 11171 11172 11173 11174 11175 11176 11177 11178 11179 11180 11181 11182 11183 11184 11185 11186 11187 11188 11189 11190 11191 11192 11193 11194 11195 11196 11197 11198 11199 11200 11201 11202 11203 11204 11205 11206 11207 11208 11209 11210 11211 11212 11213 11214 11215 11216 11217 11218 11219 11220 11221 11222 11223 11224 11225 11226 11227 11228 11229 11230 11231 11232 11233 11234 11235 11236 11237 11238 11239 11240 11241 11242 11243 11244 11245 11246 11247 11248 11249 11250 11251 11252 11253 11254 11255 11256 11257 11258 11259 11260 11261 11262 11263 11264 11265 11266 11267 11268 11269 11270 11271 11272 11273 11274 11275 11276 11277 11278 11279 11280 11281 11282 11283 11284 11285 11286 11287 11288 11289 11290 11291 11292 11293 11294 11295 11296 11297 11298 11299 11300 11301 11302 11303 11304 11305 11306 11307 11308 11309 11310 11311 11312 11313 11314 11315 11316 11317 11318 11319 11320 11321 11322 11323 11324 11325 11326 11327 11328 11329 11330 11331 11332 11333 11334 11335 11336 11337 11338 11339 11340 11341 11342 11343 11344 11345 11346 11347 11348 11349 11350 11351 11352 11353 11354 11355 11356 11357 11358 11359 11360 11361 11362 11363 11364 11365 11366 11367 11368 11369 11370 11371 11372 11373 11374 11375 11376 11377 11378 11379 11380 11381 11382 11383 11384 11385 11386 11387 11388 11389 11390 11391 11392 11393 11394 11395 11396 11397 11398 11399 11400 11401 11402 11403 11404 11405 11406 11407 11408 11409 11410 11411 11412 11413 11414 11415 11416 11417 11418 11419 11420 11421 11422 11423 11424 11425 11426 11427 11428 11429 11430 11431 11432 11433 11434 11435 11436 11437 11438 11439 11440 11441 11442 11443 11444 11445 11446 11447 11448 11449 11450 11451 11452 11453 11454 11455 11456 11457 11458 11459 11460 11461 11462 11463 11464 11465 11466 11467 11468 11469 11470 11471 11472 11473 11474 11475 11476 11477 11478 11479 11480 11481 11482 11483 11484 11485 11486 11487 11488 11489 11490 11491 11492 11493 11494 11495 11496 11497 11498 11499 11500 11501 11502 11503 11504 11505 11506 11507 11508 11509 11510 11511 11512 11513 11514 11515 11516 11517 11518 11519 11520 11521 11522 11523 11524 11525 11526 11527 11528 11529 11530 11531 11532 11533 11534 11535 11536 11537 11538 11539 11540 11541 11542 11543 11544 11545 11546 11547 11548 11549 11550 11551 11552 11553 11554 11555 11556 11557 11558 11559 11560 11561 11562 11563 11564 11565 11566 11567 11568 11569 11570 11571 11572 11573 11574 11575 11576 11577 11578 11579 11580 11581 11582 11583 11584 11585 11586 11587 11588 11589 11590 11591 11592 11593 11594 11595 11596 11597 11598 11599 11600 11601 11602 11603 11604 11605 11606 11607 11608 11609 11610 11611 11612 11613 11614 11615 11616 11617 11618 11619 11620 11621 11622 11623 11624 11625 11626 11627 11628 11629 11630 11631 11632 11633 11634 11635 11636 11637 11638 11639 11640 11641 11642 11643 11644 11645 11646 11647 11648 11649 11650 11651 11652 11653 11654 11655 11656 11657 11658 11659 11660 11661 11662 11663 11664 11665 11666 11667 11668 11669 11670 11671 11672 11673 11674 11675 11676 11677 11678 11679 11680 11681 11682 11683 11684 11685 11686 11687 11688 11689 11690 11691 11692 11693 11694 11695 11696 11697 11698 11699 11700 11701 11702 11703 11704 11705 11706 11707 11708 11709 11710 11711 11712 11713 11714 11715 11716 11717 11718 11719 11720 11721 11722 11723 11724 11725 11726 11727 11728 11729 11730 11731 11732 11733 11734 11735 11736 11737 11738 11739 11740 11741 11742 11743 11744 11745 11746 11747 11748 11749 11750 11751 11752 11753 11754 11755 11756 11757 11758 11759 11760 11761 11762 11763 11764 11765 11766 11767 11768 11769 11770 11771 11772 11773 11774 11775 11776 11777 11778 11779 11780 11781 11782 11783 11784 11785 11786 11787 11788 11789 11790 11791 11792 11793 11794 11795 11796 11797 11798 11799 11800 11801 11802 11803 11804 11805 11806 11807 11808 11809 11810 11811 11812 11813 11814 11815 11816 11817 11818 11819 11820 11821 11822 11823 11824 11825 11826 11827 11828 11829 11830 11831 11832 11833 11834 11835 11836 11837 11838 11839 11840 11841 11842 11843 11844 11845 11846 11847 11848 11849 11850 11851 11852 11853 11854 11855 11856 11857 11858 11859 11860 11861 11862 11863 11864 11865 11866 11867 11868 11869 11870 11871 11872 11873 11874 11875 11876 11877 11878 11879 11880 11881 11882 11883 11884 11885 11886 11887 11888 11889 11890 11891 11892 11893 11894 11895 11896 11897 11898 11899 11900 11901 11902 11903 11904 11905 11906 11907 11908 11909 11910 11911 11912 11913 11914 11915 11916 11917 11918 11919 11920 11921 11922 11923 11924 11925 11926 11927 11928 11929 11930 11931 11932 11933 11934 11935 11936 11937 11938 11939 11940 11941 11942 11943 11944 11945 11946 11947 11948 11949 11950 11951 11952 11953 11954 11955 11956 11957 11958 11959 11960 11961 11962 11963 11964 11965 11966 11967 11968 11969 11970 11971 11972 11973 11974 11975 11976 11977 11978 11979 11980 11981 11982 11983 11984 11985 11986 11987 11988 11989 11990 11991 11992 11993 11994 11995 11996 11997 11998 11999 12000 12001 12002 12003 12004 12005 12006 12007 12008 12009 12010 12011 12012 12013 12014 12015 12016 12017 12018 12019 12020 12021 12022 12023 12024 12025 12026 12027 12028 12029 12030 12031 12032 12033 12034 12035 12036 12037 12038 12039 12040 12041 12042 12043 12044 12045 12046 12047 12048 12049 12050 12051 12052 12053 12054 12055 12056 12057 12058 12059 12060 12061 12062 12063 12064 12065 12066 12067 12068 12069 12070 12071 12072 12073 12074 12075 12076 12077 12078 12079 12080 12081 12082 12083 12084 12085 12086 12087 12088 12089 12090 12091 12092 12093 12094 12095 12096 12097 12098 12099 12100 12101 12102 12103 12104 12105 12106 12107 12108 12109 12110 12111 12112 12113 12114 12115 12116 12117 12118 12119 12120 12121 12122 12123 12124 12125 12126 12127 12128 12129 12130 12131 12132 12133 12134 12135 12136 12137 12138 12139 12140 12141 12142 12143 12144 12145 12146 12147 12148 12149 12150 12151 12152 12153 12154 12155 12156 12157 12158 12159 12160 12161 12162 12163 12164 12165 12166 12167 12168 12169 12170 12171 12172 12173 12174 12175 12176 12177 12178 12179 12180 12181 12182 12183 12184 12185 12186 12187 12188 12189 12190 12191 12192 12193 12194 12195 12196 12197 12198 12199 12200 12201 12202 12203 12204 12205 12206 12207 12208 12209 12210 12211 12212 12213 12214 12215 12216 12217 12218 12219 12220 12221 12222 12223 12224 12225 12226 12227 12228 12229 12230 12231 12232 12233 12234 12235 12236 12237 12238 12239 12240 12241 12242 12243 12244 12245 12246 12247 12248 12249 12250 12251 12252 12253 12254 12255 12256 12257 12258 12259 12260 12261 12262 12263 12264 12265 12266 12267 12268 12269 12270 12271 12272 12273 12274 12275 12276 12277 12278 12279 12280 12281 12282 12283 12284 12285 12286 12287 12288 12289 12290 12291 12292 12293 12294 12295 12296 12297 12298 12299 12300 12301 12302 12303 12304 12305 12306 12307 12308 12309 12310 12311 12312 12313 12314 12315 12316 12317 12318 12319 12320 12321 12322 12323 12324 12325 12326 12327 12328 12329 12330 12331 12332 12333 12334 12335 12336 12337 12338 12339 12340 12341 12342 12343 12344 12345 12346 12347 12348 12349 12350 12351 12352 12353 12354 12355 12356 12357 12358 12359 12360 12361 12362 12363 12364 12365 12366 12367 12368 12369 12370 12371 12372 12373 12374 12375 12376 12377 12378 12379 12380 12381 12382 12383 12384 12385 12386 12387 12388 12389 12390 12391 12392 12393 12394 12395 12396 12397 12398 12399 12400 12401 12402 12403 12404 12405 12406 12407 12408 12409 12410 12411 12412 12413 12414 12415 12416 12417 12418 12419 12420 12421 12422 12423 12424 12425 12426 12427 12428 12429 12430 12431 12432 12433 12434 12435 12436 12437 12438 12439 12440 12441 12442 12443 12444 12445 12446 12447 12448 12449 12450 12451 12452 12453 12454 12455 12456 12457 12458 12459 12460 12461 12462 12463 12464 12465 12466 12467 12468 12469 12470 12471 12472 12473 12474 12475 12476 12477 12478 12479 12480 12481 12482 12483 12484 12485 12486 12487 12488 12489 12490 12491 12492 12493 12494 12495 12496 12497 12498 12499 12500 12501 12502 12503 12504 12505 12506 12507 12508 12509 12510 12511 12512 12513 12514 12515 12516 12517 12518 12519 12520 12521 12522 12523 12524 12525 12526 12527 12528 12529 12530 12531 12532 12533 12534 12535 12536 12537 12538 12539 12540 12541 12542 12543 12544 12545 12546 12547 12548 12549 12550 12551 12552 12553 12554 12555 12556 12557 12558 12559 12560 12561 12562 12563 12564 12565 12566 12567 12568 12569 12570 12571 12572 12573 12574 12575 12576 12577 12578 12579 12580 12581 12582 12583 12584 12585 12586 12587 12588 12589 12590 12591 12592 12593 12594 12595 12596 12597 12598 12599 12600 12601 12602 12603 12604 12605 12606 12607 12608 12609 12610 12611 12612 12613 12614 12615 12616 12617 12618 12619 12620 12621 12622 12623 12624 12625 12626 12627 12628 12629 12630 12631 12632 12633 12634 12635 12636 12637 12638 12639 12640 12641 12642 12643 12644 12645 12646 12647 12648 12649 12650 12651 12652 12653 12654 12655 12656 12657 12658 12659 12660 12661 12662 12663 12664 12665 12666 12667 12668 12669 12670 12671 12672 12673 12674 12675 12676 12677 12678 12679 12680 12681 12682 12683 12684 12685 12686 12687 12688 12689 12690 12691 12692 12693 12694 12695 12696 12697 12698 12699 12700 12701 12702 12703 12704 12705 12706 12707 12708 12709 12710 12711 12712 12713 12714 12715 12716 12717 12718 12719 12720 12721 12722 12723 12724 12725 12726 12727 12728 12729 12730 12731 12732 12733 12734 12735 12736 12737 12738 12739 12740 12741 12742 12743 12744 12745 12746 12747 12748 12749 12750 12751 12752 12753 12754 12755 12756 12757 12758 12759 12760 12761 12762 12763 12764 12765 12766 12767 12768 12769 12770 12771 12772 12773 12774 12775 12776 12777 12778 12779 12780 12781 12782 12783 12784 12785 12786 12787 12788 12789 12790 12791 12792 12793 12794 12795 12796 12797 12798 12799 12800 12801 12802 12803 12804 12805 12806 12807 12808 12809 12810 12811 12812 12813 12814 12815 12816 12817 12818 12819 12820 12821 12822 12823 12824 12825 12826 12827 12828 12829 12830 12831 12832 12833 12834 12835 12836 12837 12838 12839 12840 12841 12842 12843 12844 12845 12846 12847 12848 12849 12850 12851 12852 12853 12854 12855 12856 12857 12858 12859 12860 12861 12862 12863 12864 12865 12866 12867 12868 12869 12870 12871 12872 12873 12874 12875 12876 12877 12878 12879 12880 12881 12882 12883 12884 12885 12886 12887 12888 12889 12890 12891 12892 12893 12894 12895 12896 12897 12898 12899 12900 12901 12902 12903 12904 12905 12906 12907 12908 12909 12910 12911 12912 12913 12914 12915 12916 12917 12918 12919 12920 12921 12922 12923 12924 12925 12926 12927 12928 12929 12930 12931 12932 12933 12934 12935 12936 12937 12938 12939 12940 12941 12942 12943 12944 12945 12946 12947 12948 12949 12950 12951 12952 12953 12954 12955 12956 12957 12958 12959 12960 12961 12962 12963 12964 12965 12966 12967 12968 12969 12970 12971 12972 12973 12974 12975 12976 12977 12978 12979 12980 12981 12982 12983 12984 12985 12986 12987 12988 12989 12990 12991 12992 12993 12994 12995 12996 12997 12998 12999 13000 13001 13002 13003 13004 13005 13006 13007 13008 13009 13010 13011 13012 13013 13014 13015 13016 13017 13018 13019 13020 13021 13022 13023 13024 13025 13026 13027 13028 13029 13030 13031 13032 13033 13034 13035 13036 13037 13038 13039 13040 13041 13042 13043 13044 13045 13046 13047 13048 13049 13050 13051 13052 13053 13054 13055 13056 13057 13058 13059 13060 13061 13062 13063 13064 13065 13066 13067 13068 13069 13070 13071 13072 13073 13074 13075 13076 13077 13078 13079 13080 13081 13082 13083 13084 13085 13086 13087 13088 13089 13090 13091 13092 13093 13094 13095 13096 13097 13098 13099 13100 13101 13102 13103 13104 13105 13106 13107 13108 13109 13110 13111 13112 13113 13114 13115 13116 13117 13118 13119 13120 13121 13122 13123 13124 13125 13126 13127 13128 13129 13130 13131 13132 13133 13134 13135 13136 13137 13138 13139 13140 13141 13142 13143 13144 13145 13146 13147 13148 13149 13150 13151 13152 13153 13154 13155 13156 13157 13158 13159 13160 13161 13162 13163 13164 13165 13166 13167 13168 13169 13170 13171 13172 13173 13174 13175 13176 13177 13178 13179 13180 13181 13182 13183 13184 13185 13186 13187 13188 13189 13190 13191 13192 13193 13194 13195 13196 13197 13198 13199 13200 13201 13202 13203 13204 13205 13206 13207 13208 13209 13210 13211 13212 13213 13214 13215 13216 13217 13218 13219 13220 13221 13222 13223 13224 13225 13226 13227 13228 13229 13230 13231 13232 13233 13234 13235 13236 13237 13238 13239 13240 13241 13242 13243 13244 13245 13246 13247 13248 13249 13250 13251 13252 13253 13254 13255 13256 13257 13258 13259 13260 13261 13262 13263 13264 13265 13266 13267 13268 13269 13270 13271 13272 13273 13274 13275 13276 13277 13278 13279 13280 13281 13282 13283 13284 13285 13286 13287 13288 13289 13290 13291 13292 13293 13294 13295 13296 13297 13298 13299 13300 13301 13302 13303 13304 13305 13306 13307 13308 13309 13310 13311 13312 13313 13314 13315 13316 13317 13318 13319 13320 13321 13322 13323 13324 13325 13326 13327 13328 13329 13330 13331 13332 13333 13334 13335 13336 13337 13338 13339 13340 13341 13342 13343 13344 13345 13346 13347 13348 13349 13350 13351 13352 13353 13354 13355 13356 13357 13358 13359 13360 13361 13362 13363 13364 13365 13366 13367 13368 13369 13370 13371 13372 13373 13374 13375 13376 13377 13378 13379 13380 13381 13382 13383 13384 13385 13386 13387 13388 13389 13390 13391 13392 13393 13394 13395 13396 13397 13398 13399 13400 13401 13402 13403 13404 13405 13406 13407 13408 13409 13410 13411 13412 13413 13414 13415 13416 13417 13418 13419 13420 13421 13422 13423 13424 13425 13426 13427 13428 13429 13430 13431 13432 13433 13434 13435 13436 13437 13438 13439 13440 13441 13442 13443 13444 13445 13446 13447 13448 13449 13450 13451 13452 13453 13454 13455 13456 13457 13458 13459 13460 13461 13462 13463 13464 13465 13466 13467 13468 13469 13470 13471 13472 13473 13474 13475 13476 13477 13478 13479 13480 13481 13482 13483 13484 13485 13486 13487 13488 13489 13490 13491 13492 13493 13494 13495 13496 13497 13498 13499 13500 13501 13502 13503 13504 13505 13506 13507 13508 13509 13510 13511 13512 13513 13514 13515 13516 13517 13518 13519 13520 13521 13522 13523 13524 13525 13526 13527 13528 13529 13530 13531 13532 13533 13534 13535 13536 13537 13538 13539 13540 13541 13542 13543 13544 13545 13546 13547 13548 13549 13550 13551 13552 13553 13554 13555 13556 13557 13558 13559 13560 13561 13562 13563 13564 13565 13566 13567 13568 13569 13570 13571 13572 13573 13574 13575 13576 13577 13578 13579 13580 13581 13582 13583 13584 13585 13586 13587 13588 13589 13590 13591 13592 13593 13594 13595 13596 13597 13598 13599 13600 13601 13602 13603 13604 13605 13606 13607 13608 13609 13610 13611 13612 13613 13614 13615 13616 13617 13618 13619 13620 13621 13622 13623 13624 13625 13626 13627 13628 13629 13630 13631 13632 13633 13634 13635 13636 13637 13638 13639 13640 13641 13642 13643 13644 13645 13646 13647 13648 13649 13650 13651 13652 13653 13654 13655 13656 13657 13658 13659 13660 13661 13662 13663 13664 13665 13666 13667 13668 13669 13670 13671 13672 13673 13674 13675 13676 13677 13678 13679 13680 13681 13682 13683 13684 13685 13686 13687 13688 13689 13690 13691 13692 13693 13694 13695 13696 13697 13698 13699 13700 13701 13702 13703 13704 13705 13706 13707 13708 13709 13710 13711 13712 13713 13714 13715 13716 13717 13718 13719 13720 13721 13722 13723 13724 13725 13726 13727 13728 13729 13730 13731 13732 13733 13734 13735 13736 13737 13738 13739 13740 13741 13742 13743 13744 13745 13746 13747 13748 13749 13750 13751 13752 13753 13754 13755 13756 13757 13758 13759 13760 13761 13762 13763 13764 13765 13766 13767 13768 13769 13770 13771 13772 13773 13774 13775 13776 13777 13778 13779 13780 13781 13782 13783 13784 13785 13786 13787 13788 13789 13790 13791 13792 13793 13794 13795 13796 13797 13798 13799 13800 13801 13802 13803 13804 13805 13806 13807 13808 13809 13810 13811 13812 13813 13814 13815 13816 13817 13818 13819 13820 13821 13822 13823 13824 13825 13826 13827 13828 13829 13830 13831 13832 13833 13834 13835 13836 13837 13838 13839 13840 13841 13842 13843 13844 13845 13846 13847 13848 13849 13850 13851 13852 13853 13854 13855 13856 13857 13858 13859 13860 13861 13862 13863 13864 13865 13866 13867 13868 13869 13870 13871 13872 13873 13874 13875 13876 13877 13878 13879 13880 13881 13882 13883 13884 13885 13886 13887 13888 13889 13890 13891 13892 13893 13894 13895 13896 13897 13898 13899 13900 13901 13902 13903 13904 13905 13906 13907 13908 13909 13910 13911 13912 13913 13914 13915 13916 13917 13918 13919 13920 13921 13922 13923 13924 13925 13926 13927 13928 13929 13930 13931 13932 13933 13934 13935 13936 13937 13938 13939 13940 13941 13942 13943 13944 13945 13946 13947 13948 13949 13950 13951 13952 13953 13954 13955 13956 13957 13958 13959 13960 13961 13962 13963 13964 13965 13966 13967 13968 13969 13970 13971 13972 13973 13974 13975 13976 13977 13978 13979 13980 13981 13982 13983 13984 13985 13986 13987 13988 13989 13990 13991 13992 13993 13994 13995 13996 13997 13998 13999 14000 14001 14002 14003 14004 14005 14006 14007 14008 14009 14010 14011 14012 14013 14014 14015 14016 14017 14018 14019 14020 14021 14022 14023 14024 14025 14026 14027 14028 14029 14030 14031 14032 14033 14034 14035 14036 14037 14038 14039 14040 14041 14042 14043 14044 14045 14046 14047 14048 14049 14050 14051 14052 14053 14054 14055 14056 14057 14058 14059 14060 14061 14062 14063 14064 14065 14066 14067 14068 14069 14070 14071 14072 14073 14074 14075 14076 14077 14078 14079 14080 14081 14082 14083 14084 14085 14086 14087 14088 14089 14090 14091 14092 14093 14094 14095 14096 14097 14098 14099 14100 14101 14102 14103 14104 14105 14106 14107 14108 14109 14110 14111 14112 14113 14114 14115 14116 14117 14118 14119 14120 14121 14122 14123 14124 14125 14126 14127 14128 14129 14130 14131 14132 14133 14134 14135 14136 14137 14138 14139 14140 14141 14142 14143 14144 14145 14146 14147 14148 14149 14150 14151 14152 14153 14154 14155 14156 14157 14158 14159 14160 14161 14162 14163 14164 14165 14166 14167 14168 14169 14170 14171 14172 14173 14174 14175 14176 14177 14178 14179 14180 14181 14182 14183 14184 14185 14186 14187 14188 14189 14190 14191 14192 14193 14194 14195 14196 14197 14198 14199 14200 14201 14202 14203 14204 14205 14206 14207 14208 14209 14210 14211 14212 14213 14214 14215 14216 14217 14218 14219 14220 14221 14222 14223 14224 14225 14226 14227 14228 14229 14230 14231 14232 14233 14234 14235 14236 14237 14238 14239 14240 14241 14242 14243 14244 14245 14246 14247 14248 14249 14250 14251 14252 14253 14254 14255 14256 14257 14258 14259 14260 14261 14262 14263 14264 14265 14266 14267 14268 14269 14270 14271 14272 14273 14274 14275 14276 14277 14278 14279 14280 14281 14282 14283 14284 14285 14286 14287 14288 14289 14290 14291 14292 14293 14294 14295 14296 14297 14298 14299 14300 14301 14302 14303 14304 14305 14306 14307 14308 14309 14310 14311 14312 14313 14314 14315 14316 14317 14318 14319 14320 14321 14322 14323 14324 14325 14326 14327 14328 14329 14330 14331 14332 14333 14334 14335 14336 14337 14338 14339 14340 14341 14342 14343 14344 14345 14346 14347 14348 14349 14350 14351 14352 14353 14354 14355 14356 14357 14358 14359 14360 14361 14362 14363 14364 14365 14366 14367 14368 14369 14370 14371 14372 14373 14374 14375 14376 14377 14378 14379 14380 14381 14382 14383 14384 14385 14386 14387 14388 14389 14390 14391 14392 14393 14394 14395 14396 14397 14398 14399 14400 14401 14402 14403 14404 14405 14406 14407 14408 14409 14410 14411 14412 14413 14414 14415 14416 14417 14418 14419 14420 14421 14422 14423 14424 14425 14426 14427 14428 14429 14430 14431 14432 14433 14434 14435 14436 14437 14438 14439 14440 14441 14442 14443 14444 14445 14446 14447 14448 14449 14450 14451 14452 14453 14454 14455 14456 14457 14458 14459 14460 14461 14462 14463 14464 14465 14466 14467 14468 14469 14470 14471 14472 14473 14474 14475 14476 14477 14478 14479 14480 14481 14482 14483 14484 14485 14486 14487 14488 14489 14490 14491 14492 14493 14494 14495 14496 14497 14498 14499 14500 14501 14502 14503 14504 14505 14506 14507 14508 14509 14510 14511 14512 14513 14514 14515 14516 14517 14518 14519 14520 14521 14522 14523 14524 14525 14526 14527 14528 14529 14530 14531 14532 14533 14534 14535 14536 14537 14538 14539 14540 14541 14542 14543 14544 14545 14546 14547 14548 14549 14550 14551 14552 14553 14554 14555 14556 14557 14558 14559 14560 14561 14562 14563 14564 14565 14566 14567 14568 14569 14570 14571 14572 14573 14574 14575 14576 14577 14578 14579 14580 14581 14582 14583 14584 14585 14586 14587 14588 14589 14590 14591 14592 14593 14594 14595 14596 14597 14598 14599 14600 14601 14602 14603 14604 14605 14606 14607 14608 14609 14610 14611 14612 14613 14614 14615 14616 14617 14618 14619 14620 14621 14622 14623 14624 14625 14626 14627 14628 14629 14630 14631 14632 14633 14634 14635 14636 14637 14638 14639 14640 14641 14642 14643 14644 14645 14646 14647 14648 14649 14650 14651 14652 14653 14654 14655 14656 14657 14658 14659 14660 14661 14662 14663 14664 14665 14666 14667 14668 14669 14670 14671 14672 14673 14674 14675 14676 14677 14678 14679 14680 14681 14682 14683 14684 14685 14686 14687 14688 14689 14690 14691 14692 14693 14694 14695 14696 14697 14698 14699 14700 14701 14702 14703 14704 14705 14706 14707 14708 14709 14710 14711 14712 14713 14714 14715 14716 14717 14718 14719 14720 14721 14722 14723 14724 14725 14726 14727 14728 14729 14730 14731 14732 14733 14734 14735 14736 14737 14738 14739 14740 14741 14742 14743 14744 14745 14746 14747 14748 14749 14750 14751 14752 14753 14754 14755 14756 14757 14758 14759 14760 14761 14762 14763 14764 14765 14766 14767 14768 14769 14770 14771 14772 14773 14774 14775 14776 14777 14778 14779 14780 14781 14782 14783 14784 14785 14786 14787 14788 14789 14790 14791 14792 14793 14794 14795 14796 14797 14798 14799 14800 14801 14802 14803 14804 14805 14806 14807 14808 14809 14810 14811 14812 14813 14814 14815 14816 14817 14818 14819 14820 14821 14822 14823 14824 14825 14826 14827 14828 14829 14830 14831 14832 14833 14834 14835 14836 14837 14838 14839 14840 14841 14842 14843 14844 14845 14846 14847 14848 14849 14850 14851 14852 14853 14854 14855 14856 14857 14858 14859 14860 14861 14862 14863 14864 14865 14866 14867 14868 14869 14870 14871 14872 14873 14874 14875 14876 14877 14878 14879 14880 14881 14882 14883 14884 14885 14886 14887 14888 14889 14890 14891 14892 14893 14894 14895 14896 14897 14898 14899 14900 14901 14902 14903 14904 14905 14906 14907 14908 14909 14910 14911 14912 14913 14914 14915 14916 14917 14918 14919 14920 14921 14922 14923 14924 14925 14926 14927 14928 14929 14930 14931 14932 14933 14934 14935 14936 14937 14938 14939 14940 14941 14942 14943 14944 14945 14946 14947 14948 14949 14950 14951 14952 14953 14954 14955 14956 14957 14958 14959 14960 14961 14962 14963 14964 14965 14966 14967 14968 14969 14970 14971 14972 14973 14974 14975 14976 14977 14978 14979 14980 14981 14982 14983 14984 14985 14986 14987 14988 14989 14990 14991 14992 14993 14994 14995 14996 14997 14998 14999 15000 15001 15002 15003 15004 15005 15006 15007 15008 15009 15010 15011 15012 15013 15014 15015 15016 15017 15018 15019 15020 15021 15022 15023 15024 15025 15026 15027 15028 15029 15030 15031 15032 15033 15034 15035 15036 15037 15038 15039 15040 15041 15042 15043 15044 15045 15046 15047 15048 15049 15050 15051 15052 15053 15054 15055 15056 15057 15058 15059 15060 15061 15062 15063 15064 15065 15066 15067 15068 15069 15070 15071 15072 15073 15074 15075 15076 15077 15078 15079 15080 15081 15082 15083 15084 15085 15086 15087 15088 15089 15090 15091 15092 15093 15094 15095 15096 15097 15098 15099 15100 15101 15102 15103 15104 15105 15106 15107 15108 15109 15110 15111 15112 15113 15114 15115 15116 15117 15118 15119 15120 15121 15122 15123 15124 15125 15126 15127 15128 15129 15130 15131 15132 15133 15134 15135 15136 15137 15138 15139 15140 15141 15142 15143 15144 15145 15146 15147 15148 15149 15150 15151 15152 15153 15154 15155 15156 15157 15158 15159 15160 15161 15162 15163 15164 15165 15166 15167 15168 15169 15170 15171 15172 15173 15174 15175 15176 15177 15178 15179 15180 15181 15182 15183 15184 15185 15186 15187 15188 15189 15190 15191 15192 15193 15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 15205 15206 15207 15208 15209 15210 15211 15212 15213 15214 15215 15216 15217 15218 15219 15220 15221 15222 15223 15224 15225 15226 15227 15228 15229 15230 15231 15232 15233 15234 15235 15236 15237 15238 15239 15240 15241 15242 15243 15244 15245 15246 15247 15248 15249 15250 15251 15252 15253 15254 15255 15256 15257 15258 15259 15260 15261 15262 15263 15264 15265 15266 15267 15268 15269 15270 15271 15272 15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 15321 15322 15323 15324 15325 15326 15327 15328 15329 15330 15331 15332 15333 15334 15335 15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 15347 15348 15349 15350 15351 15352 15353 15354 15355 15356 15357 15358 15359 15360 15361 15362 15363 15364 15365 15366 15367 15368 15369 15370 15371 15372 15373 15374 15375 15376 15377 15378 15379 15380 15381 15382 15383 15384 15385 15386 15387 15388 15389 15390 15391 15392 15393 15394 15395 15396 15397 15398 15399 15400 15401 15402 15403 15404 15405 15406 15407 15408 15409 15410 15411 15412 15413 15414 15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 15463 15464 15465 15466 15467 15468 15469 15470 15471 15472 15473 15474 15475 15476 15477 15478 15479 15480 15481 15482 15483 15484 15485 15486 15487 15488 15489 15490 15491 15492 15493 15494 15495 15496 15497 15498 15499 15500 15501 15502 15503 15504 15505 15506 15507 15508 15509 15510 15511 15512 15513 15514 15515 15516 15517 15518 15519 15520 15521 15522 15523 15524 15525 15526 15527 15528 15529 15530 15531 15532 15533 15534 15535 15536 15537 15538 15539 15540 15541 15542 15543 15544 15545 15546 15547 15548 15549 15550 15551 15552 15553 15554 15555 15556 15557 15558 15559 15560 15561 15562 15563 15564 15565 15566 15567 15568 15569 15570 15571 15572 15573 15574 15575 15576 15577 15578 15579 15580 15581 15582 15583 15584 15585 15586 15587 15588 15589 15590 15591 15592 15593 15594 15595 15596 15597 15598 15599 15600 15601 15602 15603 15604 15605 15606 15607 15608 15609 15610 15611 15612 15613 15614 15615 15616 15617 15618 15619 15620 15621 15622 15623 15624 15625 15626 15627 15628 15629 15630 15631 15632 15633 15634 15635 15636 15637 15638 15639 15640 15641 15642 15643 15644 15645 15646 15647 15648 15649 15650 15651 15652 15653 15654 15655 15656 15657 15658 15659 15660 15661 15662 15663 15664 15665 15666 15667 15668 15669 15670 15671 15672 15673 15674 15675 15676 15677 15678 15679 15680 15681 15682 15683 15684 15685 15686 15687 15688 15689 15690 15691 15692 15693 15694 15695 15696 15697 15698 15699 15700 15701 15702 15703 15704 15705 15706 15707 15708 15709 15710 15711 15712 15713 15714 15715 15716 15717 15718 15719 15720 15721 15722 15723 15724 15725 15726 15727 15728 15729 15730 15731 15732 15733 15734 15735 15736 15737 15738 15739 15740 15741 15742 15743 15744 15745 15746 15747 15748 15749 15750 15751 15752 15753 15754 15755 15756 15757 15758 15759 15760 15761 15762 15763 15764 15765 15766 15767 15768 15769 15770 15771 15772 15773 15774 15775 15776 15777 15778 15779 15780 15781 15782 15783 15784 15785 15786 15787 15788 15789 15790 15791 15792 15793 15794 15795 15796 15797 15798 15799 15800 15801 15802 15803 15804 15805 15806 15807 15808 15809 15810 15811 15812 15813 15814 15815 15816 15817 15818 15819 15820 15821 15822 15823 15824 15825 15826 15827 15828 15829 15830 15831 15832 15833 15834 15835 15836 15837 15838 15839 15840 15841 15842 15843 15844 15845 15846 15847 15848 15849 15850 15851 15852 15853 15854 15855 15856 15857 15858 15859 15860 15861 15862 15863 15864 15865 15866 15867 15868 15869 15870 15871 15872 15873 15874 15875 15876 15877 15878 15879 15880 15881 15882 15883 15884 15885 15886 15887 15888 15889 15890 15891 15892 15893 15894 15895 15896 15897 15898 15899 15900 15901 15902 15903 15904 15905 15906 15907 15908 15909 15910 15911 15912 15913 15914 15915 15916 15917 15918 15919 15920 15921 15922 15923 15924 15925 15926 15927 15928 15929 15930 15931 15932 15933 15934 15935 15936 15937 15938 15939 15940 15941 15942 15943 15944 15945 15946 15947 15948 15949 15950 15951 15952 15953 15954 15955 15956 15957 15958 15959 15960 15961 15962 15963 15964 15965 15966 15967 15968 15969 15970 15971 15972 15973 15974 15975 15976 15977 15978 15979 15980 15981 15982 15983 15984 15985 15986 15987 15988 15989 15990 15991 15992 15993 15994 15995 15996 15997 15998 15999 16000 16001 16002 16003 16004 16005 16006 16007 16008 16009 16010 16011 16012 16013 16014 16015 16016 16017 16018 16019 16020 16021 16022 16023 16024 16025 16026 16027 16028 16029 16030 16031 16032 16033 16034 16035 16036 16037 16038 16039 16040 16041 16042 16043 16044 16045 16046 16047 16048 16049 16050 16051 16052 16053 16054 16055 16056 16057 16058 16059 16060 16061 16062 16063 16064 16065 16066 16067 16068 16069 16070 16071 16072 16073 16074 16075 16076 16077 16078 16079 16080 16081 16082 16083 16084 16085 16086 16087 16088 16089 16090 16091 16092 16093 16094 16095 16096 16097 16098 16099 16100 16101 16102 16103 16104 16105 16106 16107 16108 16109 16110 16111 16112 16113 16114 16115 16116 16117 16118 16119 16120 16121 16122 16123 16124 16125 16126 16127 16128 16129 16130 16131 16132 16133 16134 16135 16136 16137 16138 16139 16140 16141 16142 16143 16144 16145 16146 16147 16148 16149 16150 16151 16152 16153 16154 16155 16156 16157 16158 16159 16160 16161 16162 16163 16164 16165 16166 16167 16168 16169 16170 16171 16172 16173 16174 16175 16176 16177 16178 16179 16180 16181 16182 16183 16184 16185 16186 16187 16188 16189 16190 16191 16192 16193 16194 16195 16196 16197 16198 16199 16200 16201 16202 16203 16204 16205 16206 16207 16208 16209 16210 16211 16212 16213 16214 16215 16216 16217 16218 16219 16220 16221 16222 16223 16224 16225 16226 16227 16228 16229 16230 16231 16232 16233 16234 16235 16236 16237 16238 16239 16240 16241 16242 16243 16244 16245 16246 16247 16248 16249 16250 16251 16252 16253 16254 16255 16256 16257 16258 16259 16260 16261 16262 16263 16264 16265 16266 16267 16268 16269 16270 16271 16272 16273 16274 16275 16276 16277 16278 16279 16280 16281 16282 16283 16284 16285 16286 16287 16288 16289 16290 16291 16292 16293 16294 16295 16296 16297 16298 16299 16300 16301 16302 16303 16304 16305 16306 16307 16308 16309 16310 16311 16312 16313 16314 16315 16316 16317 16318 16319 16320 16321 16322 16323 16324 16325 16326 16327 16328 16329 16330 16331 16332 16333 16334 16335 16336 16337 16338 16339 16340 16341 16342 16343 16344 16345 16346 16347 16348 16349 16350 16351 16352 16353 16354 16355 16356 16357 16358 16359 16360 16361 16362 16363 16364 16365 16366 16367 16368 16369 16370 16371 16372 16373 16374 16375 16376 16377 16378 16379 16380 16381 16382 16383 16384 16385 16386 16387 16388 16389 16390 16391 16392 16393 16394 16395 16396 16397 16398 16399 16400 16401 16402 16403 16404 16405 16406 16407 16408 16409 16410 16411 16412 16413 16414 16415 16416 16417 16418 16419 16420 16421 16422 16423 16424 16425 16426 16427 16428 16429 16430 16431 16432 16433 16434 16435 16436 16437 16438 16439 16440 16441 16442 16443 16444 16445 16446 16447 16448 16449 16450 16451 16452 16453 16454 16455 16456 16457 16458 16459 16460 16461 16462 16463 16464 16465 16466 16467 16468 16469 16470 16471 16472 16473 16474 16475 16476 16477 16478 16479 16480 16481 16482 16483 16484 16485 16486 16487 16488 16489 16490 16491 16492 16493 16494 16495 16496 16497 16498 16499 16500 16501 16502 16503 16504 16505 16506 16507 16508 16509 16510 16511 16512 16513 16514 16515 16516 16517 16518 16519 16520 16521 16522 16523 16524 16525 16526 16527 16528 16529 16530 16531 16532 16533 16534 16535 16536 16537 16538 16539 16540 16541 16542 16543 16544 16545 16546 16547 16548 16549 16550 16551 16552 16553 16554 16555 16556 16557 16558 16559 16560 16561 16562 16563 16564 16565 16566 16567 16568 16569 16570 16571 16572 16573 16574 16575 16576 16577 16578 16579 16580 16581 16582 16583 16584 16585 16586 16587 16588 16589 16590 16591 16592 16593 16594 16595 16596 16597 16598 16599 16600 16601 16602 16603 16604 16605 16606 16607 16608 16609 16610 16611 16612 16613 16614 16615 16616 16617 16618 16619 16620 16621 16622 16623 16624 16625 16626 16627 16628 16629 16630 16631 16632 16633 16634 16635 16636 16637 16638 16639 16640 16641 16642 16643 16644 16645 16646 16647 16648 16649 16650 16651 16652 16653 16654 16655 16656 16657 16658 16659 16660 16661 16662 16663 16664 16665 16666 16667 16668 16669 16670 16671 16672 16673 16674 16675 16676 16677 16678 16679 16680 16681 16682 16683 16684 16685 16686 16687 16688 16689 16690 16691 16692 16693 16694 16695 16696 16697 16698 16699 16700 16701 16702 16703 16704 16705 16706 16707 16708 16709 16710 16711 16712 16713 16714 16715 16716 16717 16718 16719 16720 16721 16722 16723 16724 16725 16726 16727 16728 16729 16730 16731 16732 16733 16734 16735 16736 16737 16738 16739 16740 16741 16742 16743 16744 16745 16746 16747 16748 16749 16750 16751 16752 16753 16754 16755 16756 16757 16758 16759 16760 16761 16762 16763 16764 16765 16766 16767 16768 16769 16770 16771 16772 16773 16774 16775 16776 16777 16778 16779 16780 16781 16782 16783 16784 16785 16786 16787 16788 16789 16790 16791 16792 16793 16794 16795 16796 16797 16798 16799 16800 16801 16802 16803 16804 16805 16806 16807 16808 16809 16810 16811 16812 16813 16814 16815 16816 16817 16818 16819 16820 16821 16822 16823 16824 16825 16826 16827 16828 16829 16830 16831 16832 16833 16834 16835 16836 16837 16838 16839 16840 16841 16842 16843 16844 16845 16846 16847 16848 16849 16850 16851 16852 16853 16854 16855 16856 16857 16858 16859 16860 16861 16862 16863 16864 16865 16866 16867 16868 16869 16870 16871 16872 16873 16874 16875 16876 16877 16878 16879 16880 16881 16882 16883 16884 16885 16886 16887 16888 16889 16890 16891 16892 16893 16894 16895 16896 16897 16898 16899 16900 16901 16902 16903 16904 16905 16906 16907 16908 16909 16910 16911 16912 16913 16914 16915 16916 16917 16918 16919 16920 16921 16922 16923 16924 16925 16926 16927 16928 16929 16930 16931 16932 16933 16934 16935 16936 16937 16938 16939 16940 16941 16942 16943 16944 16945 16946 16947 16948 16949 16950 16951 16952 16953 16954 16955 16956 16957 16958 16959 16960 16961 16962 16963 16964 16965 16966 16967 16968 16969 16970 16971 16972 16973 16974 16975 16976 16977 16978 16979 16980 16981 16982 16983 16984 16985 16986 16987 16988 16989 16990 16991 16992 16993 16994 16995 16996 16997 16998 16999 17000 17001 17002 17003 17004 17005 17006 17007 17008 17009 17010 17011 17012 17013 17014 17015 17016 17017 17018 17019 17020 17021 17022 17023 17024 17025 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 17036 17037 17038 17039 17040 17041 17042 17043 17044 17045 17046 17047 17048 17049 17050 17051 17052 17053 17054 17055 17056 17057 17058 17059 17060 17061 17062 17063 17064 17065 17066 17067 17068 17069 17070 17071 17072 17073 17074 17075 17076 17077 17078 17079 17080 17081 17082 17083 17084 17085 17086 17087 17088 17089 17090 17091 17092 17093 17094 17095 17096 17097 17098 17099 17100 17101 17102 17103 17104 17105 17106 17107 17108 17109 17110 17111 17112 17113 17114 17115 17116 17117 17118 17119 17120 17121 17122 17123 17124 17125 17126 17127 17128 17129 17130 17131 17132 17133 17134 17135 17136 17137 17138 17139 17140 17141 17142 17143 17144 17145 17146 17147 17148 17149 17150 17151 17152 17153 17154 17155 17156 17157 17158 17159 17160 17161 17162 17163 17164 17165 17166 17167 17168 17169 17170 17171 17172 17173 17174 17175 17176 17177 17178 17179 17180 17181 17182 17183 17184 17185 17186 17187 17188 17189 17190 17191 17192 17193 17194 17195 17196 17197 17198 17199 17200 17201 17202 17203 17204 17205 17206 17207 17208 17209 17210 17211 17212 17213 17214 17215 17216 17217 17218 17219 17220 17221 17222 17223 17224 17225 17226 17227 17228 17229 17230 17231 17232 17233 17234 17235 17236 17237 17238 17239 17240 17241 17242 17243 17244 17245 17246 17247 17248 17249 17250 17251 17252 17253 17254 17255 17256 17257 17258 17259 17260 17261 17262 17263 17264 17265 17266 17267 17268 17269 17270 17271 17272 17273 17274 17275 17276 17277 17278 17279 17280 17281 17282 17283 17284 17285 17286 17287 17288 17289 17290 17291 17292 17293 17294 17295 17296 17297 17298 17299 17300 17301 17302 17303 17304 17305 17306 17307 17308 17309 17310 17311 17312 17313 17314 17315 17316 17317 17318 17319 17320 17321 17322 17323 17324 17325 17326 17327 17328 17329 17330 17331 17332 17333 17334 17335 17336 17337 17338 17339 17340 17341 17342 17343 17344 17345 17346 17347 17348 17349 17350 17351 17352 17353 17354 17355 17356 17357 17358 17359 17360 17361 17362 17363 17364 17365 17366 17367 17368 17369 17370 17371 17372 17373 17374 17375 17376 17377 17378 17379 17380 17381 17382 17383 17384 17385 17386 17387 17388 17389 17390 17391 17392 17393 17394 17395 17396 17397 17398 17399 17400 17401 17402 17403 17404 17405 17406 17407 17408 17409 17410 17411 17412 17413 17414 17415 17416 17417 17418 17419 17420 17421 17422 17423 17424 17425 17426 17427 17428 17429 17430 17431 17432 17433 17434 17435 17436 17437 17438 17439 17440 17441 17442 17443 17444 17445 17446 17447 17448 17449 17450 17451 17452 17453 17454 17455 17456 17457 17458 17459 17460 17461 17462 17463 17464 17465 17466 17467 17468 17469 17470 17471 17472 17473 17474 17475 17476 17477 17478 17479 17480 17481 17482 17483 17484 17485 17486 17487 17488 17489 17490 17491 17492 17493 17494 17495 17496 17497 17498 17499 17500 17501 17502 17503 17504 17505 17506 17507 17508 17509 17510 17511 17512 17513 17514 17515 17516 17517 17518 17519 17520 17521 17522 17523 17524 17525 17526 17527 17528 17529 17530 17531 17532 17533 17534 17535 17536 17537 17538 17539 17540 17541 17542 17543 17544 17545 17546 17547 17548 17549 17550 17551 17552 17553 17554 17555 17556 17557 17558 17559 17560 17561 17562 17563 17564 17565 17566 17567 17568 17569 17570 17571 17572 17573 17574 17575 17576 17577 17578 17579 17580 17581 17582 17583 17584 17585 17586 17587 17588 17589 17590 17591 17592 17593 17594 17595 17596 17597 17598 17599 17600 17601 17602 17603 17604 17605 17606 17607 17608 17609 17610 17611 17612 17613 17614 17615 17616 17617 17618 17619 17620 17621 17622 17623 17624 17625 17626 17627 17628 17629 17630 17631 17632 17633 17634 17635 17636 17637 17638 17639 17640 17641 17642 17643 17644 17645 17646 17647 17648 17649 17650 17651 17652 17653 17654 17655 17656 17657 17658 17659 17660 17661 17662 17663 17664 17665 17666 17667 17668 17669 17670 17671 17672 17673 17674 17675 17676 17677 17678 17679 17680 17681 17682 17683 17684 17685 17686 17687 17688 17689 17690 17691 17692 17693 17694 17695 17696 17697 17698 17699 17700 17701 17702 17703 17704 17705 17706 17707 17708 17709 17710 17711 17712 17713 17714 17715 17716 17717 17718 17719 17720 17721 17722 17723 17724 17725 17726 17727 17728 17729 17730 17731 17732 17733 17734 17735 17736 17737 17738 17739 17740 17741 17742 17743 17744 17745 17746 17747 17748 17749 17750 17751 17752 17753 17754 17755 17756 17757 17758 17759 17760 17761 17762 17763 17764 17765 17766 17767 17768 17769 17770 17771 17772 17773 17774 17775 17776 17777 17778 17779 17780 17781 17782 17783 17784 17785 17786 17787 17788 17789 17790 17791 17792 17793 17794 17795 17796 17797 17798 17799 17800 17801 17802 17803 17804 17805 17806 17807 17808 17809 17810 17811 17812 17813 17814 17815 17816 17817 17818 17819 17820 17821 17822 17823 17824 17825 17826 17827 17828 17829 17830 17831 17832 17833 17834 17835 17836 17837 17838 17839 17840 17841 17842 17843 17844 17845 17846 17847 17848 17849 17850 17851 17852 17853 17854 17855 17856 17857 17858 17859 17860 17861 17862 17863 17864 17865 17866 17867 17868 17869 17870 17871 17872 17873 17874 17875 17876 17877 17878 17879 17880 17881 17882 17883 17884 17885 17886 17887 17888 17889 17890 17891 17892 17893 17894 17895 17896 17897 17898 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918 17919 17920 17921 17922 17923 17924 17925 17926 17927 17928 17929 17930 17931 17932 17933 17934 17935 17936 17937 17938 17939 17940 17941 17942 17943 17944 17945 17946 17947 17948 17949 17950 17951 17952 17953 17954 17955 17956 17957 17958 17959 17960 17961 17962 17963 17964 17965 17966 17967 17968 17969 17970 17971 17972 17973 17974 17975 17976 17977 17978 17979 17980 17981 17982 17983 17984 17985 17986 17987 17988 17989 17990 17991 17992 17993 17994 17995 17996 17997 17998 17999 18000 18001 18002 18003 18004 18005 18006 18007 18008 18009 18010 18011 18012 18013 18014 18015 18016 18017 18018 18019 18020 18021 18022 18023 18024 18025 18026 18027 18028 18029 18030 18031 18032 18033 18034 18035 18036 18037 18038 18039 18040 18041 18042 18043 18044 18045 18046 18047 18048 18049 18050 18051 18052 18053 18054 18055 18056 18057 18058 18059 18060 18061 18062 18063 18064 18065 18066 18067 18068 18069 18070 18071 18072 18073 18074 18075 18076 18077 18078 18079 18080 18081 18082 18083 18084 18085 18086 18087 18088 18089 18090 18091 18092 18093 18094 18095 18096 18097 18098 18099 18100 18101 18102 18103 18104 18105 18106 18107 18108 18109 18110 18111 18112 18113 18114 18115 18116 18117 18118 18119 18120 18121 18122 18123 18124 18125 18126 18127 18128 18129 18130 18131 18132 18133 18134 18135 18136 18137 18138 18139 18140 18141 18142 18143 18144 18145 18146 18147 18148 18149 18150 18151 18152 18153 18154 18155 18156 18157 18158 18159 18160 18161 18162 18163 18164 18165 18166 18167 18168 18169 18170 18171 18172 18173 18174 18175 18176 18177 18178 18179 18180 18181 18182 18183 18184 18185 18186 18187 18188 18189 18190 18191 18192 18193 18194 18195 18196 18197 18198 18199 18200 18201 18202 18203 18204 18205 18206 18207 18208 18209 18210 18211 18212 18213 18214 18215 18216 18217 18218 18219 18220 18221 18222 18223 18224 18225 18226 18227 18228 18229 18230 18231 18232 18233 18234 18235 18236 18237 18238 18239 18240 18241 18242 18243 18244 18245 18246 18247 18248 18249 18250 18251 18252 18253 18254 18255 18256 18257 18258 18259 18260 18261 18262 18263 18264 18265 18266 18267 18268 18269 18270 18271 18272 18273 18274 18275 18276 18277 18278 18279 18280 18281 18282 18283 18284 18285 18286 18287 18288 18289 18290 18291 18292 18293 18294 18295 18296 18297 18298 18299 18300 18301 18302 18303 18304 18305 18306 18307 18308 18309 18310 18311 18312 18313 18314 18315 18316 18317 18318 18319 18320 18321 18322 18323 18324 18325 18326 18327 18328 18329 18330 18331 18332 18333 18334 18335 18336 18337 18338 18339 18340 18341 18342 18343 18344 18345 18346 18347 18348 18349 18350 18351 18352 18353 18354 18355 18356 18357 18358 18359 18360 18361 18362 18363 18364 18365 18366 18367 18368 18369 18370 18371 18372 18373 18374 18375 18376 18377 18378 18379 18380 18381 18382 18383 18384 18385 18386 18387 18388 18389 18390 18391 18392 18393 18394 18395 18396 18397 18398 18399 18400 18401 18402 18403 18404 18405 18406 18407 18408 18409 18410 18411 18412 18413 18414 18415 18416 18417 18418 18419 18420 18421 18422 18423 18424 18425 18426 18427 18428 18429 18430 18431 18432 18433 18434 18435 18436 18437 18438 18439 18440 18441 18442 18443 18444 18445 18446 18447 18448 18449 18450 18451 18452 18453 18454 18455 18456 18457 18458 18459 18460 18461 18462 18463 18464 18465 18466 18467 18468 18469 18470 18471 18472 18473 18474 18475 18476 18477 18478 18479 18480 18481 18482 18483 18484 18485 18486 18487 18488 18489 18490 18491 18492 18493 18494 18495 18496 18497 18498 18499 18500 18501 18502 18503 18504 18505 18506 18507 18508 18509 18510 18511 18512 18513 18514 18515 18516 18517 18518 18519 18520 18521 18522 18523 18524 18525 18526 18527 18528 18529 18530 18531 18532 18533 18534 18535 18536 18537 18538 18539 18540 18541 18542 18543 18544 18545 18546 18547 18548 18549 18550 18551 18552 18553 18554 18555 18556 18557 18558 18559 18560 18561 18562 18563 18564 18565 18566 18567 18568 18569 18570 18571 18572 18573 18574 18575 18576 18577 18578 18579 18580 18581 18582 18583 18584 18585 18586 18587 18588 18589 18590 18591 18592 18593 18594 18595 18596 18597 18598 18599 18600 18601 18602 18603 18604 18605 18606 18607 18608 18609 18610 18611 18612 18613 18614 18615 18616 18617 18618 18619 18620 18621 18622 18623 18624 18625 18626 18627 18628 18629 18630 18631 18632 18633 18634 18635 18636 18637 18638 18639 18640 18641 18642 18643 18644 18645 18646 18647 18648 18649 18650 18651 18652 18653 18654 18655 18656 18657 18658 18659 18660 18661 18662 18663 18664 18665 18666 18667 18668 18669 18670 18671 18672 18673 18674 18675 18676 18677 18678 18679 18680 18681 18682 18683 18684 18685 18686 18687 18688 18689 18690 18691 18692 18693 18694 18695 18696 18697 18698 18699 18700 18701 18702 18703 18704 18705 18706 18707 18708 18709 18710 18711 18712 18713 18714 18715 18716 18717 18718 18719 18720 18721 18722 18723 18724 18725 18726 18727 18728 18729 18730 18731 18732 18733 18734 18735 18736 18737 18738 18739 18740 18741 18742 18743 18744 18745 18746 18747 18748 18749 18750 18751 18752 18753 18754 18755 18756 18757 18758 18759 18760 18761 18762 18763 18764 18765 18766 18767 18768 18769 18770 18771 18772 18773 18774 18775 18776 18777 18778 18779 18780 18781 18782 18783 18784 18785 18786 18787 18788 18789 18790 18791 18792 18793 18794 18795 18796 18797 18798 18799 18800 18801 18802 18803 18804 18805 18806 18807 18808 18809 18810 18811 18812 18813 18814 18815 18816 18817 18818 18819 18820 18821 18822 18823 18824 18825 18826 18827 18828 18829 18830 18831 18832 18833 18834 18835 18836 18837 18838 18839 18840 18841 18842 18843 18844 18845 18846 18847 18848 18849 18850 18851 18852 18853 18854 18855 18856 18857 18858 18859 18860 18861 18862 18863 18864 18865 18866 18867 18868 18869 18870 18871 18872 18873 18874 18875 18876 18877 18878 18879 18880 18881 18882 18883 18884 18885 18886 18887 18888 18889 18890 18891 18892 18893 18894 18895 18896 18897 18898 18899 18900 18901 18902 18903 18904 18905 18906 18907 18908 18909 18910 18911 18912 18913 18914 18915 18916 18917 18918 18919 18920 18921 18922 18923 18924 18925 18926 18927 18928 18929 18930 18931 18932 18933 18934 18935 18936 18937 18938 18939 18940 18941 18942 18943 18944 18945 18946 18947 18948 18949 18950 18951 18952 18953 18954 18955 18956 18957 18958 18959 18960 18961 18962 18963 18964 18965 18966 18967 18968 18969 18970 18971 18972 18973 18974 18975 18976 18977 18978 18979 18980 18981 18982 18983 18984 18985 18986 18987 18988 18989 18990 18991 18992 18993 18994 18995 18996 18997 18998 18999 19000 19001 19002 19003 19004 19005 19006 19007 19008 19009 19010 19011 19012 19013 19014 19015 19016 19017 19018 19019 19020 19021 19022 19023 19024 19025 19026 19027 19028 19029 19030 19031 19032 19033 19034 19035 19036 19037 19038 19039 19040 19041 19042 19043 19044 19045 19046 19047 19048 19049 19050 19051 19052 19053 19054 19055 19056 19057 19058 19059 19060 19061 19062 19063 19064 19065 19066 19067 19068 19069 19070 19071 19072 19073 19074 19075 19076 19077 19078 19079 19080 19081 19082 19083 19084 19085 19086 19087 19088 19089 19090 19091 19092 19093 19094 19095 19096 19097 19098 19099 19100 19101 19102 19103 19104 19105 19106 19107 19108 19109 19110 19111 19112 19113 19114 19115 19116 19117 19118 19119 19120 19121 19122 19123 19124 19125 19126 19127 19128 19129 19130 19131 19132 19133 19134 19135 19136 19137 19138 19139 19140 19141 19142 19143 19144 19145 19146 19147 19148 19149 19150 19151 19152 19153 19154 19155 19156 19157 19158 19159 19160 19161 19162 19163 19164 19165 19166 19167 19168 19169 19170 19171 19172 19173 19174 19175 19176 19177 19178 19179 19180 19181 19182 19183 19184 19185 19186 19187 19188 19189 19190 19191 19192 19193 19194 19195 19196 19197 19198 19199 19200 19201 19202 19203 19204 19205 19206 19207 19208 19209 19210 19211 19212 19213 19214 19215 19216 19217 19218 19219 19220 19221 19222 19223 19224 19225 19226 19227 19228 19229 19230 19231 19232 19233 19234 19235 19236 19237 19238 19239 19240 19241 19242 19243 19244 19245 19246 19247 19248 19249 19250 19251 19252 19253 19254 19255 19256 19257 19258 19259 19260 19261 19262 19263 19264 19265 19266 19267 19268 19269 19270 19271 19272 19273 19274 19275 19276 19277 19278 19279 19280 19281 19282 19283 19284 19285 19286 19287 19288 19289 19290 19291 19292 19293 19294 19295 19296 19297 19298 19299 19300 19301 19302 19303 19304 19305 19306 19307 19308 19309 19310 19311 19312 19313 19314 19315 19316 19317 19318 19319 19320 19321 19322 19323 19324 19325 19326 19327 19328 19329 19330 19331 19332 19333 19334 19335 19336 19337 19338 19339 19340 19341 19342 19343 19344 19345 19346 19347 19348 19349 19350 19351 19352 19353 19354 19355 19356 19357 19358 19359 19360 19361 19362 19363 19364 19365 19366 19367 19368 19369 19370 19371 19372 19373 19374 19375 19376 19377 19378 19379 19380 19381 19382 19383 19384 19385 19386 19387 19388 19389 19390 19391 19392 19393 19394 19395 19396 19397 19398 19399 19400 19401 19402 19403 19404 19405 19406 19407 19408 19409 19410 19411 19412 19413 19414 19415 19416 19417 19418 19419 19420 19421 19422 19423 19424 19425 19426 19427 19428 19429 19430 19431 19432 19433 19434 19435 19436 19437 19438 19439 19440 19441 19442 19443 19444 19445 19446 19447 19448 19449 19450 19451 19452 19453 19454 19455 19456 19457 19458 19459 19460 19461 19462 19463 19464 19465 19466 19467 19468 19469 19470 19471 19472 19473 19474 19475 19476 19477 19478 19479 19480 19481 19482 19483 19484 19485 19486 19487 19488 19489 19490 19491 19492 19493 19494 19495 19496 19497 19498 19499 19500 19501 19502 19503 19504 19505 19506 19507 19508 19509 19510 19511 19512 19513 19514 19515 19516 19517 19518 19519 19520 19521 19522 19523 19524 19525 19526 19527 19528 19529 19530 19531 19532 19533 19534 19535 19536 19537 19538 19539 19540 19541 19542 19543 19544 19545 19546 19547 19548 19549 19550 19551 19552 19553 19554 19555 19556 19557 19558 19559 19560 19561 19562 19563 19564 19565 19566 19567 19568 19569 19570 19571 19572 19573 19574 19575 19576 19577 19578 19579 19580 19581 19582 19583 19584 19585 19586 19587 19588 19589 19590 19591 19592 19593 19594 19595 19596 19597 19598 19599 19600 19601 19602 19603 19604 19605 19606 19607 19608 19609 19610 19611 19612 19613 19614 19615 19616 19617 19618 19619 19620 19621 19622 19623 19624 19625 19626 19627 19628 19629 19630 19631 19632 19633 19634 19635 19636 19637 19638 19639 19640 19641 19642 19643 19644 19645 19646 19647 19648 19649 19650 19651 19652 19653 19654 19655 19656 19657 19658 19659 19660 19661 19662 19663 19664 19665 19666 19667 19668 19669 19670 19671 19672 19673 19674 19675 19676 19677 19678 19679 19680 19681 19682 19683 19684 19685 19686 19687 19688 19689 19690 19691 19692 19693 19694 19695 19696 19697 19698 19699 19700 19701 19702 19703 19704 19705 19706 19707 19708 19709 19710 19711 19712 19713 19714 19715 19716 19717 19718 19719 19720 19721 19722 19723 19724 19725 19726 19727 19728 19729 19730 19731 19732 19733 19734 19735 19736 19737 19738 19739 19740 19741 19742 19743 19744 19745 19746 19747 19748 19749 19750 19751 19752 19753 19754 19755 19756 19757 19758 19759 19760 19761 19762 19763 19764 19765 19766 19767 19768 19769 19770 19771 19772 19773 19774 19775 19776 19777 19778 19779 19780 19781 19782 19783 19784 19785 19786 19787 19788 19789 19790 19791 19792 19793 19794 19795 19796 19797 19798 19799 19800 19801 19802 19803 19804 19805 19806 19807 19808 19809 19810 19811 19812 19813 19814 19815 19816 19817 19818 19819 19820 19821 19822 19823 19824 19825 19826 19827 19828 19829 19830 19831 19832 19833 19834 19835 19836 19837 19838 19839 19840 19841 19842 19843 19844 19845 19846 19847 19848 19849 19850 19851 19852 19853 19854 19855 19856 19857 19858 19859 19860 19861 19862 19863 19864 19865 19866 19867 19868 19869 19870 19871 19872 19873 19874 19875 19876 19877 19878 19879 19880 19881 19882 19883 19884 19885 19886 19887 19888 19889 19890 19891 19892 19893 19894 19895 19896 19897 19898 19899 19900 19901 19902 19903 19904 19905 19906 19907 19908 19909 19910 19911 19912 19913 19914 19915 19916 19917 19918 19919 19920 19921 19922 19923 19924 19925 19926 19927 19928 19929 19930 19931 19932 19933 19934 19935 19936 19937 19938 19939 19940 19941 19942 19943 19944 19945 19946 19947 19948 19949 19950 19951 19952 19953 19954 19955 19956 19957 19958 19959 19960 19961 19962 19963 19964 19965 19966 19967 19968 19969 19970 19971 19972 19973 19974 19975 19976 19977 19978 19979 19980 19981 19982 19983 19984 19985 19986 19987 19988 19989 19990 19991 19992 19993 19994 19995 19996 19997 19998 19999 20000 20001 20002 20003 20004 20005 20006 20007 20008 20009 20010 20011 20012 20013 20014 20015 20016 20017 20018 20019 20020 20021 20022 20023 20024 20025 20026 20027 20028 20029 20030 20031 20032 20033 20034 20035 20036 20037 20038 20039 20040 20041 20042 20043 20044 20045 20046 20047 20048 20049 20050 20051 20052 20053 20054 20055 20056 20057 20058 20059 20060 20061 20062 20063 20064 20065 20066 20067 20068 20069 20070 20071 20072 20073 20074 20075 20076 20077 20078 20079 20080 20081 20082 20083 20084 20085 20086 20087 20088 20089 20090 20091 20092 20093 20094 20095 20096 20097 20098 20099 20100 20101 20102 20103 20104 20105 20106 20107 20108 20109 20110 20111 20112 20113 20114 20115 20116 20117 20118 20119 20120 20121 20122 20123 20124 20125 20126 20127 20128 20129 20130 20131 20132 20133 20134 20135 20136 20137 20138 20139 20140 20141 20142 20143 20144 20145 20146 20147 20148 20149 20150 20151 20152 20153 20154 20155 20156 20157 20158 20159 20160 20161 20162 20163 20164 20165 20166 20167 20168 20169 20170 20171 20172 20173 20174 20175 20176 20177 20178 20179 20180 20181 20182 20183 20184 20185 20186 20187 20188 20189 20190 20191 20192 20193 20194 20195 20196 20197 20198 20199 20200 20201 20202 20203 20204 20205 20206 20207 20208 20209 20210 20211 20212 20213 20214 20215 20216 20217 20218 20219 20220 20221 20222 20223 20224 20225 20226 20227 20228 20229 20230 20231 20232 20233 20234 20235 20236 20237 20238 20239 20240 20241 20242 20243 20244 20245 20246 20247 20248 20249 20250 20251 20252 20253 20254 20255 20256 20257 20258 20259 20260 20261 20262 20263 20264 20265 20266 20267 20268 20269 20270 20271 20272 20273 20274 20275 20276 20277 20278 20279 20280 20281 20282 20283 20284 20285 20286 20287 20288 20289 20290 20291 20292 20293 20294 20295 20296 20297 20298 20299 20300 20301 20302 20303 20304 20305 20306 20307 20308 20309 20310 20311 20312 20313 20314 20315 20316 20317 20318 20319 20320 20321 20322 20323 20324 20325 20326 20327 20328 20329 20330 20331 20332 20333 20334 20335 20336 20337 20338 20339 20340 20341 20342 20343 20344 20345 20346 20347 20348 20349 20350 20351 20352 20353 20354 20355 20356 20357 20358 20359 20360 20361 20362 20363 20364 20365 20366 20367 20368 20369 20370 20371 20372 20373 20374 20375 20376 20377 20378 20379 20380 20381 20382 20383 20384 20385 20386 20387 20388 20389 20390 20391 20392 20393 20394 20395 20396 20397 20398 20399 20400 20401 20402 20403 20404 20405 20406 20407 20408 20409 20410 20411 20412 20413 20414 20415 20416 20417 20418 20419 20420 20421 20422 20423 20424 20425 20426 20427 20428 20429 20430 20431 20432 20433 20434 20435 20436 20437 20438 20439 20440 20441 20442 20443 20444 20445 20446 20447 20448 20449 20450 20451 20452 20453 20454 20455 20456 20457 20458 20459 20460 20461 20462 20463 20464 20465 20466 20467 20468 20469 20470 20471 20472 20473 20474 20475 20476 20477 20478 20479 20480 20481 20482 20483 20484 20485 20486 20487 20488 20489 20490 20491 20492 20493 20494 20495 20496 20497 20498 20499 20500 20501 20502 20503 20504 20505 20506 20507 20508 20509 20510 20511 20512 20513 20514 20515 20516 20517 20518 20519 20520 20521 20522 20523 20524 20525 20526 20527 20528 20529 20530 20531 20532 20533 20534 20535 20536 20537 20538 20539 20540 20541 20542 20543 20544 20545 20546 20547 20548 20549 20550 20551 20552 20553 20554 20555 20556 20557 20558 20559 20560 20561 20562 20563 20564 20565 20566 20567 20568 20569 20570 20571 20572 20573 20574 20575 20576 20577 20578 20579 20580 20581 20582 20583 20584 20585 20586 20587 20588 20589 20590 20591 20592 20593 20594 20595 20596 20597 20598 20599 20600 20601 20602 20603 20604 20605 20606 20607 20608 20609 20610 20611 20612 20613 20614 20615 20616 20617 20618 20619 20620 20621 20622 20623 20624 20625 20626 20627 20628 20629 20630 20631 20632 20633 20634 20635 20636 20637 20638 20639 20640 20641 20642 20643 20644 20645 20646 20647 20648 20649 20650 20651 20652 20653 20654 20655 20656 20657 20658 20659 20660 20661 20662 20663 20664 20665 20666 20667 20668 20669 20670 20671 20672 20673 20674 20675 20676 20677 20678 20679 20680 20681 20682 20683 20684 20685 20686 20687 20688 20689 20690 20691 20692 20693 20694 20695 20696 20697 20698 20699 20700 20701 20702 20703 20704 20705 20706 20707 20708 20709 20710 20711 20712 20713 20714 20715 20716 20717 20718 20719 20720 20721 20722 20723 20724 20725 20726 20727 20728 20729 20730 20731 20732 20733 20734 20735 20736 20737 20738 20739 20740 20741 20742 20743 20744 20745 20746 20747 20748 20749 20750 20751 20752 20753 20754 20755 20756 20757 20758 20759 20760 20761 20762 20763 20764 20765 20766 20767 20768 20769 20770 20771 20772 20773 20774 20775 20776 20777 20778 20779 20780 20781 20782 20783 20784 20785 20786 20787 20788 20789 20790 20791 20792 20793 20794 20795 20796 20797 20798 20799 20800 20801 20802 20803 20804 20805 20806 20807 20808 20809 20810 20811 20812 20813 20814 20815 20816 20817 20818 20819 20820 20821 20822 20823 20824 20825 20826 20827 20828 20829 20830 20831 20832 20833 20834 20835 20836 20837 20838 20839 20840 20841 20842 20843 20844 20845 20846 20847 20848 20849 20850 20851 20852 20853 20854 20855 20856 20857 20858 20859 20860 20861 20862 20863 20864 20865 20866 20867 20868 20869 20870 20871 20872 20873 20874 20875 20876 20877 20878 20879 20880 20881 20882 20883 20884 20885 20886 20887 20888 20889 20890 20891 20892 20893 20894 20895 20896 20897 20898 20899 20900 20901 20902 20903 20904 20905 20906 20907 20908 20909 20910 20911 20912 20913 20914 20915 20916 20917 20918 20919 20920 20921 20922 20923 20924 20925 20926 20927 20928 20929 20930 20931 20932 20933 20934 20935 20936 20937 20938 20939 20940 20941 20942 20943 20944 20945 20946 20947 20948 20949 20950 20951 20952 20953 20954 20955 20956 20957 20958 20959 20960 20961 20962 20963 20964 20965 20966 20967 20968 20969 20970 20971 20972 20973 20974 20975 20976 20977 20978 20979 20980 20981 20982 20983 20984 20985 20986 20987 20988 20989 20990 20991 20992 20993 20994 20995 20996 20997 20998 20999 21000 21001 21002 21003 21004 21005 21006 21007 21008 21009 21010 21011 21012 21013 21014 21015 21016 21017 21018 21019 21020 21021 21022 21023 21024 21025 21026 21027 21028 21029 21030 21031 21032 21033 21034 21035 21036 21037 21038 21039 21040 21041 21042 21043 21044 21045 21046 21047 21048 21049 21050 21051 21052 21053 21054 21055 21056 21057 21058 21059 21060 21061 21062 21063 21064 21065 21066 21067 21068 21069 21070 21071 21072 21073 21074 21075 21076 21077 21078 21079 21080 21081 21082 21083 21084 21085 21086 21087 21088 21089 21090 21091 21092 21093 21094 21095 21096 21097 21098 21099 21100 21101 21102 21103 21104 21105 21106 21107 21108 21109 21110 21111 21112 21113 21114 21115 21116 21117 21118 21119 21120 21121 21122 21123 21124 21125 21126 21127 21128 21129 21130 21131 21132 21133 21134 21135 21136 21137 21138 21139 21140 21141 21142 21143 21144 21145 21146 21147 21148 21149 21150 21151 21152 21153 21154 21155 21156 21157 21158 21159 21160 21161 21162 21163 21164 21165 21166 21167 21168 21169 21170 21171 21172 21173 21174 21175 21176 21177 21178 21179 21180 21181 21182 21183 21184 21185 21186 21187 21188 21189 21190 21191 21192 21193 21194 21195 21196 21197 21198 21199 21200 21201 21202 21203 21204 21205 21206 21207 21208 21209 21210 21211 21212 21213 21214 21215 21216 21217 21218 21219 21220 21221 21222 21223 21224 21225 21226 21227 21228 21229 21230 21231 21232 21233 21234 21235 21236 21237 21238 21239 21240 21241 21242 21243 21244 21245 21246 21247 21248 21249 21250 21251 21252 21253 21254 21255 21256 21257 21258 21259 21260 21261 21262 21263 21264 21265 21266 21267 21268 21269 21270 21271 21272 21273 21274 21275 21276 21277 21278 21279 21280 21281 21282 21283 21284 21285 21286 21287 21288 21289 21290 21291 21292 21293 21294 21295 21296 21297 21298 21299 21300 21301 21302 21303 21304 21305 21306 21307 21308 21309 21310 21311 21312 21313 21314 21315 21316 21317 21318 21319 21320 21321 21322 21323 21324 21325 21326 21327 21328 21329 21330 21331 21332 21333 21334 21335 21336 21337 21338 21339 21340 21341 21342 21343 21344 21345 21346 21347 21348 21349 21350 21351 21352 21353 21354 21355 21356 21357 21358 21359 21360 21361 21362 21363 21364 21365 21366 21367 21368 21369 21370 21371 21372 21373 21374 21375 21376 21377 21378 21379 21380 21381 21382 21383 21384 21385 21386 21387 21388 21389 21390 21391 21392 21393 21394 21395 21396 21397 21398 21399 21400 21401 21402 21403 21404 21405 21406 21407 21408 21409 21410 21411 21412 21413 21414 21415 21416 21417 21418 21419 21420 21421 21422 21423 21424 21425 21426 21427 21428 21429 21430 21431 21432 21433 21434 21435 21436 21437 21438 21439 21440 21441 21442 21443 21444 21445 21446 21447 21448 21449 21450 21451 21452 21453 21454 21455 21456 21457 21458 21459 21460 21461 21462 21463 21464 21465 21466 21467 21468 21469 21470 21471 21472 21473 21474 21475 21476 21477 21478 21479 21480 21481 21482 21483 21484 21485 21486 21487 21488 21489 21490 21491 21492 21493 21494 21495 21496 21497 21498 21499 21500 21501 21502 21503 21504 21505 21506 21507 21508 21509 21510 21511 21512 21513 21514 21515 21516 21517 21518 21519 21520 21521 21522 21523 21524 21525 21526 21527 21528 21529 21530 21531 21532 21533 21534 21535 21536 21537 21538 21539 21540 21541 21542 21543 21544 21545 21546 21547 21548 21549 21550 21551 21552 21553 21554 21555 21556 21557 21558 21559 21560 21561 21562 21563 21564 21565 21566 21567 21568 21569 21570 21571 21572 21573 21574 21575 21576 21577 21578 21579 21580 21581 21582 21583 21584 21585 21586 21587 21588 21589 21590 21591 21592 21593 21594 21595 21596 21597 21598 21599 21600 21601 21602 21603 21604 21605 21606 21607 21608 21609 21610 21611 21612 21613 21614 21615 21616 21617 21618 21619 21620 21621 21622 21623 21624 21625 21626 21627 21628 21629 21630 21631 21632 21633 21634 21635 21636 21637 21638 21639 21640 21641 21642 21643 21644 21645 21646 21647 21648 21649 21650 21651 21652 21653 21654 21655 21656 21657 21658 21659 21660 21661 21662 21663 21664 21665 21666 21667 21668 21669 21670 21671 21672 21673 21674 21675 21676 21677 21678 21679 21680 21681 21682 21683 21684 21685 21686 21687 21688 21689 21690 21691 21692 21693 21694 21695 21696 21697 21698 21699 21700 21701 21702 21703 21704 21705 21706 21707 21708 21709 21710 21711 21712 21713 21714 21715 21716 21717 21718 21719 21720 21721 21722 21723 21724 21725 21726 21727 21728 21729 21730 21731 21732 21733 21734 21735 21736 21737 21738 21739 21740 21741 21742 21743 21744 21745 21746 21747 21748 21749 21750 21751 21752 21753 21754 21755 21756 21757 21758 21759 21760 21761 21762 21763 21764 21765 21766 21767 21768 21769 21770 21771 21772 21773 21774 21775 21776 21777 21778 21779 21780 21781 21782 21783 21784 21785 21786 21787 21788 21789 21790 21791 21792 21793 21794 21795 21796 21797 21798 21799 21800 21801 21802 21803 21804 21805 21806 21807 21808 21809 21810 21811 21812 21813 21814 21815 21816 21817 21818 21819 21820 21821 21822 21823 21824 21825 21826 21827 21828 21829 21830 21831 21832 21833 21834 21835 21836 21837 21838 21839 21840 21841 21842 21843 21844 21845 21846 21847 21848 21849 21850 21851 21852 21853 21854 21855 21856 21857 21858 21859 21860 21861 21862 21863 21864 21865 21866 21867 21868 21869 21870 21871 21872 21873 21874 21875 21876 21877 21878 21879 21880 21881 21882 21883 21884 21885 21886 21887 21888 21889 21890 21891 21892 21893 21894 21895 21896 21897 21898 21899 21900 21901 21902 21903 21904 21905 21906 21907 21908 21909 21910 21911 21912 21913 21914 21915 21916 21917 21918 21919 21920 21921 21922 21923 21924 21925 21926 21927 21928 21929 21930 21931 21932 21933 21934 21935 21936 21937 21938 21939 21940 21941 21942 21943 21944 21945 21946 21947 21948 21949 21950 21951 21952 21953 21954 21955 21956 21957 21958 21959 21960 21961 21962 21963 21964 21965 21966 21967 21968 21969 21970 21971 21972 21973 21974 21975 21976 21977 21978 21979 21980 21981 21982 21983 21984 21985 21986 21987 21988 21989 21990 21991 21992 21993 21994 21995 21996 21997 21998 21999 22000 22001 22002 22003 22004 22005 22006 22007 22008 22009 22010 22011 22012 22013 22014 22015 22016 22017 22018 22019 22020 22021 22022 22023 22024 22025 22026 22027 22028 22029 22030 22031 22032 22033 22034 22035 22036 22037 22038 22039 22040 22041 22042 22043 22044 22045 22046 22047 22048 22049 22050 22051 22052 22053 22054 22055 22056 22057 22058 22059 22060 22061 22062 22063 22064 22065 22066 22067 22068 22069 22070 22071 22072 22073 22074 22075 22076 22077 22078 22079 22080 22081 22082 22083 22084 22085 22086 22087 22088 22089 22090 22091 22092 22093 22094 22095 22096 22097 22098 22099 22100 22101 22102 22103 22104 22105 22106 22107 22108 22109 22110 22111 22112 22113 22114 22115 22116 22117 22118 22119 22120 22121 22122 22123 22124 22125 22126 22127 22128 22129 22130 22131 22132 22133 22134 22135 22136 22137 22138 22139 22140 22141 22142 22143 22144 22145 22146 22147 22148 22149 22150 22151 22152 22153 22154 22155 22156 22157 22158 22159 22160 22161 22162 22163 22164 22165 22166 22167 22168 22169 22170 22171 22172 22173 22174 22175 22176 22177 22178 22179 22180 22181 22182 22183 22184 22185 22186 22187 22188 22189 22190 22191 22192 22193 22194 22195 22196 22197 22198 22199 22200 22201 22202 22203 22204 22205 22206 22207 22208 22209 22210 22211 22212 22213 22214 22215 22216 22217 22218 22219 22220 22221 22222 22223 22224 22225 22226 22227 22228 22229 22230 22231 22232 22233 22234 22235 22236 22237 22238 22239 22240 22241 22242 22243 22244 22245 22246 22247 22248 22249 22250 22251 22252 22253 22254 22255 22256 22257 22258 22259 22260 22261 22262 22263 22264 22265 22266 22267 22268 22269 22270 22271 22272 22273 22274 22275 22276 22277 22278 22279 22280 22281 22282 22283 22284 22285 22286 22287 22288 22289 22290 22291 22292 22293 22294 22295 22296 22297 22298 22299 22300 22301 22302 22303 22304 22305 22306 22307 22308 22309 22310 22311 22312 22313 22314 22315 22316 22317 22318 22319 22320 22321 22322 22323 22324 22325 22326 22327 22328 22329 22330 22331 22332 22333 22334 22335 22336 22337 22338 22339 22340 22341 22342 22343 22344 22345 22346 22347 22348 22349 22350 22351 22352 22353 22354 22355 22356 22357 22358 22359 22360 22361 22362 22363 22364 22365 22366 22367 22368 22369 22370 22371 22372 22373 22374 22375 22376 22377 22378 22379 22380 22381 22382 22383 22384 22385 22386 22387 22388 22389 22390 22391 22392 22393 22394 22395 22396 22397 22398 22399 22400 22401 22402 22403 22404 22405 22406 22407 22408 22409 22410 22411 22412 22413 22414 22415 22416 22417 22418 22419 22420 22421 22422 22423 22424 22425 22426 22427 22428 22429 22430 22431 22432 22433 22434 22435 22436 22437 22438 22439 22440 22441 22442 22443 22444 22445 22446 22447 22448 22449 22450 22451 22452 22453 22454 22455 22456 22457 22458 22459 22460 22461 22462 22463 22464 22465 22466 22467 22468 22469 22470 22471 22472 22473 22474 22475 22476 22477 22478 22479 22480 22481 22482 22483 22484 22485 22486 22487 22488 22489 22490 22491 22492 22493 22494 22495 22496 22497 22498 22499 22500 22501 22502 22503 22504 22505 22506 22507 22508 22509 22510 22511 22512 22513 22514 22515 22516 22517 22518 22519 22520 22521 22522 22523 22524 22525 22526 22527 22528 22529 22530 22531 22532 22533 22534 22535 22536 22537 22538 22539 22540 22541 22542 22543 22544 22545 22546 22547 22548 22549 22550 22551 22552 22553 22554 22555 22556 22557 22558 22559 22560 22561 22562 22563 22564 22565 22566 22567 22568 22569 22570 22571 22572 22573 22574 22575 22576 22577 22578 22579 22580 22581 22582 22583 22584 22585 22586 22587 22588 22589 22590 22591 22592 22593 22594 22595 22596 22597 22598 22599 22600 22601 22602 22603 22604 22605 22606 22607 22608 22609 22610 22611 22612 22613 22614 22615 22616 22617 22618 22619 22620 22621 22622 22623 22624 22625 22626 22627 22628 22629 22630 22631 22632 22633 22634 22635 22636 22637 22638 22639 22640 22641 22642 22643 22644 22645 22646 22647 22648 22649 22650 22651 22652 22653 22654 22655 22656 22657 22658 22659 22660 22661 22662 22663 22664 22665 22666 22667 22668 22669 22670 22671 22672 22673 22674 22675 22676 22677 22678 22679 22680 22681 22682 22683 22684 22685 22686 22687 22688 22689 22690 22691 22692 22693 22694 22695 22696 22697 22698 22699 22700 22701 22702 22703 22704 22705 22706 22707 22708 22709 22710 22711 22712 22713 22714 22715 22716 22717 22718 22719 22720 22721 22722 22723 22724 22725 22726 22727 22728 22729 22730 22731 22732 22733 22734 22735 22736 22737 22738 22739 22740 22741 22742 22743 22744 22745 22746 22747 22748 22749 22750 22751 22752 22753 22754 22755 22756 22757 22758 22759 22760 22761 22762 22763 22764 22765 22766 22767 22768 22769 22770 22771 22772 22773 22774 22775 22776 22777 22778 22779 22780 22781 22782 22783 22784 22785 22786 22787 22788 22789 22790 22791 22792 22793 22794 22795 22796 22797 22798 22799 22800 22801 22802 22803 22804 22805 22806 22807 22808 22809 22810 22811 22812 22813 22814 22815 22816 22817 22818 22819 22820 22821 22822 22823 22824 22825 22826 22827 22828 22829 22830 22831 22832 22833 22834 22835 22836 22837 22838 22839 22840 22841 22842 22843 22844 22845 22846 22847 22848 22849 22850 22851 22852 22853 22854 22855 22856 22857 22858 22859 22860 22861 22862 22863 22864 22865 22866 22867 22868 22869 22870 22871 22872 22873 22874 22875 22876 22877 22878 22879 22880 22881 22882 22883 22884 22885 22886 22887 22888 22889 22890 22891 22892 22893 22894 22895 22896 22897 22898 22899 22900 22901 22902 22903 22904 22905 22906 22907 22908 22909 22910 22911 22912 22913 22914 22915 22916 22917 22918 22919 22920 22921 22922 22923 22924 22925 22926 22927 22928 22929 22930 22931 22932 22933 22934 22935 22936 22937 22938 22939 22940 22941 22942 22943 22944 22945 22946 22947 22948 22949 22950 22951 22952 22953 22954 22955 22956 22957 22958 22959 22960 22961 22962 22963 22964 22965 22966 22967 22968 22969 22970 22971 22972 22973 22974 22975 22976 22977 22978 22979 22980 22981 22982 22983 22984 22985 22986 22987 22988 22989 22990 22991 22992 22993 22994 22995 22996 22997 22998 22999 23000 23001 23002 23003 23004 23005 23006 23007 23008 23009 23010 23011 23012 23013 23014 23015 23016 23017 23018 23019 23020 23021 23022 23023 23024 23025 23026 23027 23028 23029 23030 23031 23032 23033 23034 23035 23036 23037 23038 23039 23040 23041 23042 23043 23044 23045 23046 23047 23048 23049 23050 23051 23052 23053 23054 23055 23056 23057 23058 23059 23060 23061 23062 23063 23064 23065 23066 23067 23068 23069 23070 23071 23072 23073 23074 23075 23076 23077 23078 23079 23080 23081 23082 23083 23084 23085 23086 23087 23088 23089 23090 23091 23092 23093 23094 23095 23096 23097 23098 23099 23100 23101 23102 23103 23104 23105 23106 23107 23108 23109 23110 23111 23112 23113 23114 23115 23116 23117 23118 23119 23120 23121 23122 23123 23124 23125 23126 23127 23128 23129 23130 23131 23132 23133 23134 23135 23136 23137 23138 23139 23140 23141 23142 23143 23144 23145 23146 23147 23148 23149 23150 23151 23152 23153 23154 23155 23156 23157 23158 23159 23160 23161 23162 23163 23164 23165 23166 23167 23168 23169 23170 23171 23172 23173 23174 23175 23176 23177 23178 23179 23180 23181 23182 23183 23184 23185 23186 23187 23188 23189 23190 23191 23192 23193 23194 23195 23196 23197 23198 23199 23200 23201 23202 23203 23204 23205 23206 23207 23208 23209 23210 23211 23212 23213 23214 23215 23216 23217 23218 23219 23220 23221 23222 23223 23224 23225 23226 23227 23228 23229 23230 23231 23232 23233 23234 23235 23236 23237 23238 23239 23240 23241 23242 23243 23244 23245 23246 23247 23248 23249 23250 23251 23252 23253 23254 23255 23256 23257 23258 23259 23260 23261 23262 23263 23264 23265 23266 23267 23268 23269 23270 23271 23272 23273 23274 23275 23276 23277 23278 23279 23280 23281 23282 23283 23284 23285 23286 23287 23288 23289 23290 23291 23292 23293 23294 23295 23296 23297 23298 23299 23300 23301 23302 23303 23304 23305 23306 23307 23308 23309 23310 23311 23312 23313 23314 23315 23316 23317 23318 23319 23320 23321 23322 23323 23324 23325 23326 23327 23328 23329 23330 23331 23332 23333 23334 23335 23336 23337 23338 23339 23340 23341 23342 23343 23344 23345 23346 23347 23348 23349 23350 23351 23352 23353 23354 23355 23356 23357 23358 23359 23360 23361 23362 23363 23364 23365 23366 23367 23368 23369 23370 23371 23372 23373 23374 23375 23376 23377 23378 23379 23380 23381 23382 23383 23384 23385 23386 23387 23388 23389 23390 23391 23392 23393 23394 23395 23396 23397 23398 23399 23400 23401 23402 23403 23404 23405 23406 23407 23408 23409 23410 23411 23412 23413 23414 23415 23416 23417 23418 23419 23420 23421 23422 23423 23424 23425 23426 23427 23428 23429 23430 23431 23432 23433 23434 23435 23436 23437 23438 23439 23440 23441 23442 23443 23444 23445 23446 23447 23448 23449 23450 23451 23452 23453 23454 23455 23456 23457 23458 23459 23460 23461 23462 23463 23464 23465 23466 23467 23468 23469 23470 23471 23472 23473 23474 23475 23476 23477 23478 23479 23480 23481 23482 23483 23484 23485 23486 23487 23488 23489 23490 23491 23492 23493 23494 23495 23496 23497 23498 23499 23500 23501 23502 23503 23504 23505 23506 23507 23508 23509 23510 23511 23512 23513 23514 23515 23516 23517 23518 23519 23520 23521 23522 23523 23524 23525 23526 23527 23528 23529 23530 23531 23532 23533 23534 23535 23536 23537 23538 23539 23540 23541 23542 23543 23544 23545 23546 23547 23548 23549 23550 23551 23552 23553 23554 23555 23556 23557 23558 23559 23560 23561 23562 23563 23564 23565 23566 23567 23568 23569 23570 23571 23572 23573 23574 23575 23576 23577 23578 23579 23580 23581 23582 23583 23584 23585 23586 23587 23588 23589 23590 23591 23592 23593 23594 23595 23596 23597 23598 23599 23600 23601 23602 23603 23604 23605 23606 23607 23608 23609 23610 23611 23612 23613 23614 23615 23616 23617 23618 23619 23620 23621 23622 23623 23624 23625 23626 23627 23628 23629 23630 23631 23632 23633 23634 23635 23636 23637 23638 23639 23640 23641 23642 23643 23644 23645 23646 23647 23648 23649 23650 23651 23652 23653 23654 23655 23656 23657 23658 23659 23660 23661 23662 23663 23664 23665 23666 23667 23668 23669 23670 23671 23672 23673 23674 23675 23676 23677 23678 23679 23680 23681 23682 23683 23684 23685 23686 23687 23688 23689 23690 23691 23692 23693 23694 23695 23696 23697 23698 23699 23700 23701 23702 23703 23704 23705 23706 23707 23708 23709 23710 23711 23712 23713 23714 23715 23716 23717 23718 23719 23720 23721 23722 23723 23724 23725 23726 23727 23728 23729 23730 23731 23732 23733 23734 23735 23736 23737 23738 23739 23740 23741 23742 23743 23744 23745 23746 23747 23748 23749 23750 23751 23752 23753 23754 23755 23756 23757 23758 23759 23760 23761 23762 23763 23764 23765 23766 23767 23768 23769 23770 23771 23772 23773 23774 23775 23776 23777 23778 23779 23780 23781 23782 23783 23784 23785 23786 23787 23788 23789 23790 23791 23792 23793 23794 23795 23796 23797 23798 23799 23800 23801 23802 23803 23804 23805 23806 23807 23808 23809 23810 23811 23812 23813 23814 23815 23816 23817 23818 23819 23820 23821 23822 23823 23824 23825 23826 23827 23828 23829 23830 23831 23832 23833 23834 23835 23836 23837 23838 23839 23840 23841 23842 23843 23844 23845 23846 23847 23848 23849 23850 23851 23852 23853 23854 23855 23856 23857 23858 23859 23860 23861 23862 23863 23864 23865 23866 23867 23868 23869 23870 23871 23872 23873 23874 23875 23876 23877 23878 23879 23880 23881 23882 23883 23884 23885 23886 23887 23888 23889 23890 23891 23892 23893 23894 23895 23896 23897 23898 23899 23900 23901 23902 23903 23904 23905 23906 23907 23908 23909 23910 23911 23912 23913 23914 23915 23916 23917 23918 23919 23920 23921 23922 23923 23924 23925 23926 23927 23928 23929 23930 23931 23932 23933 23934 23935 23936 23937 23938 23939 23940 23941 23942 23943 23944 23945 23946 23947 23948 23949 23950 23951 23952 23953 23954 23955 23956 23957 23958 23959 23960 23961 23962 23963 23964 23965 23966 23967 23968 23969 23970 23971 23972 23973 23974 23975 23976 23977 23978 23979 23980 23981 23982 23983 23984 23985 23986 23987 23988 23989 23990 23991 23992 23993 23994 23995 23996 23997 23998 23999 24000 24001 24002 24003 24004 24005 24006 24007 24008 24009 24010 24011 24012 24013 24014 24015 24016 24017 24018 24019 24020 24021 24022 24023 24024 24025 24026 24027 24028 24029 24030 24031 24032 24033 24034 24035 24036 24037 24038 24039 24040 24041 24042 24043 24044 24045 24046 24047 24048 24049 24050 24051 24052 24053 24054 24055 24056 24057 24058 24059 24060 24061 24062 24063 24064 24065 24066 24067 24068 24069 24070 24071 24072 24073 24074 24075 24076 24077 24078 24079 24080 24081 24082 24083 24084 24085 24086 24087 24088 24089 24090 24091 24092 24093 24094 24095 24096 24097 24098 24099 24100 24101 24102 24103 24104 24105 24106 24107 24108 24109 24110 24111 24112 24113 24114 24115 24116 24117 24118 24119 24120 24121 24122 24123 24124 24125 24126 24127 24128 24129 24130 24131 24132 24133 24134 24135 24136 24137 24138 24139 24140 24141 24142 24143 24144 24145 24146 24147 24148 24149 24150 24151 24152 24153 24154 24155 24156 24157 24158 24159 24160 24161 24162 24163 24164 24165 24166 24167 24168 24169 24170 24171 24172 24173 24174 24175 24176 24177 24178 24179 24180 24181 24182 24183 24184 24185 24186 24187 24188 24189 24190 24191 24192 24193 24194 24195 24196 24197 24198 24199 24200 24201 24202 24203 24204 24205 24206 24207 24208 24209 24210 24211 24212 24213 24214 24215 24216 24217 24218 24219 24220 24221 24222 24223 24224 24225 24226 24227 24228 24229 24230 24231 24232 24233 24234 24235 24236 24237 24238 24239 24240 24241 24242 24243 24244 24245 24246 24247 24248 24249 24250 24251 24252 24253 24254 24255 24256 24257 24258 24259 24260 24261 24262 24263 24264 24265 24266 24267 24268 24269 24270 24271 24272 24273 24274 24275 24276 24277 24278 24279 24280 24281 24282 24283 24284 24285 24286 24287 24288 24289 24290 24291 24292 24293 24294 24295 24296 24297 24298 24299 24300 24301 24302 24303 24304 24305 24306 24307 24308 24309 24310 24311 24312 24313 24314 24315 24316 24317 24318 24319 24320 24321 24322 24323 24324 24325 24326 24327 24328 24329 24330 24331 24332 24333 24334 24335 24336 24337 24338 24339 24340 24341 24342 24343 24344 24345 24346 24347 24348 24349 24350 24351 24352 24353 24354 24355 24356 24357 24358 24359 24360 24361 24362 24363 24364 24365 24366 24367 24368 24369 24370 24371 24372 24373 24374 24375 24376 24377 24378 24379 24380 24381 24382 24383 24384 24385 24386 24387 24388 24389 24390 24391 24392 24393 24394 24395 24396 24397 24398 24399 24400 24401 24402 24403 24404 24405 24406 24407 24408 24409 24410 24411 24412 24413 24414 24415 24416 24417 24418 24419 24420 24421 24422 24423 24424 24425 24426 24427 24428 24429 24430 24431 24432 24433 24434 24435 24436 24437 24438 24439 24440 24441 24442 24443 24444 24445 24446 24447 24448 24449 24450 24451 24452 24453 24454 24455 24456 24457 24458 24459 24460 24461 24462 24463 24464 24465 24466 24467 24468 24469 24470 24471 24472 24473 24474 24475 24476 24477 24478 24479 24480 24481 24482 24483 24484 24485 24486 24487 24488 24489 24490 24491 24492 24493 24494 24495 24496 24497 24498 24499 24500 24501 24502 24503 24504 24505 24506 24507 24508 24509 24510 24511 24512 24513 24514 24515 24516 24517 24518 24519 24520 24521 24522 24523 24524 24525 24526 24527 24528 24529 24530 24531 24532 24533 24534 24535 24536 24537 24538 24539 24540 24541 24542 24543 24544 24545 24546 24547 24548 24549 24550 24551 24552 24553 24554 24555 24556 24557 24558 24559 24560 24561 24562 24563 24564 24565 24566 24567 24568 24569 24570 24571 24572 24573 24574 24575 24576 24577 24578 24579 24580 24581 24582 24583 24584 24585 24586 24587 24588 24589 24590 24591 24592 24593 24594 24595 24596 24597 24598 24599 24600 24601 24602 24603 24604 24605 24606 24607 24608 24609 24610 24611 24612 24613 24614 24615 24616 24617 24618 24619 24620 24621 24622 24623 24624 24625 24626 24627 24628 24629 24630 24631 24632 24633 24634 24635 24636 24637 24638 24639 24640 24641 24642 24643 24644 24645 24646 24647 24648 24649 24650 24651 24652 24653 24654 24655 24656 24657 24658 24659 24660 24661 24662 24663 24664 24665 24666 24667 24668 24669 24670 24671 24672 24673 24674 24675 24676 24677 24678 24679 24680 24681 24682 24683 24684 24685 24686 24687 24688 24689 24690 24691 24692 24693 24694 24695 24696 24697 24698 24699 24700 24701 24702 24703 24704 24705 24706 24707 24708 24709 24710 24711 24712 24713 24714 24715 24716 24717 24718 24719 24720 24721 24722 24723 24724 24725 24726 24727 24728 24729 24730 24731 24732 24733 24734 24735 24736 24737 24738 24739 24740 24741 24742 24743 24744 24745 24746 24747 24748 24749 24750 24751 24752 24753 24754 24755 24756 24757 24758 24759 24760 24761 24762 24763 24764 24765 24766 24767 24768 24769 24770 24771 24772 24773 24774 24775 24776 24777 24778 24779 24780 24781 24782 24783 24784 24785 24786 24787 24788 24789 24790 24791 24792 24793 24794 24795 24796 24797 24798 24799 24800 24801 24802 24803 24804 24805 24806 24807 24808 24809 24810 24811 24812 24813 24814 24815 24816 24817 24818 24819 24820 24821 24822 24823 24824 24825 24826 24827 24828 24829 24830 24831 24832 24833 24834 24835 24836 24837 24838 24839 24840 24841 24842 24843 24844 24845 24846 24847 24848 24849 24850 24851 24852 24853 24854 24855 24856 24857 24858 24859 24860 24861 24862 24863 24864 24865 24866 24867 24868 24869 24870 24871 24872 24873 24874 24875 24876 24877 24878 24879 24880 24881 24882 24883 24884 24885 24886 24887 24888 24889 24890 24891 24892 24893 24894 24895 24896 24897 24898 24899 24900 24901 24902 24903 24904 24905 24906 24907 24908 24909 24910 24911 24912 24913 24914 24915 24916 24917 24918 24919 24920 24921 24922 24923 24924 24925 24926 24927 24928 24929 24930 24931 24932 24933 24934 24935 24936 24937 24938 24939 24940 24941 24942 24943 24944 24945 24946 24947 24948 24949 24950 24951 24952 24953 24954 24955 24956 24957 24958 24959 24960 24961 24962 24963 24964 24965 24966 24967 24968 24969 24970 24971 24972 24973 24974 24975 24976 24977 24978 24979 24980 24981 24982 24983 24984 24985 24986 24987 24988 24989 24990 24991 24992 24993 24994 24995 24996 24997 24998 24999 25000 25001 25002 25003 25004 25005 25006 25007 25008 25009 25010 25011 25012 25013 25014 25015 25016 25017 25018 25019 25020 25021 25022 25023 25024 25025 25026 25027 25028 25029 25030 25031 25032 25033 25034 25035 25036 25037 25038 25039 25040 25041 25042 25043 25044 25045 25046 25047 25048 25049 25050 25051 25052 25053 25054 25055 25056 25057 25058 25059 25060 25061 25062 25063 25064 25065 25066 25067 25068 25069 25070 25071 25072 25073 25074 25075 25076 25077 25078 25079 25080 25081 25082 25083 25084 25085 25086 25087 25088 25089 25090 25091 25092 25093 25094 25095 25096 25097 25098 25099 25100 25101 25102 25103 25104 25105 25106 25107 25108 25109 25110 25111 25112 25113 25114 25115 25116 25117 25118 25119 25120 25121 25122 25123 25124 25125 25126 25127 25128 25129 25130 25131 25132 25133 25134 25135 25136 25137 25138 25139 25140 25141 25142 25143 25144 25145 25146 25147 25148 25149 25150 25151 25152 25153 25154 25155 25156 25157 25158 25159 25160 25161 25162 25163 25164 25165 25166 25167 25168 25169 25170 25171 25172 25173 25174 25175 25176 25177 25178 25179 25180 25181 25182 25183 25184 25185 25186 25187 25188 25189 25190 25191 25192 25193 25194 25195 25196 25197 25198 25199 25200 25201 25202 25203 25204 25205 25206 25207 25208 25209 25210 25211 25212 25213 25214 25215 25216 25217 25218 25219 25220 25221 25222 25223 25224 25225 25226 25227 25228 25229 25230 25231 25232 25233 25234 25235 25236 25237 25238 25239 25240 25241 25242 25243 25244 25245 25246 25247 25248 25249 25250 25251 25252 25253 25254 25255 25256 25257 25258 25259 25260 25261 25262 25263 25264 25265 25266 25267 25268 25269 25270 25271 25272 25273 25274 25275 25276 25277 25278 25279 25280 25281 25282 25283 25284 25285 25286 25287 25288 25289 25290 25291 25292 25293 25294 25295 25296 25297 25298 25299 25300 25301 25302 25303 25304 25305 25306 25307 25308 25309 25310 25311 25312 25313 25314 25315 25316 25317 25318 25319 25320 25321 25322 25323 25324 25325 25326 25327 25328 25329 25330 25331 25332 25333 25334 25335 25336 25337 25338 25339 25340 25341 25342 25343 25344 25345 25346 25347 25348 25349 25350 25351 25352 25353 25354 25355 25356 25357 25358 25359 25360 25361 25362 25363 25364 25365 25366 25367 25368 25369 25370 25371 25372 25373 25374 25375 25376 25377 25378 25379 25380 25381 25382 25383 25384 25385 25386 25387 25388 25389 25390 25391 25392 25393 25394 25395 25396 25397 25398 25399 25400 25401 25402 25403 25404 25405 25406 25407 25408 25409 25410 25411 25412 25413 25414 25415 25416 25417 25418 25419 25420 25421 25422 25423 25424 25425 25426 25427 25428 25429 25430 25431 25432 25433 25434 25435 25436 25437 25438 25439 25440 25441 25442 25443 25444 25445 25446 25447 25448 25449 25450 25451 25452 25453 25454 25455 25456 25457 25458 25459 25460 25461 25462 25463 25464 25465 25466 25467 25468 25469 25470 25471 25472 25473 25474 25475 25476 25477 25478 25479 25480 25481 25482 25483 25484 25485 25486 25487 25488 25489 25490 25491 25492 25493 25494 25495 25496 25497 25498 25499 25500 25501 25502 25503 25504 25505 25506 25507 25508 25509 25510 25511 25512 25513 25514 25515 25516 25517 25518 25519 25520 25521 25522 25523 25524 25525 25526 25527 25528 25529 25530 25531 25532 25533 25534 25535 25536 25537 25538 25539 25540 25541 25542 25543 25544 25545 25546 25547 25548 25549 25550 25551 25552 25553 25554 25555 25556 25557 25558 25559 25560 25561 25562 25563 25564 25565 25566 25567 25568 25569 25570 25571 25572 25573 25574 25575 25576 25577 25578 25579 25580 25581 25582 25583 25584 25585 25586 25587 25588 25589 25590 25591 25592 25593 25594 25595 25596 25597 25598 25599 25600 25601 25602 25603 25604 25605 25606 25607 25608 25609 25610 25611 25612 25613 25614 25615 25616 25617 25618 25619 25620 25621 25622 25623 25624 25625 25626 25627 25628 25629 25630 25631 25632 25633 25634 25635 25636 25637 25638 25639 25640 25641 25642 25643 25644 25645 25646 25647 25648 25649 25650 25651 25652 25653 25654 25655 25656 25657 25658 25659 25660 25661 25662 25663 25664 25665 25666 25667 25668 25669 25670 25671 25672 25673 25674 25675 25676 25677 25678 25679 25680 25681 25682 25683 25684 25685 25686 25687 25688 25689 25690 25691 25692 25693 25694 25695 25696 25697 25698 25699 25700 25701 25702 25703 25704 25705 25706 25707 25708 25709 25710 25711 25712 25713 25714 25715 25716 25717 25718 25719 25720 25721 25722 25723 25724 25725 25726 25727 25728 25729 25730 25731 25732 25733 25734 25735 25736 25737 25738 25739 25740 25741 25742 25743 25744 25745 25746 25747 25748 25749 25750 25751 25752 25753 25754 25755 25756 25757 25758 25759 25760 25761 25762 25763 25764 25765 25766 25767 25768 25769 25770 25771 25772 25773 25774 25775 25776 25777 25778 25779 25780 25781 25782 25783 25784 25785 25786 25787 25788 25789 25790 25791 25792 25793 25794 25795 25796 25797 25798 25799 25800 25801 25802 25803 25804 25805 25806 25807 25808 25809 25810 25811 25812 25813 25814 25815 25816 25817 25818 25819 25820 25821 25822 25823 25824 25825 25826 25827 25828 25829 25830 25831 25832 25833 25834 25835 25836 25837 25838 25839 25840 25841 25842 25843 25844 25845 25846 25847 25848 25849 25850 25851 25852 25853 25854 25855 25856 25857 25858 25859 25860 25861 25862 25863 25864 25865 25866 25867 25868 25869 25870 25871 25872 25873 25874 25875 25876 25877 25878 25879 25880 25881 25882 25883 25884 25885 25886 25887 25888 25889 25890 25891 25892 25893 25894 25895 25896 25897 25898 25899 25900 25901 25902 25903 25904 25905 25906 25907 25908 25909 25910 25911 25912 25913 25914 25915 25916 25917 25918 25919 25920 25921 25922 25923 25924 25925 25926 25927 25928 25929 25930 25931 25932 25933 25934 25935 25936 25937 25938 25939 25940 25941 25942 25943 25944 25945 25946 25947 25948 25949 25950 25951 25952 25953 25954 25955 25956 25957 25958 25959 25960 25961 25962 25963 25964 25965 25966 25967 25968 25969 25970 25971 25972 25973 25974 25975 25976 25977 25978 25979 25980 25981 25982 25983 25984 25985 25986 25987 25988 25989 25990 25991 25992 25993 25994 25995 25996 25997 25998 25999 26000 26001 26002 26003 26004 26005 26006 26007 26008 26009 26010 26011 26012 26013 26014 26015 26016 26017 26018 26019 26020 26021 26022 26023 26024 26025 26026 26027 26028 26029 26030 26031 26032 26033 26034 26035 26036 26037 26038 26039 26040 26041 26042 26043 26044 26045 26046 26047 26048 26049 26050 26051 26052 26053 26054 26055 26056 26057 26058 26059 26060 26061 26062 26063 26064 26065 26066 26067 26068 26069 26070 26071 26072 26073 26074 26075 26076 26077 26078 26079 26080 26081 26082 26083 26084 26085 26086 26087 26088 26089 26090 26091 26092 26093 26094 26095 26096 26097 26098 26099 26100 26101 26102 26103 26104 26105 26106 26107 26108 26109 26110 26111 26112 26113 26114 26115 26116 26117 26118 26119 26120 26121 26122 26123 26124 26125 26126 26127 26128 26129 26130 26131 26132 26133 26134 26135 26136 26137 26138 26139 26140 26141 26142 26143 26144 26145 26146 26147 26148 26149 26150 26151 26152 26153 26154 26155 26156 26157 26158 26159 26160 26161 26162 26163 26164 26165 26166 26167 26168 26169 26170 26171 26172 26173 26174 26175 26176 26177 26178 26179 26180 26181 26182 26183 26184 26185 26186 26187 26188 26189 26190 26191 26192 26193 26194 26195 26196 26197 26198 26199 26200 26201 26202 26203 26204 26205 26206 26207 26208 26209 26210 26211 26212 26213 26214 26215 26216 26217 26218 26219 26220 26221 26222 26223 26224 26225 26226 26227 26228 26229 26230 26231 26232 26233 26234 26235 26236 26237 26238 26239 26240 26241 26242 26243 26244 26245 26246 26247 26248 26249 26250 26251 26252 26253 26254 26255 26256 26257 26258 26259 26260 26261 26262 26263 26264 26265 26266 26267 26268 26269 26270 26271 26272 26273 26274 26275 26276 26277 26278 26279 26280 26281 26282 26283 26284 26285 26286 26287 26288 26289 26290 26291 26292 26293 26294 26295 26296 26297 26298 26299 26300 26301 26302 26303 26304 26305 26306 26307 26308 26309 26310 26311 26312 26313 26314 26315 26316 26317 26318 26319 26320 26321 26322 26323 26324 26325 26326 26327 26328 26329 26330 26331 26332 26333 26334 26335 26336 26337 26338 26339 26340 26341 26342 26343 26344 26345 26346 26347 26348 26349 26350 26351 26352 26353 26354 26355 26356 26357 26358 26359 26360 26361 26362 26363 26364 26365 26366 26367 26368 26369 26370 26371 26372 26373 26374 26375 26376 26377 26378 26379 26380 26381 26382 26383 26384 26385 26386 26387 26388 26389 26390 26391 26392 26393 26394 26395 26396 26397 26398 26399 26400 26401 26402 26403 26404 26405 26406 26407 26408 26409 26410 26411 26412 26413 26414 26415 26416 26417 26418 26419 26420 26421 26422 26423 26424 26425 26426 26427 26428 26429 26430 26431 26432 26433 26434 26435 26436 26437 26438 26439 26440 26441 26442 26443 26444 26445 26446 26447 26448 26449 26450 26451 26452 26453 26454 26455 26456 26457 26458 26459 26460 26461 26462 26463 26464 26465 26466 26467 26468 26469 26470 26471 26472 26473 26474 26475 26476 26477 26478 26479 26480 26481 26482 26483 26484 26485 26486 26487 26488 26489 26490 26491 26492 26493 26494 26495 26496 26497 26498 26499 26500 26501 26502 26503 26504 26505 26506 26507 26508 26509 26510 26511 26512 26513 26514 26515 26516 26517 26518 26519 26520 26521 26522 26523 26524 26525 26526 26527 26528 26529 26530 26531 26532 26533 26534 26535 26536 26537 26538 26539 26540 26541 26542 26543 26544 26545 26546 26547 26548 26549 26550 26551 26552 26553 26554 26555 26556 26557 26558 26559 26560 26561 26562 26563 26564 26565 26566 26567 26568 26569 26570 26571 26572 26573 26574 26575 26576 26577 26578 26579 26580 26581 26582 26583 26584 26585 26586 26587 26588 26589 26590 26591 26592 26593 26594 26595 26596 26597 26598 26599 26600 26601 26602 26603 26604 26605 26606 26607 26608 26609 26610 26611 26612 26613 26614 26615 26616 26617 26618 26619 26620 26621 26622 26623 26624 26625 26626 26627 26628 26629 26630 26631 26632 26633 26634 26635 26636 26637 26638 26639 26640 26641 26642 26643 26644 26645 26646 26647 26648 26649 26650 26651 26652 26653 26654 26655 26656 26657 26658 26659 26660 26661 26662 26663 26664 26665 26666 26667 26668 26669 26670 26671 26672 26673 26674 26675 26676 26677 26678 26679 26680 26681 26682 26683 26684 26685 26686 26687 26688 26689 26690 26691 26692 26693 26694 26695 26696 26697 26698 26699 26700 26701 26702 26703 26704 26705 26706 26707 26708 26709 26710 26711 26712 26713 26714 26715 26716 26717 26718 26719 26720 26721 26722 26723 26724 26725 26726 26727 26728 26729 26730 26731 26732 26733 26734 26735 26736 26737 26738 26739 26740 26741 26742 26743 26744 26745 26746 26747 26748 26749 26750 26751 26752 26753 26754 26755 26756 26757 26758 26759 26760 26761 26762 26763 26764 26765 26766 26767 26768 26769 26770 26771 26772 26773 26774 26775 26776 26777 26778 26779 26780 26781 26782 26783 26784 26785 26786 26787 26788 26789 26790 26791 26792 26793 26794 26795 26796 26797 26798 26799 26800 26801 26802 26803 26804 26805 26806 26807 26808 26809 26810 26811 26812 26813 26814 26815 26816 26817 26818 26819 26820 26821 26822 26823 26824 26825 26826 26827 26828 26829 26830 26831 26832 26833 26834 26835 26836 26837 26838 26839 26840 26841 26842 26843 26844 26845 26846 26847 26848 26849 26850 26851 26852 26853 26854 26855 26856 26857 26858 26859 26860 26861 26862 26863 26864 26865 26866 26867 26868 26869 26870 26871 26872 26873 26874 26875 26876 26877 26878 26879 26880 26881 26882 26883 26884 26885 26886 26887 26888 26889 26890 26891 26892 26893 26894 26895 26896 26897 26898 26899 26900 26901 26902 26903 26904 26905 26906 26907 26908 26909 26910 26911 26912 26913 26914 26915 26916 26917 26918 26919 26920 26921 26922 26923 26924 26925 26926 26927 26928 26929 26930 26931 26932 26933 26934 26935 26936 26937 26938 26939 26940 26941 26942 26943 26944 26945 26946 26947 26948 26949 26950 26951 26952 26953 26954 26955 26956 26957 26958 26959 26960 26961 26962 26963 26964 26965 26966 26967 26968 26969 26970 26971 26972 26973 26974 26975 26976 26977 26978 26979 26980 26981 26982 26983 26984 26985 26986 26987 26988 26989 26990 26991 26992 26993 26994 26995 26996 26997 26998 26999 27000 27001 27002 27003 27004 27005 27006 27007 27008 27009 27010 27011 27012 27013 27014 27015 27016 27017 27018 27019 27020 27021 27022 27023 27024 27025 27026 27027 27028 27029 27030 27031 27032 27033 27034 27035 27036 27037 27038 27039 27040 27041 27042 27043 27044 27045 27046 27047 27048 27049 27050 27051 27052 27053 27054 27055 27056 27057 27058 27059 27060 27061 27062 27063 27064 27065 27066 27067 27068 27069 27070 27071 27072 27073 27074 27075 27076 27077 27078 27079 27080 27081 27082 27083 27084 27085 27086 27087 27088 27089 27090 27091 27092 27093 27094 27095 27096 27097 27098 27099 27100 27101 27102 27103 27104 27105 27106 27107 27108 27109 27110 27111 27112 27113 27114 27115 27116 27117 27118 27119 27120 27121 27122 27123 27124 27125 27126 27127 27128 27129 27130 27131 27132 27133 27134 27135 27136 27137 27138 27139 27140 27141 27142 27143 27144 27145 27146 27147 27148 27149 27150 27151 27152 27153 27154 27155 27156 27157 27158 27159 27160 27161 27162 27163 27164 27165 27166 27167 27168 27169 27170 27171 27172 27173 27174 27175 27176 27177 27178 27179 27180 27181 27182 27183 27184 27185 27186 27187 27188 27189 27190 27191 27192 27193 27194 27195 27196 27197 27198 27199 27200 27201 27202 27203 27204 27205 27206 27207 27208 27209 27210 27211 27212 27213 27214 27215 27216 27217 27218 27219 27220 27221 27222 27223 27224 27225 27226 27227 27228 27229 27230 27231 27232 27233 27234 27235 27236 27237 27238 27239 27240 27241 27242 27243 27244 27245 27246 27247 27248 27249 27250 27251 27252 27253 27254 27255 27256 27257 27258 27259 27260 27261 27262 27263 27264 27265 27266 27267 27268 27269 27270 27271 27272 27273 27274 27275 27276 27277 27278 27279 27280 27281 27282 27283 27284 27285 27286 27287 27288 27289 27290 27291 27292 27293 27294 27295 27296 27297 27298 27299 27300 27301 27302 27303 27304 27305 27306 27307 27308 27309 27310 27311 27312 27313 27314 27315 27316 27317 27318 27319 27320 27321 27322 27323 27324 27325 27326 27327 27328 27329 27330 27331 27332 27333 27334 27335 27336 27337 27338 27339 27340 27341 27342 27343 27344 27345 27346 27347 27348 27349 27350 27351 27352 27353 27354 27355 27356 27357 27358 27359 27360 27361 27362 27363 27364 27365 27366 27367 27368 27369 27370 27371 27372 27373 27374 27375 27376 27377 27378 27379 27380 27381 27382 27383 27384 27385 27386 27387 27388 27389 27390 27391 27392 27393 27394 27395 27396 27397 27398 27399 27400 27401 27402 27403 27404 27405 27406 27407 27408 27409 27410 27411 27412 27413 27414 27415 27416 27417 27418 27419 27420 27421 27422 27423 27424 27425 27426 27427 27428 27429 27430 27431 27432 27433 27434 27435 27436 27437 27438 27439 27440 27441 27442 27443 27444 27445 27446 27447 27448 27449 27450 27451 27452 27453 27454 27455 27456 27457 27458 27459 27460 27461 27462 27463 27464 27465 27466 27467 27468 27469 27470 27471 27472 27473 27474 27475 27476 27477 27478 27479 27480 27481 27482 27483 27484 27485 27486 27487 27488 27489 27490 27491 27492 27493 27494 27495 27496 27497 27498 27499 27500 27501 27502 27503 27504 27505 27506 27507 27508 27509 27510 27511 27512 27513 27514 27515 27516 27517 27518 27519 27520 27521 27522 27523 27524 27525 27526 27527 27528 27529 27530 27531 27532 27533 27534 27535 27536 27537 27538 27539 27540 27541 27542 27543 27544 27545 27546 27547 27548 27549 27550 27551 27552 27553 27554 27555 27556 27557 27558 27559 27560 27561 27562 27563 27564 27565 27566 27567 27568 27569 27570 27571 27572 27573 27574 27575 27576 27577 27578 27579 27580 27581 27582 27583 27584 27585 27586 27587 27588 27589 27590 27591 27592 27593 27594 27595 27596 27597 27598 27599 27600 27601 27602 27603 27604 27605 27606 27607 27608 27609 27610 27611 27612 27613 27614 27615 27616 27617 27618 27619 27620 27621 27622 27623 27624 27625 27626 27627 27628 27629 27630 27631 27632 27633 27634 27635 27636 27637 27638 27639 27640 27641 27642 27643 27644 27645 27646 27647 27648 27649 27650 27651 27652 27653 27654 27655 27656 27657 27658 27659 27660 27661 27662 27663 27664 27665 27666 27667 27668 27669 27670 27671 27672 27673 27674 27675 27676 27677 27678 27679 27680 27681 27682 27683 27684 27685 27686 27687 27688 27689 27690 27691 27692 27693 27694 27695 27696 27697 27698 27699 27700 27701 27702 27703 27704 27705 27706 27707 27708 27709 27710 27711 27712 27713 27714 27715 27716 27717 27718 27719 27720 27721 27722 27723 27724 27725 27726 27727 27728 27729 27730 27731 27732 27733 27734 27735 27736 27737 27738 27739 27740 27741 27742 27743 27744 27745 27746 27747 27748 27749 27750 27751 27752 27753 27754 27755 27756 27757 27758 27759 27760 27761 27762 27763 27764 27765 27766 27767 27768 27769 27770 27771 27772 27773 27774 27775 27776 27777 27778 27779 27780 27781 27782 27783 27784 27785 27786 27787 27788 27789 27790 27791 27792 27793 27794 27795 27796 27797 27798 27799 27800 27801 27802 27803 27804 27805 27806 27807 27808 27809 27810 27811 27812 27813 27814 27815 27816 27817 27818 27819 27820 27821 27822 27823 27824 27825 27826 27827 27828 27829 27830 27831 27832 27833 27834 27835 27836 27837 27838 27839 27840 27841 27842 27843 27844 27845 27846 27847 27848 27849 27850 27851 27852 27853 27854 27855 27856 27857 27858 27859 27860 27861 27862 27863 27864 27865 27866 27867 27868 27869 27870 27871 27872 27873 27874 27875 27876 27877 27878 27879 27880 27881 27882 27883 27884 27885 27886 27887 27888 27889 27890 27891 27892 27893 27894 27895 27896 27897 27898 27899 27900 27901 27902 27903 27904 27905 27906 27907 27908 27909 27910 27911 27912 27913 27914 27915 27916 27917 27918 27919 27920 27921 27922 27923 27924 27925 27926 27927 27928 27929 27930 27931 27932 27933 27934 27935 27936 27937 27938 27939 27940 27941 27942 27943 27944 27945 27946 27947 27948 27949 27950 27951 27952 27953 27954 27955 27956 27957 27958 27959 27960 27961 27962 27963 27964 27965 27966 27967 27968 27969 27970 27971 27972 27973 27974 27975 27976 27977 27978 27979 27980 27981 27982 27983 27984 27985 27986 27987 27988 27989 27990 27991 27992 27993 27994 27995 27996 27997 27998 27999 28000 28001 28002 28003 28004 28005 28006 28007 28008 28009 28010 28011 28012 28013 28014 28015 28016 28017 28018 28019 28020 28021 28022 28023 28024 28025 28026 28027 28028 28029 28030 28031 28032 28033 28034 28035 28036 28037 28038 28039 28040 28041 28042 28043 28044 28045 28046 28047 28048 28049 28050 28051 28052 28053 28054 28055 28056 28057 28058 28059 28060 28061 28062 28063 28064 28065 28066 28067 28068 28069 28070 28071 28072 28073 28074 28075 28076 28077 28078 28079 28080 28081 28082 28083 28084 28085 28086 28087 28088 28089 28090 28091 28092 28093 28094 28095 28096 28097 28098 28099 28100 28101 28102 28103 28104 28105 28106 28107 28108 28109 28110 28111 28112 28113 28114 28115 28116 28117 28118 28119 28120 28121 28122 28123 28124 28125 28126 28127 28128 28129 28130 28131 28132 28133 28134 28135 28136 28137 28138 28139 28140 28141 28142 28143 28144 28145 28146 28147 28148 28149 28150 28151 28152 28153 28154 28155 28156 28157 28158 28159 28160 28161 28162 28163 28164 28165 28166 28167 28168 28169 28170 28171 28172 28173 28174 28175 28176 28177 28178 28179 28180 28181 28182 28183 28184 28185 28186 28187 28188 28189 28190 28191 28192 28193 28194 28195 28196 28197 28198 28199 28200 28201 28202 28203 28204 28205 28206 28207 28208 28209 28210 28211 28212 28213 28214 28215 28216 28217 28218 28219 28220 28221 28222 28223 28224 28225 28226 28227 28228 28229 28230 28231 28232 28233 28234 28235 28236 28237 28238 28239 28240 28241 28242 28243 28244 28245 28246 28247 28248 28249 28250 28251 28252 28253 28254 28255 28256 28257 28258 28259 28260 28261 28262 28263 28264 28265 28266 28267 28268 28269 28270 28271 28272 28273 28274 28275 28276 28277 28278 28279 28280 28281 28282 28283 28284 28285 28286 28287 28288 28289 28290 28291 28292 28293 28294 28295 28296 28297 28298 28299 28300 28301 28302 28303 28304 28305 28306 28307 28308 28309 28310 28311 28312 28313 28314 28315 28316 28317 28318 28319 28320 28321 28322 28323 28324 28325 28326 28327 28328 28329 28330 28331 28332 28333 28334 28335 28336 28337 28338 28339 28340 28341 28342 28343 28344 28345 28346 28347 28348 28349 28350 28351 28352 28353 28354 28355 28356 28357 28358 28359 28360 28361 28362 28363 28364 28365 28366 28367 28368 28369 28370 28371 28372 28373 28374 28375 28376 28377 28378 28379 28380 28381 28382 28383 28384 28385 28386 28387 28388 28389 28390 28391 28392 28393 28394 28395 28396 28397 28398 28399 28400 28401 28402 28403 28404 28405 28406 28407 28408 28409 28410 28411 28412 28413 28414 28415 28416 28417 28418 28419 28420 28421 28422 28423 28424 28425 28426 28427 28428 28429 28430 28431 28432 28433 28434 28435 28436 28437 28438 28439 28440 28441 28442 28443 28444 28445 28446 28447 28448 28449 28450 28451 28452 28453 28454 28455 28456 28457 28458 28459 28460 28461 28462 28463 28464 28465 28466 28467 28468 28469 28470 28471 28472 28473 28474 28475 28476 28477 28478 28479 28480 28481 28482 28483 28484 28485 28486 28487 28488 28489 28490 28491 28492 28493 28494 28495 28496 28497 28498 28499 28500 28501 28502 28503 28504 28505 28506 28507 28508 28509 28510 28511 28512 28513 28514 28515 28516 28517 28518 28519 28520 28521 28522 28523 28524 28525 28526 28527 28528 28529 28530 28531 28532 28533 28534 28535 28536 28537 28538 28539 28540 28541 28542 28543 28544 28545 28546 28547 28548 28549 28550 28551 28552 28553 28554 28555 28556 28557 28558 28559 28560 28561 28562 28563 28564 28565 28566 28567 28568 28569 28570 28571 28572 28573 28574 28575 28576 28577 28578 28579 28580 28581 28582 28583 28584 28585 28586 28587 28588 28589 28590 28591 28592 28593 28594 28595 28596 28597 28598 28599 28600 28601 28602 28603 28604 28605 28606 28607 28608 28609 28610 28611 28612 28613 28614 28615 28616 28617 28618 28619 28620 28621 28622 28623 28624 28625 28626 28627 28628 28629 28630 28631 28632 28633 28634 28635 28636 28637 28638 28639 28640 28641 28642 28643 28644 28645 28646 28647 28648 28649 28650 28651 28652 28653 28654 28655 28656 28657 28658 28659 28660 28661 28662 28663 28664 28665 28666 28667 28668 28669 28670 28671 28672 28673 28674 28675 28676 28677 28678 28679 28680 28681 28682 28683 28684 28685 28686 28687 28688 28689 28690 28691 28692 28693 28694 28695 28696 28697 28698 28699 28700 28701 28702 28703 28704 28705 28706 28707 28708 28709 28710 28711 28712 28713 28714 28715 28716 28717 28718 28719 28720 28721 28722 28723 28724 28725 28726 28727 28728 28729 28730 28731 28732 28733 28734 28735 28736 28737 28738 28739 28740 28741 28742 28743 28744 28745 28746 28747 28748 28749 28750 28751 28752 28753 28754 28755 28756 28757 28758 28759 28760 28761 28762 28763 28764 28765 28766 28767 28768 28769 28770 28771 28772 28773 28774 28775 28776 28777 28778 28779 28780 28781 28782 28783 28784 28785 28786 28787 28788 28789 28790 28791 28792 28793 28794 28795 28796 28797 28798 28799 28800 28801 28802 28803 28804 28805 28806 28807 28808 28809 28810 28811 28812 28813 28814 28815 28816 28817 28818 28819 28820 28821 28822 28823 28824 28825 28826 28827 28828 28829 28830 28831 28832 28833 28834 28835 28836 28837 28838 28839 28840 28841 28842 28843 28844 28845 28846 28847 28848 28849 28850 28851 28852 28853 28854 28855 28856 28857 28858 28859 28860 28861 28862 28863 28864 28865 28866 28867 28868 28869 28870 28871 28872 28873 28874 28875 28876 28877 28878 28879 28880 28881 28882 28883 28884 28885 28886 28887 28888 28889 28890 28891 28892 28893 28894 28895 28896 28897 28898 28899 28900 28901 28902 28903 28904 28905 28906 28907 28908 28909 28910 28911 28912 28913 28914 28915 28916 28917 28918 28919 28920 28921 28922 28923 28924 28925 28926 28927 28928 28929 28930 28931 28932 28933 28934 28935 28936 28937 28938 28939 28940 28941 28942 28943 28944 28945 28946 28947 28948 28949 28950 28951 28952 28953 28954 28955 28956 28957 28958 28959 28960 28961 28962 28963 28964 28965 28966 28967 28968 28969 28970 28971 28972 28973 28974 28975 28976 28977 28978 28979 28980 28981 28982 28983 28984 28985 28986 28987 28988 28989 28990 28991 28992 28993 28994 28995 28996 28997 28998 28999 29000 29001 29002 29003 29004 29005 29006 29007 29008 29009 29010 29011 29012 29013 29014 29015 29016 29017 29018 29019 29020 29021 29022 29023 29024 29025 29026 29027 29028 29029 29030 29031 29032 29033 29034 29035 29036 29037 29038 29039 29040 29041 29042 29043 29044 29045 29046 29047 29048 29049 29050 29051 29052 29053 29054 29055 29056 29057 29058 29059 29060 29061 29062 29063 29064 29065 29066 29067 29068 29069 29070 29071 29072 29073 29074 29075 29076 29077 29078 29079 29080 29081 29082 29083 29084 29085 29086 29087 29088 29089 29090 29091 29092 29093 29094 29095 29096 29097 29098 29099 29100 29101 29102 29103 29104 29105 29106 29107 29108 29109 29110 29111 29112 29113 29114 29115 29116 29117 29118 29119 29120 29121 29122 29123 29124 29125 29126 29127 29128 29129 29130 29131 29132 29133 29134 29135 29136 29137 29138 29139 29140 29141 29142 29143 29144 29145 29146 29147 29148 29149 29150 29151 29152 29153 29154 29155 29156 29157 29158 29159 29160 29161 29162 29163 29164 29165 29166 29167 29168 29169 29170 29171 29172 29173 29174 29175 29176 29177 29178 29179 29180 29181 29182 29183 29184 29185 29186 29187 29188 29189 29190 29191 29192 29193 29194 29195 29196 29197 29198 29199 29200 29201 29202 29203 29204 29205 29206 29207 29208 29209 29210 29211 29212 29213 29214 29215 29216 29217 29218 29219 29220 29221 29222 29223 29224 29225 29226 29227 29228 29229 29230 29231 29232 29233 29234 29235 29236 29237 29238 29239 29240 29241 29242 29243 29244 29245 29246 29247 29248 29249 29250 29251 29252 29253 29254 29255 29256 29257 29258 29259 29260 29261 29262 29263 29264 29265 29266 29267 29268 29269 29270 29271 29272 29273 29274 29275 29276 29277 29278 29279 29280 29281 29282 29283 29284 29285 29286 29287 29288 29289 29290 29291 29292 29293 29294 29295 29296 29297 29298 29299 29300 29301 29302 29303 29304 29305 29306 29307 29308 29309 29310 29311 29312 29313 29314 29315 29316 29317 29318 29319 29320 29321 29322 29323 29324 29325 29326 29327 29328 29329 29330 29331 29332 29333 29334 29335 29336 29337 29338 29339 29340 29341 29342 29343 29344 29345 29346 29347 29348 29349 29350 29351 29352 29353 29354 29355 29356 29357 29358 29359 29360 29361 29362 29363 29364 29365 29366 29367 29368 29369 29370 29371 29372 29373 29374 29375 29376 29377 29378 29379 29380 29381 29382 29383 29384 29385 29386 29387 29388 29389 29390 29391 29392 29393 29394 29395 29396 29397 29398 29399 29400 29401 29402 29403 29404 29405 29406 29407 29408 29409 29410 29411 29412 29413 29414 29415 29416 29417 29418 29419 29420 29421 29422 29423 29424 29425 29426 29427 29428 29429 29430 29431 29432 29433 29434 29435 29436 29437 29438 29439 29440 29441 29442 29443 29444 29445 29446 29447 29448 29449 29450 29451 29452 29453 29454 29455 29456 29457 29458 29459 29460 29461 29462 29463 29464 29465 29466 29467 29468 29469 29470 29471 29472 29473 29474 29475 29476 29477 29478 29479 29480 29481 29482 29483 29484 29485 29486 29487 29488 29489 29490 29491 29492 29493 29494 29495 29496 29497 29498 29499 29500 29501 29502 29503 29504 29505 29506 29507 29508 29509 29510 29511 29512 29513 29514 29515 29516 29517 29518 29519 29520 29521 29522 29523 29524 29525 29526 29527 29528 29529 29530 29531 29532 29533 29534 29535 29536 29537 29538 29539 29540 29541 29542 29543 29544 29545 29546 29547 29548 29549 29550 29551 29552 29553 29554 29555 29556 29557 29558 29559 29560 29561 29562 29563 29564 29565 29566 29567 29568 29569 29570 29571 29572 29573 29574 29575 29576 29577 29578 29579 29580 29581 29582 29583 29584 29585 29586 29587 29588 29589 29590 29591 29592 29593 29594 29595 29596 29597 29598 29599 29600 29601 29602 29603 29604 29605 29606 29607 29608 29609 29610 29611 29612 29613 29614 29615 29616 29617 29618 29619 29620 29621 29622 29623 29624 29625 29626 29627 29628 29629 29630 29631 29632 29633 29634 29635 29636 29637 29638 29639 29640 29641 29642 29643 29644 29645 29646 29647 29648 29649 29650 29651 29652 29653 29654 29655 29656 29657 29658 29659 29660 29661 29662 29663 29664 29665 29666 29667 29668 29669 29670 29671 29672 29673 29674 29675 29676 29677 29678 29679 29680 29681 29682 29683 29684 29685 29686 29687 29688 29689 29690 29691 29692 29693 29694 29695 29696 29697 29698 29699 29700 29701 29702 29703 29704 29705 29706 29707 29708 29709 29710 29711 29712 29713 29714 29715 29716 29717 29718 29719 29720 29721 29722 29723 29724 29725 29726 29727 29728 29729 29730 29731 29732 29733 29734 29735 29736 29737 29738 29739 29740 29741 29742 29743 29744 29745 29746 29747 29748 29749 29750 29751 29752 29753 29754 29755 29756 29757 29758 29759 29760 29761 29762 29763 29764 29765 29766 29767 29768 29769 29770 29771 29772 29773 29774 29775 29776 29777 29778 29779 29780 29781 29782 29783 29784 29785 29786 29787 29788 29789 29790 29791 29792 29793 29794 29795 29796 29797 29798 29799 29800 29801 29802 29803 29804 29805 29806 29807 29808 29809 29810 29811 29812 29813 29814 29815 29816 29817 29818 29819 29820 29821 29822 29823 29824 29825 29826 29827 29828 29829 29830 29831 29832 29833 29834 29835 29836 29837 29838 29839 29840 29841 29842 29843 29844 29845 29846 29847 29848 29849 29850 29851 29852 29853 29854 29855 29856 29857 29858 29859 29860 29861 29862 29863 29864 29865 29866 29867 29868 29869 29870 29871 29872 29873 29874 29875 29876 29877 29878 29879 29880 29881 29882 29883 29884 29885 29886 29887 29888 29889 29890 29891 29892 29893 29894 29895 29896 29897 29898 29899 29900 29901 29902 29903 29904 29905 29906 29907 29908 29909 29910 29911 29912 29913 29914 29915 29916 29917 29918 29919 29920 29921 29922 29923 29924 29925 29926 29927 29928 29929 29930 29931 29932 29933 29934 29935 29936 29937 29938 29939 29940 29941 29942 29943 29944 29945 29946 29947 29948 29949 29950 29951 29952 29953 29954 29955 29956 29957 29958 29959 29960 29961 29962 29963 29964 29965 29966 29967 29968 29969 29970 29971 29972 29973 29974 29975 29976 29977 29978 29979 29980 29981 29982 29983 29984 29985 29986 29987 29988 29989 29990 29991 29992 29993 29994 29995 29996 29997 29998 29999 30000 30001 30002 30003 30004 30005 30006 30007 30008 30009 30010 30011 30012 30013 30014 30015 30016 30017 30018 30019 30020 30021 30022 30023 30024 30025 30026 30027 30028 30029 30030 30031 30032 30033 30034 30035 30036 30037 30038 30039 30040 30041 30042 30043 30044 30045 30046 30047 30048 30049 30050 30051 30052 30053 30054 30055 30056 30057 30058 30059 30060 30061 30062 30063 30064 30065 30066 30067 30068 30069 30070 30071 30072 30073 30074 30075 30076 30077 30078 30079 30080 30081 30082 30083 30084 30085 30086 30087 30088 30089 30090 30091 30092 30093 30094 30095 30096 30097 30098 30099 30100 30101 30102 30103 30104 30105 30106 30107 30108 30109 30110 30111 30112 30113 30114 30115 30116 30117 30118 30119 30120 30121 30122 30123 30124 30125 30126 30127 30128 30129 30130 30131 30132 30133 30134 30135 30136 30137 30138 30139 30140 30141 30142 30143 30144 30145 30146 30147 30148 30149 30150 30151 30152 30153 30154 30155 30156 30157 30158 30159 30160 30161 30162 30163 30164 30165 30166 30167 30168 30169 30170 30171 30172 30173 30174 30175 30176 30177 30178 30179 30180 30181 30182 30183 30184 30185 30186 30187 30188 30189 30190 30191 30192 30193 30194 30195 30196 30197 30198 30199 30200 30201 30202 30203 30204 30205 30206 30207 30208 30209 30210 30211 30212 30213 30214 30215 30216 30217 30218 30219 30220 30221 30222 30223 30224 30225 30226 30227 30228 30229 30230 30231 30232 30233 30234 30235 30236 30237 30238 30239 30240 30241 30242 30243 30244 30245 30246 30247 30248 30249 30250 30251 30252 30253 30254 30255 30256 30257 30258 30259 30260 30261 30262 30263 30264 30265 30266 30267 30268 30269 30270 30271 30272 30273 30274 30275 30276 30277 30278 30279 30280 30281 30282 30283 30284 30285 30286 30287 30288 30289 30290 30291 30292 30293 30294 30295 30296 30297 30298 30299 30300 30301 30302 30303 30304 30305 30306 30307 30308 30309 30310 30311 30312 30313 30314 30315 30316 30317 30318 30319 30320 30321 30322 30323 30324 30325 30326 30327 30328 30329 30330 30331 30332 30333 30334 30335 30336 30337 30338 30339 30340 30341 30342 30343 30344 30345 30346 30347 30348 30349 30350 30351 30352 30353 30354 30355 30356 30357 30358 30359 30360 30361 30362 30363 30364 30365 30366 30367 30368 30369 30370 30371 30372 30373 30374 30375 30376 30377 30378 30379 30380 30381 30382 30383 30384 30385 30386 30387 30388 30389 30390 30391 30392 30393 30394 30395 30396 30397 30398 30399 30400 30401 30402 30403 30404 30405 30406 30407 30408 30409 30410 30411 30412 30413 30414 30415 30416 30417 30418 30419 30420 30421 30422 30423 30424 30425 30426 30427 30428 30429 30430 30431 30432 30433 30434 30435 30436 30437 30438 30439 30440 30441 30442 30443 30444 30445 30446 30447 30448 30449 30450 30451 30452 30453 30454 30455 30456 30457 30458 30459 30460 30461 30462 30463 30464 30465 30466 30467 30468 30469 30470 30471 30472 30473 30474 30475 30476 30477 30478 30479 30480 30481 30482 30483 30484 30485 30486 30487 30488 30489 30490 30491 30492 30493 30494 30495 30496 30497 30498 30499 30500 30501 30502 30503 30504 30505 30506 30507 30508 30509 30510 30511 30512 30513 30514 30515 30516 30517 30518 30519 30520 30521 30522 30523 30524 30525 30526 30527 30528 30529 30530 30531 30532 30533 30534 30535 30536 30537 30538 30539 30540 30541 30542 30543 30544 30545 30546 30547 30548 30549 30550 30551 30552 30553 30554 30555 30556 30557 30558 30559 30560 30561 30562 30563 30564 30565 30566 30567 30568 30569 30570 30571 30572 30573 30574 30575 30576 30577 30578 30579 30580 30581 30582 30583 30584 30585 30586 30587 30588 30589 30590 30591 30592 30593 30594 30595 30596 30597 30598 30599 30600 30601 30602 30603 30604 30605 30606 30607 30608 30609 30610 30611 30612 30613 30614 30615 30616 30617 30618 30619 30620 30621 30622 30623 30624 30625 30626 30627 30628 30629 30630 30631 30632 30633 30634 30635 30636 30637 30638 30639 30640 30641 30642 30643 30644 30645 30646 30647 30648 30649 30650 30651 30652 30653 30654 30655 30656 30657 30658 30659 30660 30661 30662 30663 30664 30665 30666 30667 30668 30669 30670 30671 30672 30673 30674 30675 30676 30677 30678 30679 30680 30681 30682 30683 30684 30685 30686 30687 30688 30689 30690 30691 30692 30693 30694 30695 30696 30697 30698 30699 30700 30701 30702 30703 30704 30705 30706 30707 30708 30709 30710 30711 30712 30713 30714 30715 30716 30717 30718 30719 30720 30721 30722 30723 30724 30725 30726 30727 30728 30729 30730 30731 30732 30733 30734 30735 30736 30737 30738 30739 30740 30741 30742 30743 30744 30745 30746 30747 30748 30749 30750 30751 30752 30753 30754 30755 30756 30757 30758 30759 30760 30761 30762 30763 30764 30765 30766 30767 30768 30769 30770 30771 30772 30773 30774 30775 30776 30777 30778 30779 30780 30781 30782 30783 30784 30785 30786 30787 30788 30789 30790 30791 30792 30793 30794 30795 30796 30797 30798 30799 30800 30801 30802 30803 30804 30805 30806 30807 30808 30809 30810 30811 30812 30813 30814 30815 30816 30817 30818 30819 30820 30821 30822 30823 30824 30825 30826 30827 30828 30829 30830 30831 30832 30833 30834 30835 30836 30837 30838 30839 30840 30841 30842 30843 30844 30845 30846 30847 30848 30849 30850 30851 30852 30853 30854 30855 30856 30857 30858 30859 30860 30861 30862 30863 30864 30865 30866 30867 30868 30869 30870 30871 30872 30873 30874 30875 30876 30877 30878 30879 30880 30881 30882 30883 30884 30885 30886 30887 30888 30889 30890 30891 30892 30893 30894 30895 30896 30897 30898 30899 30900 30901 30902 30903 30904 30905 30906 30907 30908 30909 30910 30911 30912 30913 30914 30915 30916 30917 30918 30919 30920 30921 30922 30923 30924 30925 30926 30927 30928 30929 30930 30931 30932 30933 30934 30935 30936 30937 30938 30939 30940 30941 30942 30943 30944 30945 30946 30947 30948 30949 30950 30951 30952 30953 30954 30955 30956 30957 30958 30959 30960 30961 30962 30963 30964 30965 30966 30967 30968 30969 30970 30971 30972 30973 30974 30975 30976 30977 30978 30979 30980 30981 30982 30983 30984 30985 30986 30987 30988 30989 30990 30991 30992 30993 30994 30995 30996 30997 30998 30999 31000 31001 31002 31003 31004 31005 31006 31007 31008 31009 31010 31011 31012 31013 31014 31015 31016 31017 31018 31019 31020 31021 31022 31023 31024 31025 31026 31027 31028 31029 31030 31031 31032 31033 31034 31035 31036 31037 31038 31039 31040 31041 31042 31043 31044 31045 31046 31047 31048 31049 31050 31051 31052 31053 31054 31055 31056 31057 31058 31059 31060 31061 31062 31063 31064 31065 31066 31067 31068 31069 31070 31071 31072 31073 31074 31075 31076 31077 31078 31079 31080 31081 31082 31083 31084 31085 31086 31087 31088 31089 31090 31091 31092 31093 31094 31095 31096 31097 31098 31099 31100 31101 31102 31103 31104 31105 31106 31107 31108 31109 31110 31111 31112 31113 31114 31115 31116 31117 31118 31119 31120 31121 31122 31123 31124 31125 31126 31127 31128 31129 31130 31131 31132 31133 31134 31135 31136 31137 31138 31139 31140 31141 31142 31143 31144 31145 31146 31147 31148 31149 31150 31151 31152 31153 31154 31155 31156 31157 31158 31159 31160 31161 31162 31163 31164 31165 31166 31167 31168 31169 31170 31171 31172 31173 31174 31175 31176 31177 31178 31179 31180 31181 31182 31183 31184 31185 31186 31187 31188 31189 31190 31191 31192 31193 31194 31195 31196 31197 31198 31199 31200 31201 31202 31203 31204 31205 31206 31207 31208 31209 31210 31211 31212 31213 31214 31215 31216 31217 31218 31219 31220 31221 31222 31223 31224 31225 31226 31227 31228 31229 31230 31231 31232 31233 31234 31235 31236 31237 31238 31239 31240 31241 31242 31243 31244 31245 31246 31247 31248 31249 31250 31251 31252 31253 31254 31255 31256 31257 31258 31259 31260 31261 31262 31263 31264 31265 31266 31267 31268 31269 31270 31271 31272 31273 31274 31275 31276 31277 31278 31279 31280 31281 31282 31283 31284 31285 31286 31287 31288 31289 31290 31291 31292 31293 31294 31295 31296 31297 31298 31299 31300 31301 31302 31303 31304 31305 31306 31307 31308 31309 31310 31311 31312 31313 31314 31315 31316 31317 31318 31319 31320 31321 31322 31323 31324 31325 31326 31327 31328 31329 31330 31331 31332 31333 31334 31335 31336 31337 31338 31339 31340 31341 31342 31343 31344 31345 31346 31347 31348 31349 31350 31351 31352 31353 31354 31355 31356 31357 31358 31359 31360 31361 31362 31363 31364 31365 31366 31367 31368 31369 31370 31371 31372 31373 31374 31375 31376 31377 31378 31379 31380 31381 31382 31383 31384 31385 31386 31387 31388 31389 31390 31391 31392 31393 31394 31395 31396 31397 31398 31399 31400 31401 31402 31403 31404 31405 31406 31407 31408 31409 31410 31411 31412 31413 31414 31415 31416 31417 31418 31419 31420 31421 31422 31423 31424 31425 31426 31427 31428 31429 31430 31431 31432 31433 31434 31435 31436 31437 31438 31439 31440 31441 31442 31443 31444 31445 31446 31447 31448 31449 31450 31451 31452 31453 31454 31455 31456 31457 31458 31459 31460 31461 31462 31463 31464 31465 31466 31467 31468 31469 31470 31471 31472 31473 31474 31475 31476 31477 31478 31479 31480 31481 31482 31483 31484 31485 31486 31487 31488 31489 31490 31491 31492 31493 31494 31495 31496 31497 31498 31499 31500 31501 31502 31503 31504 31505 31506 31507 31508 31509 31510 31511 31512 31513 31514 31515 31516 31517 31518 31519 31520 31521 31522 31523 31524 31525 31526 31527 31528 31529 31530 31531 31532 31533 31534 31535 31536 31537 31538 31539 31540 31541 31542 31543 31544 31545 31546 31547 31548 31549 31550 31551 31552 31553 31554 31555 31556 31557 31558 31559 31560 31561 31562 31563 31564 31565 31566 31567 31568 31569 31570 31571 31572 31573 31574 31575 31576 31577 31578 31579 31580 31581 31582 31583 31584 31585 31586 31587 31588 31589 31590 31591 31592 31593 31594 31595 31596 31597 31598 31599 31600 31601 31602 31603 31604 31605 31606 31607 31608 31609 31610 31611 31612 31613 31614 31615 31616 31617 31618 31619 31620 31621 31622 31623 31624 31625 31626 31627 31628 31629 31630 31631 31632 31633 31634 31635 31636 31637 31638 31639 31640 31641 31642 31643 31644 31645 31646 31647 31648 31649 31650 31651 31652 31653 31654 31655 31656 31657 31658 31659 31660 31661 31662 31663 31664 31665 31666 31667 31668 31669 31670 31671 31672 31673 31674 31675 31676 31677 31678 31679 31680 31681 31682 31683 31684 31685 31686 31687 31688 31689 31690 31691 31692 31693 31694 31695 31696 31697 31698 31699 31700 31701 31702 31703 31704 31705 31706 31707 31708 31709 31710 31711 31712 31713 31714 31715 31716 31717 31718 31719 31720 31721 31722 31723 31724 31725 31726 31727 31728 31729 31730 31731 31732 31733 31734 31735 31736 31737 31738 31739 31740 31741 31742 31743 31744 31745 31746 31747 31748 31749 31750 31751 31752 31753 31754 31755 31756 31757 31758 31759 31760 31761 31762 31763 31764 31765 31766 31767 31768 31769 31770 31771 31772 31773 31774 31775 31776 31777 31778 31779 31780 31781 31782 31783 31784 31785 31786 31787 31788 31789 31790 31791 31792 31793 31794 31795 31796 31797 31798 31799 31800 31801 31802 31803 31804 31805 31806 31807 31808 31809 31810 31811 31812 31813 31814 31815 31816 31817 31818 31819 31820 31821 31822 31823 31824 31825 31826 31827 31828 31829 31830 31831 31832 31833 31834 31835 31836 31837 31838 31839 31840 31841 31842 31843 31844 31845 31846 31847 31848 31849 31850 31851 31852 31853 31854 31855 31856 31857 31858 31859 31860 31861 31862 31863 31864 31865 31866 31867 31868 31869 31870 31871 31872 31873 31874 31875 31876 31877 31878 31879 31880 31881 31882 31883 31884 31885 31886 31887 31888 31889 31890 31891 31892 31893 31894 31895 31896 31897 31898 31899 31900 31901 31902 31903 31904 31905 31906 31907 31908 31909 31910 31911 31912 31913 31914 31915 31916 31917 31918 31919 31920 31921 31922 31923 31924 31925 31926 31927 31928 31929 31930 31931 31932 31933 31934 31935 31936 31937 31938 31939 31940 31941 31942 31943 31944 31945 31946 31947 31948 31949 31950 31951 31952 31953 31954 31955 31956 31957 31958 31959 31960 31961 31962 31963 31964 31965 31966 31967 31968 31969 31970 31971 31972 31973 31974 31975 31976 31977 31978 31979 31980 31981 31982 31983 31984 31985 31986 31987 31988 31989 31990 31991 31992 31993 31994 31995 31996 31997 31998 31999 32000 32001 32002 32003 32004 32005 32006 32007 32008 32009 32010 32011 32012 32013 32014 32015 32016 32017 32018 32019 32020 32021 32022 32023 32024 32025 32026 32027 32028 32029 32030 32031 32032 32033 32034 32035 32036 32037 32038 32039 32040 32041 32042 32043 32044 32045 32046 32047 32048 32049 32050 32051 32052 32053 32054 32055 32056 32057 32058 32059 32060 32061 32062 32063 32064 32065 32066 32067 32068 32069 32070 32071 32072 32073 32074 32075 32076 32077 32078 32079 32080 32081 32082 32083 32084 32085 32086 32087 32088 32089 32090 32091 32092 32093 32094 32095 32096 32097 32098 32099 32100 32101 32102 32103 32104 32105 32106 32107 32108 32109 32110 32111 32112 32113 32114 32115 32116 32117 32118 32119 32120 32121
|
/**********************************************************************
* cbf_hdf5 -- read and write HDF5/NeXus files *
* *
* Version 0.9.5 27 April 2014 *
* *
* Paul Ellis and *
* Herbert J. Bernstein (yaya@bernstein-plus-sons.com) *
* *
* (C) Copyright 2009, 2012, 2013, 2014 Herbert J. Bernstein *
* *
**********************************************************************/
/**********************************************************************
* *
* YOU MAY REDISTRIBUTE THE CBFLIB PACKAGE UNDER THE TERMS OF THE GPL *
* *
* ALTERNATIVELY YOU MAY REDISTRIBUTE THE CBFLIB API UNDER THE TERMS *
* OF THE LGPL *
* *
**********************************************************************/
/*************************** GPL NOTICES ******************************
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* (the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *
* 02111-1307 USA *
* *
**********************************************************************/
/************************* LGPL NOTICES *******************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301 USA *
* *
**********************************************************************/
/**********************************************************************
* *
* Stanford University Notices *
* for the CBFlib software package that incorporates SLAC software *
* on which copyright is disclaimed *
* *
* This software *
* ------------- *
* The term "this software", as used in these Notices, refers to *
* those portions of the software package CBFlib that were created by *
* employees of the Stanford Linear Accelerator Center, Stanford *
* University. *
* *
* Stanford disclaimer of copyright *
* -------------------------------- *
* Stanford University, owner of the copyright, hereby disclaims its *
* copyright and all other rights in this software. Hence, anyone *
* may freely use it for any purpose without restriction. *
* *
* Acknowledgement of sponsorship *
* ------------------------------ *
* This software was produced by the Stanford Linear Accelerator *
* Center, Stanford University, under Contract DE-AC03-76SFO0515 with *
* the Department of Energy. *
* *
* Government disclaimer of liability *
* ---------------------------------- *
* Neither the United States nor the United States Department of *
* Energy, nor any of their employees, makes any warranty, express or *
* implied, or assumes any legal liability or responsibility for the *
* accuracy, completeness, or usefulness of any data, apparatus, *
* product, or process disclosed, or represents that its use would *
* not infringe privately owned rights. *
* *
* Stanford disclaimer of liability *
* -------------------------------- *
* Stanford University makes no representations or warranties, *
* express or implied, nor assumes any liability for the use of this *
* software. *
* *
* Maintenance of notices *
* ---------------------- *
* In the interest of clarity regarding the origin and status of this *
* software, this and all the preceding Stanford University notices *
* are to remain affixed to any copy or derivative of this software *
* made or distributed by the recipient and are to be affixed to any *
* copy of software made or distributed by the recipient that *
* contains a copy or derivative of this software. *
* *
* Based on SLAC Software Notices, Set 4 *
* OTT.002a, 2004 FEB 03 *
**********************************************************************/
/**********************************************************************
* NOTICE *
* Creative endeavors depend on the lively exchange of ideas. There *
* are laws and customs which establish rights and responsibilities *
* for authors and the users of what authors create. This notice *
* is not intended to prevent you from using the software and *
* documents in this package, but to ensure that there are no *
* misunderstandings about terms and conditions of such use. *
* *
* Please read the following notice carefully. If you do not *
* understand any portion of this notice, please seek appropriate *
* professional legal advice before making use of the software and *
* documents included in this software package. In addition to *
* whatever other steps you may be obliged to take to respect the *
* intellectual property rights of the various parties involved, if *
* you do make use of the software and documents in this package, *
* please give credit where credit is due by citing this package, *
* its authors and the URL or other source from which you obtained *
* it, or equivalent primary references in the literature with the *
* same authors. *
* *
* Some of the software and documents included within this software *
* package are the intellectual property of various parties, and *
* placement in this package does not in any way imply that any *
* such rights have in any way been waived or diminished. *
* *
* With respect to any software or documents for which a copyright *
* exists, ALL RIGHTS ARE RESERVED TO THE OWNERS OF SUCH COPYRIGHT. *
* *
* Even though the authors of the various documents and software *
* found here have made a good faith effort to ensure that the *
* documents are correct and that the software performs according *
* to its documentation, and we would greatly appreciate hearing of *
* any problems you may encounter, the programs and documents any *
* files created by the programs are provided **AS IS** without any *
* warranty as to correctness, merchantability or fitness for any *
* particular or general use. *
* *
* THE RESPONSIBILITY FOR ANY ADVERSE CONSEQUENCES FROM THE USE OF *
* PROGRAMS OR DOCUMENTS OR ANY FILE OR FILES CREATED BY USE OF THE *
* PROGRAMS OR DOCUMENTS LIES SOLELY WITH THE USERS OF THE PROGRAMS *
* OR DOCUMENTS OR FILE OR FILES AND NOT WITH AUTHORS OF THE *
* PROGRAMS OR DOCUMENTS. *
**********************************************************************/
/**********************************************************************
* *
* The IUCr Policy *
* for the Protection and the Promotion of the STAR File and *
* CIF Standards for Exchanging and Archiving Electronic Data *
* *
* Overview *
* *
* The Crystallographic Information File (CIF)[1] is a standard for *
* information interchange promulgated by the International Union of *
* Crystallography (IUCr). CIF (Hall, Allen & Brown, 1991) is the *
* recommended method for submitting publications to Acta *
* Crystallographica Section C and reports of crystal structure *
* determinations to other sections of Acta Crystallographica *
* and many other journals. The syntax of a CIF is a subset of the *
* more general STAR File[2] format. The CIF and STAR File approaches *
* are used increasingly in the structural sciences for data exchange *
* and archiving, and are having a significant influence on these *
* activities in other fields. *
* *
* Statement of intent *
* *
* The IUCr's interest in the STAR File is as a general data *
* interchange standard for science, and its interest in the CIF, *
* a conformant derivative of the STAR File, is as a concise data *
* exchange and archival standard for crystallography and structural *
* science. *
* *
* Protection of the standards *
* *
* To protect the STAR File and the CIF as standards for *
* interchanging and archiving electronic data, the IUCr, on behalf *
* of the scientific community, *
* *
* * holds the copyrights on the standards themselves, *
* *
* * owns the associated trademarks and service marks, and *
* *
* * holds a patent on the STAR File. *
* *
* These intellectual property rights relate solely to the *
* interchange formats, not to the data contained therein, nor to *
* the software used in the generation, access or manipulation of *
* the data. *
* *
* Promotion of the standards *
* *
* The sole requirement that the IUCr, in its protective role, *
* imposes on software purporting to process STAR File or CIF data *
* is that the following conditions be met prior to sale or *
* distribution. *
* *
* * Software claiming to read files written to either the STAR *
* File or the CIF standard must be able to extract the pertinent *
* data from a file conformant to the STAR File syntax, or the CIF *
* syntax, respectively. *
* *
* * Software claiming to write files in either the STAR File, or *
* the CIF, standard must produce files that are conformant to the *
* STAR File syntax, or the CIF syntax, respectively. *
* *
* * Software claiming to read definitions from a specific data *
* dictionary approved by the IUCr must be able to extract any *
* pertinent definition which is conformant to the dictionary *
* definition language (DDL)[3] associated with that dictionary. *
* *
* The IUCr, through its Committee on CIF Standards, will assist *
* any developer to verify that software meets these conformance *
* conditions. *
* *
* Glossary of terms *
* *
* [1] CIF: is a data file conformant to the file syntax defined *
* at http://www.iucr.org/iucr-top/cif/spec/index.html *
* *
* [2] STAR File: is a data file conformant to the file syntax *
* defined at http://www.iucr.org/iucr-top/cif/spec/star/index.html *
* *
* [3] DDL: is a language used in a data dictionary to define data *
* items in terms of "attributes". Dictionaries currently approved *
* by the IUCr, and the DDL versions used to construct these *
* dictionaries, are listed at *
* http://www.iucr.org/iucr-top/cif/spec/ddl/index.html *
* *
* Last modified: 30 September 2000 *
* *
* IUCr Policy Copyright (C) 2000 International Union of *
* Crystallography *
**********************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
#include "cbf.h"
#include "cbf_tree.h"
#include "cbf_hdf5.h"
#include "cbf_ascii.h"
#include "cbf_binary.h"
#include "cbf_compress.h"
#include "cbf_file.h"
#include "cbf_write.h"
#include "cbf_write_binary.h"
#include "cbf_read_mime.h"
#include "cbf_string.h"
#include "cbf_codes.h"
#include "cbf_alloc.h"
#include "cbf_simple.h"
#include "cbf_tree.h"
#include "cbf_hdf5_filter.h"
#ifdef CBF_USE_ULP
#include "cbf_ulp.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <errno.h>
#if !defined(CBF_NO_REGEX)
#ifdef CBF_REGEXLIB_REGEX
#include <regex.h>
#else
#include <pcreposix.h>
#endif
#endif
static int cbf_write_nx2cbf__cbfdb_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data);
static int cbf_find_array_data_h5type
(hid_t * const type,
unsigned int bits,
int sign,
int real,
const char *byteorder);
/* CBF column -- NeXus DataSet Mapping Tables
applicable to categories that map to
datasets or to attributes
In each of the names the constructs
$(cifcolumn)
$(cifcolumn,rownum)
$(_category.cifcolumn,rownum)
will be replaced by the value of the specified
CIF tag at the current row, or at the specified
row. Failure to find the designated item, will
cause a fail-through in $or to the next argument.
The construct $axis(value) is replaced by x, y or z
for values of 1, 2 and 3, respectively, after
evaluating value.
The construct $matchrow(tag,value) is replaced by the row number
in the category for the tag that matches the value given
The construct $matchrowmax(tag,value,controlcolumn) is replaced by the row number
in the category for the tag that matches the value given, subject to maximizing
the value of the control column in the category of tag.
The construct $matchcase(tag1,value1,expression1,tag2,value2,expression2,...,expressionn)
The contruct $or(arg1,arg2,arg3) is replaced by the first
of the arguments that can be successfully evaluated
The construct $equipment(value) is replaced by
CBF_diffrn_detector__$(_diffrn_detector.id,0):NXdetector
CBF_diffrn_measurement__$(_diffrn_measurement.diffrn_id,0):NXsample
coordinate_system:NXcoordinate_system
and $equipment_class(value) is replaced by
NXdetector
NXsample
Nxcoordinate_system
for the values "detector", "goniometer", or "general"
respectively
*/
/* The top of the target tree is
/CBF_diffrn_scan__SCANID:NXentry
/CBF_scan_id="SCANID"
/CBF_diffrn_id="DIFFRNID"
/CBF_entry_id="ENTRYID"
/instrument:NXinstrument
*/
typedef struct {
int objtype; /* CBF_H5_COLUMN_GROUP
or CBF_H5_COLUMN_DATASET
possibly ored with CBF_H5_TERMINATE
*/
const char * h5pathelement; /* An hdf5 path element
and optional class*/
} cbf_h5path_element;
typedef struct {
int dsorat; /* CBF_H5_COLUMN_DATASET
or CBF_H5_COLUMN_ATTRIBUTE */
int parent_index; /* index into the cbfcol_h5parent table
If the parent_index is -1, the new
items goes in at the same level
as the first item in the cbfcol_h5parent
as a sibling.
*/
char * cifcolumn; /* the name of the cif column to be mapped */
char * mappedname; /* the hdf5 target name or NULL to use parent */
char * units; /* units or NULL */
int numeric; /* CBF_H5_TEXT, CBF_H5_FLOAT, CBF_H5_INTEGER */
int special; /* 0 for normal handling, 1 for special case,
add CBF_H5_TERMINATE for terminal */
} cbf_colnxdsat;
typedef struct {
char * cifcategory; /* the name of the cf category to be mapped */
cbf_h5path_element * h5path;
/* the array of nexus path elements used to map
this category */
cbf_colnxdsat * nxmapping;
/* the array of mappings of columns into datasets
and attributes */
} cbf_catnxmapping;
/* column mappings for ARRAY_DATA */
cbf_h5path_element cbf_nxmapping_array_data_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE, "data__$(array_id)_$(binary_id)"}
};
cbf_colnxdsat cbf_nxmapping_array_data[] = {
{CBF_H5_COLUMN_ATTRIBUTE, 3, "id", "CBF_array_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "binary_id", "CBF_binary_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "data", NULL,NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "header_contents", "CBF_header_contents",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "header_convention", "CBF_header_convention",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,CBF_H5_TERMINATE}
};
/* column mappings for ARRAY_ELEMENT_SIZE */
cbf_h5path_element cbf_nxmapping_array_element_size_data_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE,
"$axis($(index))_pixel_size_$(array_id):m"}
};
cbf_colnxdsat cbf_nxmapping_array_element_size[] = {
{CBF_H5_COLUMN_ATTRIBUTE, 3, "id", "CBF_array_id",NULL,0,0},
{CBF_H5_COLUMN_DATASET, 2, "size", "$axis($(index))_pixel_size_$(array_id)","m",CBF_H5_FLOAT,CBF_H5_TERMINATE}
};
/* column mappings for ARRAY_INTENSITIES */
cbf_h5path_element cbf_nxmapping_array_intensities_data_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE, "data__$(array_id)_$(binary_id)"}
};
cbf_colnxdsat cbf_nxmapping_array_intensities[] = {
{CBF_H5_COLUMN_ATTRIBUTE, 3, "array_id", "CBF_array_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "binary_id", "CBF_binary_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "details", "CBF_array_intensities__details",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "gain", "CBF_array_intensities__gain",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "gain_esd", "CBF_array_intensities__gain_esd",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "linearity", "CBF_array_intensities__linearity",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "offset", "CBF_array_intensities__offset",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "scaling", "CBF_array_intensities__scaling",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "overload", "CBF_array_intensities__overload",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "undefined_value", "CBF_array_intensities__undefined_value",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "pixel_fast_bin_size", "CBF_array_intensities__pixel_fast_bin_size",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "pixel_slow_bin_size", "CBF_array_intensities__pixel_slow_bin_size",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "pixel_binning_method",
"CBF_array_intensities__pixel_binning_method",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,CBF_H5_TERMINATE}
};
/* column mappings for ARRAY_STRUCTURE_LIST */
cbf_h5path_element cbf_nxmapping_array_structure_list_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE, "CBF_array_structure_list__$(axis_set_id)"}
};
cbf_colnxdsat cbf_nxmapping_array_structure_list[] = {
{CBF_H5_COLUMN_ATTRIBUTE, 3, "axis_set_id", "CBF_array_structure_list__axis_set_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "array_id", "CBF_array_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "dimension", "CBF_array_structure_list__dimension",NULL,CBF_H5_INTEGER|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "direction", "CBF_array_structure_list__direction",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "index", "CBF_array_structure_list__index",NULL,CBF_H5_INTEGER|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "precedence", "CBF_array_structure_list__precedence",NULL,CBF_H5_INTEGER|CBF_H5_ARRAY,CBF_H5_TERMINATE}
};
/* column mappings for ARRAY_STRUCTURE_LIST_SECTION */
cbf_h5path_element cbf_nxmapping_array_structure_list_section_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE, "CBF_array_structure_list_section__$(id)"}
};
cbf_colnxdsat cbf_nxmapping_array_structure_list_section[] = {
{CBF_H5_COLUMN_ATTRIBUTE, 3, "array_id", "CBF_array_id",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "id", "CBF_section_id",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "index", "CBF_array_structure_list_section__index",NULL,CBF_H5_INTEGER,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "end", "CBF_array_structure_list_section__end",NULL,CBF_H5_INTEGER,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "start", "CBF_array_structure_list_section__start",NULL,CBF_H5_INTEGER,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "stride", "CBF_array_structure_list_section__stride",NULL,CBF_H5_INTEGER,CBF_H5_TERMINATE}
};
/* column mappings for ARRAY_STRUCTURE_LIST_AXIS */
cbf_h5path_element cbf_nxmapping_array_structure_list_axis_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE, "$(axis_id)"}
};
cbf_colnxdsat cbf_nxmapping_array_structure_list_axis[] = {
{CBF_H5_COLUMN_ATTRIBUTE, 3, "axis_id", "CBF_array_structure_list_axis__axis_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "axis_set_id", "CBF_array_structure_list_axis__axis_set_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "angle", "CBF_array_structure_list_axis__angle",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "angle_increment", "CBF_array_structure_list_axis__angle_FLOAT|CBF_H5_ARRAY",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "displacement", "CBF_array_structure_list_axis__displacement",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "fract_displacement", "CBF_array_structure_list_axis__fract_displacement",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "displacement_increment", "CBF_array_structure_list_axis__displacement_increment",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "fract_displacement_increment", "CBF_array_structure_list_axis__fract_displacement_increment",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "angular_pitch", "CBF_array_structure_list_axis__angular_pitch",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "radial_pitch", "CBF_array_structure_list_axis__radial_pitch",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "reference_angle", "CBF_array_structure_list_axis__reference_angle",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "reference_displacement", "CBF_array_structure_list_axis__reference_displacement",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,CBF_H5_TERMINATE}
};
/* column mappings for AXIS */
cbf_h5path_element cbf_nxmapping_axis_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP,
"$ifeq($(_axis.equipment,$match(_axis.id,$(axis_id))),detector,$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdectector,"
"$(_axis.equipment,$match(_axis.id,$(axis_id))),goniometer,$or(CBF_diffrn_measurement__$(_diffrn_measurement.id,0),goniometer):NXsample,"
"detector:NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE, "$(id)"}
};
cbf_colnxdsat cbf_nxmapping_axis[] = {
{CBF_H5_COLUMN_ATTRIBUTE, 3, "depends_on", "depends_on",NULL,CBF_H5_TEXT,CBF_H5_COLUMN_PATH},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "offset[?]", "offset","mm",CBF_H5_FLOAT,CBF_H5_COLUMN_MAP_AXES},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "type", "transformation_type",NULL,CBF_H5_TEXT,CBF_H5_COLUMN_MAP_AXES},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "system", "system",NULL,CBF_H5_TEXT,CBF_H5_COLUMN_MAP_AXES},
{CBF_H5_COLUMN_ATTRIBUTE, 3, "vector[?]", "vector",NULL,CBF_H5_FLOAT,CBF_H5_COLUMN_MAP_AXES|CBF_H5_TERMINATE},
};
/* column mappings for DIFFRN_DATA_FRAME */
cbf_h5path_element cbf_nxmapping_diffrn_data_frame_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP|CBF_H5_TERMINATE, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"}
};
cbf_colnxdsat cbf_nxmapping_diffrn_data_frame[] = {
{CBF_H5_COLUMN_DATASET, 2, "array_section_id", "CBF_diffrn_data_frame__section_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "array_id", "CBF_diffrn_data_frame__section_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "binary_id", "CBF_diffrn_data_frame__binary_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "center_fast", "CBF_diffrn_data_frame__center_fast_slow["
"$(_diffrn_scan_frame.frame_number,$matchrow(_diffrn_scan_frame.frame_id,_diffrn_data_frame.id),0,"
"$matchrow(diffrn_detector_element.id,_diffrn_data_frame.detector_element_id)],$(center_units)",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "center_slow", "CBF_diffrn_data_frame__center_fast_slow["
"$(_diffrn_scan_frame.frame_number,$matchrow(_diffrn_scan_frame.frame_id,$(_diffrn_data_frame.id)),1,"
"$matchrow(diffrn_detector_element.id,_diffrn_data_frame.detector_element_id)],$(center_units)",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "details", "CBF_diffrn_data_frame__details["
"$(_diffrn_scan_frame.frame_number,$matchrow(_diffrn_scan_frame.frame_id,_diffrn_data_frame.id),"
"$matchrow(diffrn_detector_element.id,_diffrn_data_frame.detector_element_id)]",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,CBF_H5_TERMINATE}
};
/* column mappings for DIFFRN_DETECTOR */
cbf_h5path_element cbf_nxmapping_diffrn_detector_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE, "CBF_diffrn_detector__id"}
};
cbf_colnxdsat cbf_nxmapping_diffrn_detector[] = {
{CBF_H5_COLUMN_DATASET, 0, "diffrn_id", "CBF_diffrn_id",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "id", "CBF_diffrn_id",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "details", "details",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "detector", "type",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "dtime", "deadtime",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "number_of_axes", "number_of_axes",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "type", "instrument",NULL,CBF_H5_TEXT,CBF_H5_TERMINATE},
};
/* column mappings for DIFFRN_DETECTOR_AXIS */
cbf_h5path_element cbf_nxmapping_diffrn_detector_axis_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"},
{CBF_H5_COLUMN_DATASET|CBF_H5_TERMINATE, "$(axis_id)"}
};
cbf_colnxdsat * cbf_nxmapping_diffrn_detector_axis = NULL;
/* column mappings for DIFFRN_DETECTOR_ELEMENT */
cbf_h5path_element cbf_nxmapping_diffrn_detector_element_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP|CBF_H5_TERMINATE, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"}
};
cbf_colnxdsat cbf_nxmapping_diffrn_detector_element[] = {
{CBF_H5_COLUMN_DATASET, 2, "id",
"CBF_diffrn_detector_element__id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "detector_id",
"CBF_diffrn_detector_element__detector_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "reference_center_fast",
"CBF_diffrn_detector_element__reference_center_fast","$(reference_center_units)",CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "reference_center_slow",
"CBF_diffrn_detector_element__reference_center_slow","$(reference_center_units)",CBF_H5_FLOAT|CBF_H5_ARRAY,CBF_H5_TERMINATE},
};
/* column mappings for DIFFRN_MEASUREMENT */
cbf_h5path_element cbf_nxmapping_diffrn_measurement_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP|CBF_H5_TERMINATE, "instrument:NXinstrument"}
};
cbf_colnxdsat cbf_nxmapping_diffrn_measurement[] = {
{CBF_H5_COLUMN_DATASET, 0, "diffrn_id",
"CBF_diffrn_id",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 1, "details",
"CBF_diffrn_measurement__details",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 1, "device",
"CBF_diffrn_measurement__device",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 1, "device_details",
"CBF_diffrn_measurement__device_details",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 1, "device_type",
"CBF_diffrn_measurement__device_type",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 1, "id",
"CBF_diffrn_measurement__id",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 1, "method",
"CBF_diffrn_measurement__method",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 1, "number_of_axes",
"CBF_diffrn_measurement__number_of_axes",NULL,CBF_H5_INTEGER,0},
{CBF_H5_COLUMN_DATASET, 1, "sample_detector_distance",
"distance","mm",CBF_H5_FLOAT,CBF_H5_LINK},
{CBF_H5_LINK, 1, "sample_detector_distance",
"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id):NXentry"
"/instrument:NXinstrument"
"/$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector",
NULL,0,CBF_H5_LINK},
{CBF_H5_COLUMN_DATASET, 1, "sample_detector_voffset",
"CBF_diffrn_measurement__sample_detector_voffset","mm",CBF_H5_FLOAT,CBF_H5_SPECIAL},
{CBF_H5_COLUMN_DATASET, 1, "specimen_support",
"CBF_diffrn_measurement__specimen_support",NULL,CBF_H5_TEXT,0}
};
/* column mappings for DIFFRN_MEASUREMENT_AXIS */
cbf_h5path_element cbf_nxmapping_diffrn_measurement_axis_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP,"instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP|CBF_H5_TERMINATE,
"CBF_diffrn_measurement__$(measurement_id):NXsample"}
};
cbf_colnxdsat cbf_nxmapping_diffrn_measurement_axis[] = {
{CBF_H5_COLUMN_DATASET, 1, "axis_id",
"CBF_axis__axis_id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 1, "measurement_device",
"CBF_axis__measurement_device",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 1, "measurement_device",
"CBF_axis__measurement_device",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,CBF_H5_TERMINATE},
};
/* column mappings for DIFFRN_RADIATION */
cbf_h5path_element cbf_nxmapping_diffrn_radiation_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP|CBF_H5_TERMINATE, "collimator:NXcollimator"}
};
cbf_colnxdsat cbf_nxmapping_diffrn_radiation[] = {
{CBF_H5_COLUMN_DATASET, 2, "collimation",
"CBF_diffrn_radiation__collimation",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 0, "diffrn_id",
"CBF_diffrn_id",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "div_x_source",
"divergence_x","degrees",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "div_y_source",
"divergence_y","degrees",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "div_x_y_source",
"CBF_diffrn_radiation__div_x_y_source","deg^2",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "filter_edge",
"CBF_diffrn_radiation__filter_edge","angstroms",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "inhomogeneity",
"CBF_diffrn_radiation__inhomogeneity","mm",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "monochromator",
"CBF_diffrn_radiation__monochromator",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "polarisn_norm",
"CBF_diffrn_radiation__polarisn_norm","degrees",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "polarisn_ratio",
"CBF_diffrn_radiation__polarisn_ratio",NULL,CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "polarizn_source_norm",
"CBF_diffrn_radiation__polarizn_source_norm","degrees",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "polarizn_source_ratio",
"CBF_diffrn_radiation__polarizn_source_ratio",NULL,CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 2, "probe",
"CBF_diffrn_radiation__probe",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "type",
"CBF_diffrn_radiation__type",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "xray_symbol",
"CBF_diffrn_radiation__xray_symbol",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 2, "wavelength_id",
"CBF_diffrn_radiation__wavelength_id",NULL,CBF_H5_TEXT,CBF_H5_MORE},
{CBF_H5_COLUMN_DATASET, 2, "$(_diffrn_radiation_wavelength.wavelength,$matchrowmax(_diffrn_radiation_wavelength.id,wavelength_id,_diffrn_radiation_wavelength.wt)","wavelength","angstroms",CBF_H5_FLOAT,0},
};
/* column mappings for DIFFRN_RADIATION_WAVELENGTH */
cbf_h5path_element cbf_nxmapping_diffrn_radiation_wavelength_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP|CBF_H5_TERMINATE, "collimator:NXcollimator"}
};
cbf_colnxdsat cbf_nxmapping_diffrn_radiation_wavelength[] = {
{CBF_H5_COLUMN_DATASET, 2, "id",
"CBF_diffrn_radiation_wavelength__id",NULL,CBF_H5_TEXT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "wavelength",
"CBF_diffrn_radiation_wavelength__wavelength","angstroms",CBF_H5_FLOAT|CBF_H5_ARRAY,0},
{CBF_H5_COLUMN_DATASET, 2, "wt",
"CBF_diffrn_radiation_wavelength__wt",NULL,CBF_H5_FLOAT|CBF_H5_ARRAY,0}
};
/* column mappings for DIFFRN_SCAN */
cbf_h5path_element cbf_nxmapping_diffrn_scan_path[] = {
{CBF_H5_COLUMN_GROUP,"$or(CBF_diffrn_scan__$(_diffrn_scan.id),"
"CBF_diffrn__$(_diffrn.id),"
"CBF_entry__$(_entry.id),"
"entry):NXentry"},
{CBF_H5_COLUMN_GROUP, "instrument:NXinstrument"},
{CBF_H5_COLUMN_GROUP|CBF_H5_TERMINATE, "$or(CBF_diffrn_detector__$(_diffrn_detector.id,0),detector):NXdetector"}
};
cbf_colnxdsat cbf_nxmapping_diffrn_scan[] = {
{CBF_H5_COLUMN_DATASET, 0, "$(id)", "CBF_scan_id",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 0, "$(date_end)", "end_time",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 0, "$(date_start)", "start_time",NULL,CBF_H5_TEXT,0},
{CBF_H5_COLUMN_DATASET, 0, "$(integration_time)", "average_count_time","sec",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 0, "$(_diffrn_scan_frame.frame_number,$matchrow(_diffrn_scan_frame.frame_id,$(frame_id_start))", "frame_start_number",NULL,CBF_H5_INTEGER,0},
{CBF_H5_COLUMN_DATASET, 0, "$(_diffrn_scan_frame.frame_number,$matchrow(_diffrn_scan_frame.frame_id,$(frame_id_end))", "frame_end_number",NULL,CBF_H5_INTEGER,0},
{CBF_H5_COLUMN_DATASET, 0, "$(frames)", "CBF_diffrn_scan__frames",NULL,CBF_H5_INTEGER,0},
{CBF_H5_COLUMN_DATASET, 0, "$(time_period)", "frame_time","sec",CBF_H5_FLOAT,0},
{CBF_H5_COLUMN_DATASET, 0, "$(time_rstrt_inc$", "frame_restart_time","sec",CBF_H5_FLOAT,0}
};
/* Macros to get the current location in a file in form `file:line' */
#define __STR2(n) #n
#define __STR(n) __STR2(n)
/*
Call a function if 'error' is 'CBF_SUCCESS', always printing an error message if it fails.
Require a terminating semi-colon to make it behave as a statement.
*/
#define CBF_CALL(exp) \
CBFM_PROLOG { \
if (CBF_SUCCESS==error) { \
if (CBF_SUCCESS!=(error|=(exp))) { \
cbf_debug_print(cbf_strerror(error)) \
} \
} \
} CBFM_EPILOG
#define CBF_CALL2(exp,error) \
CBFM_PROLOG { \
if (CBF_SUCCESS==error) { \
if (CBF_SUCCESS!=(error|=(exp))) { \
cbf_debug_print(cbf_strerror(error)) \
} \
} \
} CBFM_EPILOG
/*
Call an HDF5 function unconditionally, reporting any errors.
Only use this if 'exp' should return negative values on failure.
This should only be defined within the cbf_H5* functions,
as it isn't helpful everywhere.
*/
#define CBF_H5CALL(exp) \
CBFM_PROLOG { \
const int err = (exp); \
if (err < 0) { \
error |= CBF_H5ERROR; \
cbf_debug_print2("%s", "'" #exp "' failed"); \
} \
} CBFM_EPILOG
/* The strcpy function stresses valgrind, This is a replacement */
static char * _cbf_strcpy(char * dst, const char * src)
{
char *s;
char *d;
if (src) {
s = (char *)src;
d = dst;
while (*s) {*(d++)= *(s++);}
*d = '\0';
return dst;
} else {
if (dst) *dst = '\0';
return dst;
}
}
static char * _cbf_strncpy(char * dst, const char * src, size_t n)
{
ssize_t i;
char *s;
char *d;
if (src) {
s = (char *)src;
d = dst;
for (i = 0; i < (ssize_t)n; i++) if (*s) {*(d++)= *(s++);} else {*(d++) = '\0';}
return dst;
} else {
for (i = 0; i < (ssize_t)n; i++) dst[i] = '\0';
return dst;
}
}
static size_t _cbf_strlen(const char * src)
{
size_t n;
char * s;
n = 0;
if (src) {
s = (char *)src;
while (*(s++)) {n++;}
}
return n;
}
/*
The 'strdup' function isn't available when compiling with -ansi on GCC, so provide an alternative.
*/
static char * _cbf_strdup(const char *s)
{
char * t;
ssize_t n;
if (s) {
n = _cbf_strlen(s);
t = _cbf_strcpy((char *)malloc(sizeof(char)*(n+2)),(char *)s);
} else {
n = -1;
t = (char *)malloc(sizeof(char));
}
t[n+1] = '\0';
return t;
}
/**
Concatenate several null-terminated strings into a single string, with each component
separated by one 'sep' character. A leading or trailing empty string will cause a
leading or trailing 'sep' character to be written, the character may be repeated by
inserting an empty string into the 'parts' array at the appropriate place. The 'parts'
array must be terminated by a null pointer, not a pointer to an empty string.
Examples:
_cbf_str_join({0},c);
→ ""
_cbf_str_join({"string",0},c);
→ "string"
_cbf_str_join({"","",0},'#');
→ "#"
_cbf_str_join({"multi","part","string",0},'-');
→ "multi-part-string"
_cbf_str_join({"A","string","","with","more","separators",0},'_');
→ "A_string__with_more_separators"
_cbf_str_join({"","leading",0},'_');
→ "_leading"
_cbf_str_join({"trailing","",0},'_');
→ "trailing_"
Returns a string which must be free'd on success, NULL on failure.
Creating an empty string is not failure to create a string.
*/
char * _cbf_str_join(const char * const * const parts, const char sep)
{
/* define local variables & check the argument */
size_t len = 0, n = 0;
const char * const * p = parts;
if (!parts) return NULL;
/* get the combined length of all component strings, and the number of them */
while (*p) {
len += _cbf_strlen(*(p++));
++n;
}
if (0 == n) {
char * const str = malloc(sizeof(char)+1);
if (!str) return NULL;
return _cbf_strcpy(str,"");
} else {
/*
Allocate a new string with enough room for every component of
the path, N-1 1-character path seperators & a null-terminator.
*/
char * const str = malloc((1+len+n)*sizeof(char));
char * c = str;
if (!str) return NULL;
n = 1;
for (p = parts; *p; ) {
/* cbf_debug_print3("_cbf_str_join arg %d '%s'",n,*p); */
c = _cbf_strcpy(c,*p) + _cbf_strlen(*p);
if (!*(++p)) break;
*c++ = sep;
n++;
}
str[len+n] = '\0';
return str;
}
}
/****************************************************************
Store & manuipluate a set of keys identifying a set of data of interest.
****************************************************************/
/**
Map an axis set ID to a direction, for use in extracting axis data for images.
*/
typedef struct cbf_arrayAxisSet_t {
unsigned int count;
const char * * axis_set_id;
const char * * path;
unsigned int * index;
unsigned int * precedence;
unsigned int * dimension;
const char * * direction;
} cbf_arrayAxisSet_t;
static cbf_arrayAxisSet_t _cbf_make_arrayAxisSet( void )
{
cbf_arrayAxisSet_t obj;
obj.count = 0;
obj.axis_set_id = NULL;
obj.path = NULL;
obj.index = NULL;
obj.precedence = NULL;
obj.dimension = NULL;
obj.direction = NULL;
return obj;
}
static void _cbf_free_arrayAxisSet(const cbf_arrayAxisSet_t obj)
{
const char * const * const path_end = obj.path + obj.count;
const char * const * p;
for (p = obj.path; path_end != p; ++p) free((void*)(*p));
free((void*)obj.axis_set_id);
free((void*)obj.path);
free((void*)obj.index);
free((void*)obj.precedence);
free((void*)obj.dimension);
free((void*)obj.direction);
}
static int _cbf_insert_arrayAxisSet
(cbf_arrayAxisSet_t * const obj,
const char * const axis_set_id,
const char * const path,
unsigned int index,
unsigned int precedence,
unsigned int dimension,
const char * const direction)
{
if (!obj) {
return CBF_ARGUMENT;
} else {
const unsigned int idx = obj->count++;
int error = CBF_SUCCESS;
if (!(obj->axis_set_id = realloc(obj->axis_set_id,obj->count*sizeof(char*)))) error |= CBF_ALLOC;
if (!(obj->path = realloc(obj->path,obj->count*sizeof(char*)))) error |= CBF_ALLOC;
if (!(obj->index = realloc(obj->index,obj->count*sizeof(unsigned int)))) error |= CBF_ALLOC;
if (!(obj->precedence = realloc(obj->precedence,obj->count*sizeof(unsigned int)))) error |= CBF_ALLOC;
if (!(obj->dimension = realloc(obj->dimension,obj->count*sizeof(unsigned int)))) error |= CBF_ALLOC;
if (!(obj->direction = realloc(obj->direction,obj->count*sizeof(char*)))) error |= CBF_ALLOC;
if (CBF_SUCCESS == error) {
obj->axis_set_id[idx] = axis_set_id;
obj->path[idx] = _cbf_strdup(path);
obj->index[idx] = index;
obj->precedence[idx] = precedence;
obj->dimension[idx] = dimension;
obj->direction[idx] = direction;
}
return error;
}
}
static void _cbf_reset_arrayAxisSet(cbf_arrayAxisSet_t * const obj)
{
_cbf_free_arrayAxisSet(*obj);
*obj = _cbf_make_arrayAxisSet();
}
/**
Map an axis ID to an object, a dataset reference, a path to access it and the axis it depends on (or NULL).
For use in converting goniometer & detector axes.
Want to be able to partially order the list, with axes grouped by object and sorted by dependency chain.
Require axis to appear after anything that depends on it, incremental swapping should be stable for acyclic graphs.
*/
typedef struct cbf_axis_t {
/* the name of the axis, which can be used to look up axis data from the CBF axis categories */
const char * * axis_id;
/* axis path within the HDF5 file, these need free'ing individually */
const char * * path;
/*
for use in checking dependency chains & identifying leves,
in_degree of 0 => leaf axis, default should be 0.
*/
unsigned int * in_degree;
/*
index of the axis that this depends on within this structure,
the default should be a very large value like UINT_MAX.
*/
unsigned int * depends_on;
unsigned int count;
} cbf_axis_t;
static cbf_axis_t _cbf_make_axis( void )
{
cbf_axis_t obj;
obj.count = 0;
obj.axis_id = NULL;
obj.path = NULL;
obj.in_degree = NULL;
obj.depends_on = NULL;
return obj;
}
static void _cbf_free_axis(const cbf_axis_t obj)
{
const char * const * const path_end = obj.path + obj.count;
const char * const * p;
for (p = obj.path; path_end != p; ++p) free((void*)(*p));
free((void*)obj.axis_id);
free((void*)obj.path);
free((void*)obj.in_degree);
free((void*)obj.depends_on);
}
/*
append an axis entry to the given axis list object, taking ownership of the path iff the function returns successfully.
*/
static int _cbf_insert_axis
(cbf_axis_t * const obj,
const char * const axis_id,
const char * const path)
{
if (!obj) {
return CBF_ARGUMENT;
} else {
int error = CBF_SUCCESS;
const unsigned int idx = obj->count++;
if (!(obj->axis_id = realloc(obj->axis_id,obj->count*sizeof(char*)))) error |= CBF_ALLOC;
if (!(obj->path = realloc(obj->path,obj->count*sizeof(char*)))) error |= CBF_ALLOC;
if (!(obj->in_degree = realloc(obj->in_degree,obj->count*sizeof(unsigned int*)))) error |= CBF_ALLOC;
if (!(obj->depends_on = realloc(obj->depends_on,obj->count*sizeof(unsigned int*)))) error |= CBF_ALLOC;
if (CBF_SUCCESS == error) {
obj->axis_id[idx] = axis_id;
obj->path[idx] = path;
obj->in_degree[idx] = 0;
obj->depends_on[idx] = ~((unsigned int)(0));
}
return error;
}
}
static void _cbf_reset_axis(cbf_axis_t * const obj)
{
_cbf_free_axis(*obj);
*obj = _cbf_make_axis();
}
/*
Search the array of strings in 'array' for the first matching 'str'.
Set the value pointed by 'idx' to the index of the match, or 'size', if it can be dereferenced.
Return successfully iff a match is found.
*/
static int find_index_of_str_in_array
(const char * const * const array,
const size_t size,
const char * const str,
size_t * const idx)
{
int error = CBF_SUCCESS;
if (!array || !str) {
error |= CBF_ARGUMENT;
} else {
size_t i;
for (i = 0; size != i; ++i) {
if (!strcmp(array[i],str)) break;
}
if (size==i) error |= CBF_NOTFOUND;
if (idx) *idx = i;
}
return error;
}
/**
Track data describing the current frame/scan that is being converted.
May be removed once conversion code is more stable, with parameters passed one-by-one to functions.
*/
typedef struct cbf_cbf2nx_key_t {
double matrix [3][3];
cbf_node * * categories;
const char * scan;
const char * frame;
const char * binary;
const char * array;
const char * diffrn;
const char * diffrn_detector;
const char * diffrn_detector_element;
const char * diffrn_measurement;
const char * wavelength_id;
const char * data_overload;
const char * data_undefined;
const char * detector_dependency;
const char * goniometer_dependency;
unsigned int nScans;
unsigned int nFrames;
unsigned int frame_number;
int has_start_time;
int has_end_time;
unsigned int indent;
unsigned int nCat;
cbf_arrayAxisSet_t arrayAxisSet;
cbf_axis_t axis;
} cbf_cbf2nx_key_t;
static cbf_cbf2nx_key_t _cbf_make_cbf2nx_key( void )
{
cbf_cbf2nx_key_t key;
key.matrix[0][0] = 0.;
key.matrix[0][1] = 0.;
key.matrix[0][2] = 0.;
key.matrix[1][0] = 0.;
key.matrix[1][1] = 0.;
key.matrix[1][2] = 0.;
key.matrix[2][0] = 0.;
key.matrix[2][1] = 0.;
key.matrix[2][2] = 0.;
key.scan = NULL;
key.frame = NULL;
key.binary = NULL;
key.array = NULL;
key.diffrn = NULL;
key.diffrn_detector = NULL;
key.diffrn_detector_element = NULL;
key.diffrn_measurement = NULL;
key.wavelength_id = NULL;
key.data_overload = NULL;
key.data_undefined = NULL;
key.detector_dependency = NULL;
key.goniometer_dependency = NULL;
key.nScans = 0;
key.nFrames = 0;
key.frame_number = 0;
key.has_start_time = 0;
key.has_end_time = 0;
key.indent = 0;
key.nCat = 0;
key.categories = NULL;
key.arrayAxisSet = _cbf_make_arrayAxisSet();
key.axis = _cbf_make_axis();
return key;
}
static void _cbf_reset_cbf2nx_key(cbf_cbf2nx_key_t * const key)
{
key->frame = NULL;
key->binary = NULL;
key->array = NULL;
key->diffrn = NULL;
key->diffrn_detector = NULL;
key->diffrn_detector_element = NULL;
key->diffrn_measurement = NULL;
key->wavelength_id = NULL;
key->data_overload = NULL;
key->data_undefined = NULL;
key->detector_dependency = NULL;
key->goniometer_dependency = NULL;
key->nFrames = 0;
key->frame_number = 0;
key->has_start_time = 0;
key->has_end_time = 0;
key->indent = 0;
_cbf_reset_arrayAxisSet(&key->arrayAxisSet);
_cbf_reset_axis(&key->axis);
}
static void _cbf_free_cbf2nx_key(const cbf_cbf2nx_key_t key)
{
_cbf_free_arrayAxisSet(key.arrayAxisSet);
_cbf_free_axis(key.axis);
}
static int _cbf2nx_key_remove_category
(cbf_cbf2nx_key_t * const key,
const char * const categoryName)
{
int error = CBF_SUCCESS;
if (!key || !key->categories || !categoryName) {
error |= CBF_ARGUMENT;
} else {
cbf_node * * it;
cbf_node * const * const end = key->nCat+key->categories;
for (it = key->categories; end != it; ++it) {
if (*it) {
*it = strcmp((*it)->name,categoryName) ? *it : NULL;
}
}
}
return error;
}
/*
Some simple 'getters' to access read-only primary key values. C's
'offsetof' isn't used in order to allow simpler refactoring if keys should
be stored by column & index instead of directly by value.
These functions should have a signature compatible with:
const char * f(const cbf_cbf2nx_key_t *);
The first parameter is the key to get the value from.
*/
/*
Many of these are small functions that do almost exactly the same thing, so
define the implementation in one place so they can be more easily
maintained.
-CBF2NX_DECL_KEY_STR_GETTER(FUNC,MEMBER):
Declares and defines a function 'FUNC' to get the (const char *) value
of 'key->MEMBER'.
*/
#define CBF2NX_DECL_KEY_STR_GETTER(FUNC,MEMBER) \
static const char * FUNC \
(const cbf_cbf2nx_key_t * const key) \
{ \
if (key) { \
return key->MEMBER; \
} else { \
return NULL; \
} \
}
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_array_id,array)
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_binary_id,binary)
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_scan_id,scan)
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_frame_id,frame)
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_diffrn_id,diffrn)
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_diffrnDetector_id,diffrn_detector)
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_diffrnDetectorElement_id,diffrn_detector_element)
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_diffrnMeasurement_id,diffrn_measurement)
CBF2NX_DECL_KEY_STR_GETTER(cbf2nx_key_get_wavelength_id,wavelength_id)
/*
Some simple 'setters' to take a string and set a value in the key.
These functions should have a signature compatible with:
int f(cbf_cbf2nx_key_t *, const char *);
The first parameter is the key to store the value in.
The second parameter is the value to be stored.
*/
/*
Many of these are small functions that do almost exactly the same thing, so
define the implementation in one place so they can be more easily
maintained.
- CBF2NX_DECL_KEY_STR_SETTER(FUNC,MEMBER):
Declares and defines a function 'FUNC' to set the (const char *) value
of 'key->MEMBER'.
*/
#define CBF2NX_DECL_KEY_STR_SETTER(FUNC,MEMBER) \
static int FUNC \
(cbf_cbf2nx_key_t * const key, \
const char * const value) \
{ \
int error = CBF_SUCCESS; \
if (!key || !value) { \
error |= CBF_ARGUMENT; \
} else { \
key->MEMBER = value; \
} \
return error; \
}
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_diffrn,diffrn)
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_diffrn_detector,diffrn_detector)
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_diffrn_measurement,diffrn_measurement)
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_array,array)
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_binary,binary)
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_overload,data_overload)
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_undefined,data_undefined)
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_diffrn_detector_element,diffrn_detector_element)
CBF2NX_DECL_KEY_STR_SETTER(cbf2nx_key_set_wavelength_id,wavelength_id)
/*
Function to set the (unsigned integer) value of 'key->frame_number'.
*/
static int cbf2nx_key_set_frame_number
(cbf_cbf2nx_key_t * const key,
const char * const value)
{
int error = CBF_SUCCESS;
if (!key || !value) {
error |= CBF_ARGUMENT;
} else {
char * end;
unsigned long frame_number = strtoul(value,&end,10);
if (end!=value)
key->frame_number = frame_number;
else
error |= CBF_FORMAT;
}
return error;
}
/****************************************************************
The following section of code is extracted from J. Sloan's
cbf_hdf5.i
****************************************************************/
/*
Some comparison functions for use in checking the content of
HDF5 datasets/attributes
Should return 0 on success, non-zero on failure
*/
/* We provide a macro and 2 versions of each of the calls with _ULP
variants. */
#ifdef CBF_USE_ULP
#define CBFM_cmp_double(exp,ext,len,prm) cmp_double(exp,ext,len,prm)
#define CBFM_cmp_int(exp,ext,len,prm) cmp_int(exp,ext,len,prm)
#define CBFM_cmp_vlstring(exp,ext,len,prm) cmp_vlstring(exp,ext,len,prm)
#else
#define CBFM_cmp_double(exp,ext,len,prm) cmp_double(exp,ext,len)
#define CBFM_cmp_int(exp,ext,len,prm) cmp_int(exp,ext,len)
#define CBFM_cmp_vlstring(exp,ext,len,prm) cmp_vlstring(exp,ext,len)
#endif
#ifdef CBF_USE_ULP
typedef struct cmp_double_param_t {
int cmp_double_as_float; /* do I use the 64 bit version, if available? */
unsigned int ulp32; /* 32-bit comparison parameter */
#ifndef NO_UINT64_TYPE
uint64_t ulp64; /* 64-bit comparison parameter */
#endif
} cmp_double_param_t;
/** Compare two vectors of doubles */
static int cmp_double(const void * const expected, const void * const existing,
size_t length, const void * const params)
{
const cmp_double_param_t * const cmp_params = (cmp_double_param_t*)(params);
const double * A = expected;
const double * B = existing;
/* go through each vector comparing all values */
#ifndef NO_UINT64_TYPE
if (!cmp_params->cmp_double_as_float && cbf_has_ULP64()) {
while (length && cbf_ULP64(*A++, *B++) < cmp_params->ulp64) --length;
}
else
#endif
{
while (length && cbf_ULP32(*A++, *B++) < cmp_params->ulp32) --length;
}
/* if any are not equal the loop will exit early and length is non-zero */
return length;
}
#else
static int cmp_double(const void * a, const void * b, size_t N)
{
/* go through each vector comparing all values
The allowed delta is 1.e-38+1.e-13*maxmimum of the
summed pairs of absolute values
*/
double delta = 0.;
const double * da = a;
const double * db = b;
size_t i;
for (i=0; i < N; i++) {
if (fabs(da[i])+fabs(db[i]) > delta) delta = fabs(da[i])+fabs(db[i]);
if (fabs(da[i]-db[i]) > 1.e-38+1.e-13*delta) return 1;
}
for (i=0; i < N-1; i++) {
if (fabs(da[i]-da[i]) > 1.e-38+1.e-13*delta) return 1;
}
return 0;
}
#endif
#ifdef CBF_USE_ULP
static int cmp_int(const void * const expected,
const void * const existing,
size_t length,
const void * const params)
#else
static int cmp_int(const void * const expected,
const void * const existing,
size_t length)
#endif
{
const int * A = expected;
const int * B = existing;
/* go through each vector comparing all values */
while (length && *A++ == *B++) --length;
/* if any are not equal the loop will exit early and length is non-zero */
return length;
}
#ifdef CBF_USE_ULP
static int cmp_vlstring(const void * a, const void * b, size_t N,
const void * const params)
#else
static int cmp_vlstring(const void * a, const void * b, size_t N)
#endif
{
const char * const * A = a;
const char * const * B = b;
/* go through each vector comparing all values */
while (N && !cbf_cistrcmp(*A++, *B++)) --N;
/* if any are not equal the loop will exit early and N is non-zero */
return N;
}
/**
Put a character into a buffer at a given position.
Ensures the buffer is long enough to hold the new character, realloc'ing if needed, and then inserts it.
\param c The character.
\param buf A pointer to the realloc'able buffer.
\param n A pointer to the current length of the buffer.
\param k The offset to place the character <code>c</code> in.
\return void
*/
static void cbf_push_buf(const int c, char * * const buf, size_t * const n, size_t *k)
{
int errorcode=0;
size_t old_nelem;
assert(buf);
assert(n);
assert(k);
old_nelem = *n;
if (*k >= *n) {
*n = 1+2 * (*k);
errorcode = cbf_realloc((void **)buf, &old_nelem, 1, *n);
}
if (!errorcode) {
(*buf)[(*k)++] = c;
}
}
/**
Function to tokenise a pilatus v1.2 minicbf header
Will split the header into null-terminated strings that are passed around via a given realloc'able buffer.
Behviour is determined by <code>newline</code>, if <code>newline</code> is non-zero:
A token starting with a digit, <code>[0-9]</code>, will cause a string consisting of digits
and any number or combination of <code>[T:-.]</code> characters to be matched as a token.
Otherwise, the stream will be tokenised according to the default rules.
The default tokenisation method is to split the stream on groups of characters from the set <code>[#=:,() \t\f\v\r\n]</code>.
Any number of adjacent <code>'\r'</code> and <code>'\n'</code> characters are compressed into a single
newline (<code>"\n"</code>) token.
*/
static int _cbf_scan_pilatus_V1_2_miniheader
(char * * const buf,
size_t * const n,
int * const newline,
const int getRestOfLine,
const char * * const string)
{
#define cbf_sgetc(str) (**str ? *(*str)++ : 0)
#define cbf_seof(str) (!**str)
#define cbf_ungetc(str) (--(*str))
int c = 0; /* current character */
size_t k = 0; /* current line length */
const char spaceChars[] = "#=:,() \t\f\v";
const char newlineChars[] = "\n\r";
/* check that sensible arguments are given */
assert(buf);
assert(n);
assert(newline);
assert(string);
/*
Skip space-equivalent characters to find something interesting.
This ends with one of two states: a non-space character is found, or EOF.
*/
do {
c = cbf_sgetc(string);
if (cbf_seof(string)) break;
if (strchr(spaceChars,c)) continue;
else break;
} while (1);
/* if I am at the end of the stream, free the buffer & return 0 */
if (cbf_seof(string)) {
free((void*)(*buf));
*buf = 0;
*n = 0;
return CBF_SUCCESS;
}
/* I have a token, starting with the character c: read it */
if (strchr(newlineChars,c)) {
/* it's a newline token: consume all consecutive newline-equivalent characters */
do {
c = cbf_sgetc(string);
} while (!cbf_seof(string) && strchr(newlineChars,c));
if (!cbf_seof(string)) cbf_ungetc(string);
cbf_push_buf('\n', buf, n, &k);
*newline = 1;
} else {
/* It's a string token */
if (getRestOfLine || (*newline && isdigit(c))) {
/* scan to the end of the current line */
do {
cbf_push_buf(c, buf, n, &k);
c = cbf_sgetc(string);
} while (!cbf_seof(string) && !strchr(newlineChars,c));
if (!cbf_seof(string)) cbf_ungetc(string);
} else {
/* scan it as an arbitrary token */
do {
cbf_push_buf(c, buf, n, &k);
c = cbf_sgetc(string);
} while (!cbf_seof(string) && !strchr(spaceChars,c) && !strchr(newlineChars,c));
if (!cbf_seof(string)) cbf_ungetc(string);
}
*newline = 0;
}
/* null-terminate the token */
cbf_push_buf('\0', buf, n, &k);
return CBF_SUCCESS;
}
/**
Helper function to check if a string is a valid (pilatus format) null-terminated date.
Requires string of format:
<code>YYYY-MM-DDThh:mm:ss.s+</code>
<code>Y</code>: year
<code>M</code>: month
<code>D</code>: day
<code>h</code>: hour
<code>m</code>: minute
<code>s</code>: second
<code>+</code>: second may be fractional, any number of digits are allowed
<code>[-T:]</code>: literal characters
\param str The string to test.
\return non-zero on success, zero otherwise.
*/
static int isDateTime(const char * str)
{
if (!str) return 0;
if (isdigit(*str)) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if ('-'==*str) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if ('-'==*str) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if ('T'==*str) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (':'==*str) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (':'==*str) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
if (isdigit(*str)) ++str; else return 0;
/* Optional '.s*' (fractional second) component: */
if ('.'==*str) ++str; else return '\0'==*str;
while (isdigit(*str)) ++str;
return '\0'==*str;
}
/****************************************************************
The following section of code is extracted from J. Sloan's
cbf_hdf5_common.c
****************************************************************/
/**
Function to check validity of a HDF5 identifier.
HDF5's predefined types are never counted as valid by this function,
so it can't be used to test the validity of a type constant.
Types obtained by using H5Tcopy are safe to test.
\param ID An HDF5 object identifier.
\sa cbf_H5Ocmp
\return Non-zero if the type is valid, zero otherwise.
*/
int cbf_H5Ivalid(const hid_t ID)
{
const htri_t v = H5Iis_valid(ID);
if ( v < 0 ) {
cbf_debug_print("H5Iis_valid call failed.\n");
}
return (ID >= 0 && v > 0);
}
/*
find/create/free a HDF5 group if it's valid & possibly set the ID to an invalid identifier.
*/
/**
<p>Helper function to attempt to create a HDF5 group identified by <code>name</code> and return an error
code, to make error handling more consistant. This will fail if a link with the same name already exists
in <code>parent</code>.</p>
\param location The group that will contain the newly created group.
\param group A pointer to a HDF5 ID type where the group will be stored.
\param name The name that the group will be given.
\sa cbf_H5Gcreate
\sa cbf_H5Gfind
\sa cbf_H5Grequire
\sa cbf_H5Gfree
\return An error code.
*/
int cbf_H5Gcreate(const hid_t location, hid_t * const group, const char * const name)
{
htri_t l;
if (!group || !name || !cbf_H5Ivalid(location)) return CBF_ARGUMENT;
/* check if the link exists */
l = H5Lexists(location, name, H5P_DEFAULT);
if (l < 0) return CBF_H5ERROR;
else if (!l) {
/* no group exists: try to create it */
*group = H5Gcreate2(location,name,H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
return cbf_H5Ivalid(*group) ? CBF_SUCCESS : CBF_H5ERROR;
} else {
/* something exists, hence I can't *create* anything: error */
cbf_debug_print2("A link of the same name, '%s', already exists\n",name)
return CBF_H5ERROR;
}
}
/**
<p>Checks for the existance of a group with the given <code>name</code> and <code>parent</code>. Will return
<code>CBF_NOTFOUND</code> if it cannot be found, or open it if it already exists. An error code will be
returned if something other than a group exists at the specified location.</p>
\param location The group to be searched.
\param group A pointer to a HDF5 ID type where the group will be stored.
\param name The path (ie, name) of the group to be found.
\sa cbf_H5Gcreate
\sa cbf_H5Gfind
\sa cbf_H5Grequire
\sa cbf_H5Gfree
\return An error code.
*/
int cbf_H5Gfind(const hid_t location, hid_t * const group, const char * const name)
{
htri_t l;
/* check the arguments */
if (!group || !name || !cbf_H5Ivalid(location)) return CBF_ARGUMENT;
/* check if the link exists */
l = H5Lexists(location, name, H5P_DEFAULT);
if (l < 0) return CBF_H5ERROR;
else if (!l) {
/* no group exists */
return CBF_NOTFOUND;
} else {
/* something exists, check what it is */
const htri_t e = H5Oexists_by_name(location, name, H5P_DEFAULT);
if (e < 0) return CBF_H5ERROR;
else if (!e) {
/* The link exists but the object doesn't - remove the link & return */
if (H5Ldelete(location, name, H5P_DEFAULT) < 0) return CBF_H5ERROR;
else return CBF_NOTFOUND;
} else {
/* my object exists - check its type */
hid_t g = H5Oopen(location, name, H5P_DEFAULT);
if (H5I_GROUP == H5Iget_type(g)) {
/* it's a group - return it */
*group = g;
return CBF_SUCCESS;
} else {
/* not a group - close the object & fail */
H5Oclose(g);
cbf_debug_print("object is not a group");
return CBF_H5DIFFERENT;
}
}
}
}
/**
<p>Checks for the existance of a group with the given <code>name</code> and <code>parent</code>. Will create the
group if it cannot be found, or open it if it already exists. It is an error if a matching group cannot be found or
created. This uses <code>cbf_H5Gcreate</code> to create any new groups.</p>
\param location The group that will contain the newly created group.
\param group A pointer to a HDF5 ID type where the group will be stored.
\param name The name that the group will be given.
\sa cbf_H5Gcreate
\sa cbf_H5Gfind
\sa cbf_H5Grequire
\sa cbf_H5Gfree
\return An error code.
*/
int cbf_H5Grequire(const hid_t location, hid_t * const group, const char * const name)
{
htri_t l;
/* check the arguments */
if (!group || !name || !cbf_H5Ivalid(location)) return CBF_ARGUMENT;
/* check if the link exists */
l = H5Lexists(location, name, H5P_DEFAULT);
if (l < 0) return CBF_H5ERROR;
else if (!l) {
/* no group exists: try to create it */
return cbf_H5Gcreate(location,group,name);
} else {
/* something exists, check what it is */
const htri_t e = H5Oexists_by_name(location, name, H5P_DEFAULT);
if (e < 0) return CBF_H5ERROR;
else if (!e) {
/* The link exists but the object doesn't - remove the link & create the object */
if (H5Ldelete(location, name, H5P_DEFAULT) < 0) return CBF_H5ERROR;
else {
return cbf_H5Gcreate(location,group,name);
}
} else {
/* my object exists - check its type */
hid_t g = H5Oopen(location, name, H5P_DEFAULT);
if (H5I_GROUP == H5Iget_type(g)) {
/* it's a group - return it */
*group = g;
return CBF_SUCCESS;
} else {
/* not a group - close the object & fail */
H5Oclose(g);
cbf_debug_print("object is not a group");
return CBF_H5DIFFERENT;
}
}
}
}
/**
Attempt to close a group, but don't modify the identifier that described it.
\param ID The HDF5 group to be closed.
\sa cbf_H5Gcreate
\sa cbf_H5Gfind
\sa cbf_H5Grequire
\sa cbf_H5Gfree
\return An error code.
*/
int cbf_H5Gfree(const hid_t ID)
{
if (cbf_H5Ivalid(ID)) return H5Gclose(ID)>=0 ? CBF_SUCCESS : CBF_H5ERROR;
else return CBF_ARGUMENT;
}
/* Open/close a HDF5 file if it's valid & possibly set the ID to an invalid identifier - deliberately avoid find/create/free or
get/set/clear naming convensions */
/**
<p>Will try to open a file of the given name with suitable values for some of it's properties to make memory leaks
less likely.</p>
<p><em>Warning:</em> this function will destroy any existing data in the file, do not pass the name of any file
containing data you want to keep.</p>
\param file A pointer to an HDF5 ID where the newly opened file should be stored.
\param name The name of the file to attempt to open.
\sa cbf_H5Fopen
\sa cbf_H5Fclose
\return An error code.
*/
int cbf_H5Fopen(hid_t * const file, const char * const name)
{
/* define variables & check args */
int error = CBF_SUCCESS;
if (!file || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t fapl = H5Pcreate(H5P_FILE_ACCESS);
if (!cbf_H5Ivalid(fapl)) {
cbf_debug_print("error, couldn't create file access property list.\n");
error |= CBF_H5ERROR;
} else {
CBF_H5CALL(H5Pset_fclose_degree(fapl,H5F_CLOSE_STRONG));
/*
in H5Fcreate:
- H5F_ACC_TRUNC:
overwrites any previous file, losing any existing data
- H5F_ACC_EXCL:
fail if the file already exists, so data isn't lost
can then fall back to H5Fopen to try to open the existing file safely
*/
CBF_H5CALL(*file = H5Fcreate(name,H5F_ACC_TRUNC,H5P_DEFAULT,fapl));
}
if (cbf_H5Ivalid(fapl)) H5Pclose(fapl);
}
return error;
}
/**
Attempt to close a file, but don't modify the identifier that described it.
\param ID The HDF5 file to be closed.
\sa cbf_H5Fopen
\sa cbf_H5Fclose
\return An error code.
*/
int cbf_H5Fclose(const hid_t ID)
{
if (cbf_H5Ivalid(ID)) return H5Fclose(ID)>=0 ? CBF_SUCCESS : CBF_H5ERROR;
else return CBF_ARGUMENT;
}
/* Attributes */
/**
Creates a new attribute of the object <code>location</code> with name given by <code>name</code>,
optionally returning it in the <code>attr</code> variable. An error will occur if a similarly
named attribute already exists.
\param location The hdf5 group/file in which to put the attribute.
\param attr A pointer to a HDF5 object identifier that is set to the location of a valid object
if the function succeeds, otherwise is left untouched.
\param name The name of the existing/new dataset.
\param type The type of data to be stored in the attribute.
\param space The dataspace of the attribute.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
int cbf_H5Acreate
(const hid_t location,
hid_t * const attr,
const char * const name,
const hid_t type,
const hid_t space)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(location)
|| !attr
|| !name
|| H5I_DATATYPE!=H5Iget_type(type)
|| !cbf_H5Ivalid(space)
) error |= CBF_ARGUMENT;
if (CBF_SUCCESS==error) {
const htri_t exists = H5Aexists(location,name);
if (exists<0) {
error |= CBF_H5ERROR;
} else if (exists<=0) {
const hid_t attribute = H5Acreate2(location, name, type, space, H5P_DEFAULT, H5P_DEFAULT);
if (attribute>=0) *attr = attribute;
else error |= CBF_H5ERROR;
} else {
cbf_debug_print2("attribute '%s' exists\n",name);
error |= CBF_IDENTICAL;
}
}
return error;
}
/**
Checks for the existance of an attribute with the given <code>name</code> at <code>location</code>
with a datatype of <code>type</code> and dataspace of <code>space</code>. Will return
<code>CBF_NOTFOUND</code> if it cannot be found, or open it if it already exists.
If <code>type</code> is not a datatype then no check of the attribute datatype will be done.
If <code>space</code> is not a dataspace then no checks of the attribute dataspace wil be done.
\param location The hdf5 group/file in which to put the attribute.
\param attr A pointer to a HDF5 object identifier that is set to the location of a valid object
if the function succeeds, otherwise is left untouched.
\param name The name of the existing/new attribute.
\param type The type of data stored in the attribute, or an invalid identifier if it should not be checked.
\param space The dataspace of the attribute, or an invalid identifier if it should not be checked.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
int cbf_H5Afind
(const hid_t location,
hid_t * const attr,
const char * const name,
const hid_t type,
const hid_t space)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(location) || !attr || !name) {
/* type & space should not be checked here, they are optional */
error |= CBF_ARGUMENT;
} else {
const htri_t exists = H5Aexists(location,name);
if (exists<0) {
error |= CBF_H5ERROR;
} else if (!exists) {
cbf_debug_print2("attribute '%s' doesn't exist\n",name);
error |= CBF_NOTFOUND;
} else {
hid_t attribute = H5Aopen(location,name,H5P_DEFAULT);
if (attribute<0) {
cbf_debug_print2("could not open '%s' attribute\n",name);
error |= CBF_H5ERROR;
} else {
/* get the datatype */
const hid_t currType = H5Aget_type(attribute);
const hid_t currSpace = H5Aget_space(attribute);
if (!(cbf_H5Ivalid(currType) && cbf_H5Ivalid(currSpace))) {
cbf_debug_print2("could not get type or space of '%s' attribute\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_DATATYPE==H5Iget_type(type)) {
/* check the datatype is correct */
const htri_t eq = H5Tequal(currType,type);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) error |= CBF_H5DIFFERENT;
}
if (H5I_DATASPACE==H5Iget_type(space)) {
/* check the dataspace is correct */
const htri_t eq = H5Sextent_equal(currSpace,space);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) error |= CBF_H5DIFFERENT;
}
if (CBF_SUCCESS==error) {
*attr = attribute;
attribute = CBF_H5FAIL;
}
}
cbf_H5Tfree(currType);
cbf_H5Sfree(currSpace);
}
cbf_H5Afree(attribute);
}
}
return error;
}
/**
Reads all of the data from <code>attr</code> into <code>buf</code>, which should have been
allocated as the native type indicated by <code>mem_type</code>.
\param attr A valid hdf5 handle for an attribute.
\param type The type of data in memory.
\param buf The location where the data is to be stored.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
int cbf_H5Aread
(const hid_t attr,
const hid_t type,
void * const buf)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(attr) || H5I_DATATYPE!=H5Iget_type(type) || !buf) {
error |= CBF_ARGUMENT;
} else {
if (H5Aread(attr,type,buf)<0) {
cbf_debug_print("couldn't read attribute\n");
error |= CBF_H5ERROR;
}
}
return error;
}
/**
Read a string attribute into memory, returning a pointer that must be free'd by the caller
in <code>val</code>.
\param attr The attribute to read from.
\param val A pointer to a place the string may be stored.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
int cbf_H5Aread_string
(const hid_t attr,
const char * * const val)
{
/*
TODO: tests
*/
int error = CBF_SUCCESS;
hid_t ftype = CBF_H5FAIL;
hid_t space = CBF_H5FAIL;
hssize_t points = -1;
if (!cbf_H5Ivalid(attr) || !val) error |= CBF_ARGUMENT;
if ((ftype=H5Aget_type(attr))<0) {
cbf_debug_print(" couldn't get type of attribute\n");
error |= CBF_H5ERROR;
} else if (H5T_STRING!=H5Tget_class(ftype)) {
cbf_debug_print("wrong type class for attribute\n");
error |= CBF_H5DIFFERENT;
} else if ((space=H5Aget_space(attr))<0) {
cbf_debug_print("couldn't get space of attribute\n");
error |= CBF_H5ERROR;
} else if ((points=H5Sget_simple_extent_npoints(space))<0) {
cbf_debug_print("couldn't get number of datapoints\n");
error |= CBF_H5ERROR;
} else if (1!=points) {
cbf_debug_print("unexpected number of datapoints\n");
error |= CBF_H5DIFFERENT;
} else {
hid_t mtype = CBF_H5FAIL;
char * value = NULL;
void * ptr = NULL;
if (H5Tis_variable_str(ftype)) {
error |= cbf_H5Tcreate_string(&mtype, H5T_VARIABLE);
ptr = &value;
} else {
const size_t size = H5Tget_size(ftype);
error |= cbf_H5Tcreate_string(&mtype, size);
value = malloc(1+size);
ptr = value;
}
if (CBF_SUCCESS!=error) {
cbf_debug_print("couldn't create string datatype\n");
} else if (H5Aread(attr, mtype, ptr)<0) {
cbf_debug_print("could not read attribute\n");
error |= CBF_H5ERROR;
} else {
*val = value;
}
cbf_H5Tfree(mtype);
}
cbf_H5Tfree(ftype);
return error;
}
/**
Writes all of the data from <code>buf</code>, which should contain data of the type
indicated by <code>mem_type</code>, into <code>attr</code>.
\param attr A valid hdf5 handle for an attribute.
\param type The type of data in memory.
\param buf The address of the data to be written.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
int cbf_H5Awrite
(const hid_t attr,
const hid_t type,
void * const buf)
{
CBF_UNUSED(attr);
CBF_UNUSED(type);
CBF_UNUSED(buf);
return CBF_NOTIMPLEMENTED;
}
/* \brief Check for an attribute with the given space/type/value, or set one if it doesn't exist.
Checks the existance of an attribute of the given name, creating it if it doesn't exist.
Checks the size, type and value of an existing attribute to find out if it is the same as
what was requested as determined by a custom comparison function which may use some extra data
for more sophisticated tests.
\param ID The HDF5 object that the attribute will be applied to.
\param name The name of the attribute.
\param rank The number of dimensions of the attribute data, 0 for scalar data.
\param dim The length of each dimension, not used for scalar data.
\param type The HDF5 type of the attribute data.
\param value The data to be written to the attribute.
\param buf A buffer to be used when reading an existing attribute of the same size.
\param cmp A comparison function to test if a previously set value is equal to the value I asked for.
\sa cbf_H5Arequire_string
\return An error code.
*/
int cbf_H5Arequire_cmp
(const hid_t ID,
const char * const name,
const int rank,
const hsize_t * const dim,
const hid_t attrType,
const void * const value,
void * const buf,
int (*cmp)(const void * a, const void * b, size_t N))
{
/* define variables & check args */
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(ID) || !name || (rank && !dim) || rank<0) {
error |= CBF_ARGUMENT;
} else {
hid_t attrSpace = CBF_H5FAIL;
if (CBF_SUCCESS != (error|=cbf_H5Screate(&attrSpace, rank, dim, 0))) {
cbf_debug_print("error: couldn't create data space");
} else {
if (H5Aexists(ID,name)>0) {
hid_t attr = H5Aopen(ID,name,H5P_DEFAULT);
hid_t currSpace = H5Aget_space(attr);
hid_t currType = H5Aget_type(attr);
/* check everything was opened properly */
if (!cbf_H5Ivalid(attr)) error |= CBF_H5ERROR;
if (!cbf_H5Ivalid(currSpace)) error |= CBF_H5ERROR;
if (!cbf_H5Ivalid(currType)) error |= CBF_H5ERROR;
if (CBF_SUCCESS==error) {
/* check that the dataspace is correct */
const htri_t eq = H5Sextent_equal(currSpace,attrSpace);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) error |= CBF_H5DIFFERENT;
/* else success */;
}
if (CBF_SUCCESS==error) {
/* check the datatype is correct */
if (H5Tget_class(currType) != H5Tget_class(attrType))
error |= CBF_H5DIFFERENT;
/* const htri_t eq = H5Tequal(currType,attrType);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) error |= CBF_H5DIFFERENT; */
/* else success */;
}
/* check that the data is correct */
if (CBF_SUCCESS==error) {
const size_t N = H5Sget_simple_extent_npoints(currSpace);
const htri_t vlStr = H5Tis_variable_str(currType);
H5Aread(attr,currType,buf);
if (cmp(value,buf,N)) {
cbf_debug_print("Incorrect attribute value\n");
error |= CBF_H5DIFFERENT;
}
if (vlStr < 0) error |= CBF_H5ERROR;
if (vlStr > 0) H5Dvlen_reclaim(currType, currSpace, H5P_DEFAULT, buf);
}
/* check local variables are properly closed */
if (cbf_H5Ivalid(currSpace)) H5Sclose(currSpace);
if (cbf_H5Ivalid(currType)) H5Tclose(currType);
if (cbf_H5Ivalid(attr)) H5Aclose(attr);
} else {
hid_t attr = CBF_H5FAIL;
CBF_H5CALL(attr = H5Acreate2(ID,name,attrType,attrSpace,H5P_DEFAULT,H5P_DEFAULT));
CBF_H5CALL(H5Awrite(attr,attrType,value));
}
H5Sclose(attrSpace);
}
}
return error;
}
/**
Checks the existance of an attribute of the given name, size, type and value. Equal value is determined by a
custom comparison function which may use some extra data for more sophisticated tests. A new attribute with the
given properties will be created if none currently exist, the function will fail if an incompatible attribute
exists.
\param ID The HDF5 object that the attribute will be applied to.
\param name The name of the attribute.
\param rank The number of dimensions of the attribute data, 0 for scalar data.
\param dim The length of each dimension, not used for scalar data.
\param fileType The HDF5 type of the attribute data in the file.
\param memType The HDF5 type of the attribute data in memory.
\param value The data to be written to the attribute.
\param buf A buffer to be used when reading an existing attribute of the same size.
\param cmp A comparison function to test if a previously set value is equal to the value I asked for.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
int cbf_H5Arequire_cmp2
(const hid_t ID,
const char * const name,
const int rank,
const hsize_t * const dim,
const hid_t fileType,
const hid_t memType,
const void * const value,
void * const buf,
int (*cmp)(const void *, const void *, size_t))
{
/* define variables & check args */
int error = CBF_SUCCESS;
hid_t attrSpace = CBF_H5FAIL;
hid_t attr = CBF_H5FAIL;
if (!cbf_H5Ivalid(ID)) error |= CBF_ARGUMENT;
if (!name || (rank && !dim) || rank<0) error |= CBF_ARGUMENT;
if (H5I_DATATYPE!=H5Iget_type(fileType)) error |= CBF_ARGUMENT;
if (H5I_DATATYPE!=H5Iget_type(memType)) error |= CBF_ARGUMENT;
if (!value) error |= CBF_ARGUMENT;
cbf_reportFail(cbf_H5Screate(&attrSpace, rank, dim, 0), error);
/* do some work */
if (CBF_SUCCESS==error) {
if (H5Aexists(ID,name) > 0) {
hid_t attr = H5Aopen(ID,name,H5P_DEFAULT);
hid_t currSpace = H5Aget_space(attr);
hid_t currType = H5Aget_type(attr);
/* check everything was opened properly */
if (!cbf_H5Ivalid(attr)) error |= CBF_H5ERROR;
if (!cbf_H5Ivalid(currSpace)) error |= CBF_H5ERROR;
if (!cbf_H5Ivalid(currType)) error |= CBF_H5ERROR;
if (CBF_SUCCESS==error) {
/* check that the dataspace is correct */
const htri_t eq = H5Sextent_equal(currSpace,attrSpace);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) error |= CBF_H5DIFFERENT;
/* else success */;
}
if (CBF_SUCCESS==error) {
/* check the datatype is correct */
if (H5Tget_class(currType) != H5Tget_class(fileType))
error |= CBF_H5DIFFERENT;
/* const htri_t eq = H5Tequal(currType,fileType);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) error |= CBF_H5DIFFERENT*/;
/* else success */;
}
/* check that the data is correct */
if (CBF_SUCCESS==error) {
const size_t N = H5Sget_simple_extent_npoints(currSpace);
const htri_t vlStr = H5Tis_variable_str(currType);
void * const _buf = buf ? 0 : malloc(H5Sget_simple_extent_npoints(currSpace)*H5Tget_size(memType));
void * const membuf = buf ? buf : _buf;
H5Aread(attr,memType,membuf);
if (cmp(value,membuf,N)) {
cbf_debug_print("Incorrect attribute value\n");
error |= CBF_H5DIFFERENT;
}
if (vlStr < 0) error |= CBF_H5ERROR;
if (vlStr > 0) H5Dvlen_reclaim(currType, currSpace, H5P_DEFAULT, membuf);
if (_buf) free(_buf);
}
/* check local variables are properly closed */
cbf_H5Sfree(currSpace);
cbf_H5Tfree(currType);
} else {
reportFail(cbf_H5Ivalid(attr = H5Acreate2(ID,name,fileType,attrSpace,H5P_DEFAULT,H5P_DEFAULT)), CBF_H5ERROR, error);
reportFail(H5Awrite(attr,memType,value)>=0, CBF_H5ERROR, error);
}
}
/* check local variables are properly closed */
if (cbf_H5Ivalid(attrSpace)) H5Sclose(attrSpace);
if (cbf_H5Ivalid(attr)) H5Aclose(attr);
/* done */
return error;
}
/** \brief Check for an attribute with the given space/type/value, or set one if it doesn't exist.
<p>Checks the existance of an attribute of the given name, size, type and value. Equal value is determined by a
custom comparison function which may use some extra data for more sophisticated tests. A new attribute with the
given properties will be created if none currently exist, the function will fail if an incompatible attribute
exists.</p>
\param ID The HDF5 object that the attribute will be applied to.
\param name The name of the attribute.
\param rank The number of dimensions of the attribute data, 0 for scalar data.
\param dim The length of each dimension, not used for scalar data.
\param fileType The HDF5 type of the attribute data in the file.
\param memType The HDF5 type of the attribute data in memory.
\param value The data to be written to the attribute.
\param buf A buffer to be used when reading an existing attribute of the same size.
\param cmp A comparison function to test if a previously set value is equal to the value I asked for.
\param cmp_params A pointer to a data structure which may be used by the comparison function.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
int cbf_H5Arequire_cmp2_ULP
(const hid_t ID,
const char * const name,
const int rank,
const hsize_t * const dim,
const hid_t fileType,
const hid_t memType,
const void * const value,
void * const buf,
int (*cmp)(const void *, const void *, size_t, const void *),
const void * const cmp_params)
{
/* define variables & check args */
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(ID) || !name || (rank && !dim) || rank<0 || H5I_DATATYPE!=H5Iget_type(fileType) || H5I_DATATYPE!=H5Iget_type(memType) || !value) {
error |= CBF_ARGUMENT;
} else {
hid_t attrSpace = CBF_H5FAIL;
if (CBF_SUCCESS != (error|=cbf_H5Screate(&attrSpace, rank, dim, 0))) {
cbf_debug_print("error: couldn't create data space");
} else {
if (H5Aexists(ID,name) > 0) {
hid_t attr = H5Aopen(ID,name,H5P_DEFAULT);
hid_t currSpace = H5Aget_space(attr);
hid_t currType = H5Aget_type(attr);
/* check everything was opened properly */
if (!cbf_H5Ivalid(attr)) error |= CBF_H5ERROR;
if (!cbf_H5Ivalid(currSpace)) error |= CBF_H5ERROR;
if (!cbf_H5Ivalid(currType)) error |= CBF_H5ERROR;
if (CBF_SUCCESS==error) {
/* check that the dataspace is correct */
const htri_t eq = H5Sextent_equal(currSpace,attrSpace);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) {
error |= CBF_H5DIFFERENT;
cbf_debug_print("error: dataspace extent different");
}
/* else success */;
}
if (CBF_SUCCESS==error) {
/* check the datatype is correct */
if (H5Tget_class(currType) != H5Tget_class(fileType)){
error |= CBF_H5DIFFERENT;
cbf_debug_print("error: datastype class different");
}
/* const htri_t eq = H5Tequal(currType,fileType);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) error |= CBF_H5DIFFERENT;*/
/* else success */;
}
/* check that the data is correct */
if (CBF_SUCCESS==error) {
const size_t N = H5Sget_simple_extent_npoints(currSpace);
const htri_t vlStr = H5Tis_variable_str(currType);
void * const _buf = buf ? 0 : malloc(H5Sget_simple_extent_npoints(currSpace)*H5Tget_size(memType));
void * const membuf = buf ? buf : _buf;
H5Aread(attr,memType,membuf);
if (cmp(value,membuf,N,cmp_params)) {
cbf_debug_print("Incorrect attribute value\n");
error |= CBF_H5DIFFERENT;
}
if (vlStr < 0) error |= CBF_H5ERROR;
if (vlStr > 0) H5Dvlen_reclaim(currType, currSpace, H5P_DEFAULT, membuf);
free(_buf);
}
/* check local variables are properly closed */
cbf_H5Sfree(currSpace);
cbf_H5Tfree(currType);
cbf_H5Afree(attr);
} else {
hid_t attr = CBF_H5FAIL;
CBF_H5CALL(attr = H5Acreate2(ID,name,fileType,attrSpace,H5P_DEFAULT,H5P_DEFAULT));
CBF_H5CALL(H5Awrite(attr,memType,value));
cbf_H5Afree(attr);
}
H5Sclose(attrSpace);
}
}
return error;
}
static int cmp_string(const void * const a, const void * const b, const size_t N
#ifdef CBF_USE_ULP
, const void * const params
#endif
)
{
if (1 != N) return 1;
else return strcmp(a,b);
}
/**
Forwarding function that calls <code>cbf_H5Arequire_cmp2_ULP</code> with the appropriate arguments to compare two
strings. The <code>strcmp</code> function is used for string comparison, with a small wrapper to verify array
length:
<code><pre>\/\*\* a possible implementation of a function to compare two strings for equality \*\/
static int cmp_string
(const void * const a,
const void * const b,
const size_t N,
const void * const params)
{
\/\* first ensure the arrays have one element each \*\/
if (1 != N) return 1;
\/\* then forward to 'strcmp' for the actual comparison \*\/
else return strcmp(a,b);
}</pre></code>
\param location HDF5 object to which the string attribute should/will belong.
\param name The name of the attribute.
\param value The value which the attribute should/will have.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
/* TODO: allow a buffer to be provided to avoid malloc/free */
int cbf_H5Arequire_string
(const hid_t location,
const char * const name,
const char * const value)
{
int error = CBF_SUCCESS;
if (!value) error |= CBF_ARGUMENT;
if (CBF_SUCCESS==error) {
hid_t h5atype = CBF_H5FAIL;
error |= cbf_H5Tcreate_string(&h5atype,_cbf_strlen(value));
error |= CBFM_H5Arequire_cmp2(location,name,0,0,h5atype,h5atype,value,0,cmp_string,0);
if (cbf_H5Ivalid(h5atype))cbf_H5Tfree(h5atype);
}
return error;
}
/**
Attempt to close an attribute, but don't modify the identifier that described it.
\param ID The HDF5 attribute to be closed.
\sa cbf_H5Acreate
\sa cbf_H5Afind
\sa cbf_H5Aread
\sa cbf_H5Aread_string
\sa cbf_H5Awrite
\sa cbf_H5Arequire_cmp2
\sa cbf_H5Arequire_cmp2_ULP
\sa cbf_H5Arequire_string
\sa cbf_H5Afree
\return An error code.
*/
int cbf_H5Afree(const hid_t ID)
{
if (cbf_H5Ivalid(ID)) return H5Aclose(ID)>=0 ? CBF_SUCCESS : CBF_H5ERROR;
else return CBF_ARGUMENT;
}
/*
find/create/free hdf5 datasets without directly using hdf5 API.
TODO: function to support arbitrary dataspace/valprop, and forward to it.
*/
/**
The <code>dataset</code> parameter gives a location to store the dataset for use by the caller, for example to
add an attribute to it. If non-zero the returned handle MUST be free'd by the caller with <code>cbf_H5Dfree</code>.
If the <code>name</code> is null, an anonymous dataset is created.
The dimensions of the dataset to create are given in <code>dim</code>. The maximum extents of the dataset are
given in <code>max</code>, which uses the values in <code>dim</code> as defaults if set to a null pointer.
Each element of <code>max</code> must be at least as large as the corresponding element of <code>dim</code>.
The dataset created will be a fixed-size dataset unless one of the elements of <code>max</code> is set to
<code>H5S_UNLIMITED</code>.
A chunk size must be given in the <code>chunk</code> argument if any element of <code>max</code> is set to
<code>H5S_UNLIMITED</code> or is greater than the corresponding element of <code>dim</code>. If the dataset
should not be chunked then a null pointer should be given.
The <code>dim</code>, <code>max</code> and <code>chunk</code> arrays - if given - must each contain <code>rank</code> elements.
This function will fail if a link with the same name already exists in <code>location</code>.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Dcreate
(const hid_t location, /**< The hdf5 group/file in which to put the dataset. */
hid_t * const dataset, /**< An optional pointer to a location where the dataset handle should be stored. */
const char * const name, /**< The name of the new dataset. */
const int rank, /**< The rank of the data. */
const hsize_t * const dim, /**< The dimensions of the dataset to create. Unused if <code>rank == 0</code>. */
const hsize_t * const max, /**< The maximum size of each dimension. Unused if <code>rank == 0</code>. */
const hsize_t * const chunk, /**< The chunk size for the dataset. */
const hid_t type /**< The type of each data element in the file. */)
{
/* define variables & check args */
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(location) || (rank && !dim) || rank<0 || H5I_DATATYPE!=H5Iget_type(type)) {
error |= CBF_ARGUMENT;
} else {
hid_t dataset_local = CBF_H5FAIL;
hid_t dataSpace = CBF_H5FAIL;
hid_t valprop = CBF_H5FAIL;
hid_t * const dsetp = dataset ? dataset : &dataset_local;
htri_t exists = name?H5Lexists(location,name,H5P_DEFAULT):0;
CBF_H5CALL(valprop = H5Pcreate(H5P_DATASET_CREATE));
CBF_CALL2(cbf_H5Screate(&dataSpace, rank, dim, max),error);
/* check if some more variables are valid */
if (rank && max) {
const hsize_t * m;
for (m = max; m != max+rank; ++m) {
if (H5S_UNLIMITED == *m) {
if (!chunk) error |= CBF_ARGUMENT;
}
}
}
/* allow dataset to be chunked */
if (rank && chunk) CBF_H5CALL(H5Pset_chunk(valprop,rank,chunk));
if (exists < 0) {
cbf_debug_print("error: couldn't check is dataset exists");
error |= CBF_H5ERROR;
} else if (exists) {
cbf_debug_print("error: dataset already exists");
error |= CBF_H5ERROR;
} else {
if (name) {
CBF_H5CALL(*dsetp = H5Dcreate2(location,name,type,dataSpace,H5P_DEFAULT,valprop,H5P_DEFAULT));
} else {
CBF_H5CALL(*dsetp = H5Dcreate_anon(location,type,dataSpace,valprop,H5P_DEFAULT));
}
}
if (cbf_H5Ivalid(dataSpace)) H5Sclose(dataSpace);
if (cbf_H5Ivalid(valprop)) H5Pclose(valprop);
/* if the dataset object is requested then return it, otherwise close it */
cbf_H5Dfree(dataset_local);
}
return error;
}
/* \brief Look for a dataset with the given properties.
Succeeds without returning a valid dataset ID if no dataset exists and fails if one with different properties exists.
Finding that the dataset doesn't exist is not a failure - the function worked and returned useful information.
So, if it returns \c CBF_SUCCESS then the dataset must be free'd at some point, otherwise it doesn't need to be free'd.
Use as:
\code
int error = cbf_H5Dfind(., &dataset, ...);
if (CBF_SUCCESS==error) {
if (cbf_H5Ivalid(dataset)) {
use_existing_dataset(dataset);
} else {
cbf_H5Dcreate(...);
use_new_datset(dataset);
}
} else {
handle_error(error);
}
\endcode
\param location The hdf5 group/file in which to put the dataset.
\param dataset A pointer to a HDF5 object identifier that is set to the location of a valid object or an invalid value if the function
succeeds, otherwise is left in an undefined state.
\param name The name of the existing/new dataset.
\param rank See \c cbf_H5Dcreate
\param dim See \c cbf_H5Dcreate
\param max See \c cbf_H5Dcreate
\param chunk See \c cbf_H5Dcreate
\param type See \c cbf_H5Dcreate
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code indicating whether the function successfully determined the presence (or otherwise) of an appropriate dataset.
*/
int cbf_H5Dfind
(const hid_t location,
hid_t * const dataset,
const char * const name,
const int rank,
const hsize_t * const dim,
const hsize_t * const max,
const hsize_t * const chunk,
const hid_t type)
{
htri_t l;
/* check the arguments */
if (!cbf_H5Ivalid(location) || !dataset || !name || (!!rank && !dim) || rank<0) return CBF_ARGUMENT;
CBF_UNUSED(chunk);
/* check if the link exists */
l = H5Lexists(location, name, H5P_DEFAULT);
if (l < 0) return CBF_H5ERROR;
else if (!l) {
*dataset = CBF_H5FAIL;
return CBF_SUCCESS;
} else {
/* check if the group exists */
const htri_t e = H5Oexists_by_name(location, name, H5P_DEFAULT);
if (e < 0) return CBF_H5ERROR;
else if (!e) {
/* The link exists but the object doesn't - try to remove the link & tell the caller that there is no dataset */
if (H5Ldelete(location, name, H5P_DEFAULT) < 0) return CBF_H5ERROR;
else {
*dataset = CBF_H5FAIL;
return CBF_SUCCESS;
}
} else {
/* my object exists - check its type */
hid_t g = H5Oopen(location, name, H5P_DEFAULT);
if (H5I_DATASET == H5Iget_type(g)) {
int error = CBF_SUCCESS;
/* it's a dataset - check its properties */
hid_t currSpace = H5Dget_space(g);
hid_t currType = H5Dget_type(g);
hid_t dataSpace = CBF_H5FAIL;
cbf_reportFail(cbf_H5Screate(&dataSpace, rank, dim, max), error);
if (CBF_SUCCESS==error) {
/* Check space */
const htri_t eq = H5Sextent_equal(currSpace,dataSpace);
if (eq < 0) error |= CBF_H5ERROR;
else if (!eq) {
error |= CBF_H5DIFFERENT;
cbf_debug_print("error: dataspace extent different");
}
/* else success */;
}
/* TODO: Check chunk? */
if (CBF_SUCCESS==error) {
/* check the datatype is correct */
if (H5Tget_class(currType) != H5Tget_class(type)) {
error |= CBF_H5DIFFERENT;
cbf_debug_print("error: datatype class different");
}
/* const htri_t eq = H5Tequal(currType,type);
if (eq<0) error |= CBF_H5ERROR;
else if (!eq) error |= CBF_H5DIFFERENT */;
/* else success */;
}
if (cbf_H5Ivalid(currType))H5Tclose(currType);
if (cbf_H5Ivalid(currSpace))H5Sclose(currSpace);
if (cbf_H5Ivalid(dataSpace))H5Sclose(dataSpace);
*dataset = CBF_SUCCESS==error ? g : CBF_H5FAIL;
return error;
} else {
/* not a dataset - close the object & fail */
H5Oclose(g);
cbf_debug_print("not a dataset");
return CBF_H5DIFFERENT;
}
}
}
}
/**
Returns <code>CBF_NOTFOUND</code> without modifying <code>dataset</code> if no dataset exists and fails without
modifying <code>dataset</code> if one with different properties exists. A dataset will be 'found' if it has the
same name and a maximum size which is at least as big as the size requested in <code>max</code>.
A buffer of <code>rank</code> elements pointed to by <code>buf</code> may be used to store the array of
maximum extents for a potentially matching dataset, in order to avoid the use of <code>malloc</code> &
<code>free</code> for very small amounts of memory.
Use as:
<code><pre>\/\* Get the return code from the function call, \*\/
const int found = cbf_H5Dfind(location, &dataset, ...);
\/\* and check what it was: \*\/
if (CBF_SUCCESS==found) {
\/\* A dataset already existed and I have a handle for it: \*\/
use_existing_dataset(dataset);
} else if (CBF_NOTFOUND==found) {
\/\* No matching dataset existed, so I can create one: \*\/
cbf_H5Dcreate(location, &dataset, ...);
use_new_datset(dataset);
} else {
\/\*
The function call failed, do something with the error.
In this case, store it for later use and print a message.
\*\/
error |= found;
cbf_debug_print(cbf_strerror(error));
}
\/\* clean up: \*\/
cbf_H5Dfree(dataset);</pre></code>
\param location The hdf5 group/file in which to put the dataset.
\param dataset A pointer to a HDF5 object identifier that is set to the location of
a valid object if the function succeeds, otherwise is left in an undefined state.
\param name The name of the existing/new dataset.
\param rank The rank of the data, must be equal to the length of the <code>max</code> and <code>buf</code> arrays, if they are given.
\param max The (optional) maximum size of each dimension, pointer or an array of length <code>rank</code> where
<code>0 <= max[i] <= H5S_UNLIMITED</code> for <code>i = [0, rank)</code>, unused if <code>rank == 0</code>.
\param buf An optional buffer with <code>rank</code> elements which may be used to store the
current maximum dimensions of a potential match to avoid a malloc/free call.
\param type The type of each data element in the file. If an invalid type is given a dataset of any type may be returned.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return <code>CBF_SUCCESS</code> if a matching dataset was found, <code>CBF_NOTFOUND</code> if nothing with the same
name was found, some other error code otherwise.
*/
int cbf_H5Dfind2
(const hid_t location,
hid_t * const dataset,
const char * const name,
const int rank,
const hsize_t * const max,
hsize_t * const buf,
const hid_t type)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(location) || !dataset || !name || rank<0) {
error |= CBF_ARGUMENT;
} else {
/* check if the link exists */
const htri_t l = H5Lexists(location, name, H5P_DEFAULT);
if (l < 0) {
cbf_debug_print2("error: Could not check if link '%s' exists\n",name);
error |= CBF_H5ERROR;
} else if (!l) {
error |= CBF_NOTFOUND;
} else {
/* check if the linked object exists */
const htri_t e = H5Oexists_by_name(location, name, H5P_DEFAULT);
if (e < 0) {
cbf_debug_print2("Could not check if object '%s' exists\n",name);
error |= CBF_H5ERROR;
} else if (!e) {
/* The link exists but the object doesn't - try to remove the link & tell the caller that there is no dataset */
if (H5Ldelete(location, name, H5P_DEFAULT) < 0) {
cbf_debug_print2("Could not remove dead link '%s'\n",name);
error |= CBF_H5ERROR;
} else {
error |= CBF_NOTFOUND;
}
} else {
/* my object exists - check its type */
hid_t g = H5Oopen(location, name, H5P_DEFAULT);
if (!cbf_H5Ivalid(g)) {
error |= CBF_H5ERROR;
} else {
if (H5I_DATASET != H5Iget_type(g)) {
cbf_debug_print("type differs");
error |= CBF_H5DIFFERENT;
} else {
/* it's a dataset - check its properties */
const hid_t currSpace = H5Dget_space(g);
if (!cbf_H5Ivalid(currSpace)) {
cbf_debug_print("error: couldn't get data space");
error |= CBF_H5ERROR;
} else {
const int currRank = H5Sget_simple_extent_dims(currSpace, 0, 0);
if (currRank < 0) {
cbf_debug_print2("Could not get rank of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (currRank != rank) {
if (currRank > rank) {
cbf_debug_print3("error: Current rank of '%d' differs from expected rank of '%d'\n",currRank, rank);
error |= CBF_H5DIFFERENT;
}
} else if (CBF_SUCCESS==error && currRank > 0 && max && rank > 0) {
int i;
/* Check dataspace if it makes sense to do so */
hsize_t * const _buf = buf ? 0 : malloc(rank*sizeof(hsize_t));
hsize_t * const currMax = buf ? buf : _buf;
if (H5Sget_simple_extent_dims(currSpace, 0, currMax)<0) {
cbf_debug_print2("Could not get extent of '%s'\n",name);
error |= CBF_H5ERROR;
}
for (i=0; i != currRank; ++i) {
if (currMax[i] != max[i] && currMax[i] < max[i]) {
cbf_debug_print("dataspace differs");
error |= CBF_H5DIFFERENT;
}
}
free((void*)_buf);
}
}
cbf_H5Sfree(currSpace);
if (CBF_SUCCESS==error && H5I_DATATYPE==H5Iget_type(type)) {
/* check the datatype is correct */
const hid_t currType = H5Dget_type(g);
if (!cbf_H5Ivalid(currType)) {
cbf_debug_print("error: couldn't get data space");
error |= CBF_H5ERROR;
} else {
if (H5Tget_class(currType) != H5Tget_class(type) )
error |= CBF_H5DIFFERENT;
/* const htri_t eq = H5Tequal(currType,type);
if (eq<0) {
cbf_debug_print2("error: couldn't test type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (!eq) error |= CBF_H5DIFFERENT;*/
}
cbf_H5Tfree(currType);
}
/* return the dataset & transfer ownership of it to the caller, or keep it to free it later */
if (CBF_SUCCESS==error) {
*dataset = g;
g = CBF_H5FAIL;
} else {
*dataset = CBF_H5FAIL;
}
}
}
if (cbf_H5Ivalid(g)) H5Oclose(g);
}
}
}
return error;
}
/**
Ensure a dataset of the given <code>rank</code> exists and can hold at least as many elements as specified in
<code>max</code>. If no dataset exists then one will be created with dimensions of [0, 0, ... 0].
<code>cbf_H5Dfind</code> and <code>cbf_H5Dcreate</code> are used in the implementation of this function.
An existing dataset may be found using <code>cbf_H5Dfind2(location, dataset, name, rank, max, buf, type)</code>.
If no dataset can be found then a dataset will be created by setting each element of a buffer of length
<code>rank</code> to zero and using <code>cbf_H5Dcreate(location, dataset, name, rank, buffer, max, chunk, type)</code>.
A buffer of <code>rank</code> elements may be provided to avoid using malloc to allocate memory for a small array
whose size may already be known.
The value pointed to by <code>dataset</code> should be a valid object identifier if the function exits successfully,
and will be left in an undefined state otherwise.
This is roughly equivalent to:
<code><pre>const int error = cbf_H5Dfind2(location, dataset, name, rank, max, buf, type);
if (CBF_NOTFOUND==error) {
int i;
for (i = 0; i != rank; ++i) buf[i] = 0;
return cbf_H5Dcreate(location, dataset, name, rank, buf, max, chunk, type);
} else {
\/\* 'error' may be 'CBF_SUCCESS' or could indicate an error: \*\/
return error;
}</pre></code>
but contains more sophisticated error handling code and allows for some parameters to be omitted.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Drequire
(const hid_t location, /**< The hdf5 group/file in which to put the dataset. */
hid_t * const dataset, /**< A pointer to a location to store the dataset. */
const char * const name, /**< The name of the existing/new dataset. */
const int rank, /**< The rank of the data. */
const hsize_t * const max, /**< The (optional) maximum size of each dimension. */
const hsize_t * const chunk, /**< The chunk size used if creating a new dataset. */
hsize_t * const buf, /**< An optional buffer with <code>rank</code> elements. */
const hid_t type /**< The type of each data element in the file. */)
{
int error = CBF_SUCCESS;
if (rank < 0) {
error |= CBF_ARGUMENT;
} else {
int found = CBF_SUCCESS;
hid_t dset = CBF_H5FAIL; /* always free'able */
hid_t * dsetp = dataset ? dataset : &dset; /* always usable */
found = cbf_H5Dfind2(location,dsetp,name,rank,max,buf,type);
if (CBF_SUCCESS == found) {
/* cbf_H5Dfind already checked the dimensions & type, so I don't need to do anything here */
} else if (CBF_NOTFOUND==found) {
/* create a suitable dataset */
hsize_t * const _buf = (buf || !rank) ? NULL : malloc(rank*sizeof(hsize_t)); /* always free'able */
hsize_t * const dim = buf ? buf : _buf; /* always usable */
hsize_t * it;
for (it = dim; it != dim+rank; ++it) *it = 0;
CBF_CALL2(cbf_H5Dcreate(location,dsetp,name,rank,dim,max,chunk,type),error);
free((void*)_buf);
} else {
error |= found;
/* maybe report the failure? */
cbf_debug_print(cbf_strerror(error));
}
cbf_H5Dfree(dset);
}
return error;
}
/**
Insert a slice of data into <code>dataset</code> with the appropriate <code>offset</code> &
<code>stride</code>, ensuring that no existing data is lost due to resizing the dataset but not checking
that previous data isn't being overwritten.
The <code>offset</code>, <code>stride</code>, <code>count</code> and <code>buf</code> arrays must each
have <code>rank</code> elements. If <code>stride</code> is set to the null pointer then a default of
<code>[1, 1, 1, ..., 1]</code> will be used. An optional buffer may be provided in <code>buf</code> to
avoid using malloc to allocate a small amount of memory whose size may actually be known at compile time.
The <code>value</code> array should contain <code>count[0] * count[1] * ... * count[rank-1] === product(count)</code> elements of data.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Dinsert
(const hid_t dataset, /**< The dataset to write the data to. */
const hsize_t * const offset, /**< Where to start writing the data. */
const hsize_t * const stride, /**< The number of elements in the dataset to step for each element to be written. */
const hsize_t * const count, /**< The number of elements in each dimension to be written. */
hsize_t * const buf, /**< An optional buffer to avoid using the heap for small amounts of memory. */
const void * const value, /**< The address of the data to be written. */
const hid_t type) /**< The type of data in memory. */
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(dataset) || !offset || !count || !value || H5I_DATATYPE!=H5Iget_type(type)) {
error |= CBF_ARGUMENT;
} else {
/* get the rank and current dimensions of the dataset */
const hid_t oldSpace = H5Dget_space(dataset);
const int rank = H5Sget_simple_extent_dims(oldSpace,0,0);
hsize_t * const _buf = buf ? 0 : malloc(rank*sizeof(hsize_t));
hsize_t * dim = buf ? buf : _buf;
if (H5Sget_simple_extent_dims(oldSpace,dim,0) != rank) error |= CBF_H5ERROR;
if (rank >= 0) {
hid_t memSpace = !rank ? H5Screate(H5S_SCALAR) : H5Screate_simple(rank,count,0);
hid_t newSpace = CBF_H5FAIL;
/* extend the dimensions, if required */
unsigned int i;
for (i = 0; i != (unsigned int)rank; ++i) {
const hsize_t sz = offset[i] + (stride?stride[i]:1)*(count[i]-1)+1;
dim[i] = (dim[i]>sz) ? dim[i] : sz;
}
CBF_H5CALL(H5Dset_extent(dataset,dim));
newSpace = H5Dget_space(dataset);
/* select elements & write the dataset */
if (rank) {
CBF_H5CALL(H5Sselect_hyperslab(newSpace, H5S_SELECT_SET, offset, stride, count, 0));
} else {
CBF_H5CALL(H5Sselect_all(newSpace));
}
CBF_H5CALL(H5Dwrite(dataset,type,memSpace,newSpace,H5P_DEFAULT,value));
/* check local variables are properly closed */
if (cbf_H5Ivalid(memSpace)) H5Sclose(memSpace);
if (cbf_H5Ivalid(newSpace)) H5Sclose(newSpace);
}
if (cbf_H5Ivalid(oldSpace)) H5Sclose(oldSpace);
free((void*)_buf);
}
return error;
}
/**
Forwards to a HDF5 function to change the extent of <code>dataset</code>. The <code>dim</code> array must have
the same number of elements as the rank of the dataset, but this can't be checked within this function.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Dset_extent
(const hid_t dataset, /**< A handle for the dataset whose extent is to be changed. */
const hsize_t * const dim /**< The new extent of the dataset, if the function succeeds. */)
{
int error = CBF_SUCCESS;
if (!dim || !cbf_H5Ivalid(dataset) || H5I_DATASET!=H5Iget_type(dataset)) {
error |= CBF_ARGUMENT;
} else {
CBF_H5CALL(H5Dset_extent(dataset,dim));
}
return error;
}
/* \brief Add some data to the specified position in the dataset, without checking what (if anything) was there before.
Assumes the dataset has the appropriate size to contain all the data and overwrites any existing data that may be there.
The \c rank of the dataset is assumed to be known, and the size of the array parameters is not tested.
\param dataset The dataset to write the data to.
\param offset Where to start writing the data, as an array of \c rank numbers.
\param stride The number of elements in the dataset to step for each element to be written, where
null is equivalent to a stride of [1, 1, 1, ..., 1], as an array of \c rank numbers.
\param count The number of elements in each dimension to be written, as an array of \c rank numbers.
\param value The address of the data to be written.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Dset_extent
\sa cbf_H5Dread
\sa cbf_H5Drequire_scalar_F64LE
\sa cbf_H5Drequire_string
\sa cbf_H5Dfree
\sa cbf_H5Ddestroy
\return An error code.
*/
int cbf_H5Dwrite
(const hid_t dataset,
const hsize_t * const offset,
const hsize_t * const stride,
const hsize_t * const count,
const void * const value)
{
/* define variables & check args */
int error = (!cbf_H5Ivalid(dataset)) ? CBF_ARGUMENT : CBF_SUCCESS;
hid_t datatype = H5Dget_type(dataset);
hid_t filespace = H5Dget_space(dataset);
const int rank = H5Sget_simple_extent_ndims(filespace);
hid_t memspace = !rank ? H5Screate(H5S_SCALAR) : H5Screate_simple(rank,count,0);
hid_t memtype = H5Tget_native_type(datatype, H5T_DIR_ASCEND);
if ((!!rank && (!offset || !count)) || rank<0) error |= CBF_ARGUMENT;
/* check variables are valid */
reportFail(cbf_H5Ivalid(filespace), CBF_H5ERROR, error);
reportFail(cbf_H5Ivalid(datatype), CBF_H5ERROR, error);
/* select elements & write the dataset */
if (rank) {
reportFail(H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, stride, count, 0)>=0, CBF_H5ERROR, error);
} else {
reportFail(H5Sselect_all(filespace)>=0, CBF_H5ERROR, error);
}
reportFail(H5Dwrite(dataset,memtype,memspace,filespace,H5P_DEFAULT,value)>=0, CBF_H5ERROR, error);
/* check local variables are properly closed */
if (cbf_H5Ivalid(memtype)) H5Tclose(memtype);
if (cbf_H5Ivalid(memspace)) H5Sclose(memspace);
if (cbf_H5Ivalid(filespace)) H5Sclose(filespace);
if (cbf_H5Ivalid(datatype)) H5Tclose(datatype);
/* done */
return error;
}
/**
Assumes the dataset has the appropriate size to contain all the data and overwrites any existing data that may
be there. The <code>rank</code> of the dataset is assumed to be known, and the size of the array parameters is not
tested. When <code>rank</code> is zero - in the case of scalar datasets - the <code>offset</code>,
<code>stride</code> and <code>count</code> parameters are meaningless and should be omitted by setting them to
zero.
\param dataset The dataset to write the data to.
\param offset Where to start writing the data, as an array of <code>rank</code> numbers.
\param stride The number of elements in the dataset to step for each element to be written, where
null is equivalent to a stride of [1, 1, 1, ..., 1], as an array of <code>rank</code> numbers.
\param count The number of elements in each dimension to be written, as an array of <code>rank</code> numbers.
\param value The address of the data to be written.
\param type The type of data in memory.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Dwrite2
(const hid_t dataset,
const hsize_t * const offset,
const hsize_t * const stride,
const hsize_t * const count,
const void * const value,
const hid_t type)
{
int error = CBF_SUCCESS;
if (H5I_DATASET!=H5Iget_type(dataset) || !cbf_H5Ivalid(dataset) || !value || H5I_DATATYPE!=H5Iget_type(type)) {
error |= CBF_ARGUMENT;
} else {
hid_t filespace = H5Dget_space(dataset);
if (!cbf_H5Ivalid(filespace)) {
error |= CBF_H5ERROR;
cbf_debug_print(cbf_strerror(error));
} else {
/* get some data from the dataspace */
const int rank = H5Sget_simple_extent_ndims(filespace);
if ((rank && (!offset || !count)) || rank<0) {
error |= CBF_ARGUMENT;
} else {
hid_t memspace = rank ? H5Screate_simple(rank,count,0) : H5Screate(H5S_SCALAR);
if (!cbf_H5Ivalid(memspace)) {
error |= CBF_H5ERROR;
cbf_debug_print("couldn't create dataspace");
} else {
/* select elements */
if (rank) {
CBF_H5CALL(H5Sselect_hyperslab(filespace,H5S_SELECT_SET,offset,stride,count,0));
} else {
CBF_H5CALL(H5Sselect_all(filespace));
}
/* write the dataset */
CBF_H5CALL(H5Dwrite(dataset,type,memspace,filespace,H5P_DEFAULT,value));
}
cbf_H5Sfree(memspace);
}
}
cbf_H5Sfree(filespace);
}
return error;
}
/* \brief Extract some existing data from a dataset at a known position.
Read some data from a given location in the dataset to an existing location in memory.
Does not check the length of the array parameters, which should all have \c rank elements or (in some cases) be null.
\param dataset The dataset to read the data from.
\param offset Where to start reading the data, as an array of \c rank numbers.
\param stride The number of elements in the dataset to step for each element to be read, where
null is equivalent to a stride of [1, 1, 1, ..., 1], as an array of \c rank numbers.
\param count The number of elements in each dimension to be read, as an array of \c rank numbers.
\param value The location where the data is to be stored.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite
\sa cbf_H5Drequire_scalar_F64LE
\sa cbf_H5Drequire_string
\sa cbf_H5Dfree
\sa cbf_H5Ddestroy
\return An error code.
*/
int cbf_H5Dread
(const hid_t dataset,
const hsize_t * const offset,
const hsize_t * const stride,
const hsize_t * const count,
void * const value)
{
/* define variables & check args */
int error = (!cbf_H5Ivalid(dataset)) ? CBF_ARGUMENT : CBF_SUCCESS;
hid_t datatype = H5Dget_type(dataset);
hid_t filespace = H5Dget_space(dataset);
const int rank = H5Sget_simple_extent_ndims(filespace);
hid_t memspace = !rank ? H5Screate(H5S_SCALAR) : H5Screate_simple(rank,count,0);
if ((!!rank && (!offset || !count)) || rank<0) error |= CBF_ARGUMENT;
/* check variables are valid */
reportFail(cbf_H5Ivalid(filespace), CBF_H5ERROR, error);
reportFail(cbf_H5Ivalid(datatype), CBF_H5ERROR, error);
/* select elements & read the dataset */
if (!!rank) {
reportFail(H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, stride, count, 0)>=0, CBF_H5ERROR, error);
} else {
reportFail(H5Sselect_all(filespace)>=0, CBF_H5ERROR, error);
}
reportFail(H5Dread(dataset, datatype, memspace, filespace, H5P_DEFAULT, value)>=0, CBF_H5ERROR, error);
/* check local variables are properly closed */
if (cbf_H5Ivalid(memspace)) H5Sclose(memspace);
if (cbf_H5Ivalid(filespace)) H5Sclose(filespace);
if (cbf_H5Ivalid(datatype)) H5Tclose(datatype);
/* done */
return error;
}
/**
Read some data from a given location in the dataset to a string. Does not check the
length of the array parameters, which should all have <code>rank</code> elements or (in some cases) be
<code>null</code>. When <code>rank</code> is zero - in the case of scalar datasets - the <code>offset</code>,
<code>stride</code> and <code>count</code> parameters are meaningless and should be omitted by setting them to
zero.
\param dataset The dataset to read the data from.
\param offset Where to start read the data, as an array of <code>rank</code> numbers.
\param value The location into which to store the address of the newly allocated string.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Dread_element_as_string
(const hid_t dataset,
const hsize_t * const offsets,
const size_t noffsets,
char * * value)
{
/* define variables & check args */
int error = CBF_SUCCESS;
char h5t_type_class[14];
hid_t file_type;
hid_t mem_type;
H5T_class_t file_type_class;
hid_t file_space;
hid_t mem_space;
H5T_sign_t type_sign = H5T_SGN_ERROR;
size_t type_size;
int atomic;
size_t ndims = 0, kdims = 0, ii;
unsigned char* data = NULL;
hsize_t offset[H5S_MAX_RANK];
hsize_t count[H5S_MAX_RANK];
if (!cbf_H5Ivalid(dataset) || !value) {
return CBF_ARGUMENT;
} else {
file_space = H5Dget_space(dataset);
file_type = H5Dget_type(dataset);
mem_type = H5Tget_native_type(file_type,H5T_DIR_ASCEND);
file_type_class = H5Tget_class(file_type);
cbf_reportnez(cbf_h5type_class_string(file_type_class,
h5t_type_class,&atomic,14),error);
if (!error) {
cbf_H5Sfree(file_space);
H5Tclose(mem_type);
H5Tclose(file_type);
cbf_debug_print(
"cbf_H5Dread_element_as_string does not support compound types");
return error;
}
kdims= ndims = H5Sget_simple_extent_ndims(file_space);
if (ndims <= 0) ndims = 1;
count[0] = 1;
if (kdims) {
for (ii=0; ii < ndims; ii++) {
if (offsets && ii < noffsets) {
offset[ii] = offsets[ii];
} else {
offset[ii] = 0;
}
count[ii] = 1;
}
mem_space = H5Screate_simple(ndims,count,0);
CBF_H5CALL(H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, 0, count, 0));
} else {
mem_space = H5Screate(H5S_SCALAR);
CBF_H5CALL(H5Sselect_all(file_space));
}
type_size = H5Tget_size(mem_type);
cbf_reportnez(cbf_alloc(((void **) &data),NULL,1,type_size+1),error);
CBF_H5CALL(H5Dread(dataset, mem_type, mem_space, file_space, H5P_DEFAULT, data));
cbf_H5Sfree(mem_space);
cbf_H5Sfree(file_space);
H5Tclose(mem_type);
H5Tclose(file_type);
if (file_type_class==H5T_STRING) {
*value = (char *)data;
} else if (file_type_class==H5T_INTEGER){
/* Read of a single integer or an integer array of
up to 3 dimensions */
char * ivalue = NULL;
long xdata = 0;
unsigned long uxdata = 0;
int sign;
sign = (type_sign==H5T_SGN_2)?1:0;
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,type_size*3+1),error);
if (H5Tequal(mem_type, H5T_NATIVE_CHAR)&&sign) xdata = *((signed char *)data);
if (H5Tequal(mem_type, H5T_NATIVE_CHAR)&&!sign) uxdata = *((unsigned char *)data);
if (H5Tequal(mem_type, H5T_NATIVE_SCHAR)) xdata = *((signed char *)data);
if (H5Tequal(mem_type, H5T_NATIVE_UCHAR)) uxdata = *((unsigned char *)data);
if (H5Tequal(mem_type, H5T_NATIVE_SHORT)) xdata = *((unsigned short *)data);
if (H5Tequal(mem_type, H5T_NATIVE_USHORT)) uxdata = *((unsigned short *)data);
if (H5Tequal(mem_type, H5T_NATIVE_INT)) xdata = *((int *)data);
if (H5Tequal(mem_type, H5T_NATIVE_UINT)) uxdata = *((unsigned int *)data);
if (H5Tequal(mem_type, H5T_NATIVE_LONG)) xdata = *((long *)data);
if (H5Tequal(mem_type, H5T_NATIVE_ULONG)) uxdata = *((unsigned long *)data);
if (sign) {
sprintf(ivalue,"%ld",xdata);
} else {
sprintf(ivalue,"%lu",uxdata);
}
*value = ivalue;
cbf_reportnez(cbf_free((void**)&data,NULL),error);
} else if (file_type_class==H5T_FLOAT){
/* Read of a single float or double or a float or
double array of up to 3 dimensions */
char * ivalue = NULL;
double dxdata;
float xdata;
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,type_size*2+6),error);
if (H5Tequal(mem_type, H5T_NATIVE_FLOAT)) {
xdata = *((float *)data);
snprintf(ivalue,type_size*2+5,"%.7g",(double) xdata);
} else {
dxdata = *((double *)data);
snprintf(ivalue,type_size*2+5,"%.15g",dxdata);
}
*value = ivalue;
cbf_reportnez(cbf_free((void**)&data,NULL),error);
} else if (file_type_class!= H5T_OPAQUE){
char * hexvalue = NULL;
char hexdigs[16] = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f'};
size_t ii;
cbf_reportnez(cbf_alloc(((void **) &hexvalue),NULL,
1,2*type_size+1),error);
hexvalue[2*type_size+1] = '\0';
for (ii=0; ii< type_size; ii++) {
hexvalue[(type_size-ii)*2-2] =
hexdigs[((int)(((unsigned char *)data))[ii])&0xF];
hexvalue[(type_size-ii)*2-1] =
hexdigs[((int)((((unsigned char *)data))[ii])>>4)&0xF];
}
cbf_reportnez(cbf_free((void**)&data,NULL),error);
*value = hexvalue;
}
}
return error;
}
/**
Read some data from a given location in the dataset to an existing location in memory. Does not check the
length of the array parameters, which should all have <code>rank</code> elements or (in some cases) be
<code>null</code>. When <code>rank</code> is zero - in the case of scalar datasets - the <code>offset</code>,
<code>stride</code> and <code>count</code> parameters are meaningless and should be omitted by setting them to
zero.
\param dataset The dataset to read the data from.
\param offset Where to start writing the data, as an array of <code>rank</code> numbers.
\param stride The number of elements in the dataset to step for each element to be written, where
null is equivalent to a stride of [1, 1, 1, ..., 1], as an array of <code>rank</code> numbers.
\param count The number of elements in each dimension to be written, as an array of <code>rank</code> numbers.
\param value The location where the data is to be stored.
\param type The type of data in memory.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Dread2
(const hid_t dataset,
const hsize_t * const offset,
const hsize_t * const stride,
const hsize_t * const count,
void * const value,
const hid_t type)
{
/* define variables & check args */
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(dataset)) {
error |= CBF_ARGUMENT;
} else {
hid_t filespace = H5Dget_space(dataset);
if (!cbf_H5Ivalid(filespace)) {
error |= CBF_H5ERROR;
} else {
/* get rank of dataset and dataspace of memory block */
const int rank = H5Sget_simple_extent_ndims(filespace);
hid_t memspace = !rank ? H5Screate(H5S_SCALAR) : H5Screate_simple(rank,count,0);
if ((rank && (!offset || !count)) || rank<0) {
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(memspace)) {
error |= CBF_H5ERROR;
} else {
/* select elements & read the dataset */
if (rank) {
CBF_H5CALL(H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, stride, count, 0));
} else {
CBF_H5CALL(H5Sselect_all(filespace));
}
CBF_H5CALL(H5Dread(dataset, type, memspace, filespace, H5P_DEFAULT, value));
}
cbf_H5Sfree(memspace);
}
cbf_H5Sfree(filespace);
}
return error;
}
/* \brief Write a scalar 64-bit floating point number as a dataset.
Convenience function using the HDF5 abstraction layer to avoid the need to consider array-related
parameters for a scalar dataset and to automatically set the string type to the correct size.
\param location The group containing the new dataset.
\param dataset An optional pointer to a place to store the new dataset.
\param name The name of the new dataset.
\param value The value of the new dataset.
\param cmp A comparison function to test if a previously set value is equal to the value I asked for.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite
\sa cbf_H5Dread
\sa cbf_H5Drequire_string
\sa cbf_H5Dfree
\sa cbf_H5Ddestroy
\return An error code.
*/
int cbf_H5Drequire_scalar_F64LE
(const hid_t location,
hid_t * const dataset,
const char * const name,
const double value)
{
int error = CBF_SUCCESS;
hid_t _dataset = CBF_H5FAIL;
error |= cbf_H5Dfind(location,&_dataset,name,0,0,0,0,H5T_IEEE_F64LE);
if (CBF_SUCCESS==error) {
if (!cbf_H5Ivalid(_dataset)) {
error |= cbf_H5Dcreate(location,&_dataset,name,0,0,0,0,H5T_IEEE_F64LE);
error |= cbf_H5Dwrite2(_dataset,0,0,0,&value,H5T_NATIVE_DOUBLE);
} else {
double data = 0./0.;
error |= cbf_H5Dread(_dataset,0,0,0,&data);
if (fabs(value - data)> 1.e-38+1.e-13*(fabs(value)+fabs(data))) {
cbf_debug_print4("Error: data doesn't match (%g vs %g) for nexus field '%s'\n",data,value,name);
error |= CBF_H5DIFFERENT;
}
}
/* cleanup temporary dataset? */
if (dataset) *dataset = _dataset;
else cbf_H5Dfree(_dataset);
} else {
cbf_debug_print2("Attempt to determine existence of nexus field '%s' failed\n",name);
}
return error;
}
/**
Convenience function using the HDF5 abstraction layer to avoid the need to consider array-related parameters
for a scalar dataset.It ensures that a scalar 64-bit IEEE floating point dataset exists with the appropriate
name and (for an existing dataset) the correct value as determined by the comparison function <code>cmp</code>.
\param location The group containing the new dataset.
\param dataset An optional pointer to a place to store the new dataset.
\param name The name of the new dataset.
\param value The value of the new dataset.
\param cmp A comparison function to test if a previously set value is equal to the value I asked for.
\param cmp_params Some extra data required by the comparison function.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Drequire_scalar_F64LE2
(const hid_t location,
hid_t * const dataset,
const char * const name,
const double value,
int (*cmp)(const void *, const void *, size_t))
{
int error = CBF_SUCCESS;
int found = CBF_SUCCESS;
hid_t _dataset = CBF_H5FAIL;
hid_t * dset = dataset ? dataset : &_dataset;
found = cbf_H5Dfind2(location,dset,name,0,0,0,H5T_IEEE_F64LE);
if (CBF_SUCCESS==found) {
double data = 0./0.;
error |= cbf_H5Dread2(*dset,0,0,0,&data,H5T_NATIVE_DOUBLE);
if (cmp(&value, &data, 1)) {
cbf_debug_print4("data doesn't match (%g vs %g) for nexus field '%s'\n",data,value,name);
error |= CBF_H5DIFFERENT;
}
} else if (CBF_NOTFOUND==found) {
error |= cbf_H5Dcreate(location,dset,name,0,0,0,0,H5T_IEEE_F64LE);
error |= cbf_H5Dwrite2(*dset,0,0,0,&value,H5T_NATIVE_DOUBLE);
} else {
error |= found;
cbf_debug_print2("Attempt to determine existence of nexus field '%s' failed\n",name);
}
/* cleanup temporary dataset */
cbf_H5Dfree(_dataset);
return error;
}
/**
Convenience function using the HDF5 abstraction layer to avoid the need to consider array-related parameters
for a scalar dataset. It ensures that a scalar 64-bit IEEE floating point dataset exists with the appropriate
name and (for an existing dataset) the correct value as determined by the user-supplied comparison function
<code>cmp</code>.
It is implemented using some of the other dataset functions:
- cbf_H5Dfind2
- cbf_H5Dcreate
- cbf_H5Dread2
- cbf_H5Dwrite2
\param location The group containing the new dataset.
\param dataset An optional pointer to a place to store the new dataset.
\param name The name of the new dataset.
\param value The value of the new dataset.
\param cmp A comparison function to test if a previously set value is equal to the value I asked for.
\param cmp_params Some extra data which may be required by the comparison function.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Drequire_scalar_F64LE2_ULP
(const hid_t location,
hid_t * const dataset,
const char * const name,
const double value,
int (*cmp)(const void *, const void *, size_t, const void *),
const void * const cmp_params)
{
int error = CBF_SUCCESS;
int found = CBF_SUCCESS;
hid_t _dataset = CBF_H5FAIL; /* always free'able */
hid_t * dset = dataset ? dataset : &_dataset; /* always usable */
found = cbf_H5Dfind2(location,dset,name,0,0,0,H5T_IEEE_F64LE);
if (CBF_SUCCESS==found) {
double data = 0./0.;
CBF_CALL2(cbf_H5Dread2(*dset,0,0,0,&data,H5T_NATIVE_DOUBLE),error);
if (cmp(&value, &data, 1, cmp_params)) {
cbf_debug_print4("data doesn't match (%g vs %g) for nexus field '%s'\n",data,value,name);
error |= CBF_H5DIFFERENT;
}
} else if (CBF_NOTFOUND==found) {
CBF_CALL2(cbf_H5Dcreate(location,dset,name,0,0,0,0,H5T_IEEE_F64LE),error);
CBF_CALL2(cbf_H5Dwrite2(*dset,0,0,0,&value,H5T_NATIVE_DOUBLE),error);
} else {
error |= found;
cbf_debug_print2("Attempt to determine existence of nexus field '%s' failed\n",name);
}
/* cleanup temporary dataset */
cbf_H5Dfree(_dataset);
return error;
}
/**
Convenience function using the HDF5 abstraction layer to avoid the need to consider array-related parameters
for a scalar dataset and to automatically set the string type to the correct size.
\param location The group containing the new dataset.
\param dataset An optional pointer to a place to store the new dataset.
\param name The name of the new dataset.
\param value The value of the new dataset.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Drequire_flstring
(const hid_t location,
hid_t * const dataset,
const char * const name,
const char * const value)
{
int error = CBF_SUCCESS;
if (!value) {
error |= CBF_ARGUMENT;
} else {
hid_t dataType = CBF_H5FAIL;
CBF_CALL2(cbf_H5Tcreate_string(&dataType,_cbf_strlen(value)),error);
if (CBF_SUCCESS==error) {
int found = CBF_SUCCESS;
hid_t _dataset = CBF_H5FAIL;
hid_t * dset = dataset ? dataset : &_dataset;
found = cbf_H5Dfind2(location,dset,name,0,0,0,dataType);
if (CBF_SUCCESS==found) {
hid_t currType = H5Dget_type(*dset);
if (!cbf_H5Ivalid(currType)) {
error |= CBF_H5ERROR;
} else {
char * data = malloc(1+ H5Tget_size(currType));
if (!data) {
error |= CBF_ALLOC;
} else if (CBF_SUCCESS!=(error|=cbf_H5Dread2(*dset,0,0,0,(void * const)(data),dataType))) {
cbf_debug_print(cbf_strerror(error));
} else if (strcmp(value, data)) {
error |= CBF_H5DIFFERENT;
cbf_debug_print4("data doesn't match ('%s' vs '%s') for dataset '%s'\n",data,value,name);
}
/* 'data' is either allocated by me or by the HDF5 library: always free it */
free((void*)data);
}
H5Tclose(currType);
} else if (CBF_NOTFOUND==found) {
CBF_CALL2(cbf_H5Dcreate(location,dset,name,0,0,0,0,dataType),error);
CBF_CALL2(cbf_H5Dwrite2(*dset,0,0,0,(const void * const)(value),dataType),error);
} else {
error |= found;
cbf_debug_print2("Attempt to determine existence of nexus field '%s' failed\n",name);
}
cbf_H5Dfree(_dataset);
}
cbf_H5Tfree(dataType);
}
return error;
}
/**
Attempt to close a dataset, but don't modify the identifier that described it.
\param ID The HDF5 dataset to be closed.
\sa cbf_H5Dcreate
\sa cbf_H5Dfind2
\sa cbf_H5Drequire
\sa cbf_H5Dinsert
\sa cbf_H5Dset_extent
\sa cbf_H5Dwrite2
\sa cbf_H5Dread2
\sa cbf_H5Drequire_scalar_F64LE2
\sa cbf_H5Drequire_scalar_F64LE2_ULP
\sa cbf_H5Drequire_flstring
\sa cbf_H5Dfree
\return An error code.
*/
int cbf_H5Dfree(const hid_t ID)
{
if (cbf_H5Ivalid(ID)) return H5Dclose(ID)>=0 ? CBF_SUCCESS : CBF_H5ERROR;
else return CBF_ARGUMENT;
}
/* Custom HDF5 types - to get the correct string type for datasets in a consistent way */
/** \brief Get a HDF5 string datatype with a specified length.
Convenience function to create a string datatype suitable for use when storing a string of length
<code>len</code>, returning it in the identifier pointed to by <code>type</code>.
\param type A pointer to a the HDF5 handle of the new datatype, which should be free'd with \c cbf_H5Tfree
\param len The length of the string datatype - should be \c _cbf_strlen() or \c H5T_VARIABLE
\sa cbf_H5Tcreate_string
\sa cbf_H5Tfree
\return An error code.
*/
int cbf_H5Tcreate_string(hid_t * const type, const size_t len)
{
int error = CBF_SUCCESS;
CBF_H5CALL(*type = H5Tcopy(H5T_C_S1));
CBF_H5CALL(H5Tset_size(*type,H5T_VARIABLE==len?len:len+1));
return error;
}
/** \brief Close a HDF5 datatype identifier
Attempt to close a datatype identifier, but don't modify the identifier that described it.
\param ID The HDF5 datatype to be closed.
\sa cbf_H5Tcreate_string
\sa cbf_H5Tfree
\return An error code.
*/
int cbf_H5Tfree(const hid_t ID)
{
if (cbf_H5Ivalid(ID)) return H5Tclose(ID)>=0 ? CBF_SUCCESS : CBF_H5ERROR;
else return CBF_ARGUMENT;
}
/* HDF5 dataspace functions: I need a uniform method of creating data spaces to ensure correct operation of comparison functions */
/** \brief Create a dataspace with some given values
Helper function which creates a HDF5 dataspace.
Maximum dimensions can be set to infinity by passing <code>H5S_UNLIMITED</code> in the appropriate slot of the
<code>max</code> parameter. If <code>rank</code> is zero then neither <code>dim</code> nor <code>max</code> are
used and a scalar dataspace is created. If <code>rank</code> is non-zero and <code>dim</code> is a null pointer
then <code>ID</code> will not be modified and the function will fail. If <code>rank</code> is non-zero and
<code>max</code> is a null pointer the maximum length is set to the current length as given by <code>dim</code>.
\param ID A pointer to a HDF5 identifier that will contain the new dataspace.
\param rank The number of dimensions of the new dataspace.
\param dim The current size of each dimension of the dataspace, should be an array of length \c rank .
\param max The maximum size of each dimension, should be an array of length \c rank .
\sa cbf_H5Screate
\sa cbf_H5Sfree
\return An error code.
*/
int cbf_H5Screate
(hid_t * const ID,
const int rank,
const hsize_t * const dim,
const hsize_t * const max)
{
int error = CBF_SUCCESS;
if (!ID || (rank && !dim) || rank<0) {
error |= CBF_ARGUMENT;
} else {
hid_t space = rank ? H5Screate_simple(rank, dim, max) : H5Screate(H5S_SCALAR);
if (!cbf_H5Ivalid(space)) {
error |= CBF_H5ERROR;
} else {
*ID = space;
}
}
return error;
}
/** \brief Close a HDF5 dataspace identifier
Attempt to close a dataspace identifier, but don't modify the identifier that described it.
\param ID The HDF5 dataspace to be closed.
\sa cbf_H5Screate
\sa cbf_H5Sfree
\return An error code.
*/
int cbf_H5Sfree(const hid_t ID)
{
if (cbf_H5Ivalid(ID)) return H5Sclose(ID)>=0 ? CBF_SUCCESS : CBF_H5ERROR;
else return CBF_ARGUMENT;
}
/* HDF5 objects */
/**
Compare two HDF5 object ID's for equality. This follows the standard practice of returning zero if objects
should be considered equal, and the HDF5 practice of returning a negative number if there is an error.
\param id0 An HDF5 identifier.
\param id1 An HDF5 identifier.
\sa cbf_H5Ocmp
\sa cbf_H5Ivalid
\return 0 if equal, a positive value if not equal, or a negative value if there is an error.
*/
htri_t cbf_H5Ocmp
(const hid_t id0,
const hid_t id1)
{
htri_t error = 0;
herr_t valid0 = H5Iis_valid(id0);
herr_t valid1 = H5Iis_valid(id1);
if (valid0 < 0 || valid1 < 0) {
error = -1;
} else if (!valid0 || !valid1) {
error = 1;
} else {
H5O_info_t info0, info1;
herr_t err0 = H5Oget_info(id0,&info0);
herr_t err1 = H5Oget_info(id1,&info1);
if (err0 < 0 || err1 < 0) {
error = -1;
} else if (info0.fileno != info1.fileno || info0.addr != info1.addr || info0.type != info1.type) {
error = 1;
} else {
error = 0;
}
}
return error;
}
/**
Attempt to close an object identifier of unknown type, but don't modify the identifier that described it.
\param ID The HDF5 object to be closed.
\sa cbf_H5Ocmp
\sa cbf_H5Ofree
\sa cbf_H5Ivalid
\return An error code.
*/
int cbf_H5Ofree(const hid_t ID)
{
if (cbf_H5Ivalid(ID)) return H5Oclose(ID)>=0 ? CBF_SUCCESS : CBF_H5ERROR;
else return CBF_ARGUMENT;
}
/****************************************************************
End of section of code extracted from J. Sloan's
cbf_hdf5_common.c
****************************************************************/
/****************************************************************
The following section of code is extracted from J. Sloan's
config.c
****************************************************************/
/* Some error codes for use by the parsing functions - definitions should not be visible */
const int cbf_configError_success = 0;
const int cbf_configError_unexpectedInput = 1;
const int cbf_configError_expectedDelimeter = 2;
const int cbf_configError_expectedNumber = 3;
const int cbf_configError_openingFile = 4;
const int cbf_configError_expectedString = 5;
const int cbf_configError_duplicateField = 6;
const int cbf_configError_unexpectedEOF = 7;
const int cbf_configError_undefinedValue = 8;
const int cbf_configError_invalidDependency = 9;
const int cbf_configError_missingDependency = 10;
const int cbf_configError_loop = 11;
static int cbf_isblank (int c)
{
return (' ' == c || '\t' == c);
}
/*
Tokenise an input stream, returning one token at a time into the given buffer.
\param buf A pointer to a realloc-able buffer for storing the input, free'd when EOF is reached.
\param n The current size of \c buf.
\param ln The current line number of the file.
\param pre The previous character, needed to test for unexpected EOL state.
\param stream The stream to be tokenised.
\return A parser error code.
*/
static int cbf_configParse_scan(char * * const buf, size_t * n, size_t * ln, char * const pre, FILE * stream)
{
int c = 0; /* current character */
size_t k = 0; /* current line length */
/* check that sensible arguments are given */
assert(buf);
assert(n);
assert(ln);
assert(pre);
assert(stream);
/* skip blanks & comments to get to an interesting character */
do {
c = fgetc(stream);
if (feof(stream)) break;
*pre = c;
if ('\n' == c) ++*ln;
if ('#' == c) {
do {
c = fgetc(stream);
if (feof(stream)) break;
*pre = c;
if ('\n' == c) ++*ln;
} while (!isspace(c) || cbf_isblank(c));
}
if (cbf_isblank(c)) continue;
else break;
} while (1);
/* if I am at the end of the stream, free the buffer & return 0 */
if (feof(stream)) {
free((void*)(*buf));
*buf = 0;
*n = 0;
return '\n'==*pre ? cbf_configError_success : cbf_configError_unexpectedEOF;
}
/* I have a token: read it */
if (isspace(c)) {
/* it's a newline token */
cbf_push_buf('\n', buf, n, &k);
} else {
/* it's a string : leave the terminating character in the stream as it may be a token itself */
if ('[' == c || ']' == c) {
/* it's a vector start or end token : match the structure of a vector at a higher level */
cbf_push_buf(c, buf, n, &k);
} else while (1) {
cbf_push_buf(c, buf, n, &k);
c = fgetc(stream);
if (feof(stream)) break;
*pre = c;
if (isspace(c) || '[' == c || ']' == c) {
ungetc(c, stream);
break;
}
}
}
/* null-terminate the token */
cbf_push_buf('\0', buf, n, &k);
return (feof(stream) && '\n'!=*pre) ? cbf_configError_unexpectedEOF : cbf_configError_success;
}
/**
The returned string is "none" for success, "unknown error" if the given error code is
not recognised and a non-empty string briefly describing the error otherwise.
The returned string must not be free'd.
\param error An error returned by a <code>cbf_config_*</code> function.
\return A string describing the error.
*/
const char * cbf_config_strerror(const int error)
{
if (error == cbf_configError_success) return "none";
else if (error == cbf_configError_unexpectedInput) return "unexpected input";
else if (error == cbf_configError_expectedDelimeter) return "expected a delimiter";
else if (error == cbf_configError_expectedNumber) return "expected a number";
else if (error == cbf_configError_openingFile) return "could not open file";
else if (error == cbf_configError_expectedString) return "expected a string";
else if (error == cbf_configError_duplicateField) return "duplicate data";
else if (error == cbf_configError_undefinedValue) return "a value was not defined at point of use";
else if (error == cbf_configError_invalidDependency) return "invalid dependency found";
else if (error == cbf_configError_missingDependency) return "missing dependency";
else if (error == cbf_configError_loop) return "dependency loop detected";
else return "unknown error";
}
/* POD to define a basic set of configuration settings for an axis */
typedef struct cbf_configItem_t
{
double vector[3];
const char * minicbf;
const char * nexus;
const char * depends_on;
struct cbf_configItem_t * next;
int convert;
} cbf_configItem_t;
/*
Initialises name & depends_on to null, vector to [0,0,0].
*/
static cbf_configItem_t cbf_configItem_create( void )
{
cbf_configItem_t item;
item.vector[0] = 0.;
item.vector[1] = 0.;
item.vector[2] = 0.;
item.minicbf = NULL;
item.nexus = NULL;
item.depends_on = NULL;
item.next = NULL;
item.convert = 0;
return item;
}
static void cbf_configItem_free(const cbf_configItem_t * item)
{
if (item) {
free((void*)(item->minicbf));
free((void*)(item->nexus));
free((void*)(item->depends_on));
}
}
/*
Should not be manipulated directly, takes ownership of the config items which it contains.
*/
struct cbf_config_t
{
size_t nItems;
size_t maxItems;
const char * sample_depends_on;
cbf_configItem_t * item;
};
/**
Allocates a new collection of configuration settings on the heap, and initialises it. The returned
pointer should be destroyed by the caller.
\return A newly allocated object for miniCBF configuration settings, or <code>NULL</code>.
*/
cbf_config_t * cbf_config_create( void )
{
cbf_config_t * const vector = malloc(sizeof(cbf_config_t));
if (vector) {
vector->nItems = 0;
vector->maxItems = 0;
vector->sample_depends_on = NULL;
vector->item = NULL;
}
return vector;
}
/**
<p>Destroys an existing collection of configuration settings. The settings should have been obtained by a call to
<code>cbf_config_create</code>.</p>
\param vector The configuration data to be free'd.
\return Nothing.
*/
void cbf_config_free(const cbf_config_t * vector)
{
if (vector) {
const cbf_configItem_t * it = vector->item;
for (; it != vector->item+vector->nItems; ++it) cbf_configItem_free(it);
free((void*)vector->item);
free((void*)vector->sample_depends_on);
free((void*)vector);
}
}
/*
Releases any previously held dependency and takes ownership of a new one.
The given string will be free'd by the object when it is no longer needed.
*/
static void cbf_config_setSampleDependsOn(cbf_config_t * vector, const char * const depends_on)
{
if (vector) {
cbf_free_text(&(vector->sample_depends_on),NULL);
vector->sample_depends_on = depends_on;
}
}
/*
\return The current dependency setting for the sample group, or null if not set.
*/
static const char * cbf_config_getSampleDependsOn(const cbf_config_t * const vector)
{
if (vector) return vector->sample_depends_on;
else return 0;
}
/*
\return A pointer to an item in the vector that may be modified but should not be free'd,
subsequent vector operations may invalidate this pointer.
*/
static cbf_configItem_t * cbf_config_begin(const cbf_config_t * const vector)
{
if (vector) return vector->item;
else return 0;
}
/*
\return A pointer to an item in the vector that may be modified but should not be free'd,
subsequent vector operations may invalidate this pointer.
*/
static const cbf_configItem_t * cbf_config_end(const cbf_config_t * const vector)
{
if (vector) return vector->item+vector->nItems;
else return 0;
}
/*
\brief Append an item to the configuration vector.
The vector will take ownership of the item's contents. This may invalidate any previously obtained pointers to items in the vector.
\return An iterator to the new item, or NULL on failure.
*/
static cbf_configItem_t * cbf_config_push(cbf_config_t * const vector, cbf_configItem_t item)
{
if (vector) {
if (!(vector->nItems < vector->maxItems)) {
/* increase the maximum number of items */
const size_t k = 4;
vector->maxItems = (size_t)(vector->nItems/k) * k + k;
vector->item = realloc(vector->item, vector->maxItems*sizeof(cbf_configItem_t));
}
/* ensure I have enough items */
assert(vector->maxItems > vector->nItems);
/* add the item to the end of the vector & set the item count to the correct number. */
vector->item[vector->nItems++] = item;
return vector->item+vector->nItems-1;
} else return 0;
}
/*
\return An iterator to a matching entry, to the current end element if there is no matching entry, or NULL on failure.
*/
static cbf_configItem_t * cbf_config_findMinicbf(const cbf_config_t * const vector, const char * const name)
{
cbf_configItem_t * it = cbf_config_begin(vector);
const cbf_configItem_t * const end = cbf_config_end(vector);
if (!vector || !name) return 0;
while (end != it && (!it->minicbf || strcmp(it->minicbf,name))) ++it;
return it;
}
/*
\return An iterator to a matching entry, to the current end element if there is no matching entry, or NULL on failure.
*/
static cbf_configItem_t * cbf_config_findNexus(const cbf_config_t * const vector, const char * const name)
{
cbf_configItem_t * it = cbf_config_begin(vector);
const cbf_configItem_t * const end = cbf_config_end(vector);
if (!vector || !name) return 0;
while (end != it && (!it->nexus || strcmp(it->nexus,name))) ++it;
return it;
}
/*
<p>Looks for missing or cyclic dependencies:</p>
<ul>
<li>The base coordinate system is expected to be represented by ".", it is an error if any dependency within
the chain leading to the sample object is not in the set and is not equal to this.</li>
<li>Cyclic dependencies in the chain leading to the sample is also an error.</li>
<li>Dependency chains not leading to the sample object are not tested.</li>
</ul>
<p>This also sets pointers in the config items to quickly locate their dependencies later on, and marks each axis
in the chain to allow irrelevant axes to be skipped.</p>
TODO: test to verify that loops can be caught.
Need to factor out the loop body and count the number steps on a
pre-defined loop to verify that it is identified on the correct step.
\return <p>A parser error code.</p>
*/
static int _cbf_config_validate(const cbf_config_t * const vector)
{
cbf_configItem_t * it1 = cbf_config_findNexus(vector,cbf_config_getSampleDependsOn(vector));
cbf_configItem_t * it2 = it1;
do {
/* mark it2 for conversion & check dependency */
it2->convert = 1;
if (!it2->depends_on) return cbf_configError_missingDependency;
if (!strcmp(it2->depends_on,".")) return cbf_configError_success;
/* set & check it2->next */
if (!it2->next) it2->next = cbf_config_findNexus(vector,it2->depends_on);
if (cbf_config_end(vector)==it2->next) return cbf_configError_invalidDependency;
/* increment it2 & check for loop */
it2 = it2->next;
if (it2==it1) return cbf_configError_loop;
/* mark it2 for conversion & check dependency */
it2->convert = 1;
if (!it2->depends_on) return cbf_configError_missingDependency;
if (!strcmp(it2->depends_on,".")) return cbf_configError_success;
/* set & check it2->next */
if (!it2->next) it2->next = cbf_config_findNexus(vector,it2->depends_on);
if (cbf_config_end(vector)==it2->next) return cbf_configError_invalidDependency;
/* increment it2 & check for loop */
it2 = it2->next;
if (it2==it1) return cbf_configError_loop;
/* increment it1 */
it1 = it1->next;
} while (1);
return cbf_configError_success;
}
static int cbf_configParse_extractVector
(FILE * const configFile,
FILE * const logFile,
cbf_configItem_t * const it,
char * * const buf,
size_t * n,
size_t * ln,
char * const pre)
{
char * end = 0;
#define GET_TOKEN() \
CBFM_PROLOG { \
const int e = cbf_configParse_scan(buf, n, ln, pre, configFile); \
if (cbf_configError_success!=e) { \
fprintf(logFile,"\nError: %s\n",cbf_config_strerror(e)); \
return e; \
} \
} CBFM_EPILOG
#define REQUIRE_TOKEN(TKN) \
CBFM_PROLOG { \
const char * const _tkn = (TKN); \
if (strcmp(_tkn,*buf)) { \
fprintf(logFile,"Config parsing error on line %lu: expected " #TKN ", got '%s'\n",(long unsigned int)*ln,*buf); \
return cbf_configError_unexpectedInput; \
} \
} CBFM_EPILOG
#define REQUIRE_NOT_EOL() \
CBFM_PROLOG { \
if (!strcmp("\n",*buf)) { \
fprintf(logFile,"Config parsing error on line %lu: unexpected newline\n",*ln); \
return cbf_configError_unexpectedInput; \
} \
} CBFM_EPILOG
/* literal '['. */
GET_TOKEN();
REQUIRE_NOT_EOL();
REQUIRE_TOKEN("[");
GET_TOKEN();
REQUIRE_NOT_EOL();
errno = 0;
it->vector[0] = strtod(*buf, &end);
if (errno != 0 || end == *buf) {
fprintf(logFile,"Config parsing error on line %lu: expected a number, got '%s'\n",*ln,*buf);
return cbf_configError_expectedNumber;
}
GET_TOKEN();
REQUIRE_NOT_EOL();
errno = 0;
it->vector[1] = strtod(*buf, &end);
if (errno != 0 || end == *buf) {
fprintf(logFile,"Config parsing error on line %lu: expected a number, got '%s'\n",*ln,*buf);
return cbf_configError_expectedNumber;
}
GET_TOKEN();
REQUIRE_NOT_EOL();
errno = 0;
it->vector[2] = strtod(*buf, &end);
if (errno != 0 || end == *buf) {
fprintf(logFile,"Config parsing error on line %lu: expected a number, got '%s'\n",*ln,*buf);
return cbf_configError_expectedNumber;
}
/* literal ']'. */
GET_TOKEN();
REQUIRE_NOT_EOL();
REQUIRE_TOKEN("]");
#undef GET_TOKEN
#undef REQUIRE_TOKEN
#undef REQUIRE_NOT_EOL
return CBF_SUCCESS;
}
/**
Parses a configuration file to extract a collection of configuration settings for a miniCBF file, storing them
in the given configuration settings object. The pointer should have been obtained by a call to
<code>cbf_config_create</code>. The configuration file format is described in the
<code>minicbf2nexus</code> documentation.
\param configFile The file from which the config settings should be read.
\param logFile A stream to be used for logging error messages.
\param vec An object describing the configuration settings.
\return A parser error code.
*/
int cbf_config_parse
(FILE * const configFile,
FILE * const logFile,
cbf_config_t * const vec)
{
char * tkn = 0;
size_t n = 0, ln = 1;
char pre = '\0';
#define GET_TOKEN() \
CBFM_PROLOG { \
const int e = cbf_configParse_scan(&tkn, &n, &ln, &pre, configFile); \
if (cbf_configError_success!=e) { \
fprintf(logFile,"\nError: %s\n",cbf_config_strerror(e)); \
return e; \
} \
} CBFM_EPILOG
#define REQUIRE_TOKEN(TKN) \
CBFM_PROLOG { \
const char * const _tkn = (TKN); \
if (strcmp(_tkn,tkn)) { \
fprintf(logFile,"Config parsing error on line %lu: expected " #TKN ", got '%s'\n",ln,tkn); \
return cbf_configError_unexpectedInput; \
} \
} CBFM_EPILOG
#define REQUIRE_EOL() \
CBFM_PROLOG { \
if (strcmp("\n",tkn)) { \
fprintf(logFile,"Config parsing error on line %lu: expected '\\n', got '%s'\n",ln,tkn); \
return cbf_configError_unexpectedInput; \
} \
} CBFM_EPILOG
#define REQUIRE_NOT_EOL() \
CBFM_PROLOG { \
if (!strcmp("\n",tkn)) { \
fprintf(logFile,"Config parsing error on line %lu: unexpected newline\n",ln); \
return cbf_configError_unexpectedInput; \
} \
} CBFM_EPILOG
#define REQUIRE_NEXUS_AXIS() \
CBFM_PROLOG { \
if (strcmp(".",tkn) && cbf_config_end(vec) == cbf_config_findNexus(vec,tkn)) { \
fprintf(logFile,"Config parsing error on line %lu: Nexus axis '%s' not defined\n",ln,tkn); \
return cbf_configError_undefinedValue; \
} \
} CBFM_EPILOG
#define REQUIRE_VECTOR() \
CBFM_PROLOG { \
const int e = cbf_configParse_extractVector(configFile, logFile, it, &tkn, &n, &ln, &pre); \
if (cbf_configError_success!=e) { \
fprintf(logFile,"Error reading a vector: %s\n",cbf_config_strerror(e)); \
return e; \
} \
} CBFM_EPILOG
/* first token of the line */
GET_TOKEN();
while (tkn) {
if (!cbf_cistrcmp("map",tkn)) {
/* storage that I don't need to free within this function */
cbf_configItem_t * it;
/* minicbf axis name */
GET_TOKEN();
REQUIRE_NOT_EOL();
it = cbf_config_findMinicbf(vec,tkn);
if (cbf_config_end(vec) != it) {
fprintf(logFile,"Config parsing error on line %lu: Duplicate axis definition for minicbf axis '%s'\n",ln,tkn);
return cbf_configError_duplicateField;
}
it = cbf_config_push(vec,cbf_configItem_create());
it->minicbf = _cbf_strdup(tkn);
/* literal 'to'. */
GET_TOKEN();
REQUIRE_NOT_EOL();
REQUIRE_TOKEN("to");
/* nexus axis name */
GET_TOKEN();
REQUIRE_NOT_EOL();
if (cbf_config_end(vec) != cbf_config_findNexus(vec,tkn)) {
fprintf(logFile,"Config parsing error on line %lu: Duplicate axis definition for Nexus axis '%s'\n",ln,tkn);
return cbf_configError_duplicateField;
}
it->nexus = _cbf_strdup(tkn);
/* newline */
GET_TOKEN();
REQUIRE_EOL();
} else if (!cbf_cistrcmp("Sample",tkn)) {
/* literal 'depends-on'. */
GET_TOKEN();
REQUIRE_NOT_EOL();
REQUIRE_TOKEN("depends-on");
/* nexus axis name */
GET_TOKEN();
REQUIRE_NOT_EOL();
REQUIRE_NEXUS_AXIS();
cbf_config_setSampleDependsOn(vec,_cbf_strdup(tkn));
/* newline */
GET_TOKEN();
REQUIRE_EOL();
} else if (!cbf_cistrcmp("\n",tkn)) {
} else {
/* find entry by nexus axis name */
cbf_configItem_t * const it = cbf_config_findNexus(vec,tkn);
if (cbf_config_end(vec) == it) {
fprintf(logFile,"Config parsing error on line %lu: Nexus axis '%s' not defined\n",ln,tkn);
return cbf_configError_undefinedValue;
}
/* match depends-on -> vector OR vector -> depends-on. */
GET_TOKEN();
REQUIRE_NOT_EOL();
if (!cbf_cistrcmp("vector",tkn)) {
/* try to get a vector */
REQUIRE_VECTOR();
GET_TOKEN();
/* optional 'depends-on' */
if (!cbf_cistrcmp("depends-on",tkn)||!cbf_cistrcmp("depends_on",tkn)) {
/* nexus axis name */
GET_TOKEN();
REQUIRE_NOT_EOL();
REQUIRE_NEXUS_AXIS();
/* this is a potential memory leak */
it->depends_on = _cbf_strdup(tkn);
GET_TOKEN();
}
/* check for newline */
REQUIRE_EOL();
} else if (!cbf_cistrcmp("depends-on",tkn)||!cbf_cistrcmp("depends_on",tkn)) {
/* nexus axis name */
GET_TOKEN();
REQUIRE_NOT_EOL();
REQUIRE_NEXUS_AXIS();
/* this is a potential memory leak */
it->depends_on = _cbf_strdup(tkn);
GET_TOKEN();
/* optional 'vector' */
if (!cbf_cistrcmp("vector",tkn)) {
/* try to get a vector */
REQUIRE_VECTOR();
GET_TOKEN();
}
/* check for newline */
REQUIRE_EOL();
} else return cbf_configError_unexpectedInput;
}
GET_TOKEN();
}
return _cbf_config_validate(vec);
}
/*
Helper function to take the data associated with a pilatus axis and write the
axis attributes to a nexus axis.
\return An error code
*/
#ifdef CBF_USE_ULP
#define CBFM_pilatusAxis2nexusAxisAttrs(h4data,units,depends_on,exsisItem,cmp,cmp_params) \
_cbf_pilatusAxis2nexusAxisAttrs(h4data,units,depends_on,exsisItem,cmp,cmp_params)
#else
#define CBFM_pilatusAxis2nexusAxisAttrs(h4data,units,depends_on,exsisItem,cmp,cmp_params) \
_cbf_pilatusAxis2nexusAxisAttrs(h4data,units,depends_on,exsisItem,cmp)
#endif
static int _cbf_pilatusAxis2nexusAxisAttrs
(hid_t h5data,
const char * const units,
const char * const depends_on,
const cbf_configItem_t * const axisItem,
#ifdef CBF_USE_ULP
int (*cmp)(const void *, const void *, size_t, const void * const)
,const void * const cmp_params
#else
int (*cmp)(const void *, const void *, size_t)
#endif
)
{
int error = CBF_SUCCESS;
CBF_CALL2(cbf_H5Arequire_string(h5data,"units",strcmp(units, "deg.")?units:"degrees"),error);
/* transformation type */
CBF_CALL2(cbf_H5Arequire_string(h5data,"transformation_type","rotation"),error);
/* dependency */
CBF_CALL2(cbf_H5Arequire_string(h5data,"depends_on",depends_on),error);
if (!axisItem->depends_on) {
cbf_debug_print2("Error: missing dependency for nexus axis '%s'\n",axisItem->nexus);
error |= CBF_UNDEFINED;
}
if (CBF_SUCCESS==error) { /* vector */
const hsize_t vdims[] = {3};
double buf[3] = {0./0.};
double matrix[3][3] = {
{-1.,0.0,0.0},
{0.0,1.0,0.0},
{0.0,0.0,-1.},
};
double vector[3];
CBF_CALL2(cbf_apply_matrix(matrix,(double *)axisItem->vector,vector),error);
CBF_CALL2(CBFM_H5Arequire_cmp2(h5data,"vector",1,vdims,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,vector,buf,cmp,cmp_params),error);
}
return error;
}
/****************************************************************
End of section of code extracted from J. Sloan's
config.c
****************************************************************/
/*
Simple wrapper function to create a nexus group.
Forwards to cbf_H5Grequire and cbf_H5Arequire_string, letting them check argument validity.
*/
static int _cbf_NXGrequire
(const hid_t location,
hid_t * const dataset,
const char * const name,
const char * const class)
{
int error = CBF_SUCCESS;
hid_t dset = CBF_H5FAIL;
cbf_reportnez(cbf_H5Grequire(location,&dset,name),error);
cbf_reportnez(cbf_H5Arequire_string(dset,"NX_class",class),error);
if (dataset) *dataset = dset;
else cbf_H5Dfree(dset);
return error;
}
/*
Extract an attribute from a HDF5 object, returning it in attrval as a string that must be free'd,
unless returned as NULL.
*/
static int _cbf_Attrval
(const hid_t object,
const char * attrName,
const char * * const attrval)
{
int error = CBF_SUCCESS;
*attrval = NULL;
/* check the arguments */
if (!cbf_H5Ivalid(object) || !attrval) error |= CBF_ARGUMENT;
if (CBF_SUCCESS==error) {
/* check the NX_class attribute */
hid_t attr = CBF_H5FAIL;
const int found = cbf_H5Afind(object,&attr,attrName,CBF_H5FAIL,CBF_H5FAIL);
if (CBF_SUCCESS!=found) {
cbf_debug_print(cbf_strerror(found));
error |= found;
} else {
error |= cbf_H5Aread_string(attr, attrval);
}
cbf_H5Afree(attr);
}
return error;
}
/*
Extract the NX_class attribute from a HDF5 object, returning it in 'class' as a string that must be free'd.
*/
static int _cbf_NXclass
(const hid_t object,
const char * * const class)
{
return _cbf_Attrval(object,"NX_class",class);
}
/* ************************************************************************
Define a set of node manipulation functions which are missing from cbf's
node tree interface. All to be removed and replaced with the regular
routines they duplicate. This level of access is error-prone and
produces unclear code.
************************************************************************ */
/*
Get the ascii value of a given row of a node, in the same way as 'cbf_get_value' gets data from a handle.
*/
static int cbf_node_get_value
(cbf_node * const node,
const unsigned int row,
const char * * const value)
{
int error = CBF_SUCCESS;
const char * text = NULL;
/* Check the arguments */
if (!node || row >= node->children || !value) {
error |= CBF_ARGUMENT;
} else if (cbf_is_binary(node, row)) {
error |= CBF_BINARY;
} else if (CBF_SUCCESS!=(error|=cbf_get_columnrow(&text, node, row))) {
cbf_debug_print2("error: %s\n", cbf_strerror(error));
} else {
*value = text ? text+1 : NULL;
}
return error;
}
static int cbf_node_get_doublevalue
(cbf_node * const node,
const unsigned int row,
double * const value)
{
int error = CBF_SUCCESS;
const char * text = NULL;
if (!node || row >= node->children || !value) {
error |= CBF_ARGUMENT;
} else if (cbf_is_binary(node, row)) {
error |= CBF_BINARY;
} else if (CBF_SUCCESS!=(error|=cbf_get_columnrow(&text, node, row))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else if (!text || *text==CBF_TOKEN_NULL) {
*value = 0.;
return CBF_SUCCESS;
} else {
char * end = NULL;
const double tmp_val = strtod(text+1, &end);
if (end == text+1) {
cbf_debug_print2("error: %s\n", cbf_strerror(CBF_FORMAT));
*value = 0.;
} else {
*value = tmp_val;
}
}
return error;
}
/* Test for the presence of a floating-point value in a column.
Attempt to access the value at the given column/row location and try to
convert it to a floating point value, discarding the result and returning
a boolean value which can be used to find out if a conversion would
succeed.
TODO: Merge this functionality into the 'cbf_node_get_doublevalue'
function, to allow the test an conversion to be performed in a single
operation.
\return A boolean value suitable for use in a conditional statement.
*/
static int cbf_node_has_doublevalue
(cbf_node * const node,
const unsigned int row)
{
int ret;
const char * text = NULL;
if (!node || row >= node->children) {
ret = 0;
} else if (cbf_is_binary(node, row)) {
ret = 0;
} else if (CBF_SUCCESS!=cbf_get_columnrow(&text, node, row)) {
ret = 0;
} else if (!text || *text==CBF_TOKEN_NULL) {
ret = 0;
} else {
char * end = NULL;
/* check if it can be converted, but don't store the value */
(void)(strtod(text+1, &end));
if (end == text+1) {
ret = 0;
} else {
ret = 1;
}
}
return ret;
}
static int cbf_node_get_uintvalue
(cbf_node * const node,
const unsigned int row,
unsigned int * const value)
{
int error = CBF_SUCCESS;
const char * text = NULL;
if (!node || row >= node->children || !value) {
error |= CBF_ARGUMENT;
} else if (cbf_is_binary(node, row)) {
error |= CBF_BINARY;
} else if (CBF_SUCCESS!=(error|=cbf_get_columnrow(&text, node, row))) {
cbf_debug_print2("error: %s\n", cbf_strerror(error));
} else if (!text || *text==CBF_TOKEN_NULL) {
*value = 0;
return CBF_SUCCESS;
} else {
char * end = NULL;
const unsigned int tmp_val = strtoul(text+1, &end, 0);
if (end == text+1) {
cbf_debug_print2("error: %s\n", cbf_strerror(CBF_FORMAT));
error |= CBF_FORMAT;
*value = 0;
} else {
*value = tmp_val;
}
}
return error;
}
/*
Search for a row with matching value, with the first to check being given by 'row'.
If you want to find the first matching row in a column then set row to '0'.
If you already have a matching row and want to find the next then set row to '1+matching_row'.
*/
static int cbf_node_find_nextrow
(cbf_node * const node,
unsigned int row,
const char * const value,
unsigned int * const nextrow)
{
int error = CBF_SUCCESS;
if (!nextrow || !node || !value) {
error |= CBF_ARGUMENT;
} else if (row >= node->children || CBF_COLUMN != node->type) {
error |= CBF_ARGUMENT;
} else {
for (; row != node->children; ++row) {
const char * str = NULL;
cbf_node_get_value(node,row,&str);
if (!strcmp(value,str)) {
break;
}
}
if (row == node->children) {
error |= CBF_NOTFOUND;
} else {
*nextrow = row;
}
}
return error;
}
/**
Check the handle for the presence of a file, optionally returning it.
\param nx A handle to query for the presence of the requested information.
\param file A place to store the file (if found), or null if the file isn't wanted.
\sa cbf_h5handle_get_file
\sa cbf_h5handle_set_file
\return An error code.
*/
int cbf_h5handle_get_file
(const cbf_h5handle nx,
hid_t * const file)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (file) {
if (cbf_H5Ivalid(nx->hfile)) *file = nx->hfile;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the file id within the handle to the given value. Doesn't check or modify any attributes in any way.
\param nx The handle to add information to.
\param file The file to be set as the current file id.
\sa cbf_h5handle_get_file
\sa cbf_h5handle_set_file
\return An error code.
*/
int cbf_h5handle_set_file
(const cbf_h5handle nx,
const hid_t file)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(file)) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->hfile);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,file);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = file;
}
}
return error;
}
/**
Check the handle for the presence of an entry group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items of data
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_entry
\sa cbf_h5handle_set_entry
\sa cbf_h5handle_require_entry
\return An error code.
*/
int cbf_h5handle_get_entry
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxid);
const char * * const nxName = &(nx->nxid_name);
/* check for a valid group */
if (group) {
if ((*nxGroup) >=0 && cbf_H5Ivalid(*nxGroup)) *group = *nxGroup;
else error |= CBF_NOTFOUND;
}
/* load actual entry group if not found before */
if ((*nxGroup) < 0 || !cbf_H5Ivalid(*nxGroup)) {
error = cbf_H5Gfind(nx->hfile,nxGroup,"entry");
if (error == CBF_SUCCESS && group ) *group = *nxGroup;
if (error == CBF_SUCCESS) {
if (*nxName) {
cbf_debug_print2("freeing entry name '%s'",*nxName);
cbf_free_text(nxName,0);
}
*nxName = _cbf_strdup("entry");
}
}
/* check for a name */
if (name) {
if (*nxName) *name = *nxName;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Check the handle for the presence of data group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_entry
\return An error code.
*/
int cbf_h5handle_get_data
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxid);
hid_t * const nxData = &(nx->nxdata);
const char * * const nxName = &(nx->nxdata_name);
/* ensure the nxid is there */
if (*nxData < 0 || !cbf_H5Ivalid(*nxData) ){
cbf_failnez(cbf_h5handle_get_entry(nx,nxGroup,0));
}
/* check for a valid group */
if (group) {
if ((*nxData) >=0 && cbf_H5Ivalid(*nxData)) *group = *nxData;
else error |= CBF_NOTFOUND;
}
/* load actual nxdata if not found before */
if ((*nxData) < 0 || !cbf_H5Ivalid(*nxData)) {
error = cbf_H5Gfind(*nxGroup,nxData,"data");
if (error == CBF_SUCCESS && group ) *group = *nxData;
if (error == CBF_SUCCESS) *nxName = "data";
}
/* check for a name */
if (name) {
if (*nxName) *name = *nxName;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the entry group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current entry group
\param name The name which the group should be given.
\sa cbf_h5handle_get_entry
\sa cbf_h5handle_set_entry
\sa cbf_h5handle_require_entry
\return An error code.
*/
int cbf_h5handle_set_entry
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxid);
const char * * const nxName = &(nx->nxid_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the entry group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"entry"</code>.
\sa cbf_h5handle_get_entry
\sa cbf_h5handle_set_entry
\sa cbf_h5handle_require_entry
\return An error code.
*/
int cbf_h5handle_require_entry
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
int match = 0;
const char * curr_name = NULL;
const char default_name[] = "entry";
const char * group_name = name ? name : default_name;
CBF_CALL2(cbf_h5handle_get_file(nx,&parent),error);
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_entry(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
match = 1;
/*
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
*/
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
CBF_CALL2(cbf_H5Grequire(parent,&new_group,group_name),error);
CBF_CALL2(cbf_H5Arequire_string(new_group,"NX_class","NXentry"),error);
CBF_CALL2(cbf_h5handle_set_entry(nx,new_group,group_name),error);
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
CBF_CALL2(cbf_h5handle_get_entry(nx,group,0),error);
}
return error;
}
/**
This will check if the entry group and definition within the handle matches any
existing group of the same name within the current file and has a definition
designation that agrees. If the group name doesn't match a new group is opened or created
and added to the handle. If the <code>definition</code> does not match, it is replaced with
the new one. If the <code>version</code> attribute does not match it is replaced with the
new one. If the <code>URL></code> attribute does not match it is replace with the new
one. The <code>NX_class</code> attributes are not checked, but if a new entry
is created it will be created with <code>NX_class</code> NXentry.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group ID should be stored.
\param name The group name, or null to use the default name of <code>"entry"</code>.
\param definition The definition name, or null to not specify a definition name.
\param version The version string, or null to not specify a version string.
\param URL The URL at which the definition is stored, or null to not specify a URL
\sa cbf_h5handle_get_entry
\sa cbf_h5handle_set_entry
\sa cbf_h5handle_require_entry
\return An error code.
*/
int cbf_h5handle_require_entry_definition
(const cbf_h5handle nx,
hid_t * const group,
const char * name,
const char * definition,
const char * version,
const char * URL
)
{
int matchgroup; /* 0 for not found, 1 for matched, -1 for not matched*/
int matchdefinition; /* 0 for not found, 1 for matched, -1 for not matched*/
int matchversion; /* 0 for not found, 1 for matched, -1 for not matched*/
int matchURL; /* 0 for not found, 1 for matched, -1 for not matched*/
int error = CBF_SUCCESS;
hid_t type = CBF_H5FAIL;
matchgroup = 0;
matchdefinition = definition?0:1;
matchversion = version?0:1;
matchURL = URL?0:1;
CBF_CALL2(cbf_H5Tcreate_string(&type,H5T_VARIABLE),error);
if (!nx) {
error |= CBF_ARGUMENT;
} else {
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
hid_t dataset = CBF_H5FAIL;
hid_t attribute = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "entry";
const char * group_name = name ? name : default_name;
CBF_CALL2(cbf_h5handle_get_file(nx,&parent),error);
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_entry(nx,&curr_group,&curr_name)) {
if (!cbf_cistrcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
if (CBF_SUCCESS==cbf_H5Gfind(parent,&test_group,group_name)) {
if (!cbf_H5Ocmp(test_group,curr_group)) matchgroup = 1;
/* If we have the group and want the definition to match
search for the definition field */
if (matchgroup && (!matchdefinition)) {
if (CBF_SUCCESS==cbf_H5Dfind2(test_group,&dataset,"definition",0,0,0,type)){
const hid_t currType = H5Dget_type(dataset);
char * * buf = malloc(H5Tget_size(currType));
hid_t currMemType = CBF_H5FAIL;
cbf_H5Tcreate_string(&currMemType,H5T_VARIABLE);
cbf_H5Dread2(dataset,0,0,0,(void * const)buf,currMemType);
cbf_H5Tfree(currMemType);
/* then compare them */
if (buf[0] && cbf_cistrcmp(definition,buf[0]) != 0) {
CBF_CALL2(cbf_H5Dwrite2(dataset,0,0,0,&definition,type),error);
}
free((void*)buf);
cbf_H5Tfree(currType);
} else {
CBF_CALL2(cbf_H5Dcreate(test_group,&dataset,"definition",0,0,0,0,type),error);
CBF_CALL2(cbf_H5Dwrite2(dataset,0,0,0,&definition,type),error);
}
/* The definition now matches, see if we want the version and/or URL */
if (!matchversion) {
CBF_CALL2(cbf_H5Arequire_string(dataset,"version",version),error);
}
if (!matchURL) {
CBF_CALL2(cbf_H5Arequire_string(dataset,"URL",URL),error);
}
}
} else {
matchgroup = 0;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !matchgroup) {
hid_t new_group = CBF_H5FAIL;
CBF_CALL2(cbf_H5Grequire(parent,&new_group,group_name),error);
CBF_CALL2(cbf_H5Arequire_string(new_group,"NX_class","NXentry"),error);
CBF_CALL2(cbf_h5handle_set_entry(nx,new_group,group_name),error);
if (!matchdefinition) {
CBF_CALL2(cbf_H5Dcreate(new_group,&dataset,"definition",0,0,0,0,type),error);
CBF_CALL2(cbf_H5Dwrite2(dataset,0,0,0,&definition,type),error);
if (!matchversion) {
CBF_CALL2(cbf_H5Arequire_string(dataset,"version",version),error);
}
if (!matchURL) {
CBF_CALL2(cbf_H5Arequire_string(dataset,"URL",URL),error);
}
}
if (CBF_SUCCESS!=error) {
cbf_H5Gfree(new_group);
}
}
/* if there haven't been any major problems, return any requested data */
CBF_CALL2(cbf_h5handle_get_entry(nx,group,0),error);
cbf_H5Tfree(type);
cbf_H5Dfree(dataset);
cbf_H5Afree(attribute);
}
return error;
}
/**
Check the handle for the presence of an sample group and its name, optionally returning any combination of them.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_sample
\sa cbf_h5handle_set_sample
\sa cbf_h5handle_require_sample
\return An error code.
*/
int cbf_h5handle_get_sample
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->nxsample)) *group = nx->nxsample;
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->nxsample_name) *name = nx->nxsample_name;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the sample group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current sample group
\param name The name which the group should be given.
\sa cbf_h5handle_get_sample
\sa cbf_h5handle_set_sample
\sa cbf_h5handle_require_sample
\return An error code.
*/
int cbf_h5handle_set_sample
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxsample);
const char * * const nxName = &(nx->nxsample_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the sample group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"sample"</code>.
\sa cbf_h5handle_get_sample
\sa cbf_h5handle_set_sample
\sa cbf_h5handle_require_sample
\return An error code.
*/
int cbf_h5handle_require_sample
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
int match = 0;
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "sample";
const char * group_name = name ? name : default_name;
CBF_CALL2(cbf_h5handle_get_entry(nx,&parent,0),error);
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_sample(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
CBF_CALL2(cbf_H5Grequire(parent,&new_group,group_name),error);
CBF_CALL2(cbf_H5Arequire_string(new_group,"NX_class","NXsample"),error);
CBF_CALL2(cbf_h5handle_set_sample(nx,new_group,group_name),error);
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
CBF_CALL2(cbf_h5handle_get_sample(nx,group,0),error);
}
return error;
}
/**
Check the handle for the presence of an cbfdb group and its name, optionally returning any combination of them.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_cbfdb
\sa cbf_h5handle_set_cbfdb
\sa cbf_h5handle_require_cbfdb
\return An error code.
*/
int cbf_h5handle_get_cbfdb
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->dbid)) *group = nx->dbid;
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->dbid_name) *name = nx->dbid_name;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the cbfdb group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current sample group
\param name The name which the group should be given.
\sa cbf_h5handle_get_cbfdb
\sa cbf_h5handle_set_cbfdb
\sa cbf_h5handle_require_cbfdb
\return An error code.
*/
int cbf_h5handle_set_cbfdb
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->dbid);
const char * * const nxName = &(nx->dbid_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the cbfdb group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"sample"</code>.
\sa cbf_h5handle_get_cbfdb
\sa cbf_h5handle_set_cbfdb
\sa cbf_h5handle_require_cbfdb
\return An error code.
*/
int cbf_h5handle_require_cbfdb
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
int match = 0;
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "cbfdb";
const char * group_name = name ? name : default_name;
CBF_CALL2(cbf_h5handle_get_entry(nx,&parent,0),error);
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_sample(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
CBF_CALL2(cbf_H5Grequire(parent,&new_group,group_name),error);
if (nx->flags&CBF_H5_NXPDB) {
CBF_CALL2(cbf_H5Arequire_string(new_group,"NX_class","NXpdb"),error);
} else {
CBF_CALL2(cbf_H5Arequire_string(new_group,"NX_class","NXsample"),error);
}
CBF_CALL2(cbf_h5handle_set_cbfdb(nx,new_group,group_name),error);
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
CBF_CALL2(cbf_h5handle_get_sample(nx,group,0),error);
}
return error;
}
/**
Check the handle for the presence of a beam group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items of data
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_beam
\sa cbf_h5handle_set_beam
\sa cbf_h5handle_require_beam
\return An error code.
*/
int cbf_h5handle_get_beam
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->nxbeam)) *group = nx->nxbeam;
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->nxbeam_name) *name = nx->nxbeam_name;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the beam group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current beam group
\param name The name which the group should be given.
\sa cbf_h5handle_get_beam
\sa cbf_h5handle_set_beam
\sa cbf_h5handle_require_beam
\return An error code.
*/
int cbf_h5handle_set_beam
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxbeam);
const char * * const nxName = &(nx->nxbeam_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the beam group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"beam"</code>.
\sa cbf_h5handle_get_beam
\sa cbf_h5handle_set_beam
\sa cbf_h5handle_require_beam
\return An error code.
*/
int cbf_h5handle_require_beam
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
int match = 0;
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "beam";
const char * group_name = name ? name : default_name;
CBF_CALL2(cbf_h5handle_require_sample(nx,&parent,0),error);
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_beam(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
CBF_CALL2(cbf_H5Grequire(parent,&new_group,group_name),error);
CBF_CALL2(cbf_H5Arequire_string(new_group,"NX_class","NXbeam"),error);
CBF_CALL2(cbf_h5handle_set_beam(nx,new_group,group_name),error);
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
CBF_CALL2(cbf_h5handle_get_beam(nx,group,0),error);
}
return error;
}
/**
Check the handle for the presence of an instrument group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items of data
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_instrument
\sa cbf_h5handle_set_instrument
\sa cbf_h5handle_require_instrument
\return An error code.
*/
int cbf_h5handle_get_instrument
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->nxinst)) *group = nx->nxinst;
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->nxinstrument_name) *name = nx->nxinstrument_name;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the instrument group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current instrument group
\param name The name which the group should be given.
\sa cbf_h5handle_get_instrument
\sa cbf_h5handle_set_instrument
\sa cbf_h5handle_require_instrument
\return An error code.
*/
int cbf_h5handle_set_instrument
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxinst);
const char * * const nxName = &(nx->nxinstrument_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/* TODO: add a 'find NXgroup' function, instead of relying on pre-set group names here */
/**
This will check if the instrument group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"instrument"</code>.
\sa cbf_h5handle_get_instrument
\sa cbf_h5handle_set_instrument
\sa cbf_h5handle_require_instrument
\return An error code.
*/
int cbf_h5handle_require_instrument
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
int match = 0;
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "instrument";
const char * group_name = name ? name : default_name;
CBF_CALL(cbf_h5handle_get_entry(nx,&parent,0));
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_instrument(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
CBF_CALL(cbf_H5Grequire(parent,&new_group,group_name));
CBF_CALL(cbf_H5Arequire_string(new_group,"NX_class","NXinstrument"));
CBF_CALL(cbf_h5handle_set_instrument(nx,new_group,group_name));
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
CBF_CALL(cbf_h5handle_get_instrument(nx,group,0));
}
return error;
}
/**
Check the handle for the presence of a detector_group group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items of data
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_detector_group
\sa cbf_h5handle_set_detector_group
\sa cbf_h5handle_require_detector_group
\return An error code.
*/
int cbf_h5handle_get_detector_group
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->nxdetector_group)) *group = nx->nxdetector_group;
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->nxdetector_group_name) *name = nx->nxdetector_group_name;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the detector_group group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current detector_group group
\param name The name which the group should be given.
\sa cbf_h5handle_get_detector_group
\sa cbf_h5handle_set_detector_group
\sa cbf_h5handle_require_detector_group
\return An error code.
*/
int cbf_h5handle_set_detector_group
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxdetector_group);
const char * * const nxName = &(nx->nxdetector_group_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the detector_group group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"detector_group"</code>.
\sa cbf_h5handle_get_detector_group
\sa cbf_h5handle_set_detector_group
\sa cbf_h5handle_require_detector_group
\return An error code.
*/
int cbf_h5handle_require_detector_group
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
int match = 0;
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "detector_group";
const char * group_name = name ? name : default_name;
CBF_CALL(cbf_h5handle_get_instrument(nx,&parent,0));
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_detector_group(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
CBF_CALL(cbf_H5Grequire(parent,&new_group,group_name));
CBF_CALL(cbf_H5Arequire_string(new_group,"NX_class","NXdetector_group"));
CBF_CALL(cbf_h5handle_set_detector_group(nx,new_group,group_name));
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
CBF_CALL(cbf_h5handle_get_detector_group(nx,group,0));
}
return error;
}
/**
Check the handle for the presence of a detector group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items of data
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_detector
\sa cbf_h5handle_set_detector
\sa cbf_h5handle_require_detector
\return An error code.
*/
int cbf_h5handle_get_detector
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
if (nx->num_detectors < 1 || nx->cur_detector >= nx->num_detectors) {
return CBF_NOTFOUND;
}
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->nxdetectors[nx->cur_detector])) *group = nx->nxdetectors[nx->cur_detector];
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->nxdetector_names[nx->cur_detector]) *name = nx->nxdetector_names[nx->cur_detector];
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the detector group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current detector group
\param name The name which the group should be given.
\sa cbf_h5handle_get_detector
\sa cbf_h5handle_set_detector
\sa cbf_h5handle_require_detector
\return An error code.
*/
int cbf_h5handle_set_detector
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int ii;
int match;
int error = CBF_SUCCESS;
void * nameblock;
nameblock = (void *)(nx->nxdetector_names);
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
return error;
}
/* See if there is a match on name */
match = 0;
for (ii = 0; ii < (int)(nx->num_detectors); ii++) {
if (!cbf_cistrcmp(name,nx->nxdetector_names[ii])) {
nx->cur_detector = ii;
match = 1;
break;
}
}
if (!match) {
/* There is no matching name, we need to find or create
a new slot */
if (nx->nxdetectors == NULL){
cbf_failnez(cbf_alloc(((void **) &(nx->nxdetectors)),NULL,
sizeof(hid_t),10));
cbf_onfailnez(cbf_alloc(((void **) &(nx->nxdetector_names)),NULL,
sizeof(char *),10),cbf_free(&(nameblock),NULL));
for (ii = 0; ii < 10; ii ++) {
nx->nxdetectors[ii] = (hid_t)CBF_H5FAIL;
nx->nxdetector_names[ii] = NULL;
}
nx->num_detectors = 0;
nx->cap_detectors = 10;
nx->cur_detector = 0;
}
if (nx->num_detectors >= nx->cap_detectors ) {
unsigned int newcap = 2*nx->cap_detectors;
unsigned int oldcap = nx->cap_detectors;
hid_t * oldnxdetectors = nx->nxdetectors;
const char ** oldnxdetector_names = nx->nxdetector_names;
void * oldnxdmap = (void *)nx->nxdetectors;
void * oldnxdnmap = (void *)nx->nxdetector_names;
cbf_failnez(cbf_alloc(((void **) &(nx->nxdetectors)),NULL,
sizeof(hid_t),newcap));
cbf_onfailnez(cbf_alloc(((void **) &(nx->nxdetector_names)),NULL,
sizeof(char *),newcap),cbf_free(&(nameblock),NULL));
for (ii = 0; ii < (int)newcap; ii ++) {
if (ii < (int)oldcap) {
nx->nxdetectors[ii] = oldnxdetectors[ii];
nx->nxdetector_names[ii] = oldnxdetector_names[ii];
} else {
nx->nxdetectors[ii] = (hid_t)CBF_H5FAIL;
nx->nxdetector_names[ii] = NULL;
}
}
nx->cap_detectors = newcap;
if (oldcap && oldnxdetectors) {
cbf_free(&oldnxdmap,NULL);
cbf_free(&oldnxdnmap,NULL);
}
}
nx->cur_detector = nx->num_detectors;
(nx->num_detectors)++;
}
{
hid_t * const nxGroup = &(nx->nxdetectors[nx->cur_detector]);
const char * * const nxName = &(nx->nxdetector_names[nx->cur_detector]);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the detector group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"detector"</code>.
\sa cbf_h5handle_get_detector
\sa cbf_h5handle_set_detector
\sa cbf_h5handle_require_detector
\return An error code.
*/
int cbf_h5handle_require_detector
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int ii;
unsigned int save_cur_detector;
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char default_name[] = "detector";
const char * group_name = name ? name : default_name;
hid_t test_group = CBF_H5FAIL;
hid_t cur_group = CBF_H5FAIL;
const char * cur_name = NULL;
int found;
CBF_UNUSED( cur_name );
if (nx->nxdetectors&&nx->num_detectors>0&&!name&&!group){
nx->cur_detector = 0;
return error;
}
if (nx->nxdetectors&&nx->num_detectors>0&&!name&&group&&!cbf_H5Ivalid(*group)){
*group = nx->nxdetectors[nx->cur_detector];
return error;
}
CBF_CALL(cbf_h5handle_get_instrument(nx,&parent,0));
/* check if the names of the groups match, and if the parent contains the assumed group */
if (error==CBF_SUCCESS &&((group!=NULL && cbf_H5Ivalid(*group)) || name!=NULL)) {
save_cur_detector = nx->cur_detector;
for (ii = 0; ii < (int)(nx->num_detectors); ii++) {
cur_group = nx->nxdetectors[ii];
cur_name = nx->nxdetector_names[ii];
if (nx->nxdetector_names[ii] && !cbf_cistrcmp(group_name,nx->nxdetector_names[ii])){
if (group == NULL || !cbf_H5Ivalid(*group)) {
nx->cur_detector = ii;
if (group) *group = cur_group;
return CBF_SUCCESS;
}
found = cbf_H5Gfind(parent,&test_group,group_name);
if (found == CBF_SUCCESS){
if (!cbf_H5Ocmp(test_group,curr_group)) {
nx->cur_detector = ii;
cbf_H5Gfree(test_group);
if (group) *group = cur_group;
return CBF_SUCCESS;
} else {
error |= found;
cbf_H5Gfree(test_group);
nx->cur_detector = save_cur_detector;
break;
}
}
}
}
}
/* One of the following holds:
1. We failed to find the instrument -- leave now with an error
2. We specified a group, but the name did not match
3. We specified a group, the name matched, but the group didn't
4. We specified a group that did not match
5. We did not specify a group
In cases 2 -- 5, we need to create a new detector group with the
name specified and add it as a new detector. If the same matches
it goes into that slot, otherwise into a new slot.
*/
if (CBF_SUCCESS==error) {
hid_t new_group = CBF_H5FAIL;
CBF_CALL(cbf_H5Grequire(parent,&new_group,group_name));
CBF_CALL(cbf_H5Arequire_string(new_group,"NX_class","NXdetector"));
CBF_CALL(cbf_h5handle_set_detector(nx,new_group,group_name));
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
CBF_CALL(cbf_h5handle_get_detector(nx,group,0));
}
return error;
}
/* Code to either generate an NXdata group data_... field name from the
array_id and binary_id, or to use the value associated with the
optional _array_data.nxdata_field_name tag, if provided.
The value provided in dataset name is always a new copy that will need to be
freed.
A similar link target path will be provided in nxdata_field_link_target_path,
which also will need to be freed.
Similarly, either the group name "data" is generated
the value associated with the optional _array_data.nxdata_group_name
is returned, but this value should not be freed.
If _array_data.nxdata_field_link_target_file or
_array_data.nxdata_field_link_target_path
are provided, their values are returned
If a binary value is available, the type string 'bnry' is
returned in *typeofvalue. */
static int cbf_get_nxdata_field_name2(const cbf_handle handle,
const char * arrayid,
const char * binaryid,
const int block,
const char ** datasetname,
const char ** groupname,
const char ** nxdata_field_link_target_file,
const char ** nxdata_field_link_target_path,
const char ** typeofvalue) {
unsigned int rows;
cbf_node * node;
unsigned int row;
const char * xarray_id;
const char * xbinary_id;
const char * nxdata_field_name = NULL;
const char * datasetname_parts[5]={NULL,NULL,NULL,NULL,NULL};
node = handle->node;
row = handle->row;
int foundrow = -1;
datasetname_parts[0] = "data";
datasetname_parts[1] = arrayid;
datasetname_parts[2] = binaryid;
datasetname_parts[3] = NULL;
datasetname_parts[4] = NULL;
char buffer[7];
cbf_debug_print4(" nxdata_field_name arrayid '%s' binaryid '%s' block %d",arrayid, binaryid, block);
if (!cbf_find_category(handle,"array_data") &&
!cbf_rewind_row(handle) &&
!cbf_count_rows(handle,&rows) && rows > 0) {
unsigned int row;
for (row = 0; row < rows; row++) {
cbf_debug_print4(" nxdata_field_name row %d, arrayid '%s' binaryid '%s'",row,arrayid, binaryid);
if (!cbf_find_column(handle,"array_id") &&
!cbf_select_row(handle,row) &&
!cbf_get_value(handle,&xarray_id) &&
xarray_id && !cbf_cistrcmp(arrayid,xarray_id) &&
!cbf_find_column(handle,"binary_id") &&
!cbf_get_value(handle,&xbinary_id) &&
xbinary_id && !cbf_cistrcmp(binaryid,xbinary_id)){
cbf_debug_print(" Found row ");
foundrow = row;
if (typeofvalue)
if(!(!cbf_find_column(handle,"data") &&
!cbf_get_typeofvalue(handle,typeofvalue) &&
(*typeofvalue)))
*typeofvalue = "null";
if (!(!cbf_find_column(handle,"nxdata_field_name") &&
!cbf_get_value(handle,&nxdata_field_name) &&
nxdata_field_name && nxdata_field_name[0] &&
cbf_cistrcmp(nxdata_field_name,".") &&
cbf_cistrcmp(nxdata_field_name,"?")))
nxdata_field_name = NULL;
else {
cbf_debug_print2("nxdata_field_name = '%s'\n",nxdata_field_name);
}
if (groupname)
if (!(!cbf_find_column(handle,"nxdata_group_name") &&
!cbf_get_value(handle,groupname) &&
*groupname &&
(*groupname)[0] &&
cbf_cistrcmp(*groupname,".") &&
cbf_cistrcmp(*groupname,"?")))
*groupname = "data";
if (nxdata_field_link_target_file)
if (!(!cbf_find_column(handle,"nxdata_field_link_target_file") &&
!cbf_get_value(handle,nxdata_field_link_target_file) &&
*nxdata_field_link_target_file &&
(*nxdata_field_link_target_file)[0] &&
cbf_cistrcmp(*nxdata_field_link_target_file,".") &&
cbf_cistrcmp(*nxdata_field_link_target_file,"?")))
*nxdata_field_link_target_file = NULL;
if (nxdata_field_link_target_path)
if (!(!cbf_find_column(handle,"nxdata_field_link_target_path") &&
!cbf_get_value(handle,nxdata_field_link_target_path) &&
*nxdata_field_link_target_path &&
(*nxdata_field_link_target_path)[0] &&
cbf_cistrcmp(*nxdata_field_link_target_path,".") &&
cbf_cistrcmp(*nxdata_field_link_target_path,"?")))
*nxdata_field_link_target_path = NULL;
break;
} else {
nxdata_field_name = NULL;
if (groupname) *groupname="data";
if (nxdata_field_link_target_file)
*nxdata_field_link_target_file = NULL;
if (nxdata_field_link_target_path)
*nxdata_field_link_target_path = NULL;
if (typeofvalue)
*typeofvalue = "null";
}
}
}
if (nxdata_field_name) {
*datasetname = _cbf_strdup(nxdata_field_name);
} else {
nxdata_field_name = NULL;
if (groupname) *groupname="data";
if (nxdata_field_link_target_file)
*nxdata_field_link_target_file = NULL;
if (nxdata_field_link_target_path)
*nxdata_field_link_target_path = NULL;
if (foundrow >= 0) {
if (block > 0) {
sprintf(buffer,"%06d",block);
datasetname_parts[3]=buffer;
}
*datasetname = _cbf_str_join(datasetname_parts,'_');
} else {
*datasetname = _cbf_strdup("data");
if (typeofvalue &&
!cbf_find_category(handle,"array_data") &&
!cbf_rewind_row(handle) &&
!cbf_count_rows(handle,&rows) && rows > 0 ) {
if(!(!cbf_find_column(handle,"data") &&
!cbf_get_typeofvalue(handle,typeofvalue) &&
(*typeofvalue))) {
*typeofvalue = "null";
}
}
}
}
if (*datasetname && nxdata_field_link_target_path && !(*nxdata_field_link_target_path)) {
const char * target_path_parts[5] = {
"",
"entry",
"data",
*datasetname,
NULL
};
*nxdata_field_link_target_path = _cbf_str_join(target_path_parts,'/');
}
cbf_debug_print2("datasetname = '%s'\n", *datasetname);
cbf_debug_print2("link_target_path = '%s'\n", *nxdata_field_link_target_path);
handle->node = node;
handle->row = row;
return CBF_SUCCESS;
}
/*
Decompress the data selected in the handle, ensure an appropriate
HDF5 dataset exists to store it, insert it at the given index with
some parameter values set according to the given flags.
Writes saturation_value, undefined_value and data to parent if
requested.
*/
static int cbf_write_array_h5file2(const cbf_handle handle,
const cbf_h5handle h5handle,
const char * arrayid,
const char * binaryid,
const char * saturation_value,
const char * undefined_value,
const char * details,
const char * gain,
const char * gain_esd,
const char * linearity,
const char * offset,
const char * scaling,
const hid_t parent)
{
int error = CBF_SUCCESS;
cbf_node * node;
unsigned int row;
int bits=0, sign=0, real=0;
size_t rank = 0;
hid_t dset = CBF_H5FAIL;
unsigned int elsize=0;
hid_t h5type = CBF_H5FAIL;
const char *byteorder=NULL;
const char * datasetname=NULL;
const char * binval = NULL;
const char * nxdata_field_link_target_file = NULL;
const char * nxdata_field_link_target_path = NULL;
const char * typeofvalue;
hsize_t h5offset[4];
const int sig[] = {1};
int sigbuf[] = {0};
int ii;
h5offset[0] = h5handle->slice;
h5offset[1] = h5offset[2] = h5offset[3] = 0;
node = handle->node;
row = handle->row;
cbf_debug_print("write_array_h5file2\n");
cbf_get_nxdata_field_name2(handle, arrayid, binaryid,
h5handle->block,
&datasetname,
0,
&nxdata_field_link_target_file,
&nxdata_field_link_target_path,
&typeofvalue);
cbf_debug_print2("cbf_write_array_h5file2 nxdata_field_link_target_file = %s",nxdata_field_link_target_file);
cbf_debug_print2("cbf_write_array_h5file2 nxdata_field_link_target_path = %s",nxdata_field_link_target_path);
cbf_debug_print2("cbf_write_array_h5file2 typeofvalue = %s",typeofvalue);
if (!node) {
cbf_debug_print("Invalid node given\n");
error |= CBF_ARGUMENT;
} else if (!h5handle) {
cbf_debug_print("Invalid hdf5 handle given\n");
error |= CBF_ARGUMENT;
} else if (datasetname){
int found = CBF_SUCCESS, id = 0;
cbf_file *file=NULL;
long start=0;
size_t size=0, nelem=0, padding=0;
unsigned int compression=0;
CBF_CALL(cbf_get_array_section_rank(handle,arrayid,&rank));
if (!cbf_cistrcmp(typeofvalue,"bnry") && cbf_is_binary(node,row)) {
CBF_CALL(cbf_get_columnrow (&binval, node, row));
} else {
binval = NULL;
}
if (!binval){
cbf_debug_print2(" non-binary value %s",typeofvalue);
CBF_START_ARRAY(hsize_t,buf,(rank+1));
CBF_START_ARRAY(hsize_t,h5dim,(rank+1));
CBF_START_ARRAY(size_t,cbfdim,rank);
CBF_START_ARRAY(hsize_t,h5max,(rank+1));
CBF_START_ARRAY(hsize_t,h5chunk,(rank+1));
if (rank > 3) return CBF_FORMAT;
CBF_CALL(cbf_get_array_section_size(handle,arrayid,rank,cbfdim));
CBF_CALL(cbf_get_array_section_type(handle,arrayid,&bits,&sign,&real));
elsize = (bits+7)/8;
CBF_CALL(cbf_find_array_data_h5type(&h5type,bits,sign,real,"little-endian"));
binval = "";
buf[0] = 0;
h5dim[0] = 0;
h5max[0] = H5S_UNLIMITED;
h5chunk[0] = 1;
for (ii = 0; ii < (int)rank; ii ++){
h5dim[ii+1] = h5max[ii+1] = h5chunk[ii+1] = cbfdim[rank-ii-1];
buf[ii] = 0;
}
if (nxdata_field_link_target_file) {
if(!nxdata_field_link_target_path)
nxdata_field_link_target_path ="/entry/data/data";
cbf_h5reportneg(H5Lcreate_external( nxdata_field_link_target_file,
nxdata_field_link_target_path,
parent,datasetname,
H5P_DEFAULT,H5P_DEFAULT),CBF_H5ERROR,error);
} else if (cbf_H5Dfind2(parent,
&dset,datasetname,rank+1,h5max,buf,h5type)) {
/* define variables & check args */
hid_t dataSpace = CBF_H5FAIL;
hid_t valprop = H5Pcreate(H5P_DATASET_CREATE);
/* check variables are valid */
CBF_CALL(cbf_H5Screate(&dataSpace, rank+1, h5dim, h5max));
/* allow dataset to be chunked */
CBF_H5CALL(H5Pset_chunk(valprop,rank+1,h5chunk));
/* allow compression */
if (CBF_SUCCESS==error) {
if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_ZLIB) {
H5Pset_deflate(valprop, 1);
} else if (h5handle->flags & CBF_H5COMPRESSION_CBF) {
unsigned int cd_values[CBF_H5Z_FILTER_CBF_NELMTS];
cd_values[CBF_H5Z_FILTER_CBF_COMPRESSION] =
h5handle->flags &
(CBF_COMPRESSION_MASK|CBF_FLAG_MASK) &
(~(CBF_H5COMPRESSION_CBF));
cd_values[CBF_H5Z_FILTER_CBF_RESERVED] = 0;
cd_values[CBF_H5Z_FILTER_CBF_BINARY_ID] = id;
cd_values[CBF_H5Z_FILTER_CBF_PADDING] = padding;
cd_values[CBF_H5Z_FILTER_CBF_ELSIZE] = elsize;
cd_values[CBF_H5Z_FILTER_CBF_ELSIGN] = sign;
cd_values[CBF_H5Z_FILTER_CBF_REAL] = real;
cd_values[CBF_H5Z_FILTER_CBF_DIMFAST] = *(h5chunk+rank);
if (rank == 3) {
cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = *(h5chunk+2);
cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = *(h5chunk+1);
} else if (rank == 2) {
cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = *(h5chunk+1);
cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = 1;
} else if (rank == 2) {
cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = 1;
cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = 1;
}
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_CBF)) {
cbf_h5reportneg(H5Zregister(CBF_H5Z_CBF),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_CBF (32006) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_CBF, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_CBF_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_CBF %d\n",error);
}
}
#ifdef CBF_H5Z_USE_LZ4
else if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4 ||
(h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4_2) {
unsigned int cd_values[CBF_H5Z_FILTER_LZ4_NELMTS];
cd_values[CBF_H5Z_FILTER_LZ4_BLOCKSIZE] = 1<<30;
cd_values[CBF_H5Z_FILTER_LZ4_THREADS] = 1;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_LZ4)) {
cbf_h5reportneg(H5Zregister(H5Z_LZ4),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_LZ4 (32004) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_LZ4, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_LZ4_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_LZ4 %d\n",error);
}
if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4_2) {
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_LZ4, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_LZ4_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_LZ4 %d\n",error);
}
}
}
#endif
#ifdef CBF_H5Z_USE_BSHUF
else if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_BSLZ4 ) {
unsigned int cd_values[CBF_H5Z_FILTER_BSHUF_NELMTS];
cd_values[CBF_H5Z_FILTER_BSHUF_BLOCKSIZE] = 8192/elsize;
cd_values[CBF_H5Z_FILTER_BSHUF_COMPRESSION] =
CBF_H5Z_FILTER_BSHUF_LZ4;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_BSHUF)) {
cbf_h5reportneg(H5Zregister(bshuf_H5Filter),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_BSHUF (32008) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_BSHUF, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_BSHUF_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_BSHUF %d\n",error);
}
}
}
#endif
}
/* create the dataset */
if (CBF_SUCCESS == error)
dset = H5Dcreate2(parent,datasetname,h5type,dataSpace,H5P_DEFAULT,valprop,H5P_DEFAULT);
/* check local variables are properly closed */
if (cbf_H5Ivalid(dataSpace)) H5Sclose(dataSpace);
if (cbf_H5Ivalid(valprop)) H5Pclose(valprop);
}
if (binval && cbf_cistrcmp(binval,".")){
CBF_CALL(cbf_add_h5text_attribute_slab(dset, parent,
"non_binary_value",
arrayid,
NULL, binval,
(hsize_t) (h5handle->slice),
error));
}
CBF_END_ARRAY_REPORTNEZ(h5chunk,error);
CBF_END_ARRAY_REPORTNEZ(h5max,error);
CBF_END_ARRAY_REPORTNEZ(cbfdim,error);
CBF_END_ARRAY_REPORTNEZ(h5dim,error);
CBF_END_ARRAY_REPORTNEZ(buf,error);
} else {
cbf_debug_print(" binary value");
CBF_START_ARRAY(hsize_t,buf,(rank+1));
CBF_START_ARRAY(hsize_t,h5dim,(rank+1));
CBF_START_ARRAY(size_t,cbfdim,rank+1);
CBF_START_ARRAY(hsize_t,h5max,(rank+1));
CBF_START_ARRAY(hsize_t,h5chunk,(rank+1));
/* find the datatype and array size */
CBF_CALL(cbf_get_bintext(node, row, NULL,
&id, &file, &start, &size,
NULL, NULL, &bits, &sign, &real,
&byteorder, &nelem, NULL, NULL, NULL, &padding,
&compression));
CBF_CALL(cbf_find_array_data_h5type(&h5type,bits,sign,real,byteorder));
elsize = (bits+7)/8;
CBF_CALL(cbf_get_array_section_size(handle,arrayid,rank,cbfdim));
for (ii = 0; ii < (int)rank; ii ++){
h5dim[ii+1] = h5max[ii+1] = h5chunk[ii+1] = cbfdim[rank-ii-1];
buf[ii] = 0;
}
buf[0] = 0;
h5chunk[0] = 1;
h5dim[0] = 1;
h5max[0] = H5S_UNLIMITED;
/* allocate space for the decompressed data */
if (CBF_SUCCESS == error) {
void * value;
const unsigned int elsize = (bits+7)/8;
size_t nelem_read = 0;
value = malloc(nelem*elsize);
CBF_CALL(cbf_set_fileposition(file, start, SEEK_SET));
CBF_CALL(cbf_decompress_parameters(NULL, NULL, NULL, NULL, NULL, NULL, NULL, compression, file));
{
cbf_debug_print2("masked compression: %d\n",compression&CBF_COMPRESSION_MASK);
cbf_debug_print("compression type: ");
if (compression == CBF_CANONICAL) {
cbf_debug_print("CBF_CANONICAL\n");
} else if ((compression&CBF_COMPRESSION_MASK) == CBF_PACKED){
cbf_debug_print("CBF_PACKED\n");
} else if ((compression&CBF_COMPRESSION_MASK) == CBF_PACKED_V2) {
cbf_debug_print("CBF_PACKED_V2\n");
} else if (compression == CBF_BYTE_OFFSET) {
cbf_debug_print("CBF_BYTE_OFFSET\n");
} else if (compression == CBF_NIBBLE_OFFSET) {
cbf_debug_print("CBF_NIBBLE_OFFSET\n");
} else if (compression == CBF_PREDICTOR) {
cbf_debug_print("CBF_PREDICTOR\n");
} else if (compression == CBF_NONE) {
cbf_debug_print("CBF_NONE\n");
} else {
cbf_debug_print("Unknown\n");
}
cbf_debug_print2("element size: %d\n",(unsigned int)(elsize));
cbf_debug_print2("real?: %s\n",real?"yes":"no");
}
/* ensure a dataset exists in the detector */
found = cbf_H5Dfind2(parent,
&dset,datasetname,rank+1,h5max,buf,h5type);
if (CBF_SUCCESS==found) {
} else if (CBF_NOTFOUND==found) {
/* define variables & check args */
hid_t dataSpace = CBF_H5FAIL;
hid_t valprop = H5Pcreate(H5P_DATASET_CREATE);
/* check variables are valid */
CBF_CALL(cbf_H5Screate(&dataSpace, rank+1, h5dim, h5max));
/* allow dataset to be chunked */
CBF_H5CALL(H5Pset_chunk(valprop,rank+1,h5chunk));
/* allow compression */
if (CBF_SUCCESS==error) {
if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_ZLIB) {
H5Pset_deflate(valprop, 1);
} else if (h5handle->flags & CBF_H5COMPRESSION_CBF) {
unsigned int cd_values[CBF_H5Z_FILTER_CBF_NELMTS];
cd_values[CBF_H5Z_FILTER_CBF_COMPRESSION] =
h5handle->flags &
(CBF_COMPRESSION_MASK|CBF_FLAG_MASK) &
(~(CBF_H5COMPRESSION_CBF));
cd_values[CBF_H5Z_FILTER_CBF_RESERVED] = 0;
cd_values[CBF_H5Z_FILTER_CBF_BINARY_ID] = id;
cd_values[CBF_H5Z_FILTER_CBF_PADDING] = padding;
cd_values[CBF_H5Z_FILTER_CBF_ELSIZE] = elsize;
cd_values[CBF_H5Z_FILTER_CBF_ELSIGN] = sign;
cd_values[CBF_H5Z_FILTER_CBF_REAL] = real;
cd_values[CBF_H5Z_FILTER_CBF_DIMFAST] = *(h5chunk+rank);
if (rank == 3) {
cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = *(h5chunk+2);
cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = *(h5chunk+1);
} else if (rank == 2) {
cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = *(h5chunk+1);
cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = 1;
} else if (rank == 2) {
cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = 1;
cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = 1;
}
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_CBF)) {
cbf_h5reportneg(H5Zregister(CBF_H5Z_CBF),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_CBF (32006) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_CBF, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_CBF_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_CBF %d\n",error);
}
}
#ifdef CBF_H5Z_USE_LZ4
else if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4 ||
(h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4_2) {
unsigned int cd_values[CBF_H5Z_FILTER_LZ4_NELMTS];
cd_values[CBF_H5Z_FILTER_LZ4_BLOCKSIZE] = 1<<30;
cd_values[CBF_H5Z_FILTER_LZ4_THREADS] = 1;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_LZ4)) {
cbf_h5reportneg(H5Zregister(H5Z_LZ4),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_LZ4 (32004) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_LZ4, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_LZ4_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_LZ4 %d\n",error);
}
if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4_2) {
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_LZ4, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_LZ4_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_LZ4 %d\n",error);
}
}
}
#endif
#ifdef CBF_H5Z_USE_BSHUF
else if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_BSLZ4 ) {
unsigned int cd_values[CBF_H5Z_FILTER_BSHUF_NELMTS];
cd_values[CBF_H5Z_FILTER_BSHUF_BLOCKSIZE] = 8192/elsize;
cd_values[CBF_H5Z_FILTER_BSHUF_COMPRESSION] =
CBF_H5Z_FILTER_BSHUF_LZ4;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_BSHUF)) {
cbf_h5reportneg(H5Zregister(bshuf_H5Filter),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_BSHUF (32008) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_BSHUF, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_BSHUF_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_BSHUF %d\n",error);
}
}
}
#endif
}
/* create the dataset */
if (CBF_SUCCESS == error) {
dset = H5Dcreate2(parent,datasetname,h5type,dataSpace,H5P_DEFAULT,valprop,H5P_DEFAULT);
cbf_debug_print2("creating dataset '%s'",datasetname);
} else {
cbf_debug_print2("unable to create dataset, prior error %x", error);
}
/* check local variables are properly closed */
if (cbf_H5Ivalid(dataSpace)) H5Sclose(dataSpace);
if (cbf_H5Ivalid(valprop)) H5Pclose(valprop);
} else {
error |= found;
cbf_debug_print2("error locating primary dataset: %s\n", cbf_strerror(found));
}
if (CBF_SUCCESS==error) {
/* extract the image data from CBF */
CBF_CALL(cbf_decompress(value, elsize, sign, nelem, &nelem_read,
size, compression, bits, sign, file, real, byteorder,
nelem, cbfdim[2], cbfdim[1], cbfdim[0], padding));
if (nelem_read != nelem) error |= CBF_ENDOFDATA;
/* store the image data in HDF5 */
CBF_CALL(cbf_H5Dinsert(dset,h5offset,0,h5chunk,buf,value,h5type));
free((void*)value);
}
}
CBF_END_ARRAY_REPORTNEZ(h5chunk,error);
CBF_END_ARRAY_REPORTNEZ(h5max,error);
CBF_END_ARRAY_REPORTNEZ(cbfdim,error);
CBF_END_ARRAY_REPORTNEZ(h5dim,error);
CBF_END_ARRAY_REPORTNEZ(buf,error);
}
if (CBF_SUCCESS==error) {
const char* datasetnamesuffix;
char datasetnameprefix[6];
int wsvasstr, wuvasstr, wsvasdbl, wuvasdbl, wsvaslong, wuvaslong;
double svasdbl, uvasdbl;
long svaslong, uvaslong;
htri_t svexists, uvexists;
int errorsave;
char * endptr;
CBF_UNUSED( wuvaslong );
CBF_UNUSED( wsvaslong );
_cbf_strncpy(datasetnameprefix,datasetname,5);
datasetnameprefix[5] = '\0';
if (!cbf_cistrcmp(datasetnameprefix,"data_")) {
datasetnamesuffix = datasetname+5;
} else {
datasetnamesuffix = datasetname;
}
errorsave = error;
if (cbf_apply_h5text_list_attribute(parent,"signal",1,&datasetname,error)) {
error = errorsave;
cbf_reportnez(cbf_add_h5text_list_attribute_slab(parent,
"signal",datasetname,
(hsize_t) (h5handle->slice),error),error);
}
cbf_reportnez(CBFM_H5Arequire_cmp2(dset,"signal",0,0,H5T_STD_I32LE,H5T_NATIVE_INT,sig,sigbuf,cmp_int,0),error);
/* The saturation_value and undefined_value need to be inserted as datasets
If we do not have a binary value, or if the values given are not numeric,
they will be inserted as text. If we do have a binary value they will
be inserted as numeric. In all cases, because these are fixed properties
if the detector, rather than of the image, they will be inserted as a
scalar value, unless there is an existing link of that name, in which
case, no action will be taken */
wsvasstr = 0;
wuvasstr = 0;
wsvasdbl = 0;
wuvasdbl = 0;
wsvaslong = 0;
wuvaslong = 0;
if (saturation_value) {
svexists = H5Lexists(parent,"saturation_value",H5P_DEFAULT);
if (binval) {
if (real) {
svasdbl = strtod(saturation_value,&endptr);
if (endptr == saturation_value || *endptr != '\0') {
wsvasstr = 1;
} else {
wsvasdbl = 1;
}
} else {
svaslong = strtol(saturation_value,&endptr,10);
if (endptr == saturation_value || *endptr != '\0') {
wsvasstr = 1;
} else {
wsvaslong = 1;
}
}
} else {
wsvasstr = 1;
}
if ( svexists == 0 ) {
if (wsvasstr) {
cbf_reportnez(cbf_add_h5text_dataset(parent,"saturation_value",saturation_value,error),error);
} else if (wsvasdbl) {
cbf_reportnez(cbf_add_h5double_dataset_slab(parent,"saturation_value",svasdbl,(hsize_t)0, error),error);
} else {
cbf_reportnez(cbf_add_h5long_dataset_slab(parent,"saturation_value",svaslong,(hsize_t)0, error), error);
}
} else {
cbf_debug_print2 ("redundant saturation_value '%s'",saturation_value);
}
}
if (undefined_value) {
uvexists = H5Lexists(parent,"undefined_value",H5P_DEFAULT);
if (binval) {
if (real) {
uvasdbl = strtod(undefined_value,&endptr);
if (endptr == undefined_value || *endptr != '\0') {
wuvasstr = 1;
} else {
wuvasdbl = 1;
}
} else {
uvaslong = strtol(undefined_value,&endptr,10);
if (endptr == undefined_value || *endptr != '\0') {
wuvasstr = 1;
} else {
wuvaslong = 1;
}
}
} else {
wuvasstr = 1;
}
if ( uvexists == 0 ) {
if (wuvasstr) {
cbf_reportnez(cbf_add_h5text_dataset(parent,"undefined_value",saturation_value,error),error);
} else if (wuvasdbl) {
cbf_reportnez(cbf_add_h5double_dataset_slab(parent,"undefined_value",uvasdbl,(hsize_t)0,error),error);
} else {
cbf_reportnez(cbf_add_h5long_dataset_slab(parent,"undefined_value",uvaslong,(hsize_t)0,error),error);
}
} else {
cbf_debug_print2 ("redundant undefined_value '%s'",undefined_value);
}
}
if (arrayid){
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"array_id",
arrayid,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (binaryid){
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"binary_id",
binaryid,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (details) {
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"details",
details,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (gain) {
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"gain",
gain,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (gain_esd) {
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"gain_esd",
gain_esd,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (linearity) {
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"linearity",
linearity,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (offset) {
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"offset",
offset,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (scaling) {
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"scaling",
scaling,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (saturation_value) {
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"overload",
saturation_value,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
if (undefined_value) {
cbf_reportnez(cbf_add_h5text_attribute_slab(dset, parent,
"undefined_value",
undefined_value,
NULL, datasetnamesuffix,
(hsize_t) (h5handle->slice),
error),error);
}
cbf_free_text(&datasetname,NULL);
datasetname = NULL;
cbf_H5Dfree(dset);
dset = CBF_H5ERROR;
}
}
if (datasetname) cbf_free_text(&datasetname, NULL);
if (cbf_H5Ivalid(dset)) cbf_H5Dfree(dset);
return error;
}
/* Find row by multiple columns
n is the number of columns
column is an array of n column names
value is an array of values to match
*/
static int cbf_find_row_by_columns(cbf_handle handle, int n,
const char * column[],
const char * value[]){
int icol;
int match;
const char *colvalue;
if (!handle || n < 1
|| !column || !value
|| !column[0] || !value[0]) return CBF_ARGUMENT;
cbf_failnez(cbf_rewind_row(handle));
while (!cbf_find_column(handle,column[0])
&&!cbf_find_nextrow(handle,value[0])) {
match = 1;
for (icol = 1; icol < n; icol++) {
if ( !column[icol] || !value[icol] ) return CBF_ARGUMENT;
cbf_failnez(cbf_find_column(handle,column[icol]));
if (cbf_get_value(handle,&colvalue)
|| !colvalue
|| cbf_cistrcmp(colvalue,value[icol])) {
match = 0;
break;
}
}
if (match) return CBF_SUCCESS;
}
return CBF_NOTFOUND;
}
/* Process the array id related fields for a given nxdata and nxdetector
*/
static int cbf_require_nxarrayid(const cbf_handle handle,
const cbf_h5handle h5handle,
const char * array_id,
const char * binary_id,
hid_t nxdata, hid_t nxdetector) {
const char * axis_set_id = NULL;
const char * axis_id = NULL;
const char * xarray_id = NULL;
const char * xarray_section_id = NULL;
const char * axis_indices = NULL;
const char * axis_path = NULL;
const char * axis_indices_parts[3] = {NULL, NULL, NULL};
hid_t dsetid;
hid_t nxdetector_module;
hid_t nxdetector_module_prev;
const char * datasetname = NULL;
htri_t alexists;
int error = 0;
unsigned int row, rows, ii;
size_t rank = 0;
int array_id_found, array_section_id_found;
int precedence;
const char * nxdata_field_link_target_file = NULL;
const char * nxdata_field_link_target_path = NULL;
if (!handle
|| !h5handle
|| !array_id ) return CBF_ARGUMENT;
cbf_debug_print3("Entering cbf_require_nxarryid, array_id %s, binary_id %s", array_id, binary_id);
dsetid = CBF_H5FAIL;
if (cbf_H5Ivalid(nxdata)) {
cbf_get_nxdata_field_name2(handle,array_id,binary_id,
h5handle->block,
&datasetname,
0,
&nxdata_field_link_target_file,
&nxdata_field_link_target_path, 0);
cbf_debug_print2("opening dataset '%s'",datasetname);
dsetid = H5Dopen2(nxdata,datasetname,H5P_DEFAULT);
cbf_free_text(&datasetname,NULL);
}
if (!cbf_get_array_section_array_id(handle,array_id,&xarray_id)
&& xarray_id
&& cbf_H5Ivalid(nxdetector)) {
if (cbf_cistrcmp(array_id,xarray_id)) {
/* array_id is an array_section and xarray_id is an array */
cbf_reportnez(cbf_H5Grequire(nxdetector,&nxdetector_module_prev,xarray_id),error);
cbf_reportnez(cbf_H5Arequire_string(nxdetector_module_prev,"NX_class","NXdetector_module"),error);
cbf_reportnez(cbf_H5Grequire(nxdetector_module_prev,&nxdetector_module,array_id),error);
cbf_reportnez(cbf_H5Arequire_string(nxdetector_module,"NX_class","NXdetector_module"),error);
} else {
/* array_id is an array and xarray_id is the same array */
nxdetector_module_prev = -1;
cbf_reportnez(cbf_H5Grequire(nxdetector,&nxdetector_module,array_id),error);
cbf_reportnez(cbf_H5Arequire_string(nxdetector_module,"NX_class","NXdetector_module"),error);
}
cbf_reportnez(cbf_get_array_section_rank(handle,array_id,&rank),error);
if (rank) {
/* If this array_id has a rank, prepare the detector_module for
this array section or array */
hsize_t chunk[1];
hsize_t max[1];
hsize_t offset[1];
hsize_t count[1];
hsize_t buf[1];
hid_t dset = CBF_H5FAIL;
hid_t datasetid = CBF_H5FAIL;
CBF_START_ARRAY(size_t, cbf_dims, rank);
CBF_START_ARRAY(hsize_t, dims, rank);
CBF_START_ARRAY(size_t, cbf_origins,rank);
CBF_START_ARRAY(hsize_t, origins, rank);
CBF_START_ARRAY(long, cbf_strides,rank);
CBF_START_ARRAY(hssize_t,strides, rank);
CBF_START_ARRAY(double, psizes, rank);
cbf_reportnez(cbf_get_array_section_pixel_sizes(handle,array_id,rank,psizes),error);
if (!error && psizes[0] > 0.) {
const char * xpixelsizepath;
const char * x_pixel_size_parts[3];
const char * fastpixelsizepath = "fast_pixel_size";
htri_t dsexists;
x_pixel_size_parts[0] = "x_pixel_size";
x_pixel_size_parts[1] = array_id;
x_pixel_size_parts[2] = 0;
xpixelsizepath = _cbf_str_join(x_pixel_size_parts,'_');
dsexists = H5Lexists(nxdetector,xpixelsizepath,H5P_DEFAULT);
if (dsexists < 0 || !dsexists) {
cbf_reportnez(cbf_add_h5double_dataset(nxdetector,
xpixelsizepath,
psizes[0],
error),error);
cbf_h5reportneg(datasetid = H5Dopen2(nxdetector,
xpixelsizepath, H5P_DEFAULT),CBF_H5ERROR,error);
cbf_reportnez(cbf_apply_h5text_attribute(datasetid,
"units",
"mm",error),error);
cbf_h5reportneg(H5Dclose(datasetid),CBF_H5ERROR,error);
}
cbf_free((void **)&xpixelsizepath,NULL);
dsexists = H5Lexists(nxdetector_module,fastpixelsizepath,H5P_DEFAULT);
if (dsexists < 0 || !dsexists) {
error |= cbf_add_h5double_dataset(nxdetector_module,
fastpixelsizepath,
psizes[0],
error);
cbf_h5reportneg(datasetid = H5Dopen2(nxdetector_module,
fastpixelsizepath, H5P_DEFAULT),CBF_H5ERROR,error);
cbf_reportnez(cbf_apply_h5text_attribute(datasetid,
"units",
"mm",error),error);
cbf_h5reportneg(H5Dclose(datasetid),CBF_H5ERROR,error);
}
}
if (!error && psizes[1] > 0. && rank > 1) {
const char * ypixelsizepath;
const char * y_pixel_size_parts[3];
const char * slowpixelsizepath = "slow_pixel_size";
htri_t dsexists;
y_pixel_size_parts[0] = "y_pixel_size";
y_pixel_size_parts[1] = array_id;
y_pixel_size_parts[2] = 0;
ypixelsizepath = _cbf_str_join(y_pixel_size_parts,'_');
dsexists = H5Lexists(nxdetector,ypixelsizepath,H5P_DEFAULT);
if (dsexists < 0 || !dsexists) {
error |= cbf_add_h5double_dataset(nxdetector,
ypixelsizepath,
psizes[1],
error);
cbf_h5reportneg(datasetid = H5Dopen2(nxdetector,
ypixelsizepath, H5P_DEFAULT),CBF_H5ERROR,error);
cbf_reportnez(cbf_apply_h5text_attribute(datasetid,
"units",
"mm",error),error);
cbf_h5reportneg(H5Dclose(datasetid),CBF_H5ERROR,error);
}
cbf_free((void **)&ypixelsizepath,NULL);
dsexists = H5Lexists(nxdetector_module,slowpixelsizepath,H5P_DEFAULT);
if (dsexists < 0 || !dsexists) {
error |= cbf_add_h5double_dataset(nxdetector_module,
slowpixelsizepath,
psizes[1],
error);
cbf_h5reportneg(datasetid = H5Dopen2(nxdetector_module,
slowpixelsizepath, H5P_DEFAULT),CBF_H5ERROR,error);
cbf_reportnez(cbf_apply_h5text_attribute(datasetid,
"units",
"mm",error),error);
cbf_h5reportneg(H5Dclose(datasetid),CBF_H5ERROR,error);
}
}
error |= cbf_get_array_section_sizes(handle,array_id,rank,cbf_dims,cbf_origins,cbf_strides);
chunk[0] = rank;
max[0] = rank;
offset[0] = 0;
count[0] = rank;
strides[0] = 1;
for (ii = 0; ii < rank; ii++) {
origins[ii] = cbf_origins[ii]-1;
dims[ii] = cbf_dims[ii];
strides[ii] = cbf_strides[ii];
if (strides[ii] == 0) strides[ii] = 1;
}
cbf_reportnez(cbf_H5Drequire(nxdetector_module,&dset,"data_size",1,max,chunk,buf,H5T_STD_U64LE),error);
cbf_reportnez(cbf_H5Dinsert(dset,offset,NULL,count,buf,dims,H5T_NATIVE_HSIZE),error);
cbf_reportnez(cbf_H5Drequire(nxdetector_module,&dset,"data_origin",1,max,chunk,buf,H5T_STD_U64LE),error);
cbf_reportnez(cbf_H5Dinsert(dset,offset,NULL,count,buf,origins,H5T_NATIVE_HSIZE),error);
cbf_reportnez(cbf_H5Drequire(nxdetector_module,&dset,"data_stride",1,max,chunk,buf,H5T_STD_I64LE),error);
cbf_reportnez(cbf_H5Dinsert(dset,offset,NULL,count,buf,strides,H5T_NATIVE_HSSIZE),error);
if (cbf_H5Ivalid (dset)) {
cbf_h5reportneg(H5Dclose(dset),CBF_FORMAT,error);
}
if (cbf_H5Ivalid(nxdetector_module)) {
cbf_h5reportneg(H5Gclose(nxdetector_module),CBF_FORMAT,error);
}
if (cbf_H5Ivalid(nxdetector_module_prev)) {
cbf_h5reportneg(H5Gclose(nxdetector_module_prev),CBF_FORMAT,error);
}
cbf_debug_print3("cbf_require_nxarrayid, array_id %s xarray_id %s", array_id, xarray_id);
if (!cbf_cistrcmp(array_id,xarray_id)) {
/* This array section is actually an array. We need to create
a detector module for each associated array_section, one level
down */
if (!cbf_find_category(handle,"array_structure_list_section")
&& !cbf_find_column(handle,"array_id")
&& !cbf_rewind_row(handle)
&& !cbf_find_row(handle,xarray_id)
&& !cbf_count_rows(handle,&rows)){
for (row = 0; row < rows; row++) {
const char * yarray_id;
const char * array_section_id;
htri_t dsexists;
if (!cbf_find_category(handle,"array_structure_list_section")
&& !cbf_find_column(handle,"array_id")
&& !cbf_select_row(handle,row)
&& !cbf_get_value(handle,&yarray_id)
&& yarray_id
&& !cbf_cistrcmp(xarray_id,yarray_id)
&& !cbf_find_column(handle,"id")
&& !cbf_get_value(handle,&array_section_id)
&& array_section_id
&& ((dsexists = H5Lexists(nxdetector,array_section_id,H5P_DEFAULT))==0
|| dsexists < 0)) {
cbf_reportnez(cbf_H5Grequire(nxdetector,&nxdetector_module_prev,xarray_id),error);
cbf_reportnez(cbf_H5Grequire(nxdetector_module_prev,&nxdetector_module,array_section_id),error);
cbf_reportnez(cbf_H5Arequire_string(nxdetector_module,"NX_class","NXdetector_module"),error);
cbf_reportnez(cbf_get_array_section_rank(handle,array_section_id,&rank),error);
if (rank) {
/* If this array_id has a rank, prepare the detector_module for
this array section or array */
hsize_t chunk[1];
hsize_t max[1];
hsize_t offset[1];
hsize_t count[1];
hsize_t buf[1];
hid_t dset = CBF_H5FAIL;
CBF_START_ARRAY(size_t, cbf_dims, rank);
CBF_START_ARRAY(hsize_t, dims, rank);
CBF_START_ARRAY(size_t, cbf_origins,rank);
CBF_START_ARRAY(hsize_t, origins, rank);
CBF_START_ARRAY(long, cbf_strides,rank);
CBF_START_ARRAY(hssize_t,strides, rank);
CBF_START_ARRAY(double, psizes, rank);
cbf_reportnez(cbf_get_array_section_pixel_sizes(handle,array_section_id,rank,psizes),error);
if (!error && psizes[0] > 0.) {
const char * xpixelsizepath;
const char * x_pixel_size_parts[3];
const char * fastpixelsizepath = "fast_pixel_size";
htri_t dsexists;
x_pixel_size_parts[0] = "x_pixel_size";
x_pixel_size_parts[1] = array_id;
x_pixel_size_parts[2] = 0;
xpixelsizepath = _cbf_str_join(x_pixel_size_parts,'_');
dsexists = H5Lexists(nxdetector,xpixelsizepath,H5P_DEFAULT);
if (dsexists < 0 || !dsexists) {
error |= cbf_add_h5double_dataset(nxdetector,
xpixelsizepath,
psizes[0],
error);
}
cbf_free((void **)&xpixelsizepath,NULL);
dsexists = H5Lexists(nxdetector_module,fastpixelsizepath,H5P_DEFAULT);
if (dsexists < 0 || !dsexists) {
error |= cbf_add_h5double_dataset(nxdetector_module,
fastpixelsizepath,
psizes[0],
error);
}
}
if (!error && psizes[1] > 0. && rank > 1) {
const char * ypixelsizepath;
const char * y_pixel_size_parts[3];
const char * slowpixelsizepath = "slow_pixel_size";
htri_t dsexists;
y_pixel_size_parts[0] = "y_pixel_size";
y_pixel_size_parts[1] = array_id;
y_pixel_size_parts[2] = 0;
ypixelsizepath = _cbf_str_join(y_pixel_size_parts,'_');
dsexists = H5Lexists(nxdetector,ypixelsizepath,H5P_DEFAULT);
if (dsexists < 0 || !dsexists) {
error |= cbf_add_h5double_dataset(nxdetector,
ypixelsizepath,
psizes[1],
error);
cbf_h5reportneg(datasetid = H5Dopen2(nxdetector,
ypixelsizepath, H5P_DEFAULT),CBF_H5ERROR,error);
cbf_reportnez(cbf_apply_h5text_attribute(datasetid,
"units",
"mm",error),error);
cbf_h5reportneg(H5Dclose(datasetid),CBF_H5ERROR,error);
}
cbf_free((void **)&ypixelsizepath,NULL);
dsexists = H5Lexists(nxdetector_module,slowpixelsizepath,H5P_DEFAULT);
if (dsexists < 0 || !dsexists) {
error |= cbf_add_h5double_dataset(nxdetector_module,
slowpixelsizepath,
psizes[1],
error);
cbf_h5reportneg(datasetid = H5Dopen2(nxdetector_module,
slowpixelsizepath, H5P_DEFAULT),CBF_H5ERROR,error);
cbf_reportnez(cbf_apply_h5text_attribute(datasetid,
"units",
"mm",error),error);
cbf_h5reportneg(H5Dclose(datasetid),CBF_H5ERROR,error);
}
}
cbf_reportnez(cbf_get_array_section_sizes(handle,array_section_id,rank,cbf_dims,cbf_origins,cbf_strides),error);
chunk[0] = rank;
max[0] = rank;
offset[0] = 0;
count[0] = rank;
strides[0] = 1;
for (ii = 0; ii < rank; ii++) {
origins[ii] = cbf_origins[ii]-1;
dims[ii] = cbf_dims[ii];
strides[ii] = cbf_strides[ii];
if (strides[ii] == 0) strides[ii] = 1;
}
cbf_reportnez(cbf_H5Drequire(nxdetector_module,&dset,"data_size",1,max,chunk,buf,H5T_STD_U64LE),error);
cbf_reportnez(cbf_H5Dinsert(dset,offset,NULL,count,buf,dims,H5T_NATIVE_HSIZE),error);
cbf_reportnez(cbf_H5Drequire(nxdetector_module,&dset,"data_origin",1,max,chunk,buf,H5T_STD_U64LE),error);
cbf_reportnez(cbf_H5Dinsert(dset,offset,NULL,count,buf,origins,H5T_NATIVE_HSIZE),error);
cbf_reportnez(cbf_H5Drequire(nxdetector_module,&dset,"data_stride",1,max,chunk,buf,H5T_STD_I64LE),error);
cbf_reportnez(cbf_H5Dinsert(dset,offset,NULL,count,buf,strides,H5T_NATIVE_HSSIZE),error);
if (cbf_H5Ivalid(dset)) {
cbf_h5reportneg(H5Dclose(dset),CBF_FORMAT,error);
}
if (cbf_H5Ivalid(nxdetector_module)) {
cbf_h5reportneg(H5Gclose(nxdetector_module),CBF_FORMAT,error);
}
CBF_END_ARRAY_REPORTNEZ(psizes,error);
CBF_END_ARRAY_REPORTNEZ(strides,error);
CBF_END_ARRAY_REPORTNEZ(cbf_strides,error);
CBF_END_ARRAY_REPORTNEZ(origins,error);
CBF_END_ARRAY_REPORTNEZ(cbf_origins,error);
CBF_END_ARRAY_REPORTNEZ(dims,error);
CBF_END_ARRAY_REPORTNEZ(cbf_dims,error);
}
}
}
}
}
CBF_END_ARRAY_REPORTNEZ(psizes,error);
CBF_END_ARRAY_REPORTNEZ(strides,error);
CBF_END_ARRAY_REPORTNEZ(cbf_strides,error);
CBF_END_ARRAY_REPORTNEZ(origins,error);
CBF_END_ARRAY_REPORTNEZ(cbf_origins,error);
CBF_END_ARRAY_REPORTNEZ(dims,error);
CBF_END_ARRAY_REPORTNEZ(cbf_dims,error);
}
}
/* Scan array_structure list for axis_set references for this array_id
Use them to populate axis_indices and axes in nxdata and to add links
for the individual axes to NXdata, to NXdetector_element and
to NXdetector_module */
if (cbf_H5Ivalid(nxdata) && !cbf_find_category(handle,"array_structure_list")
&& (!cbf_find_column(handle,"array_id")||!cbf_find_column(handle,"array_section"))
&& !cbf_count_rows(handle,&rows)) {
for (row = 0; row < rows; row++) {
size_t arank;
size_t asrank;
cbf_failnez(cbf_find_category(handle,"array_structure_list"));
if (!(!cbf_find_column(handle,"array_id")||!cbf_find_column(handle,"array_section"))) {
cbf_failnez(cbf_find_column(handle,"array_section_id"));
}
cbf_failnez(cbf_select_row(handle,row));
array_id_found = 0;
array_section_id_found = 0;
arank = 0;
asrank = 0;
if ((!cbf_find_column(handle,"array_id")
||!cbf_find_column(handle,"array_section"))
&& !cbf_get_value(handle,&xarray_id)
&& xarray_id
&& (!cbf_cistrcmp(xarray_id,array_id))){
array_id_found = 1;
}
if (!cbf_find_column(handle,"array_section_id")
&& !cbf_get_value(handle,&xarray_section_id)
&& xarray_section_id
&& !cbf_get_array_section_array_id(handle,xarray_section_id,&xarray_id)
&& (!cbf_cistrcmp(xarray_id,array_id))){
array_id_found = 1;
array_section_id_found = 1;
}
if (cbf_get_array_section_rank(handle,array_id,&arank)) arank=0;
if (!array_id_found) continue;
cbf_failnez(cbf_find_category(handle,"array_structure_list"));
if (!(!cbf_find_column(handle,"array_id")||!cbf_find_column(handle,"array_section"))) {
cbf_failnez(cbf_find_column(handle,"array_section_id"));
}
cbf_failnez(cbf_select_row(handle,row));
if (!cbf_find_column(handle,"axis_set_id")
&& !cbf_get_value(handle,&axis_set_id)
&& axis_set_id
&& !cbf_find_column(handle,"precedence")
&& !cbf_get_integervalue(handle,&precedence)
&& !cbf_find_category(handle,"array_structure_list_axis")
&& !cbf_find_column(handle,"axis_set_id")
&& !cbf_find_row(handle,axis_set_id)
&& !cbf_rewind_row(handle)) {
while(!cbf_find_nextrow(handle,axis_set_id)) {
cbf_failnez(cbf_find_column(handle,"axis_id"));
cbf_failnez(cbf_get_value(handle,&axis_id));
axis_indices_parts[0] = axis_id;
axis_indices_parts[1] = "indices";
axis_indices_parts[2] = 0;
axis_indices = _cbf_str_join(axis_indices_parts,'_');
if (cbf_H5Ivalid(nxdata)) {
error |=
cbf_apply_h5integer_attribute(nxdata,axis_indices,precedence-1,error);
cbf_failnez(cbf_get_NX_axis_path(h5handle,axis_id,&axis_path))
if (error) return error;
alexists = H5Lexists(nxdata, axis_id, H5P_DEFAULT);
if (alexists < 0 || !alexists) {
cbf_debug_print2("linking %s\n",axis_id);
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdata,axis_id,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
}
/* The following are included for historical reasons,
the least axis of any given index is used.
The first call will fall back to a "_axes" dataset if more
than 256 axes have been specified the group level
attribute will be omitted */
cbf_reportnez(cbf_add_h5text_attribute_slab(dsetid,nxdata,"axes",
axis_id,NULL,NULL,
arank-precedence,error),error);
cbf_reportnez(cbf_add_h5text_list_attribute_slab(nxdata,"axes",axis_id,precedence-1,error),error);
}
if (cbf_H5Ivalid(nxdetector)) {
if (H5Lexists(nxdetector,array_id,H5P_DEFAULT)>0) {
cbf_reportnez(cbf_H5Grequire(nxdetector,&nxdetector_module,array_id),error);
cbf_failnez(cbf_get_NX_axis_path(h5handle,axis_id,&axis_path))
if (error) return error;
cbf_apply_h5integer_attribute(nxdetector_module,axis_indices,precedence-1,error);
alexists = H5Lexists(nxdetector_module, axis_id, H5P_DEFAULT);
if ((alexists < 0 || !alexists) && arank > 0 && arank < 4 ) {
cbf_debug_print2("linking %s\n",axis_id);
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector_module,axis_id,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
if (precedence == 1 && H5Lexists(nxdetector_module, "fast_pixel_direction", H5P_DEFAULT)==0) {
const char * xpixelpath;
const char * x_pixel_offset_parts[3];
x_pixel_offset_parts[0] = "x_pixel_offset";
x_pixel_offset_parts[1] = array_id;
x_pixel_offset_parts[2] = 0;
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector_module,"fast_pixel_direction",H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
xpixelpath = _cbf_str_join(x_pixel_offset_parts,'_');
if (H5Lexists(nxdetector,xpixelpath,H5P_DEFAULT)==0) {
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector,xpixelpath,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
}
cbf_free((void **)&xpixelpath,NULL);
}
if (precedence == 2 && H5Lexists(nxdetector_module, "slow_pixel_direction", H5P_DEFAULT)==0) {
const char * ypixelpath;
const char * y_pixel_offset_parts[3];
y_pixel_offset_parts[0] = "y_pixel_offset";
y_pixel_offset_parts[1] = array_id;
y_pixel_offset_parts[2] = 0;
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector_module,"slow_pixel_direction",H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
ypixelpath = _cbf_str_join(y_pixel_offset_parts,'_');
if (H5Lexists(nxdetector,ypixelpath,H5P_DEFAULT)==0) {
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector,ypixelpath,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
}
cbf_free((void **)&ypixelpath,NULL);
}
if (precedence == 3 && H5Lexists(nxdetector_module, "slower_pixel_offset", H5P_DEFAULT)==0) {
const char * zpixelpath;
const char * z_pixel_offset_parts[3];
z_pixel_offset_parts[0] = "z_pixel_offset";
z_pixel_offset_parts[1] = array_id;
z_pixel_offset_parts[2] = 0;
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector_module,"slower_pixel_offset",H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
zpixelpath = _cbf_str_join(z_pixel_offset_parts,'_');
if (H5Lexists(nxdetector,zpixelpath,H5P_DEFAULT)==0) {
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector,zpixelpath,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
}
cbf_free((void **)&zpixelpath,NULL);
}
}
if (cbf_H5Ivalid(nxdetector_module)) H5Gclose(nxdetector_module);
}
if (array_section_id_found && cbf_cistrcmp(array_id,xarray_section_id)
&& H5Lexists(nxdetector,xarray_section_id,H5P_DEFAULT)==0) {
nxdetector_module_prev = -1;
cbf_reportnez(cbf_H5Grequire(nxdetector,&nxdetector_module_prev,array_id),error);
cbf_reportnez(cbf_H5Grequire(nxdetector_module_prev,&nxdetector_module,xarray_section_id),error);
cbf_failnez(cbf_get_NX_axis_path(h5handle,axis_id,&axis_path))
if (error) {
if (cbf_H5Ivalid(nxdetector_module)) H5Gclose(nxdetector_module);
if (cbf_H5Ivalid(nxdetector_module_prev)) H5Gclose(nxdetector_module_prev);
return error;
}
cbf_apply_h5integer_attribute(nxdetector_module,axis_indices,precedence-1,error);
alexists = H5Lexists(nxdetector_module, axis_id, H5P_DEFAULT);
if (alexists < 0 || !alexists) {
cbf_debug_print2("linking %s\n",axis_id);
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector_module,axis_id,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
}
if (cbf_get_array_section_rank(handle,xarray_section_id,&asrank)) asrank=0;
if (precedence == (ssize_t)asrank && H5Lexists(nxdetector_module, "fast_pixel_direction", H5P_DEFAULT)==0) {
const char * xpixelpath;
const char * x_pixel_offset_parts[3];
x_pixel_offset_parts[0] = "x_pixel_offset";
x_pixel_offset_parts[1] = xarray_section_id;
x_pixel_offset_parts[2] = 0;
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector_module,"fast_pixel_direction",H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
xpixelpath = _cbf_str_join(x_pixel_offset_parts,'_');
if (H5Lexists(nxdetector,xpixelpath,H5P_DEFAULT)==0) {
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector,xpixelpath,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
}
cbf_free((void **)&xpixelpath,NULL);
}
if (precedence == (ssize_t)asrank-1 && H5Lexists(nxdetector_module, "slow_pixel_direction", H5P_DEFAULT)==0) {
const char * ypixelpath;
const char * y_pixel_offset_parts[3];
y_pixel_offset_parts[0] = "y_pixel_offset";
y_pixel_offset_parts[1] = xarray_section_id;
y_pixel_offset_parts[2] = 0;
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector_module,"slow_pixel_direction",H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
ypixelpath = _cbf_str_join(y_pixel_offset_parts,'_');
if (H5Lexists(nxdetector,ypixelpath,H5P_DEFAULT)==0) {
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector,ypixelpath,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
}
cbf_free((void **)&ypixelpath,NULL);
}
if (precedence == (ssize_t)asrank-2 && H5Lexists(nxdetector_module, "slower_pixel_direction", H5P_DEFAULT)==0) {
const char * zpixelpath;
const char * z_pixel_offset_parts[3];
z_pixel_offset_parts[0] = "z_pixel_offset";
z_pixel_offset_parts[1] = xarray_section_id;
z_pixel_offset_parts[2] = 0;
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector_module,"slower_pixel_direction",H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
zpixelpath = _cbf_str_join(z_pixel_offset_parts,'_');
if (H5Lexists(nxdetector,zpixelpath,H5P_DEFAULT)==0) {
cbf_h5reportneg(H5Lcreate_soft(axis_path,nxdetector,zpixelpath,H5P_DEFAULT, H5P_DEFAULT),CBF_FORMAT,error);
}
cbf_free((void **)&zpixelpath,NULL);
}
if (cbf_H5Ivalid(nxdetector_module)) H5Gclose(nxdetector_module);
}
}
cbf_free_text(&axis_indices,NULL);
cbf_failnez(cbf_find_column(handle,"axis_set_id"));
}
}
}
if (cbf_H5Ivalid(nxdetector_module)) H5Gclose(nxdetector_module);
if (cbf_H5Ivalid(nxdetector_module_prev)) H5Gclose(nxdetector_module_prev);
if (cbf_H5Ivalid(dsetid)) H5Dclose(dsetid);
return CBF_SUCCESS;
} else {
if (cbf_H5Ivalid(nxdetector_module)) H5Gclose(nxdetector_module);
if (cbf_H5Ivalid(nxdetector_module_prev)) H5Gclose(nxdetector_module_prev);
if (cbf_H5Ivalid(dsetid)) H5Dclose(dsetid);
return CBF_NOTFOUND;
}
}
static int cbf_get_nx_object_path(hid_t hid, const char * name, char ** path) {
size_t plen;
int error = 0;
const char * pathparts[3];
plen = H5Iget_name(hid,NULL,0);
if (plen <= 0) return CBF_NOTFOUND;
CBF_START_ARRAY(char,base,plen+1);
pathparts[0] = base;
pathparts[1] = name;
pathparts[2] = 0;
plen = H5Iget_name(hid,base,plen+1);
if (!name || !cbf_cistrcmp(name,".")) {
*path = _cbf_strdup(base);
} else {
*path = _cbf_str_join(pathparts,'/');
}
CBF_END_ARRAY_REPORTNEZ(base, error);
return error;
}
/* process the data in a CBF looking for all combinations of array_ids
and binary_ids for the given detector element and storing the
data in data_ARRAYID_BINARYID:NXdata groups.
For block > 0, "_block" is appended to the BINARYID
There are two choices on where to actually store the data;
1. Directly in NXdata, with an incoming link from NXdetector
2. Directly in NXdetector, with an incoming link from NXdetector
and for each of those choices, with external links from the
equivalent locations in a master h5handle. If a master h5handle
is given, the data h5handle NXdata or NXdetector that does not
have the data will not be created here.
data_h5handle must always be valid.
master_h5handle may be NULL.
If store_in_NXdata is true:
data stored in data_h5handle:NXentry/data:NXdata/data_...
If master_h5handle is valid:
create an external link
from master_h5handle:NXentry/data:NXdata/data_...
to data_h5handle:NXentry/data:NXdata/data_...
create a soft link
from master_h5handle:NXentry/.../NX_detector/data_...
to master_h5handle:NXentry/data:NXdata/data_...
If master_h5 handle is NULL
create a soft link
from data_h5handle:NXentry/.../NX_detector/data ...
to data_h5handle:NXentry/data:NXdata/data_...
If store_in_NXdata is false:
data stored in data_h5handle:NXentry/.../NX_detector/data_...
create a soft link
from data_h5handle:NXentry/data:NXdata/data_...
to data_h5handle:NXentry/.../NX_detector/data ...
create an external link
from master_h5handle:NXentry/data:NXdata/data_...
to data_h5handle:NXentry/.../NX_detector/data ...
create an external link
from master_h5handle:NXentry/.../NX_detector/data ...
to data_h5handle:NXentry/.../NX_detector/data ...
If a we are dealing with a template, only a data_h5handle will
be given. master_h5handle will be NULL.
If no detector_element is given, a special pseudo detector
_NO_DETECTOR_ is used.
Under each NXdetector, store at least one NXdetector_module with the
axis and indexinf information for each module.
*/
static int cbf_require_nxdata(const cbf_handle handle,
const cbf_h5handle data_h5handle,
const cbf_h5handle master_h5handle,
const char * detector_element,
const int store_in_NXdata) {
unsigned int row, rows;
int matchfound;
const cbf_node * node;
const char * nxdataname = NULL;
const char * nxdata_field_link_target_file = NULL;
const char * nxdata_field_link_target_path = NULL;
const char * typeofvalue;
const char * nxdetector;
const char * groupname;
const char * abdetelid;
const char * arrayid = NULL;
const char * binaryid = NULL;
const char * undefined_value = NULL;
const char * details = NULL;
const char * gain = NULL;
const char * gain_esd = NULL;
const char * linearity = NULL;
const char * offset = NULL;
const char * scaling = NULL;
const char * overload = NULL;
const char * searchcols[] = {"array_id", "binary_id"};
const char * values[2];
const char * default_array_id;
hid_t data_instrumentid;
hid_t master_instrumentid;
hid_t data_detectorid;
hid_t master_detectorid;
hid_t data_dataid;
hid_t master_dataid;
hid_t dsetid;
int error = 0;
values[0] = arrayid;
values[1] = binaryid;
if (!handle || !data_h5handle ) return CBF_ARGUMENT;
nxdetector = detector_element?detector_element:"_NO_DETECTOR_";
data_instrumentid = master_instrumentid = CBF_H5FAIL;
data_detectorid = master_detectorid = CBF_H5FAIL;
data_dataid = master_dataid = CBF_H5FAIL;
dsetid = CBF_H5FAIL;
CBF_UNUSED( dsetid );
CBF_UNUSED( nxdetector );
cbf_failnez(cbf_h5handle_require_entry(data_h5handle,0,0));
if (master_h5handle) {
/* require an NXinstrument group and an NXdetector*/
cbf_failnez(cbf_h5handle_require_entry(master_h5handle,0,0));
cbf_failnez(cbf_h5handle_require_instrument(master_h5handle,
&master_instrumentid,0));
cbf_failnez(cbf_h5handle_require_detector(master_h5handle,&master_detectorid,detector_element));
master_detectorid = master_h5handle->nxdetectors[master_h5handle->cur_detector];
cbf_debug_print2("created master_detectorid for '%s'\n",detector_element);
}
if (!master_h5handle || !store_in_NXdata) {
/* require an NXinstrument group and an NXdetector in the data hdf5*/
cbf_failnez(cbf_h5handle_require_instrument(data_h5handle,
&data_instrumentid,0));
cbf_failnez(cbf_h5handle_require_detector(data_h5handle,&data_detectorid,detector_element));
data_detectorid= data_h5handle->nxdetectors[data_h5handle->cur_detector];
cbf_debug_print2("created data_detectorid for '%s'\n",detector_element);
}
if (cbf_find_category(handle,"array_structure")
||cbf_find_column(handle,"id")
||cbf_rewind_row(handle)
||cbf_get_value(handle,&default_array_id)
||!default_array_id) default_array_id = "None";
cbf_failnez(cbf_find_category(handle,"array_data"));
rows = 0;
cbf_failnez(cbf_count_rows(handle,&rows));
matchfound = 0;
node = NULL;
for (row = 0; row < rows && !matchfound; row ++) {
cbf_failnez(cbf_find_category(handle,"array_data"));
if (cbf_find_column(handle,"array_id")
||cbf_select_row(handle,row)
||cbf_get_value(handle,&arrayid)
|| !arrayid) arrayid = default_array_id;
if (cbf_find_column(handle,"binary_id")
||cbf_select_row(handle,row)
||cbf_get_value(handle,&binaryid)
|| !binaryid) binaryid = "1";
if (!cbf_find_column(handle,"data")) {
node = handle->node;
}
/* See if this array_id and binary_id are
for this detector_element */
matchfound = 0;
values[0] = arrayid;
values[1] = binaryid;
if ((!cbf_find_category(handle,"diffrn_data_frame")
|| !cbf_find_category(handle,"diffrn_frame_data"))
&& !cbf_find_column(handle,"array_id")
&& !cbf_find_column(handle,"binary_id")
&& !cbf_find_column(handle,"detector_element_id")
&& !cbf_find_row_by_columns(handle,2,searchcols,values)) {
if (cbf_find_column(handle,"detector_element_id")
||cbf_get_value(handle,&abdetelid)
||!abdetelid) abdetelid = "no_detector_element_id";
if (!detector_element) {
if (cbf_cistrcmp(abdetelid,"no_detector_element_id")) {
matchfound = 1;
break;
}
} else {
if (!cbf_cistrcmp(abdetelid,detector_element)) {
matchfound = 1;
break;
}
}
} else {
matchfound = 1;
}
if (!matchfound) continue;
}
if (matchfound && node) {
cbf_get_nxdata_field_name2(handle,arrayid,binaryid,
data_h5handle->block,
&nxdataname,
&groupname,
&nxdata_field_link_target_file,
&nxdata_field_link_target_path,
&typeofvalue);
cbf_debug_print2("cbf_require_nxdata arrayid = %s",arrayid);
cbf_debug_print2("cbf_require_nxdata binaryid = %s",binaryid);
cbf_debug_print2("cbf_require_nxdata nxdataname = %s",nxdataname);
cbf_debug_print2("cbf_require_nxdata groupname = %s",groupname);
cbf_debug_print2("cbf_require_nxdata nxdata_field_link_target_file = %s",nxdata_field_link_target_file);
cbf_debug_print2("cbf_require_nxdata nxdata_field_link_target_path = %s",nxdata_field_link_target_path);
cbf_debug_print2("cbf_require_nxdata typeofvalue = %s",typeofvalue);
if (!cbf_find_category(handle,"array_intensities")
&&!cbf_find_row_by_columns(handle,2,searchcols,values)){
if (cbf_find_column(handle,"undefined_value")
|| cbf_get_value(handle,&undefined_value))
undefined_value = NULL;
if (cbf_find_column(handle,"details")
|| cbf_get_value(handle,&details))
details = NULL;
if (cbf_find_column(handle,"gain")
|| cbf_get_value(handle,&gain))
gain = NULL;
if (cbf_find_column(handle,"gain_esd")
|| cbf_get_value(handle,&gain_esd))
gain_esd = NULL;
if (cbf_find_column(handle,"linearity")
|| cbf_get_value(handle,&linearity))
linearity = NULL;
if (cbf_find_column(handle,"offset")
|| cbf_get_value(handle,&offset))
offset = NULL;
if (cbf_find_column(handle,"scaling")
|| cbf_get_value(handle,&scaling))
scaling = NULL;
if (cbf_find_column(handle,"overload")
|| cbf_get_value(handle,&overload))
overload = NULL;
if (cbf_find_column(handle,"undefined_value")
|| cbf_get_value(handle,&undefined_value))
undefined_value = NULL;
}
/* In all cases, data will be in data_h5handle.
If master_h5handle is given, the metadata will be in master_h5handle */
cbf_reportnez(cbf_H5Grequire(data_h5handle->nxid,&data_dataid,groupname),error);
cbf_reportnez(cbf_H5Arequire_string(data_dataid,"NX_class","NXdata"),error);
data_h5handle->nxdata = data_dataid;
if (master_h5handle) {
cbf_reportnez(cbf_H5Grequire(master_h5handle->nxid,&master_dataid,groupname),error);
cbf_reportnez(cbf_H5Arequire_string(master_dataid,"NX_class","NXdata"),error);
master_h5handle->nxdata = master_dataid;
if (store_in_NXdata) {
/* If we are storing the data in NXdata in the data file,
we need to put a link to this data in master file
and store the summary axis information there and
the detailed dectector module information with
axes in NXdetector in the master file */
handle->node = (cbf_node *)node;
handle->row = row;
error |= cbf_write_array_h5file2(handle,data_h5handle,
arrayid,
binaryid,
overload,
undefined_value,
details,
gain,
gain_esd,
linearity,
offset,
scaling,
data_dataid);
if (cbf_H5Ivalid(data_dataid)) {
error|= cbf_require_nxarrayid(handle,data_h5handle,
arrayid,
binaryid,
data_dataid,
CBF_H5FAIL);
}
if (cbf_H5Ivalid(master_detectorid)) {
error|= cbf_require_nxarrayid(handle,master_h5handle,
arrayid,
binaryid,
CBF_H5FAIL,
master_detectorid);
}
/* need to set up links
master_h5handle/entry/instrument/detector/data_...
--> master_h5handle/entry/data/data_...
master_h5handle/entry/data/data_...
--> data_h5handle/entry/data/data_...
*/
if (cbf_H5Ivalid(master_detectorid) && cbf_H5Ivalid(master_dataid)) {
int oerror = error;
#ifdef CBFDEBUG
hid_t master_detectorid_file_id, master_dataid_file_id, data_dataid_file_id;
char master_detectorid_name[128];
char master_dataid_name[128];
char data_dataid_name[128];
ssize_t master_detectorid_name_len;
ssize_t master_dataid_name_len;
ssize_t data_dataid_name_len;
#endif
char * dataname=NULL;
char * masterdataname = NULL;
error = 0;
cbf_reportnez(cbf_get_nx_object_path(master_dataid,nxdataname,&masterdataname),error);
cbf_reportnez(cbf_get_nx_object_path(data_dataid,nxdataname,&dataname),error);
cbf_debug_print2("nxdataname '%s'\n", nxdataname);
cbf_debug_print2("masterdataname '%s'\n", masterdataname);
cbf_debug_print2("dataname '%s'\n", dataname);
#ifdef CBFDEBUG
master_detectorid_file_id = H5Iget_file_id(master_detectorid);
master_dataid_file_id = H5Iget_file_id(master_dataid);
data_dataid_file_id = H5Iget_file_id(data_dataid);
master_detectorid_name_len = H5Iget_name(master_detectorid,master_detectorid_name,127);
master_dataid_name_len = H5Iget_name(master_dataid,master_dataid_name,127);
data_dataid_name_len = H5Iget_name(data_dataid,data_dataid_name,127);
master_detectorid_name[127]
= master_dataid_name[127]
= data_dataid_name[127] = '\0';
cbf_debug_print3("master_detectorid_name '%s', file %x\n", master_detectorid_name,master_detectorid_file_id);
cbf_debug_print3("master_dataid_name '%s', file %x\n", master_dataid_name,master_dataid_file_id );
cbf_debug_print3("data_dataid_name '%s', file %x\n", data_dataid_name,data_dataid_file_id );
#endif
if (!error && H5Lexists(master_detectorid,nxdataname,H5P_DEFAULT) == 0) {
cbf_h5reportneg(H5Lcreate_soft(masterdataname,master_detectorid,nxdataname,H5P_DEFAULT,H5P_DEFAULT),CBF_H5ERROR,error);
}
if (H5Lexists(master_dataid,nxdataname,H5P_DEFAULT) == 0) {
if (!nxdata_field_link_target_file)
nxdata_field_link_target_file = data_h5handle->nxfilename;
if (!nxdata_field_link_target_path)
nxdata_field_link_target_path = "/entry/data/data";
cbf_debug_print2("nxdata_field_link_target_file '%s'\n", nxdata_field_link_target_file);
cbf_debug_print2("nxdata_field_link_target_path '%s'\n", nxdata_field_link_target_path);
cbf_h5reportneg(H5Lcreate_external( nxdata_field_link_target_file,
nxdata_field_link_target_path,
master_dataid,nxdataname,
H5P_DEFAULT,H5P_DEFAULT),CBF_H5ERROR,error);
}
if (dataname) cbf_free((void **)&dataname,NULL);
if (masterdataname) cbf_free((void **)&masterdataname,NULL);
error |= oerror;
}
/* We will store in NXdetector */
} else {
handle->node = (cbf_node *)node;
handle->row = row;
cbf_reportnez(cbf_write_array_h5file2(handle,data_h5handle,
arrayid,
binaryid,
overload,
undefined_value,
details,
gain,
gain_esd,
linearity,
offset,
scaling,
data_detectorid),error);
cbf_reportnez(cbf_require_nxarrayid(handle,data_h5handle,
arrayid,
binaryid,
data_detectorid,
CBF_H5FAIL),error);
cbf_reportnez(cbf_require_nxarrayid(handle,master_h5handle,
arrayid,
binaryid,
CBF_H5FAIL,
master_detectorid),error);
/* need to set up links
master_h5handle/entry/data_arrayid_binaryid/data_arrayid_binaryid
--> master_h5handle/entry/instrument/detector/data_arrayid_binaryid
master_h5handle/entry/instrument/detector/data_arrayid_binaryid
--> data_h5handle/entry/instrument/detector/data_arrayid_binaryid
need to set up axes in
master_h5handle/entry/data_arrayid_binaryid/
*/
if (cbf_H5Ivalid(master_detectorid)&&cbf_H5Ivalid(master_dataid)) {
int oerror = error;
char * dataname=NULL;
char * masterdataname = NULL;
error = 0;
cbf_reportnez(cbf_get_nx_object_path(master_detectorid,nxdataname,&masterdataname),error);
cbf_reportnez(cbf_get_nx_object_path(data_detectorid,nxdataname,&dataname),error);
if (!nxdata_field_link_target_file)
nxdata_field_link_target_file = data_h5handle->nxfilename;
if (!nxdata_field_link_target_path)
nxdata_field_link_target_path = "/entry/detector/data";
if (H5Lexists(master_dataid,nxdataname,H5P_DEFAULT) == 0) {
cbf_h5reportneg(H5Lcreate_soft(masterdataname,master_dataid,nxdataname,H5P_DEFAULT,H5P_DEFAULT),CBF_H5ERROR,error);
}
if (H5Lexists(master_dataid,nxdataname,H5P_DEFAULT) == 0) {
cbf_h5reportneg(H5Lcreate_external( nxdata_field_link_target_file,
nxdata_field_link_target_path,
master_detectorid,
nxdataname,
H5P_DEFAULT,H5P_DEFAULT),CBF_H5ERROR,error);
}
if (dataname) cbf_free((void **)&dataname,NULL);
if (masterdataname) cbf_free((void **)&masterdataname,NULL);
error |= oerror;
}
}
} else {
/* There is no master file. We still may use an external
link if there is no real data */
cbf_reportnez(cbf_H5Grequire(data_h5handle->nxid,&data_dataid,nxdataname),error);
cbf_reportnez(cbf_H5Arequire_string(data_dataid,"NX_class","NXdata"),error);
data_h5handle->nxdata = data_dataid;
if (store_in_NXdata) {
handle->node = (cbf_node *)node;
handle->row = row;
cbf_reportnez(cbf_write_array_h5file2(handle,data_h5handle,
arrayid,
binaryid,
overload,
undefined_value,
details,
gain,
gain_esd,
linearity,
offset,
scaling,
data_dataid),error);
/* need to set up link
data_h5handle/entry/instrument/detector/data_arrayid_binaryid
--> data_h5handle/entry/data/data_...
*/
if (cbf_H5Ivalid(data_detectorid)&&cbf_H5Ivalid(data_dataid) ) {
char * dataname=NULL;
cbf_reportnez(cbf_get_nx_object_path(data_dataid,nxdataname,&dataname),error);
if (H5Lexists(data_detectorid,nxdataname,H5P_DEFAULT) == 0) {
cbf_h5reportneg(H5Lcreate_soft(dataname,data_detectorid,nxdataname,H5P_DEFAULT,H5P_DEFAULT),CBF_H5ERROR,error);
}
if (dataname) cbf_free((void **)&dataname,NULL);
} else {
cbf_debug_print("Failed to create link from NXdetector to NXdata");
}
cbf_reportnez(cbf_require_nxarrayid(handle,data_h5handle,
arrayid,
binaryid,
data_dataid,
data_detectorid),error);
} else {
int oerror = error;
handle->node = (cbf_node *)node;
handle->row = row;
error = 0;
cbf_reportnez(cbf_write_array_h5file2(handle,data_h5handle,
arrayid,
binaryid,
overload,
undefined_value,
details,
gain,
gain_esd,
linearity,
offset,
scaling,
data_detectorid),error);
if (cbf_H5Ivalid(data_dataid) && cbf_H5Ivalid(data_detectorid) ) {
char * dataname=NULL;
cbf_reportnez(cbf_get_nx_object_path(data_detectorid,nxdataname,&dataname),error);
if (H5Lexists(data_dataid,nxdataname,H5P_DEFAULT) == 0) {
cbf_h5reportneg(H5Lcreate_soft(dataname,data_dataid,nxdataname,H5P_DEFAULT,H5P_DEFAULT),CBF_H5ERROR,error);
}
if (dataname) cbf_free((void **)&dataname,NULL);
} else {
cbf_debug_print("Failed to create link from NXdata to NXdetector");
}
cbf_reportnez(cbf_require_nxarrayid(handle,data_h5handle,
arrayid,
binaryid,
data_dataid,
data_detectorid),error);
error |= oerror;
}
}
}
if (nxdataname) cbf_free((void **)&nxdataname,NULL);
return CBF_SUCCESS;
}
/* process a CBF looking for all detector elements
and process them to produce NXdetector groups in the h5handle.
Under each NXdetector group place at least one NXdetector_module
group with the array indexing and axis information for that
module.
In addition, create a NXdetector_group from each CBF DIFFRN_DETECTOR
row, linking the detectors to the detector elements
If h5datahandle is given, the metadata goes to h5handle,
the data to h5datahandle
*/
static int cbf_require_nxdetectors2(const cbf_handle handle,
const cbf_h5handle h5handle,
const cbf_h5handle h5datahandle){
const char * detmodname;
const char * detname;
int error=0;
int processed_nxdetectors=0;
hid_t instrumentid;
hid_t detectorid;
hid_t detector_groupid;
hid_t group_namesid;
htri_t dsexists;
unsigned int row, rows, curdet, numdet = 0;
if (!handle || !h5handle) return CBF_ARGUMENT;
/* We will need to use the instrument group or create it*/
cbf_reportnez(cbf_h5handle_require_entry(h5handle,0,0), error);
cbf_reportnez(cbf_h5handle_require_instrument(h5handle,
&instrumentid,0),error);
/* First process for DIFFRN_DATA_FRAME (or the deprecated
DIFFRN_FRAME_DATA) that directly associates data
with detector elements */
if ((!cbf_find_category(handle,"diffrn_data_frame")||
!cbf_find_category(handle,"diffrn_frame_data"))
&& !cbf_find_column(handle,"detector_element_id")
&& !cbf_count_rows(handle,&rows)){
for (row = 0; row < rows; row++) {
if ((!cbf_find_category(handle,"diffrn_data_frame")||
!cbf_find_category(handle,"diffrn_frame_data"))
&& !cbf_find_column(handle,"detector_element_id")
&& !cbf_select_row(handle,row)
&& !cbf_get_value(handle,&detmodname) && detmodname){
cbf_reportnez(cbf_h5handle_require_detector(h5handle,NULL,detmodname),error);
cbf_reportnez(cbf_set_NX_detector_name(h5handle,detmodname),error);
}
}
processed_nxdetectors=1;
}
/* Then process DIFFRN_DETECTOR_ELEMENT */
if (!cbf_find_category(handle,"diffrn_detector_element")
&& !cbf_find_column(handle,"id")
&& !cbf_count_rows(handle,&rows)){
for (row = 0; row < rows; row++) {
detectorid = CBF_H5FAIL;
if (!cbf_find_category(handle,"diffrn_detector_element")
&& !cbf_find_column(handle,"id")
&& !cbf_select_row(handle,row)
&& !cbf_get_value(handle,&detmodname) && detmodname) {
if (!processed_nxdetectors) {
cbf_reportnez(cbf_h5handle_require_detector(h5handle,NULL,detmodname),error);
cbf_reportnez(cbf_set_NX_detector_name(h5handle,detmodname),error);
}
}
if (!cbf_find_category(handle,"diffrn_detector_element")
&&!cbf_find_column(handle,"detector_id")
&& !cbf_select_row(handle,row)
&&!cbf_get_value(handle,&detname) && detname) {
cbf_reportnez(cbf_h5handle_require_detector_group(h5handle,&detector_groupid,detname),error);
/* cbf_reportnez(_cbf_NXGrequire(instrumentid,&detector_groupid,
detname,
"NXdetector_group"),error); */
/* If there is no group_names dataset, we need to
create it, as well as group_index, group_parent
and group_type */
dsexists = H5Lexists(detector_groupid,"group_names", H5P_DEFAULT);
if (dsexists < 0 || !dsexists ) {
cbf_reportnez(cbf_add_h5text_dataset_slab(detector_groupid,
"group_names",
detname,
(hsize_t) 0L,
error),error);
cbf_reportnez(cbf_add_h5text_dataset_slab(detector_groupid,
"group_names",
detmodname,
(hsize_t) 1L,
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_index",
1L,
(hsize_t) 0L,
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_index",
2L,
(hsize_t) 1L,
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_parent",
-1L,
(hsize_t) 0L,
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_parent",
1L,
(hsize_t) 1L,
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_type",
1L,
(hsize_t) 0L,
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_type",
2L,
(hsize_t) 1L,
error),error);
} else if (cbf_find_h5text_dataset_slab(detector_groupid,
"group_names",detmodname, NULL, error)){
/* group_names exists, see if it contains detmodname */
hid_t datasetspace;
int ndims = 0;
hsize_t dsdims[1];
hsize_t dsmaxdims[1];
int havedmn;
CBF_UNUSED( havedmn );
havedmn = 0;
group_namesid=H5Dopen2(detector_groupid,"group_names", H5P_DEFAULT);
if (group_namesid<0) {
if (cbf_H5Ivalid(detector_groupid)) H5Gclose(detector_groupid);
return CBF_H5ERROR;
}
datasetspace = H5Dget_space(group_namesid);
if (datasetspace < 0) {
if (cbf_H5Ivalid(group_namesid)) H5Dclose(group_namesid);
if (cbf_H5Ivalid(detector_groupid)) H5Gclose(detector_groupid);
return CBF_H5ERROR;
}
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),
CBF_H5ERROR,error);
if ( error || ndims != 1 ) {
if (cbf_H5Ivalid(datasetspace)) H5Sclose(datasetspace);
if (cbf_H5Ivalid(group_namesid)) H5Dclose(group_namesid);
if (cbf_H5Ivalid(detectorid)) H5Gclose(detectorid);
return CBF_H5ERROR;
}
if (cbf_H5Ivalid(group_namesid)) H5Dclose(group_namesid);
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,
dsdims,dsmaxdims),
CBF_H5ERROR,error);
if (cbf_H5Ivalid(datasetspace)) H5Sclose(datasetspace);
cbf_reportnez(cbf_add_h5text_dataset_slab(detector_groupid,
"group_names",
detmodname,
dsdims[0],
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_index",
1L+(long)dsdims[0],
dsdims[0],
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_parent",
1L,
dsdims[0],
error),error);
cbf_reportnez(cbf_add_h5long_dataset_slab(detector_groupid,
"group_type",
2L,
dsdims[0],
error),error);
}
if (cbf_H5Ivalid(detector_groupid)) H5Gclose(detector_groupid);
}
}
/* Add in the axes */
cbf_reportnez(cbf_write_h5nxaxes(handle, h5handle),error);
/* Process the data for each detector module, i.e.
for each NXdetector group */
cbf_reportnez(cbf_count_NX_detector_names(h5handle,&numdet),error);
for (curdet = 0; curdet < numdet; curdet++) {
cbf_reportnez(cbf_get_NX_detector_name(h5handle,curdet,&detmodname),error);
if (!h5datahandle) {
cbf_reportnez(cbf_require_nxdata(handle, h5handle, NULL,
detmodname, 0),error);
} else {
cbf_reportnez(cbf_require_nxdata(handle, h5datahandle, h5handle,
detmodname, 1),error);
}
}
return error;
}
return CBF_NOTFOUND;
}
/* process a CBF looking for all detector elements
and process them to produce NXdetector groups in the h5handle.
Under each NXdetector group place at least one NXdetector_module
group with the array indexing and axis information for that
module.
In addition, create a NXdetector_group from each CBF DIFFRN_DETECTOR
row, linking the detectors to the detector elements
*/
static int cbf_require_nxdetectors(const cbf_handle handle,
const cbf_h5handle h5handle){
return cbf_require_nxdetectors2(handle,
h5handle,
NULL);
}
/**
Check the handle for the presence of an goniometer group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items of data
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_goniometer
\sa cbf_h5handle_set_goniometer
\sa cbf_h5handle_require_goniometer
\return An error code.
*/
int cbf_h5handle_get_goniometer
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->nxgoniometer)) *group = nx->nxgoniometer;
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->nxgoniometer_name) *name = nx->nxgoniometer_name;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the goniometer group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current goniometer group
\param name The name which the group should be given.
\sa cbf_h5handle_get_goniometer
\sa cbf_h5handle_set_goniometer
\sa cbf_h5handle_require_goniometer
\return An error code.
*/
int cbf_h5handle_set_goniometer
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxgoniometer);
const char * * const nxName = &(nx->nxgoniometer_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the goniometer group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"goniometer"</code>.
\sa cbf_h5handle_get_goniometer
\sa cbf_h5handle_set_goniometer
\sa cbf_h5handle_require_goniometer
\return An error code.
*/
int cbf_h5handle_require_goniometer
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
int match = 0;
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "goniometer";
const char * group_name = name ? name : default_name;
cbf_reportnez(cbf_h5handle_get_instrument(nx,&parent,0),error);
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_goniometer(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
cbf_reportnez(cbf_H5Grequire(parent,&new_group,group_name),error);
cbf_reportnez(cbf_H5Arequire_string(new_group,"NX_class","NXgoniometer"),error);
cbf_reportnez(cbf_h5handle_set_goniometer(nx,new_group,group_name),error);
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
cbf_reportnez(cbf_h5handle_get_goniometer(nx,group,0),error);
}
return error;
}
/**
Check the handle for the presence of an monochromator group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items of data
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_monochromator
\sa cbf_h5handle_set_monochromator
\sa cbf_h5handle_require_monochromator
\return An error code.
*/
int cbf_h5handle_get_monochromator
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->nxmonochromator)) *group = nx->nxmonochromator;
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->nxmonochromator_name) *name = nx->nxmonochromator_name;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the monochromator group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current monochromator group
\param name The name which the group should be given.
\sa cbf_h5handle_get_monochromator
\sa cbf_h5handle_set_monochromator
\sa cbf_h5handle_require_monochromator
\return An error code.
*/
int cbf_h5handle_set_monochromator
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxmonochromator);
const char * * const nxName = &(nx->nxmonochromator_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the monochromator group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"monochromator"</code>.
\sa cbf_h5handle_get_monochromator
\sa cbf_h5handle_set_monochromator
\sa cbf_h5handle_require_monochromator
\return An error code.
*/
int cbf_h5handle_require_monochromator
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
int match = 0;
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "monochromator";
const char * group_name = name ? name : default_name;
cbf_reportnez(cbf_h5handle_get_instrument(nx,&parent,0),error);
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_monochromator(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
cbf_reportnez(cbf_H5Grequire(parent,&new_group,group_name),error);
cbf_reportnez(cbf_H5Arequire_string(new_group,"NX_class","NXmonochromator"),error);
cbf_reportnez(cbf_h5handle_set_monochromator(nx,new_group,group_name),error);
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
cbf_reportnez(cbf_h5handle_get_monochromator(nx,group,0),error);
}
return error;
}
/**
Check the handle for the presence of an source group and its name,
optionally returning any combination of them. The error code
'CBF_NOTFOUND' will be returned if any of the requested items of data
cannot be found.
The handle retains ownership of the returned object and/or string, neither
of them should be free'd by the caller.
\param nx A handle to query for the presence of the requested information.
\param group A place to store the group (if found), or null if the group isn't wanted.
\param name A place to store the name of the group (if found), or null if the name isn't wanted.
\sa cbf_h5handle_get_source
\sa cbf_h5handle_set_source
\sa cbf_h5handle_require_source
\return An error code.
*/
int cbf_h5handle_get_source
(const cbf_h5handle nx,
hid_t * const group,
const char * * const name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
/* check for a valid group */
if (group) {
if (cbf_H5Ivalid(nx->nxsource)) *group = nx->nxsource;
else error |= CBF_NOTFOUND;
}
/* check for a name */
if (name) {
if (nx->nxsource_name) *name = nx->nxsource_name;
else error |= CBF_NOTFOUND;
}
}
return error;
}
/**
Sets the source group and name within the handle to the given values.
Doesn't check or modify the <code>NX_class</code> attribute in any way.
The handle will take ownership of the group id iff this function succeeds.
\param nx The handle to add information to.
\param group The group to be set as the current source group
\param name The name which the group should be given.
\sa cbf_h5handle_get_source
\sa cbf_h5handle_set_source
\sa cbf_h5handle_require_source
\return An error code.
*/
int cbf_h5handle_set_source
(const cbf_h5handle nx,
const hid_t group,
const char * const name)
{
int error = CBF_SUCCESS;
if (!nx || !cbf_H5Ivalid(group) || !name) {
error |= CBF_ARGUMENT;
} else {
hid_t * const nxGroup = &(nx->nxsource);
const char * * const nxName = &(nx->nxsource_name);
htri_t cmp;
if (cbf_H5Ivalid(*nxGroup)) {
cmp = cbf_H5Ocmp(*nxGroup,group);
} else {
cmp = 1;
}
if (cmp < 0) {
error |= CBF_H5ERROR;
} else if (cmp) {
/* free the old group, take ownership of the new one */
if (cbf_H5Ivalid(*nxGroup)) cbf_H5Gfree(*nxGroup);
*nxGroup = group;
/* set the name */
if (*nxName) cbf_free_text(nxName,0);
*nxName = _cbf_strdup(name);
} else {
/* already set - check that the names match, too */
if (!*nxName || strcmp(name,*nxName)) error |= CBF_H5DIFFERENT;
}
}
return error;
}
/**
This will check if the source group within the handle matches any existing group of the
same name within the current file. If they don't match a new group is opened or created
and added to the handle. The <code>NX_class</code> attributes are not checked.
\param nx The HDF5 handle to use.
\param group An optional pointer to a place where the group should be stored.
\param name The group name, or null to use the default name of <code>"source"</code>.
\sa cbf_h5handle_get_source
\sa cbf_h5handle_set_source
\sa cbf_h5handle_require_source
\return An error code.
*/
int cbf_h5handle_require_source
(const cbf_h5handle nx,
hid_t * const group,
const char * name)
{
int error = CBF_SUCCESS;
if (!nx) {
error |= CBF_ARGUMENT;
} else {
int match = 0;
hid_t curr_group = CBF_H5FAIL;
hid_t parent = CBF_H5FAIL;
const char * curr_name = NULL;
const char default_name[] = "source";
const char * group_name = name ? name : default_name;
cbf_reportnez(cbf_h5handle_get_instrument(nx,&parent,0),error);
/* check if the names of the groups match, and if the parent contains the assumed group */
if (CBF_SUCCESS==cbf_h5handle_get_source(nx,&curr_group,&curr_name)) {
if (!strcmp(group_name,curr_name)) {
hid_t test_group = CBF_H5FAIL;
const int found = cbf_H5Gfind(parent,&test_group,group_name);
if (CBF_SUCCESS==found) {
if (!cbf_H5Ocmp(test_group,curr_group)) match = 1;
} else if (CBF_NOTFOUND!=found) {
error |= found;
}
cbf_H5Gfree(test_group);
}
}
/* if there is no match I need to create/find a suitable group and put it in the handle */
if (CBF_SUCCESS==error && !match) {
hid_t new_group = CBF_H5FAIL;
cbf_reportnez(cbf_H5Grequire(parent,&new_group,group_name),error);
cbf_reportnez(cbf_H5Arequire_string(new_group,"NX_class","NXsource"),error);
cbf_reportnez(cbf_h5handle_set_source(nx,new_group,group_name),error);
if (CBF_SUCCESS!=error) cbf_H5Gfree(new_group);
}
/* if there haven't been any major problems, return any requested data */
cbf_reportnez(cbf_h5handle_get_source(nx,group,0),error);
}
return error;
}
/* Create a dotted CBF location string
returns a newly allocated string that
must be freed */
int cbf_location_string(const char* datablock,
const char* category,
const char* column,
unsigned int row,
char * * stringout) {
size_t dblen, catlen,collen,rowlen;
char rownum[20];
if (!stringout) return CBF_ARGUMENT;
if (!datablock) datablock = "_(NULL)_";
if (!category) category = "_(NULL)_";
if (!column) column = "_(NULL)_";
sprintf(rownum,"%u",row);
dblen = _cbf_strlen(datablock);
catlen = _cbf_strlen(category);
collen = _cbf_strlen(column);
rowlen = _cbf_strlen(rownum);
if (dblen == 0) {
datablock = "_(NULL)_"; dblen = 8;
}
if (catlen == 0) {
category = "_(NULL)_"; catlen = 8;
}
if (collen == 0) {
column = "_(NULL)_"; collen = 8;
}
cbf_failnez(cbf_alloc(((void **) stringout),NULL,
1,dblen+catlen+collen+rowlen+4));
_cbf_strcpy(*stringout,datablock);
_cbf_strcpy((*stringout)+dblen,".");
_cbf_strcpy((*stringout)+dblen+1,category);
_cbf_strcpy((*stringout)+dblen+1+catlen,".");
_cbf_strcpy((*stringout)+dblen+1+catlen+1,column);
_cbf_strcpy((*stringout)+dblen+1+catlen+1+collen,".");
_cbf_strcpy((*stringout)+dblen+1+catlen+1+collen+1,rownum);
return CBF_SUCCESS;
}
/* Conatenate two strings, returning a newly allocated string */
int cbf_strcat(const char * string1, const char * string2,
char * * stringout) {
int errorcode;
size_t len1, len2;
CBF_UNUSED( errorcode );
if (!string1 || !string2 || !stringout) return CBF_ARGUMENT;
errorcode = 0;
len1 = _cbf_strlen(string1);
len2 = _cbf_strlen(string2);
cbf_failnez(cbf_alloc(((void **) stringout),NULL,
1,len1+len2+1));
_cbf_strcpy(*stringout,string1);
_cbf_strcpy((*stringout)+len1,string2);
return CBF_SUCCESS;
}
/* Either open or create a NeXus group*/
int cbf_require_nxgroup(cbf_h5handle h5handle,
const char * nxgroup,
const char * nxclass,
hid_t parent_id,
hid_t * groupid) {
int errorcode;
if (!h5handle || !nxgroup || !nxclass || !groupid ) return CBF_ARGUMENT;
errorcode = 0;
if (parent_id < 0) parent_id = h5handle->curnxid;
if (parent_id < 0) parent_id = h5handle->nxid;
if ((H5Lexists(parent_id,nxgroup,H5P_DEFAULT)) != 1) {
/* ensure it goes right below the parent_id */
if (h5handle->curnxid>=0 && h5handle->curnxid!= parent_id) {
cbf_h5reportneg(H5Gclose(h5handle->curnxid),CBF_ARGUMENT,errorcode);
}
h5handle->curnxid = parent_id;
cbf_reportnez(cbf_H5Gcreate_in_handle(h5handle,nxgroup,groupid),errorcode);
cbf_reportnez(cbf_apply_h5text_attribute(*groupid,
"NX_class",nxclass,0),errorcode);
h5handle->curnxid = CBF_H5FAIL;
} else {
if ((*groupid = H5Gopenx(parent_id,nxgroup))<0) return CBF_NOTFOUND;
}
return errorcode;
}
/* Get an axis' vector and offset.
Attempts to locate the row of the 'id' column in the given category whose
value is the same as that given by 'axis_id'. If the row can be found then
the vector and offset are read.
This reads data directly from the node tree, instead of using a handle.
\return An error code.
*/
static int cbf_node_get_axis_vector_and_offset
(/* The axis category to read axes from. */
cbf_node * axes,
/* The (case-sensitive) name of the axis to find and extract data from. */
const char *axis_id,
/* A place to store the 3-element vector attribute, or 'NULL' if it isn't wanted. */
double vector[3],
/* A place to store the 3-element offset attribute, or 'NULL' if it isn't wanted. */
double offset[3])
{
int error = CBF_SUCCESS;
if (!axes || !axis_id) {
return CBF_ARGUMENT;
} else if (CBF_CATEGORY != axes->type) {
error |= CBF_ARGUMENT;
} else {
int found = CBF_SUCCESS;
cbf_node * col = NULL;
unsigned int row = 0;
cbf_reportnez(cbf_find_child(&col, axes, "id"),error);
found = cbf_node_find_nextrow(col, 0, axis_id, &row);
if (!found) {
if (vector) {
cbf_failnez(cbf_find_child(&col,axes,"vector[1]"));
if (cbf_node_get_doublevalue(col,row,vector+0)) vector[0] = 0.;
cbf_failnez(cbf_find_child(&col,axes,"vector[2]"));
if (cbf_node_get_doublevalue(col,row,vector+1)) vector[1] = 0.;
cbf_failnez(cbf_find_child(&col,axes,"vector[3]"));
if (cbf_node_get_doublevalue(col,row,vector+2)) vector[2] = 0.;
}
if (offset) {
cbf_failnez(cbf_find_child(&col,axes,"offset[1]"));
if (cbf_node_get_doublevalue(col,row,offset+0)) offset[0] = 0.;
cbf_failnez(cbf_find_child(&col,axes,"offset[2]"));
if (cbf_node_get_doublevalue(col,row,offset+1)) offset[1] = 0.;
cbf_failnez(cbf_find_child(&col,axes,"offset[3]"));
if (cbf_node_get_doublevalue(col,row,offset+2)) offset[2] = 0.;
}
} else {
if (CBF_NOTFOUND != found) CBF_PRINT_ERROR(cbf_strerror(found));
error |= found;
}
}
return error;
}
/* get an axis vector and offset */
int cbf_get_axis_vector_and_offset(cbf_handle handle,
const char *axis_id,
double vector[3],
double offset[3]) {
/***** Allow for missing vector or offset treat as 0 ***/
if (!handle || !axis_id ) return CBF_ARGUMENT;
cbf_failnez (cbf_find_category (handle, "axis"))
cbf_failnez (cbf_find_column (handle, "id"))
cbf_failnez (cbf_find_row (handle, axis_id))
if (vector) {
cbf_failnez (cbf_find_column (handle, "vector[1]"))
if (cbf_get_doublevalue (handle, vector)) vector[0] = 0;
cbf_failnez (cbf_find_column (handle, "vector[2]"))
if (cbf_get_doublevalue (handle, vector+1)) vector[1] = 0.;
cbf_failnez (cbf_find_column (handle, "vector[3]"))
if (cbf_get_doublevalue (handle, vector+2)) vector[2] = 0.;
}
if (offset) {
cbf_failnez (cbf_find_column (handle, "offset[1]"))
if (cbf_get_doublevalue (handle, offset)) offset[0] = 0.;
cbf_failnez (cbf_find_column (handle, "offset[2]"))
if (cbf_get_doublevalue (handle, offset+1)) offset[1] = 0.;
cbf_failnez (cbf_find_column (handle, "offset[3]"))
if (cbf_get_doublevalue (handle, offset+2)) offset[2] = 0.;
}
return CBF_SUCCESS;
}
/* Compute the cross-product of 2 3-vectors */
int cbf_cross_product(double vecin1[3],
double vecin2[3],
double vecout[3] ) {
if (!vecin1 || !vecin2 || !vecout) return CBF_ARGUMENT;
vecout[0] = vecin1[1]*vecin2[2] - vecin1[2]*vecin2[1];
vecout[1] = vecin1[2]*vecin2[0] - vecin1[0]*vecin2[2];
vecout[2] = vecin1[0]*vecin2[1] - vecin1[1]*vecin2[0];
return CBF_SUCCESS;
}
/* compute the L2 norm of a 3-vector */
double cbf_norm(double vector[3]) {
if (!vector) return -1;
return sqrt(vector[0]*vector[0]+vector[1]*vector[1]+vector[2]*vector[2]);
}
/* compute the product of a scalar and a vector */
int cbf_scalar_product(double scalar, double vecin[3], double vecout[3]) {
if (!vecin || ! vecout) return CBF_ARGUMENT;
vecout[0] = scalar * vecin[0];
vecout[1] = scalar * vecin[1];
vecout[2] = scalar * vecin[2];
return CBF_SUCCESS;
}
/* Apply a matrix to a vector */
int cbf_apply_matrix(double matrix[3][3], double vecin[3], double vecout[3]) {
int ii;
if (!matrix || !vecin || !vecout ) return CBF_ARGUMENT;
for (ii=0; ii < 3; ii++) {
vecout[ii] = matrix[ii][0]*vecin[0]
+ matrix[ii][1]*vecin[1]
+ matrix[ii][2]*vecin[2];
if (fabs(vecout[ii])<=1.e-15) vecout[ii] = 0.;
}
return CBF_SUCCESS;
}
/* Extract the direction of the beam.
Attempt to read data from the 'BEAM' axis, if it exists, otherwise attempt
to read data from the 'SOURCE' axis and multiply the direction by '-1'. If
neither of those axes can be found then assume that the beam direction is
[0,0,-1]. The axis names are case-sensitive.
This reads data directly from the node tree, instead of using a handle.
\return An error code.
*/
static int cbf_node_get_beam_dirn
(/* The axis category to read axes from. */
cbf_node * axes,
/* A place to store the 3-element direction vector. */
double vector[3])
{
int error = CBF_SUCCESS;
if (!axes || !vector) {
error |= CBF_ARGUMENT;
} else if (CBF_CATEGORY != axes->type) {
error |= CBF_ARGUMENT;
} else {
/* take the beam direction, if given, -source if source is given, or [0, 0, -1] */
if (cbf_node_get_axis_vector_and_offset(axes,"BEAM",vector,NULL)) {
if (cbf_node_get_axis_vector_and_offset(axes,"SOURCE",vector, NULL)) {
vector[0] = 0.;
vector[1] = 0.;
vector[2] = -1.;
} else {
vector[0] = -vector[0];
vector[1] = -vector[1];
vector[2] = -vector[2];
}
}
}
return error;
}
/* Extract the direction of gravity.
Attempt to read data from the 'GRAVITY' axis, if it exists, otherwise
attempt to read data from the 'UP' axis and multiply the direction by '-1'.
If neither of those axes can be found then assume that the gravity
direction is [0,-1,0]. The axis names are case-sensitive.
This reads data directly from the node tree, instead of using a handle.
\return An error code.
*/
static int cbf_node_get_gravity_dirn
(/* The axis category to read axes from. */
cbf_node * axes,
/* A place to store the 3-element direction vector. */
double vector[3])
{
int error = CBF_SUCCESS;
if (!axes || !vector) {
error |= CBF_ARGUMENT;
} else if (CBF_CATEGORY != axes->type) {
error |= CBF_ARGUMENT;
} else {
/* take gravity if given, -up if up is given, otherwise [0, -1, 0 ] */
if (cbf_node_get_axis_vector_and_offset(axes,"GRAVITY",vector,NULL)) {
if (cbf_node_get_axis_vector_and_offset(axes,"UP",vector,NULL)) {
vector[0] = 0.;
vector[1] = -1.;
vector[2] = 0.;
} else {
vector[0] = -vector[0];
vector[1] = -vector[1];
vector[2] = -vector[2];
}
}
}
return error;
}
/* Get the direction of the beam.
Locates the 'axis' category in the handle and then forwards to the
corresponding 'node' variant ('cbf_node_get_beam_dirn') to extract the
data.
\return An error code.
*/
static int cbf_get_beam_dirn
(cbf_handle handle,
double vector[3])
{
int error = CBF_SUCCESS;
if (!handle) {
error |= CBF_ARGUMENT;
} else {
cbf_node * axes = NULL;
cbf_reportnez(cbf_find_parent(&axes,handle->node,CBF_DATABLOCK),error);
cbf_reportnez(cbf_find_child(&axes,axes,"axis"),error);
cbf_reportnez(cbf_node_get_beam_dirn(axes,vector),error);
}
return error;
}
/* Get the direction of gravity.
Locates the 'axis' category in the handle and then forwards to the
corresponding 'node' variant ('cbf_node_get_gravity_dirn') to extract the
data.
\return An error code.
*/
static int cbf_get_gravity_dirn
(cbf_handle handle,
double vector[3])
{
int error = CBF_SUCCESS;
if (!handle) {
error |= CBF_ARGUMENT;
} else {
cbf_node * axes = NULL;
cbf_reportnez(cbf_find_parent(&axes,handle->node,CBF_DATABLOCK),error);
cbf_reportnez(cbf_find_child(&axes,axes,"axis"),error);
cbf_reportnez(cbf_node_get_gravity_dirn(axes,vector),error);
}
return error;
}
/* compute the transform from CBF vectors to NeXus vectors
Use the transpose to transfrom from NeXus vectors to CBF*/
int cbf_get_NX_axis_transform(cbf_handle handle,
double matrix [3][3]) {
double beam[3];
double gravity[3];
double x_nx[3], y_nx[3];
double normx_nx, normy_nx;
if (!handle || !matrix) return CBF_ARGUMENT;
/* cross multiple datablocks to find the AXIS category */
if (cbf_find_tag(handle,"_axis.id")) return CBF_NOTFOUND;
/* Get the 'beam' and 'gravity' directions, or usable defaults, from the current datablock. */
cbf_failnez(cbf_get_beam_dirn(handle,beam));
cbf_failnez(cbf_get_gravity_dirn(handle,gravity));
cbf_failnez(cbf_cross_product(beam,gravity,x_nx));
normx_nx = cbf_norm(x_nx);
if (normx_nx <= 1.e-38) return CBF_ARGUMENT;
cbf_failnez(cbf_scalar_product(1./normx_nx,x_nx,x_nx));
cbf_failnez(cbf_cross_product(beam,x_nx,y_nx));
normy_nx = cbf_norm(y_nx);
if (normy_nx <= 1.e-38) return CBF_ARGUMENT;
cbf_failnez(cbf_scalar_product(1./normy_nx,y_nx,y_nx));
matrix[0][0] = x_nx[0]; matrix[0][1] = x_nx[1]; matrix[0][2] = x_nx[2];
matrix[1][0] = y_nx[0]; matrix[1][1] = y_nx[1]; matrix[1][2] = y_nx[2];
matrix[2][0] = beam[0]; matrix[2][1] = beam[1]; matrix[2][2] = beam[2];
return CBF_SUCCESS;
}
/* Get a nexus detector name */
int cbf_get_NX_detector_name(cbf_h5handle h5handle,
unsigned int row,
const char * * detector_element) {
if (!h5handle || !detector_element ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_find_datablock(h5handle->scratch_tables,"scratch")
|| cbf_find_category(h5handle->scratch_tables,"scratch_detector")
|| cbf_find_column(h5handle->scratch_tables,"detector_element")
|| cbf_select_row(h5handle->scratch_tables,row)
|| cbf_get_value(h5handle->scratch_tables,detector_element)) {
return CBF_NOTFOUND;
}
return CBF_SUCCESS;
}
/* Set a nexus detector name */
int cbf_set_NX_detector_name(cbf_h5handle h5handle, const char * detector_element) {
if (!h5handle || !detector_element ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_require_datablock(h5handle->scratch_tables,"scratch")
|| cbf_require_category(h5handle->scratch_tables,"scratch_detector")
|| cbf_rewind_row(h5handle->scratch_tables)
|| cbf_require_column(h5handle->scratch_tables,"detector_element")) {
return CBF_FORMAT;
}
if (cbf_find_hashedvalue(h5handle->scratch_tables,detector_element,"detector_element",CBF_CASE_INSENSITIVE)) {
cbf_failnez(cbf_set_hashedvalue(h5handle->scratch_tables, detector_element,"detector_element",-1));
}
return CBF_SUCCESS;
}
int cbf_count_NX_detector_names(cbf_h5handle h5handle, unsigned int * count) {
if (!h5handle || !count ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_find_datablock(h5handle->scratch_tables,"scratch")
|| cbf_find_category(h5handle->scratch_tables,"scratch_detector")
|| cbf_find_column(h5handle->scratch_tables,"detector_element")
|| cbf_count_rows(h5handle->scratch_tables,count)) {
return CBF_FORMAT;
}
return CBF_SUCCESS;
}
/* get the Nexus axis path if previously established.
If not, try to create both the path and the intervening groups
*/
int cbf_require_NX_axis_path(cbf_handle handle,
cbf_h5handle h5handle,
const char * axis_id,
const char * * nexus_path) {
const char * axiselementid = NULL;
const char * axispath = NULL;
const char * diffrn_measurement_id = NULL;
const char * equipment = NULL;
const char * equipmentclass = NULL;
const char * equipmentcomponent = NULL;
const char * equipmentname = NULL;
const char * first_element_id = NULL;
const char * entryname = NULL;
const char * instrumentname = NULL;
char nxequipment[2048];
const char * path_axis_detector[8]
= {"(null)","(null)","(null)","(null)",
"(null)","(null)","(null)","(null)"};
const char * path_axis_general[8]
= {"(null)","(null)","(null)","(null)",
"(null)","(null)","(null)","(null)"};
const char * path_axis_goniometer[8]
= {"(null)","(null)","(null)","(null)",
"(null)","(null)","(null)","(null)"};
const char * path_parent_detector[6]
= {"(null)","(null)","(null)","(null)",
"(null)","(null)"};
const char * path_parent_general[6]
= {"(null)","(null)","(null)","(null)",
"(null)","(null)"};;
const char * path_parent_goniometer[6]
= {"(null)","(null)","(null)","(null)",
"(null)","(null)"};
const char * parentpath = NULL;
hid_t equipmentid;
hid_t instrumentid;
hid_t poiseid;
unsigned int elements = 0;
int errorcode = 0;
if (!cbf_get_NX_axis_path(h5handle,axis_id,nexus_path) &&
!(*nexus_path)) return CBF_SUCCESS;
if (h5handle->nxid_name) {
entryname = h5handle->nxid_name;
} else {
entryname = "entry";
}
if (h5handle->nxinstrument_name) {
instrumentname = h5handle->nxinstrument_name;
} else {
instrumentname = "instrument";
}
path_axis_general[0] = "";
path_axis_general[1] = entryname;
path_axis_general[2] = instrumentname;
path_axis_general[3] = "transformations";
path_axis_general[4] = 0;
path_axis_detector[0] = "";
path_axis_detector[1] = entryname;
path_axis_detector[2] = instrumentname;
path_axis_detector[3] = 0;
path_axis_goniometer[0] = "";
path_axis_goniometer[1] = entryname;
path_axis_goniometer[2] = "sample";
path_axis_goniometer[3] = 0;
path_parent_general[0] = "";
path_parent_general[1] = entryname;
path_parent_general[2] = instrumentname;
path_parent_general[3] = 0;
path_parent_general[4] = 0;
path_parent_detector[0] = "";
path_parent_detector[1] = entryname;
path_parent_detector[2] = instrumentname;
path_parent_detector[3] = 0;
path_parent_goniometer[0] = "";
path_parent_goniometer[1] = entryname;
path_parent_goniometer[2] = "sample";
path_parent_goniometer[3] = 0;
/* We will need an ID for the goniometer */
if (!(!cbf_find_category(handle,"diffrn_measurement") &&
!cbf_find_column(handle,"id") &&
!cbf_rewind_row(handle) &&
!cbf_get_value(handle,&diffrn_measurement_id) &&
diffrn_measurement_id)) {
diffrn_measurement_id = "goniometer";
}
/* We will need to use the instrument group or create it*/
cbf_reportnez(cbf_h5handle_require_entry(h5handle,0,0), errorcode);
cbf_reportnez(cbf_h5handle_require_instrument(h5handle,
&instrumentid,0),errorcode);
/* We will need scratch_tables */
cbf_reportnez(cbf_require_datablock(h5handle->scratch_tables,"scratch"),errorcode);
cbf_reportnez(cbf_require_category(h5handle->scratch_tables,"scratch_axis"),errorcode);
/* Try to find the row with the axis_id*/
cbf_reportnez(cbf_find_category(handle, "axis"),errorcode);
cbf_reportnez(cbf_find_column(handle,"id"),errorcode);
cbf_failnez(cbf_rewind_row(handle));
cbf_failnez(cbf_find_row(handle,axis_id));
/* Get the equipment for this axis
Note: This will fail to find a second instance of the axis with
different equipment */
if (!cbf_find_column(handle,"equipment")) {
cbf_reportnez(cbf_get_value(handle,&equipment),errorcode);
if (!equipment) equipment = "general";
}
if (!cbf_find_column(handle,"equipment_component")) {
cbf_reportnez(cbf_get_value(handle,&equipmentcomponent),errorcode);
if (!equipmentcomponent) equipmentcomponent = ".";
} else {
equipmentcomponent = ".";
}
/* count the detector elements and get the name of the first
detector element */
cbf_reportnez(cbf_count_elements(handle,&elements),errorcode);
first_element_id = NULL;
if (elements == 1) {
cbf_reportnez(cbf_get_element_id(handle,0,&first_element_id),errorcode);
if (!first_element_id
|| !cbf_cistrcmp(first_element_id,".")
|| !cbf_cistrcmp(first_element_id,"?"))
first_element_id = "detector";
} else if (elements == 0) {
first_element_id = "detector";
} else {
first_element_id = NULL;
}
/* We have the equipment type in equipment and the axis is in axis_id
If the equipment type is detector, we need to map the axis_id
to the appropriate detector so we can put this axis in
/instrument:NXinstrument
/ELEMENTNAME:NXdetector
/transformations:NXtransformations
/AXISID=[]
or
/instrument:NXinstrument
/DETECTORNAME:NXdetector_group
/transformations:NXtransformations
/AXISID=[]
or
/data:NXdata
/array_axes:NXtransformatioms
/AXISID=[[]
with the first used for all axes, except array axes when there
is only one detector element or if the axis is specific to a
particular element, and the second used for all other detector
axes except for array axes
If the equipment type is goniometer, we need to map the axis_id
to the appropriate goniometer, so we can put this axis in
/instrument:NXinstrument
/sample:NXsample
/GONIOMETERNAME:NXtransformations
/AXISID=[]
For other equipment types, we put this axis in
/instrument:NXinstrument
/transformations:NXtransformations
/AXISID=[]
*/
cbf_reportnez(cbf_get_axis_equipment_id(handle,&equipmentname,equipment,axis_id),errorcode);
if (cbf_cistrcmp(equipment,"detector")==0) {
nxequipment[0]='\0';
axiselementid = NULL;
if (elements < 2 ||
(!cbf_get_axis_element_id(handle,&axiselementid,
equipmentname,equipment,axis_id)
&& axiselementid && cbf_cistrcmp(axiselementid,".")
&& cbf_cistrcmp(axiselementid,"?"))) {
if (axiselementid
&& cbf_cistrcmp(axiselementid,".")
&& cbf_cistrcmp(axiselementid,"?")) {
strncat(nxequipment,axiselementid,2020);
} else {
_cbf_strcpy(nxequipment,first_element_id);
}
equipmentclass = "NXdetector";
} else {
if (equipmentname) {
strncat(nxequipment,equipmentname,2020);
} else {
_cbf_strcpy(nxequipment,"detector_group");
}
equipmentclass = "NXdetector_group";
}
path_parent_detector[3] = nxequipment;
path_axis_detector[3] = nxequipment;
path_parent_detector[4] = 0;
path_axis_detector[4] = "transformations";
path_axis_detector[5] = axis_id;
path_axis_detector[6] = 0;
/* {
int ii;
for (ii= 0; ii < 7; ii++) {
cbf_debug_print4("path_axis_detector [%d] length = %d, '%s'",
ii,_cbf_strlen(path_axis_detector[ii]),
path_axis_detector[ii]);
}
}
{
int ii;
for (ii= 0; ii < 5; ii++) {
cbf_debug_print4("path_parent_detector [%d] length = %d, '%s'",
ii,_cbf_strlen(path_parent_detector[ii]),
path_parent_detector[ii]);
}
} */
axispath = _cbf_str_join(path_axis_detector,'/');
parentpath = _cbf_str_join(path_parent_detector,'/');
cbf_reportnez(cbf_require_nxgroup(h5handle,
nxequipment, equipmentclass,
h5handle->nxinst, &equipmentid),errorcode);
cbf_reportnez(cbf_require_nxgroup(h5handle,
"transformations", "NXtransformations",
equipmentid, &poiseid),errorcode);
} else if (cbf_cistrcmp(equipment,"goniometer")==0) {
nxequipment[0]='\0';
_cbf_strcpy(nxequipment,"sample");
equipmentclass = "NXsample";
path_axis_goniometer[3] = diffrn_measurement_id;
path_parent_goniometer[4] = 0;
path_axis_goniometer[4] = axis_id;
path_axis_goniometer[5] = 0;
axispath = _cbf_str_join(path_axis_goniometer,'/');
parentpath = _cbf_str_join(path_parent_goniometer,'/');
cbf_reportnez(cbf_h5handle_require_sample(h5handle,0,nxequipment),errorcode);
cbf_reportnez(cbf_require_nxgroup(h5handle,
diffrn_measurement_id, "NXtransformations",
h5handle->nxsample, &poiseid),errorcode);
} else if (cbf_cistrcmp(equipment,"general")==0
|| cbf_cistrcmp(equipment,"beam")==0
|| cbf_cistrcmp(equipment,"gravity")==0){
_cbf_strcpy(nxequipment,"transformations");
equipmentclass = "NXtransformations";
path_axis_general[4] = axis_id;
path_axis_general[5] = 0;
axispath = _cbf_str_join(path_axis_general,'/');
parentpath = _cbf_str_join(path_parent_general,'/');
cbf_reportnez(cbf_require_nxgroup(h5handle,
"transformations", "NXtransformations",
h5handle->nxinst, &poiseid),errorcode);
} else {
if (!cbf_cistrcmp(equipmentcomponent,".")) {
equipmentcomponent = equipment;
equipment = "general";
}
_cbf_strcpy(nxequipment,"transformations");
equipmentclass = "NXtransformations";
path_axis_general[4] = axis_id;
path_axis_general[5] = 0;
axispath = _cbf_str_join(path_axis_general,'/');
parentpath = _cbf_str_join(path_parent_general,'/');
cbf_reportnez(cbf_require_nxgroup(h5handle,
"transformations", "NXtransformations",
h5handle->nxinst, &poiseid),errorcode);
}
cbf_reportnez(cbf_set_NX_axis_path(h5handle, axis_id, axispath),errorcode);
cbf_debug_print3("logged axis_id '%s' axis_path '%s'\n", axis_id, axispath);
if (axispath) cbf_free_text(&axispath,NULL);
cbf_reportnez(cbf_set_NX_parent_path(h5handle, axis_id, parentpath),errorcode);
cbf_debug_print3("logged axis_id '%s' parent_path '%s'\n", axis_id, parentpath);
if (parentpath) cbf_free_text(&parentpath,NULL);
cbf_reportnez(cbf_get_NX_axis_path(h5handle, axis_id, nexus_path),errorcode);
return errorcode;
}
/* Get the nexus path of an axis, if previously set. */
int cbf_get_NX_axis_path(cbf_h5handle h5handle, const char * axis_id, const char * * nexus_path) {
if (!h5handle || !axis_id || !nexus_path ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_find_datablock(h5handle->scratch_tables,"scratch")
|| cbf_find_category(h5handle->scratch_tables,"scratch_axis")
|| cbf_find_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",CBF_CASE_INSENSITIVE)
|| cbf_find_column(h5handle->scratch_tables,"nexus_path")
|| cbf_get_value(h5handle->scratch_tables,nexus_path)
|| !(*nexus_path)
|| !cbf_cistrcmp(*nexus_path,".")){
return CBF_NOTFOUND;
}
return CBF_SUCCESS;
}
/* Get the nexus poise path of an axis, if previously set. */
int cbf_get_NX_axis_poise_path(cbf_h5handle h5handle, const char * axis_id, const char * * poise_path) {
if (!h5handle || !axis_id || !poise_path ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_find_datablock(h5handle->scratch_tables,"scratch")
|| cbf_find_category(h5handle->scratch_tables,"scratch_axis")
|| cbf_find_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",CBF_CASE_INSENSITIVE)
|| cbf_find_column(h5handle->scratch_tables,"poise_path")
|| cbf_get_value(h5handle->scratch_tables,poise_path)
|| !(*poise_path)
|| !cbf_cistrcmp(*poise_path,".")){
return CBF_NOTFOUND;
}
return CBF_SUCCESS;
}
/* Set the parent path of an axis */
int cbf_set_NX_parent_path(cbf_h5handle h5handle, const char * axis_id, const char * parent_path) {
int errorcode = 0;
unsigned int rownum;
if (!h5handle || !axis_id || !parent_path ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_require_datablock(h5handle->scratch_tables,"scratch")
|| cbf_require_category(h5handle->scratch_tables,"scratch_axis")
|| cbf_require_column(h5handle->scratch_tables,"axis_id")
|| cbf_rewind_row(h5handle->scratch_tables)) {
return CBF_FORMAT;
}
if (cbf_find_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",CBF_CASE_INSENSITIVE)) {
cbf_failnez(cbf_find_category(h5handle->scratch_tables,"scratch_axis"));
cbf_failnez(cbf_set_hashedvalue(h5handle->scratch_tables, axis_id, "axis_id", -1));
}
cbf_failnez(cbf_row_number(h5handle->scratch_tables,&rownum));
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"parent_path"));
cbf_failnez(cbf_set_value(h5handle->scratch_tables, parent_path));
return errorcode;
}
/* Get the flag that this is an array_axis to the value given */
int cbf_get_NX_axis_array_axis(cbf_h5handle h5handle,
const char * axis_id, int *flag) {
int errorcode = 0;
if (!h5handle || !axis_id ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_require_datablock(h5handle->scratch_tables,"scratch")
|| cbf_require_category(h5handle->scratch_tables,"scratch_axis")
|| cbf_require_column(h5handle->scratch_tables,"axis_id")
|| cbf_rewind_row(h5handle->scratch_tables)) {
return CBF_FORMAT;
}
if (cbf_find_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",CBF_CASE_INSENSITIVE)) {
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"axis_id"));
cbf_failnez(cbf_set_hashedvalue(h5handle->scratch_tables, axis_id,"axis_id",-1));
}
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"is_array_axis"));
cbf_failnez(cbf_get_integervalue(h5handle->scratch_tables,flag));
return errorcode;
}
/* Set the flag that this is an array_axis to the value given */
int cbf_set_NX_axis_array_axis(cbf_h5handle h5handle,
const char * axis_id, const int flag) {
int errorcode = 0;
if (!h5handle || !axis_id ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_require_datablock(h5handle->scratch_tables,"scratch")
|| cbf_require_category(h5handle->scratch_tables,"scratch_axis")
|| cbf_require_column(h5handle->scratch_tables,"axis_id")
|| cbf_rewind_row(h5handle->scratch_tables)) {
return CBF_FORMAT;
}
if (cbf_find_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",CBF_CASE_INSENSITIVE)) {
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"axis_id"));
cbf_failnez(cbf_set_hashedvalue(h5handle->scratch_tables, axis_id, "axis_id", -1));
}
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"is_array_axis"));
cbf_failnez(cbf_set_integervalue(h5handle->scratch_tables,flag));
return errorcode;
}
/* increment the count of axes dependent on this axis */
int cbf_increment_NX_axis_depcount(cbf_h5handle h5handle, const char * axis_id) {
int ii;
int errorcode = 0;
if (!h5handle || !axis_id ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_require_datablock(h5handle->scratch_tables,"scratch")
|| cbf_require_category(h5handle->scratch_tables,"scratch_axis")
|| cbf_require_column(h5handle->scratch_tables,"axis_id")
|| cbf_rewind_row(h5handle->scratch_tables)) {
return CBF_FORMAT;
}
if (cbf_find_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",CBF_CASE_INSENSITIVE)) {
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"axis_id"));
cbf_failnez(cbf_set_hashedvalue(h5handle->scratch_tables, axis_id, "axis_id", -1));
}
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"depcount"));
if (cbf_get_integervalue(h5handle->scratch_tables, &ii)|| ii < 0) ii = 0;
cbf_failnez(cbf_set_integervalue(h5handle->scratch_tables,ii+1));
return errorcode;
}
/* Set the nexus path of an axis */
int cbf_set_NX_axis_path(cbf_h5handle h5handle, const char * axis_id, const char * nexus_path) {
int ii, klen;
int errorcode = 0;
unsigned int targetrow;
if (!h5handle || !axis_id || !nexus_path ) return CBF_ARGUMENT;
if (!(h5handle->scratch_tables)
|| cbf_require_datablock(h5handle->scratch_tables,"scratch")
|| cbf_require_category(h5handle->scratch_tables,"scratch_axis")
|| cbf_require_column(h5handle->scratch_tables,"axis_id")
|| cbf_rewind_row(h5handle->scratch_tables)) {
return CBF_FORMAT;
}
cbf_debug_print3("cbf_set_NX_axis_path '%s' '%s'",axis_id, nexus_path )
if (cbf_find_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",CBF_CASE_INSENSITIVE)) {
cbf_failnez(cbf_set_hashedvalue(h5handle->scratch_tables, axis_id,"axis_id",-1));
}
cbf_failnez(cbf_row_number(h5handle->scratch_tables,&targetrow));
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"nexus_path"));
cbf_failnez(cbf_select_row(h5handle->scratch_tables,targetrow));
cbf_failnez(cbf_set_value(h5handle->scratch_tables,nexus_path));
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"depcount"));
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"parent_path"));
cbf_debug_print2("cbf_set_NX_axis_path path set %x", errorcode);
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"poise_path"));
klen = _cbf_strlen(nexus_path);
CBF_START_ARRAY(char,poise_path,(klen+1));
poise_path[0] = '\0';
strncat(poise_path,nexus_path,klen);
poise_path[klen] = '\0';
cbf_debug_print2("cbf_set_NX_axis_path path set 10274 %x", errorcode);
for (ii = klen-1; ii >=0 && poise_path[ii] != '/'; ii--) poise_path[ii] = '\0';
if (ii >=0) poise_path[ii] = '\0';
cbf_failnez(cbf_select_row(h5handle->scratch_tables,targetrow));
cbf_failnez(cbf_set_value(h5handle->scratch_tables,poise_path));
for (ii = klen-1; ii >=0 && poise_path[ii] != '/'; ii--) poise_path[ii] = '\0';
if (ii >=0) poise_path[ii] = '\0';
cbf_failnez(cbf_require_column(h5handle->scratch_tables,"parent_path"));
cbf_failnez(cbf_set_value(h5handle->scratch_tables,poise_path));
CBF_END_ARRAY_REPORTNEZ(poise_path,errorcode);
cbf_debug_print2("cbf_set_NX_axis_path path set %x", errorcode);
return errorcode;
}
/* Write the HDF5 version of the NeXus axis definitions, if
the original CBF had axis definitions */
int cbf_write_h5nxaxes(cbf_handle handle, cbf_h5handle h5handle) {
int errorcode;
hid_t instrumentid;
unsigned int rows, row, nrow;
int idepcount;
double matrix[3][3];
double zero[1];
const char* datablock = NULL;
const char * diffrn_measurement_id = NULL;
const char * first_element_id = NULL;
const char * axis_id = NULL;
unsigned int elements = 0;
errorcode = 0;
zero[0] = 0.;
/* If we get a transform, there are axes to convert */
cbf_reportnez(cbf_get_NX_axis_transform(handle, matrix),errorcode);
if (errorcode) return errorcode;
/* We will need an ID for the goniometer */
if (!(!cbf_find_category(handle,"diffrn_measurement") &&
!cbf_find_column(handle,"id") &&
!cbf_rewind_row(handle) &&
!cbf_get_value(handle,&diffrn_measurement_id) &&
diffrn_measurement_id)) {
diffrn_measurement_id = "goniometer";
}
/* We will need to use the instrument group or create it*/
cbf_reportnez(cbf_h5handle_require_entry(h5handle,0,0), errorcode);
cbf_reportnez(cbf_h5handle_require_instrument(h5handle,
&instrumentid,0),errorcode);
cbf_reportnez(cbf_require_datablock(h5handle->scratch_tables,"scratch"),errorcode);
cbf_reportnez(cbf_require_category(h5handle->scratch_tables,"scratch_axis"),errorcode);
cbf_reportnez(cbf_find_category(handle, "axis"),errorcode);
cbf_reportnez(cbf_find_column(handle,"id"),errorcode);
rows = 0;
cbf_reportnez(cbf_count_rows(handle,&rows),errorcode);
cbf_reportnez(cbf_datablock_name(handle,&datablock),errorcode);
cbf_reportnez(cbf_count_elements(handle,&elements),errorcode);
first_element_id = NULL;
if (elements == 1) {
cbf_reportnez(cbf_get_element_id(handle,0,&first_element_id),errorcode);
if (!first_element_id
|| !cbf_cistrcmp(first_element_id,".")
|| !cbf_cistrcmp(first_element_id,"?"))
first_element_id = "detector";
} else if (elements == 0) {
first_element_id = "detector";
} else {
first_element_id = NULL;
}
for(row=0; row < rows; row++) {
char * cbfloc = NULL;
hid_t poiseid = CBF_H5FAIL;
hid_t equipmentid = CBF_H5FAIL;
hid_t nxaxisid = CBF_H5FAIL;
hid_t nxaxisendpointid = CBF_H5FAIL;
hid_t nxaxisrangeavgid = CBF_H5FAIL;
hid_t nxaxisrangetotid = CBF_H5FAIL;
hid_t dtype = CBF_H5FAIL, dspace = CBF_H5FAIL, dprop = CBF_H5FAIL;
hid_t dspaceavg = CBF_H5FAIL, dspacetot = CBF_H5FAIL;
const char * equipment = NULL;
const char * equipmentclass = NULL;
const char * equipmentcomponent = NULL;
const char * depends_on = NULL;
const char * rotation_axis = NULL;
const char * type = NULL;
const char * system = NULL;
const char * axisequipment = NULL;
const char * axispath = NULL;
char * axisendpointpath = NULL;
char * axisrangeavg = NULL;
char * axisrangetot = NULL;
const char * axispoisepath = NULL;
const char * axisendpointparts[3] = {NULL, NULL, NULL};
int isarrayaxis = 0;
int isscanaxis = 0;
hsize_t scanpoints = 0;
size_t sscanpoints = 0;
const char * units = NULL;
double vector[3] = {0.,0.,0.}, offset[3] = {0.,0.,0.};
double rotation = 0.;
hsize_t one = 1;
hsize_t two = 2;
int isrot;
CBF_UNUSED( axisrangetot );
CBF_UNUSED( axisrangeavg );
CBF_UNUSED( axisequipment );
CBF_UNUSED( equipmentclass );
CBF_UNUSED( dprop );
CBF_UNUSED( dtype );
CBF_UNUSED( nxaxisrangetotid );
CBF_UNUSED( nxaxisrangeavgid );
CBF_UNUSED( nxaxisendpointid );
CBF_UNUSED( zero );
CBF_UNUSED( one );
CBF_UNUSED( two );
cbf_reportnez(cbf_find_datablock(handle, datablock),errorcode);
cbf_reportnez(cbf_find_category(handle, "axis"),errorcode);
cbf_reportnez(cbf_find_column(handle,"id"),errorcode);
depends_on = ".";
rotation_axis = ".";
axisequipment = ".";
rotation = 0.0;
equipment = "general";
equipmentcomponent = ".";
equipmentclass = "NXtransformations";
type = "general";
system = "laboratory";
cbf_reportnez(cbf_select_row(handle,row),errorcode);
cbf_reportnez(cbf_find_column(handle,"id"),errorcode);
axis_id = NULL;
cbf_reportnez(cbf_get_value(handle,&axis_id),errorcode);
cbf_reportnez(cbf_require_column(h5handle->scratch_tables,"axis_id"),errorcode);
cbf_reportnez(cbf_rewind_row(h5handle->scratch_tables),errorcode);
if (cbf_find_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",CBF_CASE_INSENSITIVE)) {
cbf_reportnez(cbf_set_hashedvalue(h5handle->scratch_tables,axis_id,"axis_id",-1),errorcode);
}
if (!cbf_find_column(handle,"equipment")) {
cbf_reportnez(cbf_get_value(handle,&equipment),errorcode);
if (!equipment) equipment = "general";
}
if (!cbf_find_column(handle,"equipment_component")) {
cbf_reportnez(cbf_get_value(handle,&equipmentcomponent),errorcode);
if (!equipmentcomponent) equipmentcomponent = ".";
}
if (!cbf_find_column(handle,"depends_on")) {
cbf_reportnez(cbf_get_value(handle,&depends_on),errorcode);
if (!depends_on) depends_on = ".";
}
if (!cbf_find_column(handle,"rotation_axis")) {
cbf_reportnez(cbf_get_value(handle,&rotation_axis),errorcode);
if (!rotation_axis) rotation_axis = ".";
}
if (!cbf_find_column(handle,"rotation")) {
cbf_reportnez(cbf_get_doublevalue(handle,&rotation),errorcode);
}
if (!cbf_find_column(handle,"type")) {
cbf_reportnez(cbf_get_value(handle,&type),errorcode);
if (!type) type = "general";
} else {
type = "general";
}
isrot = 0;
if (cbf_cistrcmp(type,"rotation")) isrot = 1;
if (!cbf_find_column(handle,"system")) {
cbf_reportnez(cbf_get_value(handle,&system),errorcode);
if (!system||!system[0]) system = "laboratory";
if (cbf_cistrcmp(system,".")||cbf_cistrcmp(system,"?"))
system = "laboratory";
} else {
system = "laboratory";
}
if (cbf_cistrcmp(system,"laboratory")) {
cbf_reportnez(cbf_get_axis_vector_and_offset(handle,axis_id,
vector, offset),errorcode);
} else {
double cbfvector[3], cbfoffset[3];
cbf_reportnez(cbf_get_axis_vector_and_offset(handle,axis_id,
cbfvector, cbfoffset),errorcode);
system = "McStas_absolute";
cbf_reportnez(cbf_apply_matrix(matrix,cbfvector,vector),errorcode);
cbf_reportnez(cbf_apply_matrix(matrix,cbfoffset,offset),errorcode);
}
cbf_debug_print("calling cbf_require_NX_axis_path");
cbf_reportnez(cbf_require_NX_axis_path(handle,h5handle, axis_id, &axispath),errorcode);
axisendpointparts[0] = axispath;
axisendpointparts[1] = "end";
axisendpointparts[2] = NULL;
axisendpointpath = _cbf_str_join(axisendpointparts,'_');
axisendpointparts[1] = "increment_set";
axisrangeavg = _cbf_str_join(axisendpointparts,'_');
axisendpointparts[1] = "range_total";
axisrangetot = _cbf_str_join(axisendpointparts,'_');
cbf_reportnez(cbf_get_axis_parameters2(handle,
&sscanpoints,
&isarrayaxis,
&isscanaxis,
&units,
equipment,
axis_id),errorcode);
if (!isarrayaxis) {
cbf_reportnez(cbf_get_NX_axis_poise_path(h5handle,axis_id, &axispoisepath),errorcode);
cbf_reportnez(_cbf_NXGrequire(h5handle->hfile, &poiseid,
axispoisepath, "NXtransformations"),errorcode);
cbf_reportnez(cbf_set_NX_axis_array_axis(h5handle,axis_id,0),errorcode);
} else {
cbf_reportnez(cbf_set_NX_axis_array_axis(h5handle,axis_id,1),errorcode);
}
scanpoints = (hsize_t)sscanpoints;
/* At this point we are ready to write the field AXISID[] */
{
if (scanpoints > 0) {
hsize_t scanpointsfound = 0;
hid_t mtype = CBF_H5FAIL;
hsize_t chunk[] = {1};
hsize_t maxsize[] = {H5S_UNLIMITED};
size_t sscanpointsfound = 0;
size_t is;
double * scanarray = NULL;
double * scanendpointarray = NULL;
double range, scan_range_average;
double scan_range_total[2];
CBF_UNUSED( maxsize );
CBF_UNUSED( chunk );
CBF_UNUSED( mtype );
cbf_reportnez(cbf_alloc(((void **) &scanarray),NULL,
sizeof(double),scanpoints),errorcode);
cbf_reportnez(cbf_alloc(((void **) &scanendpointarray),NULL,
sizeof(double),scanpoints),errorcode);
cbf_reportnez(cbf_get_axis_scan_points2(handle,
scanarray,
scanendpointarray,
(size_t)scanpoints,
&sscanpointsfound,
&isarrayaxis,
&isscanaxis,
units,
axis_id),errorcode);
scanpointsfound = (hsize_t)sscanpointsfound;
scan_range_average = 0.;
scan_range_total[0] = 1.e10;
scan_range_total[1] = -1.e10;
if (!isarrayaxis) {
for (is=0; is < sscanpointsfound; is++) {
range = scanarray[is] - scanendpointarray[is];
if (range < 0.) range = -range;
if (isrot) {
while (range > 360.) range = range-360.;
if (range > 180.) range = 360 - range;
}
scan_range_average += range;
if (scanarray[is] < scan_range_total[0] )
scan_range_total[0] = scanarray[is];
if (scanendpointarray[is] < scan_range_total[0] )
scan_range_total[0] = scanendpointarray[is];
if (scanarray[is] > scan_range_total[1] )
scan_range_total[1] = scanarray[is];
if (scanendpointarray[is] > scan_range_total[1] )
scan_range_total[1] = scanendpointarray[is];
}
if (sscanpointsfound > 0 && scan_range_total[0] <= scan_range_total[1]) {
scan_range_average = scan_range_average/((double)sscanpointsfound);
} else {
scan_range_average = 0.;
}
}
if (sscanpointsfound==0) scanpointsfound=1;
if (scanpointsfound == 1) {
cbf_reportnez(cbf_add_h5double_dataset_slab(h5handle->hfile,
axispath,
scanarray[0],
h5handle->slice,
errorcode),errorcode);
} else {
cbf_reportnez(cbf_add_h5double_vector_dataset_slab(h5handle->hfile,
axispath,
scanarray,
scanpointsfound,
h5handle->slice,
errorcode),errorcode);
}
cbf_h5reportneg(nxaxisid = H5Dopen(h5handle->hfile,
axispath,H5P_DEFAULT),CBF_H5ERROR,errorcode);
/*
cbf_h5reportneg(dspace = H5Screate_simple(1,&scanpointsfound,maxsize),CBF_ALLOC,errorcode);
cbf_h5reportneg(dtype = H5Tcopy(H5T_IEEE_F64LE),CBF_ALLOC,errorcode);
cbf_h5reportneg(mtype = H5Tcopy(H5T_NATIVE_DOUBLE),CBF_ALLOC,errorcode);
cbf_h5reportneg(dprop = H5Pcreate(H5P_DATASET_CREATE),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Pset_chunk(dprop,1,chunk),CBF_ALLOC,errorcode);
cbf_h5reportneg(nxaxisid = H5Dcreatex(h5handle->hfile,axispath,dtype,dspace,dprop),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dwrite(nxaxisid, mtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, (void *)scanarray),CBF_ALLOC,errorcode);
*/
if (!isarrayaxis) {
if (scanpointsfound == 1) {
cbf_reportnez(cbf_add_h5double_dataset_slab(h5handle->hfile,
axisendpointpath,
scanendpointarray[0],
h5handle->slice,
errorcode),errorcode);
} else {
cbf_reportnez(cbf_add_h5double_vector_dataset_slab(h5handle->hfile,
axisendpointpath,
scanendpointarray,
scanpointsfound,
h5handle->slice,
errorcode),errorcode);
}
/*
cbf_h5reportneg(nxaxisendpointid = H5Dcreatex(h5handle->hfile,axisendpointpath,dtype,dspace,dprop),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dwrite(nxaxisendpointid, mtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, (void *)scanendpointarray),CBF_ALLOC,errorcode);
*/
/*if (scan_range_total[0] <= scan_range_total[1]) {
cbf_reportnez(cbf_add_h5double_dataset_slab(h5handle->hfile,
axisrangeavg,
scan_range_average,
h5handle->slice,
errorcode),errorcode);
cbf_reportnez(cbf_add_h5double_vector_dataset_slab(h5handle->hfile,
axisrangetot,
scan_range_total,
2,
h5handle->slice,
errorcode),errorcode);
cbf_h5reportneg(dspaceavg = H5Screate_simple(1,&one,&one),CBF_ALLOC,errorcode);
cbf_h5reportneg(nxaxisrangeavgid = H5Dcreatex(h5handle->hfile,axisrangeavg,dtype,dspaceavg,dprop),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dwrite(nxaxisrangeavgid, mtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, (void *)&scan_range_average),CBF_ALLOC,errorcode);
cbf_h5reportneg(dspacetot = H5Screate_simple(1,&two,&two),CBF_ALLOC,errorcode);
cbf_h5reportneg(nxaxisrangetotid = H5Dcreatex(h5handle->hfile,axisrangetot,dtype,dspacetot,dprop),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dwrite(nxaxisrangetotid, mtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, (void *)scan_range_total),CBF_ALLOC,errorcode);
if (cbf_H5Ivalid(dspaceavg)) { cbf_h5reportneg(H5Sclose(dspaceavg),CBF_ALLOC,errorcode); }
if (cbf_H5Ivalid(dspacetot)) { cbf_h5reportneg(H5Sclose(dspacetot),CBF_ALLOC,errorcode); }
cbf_h5reportneg(H5Dclose(nxaxisrangeavgid),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dclose(nxaxisrangetotid),CBF_ALLOC,errorcode);
} */
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"end_point",
axisendpointpath,
errorcode);
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"start_point",
axispath,
errorcode);
}
cbf_reportnez(cbf_free((void **)(&scanarray),NULL),errorcode);
cbf_reportnez(cbf_free((void **)(&scanendpointarray),NULL),errorcode);
if (cbf_H5Ivalid(dspace)) { cbf_h5reportneg(H5Sclose(dspace),CBF_ALLOC,errorcode); }
dspace = dspaceavg = dspacetot = CBF_H5FAIL;
/* cbf_h5reportneg(H5Tclose(dtype),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tclose(mtype),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Pclose(dprop),CBF_ALLOC,errorcode);
if (!isarrayaxis) {
cbf_h5reportneg(H5Dclose(nxaxisendpointid),CBF_ALLOC,errorcode);
}
*/
if (units) {
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"units",units,errorcode);
}
} else {
cbf_reportnez(cbf_add_h5double_dataset_slab(poiseid,
axis_id,
0.,
h5handle->slice,
errorcode),errorcode);
cbf_h5reportneg(nxaxisid = H5Dopen2(poiseid,axis_id, H5P_DEFAULT),CBF_NOTFOUND,errorcode);
/* const double zeropoint[1] = {0.};
const hsize_t scanpointsfound[1] = {1};
hid_t mtype;
hsize_t chunk[] = {1};
hsize_t maxsize[] = {H5S_UNLIMITED};
cbf_h5reportneg(dspace = H5Screate_simple(1,scanpointsfound,maxsize),CBF_ALLOC,errorcode);
cbf_h5reportneg(dtype = H5Tcopy(H5T_IEEE_F64LE),CBF_ALLOC,errorcode);
cbf_h5reportneg(mtype = H5Tcopy(H5T_NATIVE_DOUBLE),CBF_ALLOC,errorcode);
cbf_h5reportneg(dprop = H5Pcreate(H5P_DATASET_CREATE),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Pset_chunk(dprop,1,chunk),CBF_ALLOC,errorcode);
cbf_h5reportneg(nxaxisid = H5Dcreatex(poiseid,axis_id,dtype,dspace,dprop),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dwrite(nxaxisid, mtype, H5S_ALL, H5S_ALL, H5P_DEFAULT, (void *)zeropoint),CBF_ALLOC,errorcode);
*/
/* if (!errorcode) {
cbf_debug_print4("write axis %s data %d points to %s\n",axis_id,(int)scanpointsfound[0], axispath);
} */
/*
cbf_h5reportneg(H5Sclose(dspace),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tclose(dtype),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tclose(mtype),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Pclose(dprop),CBF_ALLOC,errorcode);
*/
}
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"long_name",
axis_id,
errorcode);
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"transformation_type",
type,
errorcode);
if (cbf_cistrcmp(system,"McStas_absolute")) {
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"system",system,errorcode);
}
if (cbf_norm(offset) > 1.e-20) {
errorcode |= cbf_apply_h5vector_attribute(nxaxisid,
"offset",(double *)offset,3,errorcode);
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"offset_units","mm",errorcode);
}
errorcode |= cbf_apply_h5vector_attribute(nxaxisid,
"vector",(double *)vector,3,errorcode);
if (!cbf_cistrcmp(depends_on,".")) {
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"depends_on",depends_on,errorcode);
} else {
const char * dopath;
int isarrayaxis;
cbf_debug_print("calling cbf_require_NX_axis_path");
errorcode |= cbf_require_NX_axis_path(handle, h5handle,depends_on,&dopath);
if (!errorcode) {
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"depends_on",
dopath,errorcode);
}
if (!cbf_get_NX_axis_array_axis(h5handle, axis_id, &isarrayaxis)
&& !isarrayaxis) {
errorcode |= cbf_increment_NX_axis_depcount(h5handle,depends_on);
}
}
if (cbf_cistrcmp(rotation_axis,".")) {
const char * rotpath;
cbf_debug_print("calling cbf_require_NX_axis_path");
errorcode |= cbf_require_NX_axis_path(handle, h5handle,rotation_axis,&rotpath);
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"rotation_axis",rotpath,errorcode);
errorcode |= cbf_apply_h5vector_attribute(nxaxisid,
"rotation",&rotation,1,errorcode);
errorcode |= cbf_increment_NX_axis_depcount(h5handle,rotation_axis);
}
if (equipment && cbf_cistrcmp(equipment,".")) {
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"equipment",equipment,errorcode);
}
if (equipmentcomponent && cbf_cistrcmp(equipmentcomponent,".")) {
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"equipment_component",equipmentcomponent,errorcode);
}
/* Uncomment to include a trace of the CBF origin of an axis
cbf_reportnez(cbf_location_string(datablock,"axis","vector",row,&cbfloc),errorcode);
errorcode |= cbf_apply_h5text_attribute(nxaxisid,
"cbf_location",
cbfloc,
errorcode);
*/
cbf_h5reportneg(H5Dclose(nxaxisid),CBF_FORMAT,errorcode);
cbf_reportnez(cbf_free((void **)&cbfloc,NULL),errorcode);
}
if (axisendpointpath) cbf_free_text((const char **)(&axisendpointpath),0);
if (cbf_H5Ivalid(poiseid)){
cbf_debug_print("calling H5Gclose");
cbf_h5reportneg(H5Gclose(poiseid),CBF_FORMAT,errorcode);
}
if (cbf_H5Ivalid(equipmentid)) {
cbf_h5reportneg(H5Gclose(equipmentid),CBF_FORMAT,errorcode);
}
}
/* Process the depends_on fields */
if (!(h5handle->scratch_tables)
|| cbf_require_datablock(h5handle->scratch_tables,"scratch")
|| cbf_require_category(h5handle->scratch_tables,"scratch_axis")
|| cbf_require_column(h5handle->scratch_tables,"axis_id")
|| cbf_rewind_row(h5handle->scratch_tables)) {
return CBF_FORMAT;
}
cbf_reportnez(cbf_count_rows(h5handle->scratch_tables,&rows),errorcode);
for(row=0; row < rows; row++) {
const char * parent_path;
const char * nexus_path;
int isarrayaxis;
cbf_reportnez(cbf_select_row(h5handle->scratch_tables, row),errorcode);
cbf_reportnez(cbf_find_column(h5handle->scratch_tables,"axis_id"),errorcode);
cbf_reportnez(cbf_get_value(h5handle->scratch_tables,&axis_id),errorcode);
cbf_reportnez(cbf_find_column(h5handle->scratch_tables,"depcount"),errorcode);
cbf_reportnez(cbf_get_integervalue(h5handle->scratch_tables,&idepcount),errorcode);
cbf_reportnez(cbf_find_column(h5handle->scratch_tables,"is_array_axis"),errorcode);
isarrayaxis = 0;
cbf_get_integervalue(h5handle->scratch_tables,&isarrayaxis);
cbf_debug_print4("axis_id = '%s', depcount = %d, isarrayaxid = %d",axis_id,idepcount, isarrayaxis);
if (idepcount == 0 && isarrayaxis == 0) {
parent_path = nexus_path = NULL;
cbf_reportnez(cbf_find_column(h5handle->scratch_tables,"parent_path"),errorcode);
cbf_reportnez(cbf_get_value(h5handle->scratch_tables,&parent_path),errorcode);
cbf_reportnez(cbf_find_column(h5handle->scratch_tables,"nexus_path"),errorcode);
cbf_reportnez(cbf_get_value(h5handle->scratch_tables,&nexus_path),errorcode);
/* cbf_debug_print3("parent_path = '%s', nexus_path = '%s'\n",parent_path,nexus_path); */
if (!nexus_path || !nexus_path[0]
|| !cbf_cistrcmp(nexus_path,".")
|| !cbf_cistrcmp(nexus_path,"?")){
nexus_path = ".";
}
if (parent_path && parent_path[0]
&& cbf_cistrcmp(parent_path,".")
&& cbf_cistrcmp(parent_path,"?")){
hid_t parentid;
htri_t dsexists;
parentid = CBF_H5FAIL;
cbf_reportnez(cbf_H5Grequire(h5handle->hfile,&parentid,parent_path),errorcode);
dsexists = H5Lexists(parentid,"depends_on",H5P_DEFAULT);
if (dsexists > 0 ) {
cbf_reportnez(cbf_require_h5text_dataset_slab(parentid,
"depends_on",
nexus_path,
errorcode),errorcode);
cbf_debug_print2("depends_on_path '%s'", nexus_path);
} else {
const char * nparent_path;
int done;
done = 0;
for (nrow = row+1; nrow < rows; nrow++) {
cbf_reportnez(cbf_select_row(h5handle->scratch_tables, nrow),errorcode);
cbf_reportnez(cbf_find_column(h5handle->scratch_tables,"depcount"),errorcode);
cbf_reportnez(cbf_get_integervalue(h5handle->scratch_tables,&idepcount),errorcode);
cbf_reportnez(cbf_find_column(h5handle->scratch_tables,"parent_path"),errorcode);
cbf_reportnez(cbf_get_value(h5handle->scratch_tables,&nparent_path),errorcode);
if (idepcount == 0 && nparent_path &&
!cbf_cistrcmp(nparent_path,parent_path)) {
cbf_reportnez(cbf_add_h5text_dataset(parentid,
"depends_on",
nexus_path,
errorcode),errorcode);
done = 1;
break;
}
}
if (!done) {
cbf_reportnez(cbf_add_h5text_dataset(parentid,
"depends_on",
nexus_path,
errorcode),errorcode);
}
}
if (cbf_H5Ivalid(parentid)) H5Gclose(parentid);
}
}
}
return errorcode;
}
/* apply a double vector attribute to a group or dataset */
int cbf_apply_h5vector_attribute(hid_t hid,
const char* attribname,
const double* attribvec,
const size_t dimension,
int errorcode)
{
htri_t attribexists;
hid_t attribspace, attribtype, attribid;
hid_t attribmemtype;
hsize_t dims[1];
attribspace = attribtype = attribmemtype = attribid = CBF_H5FAIL;
/* ensure arguments all given */
if (hid < 0 || !attribname|| !attribvec || dimension < 1 ) return CBF_ARGUMENT;
/* if the attribute exists, check the value is the same */
attribexists = H5Aexists(hid,attribname);
if (attribexists > 0) {
hsize_t attribsize = CBF_H5ERROR;
hsize_t memtype = CBF_H5ERROR;
size_t ii, rank, adim;
double oldvec[dimension];
CBF_UNUSED( adim );
CBF_UNUSED( attribsize );
for (ii = 0; ii < dimension; ii++) oldvec[ii] = -1.e38;
cbf_h5failneg(attribid = H5Aopen(hid, attribname, H5P_DEFAULT),CBF_H5ERROR);
cbf_h5reportneg(attribspace=H5Aget_space(attribid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(attribtype = H5Aget_type(attribid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(memtype = H5Tget_native_type(attribtype,H5T_DIR_ASCEND),CBF_H5ERROR,errorcode);
if (errorcode ==0 && H5Tequal(memtype,H5T_NATIVE_DOUBLE) <= 0) errorcode |= CBF_H5DIFFERENT;
if (errorcode == 0 )rank = H5Sget_simple_extent_ndims(attribspace);
cbf_h5reportneg(H5Sget_simple_extent_dims(attribspace, dims, NULL),CBF_H5ERROR,errorcode);
if (errorcode== 0 && (rank != 1 || dims[0] != dimension )) errorcode |= CBF_H5DIFFERENT;
cbf_h5reportneg(H5Aread(attribid,attribtype,oldvec),CBF_H5ERROR,errorcode);
for (ii=0; ii < dimension && errorcode == 0; ii++) {
if (fabs(oldvec[ii]-attribvec[ii]) > 1.e-38) errorcode |= CBF_H5DIFFERENT;
}
if (cbf_H5Ivalid(memtype)) H5Tclose(memtype);
if (cbf_H5Ivalid(attribspace)) H5Sclose(attribspace);
if (cbf_H5Ivalid(attribtype)) H5Tclose(attribtype);
if (cbf_H5Ivalid(attribid)) H5Aclose(attribid);
return errorcode;
}
cbf_h5reportneg(attribspace = H5Screate(H5S_SIMPLE),CBF_ALLOC,errorcode);
dims[0] = dimension;
cbf_h5reportneg(H5Sset_extent_simple(attribspace, 1, dims, NULL),CBF_FORMAT,errorcode);
cbf_h5reportneg(attribtype = H5Tcopy(H5T_IEEE_F64LE),CBF_ALLOC,errorcode);
cbf_h5reportneg(attribmemtype = H5Tcopy(H5T_NATIVE_DOUBLE),CBF_ALLOC,errorcode);
cbf_h5reportneg(attribid = H5Acreatex(hid,attribname,
attribtype,
attribspace,
H5P_DEFAULT),
CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Awrite(attribid,attribmemtype,
attribvec),CBF_ALLOC,errorcode);
if (attribspace >= 0) H5Sclose(attribspace);
if (attribtype >= 0) H5Tclose(attribtype);
if (attribmemtype >= 0) H5Tclose(attribmemtype);
if (attribid >= 0) H5Aclose(attribid);
return errorcode;
}
/* apply a long attribute to a group or dataset */
int cbf_apply_h5longasstr_attribute(hid_t hid,
const char* attribname,
const long attriblong,
int errorcode)
{
char buffer[20];
if (attriblong > -10 && attriblong < 10) {
sprintf(buffer,"%ld",attriblong);
} else {
sprintf(buffer,"0x%lx",attriblong);
}
return cbf_apply_h5text_attribute(hid,attribname,buffer,errorcode);
}
/* apply an integer attribute to a group or dataset as a string */
int cbf_apply_h5intasstr_attribute(hid_t hid,
const char* attribname,
const int attribint,
int errorcode)
{
char buffer[20];
if (attribint > -10 && attribint < 10) {
sprintf(buffer,"%d",attribint);
} else {
sprintf(buffer,"0x%x",attribint);
}
return cbf_apply_h5text_attribute(hid,attribname,buffer,errorcode);
}
/* apply a reference attribute to a group or dataset */
static int cbf_apply_h5reference_attribute(hid_t hid,
const char* attribname,
const hobj_ref_t attribref,
int errorcode)
{
hid_t attribspace, attribtype, attribid;
attribspace = attribtype = attribid = CBF_H5FAIL;
/* ensure arguments all given */
if (hid < 0 || !attribname ) return CBF_ARGUMENT;
cbf_h5reportneg(attribspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(attribtype = H5Tcopy(H5T_STD_REF_OBJ),CBF_ALLOC,errorcode);
cbf_h5reportneg(attribid = H5Acreatex(hid,attribname,
attribtype,
attribspace,
H5P_DEFAULT),
CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Awrite(attribid,attribtype,
&attribref),CBF_ALLOC,errorcode);
if (attribspace >= 0) H5Sclose(attribspace);
if (attribtype >= 0) H5Tclose(attribtype);
if (attribid >= 0) H5Aclose(attribid);
return errorcode;
}
/* apply a integer attribute to a group or dataset */
int cbf_apply_h5integer_attribute(hid_t hid,
const char* attribname,
const int attribint,
int errorcode)
{
hid_t attribspace, attribtype, attribid;
htri_t attribexists;
attribspace = attribtype = attribid = CBF_H5FAIL;
/* ensure arguments all given */
if (hid < 0 || !attribname ) return CBF_ARGUMENT;
/* if the attribute exists, check the value is the same */
attribexists = H5Aexists(hid,attribname);
if (attribexists > 0) {
hsize_t attribsize = 0;
hsize_t memtype = 0;
int oldattribint;
int errorcode = 0;
cbf_h5reportneg(attribid = H5Aopen(hid, attribname, H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_h5reportneg(attribtype = H5Aget_type(attribid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(attribsize = H5Tget_size(attribtype),CBF_H5ERROR,errorcode);
cbf_h5reportneg(memtype = H5Tget_native_type(attribtype,H5T_DIR_ASCEND),CBF_H5ERROR,errorcode);
if (errorcode || attribsize != 1 || !H5Tequal(attribtype,H5T_NATIVE_INT)) errorcode |= CBF_H5DIFFERENT;
cbf_h5reportneg(H5Aread(attribid,memtype,&oldattribint),CBF_H5ERROR,errorcode);
if (errorcode || oldattribint != attribint) errorcode |= CBF_H5DIFFERENT;
if (cbf_H5Ivalid(memtype)) H5Tclose(memtype);
if (cbf_H5Ivalid(attribtype)) H5Tclose(attribtype);
if (cbf_H5Ivalid(attribid)) H5Aclose(attribid);
return errorcode;
}
cbf_h5reportneg(attribspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(attribtype = H5Tcopy(H5T_NATIVE_INT),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(attribtype,1),CBF_ALLOC,errorcode);
cbf_h5reportneg(attribid = H5Acreatex(hid,attribname,
attribtype,
attribspace,
H5P_DEFAULT),
CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Awrite(attribid,attribtype,
&attribint),CBF_ALLOC,errorcode);
if (attribspace >= 0) H5Sclose(attribspace);
if (attribtype >= 0) H5Tclose(attribtype);
if (attribid >= 0) H5Aclose(attribid);
return errorcode;
}
/* apply a text attribute to a group or dataset */
int cbf_apply_h5text_attribute(hid_t hid,
const char* attribname,
const char* attribtext,
int errorcode)
{
hid_t attribspace, attribtype, attribid;
htri_t attribexists;
attribspace = attribtype = attribid = CBF_H5FAIL;
/* ensure arguments all given */
if (hid < 0 || !attribname || !attribtext ) return CBF_ARGUMENT;
/* if (!cbf_cistrcmp(attribname,"depends_on")) {
cbf_debug_print2("cbf_apply_h5text_attribute('depends_on','%s')\n",attribtext);
} */
/* if the attribute exists, check the value is the same */
attribexists = H5Aexists(hid,attribname);
if (attribexists > 0) {
hsize_t attribsize = 0;
hsize_t memtype = 0;
char * attribtextbuffer;
cbf_h5reportneg(attribid = H5Aopen(hid, attribname, H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_h5reportneg(attribtype = H5Aget_type(attribid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(attribsize = H5Tget_size(attribtype),CBF_H5ERROR,errorcode);
cbf_reportnez(cbf_alloc(((void **) &attribtextbuffer),NULL,
1,attribsize+1),errorcode);
cbf_h5reportneg(memtype = H5Tget_native_type(attribtype,H5T_DIR_ASCEND),CBF_H5ERROR,errorcode);
cbf_h5reportneg(H5Aread(attribid,memtype,attribtextbuffer),CBF_H5ERROR,errorcode);
if (cbf_cistrcmp(attribtext,attribtextbuffer)) {
errorcode |= CBF_H5DIFFERENT;
cbf_debug_print3("attribute text different '%s' '%s' ", attribtext, attribtextbuffer)
}
if (cbf_H5Ivalid(memtype)) H5Tclose(memtype);
if (cbf_H5Ivalid(attribtype)) H5Tclose(attribtype);
if (cbf_H5Ivalid(attribid)) H5Aclose(attribid);
if (attribtextbuffer) cbf_reportnez(cbf_free((void * *) &attribtextbuffer,NULL),errorcode);
return errorcode;
}
cbf_h5reportneg(attribspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(attribtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(attribtype,_cbf_strlen(attribtext)),CBF_ALLOC,errorcode);
cbf_h5reportneg(attribid = H5Acreatex(hid,attribname,
attribtype,
attribspace,
H5P_DEFAULT),
CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Awrite(attribid,attribtype,
(const void *)attribtext),CBF_ALLOC,errorcode);
if (attribspace >= 0) H5Sclose(attribspace);
if (attribtype >= 0) H5Tclose(attribtype);
if (attribid >= 0) H5Aclose(attribid);
return errorcode;
}
/* apply a text attribute slab to a dataset
Actually works with a text data set and makes
the attribute be an object reference to that
dataset. The name of the dataset is the
name of the attribute with the optional
prefix and suffix
*/
int cbf_add_h5text_attribute_slab(hid_t datasetid,
hid_t groupid,
const char* attributename,
const char* attributetext,
const char* datasetnameprefix,
const char* datasetnamesuffix,
const hsize_t slab,
int errorcode) {
const char * datasetname;
const char * datasetnameparts[4];
int iparts;
hobj_ref_t dsref = -1;
htri_t aexists = -1;
htri_t dsexists = -1;;
htri_t dsaexists = -1;
size_t length = 0, ii;
if (!cbf_H5Ivalid(datasetid)
|| !cbf_H5Ivalid(groupid)
|| !attributename
|| !attributetext) return CBF_ARGUMENT;
iparts = 0;
if (datasetnameprefix) datasetnameparts[iparts++] = datasetnameprefix;
datasetnameparts[iparts++] = attributename;
if (datasetnamesuffix) datasetnameparts[iparts++] = datasetnamesuffix;
datasetnameparts[iparts++] = 0;
datasetname = _cbf_str_join(datasetnameparts,'_');
aexists = H5Aexists(datasetid,attributename);
dsexists = H5Lexists(groupid,datasetname, H5P_DEFAULT);
dsaexists = H5Aexists(groupid,datasetname);
if ((dsexists <= 0 || aexists <= 0) && slab < 256 && _cbf_strlen(attributetext) < 255 ) {
errorcode |= cbf_add_h5text_list_attribute_slab(datasetid,attributename,attributetext,slab,errorcode);
cbf_free_text(&datasetname,NULL);
return errorcode;
} else if (aexists && dsexists <= 0) {
/* copy the small attribute form to the large one */
if (dsaexists > 0) {
cbf_h5reportneg(H5Adelete(groupid,datasetname),CBF_H5ERROR,errorcode);
dsaexists = 0;
}
if (!cbf_get_h5text_list_attribute_length(datasetid,attributename,&length,errorcode) && length > 0) {
CBF_START_ARRAY(char *,attriblist,length);
for (ii=0; ii < (size_t)length; ii++) attriblist[ii] = NULL;
cbf_reportnez(cbf_get_h5text_list_attribute(datasetid,attributename,length,(const char **)attriblist, errorcode),errorcode);
cbf_h5reportneg(H5Adelete(datasetid,attributename),CBF_H5ERROR,errorcode);
aexists = 0;
for (ii=0; ii < (size_t)length; ii++) {
if (attriblist[length-1-ii]) {
cbf_reportnez(cbf_add_h5text_dataset_slab(groupid,datasetname,attriblist[length-1-ii],length-1-ii,errorcode),errorcode);
cbf_free_text((const char * *)&(attriblist[length-1-ii]),NULL);
}
}
CBF_END_ARRAY_REPORTNEZ(attriblist,errorcode);
}
}
errorcode |= cbf_add_h5text_dataset_slab(groupid,datasetname,attributetext,slab,0);
if (aexists < 0 || !aexists ) {
cbf_h5reportneg(H5Rcreate(&dsref,groupid,datasetname,H5R_OBJECT,-1),CBF_H5ERROR,errorcode);
errorcode |= cbf_apply_h5reference_attribute(datasetid,attributename,dsref,errorcode);
}
cbf_free_text(&datasetname,NULL);
return errorcode;
}
/* get the length of text list attribute from a group or dataset
this uses fixed length strings*/
int cbf_get_h5text_list_attribute_length(hid_t hid,
const char* attribname,
size_t *length,
int errorcode)
{
hid_t attribspace, attribid;
htri_t attribexists;
int ndims = 0;
hsize_t attribdims[1];
hsize_t attribmaxdims[1];
if (!cbf_H5Ivalid(hid) || ! attribname || !length ) return CBF_ARGUMENT;
attribspace = attribid = CBF_H5FAIL;
*length = 0;
attribexists = H5Aexists(hid,attribname);
if (attribexists <= 0) {
return CBF_NOTFOUND;
}
cbf_h5reportneg(attribid = H5Aopen(hid,attribname,H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_h5reportneg(attribspace = H5Aget_space(attribid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(attribspace),CBF_FORMAT,errorcode);
if (ndims != 1) {
errorcode |= CBF_FORMAT;
}
if (errorcode == CBF_SUCCESS) {
cbf_h5reportneg(H5Sget_simple_extent_dims(attribspace,
attribdims,attribmaxdims),CBF_FORMAT,errorcode);
*length = attribdims[0];
}
if (cbf_H5Ivalid(attribid)) H5Aclose(attribid);
if (cbf_H5Ivalid(attribspace)) H5Sclose(attribspace);
return errorcode;
}
/* apply a text list attribute to a group or dataset
this uses fixed length strings*/
int cbf_apply_h5text_list_attribute(hid_t hid,
const char* attribname,
const size_t length,
const char** attriblist,
int errorcode)
{
hsize_t attribdims[1];
hid_t attribspace, attribtype, attribid;
htri_t attribexists;
int ii;
size_t width;
if (!cbf_H5Ivalid(hid) || ! attribname || length == 0 || !attriblist ) return CBF_ARGUMENT;
attribspace = attribtype = attribid = CBF_H5FAIL;
attribexists = H5Aexists(hid,attribname);
if (attribexists > 0) H5Adelete(hid,attribname);
width = 0;
for (ii=0; ii < (ssize_t)length; ii++) {
size_t swidth;
swidth = _cbf_strlen(attriblist[ii]);
if (swidth > width) width = swidth;
}
width ++;
cbf_h5reportneg(attribtype = H5Tcopy (H5T_C_S1), CBF_H5ERROR,errorcode);
cbf_h5reportneg(H5Tset_size(attribtype, width),CBF_H5ERROR,errorcode);
attribdims[0] = length;
cbf_h5reportneg(attribspace = H5Screate_simple(1,attribdims, NULL),CBF_H5ERROR,errorcode);
cbf_h5reportneg(attribid = H5Acreate2(hid, attribname, attribtype, attribspace, H5P_DEFAULT, H5P_DEFAULT),CBF_H5ERROR,errorcode);
{
CBF_START_ARRAY(char,membuf,length*width);
for (ii=0; ii < (ssize_t)length; ii++) {
_cbf_strncpy(membuf+ii*width,attriblist[ii],width);
}
cbf_h5reportneg(H5Awrite(attribid, attribtype, membuf),CBF_H5ERROR,errorcode);
CBF_END_ARRAY_REPORTNEZ(membuf,errorcode);
}
if (cbf_H5Ivalid(attribid)) H5Aclose(attribid);
if (cbf_H5Ivalid(attribtype)) H5Tclose(attribtype);
if (cbf_H5Ivalid(attribspace)) H5Sclose(attribspace);
return errorcode;
}
/* read a text list attribute from a group or dataset
this uses fixed length strings. To use this code,
The calling routine should first call
cbf_get_h5text_list_attribute_length and allocate
attribtext as an array of char * of that length.
The call to the cbf_get_h5text_list_attribute will allocate
the individual text strings.
*/
int cbf_get_h5text_list_attribute(hid_t hid,
const char* attribname,
const size_t length,
const char** attriblist,
int errorcode)
{
hid_t attribspace, attribtype, attribmemtype, attribid;
htri_t attribexists;
size_t width;
int ndims = 0;
int ii;
hsize_t attribdims[1];
hsize_t attribmaxdims[1];
if (!cbf_H5Ivalid(hid) || ! attribname || !length || !attriblist ) return CBF_ARGUMENT;
attribspace = attribid = attribtype = attribmemtype = CBF_H5FAIL;
for (ii = 0; ii < (ssize_t)length; ii++) attriblist[ii] = NULL;
attribexists = H5Aexists(hid,attribname);
if (attribexists <= 0) {
return CBF_NOTFOUND;
}
cbf_h5reportneg(attribid = H5Aopen(hid,attribname,H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_h5reportneg(attribtype = H5Aget_type(attribid),CBF_FORMAT,errorcode);
cbf_h5reportneg(attribmemtype = H5Tget_native_type(attribtype, H5T_DIR_ASCEND),CBF_FORMAT,errorcode);
width = H5Tget_size(attribtype);
cbf_h5reportneg(attribspace = H5Aget_space(attribid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(attribspace),CBF_FORMAT,errorcode);
if (ndims != 1) {
errorcode |= CBF_FORMAT;
}
if (errorcode == CBF_SUCCESS) {
cbf_h5reportneg(H5Sget_simple_extent_dims(attribspace,
attribdims,attribmaxdims),CBF_FORMAT,errorcode);
if (attribdims[0] > length) return CBF_FORMAT;
}
if (width == 0 || width > 257) return CBF_FORMAT;
if (width > 0 && attribdims[0] > 0) {
char* membuf=NULL;
cbf_failnez(cbf_alloc((void * *)(&membuf),NULL,1,length*width));
cbf_h5reportneg(H5Aread(attribid,attribmemtype,membuf),CBF_H5ERROR,errorcode);
for (ii = 0; ii < (ssize_t)attribdims[0]; ii++) {
cbf_failnez(cbf_alloc((void * *)(&(attriblist[ii])),NULL,1,width+1));
_cbf_strncpy((char *)(attriblist[ii]),membuf+width*ii,width);
((char *)attriblist[ii])[width] = '\0';
}
free(membuf);
}
if (cbf_H5Ivalid(attribid)) H5Aclose(attribid);
if (cbf_H5Ivalid(attribtype)) H5Tclose(attribtype);
if (cbf_H5Ivalid(attribmemtype)) H5Tclose(attribmemtype);
if (cbf_H5Ivalid(attribspace)) H5Sclose(attribspace);
return errorcode;
}
/* add a text list attribute slab to a text list attribue of a group or dataset
this uses fixed length strings*/
int cbf_add_h5text_list_attribute_slab(hid_t hid,
const char* attribname,
const char* attribtext,
const hsize_t slab,
int errorcode)
{
htri_t attribexists;
hsize_t ii;
size_t length;
if (!cbf_H5Ivalid(hid) || ! attribname || !attribtext ) return CBF_ARGUMENT;
attribexists = H5Aexists(hid,attribname);
if (attribexists < 0 || !attribexists) {
CBF_START_ARRAY(char *, attriblist, slab+1);
for (ii=0; ii < slab; ii++) {
attriblist[ii] = "";
}
attriblist[slab] = (char *)attribtext;
cbf_reportnez(cbf_apply_h5text_list_attribute(hid,attribname,slab+1,(const char **)attriblist,errorcode),errorcode);
CBF_END_ARRAY_REPORTNEZ(attriblist,errorcode);
return errorcode;
}
cbf_failnez(cbf_get_h5text_list_attribute_length(hid,attribname,&length,errorcode));
if (length > slab ) {
char * bumped;
CBF_START_ARRAY(char *, attriblist, length);
cbf_onfailnez(cbf_get_h5text_list_attribute(hid,attribname,length,(const char **)attriblist,errorcode),cbf_free((void **)(&attriblist),NULL));
bumped = attriblist[slab];
attriblist[slab] = (char *)attribtext;
cbf_onfailnez(cbf_apply_h5text_list_attribute(hid,attribname,length,(const char **)attriblist,errorcode),cbf_free((void **)(&attriblist),NULL));
attriblist[slab] = bumped;
for (ii=0; ii < (size_t)length; ii++) if (attriblist[ii]) free(attriblist[ii]);
CBF_END_ARRAY_REPORTNEZ(attriblist,errorcode);
} else {
CBF_START_ARRAY(char *, attriblist, slab+1);
cbf_onfailnez(cbf_get_h5text_list_attribute(hid,attribname,length,(const char **)attriblist,errorcode),cbf_free((void **)(&attriblist),NULL));
for (ii = length; ii < slab; ii++) attriblist[ii] = "";
attriblist[slab] = (char *)attribtext;
cbf_onfailnez(cbf_apply_h5text_list_attribute(hid,attribname,slab+1,(const char **)attriblist,errorcode),cbf_free((void **)(&attriblist),NULL));
for (ii=0; ii < (size_t)length; ii++) if (attriblist[ii]) free(attriblist[ii]);
CBF_END_ARRAY_REPORTNEZ(attriblist,errorcode);
}
return errorcode;
}
/* Get the rank of a dataset */
int cbf_get_h5dataset_rank(hid_t hid, const char * datasetname, size_t * rank) {
hid_t datasetspace, datasetid;
htri_t dsexists;
int errorcode = 0;
int ndims = 0;
if (!cbf_H5Ivalid(hid) || !datasetname || !rank ) return CBF_ARGUMENT;
datasetspace = datasetid = CBF_H5FAIL;
dsexists = H5Lexists(hid,datasetname, H5P_DEFAULT);
if (dsexists < 0 ||
!dsexists
|| (datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT))< 0) {
return CBF_NOTFOUND;
}
if (datasetid <= 0) {
datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT);
}
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if ( cbf_H5Ivalid(datasetspace) ) H5Sclose(datasetspace);
if ( cbf_H5Ivalid(datasetid) ) H5Dclose(datasetid);
if (ndims < 0) return CBF_FORMAT;
*rank = ndims;
return CBF_SUCCESS;
}
/* Get the dimensions of a dataset */
int cbf_get_h5dataset_dims(hid_t hid, const char * datasetname, size_t rank,
size_t * dims,
size_t * maxdims) {
hid_t datasetspace, datasetid;
htri_t dsexists;
int errorcode=0;
int ndims = 0;
if (!cbf_H5Ivalid(hid) || !datasetname || rank == 0 ) return CBF_ARGUMENT;
else {
size_t ii;
CBF_START_ARRAY(hsize_t,h5dims,rank);
CBF_START_ARRAY(hsize_t,h5maxdims,rank);
datasetspace = datasetid = CBF_H5FAIL;
dsexists = H5Lexists(hid,datasetname, H5P_DEFAULT);
if (dsexists < 0 ||
!dsexists
|| (datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT))< 0) {
return CBF_NOTFOUND;
}
if (datasetid <= 0) {
datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT);
}
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if (ndims <= (ssize_t)rank) {
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,h5dims,h5maxdims),CBF_FORMAT,errorcode);
}
if ( cbf_H5Ivalid(datasetspace) ) H5Sclose(datasetspace);
if ( cbf_H5Ivalid(datasetid) ) H5Dclose(datasetid);
if (ndims < 0) return CBF_FORMAT;
for (ii = 0; ii < (size_t)ndims; ii ++) {
if (dims) dims[ii] = h5dims[ii];
if (maxdims) maxdims[ii] = h5maxdims[ii];
}
CBF_END_ARRAY_REPORTNEZ( h5maxdims, errorcode );
CBF_END_ARRAY_REPORTNEZ( h5dims, errorcode );
return CBF_SUCCESS;
}
}
/* find a text dataset slab in a dataset in a group, with slab location
places the specified datasettext in some slab of the
specified datasetname for group hid. The slab where the text is
found or placed is reported in textslab.
If the new text is the same as the existing text in any slab, nothing changes.
A case-insensitive compare is used.
The slabs are indexed from 0
*/
int cbf_find_h5text_dataset_slab(hid_t hid,
const char* datasetname,
const char* datasettext,
hsize_t * textslab,
int errorcode)
{
hid_t datasetspace, datasettype, datasetid;
hid_t memspace, memtype;
int ndims = 0;
hsize_t offset[1] = {0};
hsize_t stride[1] = {1};
hsize_t count[1] = {1};
hsize_t chunk[1] = {1};
hsize_t curdim[1];
hsize_t memsize[1] = {1};
htri_t dsexists;
hsize_t dssize[1];
hsize_t maxdssize[1];
hsize_t dsdims[1];
hsize_t dsmaxdims[1];
hsize_t dsslab;
void * datasettextbuffer;
size_t text_size, old_size, new_size;
CBF_UNUSED( maxdssize );
CBF_UNUSED( dssize );
CBF_UNUSED( memsize );
CBF_UNUSED( curdim );
CBF_UNUSED( chunk );
datasetspace = datasettype = memspace = memtype = CBF_H5FAIL;
datasetid = CBF_H5FAIL;
memsize[0] = 1;
dssize[0] = 1;
maxdssize[0] = H5S_UNLIMITED;
chunk[0] = 1;
datasettextbuffer = NULL;
/* ensure arguments all given */
if (hid < 0 || !datasetname ||
!datasettext || errorcode ) return CBF_ARGUMENT;
text_size = _cbf_strlen(datasettext);
dsexists = H5Lexists(hid,datasetname, H5P_DEFAULT);
if (dsexists < 0 ||
!dsexists
|| (datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT))< 0) {
/* If there is no dataset, return CBF_NOTFOUND. */
return CBF_NOTFOUND;
}
if (datasetid <= 0) {
datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT);
}
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if ( errorcode || ndims > 1 ) return CBF_FORMAT;
old_size = H5Tget_size(datasettype);
new_size = text_size+1;
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,
dsdims,dsmaxdims),CBF_FORMAT,errorcode);
cbf_reportnez(cbf_alloc(&datasettextbuffer,NULL,1,old_size+1),errorcode);
if ( old_size < new_size ) {
errorcode = CBF_NOTFOUND;
} else if ( ndims == 0 ) {
cbf_h5reportneg(memtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(memspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Sselect_all(datasetspace),CBF_H5ERROR,errorcode);
cbf_h5reportneg(H5Tset_size(memtype,old_size+1),CBF_ALLOC,errorcode);
if(H5Dread(datasetid, memtype, memspace, datasetspace, H5P_DEFAULT, (void *)datasettextbuffer) >= 0 ) {
if (memspace >= 0) H5Sclose(memspace);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
if (cbf_cistrcmp(datasettextbuffer,datasettext)==0){
if (textslab) *textslab = (hsize_t)0L;
errorcode = CBF_SUCCESS;
} else {
errorcode |= CBF_NOTFOUND;
}
if (datasettextbuffer) cbf_free(&datasettextbuffer,NULL);
return errorcode;
}
} else {
cbf_h5reportneg(memtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(memspace = H5Screate_simple(ndims,count,0),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(memtype,old_size+1),CBF_ALLOC,errorcode);
for (dsslab=0; dsslab < dsdims[0] && errorcode == CBF_SUCCESS; dsslab++) {
offset[0] = dsslab;
cbf_h5reportneg(H5Sselect_hyperslab(datasetspace,H5S_SELECT_SET,
offset,stride,count,0), CBF_FORMAT,errorcode);
if (H5Dread(datasetid, memtype, memspace, datasetspace,
H5P_DEFAULT, (void *)datasettextbuffer)>=0) {
if (cbf_cistrcmp(datasettextbuffer,datasettext)==0){
if (memspace >= 0) H5Sclose(memspace);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
if (datasettextbuffer) cbf_free(&datasettextbuffer,NULL);
if (textslab) *textslab = dsslab;
return CBF_SUCCESS;
}
curdim[0] = dsslab+1;
}
}
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
datasetspace = memtype = datasetid = CBF_H5FAIL;
}
if (datasetspace >=0 )H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
if (datasettextbuffer) cbf_free(&datasettextbuffer,NULL);
return CBF_NOTFOUND;
}
/* require a text dataset slab in a dataset in a group
places the specified datasettext in some slab of the
specified datasetname for group hid. The dataset is created
if it does not already exist.
If the new text is the same as the existing text in any slab, nothing changes.
A case-insensitive compare is used.
The first slab goes in rank = 0. Additional slabs go in rank 1.
If the new text is different, a new slab is appended.
The slabs are indexed from 0
*/
int cbf_require_h5text_dataset_slab(hid_t hid,
const char* datasetname,
const char* datasettext,
int errorcode)
{
hid_t datasetspace, datasettype, datasetid;
hid_t memspace, memtype;
hid_t ndatasettype;
hid_t ndatasetspace;
hid_t nmemtype;
int ndims = 0;
hsize_t offset[1] = {0};
hsize_t stride[1] = {1};
hsize_t count[1] = {1};
hsize_t chunk[1] = {1};
hsize_t curdim[1];
hsize_t memsize[1] = {1};
htri_t dsexists;
hsize_t dssize[1];
hsize_t maxdssize[1];
hsize_t dsdims[1];
hsize_t dsmaxdims[1];
hsize_t dsslab;
hid_t anondataset;
void * datasettextbuffer;
size_t text_size, old_size, new_size, ii;
CBF_UNUSED( dssize );
CBF_UNUSED( dsexists );
CBF_UNUSED( count );
CBF_UNUSED( stride );
CBF_UNUSED( ndims );
CBF_UNUSED( ndatasetspace );
datasetspace = datasettype = memspace = memtype = CBF_H5FAIL;
ndatasetspace = ndatasettype = nmemtype = CBF_H5FAIL;
datasetid = CBF_H5FAIL;
memsize[0] = 1;
dssize[0] = 1;
maxdssize[0] = H5S_UNLIMITED;
chunk[0] = 1;
datasettextbuffer = NULL;
/* ensure arguments all given */
if (hid < 0 || !datasetname ||
!datasettext || errorcode) return CBF_ARGUMENT;
text_size = _cbf_strlen(datasettext);
dsexists = H5Lexists(hid,datasetname, H5P_DEFAULT);
if (dsexists < 0 ||
!dsexists
|| (datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT))< 0) {
/* If there is no dataset, just create it and exit. */
return cbf_add_h5text_dataset(hid, datasetname, datasettext, errorcode);
}
if (datasetid <= 0) {
datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT);
}
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if ( errorcode || ndims > 1 ) return CBF_FORMAT;
old_size = H5Tget_size(datasettype);
new_size = text_size+1;
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,
dsdims,dsmaxdims),CBF_FORMAT,errorcode);
if ( old_size < new_size || ndims == 0 ) {
ndatasettype = CBF_H5FAIL;
cbf_h5reportneg(ndatasettype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
/* rebuild the dataset with longer strings */
if (old_size+(old_size>>1)+1 >= new_size) {
new_size = old_size+(old_size>>1)+1;
}
cbf_reportnez(cbf_alloc(&datasettextbuffer,NULL,
1,new_size),errorcode);
if (datasettype >= 0) H5Tclose(datasettype);
datasettype = ndatasettype;
cbf_h5reportneg(H5Tset_size(datasettype,new_size),CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(nmemtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
if (ndims==0) {
cbf_h5reportneg(memspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
dsdims[0] = 0;
dsmaxdims[0] = H5S_UNLIMITED;
} else {
cbf_h5reportneg(memspace = H5Screate_simple(1,memsize,NULL),CBF_ALLOC,errorcode);
}
cbf_h5reportneg(H5Tset_size(memtype,new_size),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(nmemtype,new_size),CBF_ALLOC,errorcode);
cbf_reportnez(cbf_H5Dcreate(hid,&anondataset,NULL,1,dsdims,maxdssize,chunk,datasettype),errorcode);
/* If we had a rank 0 dataset, leave it alone if the text is already in
or convert to rank 1 if the text is new */
if (ndims == 0) {
if(H5Dread(datasetid, memtype, memspace, datasetspace, H5P_DEFAULT, (void *)datasettextbuffer) >= 0 ) {
if (cbf_cistrcmp(datasettextbuffer,datasettext)==0){
if (nmemtype >= 0) H5Tclose(nmemtype);
if (ndatasetspace >= 0) H5Sclose(ndatasetspace);
if (memspace >= 0) H5Sclose(memspace);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (anondataset >= 0) H5Dclose(anondataset);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
return CBF_SUCCESS;
}
curdim[0] = 1;
cbf_h5reportneg(H5Dset_extent(anondataset,curdim),CBF_FORMAT,errorcode);
offset[0] = 0;
stride[0] = 1;
count[0] = 1;
cbf_reportnez(cbf_H5Dwrite2(anondataset,offset,stride,count,(void *)datasettextbuffer,nmemtype),errorcode);
dsdims[0] = 1;
curdim[0] = 1;
}
/* If we have a rank 1 dataset, leave it alone if the new text matches one of the existing lines,
otherwise copy the existing lines over */
} else {
for (dsslab=0; dsslab < dsdims[0]; dsslab++) {
offset[0] = dsslab;
cbf_h5reportneg(H5Sselect_hyperslab(datasetspace,H5S_SELECT_SET,
offset,stride,count,0), CBF_FORMAT,errorcode);
if (H5Dread(datasetid, memtype, memspace, datasetspace,
H5P_DEFAULT, (void *)datasettextbuffer)>=0) {
if (cbf_cistrcmp(datasettextbuffer,datasettext)==0){
if (nmemtype >= 0) H5Tclose(nmemtype);
if (ndatasetspace >= 0) H5Sclose(ndatasetspace);
if (memspace >= 0) H5Sclose(memspace);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (anondataset >= 0) H5Dclose(anondataset);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
return CBF_SUCCESS;
}
curdim[0] = dsslab+1;
cbf_h5reportneg(H5Dset_extent(anondataset,curdim),CBF_FORMAT,errorcode);
offset[0] = dsslab;
stride[0] = 1;
count[0] = 1;
cbf_reportnez(cbf_H5Dwrite2(anondataset,offset,stride,count,(void *)datasettextbuffer,nmemtype),errorcode);
}
}
}
if (nmemtype >= 0) H5Tclose(nmemtype);
if (ndatasetspace >= 0) H5Sclose(ndatasetspace);
if (datasetspace >= 0) H5Sclose(datasetspace);
cbf_h5reportneg(H5Ldelete(hid,datasetname,H5P_DEFAULT), CBF_FORMAT, errorcode);
cbf_h5reportneg(H5Olink(anondataset,hid,datasetname,H5P_DEFAULT,H5P_DEFAULT), CBF_FORMAT, errorcode);
if (datasetid >= 0) H5Dclose(datasetid);
datasetid = anondataset;
if (memtype >= 0) H5Tclose(memtype);
datasetspace = memtype = CBF_H5FAIL;
old_size = new_size;
}
if (datasetspace >=0 )H5Sclose(datasetspace);
datasetspace = CBF_H5FAIL;
offset[0] = dsdims[0];
curdim[0] = dsdims[0]+1;
cbf_h5reportneg(H5Dset_extent(datasetid,curdim),CBF_FORMAT,errorcode);
stride[0] = 1;
count[0] = 1;
if (!datasettextbuffer) {
cbf_reportnez(cbf_alloc(&datasettextbuffer,NULL,
1,old_size),errorcode);
}
for (ii = 0; ii < text_size; ii++) {
((char *)datasettextbuffer)[ii] = datasettext[ii];
}
for (ii = text_size; ii < old_size; ii++) {
((char *)datasettextbuffer)[ii] = '\0';
}
/* cbf_debug_print4(" cbf_require_h5text_dataset_slab, text: '%s', oldsize: %d, newsize: %d", (char *)datasettextbuffer, old_size, new_size); */
cbf_reportnez(cbf_H5Dwrite2(datasetid,offset,stride,count,datasettextbuffer,datasettype),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
if (datasettextbuffer) cbf_free(&datasettextbuffer,NULL);
return errorcode;
}
/* apply a text dataset slab to dataset in a group
places the specified datasettext in the specified slab of the
specified datasetname for group hid. The dataset is created
if it does not already exist.
The slabs are indexed from 0
*/
int cbf_add_h5text_dataset_slab(hid_t hid,
const char* datasetname,
const char* datasettext,
const hsize_t slab,
int errorcode)
{
hid_t datasetspace, datasettype, datasetid;
hid_t memspace, memtype;
hid_t ndatasettype;
hid_t ndatasetspace;
hid_t nmemtype;
int ndims = 0;
hsize_t offset[1] = {0};
hsize_t stride[1] = {1};
hsize_t count[1] = {1};
hsize_t chunk[1] = {1};
hsize_t curdim[1];
hsize_t memsize[1] = {1};
htri_t dsexists;
hsize_t dssize[1];
hsize_t maxdssize[1];
hsize_t dsdims[1];
hsize_t dsmaxdims[1];
hsize_t dsslab;
hid_t anondataset;
void * datasettextbuffer;
size_t text_size, old_size, new_size, ii;
datasetspace = datasettype = memspace = memtype = CBF_H5FAIL;
ndatasetspace = ndatasettype = nmemtype = CBF_H5FAIL;
datasetid = CBF_H5FAIL;
memsize[0] = 1;
dssize[0] = 1;
maxdssize[0] = H5S_UNLIMITED;
chunk[0] = 1;
datasettextbuffer = NULL;
/* ensure arguments all given */
if (hid < 0 || !datasetname ||
!datasettext || errorcode) return CBF_ARGUMENT;
text_size = _cbf_strlen(datasettext);
dsexists = H5Lexists(hid,datasetname, H5P_DEFAULT);
if (dsexists < 0 ||
!dsexists
|| (datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT))< 0) {
/* Create the dataset if we were unable to open it */
cbf_h5reportneg(datasettype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(datasettype,text_size+1),CBF_ALLOC,errorcode);
cbf_reportnez(cbf_H5Dcreate(hid,&datasetid,datasetname,1,dssize,maxdssize,chunk,datasettype),errorcode);
curdim[0] = 0;
dsdims[0] = 0;
}
if (datasetid <= 0) {
datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT);
}
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if ( errorcode || ndims > 1 ) return CBF_FORMAT;
old_size = H5Tget_size(datasettype);
new_size = text_size+1;
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,
dsdims,dsmaxdims),CBF_FORMAT,errorcode);
if ( old_size < new_size || ndims == 0 ) {
ndatasettype = CBF_H5FAIL;
cbf_h5reportneg(ndatasettype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
/* rebuild the dataset with longer strings */
if (old_size+(old_size>>1)+1 >= new_size) {
new_size = old_size+(old_size>>1)+1;
}
cbf_reportnez(cbf_alloc(&datasettextbuffer,NULL,
1,new_size),errorcode);
if (datasettype >= 0) H5Tclose(datasettype);
datasettype = ndatasettype;
cbf_h5reportneg(H5Tset_size(datasettype,new_size),CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(nmemtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
if (ndims==0) {
cbf_h5reportneg(memspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
dsdims[0] = 0;
dsmaxdims[0] = H5S_UNLIMITED;
} else {
cbf_h5reportneg(memspace = H5Screate_simple(1,memsize,NULL),CBF_ALLOC,errorcode);
}
cbf_h5reportneg(H5Tset_size(memtype,new_size),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(nmemtype,new_size),CBF_ALLOC,errorcode);
cbf_reportnez(cbf_H5Dcreate(hid,&anondataset,NULL,1,dsdims,maxdssize,chunk,datasettype),errorcode);
if (ndims == 0) {
if(H5Dread(datasetid, memtype, memspace, datasetspace, H5P_DEFAULT, (void *)datasettextbuffer) >= 0 ) {
curdim[0] = 1;
cbf_h5reportneg(H5Dset_extent(anondataset,curdim),CBF_FORMAT,errorcode);
offset[0] = 0;
stride[0] = 1;
count[0] = 1;
cbf_reportnez(cbf_H5Dwrite2(anondataset,offset,stride,count,(void *)datasettextbuffer,nmemtype),errorcode);
dsdims[0] = 1;
curdim[0] = 1;
}
} else {
for (dsslab=0; dsslab < dsdims[0]; dsslab++) {
offset[0] = dsslab;
cbf_h5reportneg(H5Sselect_hyperslab(datasetspace,H5S_SELECT_SET,
offset,stride,count,0), CBF_FORMAT,errorcode);
if (H5Dread(datasetid, memtype, memspace, datasetspace,
H5P_DEFAULT, (void *)datasettextbuffer)>=0) {
curdim[0] = dsslab+1;
cbf_h5reportneg(H5Dset_extent(anondataset,curdim),CBF_FORMAT,errorcode);
offset[0] = dsslab;
stride[0] = 1;
count[0] = 1;
cbf_reportnez(cbf_H5Dwrite2(anondataset,offset,stride,count,(void *)datasettextbuffer,nmemtype),errorcode);
}
}
}
if (nmemtype >= 0) H5Tclose(nmemtype);
if (ndatasetspace >= 0) H5Sclose(ndatasetspace);
if (datasetspace >= 0) H5Sclose(datasetspace);
cbf_h5reportneg(H5Ldelete(hid,datasetname,H5P_DEFAULT), CBF_FORMAT, errorcode);
cbf_h5reportneg(H5Olink(anondataset,hid,datasetname,H5P_DEFAULT,H5P_DEFAULT), CBF_FORMAT, errorcode);
if (datasetid >= 0) H5Dclose(datasetid);
datasetid = anondataset;
if (memtype >= 0) H5Tclose(memtype);
datasetspace = memtype = CBF_H5FAIL;
old_size = new_size;
}
if (datasetspace >=0 )H5Sclose(datasetspace);
datasetspace = CBF_H5FAIL;
if (slab == H5S_UNLIMITED) {
offset[0] = dsdims[0];
curdim[0] = dsdims[0]+1;
} else {
offset[0] = slab;
curdim[0] = slab+1;
}
cbf_h5reportneg(H5Dset_extent(datasetid,curdim),CBF_FORMAT,errorcode);
stride[0] = 1;
count[0] = 1;
if (!datasettextbuffer) {
cbf_reportnez(cbf_alloc(&datasettextbuffer,NULL,
1,old_size),errorcode);
}
for (ii = 0; ii < text_size; ii++) {
((char *)datasettextbuffer)[ii] = datasettext[ii];
}
for (ii = text_size; ii < old_size; ii++) {
((char *)datasettextbuffer)[ii] = '\0';
}
/* cbf_debug_print4(" cbf_add_h5text_dataset_slab, text: '%s', oldsize: %d, newsize: %d", (char *)datasettextbuffer, old_size, new_size); */
cbf_reportnez(cbf_H5Dwrite2(datasetid,offset,stride,count,datasettextbuffer,datasettype),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
if (datasettextbuffer) cbf_free(&datasettextbuffer,NULL);
return errorcode;
}
/* add a text dataset to a group */
int cbf_add_h5text_dataset(hid_t hid,
const char* datasetname,
const char* datasettext,
int errorcode)
{
hid_t datasetspace = CBF_H5FAIL, datasettype = CBF_H5FAIL, datasetid = CBF_H5FAIL, datasetprop = CBF_H5FAIL;
htri_t dsexists;
/* ensure arguments all given */
if (hid < 0 || !datasetname || !datasettext
|| errorcode) return CBF_ARGUMENT;
dsexists = H5Lexists(hid,datasetname,H5P_DEFAULT);
if ( dsexists >=0 && dsexists ) {
hsize_t dssize;
hid_t memtype = CBF_H5FAIL;
int datasetrank = -1;
char * datasettextbuffer = NULL;
/* The dataset exists, and will be replaced with an array
of strings, unless it is already an array, in which case
we just add to it */
cbf_h5reportneg(datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetrank = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if (datasetrank == 1) {
/* This is an array, write the next slab */
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
cbf_reportnez(cbf_add_h5text_dataset_slab(hid,
datasetname,
datasettext,
H5S_UNLIMITED, errorcode),errorcode);
return errorcode;
}
dssize = H5Tget_size(datasettype);
cbf_reportnez(cbf_alloc((void * *)(&datasettextbuffer),NULL,
1,dssize+1),errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(memtype,dssize),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dread(datasetid, memtype, H5S_ALL, datasetspace,
H5P_DEFAULT, (void *)datasettextbuffer),CBF_FORMAT,errorcode);
/* unlink the current dataset and write the old data and the new data */
cbf_h5reportneg(H5Ldelete(hid,datasetname,H5P_DEFAULT),CBF_FORMAT,errorcode);
cbf_reportnez(cbf_add_h5text_dataset_slab(hid,
datasetname,
datasettextbuffer,
0, errorcode),errorcode);
cbf_reportnez(cbf_add_h5text_dataset_slab(hid,
datasetname,
datasettext,
1, errorcode),errorcode);
if (datasettextbuffer) cbf_free((void * *)(&datasettextbuffer),NULL);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (memtype >= 0) H5Tclose(memtype);
if (datasetid >= 0) H5Dclose(datasetid);
return errorcode;
}
cbf_h5reportneg(datasetspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(datasettype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(datasettype,_cbf_strlen(datasettext)),CBF_ALLOC,errorcode);
cbf_h5reportneg(datasetprop = H5Pcreate(H5P_DATASET_CREATE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(datasetid = H5Dcreatex(hid,datasetname,
datasettype,
datasetspace,
datasetprop),
CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dwrite(datasetid,datasettype,
H5S_ALL,H5S_ALL,
H5P_DEFAULT,
(const void *)datasettext),
CBF_ALLOC,errorcode);
if (datasetprop >= 0) H5Pclose(datasetprop);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
return errorcode;
}
/* add a double vector dataset slab to a group
places the specified dataset vector in the specified slab of the
specified datasetname for group hid. The dataset is created
if it does not already exist.
The slabs are indexed from 0 in the slowest dimension
*/
int cbf_add_h5double_vector_dataset_slab(hid_t hid,
const char* datasetname,
const double* datasetvalue,
const size_t dimension,
const hsize_t slab,
int errorcode)
{
hid_t datasetspace, datasettype, datasetid;
hid_t memspace, memtype;
hid_t ndatasettype;
hid_t ndatasetspace;
hid_t nmemtype;
int ndims = 0;
hsize_t offset[2] = {0,0};
hsize_t stride[2] = {1,1};
hsize_t count[2] = {1,1};
hsize_t chunk[2] = {1,0};
hsize_t curdim[2] = {1,1};
hsize_t memsize[2] = {1,1};
htri_t dsexists;
hsize_t dssize[2];
hsize_t maxdssize[2]= {H5S_UNLIMITED,0};
hsize_t dsdims[2];
hsize_t dsmaxdims[2];
CBF_UNUSED( dssize );
CBF_UNUSED( dsexists );
CBF_UNUSED( count );
CBF_UNUSED( stride );
CBF_UNUSED( ndims );
CBF_UNUSED( ndatasetspace) ;
datasetspace = datasettype = memspace = memtype = CBF_H5FAIL;
ndatasetspace = ndatasettype = nmemtype = CBF_H5FAIL;
datasetid = CBF_H5FAIL;
memsize[0] = 1;
dssize[0] = 1;
chunk[0] = 1;
/* ensure arguments all given */
if (hid < 0 || !datasetname ||
errorcode) return CBF_ARGUMENT;
maxdssize[1] = chunk[1] = dimension;
cbf_failnez(errorcode |= cbf_H5Drequire(hid,&datasetid,
datasetname,
2,
maxdssize,
chunk,
curdim,
H5T_IEEE_F64LE));
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,
dsdims,dsmaxdims),CBF_FORMAT,errorcode);
if (slab == H5S_UNLIMITED) {
offset[0] = dsdims[0];
curdim[0] = dsdims[0]+1;
} else {
offset[0] = slab;
curdim[0] = slab+1;
}
offset[1] = 0;
curdim[1] = dimension;
memsize[1] = dimension;
cbf_reportnez(cbf_H5Dinsert(datasetid,offset,0,memsize,curdim,datasetvalue,H5T_NATIVE_DOUBLE),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
return errorcode;
}
/* add a double dataset slab to a group
places the specified datasetvalue in the specified slab of the
specified datasetname for group hid. The dataset is created
if it does not already exist.
The slabs are indexed from 0
*/
int cbf_add_h5double_dataset_slab(hid_t hid,
const char* datasetname,
const double datasetvalue,
const hsize_t slab,
int errorcode)
{
hid_t datasetspace, datasettype, datasetid;
hid_t memspace, memtype;
hid_t ndatasettype;
hid_t ndatasetspace;
hid_t nmemtype;
int ndims = 0;
hsize_t offset[1] = {0};
hsize_t stride[1] = {1};
hsize_t count[1] = {1};
hsize_t chunk[1] = {1};
hsize_t curdim[1];
hsize_t memsize[1] = {1};
htri_t dsexists;
hsize_t dssize[1];
hsize_t maxdssize[1];
hsize_t dsdims[1];
hsize_t dsmaxdims[1];
CBF_UNUSED( memsize );
CBF_UNUSED( dssize );
CBF_UNUSED( dsexists );
CBF_UNUSED( count );
CBF_UNUSED( stride );
CBF_UNUSED( ndims );
CBF_UNUSED( ndatasetspace );
datasetspace = datasettype = memspace = memtype = CBF_H5FAIL;
ndatasetspace = ndatasettype = nmemtype = CBF_H5FAIL;
datasetid = CBF_H5FAIL;
memsize[0] = 1;
dssize[0] = 1;
maxdssize[0] = H5S_UNLIMITED;
chunk[0] = 1;
/* ensure arguments all given */
if (hid < 0 || !datasetname ||
errorcode) return CBF_ARGUMENT;
dsexists = H5Lexists(hid,datasetname, H5P_DEFAULT);
if ( dsexists < 0 ||
!dsexists
|| (datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT))< 0 ) {
/* Create the dataset if we were unable to open it */
cbf_h5reportneg(datasettype = H5Tcopy(H5T_IEEE_F64LE),CBF_ALLOC,errorcode);
cbf_reportnez(cbf_H5Dcreate(hid,&datasetid,datasetname,1,dssize,maxdssize,chunk,datasettype),errorcode);
curdim[0] = 0;
dsdims[0] = 0;
}
if ( datasetid <= 0 ) {
datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT);
}
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if ( errorcode || ndims != 1 ) return CBF_FORMAT;
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,
dsdims,dsmaxdims),CBF_FORMAT,errorcode);
if (datasetspace >=0 )H5Sclose(datasetspace);
datasetspace = CBF_H5FAIL;
if (slab == H5S_UNLIMITED) {
offset[0] = dsdims[0];
curdim[0] = dsdims[0]+1;
} else {
offset[0] = slab;
curdim[0] = slab+1;
}
cbf_h5reportneg(H5Dset_extent(datasetid,curdim),CBF_FORMAT,errorcode);
stride[0] = 1;
count[0] = 1;
cbf_reportnez(cbf_H5Dwrite2(datasetid,offset,stride,count,(void *)(&datasetvalue),H5T_NATIVE_DOUBLE),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
return errorcode;
}
/* add a double dataset to a group */
int cbf_add_h5double_dataset(hid_t hid,
const char* datasetname,
const double datasetvalue,
int errorcode)
{
hid_t datasetspace = CBF_H5FAIL, datasettype = CBF_H5FAIL, datasetid = CBF_H5FAIL, datasetprop = CBF_H5FAIL;
htri_t dsexists;
double dsvalue;
/* ensure arguments all given */
if (hid < 0 || !datasetname
|| errorcode) return CBF_ARGUMENT;
dsexists = H5Lexists(hid,datasetname,H5P_DEFAULT);
if ( dsexists >=0 && dsexists ) {
hid_t memtype = CBF_H5FAIL;
int datasetrank = -1;
/* The dataset exists, and will be replaced with an array
of strings, unless it is already an array, in which case
we just add to it */
cbf_h5reportneg(datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasetrank = H5Sget_simple_extent_ndims(datasetspace),CBF_H5ERROR,errorcode);
if (datasetrank == 1) {
/* This is an array, write the next slab */
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
cbf_reportnez(cbf_add_h5double_dataset_slab(hid,
datasetname,
datasetvalue,
H5S_UNLIMITED, errorcode),errorcode);
return errorcode;
}
cbf_h5reportneg(memtype=H5Tget_native_type(datasettype,H5T_DIR_ASCEND),CBF_H5ERROR,errorcode);
cbf_h5reportneg(H5Dread(datasetid, memtype, H5S_ALL, datasetspace,
H5P_DEFAULT, (void *)(&dsvalue)),CBF_H5ERROR,errorcode);
/* unlink the current dataset and write the old data and the new data */
cbf_h5reportneg(H5Ldelete(hid,datasetname,H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_reportnez(cbf_add_h5double_dataset_slab(hid,
datasetname,
dsvalue,
0, errorcode),errorcode);
cbf_reportnez(cbf_add_h5double_dataset_slab(hid,
datasetname,
datasetvalue,
1, errorcode),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (memtype >= 0) H5Tclose(memtype);
if (datasetid >= 0) H5Dclose(datasetid);
return errorcode;
}
cbf_h5reportneg(datasetspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(datasettype = H5Tcopy(H5T_IEEE_F64LE),CBF_ALLOC,errorcode);
cbf_h5reportneg(datasetprop = H5Pcreate(H5P_DATASET_CREATE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(datasetid = H5Dcreatex(hid,datasetname,
datasettype,
datasetspace,
datasetprop),
CBF_ALLOC,errorcode);
dsvalue = datasetvalue;
cbf_h5reportneg(H5Dwrite(datasetid,datasettype,
H5S_ALL,H5S_ALL,
H5P_DEFAULT,
(const void *)(&dsvalue)),
CBF_ALLOC,errorcode);
if (datasetprop >= 0) H5Pclose(datasetprop);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
return errorcode;
}
/* add a long dataset slab to a group
places the specified datasetvalue in the specified slab of the
specified datasetname for group hid. The dataset is created
if it does not already exist.
The slabs are indexed from 0
*/
int cbf_add_h5long_dataset_slab(hid_t hid,
const char* datasetname,
const long datasetvalue,
const hsize_t slab,
int errorcode)
{
hid_t datasetspace, datasettype, datasetid;
hid_t memspace, memtype;
hid_t ndatasettype;
hid_t ndatasetspace;
hid_t nmemtype;
int ndims = 0;
hsize_t offset[1] = {0};
hsize_t stride[1] = {1};
hsize_t count[1] = {1};
hsize_t chunk[1] = {1};
hsize_t curdim[1];
hsize_t memsize[1] = {1};
htri_t dsexists;
hsize_t dssize[1];
hsize_t maxdssize[1];
hsize_t dsdims[1];
hsize_t dsmaxdims[1];
datasetspace = datasettype = memspace = memtype = CBF_H5FAIL;
ndatasetspace = ndatasettype = nmemtype = CBF_H5FAIL;
datasetid = CBF_H5FAIL;
memsize[0] = 1;
dssize[0] = 1;
maxdssize[0] = H5S_UNLIMITED;
chunk[0] = 1;
CBF_UNUSED( ndatasetspace );
CBF_UNUSED( memsize );
/* ensure arguments all given */
if (hid < 0 || !datasetname ||
errorcode) return CBF_ARGUMENT;
dsexists = H5Lexists(hid,datasetname, H5P_DEFAULT);
if ( dsexists < 0 ||
!dsexists
|| (datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT))< 0 ) {
/* Create the dataset if we were unable to open it */
cbf_h5reportneg(datasettype = H5Tcopy(H5T_STD_I64LE),CBF_ALLOC,errorcode);
cbf_reportnez(cbf_H5Dcreate(hid,&datasetid,datasetname,1,dssize,maxdssize,chunk,datasettype),errorcode);
curdim[0] = 0;
dsdims[0] = 0;
}
if ( datasetid <= 0 ) {
datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT);
}
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if ( errorcode || ndims != 1 ) return CBF_FORMAT;
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,
dsdims,dsmaxdims),CBF_FORMAT,errorcode);
if (datasetspace >=0 )H5Sclose(datasetspace);
datasetspace = CBF_H5FAIL;
if (slab == H5S_UNLIMITED) {
offset[0] = dsdims[0];
curdim[0] = dsdims[0]+1;
} else {
offset[0] = slab;
curdim[0] = slab+1;
}
cbf_h5reportneg(H5Dset_extent(datasetid,curdim),CBF_FORMAT,errorcode);
stride[0] = 1;
count[0] = 1;
cbf_reportnez(cbf_H5Dwrite2(datasetid,offset,stride,count,(void *)(&datasetvalue),H5T_NATIVE_LONG),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
return errorcode;
}
/* add a long dataset to a group */
int cbf_add_h5long_dataset(hid_t hid,
const char* datasetname,
const long datasetvalue,
int errorcode)
{
hid_t datasetspace = CBF_H5FAIL, datasettype = CBF_H5FAIL, datasetid = CBF_H5FAIL, datasetprop = CBF_H5FAIL;
htri_t dsexists;
long dsvalue;
/* ensure arguments all given */
if (hid < 0 || !datasetname
|| errorcode) return CBF_ARGUMENT;
dsexists = H5Lexists(hid,datasetname,H5P_DEFAULT);
if ( dsexists >=0 && dsexists ) {
hid_t memtype = CBF_H5FAIL;
int datasetrank = -1;
/* The dataset exists, and will be replaced with an array
of strings, unless it is already an array, in which case
we just add to it */
cbf_h5reportneg(datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasetrank = H5Sget_simple_extent_ndims(datasetspace),CBF_H5ERROR,errorcode);
if (datasetrank == 1) {
/* This is an array, write the next slab */
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
cbf_reportnez(cbf_add_h5long_dataset_slab(hid,
datasetname,
datasetvalue,
H5S_UNLIMITED, errorcode),errorcode);
return errorcode;
}
cbf_h5reportneg(memtype=H5Tget_native_type(datasettype,H5T_DIR_ASCEND),CBF_H5ERROR,errorcode);
cbf_h5reportneg(H5Dread(datasetid, memtype, H5S_ALL, datasetspace,
H5P_DEFAULT, (void *)(&dsvalue)),CBF_H5ERROR,errorcode);
/* unlink the current dataset and write the old data and the new data */
cbf_h5reportneg(H5Ldelete(hid,datasetname,H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_reportnez(cbf_add_h5double_dataset_slab(hid,
datasetname,
dsvalue,
0, errorcode),errorcode);
cbf_reportnez(cbf_add_h5double_dataset_slab(hid,
datasetname,
datasetvalue,
1, errorcode),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (memtype >= 0) H5Tclose(memtype);
if (datasetid >= 0) H5Dclose(datasetid);
return errorcode;
}
cbf_h5reportneg(datasetspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(datasettype = H5Tcopy(H5T_STD_I64LE),CBF_ALLOC,errorcode);
cbf_h5reportneg(datasetprop = H5Pcreate(H5P_DATASET_CREATE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(datasetid = H5Dcreatex(hid,datasetname,
datasettype,
datasetspace,
datasetprop),
CBF_ALLOC,errorcode);
dsvalue = datasetvalue;
cbf_h5reportneg(H5Dwrite(datasetid,datasettype,
H5S_ALL,H5S_ALL,
H5P_DEFAULT,
(const void *)(&dsvalue)),
CBF_ALLOC,errorcode);
if (datasetprop >= 0) H5Pclose(datasetprop);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
return errorcode;
}
/* add an unsigned long dataset slab to a group
places the specified datasetvalue in the specified slab of the
specified datasetname for group hid. The dataset is created
if it does not already exist.
The slabs are indexed from 0
*/
int cbf_add_h5ulong_dataset_slab(hid_t hid,
const char* datasetname,
const unsigned long datasetvalue,
const hsize_t slab,
int errorcode)
{
hid_t datasetspace, datasettype, datasetid;
hid_t memspace, memtype;
hid_t ndatasettype;
hid_t ndatasetspace;
hid_t nmemtype;
int ndims = 0;
hsize_t offset[1] = {0};
hsize_t stride[1] = {1};
hsize_t count[1] = {1};
hsize_t chunk[1] = {1};
hsize_t curdim[1];
hsize_t memsize[1] = {1};
htri_t dsexists;
hsize_t dssize[1];
hsize_t maxdssize[1];
hsize_t dsdims[1];
hsize_t dsmaxdims[1];
CBF_UNUSED( memsize );
CBF_UNUSED( ndatasetspace );
datasetspace = datasettype = memspace = memtype = CBF_H5FAIL;
ndatasetspace = ndatasettype = nmemtype = CBF_H5FAIL;
datasetid = CBF_H5FAIL;
memsize[0] = 1;
dssize[0] = 1;
maxdssize[0] = H5S_UNLIMITED;
chunk[0] = 1;
/* ensure arguments all given */
if (hid < 0 || !datasetname ||
errorcode) return CBF_ARGUMENT;
dsexists = H5Lexists(hid,datasetname, H5P_DEFAULT);
if ( dsexists < 0 ||
!dsexists
|| (datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT))< 0 ) {
/* Create the dataset if we were unable to open it */
cbf_h5reportneg(datasettype = H5Tcopy(H5T_STD_U64LE),CBF_ALLOC,errorcode);
cbf_reportnez(cbf_H5Dcreate(hid,&datasetid,datasetname,1,dssize,maxdssize,chunk,datasettype),errorcode);
curdim[0] = 0;
dsdims[0] = 0;
}
if ( datasetid <= 0 ) {
datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT);
}
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if ( errorcode || ndims != 1 ) return CBF_FORMAT;
cbf_h5reportneg(H5Sget_simple_extent_dims(datasetspace,
dsdims,dsmaxdims),CBF_FORMAT,errorcode);
if (datasetspace >=0 )H5Sclose(datasetspace);
datasetspace = CBF_H5FAIL;
if (slab == H5S_UNLIMITED) {
offset[0] = dsdims[0];
curdim[0] = dsdims[0]+1;
} else {
offset[0] = slab;
curdim[0] = slab+1;
}
cbf_h5reportneg(H5Dset_extent(datasetid,curdim),CBF_FORMAT,errorcode);
stride[0] = 1;
count[0] = 1;
cbf_reportnez(cbf_H5Dwrite2(datasetid,offset,stride,count,(void *)(&datasetvalue),H5T_NATIVE_ULONG),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
return errorcode;
}
/* add an unsigned long dataset to a group */
int cbf_add_h5ulong_dataset(hid_t hid,
const char* datasetname,
const long datasetvalue,
int errorcode)
{
hid_t datasetspace = CBF_H5FAIL, datasettype = CBF_H5FAIL, datasetid = CBF_H5FAIL, datasetprop = CBF_H5FAIL;
htri_t dsexists;
long dsvalue;
/* ensure arguments all given */
if (hid < 0 || !datasetname
|| errorcode) return CBF_ARGUMENT;
dsexists = H5Lexists(hid,datasetname,H5P_DEFAULT);
if ( dsexists >=0 && dsexists ) {
hid_t memtype = CBF_H5FAIL;
int datasetrank = -1;
/* The dataset exists, and will be replaced with an array
of strings, unless it is already an array, in which case
we just add to it */
cbf_h5reportneg(datasetid = H5Dopen2(hid,datasetname, H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_H5ERROR,errorcode);
cbf_h5reportneg(datasetrank = H5Sget_simple_extent_ndims(datasetspace),CBF_H5ERROR,errorcode);
if (datasetrank == 1) {
/* This is an array, write the next slab */
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
cbf_reportnez(cbf_add_h5long_dataset_slab(hid,
datasetname,
datasetvalue,
H5S_UNLIMITED, errorcode),errorcode);
return errorcode;
}
cbf_h5reportneg(memtype=H5Tget_native_type(datasettype,H5T_DIR_ASCEND),CBF_H5ERROR,errorcode);
cbf_h5reportneg(H5Dread(datasetid, memtype, H5S_ALL, datasetspace,
H5P_DEFAULT, (void *)(&dsvalue)),CBF_H5ERROR,errorcode);
/* unlink the current dataset and write the old data and the new data */
cbf_h5reportneg(H5Ldelete(hid,datasetname,H5P_DEFAULT),CBF_H5ERROR,errorcode);
cbf_reportnez(cbf_add_h5double_dataset_slab(hid,
datasetname,
dsvalue,
0, errorcode),errorcode);
cbf_reportnez(cbf_add_h5double_dataset_slab(hid,
datasetname,
datasetvalue,
1, errorcode),errorcode);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (memtype >= 0) H5Tclose(memtype);
if (datasetid >= 0) H5Dclose(datasetid);
return errorcode;
}
cbf_h5reportneg(datasetspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(datasettype = H5Tcopy(H5T_STD_U64LE),CBF_ALLOC,errorcode);
cbf_h5reportneg(datasetprop = H5Pcreate(H5P_DATASET_CREATE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(datasetid = H5Dcreatex(hid,datasetname,
datasettype,
datasetspace,
datasetprop),
CBF_ALLOC,errorcode);
dsvalue = datasetvalue;
cbf_h5reportneg(H5Dwrite(datasetid,datasettype,
H5S_ALL,H5S_ALL,
H5P_DEFAULT,
(const void *)(&dsvalue)),
CBF_ALLOC,errorcode);
if (datasetprop >= 0) H5Pclose(datasetprop);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasettype >= 0) H5Tclose(datasettype);
if (datasetid >= 0) H5Dclose(datasetid);
return errorcode;
}
/* Count the number of links in an HDF5 group */
int cbf_count_h5group_links (const hid_t parent,
const char * group,
hsize_t * numlinks) {
H5G_info_t group_info;
if (!cbf_H5Ivalid(parent) || !group) return CBF_ARGUMENT;
cbf_h5failneg(H5Gget_info_by_name( parent, group, &group_info, H5P_DEFAULT ),CBF_NOTFOUND);
if (numlinks) *numlinks = group_info.nlinks;
return CBF_SUCCESS;
}
/* Count the number of attributes of an HDF5 object */
int cbf_count_h5object_attributes (const hid_t parent,
const char * object,
hsize_t * numattribs) {
H5O_info_t object_info;
if (!cbf_H5Ivalid(parent) || !object) return CBF_ARGUMENT;
cbf_h5failneg(H5Oget_info_by_name( parent, object, &object_info, H5P_DEFAULT ),CBF_NOTFOUND);
if (numattribs) *numattribs = object_info.num_attrs;
return CBF_SUCCESS;
}
/* Find an hdf5 group link by name pattern */
int cbf_find_h5group_link(const hid_t parent,
const char * group,
const char * name_pattern,
hsize_t search_index,
hsize_t *found_index,
char ** name) {
hsize_t numlinks, ii;
ssize_t namelen, namebuflen;
char * namebuf;
namelen = namebuflen = 0;
int errorcode;
if (!cbf_H5Ivalid(parent) || !group) return CBF_ARGUMENT;
cbf_failnez(cbf_count_h5group_links(parent,group,&numlinks));
if (search_index >= numlinks) return CBF_ARGUMENT;
for (ii = search_index; ii < numlinks; ii++) {
namelen = H5Lget_name_by_idx(parent, group, H5_INDEX_NAME, H5_ITER_INC, ii, NULL, 0, H5P_DEFAULT );
if (namelen < 0 ) break;
if (namebuflen > 0 && namebuflen < namelen+1) {
void ** vnamebuf;
vnamebuf = (void **)(&namebuf);
cbf_failnez(cbf_free(vnamebuf,NULL));
namebuflen = 0;
}
if (namebuflen == 0) {
errorcode = cbf_alloc((void **)&namebuf,NULL,1,namelen+1);
if (errorcode) break;
namebuflen = namelen+1;
}
namelen = H5Lget_name_by_idx(parent, group, H5_INDEX_NAME, H5_ITER_INC, ii, namebuf, namebuflen, H5P_DEFAULT );
namebuf[namelen] = '\0';
if (!cbf_cimatch(namebuf,name_pattern)) {
if (name) {
*name = namebuf;
} else {
void ** vnamebuf;
vnamebuf = (void **)(&namebuf);
cbf_failnez(cbf_free(vnamebuf,NULL));
namebuflen = 0;
}
if (found_index) *found_index = ii;
return CBF_SUCCESS;
}
}
if (namebuflen > 0 ) {
void ** vnamebuf;
vnamebuf = (void **)(&namebuf);
cbf_failnez(cbf_free(vnamebuf,NULL));
namebuflen = 0;
}
if (found_index) *found_index = ii;
return CBF_NOTFOUND;
}
/* Find an hdf5 attribute by name pattern */
int cbf_find_h5attribute(const hid_t parent,
const char * object_name,
const char * name_pattern,
hsize_t search_index,
hsize_t *found_index,
char ** name ) {
hsize_t numattribs, ii;
ssize_t namelen, namebuflen;
char * namebuf;
namelen = namebuflen = 0;
int errorcode;
if (!cbf_H5Ivalid(parent) || !object_name) return CBF_ARGUMENT;
cbf_failnez(cbf_count_h5object_attributes(parent,object_name,&numattribs));
if (search_index >= numattribs) return CBF_ARGUMENT;
for (ii = search_index; ii < numattribs; ii++) {
namelen = H5Aget_name_by_idx(parent, object_name, H5_INDEX_NAME, H5_ITER_INC, ii, NULL, 0, H5P_DEFAULT );
if (namelen < 0 ) break;
if (namebuflen > 0 && namebuflen < namelen+1) {
void ** vnamebuf;
vnamebuf = (void **)(&namebuf);
cbf_failnez(cbf_free(vnamebuf,NULL));
namebuflen = 0;
}
if (namebuflen == 0) {
errorcode = cbf_alloc((void **)&namebuf,NULL,1,namelen+1);
if (errorcode) break;
namebuflen = namelen+1;
}
namelen = H5Aget_name_by_idx(parent, object_name, H5_INDEX_NAME, H5_ITER_INC, ii, namebuf, namebuflen, H5P_DEFAULT );
namebuf[namelen] = '\0';
if (!cbf_cimatch(namebuf,name_pattern)) {
if (name) {
*name = namebuf;
} else {
void ** vnamebuf;
vnamebuf = (void **)(&namebuf);
cbf_failnez(cbf_free(vnamebuf,NULL));
namebuflen = 0;
}
if (found_index) *found_index = ii;
return CBF_SUCCESS;
}
}
if (namebuflen > 0 ) {
void ** vnamebuf;
vnamebuf = (void **)(&namebuf);
cbf_failnez(cbf_free(vnamebuf,NULL));
namebuflen = 0;
}
if (found_index) *found_index = ii;
return CBF_NOTFOUND;
}
/* Write a binary value to an HDF5 file */
int cbf_write_h5binary (cbf_handle handle,
cbf_node *column,
unsigned int row,
cbf_h5handle h5handle)
{
hid_t valid = CBF_H5FAIL, valtype = CBF_H5FAIL, memtype = CBF_H5FAIL, valprop = CBF_H5FAIL, valspace = CBF_H5FAIL;
int errorcode;
char rownum[10];
cbf_file *infile;
char digest [25];
long start;
size_t size;
hsize_t hsize[3];
unsigned int compression;
unsigned char * rawdata;
void * uncompressedarray;
int id, bits, sign, type, checked_digest, realarray;
const char *byteorder;
size_t dimover, dimfast, dimmid, dimslow;
size_t padding;
size_t elsize;
size_t nelems_read;
/* Check the arguments */
if (!handle || !h5handle || !h5handle->hfile)
return CBF_ARGUMENT;
if (!cbf_is_binary (column, row))
return CBF_ARGUMENT;
if (cbf_is_mimebinary (column, row))
return CBF_ARGUMENT;
cbf_failnez (cbf_get_bintext (column, row, &type, &id, &infile,
&start, &size, &checked_digest,
digest, &bits, &sign, &realarray,
&byteorder, &dimover, &dimfast, &dimmid, &dimslow,
&padding, &compression))
if (dimslow == 0) dimslow = 1;
if (dimmid == 0) dimmid = 1;
if (dimfast == 0) dimfast = 1;
/* Position the file at the start of the binary section */
cbf_failnez (cbf_set_fileposition (infile, start, SEEK_SET))
/* Calculate the digest if necessary */
if (!cbf_is_base64digest (digest))
{
/* Compute the message digest */
cbf_failnez (cbf_md5digest (infile, size, digest))
/* Go back to the start of the binary data */
cbf_failnez (cbf_set_fileposition (infile, start, SEEK_SET))
/* Update the entry */
checked_digest = 1;
cbf_failnez (cbf_set_bintext (column, row, type,
id, infile, start, size,
checked_digest, digest, bits,
sign, realarray,
byteorder, dimover, dimfast, dimmid, dimslow,
padding, compression))
}
/* Discard any bits in the buffers */
infile->bits [0] = 0;
infile->bits [1] = 0;
valid = valtype = valprop = valspace = CBF_H5FAIL;
sprintf(rownum,"%d", row);
/* prepare the errorcode */
errorcode = 0;
if (h5handle->flags & CBF_H5_OPAQUE) {
/* Treat the image as an opaque stream of size bytes */
hsize[0] = size;
cbf_h5reportneg(valspace = H5Screate_simple(1,hsize,NULL),
CBF_ALLOC,errorcode);
cbf_h5reportneg(valtype = H5Tcreate(H5T_OPAQUE,1),
CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_tag(valtype,"stream of opaque bytes"),
CBF_ALLOC,errorcode);
cbf_h5reportneg(valprop = H5Pcreate(H5P_DATASET_CREATE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(valid = H5Dcreatex(h5handle->colid,rownum,
valtype,valspace,
valprop),
CBF_ALLOC,errorcode);
/* get all the data */
/* first ensure enough space at infile->characters
for size characters, which means we need enough
at characters->base for size + the old data,
if any */
cbf_reportnez(cbf_set_io_buffersize(infile, size),errorcode);
/* now we can safely do the read
because of the file positioning done, infile->characters
is actually the same as infile_characters_base for a
file stream based file */
rawdata = (unsigned char*) infile->characters;
if (infile->characters_used < size) {
/* We cannot get any more characters from
a temporary file of file without a stream */
if (infile->temporary || !infile->stream) {
errorcode |= CBF_FILEREAD;
}
if (!errorcode && fread(rawdata, 1,
size-(infile->characters_used), infile->stream)
!= size-infile->characters_used)
errorcode |= CBF_FILEREAD;
if (!errorcode) infile->characters_used = size;
}
cbf_h5reportneg(H5Dwrite(valid,valtype,
valspace,H5S_ALL,H5P_DEFAULT,rawdata),
CBF_ARGUMENT,errorcode);
} else {
/* Treat the data as an array to be compressed
chunking in planes dimfast x dimmid
*/
hsize_t chunk[3];
hsize_t maxdim[3];
hsize[0] = dimslow;
chunk[0] = 1;
maxdim[0] = H5S_UNLIMITED;
hsize[1] = chunk[1] = maxdim[1] = dimmid;
hsize[2] = chunk[2] = maxdim[2] = dimfast;
cbf_h5reportneg(valspace = H5Screate_simple(3,hsize,maxdim),
CBF_ALLOC,errorcode);
if (realarray) {
if (byteorder[0]=='l' || byteorder[0]=='L') {
if (bits <= 32) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_IEEE_F32LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_FLOAT),
CBF_ALLOC,errorcode)
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_IEEE_F64LE),
CBF_ALLOC,errorcode)
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_DOUBLE),
CBF_ALLOC,errorcode)
}
} else {
if (bits <= 32) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_IEEE_F32BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_FLOAT),
CBF_ALLOC,errorcode)
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_IEEE_F64BE),
CBF_ALLOC,errorcode)
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_DOUBLE),
CBF_ALLOC,errorcode)
}
}
} else {
if (byteorder[0]=='l' || byteorder[0]=='L') {
if (bits <= 8) {
if (sign) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_I8LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_CHAR),
CBF_ALLOC,errorcode);
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_U8LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_UCHAR),
CBF_ALLOC,errorcode);
}
} else if (bits <= 16) {
if (sign) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_I16LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_SHORT),
CBF_ALLOC,errorcode);
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_U16LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_USHORT),
CBF_ALLOC,errorcode);
}
} else if (bits <= 32) {
if (sign) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_I32LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_INT),
CBF_ALLOC,errorcode);
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_U32LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_UINT),
CBF_ALLOC,errorcode);
}
} else {
if (sign) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_I64LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_LLONG),
CBF_ALLOC,errorcode);
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_U64LE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_ULLONG),
CBF_ALLOC,errorcode);
}
}
} else {
if (bits <= 8) {
if (sign) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_I8BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_CHAR),
CBF_ALLOC,errorcode);
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_U8BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_UCHAR),
CBF_ALLOC,errorcode);
}
} else if (bits <= 16) {
if (sign) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_I16BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_SHORT),
CBF_ALLOC,errorcode);
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_U16BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_USHORT),
CBF_ALLOC,errorcode);
}
} else if (bits <= 32) {
if (sign) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_I32BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_INT),
CBF_ALLOC,errorcode);
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_U32BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_UINT),
CBF_ALLOC,errorcode);
}
} else {
if (sign) {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_I64BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_LLONG),
CBF_ALLOC,errorcode);
} else {
cbf_h5reportneg(valtype = H5Tcopy(H5T_STD_U64BE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(memtype = H5Tcopy(H5T_NATIVE_ULLONG),
CBF_ALLOC,errorcode);
}
}
}
}
cbf_h5reportneg(valprop = H5Pcreate(H5P_DATASET_CREATE),
CBF_H5ERROR,errorcode);
cbf_h5reportneg(H5Pset_chunk(valprop,3,chunk),
CBF_H5ERROR,errorcode);
/* allow compression */
if (CBF_SUCCESS==errorcode) {
/* cbf_debug_print2("compression: %x",h5handle->flags); */
if ((h5handle->flags & CBF_COMPRESSION_MASK)==CBF_H5COMPRESSION_ZLIB) {
H5Pset_deflate(valprop, 1);
} else if (h5handle->flags & CBF_H5COMPRESSION_CBF) {
unsigned int cd_values[CBF_H5Z_FILTER_CBF_NELMTS];
cd_values[CBF_H5Z_FILTER_CBF_COMPRESSION] =
h5handle->flags &
(CBF_COMPRESSION_MASK|CBF_FLAG_MASK) &
(~(CBF_H5COMPRESSION_CBF));
cd_values[CBF_H5Z_FILTER_CBF_RESERVED] = 0;
cd_values[CBF_H5Z_FILTER_CBF_BINARY_ID] = id;
cd_values[CBF_H5Z_FILTER_CBF_PADDING] = padding;
cd_values[CBF_H5Z_FILTER_CBF_ELSIZE] = (bits+7)/8;
cd_values[CBF_H5Z_FILTER_CBF_ELSIGN] = sign;
cd_values[CBF_H5Z_FILTER_CBF_REAL] = realarray;
cd_values[CBF_H5Z_FILTER_CBF_DIMFAST] = dimfast;
cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = dimmid;
cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = dimslow;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_CBF)) {
cbf_h5reportneg(H5Zregister(CBF_H5Z_CBF),CBF_H5ERROR ,errorcode);
if (errorcode) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_CBF (32006) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_CBF, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_CBF_NELMTS, cd_values),CBF_ALLOC,errorcode);
if (errorcode) {
cbf_debug_print2("errorcode on setting filter CBF_H5Z_FILTER_CBF %d\n",errorcode);
}
}
#ifdef CBF_H5Z_USE_LZ4
else if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4 ||
(h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4_2) {
unsigned int cd_values[CBF_H5Z_FILTER_LZ4_NELMTS];
cd_values[CBF_H5Z_FILTER_LZ4_BLOCKSIZE] = 1<<30;
cd_values[CBF_H5Z_FILTER_LZ4_THREADS] = 1;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_LZ4)) {
cbf_h5reportneg(H5Zregister(H5Z_LZ4),CBF_H5ERROR ,errorcode);
if (errorcode) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_LZ4 (32004) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_LZ4, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_LZ4_NELMTS, cd_values),CBF_H5ERROR,errorcode);
if (errorcode) {
cbf_debug_print2("errorcode on setting filter CBF_H5Z_FILTER_CBF %d\n",errorcode);
}
if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4_2) {
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_LZ4, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_LZ4_NELMTS, cd_values),CBF_H5ERROR,errorcode);
if (errorcode) {
cbf_debug_print2("errorcode on setting filter CBF_H5Z_FILTER_CBF %d\n",errorcode);
}
}
}
#endif
#ifdef CBF_H5Z_USE_BSHUF
else if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_BSLZ4 ) {
int elsize = (bits+7)/8;
unsigned int cd_values[CBF_H5Z_FILTER_BSHUF_NELMTS];
cd_values[CBF_H5Z_FILTER_BSHUF_BLOCKSIZE] = 8192/elsize;
cd_values[CBF_H5Z_FILTER_BSHUF_COMPRESSION] =
CBF_H5Z_FILTER_BSHUF_LZ4;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_BSHUF)) {
cbf_h5reportneg(H5Zregister(bshuf_H5Filter),CBF_H5ERROR ,errorcode);
if (errorcode) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_BSHUF (32008) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_BSHUF, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_BSHUF_NELMTS, cd_values),CBF_H5ERROR,errorcode);
if (errorcode) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_BSHUF %d\n",errorcode);
}
}
}
#endif
}
valid = H5Dcreatex(h5handle->colid,rownum,
valtype,valspace,
valprop);
/* get all the data */
elsize = (bits+7)/8;
cbf_reportnez(cbf_alloc(((void **) &uncompressedarray),NULL,
elsize,dimover),errorcode);
nelems_read = 0;
/* Get the data */
{
int text_realarray, text_id;
size_t text_dimover, text_dimfast, text_dimmid, text_dimslow, text_padding;
const char *text_byteorder;
cbf_reportnez (cbf_get_binary (column, row,
&text_id,
uncompressedarray,
elsize,
sign,
dimover,
&nelems_read,
&text_realarray,
&text_byteorder,
&text_dimover,
&text_dimfast,
&text_dimmid,
&text_dimslow,
&text_padding),errorcode);
}
if (nelems_read < dimover) {
cbf_failnez(cbf_free(&uncompressedarray,NULL));
return errorcode|CBF_ENDOFDATA;
}
cbf_h5reportneg(H5Dwrite(valid,memtype,H5S_ALL,
H5S_ALL,H5P_DEFAULT,uncompressedarray),
CBF_ARGUMENT,errorcode);
if (memtype >= 0) {
cbf_h5failneg(H5Tclose(memtype),CBF_ARGUMENT);
}
cbf_failnez(cbf_free(&uncompressedarray,NULL));
}
errorcode |= cbf_apply_h5integer_attribute(valid,"signal",row+1,errorcode);
errorcode |= cbf_apply_h5intasstr_attribute(valid,"compression",compression,errorcode);
errorcode |= cbf_apply_h5intasstr_attribute(valid,"binid",id,errorcode);
errorcode |= cbf_apply_h5intasstr_attribute(valid,"bits",bits,errorcode);
errorcode |= cbf_apply_h5intasstr_attribute(valid,"sign",sign,errorcode);
errorcode |= cbf_apply_h5intasstr_attribute(valid,"bintype",type,errorcode);
errorcode |= cbf_apply_h5text_attribute(valid,"cbftype","bnry",errorcode);
errorcode |= cbf_apply_h5intasstr_attribute(valid,"checked_digest",checked_digest,errorcode);
errorcode |= cbf_apply_h5longasstr_attribute(valid,"size",(long)size,errorcode);
errorcode |= cbf_apply_h5intasstr_attribute(valid,"real",realarray,errorcode);
errorcode |= cbf_apply_h5text_attribute(valid,"byteorder",byteorder,errorcode);
if (cbf_is_base64digest (digest)) {
errorcode |= cbf_apply_h5text_attribute(valid,"MD5_digest",digest,errorcode);
}
errorcode |= cbf_apply_h5longasstr_attribute(valid,"dimover",(long)dimover,errorcode);
errorcode |= cbf_apply_h5longasstr_attribute(valid,"dimfast",(long)dimfast,errorcode);
errorcode |= cbf_apply_h5longasstr_attribute(valid,"dimmid",(long)dimmid,errorcode);
errorcode |= cbf_apply_h5longasstr_attribute(valid,"dimslow",(long)dimslow,errorcode);
errorcode |= cbf_apply_h5longasstr_attribute(valid,"padding",(long)padding,errorcode);
/* now link the data to entry:NXentry/data:NXdata */
if (h5handle->dataid<0){
/* ensure it goes right below NXentry */
if (h5handle->curnxid>=0) {
cbf_h5reportneg(H5Gclose(h5handle->curnxid),CBF_ARGUMENT,errorcode);
h5handle->curnxid = CBF_H5FAIL;
}
cbf_reportnez(cbf_H5Gcreate_in_handle(h5handle,"data",&(h5handle->dataid)),errorcode);
cbf_failnez(cbf_apply_h5text_attribute(h5handle->dataid,
"NX_class","NXdata",0));
}
{
char target_path[1024];
char full_name[1024];
size_t len;
const char * pstr;
_cbf_strcpy(target_path,"/entry/CBF_cbf/");
full_name[0] = '\0';
len = _cbf_strlen(target_path)+_cbf_strlen(rownum)+1;
pstr = (h5handle->bookmark).datablock?
(h5handle->bookmark).datablock:"_(null)_";
if (len+_cbf_strlen(pstr)>1020) return CBF_FORMAT;
strncat(target_path,pstr,1020-len);
strcat(target_path,"/");
strncat(full_name,pstr,1020-len);
strcat(full_name,".");
len = _cbf_strlen(target_path)+_cbf_strlen(rownum)+1;
pstr = (h5handle->bookmark).category?
(h5handle->bookmark).category:"_(null)_";
if (len+_cbf_strlen(pstr)>1021) return CBF_FORMAT;
strncat(target_path,pstr,1021-len);
strcat(target_path,"/");
strncat(full_name,pstr,1020-len);
strcat(full_name,".");
len = _cbf_strlen(target_path)+_cbf_strlen(rownum)+1;
pstr = (h5handle->bookmark).column?
(h5handle->bookmark).column:"_(null)_";
if (len+_cbf_strlen(pstr)>1022) return CBF_FORMAT;
strncat(target_path,pstr,1022-len);
strcat(target_path,"/");
strncat(full_name,pstr,1020-len);
strcat(full_name,".");
len = _cbf_strlen(target_path);
if (len+_cbf_strlen(rownum)>1023) return CBF_FORMAT;
strncat(target_path,rownum,1023-len);
strncat(full_name,rownum,1023-len);
errorcode |= cbf_apply_h5text_attribute(valid,"target",
target_path,errorcode);
cbf_h5reportneg(H5Lcreate_hard(h5handle->colid,rownum,
h5handle->dataid,full_name,H5P_DEFAULT,H5P_DEFAULT),
CBF_ARGUMENT,
errorcode);
}
if (valid >= 0) {
cbf_h5failneg(H5Dclose(valid),CBF_ARGUMENT);
}
if (valspace >= 0) {
cbf_h5failneg(H5Sclose(valspace),CBF_ARGUMENT);
}
if (valtype >= 0) {
cbf_h5failneg(H5Tclose(valtype),CBF_ARGUMENT);
}
if (valprop >= 0) {
cbf_h5failneg(H5Pclose(valprop),CBF_ARGUMENT);
}
return errorcode;
}
/* Write an ascii value to an HDF5 file */
int cbf_write_h5ascii (cbf_handle handle,
unsigned int row,
const char *string,
cbf_h5handle h5handle)
{
static const char missing [] = { CBF_TOKEN_NULL, '?', '\0' };
hid_t valid, valtype, valprop, valspace;
char rownum[10];
char* typecode;
int errorcode;
/* Check the arguments */
if (!handle || !h5handle || h5handle->hfile < 0) return CBF_ARGUMENT;
valid = valtype = valprop = valspace = CBF_H5FAIL;
sprintf(rownum,"%d", row);
if (!string)
string = missing;
else
if (*string != CBF_TOKEN_WORD &&
*string != CBF_TOKEN_SQSTRING &&
*string != CBF_TOKEN_DQSTRING &&
*string != CBF_TOKEN_SCSTRING &&
*string != CBF_TOKEN_TSQSTRING &&
*string != CBF_TOKEN_TDQSTRING &&
*string != CBF_TOKEN_BKTSTRING &&
*string != CBF_TOKEN_BRCSTRING &&
*string != CBF_TOKEN_PRNSTRING &&
*string != CBF_TOKEN_NULL)
return CBF_ARGUMENT;
/* prepare the errorcode */
errorcode = 0;
/* Create a scalar dataspace */
cbf_h5reportneg(valspace = H5Screate(H5S_SCALAR),
CBF_ALLOC,errorcode);
cbf_h5reportneg(valtype = H5Tcopy(H5T_C_S1),
CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(valtype,_cbf_strlen(string+1)),
CBF_ALLOC,errorcode);
cbf_h5reportneg(valprop = H5Pcreate(H5P_DATASET_CREATE),
CBF_ALLOC,errorcode);
cbf_h5reportneg(valid = H5Dcreatex(h5handle->colid,rownum,
valtype,valspace,valprop),
CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dwrite(valid,valtype,
H5S_ALL,H5S_ALL,H5P_DEFAULT,string+1),
CBF_ARGUMENT,errorcode);
errorcode |= cbf_get_value_type(string,(const char **)&typecode);
errorcode |= cbf_apply_h5text_attribute(valid,
"cbftype",typecode,errorcode);
if (valid >= 0) {
cbf_h5failneg(H5Dclose(valid),CBF_ARGUMENT);
}
if (valspace >= 0) {
cbf_h5failneg(H5Sclose(valspace),CBF_ARGUMENT);
}
if (valtype >= 0) {
cbf_h5failneg(H5Tclose(valtype),CBF_ARGUMENT);
}
if (valprop >= 0) {
cbf_h5failneg(H5Pclose(valprop),CBF_ARGUMENT);
}
return errorcode;
}
/* Write a value to an HDF5 file */
int cbf_write_h5value (cbf_handle handle, cbf_node *column, unsigned int row,
cbf_h5handle h5handle)
{
const char *text;
/* Check the arguments */
if (!column || !h5handle || h5handle->hfile < 0)
return CBF_ARGUMENT;
if (row >= column->children)
return CBF_NOTFOUND;
/* Get the value */
cbf_failnez (cbf_get_columnrow (&text, column, row))
/* Missing value? */
if (!text)
return cbf_write_h5ascii (handle, row, text, h5handle);
/* Plain ASCII? */
cbf_failnez (cbf_value_type ((char *) text))
if (*text == CBF_TOKEN_WORD ||
*text == CBF_TOKEN_SQSTRING ||
*text == CBF_TOKEN_DQSTRING ||
*text == CBF_TOKEN_SCSTRING ||
*text == CBF_TOKEN_TSQSTRING ||
*text == CBF_TOKEN_TDQSTRING ||
*text == CBF_TOKEN_PRNSTRING ||
*text == CBF_TOKEN_BKTSTRING ||
*text == CBF_TOKEN_BRCSTRING ||
*text == CBF_TOKEN_NULL)
return cbf_write_h5ascii (handle, row, text, h5handle);
/* Plain binary? */
if (*text == CBF_TOKEN_BIN || *text == CBF_TOKEN_TMP_BIN)
return cbf_write_h5binary (handle, column, row, h5handle);
/* Undecoded MIME? */
if (*text == CBF_TOKEN_MIME_BIN)
{
/* Convert the value to a normal binary section */
cbf_failnez (cbf_mime_temp (column, row))
return cbf_write_h5binary (handle, column, row, h5handle);
}
/* Fail */
return CBF_ARGUMENT;
}
/* Write a CBF value into a NeXus file
Will add a piece of data with a given name to /entry/group@groupNXclass/subGroup@subGroupNXclass/name */
int cbf_map_h5value(
const char * const name, const char * const value,
const char * const group, const char * const groupNXclass,
const char * const subGroup, const char * const subGroupNXclass,
const size_t attrc, const cbf_name_value_pair * const attrv,
cbf_h5handle h5handle)
{
/* check args & define local variables */
int error = (attrc != 0 && !attrv) ? CBF_ARGUMENT : CBF_SUCCESS;
hid_t h5group = CBF_H5FAIL, h5subGroup = CBF_H5FAIL, h5dataset = CBF_H5FAIL, h5type = CBF_H5FAIL;
const cbf_name_value_pair * attrit = attrv;
/* Get the datatype */
cbf_reportnez(cbf_H5Tcreate_string(&h5type, _cbf_strlen(value)), error);
/* Assume nxid is a valid handle, need to ensure that the /entry/instrument group exists, then ensure the detector group exists */
cbf_reportnez(cbf_require_nxgroup(h5handle, group, groupNXclass, h5handle->nxid, &h5group), error);
cbf_reportnez(cbf_require_nxgroup(h5handle, subGroup, subGroupNXclass, h5group, &h5subGroup), error);
/* write some data */
cbf_reportnez(cbf_H5Dcreate(h5subGroup,&h5dataset,name,0,0,0,0,h5type),error);
cbf_reportnez(cbf_H5Dwrite(h5dataset,0,0,0,value),error);
for (; attrit < attrv+attrc; ++attrit)
cbf_reportnez(cbf_H5Arequire_string(h5dataset,attrit->name,attrit->value),error);
/* clean up */
if (cbf_H5Ivalid(h5type)) cbf_H5Tfree(h5type);
if (cbf_H5Ivalid(h5dataset)) cbf_H5Dfree(h5dataset);
if (cbf_H5Ivalid(h5subGroup)) cbf_H5Gfree(h5subGroup);
if (cbf_H5Ivalid(h5group)) cbf_H5Gfree(h5group);
return error;
}
/* Write a category to an HDF5 file */
int cbf_write_h5category (cbf_handle handle,
const cbf_node *category,
cbf_h5handle h5handle)
{
unsigned int column, row;
const char instGroup[] = "instrument";
const char instGroupClass[] = "NXinstrument";
hid_t instrumentid;
unsigned int colrow;
cbf_fast_bookmark fbkmk;
int errorcode;
errorcode = 0;
/* Check the arguments */
if (!category || !h5handle || h5handle->rootid <0 || h5handle->dbid < 0)
return CBF_ARGUMENT;
/* bookmark the handle */
cbf_failnez(cbf_get_fast_bookmark(handle,&fbkmk));
/* If another category is open, close it */
if (h5handle->colid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->colid),CBF_FORMAT);
h5handle->colid = CBF_H5FAIL;
}
if (h5handle->catid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->catid),CBF_FORMAT);
h5handle->catid = CBF_H5FAIL;
}
/* save the category name in the read bookmark */
(h5handle->bookmark).category = category->name;
/* Write the name under the open save frame or datablock */
if (h5handle->sfid <0) {
cbf_h5failneg(h5handle->catid=H5Gcreatex(h5handle->dbid,
(category->name)?(category->name):"_(null)_"),
CBF_FORMAT);
} else {
cbf_h5failneg(h5handle->catid=H5Gcreatex(h5handle->dbid,
(category->name)?(category->name):"_(null)_"),
CBF_FORMAT);
}
/* cbf_debug_print(category->name); */
handle->node = (cbf_node *)category;
handle->row = 0;
/* Process diffrn_detector category if CBF_H5_NXPDB is not set */
if (!cbf_cistrcmp(category->name,"diffrn_detector")&& !(h5handle->flags&CBF_H5_NXPDB))
{
unsigned int maxrows;
unsigned int row;
int dnamecol;
const char * dname;
const char * value;
char * fullname;
int error = 0;
hid_t detgpid;
maxrows = 0;
dnamecol = -1;
dname = NULL;
detgpid = CBF_H5FAIL;
for (column= 0; column < category->children; column++) {
if ((category->child[column])->children > maxrows)
maxrows=(category->child[column])->children;
if (cbf_cistrcmp((category->child[column])->name,"id")==0)
dnamecol = column;
}
for (row = 0; row < maxrows; row++) {
/*
_diffrn_detector.id DETECTORNAME -->
/instrument:NXinstrument/CBF_diffrn_detector__DETECTORNAME:NXdetector
*/
if (dnamecol==-1 || row >= (category->child[dnamecol])->children) {
dname = "detector";
} else {
handle->node = category->child[dnamecol];
handle->row = row;
cbf_reportnez(cbf_get_value(handle,&dname),error)
}
if (cbf_cistrcmp(dname,"detector")==0) {
fullname = (char *)dname;
} else {
cbf_onfailnez(cbf_strcat("CBF_diffrn_detector__",dname,&fullname),
cbf_goto_fast_bookmark(handle,fbkmk))
}
cbf_reportnez(cbf_require_nxgroup(h5handle,
instGroup,
instGroupClass,
h5handle->nxid,
&instrumentid),error);
cbf_reportnez(cbf_H5NXGrequire_in_handle(h5handle,
fullname,
"NXdetector",
&detgpid,
"detector"),error);
if (fullname != dname) {
cbf_reportnez(cbf_free((void **)&fullname,NULL),error);
}
for (column= 0; column < category->children; column++) {
const char * mappedname;
if (cbf_cistrcmp((category->child[column])->name,"type")==0){
mappedname = "description";
} else if (cbf_cistrcmp((category->child[column])->name,"number_of_axes")==0) {
mappedname = (category->child[column])->name;
} else if (cbf_cistrcmp((category->child[column])->name,"dtime")==0) {
mappedname = "deadtime";
} else if (cbf_cistrcmp((category->child[column])->name,"detector")==0) {
mappedname = "type";
} else if (cbf_cistrcmp((category->child[column])->name,"detector")==0) {
mappedname = "type";
} else if ((int)column == dnamecol) {
continue;
} else {
mappedname = (category->child[column])->name;
}
/* no duplicate rows are allowed because each detector
name must be unique */
if ( H5Lexists(detgpid,mappedname,H5P_DEFAULT)==1) {
cbf_onfailnez(CBF_FORMAT,
cbf_goto_fast_bookmark(handle,fbkmk));
}
handle->node = category->child[column];
handle->row = row;
if (!cbf_get_value(handle,&value) && value) {
cbf_onfailnez(error|cbf_add_h5text_dataset(detgpid,
mappedname,
value,error),
cbf_goto_fast_bookmark(handle,fbkmk));
} else {
cbf_onfailnez(error|cbf_add_h5text_dataset(detgpid,
mappedname,
".",error),
cbf_goto_fast_bookmark(handle,fbkmk));
}
}
}
if (detgpid >= 0) H5Gclose(detgpid);
}
/* Process diffrn_detector_element category */
if (!cbf_cistrcmp(category->name,"diffrn_detector_element")&&!(h5handle->flags&CBF_H5_NXPDB))
{
unsigned int maxrows;
unsigned int row;
int dnamecol, delnamecol;
const char * dname;
char * fullname;
const char * value;
double doublevalue;
int error = 0;
int numeric;
hid_t detgpid;
maxrows = 0;
dnamecol = -1;
delnamecol = -1;
numeric = 0;
dname = NULL;
delnamecol = -1;
detgpid = CBF_H5FAIL;
/* find maximum number of rows, detector id column
and detector element id column */
for (column= 0; column < category->children; column++) {
if ((category->child[column])->children > maxrows)
maxrows=(category->child[column])->children;
if (cbf_cistrcmp((category->child[column])->name,"id")==0)
delnamecol = column;
if (cbf_cistrcmp((category->child[column])->name,"detector_id")==0)
dnamecol = column;
}
for (row = 0; row < maxrows; row++) {
int multi_element;
unsigned int sow;
const char * ename;
multi_element = 0;
/*
_diffrn_detector.id DETECTORNAME -->
/instrument:NXinstrument/CBF_diffrn_detector__DETECTORNAME:NXdetector
*/
if (dnamecol==-1 || row >= (category->child[dnamecol])->children) {
dname = "detector";
} else {
handle->node = category->child[dnamecol];
handle->row = row;
cbf_reportnez(cbf_get_value(handle,&dname),error);
if (!dname) dname = "detector";
}
if (cbf_cistrcmp(dname,"detector")==0) {
fullname = (char *)dname;
} else {
cbf_failnez(cbf_strcat("CBF_diffrn_detector__",dname,&fullname))
}
cbf_reportnez(cbf_require_nxgroup(h5handle,
instGroup,
instGroupClass,
h5handle->nxid,
&instrumentid),error);
cbf_reportnez(cbf_H5NXGrequire_in_handle(h5handle,
fullname,
"NXdetector",
&detgpid,
"detector"),error);
if (fullname != dname) {
cbf_reportnez(cbf_free((void **)&fullname,NULL),error);
}
/* check for the multielement case
if there is another row with the same detector name,
this will be a multielement case to be written by slabs
*/
if (maxrows > 1 && dnamecol == -1) {
multi_element = 1;
} else {
for (sow = 0; sow < maxrows; sow++ ) {
if (sow == row) continue;
handle->node = category->child[dnamecol];
handle->row = sow;
cbf_reportnez(cbf_get_value(handle,&ename),error);
if ( (!ename && !cbf_cistrcmp(dname,"detector") )
|| (ename && !cbf_cistrcmp(ename,dname)) ) {
multi_element = 1;
break;
}
}
}
for (column= 0; column < category->children; column++) {
const char * mappedname;
const char * units;
if (cbf_cistrcmp((category->child[column])->name,"reference_center_fast")==0){
mappedname = "CBF_diffrn_detector_element__reference_center_fast";
numeric = 1;
units = NULL;
} else if (cbf_cistrcmp((category->child[column])->name,"reference_center_slow")==0) {
mappedname = "CBF_diffrn_detector_element__reference_center_slow";
numeric = 1;
units = NULL;
} else if (cbf_cistrcmp((category->child[column])->name,"reference_center_units")==0) {
mappedname = "CBF_diffrn_detector_element__reference_center_units";
numeric = 0;
units = NULL;
} else if ((int)column==delnamecol) {
mappedname = "CBF_diffrn_detector_element__id";
numeric = 0;
units = NULL;
} else if (cbf_cistrcmp((category->child[column])->name,"center[1]")==0){
mappedname = "beam_center_x";
numeric = 1;
units = "mm";
} else if (cbf_cistrcmp((category->child[column])->name,"center[2]")==0){
mappedname = "beam_center_y";
numeric = 1;
units = "mm";
} else if ((int)column == dnamecol ) {
continue;
} else {
/* until there is support for variants, skip remaining columns */
continue;
}
handle->node = category->child[column];
handle->row = row;
if (!numeric) {
if (!cbf_get_value(handle,&value) && value) {
if ( multi_element ) {
cbf_onfailnez(error|cbf_add_h5text_dataset_slab (detgpid,
mappedname,
value,
H5S_UNLIMITED,
error),
cbf_goto_fast_bookmark(handle,fbkmk));
} else {
cbf_onfailnez(error|cbf_add_h5text_dataset(detgpid,
mappedname,
value,error),
cbf_goto_fast_bookmark(handle,fbkmk));
}
} else {
if ( multi_element ) {
cbf_onfailnez(error|cbf_add_h5text_dataset_slab (detgpid,
mappedname,
".",
H5S_UNLIMITED,
error),
cbf_goto_fast_bookmark(handle,fbkmk));
} else {
cbf_onfailnez(error|cbf_add_h5text_dataset(detgpid,
mappedname,
".",error),
cbf_goto_fast_bookmark(handle,fbkmk));
}
}
} else {
if (!cbf_get_doublevalue(handle,&doublevalue) ) {
if ( multi_element ) {
cbf_onfailnez(error|cbf_add_h5double_dataset_slab (detgpid,
mappedname,
doublevalue,
H5S_UNLIMITED,
error),
cbf_goto_fast_bookmark(handle,fbkmk));
} else {
cbf_onfailnez(error|cbf_add_h5double_dataset(detgpid,
mappedname,
doublevalue,error),
cbf_goto_fast_bookmark(handle,fbkmk));
}
} else {
if ( multi_element ) {
cbf_onfailnez(error|cbf_add_h5double_dataset_slab (detgpid,
mappedname,
0.e0,
H5S_UNLIMITED,
error),
cbf_goto_fast_bookmark(handle,fbkmk));
} else {
cbf_onfailnez(error|cbf_add_h5double_dataset(detgpid,
mappedname,
0.e0,error),
cbf_goto_fast_bookmark(handle,fbkmk));
}
}
if (units) {
hid_t datasetid=CBF_H5FAIL;
cbf_h5reportneg(datasetid = H5Dopen(detgpid,mappedname,H5P_DEFAULT),CBF_H5ERROR, error);
cbf_failnez(cbf_apply_h5text_attribute(h5handle->colid,
"units",units,error));
if (cbf_H5Ivalid(datasetid)) H5Dclose(datasetid);
}
}
}
}
if (detgpid >= 0) H5Gclose(detgpid);
}
/**** Check for closing instrumentid ****/
/* Process diffrn_radiation category */
if (!cbf_cistrcmp(category->name,"diffrn_radiation")&&!(h5handle->flags&CBF_H5_NXPDB))
{
unsigned int maxrows;
unsigned int row;
const char * value;
double doublevalue;
hid_t monochromid;
int error = 0;
int numeric;
maxrows = 0;
numeric = 0;
/* find maximum number of rows, detector id column
and detector element id column */
for (column= 0; column < category->children; column++) {
if ((category->child[column])->children > maxrows)
maxrows=(category->child[column])->children;
}
cbf_reportnez(cbf_require_nxgroup(h5handle,
instGroup,
instGroupClass,
h5handle->nxid,
&instrumentid),error);
cbf_reportnez(cbf_H5NXGrequire_in_handle(h5handle,
"monochromator",
"NXmonochromator",
&monochromid,
NULL),error);
for (row = 0; row < maxrows; row++) {
int multi_element;
multi_element = 0;
/* check for the multi-element case
if there is another row with the same detector name,
this will be a multielement case to be written by slabs
*/
for (column= 0; column < category->children; column++) {
const char * mappedname;
const char * units;
CBF_UNUSED( mappedname );
CBF_UNUSED( units );
if (cbf_cistrcmp((category->child[column])->name,"wavelength")==0){
mappedname = "wavelength";
numeric = 1;
units = "Angstroms";
} else if (cbf_cistrcmp((category->child[column])->name,"reference_center_slow")==0) {
mappedname = "CBF_diffrn_detector_element__reference_center_slow";
numeric = 1;
units = NULL;
} else if (cbf_cistrcmp((category->child[column])->name,"reference_center_units")==0) {
mappedname = "CBF_diffrn_detector_element__reference_center_units";
numeric = 0;
units = NULL;
} else if (cbf_cistrcmp((category->child[column])->name,"center[1]")==0){
mappedname = "beam_center_x";
numeric = 1;
units = "mm";
} else if (cbf_cistrcmp((category->child[column])->name,"center[2]")==0){
mappedname = "beam_center_y";
numeric = 1;
units = "mm";
} else {
/* until there is support for variants, skip remaining columns */
continue;
}
handle->node = category->child[column];
handle->row = row;
if (!numeric) {
if (!cbf_get_value(handle,&value) && value) {
if ( multi_element ) {
/* cbf_onfailnez(error|cbf_add_h5text_dataset_slab (detgpid,
mappedname,
value,
H5S_UNLIMITED,
error),
cbf_goto_fast_bookmark(handle,fbkmk)); */
} else {
/* cbf_onfailnez(error|cbf_add_h5text_dataset(detgpid,
mappedname,
value,error),
cbf_goto_fast_bookmark(handle,fbkmk)); */
}
} else {
if ( multi_element ) {
/* cbf_onfailnez(error|cbf_add_h5text_dataset_slab (detgpid,
mappedname,
".",
H5S_UNLIMITED,
error),
cbf_goto_fast_bookmark(handle,fbkmk)); */
} else {
/* cbf_onfailnez(error|cbf_add_h5text_dataset(detgpid,
mappedname,
".",error),
cbf_goto_fast_bookmark(handle,fbkmk)); */
}
}
} else {
if (!cbf_get_doublevalue(handle,&doublevalue) ) {
if ( multi_element ) {
/* cbf_onfailnez(error|cbf_add_h5double_dataset_slab (detgpid,
mappedname,
doublevalue,
H5S_UNLIMITED,
error),
cbf_goto_fast_bookmark(handle,fbkmk)); */
} else {
/* cbf_onfailnez(error|cbf_add_h5double_dataset(detgpid,
mappedname,
doublevalue,error),
cbf_goto_fast_bookmark(handle,fbkmk)); */
}
} else {
if ( multi_element ) {
/* cbf_onfailnez(error|cbf_add_h5double_dataset_slab (detgpid,
mappedname,
0.e0,
H5S_UNLIMITED,
error),
cbf_goto_fast_bookmark(handle,fbkmk)); */
} else {
/* cbf_onfailnez(error|cbf_add_h5double_dataset(detgpid,
mappedname,
0.e0,error),
cbf_goto_fast_bookmark(handle,fbkmk)); */
}
}
/* if (units) {
hid_t datasetid=CBF_H5FAIL;
cbf_h5reportneg(datasetid = H5Dopen(detgpid,mappedname,H5P_DEFAULT),CBF_H5ERROR, error);
cbf_failnez(cbf_apply_h5text_attribute(h5handle->colid,
"units",units,error));
if (cbf_H5Ivalid(datasetid)) H5Dclose(datasetid);
} */
}
}
}
/* if (detgpid >= 0) H5Gclose(detgpid); */
}
if (!cbf_cistrcmp(category->name,"diffrn_radiation")&&!(h5handle->flags&CBF_H5_NXPDB))
{
for (column= 0; column < category->children; column++)
{
cbf_node * column_node = category->child[column];
/* cbf_debug_print2("\t%s",column_node->name); */
if(column_node->children > 1) cbf_debug_print2("warning: too many rows in '%s', only writing first row.\n",category->name);
if(!cbf_cistrcmp(column_node->name,"div_x_source")) {
const char * text = 0;
int error = CBF_SUCCESS;
cbf_reportnez(cbf_get_columnrow (&text, column_node, 0),error);
/* check 'text' is suitable for use as an ascii data value */
cbf_reportnez(cbf_map_h5value("divergence_x", text+1, instGroup, instGroupClass, "collimator", "NXcollimator", 0, 0,
h5handle),error);
if (CBF_SUCCESS != error) cbf_debug_print("diffrn_radiation.div_x_source could not be written to nexus file");
}
if(!cbf_cistrcmp(column_node->name,"div_y_source")) {
const char * text = 0;
int error = CBF_SUCCESS;
cbf_reportnez(cbf_get_columnrow (&text, column_node, 0),error);
/* check 'text' is suitable for use as an ascii data value */
cbf_reportnez(cbf_map_h5value("divergence_y", text+1, instGroup, instGroupClass, "collimator", "NXcollimator", 0, 0,
h5handle),error);
if (CBF_SUCCESS != error) cbf_debug_print("diffrn_radiation.div_y_source could not be written to nexus file");
}
}
}
if (!cbf_cistrcmp(category->name,"diffrn_radiation_wavelength")&&!(h5handle->flags&CBF_H5_NXPDB))
{
for (column= 0; column < category->children; column++)
{
cbf_node * column_node = category->child[column];
/* cbf_debug_print2("\t%s",column_node->name); */
if(column_node->children > 1) cbf_debug_print2( "warning: too many rows in '%s', only writing first row.\n",category->name);
if(!cbf_cistrcmp(column_node->name,"wavelength")){
cbf_name_value_pair attr = {"units","Angstroms"};
const char * text = 0;
int error = CBF_SUCCESS;
cbf_reportnez(cbf_get_columnrow (&text, column_node, 0),error);
/* check 'text' is suitable for use as an ascii data value */
cbf_reportnez(cbf_map_h5value("wavelength", text+1, instGroup, instGroupClass, "monochromator", "NXmonochromator", 1,
&attr, h5handle),error);
if (CBF_SUCCESS != error) cbf_debug_print("diffrn_radiation_wavelength.wavelength could not be written to nexus file");
}
if(!cbf_cistrcmp(column_node->name,"wt")) {
const char * text = 0;
int error = CBF_SUCCESS;
cbf_reportnez(cbf_get_columnrow (&text, column_node, 0),error);
/* check 'text' is suitable for use as an ascii data value */
cbf_reportnez(cbf_map_h5value("weight", text+1, instGroup, instGroupClass, "monochromator", "NXmonochromator", 0, 0,
h5handle),error);
if (CBF_SUCCESS != error) cbf_debug_print("diffrn_radiation_wavelength.wt could not be written to nexus file");
}
}
}
if (h5handle->flags&CBF_H5_NXPDB) {
cbf_failnez(cbf_apply_h5text_attribute(h5handle->catid,
"NX_class","NXpdb",errorcode));
cbf_failnez(cbf_apply_h5text_attribute(h5handle->catid,
"NXpdb_class", "CBF_cbfcat",0));
} else {
cbf_failnez(cbf_apply_h5text_attribute(h5handle->catid,
"NX_class","CBF_cbfcat",errorcode));
}
/* now, for each column, make it into an array dataset, or a group. For NXpdb
and for text values use an array dataset, otherwise make the column into
a group.
*/
if (h5handle->flags&CBF_H5_NXPDB) {
cbf_node * column_node;
const char * text = 0;
for (column= 0; column < category->children; column++) {
column_node = category->child[column];
if (column_node->children > 1 ) {
for (colrow=0; colrow < column_node->children; colrow++) {
if(cbf_get_columnrow (&text, column_node, colrow) || !text) {
text = " .";
}
if (cbf_add_h5text_dataset_slab(h5handle->catid,
column_node->name,
text+1,
colrow,errorcode)) break;
}
} else {
if(cbf_get_columnrow (&text, column_node, 0) || !text) {
text = " .";
}
cbf_failnez(cbf_add_h5text_dataset(h5handle->catid,
column_node->name,text+1,errorcode))
}
}
}
if (!(h5handle->flags&CBF_H5_NXPDB) )
for (column= 0; column < category->children; column++)
{
/* save the column name in the read bookmark */
(h5handle->bookmark).column = (category->child[column])->name;
cbf_h5failneg(h5handle->colid=H5Gcreatex(h5handle->catid,
(category->child[column])->name?
(category->child[column])->name:"_(null)_"),
CBF_FORMAT);
if (h5handle->flags&CBF_H5_NXPDB) {
cbf_failnez(cbf_apply_h5text_attribute(h5handle->colid,
"NX_class","NXpdb",errorcode));
cbf_failnez(cbf_apply_h5text_attribute(h5handle->colid,
"NXpdb_class", "CBF_cbfcol",0));
} else {
cbf_failnez(cbf_apply_h5text_attribute(h5handle->colid,
"NX_class","CBF_cbfcol",errorcode));
}
/* For each row, create a dataset */
for (row=0; row < category->child [column]->children; row++)
{
(h5handle->bookmark).row = row;
cbf_failnez(cbf_write_h5value(handle,
category->child [column],
row,h5handle));
}
cbf_h5failneg(H5Gclose(h5handle->colid),CBF_ARGUMENT);
h5handle->colid = CBF_H5FAIL;
}
if (!cbf_cistrcmp(category->name,"diffrn_scan")&&!(h5handle->flags&CBF_H5_NXPDB)) {
cbf_node * column_node;
const char * text = 0;
for (column= 0; column < category->children; column++) {
column_node = category->child[column];
if(!cbf_cistrcmp(column_node->name,"id")){
if (column_node->children > 1 ) {
for (colrow=0; colrow < column_node->children; colrow++) {
if(cbf_get_columnrow (&text, column_node, colrow)) {
text = " .";
}
if (cbf_add_h5text_dataset_slab(h5handle->nxid,
"CBF_scan_id",
text+1,
colrow,errorcode)) break;
}
} else {
if(cbf_get_columnrow (&text, column_node, 0)) break;
if (!text) break;
if(cbf_add_h5text_dataset(h5handle->nxid,
"CBF_scan_id",text+1,errorcode)) break;
break;
}
}
}
}
if (!cbf_cistrcmp(category->name,"entry")&&!(h5handle->flags&CBF_H5_NXPDB)) {
cbf_node * column_node;
const char * text = 0;
for (column= 0; column < category->children; column++) {
column_node = category->child[column];
if(!cbf_cistrcmp(column_node->name,"id")){
if (column_node->children > 1 ) {
for (colrow=0; colrow < column_node->children; colrow++) {
if(cbf_get_columnrow (&text, column_node, colrow)) {
text = " .";
}
if (cbf_add_h5text_dataset_slab(h5handle->nxid,
"CBF_entry_id",
text+1,
colrow,errorcode)) break;
}
} else {
if(cbf_get_columnrow (&text, column_node, 0)) break;
if (!text) break;
if(cbf_add_h5text_dataset(h5handle->nxid,
"CBF_entry_id",text+1,errorcode)) break;
break;
}
}
}
}
if (!cbf_cistrcmp(category->name,"diffrn")&&!(h5handle->flags&CBF_H5_NXPDB)) {
cbf_node * column_node;
const char * text = 0;
for (column= 0; column < category->children; column++) {
column_node = category->child[column];
if(!cbf_cistrcmp(column_node->name,"id")){
if (column_node->children > 1 ) {
for (colrow=0; colrow < column_node->children; colrow++) {
if(cbf_get_columnrow (&text, column_node, colrow)) {
text = " .";
}
if (cbf_add_h5text_dataset_slab(h5handle->nxid,
"CBF_diffrn_id",
text+1,
colrow, errorcode)) break;
}
} else {
if(cbf_get_columnrow (&text, column_node, 0)) break;
if (!text) break;
if(cbf_add_h5text_dataset(h5handle->nxid,
"CBF_diffrn_id",text+1,errorcode)) break;
break;
}
}
}
}
cbf_failnez(cbf_goto_fast_bookmark(handle,fbkmk));
/* Success */
return CBF_SUCCESS;
}
/* create top-level NXentry */
int cbf_create_NXentry(cbf_h5handle h5handle)
{
if (!h5handle ||
h5handle->nxid >= 0 ||
h5handle->hfile < 0) return CBF_ARGUMENT;
cbf_failnez(cbf_h5handle_require_entry(h5handle,0,0));
h5handle->curnxid=CBF_H5FAIL;
h5handle->dataid =CBF_H5FAIL;
return CBF_SUCCESS;
}
/* get the current hdf5 group from the handle */
static int cbf_get_H5groupid(const cbf_h5handle h5handle,
hid_t * curnxid) {
if (!h5handle || !curnxid) return CBF_ARGUMENT;
if (h5handle->nxid < 0) {
cbf_failnez(cbf_create_NXentry(h5handle));
}
*curnxid = (h5handle->curnxid >= 0)? h5handle->curnxid: h5handle->nxid;
return CBF_SUCCESS;
}
/* Create an HDF5 Group below NX entry or below curnxid */
int cbf_H5Gcreate_in_handle(cbf_h5handle h5handle,
const char * groupname,
hid_t * newgroup)
{
hid_t parent;
if (!h5handle) return CBF_ARGUMENT;
if (h5handle->nxid < 0) {
cbf_failnez(cbf_create_NXentry(h5handle));
}
parent = (h5handle->curnxid >= 0)? h5handle->curnxid: h5handle->nxid;
cbf_h5failneg(*newgroup=H5Gcreatex(parent,groupname),
CBF_FORMAT);
return CBF_SUCCESS;
}
/* Create an HDF5 NeXus Group below NX entry or below curnxid */
int cbf_H5NXGcreate(cbf_h5handle h5handle,
const char * groupname,
const char * nxclass,
hid_t * newgroup )
{
cbf_failnez(cbf_H5Gcreate_in_handle(h5handle, groupname, newgroup));
cbf_failnez(cbf_apply_h5text_attribute(*newgroup,
"NX_class",nxclass,0));
return CBF_SUCCESS;
}
/* Require a group below NXentry or below curnxid
groupname -- the final path to be used
group_id -- optional pointer to the newly opened group
if NULL, no return and the group will be
closed
oldgroupname -- an optional existing path name or NULL
to be renamed to new_name if old_name exists
and if groupname does not yet exist
returns 0 for success
*/
int cbf_H5Grequire_in_handle(const cbf_h5handle h5handle,
const char * groupname,
hid_t * group_id,
const char * oldgroupname) {
htri_t gpexists;
hid_t child_id;
hid_t parent_id;
/* Check arguments */
if (!h5handle || ! groupname) return CBF_ARGUMENT;
/* Get (or create) the parent_id */
cbf_failnez(cbf_get_H5groupid(h5handle,&parent_id));
/* see if the new name is already there */
gpexists = H5Lexists(parent_id, groupname, H5P_DEFAULT);
if (gpexists < 0 || !gpexists ) {
/* see if the old name is there */
if (oldgroupname) {
gpexists = H5Lexists(parent_id, oldgroupname, H5P_DEFAULT);
if (gpexists < 0 || !gpexists ) {
/* Neither path exists, just create the new group */
cbf_failnez(cbf_H5Gcreate_in_handle(h5handle,
groupname,
group_id));
return CBF_SUCCESS;
} else {
/* the old path exists, rename it */
cbf_h5failneg(child_id=H5Gopenx(parent_id,oldgroupname),
CBF_FORMAT);
cbf_h5failneg(H5Ldelete(parent_id,oldgroupname,H5P_DEFAULT),CBF_FORMAT);
if (H5Olink(child_id,parent_id,groupname,
H5P_DEFAULT,H5P_DEFAULT)< 0) {
H5Olink(child_id,parent_id,oldgroupname,
H5P_DEFAULT,H5P_DEFAULT);
H5Gclose(child_id);
if (*group_id) *group_id = CBF_H5FAIL;
return CBF_FORMAT;
}
if (group_id) {
*group_id = child_id;
} else {
cbf_h5failneg(H5Gclose(child_id),CBF_FORMAT);
}
}
} else {
/* create the new group */
cbf_failnez(cbf_H5Gcreate_in_handle(h5handle,
groupname,
group_id));
}
return CBF_SUCCESS;
}
if (group_id) {
cbf_h5failneg(*group_id=H5Gopenx(parent_id,groupname),
CBF_FORMAT);
if (*group_id < 0) return CBF_FORMAT;
}
return CBF_SUCCESS;
}
/* Require NeXus group below NXentry or below curnxid
groupname -- the final path to be used
group_id -- optional pointer to the newly opened group
if NULL, no return and the group will be
closed
oldgroupname -- an optional existing path name or NULL
to be renamed to new_name if old_name exists
and if groupname does not yet exist
returns 0 for success
*/
int cbf_H5NXGrequire_in_handle(const cbf_h5handle h5handle,
const char * groupname,
const char * nxclass,
hid_t * group_id,
const char * oldgroupname) {
hid_t child_id;
htri_t aexists;
if (!h5handle || !groupname || !nxclass) return CBF_ARGUMENT;
cbf_failnez(cbf_H5Grequire_in_handle(h5handle,
groupname,
&child_id,
oldgroupname));
aexists = H5Aexists(child_id,"NX_class");
if (aexists < 0 || !aexists ) {
cbf_failnez(cbf_apply_h5text_attribute(child_id,
"NX_class",nxclass,0));
}
if (group_id) {
*group_id = child_id;
} else {
cbf_h5failneg(H5Gclose(child_id),CBF_FORMAT);
}
return CBF_SUCCESS;
}
/**
Checks if the handle appears to be valid, the free's the handle and any data that the handle owns.
\param h5handle The handle to be free'd.
\sa cbf_create_h5handle3
\return An error code
*/
int cbf_free_h5handle(cbf_h5handle h5handle)
{
int error = CBF_SUCCESS;
int ii;
if (!h5handle) {
error |= CBF_ARGUMENT;
} else {
void * memblock = (void *) h5handle;
void * detblock = (void *) h5handle->nxdetectors;
void * detnameblock = (void *) h5handle->nxdetector_names;
/* cbf_debug_print("Entering cbf_free_h5handle"); */
if (cbf_H5Ivalid(h5handle->colid)) {
CBF_H5CALL(H5Gclose(h5handle->colid));
}
if (cbf_H5Ivalid(h5handle->catid)) {
CBF_H5CALL(H5Gclose(h5handle->catid));
}
if (cbf_H5Ivalid(h5handle->sfid)) {
CBF_H5CALL(H5Gclose(h5handle->sfid));
}
if (cbf_H5Ivalid(h5handle->dbid)) {
CBF_H5CALL(H5Gclose(h5handle->dbid));
}
if (cbf_H5Ivalid(h5handle->rootid)) {
CBF_H5CALL(H5Gclose(h5handle->rootid));
}
if (cbf_H5Ivalid(h5handle->curnxid)) {
CBF_H5CALL(H5Gclose(h5handle->curnxid));
}
if (cbf_H5Ivalid(h5handle->dataid)) {
CBF_H5CALL(H5Gclose(h5handle->dataid));
}
if (cbf_H5Ivalid(h5handle->nxid)) {
CBF_H5CALL(H5Gclose(h5handle->nxid));
}
if (cbf_H5Ivalid(h5handle->nxdata)) {
CBF_H5CALL(H5Gclose(h5handle->nxdata));
}
if (cbf_H5Ivalid(h5handle->nxinst)) {
CBF_H5CALL(H5Gclose(h5handle->nxinst));
}
if (cbf_H5Ivalid(h5handle->nxsample)) {
CBF_H5CALL(H5Gclose(h5handle->nxsample));
}
if (cbf_H5Ivalid(h5handle->nxbeam)) {
CBF_H5CALL(H5Gclose(h5handle->nxbeam));
}
for (ii=0; ii< (ssize_t)(h5handle->num_detectors); ii++) {
if (cbf_H5Ivalid(h5handle->nxdetectors[ii])) {
CBF_H5CALL(H5Gclose(h5handle->nxdetectors[ii]));
}
}
if (cbf_H5Ivalid(h5handle->nxdetector_group)) {
CBF_H5CALL(H5Gclose(h5handle->nxdetector_group));
}
if (cbf_H5Ivalid(h5handle->nxgoniometer)) {
CBF_H5CALL(H5Gclose(h5handle->nxgoniometer));
}
if (cbf_H5Ivalid(h5handle->nxmonochromator)) {
CBF_H5CALL(H5Gclose(h5handle->nxmonochromator));
}
if (cbf_H5Ivalid(h5handle->nxsource)) {
CBF_H5CALL(H5Gclose(h5handle->nxsource));
}
if (cbf_H5Ivalid(h5handle->hfile)) {
CBF_H5CALL(H5Fclose(h5handle->hfile));
h5handle->hfile = CBF_H5FAIL;
}
if (h5handle->scan_id) {
free((void*)h5handle->scan_id);
h5handle->scan_id = NULL;
}
if (h5handle->sample_id) {
free((void*)h5handle->sample_id);
h5handle->sample_id = NULL;
}
if (h5handle->nxid_name) {
free((void*)h5handle->nxid_name);
h5handle->nxid_name = NULL;
}
for (ii=0; ii < (ssize_t)(h5handle->num_detectors); ii++){
if (h5handle->nxdetector_names[ii]) {
free((void*)h5handle->nxdetector_names[ii]);
h5handle->nxdetector_names[ii] = NULL;
}
}
h5handle->num_detectors = 0;
if (h5handle->nxdetectors) {
error |= cbf_free(&detblock,NULL);
h5handle->nxdetectors = NULL;
}
if (h5handle->nxdetector_names) {
error |= cbf_free(&detnameblock,NULL);
h5handle->nxdetector_names = NULL;
}
if (h5handle->nxdetector_group_name) {
free((void*)h5handle->nxdetector_group_name);
h5handle->nxdetector_group_name = NULL;
}
if (h5handle->nxsample_name) {
free((void*)h5handle->nxsample_name);
h5handle->nxsample_name = NULL;
}
if (h5handle->nxbeam_name) {
free((void*)h5handle->nxbeam_name);
h5handle->nxbeam_name = NULL;
}
if (h5handle->nxinstrument_name) {
free((void*)h5handle->nxinstrument_name);
h5handle->nxinstrument_name = NULL;
}
if (h5handle->nxgoniometer_name) {
free((void*)h5handle->nxgoniometer_name);
h5handle->nxgoniometer_name = NULL;
}
if (h5handle->nxmonochromator_name) {
free((void*)h5handle->nxmonochromator_name);
h5handle->nxmonochromator_name = NULL;
}
if (h5handle->nxsource_name) {
free((void*)h5handle->nxsource_name);
h5handle->nxsource_name = NULL;
}
if (h5handle->nxfilename) {
free((void*)h5handle->nxfilename);
h5handle->nxfilename = NULL;
}
cbf_free_handle(h5handle->scratch_tables);
error |= cbf_free(&memblock,NULL);
/* cbf_debug_print("Exiting cbf_free_h5handle"); */
}
return error;
}
/* Make an (empty) H5File handle */
int cbf_make_h5handle(cbf_h5handle *h5handle) {
cbf_failnez (cbf_alloc ((void **) h5handle, NULL,
sizeof(cbf_h5handle_struct), 1));
(*h5handle)->slice = 0;
(*h5handle)->block = 0;
(*h5handle)->blocksize = 0;
(*h5handle)->num_detectors = 0;
(*h5handle)->hfile = (hid_t)CBF_H5FAIL;
(*h5handle)->rootid = (hid_t)CBF_H5FAIL;
(*h5handle)->dbid = (hid_t)CBF_H5FAIL;
(*h5handle)->sfid = (hid_t)CBF_H5FAIL;
(*h5handle)->catid = (hid_t)CBF_H5FAIL;
(*h5handle)->colid = (hid_t)CBF_H5FAIL;
(*h5handle)->nxid = (hid_t)CBF_H5FAIL;
(*h5handle)->nxdata = (hid_t)CBF_H5FAIL;
(*h5handle)->nxinst = (hid_t)CBF_H5FAIL;
(*h5handle)->nxsample = (hid_t)CBF_H5FAIL;
(*h5handle)->nxbeam = (hid_t)CBF_H5FAIL;
(*h5handle)->nxdetector_group = (hid_t)CBF_H5FAIL;
(*h5handle)->nxdetectors = NULL;
(*h5handle)->num_detectors = 0;
(*h5handle)->cur_detector = 0;
(*h5handle)->nxgoniometer = (hid_t)CBF_H5FAIL;
(*h5handle)->nxmonochromator = (hid_t)CBF_H5FAIL;
(*h5handle)->nxsource = (hid_t)CBF_H5FAIL;
(*h5handle)->curnxid = (hid_t)CBF_H5FAIL;
(*h5handle)->dataid = (hid_t)CBF_H5FAIL;
(*h5handle)->nxid_name = NULL;
(*h5handle)->nxdetector_group_name = NULL;
(*h5handle)->nxdetector_names = NULL;
(*h5handle)->nxsample_name = NULL;
(*h5handle)->nxbeam_name = NULL;
(*h5handle)->nxinstrument_name = NULL;
(*h5handle)->nxgoniometer_name = NULL;
(*h5handle)->nxmonochromator_name = NULL;
(*h5handle)->nxsource_name = NULL;
(*h5handle)->dbid_name = NULL;
(*h5handle)->sfid_name = NULL;
(*h5handle)->catid_name = NULL;
(*h5handle)->colid_name = NULL;
(*h5handle)->rwmode = 0;
(*h5handle)->flags = 0;
#ifdef CBF_USE_ULP
(*h5handle)->cmp_double_as_float = 0;
(*h5handle)->float_ulp = 0;
#ifndef NO_UINT64_TYPE
(*h5handle)->double_ulp = 0;
#endif
#endif
(*h5handle)->scan_id = NULL;
(*h5handle)->sample_id = NULL;
#ifdef CBFDEBUG
(*h5handle)->logfile = stderr;
#else
(*h5handle)->logfile = NULL;
#endif
(*h5handle)->nxfilename = NULL;
(*h5handle)->cbf_datablock = NULL;
(*h5handle)->cbf_saveframe = NULL;
(*h5handle)->cbf_category = NULL;
(*h5handle)->cbf_column = NULL;
cbf_make_handle(&((*h5handle)->scratch_tables));
return CBF_SUCCESS;
}
/* Close the current saveframe in an HDF5 file */
int cbf_close_h5saveframe (cbf_h5handle h5handle)
{
/* Does the node exist? */
if (!h5handle || h5handle->rootid <0 || h5handle->dbid <0)
return CBF_ARGUMENT;
if (h5handle->colid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->colid),CBF_FORMAT);
h5handle->colid = (hid_t)-1;
}
if (h5handle->catid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->catid),CBF_FORMAT);
h5handle->catid = (hid_t)-1;
}
if (h5handle->sfid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->sfid),CBF_FORMAT);
h5handle->sfid = (hid_t)-1;
}
return CBF_SUCCESS;
}
/* Write a saveframe name to an HDF5 file
Make a new group of NeXus class CBF_cbfsf
in the CBF_cbf current datablock
*/
int cbf_write_h5saveframename (const cbf_node *saveframe,
cbf_h5handle h5handle)
{
/* Does the node exist? */
if (!saveframe || !h5handle || h5handle->rootid <0 || h5handle->dbid <0)
return CBF_ARGUMENT;
/* If another saveframe is open, close all its children
and the saveframe itself */
if (h5handle->sfid >= 0) {
cbf_failnez(cbf_close_h5saveframe(h5handle));
}
/* Write the name */
cbf_h5failneg(h5handle->sfid=H5Gcreatex(h5handle->dbid,
saveframe->name),
CBF_FORMAT);
if (h5handle->flags&CBF_H5_NXPDB) {
cbf_failnez(cbf_apply_h5text_attribute(h5handle->sfid,
"NX_class","NXpdb",0));
cbf_failnez(cbf_apply_h5text_attribute(h5handle->sfid,
"NXpdb_class", "CBF_cbfsf",0));
} else {
cbf_failnez(cbf_apply_h5text_attribute(h5handle->sfid,
"NX_class", "CBF_cbfsf",0));
}
return CBF_SUCCESS;
}
/* Write a datablock name to an HDF5 file
Make a new group of NeXus class CBF_cbfdb in the CBF_cbf class root
*/
int cbf_write_h5datablockname (const cbf_node *datablock, cbf_h5handle h5handle)
{
int errorcode = 0;
/* Does the node exist? */
if (!datablock || !h5handle || h5handle->rootid <0)
return CBF_ARGUMENT;
/* If another datablock is open, close all its children
and the datablock itself */
if (h5handle->colid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->colid),CBF_FORMAT);
h5handle->colid = (hid_t)-1;
}
if (h5handle->catid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->catid),CBF_FORMAT);
h5handle->catid = (hid_t)-1;
}
if (h5handle->sfid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->sfid),CBF_FORMAT);
h5handle->sfid = (hid_t)-1;
}
if (h5handle->dbid >= 0) {
cbf_h5failneg(H5Gclose(h5handle->dbid),CBF_FORMAT);
h5handle->dbid = (hid_t)-1;
}
/* save the datablock name in the read bookmark */
(h5handle->bookmark).datablock = datablock->name;
/* Write the name */
cbf_h5failneg(h5handle->dbid=H5Gcreatex(h5handle->rootid,
datablock->name),
CBF_FORMAT);
if (h5handle->flags&CBF_H5_NXPDB) {
errorcode = 0;
cbf_failnez(cbf_apply_h5text_attribute(h5handle->dbid,
"NX_class","NXpdb",errorcode)||errorcode);
cbf_failnez(cbf_apply_h5text_attribute(h5handle->dbid,
"NXpdb_class", "CBF_cbfdb",0));
} else {
cbf_failnez(cbf_apply_h5text_attribute(h5handle->dbid,
"NX_class","CBF_cbfdb",errorcode)||errorcode);
}
return CBF_SUCCESS;
}
/* Write a node to an HDF5 file */
int cbf_write_h5node (cbf_handle handle, const cbf_node *node,
const cbf_h5handle h5handle)
{
unsigned int count;
/* Follow any links */
node = cbf_get_link (node);
/* Does the node exist? */
if (!node)
return CBF_ARGUMENT;
/* Node type */
switch (node->type)
{
/* For the root, start the file with a CBF group */
case CBF_ROOT:
break;
case CBF_DATABLOCK:
if (h5handle->rootid < 0) return CBF_FORMAT;
cbf_failnez (cbf_write_h5datablockname (node, h5handle))
break;
case CBF_CATEGORY:
cbf_failnez (cbf_write_h5category (handle, node, h5handle))
break;
case CBF_SAVEFRAME:
cbf_failnez (cbf_write_h5saveframename (node, h5handle))
break;
default:
return CBF_ARGUMENT;
}
/* Write the children */
if (node->type == CBF_ROOT || node->type == CBF_DATABLOCK || node->type == CBF_SAVEFRAME)
for (count = 0; count < node->children; count++)
{
cbf_failnez (cbf_write_h5node (handle, node->child [count], h5handle))
}
if (node->type == CBF_SAVEFRAME) {
cbf_failnez(cbf_close_h5saveframe(h5handle));
}
/* Flush the buffers */
cbf_h5failneg(H5Fflush(h5handle->hfile,H5F_SCOPE_LOCAL),CBF_ARGUMENT);
return CBF_SUCCESS;
}
/* Create an HDF5 File handle */
int cbf_create_h5handle(cbf_h5handle *h5handle,const char * h5filename)
{
hid_t fcreate_prop_list;
int errorcode;
cbf_failnez(cbf_make_h5handle(h5handle));
cbf_h5onfailneg(fcreate_prop_list = H5Pcreate(H5P_FILE_ACCESS),
CBF_ALLOC,cbf_free((void**) h5handle, NULL));
(*h5handle)->rwmode = 1;
cbf_h5onfailneg(H5Pset_fclose_degree(fcreate_prop_list,H5F_CLOSE_STRONG),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg((*h5handle)->hfile = H5Fcreate(h5filename,H5F_ACC_TRUNC,
H5P_DEFAULT,fcreate_prop_list),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg(H5Pclose(fcreate_prop_list),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_onfailnez(cbf_H5Gcreate_in_handle(*h5handle,"CBF_cbf",
&((*h5handle)->rootid)),
{cbf_free_h5handle(*h5handle);*h5handle=NULL;});
if ((*h5handle)->flags&CBF_H5_NXPDB) {
errorcode = 0;
cbf_failnez(cbf_apply_h5text_attribute((*h5handle)->rootid,
"NX_class","NXpdb",errorcode)||errorcode);
cbf_failnez(cbf_apply_h5text_attribute((*h5handle)->rootid,
"NXpdb_class", "CBF_cbf",0));
} else {
errorcode = 0;
cbf_failnez(cbf_apply_h5text_attribute((*h5handle)->rootid,
"NX_class","CBF_cbf",errorcode)||errorcode);
}
cbf_failnez(cbf_require_h5handle_filename(*h5handle));
return CBF_SUCCESS;
}
/* Create an HDF5 File handle */
int cbf_create_h5handle_nxpdb(cbf_h5handle *h5handle,const char * h5filename)
{
hid_t fcreate_prop_list;
cbf_failnez(cbf_make_h5handle(h5handle));
cbf_h5onfailneg(fcreate_prop_list = H5Pcreate(H5P_FILE_ACCESS),
CBF_ALLOC,cbf_free((void**) h5handle, NULL));
(*h5handle)->rwmode = 1;
cbf_h5onfailneg(H5Pset_fclose_degree(fcreate_prop_list,H5F_CLOSE_STRONG),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg((*h5handle)->hfile = H5Fcreate(h5filename,H5F_ACC_TRUNC,
H5P_DEFAULT,fcreate_prop_list),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg(H5Pclose(fcreate_prop_list),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_onfailnez(cbf_H5Gcreate_in_handle(*h5handle,"CBF_cbf",
&((*h5handle)->rootid)),
{cbf_free_h5handle(*h5handle);*h5handle=NULL;});
cbf_failnez(cbf_apply_h5text_attribute((*h5handle)->rootid,"NX_class",
"NXpdb",0));
cbf_failnez(cbf_require_h5handle_filename(*h5handle));
return CBF_SUCCESS;
}
int cbf_require_h5handle_filename(cbf_h5handle h5handle) {
char * filename;
ssize_t filesize;
if (!h5handle || !cbf_H5Ivalid(h5handle->hfile)) return CBF_ARGUMENT;
filesize = H5Fget_name(h5handle->hfile,NULL,0);
if (filesize <= 0) return CBF_NOTFOUND;
cbf_failnez(cbf_alloc(((void **) &filename),NULL,
1,filesize+1));
if (H5Fget_name(h5handle->hfile,filename,filesize+1)==filesize) {
h5handle -> nxfilename = realpath(filename,NULL);
cbf_failnez(cbf_free(((void **) &filename),NULL));
cbf_debug_print2("h5 filename '%s'\n",h5handle -> nxfilename);
return CBF_SUCCESS;
}
return CBF_H5ERROR;
}
/* Create an HDF5 File handle without adding an CBF_cbf group to it */
int cbf_create_h5handle2(cbf_h5handle *h5handle,const char * h5filename)
{
hid_t fcreate_prop_list;
char verstring[] = CBF_VERS_STRING;
char svnrev[] = CBF_SVN_REVISION_STRING;
char svndate[] = CBF_SVN_DATE_STRING;
char buffer[140];
int ii, irev, idate;
int error = CBF_SUCCESS;
cbf_failnez(cbf_make_h5handle(h5handle));
cbf_h5onfailneg(fcreate_prop_list = H5Pcreate(H5P_FILE_ACCESS),
CBF_ALLOC,cbf_free((void**) h5handle, NULL));
(*h5handle)->rwmode = 1;
cbf_h5onfailneg(H5Pset_fclose_degree(fcreate_prop_list,H5F_CLOSE_STRONG),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg((*h5handle)->hfile = H5Fcreate(h5filename,H5F_ACC_TRUNC,
H5P_DEFAULT,fcreate_prop_list),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg(H5Pclose(fcreate_prop_list), CBF_ARGUMENT,
cbf_free((void**) h5handle, NULL));
CBF_CALL(cbf_H5Arequire_string((*h5handle)->hfile,"NX_class","NXroot"));
CBF_CALL(cbf_H5Arequire_string((*h5handle)->hfile,"creator","CBFlib"));
buffer[0] = '\0';
strncat(buffer,verstring,50);
for (ii=_cbf_strlen(svnrev)-1; ii >= 0; ii--) {
if (svnrev[ii] == '$' || svnrev[ii] == ' ') {
svnrev[ii] = '\0';
} else break;
}
irev = 0;
if (_cbf_strlen(svnrev)>0 && svnrev[0]=='$') irev++;
for (ii=0; ii < (ssize_t)_cbf_strlen(svnrev); ii++) {
if (svnrev[ii]==':') {
irev = ii+1;
if (ii < (ssize_t)_cbf_strlen(svnrev)-1 && svnrev[ii+1]==' ') irev++;
break;
}
}
for (ii=_cbf_strlen(svndate)-1; ii >= 0; ii--) {
if (svndate[ii] == '$' || svndate[ii] == ' ') {
svndate[ii] = '\0';
} else break;
}
idate = 0;
if (_cbf_strlen(svndate)>0 && svndate[0]=='$') idate++;
for (ii=0; ii < (ssize_t)_cbf_strlen(svndate); ii++) {
if (svndate[ii]==':') {
idate = ii+1;
if (ii < (ssize_t)_cbf_strlen(svndate)-1 && svndate[ii]==' ') idate++;
break;
}
}
strncat(buffer," (r",3);
strncat(buffer,svnrev+irev,10);
strncat(buffer,") ",2);
strncat(buffer,svndate+idate,50);
CBF_CALL(cbf_H5Arequire_string((*h5handle)->hfile,"creator_version",
buffer));
cbf_failnez(cbf_require_h5handle_filename(*h5handle));
return error;
}
/* Create an HDF5 File handle without adding a CBF_cbf group to it
in update mode*/
int cbf_create_h5handle2u(cbf_h5handle *h5handle,const char * h5filename)
{
hid_t fcreate_prop_list;
char verstring[] = CBF_VERS_STRING;
char svnrev[] = CBF_SVN_REVISION_STRING;
char svndate[] = CBF_SVN_DATE_STRING;
char buffer[140];
int ii, irev, idate;
int error = CBF_SUCCESS;
cbf_failnez(cbf_make_h5handle(h5handle));
cbf_h5onfailneg(fcreate_prop_list = H5Pcreate(H5P_FILE_ACCESS),
CBF_ALLOC,cbf_free((void**) h5handle, NULL));
(*h5handle)->rwmode = 1;
cbf_h5onfailneg(H5Pset_fclose_degree(fcreate_prop_list,H5F_CLOSE_STRONG),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg((*h5handle)->hfile = H5Fopen(h5filename, H5F_ACC_RDWR,
fcreate_prop_list),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg(H5Pclose(fcreate_prop_list), CBF_ARGUMENT,
cbf_free((void**) h5handle, NULL));
CBF_CALL(cbf_H5Arequire_string((*h5handle)->hfile,"NX_class","NXroot"));
CBF_CALL(cbf_H5Arequire_string((*h5handle)->hfile,"creator","CBFlib"));
buffer[0] = '\0';
strncat(buffer,verstring,50);
for (ii=_cbf_strlen(svnrev)-1; ii >= 0; ii--) {
if (svnrev[ii] == '$' || svnrev[ii] == ' ') {
svnrev[ii] = '\0';
} else break;
}
irev = 0;
if (_cbf_strlen(svnrev)>0 && svnrev[0]=='$') irev++;
for (ii=0; ii < (ssize_t)_cbf_strlen(svnrev); ii++) {
if (svnrev[ii]==':') {
irev = ii+1;
if (ii < (ssize_t)_cbf_strlen(svnrev)-1 && svnrev[ii+1]==' ') irev++;
break;
}
}
for (ii=_cbf_strlen(svndate)-1; ii >= 0; ii--) {
if (svndate[ii] == '$' || svndate[ii] == ' ') {
svndate[ii] = '\0';
} else break;
}
idate = 0;
if (_cbf_strlen(svndate)>0 && svndate[0]=='$') idate++;
for (ii=0; ii < (ssize_t)_cbf_strlen(svndate); ii++) {
if (svndate[ii]==':') {
idate = ii+1;
if (ii < (ssize_t)_cbf_strlen(svndate)-1 && svndate[ii]==' ') idate++;
break;
}
}
strncat(buffer," (r",3);
strncat(buffer,svnrev+irev,10);
strncat(buffer,") ",2);
strncat(buffer,svndate+idate,50);
CBF_CALL(cbf_H5Arequire_string((*h5handle)->hfile,"creator_version",
buffer));
cbf_failnez(cbf_require_h5handle_filename(*h5handle));
return error;
}
/**
This function expects the user to create or open a hdf5 file with the appropriate parameters for what they are
trying to do, replacing older functions which would create a file with the <code>H5F_ACC_TRUNC</code> flag and
<code>H5F_CLOSE_STRONG</code> property.
\param handle A pointer to a handle which is to be allocated.
\param file A HDF5 file to store within the newly created handle.
\sa cbf_free_h5handle
\return An error code
*/
int cbf_create_h5handle3
(cbf_h5handle * handle,
hid_t file)
{
int error = CBF_SUCCESS;
if (!handle) {
error |= CBF_ARGUMENT;
} else {
CBF_CALL(cbf_make_h5handle(handle));
CBF_CALL(cbf_h5handle_set_file(*handle,file));
}
cbf_failnez(cbf_require_h5handle_filename(*handle));
return error;
}
/* Write cbf to HDF5 file hfile
Should check the type of CBF file we have (miniCBF [+header convention, eventually...] vs full CBF) and call the appropriate function.
*/
int cbf_write_h5file (cbf_handle handle, cbf_h5handle h5handle,unsigned long int flags)
{
cbf_node *node = NULL;
int errorcode = CBF_SUCCESS;
if (!handle || !h5handle)
return CBF_ARGUMENT;
/* Transfer the flags into h5handle */
h5handle->flags = flags;
/* Find the root node */
cbf_failnez (cbf_find_parent (&node, handle->node, CBF_ROOT));
/* Reset the reference counts */
cbf_failnez( cbf_reset_refcounts(handle->dictionary) );
/* ensure the handle contains some basic structure */
cbf_reportnez(cbf_h5handle_require_entry(h5handle,0,0), errorcode);
cbf_reportnez(cbf_h5handle_require_instrument(h5handle,0,0), errorcode);
/* Do the mappings from CBF to nexus */
/* Write the CBF data into the file in a special node to keep
it separate from actual mappings */
errorcode = cbf_write_h5node (handle, node, h5handle);
if (!errorcode) {
cbf_write_h5nxaxes(handle, h5handle);
}
return errorcode;
}
/*
cbf_write_nx2cbf__<NXclass>
Write the nexus class, recursively, from the given handle into a cbf handle
Need to record which tables have been created already, and provide methods to create
any new tables I need. If a table has been created it will have a node in the CBF
tree, I can store either this node or NULL to find out if it exists. I always know
which table I want to create, so can call the appropriate function without needing
a run-time lookup from a list of creation functions.
I need a list of table nodes and a list of shared keys to make this work. Like the
'cbf_nx2cbf_key_t' this can be removed and replaced with function-specific parameters once
they are known.
*/
#define CBF_AXIS_DATA_PGA 1 /* Flag bit for primary goniometer axis */
#define CBF_AXIS 2 /* Flag bit for primary name */
/*
Allocate and initialise a new 'cbf_axisData_t' object.
*/
static int _cbf_create_axisData_t
(cbf_axisData_t * * const axisData)
{
int error = CBF_SUCCESS;
if (!axisData) {
error |= CBF_ARGUMENT;
} else if (cbf_alloc((void **)axisData,NULL,sizeof(cbf_axisData_t),1)
|| !*axisData) {
error |= CBF_ALLOC;
} else {
cbf_axisData_t * const data = *axisData;
data->flags = 0;
data->name = NULL;
data->axis = CBF_H5FAIL;
data->equipment = axisEquipment_general;
data->flags = 0;
data->depends_on = NULL;
data->rotation_axis = NULL;
data->axis_hash_next_name = NULL;
data->axis_hash_next_path = NULL;
data->axis_path = NULL;
data->do_path = NULL;
data->ra_path = NULL;
data->axisData_index = -1;
}
return error;
}
/*
Free 'cbf_axisData_t' object and all associated memory.
*/
static int _cbf_free_axisData_t
(cbf_axisData_t * const axisData)
{
int error = CBF_SUCCESS;
if (!axisData) {
error |= CBF_ARGUMENT;
} else {
if (axisData->name) {
free((void*)axisData->name);
axisData->name = NULL;
}
if (cbf_H5Ivalid(axisData->axis)) {
cbf_H5Dfree(axisData->axis);
axisData->axis = CBF_H5FAIL;
}
if (axisData->axis_path) {
free((void*)axisData->axis_path);
axisData->axis_path = NULL;
}
if (axisData->do_path) {
free((void*)axisData->do_path);
axisData->do_path = NULL;
}
if (axisData->ra_path) {
free((void*)axisData->ra_path);
axisData->ra_path = NULL;
}
free((void*)axisData);
}
return error;
}
static int _cbf_create_nx2cbf_key
(cbf_nx2cbf_key_t * const key)
{
int error = CBF_SUCCESS;
int ii;
if (!key) {
error |= CBF_ARGUMENT;
} else {
/* keys */
key->array_id = NULL;
key->binary_id = 0;
key->datablock_id = NULL;
key->diffrn_id = NULL;
key->diffrn_detector_id = NULL;
key->diffrn_detector_element_id = NULL;
key->diffrn_measurement_id = NULL;
key->frame_id = NULL;
key->scan_id = NULL;
key->wavelength_id = NULL;
/* tables */
key->array_data = NULL;
key->array_element_size = NULL;
key->array_intensities = NULL;
key->diffrn = NULL;
key->diffrn_data_frame = NULL;
key->diffrn_detector = NULL;
key->diffrn_detector_element = NULL;
key->diffrn_measurement = NULL;
key->diffrn_radiation = NULL;
key->diffrn_radiation_wavelength = NULL;
key->diffrn_scan = NULL;
key->diffrn_scan_frame = NULL;
key->diffrn_source = NULL;
/* other */
key->frames = 0;
key->xdim = 0;
key->ydim = 0;
key->zdim = 0;
key->axisData = NULL;
for (ii=0; ii < CBF_NX2CBF_HASH_BINS; ii++) {
(key->axispathhash)[ii] = (key->axisnamehash)[ii] = (key->axisobjecthash)[ii] = NULL;
}
key->nAxes = 0;
key->has_scaling_factor = 0;
key->has_offset = 0;
key->indent = 0;
}
return error;
}
static int _cbf_free_nx2cbf_key
(cbf_nx2cbf_key_t * const key)
{
int error = CBF_SUCCESS;
if (!key) {
error |= CBF_ARGUMENT;
} else {
cbf_axisData_t * const * it = key->axisData;
cbf_axisData_t * const * const end = key->axisData+key->nAxes;
for (; end != it; ++it) {
error |= _cbf_free_axisData_t(*it);
}
free((void*)key->axisData);
}
return error;
}
static int _cbf_compute_path_hash(const char *path, unsigned int *hashcode) {
size_t len, ii;
if (!path || !hashcode) return CBF_ARGUMENT;
len = _cbf_strlen(path);
if (len == 0) {
*hashcode = 0;
return CBF_SUCCESS;
}
for (ii = len; ii > 0; ii--) {
if (path[ii-1] == '/' && path[ii-1] == '\\') break;
}
return cbf_compute_hashcode(path+ii, hashcode);
}
static int _cbf_nx2cbf_key_require_axis
(cbf_nx2cbf_key_t * const key, /* key object to add the axis too */
cbf_axisData_t * * const newAxisData, /* place to store a pointer to the new or existing axis data */
const hid_t axis, /* the HDF5 dataset where the axis is stored */
const char * const name, /* a name for the axis */
cbf_axisEquipment_e equipment /* CBF's 'axis.equipment' data item */)
{
int error = CBF_SUCCESS;
if (!key || !cbf_H5Ivalid(axis) || !name) {
error |= CBF_ARGUMENT;
} else {
size_t iaxis;
size_t jaxis;
H5O_info_t iinfo;
cbf_axisData_t * nextaxisobj;
/* search the list of axes for the dataset */
cbf_h5reportneg(H5Oget_info( axis, &iinfo ),CBF_H5ERROR,error);
nextaxisobj = key->axisobjecthash[iinfo.addr&(CBF_NX2CBF_HASH_BINS-1)];
while (nextaxisobj != NULL) {
const htri_t cmp = cbf_H5Ocmp(nextaxisobj->axis,axis);
if (cmp < 0) error |= CBF_H5ERROR;
else if (!cmp) break;
nextaxisobj = nextaxisobj->axis_hash_next_object;
}
for (iaxis = 0; iaxis < key->nAxes; iaxis++) {
const htri_t cmp = cbf_H5Ocmp(key->axisData[iaxis]->axis,axis);
if (cmp < 0) error |= CBF_H5ERROR;
else if (!cmp) break;
}
/* if not found add it */
if (!nextaxisobj) {
const size_t nAxes = key->nAxes;
const char * depends_on_path = NULL;
const char * rotation_axis_path = NULL;
hid_t depends_on = CBF_H5FAIL;
hid_t rotation_axis = CBF_H5FAIL;
hid_t long_name = CBF_H5FAIL;
unsigned int name_hash = 0;
unsigned int path_hash = 0;
cbf_axisData_t * * const axisData = realloc(key->axisData,sizeof(cbf_axisData_t*)*(1+key->nAxes));
if (!axisData) {
error |= CBF_ALLOC;
} else {
++key->nAxes;
key->axisData = axisData;
if (CBF_SUCCESS!=(error|=_cbf_create_axisData_t(axisData+nAxes))) {
cbf_debug_print(cbf_strerror(error));
} else {
cbf_axisData_t * const data = axisData[(key->nAxes)-1];
*newAxisData = data;
/* compute the path and link it to the hash table */
cbf_reportnez(cbf_get_nx_object_path(axis,NULL,(char **)&(data->axis_path)),error);
cbf_reportnez(_cbf_compute_path_hash(data->axis_path, &path_hash),error);
path_hash &= (CBF_NX2CBF_HASH_BINS-1);
data->axis_hash_next_path = key->axispathhash[path_hash];
key->axispathhash[path_hash] = data;
data->axis_hash_next_object = key->axisobjecthash[iinfo.addr&(CBF_NX2CBF_HASH_BINS-1)];
key->axisobjecthash[iinfo.addr&(CBF_NX2CBF_HASH_BINS-1)] = data;
depends_on_path = NULL;
rotation_axis_path = NULL;
data->axis = axis;
/* compute the name and link it to the hash table */
long_name = CBF_H5FAIL;
if (cbf_H5Afind(axis,&long_name,"long_name",CBF_H5FAIL,CBF_H5FAIL)
|| cbf_H5Aread_string(long_name,&(data->name))) {
data->name = _cbf_strdup(name);
}
if (cbf_H5Ivalid(long_name))cbf_H5Afree(long_name);
cbf_reportnez(_cbf_compute_path_hash(data->name, &name_hash),error);
name_hash &= (CBF_NX2CBF_HASH_BINS-1);
data->axis_hash_next_name = key->axisnamehash[name_hash];
key->axisnamehash[name_hash] = data;
data->axisData_index = nAxes;
data->equipment = equipment;
depends_on = CBF_H5FAIL;
if (cbf_H5Afind(axis,&depends_on,"depends_on",CBF_H5FAIL,CBF_H5FAIL)
|| cbf_H5Aread_string(depends_on,&depends_on_path)) depends_on_path = NULL;
data->do_path = depends_on_path;
if (cbf_H5Ivalid(depends_on))cbf_H5Afree(depends_on);
if (cbf_H5Afind(axis,&rotation_axis,"rotation_axis",CBF_H5FAIL,CBF_H5FAIL)
|| cbf_H5Aread_string(rotation_axis,&rotation_axis_path)) rotation_axis_path = NULL;
data->ra_path = rotation_axis_path;
if (cbf_H5Ivalid(rotation_axis))cbf_H5Afree(rotation_axis);
/* See if any prior axis depends on this one or has this one
as its rotation axis and check id this one uses one of them
for its rotation axis or dependency ********/
for (jaxis = 0; jaxis < key->nAxes; jaxis++) {
if (axisData[jaxis]->do_path
&& !cbf_cistrcmp(axisData[jaxis]->do_path,data->axis_path)
&& axisData[jaxis]->depends_on == NULL) axisData[jaxis]->depends_on = data;
if (axisData[jaxis]->ra_path
&& !cbf_cistrcmp(axisData[jaxis]->ra_path,data->axis_path)
&& axisData[jaxis]->rotation_axis == NULL) axisData[jaxis]->rotation_axis = data;
}
}
}
} else {
*newAxisData = nextaxisobj;
}
}
return error;
}
/*
Declare a bunch of table manipulation functions, because they call each other to ensure everything is defined.
TODO: Update these to return successfully if a row with the given keys already exists,
to prevent problems where duplicate rows could be created.
*/
static int _cbf_nx2cbf_table__array_data
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__array_intensities
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_data_frame
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_detector
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_detector_element
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_measurement
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_radiation
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_radiation_wavelength
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_scan
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_scan_frame
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__diffrn_source
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table);
static int _cbf_nx2cbf_table__array_data
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
CBF_UNUSED( nx );
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->array_id || !table->binary_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->array_data;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"array_data"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"array_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->array_id));
CBF_CALL(cbf_require_column(cbf,"binary_id"));
CBF_CALL(cbf_set_integervalue(cbf,table->binary_id));
/* ensure foreign keys are well-defined */
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__array_intensities
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
CBF_UNUSED( nx );
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->array_id || !table->binary_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->array_intensities;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"array_intensities"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"array_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->array_id));
CBF_CALL(cbf_require_column(cbf,"binary_id"));
CBF_CALL(cbf_set_integervalue(cbf,table->binary_id));
/* ensure foreign keys are well-defined */
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
CBF_UNUSED( nx );
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->diffrn_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->diffrn_id));
/* ensure foreign keys are well-defined */
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_data_frame
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->diffrn_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_data_frame;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_data_frame"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->frame_id));
CBF_CALL(cbf_require_column(cbf,"array_id"));
CBF_CALL(cbf_set_value(cbf,table->array_id));
CBF_CALL(cbf_require_column(cbf,"binary_id"));
CBF_CALL(cbf_set_integervalue(cbf,table->binary_id));
CBF_CALL(cbf_require_column(cbf,"detector_element_id"));
CBF_CALL(cbf_set_value(cbf,table->diffrn_detector_element_id));
/* ensure foreign keys are well-defined */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector_element(cbf,nx,table));
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_detector
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->diffrn_id || !table->diffrn_detector_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_detector;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_detector"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"diffrn_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->diffrn_id));
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_set_value(cbf,table->diffrn_detector_id));
/* ensure foreign keys are well-defined */
CBF_CALL(_cbf_nx2cbf_table__diffrn(cbf,nx,table));
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_detector_element
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->diffrn_detector_id || !table->diffrn_detector_element_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_detector_element;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_detector_element"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->diffrn_detector_element_id));
CBF_CALL(cbf_require_column(cbf,"detector_id"));
CBF_CALL(cbf_set_value(cbf,table->diffrn_detector_id));
/* ensure foreign keys are well-defined */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_measurement
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->diffrn_id || !table->diffrn_measurement_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_measurement;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_measurement"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"diffrn_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->diffrn_id));
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_set_value(cbf,table->diffrn_measurement_id));
/* ensure foreign keys are well-defined */
CBF_CALL(_cbf_nx2cbf_table__diffrn(cbf,nx,table));
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_radiation
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->diffrn_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_radiation;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_radiation"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"diffrn_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->diffrn_id));
/* ensure foreign keys are well-defined */
CBF_CALL(_cbf_nx2cbf_table__diffrn(cbf,nx,table));
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_radiation_wavelength
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->diffrn_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_radiation_wavelength;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_radiation_wavelength"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->wavelength_id));
/* ensure foreign keys are well-defined */
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"wavelength_id"));
CBF_CALL(cbf_set_value(cbf,table->wavelength_id));
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_scan
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
CBF_UNUSED( nx );
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->scan_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_scan;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_scan"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->scan_id));
/* ensure foreign keys are well-defined */
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_scan_frame
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!nx) {
cbf_debug_print("invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->datablock_id || !table->frame_id || !table->scan_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_scan_frame;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_scan_frame"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"frame_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->frame_id));
CBF_CALL(cbf_require_column(cbf,"scan_id"));
CBF_CALL(cbf_set_value(cbf,table->scan_id));
CBF_CALL(cbf_require_column(cbf,"frame_number"));
CBF_CALL(cbf_set_integervalue(cbf,1+nx->slice));
/* ensure foreign keys are well-defined */
CBF_CALL(_cbf_nx2cbf_table__diffrn_scan(cbf,nx,table));
CBF_CALL(_cbf_nx2cbf_table__diffrn_data_frame(cbf,nx,table));
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
static int _cbf_nx2cbf_table__diffrn_source
(const cbf_handle cbf,
const cbf_h5handle nx,
cbf_nx2cbf_key_t * const table)
{
int error = CBF_SUCCESS;
/* check arguments */
if (!cbf) {
cbf_debug_print("invalid CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table || !table->diffrn_id) {
cbf_debug_print("invalid table list given");
error |= CBF_ARGUMENT;
} else {
cbf_node * * const node = &table->diffrn_source;
if (!*node) {
/* create the table & store the node */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_source"));
*node = cbf->node;
/* populate the table with all the required keys */
CBF_CALL(cbf_require_column(cbf,"diffrn_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->diffrn_id));
/* ensure foreign keys are well-defined */
CBF_CALL(_cbf_nx2cbf_table__diffrn(cbf,nx,table));
}
/* set the handle to a sensible state */
cbf->node = *node;
}
return error;
}
/*
Function to read a scalar string from a dataset
*/
static int _cbf_nx2cbfDread_scalar_string
(const hid_t data,
const char * * const value)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(data) || !value) error |= CBF_ARGUMENT;
if (CBF_SUCCESS==error) {
hid_t data_type = CBF_H5FAIL;
H5T_class_t data_class = H5T_NO_CLASS;
if ((data_type=H5Dget_type(data))<0) {
cbf_debug_print("Couldn't get type of dataset");
error |= CBF_H5ERROR;
} else if (H5T_NO_CLASS==(data_class=H5Tget_class(data_type))) {
cbf_debug_print("Couldn't get class of datatype");
error |= CBF_H5ERROR;
} else if (H5T_STRING!=data_class) {
cbf_debug_print("Wrong class of datatype");
error |= CBF_H5DIFFERENT;
} else {
const htri_t vlstr = H5Tis_variable_str(data_type);
char * * lvalue = NULL;
int freelvalue = 0;
if (vlstr<0) {
cbf_debug_print("Couldn't check for a variable-length string");
error |= CBF_H5ERROR;
} else if (vlstr) {
/* I have a variable-length string */
if (!(lvalue=(char * *)malloc(sizeof(char *)))) {
cbf_debug_print(cbf_strerror(CBF_ALLOC));
error |= CBF_ALLOC;
} else if (H5Dread(data,data_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,lvalue)<0) {
cbf_debug_print("Couldn't read string");
error |= CBF_H5ERROR;
}
freelvalue = 1;
} else {
/* I have a fixed-length string */
const size_t len = H5Tget_size(data_type);
char * llvalue = NULL;
if (!len) {
cbf_debug_print("Couldn't get length of string");
error |= CBF_H5ERROR;
} else if (!(llvalue=(char *)malloc(len+1))) {
cbf_debug_print(cbf_strerror(CBF_ALLOC));
error |= CBF_ALLOC;
} else if (H5Dread(data,data_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,llvalue)<0) {
cbf_debug_print("Couldn't read string");
error |= CBF_H5ERROR;
}
if (CBF_SUCCESS==error) llvalue[len] = '\0';
lvalue = &llvalue;
}
if (CBF_SUCCESS==error) {
*value = _cbf_strdup(*lvalue);
if (!freelvalue) free(*lvalue);
}
if (freelvalue && lvalue) free(lvalue);
}
cbf_H5Tfree(data_type);
}
return error;
}
typedef struct op_data_t
{
cbf_h5handle nx;
cbf_handle cbf;
cbf_nx2cbf_key_t * table;
} op_data_t;
static void _cbf_write_name
(FILE * const out,
const char * const name,
const char * const class,
unsigned int indent,
const unsigned int processed)
{
while (indent--) fputc('\t',out);
if (!class) {
if (!processed) fprintf(out,"✗ %s\n",name);
else if (1==processed) fprintf(out,"✔ %s\n",name);
else fprintf(out,"- %s\n",name);
} else {
if (!processed) fprintf(out,"✗ %s:%s\n",name,class);
else if (1==processed) fprintf(out,"✔ %s:%s\n",name,class);
else fprintf(out,"- %s:%s\n",name,class);
}
}
static int cbf_write_nx2cbf__detector_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
cbf_debug_print("Entering cbf_write_nx2cbf__detector_op");
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
cbf_debug_print2("cbf_write_nx2cbf__detector_op dataset '%s'",name);
if (!strcmp(name,"beam_center_x")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
double value = 0., factor = 0./0.;
const int rank = H5Sget_simple_extent_ndims(data_space);
if (0==rank) {
/* read the value */
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
} else if (1==rank) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mm",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector_element(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"center[1]"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"beam_center_y")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
double value = 0., factor = 0./0.;
const int rank = H5Sget_simple_extent_ndims(data_space);
if (0==rank) {
/* read the value */
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
} else if (1==rank) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mm",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector_element(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"center[2]"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"CBF_diffrn_data_frame__details")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
const char * value = NULL;
const int rank = H5Sget_simple_extent_ndims(data_space);
/* check rank, allowing for multiple usable results */
if (0==rank) {
/* read the value */
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
} else if (1==rank) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
const char * lvalue;
hid_t vlstr = CBF_H5FAIL;
const hid_t currType = H5Dget_type(object);
hsize_t offset[1];
hsize_t count[] = {1};
offset[0] = dim[0]>1 ? nx->slice : 0;
CBF_CALL(cbf_H5Tcreate_string(&vlstr,H5T_VARIABLE));
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&lvalue,vlstr));
value = _cbf_strdup(lvalue);
H5Dvlen_reclaim(currType, data_space, H5P_DEFAULT, &lvalue);
cbf_H5Tfree(vlstr);
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
/* convert the data to the correct units: no-op */
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_data_frame(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"details"));
/* write the data */
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"count_time")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
double value = 0., factor = 0./0.;
offset[0] = dim[0]>1 ? nx->slice : 0;
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"s",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_scan_frame(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"integration_time"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"data")) {
int indent = table->indent;
if (nx->logfile) {
while (indent--) fputc('\t',nx->logfile);
fprintf(nx->logfile,"- %s\n",name);
}
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"dead_time")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
double value = 0., factor = 0./0.;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"us",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"dtime"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"description")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
if (CBF_SUCCESS==error) {
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"type"));
/* write the data */
CBF_CALL(cbf_set_value(cbf,value));
}
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"details")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
if (CBF_SUCCESS==error) {
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"details"));
/* write the data */
CBF_CALL(cbf_set_value(cbf,value));
}
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"depends_on")) {
hid_t data_space = CBF_H5FAIL;
hid_t do_base_id = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * path = NULL;
cbf_axisData_t * prevAxisPtr = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&path));
while (CBF_SUCCESS==error && path && strcmp(path,".")) {
/*
Given the path to an axis dataset, I want to extract
all relevant details and write them out to the CBF file.
*/
hid_t axis = CBF_H5FAIL;
do_base_id = g_id;
if (*path == '/') do_base_id = nx->hfile;
if (!cbf_H5Ivalid(axis=H5Dopen2(do_base_id, path, H5P_DEFAULT))) {
cbf_debug_print("couldn't open dataset");
error |= CBF_H5ERROR;
} else {
const char * _path = NULL;
hid_t depends_on = CBF_H5FAIL;
cbf_axisData_t * axisPtr = NULL;
const char * const chr = strrchr(path,'/');
const char * const name = NULL==chr ? path : 1+chr;
CBF_CALL(_cbf_nx2cbf_key_require_axis(table,&axisPtr,axis,name,axisEquipment_detector));
CBF_CALL(cbf_H5Afind(axis,&depends_on,"depends_on",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(depends_on,&_path));
if (CBF_SUCCESS==error) {
/* set dependency of previous axis */
if (prevAxisPtr) prevAxisPtr->depends_on = axisPtr;
/* some axes are shared with other pieces of equipment, set the type appropriately */
if (axisEquipment_image==axisPtr->equipment) axisPtr->equipment = axisEquipment_detector;
if (axisEquipment_detector!=axisPtr->equipment) axisPtr->equipment = axisEquipment_general;
} else {
cbf_H5Dfree(axis);
}
/* housekeeping: update the previous axis and path */
prevAxisPtr = axisPtr;
free((void*)path);
path = _path;
/* ensure hdf5 stuff is closed */
cbf_H5Afree(depends_on);
}
}
free((void*)path);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"distance")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
double value = 0., factor = 0./0.;
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mm",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_measurement(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"sample_detector_distance"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"frame_start_time")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hid_t vlstr = CBF_H5FAIL;
hsize_t offset[1];
hsize_t count[] = {1};
const hid_t currType = H5Dget_type(object);
char * * value = (char * *)malloc(dim[0]*sizeof(char *));
offset[0] = nx->slice;
CBF_CALL(cbf_H5Tcreate_string(&vlstr,H5T_VARIABLE));
CBF_CALL(cbf_H5Dread2(object,offset,0,count,(void * const)value,vlstr));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_scan_frame(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"date"));
/* write the data */
if (value[0]) {
CBF_CALL(cbf_set_value(cbf,value[0]));
}
H5Dvlen_reclaim(currType, data_space, H5P_DEFAULT, value);
if (value) free((void*)value);
cbf_H5Tfree(vlstr);
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"frame_time")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
double value = 0., factor = 0./0.;
offset[0] = dim[0]>1 ? nx->slice : 0;
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"s",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_scan_frame(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"time_period"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"gain_setting")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"gain_setting"));
/* write the data */
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"offset")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
double value = 0;
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__array_intensities(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"offset"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value));
if (CBF_SUCCESS==error) table->has_offset = 1;
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"saturation_value")||!strcmp(name,"overload")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
long value = 0;
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_LONG));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__array_intensities(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"overload"));
/* write the data */
CBF_CALL(cbf_set_integervalue(cbf,value));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"scaling_factor")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
double value = 0;
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__array_intensities(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"scaling"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value));
if (CBF_SUCCESS==error) table->has_scaling_factor = 1;
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"sensor_material")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"sensor_material"));
/* write the data */
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"sensor_thickness")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
double value = 0., factor = 0./0.;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mm",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"layer_thickness"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"threshold_energy")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
double value = 0., factor = 0./0.;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"eV",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"threshold"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"type")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_detector(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"detector"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"undefined_value")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
long value = 0;
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_LONG));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__array_intensities(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"undefined_value"));
/* write the data */
CBF_CALL(cbf_set_integervalue(cbf,value));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"x_pixel_offset")
|| !strcmp(name,"y_pixel_offset")
|| !strcmp(name,"z_pixel_offset")
|| !strcmp(name,"x_pixel_size")
|| !strcmp(name,"y_pixel_size")
|| !strcmp(name,"z_pixel_size")) {
int indent = table->indent;
if (nx->logfile) {
while (indent--) fputc('\t',nx->logfile);
fprintf(nx->logfile,"- %s\n",name);
}
/*--------------------------------------------------------------------------------------*/
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here */
const char * NX_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
cbf_debug_print2("cbf_write_nx2cbf__detector_op group '%s'",name);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
if (!strcmp(NX_class,"NXtransformations")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXpoise")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXcollection")) {
/* known class that should be ignored: ignoring it is the correct way to process it */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
cbf_debug_print2("cbf_write_nx2cbf__detector_op unrecognized '%s'",name);
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
static int cbf_write_nx2cbf__monochromator_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
if (!strcmp(name,"description")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"monochromator"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here */
const char * NX_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
if (!strcmp(NX_class,"NXcollection")) {
/* known class that should be ignored: ignoring it is the correct way to process it */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
int _cbf_write_nx2cbf__beam_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data);
static int cbf_write_nx2cbf__source_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
if (!strcmp(name,"current")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
hid_t units = CBF_H5FAIL;
double value = 0., factor = 0./0.;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mA",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
/* ensure I have suitable structure within the CBF file & write the data */
CBF_CALL(_cbf_nx2cbf_table__diffrn_source(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"current"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"name")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_source(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"type"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"power")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
hid_t units = CBF_H5FAIL;
double value = 0., factor = 0./0.;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"kW",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
/* ensure I have suitable structure within the CBF file & write the data */
CBF_CALL(_cbf_nx2cbf_table__diffrn_source(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"power"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"probe")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"probe"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"target_material")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_source(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"target"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"type")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_source(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"source"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"voltage")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
hid_t units = CBF_H5FAIL;
double value = 0., factor = 0./0.;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"kV",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
/* ensure I have suitable structure within the CBF file & write the data */
CBF_CALL(_cbf_nx2cbf_table__diffrn_source(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"voltage"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here */
const char * NX_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
if (!strcmp(NX_class,"NXcollection")) {
/* known class that should be ignored: ignoring it is the correct way to process it */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(NX_class,"NXbeam")) {
const unsigned int indent = table->indent;
/* debugging output */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/* leave ownership of the group with the iteration function, and process it */
++table->indent;
if (H5Literate(object,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,_cbf_write_nx2cbf__beam_op,op_data)<0) {
cbf_debug_print3("error: failed to iterate over items in the '%s:%s' group\n",name,NX_class);
error |= CBF_H5ERROR;
}
table->indent = indent;
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
static int cbf_write_nx2cbf__goniometer_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
if (!strcmp(name,"description")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_measurement(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"device_details"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"details")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_measurement(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"details"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"local_name")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_measurement(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"device"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"type")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_measurement(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"device_type"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here */
const char * NX_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
if (!strcmp(NX_class,"NXtransformations")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXpoise")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXcollection")) {
/* known class that should be ignored: ignoring it is the correct way to process it */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
static int cbf_write_nx2cbf__instrument_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
if (0) {
/* I don't actually have any items to match here */
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here */
const char * NX_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
if (!strcmp(NX_class,"NXtransformations")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXpoise")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXcollection")) {
/* known class that should be ignored: ignoring it is the correct way to process it */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXdetector")) {
const unsigned int indent = table->indent;
hid_t group = object;
/* debugging output */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/* try to take ownership of the group away from the iteration function */
if (CBF_SUCCESS!=(error|=cbf_h5handle_set_detector(nx,object,name))) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: failed to set the '%s' group in the handle\n",name);
}
} else {
object = CBF_H5FAIL;
/* The hdf5 handle now owns the group, process it */
++table->indent;
if (H5Literate(group,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,cbf_write_nx2cbf__detector_op,op_data)<0) {
cbf_debug_print3("error: failed to iterate over items in the '%s:%s' group\n",name,NX_class);
error |= CBF_H5ERROR;
} else {
/* success: extract some data that should have been returned via the op_data argument */
if (!table->has_offset) {
CBF_CALL(_cbf_nx2cbf_table__array_intensities(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"offset"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",0.0));
}
if (!table->has_scaling_factor) {
CBF_CALL(_cbf_nx2cbf_table__array_intensities(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"scaling"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",1.0));
}
CBF_CALL(_cbf_nx2cbf_table__array_intensities(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"linearity"));
CBF_CALL(cbf_set_value(cbf,"scaling_offset"));
}
table->indent = indent;
}
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(NX_class,"NXgoniometer")) {
const unsigned int indent = table->indent;
hid_t group = object;
/* debugging output */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/* try to take ownership of the group away from the iteration function */
if (CBF_SUCCESS!=(error|=cbf_h5handle_set_goniometer(nx,object,name))) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: failed to set the '%s' group in the handle\n",name);
}
} else {
object = CBF_H5FAIL;
/* The hdf5 handle now owns the group, process it */
++table->indent;
if (H5Literate(group,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,cbf_write_nx2cbf__goniometer_op,op_data)<0) {
cbf_debug_print3("error: failed to iterate over items in the '%s:%s' group\n",name,NX_class);
error |= CBF_H5ERROR;
}
table->indent = indent;
}
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(NX_class,"NXmonochromator")) {
const unsigned int indent = table->indent;
hid_t group = object;
/* debugging output */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/* try to take ownership of the group away from the iteration function */
if (CBF_SUCCESS!=(error|=cbf_h5handle_set_monochromator(nx,object,name))) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: failed to set the '%s' group in the handle\n",name);
}
} else {
object = CBF_H5FAIL;
/* The hdf5 handle now owns the group, process it */
++table->indent;
if (H5Literate(group,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,cbf_write_nx2cbf__monochromator_op,op_data)<0) {
cbf_debug_print3("error: failed to iterate over items in the '%s:%s' group\n",name,NX_class);
error |= CBF_H5ERROR;
}
table->indent = indent;
}
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(NX_class,"NXsource")) {
const unsigned int indent = table->indent;
hid_t group = object;
/* debugging output */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/* try to take ownership of the group away from the iteration function */
if (CBF_SUCCESS!=(error|=cbf_h5handle_set_source(nx,object,name))) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: failed to set the '%s' group in the handle\n",name);
}
} else {
object = CBF_H5FAIL;
/* The hdf5 handle now owns the group, process it */
++table->indent;
if (H5Literate(group,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,cbf_write_nx2cbf__source_op,op_data)<0) {
cbf_debug_print3("error: failed to iterate over items in the '%s:%s' group\n",name,NX_class);
error |= CBF_H5ERROR;
}
table->indent = indent;
}
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
/* iteration in a second level NXpdb group that defines a cbfcat
should contain datasets, each of which defines a column in the
category, or possibly contains groups, each of which defines
a saveframe */
static int cbf_write_nx2cbf__cbfcat_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
cbf_debug_print("Entering cbf_write_nx2cbf__cbfcat_op");
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* each dataset is a column */
int colerr=0;
{ /* log the dataset */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
/* name is the required column */
colerr=cbf_require_column(cbf,name);
if (CBF_SUCCESS!=colerr) {
cbf_debug_print(cbf_strerror(colerr));
cbf_debug_print2("error: whilst processing column '%s'\n",name);
} else {
/* iterate through the column */
hsize_t slab;
hsize_t dsslab;
int ndims = 0;
hsize_t offset[1] = {0};
hsize_t stride[1] = {1};
hsize_t count[1] = {1};
hsize_t chunk[1] = {1};
hsize_t curdim[1];
hsize_t memsize[1] = {1};
htri_t dsexists;
hsize_t dssize[1];
hsize_t maxdssize[1];
hsize_t dsdims[1];
hsize_t dsmaxdims[1];
size_t old_size;
int slaberr;
slaberr=0;
slab = 0L;
const char * datasettext;
size_t rank;
hid_t datasetspace, datasettype;
hid_t memspace, memtype;
void * datasettextbuffer;
int errorcode=0;
hid_t datasetid=object;
CBF_UNUSED( rank );
CBF_UNUSED( datasettext );
CBF_UNUSED( slaberr );
CBF_UNUSED( dsmaxdims );
CBF_UNUSED( maxdssize );
CBF_UNUSED( dssize );
CBF_UNUSED( dsexists );
CBF_UNUSED( memsize );
CBF_UNUSED( curdim );
CBF_UNUSED( chunk );
CBF_UNUSED( slab );
datasetspace = (hid_t)-1;
datasettype = (hid_t)-1;
memspace = (hid_t)-1;
memtype = (hid_t)-1;
datasettextbuffer = NULL;
cbf_h5reportneg(datasettype = H5Dget_type(datasetid),CBF_FORMAT,errorcode);
cbf_h5reportneg(datasetspace = H5Dget_space(datasetid),CBF_FORMAT,errorcode);
old_size = H5Tget_size(datasettype);
cbf_reportnez(cbf_alloc(&datasettextbuffer,NULL,1,old_size+1),errorcode);
cbf_h5reportneg(ndims = H5Sget_simple_extent_ndims(datasetspace),CBF_FORMAT,errorcode);
if ( errorcode || ndims > 1 ) {
cbf_debug_print(cbf_strerror(CBF_FORMAT));
cbf_debug_print2("error: whilst processing column '%s'\n",name);
} else {
if (ndims == 0) {
cbf_h5reportneg(memtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(memspace = H5Screate(H5S_SCALAR),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Sselect_all(datasetspace),CBF_H5ERROR,errorcode);
cbf_h5reportneg(H5Tset_size(memtype,old_size+1),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Dread(datasetid, memtype, memspace, datasetspace, H5P_DEFAULT, (void *)datasettextbuffer),
CBF_H5ERROR,errorcode);
if (errorcode==0) {
errorcode|=cbf_set_value(cbf,datasettextbuffer);
}
} else {
cbf_h5reportneg(memtype = H5Tcopy(H5T_C_S1),CBF_ALLOC,errorcode);
cbf_h5reportneg(memspace = H5Screate_simple(ndims,count,0),CBF_ALLOC,errorcode);
cbf_h5reportneg(H5Tset_size(memtype,old_size+1),CBF_ALLOC,errorcode);
errorcode|=cbf_select_row(cbf,0);
for (dsslab=0; dsslab < dsdims[0] && errorcode == CBF_SUCCESS; dsslab++) {
offset[0] = dsslab;
cbf_h5reportneg(H5Sselect_hyperslab(datasetspace,H5S_SELECT_SET,
offset,stride,count,0), CBF_FORMAT,errorcode);
errorcode|=cbf_new_row(cbf);
if (H5Dread(datasetid, memtype, memspace, datasetspace,
H5P_DEFAULT, (void *)datasettextbuffer)>=0) {
errorcode|=cbf_set_value(cbf,datasettextbuffer);
}
}
}
if (memspace >= 0) H5Sclose(memspace);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasetspace >= 0) H5Sclose(datasetspace);
if (datasetid >= 0) H5Dclose(datasetid);
if (memtype >= 0) H5Tclose(memtype);
if (datasettextbuffer) cbf_free(&datasettextbuffer,NULL);
return errorcode;
}
}
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here, should be NXpdb for a cbfcat */
const char * NX_class = NULL;
const char * NXpdb_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
const int nxpdb_found = _cbf_Attrval(object,"NXpdb_class", &NXpdb_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
if (!strcmp(NX_class,"NXpdb")||
!strcmp(NX_class,"CBF_cbf") ||
!strcmp(NX_class,"CBF_cif") ||
!strcmp(NX_class,"CBF_cbfdb") ||
!strcmp(NX_class,"CBF_cbfcat") ||
!strcmp(NX_class,"CBF_cbfsf") ||
!strcmp(NX_class,"CBF_cbfcol") ||
( NXpdb_class &&
(!strcmp(NXpdb_class,"CBF_cif") ||
!strcmp(NXpdb_class,"CBF_cbfdb") ||
!strcmp(NXpdb_class,"CBF_cbfcat") ||
!strcmp(NXpdb_class,"CBF_cbfsf") ||
!strcmp(NXpdb_class,"CBF_cbfcol")
)
) ) {
const unsigned int indent = table->indent;
/* debugging output */
if (NX_class) free((void*)NX_class);
if (NXpdb_class) free((void*)NXpdb_class);
nx->cbf_datablock = NULL;
nx->cbf_saveframe = NULL;
nx->cbf_category = NULL;
nx->cbf_column = NULL;
error |= cbf_read_h5file_group(cbf,nx,nx->flags,(hid_t)object);
if (cbf_H5Ivalid(object)) H5Oclose(object);
return (CBF_SUCCESS==error) ? 0 : -1;
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
/* iteration in a top level NXpdb group that defines a cbfdb
should contain groups, each of which defines a cbfcat or nxpdb
each of which should contain datasets, each of which defines
a cbfcol, or possibly contains groups, each of which defines
a saveframe, which contains groups, each of which defines
a cbfcat or nxpdb each of which contains datasets, each of
which defines a cbfcol. */
static int cbf_write_nx2cbf__cbfdb_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
cbf_debug_print(" entering cbf_write_nx2cbf__cbfdb_op");
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
if (0) {
/* I don't actually have any items to match here */
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here, should be NXpdb for a cbfcat */
const char * NX_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
const char * NXpdb_class = NULL;
const int nxpdb_found = _cbf_Attrval(object,"NXpdb_class", &NXpdb_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
/* I have a group with an NX_class: match on NX_class */
} else if (!strcmp(NX_class,"NXpdb")||
!strcmp(NX_class,"CBF_cbf") ||
!strcmp(NX_class,"CBF_cif") ||
!strcmp(NX_class,"CBF_cbfdb") ||
!strcmp(NX_class,"CBF_cbfcat") ||
!strcmp(NX_class,"CBF_cbfsf") ||
!strcmp(NX_class,"CBF_cbfcol") ||
( NXpdb_class &&
(!strcmp(NXpdb_class,"CBF_cif") ||
!strcmp(NXpdb_class,"CBF_cbfdb") ||
!strcmp(NXpdb_class,"CBF_cbfcat") ||
!strcmp(NXpdb_class,"CBF_cbfsf") ||
!strcmp(NXpdb_class,"CBF_cbfcol")
)
) ) {
const unsigned int indent = table->indent;
/* debugging output */
if (NX_class) free((void*)NX_class);
if (NXpdb_class) free((void*)NXpdb_class);
nx->cbf_datablock = NULL;
nx->cbf_saveframe = NULL;
nx->cbf_category = NULL;
nx->cbf_column = NULL;
error |= cbf_read_h5file_group(cbf,nx,nx->flags,(hid_t)object);
if (cbf_H5Ivalid(object)) H5Oclose(object);
return (CBF_SUCCESS==error) ? 0 : -1;
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
int _cbf_write_nx2cbf__beam_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
double sigma_x = 0.0, sigma_y = 0.0;
int have_sigma_x = 0, have_sigma_y = 0;
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
if (!strcmp(name,"beam_size_x")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
hid_t units = CBF_H5FAIL;
double value = 0., factor = 0./0.;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mm",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
/* store data for later conversion */
sigma_x = value*factor;
have_sigma_x = 1;
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"beam_size_y")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
hid_t units = CBF_H5FAIL;
double value = 0., factor = 0./0.;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mm",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
/* store data for later conversion */
sigma_y = value*factor;
have_sigma_y = 1;
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"collimation")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"collimation"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"incident_divergence_x")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
hid_t units = CBF_H5FAIL;
double value = 0., factor = 0./0.;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"degrees",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
/* ensure I have suitable structure within the CBF file & write the data */
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"div_x_source"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"incident_divergence_xy")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
hid_t units = CBF_H5FAIL;
double value = 0., factor = 0./0.;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"degrees^2",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
/* ensure I have suitable structure within the CBF file & write the data */
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"div_x_y_source"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"incident_divergence_y")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
hid_t units = CBF_H5FAIL;
double value = 0., factor = 0./0.;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Dread2(object,0,0,0,&value,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"degrees",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
/* ensure I have suitable structure within the CBF file & write the data */
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"div_y_source"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"incident_polarisation_stokes")
||!strcmp(name,"incident_polarisation_stokes_average")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (2==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[2];
if (2!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0]) || !(4==dim[1])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[2];
hsize_t count[] = {1, 4};
double value[4] = {0., 0., 0., 0.};
offset[0] = dim[0]>1 ? nx->slice : 0;
offset[1] = 0;
/* read the value */
if (CBF_SUCCESS!=(error|=cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE))) {
cbf_debug_print(cbf_strerror(error));
} else {
const double psn = atan2(value[2],value[1])*22.5/atan2(1.,1.);
const double psr = sqrt(value[1]*value[1]+value[2]*value[2])/
fabs(value[0]);
/* extract & store the 2-parameter representation */
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"polarizn_source_norm_McStas"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",psn));
CBF_CALL(cbf_require_column(cbf,"polarizn_source_ratio"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",psr));
/*
Store the stokes vector - assuming uniform values over all frames.
This should only be done if the polarisation really is the same
for all frames, if it isn't then these fields should not be used.
There is currently no check that values are uniform across frames.
At this stage we have the vector in the MCStas coordinate
frame.
*/
CBF_CALL(cbf_require_column(cbf,"polarizn_Stokes_I"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value[0]));
CBF_CALL(cbf_require_column(cbf,"polarizn_Stokes_Q_McStas"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value[1]));
CBF_CALL(cbf_require_column(cbf,"polarizn_Stokes_U_McStas"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value[2]));
CBF_CALL(cbf_require_column(cbf,"polarizn_Stokes_V"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value[3]));
/* TODO: define and then store per-frame data about the radiation
and rotate the stokes vector into the CBF coordimate frame */
}
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"incident_polarisation_stokes_uncertainties")
||!strcmp(name,"incident_polarisation_stokes_average_uncertainties")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (2==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[2];
if (2!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0]) || !(4==dim[1])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[2];
hsize_t count[] = {1, 4};
double value_esds[4] = {0., 0., 0., 0.};
offset[0] = dim[0]>1 ? nx->slice : 0;
offset[1] = 0;
/* read the value */
if (CBF_SUCCESS!=(error|=cbf_H5Dread2(object,offset,0,count,&value_esds,H5T_NATIVE_DOUBLE))) {
cbf_debug_print(cbf_strerror(error));
} else {
/* Store the stokes vector esds - assuming uniform values over all frames.
This should only be done if the polarisation really is the same
for all frames, if it isn't then these fields should not be used.
There is currently no check that values are uniform across frames.
At this stage we have the uncertainties in the MCStas coordinate
frame.
*/
CBF_CALL(cbf_require_column(cbf,"polarizn_Stokes_I_esd"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value_esds[0]));
CBF_CALL(cbf_require_column(cbf,"polarizn_Stokes_Q_McStas_esd"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value_esds[1]));
CBF_CALL(cbf_require_column(cbf,"polarizn_Stokes_U_McStas_esd"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value_esds[2]));
CBF_CALL(cbf_require_column(cbf,"polarizn_Stokes_V_esd"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value_esds[3]));
/* TODO: define and then store per-frame data about the radiation
rotate to the CBF coordinate frame, and generate the
uncertainties for the 2-parameters form.
*/
}
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"incident_wavelength")||!strcmp(name,"wavelength")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
double value = 0;
double factor;
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(object,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"angstroms",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation_wavelength(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"wavelength"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",factor*value));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"weight")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (1==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[1];
if (1!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (!(1==dim[0] || table->frames==dim[0])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_SIZE;
} else {
hsize_t offset[1];
hsize_t count[] = {1};
double value = 0;
offset[0] = dim[0]>1 ? nx->slice : 0;
/* read the value */
CBF_CALL(cbf_H5Dread2(object,offset,0,count,&value,H5T_NATIVE_DOUBLE));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation_wavelength(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"wt"));
/* write the data */
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",value));
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here */
const char * NX_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
if (!strcmp(NX_class,"NXtransformations")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXpoise")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXcollection")) {
/* known class that should be ignored: ignoring it is the correct way to process it */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
if (CBF_SUCCESS==error) {
if (have_sigma_x && have_sigma_y) {
const double inhomogeneity = (sigma_x + sigma_y) * 0.5;
/* TODO: warn user if data may be lost due to unequal sigma_x & sigma_y: needs a comparison */
/* ensure I have suitable structure within the CBF file & write the data */
CBF_CALL(_cbf_nx2cbf_table__diffrn_radiation(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"inhomogeneity"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",inhomogeneity));
}
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
static int cbf_write_nx2cbf__sample_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
cbf_debug_print("Entering cbf_write_nx2cbf__sample_op");
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
cbf_debug_print2("cbf_write_nx2cbf__sample_op dataset name '%s'",name);
if (!strcmp(name,"depends_on")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * path = NULL;
cbf_axisData_t * prevAxisPtr = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&path));
cbf_debug_print2("cbf_write_nx2cbf__sample_op depends_on '%s'",path);
while (CBF_SUCCESS==error && path && strcmp(path,".")) {
/*
Given the path to an axis dataset, I want to extract
all relevant details and write them out to the CBF file.
*/
hid_t axis = CBF_H5FAIL;
hid_t do_base_id = CBF_H5FAIL;
do_base_id = g_id;
if (*path == '/') do_base_id = nx->hfile;
if (!cbf_H5Ivalid(axis=H5Dopen2(do_base_id, path, H5P_DEFAULT))) {
cbf_debug_print("couldn't open dataset");
error |= CBF_H5ERROR;
} else {
const char * _path = NULL;
hid_t depends_on = CBF_H5FAIL;
cbf_axisData_t * axisPtr = NULL;
const char * const chr = strrchr(path,'/');
const char * const name = NULL==chr ? path : 1+chr;
CBF_CALL(_cbf_nx2cbf_key_require_axis(table,&axisPtr,axis,name,axisEquipment_goniometer));
if (!prevAxisPtr) axisPtr->flags=CBF_AXIS_DATA_PGA;
CBF_CALL(cbf_H5Afind(axis,&depends_on,"depends_on",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(depends_on,&_path));
if (CBF_SUCCESS==error) {
/* set dependency of previous axis */
if (prevAxisPtr) prevAxisPtr->depends_on = axisPtr;
/* some axes are shared with other peices of equipment, set the type appropriately */
if (axisEquipment_goniometer!=axisPtr->equipment) axisPtr->equipment = axisEquipment_general;
} else {
cbf_H5Dfree(axis);
}
/* housekeeping: update the previous axis and path */
prevAxisPtr = axisPtr;
free((void*)path);
path = _path;
/* ensure hdf5 stuff is closed */
cbf_H5Afree(depends_on);
}
}
free((void*)path);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------------------*/
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here */
const char * NX_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
cbf_debug_print3("cbf_write_nx2cbf__sample_op group name '%s', NXclass '%s'",name,NX_class);
if (!strcmp(NX_class,"NXtransformations")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXpoise")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXcollection")) {
/* known class that should be ignored: ignoring it is the correct way to process it */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXbeam")) {
const unsigned int indent = table->indent;
/* debugging output */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/* leave ownership of the group with the iteration function, and process it */
++table->indent;
if (H5Literate(object,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,_cbf_write_nx2cbf__beam_op,op_data)<0) {
cbf_debug_print3("error: failed to iterate over items in the '%s:%s' group\n",name,NX_class);
error |= CBF_H5ERROR;
}
table->indent = indent;
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
cbf_debug_print2("sample_op exit error %x",error);
return (CBF_SUCCESS==error) ? 0 : -1;
}
static int cbf_write_nx2cbf__entry_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
const op_data_t * const op_data_struct = op_data;
cbf_h5handle nx = op_data_struct->nx;
cbf_handle cbf = op_data_struct->cbf;
cbf_nx2cbf_key_t * const table = op_data_struct->table;
hid_t object = CBF_H5FAIL;
H5I_type_t type = H5I_BADID;
if (!nx) {
cbf_debug_print("Invalid NeXus handle given");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given");
error |= CBF_ARGUMENT;
} else if (!table) {
cbf_debug_print("No key given");
error |= CBF_ARGUMENT;
} else if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
if (H5I_BADID==type) {
/* something went wrong when finding the object type */
cbf_debug_print2("error: couldn't get object type of '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_DATASET==type) {
/* handle all datasets here */
if (!strcmp(name,"end_time")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_scan(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"date_end"));
/* write the data */
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"method")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
CBF_CALL(_cbf_nx2cbf_table__diffrn_measurement(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"method"));
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(name,"start_time")) {
hid_t data_space = CBF_H5FAIL;
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,1);
if (!cbf_H5Ivalid(data_space=H5Dget_space(object))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else {
/* check rank, allowing for multiple usable results */
if (0==H5Sget_simple_extent_ndims(data_space)) {
const char * value = NULL;
CBF_CALL(_cbf_nx2cbfDread_scalar_string(object,&value));
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__diffrn_scan(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"date_start"));
/* write the data */
CBF_CALL(cbf_set_value(cbf,value));
free((void*)value);
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
/*--------------------------------------------------------------------------------------*/
} else {
/* unknown field: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
} else if (H5I_GROUP==type) {
/* get NXclass & handle all groups here */
const char * NX_class = NULL;
const char * NXpdb_class = NULL;
const int found = _cbf_NXclass(object,&NX_class);
const int nxpdb_found = _cbf_Attrval(object,"NXpdb_class", &NXpdb_class);
if (CBF_NOTFOUND==found) {
/* no NX_class: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: whilst processing group '%s'\n",name);
}
error |= found;
} else {
/* I have a group with an NX_class: match on NX_class */
if (!strcmp(NX_class,"NXtransformations")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXpoise")) {
/* known class that will be processed */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXcollection") || !strcmp(NX_class,"NXdata")) {
/* known class that should be ignored: ignoring it is the correct way to process it */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
} else if (!strcmp(NX_class,"NXinstrument")) {
const unsigned int indent = table->indent;
hid_t group = object;
/* debugging output */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/* try to take ownership of the group away from the iteration function */
if (CBF_SUCCESS!=(error|=cbf_h5handle_set_instrument(nx,object,name))) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: failed to set the '%s' group in the handle\n",name);
}
} else {
object = CBF_H5FAIL;
/* The hdf5 handle now owns the group, process it */
++table->indent;
if (H5Literate(group,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,cbf_write_nx2cbf__instrument_op,op_data)<0) {
cbf_debug_print3("error: failed to iterate over items in the '%s:%s' group\n",name,NX_class);
error |= CBF_H5ERROR;
}
table->indent = indent;
}
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(NX_class,"NXsample")) {
const unsigned int indent = table->indent;
hid_t group = object;
/* debugging output */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,1);
/* try to take ownership of the group away from the iteration function */
if (CBF_SUCCESS!=(error|=cbf_h5handle_set_sample(nx,object,name))) {
if (1) {
cbf_debug_print(cbf_strerror(found));
cbf_debug_print2("error: failed to set the '%s' group in the handle\n",name);
}
} else {
object = CBF_H5FAIL;
/* The hdf5 handle now owns the group, process it */
++table->indent;
if (H5Literate(group,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,cbf_write_nx2cbf__sample_op,op_data)<0) {
cbf_debug_print3("error: failed to iterate over items in the '%s:%s' group\n",name,NX_class);
error |= CBF_H5ERROR;
}
table->indent = indent;
}
/*-----------------------------------------------------------------------------------------------*/
} else if (!strcmp(NX_class,"NXpdb")||
!strcmp(NX_class,"CBF_cbf") ||
!strcmp(NX_class,"CBF_cif") ||
!strcmp(NX_class,"CBF_cbfdb") ||
!strcmp(NX_class,"CBF_cbfcat") ||
!strcmp(NX_class,"CBF_cbfsf") ||
!strcmp(NX_class,"CBF_cbfcol") ||
( NXpdb_class &&
(!strcmp(NXpdb_class,"CBF_cif") ||
!strcmp(NXpdb_class,"CBF_cbfdb") ||
!strcmp(NXpdb_class,"CBF_cbfcat") ||
!strcmp(NXpdb_class,"CBF_cbfsf") ||
!strcmp(NXpdb_class,"CBF_cbfcol")
)
) ) {
const unsigned int indent = table->indent;
/* debugging output */
if (NX_class) free((void*)NX_class);
if (NXpdb_class) free((void*)NXpdb_class);
nx->cbf_datablock = NULL;
nx->cbf_saveframe = NULL;
nx->cbf_category = NULL;
nx->cbf_column = NULL;
error |= cbf_read_h5file_group(cbf,nx,nx->flags,(hid_t)object);
if (cbf_H5Ivalid(object)) H5Oclose(object);
return (CBF_SUCCESS==error) ? 0 : -1;
/*-----------------------------------------------------------------------------------------------*/
} else {
/* unknown NX_class: (probably) not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,NX_class,table->indent,0);
}
}
if (NX_class)free((void*)NX_class);
} else {
/* unrecognised object type: can't process it, but it's not an error */
if (nx->logfile) _cbf_write_name(nx->logfile,name,0,table->indent,0);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? 0 : -1;
}
/* sorting function for categories */
static int cmp_category
(const void * const p1,
const void * const p2)
{
const char last[] = "array_data";
const cbf_node * const * const n1 = p1;
const cbf_node * const * const n2 = p2;
const char * const s1 = (*n1)->name;
const char * const s2 = (*n2)->name;
if (!strcmp(s1,s2)) return 0;
else return (!strcmp(s1,last) || (strcmp(s2,last) && strcmp(s1,s2)>0)) ? 1 : -1;
}
/* sorting function for columns */
static int cmp_column
(const void * const p1,
const void * const p2)
{
const char str[] = "id";
const cbf_node * const * const n1 = p1;
const cbf_node * const * const n2 = p2;
const char * const s1 = (*n1)->name;
const char * const s2 = (*n2)->name;
const int cmp = strcmp(s1,s2);
/* do the comparison: first check for equality, then for important values, then order the rest */
if (!cmp) return 0;
else if (!strcmp(str,s1)) return -1;
else if (!strcmp(str,s2)) return +1;
else return cmp;
}
/* sorting function for 'array_data' category */
static int cmp_arraydata
(const void * const p1,
const void * const p2)
{
const char str[] = "data";
const cbf_node * const * const n1 = p1;
const cbf_node * const * const n2 = p2;
const char * const s1 = (*n1)->name;
const char * const s2 = (*n2)->name;
const int cmp = strcmp(s1,s2);
/* do the comparison: first check for equality, then for important values, then order the rest */
if (!cmp) return 0;
else if (!strcmp(str,s1)) return +1;
else if (!strcmp(str,s2)) return -1;
else return cmp;
}
typedef struct cbf_H5_findObject_t
{
hid_t * foundField; /*< Where to store the object that was found. */
const char * * foundName; /*< Where to store the name of the object that was found. */
const char * searchName; /*< The name of the object, or NULL for any field name. */
const char * searchClass; /*< The 'NX_class' of the object, or NULL for any class. */
H5I_type_t searchType; /*< The HDF5 type of the object (see: H5Iget_type), or 'H5I_BADID' for any type. */
} cbf_H5_findObject_t;
int cbf_cimatch(const char * name, const char * pattern) {
size_t len, ii;
int retval = 0;
len = _cbf_strlen(name);
if (len == 0 && _cbf_strlen(pattern) == 0) return 0;
if (len == 0) return -1;
{
CBF_START_ARRAY(char, lcname,len+1);
for (ii=0; ii<len; ii++) lcname[ii] = tolower(name[ii]);
retval = cbf_match(lcname,(char *)pattern);
CBF_END_ARRAY(lcname);
return retval;
}
}
/*
Given an instance of 'cbf_H5_findObject_t' as the 'op_data', find a matching object and return
both the object and its name. The name should later be free'd by the caller.
If a match is found at a given level then a positive value should be returned. If an error occurs
then a negative value should be returned. Failure to find a match will result in a return value
of 0 (unless there was an error) and the value at 'cbf_H5_findObject_t::field' being unchanged.
*/
static int cbf_findObject_op
(hid_t g_id,
const char * name,
const H5L_info_t * info,
void * op_data)
{
int error = CBF_SUCCESS;
int found = 0;
if (!cbf_H5Ivalid(g_id) || !name || !info || !op_data) {
cbf_debug_print(cbf_strerror(CBF_ARGUMENT));
error |= CBF_ARGUMENT;
} else {
H5I_type_t type = H5I_BADID;
cbf_H5_findObject_t * const findObject = (cbf_H5_findObject_t*) op_data;
if (!findObject->searchName || !cbf_cimatch(name,findObject->searchName)) {
hid_t object = CBF_H5FAIL;
if (!cbf_H5Ivalid(object=H5Oopen(g_id, name, H5P_DEFAULT))) {
cbf_debug_print2("error: couldn't open '%s'\n",name);
error |= CBF_H5ERROR;
} else if (H5I_BADID==(type=H5Iget_type(object))) {
cbf_debug_print2("error: couldn't get type of '%s'\n",name);
error |= CBF_H5ERROR;
} else {
/* check object type: */
if (H5I_BADID==findObject->searchType || findObject->searchType==type) {
const char * class = NULL;
if (findObject->searchClass && CBF_SUCCESS!=(error|=_cbf_NXclass(object,&class))) {
cbf_debug_print("couldn't get value of 'NX_class' attribute");
cbf_debug_print(cbf_strerror(error));
}
/* check 'NX_class': */
if (!findObject->searchClass || (class && !strcmp(class,findObject->searchClass))) {
/* I have a match! */
found = 1;
*(findObject->foundField) = object;
*(findObject->foundName) = _cbf_strdup(name);
object = CBF_H5FAIL;
}
free((void*)class);
}
}
if (cbf_H5Ivalid(object)) H5Oclose(object);
}
}
/*
Convert a CBF error to something the HDF5 iteration function can understand.
All errors should already have been reported, so I shouldn't need to print anything.
*/
return (CBF_SUCCESS==error) ? (found ? +1 : 0) : (-1);
}
/**
Reads NeXus-format data from the entry group defined in the <code>nx</code> handle, extracting data
related to the frame with index <code>nx->slice</code> and in CBF-format within the the <code>cbf</code>
handle.
\param nx The handle defining the NeXus data to be converted.
\param cbf The handle in which to store the resulting CBF data.
\sa cbf_write_cbf_h5file
\sa cbf_write_minicbf_h5file
\sa cbf_write_nx2cbf
\return An error code.
*/
int cbf_write_nx2cbf
(cbf_h5handle nx,
cbf_handle cbf)
{
int error = CBF_SUCCESS;
hid_t entry = CBF_H5FAIL;
const char * entry_name = NULL;
/* check for appropriate items in the handle */
if (!nx) {
cbf_debug_print("No NeXus handle given\n");
error |= CBF_ARGUMENT;
} else if (!cbf) {
cbf_debug_print("No CBF handle given\n");
error |= CBF_ARGUMENT;
} else {
size_t _array_id_size = 0;
char * _array_id = NULL;
const char array_id[] = "array_1";
const char datablock_id[] = "db_1";
const char diffrn_id[] = "diffrn_1";
const char diffrn_detector_id[] = "diffrn_detector_1";
const char diffrn_detector_element_id[] = "diffrn_detector_element_1";
const char diffrn_measurement_id[] = "diffrn_measurement_1";
const char frame_id[] = "frame_1";
const char scan_id[] = "scan_1";
const char wavelength_id[] = "wavelength_1";
cbf_nx2cbf_key_t table[1];
/* Register compression filters if requested */
if (nx->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_CBF)) {
cbf_h5reportneg(H5Zregister(CBF_H5Z_CBF),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_CBF (32006) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
#ifdef CBF_H5Z_USE_LZ4
if (!H5Zfilter_avail(CBF_H5Z_FILTER_LZ4)) {
error = 0;
cbf_h5reportneg(H5Zregister(H5Z_LZ4),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_LZ4 (32004) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
#endif
#ifdef CBF_H5Z_USE_BSHUF
if (!H5Zfilter_avail(CBF_H5Z_FILTER_BSHUF)) {
error = 0;
cbf_h5reportneg(H5Zregister(bshuf_H5Filter),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_BSHUF (32008) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
#endif
}
/* initialise & populate the key */
if (CBF_SUCCESS!=(error|=_cbf_create_nx2cbf_key(table))) {
cbf_debug_print(cbf_strerror(error));
} else {
table->array_id = array_id;
table->binary_id = 1;
table->datablock_id = datablock_id;
table->diffrn_id = diffrn_id;
table->diffrn_detector_id = diffrn_detector_id;
table->diffrn_detector_element_id = diffrn_detector_element_id;
table->diffrn_measurement_id = diffrn_measurement_id;
table->frame_id = frame_id;
table->scan_id = scan_id;
table->wavelength_id = wavelength_id;
}
/*
Start by converting the data, obtaining some primary keys required for converting the rest of the data.
When I have the data:
- match/insert settings for a given image axis in 'axis' and 'array_structure_list_axis' categories.
- obtain several axis_set_ids from the above step, one per image axis, which won't need to be changed.
- match/insert a row of 'array_structure_list' for each axis_set_id.
if all matched rows in 'array_structure_list' have the same array_id then:
- match/insert a row in 'array_structure', duplicating the relevant rows of 'array_structure_list'
if a new 'array_structure' row had to be added.
else:
- duplicate existing rows of 'array_structure_list', giving them the new array_id.
- insert a new row into 'array_structure'.
fi
- use the array_id obtained above to extract the smallest permissible binary_id for an image.
- insert the image data with the array_id & binary_id, caching them to be referred to later.
*/
if (CBF_SUCCESS!=(error|=cbf_h5handle_get_entry(nx, &entry, &entry_name))) {
cbf_debug_print("error: couldn't get current entry from NeXus file handle");
cbf_debug_print(cbf_strerror(error));
}
if (!error && nx->logfile) {
int len;
fputc('\n',nx->logfile);
len = fprintf(nx->logfile,"Extracting data from '%s:NXentry':\n",entry_name);
while (--len > 0) fputc('=',nx->logfile);
fputc('\n',nx->logfile);
}
if (CBF_SUCCESS==error) {
const char empty[] = "";
const char no_compression[] = "none";
const char canonical[] = "canonical";
const char packed[] = "packed";
const char packed_v2[] = "packed_v2";
const char byte_offset[] = "byte_offset";
const char nibble_offset[] = "nibble_offset";
const char big_endian[] = "big_endian";
const char little_endian[] = "little_endian";
const char * data_byte_order = NULL;
const char * data_compression = NULL;
char * _data_encoding = NULL;
const char * data_encoding = empty;
size_t _data_encoding_size = 0;
unsigned int data_row = ~0x0;
hid_t instrument = CBF_H5FAIL;
hid_t detector = CBF_H5FAIL;
hid_t data = CBF_H5FAIL;
if (!error && nx->logfile) {
int len;
fputc('\n',nx->logfile);
len = fprintf(nx->logfile,"Counting frames & converting data:\n");
while (--len > 0) fputc('-',nx->logfile);
fputc('\n',nx->logfile);
}
if (CBF_SUCCESS==error) {
/* find instrument */
const char * groupName = NULL;
const char class[] = "NXinstrument";
cbf_H5_findObject_t find_data;
herr_t err;
find_data.foundField = &instrument;
find_data.foundName = &groupName;
find_data.searchName = NULL;
find_data.searchClass = class;
find_data.searchType = H5I_GROUP;
err = H5Literate(entry,H5_INDEX_NAME,H5_ITER_INC,NULL,cbf_findObject_op,&find_data);
if (err < 0) {
cbf_debug_print2("error: problem while trying to find '*:%s'\n",class);
error |= CBF_H5ERROR;
} else if (!err) {
cbf_debug_print2("error: couldn't find '*:%s'\n",class);
error |= CBF_NOTFOUND;
} else {
if (CBF_SUCCESS!=(error|=cbf_h5handle_set_instrument(nx,instrument,groupName))) {
cbf_debug_print(cbf_strerror(error));
cbf_H5Gfree(instrument);
}
}
free((void*)groupName);
}
if (CBF_SUCCESS==error) {
/* find detector */
const char * groupName = NULL;
const char class[] = "NXdetector";
cbf_H5_findObject_t find_data;
herr_t err;
find_data.foundField = &detector;
find_data.foundName = &groupName;
find_data.searchName = NULL;
find_data.searchClass = class;
find_data.searchType = H5I_GROUP;
err = H5Literate(instrument,H5_INDEX_NAME,H5_ITER_INC,NULL,cbf_findObject_op,&find_data);
if (err < 0) {
cbf_debug_print2("error: problem while trying to find '*:%s'\n",class);
error |= CBF_H5ERROR;
} else if (!err) {
cbf_debug_print2("error: couldn't find '*:%s'\n",class);
error |= CBF_NOTFOUND;
} else {
if (CBF_SUCCESS!=(error|=cbf_h5handle_set_detector(nx,detector,groupName))) {
cbf_debug_print(cbf_strerror(error));
cbf_H5Gfree(detector);
}
}
free((void*)groupName);
}
if (CBF_SUCCESS==error) {
/*
find data
TODO: use more direct method, instead of using the iteration function?
*/
const char * groupName = NULL;
const char name[] = "data|data_*";
cbf_H5_findObject_t find_data;
herr_t err;
find_data.foundField = &data;
find_data.foundName = &groupName;
find_data.searchName = name;
find_data.searchClass = NULL;
find_data.searchType = H5I_DATASET;
err = H5Literate(detector,H5_INDEX_NAME,H5_ITER_INC,NULL,cbf_findObject_op,&find_data);
if (err < 0) {
cbf_debug_print2("error: problem while trying to find '%s' in NXdetector\n",name);
error |= CBF_H5ERROR;
} else if (!err) {
H5G_info_t data_info;
H5O_info_t object_info;
char oname[10];
ssize_t oname_len;
hsize_t idx;
herr_t err2, err3;
cbf_debug_print2("error: couldn't find '%s' in NXdetector\n",name);
/* error |= CBF_NOTFOUND; */
error |= cbf_h5handle_get_data(nx,0,0);
err2 = H5Gget_info(nx->nxdata,&data_info);
data = CBF_H5FAIL;
if (err2 >= 0) for (idx = 0; idx < data_info.nlinks; idx++) {
err3 = H5Oget_info_by_idx(nx->nxdata,".",H5_INDEX_NAME,H5_ITER_INC,
idx,&object_info,H5P_DEFAULT);
if (err3 < 0 || object_info.type != H5O_TYPE_DATASET) continue;
oname[0] = '\0';
oname_len = H5Lget_name_by_idx(nx->nxdata,".",H5_INDEX_NAME,H5_ITER_INC,
idx,oname,(size_t)10,H5P_DEFAULT);
if (oname_len < 0) continue;
oname[9] = '\0';
if (cbf_cimatch(oname,name)) continue;
data = H5Oopen_by_idx(nx->nxdata,".",H5_INDEX_NAME,H5_ITER_INC,
idx,H5P_DEFAULT);
break;
}
}
free((void*)groupName);
}
if (CBF_SUCCESS==error) {
/* extract dimensions of data */
hsize_t dims[4];
/* extract dimensions of data, convert it and get data format & layout metadata */
hid_t data_space = CBF_H5FAIL;
if (!cbf_H5Ivalid(data_space=H5Dget_space(data))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else if (4<(table->rank=H5Sget_simple_extent_ndims(data_space))
|| table->rank == 0) {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
} else if (table->rank!=H5Sget_simple_extent_dims(data_space,dims,0)) {
cbf_debug_print("could not get dimensions of data");
error |= CBF_H5ERROR;
} else {
table->frames = dims[0];
if (table->rank > 3) table->zdim = dims[table->rank-3]; else table->zdim = 1;
if (table->rank > 2) table->ydim = dims[table->rank-2]; else table->ydim = 1;
if (table->rank > 1) table->xdim = dims[table->rank-1]; else table->xdim = 1;
}
cbf_H5Sfree(data_space);
}
if (!error && nx->logfile) {
/* tell the user something about the data if they requested some information */
fprintf(
nx->logfile,
"Found %lu frame%s\nframe dimensions: [%lu, %lu]\n",
(unsigned long)(table->frames),
table->frames != 1 ? "s" : "",
(unsigned long)(table->ydim),
(unsigned long)(table->xdim)
);
fprintf(nx->logfile,"%lu elems per frame\n",(unsigned long)(table->xdim*table->ydim*table->zdim));
}
if (CBF_SUCCESS==error) {
/* log image axes for later use */
const char x_pixel_offset_pat[] = "x_pixel_offset|x_pixel_offset_*";
const char y_pixel_offset_pat[] = "y_pixel_offset|y_pixel_offset_*";
const char z_pixel_offset_pat[] = "z_pixel_offset|z_pixel_offset_*";
const char x_pixel_size_pat[] = "x_pixel_size|x_pixel_size_*";
const char y_pixel_size_pat[] = "y_pixel_size|y_pixel_size_*";
const char z_pixel_size_pat[] = "z_pixel_size|z_pixel_size_*";
const char * pixel_offset_pat[3];
char * pixel_offset_name[3] = {NULL,NULL,NULL};
char * pixel_offset_long_name[3] = {NULL,NULL,NULL};
const char * pixel_size_pat[3];
char * pixel_size_name[3] = {NULL,NULL,NULL};
const char * _axis_set_id[3] = {NULL, NULL,NULL}; /*< always free'able */
const char * axis_set_id[3]; /*< always useable */
hsize_t pixel_offset_dim[3];
double elem_size[] = {0./0., 0./0.,0./0.};
int i;
pixel_offset_pat[0] = z_pixel_offset_pat;
pixel_offset_pat[1] = y_pixel_offset_pat;
pixel_offset_pat[2] = x_pixel_offset_pat;
pixel_size_pat[0] = z_pixel_size_pat;
pixel_size_pat[1] = y_pixel_size_pat;
pixel_size_pat[2] = x_pixel_size_pat;
pixel_offset_dim[0] = table->zdim;
pixel_offset_dim[1] = table->ydim;
pixel_offset_dim[2] = table->xdim;
axis_set_id[0] = empty;
axis_set_id[1] = empty;
axis_set_id[2] = empty;
for (i = 4-(table->rank); i < 3; ++i) {
hid_t pixel_data = CBF_H5FAIL;
hid_t data_space = CBF_H5FAIL;
const char * path;
double disp = 0.; /*< read from '*:NXdetector/[xyz]_pixel_offset' */
double disp_incr = 0.; /*< read from '*:NXdetector/[xyz]_pixel_size' */
int sign = 0;
hsize_t index = 0;
hsize_t oindex;
/* extract pixel offset data */
if (cbf_find_h5group_link(detector,".",pixel_offset_pat[i],
index,&oindex,&(pixel_offset_name[i]))) {
/* if (H5Lexists(detector,pixel_offset_name[i],H5P_DEFAULT)<= 0) { */
cbf_debug_print2("pixel_offset_name axis '%s' not found\n",pixel_offset_name[i]);
error |= CBF_NOTFOUND;
} else if (!cbf_H5Ivalid(pixel_data=H5Dopen2(detector,pixel_offset_name[i],H5P_DEFAULT))) {
cbf_debug_print("error: couldn't open a dataset");
free((void*)pixel_offset_name[i]);pixel_offset_name[i]=NULL;
error |= CBF_H5ERROR;
} else if (!cbf_H5Ivalid(data_space=H5Dget_space(pixel_data))) {
cbf_debug_print("error: couldn't get a dataspace");
free((void*)pixel_offset_name[i]);pixel_offset_name[i]=NULL;
error |= CBF_H5ERROR;
} else {
const int rank = H5Sget_simple_extent_ndims(data_space);
if (rank < 0) {
cbf_debug_print("error: problem getting the rank of a dataset");
error |= CBF_H5ERROR;
} else if (rank > 0) {
hsize_t dims[rank];
if (rank != H5Sget_simple_extent_dims(data_space,dims,0)) {
cbf_debug_print("error: problem getting the dimensions of a dataset");
error |= CBF_H5ERROR;
} else if (dims[rank-1] != pixel_offset_dim[i]) {
cbf_debug_print3("error: dimensions of pixel offset %ld don't match dimensions of data %ld",
(long)pixel_offset_dim[i], (long)dims[rank-1]);
error |= CBF_H5DIFFERENT;
} else {
/*
I (probably) have a suitable axis:
- extract the first number to store as 'array_structure_list_axis.displacement'
- assume that it's a uniformly spaced array of pixels in 3D space, so ignore subsequent values
- store it (and dependency chain) in the key, to be converted along with other axes
*/
hsize_t off[rank];
hsize_t cnt[rank];
size_t ir;
double disp2[] = {0.,0.};
double factor = 0./0.;
for (ir = 0; ir < rank; ir++) { off[ir] = 0; cnt[ir] = (ir<rank-1)?1:2;}
/* extract data */
CBF_CALL(cbf_H5Dread2(pixel_data,off,NULL,cnt,disp2,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(pixel_data,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mm",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
disp = disp2[0]*factor;
sign = (disp2[1]-disp2[0]) >= 0. ? +1 : -1;
if ( CBF_SUCCESS == error ) {
/* change the name if a long_name attribute was given */
hid_t long_name = CBF_H5FAIL;
const char * long_name_string = NULL;
if (!cbf_H5Afind(pixel_data,&long_name,"long_name",CBF_H5FAIL,CBF_H5FAIL)
&& !cbf_H5Aread_string(long_name,&long_name_string)
&& long_name_string && long_name_string[0]) {
pixel_offset_long_name[i] = _cbf_strdup(long_name_string);
} else {
pixel_offset_long_name[i] = _cbf_strdup(pixel_offset_name[i]);
}
free((void*)long_name_string);
cbf_H5Afree(long_name);
}
}
} else {
cbf_debug_print("error: unsupported rank of a 'pixel_offset' field");
error |= CBF_NOTIMPLEMENTED;
}
}
cbf_H5Sfree(data_space);
cbf_H5Dfree(pixel_data);
pixel_data = CBF_H5FAIL;
data_space = CBF_H5FAIL;
/*
Extract pixel size data.
TODO: make the presence of this data optional, using the pixel_offset fields as a backup option.
*/
if (cbf_find_h5group_link(detector,".",pixel_size_pat[i],
index,&oindex,&(pixel_size_name[i]))) {
cbf_debug_print2("pixel_offset_size field '%s' not found\n",pixel_size_name[i]);
error |= CBF_NOTFOUND;
} else if (!cbf_H5Ivalid(pixel_data=H5Dopen2(detector,pixel_size_name[i],H5P_DEFAULT))) {
cbf_debug_print("error: couldn't open a dataset");
free((void*)pixel_size_name[i]);pixel_size_name[i]=NULL;
error |= CBF_H5ERROR;
} else if (!cbf_H5Ivalid(data_space=H5Dget_space(pixel_data))) {
cbf_debug_print("error: couldn't get a dataspace");
free((void*)pixel_size_name[i]);pixel_size_name[i]=NULL;
error |= CBF_H5ERROR;
} else {
const int rank = H5Sget_simple_extent_ndims(data_space);
if (rank < 0) {
cbf_debug_print("error: problem getting the rank of a dataset");
error |= CBF_H5ERROR;
} else if (0==rank) {
double size = 0.;
double factor = 0./0.;
CBF_CALL(cbf_H5Dread2(pixel_data,NULL,NULL,NULL,&size,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
/* convert the data to the correct units */
hid_t units = CBF_H5FAIL;
const char * unit_string = NULL;
CBF_CALL(cbf_H5Afind(pixel_data,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
CBF_CALL(cbf_scale_units(unit_string,"mm",&factor));
free((void*)unit_string);
cbf_H5Afree(units);
}
disp_incr = fabs(size)*factor*sign;
elem_size[i] = fabs(size)*factor*.001;
} else {
/* pixel size is (possibly) not uniform, this can't be handled in CBF -> fail */
cbf_debug_print("error: unsupported rank of a 'pixel_offset' field");
error |= CBF_FORMAT;
}
}
cbf_H5Sfree(data_space);
cbf_H5Dfree(pixel_data);
path = _cbf_strdup(pixel_offset_name[i]);
if (CBF_SUCCESS==error) {
cbf_axisData_t * prevAxisPtr = NULL;
while (CBF_SUCCESS==error && path && strcmp(path,".")) {
/*
Given the path to an axis dataset, I want to extract
all relevant details and write them out to the CBF file.
*/
hid_t axis = CBF_H5FAIL;
if (!cbf_H5Ivalid(axis=H5Dopen2(detector, path, H5P_DEFAULT))) {
cbf_debug_print("couldn't open dataset");
error |= CBF_H5ERROR;
} else {
const char * _path = NULL;
hid_t depends_on = CBF_H5FAIL;
cbf_axisData_t * axisPtr = NULL;
const char * const chr = strrchr(path,'/');
const char * const name = NULL==chr ? path : 1+chr;
CBF_CALL(_cbf_nx2cbf_key_require_axis(table,&axisPtr,axis,name,axisEquipment_image));
CBF_CALL(cbf_H5Afind(axis,&depends_on,"depends_on",CBF_H5FAIL,CBF_H5FAIL));
CBF_CALL(cbf_H5Aread_string(depends_on,&_path));
if (CBF_SUCCESS==error) {
/* set dependency of previous axis */
if (prevAxisPtr) prevAxisPtr->depends_on = axisPtr;
/* some axes are shared with other peices of equipment, set the type appropriately */
if (axisEquipment_image!=axisPtr->equipment && axisEquipment_detector!=axisPtr->equipment) {
axisPtr->equipment = axisEquipment_general;
}
} else {
cbf_H5Dfree(axis);
}
/* housekeeping: update the previous axis and path */
prevAxisPtr = axisPtr;
free((void*)path);
path = _path;
/* ensure hdf5 stuff is closed */
cbf_H5Afree(depends_on);
}
}
}
free((void*)path);
if (CBF_SUCCESS==error) {
/* search for all rows in 'array_structure_list_axis' with matching axis_id, get a unique axis_set_id to store */
unsigned int * rows = NULL;
cbf_node * node = NULL;
unsigned int j, nRows = 0;
/* select/insert a column in the CBF file */
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
/* TODO: add a function to ensure all foreign keys exist? */
CBF_CALL(cbf_require_category(cbf,"array_structure_list_axis"));
CBF_CALL(cbf_require_column(cbf,"axis_id"));
node = cbf->node;
/* extract a node set of rows with matching axis_ids from the CBF file */
rows = malloc(node->children*sizeof(unsigned int));
for (j = 0; CBF_SUCCESS==error && j != node->children; ++j) {
const char * val = NULL;
CBF_CALL(cbf_node_get_value(node,j,&val));
if (val && (!cbf_cistrcmp(val,pixel_offset_name[i])
|| (pixel_offset_long_name[i]
&&!cbf_cistrcmp(val,pixel_offset_long_name[i]))))
rows[nRows++] = j;
}
if (0==nRows) {
/* axis name is not yet present, no suffix needed */
if (pixel_offset_long_name[i]) {
axis_set_id[i] = pixel_offset_long_name[i];
} else {
axis_set_id[i] = pixel_offset_name[i];
}
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,axis_set_id[i]));
CBF_CALL(cbf_require_column(cbf,"axis_set_id"));
CBF_CALL(cbf_set_value(cbf,axis_set_id[i]));
CBF_CALL(cbf_require_column(cbf,"displacement"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",disp));
CBF_CALL(cbf_require_column(cbf,"displacement_increment"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",disp_incr));
} else {
/*
The node set is indexed by rows[0:nRows], generate a unique axis_set_id using the axis name
and a numeric suffix. Store the generated string in '_axis_set_id', and point 'axis_set_id'
towards it.
*/
cbf_debug_print("generation of unique 'axis_set_id's is not yet implemented");
error |= CBF_NOTIMPLEMENTED;
}
free((void*)rows);
}
}
/*
I have axis_set_ids to use, I need a matching or new array_id. Simplest solution is one array_id per
stack of frames added to the file, with a new id generated each time this function is run.
*/
if (CBF_SUCCESS==error) {
cbf_node * node = NULL;
unsigned int id = 0;
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"array_structure"));
CBF_CALL(cbf_require_column(cbf,"id"));
node = cbf->node;
/* iteratively attempt to find an id that isn't used via a template like "array_%u" */
while (CBF_SUCCESS==error) {
int found = CBF_SUCCESS;
/* generate a test id */
while (CBF_SUCCESS==error) {
char * _array_id_new = NULL;
const int n = snprintf(_array_id,_array_id_size,"array_%u",id);
/* check if the string was formed successfully */
if (n > -1 && n < (ssize_t)_array_id_size) break;
/* otherwise, set its size to the correct size or to the next higher power-of-2 */
if (n > -1) _array_id_size = 1+n;
else _array_id_size = _array_id_size ? 2*_array_id_size : 1;
/* reallocate the string */
if (NULL==(_array_id_new=realloc(_array_id,_array_id_size))) error |= CBF_ALLOC;
else _array_id = _array_id_new;
}
/* search for it in the list of existing ids */
found = cbf_find_row(cbf,_array_id);
if (CBF_NOTFOUND==found) {
table->array_id = _array_id;
break;
} else if (CBF_SUCCESS!=found) {
cbf_debug_print(cbf_strerror(found));
error |= found;
}
/* it already exists, try the next one... */
++id;
}
}
/* I have an array_id, I need to generate a unique binary id to accompany it */
if (CBF_SUCCESS==error) {
/* TODO */
}
/* write the data & extract some metadata from it */
if (CBF_SUCCESS==error) {
hid_t data_space = CBF_H5FAIL;
hid_t data_type = CBF_H5FAIL;
if (!cbf_H5Ivalid(data_space=H5Dget_space(data))) {
cbf_debug_print("could not get data space");
error |= CBF_H5ERROR;
} else if (!cbf_H5Ivalid(data_type=H5Dget_type(data))) {
cbf_debug_print("could not get data type");
error |= CBF_H5ERROR;
} else {
/* check rank of data, allowing for multiple usable results */
if (table->rank==H5Sget_simple_extent_ndims(data_space)) {
hsize_t dim[4];
if (table->rank!=H5Sget_simple_extent_dims(data_space,dim,0)) {
cbf_debug_print("Couldn't get dimensions of dataset");
error |= CBF_H5ERROR;
} else if (table->frames!=dim[0]
|| (table->rank > 3 && table->zdim!=dim[table->rank-3])
|| (table->rank > 2 && table->ydim!=dim[table->rank-2])
|| (table->rank > 1 && table->xdim!=dim[table->rank-1])) {
cbf_debug_print("invalid dimensions of dataset");
error |= CBF_H5DIFFERENT;
} else {
hid_t native_type = CBF_H5FAIL;
/* ensure I have suitable structure within the CBF file */
CBF_CALL(_cbf_nx2cbf_table__array_data(cbf,nx,table));
CBF_CALL(cbf_require_column(cbf,"data"));
data_row = cbf->row;
/* extract the data */
if (!cbf_H5Ivalid(native_type=H5Tget_native_type(data_type,H5T_DIR_ASCEND))) {
cbf_debug_print("Couldn't get native type of dataset");
error |= CBF_H5ERROR;
} else {
/* extract the data from HDF5 and store in CBF */
{
hsize_t offset[4];
hsize_t count[4];
H5T_class_t classtype;
unsigned int compression = CBF_BYTE_OFFSET;
H5T_sign_t h5sign;
size_t nelems;
const size_t elem_size = H5Tget_size(native_type);
void * array;
H5T_order_t h5order;
h5order = H5Tget_order(native_type);
h5sign = H5Tget_sign(native_type);
classtype = H5Tget_class(native_type);
offset[0] = nx->slice;
offset[1] = offset[2] = offset[3] = 0;
count[0] = 1;
count[1] = dim[1];
count[2] = dim[2];
count[3] = dim[3];
nelems = count[0]*table->xdim*table->ydim*table->zdim;
array = malloc(nelems*elem_size);
if (H5T_ORDER_LE==h5order) data_byte_order = little_endian;
else if (H5T_ORDER_BE==h5order) data_byte_order = big_endian;
/* extract data from HDF5 and store in CBF: */
if (h5sign<0) h5sign = H5T_SGN_NONE;
CBF_CALL(cbf_H5Dread2(data,offset,0,count,array,native_type));
if (classtype == H5T_INTEGER) {
CBF_CALL(
cbf_set_integerarray_wdims_fs(
cbf,
compression,
table->binary_id,
array,
elem_size,
H5T_SGN_2==h5sign ? 1 : 0,
nelems,
data_byte_order,
table->rank > 1?count[table->rank-1]:0,
table->rank > 2?count[table->rank-2]:0,
table->rank > 3?count[table->rank-3]:0,
0
)
);
} else if (classtype == H5T_FLOAT) {
CBF_CALL(
cbf_set_realarray_wdims_fs(
cbf,
compression,
table->binary_id,
array,
elem_size,
nelems,
data_byte_order,
table->rank > 1?count[table->rank-1]:0,
table->rank > 2?count[table->rank-2]:0,
table->rank > 3?count[table->rank-3]:0,
0
)
);
} else {
cbf_debug_print("Usupported array type");
error |= CBF_NOTIMPLEMENTED;
}
free((void*)array);
/* map the compression to its string */
switch (compression) {
case CBF_CANONICAL: {data_compression = canonical; break;}
case CBF_PACKED: {data_compression = packed; break;}
case CBF_PACKED_V2: {data_compression = packed_v2; break;}
case CBF_BYTE_OFFSET: {data_compression = byte_offset; break;}
case CBF_NIBBLE_OFFSET: {data_compression = nibble_offset; break;}
default: {data_compression = no_compression;}
}
/* generate the encoding string */
while (CBF_SUCCESS==error) {
char * _data_encoding_new = NULL;
const int n = snprintf(
_data_encoding,
_data_encoding_size,
"%ssigned %lu-bit integer",
H5T_SGN_2==h5sign?"":"un",
(unsigned long)8*elem_size
);
/* check if the string was formed successfully */
if (n > -1 && n < (ssize_t)_data_encoding_size) break;
/* otherwise, set its size to the correct size or to the next higher power-of-2 */
if (n > -1) _data_encoding_size = 1+n;
else _data_encoding_size = _data_encoding_size ? 2*_data_encoding_size : 1;
/* reallocate the string */
if (NULL==(_data_encoding_new=realloc(_data_encoding,_data_encoding_size))) {
error |= CBF_ALLOC;
} else {
_data_encoding = _data_encoding_new;
}
}
data_encoding = _data_encoding;
/* debugging */
if (!error && nx->logfile) {
fprintf(nx->logfile,"CBF compression: '%s'\n",data_compression);
fprintf(nx->logfile,"byte_order: '%s'\n",data_byte_order);
fprintf(nx->logfile,"encoding: '%s'\n",data_encoding);
}
}
}
cbf_H5Tfree(native_type);
}
} else {
cbf_debug_print("incorrect data rank");
error |= CBF_H5DIFFERENT;
}
}
cbf_H5Sfree(data_space);
cbf_H5Tfree(data_type);
}
/* write metadata about the data */
if (CBF_SUCCESS==error) {
size_t dimension[3];
const int precedence[] = {3,2,1};
dimension[0] = table->zdim;
dimension[1] = table->ydim;
dimension[2] = table->xdim;
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
/* array_structure category */
CBF_CALL(cbf_require_category(cbf,"array_structure"));
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->array_id));
CBF_CALL(cbf_require_column(cbf,"byte_order"));
CBF_CALL(cbf_set_value(cbf,data_byte_order));
CBF_CALL(cbf_require_column(cbf,"compression_type"));
CBF_CALL(cbf_set_value(cbf,data_compression));
CBF_CALL(cbf_require_column(cbf,"encoding_type"));
CBF_CALL(cbf_set_value(cbf,data_encoding));
/* array_structure_list category */
CBF_CALL(cbf_require_category(cbf,"array_structure_list"));
for (i = 4-table->rank; CBF_SUCCESS==error && i < 3; ++i) {
CBF_CALL(cbf_require_column(cbf,"array_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->array_id));
CBF_CALL(cbf_require_column(cbf,"axis_set_id"));
CBF_CALL(cbf_set_value(cbf,axis_set_id[i]));
CBF_CALL(cbf_require_column(cbf,"dimension"));
CBF_CALL(cbf_set_integervalue(cbf,dimension[i]));
CBF_CALL(cbf_require_column(cbf,"direction"));
CBF_CALL(cbf_set_value(cbf,"increasing"));
/* TODO: check the index & precedence are correct */
CBF_CALL(cbf_require_column(cbf,"index"));
CBF_CALL(cbf_set_integervalue(cbf,precedence[i]));
CBF_CALL(cbf_require_column(cbf,"precedence"));
CBF_CALL(cbf_set_integervalue(cbf,precedence[i]));
}
}
if (CBF_SUCCESS==error) {
const int precedence[] = {3,2,1};
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"array_element_size"));
for (i = 4-table->rank; i < 3; ++i) {
CBF_CALL(cbf_require_column(cbf,"array_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->array_id));
CBF_CALL(cbf_require_column(cbf,"index"));
CBF_CALL(cbf_set_integervalue(cbf,precedence[i]));
CBF_CALL(cbf_require_column(cbf,"size"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",elem_size[i]));
}
}
if (_axis_set_id[0]) cbf_free((void**)&_axis_set_id[0],NULL);
if (_axis_set_id[1]) cbf_free((void**)&_axis_set_id[1],NULL);
if (_axis_set_id[2]) cbf_free((void**)&_axis_set_id[2],NULL);
for (i = 4-(table->rank); i < 3; ++i) {
if (pixel_offset_name[i]) free((void*)pixel_offset_name[i]);pixel_offset_name[i]=NULL;
if (pixel_offset_long_name[i]) free((void*)pixel_offset_long_name[i]);pixel_offset_long_name[i]=NULL;
if (pixel_size_name[i]) free((void*)pixel_size_name[i]);pixel_size_name[i]=NULL;
}
}
cbf_H5Dfree(data);
free((void*)_data_encoding);
}
/*
Do the conversion of metadata in the file.
*/
if (CBF_SUCCESS==error) {
op_data_t op_data_struct;
op_data_struct.nx = nx;
op_data_struct.cbf = cbf;
op_data_struct.table = table;
if (!error && nx->logfile) {
int len;
fputc('\n',nx->logfile);
len = fprintf(nx->logfile,"Converting metadata:\n");
while (--len > 0) fputc('-',nx->logfile);
fputc('\n',nx->logfile);
}
if (H5Literate(entry,H5_INDEX_NAME,H5_ITER_NATIVE,NULL,cbf_write_nx2cbf__entry_op,&op_data_struct)<0) {
cbf_debug_print2("error: failed to iterate over items in the '%s' group\n",entry_name);
error |= CBF_H5ERROR;
}
}
/*
Convert the axes after iterating through the file, when all of them have been found.
TODO:
I need unique names for each axis, so pick an '_n' suffix, with n being some integer,
as an arbitrary scheme to break name collisions. Need to check any colliding items
for the presence of collisions, then check the colliding names for this suffix and
adjust it as needed to get non-colliding names.
TODO:
Extract the data from each axis dataset, storing it in the appropriate place in the
CBF file: 'diffrn_scan_frame_axis' for data; 'axis' for metadata.
*/
if (CBF_SUCCESS == error) {
cbf_axisData_t * const * it = NULL;
cbf_axisData_t * const * const end = table->axisData+table->nAxes;
/* Ensure I have unique names */
for (it = table->axisData; end != it; ++it) {
cbf_axisData_t * const * it2 = NULL;
for (it2 = it, ++it2; end != it2; ++it2) {
if (!strcmp((*it)->name,(*it2)->name)) {
cbf_debug_print("warning: unhandled duplicate axis names found:");
cbf_debug_print2("Duplicated name: %s\n",(*it)->name);
error |= CBF_NOTIMPLEMENTED;
}
}
}
if (!error && nx->logfile) {
int len;
fputc('\n',nx->logfile);
len = fprintf(nx->logfile,"Found %lu %s:\n",(unsigned long)(table->nAxes),table->nAxes != 1 ? "axes" : "axis");
while (--len > 0) fputc('-',nx->logfile);
fputc('\n',nx->logfile);
}
if (!error) { /* Do the conversion to CBF format */
cbf_node * diffrn_measurement_axis = NULL;
cbf_node * diffrn_detector_axis = NULL;
cbf_node * diffrn_scan_axis = NULL;
cbf_node * diffrn_scan_frame_axis = NULL;
cbf_node * axis = NULL;
double McStas2CBF[3][3];
double pgavec[3];
double norm_pgavec;
int ii, jj;
int havepga, havegravity, havesource;
double Q_McStas, U_McStas, psn_McStas;
CBF_CALL(cbf_require_datablock(cbf,table->datablock_id));
CBF_CALL(cbf_require_category(cbf,"diffrn_measurement_axis"));
diffrn_measurement_axis = cbf->node;
CBF_CALL(cbf_require_category(cbf,"diffrn_detector_axis"));
diffrn_detector_axis = cbf->node;
CBF_CALL(cbf_require_category(cbf,"diffrn_scan_axis"));
diffrn_scan_axis = cbf->node;
CBF_CALL(cbf_require_category(cbf,"diffrn_scan_frame_axis"));
diffrn_scan_frame_axis = cbf->node;
CBF_CALL(cbf_require_category(cbf,"axis"));
axis = cbf->node;
/* extract the CBF coordinate system from the data
if there is a primary goniometer axis*/
havepga = havegravity = havesource = 0;
for (it = table->axisData; CBF_SUCCESS==error && end != it; ++it) {
hid_t attr = CBF_H5FAIL;
cbf_axisData_t * const axisData = *it;
if ((axisData->flags&CBF_AXIS_DATA_PGA) == 0) continue;
if (!cbf_H5Ivalid(attr=H5Aopen(axisData->axis,"vector",H5P_DEFAULT))) {
cbf_debug_print("couldn't open attribute");
error |= CBF_H5ERROR;
} else if (CBF_SUCCESS!=((error|=cbf_H5Aread(attr,H5T_NATIVE_DOUBLE,pgavec)))) {
cbf_debug_print(cbf_strerror(error));
} else {
havepga = 1;
}
break;
}
if (havepga) {
double cbfZ[3];
double sourcedotX;
double norm_cbfZ;
norm_pgavec = cbf_norm(pgavec);
if (norm_pgavec < 1.e-38) {
error |= CBF_FORMAT;
cbf_debug_print(cbf_strerror(error));
havepga = 0;
} else {
CBF_CALL(cbf_scalar_product(1./norm_pgavec,pgavec,McStas2CBF[0]));
}
sourcedotX = -pgavec[2];
cbfZ[0] = cbfZ[1] = 0.;
cbfZ[2] = -1;
for (ii=0; ii < 0; ii++) {
cbfZ[ii] = cbfZ[ii]-sourcedotX*pgavec[ii];
}
norm_cbfZ = cbf_norm(cbfZ);
if (norm_cbfZ < 1.e-38) {
error |= CBF_FORMAT;
cbf_debug_print(cbf_strerror(error));
havepga = 0;
} else {
CBF_CALL(cbf_scalar_product(1./norm_cbfZ,cbfZ,McStas2CBF[2]));
}
CBF_CALL(cbf_cross_product(cbfZ,McStas2CBF[0],McStas2CBF[1]));
}
if (!havepga) {
error |= CBF_FORMAT;
cbf_debug_print2("%s, failed to find valid primary goniometer axis\n",
cbf_strerror(error));
for (ii=0;ii<3;ii++) {
for (jj=0;jj<3;jj++) {
McStas2CBF[ii][jj] = (ii==jj)?1.:0.;
}
}
McStas2CBF[0][0] = McStas2CBF[2][2] = -1.;
}
/* At this point, I may have some incompletely processed Stokes vector
information still in the McStas coordinate system. This requires
rotation of the second and third components of the vector.
If esd's were provided, they need to be computed
*/
if (CBF_SUCCESS==error &&
CBF_SUCCESS==cbf_find_category(cbf,"diffrn_radiation") &&
CBF_SUCCESS==cbf_find_column(cbf,"polarizn_source_norm_McStas") &&
CBF_SUCCESS==cbf_get_doublevalue(cbf,&psn_McStas)){
double phi;
phi = atan2(-McStas2CBF[1][0],McStas2CBF[1][1])*45./atan2(1.,1.);
CBF_CALL(cbf_set_column_name(cbf,"polarizn_source_norm"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",psn_McStas-phi));
}
if (CBF_SUCCESS==error &&
CBF_SUCCESS==cbf_find_category(cbf,"diffrn_radiation") &&
CBF_SUCCESS==cbf_find_column(cbf,"polarizn_Stokes_Q_McStas") &&
CBF_SUCCESS==cbf_get_doublevalue(cbf,&Q_McStas) &&
CBF_SUCCESS==cbf_find_column(cbf,"polarizn_Stokes_U_McStas") &&
CBF_SUCCESS==cbf_get_doublevalue(cbf,&U_McStas)){
double phi, Q_CBF, U_CBF, I_McStas;
double Q_McStas_esd, U_McStas_esd, I_McStas_esd;
double Q_CBF_esd, U_CBF_esd;
double psr, psr_esd, psn_esd;
phi = atan2(-McStas2CBF[1][0],McStas2CBF[1][1]);
Q_CBF = cos(2.*phi)*Q_McStas + sin(2.*phi)*U_McStas;
U_CBF = -sin(2.*phi)*Q_McStas + cos(2.*phi)*U_McStas;
CBF_CALL(cbf_find_column(cbf,"polarizn_Stokes_Q_McStas"));
CBF_CALL(cbf_set_column_name(cbf,"polarizn_Stokes_Q"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",Q_CBF));
CBF_CALL(cbf_find_column(cbf,"polarizn_Stokes_U_McStas"));
CBF_CALL(cbf_set_column_name(cbf,"polarizn_Stokes_U"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",U_CBF));
if (CBF_SUCCESS==error &&
CBF_SUCCESS==cbf_find_column(cbf,"polarizn_Stokes_Q_McStas_esd") &&
CBF_SUCCESS==cbf_get_doublevalue(cbf,&Q_McStas_esd) &&
CBF_SUCCESS==cbf_find_column(cbf,"polarizn_Stokes_U_McStas_esd") &&
CBF_SUCCESS==cbf_get_doublevalue(cbf,&U_McStas_esd)){
Q_CBF_esd = sqrt(cos(2.*phi)*Q_McStas_esd*cos(2.*phi)*Q_McStas_esd
+ sin(2.*phi)*U_McStas_esd*sin(2.*phi)*U_McStas_esd);
U_CBF_esd = sqrt(sin(2.*phi)*Q_McStas_esd*sin(2.*phi)*Q_McStas_esd
+ cos(2.*phi)*U_McStas_esd*cos(2.*phi)*U_McStas_esd);
CBF_CALL(cbf_find_column(cbf,"polarizn_Stokes_Q_McStas_esd"));
CBF_CALL(cbf_set_column_name(cbf,"polarizn_Stokes_Q_esd"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",Q_CBF_esd));
CBF_CALL(cbf_find_column(cbf,"polarizn_Stokes_U_McStas_esd"));
CBF_CALL(cbf_set_column_name(cbf,"polarizn_Stokes_U_esd"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",U_CBF_esd));
if (CBF_SUCCESS == error &&
CBF_SUCCESS==cbf_find_column(cbf,"polarizn_Stokes_I_McStas") &&
CBF_SUCCESS==cbf_get_doublevalue(cbf,&I_McStas) &&
CBF_SUCCESS==cbf_find_column(cbf,"polarizn_Stokes_I_McStas_esd") &&
CBF_SUCCESS==cbf_get_doublevalue(cbf,&I_McStas_esd) &&
Q_McStas*Q_McStas+U_McStas*U_McStas > 0. &&
fabs(I_McStas)> 0.) {
psr = sqrt(Q_McStas*Q_McStas+U_McStas*U_McStas)/fabs(I_McStas);
psn_esd = ((22.5/atan2(1.,1.))*
sqrt(Q_McStas*Q_McStas_esd*Q_McStas*Q_McStas_esd+
U_McStas*U_McStas_esd*U_McStas*U_McStas_esd)/
(Q_McStas*Q_McStas+U_McStas*U_McStas));
psr_esd = ((1./I_McStas)*(1./I_McStas)*
sqrt(Q_McStas*Q_McStas_esd*Q_McStas*Q_McStas_esd+
U_McStas*U_McStas_esd*U_McStas*U_McStas_esd+
psr*I_McStas_esd*psr*I_McStas_esd));
CBF_CALL(cbf_require_column(cbf,"polarizn_source_norm_esd"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",psn_esd));
CBF_CALL(cbf_require_column(cbf,"polarizn_source_ratio"));
CBF_CALL(cbf_set_doublevalue(cbf,"%-.15g",psr_esd));
}
}
}
/* NOTE: this loop writes data to several tables simultaneously */
for (it = table->axisData; CBF_SUCCESS==error && end != it; ++it) {
cbf_axisData_t * const axisData = *it;
unsigned int axisRow = 0;
if (CBF_SUCCESS==error) {
/* add a new row & convert the axis name */
cbf->node = axis;
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
axisRow = cbf->row;
CBF_CALL(cbf_set_value(cbf,axisData->name));
if (!error && !cbf_cistrcmp(axisData->name,"GRAVITY")) havegravity = 1;
if (!error && !cbf_cistrcmp(axisData->name,"SOURCE")) havesource = 1;
if (!error && nx->logfile) fprintf(nx->logfile,"%s\n",axisData->name);
}
if (CBF_SUCCESS==error) {
/* set the 'equipment' & add to any mapping tables for detectors & goniometers */
CBF_CALL(cbf_require_column(cbf,"equipment"));
if (axisEquipment_detector==axisData->equipment) {
const int row = cbf->row;
if (!error && nx->logfile) fprintf(nx->logfile,"Equipment: detector\n");
CBF_CALL(cbf_set_value(cbf,"detector"));
cbf->node = diffrn_detector_axis;
CBF_CALL(cbf_require_column(cbf,"detector_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,table->diffrn_detector_id));
CBF_CALL(cbf_require_column(cbf,"axis_id"));
CBF_CALL(cbf_set_value(cbf,axisData->name));
cbf->node = axis;
cbf->row = row;
} else if (axisEquipment_goniometer==axisData->equipment) {
const int row = cbf->row;
if (!error && nx->logfile) fprintf(nx->logfile,"Equipment: goniometer\n");
CBF_CALL(cbf_set_value(cbf,"goniometer"));
cbf->node = diffrn_measurement_axis;
CBF_CALL(cbf_require_column(cbf,"measurement_id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,diffrn_measurement_id));
CBF_CALL(cbf_require_column(cbf,"axis_id"));
CBF_CALL(cbf_set_value(cbf,axisData->name));
cbf->node = axis;
cbf->row = row;
} else if (axisEquipment_image==axisData->equipment) {
if (!error && nx->logfile) fprintf(nx->logfile,"Equipment: image\n");
CBF_CALL(cbf_set_value(cbf,"detector"));
} else if (axisEquipment_gravity==axisData->equipment) {
if (!error && nx->logfile) fprintf(nx->logfile,"Equipment: gravity\n");
/* CBF_CALL(cbf_set_value(cbf,"gravity")); */
CBF_CALL(cbf_set_value(cbf,"general"));
} else if (axisEquipment_source==axisData->equipment) {
if (!error && nx->logfile) fprintf(nx->logfile,"Equipment: source\n");
/* CBF_CALL(cbf_set_value(cbf,"source")); */
CBF_CALL(cbf_set_value(cbf,"general"));
} else {
if (!error && nx->logfile) fprintf(nx->logfile,"Equipment: general\n");
CBF_CALL(cbf_set_value(cbf,"general"));
}
}
if (CBF_SUCCESS==error) { /* record the name of the axis that this depends on */
cbf_axisData_t * const depends_on = axisData->depends_on;
CBF_CALL(cbf_require_column(cbf,"depends_on"));
if (depends_on) {
if (!error && nx->logfile) fprintf(nx->logfile,"Depends on: %s\n",depends_on->name);
CBF_CALL(cbf_set_value(cbf,depends_on->name));
} else CBF_CALL(cbf_set_value(cbf,"."));
}
if (CBF_SUCCESS==error) { /* extract the transformation type */
hid_t attr = CBF_H5FAIL;
const char * type = NULL;
if (!cbf_H5Ivalid(attr=H5Aopen(axisData->axis,"transformation_type",H5P_DEFAULT))) {
cbf_debug_print("couldn't open attribute");
error |= CBF_H5ERROR;
} else if (CBF_SUCCESS!=((error|=cbf_H5Aread_string(attr,&type)))) {
cbf_debug_print(cbf_strerror(error));
} else {
CBF_CALL(cbf_require_column(cbf,"type"));
if (!strcmp(type,"translation")) {
hsize_t off[1];
const hsize_t cnt[] = {1};
double val;
off[0] = nx->slice;
if (!error && nx->logfile) fprintf(nx->logfile,"Type: translation\n");
CBF_CALL(cbf_set_value(cbf,"translation"));
if (axisEquipment_image!=axisData->equipment) {
if (CBF_SUCCESS!=(error|=cbf_H5Dread2(axisData->axis,off,0,cnt,&val,H5T_NATIVE_DOUBLE))) {
cbf_debug_print(cbf_strerror(error));
} else {
cbf->node = diffrn_scan_frame_axis;
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_require_column(cbf,"axis_id"));
CBF_CALL(cbf_set_value(cbf,axisData->name));
CBF_CALL(cbf_require_column(cbf,"frame_id"));
CBF_CALL(cbf_set_value(cbf,table->frame_id));
CBF_CALL(cbf_require_column(cbf,"angle"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",0.0));
CBF_CALL(cbf_require_column(cbf,"displacement"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",val));
}
}
} else if (!strcmp(type,"rotation")) {
hsize_t off[1];
const hsize_t cnt[] = {1};
double val;
off[0] = nx->slice;
if (!error && nx->logfile) fprintf(nx->logfile,"Type: rotation\n");
CBF_CALL(cbf_set_value(cbf,"rotation"));
if (axisEquipment_image!=axisData->equipment) {
if (CBF_SUCCESS!=(error|=cbf_H5Dread2(axisData->axis,off,0,cnt,&val,H5T_NATIVE_DOUBLE))) {
cbf_debug_print(cbf_strerror(error));
} else {
cbf->node = diffrn_scan_frame_axis;
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_require_column(cbf,"axis_id"));
CBF_CALL(cbf_set_value(cbf,axisData->name));
CBF_CALL(cbf_require_column(cbf,"frame_id"));
CBF_CALL(cbf_set_value(cbf,table->frame_id));
CBF_CALL(cbf_require_column(cbf,"angle"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",val));
CBF_CALL(cbf_require_column(cbf,"displacement"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",0.0));
}
}
} else {
if (!error && nx->logfile) fprintf(nx->logfile,"Type: general\n");
CBF_CALL(cbf_set_value(cbf,"general"));
}
cbf->node = axis;
cbf->row = axisRow;
}
free((void*)type);
cbf_H5Afree(attr);
}
if (CBF_SUCCESS==error) { /* extract the vector */
hid_t attr = CBF_H5FAIL;
double vec[3];
if (!cbf_H5Ivalid(attr=H5Aopen(axisData->axis,"vector",H5P_DEFAULT))) {
cbf_debug_print("couldn't open attribute");
error |= CBF_H5ERROR;
} else if (CBF_SUCCESS!=((error|=cbf_H5Aread(attr,H5T_NATIVE_DOUBLE,vec)))) {
cbf_debug_print(cbf_strerror(error));
} else {
double vector[3];
CBF_CALL(cbf_apply_matrix(McStas2CBF,vec,vector));
if (!error && nx->logfile) fprintf(nx->logfile,"Vector: [%g, %g, %g]\n",
vector[0],vector[1],vector[2]);
CBF_CALL(cbf_require_column(cbf,"vector[1]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",vector[0]));
CBF_CALL(cbf_require_column(cbf,"vector[2]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",vector[1]));
CBF_CALL(cbf_require_column(cbf,"vector[3]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",vector[2]));
}
cbf_H5Afree(attr);
}
/* extract the offset */
if (!error) {
htri_t has_offset = H5Aexists(axisData->axis,"offset");
double off[3] = {0.,0.,0.};
double offset[3];
if (has_offset<=0) {
cbf_debug_print("offset attribute not found, treating as [0,0,0]");
offset[0] = offset[1] = offset[2] = 0.0;
} else if (has_offset>0) {
hid_t Aoffset = CBF_H5FAIL;
if (!cbf_H5Ivalid(Aoffset=H5Aopen(axisData->axis,"offset",H5P_DEFAULT))) {
cbf_debug_print("couldn't open offset attribute");
error |= CBF_H5ERROR;
} else if ((error|=cbf_H5Aread(Aoffset,H5T_NATIVE_DOUBLE,off))) {
cbf_debug_print("couldn't read attribute");
} else {
int found = CBF_SUCCESS;
const char * unit_string = NULL;
hid_t units = CBF_H5FAIL;
CBF_CALL(cbf_apply_matrix(McStas2CBF,off,offset));
found = cbf_H5Afind(axisData->axis,&units,"offset_units",CBF_H5FAIL,CBF_H5FAIL);
if (CBF_NOTFOUND==found) {
/*
No 'offset_units' specified, can use 'units' iff 'type' is "translation".
TODO: check the transformation type here
*/
CBF_CALL(cbf_H5Afind(axisData->axis,&units,"units",CBF_H5FAIL,CBF_H5FAIL));
} else if (found) {
cbf_debug_print(cbf_strerror(found));
error |= found;
}
CBF_CALL(cbf_H5Aread_string(units,&unit_string));
if (!error) {
/* need a conversion factor to apply to the offset before storing it */
double factor = 0./0.;
if (CBF_SUCCESS!=(error|=cbf_scale_units(unit_string,"mm",&factor))) {
cbf_debug_print(cbf_strerror(error));
} else {
int i;
if (!error && nx->logfile) fprintf(nx->logfile,"Offset: [%g, %g, %g]\n",offset[0]*factor,offset[1]*factor,offset[2]*factor);
for (i = 0; i != 3; ++i) offset[i] *= factor;
}
}
cbf_H5Afree(units);
free((void*)unit_string);
}
cbf_H5Afree(Aoffset);
}
if (!error) {
CBF_CALL(cbf_require_column(cbf,"offset[1]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",offset[0]));
CBF_CALL(cbf_require_column(cbf,"offset[2]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",offset[1]));
CBF_CALL(cbf_require_column(cbf,"offset[3]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",offset[2]));
}
}
if (!error && nx->logfile) fputc('\n',nx->logfile);
}
/* all real axes converted, add the gravity & beam axes to
fully specify the coordinate system for future McStas conversions*/
if (!havegravity) {
double gravity[3] = {0.,-1.,0.};
double cbf_gravity[3];
CBF_CALL(cbf_apply_matrix(McStas2CBF,gravity,cbf_gravity));
CBF_CALL(cbf_require_category(cbf,"axis"));
/* gravity: */
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,"GRAVITY"));
CBF_CALL(cbf_require_column(cbf,"equipment"));
CBF_CALL(cbf_set_value(cbf,"gravity"));
CBF_CALL(cbf_require_column(cbf,"type"));
CBF_CALL(cbf_set_value(cbf,"general"));
CBF_CALL(cbf_require_column(cbf,"depends_on"));
CBF_CALL(cbf_set_value(cbf,"."));
CBF_CALL(cbf_require_column(cbf,"vector[1]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",cbf_gravity[0]));
CBF_CALL(cbf_require_column(cbf,"vector[2]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",cbf_gravity[1]));
CBF_CALL(cbf_require_column(cbf,"vector[3]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",cbf_gravity[2]));
CBF_CALL(cbf_require_column(cbf,"offset[1]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",0.));
CBF_CALL(cbf_require_column(cbf,"offset[2]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",0.));
CBF_CALL(cbf_require_column(cbf,"offset[3]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",0.));
}
if (!havesource) {
double source[3] = {0.,0.,-1.};
double cbf_source[3];
CBF_CALL(cbf_apply_matrix(McStas2CBF,source,cbf_source));
CBF_CALL(cbf_require_category(cbf,"axis"));
/* beam: */
CBF_CALL(cbf_require_column(cbf,"id"));
CBF_CALL(cbf_new_row(cbf));
CBF_CALL(cbf_set_value(cbf,"SOURCE"));
CBF_CALL(cbf_require_column(cbf,"equipment"));
CBF_CALL(cbf_set_value(cbf,"source"));
CBF_CALL(cbf_require_column(cbf,"type"));
CBF_CALL(cbf_set_value(cbf,"general"));
CBF_CALL(cbf_require_column(cbf,"depends_on"));
CBF_CALL(cbf_set_value(cbf,"."));
CBF_CALL(cbf_require_column(cbf,"vector[1]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",cbf_source[0]));
CBF_CALL(cbf_require_column(cbf,"vector[2]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",cbf_source[1]));
CBF_CALL(cbf_require_column(cbf,"vector[3]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",cbf_source[2]));
CBF_CALL(cbf_require_column(cbf,"offset[1]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",0.));
CBF_CALL(cbf_require_column(cbf,"offset[2]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",0.));
CBF_CALL(cbf_require_column(cbf,"offset[3]"));
CBF_CALL(cbf_set_doublevalue(cbf,"%.15g",0.));
}
}
}
/* free the key */
_cbf_free_nx2cbf_key(table);
/*
Permute the CBF tree:
The data should be written last, but the writer currently writes in order of creation.
Columns should be sorted in alphabetical order in all tables, tables should be sorted
in alphabetical order except for 'array_data' which should be last.
*/
if (CBF_SUCCESS==error) {
if (CBF_SUCCESS!=(error|=cbf_rewind_datablock(cbf))) {
cbf_debug_print(cbf_strerror(error));
} else {
cbf_node * const db = cbf->node;
cbf_node * const * const cat_end = db->child + db->children;
cbf_node * const * pcat;
/* sort each table */
for (pcat = db->child; cat_end != pcat; ++pcat) {
cbf_node * const cat = *pcat;
if (!strcmp(cat->name,"array_data")) qsort(cat->child, cat->children, sizeof(void*), cmp_arraydata);
else qsort(cat->child, cat->children, sizeof(void*), cmp_column);
}
/* sort the tables */
qsort(db->child, db->children, sizeof(void*), cmp_category);
}
}
free((void*)_array_id);
}
return error;
}
/*
Find a suitable HDF5 datatype for the given parameters.
*/
static int cbf_find_array_data_h5type
(hid_t * const type,
unsigned int bits,
int sign,
int real,
const char *byteorder)
{
int error = CBF_SUCCESS;
cbf_debug_print("find_array_data_h5type\n");
/* check arguments */
if (!type) {
cbf_debug_print("Invalid type pointer given\n");
return CBF_ARGUMENT;
}
if (!byteorder) {
cbf_debug_print("No byte order given\n");
return CBF_ARGUMENT;
}
if (real) {
int order;
order = ('l'==*byteorder||'L'==*byteorder) ? 0 : 1;
if (!sign) error |= CBF_FORMAT;
else {
hid_t tbl[2][2];
unsigned int idx = 2;
tbl[0][0] = H5T_IEEE_F32LE;
tbl[0][1] = H5T_IEEE_F64LE;
tbl[1][0] = H5T_IEEE_F32BE;
tbl[1][1] = H5T_IEEE_F64BE;
idx = bits<=64 ? 1 : idx;
idx = bits<=32 ? 0 : idx;
if (idx > 1) error |= CBF_FORMAT;
else *type = tbl[order][idx];
}
} else {
const int order = ('l'==*byteorder||'L'==*byteorder) ? 0 : 1;
/* define a lookup table for integer types */
hid_t tbl[2][2][4];
unsigned int idx = 4;
tbl[0][0][0] = H5T_STD_I8LE;
tbl[0][0][1] = H5T_STD_I16LE;
tbl[0][0][2] = H5T_STD_I32LE;
tbl[0][0][3] = H5T_STD_I64LE;
tbl[0][1][0] = H5T_STD_U8LE;
tbl[0][1][1] = H5T_STD_U16LE;
tbl[0][1][2] = H5T_STD_U32LE;
tbl[0][1][3] = H5T_STD_U64LE;
tbl[1][0][0] = H5T_STD_I8BE;
tbl[1][0][1] = H5T_STD_I16BE;
tbl[1][0][2] = H5T_STD_I32BE;
tbl[1][0][3] = H5T_STD_I64BE;
tbl[1][1][0] = H5T_STD_U8BE;
tbl[1][1][1] = H5T_STD_U16BE;
tbl[1][1][2] = H5T_STD_U32BE;
tbl[1][1][3] = H5T_STD_U64BE;
/* select a value from the table */
idx = bits<=64 ? 3 : idx;
idx = bits<=32 ? 2 : idx;
idx = bits<=16 ? 1 : idx;
idx = bits<=8 ? 0 : idx;
if (idx > 3) error |= CBF_FORMAT;
else *type = tbl[order][sign?0:1][idx];
}
return error;
}
/*
Decompress the data selected in the handle, ensure an appropriate HDF5 dataset exists to store it,
insert it a the given index with some parameter values set according to the given flags.
Writes saturation_value and data to h5handle->nxdetector
*/
static int cbf_write_array_h5file(const cbf_node * node,
const unsigned int row,
cbf_h5handle h5handle,
const char * const saturation_value,
const char * const undefined_value,
hsize_t * dims)
{
int error = CBF_SUCCESS;
cbf_debug_print("write_array_h5file\n");
if (!node) {
cbf_debug_print("Invalid node given\n");
error |= CBF_ARGUMENT;
} else if (!h5handle) {
cbf_debug_print("Invalid hdf5 handle given\n");
error |= CBF_ARGUMENT;
} else {
int found = CBF_SUCCESS, id, bits, sign, real;
cbf_file *file;
long start;
size_t size, nelem, cbfdim[3], padding;
const char * byteorder;
unsigned int compression;
hid_t h5type = CBF_H5FAIL;
/* find the datatype and array size */
CBF_CALL(cbf_get_bintext(node, row, NULL,
&id, &file, &start, &size,
NULL, NULL, &bits, &sign, &real,
&byteorder, &nelem, cbfdim+2, cbfdim+1, cbfdim+0, &padding,
&compression));
CBF_CALL(cbf_find_array_data_h5type(&h5type,bits,sign,real,byteorder));
if (dims) {
dims[0] = 0;
dims[1] = cbfdim[1];
dims[2] = cbfdim[2];
}
/* check the saturation value */
if (saturation_value) {
hid_t dataset = CBF_H5FAIL;
hsize_t max[] = {H5S_UNLIMITED};
hsize_t cnk[] = {1};
hsize_t off[1];
hsize_t cnt[] = {1};
hsize_t buf[] = {0};
off[0] = h5handle->slice;
CBF_CALL(cbf_H5Drequire(h5handle->nxdetectors[h5handle->cur_detector],&dataset,"saturation_value",1,max,cnk,buf,h5type));
if (real) {
/* every float can be represented exactly by a double, so no need for float intermediate */
const double num = strtod(saturation_value,0);
CBF_CALL(cbf_H5Dinsert(dataset,off,0,cnt,buf,&num,H5T_NATIVE_DOUBLE));
} else {
if (sign) {
/* use longest signed integer: all smaller types represent a subset of these values */
const signed long num = strtol(saturation_value,0,10);
CBF_CALL(cbf_H5Dinsert(dataset,off,0,cnt,buf,&num,H5T_NATIVE_LONG));
} else {
/* use longest unsigned integer: all smaller types represent a subset of these values */
const unsigned long num = strtoul(saturation_value,0,10);
CBF_CALL(cbf_H5Dinsert(dataset,off,0,cnt,buf,&num,H5T_NATIVE_ULONG));
}
}
cbf_H5Dfree(dataset);
}
/* check the undefined value */
if (undefined_value) {
hid_t dataset = CBF_H5FAIL;
hsize_t max[] = {H5S_UNLIMITED};
hsize_t cnk[] = {1};
hsize_t off[1];
hsize_t cnt[] = {1};
hsize_t buf[] = {0};
off[0] = h5handle->slice;
CBF_CALL(cbf_H5Drequire(h5handle->nxdetectors[h5handle->cur_detector],&dataset,"undefined_value",1,max,cnk,buf,h5type));
if (real) {
/* every float can be represented exactly by a double, so no need for float intermediate */
const double num = strtod(undefined_value,0);
CBF_CALL(cbf_H5Dinsert(dataset,off,0,cnt,buf,&num,H5T_NATIVE_DOUBLE));
} else {
if (sign) {
/* use longest signed integer: all smaller types represent a subset of these values */
const signed long num = strtol(undefined_value,0,10);
CBF_CALL(cbf_H5Dinsert(dataset,off,0,cnt,buf,&num,H5T_NATIVE_LONG));
} else {
/* use longest unsigned integer: all smaller types represent a subset of these values */
const unsigned long num = strtoul(undefined_value,0,10);
CBF_CALL(cbf_H5Dinsert(dataset,off,0,cnt,buf,&num,H5T_NATIVE_ULONG));
}
}
cbf_H5Dfree(dataset);
}
/* allocate space for the decompressed data */
if (CBF_SUCCESS == error) {
void * value;
const unsigned int elsize = (bits+7)/8;
size_t nelem_read = 0;
const int rank = 3;
hsize_t buf[] = {0, 0, 0};
hsize_t h5dim[3];
hsize_t h5max[3];
hsize_t h5chunk[3];
hid_t dset = CBF_H5FAIL;
h5dim[0] = 0;
h5chunk[0] = 1;
h5max[0] = H5S_UNLIMITED;
h5dim[1] = h5chunk[1] = h5max[1] = cbfdim[1];
h5dim[2] = h5chunk[2] = h5max[2] = cbfdim[2];
value = malloc(nelem*elsize);
CBF_CALL(cbf_set_fileposition(file, start, SEEK_SET));
CBF_CALL(cbf_decompress_parameters(NULL, NULL, NULL, NULL, NULL, NULL, NULL, compression, file));
{
cbf_debug_print2("masked compression: %d\n",compression&CBF_COMPRESSION_MASK);
cbf_debug_print("compression type: ");
if (compression == CBF_CANONICAL) {
cbf_debug_print("CBF_CANONICAL\n");
} else if ((compression&CBF_COMPRESSION_MASK) == CBF_PACKED){
cbf_debug_print("CBF_PACKED\n");
} else if ((compression&CBF_COMPRESSION_MASK) == CBF_PACKED_V2) {
cbf_debug_print("CBF_PACKED_V2\n");
} else if (compression == CBF_BYTE_OFFSET) {
cbf_debug_print("CBF_BYTE_OFFSET\n");
} else if (compression == CBF_NIBBLE_OFFSET) {
cbf_debug_print("CBF_NIBBLE_OFFSET\n");
} else if (compression == CBF_PREDICTOR) {
cbf_debug_print("CBF_PREDICTOR\n");
} else if (compression == CBF_NONE) {
cbf_debug_print("CBF_NONE\n");
} else {
cbf_debug_print("Unknown\n");
}
cbf_debug_print2("element size: %d\n",(unsigned int)(elsize));
cbf_debug_print2("real?: %s\n",real?"yes":"no");
}
/* ensure a dataset exists in the detector */
found = cbf_H5Dfind2(h5handle->nxdetectors[h5handle->cur_detector],
&dset,"data",rank,h5max,buf,h5type);
if (CBF_SUCCESS==found) {
} else if (CBF_NOTFOUND==found) {
/* define variables & check args */
hid_t dataSpace = CBF_H5FAIL;
hid_t valprop = H5Pcreate(H5P_DATASET_CREATE);
/* check variables are valid */
CBF_CALL(cbf_H5Screate(&dataSpace, rank, h5dim, h5max));
/* allow dataset to be chunked */
CBF_H5CALL(H5Pset_chunk(valprop,rank,h5chunk));
/* allow compression */
if (CBF_SUCCESS==error) {
if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_ZLIB) {
H5Pset_deflate(valprop, 1);
} else if (h5handle->flags & CBF_H5COMPRESSION_CBF) {
unsigned int cd_values[CBF_H5Z_FILTER_CBF_NELMTS];
cd_values[CBF_H5Z_FILTER_CBF_COMPRESSION] =
h5handle->flags &
(CBF_COMPRESSION_MASK|CBF_FLAG_MASK) &
(~(CBF_H5COMPRESSION_CBF));
cd_values[CBF_H5Z_FILTER_CBF_RESERVED] = 0;
cd_values[CBF_H5Z_FILTER_CBF_BINARY_ID] = id;
cd_values[CBF_H5Z_FILTER_CBF_PADDING] = padding;
cd_values[CBF_H5Z_FILTER_CBF_ELSIZE] = elsize;
cd_values[CBF_H5Z_FILTER_CBF_ELSIGN] = sign;
cd_values[CBF_H5Z_FILTER_CBF_REAL] = real;
cd_values[CBF_H5Z_FILTER_CBF_DIMFAST] = *(h5chunk+2);
cd_values[CBF_H5Z_FILTER_CBF_DIMMID] = *(h5chunk+1);
cd_values[CBF_H5Z_FILTER_CBF_DIMSLOW] = *(h5chunk+0);
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_CBF)) {
cbf_h5reportneg(H5Zregister(CBF_H5Z_CBF),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_CBF (32006) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_CBF, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_CBF_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_CBF %d\n",error);
}
}
#ifdef CBF_H5Z_USE_LZ4
else if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4 ||
(h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4_2) {
unsigned int cd_values[CBF_H5Z_FILTER_LZ4_NELMTS];
cd_values[CBF_H5Z_FILTER_LZ4_BLOCKSIZE] = 1<<30;
cd_values[CBF_H5Z_FILTER_LZ4_THREADS] = 1;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_LZ4)) {
cbf_h5reportneg(H5Zregister(H5Z_LZ4),CBF_H5ERROR,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_LZ4 (32004) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_LZ4, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_LZ4_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_LZ4 %d\n",error);
}
if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_LZ4_2) {
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_LZ4, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_LZ4_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_LZ4 %d\n",error);
}
}
}
#endif
#ifdef CBF_H5Z_USE_BSHUF
else if ((h5handle->flags & CBF_COMPRESSION_MASK) == CBF_H5COMPRESSION_BSLZ4 ) {
unsigned int cd_values[CBF_H5Z_FILTER_BSHUF_NELMTS];
cd_values[CBF_H5Z_FILTER_BSHUF_BLOCKSIZE] = 8192/elsize;
cd_values[CBF_H5Z_FILTER_BSHUF_COMPRESSION] =
CBF_H5Z_FILTER_BSHUF_LZ4;
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_BSHUF)) {
cbf_h5reportneg(H5Zregister(bshuf_H5Filter),CBF_H5ERROR ,error);
if (error) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_BSHUF (32008) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
cbf_h5reportneg(H5Pset_filter(valprop, CBF_H5Z_FILTER_BSHUF, /* H5Z_FLAG_OPTIONAL*/0, CBF_H5Z_FILTER_BSHUF_NELMTS, cd_values),CBF_H5ERROR,error);
if (error) {
cbf_debug_print2("error on setting filter CBF_H5Z_FILTER_BSHUF %d\n",error);
}
}
}
#endif
}
/* create the dataset */
if (CBF_SUCCESS == error)
dset = H5Dcreate2(h5handle->nxdetectors[h5handle->cur_detector],"data",h5type,dataSpace,H5P_DEFAULT,valprop,H5P_DEFAULT);
/* check local variables are properly closed */
if (cbf_H5Ivalid(dataSpace)) H5Sclose(dataSpace);
if (cbf_H5Ivalid(valprop)) H5Pclose(valprop);
} else {
error |= found;
cbf_debug_print2("error locating primary dataset: %s\n", cbf_strerror(found));
}
if (CBF_SUCCESS==error) {
hsize_t h5offset[3];
const int sig[] = {1};
int sigbuf[] = {0};
h5offset[0] = h5handle->slice;
h5offset[1] = h5offset[2] = 0;
/* extract the image data from CBF */
CBF_CALL(cbf_decompress(value, elsize, sign, nelem, &nelem_read,
size, compression, bits, sign, file, real, byteorder,
nelem, cbfdim[2], cbfdim[1], cbfdim[0], padding));
if (nelem_read != nelem) error |= CBF_ENDOFDATA;
/* store the image data in HDF5 */
CBF_CALL(cbf_H5Dinsert(dset,h5offset,0,h5chunk,buf,value,h5type));
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"signal",0,0,H5T_STD_I32LE,H5T_NATIVE_INT,sig,sigbuf,cmp_int,0));
cbf_H5Dfree(dset);
free((void*)value);
}
}
}
return error;
}
/*
A primary key may be an integer or a string, I need to allow them both to be compared
for equality. This function must return 0 if the keys are considered equal, and
non-zero otherwise. Note that this means 'strcmp' is a valid comparison function.
*/
typedef int (*key_cmp_func)(const char *, const char *);
/* Define the signature of a function to extract a value from a 'cbf_cbf2nx_key_t' object. */
typedef const char * (*key_get_value_func)(const cbf_cbf2nx_key_t * const);
/*
Each key is defined by a column name. A method of comparing values for equality
and value to compare against allows rows to be discarded if not relevant.
*/
typedef struct cbf_primary_key_t
{
/* a unique identifier */
const char * name;
/* parameterised behaviour */
key_get_value_func getValue;
key_cmp_func cmp;
} cbf_primary_key_t;
/*
count the number of valid keys that are pointed to by 'begin'.
*/
static size_t cbf_count_primary_keys
(const cbf_primary_key_t * begin)
{
if (!begin) {
return 0;
} else {
const cbf_primary_key_t * end = begin;
while (end->name) ++end;
return end-begin;
}
}
/*
Test if an array of pointers of given length contains a given pointer.
If an invalid (null) array is given then it contains nothing.
Returns non-zero if the pointer 'value' is in the array, zero otherwise.
*/
static int array_contains
(const void * const * const begin,
const size_t size,
const void * const value)
{
if (!begin) {
return 0;
} else {
const void * const * it;
const void * const end = size+begin;
for (it = begin; end != it; ++it)
if (value == *it)
return 1;
}
return 0;
}
/*
Locate the column for each primary key of the category, and cache both the column and its requested value for later use.
*/
static int cbf_populate_primaryKey_cache
(cbf_primary_key_t * const primary_keys,
cbf_node * * * const primary_key_column,
const char * * * const primary_key_value,
const cbf_cbf2nx_key_t * const key,
cbf_node * const category)
{
int error = CBF_SUCCESS;
if (!primary_keys || !primary_key_column || !key || !category) {
error |= CBF_ARGUMENT;
} else if (!(*primary_key_column=malloc(cbf_count_primary_keys(primary_keys)*sizeof(cbf_node*)))) {
error |= CBF_ALLOC;
} else if (!(*primary_key_value=malloc(cbf_count_primary_keys(primary_keys)*sizeof(const char*)))) {
error |= CBF_ALLOC;
} else {
cbf_primary_key_t * it;
for (it = primary_keys; !error && it->name; ++it) {
int found;
/* reset the values in the cache */
(*primary_key_column)[it-primary_keys] = NULL;
/* attempt to set the column */
found = cbf_find_child((*primary_key_column)+(it-primary_keys), category, it->name);
if (CBF_NOTFOUND==found) {
cbf_debug_print3("warning: '%s.%s' not found\n",category->name,it->name);
(*primary_key_column)[it-primary_keys] = NULL;
} else if (CBF_SUCCESS!=found) {
if (1) {
cbf_debug_print3("error: problem finding '%s.%s'\n",category->name,it->name);
cbf_debug_print2("error: %s\n",cbf_strerror(found));
}
error |= found;
}
/* cache the values of primary keys */
if (it->getValue) (*primary_key_value)[it-primary_keys] = it->getValue(key);
else (*primary_key_value)[it-primary_keys] = NULL;
}
}
return error;
}
/*
Extract a set of matching rows from the given primary keys of a cbf category, looking up rows in the cached
columns and comparing data to the cached values.
Occasionally, a malformed file will be missing a column in a different table that defines the set of valid
values for a foreign key in the current table. In this case all values for the degenerate row must compare equal
within the matching row set that would be obtained if the degenerate key was ignored. If the values of such a
column were allowed to differ then this would match columns that don't belong to the same item in the parent
table.
*/
static int cbf_get_matching_rows
(cbf_primary_key_t * const primary_keys,
cbf_node * const * const primary_key_column,
const char * const * const primary_key_value,
const unsigned int nRows,
unsigned int * * const row_set,
unsigned int * const mRows)
{
int error = CBF_SUCCESS;
if (!primary_keys || !primary_key_column || !row_set || !mRows) {
error |= CBF_ARGUMENT;
} else {
if (!(*row_set=malloc(sizeof(unsigned int)*nRows))) {
error |= CBF_ALLOC;
} else {
unsigned int row;
cbf_primary_key_t * it;
*mRows = 0;
/* use well-defined keys to extract a set of matching rows */
for (row = 0; !error && row != nRows; ++row) {
int row_matches = 1;
cbf_primary_key_t * it;
for (it = primary_keys; !error && it->name; ++it) {
cbf_node * const column = primary_key_column[it-primary_keys];
const char * const value = primary_key_value[it-primary_keys];
if (column && value) {
const char * val = NULL;
if ((error|=cbf_node_get_value(column, row, &val))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
if (it->cmp(value,val)) row_matches = 0;
}
}
}
if (row_matches) (*row_set)[(*mRows)++] = row;
}
if (*mRows) {
/* check that values given for degenerate keys are consistent within the set */
for (it = primary_keys; !error && it->name; ++it) {
cbf_node * const column = primary_key_column[it-primary_keys];
const char * const value = primary_key_value[it-primary_keys];
if (column && !value) {
const char * ref = NULL;
/* 'mRows' is unsigned and non-zero, so 'rows[0]' is valid and starting iterations from '1' is safe */
CBF_CALL(cbf_node_get_value(column, (*row_set)[0], &ref));
for (row = 1; !error && row != *mRows; ++row) {
const char * val = NULL;
CBF_CALL(cbf_node_get_value(column, (*row_set)[row], &val));
if (!error && it->cmp(ref,val)) {
cbf_debug_print2("error: inconsistent values for degenerate key '%s'\n",it->name);
error |= CBF_FORMAT;
}
}
}
}
}
}
}
return error;
}
/* typedef to match the signature of the 'cbf_h5handle_require_xyzzy' functions */
typedef int (*get_h5group_func)(cbf_h5handle, hid_t *, const char *);
/*
Conversion filter function signatures for various kinds of data:
- strings should be converted by producing a new string that should be free'd.
- numbers should be converted via a '*out = f(in)' process.
- a NULL function pointer should indicate that no conversion filter is required.
*/
typedef void (*filter_func)( void );
typedef int (*filter_string_func)(const char * const in, const char * * const out);
typedef int (*filter_double_func)(const double in, double * const out);
/* conversion function for inhomogeneity, to introduce a factor of 2 in the conversion */
static int filter_inhomogeneity
(const double in,
double * const out)
{
int error = CBF_SUCCESS;
if (!out) {
error |= CBF_ARGUMENT;
} else {
*out = 2.0*in;
}
return error;
}
/*
Define the possible mapping types, which will affect how a pointer is dereferenced later on.
*/
enum mapping_type {
/* ignore this item - no mapping wanted */
CBF_MAP_NONE,
/* convert to a nexus field */
CBF_MAP_DATA,
CBF_MAP_DATA_DEPRECATED,
/* extract a key value */
CBF_MAP_KEY,
/*
Store data from a column so it can be used via a callback function
once all columns in the current row have been processed.
*/
CBF_MAP_CACHE
};
/*
A list of parameters to pass to a hypothetical 'cbf_nexus_dataset_with_units_require_cmp'
function, which can be created using existing functionality but is extremely specialised.
*/
typedef struct cbf2nx_item_datamap_t
{
const char * name;
const char * units;
get_h5group_func get_object;
filter_func filter;
int rank;
} cbf2nx_item_datamap_t;
/* Conversion function for an item of data. */
typedef int (*convert_func)(cbf_node *, const unsigned int, cbf_h5handle, const cbf2nx_item_datamap_t *);
/* Single object to store both a conversion function and its data, for reference by 'cbf2nx_column_map_t'. */
typedef struct cbf2nx_convert_t {
convert_func convert;
cbf2nx_item_datamap_t data;
} cbf2nx_convert_t;
/* Define the signature of a function to be used to set a specific value in the 'key' object. */
typedef int (*set_key_func)(cbf_cbf2nx_key_t *, const char *);
/* Add a layer of indirection. */
typedef struct cbf2nx_set_key_t {
set_key_func set_key;
} cbf2nx_set_key_t;
/* Define the signature of a function to cache the value at 'arg_0[arg_1]' in the cache object given by 'arg_2'. */
typedef int (*cache_item_func)(cbf_node *, const unsigned int, void *);
/* Add a layer of indirection. */
typedef struct cbf2nx_cache_item_t {
cache_item_func cache_item;
} cbf2nx_cache_item_t;
/* map an item name to some (discriminated) parameters used for converting it */
typedef struct cbf2nx_column_map_t
{
const char * name;
enum mapping_type type;
const void * data;
} cbf2nx_column_map_t;
/* define the information required to manage the lifetime of a cache object */
typedef struct cbf2nx_cache_t
{
size_t size; /*< the size of the cache object to be allocated */
int (*ctor) (void * self); /*< initialise 'self' to a standard state */
int (*dtor)(void * self); /*< free any allocated memory in 'self' */
} cbf2nx_cache_t;
/* define function signatures to per-row and per-table cache processing functions */
typedef int (*process_row_func)(cbf_node *, cbf_h5handle, cbf_cbf2nx_key_t *, void *, void *, const unsigned int);
typedef int (*process_tbl_func)(cbf_node *, cbf_h5handle, cbf_cbf2nx_key_t *, void *);
/*
Map a category name to an array of primary keys, data which it may contain and cache
objects & processing functions for each row and for the whole set of matched rows.
*/
typedef struct cbf2nx_category_map_t
{
const char * name;
cbf_primary_key_t * key_data;
cbf2nx_column_map_t * column_data;
cbf2nx_cache_t row_cache;
process_row_func process_row;
cbf2nx_cache_t tbl_cache;
process_tbl_func process_tbl;
} cbf2nx_category_map_t;
/*
Define some functions for manipulating caches of per-row data for various parts of the conversion process
For each kind of cahce required I need to define:
- a type
- functions to manage its lifetime
- functions to populate it
- a function to use it
*/
/*
Many caches make use of a simple method of adding string or floating-point data,
these should turn all the boilerplate code to do that into one-liners.
*/
#define DECL_CACHE_STR_SETTER(FUNCTION_NAME, CACHE_TYPE, CACHE_MEMBER) \
static int FUNCTION_NAME \
(cbf_node * const column, \
const unsigned int row, \
void * const cache) \
{ \
int error = CBF_SUCCESS; \
CACHE_TYPE * const c = cache; \
if (!column || !c) { \
error |= CBF_ARGUMENT; \
} else { \
error |= cbf_node_get_value(column, row, &c->CACHE_MEMBER); \
} \
return error; \
}
#define DECL_CACHE_DOUBLE_SETTER(FUNCTION_NAME, CACHE_TYPE, CACHE_MEMBER) \
static int FUNCTION_NAME \
(cbf_node * const column, \
const unsigned int row, \
void * const cache) \
{ \
int error = CBF_SUCCESS; \
CACHE_TYPE * const c = cache; \
if (!column || !c) { \
error |= CBF_ARGUMENT; \
} else { \
error |= cbf_node_get_doublevalue(column, row, &c->CACHE_MEMBER); \
} \
return error; \
}
#define DECL_CACHE_UINT_SETTER(FUNCTION_NAME, CACHE_TYPE, CACHE_MEMBER) \
static int FUNCTION_NAME \
(cbf_node * const column, \
const unsigned int row, \
void * const cache) \
{ \
int error = CBF_SUCCESS; \
CACHE_TYPE * const c = cache; \
if (!column || !c) { \
error |= CBF_ARGUMENT; \
} else { \
error |= cbf_node_get_uintvalue(column, row, &c->CACHE_MEMBER); \
} \
return error; \
}
#define DECL_CACHE_NODE_SETTER(FUNCTION_NAME, CACHE_TYPE, CACHE_MEMBER) \
static int FUNCTION_NAME \
(cbf_node * const column, \
const unsigned int row, \
void * const cache) \
{ \
int error = CBF_SUCCESS; \
CACHE_TYPE * const c = cache; \
CBF_UNUSED(row); \
if (!column || !c) { \
error |= CBF_ARGUMENT; \
} else { \
c->CACHE_MEMBER = column; \
} \
return error; \
}
/*
Define a type, constructor and setters to use when processing a single row of the 'array_data' table.
*/
typedef struct ArrayDataCache
{
cbf_node * column;
unsigned int row;
} ArrayDataCache;
static int ctor_ArrayDataCache
(void * const self)
{
int error = CBF_SUCCESS;
ArrayDataCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->column = NULL;
c->row = 0;
}
return error;
}
static int cache_ArrayData_data
(cbf_node * const column,
const unsigned int row,
void * const cache)
{
int error = CBF_SUCCESS;
ArrayDataCache * const c = cache;
if (!column || !c) {
error |= CBF_ARGUMENT;
} else {
c->column = column;
c->row = row;
}
return error;
}
/*
Process data from a single row of 'array_data'.
This just needs to extract some data from the 'key' object and call a function
which is also used when converting minicbf data to nexus data.
*/
static int process_ArrayDataCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const rcache,
void * const tcache,
const unsigned int row)
{
int error = CBF_SUCCESS;
ArrayDataCache * const c = rcache;
CBF_UNUSED(row);
if (!category || !nx || !key || !c || tcache) {
error |= CBF_ARGUMENT;
} else {
if (!c->column) {
cbf_debug_print("error: no data located");
} else {
CBF_CALL(cbf_write_array_h5file(c->column,c->row,nx,key->data_overload,key->data_undefined,0));
}
}
return error;
}
/*
Define a type, constructor and setters to use when processing a single row of the 'diffrn_radiation' table.
*/
typedef struct DiffrnRadiationRowCache
{
cbf_node * stokes_I;
cbf_node * stokes_Q;
cbf_node * stokes_U;
cbf_node * stokes_V;
cbf_node * psn;
cbf_node * psr;
cbf_node * stokes_I_esd;
cbf_node * stokes_Q_esd;
cbf_node * stokes_U_esd;
cbf_node * stokes_V_esd;
cbf_node * psn_esd;
cbf_node * psr_esd;
} DiffrnRadiationRowCache;
static int ctor_DiffrnRadiationRowCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnRadiationRowCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->stokes_I = NULL;
c->stokes_Q = NULL;
c->stokes_U = NULL;
c->stokes_V = NULL;
c->psn = NULL;
c->psr = NULL;
c->stokes_I_esd = NULL;
c->stokes_Q_esd = NULL;
c->stokes_U_esd = NULL;
c->stokes_V_esd = NULL;
c->psn_esd = NULL;
c->psr_esd = NULL;
}
return error;
}
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_I, DiffrnRadiationRowCache, stokes_I)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_Q, DiffrnRadiationRowCache, stokes_Q)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_U, DiffrnRadiationRowCache, stokes_U)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_V, DiffrnRadiationRowCache, stokes_V)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_PSN, DiffrnRadiationRowCache, psn)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_PSR, DiffrnRadiationRowCache, psr)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_I_ESD, DiffrnRadiationRowCache, stokes_I_esd)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_Q_ESD, DiffrnRadiationRowCache, stokes_Q_esd)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_U_ESD, DiffrnRadiationRowCache, stokes_U_esd)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_V_ESD, DiffrnRadiationRowCache, stokes_V_esd)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_PSN_ESD, DiffrnRadiationRowCache, psn_esd)
DECL_CACHE_NODE_SETTER(cache_DiffrnRadiation_PSR_ESD, DiffrnRadiationRowCache, psr_esd)
/*
Process data from a single row of 'diffrn_radiation'.
Calculate some of the stokes parameters and make reasonable assumptions for those
which can't be identified with sufficient reliability (ie: intensity, which is not
present in CBF in a usable form; and circular polarisation, which is not present
in CBF in any form). Write the data to the appropriate place in nexus.
If the stokes vector is properly specified then the 2-parameter polarisation
is ignored.
Stokes_I: Ip+In, where where Ip is the intensity
(amplitude squared) of the electric vector in the
plane of polarization and In is the intensity
(amplitude squared) of the electric vector in the
plane of the normal to the plane of polarization.
Stokes_Q: (Ip-In)*cos(2*theta), where where Ip is
the intensity (amplitude squared) of the electric vector
in the plane of polarization, In is the intensity
(amplitude squared) of the electric vector in the plane
of the normal to the plane of polarization, and theta
is the angle as viewed from the specimen, between the
normal to the polarization plane and the laboratory Y
axis.
Stokes_U: (Ip-In)*sin(2*theta), where where Ip is
the intensity (amplitude squared) of the electric vector
in the plane of polarization, In is the intensity
(amplitude squared) of the electric vector in the plane
of the normal to the plane of polarization, and theta is
the angle as viewed from the specimen, between the normal
to the polarization plane and the laboratory Y axis.
Stokes_V: +/-2*sqrt(IpIn), with a + sign for right-handed
circular polarization, where where Ip is the intensity
(amplitude squared) of the electric vector in the plane of
polarization and In is the intensity (amplitude squared) of
the electric vector in the plane of the normal to the plane
of polarization, and theta is the angle as viewed from the
specimen, between the normal to the polarization plane
and the laboratory Y axis.
The theta referred to in Stokes_Q and Stokes_U is that same
as the value of _diffrn_radiation.polarizn_source_norm,
and depends on the choice of laboratory coordinate systems.
In particular, that angle may change between the CBF laboratory
coordinate system and the NeXus McStas coordinate system. In
all cases the normal to the polarization plane is assumed to
be orthogonal to the direction of the beam
Looking head-on at the beam from sample to source, we have
\ | CIF_Y [0,1,0]
\ |
\ | | nx_y (nexus Y axis)
\ | |
\ | /
\ | |
normal \ | |
to polarization \ | /
plane \ | |
(in this case \ | |
theta =+45 deg) \ | /
\ | |
\ | |
\|/
In this example, nx_y is at a negative angle. phi, from
CIF_Y, and the new value of nx_theta with be increased
by -phi.
nx_Stokes_Q = cos(2*phi)*Stokes_Q+sin(2*phi)*Stokes_U
nx_Stokes_U = cos(2*phi)*Stokes_U-sin(2*phi)*Stohes_Q
*/
static int process_DiffrnRadiationRowCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const rcache,
void * const tcache,
const unsigned int row)
{
int error = CBF_SUCCESS;
DiffrnRadiationRowCache * const c = rcache;
if (!category || !nx || !key || !c || tcache) {
error |= CBF_ARGUMENT;
} else {
/* get the angle to rotate from cbf to nexus polarisation coordinate frames */
cbf_node * axes = NULL;
double beam_dirn[3] = {0.0,0.0,0.0};
double gravity_dirn[3] = {0.0,0.0,0.0};
double x_nx[3] = {0.0,0.0,0.0};
double y_nx[3] = {0.0,0.0,0.0};
double phi = 0.0;
CBF_CALL(cbf_find_parent(&axes,category,CBF_DATABLOCK));
CBF_CALL(cbf_find_child(&axes,axes,"axis"));
CBF_CALL(cbf_node_get_beam_dirn(axes, beam_dirn));
CBF_CALL(cbf_node_get_gravity_dirn(axes, gravity_dirn));
CBF_CALL(cbf_cross_product(beam_dirn,gravity_dirn,x_nx));
CBF_CALL(cbf_cross_product(beam_dirn,x_nx,y_nx));
if (!error) {
phi = atan2(-y_nx[0],y_nx[1]);
}
/* store computed data in nexus, if all went well */
if (!error) {
int have_data = 0;
int have_data_esds = 0;
double value[4] = {0.0,0.0,0.0,0.0};
double value_esds[4] = {0.0,0.0,0.0,0.0};
if (
cbf_node_has_doublevalue(c->stokes_I,row) &&
cbf_node_has_doublevalue(c->stokes_Q,row) &&
cbf_node_has_doublevalue(c->stokes_U,row) &&
cbf_node_has_doublevalue(c->stokes_V,row)
)
{
double Q = 0.0, U = 0.0;
/* I have all the relevant columns for a stokes vector */
CBF_CALL(cbf_node_get_doublevalue(c->stokes_I,row,value+0));
CBF_CALL(cbf_node_get_doublevalue(c->stokes_Q,row,&Q));
CBF_CALL(cbf_node_get_doublevalue(c->stokes_U,row,&U));
CBF_CALL(cbf_node_get_doublevalue(c->stokes_V,row,value+3));
value[1] = cos(2.*phi)*Q - sin(2.*phi)*U;
value[2] = +sin(2.*phi)*Q + cos(2.*phi)*U;
have_data = 1;
if (
cbf_node_has_doublevalue(c->stokes_I_esd,row) &&
cbf_node_has_doublevalue(c->stokes_Q_esd,row) &&
cbf_node_has_doublevalue(c->stokes_U_esd,row) &&
cbf_node_has_doublevalue(c->stokes_V_esd,row)
)
{
double Q_esd = 0.0, U_esd = 0.0;
/* The uncertainties are estimated by the square
root of the sum of the squares of the component
uncertainties */
CBF_CALL(cbf_node_get_doublevalue(c->stokes_I_esd,row,value_esds+0));
CBF_CALL(cbf_node_get_doublevalue(c->stokes_Q_esd,row,&Q_esd));
CBF_CALL(cbf_node_get_doublevalue(c->stokes_U_esd,row,&U_esd));
CBF_CALL(cbf_node_get_doublevalue(c->stokes_V_esd,row,value_esds+3));
value_esds[1] = sqrt(cos(2.*phi)*Q_esd*cos(2.*phi)*Q_esd
+ sin(2.*phi)*U_esd*sin(2.*phi)*U_esd);
value_esds[2] = sqrt(sin(2.*phi)*Q_esd*sin(2.*phi)*Q_esd
+ cos(2.*phi)*U_esd*cos(2.*phi)*U_esd);
have_data_esds = 1;
}
} else if (
cbf_node_has_doublevalue(c->psn,row) &&
cbf_node_has_doublevalue(c->psr,row)
)
{
/*
I don't have a valid stokes vector, but I do have a valid
2-parameter representation that I know how to convert
if we assume no circular component.
*/
double psn = 0.0, psr = 0.0;
CBF_CALL(cbf_node_get_doublevalue(c->psn,row,&psn));
CBF_CALL(cbf_node_get_doublevalue(c->psr,row,&psr));
psn *= atan2(1.,1.)/45.0;
value[0] = 1.0;
value[1] = psr*cos(2.*(psn+phi));
value[2] = psr*sin(2.*(psn+phi));
value[3] = 0.0; /* No circular component assumed */
have_data = 1;
if (
cbf_node_has_doublevalue(c->psn_esd,row) &&
cbf_node_has_doublevalue(c->psr_esd,row)
)
{ double psn_esd = 0.0, psr_esd = 0.0;
/* The uncertainties are estimated by the square
root of the sum of the squares of the component
uncertainties. In this case we are apprximating
with differentials. This is only valid for small
uncertainties */
CBF_CALL(cbf_node_get_doublevalue(c->psn_esd,row,&psn_esd));
CBF_CALL(cbf_node_get_doublevalue(c->psr_esd,row,&psr_esd));
value_esds[0] = 0.;
value_esds[1] = sqrt(cos(2.*(psn+phi))*psr_esd*cos(2.*(psn+phi))*psr_esd
+ 4*value[2]*psn_esd*value[2]*psn_esd);
value_esds[2] = sqrt(sin(2.*(psn+phi))*psr_esd*sin(2.*(psn+phi))*psr_esd
+ 4*value[1]*psn_esd*value[1]*psn_esd);
value_esds[3] = 0.;
have_data_esds = 1;
}
}
if (!error && have_data) {
hid_t dset = CBF_H5FAIL;
hid_t beam = CBF_H5FAIL;
const hsize_t max[] = {H5S_UNLIMITED,4};
const hsize_t chunk[] = {1,4};
hsize_t offset[2];
const hsize_t count[] = {1,4};
hsize_t buf[] = {0,0};
offset[0] = nx->slice;
offset[1] = 0;
CBF_CALL(cbf_h5handle_require_beam(nx,&beam,0));
CBF_CALL(cbf_H5Drequire(beam,&dset,"incident_polarisation_stokes",2,max,chunk,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dinsert(dset,offset,0,count,buf,value,H5T_NATIVE_DOUBLE));
cbf_H5Dfree(dset);
}
if (!error && have_data_esds) {
hid_t dset = CBF_H5FAIL;
hid_t beam = CBF_H5FAIL;
const hsize_t max[] = {H5S_UNLIMITED,4};
const hsize_t chunk[] = {1,4};
hsize_t offset[2];
const hsize_t count[] = {1,4};
hsize_t buf[] = {0,0};
offset[0] = nx->slice;
offset[1] = 0;
CBF_CALL(cbf_h5handle_require_beam(nx,&beam,0));
CBF_CALL(cbf_H5Drequire(beam,&dset,"incident_polarisation_stokes_uncertainties",2,max,chunk,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dinsert(dset,offset,0,count,buf,value_esds,H5T_NATIVE_DOUBLE));
cbf_H5Dfree(dset);
}
}
}
return error;
}
/*
Define a type, constructor and setters to use when processing a single row of the 'diffrn_scan_axis' table.
*/
typedef struct DiffrnScanAxisCache
{
const char * axis;
double disp_strt;
double angle_strt;
double disp_incr;
double angle_incr;
double disp_rstrt_incr;
double angle_rstrt_incr;
} DiffrnScanAxisCache;
static int ctor_DiffrnScanAxisCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnScanAxisCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->axis = NULL;
c->disp_strt = 0.0;
c->angle_strt = 0.0;
c->disp_incr = 0.0;
c->angle_incr = 0.0;
c->disp_rstrt_incr = 0.0;
c->angle_rstrt_incr = 0.0;
}
return error;
}
DECL_CACHE_STR_SETTER(cache_DiffrnScanAxis_axis, DiffrnScanAxisCache, axis)
DECL_CACHE_DOUBLE_SETTER(cache_DiffrnScanAxis_angleIncr, DiffrnScanAxisCache, angle_incr)
DECL_CACHE_DOUBLE_SETTER(cache_DiffrnScanAxis_angleRstrtIncr, DiffrnScanAxisCache, angle_rstrt_incr)
DECL_CACHE_DOUBLE_SETTER(cache_DiffrnScanAxis_angleStrt, DiffrnScanAxisCache, angle_strt)
DECL_CACHE_DOUBLE_SETTER(cache_DiffrnScanAxis_dispIncr, DiffrnScanAxisCache, disp_incr)
DECL_CACHE_DOUBLE_SETTER(cache_DiffrnScanAxis_dispRstrtIncr, DiffrnScanAxisCache, disp_rstrt_incr)
DECL_CACHE_DOUBLE_SETTER(cache_DiffrnScanAxis_dispStrt, DiffrnScanAxisCache, disp_strt)
/*
Process data from a single row of 'diffrn_scan_axis'.
For the axis with the id located in the row, locate the 'type' from the cbf file,
the path from the 'key' object, then insert a calulated displacement or angle
(depending on type) for the current frame. This value should be replaced by a per-
frame value later on, if one is given, so it acts as a bit like a default value.
*/
static int process_DiffrnScanAxisCache(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const rcache,
void * const tcache,
const unsigned int row)
{
int error = CBF_SUCCESS;
DiffrnScanAxisCache * const c = rcache;
CBF_UNUSED(row);
if (!category || !nx || !key || !c || tcache) {
error |= CBF_ARGUMENT;
} else {
unsigned int i;
/* find the index of the required axis within the axis settings */
for (i = 0; i != key->axis.count; ++i) {
if (!strcmp(key->axis.axis_id[i],c->axis)) {
break;
}
}
/* check if I found it, if so then use it */
if (i == key->axis.count) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_NOTFOUND));
error |= CBF_NOTFOUND;
} else {
cbf_node * axis_category = NULL;
cbf_node * axis_id_column = NULL;
cbf_node * axis_type_column = NULL;
const char * type = NULL;
unsigned int row = 0;
CBF_CALL(cbf_find_parent(&axis_category,category,CBF_DATABLOCK));
CBF_CALL(cbf_find_child(&axis_category,axis_category,"axis"));
CBF_CALL(cbf_find_child(&axis_id_column,axis_category,"id"));
CBF_CALL(cbf_find_child(&axis_type_column,axis_category,"type"));
CBF_CALL(cbf_node_find_nextrow(axis_id_column,0,c->axis,&row));
CBF_CALL(cbf_node_get_value(axis_type_column,row,&type));
if (CBF_SUCCESS==error) {
hid_t dset = CBF_H5FAIL;
hsize_t off[1];
const hsize_t cnt[] = {1};
const hsize_t max[] = {H5S_UNLIMITED};
hsize_t buf[] = {0};
double data;
off[0] = nx->slice;
if (!strcmp(type,"translation")) {
data = c->disp_strt + (c->disp_incr+c->disp_rstrt_incr)*key->frame_number;
} else if (!strcmp(type,"rotation")) {
data = c->angle_strt + (c->angle_incr+c->angle_rstrt_incr)*key->frame_number;
} else {
data = 0./0.;
}
CBF_CALL(cbf_H5Dfind2(nx->hfile,&dset,key->axis.path[i],1,max,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dinsert(dset,off,0,cnt,buf,&data,H5T_NATIVE_DOUBLE));
cbf_H5Dfree(dset);
}
}
}
return error;
}
/*
Define a type, constructor and setters to use when processing a single row of the 'diffrn_scan_frame_axis' table.
*/
typedef struct DiffrnScanFrameAxisCache
{
const char * axis;
double angle;
double disp;
} DiffrnScanFrameAxisCache;
static int ctor_DiffrnScanFrameAxisCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnScanFrameAxisCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->axis = NULL;
c->angle = 0.0;
c->disp = 0.0;
}
return error;
}
DECL_CACHE_STR_SETTER(cache_DiffrnScanFrameAxis_axis, DiffrnScanFrameAxisCache, axis)
DECL_CACHE_DOUBLE_SETTER(cache_DiffrnScanFrameAxis_angle, DiffrnScanFrameAxisCache, angle)
DECL_CACHE_DOUBLE_SETTER(cache_DiffrnScanFrameAxis_disp, DiffrnScanFrameAxisCache, disp)
/*
Process data from a single row of 'diffrn_scan_frame_axis'.
For the axis with the id located in the row, locate the 'type' from the cbf file,
the path from the 'key' object, then insert the displacement or angle (depending
on type) for the current frame.
*/
static int process_DiffrnScanFrameAxisCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const rcache,
void * const tcache,
const unsigned int row)
{
int error = CBF_SUCCESS;
DiffrnScanFrameAxisCache * const c = rcache;
CBF_UNUSED(row);
if (!category || !nx || !key || !c || tcache) {
error |= CBF_ARGUMENT;
} else {
unsigned int i;
/* find the index of the required axis within the axis settings */
for (i = 0; i != key->axis.count; ++i) {
if (!strcmp(key->axis.axis_id[i],c->axis)) {
break;
}
}
/* check if I found it, if so then use it */
if (i == key->axis.count) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_NOTFOUND));
error |= CBF_NOTFOUND;
} else {
cbf_node * axis_category = NULL;
cbf_node * axis_id_column = NULL;
cbf_node * axis_type_column = NULL;
const char * type = NULL;
unsigned int row = 0;
CBF_CALL(cbf_find_parent(&axis_category,category,CBF_DATABLOCK));
CBF_CALL(cbf_find_child(&axis_category,axis_category,"axis"));
CBF_CALL(cbf_find_child(&axis_id_column,axis_category,"id"));
CBF_CALL(cbf_find_child(&axis_type_column,axis_category,"type"));
CBF_CALL(cbf_node_find_nextrow(axis_id_column,0,c->axis,&row));
CBF_CALL(cbf_node_get_value(axis_type_column,row,&type));
if (CBF_SUCCESS == error) {
hid_t dset = CBF_H5FAIL;
hsize_t off[1];
const hsize_t cnt[] = {1};
const hsize_t max[] = {H5S_UNLIMITED};
hsize_t buf[] = {0};
const double * data;
off[0] = nx->slice;
if (!strcmp(type,"translation")) {
data = &c->disp;
} else if (!strcmp(type,"rotation")) {
data = &c->angle;
} else {
data = NULL;
}
CBF_CALL(cbf_H5Dfind2(nx->hfile,&dset,key->axis.path[i],1,max,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dinsert(dset,off,0,cnt,buf,data,H5T_NATIVE_DOUBLE));
cbf_H5Dfree(dset);
}
}
}
return error;
}
/*
Define a type, constructor, destructor and setters to use when accumulating data from rows of the
'diffrn_detector_axis' table and when processing that data after all of it has been acquired.
*/
typedef struct DiffrnDetectorAxisTCache
{
size_t count;
const char * * axis_id;
unsigned int * in_degree;
const char * * path;
} DiffrnDetectorAxisTCache;
static int ctor_DiffrnDetectorAxisTCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnDetectorAxisTCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->count = 0;
c->axis_id = NULL;
c->in_degree = NULL;
c->path = NULL;
}
return error;
}
static int dtor_DiffrnDetectorAxisTCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnDetectorAxisTCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
size_t i;
for (i = 0; i != c->count; ++i) free((void*)c->path[i]);
free((void*)c->axis_id);
free((void*)c->in_degree);
free((void*)c->path);
}
return error;
}
/*
A utility function to simplify usage of the 'DiffrnDetectorAxisTCache' object.
*/
static int DiffrnDetectorAxisTCache_insert_axis
(DiffrnDetectorAxisTCache * const c,
const char * const axis_id,
const char * const path)
{
int error = CBF_SUCCESS;
if (!c) {
error |= CBF_ARGUMENT;
} else {
const char * * new_axis_id = realloc(c->axis_id,(1+c->count)*sizeof(char*));
unsigned int * new_in_degree = realloc(c->in_degree,(1+c->count)*sizeof(unsigned int));
const char * * new_path = realloc(c->path,(1+c->count)*sizeof(char*));
if (!new_axis_id) {
error |= CBF_ALLOC;
} else {
c->axis_id = new_axis_id;
}
if (!new_in_degree) {
error |= CBF_ALLOC;
} else {
c->in_degree = new_in_degree;
}
if (!new_path) {
error |= CBF_ALLOC;
} else {
c->path = new_path;
}
if (!error) {
c->axis_id[c->count] = axis_id;
c->in_degree[c->count] = 0;
c->path[c->count] = path;
++c->count;
}
}
return error;
}
/*
Process data accumulated from all matching rows of 'diffrn_detector_axis'.
This searches for a single leaf axis in the extracted set of axes, then writes the
detector dependency in nexus, and traverses the dependency chain for it to add each
required axis to the 'key' object with any relevant paths which were generated
when extracting data from each individual row of 'diffrn_detector_axis'.
The single leaf is the dependency for NXdetector_group. For NXdetector, the chain
needs to be extended to include the axes for the image.
*/
static int process_DiffrnDetectorAxisTCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const tcache)
{
int error = CBF_SUCCESS;
DiffrnDetectorAxisTCache * const c = tcache;
if (!category || !nx || !key || !c) {
cbf_debug_print("error: can't process data from 'diffrn_detector_axis' table.");
error |= CBF_ARGUMENT;
} else {
/*
if (!(h5handle->flags & CBF_H5_CBFNONAMES)) return CBF_SUCCESS;
I now have all relevant axes from this category, so I can find & write the dependency for the detector.
New axes are in the cache object. For each axis I need to set an in_degree, which tells me how many axes
refer to this axis. An in_degree of 0 means I probably have a leaf, a check to ensure only one axis has
an in_degree of 0 exists in the subset is needed to ensure I have a single valid leaf axis. If I have a
valid leaf then I can write the detector's dependency and add that and all it's dependent axes to the key
for later conversion.
*/
unsigned int i;
unsigned int leaf = c->count;
unsigned int leaves = 0;
cbf_node * axis_category = NULL;
cbf_node * axis_id_column = NULL;
cbf_node * axis_depends_on_column = NULL;
cbf_node * axis_rotation_axis_column = NULL;
CBF_CALL(cbf_find_parent(&axis_category,category,CBF_DATABLOCK));
CBF_CALL(cbf_find_child(&axis_category,axis_category,"axis"));
CBF_CALL(cbf_find_child(&axis_id_column,axis_category,"id"));
if (cbf_find_child(&axis_depends_on_column,axis_category,"depends_on")) {
axis_depends_on_column = NULL;
};
if (cbf_find_child(&axis_rotation_axis_column,axis_category,"rotation_axis")) {
axis_rotation_axis_column = NULL;
};
/* populate the in-degree field in the cached data */
for (i = 0; c->count != i; ++i) {
unsigned int row = 0;
const char * depends_on = NULL;
/* locate the current axis and get its dependency */
CBF_CALL(cbf_node_find_nextrow(axis_id_column,0,c->axis_id[i],&row));
if (axis_depends_on_column) {
CBF_CALL(cbf_node_get_value(axis_depends_on_column,row,&depends_on));
if (CBF_SUCCESS==error) {
unsigned int j;
/* I know which axis the current axis depends on, look for it in the current subset */
for (j = 0; c->count != j; ++j) {
if (!cbf_cistrcmp(c->axis_id[j],depends_on)) break;
}
if (c->count != j) {
++c->in_degree[j];
}
}
}
}
/* ensure there is only one leaf, and find it */
for (i = 0; c->count != i; ++i) {
if (0==c->in_degree[i]) {
leaf = i;
++leaves;
}
}
/* test if a single valid leaf axis was found */
if (1!=leaves || leaf>=c->count) {
cbf_debug_print("error: couldn't determine what defines the detector orientation");
error |= CBF_FORMAT;
} else {
unsigned int row = 0;
unsigned int prev_idx = key->axis.count;
const char * axis_id = c->axis_id[leaf];
size_t index;
/* I now have a valid leaf axis, write the dependency... */
if (nx->flags & CBF_H5_CBFNONAMES) {
CBF_CALL(cbf_H5Drequire_flstring(nx->nxdetectors[nx->cur_detector],0,"depends_on",c->path[leaf]));
} else {
CBF_CALL(cbf_h5handle_require_detector_group(nx,NULL,NULL));
CBF_CALL(cbf_H5Drequire_flstring(nx->nxdetector_group,0,"depends_on",c->path[leaf]));
return error;
}
/* ...record the leaf axis in the key... */
if ((error|=_cbf_insert_axis(&key->axis,axis_id,c->path[leaf]))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
c->path[leaf] = NULL;
}
/* ...find the next axis to look for... */
CBF_CALL(cbf_node_find_nextrow(axis_id_column,0,axis_id,&row));
CBF_CALL(cbf_node_get_value(axis_depends_on_column,row,&axis_id));
/* ...and log all dependent axes in the key, taking paths from the cache when possible. */
while (axis_id && strcmp(axis_id,".") && !error) {
/* search for an axis with an id given by axis_id in the key, the cache, then the cbf file. */
if (!find_index_of_str_in_array(key->axis.axis_id,key->axis.count,axis_id,&index)) {
/* I already have the axis in the key, so I don't need to process any more axes. */
key->axis.depends_on[prev_idx] = index;
break;
} else if (!find_index_of_str_in_array(c->axis_id,c->count,axis_id,&index)) {
const unsigned int idx = key->axis.count;
/* I have a match in the cache - re-use its path. */
if ((error|=_cbf_insert_axis(&key->axis,axis_id,c->path[index]))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
/* give up ownership of the path */
c->path[index] = NULL;
/* set dependency indices */
key->axis.depends_on[prev_idx] = idx;
prev_idx = idx;
/* look up the next axis to process */
CBF_CALL(cbf_node_find_nextrow(axis_id_column,0,axis_id,&row));
CBF_CALL(cbf_node_get_value(axis_depends_on_column,row,&axis_id));
}
} else if (!(error|=cbf_node_find_nextrow(axis_id_column,0,axis_id,&row))) {
const unsigned int idx = key->axis.count;
/* No match found in the key or cache, found one in the axes in the cbf file. */
if ((error|=_cbf_insert_axis(&key->axis,axis_id,NULL))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
key->axis.depends_on[prev_idx] = idx;
prev_idx = idx;
}
CBF_CALL(cbf_node_get_value(axis_depends_on_column,row,&axis_id));
} else {
/* it appears that the expected axis doesn't exist, this is an error */
cbf_debug_print3("error: %s %s\n","No axis found for axis with axis_id:",axis_id);
error |= CBF_NOTFOUND;
}
}
}
}
return error;
}
/*
Define a type, constructor and setters to use when processing a single row of the 'diffrn_detector_axis' table.
*/
typedef struct DiffrnDetectorAxisRCache
{
const char * axis;
} DiffrnDetectorAxisRCache;
static int ctor_DiffrnDetectorAxisRCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnDetectorAxisRCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->axis = NULL;
}
return error;
}
DECL_CACHE_STR_SETTER(cache_DiffrnDetectorAxis_axis, DiffrnDetectorAxisRCache, axis)
/*
Process data from a single row of 'diffrn_detector_axis'.
This generates a path for the axis that was found and inserts it into the table-level cache for further processing.
*/
static int process_DiffrnDetectorAxisRCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const rcache,
void * const tcache,
const unsigned int row)
{
int error = CBF_SUCCESS;
DiffrnDetectorAxisRCache * const r = rcache;
DiffrnDetectorAxisTCache * const t = tcache;
CBF_UNUSED(row);
if (!category || !nx || !key || !r || !t) {
cbf_debug_print("error: can't process row of 'diffrn_detector_axis'.");
error |= CBF_ARGUMENT;
} else {
if (!r->axis) {
cbf_debug_print("error: 'axis_id' not found");
error |= CBF_NOTFOUND;
} else {
const char * axis_path;
const char * taxis_path;
/* the axis exists - convert the data */
hid_t det = CBF_H5FAIL;
hid_t axisGroup = CBF_H5FAIL;
const char axis_group_name[] = "transformations";
if (cbf_get_NX_axis_path(nx,r->axis,&taxis_path)) {
CBF_CALL(cbf_h5handle_require_detector(nx,&det,0));
CBF_CALL(cbf_H5Grequire(det,&axisGroup,axis_group_name));
CBF_CALL(cbf_H5Arequire_string(axisGroup,"NX_class","NXtransformations"));
if (!error) {
const char path_empty[] = "";
const char * path_parts[7];
path_parts[0] = path_empty;
path_parts[1] = nx->nxid_name;
path_parts[2] = nx->nxinstrument_name;
path_parts[3] = nx->nxdetector_names[nx->cur_detector];
path_parts[4] = axis_group_name;
path_parts[5] = r->axis;
path_parts[6] = 0;
axis_path = _cbf_str_join(path_parts,'/');
/* try to transfer ownership of the path to the table-level cache */
if ((error|=DiffrnDetectorAxisTCache_insert_axis(t,r->axis,axis_path))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
axis_path = NULL;
}
free((void*)axis_path);
}
} else {
axis_path = _cbf_strdup(taxis_path);
cbf_debug_print3("Mapped axis path for axis '%s' to '%s'\n",r->axis,axis_path);
/* try to transfer ownership of the path to the table-level cache */
if ((error|=DiffrnDetectorAxisTCache_insert_axis(t,r->axis,axis_path))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
axis_path = NULL;
}
free((void*)axis_path);
}
cbf_H5Gfree(axisGroup);
}
}
return error;
}
/*
Define a type, constructor, destructor and setters to use when accumulating data from rows of the
'diffrn_measurement_axis' table and when processing that data after all of it has been acquired.
*/
typedef struct DiffrnMeasurementAxisTCache
{
size_t count;
const char * * axis_id;
unsigned int * in_degree;
const char * * path;
} DiffrnMeasurementAxisTCache;
static int ctor_DiffrnMeasurementAxisTCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnMeasurementAxisTCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->count = 0;
c->axis_id = NULL;
c->in_degree = NULL;
c->path = NULL;
}
return error;
}
static int dtor_DiffrnMeasurementAxisTCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnMeasurementAxisTCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
size_t i;
for (i = 0; i != c->count; ++i) free((void*)c->path[i]);
free((void*)c->axis_id);
free((void*)c->in_degree);
free((void*)c->path);
}
return error;
}
/*
A utility function to simplify usage of the 'DiffrnMeasurementAxisTCache' object.
*/
static int DiffrnMeasurementAxisTCache_insert_axis
(DiffrnMeasurementAxisTCache * const c,
const char * const axis_id,
const char * const path)
{
int error = CBF_SUCCESS;
if (!c) {
error |= CBF_ARGUMENT;
} else {
const char * * new_axis_id = realloc(c->axis_id,(1+c->count)*sizeof(char*));
unsigned int * new_in_degree = realloc(c->in_degree,(1+c->count)*sizeof(unsigned int));
const char * * new_path = realloc(c->path,(1+c->count)*sizeof(char*));
if (!new_axis_id) {
error |= CBF_ALLOC;
} else {
c->axis_id = new_axis_id;
}
if (!new_in_degree) {
error |= CBF_ALLOC;
} else {
c->in_degree = new_in_degree;
}
if (!new_path) {
error |= CBF_ALLOC;
} else {
c->path = new_path;
}
if (!error) {
c->axis_id[c->count] = axis_id;
c->in_degree[c->count] = 0;
c->path[c->count] = path;
++c->count;
}
}
return error;
}
/*
Process data accumulated from all matching rows of 'diffrn_measurement_axis'.
This searches for a single leaf axis in the extracted set of axes, then writes the
sample dependency in nexus, and traverses the dependency chain for it to add each
required axis to the 'key' object with any relevant paths which were generated
when extracting data from each individual row of 'diffrn_measurement_axis'.
*/
static int process_DiffrnMeasurementAxisTCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const tcache)
{
int error = CBF_SUCCESS;
DiffrnMeasurementAxisTCache * const c = tcache;
if (!category || !nx || !key || !c) {
cbf_debug_print("error: can't process data from 'diffrn_measurement_axis' table.");
error |= CBF_ARGUMENT;
} else {
/*
I now have all relevant axes from this category, so I can find & write the dependency for the sample.
New axes are in the cache object. For each axis I need to set an in_degree, which tells me how many axes
refer to this axis. An in_degree of 0 means I probably have a leaf, a check to ensure only one axis has
an in_degree of 0 exists in the subset is needed to ensure I have a single valid leaf axis. If I have a
valid leaf then I can write the sample's dependency and add that and all it's dependent axes to the key
for later conversion.
*/
unsigned int i;
unsigned int leaf = c->count;
unsigned int leaves = 0;
cbf_node * axis_category = NULL;
cbf_node * axis_id_column = NULL;
cbf_node * axis_depends_on_column = NULL;
CBF_CALL(cbf_find_parent(&axis_category,category,CBF_DATABLOCK));
CBF_CALL(cbf_find_child(&axis_category,axis_category,"axis"));
CBF_CALL(cbf_find_child(&axis_id_column,axis_category,"id"));
CBF_CALL(cbf_find_child(&axis_depends_on_column,axis_category,"depends_on"));
/* populate the in-degree field in the cached data */
for (i = 0; c->count != i; ++i) {
unsigned int row = 0;
const char * depends_on = NULL;
/* locate the current axis and get its dependency */
CBF_CALL(cbf_node_find_nextrow(axis_id_column,0,c->axis_id[i],&row));
CBF_CALL(cbf_node_get_value(axis_depends_on_column,row,&depends_on));
if (CBF_SUCCESS == error) {
unsigned int j;
/* I know which axis the current axis depends on, look for it in the current subset */
for (j = 0; c->count != j; ++j) {
if (!cbf_cistrcmp(c->axis_id[j],depends_on)) break;
}
if (c->count != j) {
++c->in_degree[j];
}
}
}
/* ensure there is only one leaf, and find it */
for (i = 0; c->count != i; ++i) {
if (0==c->in_degree[i]) {
leaf = i;
++leaves;
}
}
/* test if a single valid leaf axis was found */
if (1!=leaves || leaf>=c->count) {
/* it *might* be possible to use multiple goniometers, so a different check might be required here */
cbf_debug_print("error: couldn't determine what defines the sample orientation");
error |= CBF_FORMAT;
} else {
unsigned int row = 0;
unsigned int prev_idx = key->axis.count;
const char * axis_id = c->axis_id[leaf];
size_t index;
/* I now have a valid leaf axis, write the dependency... */
CBF_CALL(cbf_H5Drequire_flstring(nx->nxsample,0,"depends_on",c->path[leaf]));
/* ...record the leaf axis in the key... */
if ((error|=_cbf_insert_axis(&key->axis,axis_id,c->path[leaf]))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
c->path[leaf] = NULL;
}
/* ...find the next axis to look for... */
CBF_CALL(cbf_node_find_nextrow(axis_id_column,0,axis_id,&row));
CBF_CALL(cbf_node_get_value(axis_depends_on_column,row,&axis_id));
/* ...and log all dependent axes in the key, taking paths from the cache when possible. */
while (axis_id && strcmp(axis_id,".") && !error) {
/* search for an axis with an id given by axis_id in the key, the cache, then the cbf file. */
if (!find_index_of_str_in_array(key->axis.axis_id,key->axis.count,axis_id,&index)) {
/* I already have the axis in the key, so I don't need to process any more axes. */
key->axis.depends_on[prev_idx] = index;
break;
} else if (!find_index_of_str_in_array(c->axis_id,c->count,axis_id,&index)) {
const unsigned int idx = key->axis.count;
/* I have a match in the cache - re-use its path. */
if ((error|=_cbf_insert_axis(&key->axis,axis_id,c->path[index]))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
/* give up ownership of the path */
c->path[index] = NULL;
/* set dependency indices */
key->axis.depends_on[prev_idx] = idx;
prev_idx = idx;
/* look up the next axis to process */
CBF_CALL(cbf_node_find_nextrow(axis_id_column,0,axis_id,&row));
CBF_CALL(cbf_node_get_value(axis_depends_on_column,row,&axis_id));
}
} else if (!(error|=cbf_node_find_nextrow(axis_id_column,0,axis_id,&row))) {
const unsigned int idx = key->axis.count;
/* No match found in the key or cache, found one in the axes in the cbf file. */
if ((error|=_cbf_insert_axis(&key->axis,axis_id,NULL))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
key->axis.depends_on[prev_idx] = idx;
prev_idx = idx;
}
CBF_CALL(cbf_node_get_value(axis_depends_on_column,row,&axis_id));
} else {
/* it appears that the expected axis doesn't exist, this is an error */
cbf_debug_print3("error: %s %s\n","No axis found for axis with axis_id:", axis_id);
error |= CBF_NOTFOUND;
}
}
}
}
return error;
}
/*
Define a type, constructor and setters to use when processing a single row of the 'diffrn_measurement_axis' table.
*/
typedef struct DiffrnMeasurementAxisRCache
{
const char * axis;
/* insert data to identify different goniometers here? */
} DiffrnMeasurementAxisRCache;
static int ctor_DiffrnMeasurementAxisRCache
(void * const self)
{
int error = CBF_SUCCESS;
DiffrnMeasurementAxisRCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->axis = NULL;
}
return error;
}
DECL_CACHE_STR_SETTER(cache_DiffrnMeasurementAxis_axis, DiffrnMeasurementAxisRCache, axis)
/*
Process data from a single row of 'diffrn_measurement_axis'.
This generates a path for the axis that was found and inserts it into the table-level cache for further processing.
*/
static int process_DiffrnMeasurementAxisRCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const rcache,
void * const tcache,
const unsigned int row)
{
int error = CBF_SUCCESS;
DiffrnMeasurementAxisRCache * const r = rcache;
DiffrnMeasurementAxisTCache * const t = tcache;
CBF_UNUSED(row);
if (!category || !nx || !key || !r || !t) {
cbf_debug_print("error: can't process row of 'diffrn_measurement_axis'.");
error |= CBF_ARGUMENT;
} else {
if (!r->axis) {
cbf_debug_print("error: 'axis_id' not found");
error |= CBF_NOTFOUND;
} else {
const char * axis_path;
const char * taxis_path;
/* the axis exists - convert the data */
hid_t sample = CBF_H5FAIL;
hid_t axisGroup = CBF_H5FAIL;
const char axis_group_name[] = "transformations";
if (cbf_get_NX_axis_path(nx,r->axis,&taxis_path)) {
CBF_CALL(cbf_h5handle_require_sample(nx,&sample,0));
CBF_CALL(cbf_H5Grequire(sample,&axisGroup,axis_group_name));
CBF_CALL(cbf_H5Arequire_string(axisGroup,"NX_class","NXtransformations"));
if (!error) {
const char path_empty[] = "";
const char * path_parts[6];
path_parts[0] = path_empty;
path_parts[1] = nx->nxid_name;
path_parts[2] = nx->nxsample_name;
path_parts[3] = axis_group_name;
path_parts[4] = r->axis;
path_parts[5] = 0;
axis_path = _cbf_str_join(path_parts,'/');
/* try to transfer ownership of the path to the table-level cache */
if ((error|=DiffrnMeasurementAxisTCache_insert_axis(t,r->axis,axis_path))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
axis_path = NULL;
}
free((void*)axis_path);
}
} else {
axis_path = _cbf_strdup(taxis_path);
cbf_debug_print3("Mapped axis path for axis '%s' to '%s'\n",r->axis,axis_path);
/* try to transfer ownership of the path to the table-level cache */
if ((error|=DiffrnMeasurementAxisTCache_insert_axis(t,r->axis,axis_path))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
axis_path = NULL;
}
free((void*)axis_path);
}
cbf_H5Gfree(axisGroup);
}
}
return error;
}
/*
Define a type, constructor and setters to use when processing a single row of the 'array_intensities' table.
*/
typedef struct ArrayIntensitiesCache
{
const char * linearity;
double gain;
double offset;
double scaling;
} ArrayIntensitiesCache;
static int ctor_ArrayIntensitiesCache
(void * const self)
{
int error = CBF_SUCCESS;
ArrayIntensitiesCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->linearity = NULL;
c->gain = 0.0;
c->offset = 0.0;
c->scaling = 0.0;
}
return error;
}
DECL_CACHE_STR_SETTER(cache_ArrayIntensities_linearity, ArrayIntensitiesCache, linearity)
DECL_CACHE_DOUBLE_SETTER(cache_ArrayIntensities_gain, ArrayIntensitiesCache, gain)
DECL_CACHE_DOUBLE_SETTER(cache_ArrayIntensities_offset, ArrayIntensitiesCache, offset)
DECL_CACHE_DOUBLE_SETTER(cache_ArrayIntensities_scaling, ArrayIntensitiesCache, scaling)
/*
Process data from a single row of 'array_intensities'.
The processing depends on the value of 'linearity':
if linearity is "linear" then the scaling factor is assumed to be 1/gain.
if linearity in ["linear","offset","scaling","scaling_offset"]:
write the 'offset' and 'scaling_factor' to datasets with those names
elseif linearity in ["sqrt_scaled","logarithmic_scaled"]:
throw Exception("Non-linear scaling is not supported in NeXus")
elseif linearity is "raw" then no scaling information needs to be stored for this row
else throw Exception("Unrecognised linearity")
*/
static int process_ArrayIntensitiesCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const rcache,
void * const tcache,
const unsigned int row)
{
int error = CBF_SUCCESS;
ArrayIntensitiesCache * const c = rcache;
CBF_UNUSED(row);
if (!category || !nx || !key || !c || tcache) {
error |= CBF_ARGUMENT;
} else {
/*
I have extracted all the information from a relevant row,
now I need to convert it to nexus format and write it.
*/
if (!c->linearity) {
cbf_debug_print("error: 'array_intensities.linearity' not found");
error |= CBF_NOTFOUND;
} else {
hsize_t max[] = {H5S_UNLIMITED};
hsize_t cnk[] = {1};
hsize_t buf[] = {0};
hsize_t data_offset[1];
hsize_t count[] = {1};
data_offset[0] = nx->slice;
if (!cbf_cistrcmp(c->linearity,"linear")) c->scaling = 1.0/c->gain;
if (!cbf_cistrcmp(c->linearity,"linear") || !cbf_cistrcmp(c->linearity,"offset") || !cbf_cistrcmp(c->linearity,"scaling") || !cbf_cistrcmp(c->linearity,"scaling_offset")) {
hid_t dset_offset = CBF_H5FAIL;
hid_t dset_scale = CBF_H5FAIL;
CBF_CALL(cbf_H5Drequire(nx->nxdetectors[nx->cur_detector],&dset_offset,"offset",1,max,cnk,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dinsert(dset_offset,data_offset,0,count,buf,&c->offset,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Drequire(nx->nxdetectors[nx->cur_detector],&dset_scale,"scaling_factor",1,max,cnk,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dinsert(dset_scale,data_offset,0,count,buf,&c->scaling,H5T_NATIVE_DOUBLE));
cbf_H5Dfree(dset_offset);
cbf_H5Dfree(dset_scale);
} else if (!cbf_cistrcmp(c->linearity,"raw")) {
/* no-op! */
} else if (!cbf_cistrcmp(c->linearity,"sqrt_scaled") || !cbf_cistrcmp(c->linearity,"logarithmic_scaled")) {
cbf_debug_print3("error: %s %s\n","unsupported 'array_intensities.linearity' value:",c->linearity);
} else {
cbf_debug_print3("error: %s %s\n","unrecognised 'array_intensities.linearity' value:",c->linearity);
error |= CBF_FORMAT;
}
}
}
return error;
}
/*
Define a type, constructor and setters to use when processing a single row of the 'array_structure_list' table.
*/
typedef struct ArrayStructureListCache
{
const char * axis_set_id;
const char * direction;
unsigned int dimension;
unsigned int index;
unsigned int precedence;
} ArrayStructureListCache;
static int ctor_ArrayStructureListCache
(void * const self)
{
int error = CBF_SUCCESS;
ArrayStructureListCache * const c = self;
if (!c) {
error |= CBF_ARGUMENT;
} else {
c->axis_set_id = NULL;
c->direction = NULL;
c->dimension = 0;
c->index = 0;
c->precedence = 0;
}
return error;
}
DECL_CACHE_STR_SETTER(cache_ArrayStructureList_axis_set_id, ArrayStructureListCache, axis_set_id)
DECL_CACHE_STR_SETTER(cache_ArrayStructureList_direction, ArrayStructureListCache, direction)
DECL_CACHE_UINT_SETTER(cache_ArrayStructureList_dimension, ArrayStructureListCache, dimension)
DECL_CACHE_UINT_SETTER(cache_ArrayStructureList_index, ArrayStructureListCache, index)
DECL_CACHE_UINT_SETTER(cache_ArrayStructureList_precedence, ArrayStructureListCache, precedence)
/*
Process data from a single row of 'array_structure_list'.
Picks a name for the axis set based on its precedence, constructs a path for it and then caches
that data in the given 'key'.
*/
static int process_ArrayStructureListCache
(cbf_node * const category,
cbf_h5handle nx,
cbf_cbf2nx_key_t * const key,
void * const rcache,
void * const tcache,
const unsigned int row)
{
int error = CBF_SUCCESS;
ArrayStructureListCache * const c = rcache;
CBF_UNUSED(row);
if (!category || !nx || !key || !c || tcache) {
error |= CBF_ARGUMENT;
} else {
const char empty_string[] = "";
const char pixel_y[] = "y_pixel_offset";
const char pixel_x[] = "x_pixel_offset";
const char * axis_names[3];
axis_names[0] = empty_string;
axis_names[1] = pixel_x;
axis_names[2] = pixel_y;
if (c->precedence < 1 || c->precedence > 2) {
/* I don't have axis names defined outside of this range */
cbf_debug_print("error: 'precedence' isn't in the range of valid values");
error |= CBF_FORMAT;
} else {
const char * axis_path;
const char * taxis_path;
if (cbf_get_NX_axis_path(nx,axis_names[c->precedence],&taxis_path)){
CBF_CALL(cbf_h5handle_require_detector(nx,0,0));
if (!error) {
const char * path_parts[6];
path_parts[0] = empty_string;
path_parts[1] = nx->nxid_name;
path_parts[2] = nx->nxinstrument_name;
path_parts[3] = nx->nxdetector_names[nx->cur_detector];
/* select the correct axis name */
path_parts[4] = axis_names[c->precedence];
path_parts[5] = 0;
axis_path = _cbf_str_join(path_parts,'/');
CBF_CALL(
_cbf_insert_arrayAxisSet(
&key->arrayAxisSet,
c->axis_set_id,
axis_path,
c->index,
c->precedence,
c->dimension,
c->direction
)
);
free((void*)axis_path);
}
} else {
axis_path = _cbf_strdup(taxis_path);
cbf_debug_print3("Mapped axis path for axis '%s' to '%s'\n",axis_names[c->precedence],axis_path);
/* try to transfer ownership of the path to the table-level cache */
CBF_CALL(
_cbf_insert_arrayAxisSet(
&key->arrayAxisSet,
c->axis_set_id,
axis_path,
c->index,
c->precedence,
c->dimension,
c->direction
)
);
free((void*)axis_path);
}
}
}
return error;
}
/*
Extract a fixed-length string value from 'column[row]', apply an optional
filter found in 'conv' and store it in 'nx'. The group, field name and format of the
data in 'nx' is found in 'conv'.
*/
static int cbf_convert_cbf2nx_flstr
(cbf_node * const column,
const unsigned int row,
cbf_h5handle nx,
const cbf2nx_item_datamap_t * conv)
{
int error = CBF_SUCCESS;
if (!column) {
cbf_debug_print("error: invalid column given");
error |= CBF_ARGUMENT;
} else if (row >= column->children) {
cbf_debug_print("error: invalid row given");
error |= CBF_ARGUMENT;
} else if (!nx) {
cbf_debug_print("error: invalid hdf5 handle given");
error |= CBF_ARGUMENT;
} else if (!conv) {
cbf_debug_print("error: invalid conversion data given");
error |= CBF_ARGUMENT;
} else {
hid_t obj = CBF_H5FAIL;
hid_t dset = CBF_H5FAIL;
const char * in = NULL;
const char * _out = NULL;
const char * out = NULL;
CBF_CALL(conv->get_object(nx,&obj,NULL));
CBF_CALL(cbf_node_get_value(column, row, &in));
if (conv->filter) {
filter_string_func filter = (filter_string_func)(conv->filter);
CBF_CALL(filter(in,&_out));
out = _out;
} else {
out = in;
}
if (0 != conv->rank) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_NOTIMPLEMENTED));
error |= CBF_NOTIMPLEMENTED;
}
CBF_CALL(cbf_H5Drequire_flstring(obj,&dset,conv->name,out));
if (conv->units) CBF_CALL(cbf_H5Arequire_string(dset,"units",conv->units));
cbf_H5Dfree(dset);
free((void*)_out);
}
return error;
}
/*
Extract a variable-length string value from 'column[row]', apply an optional
filter found in 'conv' and store it in 'nx'. The group, field name and format of the
data in 'nx' is found in 'conv'.
*/
static int cbf_convert_cbf2nx_vlstr
(cbf_node * const column,
const unsigned int row,
cbf_h5handle nx,
const cbf2nx_item_datamap_t * const conv)
{
int error = CBF_SUCCESS;
if (!column) {
cbf_debug_print2("error: %s\n","invalid column given");
error |= CBF_ARGUMENT;
} else if (row >= column->children) {
cbf_debug_print2("error: %s\n","invalid row given");
error |= CBF_ARGUMENT;
} else if (!nx) {
cbf_debug_print2("error: %s\n","invalid hdf5 handle given");
error |= CBF_ARGUMENT;
} else if (!conv) {
cbf_debug_print2("error: %s\n","invalid conversion data given");
error |= CBF_ARGUMENT;
} else {
hid_t obj = CBF_H5FAIL;
hid_t dset = CBF_H5FAIL;
hid_t vlstr = CBF_H5FAIL;
const char * in = NULL;
const char * _out = NULL;
const char * out = NULL;
CBF_CALL(conv->get_object(nx,&obj,NULL));
CBF_CALL(cbf_H5Tcreate_string(&vlstr,H5T_VARIABLE));
CBF_CALL(cbf_node_get_value(column, row, &in));
if (conv->filter) {
filter_string_func filter = (filter_string_func)(conv->filter);
CBF_CALL(filter(in,&_out));
out = _out;
} else {
out = in;
}
if (0==conv->rank) {
CBF_CALL(cbf_H5Drequire(obj,&dset,conv->name,0,NULL,NULL,NULL,vlstr));
CBF_CALL(cbf_H5Dwrite2(dset,NULL,NULL,NULL,&out,vlstr));
} else if (1==conv->rank) {
const hsize_t max[] = {H5S_UNLIMITED};
const hsize_t cnk[] = {1};
hsize_t off[1];
const hsize_t cnt[] = {1};
hsize_t buf[] = {0};
off[0] = nx->slice;
CBF_CALL(cbf_H5Drequire(obj,&dset,conv->name,1,max,cnk,buf,vlstr));
CBF_CALL(cbf_H5Dinsert(dset,off,NULL,cnt,buf,&out,vlstr));
} else {
cbf_debug_print("error: unrecognised/not implemented data rank");
}
if (conv->units) CBF_CALL(cbf_H5Arequire_string(dset,"units",conv->units));
cbf_H5Dfree(dset);
cbf_H5Tfree(vlstr);
free((void*)_out);
}
return error;
}
/*
Extract a double-precision floating-point value from 'column[row]', apply an optional
filter found in 'conv' and store it in 'nx'. The group, field name and format of the
data in 'nx' is found in 'conv'.
*/
static int cbf_convert_cbf2nx_double
(cbf_node * const column,
const unsigned int row,
cbf_h5handle nx,
const cbf2nx_item_datamap_t * conv)
{
int error = CBF_SUCCESS;
if (!column) {
cbf_debug_print("error: invalid column given");
error |= CBF_ARGUMENT;
} else if (row >= column->children) {
cbf_debug_print("error: invalid row given");
error |= CBF_ARGUMENT;
} else if (!nx) {
cbf_debug_print("error: invalid hdf5 handle given");
error |= CBF_ARGUMENT;
} else if (!conv) {
cbf_debug_print("error: invalid conversion data given");
error |= CBF_ARGUMENT;
} else {
hid_t obj = CBF_H5FAIL;
double in = 0., out = 0.;
hid_t dset = CBF_H5FAIL;
#ifdef CBF_USE_ULP
cmp_double_param_t cmp_double_params;
void * cmp_params = &cmp_double_params;
/* set up the comparison parameters */
cmp_double_params.cmp_double_as_float = cbf_has_ULP64() ? nx->cmp_double_as_float : 1;
cmp_double_params.ulp32 = nx->float_ulp;
#ifndef NO_UINT64_TYPE
cmp_double_params.ulp64 = nx->double_ulp;
#endif
#else
void * cmp_params = 0;
CBF_UNUSED(cmp_params);
#endif
/* it looks like I might be able to convert the data */
CBF_CALL(conv->get_object(nx,&obj,NULL));
CBF_CALL(cbf_node_get_doublevalue(column, row, &in));
if (conv->filter) {
filter_double_func filter = (filter_double_func)(conv->filter);
CBF_CALL(filter(in,&out));
} else {
out = in;
}
if (0==conv->rank) {
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(obj,&dset,conv->name,out,cmp_double,cmp_params));
} else if (1==conv->rank) {
const hsize_t max[] = {H5S_UNLIMITED};
const hsize_t cnk[] = {1};
hsize_t off[1];
const hsize_t cnt[] = {1};
hsize_t buf[] = {0};
off[0] = nx->slice;
CBF_CALL(cbf_H5Drequire(obj,&dset,conv->name,1,max,cnk,buf,H5T_IEEE_F64LE));
if ((error|=cbf_H5Dinsert(dset,off,NULL,cnt,buf,&out,H5T_NATIVE_DOUBLE))) {
cbf_debug_print ("cbf_H5Dinsert failed ");
}
} else {
cbf_debug_print("error: unrecognised/not implemented data rank");
}
if (conv->units) CBF_CALL(cbf_H5Arequire_string(dset,"units",conv->units));
cbf_H5Dfree(dset);
}
return error;
}
/*
Convert two strings to integers and compare them for equality.
If one can't be converted then the most negative representable value should be returned,
instead of any value near zero.
Used to compare binary id values for equality.
*/
static int strintcmp
(const char * const s1,
const char * const s2)
{
if (!s1 || !s2) {
return INT_MIN;
} else {
char * s1_end = NULL;
char * s2_end = NULL;
const int s1_int = strtol(s1,&s1_end,0);
const int s2_int = strtol(s2,&s2_end,0);
if (s1==s1_end || s2==s2_end) {
return INT_MIN;
} else {
return s1_int-s2_int;
}
}
}
static cbf_primary_key_t array_data_key[3] = {
{"array_id",cbf2nx_key_get_array_id,strcmp},
{"binary_id",cbf2nx_key_get_binary_id,strintcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t array_data_map[2] = {
{"data",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayData_data}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t array_intensities_key[] = {
{"array_id",cbf2nx_key_get_array_id,strcmp},
{"binary_id",cbf2nx_key_get_binary_id,strintcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t array_intensities_map[] = {
{"gain",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayIntensities_gain}}},
{"linearity",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayIntensities_linearity}}},
{"offset",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayIntensities_offset}}},
{"overload",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_overload}}},
{"scaling",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayIntensities_scaling}}},
{"undefined_value",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_undefined}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t array_structure_key[] = {
{"id",cbf2nx_key_get_array_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t array_structure_map[] = {
{"byte_order",CBF_MAP_NONE,NULL},
{"compression_type",CBF_MAP_NONE,NULL},
{"encoding_type",CBF_MAP_NONE,NULL},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t array_structure_list_key[] = {
{"array_id",cbf2nx_key_get_array_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t array_structure_list_map[] = {
{"axis_set_id",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayStructureList_axis_set_id}}},
{"dimension",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayStructureList_dimension}}},
{"direction",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayStructureList_direction}}},
{"index",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayStructureList_index}}},
{"precedence",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_ArrayStructureList_precedence}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_key[] = {
{"id",cbf2nx_key_get_diffrn_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_map[] = {
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_data_frame_keys[] = {
{"id",cbf2nx_key_get_frame_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_data_frame_map[] = {
{"array_id",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_array}}},
{"binary_id",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_binary}}},
{"details",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_vlstr,{"CBF_diffrn_data_frame__details",NULL,cbf_h5handle_require_detector,NULL,1}}}},
{"detector_element_id",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_diffrn_detector_element}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_detector_keys[] = {
{"id",cbf2nx_key_get_diffrnDetector_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_detector_map[] = {
{"details",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"details",NULL,cbf_h5handle_require_detector,NULL,0}}}},
{"detector",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"type",NULL,cbf_h5handle_require_detector,NULL,0}}}},
{"diffrn_id",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_diffrn}}},
{"dtime",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"dead_time","us",cbf_h5handle_require_detector,NULL,0}}}},
{"gain_setting",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"gain_setting",NULL,cbf_h5handle_require_detector,NULL,0}}}},
{"layer_thickness",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"sensor_thickness","mm",cbf_h5handle_require_detector,NULL,0}}}},
{"number_of_axes",CBF_MAP_NONE,NULL},
{"sensor_material",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"sensor_material",NULL,cbf_h5handle_require_detector,NULL,0}}}},
{"threshold",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"threshold_energy","eV",cbf_h5handle_require_detector,NULL,0}}}},
{"type",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"description",NULL,cbf_h5handle_require_detector,NULL,0}}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_detector_axis_keys[] = {
{"detector_id",cbf2nx_key_get_diffrnDetector_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_detector_axis_map[] = {
{"axis_id",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnDetectorAxis_axis}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_detector_element_keys[] = {
{"id",cbf2nx_key_get_diffrnDetectorElement_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_detector_element_map[] = {
{"center[1]",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"beam_center_x","mm",cbf_h5handle_require_detector,NULL,1}}}},
{"center[2]",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"beam_center_y","mm",cbf_h5handle_require_detector,NULL,1}}}},
{"detector_id",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_diffrn_detector}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_measurement_keys[] = {
{"diffrn_id",cbf2nx_key_get_diffrn_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_measurement_map[] = {
{"details",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"details",NULL,cbf_h5handle_require_goniometer,NULL,0}}}},
{"device",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"local_name",NULL,cbf_h5handle_require_goniometer,NULL,0}}}},
{"device_details",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"description",NULL,cbf_h5handle_require_goniometer,NULL,0}}}},
{"device_type",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"type",NULL,cbf_h5handle_require_goniometer,NULL,0}}}},
{"id",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_diffrn_measurement}}},
{"method",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"method",NULL,cbf_h5handle_require_entry,NULL,0}}}},
{"number_of_axes",CBF_MAP_NONE,NULL},
{"sample_detector_distance",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"distance","mm",cbf_h5handle_require_detector,NULL,1}}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_measurement_axis_keys[] = {
{"measurement_id",cbf2nx_key_get_diffrnMeasurement_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_measurement_axis_map[] = {
/* should also have 'measurement_device' somewhere, eventuallly */
{"axis_id",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnMeasurementAxis_axis}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_radiation_keys[] = {
{"diffrn_id",cbf2nx_key_get_diffrn_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_radiation_map[] = {
{"collimation",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"collimation",NULL,cbf_h5handle_require_beam,NULL,0}}}},
{"div_x_source",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"incident_divergence_x","degrees",cbf_h5handle_require_beam,NULL,0}}}},
{"div_x_y_source",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"incident_divergence_xy","degrees^2",cbf_h5handle_require_beam,NULL,0}}}},
{"div_y_source",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"incident_divergence_y","degrees",cbf_h5handle_require_beam,NULL,0}}}},
{"inhomogeneity",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"beam_size_x","mm",cbf_h5handle_require_beam,(filter_func)(filter_inhomogeneity),0}}}},
{"inhomogeneity",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"beam_size_y","mm",cbf_h5handle_require_beam,(filter_func)(filter_inhomogeneity),0}}}},
{"monochromator",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"description",NULL,cbf_h5handle_require_monochromator,NULL,0}}}},
{"polarizn_source_norm",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_PSN}}},
{"polarizn_source_ratio",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_PSR}}},
{"polarizn_source_norm_esd",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_PSN_ESD}}},
{"polarizn_source_ratio_esd",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_PSR_ESD}}},
{"probe",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_flstr,{"probe",NULL,cbf_h5handle_require_source,NULL,0}}}},
{"polarizn_Stokes_I",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_I}}},
{"polarizn_Stokes_Q",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_Q}}},
{"polarizn_Stokes_U",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_U}}},
{"polarizn_Stokes_V",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_V}}},
{"polarizn_Stokes_I_esd",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_I_ESD}}},
{"polarizn_Stokes_Q_esd",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_Q_ESD}}},
{"polarizn_Stokes_U_esd",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_U_ESD}}},
{"polarizn_Stokes_V_esd",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnRadiation_V_ESD}}},
{"wavelength_id",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_wavelength_id}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_radiation_wavelength_keys[] = {
{"id",cbf2nx_key_get_wavelength_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_radiation_wavelength_map[] = {
{"wavelength",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"incident_wavelength","angstroms",cbf_h5handle_require_beam,NULL,1}}}},
{"wt",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"weight",NULL,cbf_h5handle_require_beam,NULL,1}}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_refln_key[] = {
{"frame_id",cbf2nx_key_get_frame_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_refln_map[] = {
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_scan_keys[] = {
{"id",cbf2nx_key_get_scan_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_scan_map[] = {
{"date_start",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_vlstr,{"start_time",NULL,cbf_h5handle_require_entry,NULL,0}}}},
{"date_end",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_vlstr,{"end_time",NULL,cbf_h5handle_require_entry,NULL,0}}}},
{"frame_id_start",CBF_MAP_NONE,NULL},
{"frame_id_end",CBF_MAP_NONE,NULL},
{"frames",CBF_MAP_NONE,NULL},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_scan_axis_keys[] = {
{"scan_id",cbf2nx_key_get_scan_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_scan_axis_map[] = {
{"angle_increment",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanAxis_angleIncr}}},
{"angle_rstrt_incr",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanAxis_angleRstrtIncr}}},
{"angle_start",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanAxis_angleStrt}}},
{"axis_id",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanAxis_axis}}},
{"displacement_increment",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanAxis_dispIncr}}},
{"displacement_rstrt_incr",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanAxis_dispRstrtIncr}}},
{"displacement_start",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanAxis_dispStrt}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_scan_frame_keys[] = {
{"scan_id",cbf2nx_key_get_scan_id,strcmp},
{"frame_id",cbf2nx_key_get_frame_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_scan_frame_map[] = {
{"date",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_vlstr,{"frame_start_time",NULL,cbf_h5handle_require_detector,NULL,1}}}},
{"frame_number",CBF_MAP_KEY,(cbf2nx_set_key_t[]){{cbf2nx_key_set_frame_number}}},
{"time_period",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"frame_time","s",cbf_h5handle_require_detector,NULL,1}}}},
/* deprecated erroneous use of integration period will be supported for read with a warning */
{"integration_period",CBF_MAP_DATA_DEPRECATED,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"frame_time","s",cbf_h5handle_require_detector,NULL,1}}}},
{"integration_time",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"count_time","s",cbf_h5handle_require_detector,NULL,1}}}},
/* deprecated erroneous use of exposure will be supported for read with a warning */
{"exposure_time",CBF_MAP_DATA_DEPRECATED,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"count_time","s",cbf_h5handle_require_detector,NULL,1}}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_scan_frame_axis_keys[] = {
{"frame_id",cbf2nx_key_get_frame_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_scan_frame_axis_map[] = {
{"angle",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanFrameAxis_angle}}},
{"axis_id",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanFrameAxis_axis}}},
{"displacement",CBF_MAP_CACHE,(cbf2nx_cache_item_t[]){{cache_DiffrnScanFrameAxis_disp}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf_primary_key_t diffrn_source_keys[] = {
{"diffrn_id",cbf2nx_key_get_diffrn_id,strcmp},
{NULL,NULL,NULL},
};
static cbf2nx_column_map_t diffrn_source_map[] = {
{"current",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"current","mA",cbf_h5handle_require_source,NULL,0}}}},
{"power",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"power","kW",cbf_h5handle_require_source,NULL,0}}}},
{"source",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_vlstr,{"type",NULL,cbf_h5handle_require_source,NULL,0}}}},
{"target",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_vlstr,{"target_material",NULL,cbf_h5handle_require_source,NULL,0}}}},
{"type",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_vlstr,{"name",NULL,cbf_h5handle_require_source,NULL,0}}}},
{"voltage",CBF_MAP_DATA,(cbf2nx_convert_t[]){{cbf_convert_cbf2nx_double,{"voltage","kV",cbf_h5handle_require_source,NULL,0}}}},
{NULL,CBF_MAP_NONE,NULL},
};
static cbf2nx_category_map_t cbf_map[] = {
{
"array_data",
array_data_key,
array_data_map,
{sizeof(ArrayDataCache), ctor_ArrayDataCache, NULL},
process_ArrayDataCache,
{0, NULL, NULL}, NULL,
},
{
"array_intensities",
array_intensities_key,
array_intensities_map,
{sizeof(ArrayIntensitiesCache), ctor_ArrayIntensitiesCache, NULL},
process_ArrayIntensitiesCache,
{0, NULL, NULL}, NULL,
},
{
"array_structure",
array_structure_key,
array_structure_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"array_structure_list",
array_structure_list_key,
array_structure_list_map,
{sizeof(ArrayStructureListCache), ctor_ArrayStructureListCache, NULL},
process_ArrayStructureListCache,
{0, NULL, NULL}, NULL,
},
{
"diffrn",
diffrn_key,
diffrn_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_data_frame",
diffrn_data_frame_keys,
diffrn_data_frame_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_detector",
diffrn_detector_keys,
diffrn_detector_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_detector_axis",
diffrn_detector_axis_keys,
diffrn_detector_axis_map,
{sizeof(DiffrnDetectorAxisRCache), ctor_DiffrnDetectorAxisRCache, NULL},
process_DiffrnDetectorAxisRCache,
{sizeof(DiffrnDetectorAxisTCache), ctor_DiffrnDetectorAxisTCache, dtor_DiffrnDetectorAxisTCache},
process_DiffrnDetectorAxisTCache,
},
{
"diffrn_detector_element",
diffrn_detector_element_keys,
diffrn_detector_element_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_measurement",
diffrn_measurement_keys,
diffrn_measurement_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_measurement_axis",
diffrn_measurement_axis_keys,
diffrn_measurement_axis_map,
{sizeof(DiffrnMeasurementAxisRCache), ctor_DiffrnMeasurementAxisRCache, NULL},
process_DiffrnMeasurementAxisRCache,
{sizeof(DiffrnMeasurementAxisTCache), ctor_DiffrnMeasurementAxisTCache, dtor_DiffrnMeasurementAxisTCache},
process_DiffrnMeasurementAxisTCache,
},
{
"diffrn_radiation",
diffrn_radiation_keys,
diffrn_radiation_map,
{sizeof(DiffrnRadiationRowCache), ctor_DiffrnRadiationRowCache, NULL},
process_DiffrnRadiationRowCache,
{0, NULL, NULL}, NULL,
},
{
"diffrn_radiation_wavelength",
diffrn_radiation_wavelength_keys,
diffrn_radiation_wavelength_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_refln",
diffrn_refln_key,
diffrn_refln_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_scan",
diffrn_scan_keys,
diffrn_scan_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_scan_axis",
diffrn_scan_axis_keys,
diffrn_scan_axis_map,
{sizeof(DiffrnScanAxisCache), ctor_DiffrnScanAxisCache, NULL},
process_DiffrnScanAxisCache,
{0, NULL, NULL}, NULL,
},
{
"diffrn_scan_frame",
diffrn_scan_frame_keys,
diffrn_scan_frame_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
"diffrn_scan_frame_axis",
diffrn_scan_frame_axis_keys,
diffrn_scan_frame_axis_map,
{sizeof(DiffrnScanFrameAxisCache), ctor_DiffrnScanFrameAxisCache, NULL},
process_DiffrnScanFrameAxisCache,
{0, NULL, NULL}, NULL,
},
{
"diffrn_source",
diffrn_source_keys,
diffrn_source_map,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
{
NULL, NULL, NULL,
{0, NULL, NULL}, NULL,
{0, NULL, NULL}, NULL,
},
};
/*
Search a list of category mappings for one with a given name, returning the mapping data if found or NULL otherwise.
*/
static const cbf2nx_category_map_t * cbf2nx_find_category_mapping
(const cbf2nx_category_map_t * catmap, /*< the list of category mappings to search within */
const char * const name /*< the name of the category mapping to search for */)
{
if (!catmap || !name) {
return NULL;
} else {
for (; catmap->name; ++catmap) {
if (!strcmp(catmap->name,name)) {
return catmap;
}
}
return NULL;
}
}
/*
Write some data to a hdf5 handle according to the conversion method(s) specified in the given mapping.
Searches the given column mapping array for the method to map the given column, extracts the relevant
information from the mapping data and applies it, which usually involves forwarding to a function with
a signature which depends on the type of conversion to be performed.
This iterates over all columns in the mapping to allow multiple conversions to be applied for a single
column.
*/
static int cbf2nx_apply_conversions
(cbf_h5handle h5handle,
cbf_cbf2nx_key_t * const key,
const cbf2nx_column_map_t * const colmap_begin,
cbf_node * const column,
const unsigned int row,
void * const row_cache,
const int list,
const unsigned int matched)
{
int error = CBF_SUCCESS;
CBF_UNUSED( list );
if (!key || !column) {
error |= CBF_ARGUMENT;
} else {
int in_list = 0;
const unsigned int indent = 1+key->indent;
if (colmap_begin) {
const cbf2nx_column_map_t * colmap;
for (colmap = colmap_begin; CBF_SUCCESS==error && colmap->name; ++colmap) {
if (!cbf_cistrcmp(colmap->name,column->name)) {
int state = 3;
in_list = 1;
switch (colmap->type) {
case CBF_MAP_NONE: {
state = 2;
break;
}
case CBF_MAP_DATA_DEPRECATED: {
if (h5handle->logfile && !matched) {
fprintf(h5handle->logfile,"Deprecated CBF tag '%s' \n", column->name);
}
}
case CBF_MAP_DATA: {
const cbf2nx_convert_t * const conversion = (const cbf2nx_convert_t *)(colmap->data);
state = 1;
if (!conversion) {
cbf_debug_print("error: malformed mapping method");
error |= CBF_FORMAT;
} else {
CBF_CALL(conversion->convert(column,row,h5handle,&conversion->data));
}
break;
}
case CBF_MAP_KEY: {
const cbf2nx_set_key_t * const set_key = (const cbf2nx_set_key_t *)(colmap->data);
state = 1;
if (!set_key) {
cbf_debug_print("error: malformed mapping method");
error |= CBF_FORMAT;
} else {
const char * val = NULL;
CBF_CALL(cbf_node_get_value(column, row, &val));
CBF_CALL(set_key->set_key(key,val));
}
break;
}
case CBF_MAP_CACHE: {
const cbf2nx_cache_item_t * const cache_item = (const cbf2nx_cache_item_t *)(colmap->data);
state = 1;
if (!cache_item) {
cbf_debug_print("error: malformed mapping method");
error |= CBF_FORMAT;
} else {
CBF_CALL(cache_item->cache_item(column,row,row_cache));
}
break;
}
default: {
state = 1;
cbf_debug_print("error: invalid mapping method");
error |= CBF_FORMAT;
}
}
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,state);
}
}
}
if (!in_list && h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,0);
}
return error;
}
/*
Define a type to hold data for a single row of the 'axis' category.
The validity of string types can be tested by comparison to 'NULL',
the validity of vector types can be tested by checking if the number
of elements found (in 'nComponent') is 3.
*/
typedef struct cbf_axis_data_t
{
const char * id;
const char * depends_on;
const char * equipment;
const char * equipment_component;
const char * rotation_axis;
double rotation;
const char * type;
double offset[3];
double vector[3];
unsigned int nOffset;
unsigned int nVector;
int flags;
} cbf_axis_data_t;
/*
Generate a set of reasonable initial values for 'cbf_axis_data_t',
used to initialise and reset objects of that type.
*/
static cbf_axis_data_t cbf_axis_data_init( void )
{
return (cbf_axis_data_t) {NULL,NULL,NULL,NULL,NULL,0.,NULL,{0.,0.,0.},{0.,0.,0.},0,0,0};
}
/*
Read a given row of data from the given axis category, after initialising
the 'data' struct to sensible default values. Unrecognised columns will
cause a warning to be printed, but are otherwise ignored rather than causing
an error. Missing columns are not logged.
\return An error code.
*/
static int cbf_read_axis_row
(cbf_axis_data_t * const data /*< The location where extracted data should be stored. */,
cbf_node * const category /*< The category to extract data from. */,
const unsigned int row /*< The row to extract data from. */)
{
int error = CBF_SUCCESS;
/* check that the arguments are sensible */
if (!data || !category) {
error |= CBF_ARGUMENT;
} else {
cbf_node * const * column_it;
/* initialise the 'axis_data' type to reasonable defaults */
*data = cbf_axis_data_init();
/* iterate through all columns, storing values in the 'axis_data' type for later use */
for (column_it = category->child; !error && column_it != category->children+category->child; ++column_it) {
cbf_node * const column = *column_it;
if (!strcmp(column->name,"id")) {
CBF_CALL(cbf_node_get_value(column, row, &data->id));
} else if (!strcmp(column->name,"depends_on")) {
CBF_CALL(cbf_node_get_value(column, row, &data->depends_on));
} else if (!strcmp(column->name,"equipment")) {
CBF_CALL(cbf_node_get_value(column, row, &data->equipment));
} else if (!strcmp(column->name,"equipment_component")) {
CBF_CALL(cbf_node_get_value(column, row, &data->equipment_component));
} else if (!strcmp(column->name,"rotation_axis")) {
CBF_CALL(cbf_node_get_value(column, row, &data->rotation_axis));
} else if (!strcmp(column->name,"rotation")) {
CBF_CALL(cbf_node_get_doublevalue(column, row, &data->rotation));
} else if (!strcmp(column->name,"type")) {
CBF_CALL(cbf_node_get_value(column, row, &data->type));
} else if (!strcmp(column->name,"offset[1]")) {
CBF_CALL(cbf_node_get_doublevalue(column, row, 0+data->offset));
if (!error) ++data->nOffset;
} else if (!strcmp(column->name,"offset[2]")) {
CBF_CALL(cbf_node_get_doublevalue(column, row, 1+data->offset));
if (!error) ++data->nOffset;
} else if (!strcmp(column->name,"offset[3]")) {
CBF_CALL(cbf_node_get_doublevalue(column, row, 2+data->offset));
if (!error) ++data->nOffset;
} else if (!strcmp(column->name,"vector[1]")) {
CBF_CALL(cbf_node_get_doublevalue(column, row, 0+data->vector));
if (!error) ++data->nVector;
} else if (!strcmp(column->name,"vector[2]")) {
CBF_CALL(cbf_node_get_doublevalue(column, row, 1+data->vector));
if (!error) ++data->nVector;
} else if (!strcmp(column->name,"vector[3]")) {
CBF_CALL(cbf_node_get_doublevalue(column, row, 2+data->vector));
if (!error) ++data->nVector;
} else {
cbf_debug_print2("warning: unrecognised column in 'axis' table:\n %s\n",column->name);
}
}
}
return error;
}
/*
This function iterates through stored axis sets, it doesn't iterate of a subset of rows within a table,
so it shouldn't be forced into the same processing model as the conversion functions which do.
See the comments for a description of what this function does.
*/
static int cbf_write_cbf2nx__array_structure_list_axis
(cbf_handle handle,
cbf_h5handle h5handle,
cbf_cbf2nx_key_t * const key,
const int list)
{
int error = CBF_SUCCESS;
unsigned int matched = 0;
const char categoryName[] = "array_structure_list_axis";
#ifdef CBF_USE_ULP
cmp_double_param_t cmp_double_params;
void * cmp_params = &cmp_double_params;
CBF_UNUSED(list);
/* set up the comparison parameters */
cmp_double_params.cmp_double_as_float = cbf_has_ULP64() ? h5handle->cmp_double_as_float : 1;
cmp_double_params.ulp32 = h5handle->float_ulp;
#ifndef NO_UINT64_TYPE
cmp_double_params.ulp64 = h5handle->double_ulp;
#endif
#else
void * cmp_params = 0;
CBF_UNUSED(list);
CBF_UNUSED(cmp_params);
#endif
/* check arguments */
if (!handle) {
cbf_debug_print("Invalid handle given\n");
error |= CBF_ARGUMENT;
} else if (!h5handle) {
cbf_debug_print("Invalid hdf5 handle given\n");
error |= CBF_ARGUMENT;
} else if
(!key ||
!key->arrayAxisSet.axis_set_id ||
!key->arrayAxisSet.direction ||
!key->arrayAxisSet.precedence ||
0==key->arrayAxisSet.count)
{
cbf_debug_print("Bad key given\n");
error |= CBF_ARGUMENT;
} else if (CBF_SUCCESS!=(error|=cbf_find_category(handle,categoryName))) {
cbf_debug_print3("error: couldn't find category '%s': %s\n",categoryName,cbf_strerror(error));
} else {
cbf_node * const category = handle->node;
cbf_node * primary_keys[1];
const unsigned int npk = sizeof(primary_keys)/sizeof(*primary_keys);
unsigned int indent = 1+key->indent;
unsigned int * axes = NULL;
unsigned int nRows;
unsigned int set;
if (h5handle->logfile) _cbf_write_name(h5handle->logfile,categoryName,0,key->indent,1);
if (key->categories) {
cbf_node * * it;
cbf_node * const * const end = key->nCat+key->categories;
for (it = key->categories; end != it; ++it) {
if (*it) {
*it = strcmp((*it)->name,categoryName) ? *it : NULL;
}
}
}
/* check that I have some children, and try to find the primary key columns */
if (0==category->children) {
cbf_debug_print2("error: %s\n","category has no children");
error |= CBF_NOTFOUND;
} else {
nRows = category->child[0]->children;
axes = malloc(sizeof(unsigned int)*nRows);
CBF_CALL(cbf_find_child(primary_keys+0, category, "axis_set_id"));
}
/*
- iterate over axis sets
- count the number of axes in each set, caching them for later reference
- use the number of axes (and possibly other data) to test for valid data
*/
for (set = 0; CBF_SUCCESS==error && set != key->arrayAxisSet.count; ++set) {
unsigned int nAxes = 0;
unsigned int row;
for (row = 0; row != nRows; ++row) {
const char * set_id = NULL;
if (CBF_SUCCESS!=(error|=cbf_node_get_value(primary_keys[0], row, &set_id))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else if (!strcmp(key->arrayAxisSet.axis_set_id[set],set_id)) {
axes[nAxes++] = row;
}
}
/*
I know how many (and which) entries in ASLA relate to the current axis set:
- check the number of axes in the set
- extract and convert data as appropriate
*/
if (0 == nAxes) {
/* no axes - missing data? */
cbf_debug_print2("error: no axes found for set '%s'\n",key->arrayAxisSet.axis_set_id[set]);
error |= CBF_NOTFOUND;
} else if (1 == nAxes) {
/* 1 axis - most common case */
cbf_node * const * column_it;
double angle = 0., angle_incr = 0.;
int have_angle = 0, have_angle_incr = 0;
double disp = 0., disp_incr = 0.;
int have_disp = 0, have_disp_incr = 0;
double angular_pitch = 0., radial_pitch = 0.;
int have_angular_pitch = 0, have_radial_pitch = 0;
const char * axis = NULL;
for (column_it = category->child; CBF_SUCCESS==error && column_it != category->children+category->child; ++column_it) {
cbf_node * const column = *column_it;
int pk = 0;
cbf_node * const * it;
cbf_node * const * const end = npk+primary_keys;
for (it = primary_keys; end != it; ++it) {
pk = column==*it ? 1 : pk;
}
/*
I now have all the information required to read some relevant data:
- if a column is used as a primary key, ignore it but record it as processed
- if a column has a recognised name, perform the conversion
- otherwise, record the column as not processed and carry on
*/
if (pk) {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,1);
} else if (!cbf_cistrcmp(column->name,"angle")) {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,1);
CBF_CALL(cbf_node_get_doublevalue(column, axes[0], &angle));
if (!error) have_angle = 1;
} else if (!cbf_cistrcmp(column->name,"angle_increment")) {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,1);
CBF_CALL(cbf_node_get_doublevalue(column, axes[0], &angle_incr));
if (!error) have_angle_incr = 1;
} else if (!cbf_cistrcmp(column->name,"angular_pitch")) {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,1);
CBF_CALL(cbf_node_get_doublevalue(column, axes[0], &angular_pitch));
if (!error) have_angular_pitch = 1;
} else if (!cbf_cistrcmp(column->name,"axis_id")) {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,1);
CBF_CALL(cbf_node_get_value(column, axes[0], &axis));
} else if (!cbf_cistrcmp(column->name,"displacement")) {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,1);
CBF_CALL(cbf_node_get_doublevalue(column, axes[0], &disp));
if (!error) have_disp = 1;
} else if (!cbf_cistrcmp(column->name,"displacement_increment")) {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,1);
CBF_CALL(cbf_node_get_doublevalue(column, axes[0], &disp_incr));
if (!error) have_disp_incr = 1;
} else if (!cbf_cistrcmp(column->name,"radial_pitch")) {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,1);
CBF_CALL(cbf_node_get_doublevalue(column, axes[0], &radial_pitch));
if (!error) have_radial_pitch = 1;
} else {
if (h5handle->logfile && !matched) _cbf_write_name(h5handle->logfile,column->name,0,indent,0);
}
}
++matched;
if (CBF_SUCCESS == error) {
/*
I now have the data for the current row, use it to write the corresponding items to HDF5.
The exact comparison is correct; a difference of just 1 ulp from 0.0 implies that I need
to treat the setting as non-zero, because 0.0 is used as a signalling value instead of a
computed value. Compilers will complain about this: ignore them, they are wrong.
*/
if
(have_disp && have_disp_incr &&
(!have_angle || 0.==angle) && (!have_angle_incr || 0.==angle_incr) &&
(!have_angular_pitch || 0.==angular_pitch) && (!have_radial_pitch || 0.==radial_pitch))
{ /* it looks like a 1 dimensional displacement */
cbf_axis_data_t axis_settings = cbf_axis_data_init();
{ /* write the pixel sizes to NXdetector */
hid_t detector = CBF_H5FAIL;
hid_t dset = CBF_H5FAIL;
const char empty[] = "";
const char pixel_x[] = "x_pixel_size";
const char pixel_y[] = "y_pixel_size";
const char * p[3];
p[0] = empty;
p[1] = pixel_y;
p[2] = pixel_x;
CBF_CALL(cbf_h5handle_require_detector(h5handle,&detector,0));
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(detector,&dset,p[key->arrayAxisSet.precedence[set]],fabs(disp_incr),cmp_double,cmp_params));
CBF_CALL(cbf_H5Arequire_string(dset,"units","mm"));
cbf_H5Dfree(dset);
}
/* select the axis and extract the data it contains */
if (CBF_SUCCESS!=(error|=cbf_find_category(handle,"axis"))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
cbf_node * const axisCategory = handle->node;
if (CBF_SUCCESS!=(error|=cbf_find_column(handle,"id"))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else if (CBF_SUCCESS!=(error|=cbf_find_row(handle,axis))) {
cbf_debug_print2("error: problem finding '%s' in axis category\n",axis);
cbf_debug_print(cbf_strerror(error));
} else {
CBF_CALL(cbf_read_axis_row(&axis_settings,axisCategory,handle->row));
}
}
/* calculate the array of values the axis should have, and store the axis */
if (CBF_SUCCESS==error) {
double * const axisData = malloc(sizeof(double)*key->arrayAxisSet.dimension[set]);
if (!axisData) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_ALLOC));
error |= CBF_ALLOC;
} else {
unsigned int i;
const unsigned int dimension = key->arrayAxisSet.dimension[set];
const char * depends_on_path = NULL;
const char * rotation_axis_path = NULL;
hid_t dset = CBF_H5FAIL;
hsize_t dim[1];
const hsize_t max[] = {H5S_UNLIMITED};
hsize_t count[1];
const hsize_t offset[] = {0};
hsize_t buf[] = {0};
const int found = cbf_H5Dfind2(h5handle->hfile,&dset,key->arrayAxisSet.path[set],1,0,buf,H5T_IEEE_F64LE);
dim[0] = count[0] = dimension;
/* initialise the axisData array */
if (!key->arrayAxisSet.direction || !key->arrayAxisSet.direction[set]) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_UNDEFINED));
error |= CBF_UNDEFINED;
} else if (!strcmp(key->arrayAxisSet.direction[set],"increasing")) {
for (i = 0; i < dimension; ++i) axisData[i] = disp+i*disp_incr;
} else if (!strcmp(key->arrayAxisSet.direction[set],"decreasing")) {
for (i = 0; i < dimension; ++i) axisData[i] = disp+(dimension-i-1)*disp_incr;
} else {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_FORMAT));
error |= CBF_FORMAT;
}
/* ensure a suitable dataset exists, or create one */
if (CBF_SUCCESS==error) {
if (CBF_SUCCESS == found) {
/* TODO: check dataset dimensions */
double * const currData = malloc(sizeof(double)*dimension);
if (!currData) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_ALLOC));
error |= CBF_ALLOC;
}
CBF_CALL(cbf_H5Dread2(dset,offset,0,count,currData,H5T_NATIVE_DOUBLE));
if (CBF_SUCCESS==error) {
if (CBFM_cmp_double(axisData, currData, dimension, cmp_params)) {
cbf_debug_print("error: image axis data doesn't match");
error |= CBF_H5DIFFERENT;
}
}
free((void*)currData);
} else if (CBF_NOTFOUND == found) {
CBF_CALL(cbf_H5Dcreate(h5handle->hfile,&dset,key->arrayAxisSet.path[set],1,dim,max,count,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dwrite2(dset,offset,0,count,axisData,H5T_NATIVE_DOUBLE));
} else {
cbf_debug_print2("error: %s\n",cbf_strerror(found));
error |= found;
}
}
/*
Get dependency for current axis set:
- In the general case there may be multiple axes in the set, each with a dependency.
- I need to find the real dependencies in this function, because I should only know
enough about the axes in the set to be able to do that here.
- A dependency is always on an axis, but might imply dependency on a different set.
- Iff the parent axis is in a set then the dependency is on that set.
- As the dependency may be on an axis, all axes must already be processed.
- As the dependency may be on a set, all sets must have been assigned a path.
- Neglect possible complications caused by one axis being in multiple sets.
*/
if (CBF_SUCCESS==error) {
cbf_node * axis_id = NULL;
if (CBF_SUCCESS!=(error|=cbf_find_child(&axis_id, category, "axis_id"))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
const char * id = NULL;
unsigned int i;
/* search for the parent axis in ASLA.axis_id: */
for (i = 0; i != axis_id->children; ++i) {
if (CBF_SUCCESS!=(error|=cbf_node_get_value(axis_id, i, &id))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
if (!strcmp(id,axis_settings.depends_on)) break;
}
}
if (i != axis_id->children) {
/* the parent axis exists within an axis set - the dependency is the path for that set */
if (CBF_SUCCESS!=(error|=cbf_node_get_value(primary_keys[0], i, &id))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
for (i = 0; i != key->arrayAxisSet.count; ++i) {
if (!strcmp(key->arrayAxisSet.axis_set_id[i],id)) break;
}
if (i == key->arrayAxisSet.count) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_NOTFOUND));
error |= CBF_NOTFOUND;
} else {
depends_on_path = key->arrayAxisSet.path[i];
}
}
} else {
/* the parent axis is a free axis - the dependency is the path for that axis */
for (i = 0; i != key->axis.count; ++i) {
if (!strcmp(key->axis.axis_id[i],axis_settings.depends_on)) break;
}
if (i == key->axis.count) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_NOTFOUND));
error |= CBF_NOTFOUND;
} else {
depends_on_path = key->axis.path[i];
}
}
}
}
/* add/check attributes using the data extracted from the axis */
if (CBF_SUCCESS==error) {
CBF_CALL(cbf_H5Arequire_string(dset,"transformation_type","translation"));
CBF_CALL(cbf_H5Arequire_string(dset,"units","mm"));
}
if (CBF_SUCCESS==error && depends_on_path) {
CBF_CALL(cbf_H5Arequire_string(dset,"depends_on",depends_on_path));
}
if (CBF_SUCCESS==error && axis_settings.equipment) {
CBF_CALL(cbf_H5Arequire_string(dset,"equipment",axis_settings.equipment));
}
if (CBF_SUCCESS==error && axis_settings.equipment_component && _cbf_strlen(axis_settings.equipment_component)>0) {
CBF_CALL(cbf_H5Arequire_string(dset,"equipment_component",axis_settings.equipment_component));
}
if (CBF_SUCCESS==error && axis_settings.rotation_axis && _cbf_strlen(axis_settings.rotation_axis)>0) {
/* *** Change to a path instead of an axis name *** */
CBF_CALL(cbf_H5Arequire_string(dset,"rotation_axis",axis_settings.rotation_axis));
{
const hsize_t rdim[] = {1};
hsize_t rbuf[] = {0};
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"rotation",1,rdim,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,&(axis_settings.rotation),rbuf,cmp_double,cmp_params));
}
}
if (CBF_SUCCESS==error && axis_settings.nOffset==3) {
double offset[3];
const hsize_t vdim[] = {3};
hsize_t vbuf[] = {0};
CBF_CALL(cbf_apply_matrix(key->matrix,axis_settings.offset,offset));
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"offset",1,vdim,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,offset,vbuf,cmp_double,cmp_params));
CBF_CALL(cbf_H5Arequire_string(dset,"offset_units","mm"));
}
if (CBF_SUCCESS==error && axis_settings.nVector==3) {
double vector[3];
const hsize_t vdim[] = {3};
hsize_t vbuf[] = {0};
CBF_CALL(cbf_apply_matrix(key->matrix,axis_settings.vector,vector));
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"vector",1,vdim,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,vector,vbuf,cmp_double,cmp_params));
}
cbf_H5Dfree(dset);
}
free((void*)axisData);
}
} else {
cbf_debug_print("error: unrecognised combination of settings");
error |= CBF_FORMAT;
}
}
} else {
/* unhandled number of axes - error */
cbf_debug_print("error: unsupported array structure");
error |= CBF_NOTIMPLEMENTED;
}
}
if (!matched) {
/* TODO: in some cases this will be an error, find them */
cbf_debug_print("warning: No data located");
}
free((void*)axes);
}
return error;
}
/*
Special function to support axis conversions back from NeXus to
CBF by recording the axes CBF_X_, CBF_Y_, CBF_Z_, BEAM, GRAVITY,
UP and other general axes in NXentry/NXinstrument/NXtranformations,
if possible.
*/
static int cbf_write_cbf2nx__cbf_general_axes
(cbf_handle handle,
cbf_h5handle h5handle,
double matrix[3][3])
{
const char* anames[7] = {"CBF_X_","CBF_Y_","CBF_Z_","BEAM", "SOURCE", "GRAVITY", "UP"};
int afound[7] = {0,0,0,0,0,0,0};
double beamvector[3] = {0.,0.,-1.};
double sourcevector[3] = {0.,0.,1.};
double gravityvector[3]= {0.,-1.,0.};
double upvector[3]= {0.,1.,0.};
hid_t instrument = CBF_H5FAIL;
hid_t poise = CBF_H5FAIL;
int error;
unsigned int rows, row;
hid_t dset = CBF_H5FAIL;
const hsize_t max[] = {1};
const hsize_t cnk[] = {1};
hsize_t buf[] = {0};
if (!handle || !h5handle || !matrix) return CBF_ARGUMENT;
error = 0;
/* ensure the handle contains some basic structure */
CBF_CALL(cbf_h5handle_require_entry(h5handle,0,0));
CBF_CALL(cbf_h5handle_require_instrument(h5handle,&instrument,0));
CBF_CALL(cbf_H5Grequire(instrument,&poise,"transformations"));
CBF_CALL(cbf_H5Arequire_string(poise,"NXclass","NXtransformations"));
CBF_CALL(cbf_find_category(handle, "axis"));
CBF_CALL(cbf_find_column(handle,"id"));
rows = 0;
CBF_CALL(cbf_count_rows(handle,&rows));
for(row=0; row < rows+7 && !error; row++) {
const char * axis_id = NULL;
const char * equipment = NULL;
const char * equipment_component = NULL;
const char * depends_on = NULL;
const char * rotation_axis = NULL;
const char * type = NULL;
const char * system = NULL;
double rotation = 0.;
double vector[3] = {0.,0.,0.}, offset[3] = {0.,0.,0.};
double cbfvector[3] = {0.,0.,0.}, cbfoffset[3] = {0.,0.,0.};
if (row < rows) {
CBF_CALL(cbf_find_category(handle, "axis"));
CBF_CALL(cbf_find_column(handle,"id"));
CBF_CALL(cbf_select_row(handle,row));
CBF_CALL(cbf_find_column(handle,"id"));
CBF_CALL(cbf_get_value(handle,&axis_id));
equipment = "general";
if (!cbf_find_column(handle,"equipment")) {
CBF_CALL(cbf_get_value(handle,&equipment));
if (!equipment) equipment = "general";
}
cbf_debug_print3("processing axis %s for equipment %s\n",axis_id,equipment);
if (!cbf_cistrcmp(equipment,"detector")||
!cbf_cistrcmp(equipment,"goniometer")) continue;
equipment = "general";
depends_on = ".";
if (!cbf_find_column(handle,"depends_on")) {
CBF_CALL(cbf_get_value(handle,&depends_on));
if (!depends_on) depends_on = ".";
}
rotation_axis= ".";
if (!cbf_find_column(handle,"rotation_axis")) {
CBF_CALL(cbf_get_value(handle,&depends_on));
if (!rotation_axis) rotation_axis = ".";
}
rotation = 0.;
if (!cbf_find_column(handle,"rotation")) {
CBF_CALL(cbf_get_doublevalue(handle,&rotation));
}
type = "general";
if (!cbf_find_column(handle,"type")) {
CBF_CALL(cbf_get_value(handle,&type));
if (!type) type = "general";
}
system = "laboratory";
if (!cbf_find_column(handle,"system")) {
CBF_CALL(cbf_get_value(handle,&system));
if (!system||!system[0]) system = "laboratory";
if (cbf_cistrcmp(system,".")||cbf_cistrcmp(system,"?"))
system = "laboratory";
}
if (cbf_cistrcmp(system,"laboratory")) {
CBF_CALL(cbf_get_axis_vector_and_offset(handle,axis_id,
vector, offset));
cbfvector[0] = vector[0];
cbfvector[1] = vector[1];
cbfvector[2] = vector[2];
} else {
CBF_CALL(cbf_get_axis_poise(handle, 0.,
(double *)cbfvector,(double *)cbfvector+1,(double *)cbfvector+2,
(double *)cbfoffset,(double *)cbfoffset+1,(double *)cbfoffset+2,
NULL,axis_id,NULL));
system = "McStas_absolute";
CBF_CALL(cbf_apply_matrix(matrix,cbfvector,vector));
CBF_CALL(cbf_apply_matrix(matrix,cbfoffset,offset));
}
if (!cbf_cistrcmp(axis_id,"BEAM")) {
if (CBF_SUCCESS== error) {
afound[3] = 1;
beamvector[0] = cbfvector[0];
beamvector[1] = cbfvector[1];
beamvector[2] = cbfvector[2];
sourcevector[0] = -beamvector[0];
sourcevector[1] = -beamvector[1];
sourcevector[2] = -beamvector[2];
}
}
if (!cbf_cistrcmp(axis_id,"SOURCE")) {
if (CBF_SUCCESS== error) {
afound[4] = 1;
sourcevector[0] = cbfvector[0];
sourcevector[1] = cbfvector[1];
sourcevector[2] = cbfvector[2];
beamvector[0] = -sourcevector[0];
beamvector[1] = -sourcevector[1];
beamvector[2] = -sourcevector[2];
}
}
if (!cbf_cistrcmp(axis_id,"GRAVITY")) {
if (CBF_SUCCESS== error) {
afound[5] = 1;
gravityvector[0] = cbfvector[0];
gravityvector[1] = cbfvector[1];
gravityvector[2] = cbfvector[2];
upvector[0] = -gravityvector[0];
upvector[1] = -gravityvector[1];
upvector[2] = -gravityvector[2];
}
}
if (!cbf_cistrcmp(axis_id,"UP")) {
if (CBF_SUCCESS== error) {
afound[6] = 1;
upvector[0] = cbfvector[0];
upvector[1] = cbfvector[1];
upvector[2] = cbfvector[2];
gravityvector[0] = -upvector[0];
gravityvector[1] = -upvector[1];
gravityvector[2] = -upvector[2];
}
}
} else {
double cbfvector[3];
axis_id = anames[row-rows];
depends_on = ".";
equipment = "general";
rotation_axis = ".";
rotation = 0.;
system = "McStas_absolute";
type = "general";
offset[0] = offset[1] = offset[2] = 0.;
switch (row-rows) {
case 0:
cbfvector[0] = 1.;
cbfvector[1] = 0.;
cbfvector[2] = 0.;
CBF_CALL(cbf_apply_matrix(matrix,cbfvector,vector));
break;
case 1:
cbfvector[0] = 0.;
cbfvector[1] = 1.;
cbfvector[2] = 0.;
CBF_CALL(cbf_apply_matrix(matrix,cbfvector,vector));
break;
case 2:
cbfvector[0] = 0.;
cbfvector[1] = 0.;
cbfvector[2] = 1.;
CBF_CALL(cbf_apply_matrix(matrix,cbfvector,vector));
break;
case 3: /* BEAM */
if (afound[3] || afound[4])
{
cbfvector[0] = beamvector[0];
cbfvector[1] = beamvector[1];
cbfvector[2] = beamvector[2];
} else {
cbfvector[0] = 0.;
cbfvector[1] = 0.;
cbfvector[2] = -1.;
}
CBF_CALL(cbf_apply_matrix(matrix,cbfvector,vector));
break;
case 4: /* SOURCE */
if (afound[3] || afound[4]){
cbfvector[0] = sourcevector[0];
cbfvector[1] = sourcevector[1];
cbfvector[2] = sourcevector[2];
} else {
cbfvector[0] = 0.;
cbfvector[1] = 0.;
cbfvector[2] = 1.;
}
CBF_CALL(cbf_apply_matrix(matrix,cbfvector,vector));
break;
case 5: /* GRAVITY */
if (afound[5] || afound[6] ) {
cbfvector[0] = gravityvector[0];
cbfvector[1] = gravityvector[1];
cbfvector[2] = gravityvector[2];
} else {
cbfvector[0] = 0.;
cbfvector[1] = -1.;
cbfvector[2] = 0.;
}
CBF_CALL(cbf_apply_matrix(matrix,cbfvector,vector));
break;
case 6: /* UP */
if (afound[5] || afound[6]) {
cbfvector[0] = upvector[0];
cbfvector[1] = upvector[1];
cbfvector[2] = upvector[2];
} else {
cbfvector[0] = 0.;
cbfvector[1] = 1.;
cbfvector[2] = 0.;
}
CBF_CALL(cbf_apply_matrix(matrix,cbfvector,vector));
break;
default:
continue;
}
cbf_debug_print3("processing axis %s for equipment %s\n",axis_id,equipment);
}
CBF_CALL(cbf_H5Drequire(poise,&dset,axis_id,1,max,cnk,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Arequire_string(dset,"depends_on",depends_on));
CBF_CALL(cbf_H5Arequire_string(dset,"equipment",equipment));
CBF_CALL(cbf_H5Arequire_string(dset,"transformation_type",type));
CBF_CALL(cbf_H5Arequire_string(dset,"system",system));
if (cbf_cistrcmp(rotation_axis,".")) {
CBF_CALL(cbf_H5Arequire_string(dset,"rotation_axis",rotation_axis));
if (!error) {
/* write the rotation */
const hsize_t dim[] = {1};
double buf[] = {0};
if (H5Aexists(dset,"rotation") > 0) {
H5Adelete_by_name(dset,".","rotation",H5P_DEFAULT);
}
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"rotation",1,dim,H5T_IEEE_F64LE,
H5T_NATIVE_DOUBLE,&rotation,buf,
cmp_double,cmp_params));
CBF_CALL(cbf_H5Arequire_string(dset,"rotation_units","degrees"));
}
}
if (!error && cbf_norm(offset) > 1.e-20) {
/* write the offset */
const hsize_t dim[] = {3};
double buf[] = {0};
if (H5Aexists(dset,"offset") > 0) {
H5Adelete_by_name(dset,".","offset",H5P_DEFAULT);
}
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"offset",1,dim,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,offset,buf,cmp_double,cmp_params));
CBF_CALL(cbf_H5Arequire_string(dset,"offset_units","mm"));
}
if (!error) {
/* write the vector */
const hsize_t dim[] = {3};
double buf[] = {0};
if (H5Aexists(dset,"vector") > 0) {
H5Adelete_by_name(dset,".","vector",H5P_DEFAULT);
}
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"vector",1,dim,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,vector,buf,cmp_double,cmp_params));
}
cbf_H5Dfree(dset);
}
cbf_H5Gfree(poise);
return error;
}
/*
Special function to handle axis conversions by dealing with axis sets and
traversing the dependency chain for all referenced leaf axes. This doesn't
match a set of rows within the axis table, so should not be forced into
the same processing model as the conversion functions which do.
See comments for a description of what this function does.
*/
static int cbf_write_cbf2nx__axis
(cbf_handle handle,
cbf_h5handle h5handle,
cbf_cbf2nx_key_t * const key,
const int list)
{
int error = CBF_SUCCESS;
const char categoryName[] = "axis";
#ifdef CBF_USE_ULP
cmp_double_param_t cmp_double_params;
void * cmp_params = &cmp_double_params;
CBF_UNUSED( list );
/* set up the comparison parameters */
cmp_double_params.cmp_double_as_float = cbf_has_ULP64() ? h5handle->cmp_double_as_float : 1;
cmp_double_params.ulp32 = h5handle->float_ulp;
#ifndef NO_UINT64_TYPE
cmp_double_params.ulp64 = h5handle->double_ulp;
#endif
#else
void * cmp_params = 0;
CBF_UNUSED( list );
CBF_UNUSED(cmp_params);
#endif
/* check arguments */
if (!handle) {
cbf_debug_print("Invalid handle given\n");
error |= CBF_ARGUMENT;
} else if (!h5handle) {
cbf_debug_print("Invalid hdf5 handle given\n");
error |= CBF_ARGUMENT;
} else if (!key) {
cbf_debug_print("Bad key given\n");
error |= CBF_ARGUMENT;
}else if (CBF_SUCCESS!=(error|=cbf_find_category(handle,categoryName))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
/* The axes in key->axis are used within the scan, so I must convert them all and any axes that they depend on. */
cbf_node * const category = handle->node;
unsigned int matched = 0;
unsigned int i;
if (h5handle->logfile) _cbf_write_name(h5handle->logfile,categoryName,0,key->indent,1);
if (key->categories) {
cbf_node * * it;
cbf_node * const * const end = key->nCat+key->categories;
for (it = key->categories; end != it; ++it) {
if (*it) {
*it = strcmp((*it)->name,categoryName) ? *it : NULL;
}
}
}
/*
Each axis set may contain multiple axes. Each of these needs to have its full dependency chain recorded. I
should not generate any new axes for any axis within an axis set, but should add all parent axes that are
not in an axis set. The creation of an axis for the axis set is not the responsibility of this function.
*/
{
cbf_node * asla_setid = NULL;
cbf_node * asla_axisid = NULL;
cbf_node * axis_id = NULL;
cbf_node * axis_depends_on = NULL;
cbf_node * axis_rotation_axis = NULL;
CBF_CALL(cbf_find_category(handle,"array_structure_list_axis"));
CBF_CALL(cbf_find_child(&asla_setid,handle->node,"axis_set_id"));
CBF_CALL(cbf_find_child(&asla_axisid,handle->node,"axis_id"));
CBF_CALL(cbf_find_category(handle,"axis"));
CBF_CALL(cbf_find_child(&axis_id,handle->node,"id"));
if(cbf_find_child(&axis_depends_on,handle->node,"depends_on")) {
axis_depends_on = NULL;
};
if(cbf_find_child(&axis_depends_on,handle->node,"rotation_axis")) {
axis_rotation_axis = NULL;
};
if (CBF_SUCCESS==error) {
unsigned int i;
unsigned int nAxes = 0;
const char * * axes = NULL;
/* 1. get a list of all axes within a valid axis set */
for (i = 0; i != key->arrayAxisSet.count; ++i) {
unsigned int j = 0;
/* loop over ASLA to locate matching axis_set_id, store corresponding axis_id if it isn't already present */
for (j = 0; j != asla_setid->children; ++j) {
const char * axis = NULL;
const char * set = NULL;
CBF_CALL(cbf_node_get_value(asla_setid,j,&set));
CBF_CALL(cbf_node_get_value(asla_axisid,j,&axis));
if (h5handle->logfile) fprintf(h5handle->logfile,"axis: '%s'\n",axis);
if (h5handle->logfile) fprintf(h5handle->logfile,"set: '%s'\n",set);
if (CBF_SUCCESS==error) {
if (strcmp(set,key->arrayAxisSet.axis_set_id[i])) {
continue;
} else {
unsigned int k = 0;
/* I have an axis id from the current set, ensure it exists within the axis list */
for (k = 0; k != nAxes; ++k) {
if (!strcmp(axis,axes[k])) break;
}
if (k == nAxes) {
const char * * newAxes = NULL;
/* axis id wasn't found in the set - add it */
++nAxes;
if (!(newAxes=realloc(axes,sizeof(const char *)*nAxes))) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_ALLOC));
error |= CBF_ALLOC;
} else {
axes[nAxes-1] = axis;
}
}
}
}
}
}
/*
2. check dependency chain for each axis in axes, if the parent
axis is not in axes or in key->axis then add it to key->axis
*/
for (i = 0; i != nAxes; ++i) {
const char * depends_on = NULL;
if (CBF_SUCCESS!=(error|=cbf_node_get_value(axis_depends_on,i,&depends_on))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
unsigned int j = 0;
unsigned int k = 0;
for (j = 0; j != nAxes; ++j) {
if (i != j && !strcmp(depends_on,axes[j])) {
break;
}
}
for (k = 0; k != key->axis.count; ++k) {
if (!strcmp(depends_on,key->axis.axis_id[k])) {
break;
}
}
if (j == nAxes && k == key->axis.count) {
/*
The parent axis for CBF's axis[i] is not listed as part of an axis set for the current scan,
or as one of the axes that have already been mapped. Need to add it - and any unlisted parent
axes - to the list of axes that must be mapped.
TODO: implement this
*/
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_NOTIMPLEMENTED));
error |= CBF_NOTIMPLEMENTED;
}
}
}
free((void*)axes);
}
}
/* convert each axis: */
for (i = 0; CBF_SUCCESS==error && i != key->axis.count; ++i) {
const char * axis_name = key->axis.axis_id[i];
const char * axis_rot_name = NULL;
const char * axis_path = key->axis.path[i];
const char * axis_rot_path = NULL;
const char axis_root[] = ".";
while (CBF_SUCCESS==error && axis_name && strcmp(axis_name,".") && axis_path) {
hid_t dset = CBF_H5FAIL;
const hsize_t max[] = {H5S_UNLIMITED};
const hsize_t cnk[] = {1};
hsize_t buf[] = {0};
CBF_CALL(cbf_H5Drequire(h5handle->hfile,&dset,axis_path,1,max,cnk,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_find_column(handle,"id"));
CBF_CALL(cbf_find_row(handle,axis_name));
if (CBF_SUCCESS==error) {
cbf_axis_data_t axis_settings = cbf_axis_data_init();
cbf_read_axis_row(&axis_settings,category,handle->row);
++matched;
/* update variables for next iteration & to record the axis dependency */
axis_name = axis_settings.depends_on;
if (!axis_name || !strcmp(axis_name,axis_root)
|| !strcmp(axis_name,"?") || !strcmp(axis_name,".")) {
axis_path = axis_root;
} else {
unsigned int j;
for (j = 0; j != key->axis.count; ++j) {
if (!cbf_cistrcmp(key->axis.axis_id[j],axis_name)) break;
}
if (j != key->axis.count) {
/* assign existing axis path */
axis_path = key->axis.path[j];
} else {
/* TODO: make up a new axis path & store it for future reference */
axis_path = NULL;
cbf_debug_print("conversion of axes not in detector or goniometer axis groups is not implemented - need to define a path for them\n");
cbf_debug_print2("problematic axus %s\n",axis_name);
error |= CBF_NOTIMPLEMENTED;
}
}
axis_rot_name = axis_settings.rotation_axis;
if (!axis_rot_name || !strcmp(axis_rot_name,axis_root)
|| !strcmp(axis_rot_name,"?")|| !strcmp(axis_rot_name,".")) {
axis_rot_path = NULL;
} else {
unsigned int j;
for (j = 0; j != key->axis.count; ++j) {
if (!strcmp(key->axis.axis_id[j],axis_rot_name)) break;
}
if (j != key->axis.count) {
/* assign existing axis path */
axis_rot_path = key->axis.path[j];
} else {
/* TODO: make up a new axis path & store it for future reference */
axis_rot_path = NULL;
cbf_debug_print("conversion of axes not in detector or goniometer axis groups is not implemented - need to define a path for them\n");
cbf_debug_print2("problematic axis %s\n",axis_name);
error |= CBF_NOTIMPLEMENTED;
}
}
/* I might have the data - write it to HDF5 if I do */
if (CBF_SUCCESS==error) {
/* write the dependency */
if (axis_path) {
CBF_CALL(cbf_H5Arequire_string(dset,"depends_on",axis_path));
} else {
CBF_CALL(cbf_H5Arequire_string(dset,"depends_on","?"));
}
/* write the equipment */
if (axis_settings.equipment) CBF_CALL(cbf_H5Arequire_string(dset,"equipment",axis_settings.equipment));
if (axis_settings.equipment_component) CBF_CALL(cbf_H5Arequire_string(dset,"equipment_component",axis_settings.equipment_component));
if (axis_rot_path) {
const hsize_t dim[] = {1};
double buf[] = {0};
CBF_CALL(cbf_H5Arequire_string(dset,"rotation_axis",axis_rot_path));
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"",1,dim,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,&(axis_settings.rotation),buf,cmp_double,cmp_params));
CBF_CALL(cbf_H5Arequire_string(dset,"rotation_units","degrees"));
}
if (!error) {
/* write the offset */
double offset[3] = {0.,0.,0.};
const hsize_t dim[] = {3};
double buf[] = {0};
if (3==axis_settings.nOffset) {
CBF_CALL(cbf_apply_matrix(key->matrix,axis_settings.offset,offset));
}
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"offset",1,dim,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,offset,buf,cmp_double,cmp_params));
CBF_CALL(cbf_H5Arequire_string(dset,"offset_units","mm"));
}
/* write the type */
if (axis_settings.type) {
if (CBF_SUCCESS!=(error|=cbf_H5Arequire_string(dset,"transformation_type",axis_settings.type))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else {
if (!strcmp(axis_settings.type,"translation")) {
CBF_CALL(cbf_H5Arequire_string(dset,"units","mm"));
} else if (!strcmp(axis_settings.type,"rotation")){
CBF_CALL(cbf_H5Arequire_string(dset,"units","degrees"));
} else {
cbf_debug_print2("error: unrecognised axis type: %s\n",axis_settings.type);
error |= CBF_UNDEFINED;
}
}
}
if (!error) {
/* write the vector */
double vector[3] = {0.,0.,0.};
const hsize_t dim[] = {3};
double buf[] = {0};
if (3==axis_settings.nVector) {
CBF_CALL(cbf_apply_matrix(key->matrix,axis_settings.vector,vector));
}
if (H5Aexists(dset,"vector") > 0) {
H5Adelete_by_name(dset,".","vector",H5P_DEFAULT);
}
CBF_CALL(CBFM_H5Arequire_cmp2(dset,"vector",1,dim,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,vector,buf,cmp_double,cmp_params));
}
}
}
cbf_H5Dfree(dset);
}
}
}
return error;
}
/*
Create an 'NXdata' class and ensure it contains links to existing data within the
rest of the entry group, to allow automatic visualisation. The paths for existing
links are not checked.
*/
static int cbf_write_cbf2nx__link_h5data
(cbf_handle handle,
cbf_h5handle h5handle)
{
int error = CBF_SUCCESS;
cbf_debug_print("link_h5data\n");
/* check arguments */
if (!handle) {
cbf_debug_print("Invalid handle given\n");
error |= CBF_ARGUMENT;
} else if (!h5handle) {
cbf_debug_print("Invalid hdf5 handle given\n");
error |= CBF_ARGUMENT;
} else {
/* ensure an entry/data group exists and has a link to the dataset */
if (!cbf_H5Ivalid(h5handle->nxdata)) {
CBF_CALL(cbf_H5Grequire(h5handle->nxid,&h5handle->nxdata,"data"));
}
/* use it to link to data */
if (CBF_SUCCESS==error && cbf_H5Ivalid(h5handle->nxdata)) {
const htri_t data_exists = H5Lexists(h5handle->nxdata,"data",H5P_DEFAULT);
const htri_t data_scaling_exists = H5Lexists(h5handle->nxdetectors[h5handle->cur_detector],
"scaling_factor",H5P_DEFAULT);
const htri_t data_offset_exists = H5Lexists(h5handle->nxdetectors[h5handle->cur_detector],
"offset",H5P_DEFAULT);
CBF_CALL(cbf_H5Arequire_string(h5handle->nxdata,"NX_class","NXdata"));
CBF_CALL(cbf_H5Arequire_string(h5handle->nxdata,"signal","data"));
if (data_exists < 0) {
error |= CBF_H5ERROR;
} else if (data_exists) {
/* it exists - done */
} else {
H5Lcreate_hard(h5handle->nxdetectors[h5handle->cur_detector],
"data",h5handle->nxdata,"data",H5P_DEFAULT,H5P_DEFAULT);
}
if (data_scaling_exists < 0) {
error |= CBF_H5ERROR;
} else if (data_scaling_exists) {
const htri_t scaling_exists = H5Lexists(h5handle->nxdata,"scaling_factor",H5P_DEFAULT);
if (scaling_exists < 0) {
error |= CBF_H5ERROR;
} else if(scaling_exists) {
/* it exists - done */
} else {
H5Lcreate_hard(h5handle->nxdetectors[h5handle->cur_detector],
"scaling_factor",
h5handle->nxdata,"scaling_factor",
H5P_DEFAULT,H5P_DEFAULT);
}
} else {
/* I don't need to link to it - done */
}
if (data_offset_exists < 0) {
error |= CBF_H5ERROR;
} else if (data_offset_exists) {
const htri_t offset_exists = H5Lexists(h5handle->nxdata,"offset",H5P_DEFAULT);
if (offset_exists < 0) {
error |= CBF_H5ERROR;
} else if(offset_exists) {
/* it exists - done */
} else {
H5Lcreate_hard(h5handle->nxdetectors[h5handle->cur_detector],
"offset",
h5handle->nxdata,"offset",
H5P_DEFAULT,H5P_DEFAULT);
}
} else {
/* I don't need to link to it - done */
}
/* extract some axes based on the scan type */
if (CBF_SUCCESS==error) {
const htri_t axis_fast_exists = H5Lexists(h5handle->nxdetectors[h5handle->cur_detector],
"y_pixel_offset",H5P_DEFAULT);
const htri_t axis_slow_exists = H5Lexists(h5handle->nxdetectors[h5handle->cur_detector],
"x_pixel_offset",H5P_DEFAULT);
if (axis_fast_exists < 0) {
error |= CBF_H5ERROR;
} else if (axis_fast_exists) {
const htri_t axis_exists = H5Lexists(h5handle->nxdata,"y",H5P_DEFAULT);
if (axis_exists < 0) {
error |= CBF_H5ERROR;
} else if(axis_exists) {
/* it exists - done */
} else {
H5Lcreate_hard(h5handle->nxdetectors[h5handle->cur_detector],
"y_pixel_offset",
h5handle->nxdata,"y",
H5P_DEFAULT,H5P_DEFAULT);
}
} else {
/* I can't need to link to it - complain & fail */
cbf_debug_print("Error: cannot find axis data\n");
error |= CBF_UNDEFINED;
}
if (axis_slow_exists < 0) {
error |= CBF_H5ERROR;
} else if (axis_slow_exists) {
const htri_t axis_exists = H5Lexists(h5handle->nxdata,"x",H5P_DEFAULT);
if (axis_exists < 0) {
error |= CBF_H5ERROR;
} else if(axis_exists) {
/* it exists - done */
} else {
H5Lcreate_hard(h5handle->nxdetectors[h5handle->cur_detector],
"x_pixel_offset",
h5handle->nxdata,"x",
H5P_DEFAULT,H5P_DEFAULT);
}
} else {
/* I can't need to link to it - complain & fail */
cbf_debug_print("Error: cannot find axis data\n");
error |= CBF_UNDEFINED;
}
if (CBF_SUCCESS==error) { /* axes=[slow_dim, ..., fast_dim] */
hid_t h5atype = CBF_H5FAIL;
const char axis0[] = "";
const char axis1[] = "y";
const char axis2[] = "x";
const char * axes[3];
const hsize_t dim[] = {3};
char * buf[3] = {0};
axes[0] = axis0;
axes[1] = axis1;
axes[2] = axis2;
CBF_CALL(cbf_H5Tcreate_string(&h5atype,H5T_VARIABLE));
CBF_CALL(CBFM_H5Arequire_cmp2(h5handle->nxdata,"axes",1,dim,h5atype,h5atype,axes,buf,cmp_vlstring,0));
cbf_H5Tfree(h5atype);
}
if (CBF_SUCCESS==error) {
const char x_indices[] = "x_indices";
const char y_indices[] = "y_indices";
const char * indices[2];
const int idx[] = {1,2};
int i;
indices[0] = y_indices;
indices[1] = x_indices;
for (i = 0; i != 2; ++i) {
int buf[] = {0};
CBF_CALL(
CBFM_H5Arequire_cmp2(
h5handle->nxdata,
indices[i],
0,
0,
H5T_STD_I32LE,
H5T_NATIVE_INT,
i+idx,
buf,
cmp_int,
0
)
);
}
}
}
}
}
return error;
}
/*
Parametric conversion of data within a sebset of the rows of a single table.
Note: This accesses (read-only) global state in the form of a table which gives data about mappings.
Data is extracted and converted by:
Direct conversion from a single CBF data item to a single NeXus data item.
Collecting information from each column within a single matching row, then processing it in some way.
Collecting information from each matching row within a single table, then processing it in some way.
In complex cases, this _should_ involve collecting data in a per-row cache,
then adding it to a per-table cache, then adding it to the higher-level
'key' object. This is by design, to allow suitable processing to be
inserted in between each level in order to ensure data integrity and
perform format conversions and carry out memory management as needed.
This is done by performing the following operations:
- Locate the category requested and the mapping data for it. An
error will occur if either isn't found.
- Data about the primary keys of the table must be populated, to be used
when matching rows and iterating through columns.
- A set of matching rows is extracted from the table.
- create a table-specific cache object
- foreach matching row {
- create a row-specific cache object
- foreach column {
either {
- filter data value
- store in nexus file
} or {
- cache value as a key to be used later
} or {
- cache value in per-row cache object
}
}
- process row-specific cache object, which may involve writing data
to the nexus file or storing it in a table-specific cache object.
- destroy the row-specific cache object
}
- process and then destroy the table-specific cache object
This should be used by:
- defining a set of primary key columns and telling the program how to get at their values.
- defining a set of filter functions which may be used when converting data items directly.
- defining a set of cache objects and functions to populate and process them.
- gluing everything together with mapping tables in a pre-defined format.
Future extensions:
Replace the whole data conversion model/pipeline with a more powerful dRel-based model.
*/
static int cbf2nx_convert_category
(cbf_node * const node /*< the datablock being converted */,
cbf_h5handle nx /*< the nexus file to store it in */,
const char * const categoryName /*< the category to convert */,
cbf_cbf2nx_key_t * const key /*< a higher-level cache of values to control the conversion */,
const int list /*< boolean: list items or not? */,
const int required /*< boolean: is this category required for success? */)
{
int error = CBF_SUCCESS;
unsigned int matched = 0;
/* check arguments */
if (!node) {
cbf_debug_print2("error: %s\n","Invalid cbf node given\n");
error |= CBF_ARGUMENT;
} else if (CBF_DATABLOCK != node->type) {
cbf_debug_print2("error: %s\n","cbf node is not a datablock");
error |= CBF_ARGUMENT;
} else if (!nx) {
cbf_debug_print2("error: %s\n","Invalid hdf5 handle given");
error |= CBF_ARGUMENT;
} else if (!categoryName) {
cbf_debug_print2("error: %s\n","No category specified");
error |= CBF_ARGUMENT;
} else if (!key) {
cbf_debug_print2("error: %s\n","Bad key given\n");
error |= CBF_ARGUMENT;
} else {
/* arguments are fine, try to get a category to use and mappings to convert it */
cbf_node * category = NULL;
const cbf2nx_category_map_t * const category_map = cbf2nx_find_category_mapping(cbf_map,categoryName);
const int found = cbf_find_child(&category,node,categoryName);
if (!category_map) {
cbf_debug_print("error: No mapping located for category");
error |= CBF_NOTFOUND;
}
if (CBF_NOTFOUND==found) {
if (required) {
cbf_debug_print("error: required category not found");
error |= CBF_NOTFOUND;
}
} else if (found) {
cbf_debug_print2("error: %s\n",cbf_strerror(found));
error |= found;
}
if (category && !error) {
/* I have a category and the conversions for it, apply them */
unsigned int * rows = NULL;
unsigned int row = 0, nRows = 0, mRows = 0;
unsigned int indent = 1+key->indent;
cbf_node * * primary_key_column = NULL;
const char * * primary_key_value = NULL;
/* allocate cache objects */
void * const row_cache = category_map->row_cache.size ? malloc(category_map->row_cache.size) : NULL;
void * const tbl_cache = category_map->tbl_cache.size ? malloc(category_map->tbl_cache.size) : NULL;
if (tbl_cache && category_map->tbl_cache.ctor) CBF_CALL(category_map->tbl_cache.ctor(tbl_cache));
/* record that the category is recognised */
if (nx->logfile) _cbf_write_name(nx->logfile,category->name,0,key->indent,1);
CBF_CALL(_cbf2nx_key_remove_category(key,category->name));
/* check that I have some children, and try to find the primary key columns */
if (0==category->children) {
cbf_debug_print2("error: %s\n","category has no children");
error |= CBF_NOTFOUND;
} else {
nRows = category->child[0]->children;
CBF_CALL(cbf_populate_primaryKey_cache(category_map->key_data,&primary_key_column,&primary_key_value,key,category));
}
/* ensure I have at least one matching row */
CBF_CALL(cbf_get_matching_rows(category_map->key_data,primary_key_column,primary_key_value,nRows,&rows,&mRows));
if (0==mRows) {
cbf_debug_print2("error: %s\n","No matching data located");
error |= CBF_FORMAT;
}
/*
iterate over matching rows and over each column in each matching
row, calling a function to convert or cache the data.
*/
for (row = 0; !error && row != mRows; ++row) {
cbf_node * const * column_it;
if (row_cache && category_map->row_cache.ctor) CBF_CALL(category_map->row_cache.ctor(row_cache));
for (column_it = category->child; !error && column_it != category->children+category->child; ++column_it) {
cbf_node * const column = *column_it;
if (array_contains((const void * const *)primary_key_column,cbf_count_primary_keys(category_map->key_data),column)) {
if (nx->logfile && !matched) _cbf_write_name(nx->logfile,column->name,0,indent,1);
} else {
CBF_CALL(cbf2nx_apply_conversions(nx,key,category_map->column_data,column,row,row_cache,list,matched));
}
}
++matched;
/* use cache object */
if (row_cache && category_map->process_row) CBF_CALL(category_map->process_row(category,nx,key,row_cache,tbl_cache,row));
/* clear out old data, leaving the cache ready to be re-used or disposed of */
if (row_cache && category_map->row_cache.dtor) CBF_CALL(category_map->row_cache.dtor(row_cache));
}
if (!matched) {
/* TODO: in some cases this will be an error, find them */
cbf_debug_print("warning: No data located");
}
/* use cache object */
if (tbl_cache && category_map->process_tbl) CBF_CALL(category_map->process_tbl(category,nx,key,tbl_cache));
/* clear out old data, leaving the cache ready to be re-used or disposed of */
if (tbl_cache && category_map->tbl_cache.dtor) CBF_CALL(category_map->tbl_cache.dtor(tbl_cache));
/* clean up */
free(row_cache);
free(tbl_cache);
free((void*)rows);
free((void*)primary_key_column);
free((void*)primary_key_value);
}
}
if (error && categoryName) {
cbf_debug_print(categoryName);
}
return error;
}
/**
Equivalent to <code>cbf_write_cbf2nx(handle,h5handle,0,0,0)</code>.
\param handle The CBF file to extract data from.
\param h5handle The NeXuS file to write data to.
\sa cbf_write_cbf_h5file
\sa cbf_write_minicbf_h5file
\sa cbf_write_cbf2nx
\sa cbf_write_nx2cbf
\return An error code.
*/
int cbf_write_cbf_h5file
(cbf_handle handle,
cbf_h5handle h5handle)
{
return cbf_write_cbf2nx(handle,h5handle,0,0,0);
}
/**
Extracts data from <code>handle</code> and generates a NeXus file in <code>h5handle</code>. This will attempt
to extract metadata and image data from each scan (or the named scan) within each datablock (or the the named
datablock) and insert it into a given index into the NXentry group specified in <code>h5handle</code>.
Each scan in the CBF file corresponds to one NXentry in NeXus, so a CBF datablock with multiple scans must be
converted by calling this function with the appropriate value of <code>scan</code> once for each scan in the
datablock.
The flags (within <code>h5handle</code>) determine:
- Compression algorithm: zlib/CBF/none
- Plugin registration method: automatic/manual
The strings given by <code>h5handle->scan_id</code> and <code>h5handle->sample_id</code> define:
- The presence and value of an identifier for the scan, stored in <code>/\*:NXentry/entry_identifier</code>.
- The presence and value of an identifier for the sample, stored in <code>/\*:NXentry/\*:NXsample/sample_identifier</code>.
\sa cbf_write_cbf_h5file
\sa cbf_write_minicbf_h5file
\sa cbf_write_nx2cbf
\return An error code.
*/
int cbf_write_cbf2nx
(cbf_handle handle /**< The CBF file to extract data from. */,
cbf_h5handle h5handle /**< The NeXuS file to write data to. */,
const char * const datablock /**< The name of the datablock to convert, or NULL to convert all datablocks. */,
const char * const scan /**< The name of the scan to convert, or NULL if there is only one scan in the datablock. */,
const int list /**< Boolean flag to determine if a list of processed items is printed. */)
{
return cbf_write_cbf2nx2(handle,h5handle,0,datablock,scan,list);
}
/**
Extracts data from <code>handle</code> and generates a NeXus file in <code>h5handle</code>. This will attempt
to extract metadata and image data from each scan (or the named scan) within each datablock (or the the named
datablock) and insert it into a given index into the NXentry group specified in <code>h5handle</code>.
If h5datahandle is specified, the data is written to that file and h5handle is used
Each scan in the CBF file corresponds to one NXentry in NeXus, so a CBF datablock with multiple scans must be
converted by calling this function with the appropriate value of <code>scan</code> once for each scan in the
datablock.
The flags (within <code>h5handle</code>) determine:
- Compression algorithm: zlib/CBF/none
- Plugin registration method: automatic/manual
The strings given by <code>h5handle->scan_id</code> and <code>h5handle->sample_id</code> define:
- The presence and value of an identifier for the scan, stored in <code>/\*:NXentry/entry_identifier</code>.
- The presence and value of an identifier for the sample, stored in <code>/\*:NXentry/\*:NXsample/sample_identifier</code>.
\sa cbf_write_cbf_h5file
\sa cbf_write_minicbf_h5file
\sa cbf_write_nx2cbf
\return An error code.
*/
int cbf_write_cbf2nx2
(cbf_handle handle /**< The CBF file to extract data from. */,
cbf_h5handle h5handle /**< The NeXuS file to write metadata to. */,
cbf_h5handle h5datahandle /**< The NeXuS file to write data to. */,
const char * const datablock /**< The name of the datablock to convert, or NULL to convert all datablocks. */,
const char * const scan /**< The name of the scan to convert, or NULL if there is only one scan in the datablock. */,
const int list /**< Boolean flag to determine if a list of processed items is printed. */)
{
int error = CBF_SUCCESS;
int found = CBF_SUCCESS;
cbf_cbf2nx_key_t key[1];
cbf_node *node = NULL;
cbf_node * db = NULL;
hid_t detector = CBF_H5FAIL, instrument = CBF_H5FAIL; /* do not free */
/* check arguments */
if (!handle || !h5handle) return CBF_ARGUMENT;
/* initialise local variables */
*key = _cbf_make_cbf2nx_key();
/* ensure the handle contains some basic structure */
CBF_CALL(cbf_h5handle_require_entry(h5handle,0,0));
CBF_CALL(cbf_h5handle_require_instrument(h5handle,&instrument,0));
if (h5handle->flags & CBF_H5_CBFNONAMES) {
CBF_CALL(cbf_h5handle_require_detector(h5handle,&detector,0));
} else {
CBF_CALL(cbf_require_nxdetectors2(handle,h5handle,h5datahandle));
detector = h5handle->nxdetectors[h5handle->cur_detector];
}
if (h5handle->scan_id) {
CBF_CALL(cbf_H5Drequire_flstring(h5handle->nxid,0,"entry_identifier",h5handle->scan_id));
}
if (h5handle->sample_id) {
hid_t sample = CBF_H5FAIL;
CBF_CALL(cbf_h5handle_require_sample(h5handle,&sample,0));
CBF_CALL(cbf_H5Drequire_flstring(h5handle->nxsample,0,"sample_identifier",h5handle->sample_id));
}
/* prepare the CBF handle */
CBF_CALL(cbf_reset_refcounts(handle->dictionary));
CBF_CALL(cbf_find_parent(&node, handle->node, CBF_ROOT));
/* either select the requested datablock or the first datablock */
if (datablock) {
CBF_CALL(cbf_find_datablock(handle,datablock));
} else {
CBF_CALL(cbf_rewind_datablock(handle));
}
db = handle->node;
/*
Process the current datablock, if valid.
Must have a valid datablock selected when entering this loop.
*/
while (CBF_SUCCESS==error) {
unsigned int nCat = 0;
cbf_node * * categories = NULL;
cbf_node * scans = NULL;
CBF_CALL(cbf_count_categories(handle,&nCat));
if (!(categories=malloc(sizeof(cbf_node*)*nCat))) {
cbf_debug_print2("error: %s\n",cbf_strerror(CBF_ALLOC));
error |= CBF_ALLOC;
} else {
memcpy(categories,handle->node->child,sizeof(cbf_node*)*nCat);
key->categories = categories;
key->nCat = nCat;
}
if (h5handle->logfile) {
int printed = 0;
fputc('\n',h5handle->logfile);
printed = fprintf(h5handle->logfile,"Datablock: %s\n",handle->node->name);
while (--printed > 0) fputc('=',h5handle->logfile);
fputc('\n',h5handle->logfile);
fputc('\n',h5handle->logfile);
}
/* get the axis transformation matrix, which is constant within a datablock */
CBF_CALL(cbf_get_NX_axis_transform(handle,key->matrix));
/* Get the list of scans */
CBF_CALL(cbf_count_scans(handle, &key->nScans));
CBF_CALL(cbf_find_category(handle, "diffrn_scan"));
CBF_CALL(cbf_rewind_column(handle));
/* get the first/requested/only scan id, depending on arguments given */
found = cbf_find_column(handle, "id");
if (CBF_NOTFOUND==found) {
if (key->nScans > 1) {
/* unindexed scan ids => error */
cbf_debug_print("error: found multiple scans but no 'diffrn_scan.id'");
error |= CBF_FORMAT;
}
} else if (CBF_SUCCESS!=found) {
cbf_debug_print2("error: %s\n",cbf_strerror(found));
error |= found;
} else {
scans = handle->node;
/* select the named/first scan */
if (scan) {
CBF_CALL(cbf_find_row(handle,scan));
} else {
CBF_CALL(cbf_rewind_row(handle));
}
/* get the scan id */
CBF_CALL(cbf_get_value(handle, &key->scan));
}
if (!scan && key->nScans > 1) {
cbf_debug_print("error: insufficient data to select a single scan");
error |= CBF_ARGUMENT;
}
/*
Convert the relevant scan.
*/
if (CBF_SUCCESS == error) {
unsigned int frame_row = 0;
unsigned int nFrames = 0;
cbf_node * frames_scanid = NULL;
if (h5handle->logfile) {
int printed = fprintf(h5handle->logfile,"Scan: %s\n",key->scan);
while (--printed > 0) fputc('-',h5handle->logfile);
fputc('\n',h5handle->logfile);
fputc('\n',h5handle->logfile);
}
/* convert the scan data */
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_scan", key, list, 1));
/* get the list of frames */
CBF_CALL(cbf_find_category(handle, "diffrn_scan_frame"));
CBF_CALL(cbf_rewind_column(handle));
CBF_CALL(cbf_count_rows(handle, &nFrames));
/* get the scan id, if it exists */
found = cbf_find_column(handle, "scan_id");
frames_scanid = NULL;
if (CBF_NOTFOUND==found) {
if (scans && key->nScans > 1) {
/* multiple unindexed frame ids => error */
cbf_debug_print("error: 'diffrn_scan_frame.scan_id' not found");
error |= found;
} else {
cbf_debug_print("warning: 'diffrn_scan_frame.scan_id' not found");
}
} else if (CBF_SUCCESS!=found) {
cbf_debug_print2("error: %s\n",cbf_strerror(found));
error |= found;
} else {
frames_scanid = handle->node;
}
/* select the first frame */
CBF_CALL(cbf_rewind_row(handle));
/* loop though all frames related to this scan */
for (frame_row = 0; CBF_SUCCESS == error && nFrames != frame_row; ++frame_row) {
const char * curr_scanid = NULL;
if (frames_scanid) {
if (CBF_SUCCESS!=(error|=cbf_node_get_value(frames_scanid, frame_row, &curr_scanid))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else if (strcmp(curr_scanid, key->scan)) {
continue;
}
} else if (key->nScans == 1) {
if (CBF_SUCCESS!=(error|=cbf_get_scan_id(handle,0,&curr_scanid))) {
cbf_debug_print2("error: %s\n",cbf_strerror(error));
} else if (strcmp(curr_scanid, key->scan)) {
continue;
}
}
_cbf_reset_cbf2nx_key(key);
/* get the frame id to work with */
CBF_CALL(cbf_find_category(handle, "diffrn_scan_frame"));
CBF_CALL(cbf_find_column(handle, "frame_id"));
CBF_CALL(cbf_select_row(handle,frame_row));
CBF_CALL(cbf_get_value(handle, &key->frame));
/* convert the row if the obtained scan value matches the expected value, always go to the next row */
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_scan_frame", key, list, 1));
if (h5handle->flags & CBF_H5_CBFNONAMES) {
/* navigate to the frame data & extract some IDs */
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_data_frame", key, list, 1));
}
/* convert the metadata */
if (h5handle->flags & CBF_H5_CBFNONAMES) {
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_detector_element", key, list, 1));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_detector", key, list, 1));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_detector_axis", key, list, 1));
}
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn", key, list, 1));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_source", key, list, 1));
if (h5handle->flags & CBF_H5_CBFNONAMES) {
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_measurement", key, list, 1));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_measurement_axis", key, list, 1));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_radiation", key, list, 1));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_radiation_wavelength", key, list, 1));
} else {
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_measurement", key, list, 0));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_measurement_axis", key, list, 0));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_radiation", key, list, 0));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_radiation_wavelength", key, list, 0));
}
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_refln", key, list, 0));
if (h5handle->flags & CBF_H5_CBFNONAMES) {
CBF_CALL(cbf_write_cbf2nx__axis(handle, h5handle, key, list));
CBF_CALL(cbf_write_cbf2nx__cbf_general_axes(handle, h5handle, key->matrix));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_scan_axis", key, list, 0));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "diffrn_scan_frame_axis", key, list, 0));
/* convert the data */
CBF_CALL(cbf2nx_convert_category(db, h5handle, "array_intensities", key, list, 1));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "array_data", key, list, 1));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "array_structure", key, list, 0));
CBF_CALL(cbf2nx_convert_category(db, h5handle, "array_structure_list", key, list, 1));
CBF_CALL(cbf_write_cbf2nx__array_structure_list_axis(handle, h5handle, key, list));
CBF_CALL(cbf_write_cbf2nx__link_h5data(handle, h5handle));
}
++h5handle->slice;
}
}
if (CBF_SUCCESS==error && h5handle->logfile) {
cbf_node * const * it;
cbf_node * const * const end = nCat+categories;
for (it = categories; end != it; ++it) {
if (*it) {
_cbf_write_name(h5handle->logfile,(*it)->name,0,0,0);
}
}
fputc('\n',h5handle->logfile);
}
free((void*)categories);
if (datablock) {
/* the requested datablock has been processed - done */
break;
} else {
/* go on to the next datablock */
const int found = cbf_next_datablock(handle);
if (CBF_NOTFOUND != found) error |= found;
if (CBF_SUCCESS != found) break;
}
}
_cbf_free_cbf2nx_key(*key);
return error;
}
/**
Extracts the miniCBF data directly - by parsing the header - and uses that plus the configuration options from
<code>axisConfig</code> to generate a NeXus file in <code>h5handle</code>. This can extract metadata and image
data from miniCBF files containing multiple datablocks which each contain a single image and insert it into a
given index into the NXentry group specified in <code>h5handle</code>.
Currently, only <code>Pilatus 1.2</code> format headers are supported.
The flags determine:
- Compression algorithm: zlib/CBF/none
- Plugin registration method: automatic/manual
\param handle The miniCBF file to extract data from.
\param h5handle The NeXus file to write data to.
\param axisConfig The configuration settings desribing the axes and their relation to the sample and to each other.
\sa cbf_write_cbf_h5file
\sa cbf_write_minicbf_h5file
\sa cbf_write_nx2cbf
\return An error code.
*/
int cbf_write_minicbf_h5file
(cbf_handle handle,
cbf_h5handle h5handle,
const cbf_config_t * const axisConfig)
{
int error = CBF_SUCCESS;
cbf_node *node = NULL;
const char * saturation_value = NULL;
hid_t detector = CBF_H5FAIL, instrument = CBF_H5FAIL; /* do not free */
#ifdef CBF_USE_ULP
cmp_double_param_t cmp_double_params;
void * cmp_params = &cmp_double_params;
/* set up the comparison parameters */
cmp_double_params.cmp_double_as_float = cbf_has_ULP64() ? h5handle->cmp_double_as_float : 1;
cmp_double_params.ulp32 = h5handle->float_ulp;
#ifndef NO_UINT64_TYPE
cmp_double_params.ulp64 = h5handle->double_ulp;
#endif
#else
void * cmp_params = 0;
CBF_UNUSED(cmp_params);
#endif
if (!handle || !h5handle) return CBF_ARGUMENT;
/* Find the root node */
cbf_failnez (cbf_find_parent (&node, handle->node, CBF_ROOT));
/* Reset the reference counts */
cbf_failnez( cbf_reset_refcounts(handle->dictionary) );
/* ensure the handle contains some basic structure */
cbf_reportnez(cbf_h5handle_require_entry(h5handle,0,0), error);
cbf_reportnez(cbf_h5handle_require_instrument(h5handle,&instrument,0), error);
cbf_reportnez(cbf_h5handle_require_detector(h5handle,&detector,0), error);
/* Do the mappings from CBF to nexus */
cbf_onfailnez(error |= cbf_rewind_datablock(handle), cbf_debug_print("CBF error: cannot find datablock.\n"));
while (CBF_SUCCESS==error) {
/* get some useful parameters out of the metadata as it's converted */
double pixel_x = 0./0., pixel_y = 0./0.;
/* then search for the 'array_data' category */
cbf_onfailnez(cbf_find_category(handle,"array_data"),
cbf_debug_print("CBF error: cannot find category 'array_data'.\n"));
/* First: extract the metadata from the CBF, put it in nexus */
cbf_failnez(cbf_find_column(handle,"header_convention"));
if (1) { /* get the header convention, check it is a value I understand */
const char * value = NULL;
const char vendor_pilatus[] = "PILATUS";
cbf_failnez(cbf_get_value(handle,&value));
if (0 == strncmp(value,vendor_pilatus,_cbf_strlen(vendor_pilatus))) {
const char version_1_2[] = "1.2";
value += _cbf_strlen(vendor_pilatus) + 1;
if (0 == strncmp(value,version_1_2,_cbf_strlen(version_1_2))) {
/* define tokenisation variables, with default buffer sized to almost always be big enough */
size_t n = 256;
char * token = malloc(n*sizeof(char));
int newline = 1;
/* Numerical values for use after main parsing loop */
double beam_x = 0./0., beam_y = 0./0.;
double detector_distance = 0./0.;
/* Flags to determine what information I actually have */
/* Other useful values */
hid_t pilatusDiagnostics = CBF_H5FAIL; /* <- non-nexus group to dump some possibly useful information into */
/* Get the header data */
cbf_debug_print3("%s_%s header found.\n",vendor_pilatus,version_1_2);
cbf_onfailnez(cbf_find_column(handle,"header_contents"),
cbf_debug_print("'header_contents' not found.\n"));
/* re-use the 'value' variable, I won't need the old value anymore */
cbf_onfailnez(cbf_get_value(handle,&value), cbf_debug_print("'header_contents' inaccessible.\n"));
cbf_H5Drequire_flstring(detector,0,"type","pixel array");
if (0) {
const char * _value = value;
do {
_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 1, &_value);
if (!token) break;
newline = !strcmp("\n",token);
printf("token: %s\n", token);
} while (1);
}
cbf_debug_print2("header:\n%s\n",value);
/*
Do the mapping, iterating over each line of the header.
The entire header can be parsed using a trivial FSA, so don't bother with anything particularly complex.
*/
do {
int noMatch = 0;
{ /* Get the first token */
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (!token) break;
if (!strcmp("\n",token)) continue;
}
/* check for a time */
if (isDateTime(token)) {
int found = CBF_SUCCESS;
/* Put the time string into the hdf5 file. */
hid_t type = CBF_H5FAIL;
hid_t dataset = CBF_H5FAIL;
CBF_CALL(cbf_H5Tcreate_string(&type,H5T_VARIABLE));
found = cbf_H5Dfind2(h5handle->nxid,&dataset,"start_time",0,0,0,type);
if (CBF_SUCCESS==found) {
/* comparison with existing data, to extract earliest time */
/* time is in YYYY-MM-DDThh:mm:ss.s* format, should be able to use strcmp to order them */
/* first read the existing timestamp */
const hid_t currType = H5Dget_type(dataset);
hid_t currMemType = CBF_H5FAIL;
hid_t space = H5Dget_space(dataset);
hsize_t ndims,dims[H5S_MAX_RANK];
ndims = H5Sget_simple_extent_dims(space,dims,0);
if (ndims < 0) error |= CBF_FORMAT;
if (ndims == 0) dims[0] = 1;
if (H5Tis_variable_str(currType)) {
char * * buf = malloc(dims[0]*sizeof(char *));
cbf_H5Tcreate_string(&currMemType,H5T_VARIABLE);
cbf_H5Dread2(dataset,0,0,0,(void * const)buf,currMemType);
cbf_H5Tfree(currMemType);
/* then compare them */
if (buf[0] && strcmp(token,buf[0]) < 0) {
/* store the oldest timestamp */
CBF_CALL(cbf_H5Dwrite2(dataset,0,0,0,&token,type));
}
H5Dvlen_reclaim(currType, space, H5P_DEFAULT, buf);
free((void*)buf);
cbf_H5Tfree(currType);
} else if (H5Tget_class(currType)==H5T_STRING){
char * buf = (char *)malloc(dims[0]*(H5Tget_size(currType)+1));
cbf_H5Dread2(dataset,0,0,0,(void * const)buf,currType);
if (strcmp(token,buf) < 0) {
/* store the oldest timestamp */
CBF_CALL(cbf_H5Dwrite2(dataset,0,0,0,&token,type));
}
}
CBF_CALL(cbf_H5Sfree(space));
} else if (CBF_NOTFOUND==found) {
/* create the dataset & write the data */
CBF_CALL(cbf_H5Dcreate(h5handle->nxid,&dataset,"start_time",0,0,0,0,type));
CBF_CALL(cbf_H5Dwrite2(dataset,0,0,0,&token,type));
} else {
error |= found;
cbf_debug_print2("error: %s\n",cbf_strerror(found));
}
cbf_H5Dfree(dataset);
found = cbf_H5Dfind2(h5handle->nxid,&dataset,"end_time",0,0,0,type);
if (CBF_SUCCESS==found) {
/* comparison with existing data, to extract latest time */
/* time is in YYYY-MM-DDThh:mm:ss.s* format, should be able to use strcmp to order them */
/* first read the existing timestamp */
const hid_t currType = H5Dget_type(dataset);
hid_t currMemType = CBF_H5FAIL;
hid_t space = H5Dget_space(dataset);
hsize_t ndims,dims[H5S_MAX_RANK];
ndims = H5Sget_simple_extent_dims(space,dims,0);
if (ndims < 0) error |= CBF_FORMAT;
if (ndims == 0) dims[0] = 1;
if (H5Tis_variable_str(currType)) {
char * * buf = malloc(dims[0]*sizeof(char *));
cbf_H5Tcreate_string(&currMemType,H5T_VARIABLE);
cbf_H5Dread2(dataset,0,0,0,(void * const)buf,currMemType);
cbf_H5Tfree(currMemType);
/* then compare them */
if (buf[0] && strcmp(token,buf[0]) > 0) {
/* store the oldest timestamp */
CBF_CALL(cbf_H5Dwrite2(dataset,0,0,0,&token,type));
}
H5Dvlen_reclaim(currType, space, H5P_DEFAULT, buf);
if (buf) free((void*)buf);
cbf_H5Tfree(currType);
} else if (H5Tget_class(currType)==H5T_STRING){
char * buf = (char *)malloc(dims[0]*(H5Tget_size(currType)+1));
cbf_H5Dread2(dataset,0,0,0,(void * const)buf,currType);
if (strcmp(token,buf) < 0) {
/* store the oldest timestamp */
CBF_CALL(cbf_H5Dwrite2(dataset,0,0,0,&token,type));
}
if (buf) free((void*)buf);
}
} else if (CBF_NOTFOUND==found) {
/* create the dataset & write the data */
CBF_CALL(cbf_H5Dcreate(h5handle->nxid,&dataset,"end_time",0,0,0,0,type));
CBF_CALL(cbf_H5Dwrite2(dataset,0,0,0,&token,type));
} else {
error |= found;
cbf_debug_print2("error: %s\n",cbf_strerror(found));
}
cbf_H5Dfree(dataset);
cbf_H5Tfree(type);
} else if (!cbf_cistrcmp("Pixel_size",token)) {
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
{ /* Get x pixel size */
hid_t h5data = CBF_H5FAIL;
const double num = strtod(token,0);
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(detector,&h5data,"x_pixel_size",num,cmp_double,cmp_params));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (cbf_cistrcmp(token,"m")) {
error |= CBF_H5DIFFERENT;
cbf_debug_print("error: expected units of 'm' for 'Pixel_size'");
} else {
pixel_x = num;
}
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
cbf_H5Dfree(h5data);
}
/* Get next useful token, just skip over useless stuff */
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
{ /* Get y pixel size */
hid_t h5data = CBF_H5FAIL;
const double num = strtod(token,0);
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(detector,&h5data,"y_pixel_size",num,cmp_double,cmp_params));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (cbf_cistrcmp(token,"m")) {
error |= CBF_H5DIFFERENT;
cbf_debug_print2("error: %s\n","expected units of 'm' for 'Pixel_size'");
} else {
pixel_y = num;
}
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
cbf_H5Dfree(h5data);
}
} else if (!cbf_cistrcmp("Silicon",token)) {
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (!strcmp("sensor",token)) {
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (!strcmp("thickness",token)) {
CBF_CALL(cbf_H5Drequire_flstring(detector,0,"sensor_material","Silicon"));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
{
hid_t h5data = CBF_H5FAIL;
double num = strtod(token,0);
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(detector,&h5data,"sensor_thickness",
num,cmp_double,cmp_params));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (cbf_cistrcmp(token,"m")) {
error |= CBF_H5DIFFERENT;
cbf_debug_print("expected units of 'm' for 'Silicon sensor thickness'");
}
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
cbf_H5Dfree(h5data);
}
} else noMatch = 1;
} else noMatch = 1;
} else if (!cbf_cistrcmp("Detector_distance",token)) {
/* Get value & units */
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
detector_distance = strtod(token,0);
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (!cbf_cistrcmp(token,"m")) {
hid_t h5data = CBF_H5FAIL;
const hsize_t max[] = {H5S_UNLIMITED};
const hsize_t cnk[] = {1};
hsize_t off[1];
const hsize_t cnt[] = {1};
hsize_t buf[] = {0};
off[0] = h5handle->slice;
CBF_CALL(cbf_H5Drequire(detector,&h5data,"distance",1,max,cnk,buf,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dinsert(h5data,off,0,cnt,buf,&detector_distance,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
cbf_H5Dfree(h5data);
} else {
error |= CBF_H5DIFFERENT;
cbf_debug_print("error: expected units of 'm' for 'Detector_distance'");
}
} else if (!cbf_cistrcmp("Detector",token)) {
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 1, &value));
CBF_CALL(cbf_H5Drequire_flstring(detector,0,"description",token));
} else if (!cbf_cistrcmp("N_excluded_pixels",token)) {
hid_t h5location = CBF_H5FAIL;
CBF_CALL(_cbf_NXGrequire(detector,&h5location,"pilatus_diagnostics","NXcollection"));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 1, &value));
CBF_CALL(cbf_H5Drequire_flstring(h5location,0,"N_excluded_pixels",token));
cbf_H5Gfree(h5location);
} else if (!cbf_cistrcmp("Excluded_pixels",token)) {
hid_t h5location = CBF_H5FAIL;
CBF_CALL(_cbf_NXGrequire(detector,&h5location,"pilatus_diagnostics","NXcollection"));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 1, &value));
CBF_CALL(cbf_H5Drequire_flstring(h5location,0,"Excluded_pixels",token));
cbf_H5Gfree(h5location);
} else if (!cbf_cistrcmp("Flat_field",token)) {
hid_t h5location = CBF_H5FAIL;
CBF_CALL(_cbf_NXGrequire(detector,&h5location,"pilatus_diagnostics","NXcollection"));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 1, &value));
CBF_CALL(cbf_H5Drequire_flstring(h5location,0,"Flat_field",token));
cbf_H5Gfree(h5location);
} else if (!cbf_cistrcmp("Trim_file",token)) {
hid_t h5location = CBF_H5FAIL;
CBF_CALL(_cbf_NXGrequire(detector,&h5location,"pilatus_diagnostics","NXcollection"));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 1, &value));
CBF_CALL(cbf_H5Drequire_flstring(h5location,0,"Trim_file",token));
cbf_H5Gfree(h5location);
} else if (!cbf_cistrcmp("Exposure_time",token)) {
hid_t h5data = CBF_H5FAIL;
hid_t h5location = detector;
const hid_t h5type = H5T_IEEE_F64LE;
const char h5name[] = "count_time";
const hsize_t max[] = {H5S_UNLIMITED};
const hsize_t chunk[] = {1};
hsize_t offset[1];
const hsize_t count[] = {1};
hsize_t buf[] = {0};
double num = 0./0.;
offset[0] = h5handle->slice;
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
num = strtod(token,0);
CBF_CALL(cbf_H5Drequire(h5location,&h5data,h5name,1,max,chunk,buf,h5type));
CBF_CALL(cbf_H5Dinsert(h5data,offset,0,count,buf,&num,H5T_NATIVE_DOUBLE));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
cbf_H5Dfree(h5data);
} else if (!cbf_cistrcmp("Exposure_period",token)) {
hid_t h5data = CBF_H5FAIL;
hid_t h5location = detector;
const hid_t h5type = H5T_IEEE_F64LE;
const char h5name[] = "frame_time";
const hsize_t max[] = {H5S_UNLIMITED};
const hsize_t chunk[] = {1};
hsize_t offset[1];
const hsize_t count[] = {1};
hsize_t buf[] = {0};
double num = 0./0.;
offset[0] = h5handle->slice;
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
num = strtod(token,0);
CBF_CALL(cbf_H5Drequire(h5location,&h5data,h5name,1,max,chunk,buf,h5type));
CBF_CALL(cbf_H5Dinsert(h5data,offset,0,count,buf,&num,H5T_NATIVE_DOUBLE));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
cbf_H5Dfree(h5data);
} else if (!cbf_cistrcmp("Tau",token)) {
hid_t h5data = CBF_H5FAIL;
hid_t h5location = detector;
const char h5name[] = "dead_time";
double num = 0./0.;
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
num = strtod(token,0);
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(h5location,&h5data,h5name,num,cmp_double,cmp_params));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
cbf_H5Dfree(h5data);
} else if (!cbf_cistrcmp("Count_cutoff",token)) {
/* store the string for later interpretation */
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (CBF_SUCCESS==error) {
saturation_value = _cbf_strdup(token);
}
} else if (!cbf_cistrcmp("Threshold_setting",token)) {
double num = 0./0.;
hid_t h5data = CBF_H5FAIL;
/* Get value & units */
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
num = strtod(token,0);
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(detector,&h5data,"threshold_energy", num,cmp_double,cmp_params));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
cbf_H5Dfree(h5data);
} else if (!cbf_cistrcmp("Gain_setting",token)) {
int error = CBF_SUCCESS;
/* [1,end): gain setting string */
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 1, &value));
CBF_CALL(cbf_H5Drequire_flstring(detector,0,"gain_setting",token));
} else if (!cbf_cistrcmp("Wavelength",token)) {
/* Get value & units */
hid_t h5data = CBF_H5FAIL;
hid_t sample = CBF_H5FAIL;
hid_t beam = CBF_H5FAIL;
const hid_t h5type = H5T_IEEE_F64LE;
const char h5name[] = "incident_wavelength";
const hsize_t max[] = {H5S_UNLIMITED};
const hsize_t chunk[] = {1};
hsize_t offset[1];
const hsize_t count[] = {1};
hsize_t buf[] = {0};
double num = 0./0.;
offset[0] = h5handle->slice;
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
num = strtod(token,0);
CBF_CALL(cbf_h5handle_require_sample(h5handle, &sample,0));
CBF_CALL(_cbf_NXGrequire(sample,&beam,"beam","NXbeam"));
CBF_CALL(cbf_H5Drequire(beam,&h5data,h5name,1,max,chunk,buf,h5type));
CBF_CALL(cbf_H5Dinsert(h5data,offset,0,count,buf,&num,H5T_NATIVE_DOUBLE));
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (!strcmp(token,"A")||!strcmp(token,"angstroms")) {
CBF_CALL(cbf_H5Arequire_string(h5data,"units","angstroms"));
} else {
CBF_CALL(cbf_H5Arequire_string(h5data,"units",token));
}
cbf_H5Gfree(beam);
cbf_H5Dfree(h5data);
} else if (!cbf_cistrcmp("Beam_xy",token)) {
/*
Extract x & y positions from the header, put them into the file later.
I might need to read all the header to know if I can actually convert these values to NeXus data.
*/
double num_x = 0./0., num_y = 0./0.;
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
num_x = strtod(token,0);
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
num_y = strtod(token,0);
/* Extract the units (should be pixels, but I don't want to lose important information if it isn't) */
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (cbf_cistrcmp(token,"pixels")) {
error |= CBF_H5DIFFERENT;
cbf_debug_print("error: expected units of 'pixels' for 'Beam_xy'");
} else {
beam_x = num_x;
beam_y = num_y;
}
} else if (!cbf_cistrcmp("Flux",token)) {
/*
Either a number with some units or a random string, only do anything if it's a number.
*/
const char * end = 0;
double num = 0./0.;
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
num = strtod(token,(char**)(&end));
if (end != token && 0.0 != num) {
/* I have a valid & useful flux, map it to /entry/sample/beam/flux */
hid_t sample = CBF_H5FAIL; /* DO NOT FREE THIS! */
hid_t h5data = CBF_H5FAIL;
hid_t h5location = CBF_H5FAIL;
/* Ensure I have a valid sample group */
CBF_CALL(cbf_h5handle_require_sample(h5handle, &sample,0));
/* Ensure I have a valid beam group */
CBF_CALL(cbf_H5Grequire(sample,&h5location,"beam"));
CBF_CALL(cbf_H5Arequire_string(h5location, "NX_class", "NXbeam"));
/* Store value & units */
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(h5location, &h5data, "flux", num, cmp_double,cmp_params));
CBF_CALL(cbf_H5Arequire_string(h5data, "units", "s-1"));
/* cleanup temporary dataset */
cbf_H5Dfree(h5data);
cbf_H5Gfree(h5location);
}
} else if (!cbf_cistrcmp("Filter_transmission",token)) {
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
/* write it to the file */
if (cbf_H5Ivalid(instrument)) {
/* Get value */
const double num = strtod(token,0);
hid_t h5location = CBF_H5FAIL;
CBF_CALL(cbf_H5Grequire(instrument,&h5location,"attenuator"));
CBF_CALL(cbf_H5Arequire_string(h5location, "NX_class", "NXattenuator"));
/* Get value & units */
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(h5location, 0, "attenuator_transmission",
num, cmp_double,cmp_params));
/* cleanup temporary dataset */
cbf_H5Gfree(h5location);
}
} else if (!cbf_cistrcmp("Polarization",token)) {
hid_t sample = CBF_H5FAIL; /* DO NOT FREE THIS! */
hid_t h5location = CBF_H5FAIL;
/* Ensure I have a valid sample group */
CBF_CALL(cbf_h5handle_require_sample(h5handle, &sample,0));
/* Ensure I have a valid beam group */
CBF_CALL(cbf_H5Grequire(sample,&h5location,"beam"));
CBF_CALL(cbf_H5Arequire_string(h5location, "NX_class", "NXbeam"));
/* Get value & units */
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value));
if (CBF_SUCCESS==error) {
/* extract value from header */
const double p = strtod(token, 0);
/* convert to nexus format */
double polarisation[4];
const hsize_t max[] = {H5S_UNLIMITED,4};
const hsize_t chunk[] = {1,4};
hsize_t offset[2];
const hsize_t count[] = {1,4};
hsize_t buf[] = {0,0};
hid_t h5data = CBF_H5FAIL;
hid_t h5type = H5T_IEEE_F64LE;
const char h5name[] = "incident_polarisation_stokes";
polarisation[0] = 1.0;
polarisation[1] = p;
polarisation[2] = 0.;
polarisation[3] = 0.;
offset[0] = h5handle->slice;
offset[1] = 0;
CBF_CALL(cbf_H5Drequire(h5location,&h5data,h5name,2,max,chunk,buf,h5type));
CBF_CALL(cbf_H5Dinsert(h5data,offset,0,count,buf,polarisation,H5T_NATIVE_DOUBLE));
cbf_H5Dfree(h5data);
}
cbf_H5Gfree(h5location);
} else if (!cbf_cistrcmp("Alpha",token)) {
#define _CBF_CONVERT_AXIS(axisName) \
CBFM_PROLOG { \
/* Find the data for the current axis, or fail */ \
cbf_configItem_t * const axisItem = cbf_config_findMinicbf(axisConfig, axisName); \
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value)); \
if (cbf_config_end(axisConfig) == axisItem) { \
error |= CBF_NOTFOUND; \
cbf_debug_print2("Config settings for axis '%s' could not be found: " \
"this will eventually be a fatal error\n", axisName); \
} else if (axisItem->convert) { \
hid_t h5data = CBF_H5FAIL; \
hid_t sample = CBF_H5FAIL; \
hid_t axes = CBF_H5FAIL; \
const hsize_t max[] = {H5S_UNLIMITED}; \
const hsize_t chunk[] = {1}; \
hsize_t offset[1]; \
const hsize_t count[] = {1}; \
hsize_t buf[] = {0}; \
double num; \
num = strtod(token,0); \
offset[0] = h5handle->slice; \
CBF_CALL(cbf_h5handle_require_sample(h5handle, &sample,0)); \
CBF_CALL(cbf_H5Grequire(sample,&axes,"transformations")); \
CBF_CALL(cbf_H5Arequire_string(axes,"NX_class","NXtransformations")); \
CBF_CALL(cbf_H5Drequire(axes,&h5data,axisItem->nexus,1,max,chunk,buf,H5T_IEEE_F64LE)); \
CBF_CALL(cbf_H5Dinsert(h5data,offset,0,count,buf,&num,H5T_NATIVE_DOUBLE)); \
CBF_CALL(_cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value)); \
if (axisItem->depends_on) { \
const char dot[] = "."; \
if (strcmp(axisItem->depends_on,dot)) { \
const char path_empty[] = ""; \
const char path_sample[] = "sample"; \
const char axis_group_name[] = "transformations"; \
const char * path_parts[6]; \
const char * axis_path; \
path_parts[0] = path_empty; \
path_parts[1] = h5handle->nxid_name; \
path_parts[2] = path_sample; \
path_parts[3] = axis_group_name; \
path_parts[4] = axisItem->depends_on; \
path_parts[5] = 0; \
axis_path = _cbf_str_join(path_parts,'/'); \
CBF_CALL(CBFM_pilatusAxis2nexusAxisAttrs(h5data,token,axis_path,axisItem,cmp_double,cmp_params )); \
free((void*)axis_path); \
} else { \
CBF_CALL(CBFM_pilatusAxis2nexusAxisAttrs(h5data,token,dot,axisItem,cmp_double,cmp_params)); \
} \
} else { \
CBF_CALL(CBFM_pilatusAxis2nexusAxisAttrs(h5data,token,"",axisItem,cmp_double,cmp_params)); \
} \
cbf_H5Dfree(h5data); \
cbf_H5Gfree(axes); \
} \
} CBFM_EPILOG
_CBF_CONVERT_AXIS("Alpha");
} else if (!cbf_cistrcmp("Kappa",token)) {
_CBF_CONVERT_AXIS("Kappa");
} else if (!cbf_cistrcmp("Phi",token)) {
_CBF_CONVERT_AXIS("Phi");
} else if (!cbf_cistrcmp("Chi",token)) {
_CBF_CONVERT_AXIS("Chi");
} else if (!cbf_cistrcmp("Omega",token)) {
_CBF_CONVERT_AXIS("Omega");
} else if (!cbf_cistrcmp("Start_angle",token)) {
_CBF_CONVERT_AXIS("Start_angle");
} else if (!cbf_cistrcmp("Detector_2theta",token)) {
_CBF_CONVERT_AXIS("Detector_2theta");
} else if (!cbf_cistrcmp("Image_path",token)) {
} else if (!cbf_cistrcmp("Angle_increment",token)) {
} else noMatch = 1;
if (noMatch) {
cbf_debug_print2("warning: Could not match entry in pilatus header: '%s'\n",token);
}
/* Done matching the entry from this line, go on to the next one */
while (token && strcmp("\n",token)) {
error = _cbf_scan_pilatus_V1_2_miniheader(&token, &n, &newline, 0, &value);
if (error) cbf_debug_print2("error %s\n",cbf_strerror(error));
}
} while (1);
free(token);
{
/*
Record the axis that the sample depends on
*/
const char * const depends_on = cbf_config_getSampleDependsOn(axisConfig);
if (depends_on) {
hid_t h5location = CBF_H5FAIL; /* DO NOT FREE THIS */
const char path_empty[] = "";
const char path_sample[] = "sample";
const char axis_group_name[] = "transformations";
const char * path_parts[6];
const char * axis_path;
path_parts[0] = path_empty;
path_parts[1] = h5handle->nxid_name;
path_parts[2] = path_sample;
path_parts[3] = axis_group_name;
path_parts[4] = depends_on;
path_parts[5] = 0;
axis_path = _cbf_str_join(path_parts,'/');
CBF_CALL(cbf_h5handle_require_sample(h5handle, &h5location,0));
CBF_CALL(cbf_H5Drequire_flstring(h5location,0,"depends_on",axis_path));
free((void*)axis_path);
} else {
cbf_debug_print("Config settings for 'Sample' could not be found: "
"this will eventually be a fatal error\n");
}
cbf_debug_print("'sample/depends_on' written\n");
}
{ /* write beam_center_x */
hid_t h5data = CBF_H5FAIL;
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(detector, &h5data, "beam_center_x",
beam_x*pixel_x, cmp_double, cmp_params));
CBF_CALL(cbf_H5Arequire_string(h5data,"units","m"));
cbf_H5Dfree(h5data);
}
{ /* write beam_center_y */
hid_t h5data = CBF_H5FAIL;
CBF_CALL(CBFM_H5Drequire_scalar_F64LE2(detector, &h5data, "beam_center_y",
beam_y*pixel_y, cmp_double, cmp_params));
CBF_CALL(cbf_H5Arequire_string(h5data,"units","m"));
cbf_H5Dfree(h5data);
}
{
/* Detector axes
Requires:
* beam_center_x
* beam_center_y
* detector_distance
Creates dependency chain:
detector -> rotation -> translation -> .
*/
/* TODO: write depends_on attributes with full paths */
/* Common settings */
const hsize_t vdims[] = {3};
double vbuf[3] = {0./0.};
const hsize_t max[] = {H5S_UNLIMITED};
hsize_t buf[] = {0};
const hsize_t chunk[] = {1};
hsize_t h5offset[1];
const hsize_t h5count[] = {1};
hid_t h5location = detector;
hid_t axis_group = CBF_H5FAIL;
const char * axis_translation = "translation";
const char * axis_rotation = "rotation";
const char path_empty[] = "";
const char axis_group_name[] = "transformations";
h5offset[0] = h5handle->slice;
CBF_CALL(cbf_H5Grequire(h5location,&axis_group,axis_group_name));
CBF_CALL(cbf_H5Arequire_string(axis_group,"NX_class","NXtransformations"));
{ /* Translation-specific */
hid_t h5data = CBF_H5FAIL;
hid_t h5type = H5T_IEEE_F64LE;
const double num = detector_distance;
const double vector[] = {0.0, 0.0, 1.0};
double offset[3];
offset[0] = -beam_x*pixel_x;
offset[1] = -beam_y*pixel_y;
offset[2] = 0.0;
CBF_CALL(cbf_H5Drequire(axis_group,&h5data,axis_translation,1,max,chunk,buf,h5type));
CBF_CALL(cbf_H5Dinsert(h5data,h5offset,0,h5count,buf,&num,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Arequire_string(h5data,"units","m"));
CBF_CALL(cbf_H5Arequire_string(h5data,"offset_units","m"));
CBF_CALL(cbf_H5Arequire_string(h5data,"transformation_type","translation"));
CBF_CALL(cbf_H5Arequire_string(h5data,"depends_on","."));
CBF_CALL(CBFM_H5Arequire_cmp2(h5data,"vector",1,vdims,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,vector,
vbuf,cmp_double,cmp_params));
CBF_CALL(CBFM_H5Arequire_cmp2(h5data,"offset",1,vdims,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,offset,
vbuf,cmp_double,cmp_params));
/* cleanup temporary datasets */
cbf_H5Dfree(h5data);
cbf_debug_print2("'detector/%s' written\n",axis_translation);
}
{ /* Rotation-specific */
hid_t h5data = CBF_H5FAIL;
hid_t h5type = H5T_IEEE_F64LE;
const double num = 180.0;
const double vector[] = {0.0, 0.0, 1.0};
const char * path_parts[7];
const char * axis_path;
path_parts[0] = path_empty;
path_parts[1] = h5handle->nxid_name;
path_parts[2] = h5handle->nxinstrument_name;
path_parts[3] = h5handle->nxdetector_names[h5handle->cur_detector];
path_parts[4] = axis_group_name;
path_parts[5] = axis_translation;
path_parts[6] = 0;
axis_path = _cbf_str_join(path_parts,'/');
CBF_CALL(cbf_H5Drequire(axis_group,&h5data,axis_rotation,1,max,chunk,buf,h5type));
CBF_CALL(cbf_H5Dinsert(h5data,h5offset,0,h5count,buf,&num,H5T_NATIVE_DOUBLE));
CBF_CALL(cbf_H5Arequire_string(h5data,"units","degrees"));
CBF_CALL(cbf_H5Arequire_string(h5data,"transformation_type","rotation"));
CBF_CALL(cbf_H5Arequire_string(h5data,"depends_on",axis_path));
CBF_CALL(CBFM_H5Arequire_cmp2(h5data,"vector",1,vdims,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,
vector, vbuf,cmp_double,cmp_params));
/* cleanup temporary datasets */
cbf_H5Dfree(h5data);
free((void*)axis_path);
cbf_debug_print2("'detector/%s' written\n",axis_rotation);
}
cbf_H5Gfree(axis_group);
{ /* tie everything to the detector */
const char * path_parts[7];
const char * axis_path;
path_parts[0] = path_empty;
path_parts[1] = h5handle->nxid_name;
path_parts[2] = h5handle->nxinstrument_name;
path_parts[3] = h5handle->nxdetector_names[h5handle->cur_detector];
path_parts[4] = axis_group_name;
path_parts[5] = axis_rotation;
path_parts[6] = 0;
axis_path = _cbf_str_join(path_parts,'/');
CBF_CALL(cbf_H5Drequire_flstring(detector,0,"depends_on",axis_path));
free((void*)axis_path);
}
}
cbf_H5Gfree(pilatusDiagnostics);
}
}
}
/* Second: extract the raw data from the CBF, put it in nexus */
cbf_onfailnez(cbf_find_column(handle,"data"), cbf_debug_print("error: cannot find column `data'.\n"));
/* get the first row, TODO: convert every row */
cbf_onfailnez(cbf_select_row(handle,0), cbf_debug_print("error: cannot find row 0.\n"));
if(1) {
hsize_t h5dim[] = {0, 0, 0};
/* convert the data */
CBF_CALL(cbf_write_array_h5file(handle->node, handle->row, h5handle, saturation_value, 0, h5dim));
/* ensure I have an axis for each index of the image - mapping pixel indices to spatial coordinates */
if (1) {
hid_t h5axis = CBF_H5FAIL;
const char h5name[] = "x_pixel_offset";
hsize_t buf[] = {0};
const int found = cbf_H5Dfind2(detector,&h5axis,h5name,1,h5dim+2,buf,H5T_IEEE_F64LE);
const hsize_t offset[] = {0};
const hsize_t * count = h5dim+2;
double * const expected_data = malloc((*count) * sizeof(double));
{
hsize_t n = 0;
for (n=0; n!=h5dim[2]; ++n)
expected_data[n] = (double)(n)*pixel_x;
}
if (CBF_SUCCESS==found) {
double * const actual_data = malloc((*count) * sizeof(double));
CBF_CALL(cbf_H5Dread2(h5axis,offset,0,count,actual_data,H5T_NATIVE_DOUBLE));
if (CBFM_cmp_double(expected_data, actual_data, *count, cmp_params)) {
cbf_debug_print2("error: data doesn't match in %s, x pixel size might not match\n",h5name);
error |= CBF_H5DIFFERENT;
}
free((void*)(actual_data));
} else if (CBF_NOTFOUND==found) {
CBF_CALL(cbf_H5Dcreate(detector,&h5axis,h5name,1,h5dim+2,h5dim+2,0,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dwrite2(h5axis,offset,0,count,expected_data,H5T_NATIVE_DOUBLE));
} else {
error |= found;
cbf_debug_print2("Error locating axis: %s\n",cbf_strerror(found));
}
free((void*)(expected_data));
CBF_CALL(cbf_H5Arequire_string(h5axis,"units","m"));
{
const double vector[] = {1.0, 0.0, 0.0};
const hsize_t vdims[] = {3};
double vbuf[3] = {0./0.};
CBF_CALL(CBFM_H5Arequire_cmp2(h5axis,"vector",1,vdims,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,
vector,vbuf,cmp_double,cmp_params));
}
CBF_CALL(cbf_H5Arequire_string(h5axis,"transformation_type","translation"));
{
const char path_empty[] = "";
const char path_transformations[] = "transformations";
const char path_axis[] = "rotation";
const char * path_parts[7];
const char * axis_path;
path_parts[0] = path_empty;
path_parts[1] = h5handle->nxid_name;
path_parts[2] = h5handle->nxinstrument_name;
path_parts[3] = h5handle->nxdetector_names[h5handle->cur_detector];
path_parts[4] = path_transformations;
path_parts[5] = path_axis;
path_parts[6] = 0;
axis_path = _cbf_str_join(path_parts,'/');
CBF_CALL(cbf_H5Arequire_string(h5axis,"depends_on",axis_path));
free((void*)axis_path);
}
cbf_H5Dfree(h5axis);
}
if (1) {
hid_t h5axis = CBF_H5FAIL;
const char h5name[] = "y_pixel_offset";
hsize_t buf[] = {0};
const int found = cbf_H5Dfind2(detector,&h5axis,h5name,1,h5dim+1,buf,H5T_IEEE_F64LE);
const hsize_t offset[] = {0};
const hsize_t * count = h5dim+1;
double * const expected_data = malloc((*count) * sizeof(double));
{
hsize_t n = 0;
for (n=0; n!=h5dim[1]; ++n)
expected_data[n] = (double)(n)*pixel_y;
}
if (CBF_SUCCESS==found) {
double * const actual_data = malloc((*count) * sizeof(double));
CBF_CALL(cbf_H5Dread2(h5axis,offset,0,count,actual_data,H5T_NATIVE_DOUBLE));
if (CBFM_cmp_double(expected_data, actual_data, *count, cmp_params)) {
cbf_debug_print2("error: data doesn't match in %s, y pixel size might not match\n",h5name);
error |= CBF_H5DIFFERENT;
}
free((void*)(actual_data));
} else if (CBF_NOTFOUND==found) {
CBF_CALL(cbf_H5Dcreate(detector,&h5axis,h5name,1,h5dim+1,h5dim+1,0,H5T_IEEE_F64LE));
CBF_CALL(cbf_H5Dwrite2(h5axis,offset,0,count,expected_data,H5T_NATIVE_DOUBLE));
} else {
error |= found;
cbf_debug_print2("Error locating axis: %s\n",cbf_strerror(found));
}
free((void*)(expected_data));
CBF_CALL(cbf_H5Arequire_string(h5axis,"units","m"));
{
const double vector[] = {0.0, 1.0, 0.0};
const hsize_t vdims[] = {3};
double vbuf[3] = {0./0.};
CBF_CALL(CBFM_H5Arequire_cmp2(h5axis,"vector",1,vdims,H5T_IEEE_F64LE,H5T_NATIVE_DOUBLE,
vector,vbuf,cmp_double,cmp_params));
}
CBF_CALL(cbf_H5Arequire_string(h5axis,"transformation_type","translation"));
{
const char path_empty[] = "";
const char path_transformations[] = "transformations";
const char path_axis[] = "rotation";
const char * path_parts[7];
const char * axis_path;
path_parts[0] = path_empty;
path_parts[1] = h5handle->nxid_name;
path_parts[2] = h5handle->nxinstrument_name;
path_parts[3] = h5handle->nxdetector_names[h5handle->cur_detector];
path_parts[4] = path_transformations;
path_parts[5] = path_axis;
path_parts[6] = 0;
axis_path = _cbf_str_join(path_parts,'/');
CBF_CALL(cbf_H5Arequire_string(h5axis,"depends_on",axis_path));
free((void*)axis_path);
}
cbf_H5Dfree(h5axis);
}
CBF_CALL(cbf_write_cbf2nx__link_h5data(handle, h5handle));
}
free((void*)saturation_value);
++h5handle->slice;
if (CBF_SUCCESS != cbf_next_datablock(handle)) break;
}
return error;
}
/* Open an HDF5 File handle */
int cbf_open_h5handle(cbf_h5handle *h5handle,
const char * h5filename) {
hid_t fcreate_prop_list;
/* check that the file name has been specified and
is an HDF5 file */
if (!h5filename || !H5Fis_hdf5(h5filename)) return CBF_ARGUMENT;
/* ensure the HDF5 library is ready */
cbf_h5failneg(H5open(),CBF_H5ERROR);
cbf_failnez(cbf_make_h5handle(h5handle));
cbf_h5onfailneg(fcreate_prop_list = H5Pcreate(H5P_FILE_ACCESS),
CBF_ALLOC,cbf_free((void**) h5handle, NULL));
(*h5handle)->rwmode = 0;
cbf_h5onfailneg(H5Pset_fclose_degree(fcreate_prop_list,H5F_CLOSE_STRONG),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
cbf_h5onfailneg((*h5handle)->hfile = H5Fopen(h5filename,
H5F_ACC_RDONLY,fcreate_prop_list),
CBF_ARGUMENT,cbf_free((void**) h5handle, NULL));
if ((*h5handle)->nxfilename) {
cbf_free((void *)((*h5handle)->nxfilename),NULL);
(*h5handle)->nxfilename = NULL;
}
cbf_onfailnez(cbf_require_h5handle_filename(*h5handle),
{cbf_free_h5handle(*h5handle); *h5handle = NULL;});
cbf_h5onfailneg(H5Pclose(fcreate_prop_list), CBF_ARGUMENT,
{cbf_free_h5handle(*h5handle); *h5handle = NULL;});
return CBF_SUCCESS;
}
/* Convert an HDF5 typeclass to a string
and flag for atomic or not
copies up to n-1 characters of the
type string to buffer*/
int cbf_h5type_class_string(H5T_class_t type_class,
char * buffer,
int * atomic, size_t n ) {
int good_type;
good_type = 0;
*atomic = 1;
buffer[n-1] = '\0';
switch(type_class) {
case H5T_INTEGER:
_cbf_strncpy(buffer,"H5T_INTEGER",n-1);
break;
case H5T_FLOAT:
_cbf_strncpy(buffer,"H5T_FLOAT",n-1);
break;
case H5T_STRING:
_cbf_strncpy(buffer,"H5T_STRING",n-1);
break;
case H5T_BITFIELD:
_cbf_strncpy(buffer,"H5T_BITFIELD",n-1);
break;
case H5T_OPAQUE:
_cbf_strncpy(buffer,"H5T_OPAQUE",n-1);
break;
case H5T_COMPOUND:
_cbf_strncpy(buffer,"H5T_COMPOUND",n-1);
*atomic = 0;
break;
case H5T_REFERENCE:
_cbf_strncpy(buffer,"H5T_REFERENCE",n-1);
break;
case H5T_ENUM:
_cbf_strncpy(buffer,"H5T_ENUM",n-1);
*atomic = 0;
break;
case H5T_VLEN:
_cbf_strncpy(buffer,"H5T_VLEN",n-1);
*atomic = 0;
break;
case H5T_ARRAY:
_cbf_strncpy(buffer,"H5T_ARRAY",n-1);
*atomic = 0;
break;
case -1:
_cbf_strncpy(buffer,".",n-1);
good_type = CBF_ARGUMENT;
*atomic = 0;
break;
default:
_cbf_strncpy(buffer,"UNKNOWN",n-1);
good_type = CBF_ARGUMENT;
*atomic = 0;
break;
}
return good_type;
}
/* Store an HDF5 Dataset in CBF handle as a column, using
category categoryname, ...
If target_row is -1, the new column is appended to any
existing column
If target row is >= 0, overwrites any existing rows starting
at target_row
*/
int cbf_h5ds_store_as_column(cbf_handle handle,
int target_row,
const char * columnname,
const char * categoryname, hid_t obj_id,
hid_t space, hid_t type,
void ** value) {
char buffer[25];
int errorcode;
unsigned char* data = NULL;
char h5t_type_class[14], h5t_base_type_class[14];
hid_t base_type;
hid_t native_type;
int atomic;
int ndims = 0, kdims = 0, ii;
unsigned int rows;
hsize_t dims[H5S_MAX_RANK];
hsize_t maxdims[H5S_MAX_RANK];
char * byte_order;
size_t type_size, total_size, total_dim;
H5T_class_t type_class, base_type_class;
H5T_order_t type_order;
H5T_sign_t type_sign = H5T_SGN_ERROR;
errorcode = 0;
cbf_reportnez(cbf_require_category(handle,categoryname),errorcode);
/* Give the name of this dataset as its own id */
cbf_reportnez(cbf_require_column(handle,columnname),errorcode);
rows = 0;
cbf_reportnez(cbf_count_rows(handle,&rows),errorcode);
if (target_row==-1) {
cbf_reportnez(cbf_new_row(handle),errorcode);
target_row = rows;
} else {
if ((unsigned int)target_row >= rows ) {
for (ii=rows; ii <= target_row; ii++) {
cbf_reportnez(cbf_new_row(handle),errorcode);
}
}
cbf_reportnez(cbf_select_row(handle,target_row),errorcode);
}
/* get the class, and, if not atomic
try to get the base class for an array
give up otherwise */
type_class = H5Tget_class(type);
native_type = H5Tget_native_type(type,H5T_DIR_ASCEND);
base_type = CBF_H5FAIL;
type_size = 0;
if (value) *value = 0;
type_order = -1;
kdims= ndims = H5Sget_simple_extent_ndims(space);
if (ndims <= 0) ndims = 1;
H5Sget_simple_extent_dims(space,dims,maxdims);
if (!cbf_h5type_class_string(
type_class,
h5t_type_class,&atomic,14)) {
cbf_reportnez(cbf_set_value(handle, h5t_type_class),errorcode);
if (!atomic && type_class==H5T_ARRAY){
base_type = H5Tget_super(type);
base_type_class = H5Tget_class(base_type);
if (!cbf_h5type_class_string(
base_type_class,
h5t_base_type_class,&atomic,14)) {
if (!atomic) {
_cbf_strncpy (h5t_base_type_class,".",14);
cbf_h5failneg(H5Tclose(base_type),CBF_FORMAT);
base_type = CBF_H5FAIL;
} else {
type_size = H5Tget_size(base_type);
type_order = H5Tget_order(base_type);
type_sign = H5Tget_sign(base_type);
}
} else {
_cbf_strncpy (h5t_base_type_class,".",14);
cbf_h5failneg(H5Tclose(base_type),CBF_FORMAT);
base_type = CBF_H5FAIL;
}
} else if (atomic) {
type_size = H5Tget_size(native_type);
type_order = H5Tget_order(type);
type_sign = H5Tget_sign(type);
}
}
total_size = type_size;
total_dim = 1;
for (ii=0; ii < kdims; ii ++) {
total_size *= dims[ii];
total_dim *= dims[ii];
}
if (total_size < type_size) total_size = type_size;
if (total_dim < 1 ) total_dim = 1;
cbf_reportnez(cbf_require_column(handle,columnname),errorcode);
if(total_size > 0) {
{
/* process a dataset */
hid_t memspace;
memspace=H5Screate_simple(kdims,dims,NULL);
cbf_reportnez(cbf_alloc(((void **) &data),NULL,
1,total_size+1),errorcode);
cbf_h5failneg(H5Dread(obj_id,native_type,
H5S_ALL,memspace,H5P_DEFAULT,data),
CBF_ARGUMENT);
data[total_size]='\0';
if (type_class==H5T_STRING) {
char element[type_size+1];
size_t ii, jj;;
for (ii=0; ii<total_dim; ii++) {
for(jj=0; jj<type_size; jj++) element[jj]=data[jj+ii*type_size];
element[type_size]=0;
cbf_debug_print(element);
cbf_reportnez(cbf_select_row(handle,target_row+ii),errorcode);
cbf_reportnez(cbf_set_value(handle,(const char *)element),errorcode);
if (!cbf_cistrcmp(element,".") || !cbf_cistrcmp(element,"?") ) {
cbf_reportnez(cbf_set_typeofvalue(handle,"null"),errorcode);
}
if (ii < total_dim-1 && target_row+ii >= rows) {
cbf_reportnez(cbf_new_row(handle),errorcode);
}
}
} else if (type_class==H5T_INTEGER){
/* Read of a single integer or an integer array of
up to 3 dimensions */
char * ivalue = NULL;
long xdata = 0;
unsigned long uxdata = 0;
int sign;
sign = (type_sign==H5T_SGN_2)?1:0;
if (total_dim ==1) {
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,type_size*3+1),errorcode);
if (H5Tequal(native_type, H5T_NATIVE_CHAR)&&sign) xdata = *((signed char *)data);
if (H5Tequal(native_type, H5T_NATIVE_CHAR)&&!sign) uxdata = *((unsigned char *)data);
if (H5Tequal(native_type, H5T_NATIVE_SCHAR)) xdata = *((signed char *)data);
if (H5Tequal(native_type, H5T_NATIVE_UCHAR)) uxdata = *((unsigned char *)data);
if (H5Tequal(native_type, H5T_NATIVE_SHORT)) xdata = *((unsigned short *)data);
if (H5Tequal(native_type, H5T_NATIVE_USHORT)) uxdata = *((unsigned short *)data);
if (H5Tequal(native_type, H5T_NATIVE_INT)) xdata = *((int *)data);
if (H5Tequal(native_type, H5T_NATIVE_UINT)) uxdata = *((unsigned int *)data);
if (H5Tequal(native_type, H5T_NATIVE_LONG)) xdata = *((long *)data);
if (H5Tequal(native_type, H5T_NATIVE_ULONG)) uxdata = *((unsigned long *)data);
if (sign) {
sprintf(ivalue,"%ld",xdata);
} else {
sprintf(ivalue,"%lu",uxdata);
}
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)ivalue;
} else {
/* process arrays as multiple rows in the slowest index
*/
{
size_t indices[H5S_MAX_RANK];
size_t master_index, ival_index;
int idim, level;
char buffer[40];
char open, close;
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,total_dim*((type_size*3)+kdims*2)+1),errorcode);
for (idim = 0; idim < kdims; idim ++) {
indices[idim] = 0;
ivalue[idim] = '[';
}
level = kdims-1;
ivalue[level] = '\0';
ival_index = kdims-1;
master_index = 0;
while (master_index < total_dim) {
for (indices[level]=0; indices[level] < dims[level];) {
if (H5Tequal(native_type, H5T_NATIVE_CHAR)
&&sign) xdata = (((signed char *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_CHAR)
&&!sign) uxdata = (((unsigned char *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_SCHAR)) xdata = (((signed char *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_UCHAR)) uxdata = (((unsigned char *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_SHORT)) xdata = (((unsigned short *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_USHORT)) uxdata = (((unsigned short *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_INT)) xdata = (((int *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_UINT)) uxdata = (((unsigned int *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_LONG)) xdata = (((long *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_ULONG)) uxdata = (((unsigned long *)data)[master_index]);
if (sign) {
sprintf(buffer,"%ld",xdata);
} else {
sprintf(buffer,"%lu",uxdata);
}
strcat(ivalue+ival_index,buffer);
ival_index+=_cbf_strlen(buffer);
ivalue[ival_index++]= (indices[level] < dims[level]-1)?',':']';
master_index++;
indices[level]++;
if (indices[level] == dims[level]) {
/* We are at the end of a fast-dimension row
and therefore need to update higher level indices
if any. */
indices[level] = 0;
level --;
while (level >= 0) {
indices[level]++;
if (indices[level] < dims[level]) {
if (level > 0) {
ivalue[ival_index++] = ',';
ivalue[ival_index++] = '[';
level++;
break;
} else {
cbf_reportnez(cbf_select_row(handle,target_row),errorcode);
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_new_row(handle),errorcode);
target_row++;
ival_index = kdims-1;
}
} else {
if (level > 0) {
ivalue[ival_index++] = ']';
indices[level] = 0;
} else {
cbf_reportnez(cbf_select_row(handle,target_row),errorcode);
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
}
level --;
}
}
if (level < 0) break;
while (level < kdims-1) {
ivalue[ival_index++] = '[';
level++;
}
}
}
}
ivalue[ival_index++] = '\0';
cbf_reportnez(cbf_set_typeofvalue(handle,"bkts"),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)ivalue;
}
}
} else if (type_class==H5T_FLOAT){
/* Read of a single float or double or a float or
double array of up to 3 dimensions */
char * ivalue = NULL;
double dxdata;
float xdata;
if (total_dim ==1) {
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,type_size*2+6),errorcode);
if (H5Tequal(native_type, H5T_NATIVE_FLOAT)) {
xdata = *((float *)data);
snprintf(ivalue,type_size*2+5,"%.7g",(double) xdata);
} else {
dxdata = *((double *)data);
snprintf(ivalue,type_size*2+5,"%.15g",dxdata);
}
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)ivalue;
} else {
/* process arrays
*/
{
size_t indices[H5S_MAX_RANK];
size_t master_index, ival_index;
int idim, level;
char buffer[40];
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,total_dim*((type_size*2)+5+kdims*2)+1),errorcode);
for (idim = 0; idim < kdims; idim ++) {
indices[idim] = 0;
ivalue[idim] = '[';
}
level = kdims-1;
ival_index = kdims-1;
master_index = 0;
while (master_index < total_dim) {
for (indices[level]=0; indices[level] < dims[level];) {
if (H5Tequal(native_type, H5T_NATIVE_FLOAT)) {
xdata = (((float *)data)[master_index]);
snprintf(buffer,type_size*2+5,"%.7g",(double) xdata);
} else {
dxdata = (((double *)data)[master_index]);
snprintf(buffer,type_size*2+5,"%.15g",dxdata);
}
strcat(ivalue+ival_index,buffer);
ival_index+=_cbf_strlen(buffer);
ivalue[ival_index++]= (indices[level] < dims[level]-1)?',':']';
master_index++;
indices[level]++;
if (indices[level] == dims[level]) {
/* We are at the end of a fast-dimension row
and therefore need to update higher level indices
if any. */
indices[level] = 0;
level --;
while (level >= 0) {
indices[level]++;
if (indices[level] < dims[level]) {
if (level > 0) {
ivalue[ival_index++] = ',';
ivalue[ival_index++] = '[';
level++;
break;
} else {
cbf_reportnez(cbf_select_row(handle,target_row),errorcode);
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_new_row(handle),errorcode);
target_row++;
ival_index = kdims-1;
}
} else {
if (level > 0) {
ivalue[ival_index++] = ']';
indices[level] = 0;
} else {
cbf_reportnez(cbf_select_row(handle,target_row),errorcode);
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
}
level --;
}
}
if (level < 0) break;
while (level > kdims-1) {
ivalue[ival_index++] = '[';
level++;
}
}
}
}
ivalue[ival_index++] = '\0';
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_set_typeofvalue(handle,"bkts"),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)ivalue;
}
}
} else if (type_class!= H5T_OPAQUE){
char * hexvalue = NULL;
char hexdigs[16] = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f'};
size_t ii;
cbf_reportnez(cbf_alloc(((void **) &hexvalue),NULL,
1,2*total_size+1),errorcode);
hexvalue[2*total_size+1] = '\0';
for (ii=0; ii< total_size; ii++) {
hexvalue[(total_size-ii)*2-2] =
hexdigs[((int)(((unsigned char *)data))[ii])&0xF];
hexvalue[(total_size-ii)*2-1] =
hexdigs[((int)((((unsigned char *)data))[ii])>>4)&0xF];
}
cbf_reportnez(cbf_set_value(handle,(const char *)(hexvalue)),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)hexvalue;
}
if (value) {
*value=data;
} else {
cbf_reportnez(cbf_free((void **)&data,NULL),errorcode);
}
}
}
if (base_type >=0) H5Tclose(base_type);
if (native_type>=0) H5Tclose(native_type);
H5garbage_collect();
return CBF_SUCCESS;
}
/* Store an HDF5 Dataset in CBF handle, using
category categoryname, ...*/
int cbf_h5ds_store(cbf_handle handle, haddr_t parent,
const char * parent_name,
const int target_row,
const char * categoryname, hid_t obj_id,
hid_t space, hid_t type,
const char * name,
const int readattrib,
void ** value) {
char buffer[25];
int errorcode;
unsigned char* data = NULL;
char h5t_type_class[14], h5t_base_type_class[14];
hid_t base_type;
hid_t native_type;
int atomic;
int ndims = 0, kdims = 0, ii;
unsigned int rows;
hsize_t dims[H5S_MAX_RANK];
hsize_t maxdims[H5S_MAX_RANK];
char * byte_order;
size_t type_size, total_size, total_dim;
H5T_class_t type_class, base_type_class;
H5T_order_t type_order;
H5T_sign_t type_sign = H5T_SGN_ERROR;
errorcode = 0;
cbf_reportnez(cbf_require_category(handle,categoryname),errorcode);
/* Give the name of this dataset as its own id */
cbf_reportnez(cbf_require_column(handle,"id"),errorcode);
rows = 0;
cbf_reportnez(cbf_count_rows(handle,&rows),errorcode);
if (target_row==-1) {
cbf_reportnez(cbf_new_row(handle),errorcode);
} else {
if ((unsigned int)target_row >= rows ) {
for (ii=rows; ii <= target_row; ii++) {
cbf_reportnez(cbf_new_row(handle),errorcode);
}
}
cbf_reportnez(cbf_select_row(handle,target_row),errorcode);
}
cbf_reportnez(cbf_set_value(handle,name),errorcode);
/* Give the parent name and id for this dataset */
cbf_reportnez(cbf_require_column(handle,"parent_name"),errorcode);
cbf_reportnez(cbf_set_value(handle,parent_name),errorcode);
cbf_reportnez(cbf_require_column(handle,"parent_id"),errorcode);
sprintf(buffer,"0x%lx",(unsigned long)parent);
cbf_reportnez(cbf_set_value(handle,buffer),errorcode);
/* get the class, and, if not atomic
try to get the base class for an array
give up otherwise */
type_class = H5Tget_class(type);
native_type = H5Tget_native_type(type,H5T_DIR_ASCEND);
base_type = CBF_H5FAIL;
cbf_reportnez(cbf_require_column(handle,"type"),errorcode);
type_size = 0;
if (value) *value = 0;
type_order = -1;
kdims= ndims = H5Sget_simple_extent_ndims(space);
if (ndims <= 0) ndims = 1;
H5Sget_simple_extent_dims(space,dims,maxdims);
if (!cbf_h5type_class_string(
type_class,
h5t_type_class,&atomic,14)) {
cbf_reportnez(cbf_set_value(handle, h5t_type_class),errorcode);
if (!atomic && type_class==H5T_ARRAY){
base_type = H5Tget_super(type);
base_type_class = H5Tget_class(base_type);
if (!cbf_h5type_class_string(
base_type_class,
h5t_base_type_class,&atomic,14)) {
if (!atomic) {
_cbf_strncpy (h5t_base_type_class,".",14);
cbf_h5failneg(H5Tclose(base_type),CBF_FORMAT);
base_type = CBF_H5FAIL;
} else {
type_size = H5Tget_size(base_type);
type_order = H5Tget_order(base_type);
type_sign = H5Tget_sign(base_type);
cbf_reportnez(cbf_require_column(handle,"base_type"),errorcode);
cbf_reportnez(cbf_set_value(handle,h5t_base_type_class),errorcode);
}
} else {
_cbf_strncpy (h5t_base_type_class,".",14);
cbf_h5failneg(H5Tclose(base_type),CBF_FORMAT);
base_type = CBF_H5FAIL;
}
} else if (atomic) {
type_size = H5Tget_size(native_type);
type_order = H5Tget_order(type);
type_sign = H5Tget_sign(type);
}
}
total_size = type_size;
total_dim = 1;
for (ii=0; ii < kdims; ii ++) {
total_size *= dims[ii];
total_dim *= dims[ii];
}
if (total_size < type_size) total_size = type_size;
if (total_dim < 1 ) total_dim = 1;
cbf_reportnez(cbf_require_column(handle,"value"),errorcode);
if(total_size > 0) {
if(readattrib) {
/* Process an attribute */
cbf_reportnez(cbf_alloc(((void **) value),NULL,
1,total_size+1),errorcode);
cbf_h5failneg(H5Aread(obj_id,native_type,(void *)*value),
CBF_ARGUMENT);
(*((char **)value))[total_size]='\0';
if (type_class==H5T_STRING) {
cbf_reportnez(cbf_set_value(handle,(const char *)(*value)),errorcode);
} else if (type_class==H5T_INTEGER){
/* Read of a single integer or an integer array of
up to 3 dimensions */
char * ivalue = NULL;
long xdata = 0;
unsigned long uxdata = 0;
int sign;
sign = (type_sign==H5T_SGN_2)?1:0;
if (total_dim ==1) {
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,type_size*3+1),errorcode);
if (H5Tequal(native_type,H5T_NATIVE_CHAR)&&sign) xdata = **((signed char **)value);
if (H5Tequal(native_type,H5T_NATIVE_CHAR)&&!sign) uxdata = **((unsigned char **)value);
if (H5Tequal(native_type,H5T_NATIVE_SCHAR)) xdata = **((signed char **)value);
if (H5Tequal(native_type,H5T_NATIVE_UCHAR)) uxdata = **((unsigned char **)value);
if (H5Tequal(native_type,H5T_NATIVE_SHORT)) xdata = **((unsigned short **)value);
if (H5Tequal(native_type,H5T_NATIVE_USHORT)) uxdata = **((unsigned short **)value);
if (H5Tequal(native_type,H5T_NATIVE_INT)) xdata = **((int **)value);
if (H5Tequal(native_type,H5T_NATIVE_UINT)) uxdata = **((unsigned int **)value);
if (H5Tequal(native_type,H5T_NATIVE_LONG)) xdata = **((long **)value);
if (H5Tequal(native_type,H5T_NATIVE_ULONG)) uxdata = **((unsigned long **)value);
if (sign) {
sprintf(ivalue,"%ld",xdata);
} else {
sprintf(ivalue,"%lu",uxdata);
}
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_free((void**)value,NULL),errorcode);
*value = (unsigned char *)ivalue;
} else {
/* process arrays of up to 100 as
bracketed strings
*/
if (total_dim < 101) {
size_t indices[H5S_MAX_RANK];
size_t master_index, ival_index;
int idim, level;
char buffer[40];
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,total_dim*((type_size*3)+kdims*2)+1),errorcode);
for (idim = 0; idim < kdims; idim ++) {
indices[idim] = 0;
ivalue[idim] = '[';
}
level = kdims-1;
ival_index = kdims;
master_index = 0;
while (master_index < total_dim) {
for (indices[level]=0; indices[level] < dims[level];) {
if (H5Tequal(native_type,H5T_NATIVE_CHAR)&&sign) xdata = ((*(signed char **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_CHAR)&&!sign) uxdata = ((*(unsigned char **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_SCHAR)) xdata = ((*(signed char **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_UCHAR)) uxdata = ((*(unsigned char **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_SHORT)) xdata = ((*(unsigned short **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_USHORT)) uxdata = ((*(unsigned short **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_INT)) xdata = ((*(int **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_UINT)) uxdata = ((*(unsigned int **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_LONG)) xdata = ((*(long **)value)[master_index]);
if (H5Tequal(native_type,H5T_NATIVE_ULONG)) uxdata = ((*(unsigned long **)value)[master_index]);
if (sign) {
sprintf(buffer,"%ld",xdata);
} else {
sprintf(buffer,"%lu",uxdata);
}
strcat(ivalue+ival_index,buffer);
ival_index+=_cbf_strlen(buffer);
ivalue[ival_index++]= (indices[level] < dims[level]-1)?',':']';
master_index++;
indices[level]++;
if (indices[level] == dims[level]) {
/* We are at the end of a fast-dimension row
and therefore need to update higher level indices
if any. */
indices[level] = 0;
level --;
while (level >= 0) {
indices[level]++;
if (indices[level] < dims[level]) {
ivalue[ival_index++] = ',';
ivalue[ival_index++] = '[';
level++;
break;
} else {
ivalue[ival_index++] = ']';
indices[level] = 0;
level --;
}
}
if (level < 0) break;
while (level > kdims-1) {
ivalue[ival_index++] = '[';
level++;
}
}
}
}
ivalue[ival_index++] = '\0';
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_set_typeofvalue(handle,"bkts"),errorcode);
cbf_reportnez(cbf_free((void**)value,NULL),errorcode);
*value = (unsigned char *)ivalue;
} else {
size_t dimfast, dimmid, dimslow;
dimmid = dimslow = 1;
dimfast = dims[kdims-1];
if (kdims > 1) dimmid = dims[kdims-2];
if (kdims > 2) dimslow = total_dim/(dimfast*dimmid);
cbf_reportnez(cbf_set_integerarray_wdims_fs(handle,
CBF_NIBBLE_OFFSET,target_row,*value,
type_size,sign,total_dim,"little_endian",
dimfast,dimmid,dimslow,0),errorcode);
}
}
} else if (type_class==H5T_FLOAT){
/* Read of a single float or double or a float or
double array of up to 3 dimensions */
char * ivalue = NULL;
double dxdata;
float xdata;
if (total_dim ==1) {
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,type_size*2+6),errorcode);
if (H5Tequal(native_type,H5T_NATIVE_FLOAT)) {
xdata = **((float **)value);
snprintf(ivalue,type_size*2+5,"%.7g",(double) xdata);
} else {
dxdata = **((double **)value);
snprintf(ivalue,type_size*2+5,"%.15g",dxdata);
}
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_free((void**)value,NULL),errorcode);
*value = (unsigned char *)ivalue;
} else {
/* process arrays of up to 100 as
bracketed strings
*/
if (total_dim < 101) {
size_t indices[H5S_MAX_RANK];
size_t master_index, ival_index;
int idim, level;
char buffer[40];
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,total_dim*((type_size*2)+5+kdims*2)+1),errorcode);
for (idim = 0; idim < kdims; idim ++) {
indices[idim] = 0;
ivalue[idim] = '[';
}
level = kdims-1;
ival_index = kdims;
master_index = 0;
while (master_index < total_dim) {
for (indices[level]=0; indices[level] < dims[level];) {
if (H5Tequal(native_type,H5T_NATIVE_FLOAT)) {
xdata = ((*(float **)value)[master_index]);
snprintf(buffer,type_size*2+5,"%.7g",(double) xdata);
} else {
dxdata = ((*(double **)value)[master_index]);
snprintf(buffer,type_size*2+5,"%.15g",dxdata);
}
strcat(ivalue+ival_index,buffer);
ival_index+=_cbf_strlen(buffer);
ivalue[ival_index++]= (indices[level] < dims[level]-1)?',':']';
master_index++;
indices[level]++;
if (indices[level] == dims[level]) {
/* We are at the end of a fast-dimension row
and therefore need to update higher level indices
if any. */
indices[level] = 0;
level --;
while (level >= 0) {
indices[level]++;
if (indices[level] < dims[level]) {
ivalue[ival_index++] = ',';
ivalue[ival_index++] = '[';
level++;
break;
} else {
ivalue[ival_index++] = ']';
indices[level] = 0;
level --;
}
}
if (level < 0) break;
while (level > kdims-1) {
ivalue[ival_index++] = '[';
level++;
}
}
}
}
ivalue[ival_index++] = '\0';
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_set_typeofvalue(handle,"bkts"),errorcode);
cbf_reportnez(cbf_free((void**)value,NULL),errorcode);
*value = (unsigned char *)ivalue;
} else {
size_t dimfast, dimmid, dimslow;
dimmid = dimslow = 1;
dimfast = dims[kdims-1];
if (kdims > 1) dimmid = dims[kdims-2];
if (kdims > 2) dimslow = total_dim/(dimfast*dimmid);
cbf_reportnez(cbf_set_realarray_wdims_fs(handle,
CBF_NIBBLE_OFFSET,target_row,*value,
type_size,total_dim,"little_endian",
dimfast,dimmid,dimslow,0),errorcode);
}
}
} else if (type_class != H5T_OPAQUE) {
unsigned char * hexvalue = NULL;
unsigned char hexdigs[16] = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f'};
size_t ii;
cbf_reportnez(cbf_alloc(((void **) &hexvalue),NULL,
1,2*total_size+1),errorcode);
hexvalue[2*total_size+1] = '\0';
for (ii=0; ii< total_size; ii++) {
hexvalue[(total_size-ii)*2-2] =
hexdigs[((int)(*((unsigned char **)value))[ii])&0xF];
hexvalue[(total_size-ii)*2-1] =
hexdigs[((int)((*((unsigned char **)value))[ii])>>4)&0xF];
}
cbf_reportnez(cbf_set_value(handle,(const char *)(hexvalue)),errorcode);
cbf_reportnez(cbf_free((void**)value,NULL),errorcode);
*value = hexvalue;
}
} else {
/* process a dataset */
hid_t memspace;
memspace=H5Screate_simple(kdims,dims,NULL);
cbf_reportnez(cbf_alloc(((void **) &data),NULL,
1,total_size+1),errorcode);
cbf_h5failneg(H5Dread(obj_id,native_type,
H5S_ALL,memspace,H5P_DEFAULT,data),
CBF_ARGUMENT);
data[total_size]='\0';
if (type_class==H5T_STRING) {
cbf_reportnez(cbf_set_value(handle,(const char *)data),errorcode)
} else if (type_class==H5T_INTEGER){
/* Read of a single integer or an integer array of
up to 3 dimensions */
char * ivalue = NULL;
long xdata = 0;
unsigned long uxdata = 0;
int sign;
sign = (type_sign==H5T_SGN_2)?1:0;
if (total_dim ==1) {
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,type_size*3+1),errorcode);
if (H5Tequal(native_type, H5T_NATIVE_CHAR)&&sign) xdata = *((signed char *)data);
if (H5Tequal(native_type, H5T_NATIVE_CHAR)&&!sign) uxdata = *((unsigned char *)data);
if (H5Tequal(native_type, H5T_NATIVE_SCHAR)) xdata = *((signed char *)data);
if (H5Tequal(native_type, H5T_NATIVE_UCHAR)) uxdata = *((unsigned char *)data);
if (H5Tequal(native_type, H5T_NATIVE_SHORT)) xdata = *((unsigned short *)data);
if (H5Tequal(native_type, H5T_NATIVE_USHORT)) uxdata = *((unsigned short *)data);
if (H5Tequal(native_type, H5T_NATIVE_INT)) xdata = *((int *)data);
if (H5Tequal(native_type, H5T_NATIVE_UINT)) uxdata = *((unsigned int *)data);
if (H5Tequal(native_type, H5T_NATIVE_LONG)) xdata = *((long *)data);
if (H5Tequal(native_type, H5T_NATIVE_ULONG)) uxdata = *((unsigned long *)data);
if (sign) {
sprintf(ivalue,"%ld",xdata);
} else {
sprintf(ivalue,"%lu",uxdata);
}
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)ivalue;
} else {
/* process arrays of up to 100 as
bracketed strings
*/
if (total_dim < 101) {
size_t indices[H5S_MAX_RANK];
size_t master_index, ival_index;
int idim, level;
char buffer[40];
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,total_dim*((type_size*3)+kdims*2)+1),errorcode);
for (idim = 0; idim < kdims; idim ++) {
indices[idim] = 0;
ivalue[idim] = '[';
}
level = kdims-1;
ival_index = kdims;
master_index = 0;
while (master_index < total_dim) {
for (indices[level]=0; indices[level] < dims[level];) {
if (H5Tequal(native_type, H5T_NATIVE_CHAR)
&&sign) xdata = (((signed char *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_CHAR)
&&!sign) uxdata = (((unsigned char *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_SCHAR)) xdata = (((signed char *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_UCHAR)) uxdata = (((unsigned char *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_SHORT)) xdata = (((unsigned short *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_USHORT)) uxdata = (((unsigned short *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_INT)) xdata = (((int *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_UINT)) uxdata = (((unsigned int *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_LONG)) xdata = (((long *)data)[master_index]);
if (H5Tequal(native_type, H5T_NATIVE_ULONG)) uxdata = (((unsigned long *)data)[master_index]);
if (sign) {
sprintf(buffer,"%ld",xdata);
} else {
sprintf(buffer,"%lu",uxdata);
}
strcat(ivalue+ival_index,buffer);
ival_index+=_cbf_strlen(buffer);
ivalue[ival_index++]= (indices[level] < dims[level]-1)?',':']';
master_index++;
indices[level]++;
if (indices[level] == dims[level]) {
/* We are at the end of a fast-dimension row
and therefore need to update higher level indices
if any. */
indices[level] = 0;
level --;
while (level >= 0) {
indices[level]++;
if (indices[level] < dims[level]) {
ivalue[ival_index++] = ',';
ivalue[ival_index++] = '[';
level++;
break;
} else {
ivalue[ival_index++] = ']';
indices[level] = 0;
level --;
}
}
if (level < 0) break;
while (level > kdims-1) {
ivalue[ival_index++] = '[';
level++;
}
}
}
}
ivalue[ival_index++] = '\0';
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_set_typeofvalue(handle,"bkts"),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)ivalue;
} else {
size_t dimfast, dimmid, dimslow;
dimmid = dimslow = 1;
dimfast = dims[kdims-1];
if (kdims > 1) dimmid = dims[kdims-2];
if (kdims > 2) dimslow = total_dim/(dimfast*dimmid);
cbf_reportnez(cbf_set_integerarray_wdims_fs(handle,
CBF_NIBBLE_OFFSET,target_row,data,
type_size,sign,total_dim,"little_endian",
dimfast,dimmid,dimslow,0),errorcode);
}
}
} else if (type_class==H5T_FLOAT){
/* Read of a single float or double or a float or
double array of up to 3 dimensions */
char * ivalue = NULL;
double dxdata;
float xdata;
if (total_dim ==1) {
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,type_size*2+6),errorcode);
if (H5Tequal(native_type, H5T_NATIVE_FLOAT)) {
xdata = *((float *)data);
snprintf(ivalue,type_size*2+5,"%.7g",(double) xdata);
} else {
dxdata = *((double *)data);
snprintf(ivalue,type_size*2+5,"%.15g",dxdata);
}
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)ivalue;
} else {
/* process arrays of up to 100 as
bracketed strings
*/
if (total_dim < 101) {
size_t indices[H5S_MAX_RANK];
size_t master_index, ival_index;
int idim, level;
char buffer[40];
cbf_reportnez(cbf_alloc(((void **) &ivalue),NULL,
1,total_dim*((type_size*2)+5+kdims*2)+1),errorcode);
for (idim = 0; idim < kdims; idim ++) {
indices[idim] = 0;
ivalue[idim] = '[';
}
level = kdims-1;
ival_index = kdims;
master_index = 0;
while (master_index < total_dim) {
for (indices[level]=0; indices[level] < dims[level];) {
if (H5Tequal(native_type, H5T_NATIVE_FLOAT)) {
xdata = (((float *)data)[master_index]);
snprintf(buffer,type_size*2+5,"%.7g",(double) xdata);
} else {
dxdata = (((double *)data)[master_index]);
snprintf(buffer,type_size*2+5,"%.15g",dxdata);
}
strcat(ivalue+ival_index,buffer);
ival_index+=_cbf_strlen(buffer);
ivalue[ival_index++]= (indices[level] < dims[level]-1)?',':']';
master_index++;
indices[level]++;
if (indices[level] == dims[level]) {
/* We are at the end of a fast-dimension row
and therefore need to update higher level indices
if any. */
indices[level] = 0;
level --;
while (level >= 0) {
indices[level]++;
if (indices[level] < dims[level]) {
ivalue[ival_index++] = ',';
ivalue[ival_index++] = '[';
level++;
break;
} else {
ivalue[ival_index++] = ']';
indices[level] = 0;
level --;
}
}
if (level < 0) break;
while (level > kdims-1) {
ivalue[ival_index++] = '[';
level++;
}
}
}
}
ivalue[ival_index++] = '\0';
cbf_reportnez(cbf_set_value(handle,(const char *)(ivalue)),errorcode);
cbf_reportnez(cbf_set_typeofvalue(handle,"bkts"),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)ivalue;
} else {
size_t dimfast, dimmid, dimslow;
dimmid = dimslow = 1;
dimfast = dims[kdims-1];
if (kdims > 1) dimmid = dims[kdims-2];
if (kdims > 2) dimslow = total_dim/(dimfast*dimmid);
cbf_reportnez(cbf_set_realarray_wdims_fs(handle,
CBF_NIBBLE_OFFSET,target_row,data,
type_size,total_dim,"little_endian",
dimfast,dimmid,dimslow,0),errorcode);
}
}
} else if (type_class!= H5T_OPAQUE){
char * hexvalue = NULL;
char hexdigs[16] = {'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f'};
size_t ii;
cbf_reportnez(cbf_alloc(((void **) &hexvalue),NULL,
1,2*total_size+1),errorcode);
hexvalue[2*total_size+1] = '\0';
for (ii=0; ii< total_size; ii++) {
hexvalue[(total_size-ii)*2-2] =
hexdigs[((int)(((unsigned char *)data))[ii])&0xF];
hexvalue[(total_size-ii)*2-1] =
hexdigs[((int)((((unsigned char *)data))[ii])>>4)&0xF];
}
cbf_reportnez(cbf_set_value(handle,(const char *)(hexvalue)),errorcode);
cbf_reportnez(cbf_free((void**)&data,NULL),errorcode);
data = (unsigned char *)hexvalue;
}
if (value) {
*value=data;
} else {
cbf_reportnez(cbf_free((void **)&data,NULL),errorcode);
}
}
}
switch(type_order) {
case H5T_ORDER_LE:
byte_order = "H5T_ORDER_LE";
break;
case H5T_ORDER_BE:
byte_order = "H5T_ORDER_BE";
break;
case H5T_ORDER_VAX:
byte_order = "H5T_ORDER_VAX";
break;
case H5T_ORDER_MIXED:
byte_order = "H5T_ORDER_MIXED";
break;
case H5T_ORDER_NONE:
byte_order = "H5T_ORDER_LE";
break;
case -1:
byte_order = ".";
break;
default: byte_order="UNKNOWN";
break;
}
cbf_reportnez(cbf_require_column(handle,"h5_byte_order"),errorcode);
cbf_reportnez(cbf_set_value(handle,byte_order),errorcode);
if (type_size < 10) {
sprintf(buffer,"%ld",(unsigned long)type_size);
} else {
sprintf(buffer,"0x%lx",(unsigned long)type_size);
}
cbf_reportnez(cbf_require_column(handle,"size"),errorcode);
cbf_reportnez(cbf_set_value(handle,buffer),errorcode);
if (base_type >=0) H5Tclose(base_type);
if (native_type>=0) H5Tclose(native_type);
H5garbage_collect();
return CBF_SUCCESS;
}
/* Callback routine for objects in a group */
herr_t cbf_object_visit(hid_t loc_id, const char *name,
const H5L_info_t *info,
void *op_data){
int cbfrow = -1;
int errorcode;
cbf_handle handle;
haddr_t save_parent_addr,parent_addr;
hid_t save_parent_id,parent_id;
unsigned int row;
const char* save_parent_name;
const char* save_grand_parent_name;
const char* save_great_grand_parent_name;
const char* parent_name;
const char* grand_parent_name;
const char* great_grand_parent_name;
int save_innexus, innexus, atnexus; /* within or at NXentry */
int save_incbf, incbf, atcbf; /* within or at NXpdb or CBF_cif, etc. */
int save_incbfdb, incbfdb, atcbfdb; /* within or at CBF_cbfdb (cif datablock)*/
int save_incbfcat, incbfcat, atcbfcat; /* within or at CBF_cbfcat */
int save_incbfcol, incbfcol, atcbfcol; /* within or at CBF_cbfcol */
int save_incbfsf, incbfsf, atcbfsf; /* withing or at CBF_cbfsf */
int save_innxpdb, innxpdb; /* tree depth in NXpdb or CBF_cif */
size_t save_path_size;
hid_t group_id, dataset_id;
herr_t retval;
hsize_t i;
char buffer[25];
char digest[25];
char *value;
const char *value_nx_class;
const char *value_nxpdb_class;
char cbftype[5];
cbf_bookmark bookmark;
cbf_bookmark saved_bookmark;
H5O_info_t objinfo;
hid_t attrib_id,attrib_ds,attrib_type;
hid_t dataset_ds, dataset_type;
H5T_class_t dataset_type_class;
ssize_t attrib_name_size;
int attrib_num;
unsigned int compression;
int binary_id, bits, sign, type, checked_digest, realarray = 0;
const char *byteorder;
size_t binsize;
size_t dimover, dimfast, dimmid, dimslow;
size_t padding;
CBF_UNUSED( info );
errorcode = 0;
handle = ((cbf_h5Ovisithandle)op_data)->handle;
if (!handle) return -1;
/* skip the root group itself */
if (name[0]== '.') return 0;
cbf_h5failneg(H5Oget_info_by_name(loc_id,
name, &objinfo, H5P_DEFAULT),CBF_FORMAT);
save_path_size = ((cbf_h5Ovisithandle)op_data)->path_size;
save_parent_id=parent_id = ((cbf_h5Ovisithandle)op_data)->parent_id;
save_parent_addr=parent_addr = ((cbf_h5Ovisithandle)op_data)->parent_addr;
save_parent_name=parent_name = ((cbf_h5Ovisithandle)op_data)->parent_name;
save_grand_parent_name=grand_parent_name = ((cbf_h5Ovisithandle)op_data)->grand_parent_name;
save_great_grand_parent_name=great_grand_parent_name = ((cbf_h5Ovisithandle)op_data)->great_grand_parent_name;
save_innexus=innexus = ((cbf_h5Ovisithandle)op_data)->innexus;
save_incbf=incbf = ((cbf_h5Ovisithandle)op_data)->incbf;
save_incbfdb=incbfdb = ((cbf_h5Ovisithandle)op_data)->incbfdb;
save_incbfsf=incbfsf = ((cbf_h5Ovisithandle)op_data)->incbfsf;
save_incbfcat=incbfcat = ((cbf_h5Ovisithandle)op_data)->incbfcat;
save_incbfcol=incbfcol = ((cbf_h5Ovisithandle)op_data)->incbfcol;
save_innxpdb=innxpdb = ((cbf_h5Ovisithandle)op_data)->innxpdb;
atcbf = atcbfdb = atcbfsf = atcbfcat = atcbfcol = 0;
if (incbfcol) {
atcbfcol = 1;
} else if (incbfcat) {
atcbfcat = 1;
} else if (incbfsf) {
atcbfsf = 1;
} else if (incbfdb) {
atcbfdb = 1;
} else if (incbf) {
atcbf = 1;
}
memmove(&saved_bookmark,&(((cbf_h5Ovisithandle)op_data)->bookmark),sizeof(cbf_bookmark));
cbf_debug_print("Entering cbf_object_visit");
cbf_debug_print3("incbfcol: %d, atcbfcol: %d", incbfcol, atcbfcol);
cbf_debug_print3("incbfcat: %d, atcbfcat: %d", incbfcat, atcbfcat);
cbf_debug_print3("incbfsf: %d, atcbfsf: %d", incbfsf, atcbfsf);
cbf_debug_print3("incbfdb: %d, atcbfdb: %d", incbfdb, atcbfdb);
cbf_debug_print3("incbf: %d, atcbf: %d", incbf, atcbf);
cbf_debug_print2("name: %s", name);
cbf_debug_print2("parent: %s", parent_name);
cbf_debug_print2("grantparent: %s", grand_parent_name);
switch (objinfo.type) {
case H5O_TYPE_GROUP:
/* Skip duplicates */
for (i=0; i < ((cbf_h5Ovisithandle)op_data)->path_size; i++) {
if (objinfo.addr ==
((cbf_h5Ovisithandle)op_data)->haddr_path[i])
return 0;
}
if (((cbf_h5Ovisithandle)op_data)->path_size >=
((cbf_h5Ovisithandle)op_data)->capacity) {
size_t newcap;
newcap = 2*((cbf_h5Ovisithandle)op_data)->capacity;
cbf_reportnez(
cbf_realloc(
(void **)(&((cbf_h5Ovisithandle)op_data)->hid_path),
NULL,sizeof(hid_t),newcap),errorcode);
cbf_reportnez(
cbf_realloc(
(void **)(&((cbf_h5Ovisithandle)op_data)->haddr_path),
NULL,sizeof(haddr_t),newcap),errorcode);
((cbf_h5Ovisithandle)op_data)->capacity=newcap;
}
(((cbf_h5Ovisithandle)op_data)->
haddr_path)[((cbf_h5Ovisithandle)op_data)->path_size] =
objinfo.addr;
group_id = H5Gopenx(loc_id,name);
(((cbf_h5Ovisithandle)op_data)->
haddr_path)[((cbf_h5Ovisithandle)op_data)->path_size] =
group_id;
(((cbf_h5Ovisithandle)op_data)->path_size)++;
/* We have a group
We need to add it to the H5_Groups category
in the H5 data block.
If it has attributes, we need to add them to
the H5Attributes category
If it has datasets, we will catch them when we
iterate again
*/
cbf_reportnez(cbf_rewind_datablock(handle),errorcode);
if (cbf_find_datablock(handle,"H5")) {
cbf_reportnez(cbf_new_datablock(handle,"H5"),errorcode);
}
cbf_reportnez(cbf_require_category(handle,"H5_Groups"),errorcode);
cbf_reportnez(cbf_new_row(handle),errorcode);
cbf_reportnez(cbf_row_number(handle,&row),errorcode);
cbf_reportnez(cbf_require_column(handle,"name"),errorcode);
cbf_reportnez(cbf_set_value(handle,name),errorcode);
cbf_reportnez(cbf_require_column(handle,"parent_name"),errorcode);
cbf_reportnez(cbf_set_value(handle,parent_name),errorcode);
cbf_reportnez(cbf_require_column(handle,"parent_id"),errorcode);
if (!parent_addr) {
cbf_reportnez(cbf_set_value(handle,"."),errorcode);
cbf_reportnez(cbf_set_typeofvalue(handle,"null"),errorcode);
} else {
sprintf(buffer,"0x%lx",(unsigned long)parent_addr);
cbf_reportnez(cbf_set_value(handle,buffer),errorcode);
}
cbf_reportnez(cbf_require_column(handle,"id"),errorcode);
sprintf(buffer,"0x%lx",(unsigned long)objinfo.addr);
cbf_reportnez(cbf_set_value(handle,buffer),errorcode);
attrib_num = objinfo.num_attrs;
cbf_reportnez(cbf_require_column(handle,"no_attributes"),errorcode);
cbf_reportnez(cbf_set_integervalue(handle,attrib_num),errorcode);
{ int errorcode = 0;
value_nx_class=NULL;
cbf_reportnez(_cbf_Attrval(group_id,"NX_class", &value_nx_class),errorcode);
cbf_debug_print2("NX_class %s\n",value_nx_class);
value_nxpdb_class=NULL;
cbf_reportnez(_cbf_Attrval(group_id,"NXpdb_class", &value_nxpdb_class),errorcode);
cbf_debug_print2("NXpdb_class %s\n",value_nxpdb_class);
if (value_nx_class && !(value_nxpdb_class) ) {
value_nxpdb_class=NULL;
cbf_reportnez(_cbf_Attrval(group_id,"NX_class", &value_nxpdb_class),errorcode);
cbf_debug_print2("NXpdb_class %s\n",value_nxpdb_class);
}
}
for (i=0; (ssize_t)i < attrib_num; i++) {
char * attrib_name = NULL;
attrib_id=H5Aopen_by_idx(group_id,".",
H5_INDEX_NAME,
H5_ITER_INC,
i,H5P_DEFAULT,H5P_DEFAULT);
attrib_ds = H5Aget_space(attrib_id);
attrib_type = H5Aget_type(attrib_id);
attrib_name_size = H5Aget_name(attrib_id,0,NULL);
attrib_name = NULL;
cbf_reportnez(cbf_alloc(((void **) &attrib_name),NULL,
1,attrib_name_size+1),errorcode);
cbf_h5failneg(H5Aget_name(attrib_id,
attrib_name_size+1,attrib_name),
CBF_ARGUMENT);
cbf_h5ds_store(handle,objinfo.addr,
name,-1,
"H5_Group_attribute",
attrib_id,
attrib_ds,
attrib_type,
attrib_name,1, (void **)&value);
if (!cbf_cistrcmp(attrib_name,"NX_class")&& value) {
cbf_reportnez(cbf_rewind_datablock(handle),errorcode);
if (cbf_find_datablock(handle,"H5")) {
cbf_reportnez(cbf_new_datablock(handle,"H5"),errorcode);
}
cbf_reportnez(cbf_require_category(handle,"H5_Groups"),errorcode);
cbf_reportnez(cbf_find_column(handle,"id"),errorcode);
cbf_reportnez(cbf_select_row(handle,row),errorcode);
cbf_reportnez(cbf_require_column(handle,"NX_class"),errorcode);
cbf_reportnez(cbf_set_value(handle,value),errorcode);
cbf_debug_print(value);
if (!cbf_cistrcmp(value,"NXentry")) {
((cbf_h5Ovisithandle)op_data)->innexus = innexus =1;
}
if (!cbf_cistrcmp(value,"CBF_cbf")
||!cbf_cistrcmp(value,"NXcbf")
||!cbf_cistrcmp(value,"NXcif")
||!cbf_cistrcmp(value,"NXpdb")) {
((cbf_h5Ovisithandle)op_data)->incbf = incbf = 1;
if (incbfdb) {atcbf = 1; incbfdb = 0;}
}
if (incbf
|| !cbf_cistrcmp(value,"CBF_cbfdb")
|| !cbf_cistrcmp(value,"CBF_cbfsf")
|| !cbf_cistrcmp(value,"CBF_cbfcat")
|| !cbf_cistrcmp(value,"CBF_cbfcol")
|| (value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfdb"))
|| (value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfsf"))
|| (value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfcat"))
|| (value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfcol"))) {
/* The top level (1) NXpdb group may contain one or more
NXpdb groups, each of which is a data block.
Each data block is a level 2 NXpdb group which may contain
one of more categories or savefames. Each level 3 category is
a NXpdb group which may contain one or more
columns. In that case, each column is a level 4 NXpdb group.
Each level 3 saveframe is a NXpdb group with an NXpdb_class
of CBF_cbfsf which may contain level 4 categories.
*/
innxpdb = ((cbf_h5Ovisithandle)op_data)->innxpdb = innxpdb+1;
cbf_debug_print2("innxpdb: %d", innxpdb);
incbf = ((cbf_h5Ovisithandle)op_data)->incbf = 1;
if(( value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfdb"))
|| innxpdb == 2) {
atcbfdb =incbfdb = ((cbf_h5Ovisithandle)op_data)->incbfdb = 1;
innxpdb = ((cbf_h5Ovisithandle)op_data)->innxpdb = 2;
incbfsf = ((cbf_h5Ovisithandle)op_data)->incbfsf = 0;
incbfcat = ((cbf_h5Ovisithandle)op_data)->incbfcat = 0;
atcbf = atcbfsf = atcbfcat = atcbfcol = 0;
cbf_debug_print2("datablock %s",name);
} else if(( value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfsf"))
) {
atcbfsf = incbfsf = ((cbf_h5Ovisithandle)op_data)->incbfsf = 1;
innxpdb = ((cbf_h5Ovisithandle)op_data)->innxpdb = 3;
atcbf = atcbfdb = atcbfcat = atcbfcol = 0;
cbf_debug_print2("saveframe %s",name);
} else if(( value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfcat"))
|| innxpdb == 3+incbfsf) {
atcbfcat = incbfcat = ((cbf_h5Ovisithandle)op_data)->incbfcat = 1;
atcbf = atcbfdb = atcbfsf = atcbfcol = 0;
cbf_debug_print2("category %s",name);
} else if(( value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfcol"))
|| innxpdb == 4+incbfsf) {
atcbfcol = incbfcol = ((cbf_h5Ovisithandle)op_data)->incbfcol = 1;
atcbf = atcbfdb = atcbfsf = atcbfcat = 0;
cbf_debug_print2("column %s",name);
}
}
if (innxpdb == 2) {
atcbfdb = incbfdb = ((cbf_h5Ovisithandle)op_data)->incbfdb = 1;
atcbf = atcbfsf = atcbfcat = atcbfcol = 0;
if (value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfdb")) {
cbf_debug_print2("CBF_cbfdb agrees with tree depth %s", name);
} else {
incbfdb = ((cbf_h5Ovisithandle)op_data)->incbfdb = 1;
cbf_debug_print3("CBF_cbfdb attribute disagrees with tree depth %d %s", innxpdb, name);
}
}
if (innxpdb == 3) {
if (value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfsf")) {
incbfsf = ((cbf_h5Ovisithandle)op_data)->incbfsf = 1;
} else {
incbfsf = ((cbf_h5Ovisithandle)op_data)->incbfsf = 0;
}
}
if (innxpdb == 3+incbfsf) {
atcbfcat = incbfcat = ((cbf_h5Ovisithandle)op_data)->incbfcat = 1;
atcbf = atcbfdb = atcbfsf = atcbfcol = 0;
if (value_nxpdb_class && !cbf_cistrcmp(value_nxpdb_class,"CBF_cbfcat")) {
cbf_debug_print2("CBF_cbfcat agrees with tree depth %s", name)
} else {
cbf_debug_print3("CBF_cbfcat attribute disagrees with tree depth %d %s", innxpdb, name)
}
}
if (atcbfdb) {
cbf_get_bookmark(handle,&bookmark);
if (cbf_find_datablock(handle,name)) {
cbf_reportnez(cbf_new_datablock(handle,name),errorcode);
}
cbf_debug_print2("set datablock: %s", name);
cbf_get_bookmark(handle,
&(((cbf_h5Ovisithandle)op_data)->bookmark));
cbf_goto_bookmark(handle,bookmark);
}
if (atcbfsf) {
cbf_get_bookmark(handle,&bookmark);
cbf_goto_bookmark(handle,saved_bookmark);
if (cbf_find_saveframe(handle,name)) {
cbf_reportnez(cbf_new_saveframe(handle,name),errorcode);
}
cbf_debug_print2("set saveframe: %s", name);
cbf_get_bookmark(handle,
&(((cbf_h5Ovisithandle)op_data)->bookmark));
cbf_goto_bookmark(handle,bookmark);
}
if (atcbfcat) {
cbf_get_bookmark(handle,&bookmark);
cbf_goto_bookmark(handle,saved_bookmark);
if (cbf_find_category(handle,name)) {
cbf_reportnez(cbf_new_category(handle,name),errorcode);
}
cbf_debug_print2("set category: %s", name);
cbf_get_bookmark(handle,
&(((cbf_h5Ovisithandle)op_data)->bookmark));
cbf_goto_bookmark(handle,bookmark);
}
if (atcbfcol) {
cbf_get_bookmark(handle,&bookmark);
cbf_goto_bookmark(handle,saved_bookmark);
if (cbf_find_column(handle,name)) {
cbf_reportnez(cbf_new_column(handle,name),errorcode);
}
cbf_debug_print2("set column: %s", name);
cbf_get_bookmark(handle,
&(((cbf_h5Ovisithandle)op_data)->bookmark));
cbf_goto_bookmark(handle,bookmark);
}
}
attrib_name = NULL;
cbf_reportnez(cbf_free((void **)&attrib_name,NULL),errorcode);
if (value) {
cbf_reportnez(cbf_free((void **)&value, NULL),errorcode);
}
if (value_nx_class) {
cbf_reportnez(cbf_free((void **)&value_nx_class, NULL),errorcode);
}
if (value_nxpdb_class) {
cbf_reportnez(cbf_free((void **)&value_nxpdb_class, NULL),errorcode);
}
H5Tclose(attrib_type);
H5Sclose(attrib_ds);
H5Aclose(attrib_id);
}
((cbf_h5Ovisithandle)op_data)->parent_addr = objinfo.addr;
((cbf_h5Ovisithandle)op_data)->parent_id = group_id;
((cbf_h5Ovisithandle)op_data)->great_grand_parent_name = grand_parent_name;
((cbf_h5Ovisithandle)op_data)->grand_parent_name = parent_name;
cbf_reportnez(cbf_alloc((void **) &(((cbf_h5Ovisithandle)op_data)->parent_name),NULL,
1,_cbf_strlen(name)+1),errorcode);
if (!name) return -1;
_cbf_strcpy((char *)((cbf_h5Ovisithandle)op_data)->parent_name,name);
retval = H5Literate_by_name(loc_id, name,H5_INDEX_NAME,
H5_ITER_INC,
NULL,
cbf_object_visit,op_data,H5P_DEFAULT);
H5Gclose(group_id);
cbf_reportnez(cbf_free((void **)(&((cbf_h5Ovisithandle)op_data)->parent_name),NULL),errorcode);
(((cbf_h5Ovisithandle)op_data)->path_size)=save_path_size;;
((cbf_h5Ovisithandle)op_data)->parent_id = save_parent_id;
((cbf_h5Ovisithandle)op_data)->parent_addr = save_parent_addr;
((cbf_h5Ovisithandle)op_data)->parent_name = save_parent_name;
((cbf_h5Ovisithandle)op_data)->grand_parent_name = save_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->great_grand_parent_name = save_great_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->innexus = save_innexus;
((cbf_h5Ovisithandle)op_data)->incbf = save_incbf;
((cbf_h5Ovisithandle)op_data)->incbfdb = save_incbfdb;
((cbf_h5Ovisithandle)op_data)->incbfsf = save_incbfsf;
((cbf_h5Ovisithandle)op_data)->incbfcat = save_incbfcat;
((cbf_h5Ovisithandle)op_data)->incbfcol = save_incbfcol;
((cbf_h5Ovisithandle)op_data)->innxpdb = save_innxpdb;
return retval;
break;
case H5O_TYPE_DATASET:
dataset_id = H5Dopen2(loc_id,name,H5P_DEFAULT);
/* We have a dataset
We need to add it to the H5_Datasets category
in the current data block.
If it has attributes, we need to add them to
the H5_Attributes category
If we are innxpdb we need to convert this dataset
to a column in the CIF
*/
cbf_debug_print2("dataset %s",name);
cbf_reportnez(cbf_rewind_datablock(handle),errorcode);
if (cbf_find_datablock(handle,"H5")) {
cbf_reportnez(cbf_new_datablock(handle,"H5"),errorcode);
}
cbf_reportnez(cbf_require_category(handle,"H5_Datasets"),errorcode);
cbf_reportnez(cbf_new_row(handle),errorcode);
cbf_reportnez(cbf_row_number(handle,&row),errorcode);
cbf_reportnez(cbf_require_column(handle,"name"),errorcode);
cbf_reportnez(cbf_set_value(handle,name),errorcode);
cbf_reportnez(cbf_require_column(handle,"parent_name"),errorcode);
cbf_reportnez(cbf_set_value(handle,parent_name),errorcode);
cbf_reportnez(cbf_require_column(handle,"parent_id"),errorcode);
if (!parent_addr) {
cbf_reportnez(cbf_set_value(handle,"."),errorcode);
cbf_reportnez(cbf_set_typeofvalue(handle,"null"),errorcode);
} else {
sprintf(buffer,"0x%lx",(unsigned long)parent_addr);
cbf_reportnez(cbf_set_value(handle,buffer),errorcode);
}
cbf_reportnez(cbf_require_column(handle,"id"),errorcode);
sprintf(buffer,"0x%lx",(unsigned long)objinfo.addr);
cbf_reportnez(cbf_set_value(handle,buffer),errorcode);
attrib_num = objinfo.num_attrs;
cbf_reportnez(cbf_require_column(handle,"no_attributes"),errorcode);
cbf_reportnez(cbf_set_integervalue(handle,attrib_num),errorcode);
dimover = 0;
binsize = 0;
compression = 0;
binary_id = 0;
bits = 0;
sign = 0;
type = 0;
byteorder = " ";
dimfast=dimslow=dimmid = 0;
padding = 0;
cbftype[0] = '\0';
for (i=0; (ssize_t)i < attrib_num; i++) {
char * attrib_name = NULL;
attrib_id=H5Aopen_by_idx(dataset_id,".",
H5_INDEX_NAME,
H5_ITER_INC,
i,H5P_DEFAULT,H5P_DEFAULT);
attrib_ds = H5Aget_space(attrib_id);
attrib_type = H5Aget_type(attrib_id);
attrib_name_size = H5Aget_name(attrib_id,0,NULL);
attrib_name = NULL;
cbf_reportnez(cbf_alloc(((void **) &attrib_name),NULL,
1,attrib_name_size+1),errorcode);
cbf_h5failneg(H5Aget_name(attrib_id,
attrib_name_size+1,attrib_name),
CBF_ARGUMENT);
cbf_h5ds_store(handle,objinfo.addr,
name,-1,
"H5_Dataset_attribute",
attrib_id,
attrib_ds,
attrib_type,
attrib_name,1,(void **)&value);
if (*value) {
cbf_reportnez(cbf_rewind_datablock(handle),errorcode);
if (cbf_find_datablock(handle,"H5")) {
cbf_reportnez(cbf_new_datablock(handle,"H5"),errorcode);
}
cbf_reportnez(cbf_require_category(handle,"H5_Datasets"),errorcode);
cbf_reportnez(cbf_find_column(handle,"id"),errorcode);
cbf_reportnez(cbf_select_row(handle,row),errorcode);
cbf_reportnez(cbf_require_column(handle,attrib_name),errorcode);
cbf_reportnez(cbf_set_value(handle,value),errorcode);
if (!cbf_cistrcmp(attrib_name,"compression")) {
compression=(int)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"binid")) {
binary_id=(int)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"bits")) {
bits=(int)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"sign")) {
sign = (int)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"bintype")) {
type=(int)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"checked_digest")) {
checked_digest=(int)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"real")) {
realarray = (int)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"byteorder")) {
byteorder=
(value[0]=='l'||value[0]=='L')?
"little_endian":"big_endian";
} else if (!cbf_cistrcmp(attrib_name,"size")) {
binsize = (size_t)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"digest")
||!cbf_cistrcmp(attrib_name,"MD5_digest")) {
_cbf_strncpy(digest,value,24);
digest[24] = 0;
} else if (!cbf_cistrcmp(attrib_name,"dimover")) {
dimover = (size_t)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"dimfast")) {
dimfast = (size_t)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"dimmid")) {
dimmid = (size_t)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"dimslow")) {
dimslow = (size_t)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"padding")) {
padding = (size_t)strtol(value,NULL,0);
} else if (!cbf_cistrcmp(attrib_name,"cbftype")) {
_cbf_strncpy(cbftype,value,4);
cbftype[4] = '\0';
}
}
attrib_name = NULL;
cbf_reportnez(cbf_free((void **)&attrib_name,NULL),errorcode);
if (value) cbf_reportnez(cbf_free((void **)&value, NULL),errorcode);
H5Tclose(attrib_type);
H5Sclose(attrib_ds);
H5Aclose(attrib_id);
}
dataset_ds = H5Dget_space(dataset_id);
dataset_type = H5Dget_type(dataset_id);
dataset_type_class = H5Tget_class(dataset_type);
cbf_debug_print("cbf_h5ds_store");
if (!innxpdb) cbf_h5ds_store(handle,objinfo.addr,
parent_name,row,
"H5_Datasets",
dataset_id,
dataset_ds,
dataset_type,
name,0,(void **)&value);
if (innxpdb && (!incbfcol)) {
/*
no intervening column group, so this is a single
dataset for a single column
name is the name of the column
parent_name is the name of the category
grand_parent_name is the name of the data block or save frame
great_grand_parent_name is the name of the data block if this is a save frame
*/
cbf_get_bookmark(handle,&bookmark);
cbf_goto_bookmark(handle,saved_bookmark);
if (great_grand_parent_name && incbfsf) {
if (!cbf_find_datablock(handle,great_grand_parent_name)||
!cbf_new_datablock(handle,great_grand_parent_name)) {
if (cbf_find_saveframe(handle,grand_parent_name)) {
cbf_failnez(cbf_new_saveframe(handle,grand_parent_name));
}
} else { cbf_failnez(CBF_NOTFOUND);}
} else if ( !cbf_find_datablock(handle,grand_parent_name)||
!cbf_new_datablock(handle, grand_parent_name)){
cbf_debug_print4("Dataset %s in datablock %s cat %s",name,grand_parent_name,parent_name);
} else { cbf_failnez(CBF_NOTFOUND);}
cbf_debug_print("cbf_h5ds_store_as_column");
cbf_reportnez(cbf_h5ds_store_as_column(handle,0,name,
parent_name, dataset_id, dataset_ds,
dataset_type, (void **)&value ),errorcode);
}
if (atcbfcol) {
cbfrow = (int)strtol(name,NULL,0);
cbf_get_bookmark(handle,&bookmark);
cbf_goto_bookmark(handle,saved_bookmark);
cbf_reportnez(cbf_h5ds_store_as_column(handle, cbfrow, parent_name,
grand_parent_name, dataset_id, dataset_ds,
dataset_type, (void **)&value ),errorcode);
}
if (incbfcol&&binsize&&value) {
size_t elsize=0, nelem=0;
cbf_node * column = NULL;
cbf_file *tempfile = NULL;
long start = 0;
unsigned int localrow, ii;
elsize = (bits+CHAR_BIT-1)/CHAR_BIT;
nelem = (binsize+elsize-1)/elsize;
if (dimover <=0) dimover = nelem;
localrow = row;
if (nelem > 0 && elsize > 0) {
if (incbfcol) {
unsigned int rows;
rows = 0;
cbf_reportnez(cbf_count_rows(handle,&rows),errorcode);
if (cbfrow >= (int)rows) {
for (ii=rows; (int)ii <= cbfrow; ii++) {
cbf_reportnez(cbf_new_row(handle),errorcode);
}
}
cbf_select_row(handle,cbfrow);
localrow = cbfrow;
} else {
cbf_reportnez(cbf_rewind_datablock(handle),errorcode);
if (cbf_find_datablock(handle,"H5")) {
cbf_reportnez(cbf_new_datablock(handle,"H5"),errorcode);
}
cbf_reportnez(cbf_require_category(handle,"H5_Datasets"),errorcode);
cbf_reportnez(cbf_find_column(handle,"id"),errorcode);
cbf_reportnez(cbf_select_row(handle,localrow),errorcode);
cbf_reportnez(cbf_require_column(handle,"value"),errorcode);
}
column = handle->node;
if (dataset_type_class == H5T_OPAQUE) {
/* If we have stored as an opqaue dataset, keep it that way */
/* Remove the old value */
cbf_reportnez (cbf_set_columnrow (column, localrow, NULL, 1),errorcode)
/* Get the temporary file */
cbf_reportnez (cbf_open_temporary (column->context, &tempfile),errorcode)
/* Move to the end of the temporary file */
if (cbf_set_fileposition (tempfile, 0, SEEK_END)) {
(((cbf_h5Ovisithandle)op_data)->path_size)=save_path_size;
((cbf_h5Ovisithandle)op_data)->parent_id = save_parent_id;
((cbf_h5Ovisithandle)op_data)->parent_addr = save_parent_addr;
((cbf_h5Ovisithandle)op_data)->parent_name = save_parent_name;
((cbf_h5Ovisithandle)op_data)->grand_parent_name = save_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->great_grand_parent_name = save_great_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->innexus = save_innexus;
((cbf_h5Ovisithandle)op_data)->incbf = save_incbf;
((cbf_h5Ovisithandle)op_data)->incbfdb = save_incbfdb;
((cbf_h5Ovisithandle)op_data)->incbfsf = save_incbfsf;
((cbf_h5Ovisithandle)op_data)->incbfcat = save_incbfcat;
((cbf_h5Ovisithandle)op_data)->incbfcol = save_incbfcol;
((cbf_h5Ovisithandle)op_data)->innxpdb = save_innxpdb;
return CBF_FILESEEK | cbf_delete_fileconnection (&tempfile);
}
/* Get the starting location */
if (cbf_get_fileposition (tempfile, &start)) {
(((cbf_h5Ovisithandle)op_data)->path_size)=save_path_size;
((cbf_h5Ovisithandle)op_data)->parent_id = save_parent_id;
((cbf_h5Ovisithandle)op_data)->parent_addr = save_parent_addr;
((cbf_h5Ovisithandle)op_data)->parent_name = save_parent_name;
((cbf_h5Ovisithandle)op_data)->grand_parent_name = save_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->great_grand_parent_name = save_great_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->innexus = save_innexus;
((cbf_h5Ovisithandle)op_data)->incbf = save_incbf;
((cbf_h5Ovisithandle)op_data)->incbfdb = save_incbfdb;
((cbf_h5Ovisithandle)op_data)->incbfsf = save_incbfsf;
((cbf_h5Ovisithandle)op_data)->incbfcat = save_incbfcat;
((cbf_h5Ovisithandle)op_data)->incbfcol = save_incbfcol;
((cbf_h5Ovisithandle)op_data)->innxpdb = save_innxpdb;
return CBF_FILETELL | cbf_delete_fileconnection (&tempfile);
}
/* Discard any bits in the buffers */
cbf_reportnez (cbf_reset_bits (tempfile),errorcode)
/* Add the binary data to the temporary file */
if (!cbf_set_output_buffersize(tempfile,binsize)) {
memmove((void *)(tempfile->characters+tempfile->characters_used),
(void *)value,binsize);
tempfile->characters_used+=binsize;
}
cbf_onfailnez(cbf_set_bintext(column,localrow,CBF_TOKEN_TMP_BIN,
binary_id,tempfile,start,binsize,
1,digest,bits,sign,realarray,byteorder,
dimover, dimfast, dimmid, dimslow,
padding,compression),
cbf_delete_fileconnection (&tempfile));
cbf_onfailnez(cbf_flush_bits(tempfile),
cbf_delete_fileconnection (&tempfile));
} else {
/* If this is not an opaque object, then recompress
using the attributes */
cbf_reportnez(cbf_set_binary(handle->node,
handle->row,
compression,
binary_id,
(void *) value,
(bits+7)/8,
sign,
dimover,
realarray,
"little_endian",
dimover,
dimfast,
dimmid,
dimslow,
padding ), errorcode);
}
}
} else {
if (incbfcol && value) {
unsigned int rows;
rows = 0;
cbf_reportnez(cbf_count_rows(handle,&rows),errorcode);
if (cbfrow >= (int)rows) {
cbf_reportnez(cbf_insert_row(handle,cbfrow),errorcode);
}
cbf_reportnez(cbf_select_row(handle,cbfrow),errorcode);
cbf_reportnez(cbf_set_value(handle,value),errorcode);
if (cbftype[0] && cbf_cistrcmp(cbftype,"(null)")
&& _cbf_strlen(cbftype) == 4) {
cbf_reportnez(cbf_set_typeofvalue(handle,cbftype),errorcode);
}
}
}
if (incbfcol&&value) {
cbf_reportnez(cbf_goto_bookmark(handle,bookmark),errorcode);
}
if (value) cbf_reportnez(cbf_free((void **)&value, NULL),errorcode);
H5Dclose(dataset_id);
H5Sclose(dataset_ds);
H5Tclose(dataset_type);
break;
case H5O_TYPE_NAMED_DATATYPE:
break;
default:
(((cbf_h5Ovisithandle)op_data)->path_size)=save_path_size;
((cbf_h5Ovisithandle)op_data)->parent_id = save_parent_id;
((cbf_h5Ovisithandle)op_data)->parent_addr = save_parent_addr;
((cbf_h5Ovisithandle)op_data)->parent_name = save_parent_name;
((cbf_h5Ovisithandle)op_data)->grand_parent_name = save_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->great_grand_parent_name = save_great_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->innexus = save_innexus;
((cbf_h5Ovisithandle)op_data)->incbf = save_incbf;
((cbf_h5Ovisithandle)op_data)->incbfdb = save_incbfdb;
((cbf_h5Ovisithandle)op_data)->incbfsf = save_incbfsf;
((cbf_h5Ovisithandle)op_data)->incbfcat = save_incbfcat;
((cbf_h5Ovisithandle)op_data)->incbfcol = save_incbfcol;
((cbf_h5Ovisithandle)op_data)->innxpdb = save_innxpdb;
return CBF_FORMAT;
}
(((cbf_h5Ovisithandle)op_data)->path_size)= save_path_size;
((cbf_h5Ovisithandle)op_data)->parent_id = save_parent_id;
((cbf_h5Ovisithandle)op_data)->parent_addr = save_parent_addr;
((cbf_h5Ovisithandle)op_data)->parent_name = save_parent_name;
((cbf_h5Ovisithandle)op_data)->grand_parent_name = save_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->great_grand_parent_name = save_great_grand_parent_name;
((cbf_h5Ovisithandle)op_data)->innexus = save_innexus;
((cbf_h5Ovisithandle)op_data)->incbf = save_incbf;
((cbf_h5Ovisithandle)op_data)->incbfdb = save_incbfdb;
((cbf_h5Ovisithandle)op_data)->incbfsf = save_incbfsf;
((cbf_h5Ovisithandle)op_data)->incbfcat = save_incbfcat;
((cbf_h5Ovisithandle)op_data)->incbfcol = save_incbfcol;
((cbf_h5Ovisithandle)op_data)->innxpdb = save_innxpdb;
return 0;
}
/* Read an HDF5 file */
int cbf_read_h5file(const cbf_handle handle,
const cbf_h5handle h5handle,
const unsigned long int flags) {
cbf_node *node;
cbf_h5Ovisit_struct h5Ovisit;
int errorcode = 0;
if (!handle || !h5handle || !h5handle->hfile ) return CBF_ARGUMENT;
/* Move the flags into the h5handle */
h5handle -> flags = flags;
/* Delete the old datablocks */
if( handle->commentfile) cbf_failnez (cbf_free_file (&(handle->commentfile)));
cbf_failnez (cbf_find_parent (&node, handle->node, CBF_ROOT));
cbf_failnez (cbf_set_children (node, 0))
handle->node = node;
cbf_failnez (cbf_reset_refcounts(handle->dictionary));
/* register the CBF compressions if requested */
if (h5handle->flags & CBF_H5_REGISTER_COMPRESSIONS) {
if (!H5Zfilter_avail(CBF_H5Z_FILTER_CBF)) {
cbf_h5reportneg(H5Zregister(CBF_H5Z_CBF),CBF_H5ERROR ,errorcode);
if (errorcode) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_CBF (32006) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
#ifdef CBF_H5Z_USE_LZ4
if (!H5Zfilter_avail(CBF_H5Z_FILTER_LZ4)) {
errorcode = 0;
cbf_h5reportneg(H5Zregister(H5Z_LZ4),CBF_H5ERROR ,errorcode);
if (errorcode) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_LZ4 (32004) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
#endif
#ifdef CBF_H5Z_USE_BSHUF
if (!H5Zfilter_avail(CBF_H5Z_FILTER_BSHUF)) {
errorcode = 0;
cbf_h5reportneg(H5Zregister(bshuf_H5Filter),CBF_H5ERROR ,errorcode);
if (errorcode) {
CBF_PRINT_WARNING("Failed to load CBF_H5Z_FILTER_BSHUF (32008) compression filter "
"Check value of HDF5_PLUGIN_PATH environment variable");
}
}
#endif
}
h5Ovisit.handle = handle;
h5Ovisit.h5handle = h5handle;
h5Ovisit.parent_id = h5handle->hfile;
h5Ovisit.parent_addr = 0;
h5Ovisit.great_grand_parent_name = NULL;
h5Ovisit.grand_parent_name = NULL;
h5Ovisit.parent_name = "/";
cbf_failnez(cbf_alloc ((void **) (&(h5Ovisit.hid_path)), NULL,sizeof(hid_t), 1));
cbf_failnez(cbf_alloc ((void **) (&(h5Ovisit.haddr_path)), NULL,sizeof(haddr_t), 1));
h5Ovisit.capacity = 1;
h5Ovisit.path_size = 0;
h5Ovisit.bookmark.datablock = NULL;
h5Ovisit.bookmark.saveframe = NULL;
h5Ovisit.bookmark.category = NULL;
h5Ovisit.bookmark.column = NULL;
h5Ovisit.bookmark.row = 0;
h5Ovisit.bookmark.haverow = 0;
h5Ovisit.incbf
= h5Ovisit.incbfdb
= h5Ovisit.incbfsf
= h5Ovisit.incbfcat
= h5Ovisit.incbfcol
= h5Ovisit.innexus
= h5Ovisit.innxpdb = 0;
cbf_failnez(cbf_alloc ((void **) (&(h5Ovisit.hid_path)), NULL,
sizeof(hid_t), 1));
cbf_failnez(cbf_alloc ((void **) (&(h5Ovisit.haddr_path)), NULL,
sizeof(haddr_t), 1));
h5Ovisit.capacity = 1;
h5Ovisit.path_size = 0;
cbf_failnez(cbf_new_datablock(handle,"H5"));
/* visit the groups in the file, starting with the root group */
cbf_h5failneg(H5Literate(h5handle->hfile,
H5_INDEX_NAME,
H5_ITER_INC,
NULL,
cbf_object_visit,(void *)&h5Ovisit),CBF_FORMAT);
return CBF_SUCCESS;
}
/* Read an HDF5 file group */
int cbf_read_h5file_group(const cbf_handle handle,
const cbf_h5handle h5handle,
const unsigned long int flags,
hid_t group) {
cbf_node *node;
cbf_h5Ovisit_struct h5Ovisit;
int errorcode = 0;
if (!handle || !h5handle || !h5handle->hfile ) return CBF_ARGUMENT;
/* Move the flags into the h5handle */
h5handle -> flags = flags;
h5Ovisit.handle = handle;
h5Ovisit.h5handle = h5handle;
h5Ovisit.parent_id = h5handle->hfile;
h5Ovisit.parent_addr = 0;
h5Ovisit.great_grand_parent_name = NULL;
h5Ovisit.grand_parent_name = NULL;
h5Ovisit.parent_name = "/";
cbf_failnez(cbf_alloc ((void **) (&(h5Ovisit.hid_path)), NULL,sizeof(hid_t), 1));
cbf_failnez(cbf_alloc ((void **) (&(h5Ovisit.haddr_path)), NULL,sizeof(haddr_t), 1));
h5Ovisit.capacity = 1;
h5Ovisit.path_size = 0;
h5Ovisit.bookmark.datablock = NULL;
h5Ovisit.bookmark.saveframe = NULL;
h5Ovisit.bookmark.category = NULL;
h5Ovisit.bookmark.column = NULL;
h5Ovisit.bookmark.row = 0;
h5Ovisit.bookmark.haverow = 0;
h5Ovisit.incbf = h5Ovisit.incbfdb = h5Ovisit.incbfsf =h5Ovisit.incbfcat = h5Ovisit.incbfcol = 0;
h5Ovisit.innexus = h5Ovisit.innxpdb = 1;
cbf_failnez(cbf_alloc ((void **) (&(h5Ovisit.hid_path)), NULL,
sizeof(hid_t), 1));
cbf_failnez(cbf_alloc ((void **) (&(h5Ovisit.haddr_path)), NULL,
sizeof(haddr_t), 1));
h5Ovisit.capacity = 1;
h5Ovisit.path_size = 0;
cbf_failnez(cbf_new_datablock(handle,"H5"));
/* visit the groups in the file, starting with the root group */
cbf_h5failneg(H5Literate(group,
H5_INDEX_NAME,
H5_ITER_INC,
NULL,
cbf_object_visit,(void *)&h5Ovisit),CBF_FORMAT);
return CBF_SUCCESS;
}
/* get a fast bookmark from the current information in a cbf handle */
int cbf_get_fast_bookmark(const cbf_handle handle,
cbf_fast_bookmark * bookmark) {
if (!handle || !bookmark) return CBF_ARGUMENT;
bookmark->node = handle->node;
bookmark->row = handle->row;
return CBF_SUCCESS;
}
/* go to a fast bookmark in a cbf handle */
int cbf_goto_fast_bookmark(const cbf_handle handle,
const cbf_fast_bookmark bookmark) {
if (!handle) return CBF_ARGUMENT;
handle->node = bookmark.node;
handle->row = bookmark.row;
return CBF_SUCCESS;
}
/* get a bookmark from the current information in a cbf handle */
int cbf_get_bookmark(const cbf_handle handle,
cbf_bookmark * bookmark) {
if (cbf_datablock_name(handle,&(bookmark->datablock))) {
bookmark->datablock = NULL;
}
if (cbf_saveframe_name(handle,&(bookmark->saveframe))) {
bookmark->saveframe = NULL;
}
if (cbf_category_name(handle,&(bookmark->category))) {
bookmark->category = NULL;
}
if (cbf_column_name(handle,&(bookmark->column))) {
bookmark->column = NULL;
}
bookmark->haverow = 1;
if (cbf_row_number(handle,&(bookmark->row))) {
bookmark->row = 0;
bookmark->haverow = 0;
}
return CBF_SUCCESS;
}
/* go to a bookmark in the cbf handle */
int cbf_goto_bookmark(const cbf_handle handle,
const cbf_bookmark bookmark) {
unsigned int rows;
if (bookmark.datablock) {
cbf_failnez(cbf_rewind_datablock(handle));
cbf_failnez(cbf_find_datablock(handle,bookmark.datablock));
if (bookmark.saveframe) {
cbf_failnez(cbf_rewind_saveframe(handle));
cbf_failnez(cbf_find_saveframe(handle,bookmark.saveframe));
}
if (bookmark.category) {
cbf_failnez(cbf_rewind_category(handle));
cbf_failnez(cbf_find_category(handle,bookmark.category));
if (bookmark.column) {
cbf_failnez(cbf_rewind_column(handle));
cbf_failnez(cbf_find_column(handle,bookmark.column));
if (bookmark.haverow) {
rows = 0;
cbf_failnez(cbf_count_rows(handle,&rows));
if (bookmark.row < rows) {
cbf_failnez(cbf_select_row(handle,bookmark.row));
}
}
}
}
}
return CBF_SUCCESS;
}
#ifdef __cplusplus
}
#endif
|