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
|
/*
* This file is part of the ESO Common Pipeline Library
* Copyright (C) 2001-2022 European Southern Observatory
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <regex.h>
#include <cxstring.h>
#include <cpl_error_impl.h>
#include <cpl_type.h>
#include "cpl_multiframe.h"
/**
* @defgroup cpl_regex Regular Expression Filter
* @ingroup cpl_multiframe
*
* The module implements a regular expression filter type. The type
* @c cpl_regex is a compiled regular expression created with
* a given set of regular expression syntax options, and an optional
* negation of the result when it is applied to an input string.
*/
/**@{*/
typedef regex_t regex;
typedef enum _cpl_regex_syntax_option_ flag_type;
struct _cpl_regex_
{
regex _m_re;
flag_type _m_flags;
cxbool _m_negated;
};
/**
* @brief
* Create a new regular expression filter.
*
* @param expression Regular expression.
* @param negated Negate the result when applying the filter.
* @param flags Regular expression syntax options.
*
* @return
* The function returns a newly allocated regular expression filter object,
* or @c NULL in case an error occurred.
*
* The function allocates a regular expression filter object and initializes
* it with the compiled regular expression @em expression. If the flag
* @em negated is set the result when applying the filter to an input string
* is negated. The argument @em flags allows to specify regular expression
* syntax options for the compilation of the regular expression.
*
* The returned regular expression filter object must be destroyed using the
* destructor cpl_regex_delete().
*
* Note that the syntax option @c CPL_REGEX_NOSUBS is always set implicitly,
* since the interface does not allow to retrieve this information.
*/
cpl_regex *
cpl_regex_new(const char *expression, int negated, flag_type flags)
{
cpl_regex *self = cx_malloc(sizeof *self);
if (self) {
cxint _flags = REG_NOSUB;
/*
* Set flags. Note sub-expression matching is not supported
*/
if (flags & CPL_REGEX_ICASE) {
_flags = _flags | REG_ICASE;
}
if ((flags & CPL_REGEX_BASIC) && (flags & CPL_REGEX_EXTENDED)) {
cx_free(self);
cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
return NULL;
}
else {
if (flags & CPL_REGEX_BASIC) {
_flags = _flags & ~REG_EXTENDED;
}
if (flags & CPL_REGEX_EXTENDED) {
_flags = _flags | REG_EXTENDED;
}
}
cxint status = regcomp(&self->_m_re, expression, _flags);
if (status) {
cx_free(self);
self = NULL;
}
else {
self->_m_flags = flags | CPL_REGEX_NOSUBS;
self->_m_negated = negated ? TRUE : FALSE;
}
}
return self;
}
/**
* @brief
* Destroys a regular expression filter object.
*
* @param self The regular expression filter object.
*
* @return
* Nothing.
*
* The function destroys the given regular expression filter object @em self,
* and deallocates the memory used. If the filter object @em self is @c NULL,
* nothing is done and no error is set.
*/
void
cpl_regex_delete(cpl_regex *self)
{
if (self) {
regfree(&self->_m_re);
cx_free(self);
}
return;
}
/**
* @brief
* Compare a regular expression with a given character string.
*
* @param self The regular expression filter object to apply.
* @param string The string to be tested.
*
* @return
* The function returns 0 if the filter object does not match the input
* string, and a non-zero value otherwise.
*
* The function compares the input string @em string with the regular
* expression of the filter object @em self. The function returns a non-zero
* value for positive matches, i.e. the regular expression matches the input
* string @em string and the filter is not negated, or the regular expression
* does not match the input string but the filter is negated. Otherwise the
* function reports a negative match, i.e. returns @c 0.
*/
int
cpl_regex_apply(const cpl_regex *self, const char *string)
{
cxint status = regexec(&self->_m_re, string, 0, 0, 0);
cxbool match = (status == REG_NOMATCH) ? FALSE : TRUE;
return self->_m_negated ? !match : match;
}
/**
* @brief
* Test whether a regular expression filter is negated.
*
* @param self The regular expression filter object to test.
*
* @return
* The function returns @c 0 if the filter @em self is not negated, and
* @c 1 otherwise.
*
* The function reports whether the filter @em self is negated or not.
*/
int
cpl_regex_is_negated(const cpl_regex *self)
{
return self->_m_negated;
}
/**
* @brief
* Toggle the negation state of a regular expression filter.
*
* @param self The regular expression filter object to update.
*
* @return
* Nothing.
*
* The function toggles the negation state of the given regular expression
* filter object @em self. If @em self is negated, it is not negated after
* this function has been called, and vice versa.
*/
void
cpl_regex_negate(cpl_regex *self)
{
self->_m_negated = !self->_m_negated;
}
/**@}*/
// DICB FITS keyword ranking
typedef struct _cpl_dicb_header_order_ cpl_dicb_header_order;
struct _cpl_dicb_header_order_
{
const cxchar *key;
cxsize length;
cxuint rank;
};
static cpl_dicb_header_order _cpl_dicb_primary_ranking[] = {
{ "SIMPLE ", 7, 0 }, { "XTENSION", 8, 0 }, { "BITPIX ", 7, 10 },
{ "NAXIS ", 6, 11 }, { "NAXIS", 5, 12 }, { "GROUP ", 6, 20 },
{ "PCOUNT ", 7, 30 }, { "GCOUNT ", 7, 31 }, { "EXTEND ", 7, 32 },
{ "BZERO ", 6, 33 }, { "BSCALE ", 7, 34 }, { "BUNIT ", 6, 35 },
{ "BLANK ", 6, 36 }, { "TFIELDS ", 8, 37 }, { "TBCOL", 5, 38 },
{ "TFORM", 5, 39 }, { "TUNIT", 5, 40 }, { "INSTRUME", 8, 100 },
{ "TELESCOP", 8, 101 }, { "OBJECT ", 7, 102 }, { "ORIGIN ", 7, 103 },
{ "PI-COI ", 7, 104 }, { "EXPTIME ", 8, 105 }, { "RA ", 3, 106 },
{ "DEC ", 4, 107 }, { "EQUINOX ", 8, 108 }, { "RADESYS", 7, 109 },
{ "RADECSYS", 8, 109 }, { "MJD-OBS ", 8, 110 }, { "DATE-OBS", 8, 111 },
{ "TIMESYS ", 8, 112 }, { "LST ", 4, 113 }, { "UTC ", 4, 114 },
{ "WCSAXES ", 8, 200 }, { "CTYPE", 5, 201 }, { "CRVAL", 5, 202 },
{ "CRPIX", 5, 203 }, { "CD", 2, 204 }, { "CUNIT", 5, 205 },
{ "CUNIT", 5, 205 }, { "HDUCLASS", 8, 500 }, { "HDUCLAS", 7, 501 },
{ "HDUDOC ", 7, 502 }, { "HDUVERS ", 7, 503 }, { "SCIDATA ", 7, 504 },
{ "ERRDATA ", 7, 505 }, { "QUALDATA", 8, 506 }, { "QUALMASK", 8, 506 },
{ "ORIGFILE", 8, 600 }, { "PIPEFILE", 8, 601 }, { "ARCFILE ", 8, 602 },
{ "ZHECKSUM", 8, 700 }, { "DATASUM ", 8, 701 }, { "ZDATASUM", 8, 701 },
{ "DATAMD5 ", 8, 702 }, { "INHERIT ", 8, 800 }, { "HISTORY ", 8, 1001 },
{ "COMMENT ", 8, 1002 }, { " ", 8, 1003 }
};
static cpl_dicb_header_order _cpl_dicb_hierarch_ranking[] = {
{ "ESO DPR ", 8, 900 }, { "ESO OBS ", 8, 901 }, { "ESO TPL ", 8, 902 },
{ "ESO GEN ", 8, 903 }, { "ESO TEL ", 8, 904 }, { "ESO ADA ", 8, 905 },
{ "ESO INS ", 8, 906 }, { "ESO DET ", 8, 907 }, { "ESO DET1 ", 9, 907 },
{ "ESO DET2 ", 9, 908 }, { "ESO PRO ", 8, 909 }, { "ESO LOG ", 8, 910 },
{ "ESO QC ", 7, 911 }, { "ESO DRS ", 8, 912 }
};
inline static cxuint
_cpl_dicb_header_get_keyword_rank(const cxchar *cstr)
{
cxuint rank = 0;
cxsize ngroups;
cpl_dicb_header_order *ranks;
if (strncmp(cstr, "HIERARCH ", 9) != 0) {
rank = 899;
ranks = _cpl_dicb_primary_ranking;
ngroups = CX_N_ELEMENTS(_cpl_dicb_primary_ranking);
}
else {
/*
* add byte offset to skip 'HIERARCH ' prefix in header cards.
*/
cstr += 9;
rank = 999;
ranks = _cpl_dicb_hierarch_ranking;
ngroups = CX_N_ELEMENTS(_cpl_dicb_hierarch_ranking);
}
cxsize ig;
for (ig = 0; ig < ngroups; ++ig) {
if (strncmp(cstr, ranks[ig].key, ranks[ig].length) == 0) {
rank = ranks[ig].rank;
break;
}
}
return rank;
}
// FITS card implementation
#include <string.h>
#include <fitsio.h>
#include <cxmessages.h>
#include <cxstring.h>
typedef struct _cpl_fitscard_ cpl_fitscard;
struct _cpl_fitscard_
{
cx_string *_m_record;
};
typedef cxint (*cpl_fitscard_compare_func)(cpl_fitscard *, cpl_fitscard *);
/*
* Characters which are allowed in FITS keywords. Note
* that this also allows spaces and dots to support
* the ESO hierarchical keyword convention.
*/
static const cxchar *const _cpl_fits_keyword_traits =
" ._-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
inline static cxint
_cpl_fits_parse_keyname(cx_string *keyname, const cxchar *name)
{
cxsize length = strlen(name);
cx_string *_keyname;
if ((length == 0) || (strncmp(name, " ", 8) == 0)) {
_keyname = cx_string_create(" ");
}
else if (strncmp(name, "COMMENT", 7) == 0) {
_keyname = cx_string_create("COMMENT ");
}
else if (strncmp(name, "HISTORY", 7) == 0) {
_keyname = cx_string_create("HISTORY ");
}
else if (strncmp(name, "HIERARCH ", 9) == 0) {
if (length <= 9) {
return -1;
}
_keyname = cx_string_create(name);
cx_string_replace_character(_keyname, 9, cx_string_size(_keyname), '.',
' ');
if (cx_string_get(_keyname)[length - 1] != ' ') {
cx_string_append(_keyname, " ");
}
}
else if ((strchr(name, ' ') != 0) || (strchr(name, '.') != 0)) {
_keyname = cx_string_create("HIERARCH ");
cx_string_append(_keyname, name);
cx_string_replace_character(_keyname, 0, cx_string_size(_keyname), '.',
' ');
if (cx_string_get(_keyname)[cx_string_size(_keyname) - 1] != ' ') {
cx_string_append(_keyname, " ");
}
}
else {
if (strlen(name) > 8) {
return -2;
}
_keyname = cx_string_create(name);
cxint padding = 8 - strlen(name);
if (padding > 0) {
cx_string_extend(_keyname, padding, ' ');
}
}
cx_string_set(keyname, cx_string_get(_keyname));
cx_string_delete(_keyname);
return 0;
}
inline static cxint
_cpl_fits_get_keyname(const cxchar *card, cx_string *keyname, int *status)
{
if (*status) {
return *status;
}
cxchar name[FLEN_KEYWORD];
cxint length;
fits_get_keyname((cxchar *)card, name, &length, status);
if (*status) {
return *status;
}
if (_cpl_fits_parse_keyname(keyname, name)) {
*status = -1;
}
return *status;
}
/*
* Determine the datatype of the value from the value string
* of a FITS keyword, and return it.
*/
inline static cxchar
_cpl_fits_get_keytype(const cxchar *value)
{
char type_code = '\0';
if ((value != NULL) && value[0] != '\0') {
int status = 0;
fits_get_keytype(value, &type_code, &status);
if (status != 0) {
return '\0';
}
}
return type_code;
}
inline static cxint
_cpl_fits_parse_property_value(cx_string *value, const cpl_property *property)
{
cpl_type type = cpl_property_get_type(property);
switch (type) {
case CPL_TYPE_CHAR: {
char _value = cpl_property_get_char(property);
cx_string_sprintf(value, "'%c'", _value);
break;
}
case CPL_TYPE_BOOL: {
int _value = cpl_property_get_bool(property);
cx_string_sprintf(value, "%c", (_value == 0) ? 'F' : 'T');
break;
}
case CPL_TYPE_INT: {
int _value = cpl_property_get_int(property);
cx_string_sprintf(value, "%d", _value);
break;
}
case CPL_TYPE_LONG: {
long _value = cpl_property_get_long(property);
cx_string_sprintf(value, "%ld", _value);
break;
}
case CPL_TYPE_LONG_LONG: {
long long _value = cpl_property_get_long_long(property);
cx_string_sprintf(value, "%lld", _value);
break;
}
case CPL_TYPE_FLOAT: {
float _value = cpl_property_get_float(property);
cx_string_sprintf(value, "%.7G", _value);
/* Make sure the value string contains a decimal point */
if (!strchr(cx_string_get(value), '.')) {
cx_string_append(value, ".");
}
break;
}
case CPL_TYPE_DOUBLE: {
double _value = cpl_property_get_double(property);
cx_string_sprintf(value, "%.15G", _value);
/* Make sure the value string contains a decimal point */
if (!strchr(cx_string_get(value), '.')) {
cx_string_append(value, ".");
}
break;
}
case CPL_TYPE_FLOAT_COMPLEX: {
float complex _value = cpl_property_get_float_complex(property);
cx_string *zr = cx_string_new();
cx_string *zi = cx_string_new();
cx_string_sprintf(zr, "%.7G", crealf(_value));
if (!strchr(cx_string_get(zr), '.')) {
cx_string_append(zr, ".");
}
cx_string_sprintf(zi, "%.7G", cimagf(_value));
if (!strchr(cx_string_get(zi), '.')) {
cx_string_append(zi, ".");
}
cx_string_sprintf(value, "(%s, %s)", cx_string_get(zr),
cx_string_get(zi));
cx_string_delete(zi);
cx_string_delete(zr);
break;
}
case CPL_TYPE_DOUBLE_COMPLEX: {
double complex _value = cpl_property_get_double_complex(property);
cx_string *zr = cx_string_new();
cx_string *zi = cx_string_new();
cx_string_sprintf(zr, "%.15G", creal(_value));
if (!strchr(cx_string_get(zr), '.')) {
cx_string_append(zr, ".");
}
cx_string_sprintf(zi, "%.15G", cimag(_value));
if (!strchr(cx_string_get(zi), '.')) {
cx_string_append(zi, ".");
}
cx_string_sprintf(value, "(%s, %s)", cx_string_get(zr),
cx_string_get(zi));
cx_string_delete(zi);
cx_string_delete(zr);
break;
}
case CPL_TYPE_STRING: {
const char *_value = cpl_property_get_string(property);
cx_string_sprintf(value, "'%s'", _value);
break;
}
default:
return -1;
break;
}
return 0;
}
inline static cxint
_cpl_fits_format_card(cx_string *record,
const cxchar *keyname,
const cxchar *value,
const cxchar *comment)
{
if (strlen(keyname) == 0) {
return -1;
}
if (strlen(keyname) >= FLEN_KEYWORD) {
return -2;
}
if (value && (strlen(value) >= FLEN_VALUE)) {
return -3;
}
if (comment && (strlen(comment) >= FLEN_COMMENT)) {
return -4;
}
cxbool has_value = value != NULL;
cxbool has_comment = (comment != NULL) && (strlen(comment) > 0);
cxbool is_comment = FALSE;
/*
* Fill in the keyword name
*/
cx_string *_record = cx_string_create(keyname);
if (strncmp(keyname, " ", 8) == 0) {
is_comment = TRUE;
}
else if (strncmp(keyname, "COMMENT", 7) == 0) {
is_comment = TRUE;
}
else if (strncmp(keyname, "HISTORY", 7) == 0) {
is_comment = TRUE;
}
/*
* Add value indicator if needed
*/
if (!is_comment) {
cx_string_append(_record, "= ");
}
/*
* Add the value if it is present
*/
if (has_value) {
cxsize length = strlen(value);
if (value[0] == '\'') {
if (value[length - 1] != '\'') {
cx_string_delete(_record);
return -5;
}
cxchar *_s = cx_calloc(length - 1, sizeof(cxchar));
memcpy(_s, &value[1], length - 2);
cx_string *s = cx_string_create(_s);
cx_free(_s);
_s = (cxchar *)cx_string_get(s);
/*
* Expand single quote characters if necessary
*/
while ((_s = strchr(_s, '\'')) != NULL) {
cxsize pos = _s - cx_string_get(s);
if (pos == cx_string_size(s) - 1) {
cx_string_append(s, "'");
_s = (cxchar *)cx_string_get(s) + pos + 1;
}
else {
if (*(_s + 1) != '\'') {
cx_string_insert(s, pos, "'");
_s = (cxchar *)cx_string_get(s) + pos + 2;
}
}
}
/*
* Fixed format strings are required to have at least 8
* characters. Thus they may have to be padded with spaces.
* This does not actually apply to hierarchical keywords,
* which uses the free format convention. However cfitsio
* also applies the padding to hierarchical keywords,
* which should not be an issue since trailing blanks are not
* significant.
* Therefore the padding is applied here to all string values.
*/
cxint padding = 8 - cx_string_size(s);
if (padding > 0) {
cx_string_extend(s, padding, ' ');
}
cx_string_append(_record, "'");
cx_string_append(_record, cx_string_get(s));
cx_string_append(_record, "'");
cx_string_delete(s);
}
else if (value[0] == '(') {
if (value[length - 1] != ')') {
return -6;
}
cx_string_append(_record, value);
}
else if ((value[0] == 'T' || value[0] == 'F') && (length == 1)) {
/*
* The value of a logical keyword must appear in byte 30
* of the card.
*/
cxint padding = 29 - cx_string_size(_record);
if (padding > 0) {
cx_string_extend(_record, padding, ' ');
}
cx_string_append(_record, value);
}
else {
/*
* Fixed format numerical values occupy the bytes 11 through 30,
* and they are right justified in that field. Free format
* numerical values may appear anywhere starting at byte 11.
* With cfitsio the values are right justified at byte 30 unless
* either the keyword name or the value is to long to fit in byte
* 11 through 30.
* This behavior is replicated here.
*/
cxint padding = 30 - cx_string_size(_record) - length;
if (padding > 0) {
cx_string_extend(_record, padding, ' ');
}
cx_string_append(_record, value);
}
if (cx_string_size(_record) > FLEN_CARD - 1) {
cx_string_delete(_record);
return -7;
}
}
/*
* Add the optional comment
*/
if (has_comment) {
if (is_comment) {
cx_string_append(_record, comment);
}
else {
/*
* The keyword comment indicator ' / ' should appear in byte 32
* of the card, or later. If no character of the comment would
* appear in the final card, due to its size constraint, the whole
* comment, including the comment indicator, is dropped.
*/
if ((FLEN_CARD - 1) - cx_string_size(_record) > 3) {
cxint padding = 30 - cx_string_size(_record);
if (padding > 0) {
cx_string_extend(_record, padding, ' ');
}
cx_string_append(_record, " / ");
cx_string_append(_record, comment);
}
}
}
cx_string_truncate(_record, FLEN_CARD - 1);
cx_string_set(record, cx_string_get(_record));
cx_string_delete(_record);
return 0;
}
inline static cxssize
_cpl_fitsfile_find_extension(fitsfile *file, const cxchar *name, cxsize version)
{
cxint status = 0;
cxint chdu = -1;
fits_movnam_hdu(file, ANY_HDU, (cxchar *)name, version, &status);
if (status) {
return -1;
}
fits_get_hdu_num(file, &chdu);
return chdu - 1;
}
inline static cxssize
_cpl_fits_find_extension(const cxchar *filename,
const cxchar *name,
cxsize version)
{
cxint status = 0;
cxssize chdu = -1;
fitsfile *fp = NULL;
status = fits_open_diskfile(&fp, filename, READONLY, &status);
if (status) {
return -1;
}
chdu = _cpl_fitsfile_find_extension(fp, name, version);
fits_close_file(fp, &status);
return chdu;
}
inline static cpl_fitscard *
_cpl_fitscard_new(const cxchar *record)
{
cpl_fitscard *self = NULL;
if (record && (strlen(record) < FLEN_CARD)) {
self = cx_malloc(sizeof *self);
self->_m_record = cx_string_create(record);
}
return self;
}
inline static void
_cpl_fitscard_delete(cpl_fitscard *self)
{
if (self) {
cx_string_delete(self->_m_record);
cx_free(self);
}
return;
}
inline static const cx_string *
_cpl_fitscard_get_card(const cpl_fitscard *self)
{
cx_assert(self != NULL);
return self->_m_record;
}
inline static cpl_fitscard *
_cpl_fitscard_set_card(cpl_fitscard *self, const cxchar *record)
{
cx_assert(self != NULL);
cx_assert(record != NULL);
if (strlen(record) >= FLEN_CARD) {
return NULL;
}
cx_string_set(self->_m_record, record);
return self;
}
inline static cx_string *
_cpl_fitscard_get_key(const cpl_fitscard *self)
{
cx_assert(self != NULL);
cxchar keyname[FLEN_KEYWORD];
cxint keylen = 0;
cxint status = 0;
fits_get_keyname((cxchar *)cx_string_get(self->_m_record), keyname, &keylen,
&status);
if (status) {
return NULL;
}
return cx_string_create(keyname);
}
inline static cx_string *
_cpl_fitscard_get_value(const cpl_fitscard *self)
{
cx_assert(self != NULL);
cxchar value[FLEN_VALUE];
cxint status = 0;
fits_parse_value((cxchar *)cx_string_get(self->_m_record), value, 0,
&status);
if (status) {
return NULL;
}
return cx_string_create(value);
}
inline static cx_string *
_cpl_fitscard_get_comment(const cpl_fitscard *self)
{
cx_assert(self != NULL);
cxchar value[FLEN_VALUE];
cxchar comment[FLEN_COMMENT];
cxint status = 0;
/*
* Even if one is only interested in the comment, the value must
* always be retrieved (as the function name suggests).
*/
fits_parse_value((cxchar *)cx_string_get(self->_m_record), value, comment,
&status);
if (status) {
return NULL;
}
return cx_string_create(comment);
}
inline static cxint
_cpl_fitscard_set_key(cpl_fitscard *self, const cxchar *keyname)
{
cx_assert(self != NULL);
cx_assert(keyname != NULL);
cxint status = 0;
cx_string *_keyname = cx_string_create(keyname);
if ((cx_string_find_first_not_of(_keyname, _cpl_fits_keyword_traits) !=
cx_string_size(_keyname)) ||
(keyname[0] == '.')) {
return BAD_KEYCHAR;
}
status = _cpl_fits_parse_keyname(_keyname, keyname);
if (status) {
cx_string_delete(_keyname);
return status;
}
/*
* Reassemble the record using the new keyword name, the value and
* the comment. The value and comment are parsed to allow to
* properly check for errors or FITS standard violations.
*/
cxchar value[FLEN_VALUE];
cxchar comment[FLEN_COMMENT];
fits_parse_value((cxchar *)cx_string_get(self->_m_record), value, comment,
&status);
if (status) {
cx_string_delete(_keyname);
return status;
}
cx_string *record = cx_string_new();
status =
_cpl_fits_format_card(record, cx_string_get(_keyname), value, comment);
if (status) {
cx_string_delete(record);
cx_string_delete(_keyname);
return status;
}
cx_string_set(self->_m_record, cx_string_get(record));
cx_string_delete(record);
cx_string_delete(_keyname);
return 0;
}
inline static cxint
_cpl_fitscard_set_value(cpl_fitscard *self,
const cxchar *value,
const cxchar *comment)
{
cx_assert(self != NULL);
cx_assert(value != NULL);
cxint status = 0;
cx_string *keyname = cx_string_new();
_cpl_fits_get_keyname(cx_string_get(self->_m_record), keyname, &status);
if (status) {
cx_string_delete(keyname);
return status;
}
cx_string *record = cx_string_new();
status =
_cpl_fits_format_card(record, cx_string_get(keyname), value, comment);
if (status) {
cx_string_delete(record);
cx_string_delete(keyname);
return status;
}
cx_string_set(self->_m_record, cx_string_get(record));
cx_string_delete(record);
cx_string_delete(keyname);
return 0;
}
inline static cxint
_cpl_fitscard_set_comment(cpl_fitscard *self, const cxchar *comment)
{
cx_assert(self != NULL);
cxchar _value[FLEN_VALUE];
cxint status = 0;
cx_string *keyname = cx_string_new();
_cpl_fits_get_keyname(cx_string_get(self->_m_record), keyname, &status);
if (status) {
cx_string_delete(keyname);
return status;
}
fits_parse_value((cxchar *)cx_string_get(self->_m_record), _value, 0,
&status);
if (status) {
cx_string_delete(keyname);
return status;
}
cx_string *record = cx_string_new();
status =
_cpl_fits_format_card(record, cx_string_get(keyname), _value, comment);
if (status) {
cx_string_delete(record);
cx_string_delete(keyname);
return status;
}
cx_string_set(self->_m_record, cx_string_get(record));
cx_string_delete(record);
cx_string_delete(keyname);
return 0;
}
/*
* Sloppy comparison of data types.
*
* The function returns true if the given typeid is of a certain
* FITS keyword type. This ignores differences due to storage
* size or precision of the different data types involved.
*/
inline static cxint
_cpl_fitscard_is_type(cpl_fitscard *self, cpl_type typeid)
{
cx_string *value = _cpl_fitscard_get_value(self);
cxchar vtype = _cpl_fits_get_keytype(cx_string_get(value));
cx_string_delete(value);
if (vtype == '\0') {
return -1;
}
cxint is_type = 0;
switch (typeid) {
case CPL_TYPE_CHAR:
case CPL_TYPE_STRING:
is_type = (vtype == 'C') ? 1 : 0;
break;
case CPL_TYPE_BOOL:
is_type = (vtype == 'L') ? 1 : 0;
break;
case CPL_TYPE_INT:
case CPL_TYPE_LONG:
case CPL_TYPE_LONG_LONG:
is_type = (vtype == 'I') ? 1 : 0;
break;
case CPL_TYPE_FLOAT:
case CPL_TYPE_DOUBLE:
is_type = (vtype == 'F') ? 1 : 0;
break;
case CPL_TYPE_FLOAT_COMPLEX:
case CPL_TYPE_DOUBLE_COMPLEX:
is_type = (vtype == 'X') ? 1 : 0;
break;
default:
is_type = 0;
break;
}
return is_type;
}
inline static cxint
_cpl_fitscard_compare_dicb(cpl_fitscard *a, cpl_fitscard *b)
{
cx_assert((a != NULL) && (b != NULL));
const cxchar *_a = cx_string_get(_cpl_fitscard_get_card(a));
const cxchar *_b = cx_string_get(_cpl_fitscard_get_card(b));
cxuint ra = _cpl_dicb_header_get_keyword_rank(_a);
cxuint rb = _cpl_dicb_header_get_keyword_rank(_b);
if (ra == rb) {
return strcmp(_a, _b);
}
return (ra < rb) ? -1 : 1;
}
// FITS header implementation
#include <cxdeque.h>
typedef struct _cpl_fitsheader_ cpl_fitsheader;
struct _cpl_fitsheader_
{
cx_deque *_m_records;
};
inline static cpl_fitsheader *
_cpl_fitsheader_new(void)
{
cpl_fitsheader *self = cx_malloc(sizeof *self);
if (self) {
self->_m_records = cx_deque_new();
if (!self->_m_records) {
cx_free(self);
self = NULL;
}
}
return self;
}
inline static cpl_fitsheader *
_cpl_fitsheader_duplicate(const cpl_fitsheader *other)
{
cx_assert(other != NULL);
cpl_fitsheader *self = _cpl_fitsheader_new();
if (self) {
cx_deque *cards = other->_m_records;
cxsize ic;
cxsize sz = cx_deque_size(cards);
for (ic = 0; ic < sz; ++ic) {
cpl_fitscard *card = cx_deque_get(cards, ic);
cpl_fitscard *_card =
_cpl_fitscard_new(cx_string_get(_cpl_fitscard_get_card(card)));
cx_deque_push_back(self->_m_records, _card);
}
}
return self;
}
inline static void
_cpl_fitsheader_delete(cpl_fitsheader *self)
{
if (self) {
cx_deque_destroy(self->_m_records, (cx_free_func)_cpl_fitscard_delete);
cx_free(self);
}
return;
}
inline static cxsize
_cpl_fitsheader_get_size(const cpl_fitsheader *self)
{
cx_assert(self != NULL);
return cx_deque_size(self->_m_records);
}
inline static cpl_fitscard *
_cpl_fitsheader_get(cpl_fitsheader *self, cxsize irecord)
{
cx_assert(self != NULL);
cx_assert(irecord < cx_deque_size(self->_m_records));
cpl_fitscard *card = cx_deque_get(self->_m_records, irecord);
return card;
}
inline static const cpl_fitscard *
_cpl_fitsheader_get_const(const cpl_fitsheader *self, cxsize irecord)
{
cx_assert(self != NULL);
cx_assert(irecord < cx_deque_size(self->_m_records));
return _cpl_fitsheader_get((cpl_fitsheader *)self, irecord);
}
inline static cpl_fitscard *
_cpl_fitsheader_find(cpl_fitsheader *self, const cxchar *keyname)
{
cx_assert(self != NULL);
cx_assert(keyname != NULL);
cx_deque *cards = self->_m_records;
cxsize ic;
cxsize sz = cx_deque_size(cards);
cpl_fitscard *card = NULL;
for (ic = 0; ic < sz; ++ic) {
cpl_fitscard *_card = cx_deque_get(cards, ic);
cx_string *_keyname = _cpl_fitscard_get_key(_card);
cxint result = strcmp(cx_string_get(_keyname), keyname);
cx_string_delete(_keyname);
if (result == 0) {
card = _card;
break;
}
}
return card;
}
inline static const cpl_fitscard *
_cpl_fitsheader_find_const(const cpl_fitsheader *self, const cxchar *keyname)
{
cx_assert(self != NULL);
cx_assert(keyname != NULL);
return _cpl_fitsheader_find((cpl_fitsheader *)self, keyname);
}
inline static cxsize
_cpl_fitsheader_find_card(const cpl_fitsheader *self, const cpl_fitscard *card)
{
cx_assert(self != NULL);
cx_assert(card != NULL);
cx_deque *cards = self->_m_records;
cxsize ic;
cxsize sz = cx_deque_size(cards);
cxsize pos = sz;
cx_string *keyname = _cpl_fitscard_get_key(card);
for (ic = 0; ic < sz; ++ic) {
cpl_fitscard *_card = cx_deque_get(cards, ic);
cx_string *_keyname = _cpl_fitscard_get_key(_card);
cxint result = cx_string_compare(_keyname, keyname);
cx_string_delete(_keyname);
if (result == 0) {
pos = ic;
break;
}
}
cx_string_delete(keyname);
return pos;
}
inline static cxsize
_cpl_fitsheader_get_position(const cpl_fitsheader *self, const cxchar *keyname)
{
cx_assert(self != NULL);
cx_assert(keyname != NULL);
cx_deque *cards = self->_m_records;
cxsize ic;
cxsize sz = cx_deque_size(cards);
cxsize pos = sz;
for (ic = 0; ic < sz; ++ic) {
cpl_fitscard *_card = cx_deque_get(cards, ic);
cx_string *_keyname = _cpl_fitscard_get_key(_card);
cxint result = strcmp(cx_string_get(_keyname), keyname);
cx_string_delete(_keyname);
if (result == 0) {
pos = ic;
break;
}
}
return pos;
}
inline static cpl_fitsheader *
_cpl_fitsheader_create_from_file(fitsfile *file, cxsize position)
{
cx_assert(file != NULL);
cxint status = 0;
cxint nrecords = 0;
fits_movabs_hdu(file, position + 1, 0, &status);
fits_read_record(file, 0, NULL, &status);
fits_get_hdrspace(file, &nrecords, 0, &status);
if (status) {
return NULL;
}
cpl_fitsheader *self = _cpl_fitsheader_new();
if (nrecords > 0) {
for (cxint i = 0; i < nrecords; ++i) {
cxchar record[FLEN_CARD];
fits_read_record(file, i + 1, record, &status);
if (!status) {
cx_deque_push_back(self->_m_records, _cpl_fitscard_new(record));
}
}
}
if (status) {
_cpl_fitsheader_delete(self);
return NULL;
}
return self;
}
inline static cpl_fitsheader *
_cpl_fitsheader_create(const cxchar *filename, cxsize position)
{
cx_assert(filename != NULL);
cxint status = 0;
fitsfile *ifile = 0;
fits_open_diskfile(&ifile, filename, READONLY, &status);
if (status) {
return NULL;
}
cpl_fitsheader *self = _cpl_fitsheader_create_from_file(ifile, position);
fits_close_file(ifile, &status);
if (self == NULL) {
return NULL;
}
return self;
}
inline static cpl_fitsheader *
_cpl_fitsheader_create_from_filter(const cpl_fitsheader *other,
const cpl_regex *filter)
{
cx_assert(other != NULL);
cx_assert(filter != NULL);
cx_deque *cards = other->_m_records;
cxsize ic;
cxsize sz = cx_deque_size(cards);
cpl_fitsheader *self = _cpl_fitsheader_new();
for (ic = 0; ic < sz; ++ic) {
cpl_fitscard *card = cx_deque_get(cards, ic);
const cxchar *record = cx_string_get(_cpl_fitscard_get_card(card));
if (cpl_regex_apply(filter, record)) {
cpl_fitscard *_card = _cpl_fitscard_new(record);
cx_deque_push_back(self->_m_records, _card);
}
}
return self;
}
inline static cxint
_cpl_fitsheader_append(cpl_fitsheader *self, cpl_fitscard *card)
{
cx_assert(self != NULL);
cx_assert(card != NULL);
cx_deque_push_back(self->_m_records, card);
return 0;
}
inline static cxint
_cpl_fitsheader_insert(cpl_fitsheader *self, cpl_fitscard *card, cxsize irecord)
{
cx_assert(self != NULL);
cx_assert(card != NULL);
cx_assert(irecord < cx_deque_size(self->_m_records));
cx_deque_insert(self->_m_records, irecord, card);
return 0;
}
inline static cpl_fitscard *
_cpl_fitsheader_remove(cpl_fitsheader *self, cxsize irecord)
{
cx_assert(self != NULL);
cx_assert(irecord < cx_deque_size(self->_m_records));
cpl_fitscard *card = cx_deque_extract(self->_m_records, irecord);
return card;
}
inline static cpl_fitscard *
_cpl_fitsheader_remove_card(cpl_fitsheader *self, cpl_fitscard *card)
{
cx_assert(self != NULL);
cx_assert(card != NULL);
cxsize irecord = _cpl_fitsheader_find_card(self, card);
cpl_fitscard *_card = cx_deque_extract(self->_m_records, irecord);
return _card;
}
inline static cxint
_cpl_fitsheader_join(cpl_fitsheader *self, const cpl_fitsheader *other)
{
cx_assert(self != NULL);
cx_assert(other != NULL);
cx_deque *cards = other->_m_records;
cxsize ic;
cxsize sz = cx_deque_size(cards);
for (ic = 0; ic < sz; ++ic) {
cpl_fitscard *card = cx_deque_get(cards, ic);
const cxchar *record = cx_string_get(_cpl_fitscard_get_card(card));
cpl_fitscard *_card = _cpl_fitscard_new(record);
cx_deque_push_back(self->_m_records, _card);
}
return 0;
}
inline static cxint
_cpl_fitsheader_sort(cpl_fitsheader *self, cpl_fitscard_compare_func cmp)
{
cx_assert(self != 0);
cx_assert(cmp != NULL);
cx_deque_sort(self->_m_records, (cx_compare_func)cmp);
return 0;
}
#if 0
static void
_cpl_fitsheader_dump(const cpl_fitsheader *self, FILE *os)
{
if (self) {
cxsize sz = cx_deque_size(self->_m_records);
for (cxsize i = 0; i < sz; ++i) {
const cx_string *record =
_cpl_fitscard_get_card(cx_deque_get(self->_m_records, i));
fprintf(os, "%s\n", cx_string_get(record));
}
}
return;
}
#endif
// FITS data unit base class implementation
#include <fitsio.h>
#include <cxstring.h>
typedef enum _cpl_fits_du_type_ cpl_fits_dataunit_type;
enum _cpl_fits_du_type_
{
CPL_FITS_DU_TYPE_IMAGE = IMAGE_HDU,
CPL_FITS_DU_TYPE_ATBL = ASCII_TBL,
CPL_FITS_DU_TYPE_BTBL = BINARY_TBL,
CPL_FITS_DU_TYPE_EMPTY
};
typedef struct _cpl_fitsdataunit_ cpl_fitsdataunit;
struct _cpl_fitsdataunit_
{
fitsfile *_m_file;
cxulong _m_position;
cxint _m_type;
cx_string *_m_name;
cxulong _m_version;
cxulong _m_level;
cxint _m_bitpix;
cxlong _m_pcount;
cxlong _m_gcount;
cxulong _m_unitsize;
cxulong _m_datasize;
cxint _m_naxes;
LONGLONG *_m_naxis;
/*
* Virtual function for writing data unit structure to the target file
*/
cxint (*_m_write_layout)(cpl_fitsdataunit *self, fitsfile *file);
};
inline static cxint
_cpl_fits_read_axes(fitsfile *fptr, cxint naxis, LONGLONG *naxes, cxint *status)
{
if (*status == 0) {
cxchar keyn[FLEN_KEYWORD] = { 'N', 'A', 'X', 'I', 'S', '\0' };
cxint i;
for (i = 0; i < naxis; ++i) {
snprintf(&keyn[5], FLEN_KEYWORD - 5, "%d", i + 1);
fits_read_key(fptr, TLONGLONG, keyn, &naxes[i], 0, status);
}
}
return *status;
}
inline static int
_cpl_fits_read_extinfo(fitsfile *fptr,
cxint *hdutype,
cxint *bitpix,
cxint *naxis,
LONGLONG *naxes,
cxlong *pcount,
cxlong *gcount,
cxint *status)
{
if (*status == 0) {
cxint _status = 0;
if (hdutype) {
fits_get_hdu_type(fptr, hdutype, &_status);
}
fits_read_key(fptr, TINT, "BITPIX", bitpix, 0, &_status);
fits_read_key(fptr, TINT, "NAXIS", naxis, 0, &_status);
if (naxes) {
_cpl_fits_read_axes(fptr, *naxis, naxes, &_status);
}
if (_status) {
*status = _status;
return *status;
}
fits_read_key(fptr, TLONG, "PCOUNT", pcount, 0, &_status);
fits_read_key(fptr, TLONG, "GCOUNT", gcount, 0, &_status);
if (_status == KEY_NO_EXIST) {
cxint hdunum;
fits_get_hdu_num(fptr, &hdunum);
/*
* Reset the error status if this is a primary HDU, since
* these keyword may not be present in a primary HDU.
*/
if (hdunum == 1) {
_status = 0;
*pcount = 0;
*gcount = 1;
}
}
*status = _status;
}
return *status;
}
inline static int
_cpl_fits_read_extid(fitsfile *fptr,
cxchar *extname,
cxulong *extvers,
cxulong *extlevel,
cxint *status)
{
if (*status == 0) {
cxint _status = 0;
fits_read_key(fptr, TSTRING, "EXTNAME", extname, 0, &_status);
if (_status == KEY_NO_EXIST) {
_status = 0;
}
fits_read_key(fptr, TULONG, "EXTVER", extvers, 0, &_status);
if (_status == KEY_NO_EXIST) {
_status = 0;
}
fits_read_key(fptr, TULONG, "EXTLEVEL", extlevel, 0, &_status);
if (_status == KEY_NO_EXIST) {
_status = 0;
}
*status = _status;
}
return *status;
}
inline static cxint
_cpl_fitsdataunit_init_empty(cpl_fitsdataunit *self)
{
self->_m_file = NULL;
self->_m_position = 0;
self->_m_type = ANY_HDU;
self->_m_name = NULL;
self->_m_version = 0;
self->_m_level = 0;
self->_m_bitpix = 0;
self->_m_pcount = 0;
self->_m_gcount = 0;
self->_m_unitsize = 0;
self->_m_datasize = 0;
self->_m_naxes = 0;
self->_m_naxis = NULL;
self->_m_write_layout = NULL;
return 0;
}
inline static cxint
_cpl_fitsdataunit_init(cpl_fitsdataunit *self, fitsfile *file, cxulong position)
{
_cpl_fitsdataunit_init_empty(self);
self->_m_file = file;
self->_m_position = position + 1;
self->_m_name = cx_string_new();
return 0;
}
inline static void
_cpl_fitsdataunit_clear(cpl_fitsdataunit *self)
{
cx_string_delete(self->_m_name);
cx_free(self->_m_naxis);
self->_m_file = NULL;
self->_m_position = 0;
self->_m_type = ANY_HDU;
self->_m_name = NULL;
self->_m_version = 0;
self->_m_level = 0;
self->_m_bitpix = 0;
self->_m_pcount = 0;
self->_m_gcount = 0;
self->_m_unitsize = 0;
self->_m_datasize = 0;
self->_m_naxes = 0;
self->_m_naxis = NULL;
self->_m_write_layout = NULL;
return;
}
inline static cxint
_cpl_fitsdataunit_copy(cpl_fitsdataunit *self, const cpl_fitsdataunit *other)
{
self->_m_file = other->_m_file;
self->_m_position = other->_m_position;
self->_m_type = other->_m_type;
self->_m_name = cx_string_copy(other->_m_name);
self->_m_version = other->_m_version;
self->_m_level = other->_m_level;
self->_m_bitpix = other->_m_bitpix;
self->_m_pcount = other->_m_pcount;
self->_m_gcount = other->_m_gcount;
self->_m_unitsize = other->_m_unitsize;
self->_m_datasize = other->_m_datasize;
self->_m_naxes = other->_m_naxes;
if (other->_m_naxis) {
register size_t sz = other->_m_naxes * sizeof *other->_m_naxis;
self->_m_naxis = cx_malloc(sz);
memcpy(self->_m_naxis, other->_m_naxis, sz);
}
else {
self->_m_naxis = NULL;
}
self->_m_write_layout = other->_m_write_layout;
return 0;
}
inline static cxint
_cpl_fitsdataunit_make_current(cpl_fitsdataunit *self)
{
cxint status = 0;
cxint hdutype = ANY_HDU;
fits_movabs_hdu(self->_m_file, self->_m_position, &hdutype, &status);
return status;
}
inline static cxint
_cpl_fitsdataunit_initialize(cpl_fitsdataunit *self,
fitsfile *file,
cxulong position)
{
cxint status = 0;
cxint idummy = 0;
LONGLONG start = 0;
LONGLONG end = 0;
_cpl_fitsdataunit_init(self, file, position);
status = _cpl_fitsdataunit_make_current(self);
if (status) {
_cpl_fitsdataunit_clear(self);
return status;
}
_cpl_fits_read_extinfo(self->_m_file, &idummy, &self->_m_bitpix,
&self->_m_naxes, 0, &self->_m_pcount,
&self->_m_gcount, &status);
fits_get_hduaddrll(self->_m_file, 0, &start, &end, &status);
if (status) {
_cpl_fitsdataunit_clear(self);
return status;
}
self->_m_unitsize = end - start;
if (self->_m_naxes > 0) {
self->_m_naxis = cx_malloc(self->_m_naxes * sizeof *self->_m_naxis);
_cpl_fits_read_axes(self->_m_file, self->_m_naxes, &self->_m_naxis[0],
&status);
}
if (status) {
_cpl_fitsdataunit_clear(self);
return status;
}
if (self->_m_position == 0) {
cxint hdunum;
self->_m_position = fits_get_hdu_num(self->_m_file, &hdunum);
}
if (self->_m_type == ANY_HDU) {
fits_get_hdu_type(self->_m_file, &self->_m_type, &status);
}
if (status) {
_cpl_fitsdataunit_clear(self);
return status;
}
if (cx_string_size(self->_m_name)) {
cxchar extname[FLEN_VALUE] = { '\0' };
_cpl_fits_read_extid(self->_m_file, extname, &self->_m_version,
&self->_m_level, &status);
cx_string_set(self->_m_name, extname);
}
if (status) {
_cpl_fitsdataunit_clear(self);
return status;
}
/*
* Calculate the size of the payload in bytes
*/
if (self->_m_naxis != NULL) {
cxint i;
self->_m_datasize =
self->_m_bitpix < 0 ? -self->_m_bitpix / 8 : self->_m_bitpix / 8;
for (i = 0; i < self->_m_naxes; ++i) {
self->_m_datasize *= self->_m_naxis[i];
}
}
return status;
}
inline static fitsfile *
_cpl_fitsdataunit_get_file(const cpl_fitsdataunit *self)
{
cx_assert(self != NULL);
return self->_m_file;
}
inline static const cx_string *
_cpl_fitsdataunit_get_name(const cpl_fitsdataunit *self)
{
cx_assert(self != NULL);
return self->_m_name;
}
inline static cxulong
_cpl_fitsdataunit_get_version(const cpl_fitsdataunit *self)
{
cx_assert(self != NULL);
return self->_m_version;
}
inline static cxulong
_cpl_fitsdataunit_get_level(const cpl_fitsdataunit *self)
{
cx_assert(self != NULL);
return self->_m_level;
}
inline static cxulong
_cpl_fitsdataunit_get_position(const cpl_fitsdataunit *self)
{
cx_assert(self != NULL);
return self->_m_position - 1;
}
inline static cpl_fits_dataunit_type
_cpl_fitsdataunit_get_type(const cpl_fitsdataunit *self)
{
cx_assert(self != NULL);
return self->_m_type;
}
inline static cxulong
_cpl_fitsdataunit_get_size(const cpl_fitsdataunit *self)
{
cx_assert(self != NULL);
return self->_m_unitsize;
}
inline static cxulong
_cpl_fitsdataunit_get_datasize(const cpl_fitsdataunit *self)
{
cx_assert(self != NULL);
return self->_m_datasize;
}
inline static cx_string *
_cpl_fitsdataunit_get_filename(const cpl_fitsdataunit *self)
{
cxchar name[FLEN_FILENAME] = { '\0' };
cxint status = 0;
fits_file_name(self->_m_file, name, &status);
if (status) {
return NULL;
}
return cx_string_create(name);
}
inline static cxint
_cpl_fitsdataunit_write_data(cpl_fitsdataunit *self, fitsfile *file)
{
cxint status = 0;
status = _cpl_fitsdataunit_make_current(self);
if (status) {
return status;
}
fits_copy_data(self->_m_file, file, &status);
return status;
}
inline static cxint
_cpl_fitsdataunit_write_layout(cpl_fitsdataunit *self, fitsfile *file)
{
if (self->_m_write_layout == NULL) {
return -1;
}
return self->_m_write_layout(self, file);
}
// FITS image data unit implementation
#include <fitsio.h>
typedef struct _cpl_fitsemptyunit_ cpl_fitsemptyunit;
struct _cpl_fitsemptyunit_
{
cpl_fitsdataunit base; /* Must be the first member */
};
inline static void
_cpl_fitsemptyunit_delete(cpl_fitsemptyunit *self)
{
if (self) {
_cpl_fitsdataunit_clear((cpl_fitsdataunit *)self);
}
cx_free(self);
return;
}
static cxint
_cpl_fitsemptyunit_write_layout(cpl_fitsdataunit *self, fitsfile *file)
{
cx_assert(self != NULL);
cx_assert(file != NULL);
cxint status = 0;
fits_write_imghdrll(file, self->_m_bitpix, self->_m_naxes,
&self->_m_naxis[0], &status);
return 0;
}
inline static cpl_fitsemptyunit *
_cpl_fitsemptyunit_new(void)
{
cpl_fitsemptyunit *self = cx_malloc(sizeof *self);
cpl_fitsdataunit *_self = (cpl_fitsdataunit *)self;
_cpl_fitsdataunit_init_empty(_self);
_self->_m_bitpix = 8;
_self->_m_type = CPL_FITS_DU_TYPE_EMPTY;
_self->_m_write_layout = _cpl_fitsemptyunit_write_layout;
return self;
}
inline static cpl_fitsemptyunit *
_cpl_fitsemptyunit_clone(const cpl_fitsemptyunit *other)
{
cpl_fitsdataunit *_other = (cpl_fitsdataunit *)other;
if (((_other->_m_file != NULL) && (_other->_m_position != 0)) ||
(_other->_m_type != CPL_FITS_DU_TYPE_EMPTY)) {
return NULL;
}
cpl_fitsemptyunit *self = cx_malloc(sizeof *self);
cxint status = _cpl_fitsdataunit_copy((cpl_fitsdataunit *)self, _other);
if (status) {
_cpl_fitsemptyunit_delete(self);
return NULL;
}
return self;
}
// FITS image data unit implementation
#include <fitsio.h>
typedef struct _cpl_fitsimageunit_ cpl_fitsimageunit;
struct _cpl_fitsimageunit_
{
cpl_fitsdataunit base; /* Must be the first member */
};
inline static void
_cpl_fitsimageunit_delete(cpl_fitsimageunit *self)
{
if (self) {
_cpl_fitsdataunit_clear((cpl_fitsdataunit *)self);
}
cx_free(self);
return;
}
static cxint
_cpl_fitsimageunit_write_layout(cpl_fitsdataunit *self, fitsfile *file)
{
cx_assert(self != NULL);
cx_assert(file != NULL);
cxint status = 0;
fits_write_imghdrll(file, self->_m_bitpix, self->_m_naxes,
&self->_m_naxis[0], &status);
return 0;
}
inline static cpl_fitsimageunit *
_cpl_fitsimageunit_new(fitsfile *file, cxulong position)
{
cpl_fitsimageunit *self = cx_malloc(sizeof *self);
cpl_fitsdataunit *_self = (cpl_fitsdataunit *)self;
cxint status = _cpl_fitsdataunit_initialize(_self, file, position);
if (status) {
cx_free(self);
return NULL;
}
if (_self->_m_type != CPL_FITS_DU_TYPE_IMAGE) {
status = NOT_IMAGE;
}
if (_self->_m_pcount != 0) {
status = BAD_PCOUNT;
}
if (_self->_m_gcount != 1) {
status = BAD_GCOUNT;
}
if (status) {
_cpl_fitsimageunit_delete(self);
return NULL;
}
_self->_m_write_layout = _cpl_fitsimageunit_write_layout;
return self;
}
inline static cpl_fitsimageunit *
_cpl_fitsimageunit_clone(const cpl_fitsimageunit *other)
{
cpl_fitsdataunit *_other = (cpl_fitsdataunit *)other;
if ((_other->_m_file == NULL) || (_other->_m_position == 0) ||
(_other->_m_type != CPL_FITS_DU_TYPE_IMAGE)) {
return NULL;
}
cpl_fitsimageunit *self = cx_malloc(sizeof *self);
cxint status = _cpl_fitsdataunit_copy((cpl_fitsdataunit *)self, _other);
if (status) {
_cpl_fitsimageunit_delete(self);
return NULL;
}
return self;
}
// FITS binary table data unit implementation
#include <fitsio.h>
#include <cxstrutils.h>
typedef struct _cpl_fitsbtableunit_ cpl_fitsbtableunit;
struct _cpl_fitsbtableunit_
{
cpl_fitsdataunit base; /* Must be the first member */
cxint _m_columns;
LONGLONG _m_rows;
cxchar **_m_ttype;
cxchar **_m_tform;
cxchar **_m_tunit;
cxchar **_m_tdisp;
cxchar **_m_tdim;
cxchar **_m_tscale;
cxchar **_m_tzero;
cxchar **_m_tnull;
cxchar *_m_tfields;
cxchar *_m_theap;
};
inline static void
_cpl_fitsbtableunit_delete(cpl_fitsbtableunit *self)
{
if (self) {
cxint i;
cx_free(self->_m_theap);
cx_free(self->_m_tfields);
for (i = 0; i < self->_m_columns; ++i) {
cx_free(self->_m_tnull[i]);
cx_free(self->_m_tzero[i]);
cx_free(self->_m_tscale[i]);
cx_free(self->_m_tdim[i]);
cx_free(self->_m_tdisp[i]);
cx_free(self->_m_tunit[i]);
cx_free(self->_m_tform[i]);
cx_free(self->_m_ttype[i]);
}
cx_free(self->_m_tnull);
cx_free(self->_m_tzero);
cx_free(self->_m_tscale);
cx_free(self->_m_tdim);
cx_free(self->_m_tdisp);
cx_free(self->_m_tunit);
cx_free(self->_m_tform);
cx_free(self->_m_ttype);
_cpl_fitsdataunit_clear((cpl_fitsdataunit *)self);
}
cx_free(self);
return;
}
static cxint
_cpl_fitsbtableunit_write_layout(cpl_fitsdataunit *self, fitsfile *file)
{
cxint status = 0;
cxint hdunum = 0;
cpl_fitsbtableunit *_self = (cpl_fitsbtableunit *)self;
fits_get_hdu_num(file, &hdunum);
if (hdunum == 1) {
return NOT_BTABLE;
}
long axis[] = { self->_m_naxis[0], self->_m_naxis[1] };
fits_write_exthdr(file, "BINTABLE", self->_m_bitpix, self->_m_naxes, axis,
self->_m_pcount, self->_m_gcount, &status);
fits_write_record(file, _self->_m_tfields, &status);
// FIXME: Instead of fixing the comments written by the function writing
// a generic extension header, one could use the function writing a complete
// binary table header. However this requires getting the fully parsed value
// of the TTYPE, TFORM and TUNIT keywords!
/*
* Fix keyword comments. Drop the generic extension keyword comments
* in favor of binary table specific ones.
*/
fits_modify_comment(file, "XTENSION", "binary table extension", &status);
fits_modify_comment(file, "BITPIX", "8-bit bytes", &status);
fits_modify_comment(file, "NAXIS", "2-dimensional binary table", &status);
fits_modify_comment(file, "NAXIS1", "width of table in bytes", &status);
fits_modify_comment(file, "NAXIS2", "number of rows in table", &status);
if (status) {
return status;
}
/*
* Write table meta data
*/
typedef cxchar **_cpl_stringarray;
_cpl_stringarray *tdata[] = {
&_self->_m_ttype, &_self->_m_tform, &_self->_m_tunit,
&_self->_m_tdisp, &_self->_m_tdim, &_self->_m_tscale,
&_self->_m_tzero, &_self->_m_tnull, 0
};
cxint i;
for (i = 0; i < _self->_m_columns; ++i) {
cxsize k = 0;
status = 0;
while (tdata[k]) {
cxchar *const *td = *tdata[k];
if ((td[i] != NULL) && (strlen(td[i]) > 0)) {
fits_write_record(file, td[i], &status);
}
++k;
}
if (status) {
return status;
}
}
if ((_self->_m_theap != NULL) && (strlen(_self->_m_theap) > 0)) {
fits_write_record(file, _self->_m_theap, &status);
}
return status;
}
inline static cpl_fitsbtableunit *
_cpl_fitsbtableunit_new(fitsfile *file, cxulong position)
{
cpl_fitsbtableunit *self = cx_malloc(sizeof *self);
cpl_fitsdataunit *_self = (cpl_fitsdataunit *)self;
cxint status = _cpl_fitsdataunit_initialize(_self, file, position);
if (status) {
cx_free(self);
return NULL;
}
if (_self->_m_type != CPL_FITS_DU_TYPE_BTBL) {
status = NOT_BTABLE;
}
if (_self->_m_bitpix != 8) {
status = BAD_BITPIX;
}
if (_self->_m_naxes != 2) {
status = BAD_NAXIS;
}
if (_self->_m_gcount != 1) {
status = BAD_GCOUNT;
}
if (status) {
_cpl_fitsbtableunit_delete(self);
return NULL;
}
_self->_m_write_layout = _cpl_fitsbtableunit_write_layout;
self->_m_ttype = NULL;
self->_m_tform = NULL;
self->_m_tunit = NULL;
self->_m_tdisp = NULL;
self->_m_tdim = NULL;
self->_m_tscale = NULL;
self->_m_tzero = NULL;
self->_m_tnull = NULL;
self->_m_tfields = NULL;
self->_m_theap = NULL;
/*
* Get the number of columns and rows in the table.
* Note that the number of columns is given by the
* TFIELDS keyword.
*/
fits_get_num_cols(_self->_m_file, &self->_m_columns, &status);
fits_get_num_rowsll(_self->_m_file, &self->_m_rows, &status);
if (status) {
_cpl_fitsbtableunit_delete(self);
return NULL;
}
if (self->_m_columns) {
self->_m_ttype = cx_malloc(self->_m_columns * sizeof(cxchar *));
self->_m_tform = cx_malloc(self->_m_columns * sizeof(cxchar *));
self->_m_tunit = cx_malloc(self->_m_columns * sizeof(cxchar *));
self->_m_tdisp = cx_malloc(self->_m_columns * sizeof(cxchar *));
self->_m_tdim = cx_malloc(self->_m_columns * sizeof(cxchar *));
self->_m_tscale = cx_malloc(self->_m_columns * sizeof(cxchar *));
self->_m_tzero = cx_malloc(self->_m_columns * sizeof(cxchar *));
self->_m_tnull = cx_malloc(self->_m_columns * sizeof(cxchar *));
cxchar card[FLEN_CARD];
/*
* Read the TFIELDS record from the file. TFIELDS is actually
* the number of columns in the table, and is already available
* as a number, but to avoid any changes in the textual
* representation we keep a copy of the card too.
*/
card[0] = '\0';
fits_read_card(_self->_m_file, "TFIELDS", card, &status);
if (status) {
_cpl_fitsbtableunit_delete(self);
return NULL;
}
self->_m_tfields = (cxchar *)cx_strdup(card);
/*
* Read the table meta data
*/
typedef cxchar **_cpl_stringarray;
struct _cpl_btable_metadata
{
const cxchar *name;
cxbool required;
_cpl_stringarray *data;
cxuint error;
};
struct _cpl_btable_metadata tdata[] = {
{ "TTYPE", FALSE, &self->_m_ttype, KEY_NO_EXIST },
{ "TFORM", TRUE, &self->_m_tform, NO_TFORM },
{ "TUNIT", FALSE, &self->_m_tunit, KEY_NO_EXIST },
{ "TDISP", FALSE, &self->_m_tdisp, KEY_NO_EXIST },
{ "TDIM", FALSE, &self->_m_tdim, KEY_NO_EXIST },
{ "TSCALE", FALSE, &self->_m_tscale, KEY_NO_EXIST },
{ "TZERO", FALSE, &self->_m_tzero, KEY_NO_EXIST },
{ "TNULL", FALSE, &self->_m_tnull, KEY_NO_EXIST },
{ 0, FALSE, 0, 0 }
};
cxint i;
for (i = 0; i < self->_m_columns; ++i) {
cxsize k = 0;
while (tdata[k].name) {
cxchar keyname[FLEN_KEYWORD] = { '\0' };
snprintf(keyname, FLEN_KEYWORD - 1, "%s%d", tdata[k].name,
i + 1);
card[0] = '\0';
fits_read_card(_self->_m_file, keyname, card, &status);
if (status) {
if (status == KEY_NO_EXIST) {
/*
* Ignore keyword not found errors unless the keyword
* is a required keyword.
*/
if (tdata[k].required) {
_cpl_fitsbtableunit_delete(self);
return NULL;
}
}
else {
_cpl_fitsbtableunit_delete(self);
return NULL;
}
}
/*
* If the status flag is not set, the keyword was found and
* the FITS card is stored. A non-zero status at this point
* indicates that an optional keyword was not found. In this
* latter case the status flag is reset and the data pointer
* is set to NULL.
*/
if (status == 0) {
(*tdata[k].data)[i] = (cxchar *)cx_strdup(card);
}
else {
status = 0;
(*tdata[k].data)[i] = NULL;
}
++k;
}
}
card[0] = '\0';
fits_read_card(_self->_m_file, "THEAP", card, &status);
if (status && (status != KEY_NO_EXIST)) {
_cpl_fitsbtableunit_delete(self);
return NULL;
}
if (status == 0) {
self->_m_theap = (cxchar *)cx_strdup(card);
}
}
return self;
}
inline static cpl_fitsbtableunit *
_cpl_fitsbtableunit_clone(const cpl_fitsbtableunit *other)
{
cpl_fitsdataunit *_other = (cpl_fitsdataunit *)other;
if ((_other->_m_file == NULL) || (_other->_m_position == 0) ||
(_other->_m_type != CPL_FITS_DU_TYPE_BTBL)) {
return NULL;
}
cpl_fitsbtableunit *self = cx_malloc(sizeof *self);
cxint status = _cpl_fitsdataunit_copy((cpl_fitsdataunit *)self, _other);
if (status) {
_cpl_fitsbtableunit_delete(self);
return NULL;
}
/*
* Copy data binary table unit data members
*/
self->_m_columns = other->_m_columns;
self->_m_rows = other->_m_rows;
if (self->_m_columns) {
self->_m_ttype = cx_malloc(other->_m_columns * sizeof(cxchar *));
self->_m_tform = cx_malloc(other->_m_columns * sizeof(cxchar *));
self->_m_tunit = cx_malloc(other->_m_columns * sizeof(cxchar *));
self->_m_tdisp = cx_malloc(other->_m_columns * sizeof(cxchar *));
self->_m_tdim = cx_malloc(other->_m_columns * sizeof(cxchar *));
self->_m_tscale = cx_malloc(other->_m_columns * sizeof(cxchar *));
self->_m_tzero = cx_malloc(other->_m_columns * sizeof(cxchar *));
self->_m_tnull = cx_malloc(other->_m_columns * sizeof(cxchar *));
cxint i;
for (i = 0; i < self->_m_columns; ++i) {
self->_m_tnull[i] = cx_strdup(other->_m_tnull[i]);
self->_m_tzero[i] = cx_strdup(other->_m_tzero[i]);
self->_m_tscale[i] = cx_strdup(other->_m_tscale[i]);
self->_m_tdim[i] = cx_strdup(other->_m_tdim[i]);
self->_m_tdisp[i] = cx_strdup(other->_m_tdisp[i]);
self->_m_tunit[i] = cx_strdup(other->_m_tunit[i]);
self->_m_tform[i] = cx_strdup(other->_m_tform[i]);
self->_m_ttype[i] = cx_strdup(other->_m_ttype[i]);
}
}
else {
self->_m_ttype = NULL;
self->_m_tform = NULL;
self->_m_tunit = NULL;
self->_m_tdisp = NULL;
self->_m_tdim = NULL;
self->_m_tscale = NULL;
self->_m_tzero = NULL;
self->_m_tnull = NULL;
}
self->_m_tfields = cx_strdup(other->_m_tfields);
self->_m_theap = cx_strdup(other->_m_theap);
return self;
}
// FITS dataset implementation
#include <fitsio.h>
static const cxchar *_CPL_FITSDATASET_IGNORE_KEYPATTERN =
"(^SIMPLE =|^XTENSION=|^BITPIX =|^NAXIS =|^NAXIS[1-9][0-9]*"
"|^PCOUNT =|^GCOUNT ="
"|^EXTEND =|^EXTNAME =|^EXTVER =|^EXTLEVEL="
"|^TFIELDS ="
"|^TTYPE[1-9][0-9]* *=|^TFORM[1-9][0-9]* *=|^TUNIT[1-9][0-9]* *="
"|^TDISP[1-9][0-9]* *=|^TDIM[1-9][0-9]* *="
"|^TSCALE[1-9][0-9]* *=|^TZERO[1-9][0-9]* *=|^TNULL[1-9][0-9]* *="
"|^THEAP ="
"|^CHECKSUM=|^DATASUM =|^DATAMD5 =|^DATE =|^END *$"
"|^ZHECKSUM=|^ZDATASUM="
"|^ORIGFILE=|^ARCHFILE="
"|^COMMENT *FITS \\(Flexible Image Transport System\\) format"
"|^COMMENT *and Astrophysics', volume 376, page 359;)";
typedef struct _cpl_fitsdataset_ cpl_fitsdataset;
struct _cpl_fitsdataset_
{
const cxchar *_m_name;
cxulong _m_version;
cxulong _m_level;
cpl_fitsheader *_m_header;
cpl_fitsdataunit *_m_data;
};
inline static cpl_fitsdataunit *
_cpl_fitsdataset_make_dataunit(fitsfile *file, cxulong position)
{
cxint status = 0;
cxint hdutype = ANY_HDU;
fits_movabs_hdu(file, position + 1, &hdutype, &status);
if (status) {
return NULL;
}
cpl_fitsdataunit *dataunit = NULL;
switch (hdutype) {
case IMAGE_HDU:
dataunit =
(cpl_fitsdataunit *)_cpl_fitsimageunit_new(file, position);
break;
case BINARY_TBL:
dataunit =
(cpl_fitsdataunit *)_cpl_fitsbtableunit_new(file, position);
break;
default:
break;
}
return dataunit;
}
inline static cpl_fitsdataunit *
_cpl_fitsdataset_clone_dataunit(const cpl_fitsdataunit *other)
{
cpl_fitsdataunit *dataunit = NULL;
switch (_cpl_fitsdataunit_get_type(other)) {
case CPL_FITS_DU_TYPE_IMAGE: {
const cpl_fitsimageunit *_other = (const cpl_fitsimageunit *)other;
dataunit = (cpl_fitsdataunit *)_cpl_fitsimageunit_clone(_other);
break;
}
case CPL_FITS_DU_TYPE_BTBL: {
const cpl_fitsbtableunit *_other =
(const cpl_fitsbtableunit *)other;
dataunit = (cpl_fitsdataunit *)_cpl_fitsbtableunit_clone(_other);
break;
}
case CPL_FITS_DU_TYPE_EMPTY: {
const cpl_fitsemptyunit *_other = (const cpl_fitsemptyunit *)other;
dataunit = (cpl_fitsdataunit *)_cpl_fitsemptyunit_clone(_other);
break;
}
default:
break;
}
return dataunit;
}
inline static void
_cpl_fitsdataset_delete(cpl_fitsdataset *self)
{
if (self->_m_name) {
cx_free((cxchar *)self->_m_name);
}
if (self->_m_header) {
_cpl_fitsheader_delete(self->_m_header);
}
if (self->_m_data) {
switch (_cpl_fitsdataunit_get_type(self->_m_data)) {
case CPL_FITS_DU_TYPE_IMAGE:
_cpl_fitsimageunit_delete((cpl_fitsimageunit *)self->_m_data);
break;
case CPL_FITS_DU_TYPE_BTBL:
_cpl_fitsbtableunit_delete((cpl_fitsbtableunit *)self->_m_data);
break;
case CPL_FITS_DU_TYPE_EMPTY:
_cpl_fitsemptyunit_delete((cpl_fitsemptyunit *)self->_m_data);
break;
default:
break;
}
}
cx_free(self);
return;
}
inline static cpl_fitsdataset *
_cpl_fitsdataset_new(fitsfile *file, cxulong position)
{
cpl_fitsdataset *self = cx_malloc(sizeof *self);
self->_m_name = NULL;
self->_m_version = 0;
self->_m_level = 0;
self->_m_data = NULL;
cpl_regex *ignore_keys =
cpl_regex_new(_CPL_FITSDATASET_IGNORE_KEYPATTERN, TRUE,
CPL_REGEX_EXTENDED | CPL_REGEX_NOSUBS);
cpl_fitsheader *hdr = _cpl_fitsheader_create_from_file(file, position);
self->_m_header = _cpl_fitsheader_create_from_filter(hdr, ignore_keys);
cpl_regex_delete(ignore_keys);
ignore_keys = NULL;
cpl_fitscard *name = _cpl_fitsheader_find(hdr, "EXTNAME");
cpl_fitscard *version = _cpl_fitsheader_find(hdr, "EXTVER");
cpl_fitscard *level = _cpl_fitsheader_find(hdr, "EXTLEVEL");
if (name) {
cx_string *_name = _cpl_fitscard_get_value(name);
cxsize start = cx_string_find_first_not_of(_name, " '");
cxsize end = cx_string_find_last_not_of(_name, " '");
cx_string *s = cx_string_substr(_name, start, end);
self->_m_name = cx_strdup(cx_string_get(s));
cx_string_delete(s);
cx_string_delete(_name);
}
if (version) {
cx_string *value = _cpl_fitscard_get_value(version);
cxchar *end;
cxulong _version = strtoul(cx_string_get(value), &end, 10);
if (*end != '\0') {
cx_string_delete(value);
_cpl_fitsheader_delete(hdr);
_cpl_fitsdataset_delete(self);
return NULL;
}
cx_string_delete(value);
self->_m_version = _version;
}
if (level) {
cx_string *value = _cpl_fitscard_get_value(level);
cxchar *end;
cxulong _level = strtoul(cx_string_get(value), &end, 10);
if (*end != '\0') {
cx_string_delete(value);
_cpl_fitsheader_delete(hdr);
_cpl_fitsdataset_delete(self);
return NULL;
}
cx_string_delete(value);
self->_m_level = _level;
}
_cpl_fitsheader_delete(hdr);
self->_m_data = _cpl_fitsdataset_make_dataunit(file, position);
return self;
}
inline static cpl_fitsdataset *
_cpl_fitsdataset_create(const cpl_fitsheader *hdr, const cpl_fitsdataunit *data)
{
cpl_fitsdataset *self = cx_malloc(sizeof *self);
self->_m_name = NULL;
self->_m_version = 0;
self->_m_level = 0;
self->_m_data = NULL;
cpl_regex *ignore_keys =
cpl_regex_new(_CPL_FITSDATASET_IGNORE_KEYPATTERN, TRUE,
CPL_REGEX_EXTENDED | CPL_REGEX_NOSUBS);
self->_m_header = _cpl_fitsheader_create_from_filter(hdr, ignore_keys);
cpl_regex_delete(ignore_keys);
ignore_keys = NULL;
self->_m_data = _cpl_fitsdataset_clone_dataunit(data);
return self;
}
inline static cpl_fitsheader *
_cpl_fitsdataset_get_header(cpl_fitsdataset *self)
{
return self->_m_header;
}
inline static const cpl_fitsheader *
_cpl_fitsdataset_get_header_const(const cpl_fitsdataset *self)
{
return self->_m_header;
}
inline static cpl_fitsdataunit *
_cpl_fitsdataset_get_data(cpl_fitsdataset *self)
{
return self->_m_data;
}
inline static const cpl_fitsdataunit *
_cpl_fitsdataset_get_data_const(const cpl_fitsdataset *self)
{
return self->_m_data;
}
inline static const cxchar *
_cpl_fitsdataset_get_name(const cpl_fitsdataset *self)
{
return self->_m_name;
}
inline static cxsize
_cpl_fitsdataset_get_version(const cpl_fitsdataset *self)
{
return self->_m_version;
}
inline static cxsize
_cpl_fitsdataset_get_level(const cpl_fitsdataset *self)
{
return self->_m_level;
}
inline static cxint
_cpl_fitsdataset_set_header(cpl_fitsdataset *self, const cpl_fitsheader *hdr)
{
cpl_regex *ignore_keys =
cpl_regex_new(_CPL_FITSDATASET_IGNORE_KEYPATTERN, TRUE,
CPL_REGEX_EXTENDED | CPL_REGEX_NOSUBS);
if (ignore_keys == NULL) {
return 1;
}
cpl_fitsheader *_hdr = _cpl_fitsheader_create_from_filter(hdr, ignore_keys);
cpl_regex_delete(ignore_keys);
ignore_keys = NULL;
if (_hdr == NULL) {
return 1;
}
if (self->_m_header) {
_cpl_fitsheader_delete(self->_m_header);
}
self->_m_header = _hdr;
return 0;
}
inline static cxint
_cpl_fitsdataset_set_id(cpl_fitsdataset *self,
const cxchar *name,
cxulong version,
cxulong level)
{
cx_assert(name != NULL);
if (self->_m_name) {
cx_free((cxchar *)self->_m_name);
}
self->_m_name = cx_strdup(name);
if (version > 0) {
self->_m_version = version;
}
if (level > 0) {
self->_m_level = level;
}
return 0;
}
inline static cxint
_cpl_fitsdataset_write(const cpl_fitsdataset *self,
fitsfile *file,
cxbool is_primary,
cxbool checksums)
{
cxint status = 0;
/*
* Append new, empty FITS HDU
*/
fits_create_hdu(file, &status);
if (status) {
return status;
}
/*
* Write FITS data unit structure
*/
self->_m_data->_m_write_layout(self->_m_data, file);
/*
* If defined, write FITS extension identifier. These keywords were
* filtered out of the header when reading it from a file, so that
* appending the keywords rather than updating it is ok.
*
* Note that the data types used in the fits_write_key() calls of the
* version and the level must match the actual type definition of the
* data members, since it is passed by pointer. To be independent of
* the actual member type an assignment to a local variable could
* be used.
*/
if ((self->_m_name != NULL) && (strlen(self->_m_name) > 0)) {
fits_write_key(file, TSTRING, "EXTNAME", (cxchar *)self->_m_name,
"FITS Extension name", &status);
}
if (self->_m_version > 0) {
cxulong version = self->_m_version;
fits_write_key(file, TULONG, "EXTVER", &version,
"FITS Extension identification", &status);
}
if (self->_m_level > 0) {
cxulong level = self->_m_level;
fits_write_key(file, TULONG, "EXTLEVEL", &level, "FITS Extension level",
&status);
}
if (status) {
return status;
}
/*
* Write templates for file creation time stamp and the checksums.
*/
if (is_primary) {
fits_write_key(file, TSTRING, (cxchar *)"DATE",
(cxchar *)"YYYY-MM-DDThh:mm:ss", NULL, &status);
}
if (checksums) {
fits_write_key(file, TSTRING, (cxchar *)"CHECKSUM",
(cxchar *)"0000000000000000",
(cxchar *)"ASCII 1's complement checksum", &status);
fits_write_key(file, TSTRING, (cxchar *)"DATASUM", (cxchar *)" 0",
(cxchar *)"", &status);
}
if (status) {
return status;
}
/*
* Reserve space for the header keyword records and write them to the
* output stream.
*/
fits_set_hdrsize(file, _cpl_fitsheader_get_size(self->_m_header), &status);
cxsize i;
for (i = 0; i < _cpl_fitsheader_get_size(self->_m_header); ++i) {
const cpl_fitscard *card =
_cpl_fitsheader_get_const(self->_m_header, i);
const cx_string *record = _cpl_fitscard_get_card(card);
fits_write_record(file, cx_string_get(record), &status);
}
if (status) {
return status;
}
/*
* Dump the data to the output stream
*/
status = _cpl_fitsdataunit_write_data(self->_m_data, file);
if (status) {
return -1;
}
/*
* Write checksums and time stamp
*/
if (is_primary) {
fits_write_date(file, &status);
fits_modify_comment(file, "DATE", "Date this file was written",
&status);
}
if (checksums) {
fits_write_chksum(file, &status);
}
return status;
}
// CPL multi-frame implementation
/**
* @defgroup cpl_multiframe Multi Frames
*
* This module implements the @c cpl_multiframe container type. A multi frame
* contains references to datasets (FITS extensions) which may be distributed
* across several physical files. These references can then be merged into
* a new product file.
*
* @par Synopsis:
* @code
* #include <cpl_multiframe.h>
* @endcode
*
* @note This feature should be considered as experimental!
*/
/**@{*/
#include <cxmap.h>
#include <cxdeque.h>
#include <cpl_error_impl.h>
#include <cpl_fits.h>
struct _cpl_multiframe_
{
cx_map *_m_files;
cx_deque *_m_datasets;
};
static cxbool
_cpl_multiframe_key_compare(cxcptr a, cxcptr b)
{
const cxchar *_a = a;
const cxchar *_b = b;
return (strcmp(_a, _b) < 0) ? TRUE : FALSE;
}
inline static cpl_error_code
_cpl_multiframe_append_dataset(cpl_multiframe *self,
const cxchar *id,
const cxchar *filename,
cxsize position,
const cpl_regex *filter1,
const cpl_regex *filter2,
cxuint flags)
{
cx_assert(self != NULL);
cx_assert(id != NULL);
cx_assert(filename != NULL);
// FIXME: Should it be allowed to append a primary dataset?
// cx_assert(position > 0);
cx_assert((flags == CPL_MULTIFRAME_ID_SET) ||
(flags == CPL_MULTIFRAME_ID_PREFIX) ||
(flags == CPL_MULTIFRAME_ID_JOIN));
cx_assert((flags != CPL_MULTIFRAME_ID_PREFIX) || (*id != '\0'));
cxint status = 0;
cx_map_iterator it = cx_map_find(self->_m_files, filename);
fitsfile *fp = NULL;
if (it != cx_map_end(self->_m_files)) {
fp = cx_map_get_value(self->_m_files, it);
}
else {
fits_open_diskfile(&fp, filename, READONLY, &status);
if (status) {
return cpl_error_set_(CPL_ERROR_ASSIGNING_STREAM);
}
}
cpl_fitsheader *hdr0 = NULL;
cpl_fitsdataset *_dataset = _cpl_fitsdataset_new(fp, 0);
if (_dataset == NULL) {
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_DATA_NOT_FOUND);
}
if (filter1) {
hdr0 = _cpl_fitsheader_create_from_filter(
_cpl_fitsdataset_get_header_const(_dataset), filter1);
}
else {
hdr0 = _cpl_fitsheader_duplicate(
_cpl_fitsdataset_get_header_const(_dataset));
}
if (hdr0 == NULL) {
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
}
cpl_fitsdataset *dataset = NULL;
if (position == 0) {
cpl_fitsheader *_hdr0 = _cpl_fitsheader_new();
dataset =
_cpl_fitsdataset_create(_hdr0,
_cpl_fitsdataset_get_data_const(_dataset));
_cpl_fitsheader_delete(_hdr0);
}
else {
dataset = _cpl_fitsdataset_new(fp, position);
}
if (dataset == NULL) {
_cpl_fitsheader_delete(hdr0);
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_DATA_NOT_FOUND);
}
/*
* If a filter for the secondary header is given apply it and create a
* new, filtered header.
*/
if (filter2) {
const cpl_fitsheader *hdr = _cpl_fitsdataset_get_header_const(dataset);
cpl_fitsheader *_hdr = _cpl_fitsheader_create_from_filter(hdr, filter2);
if (_hdr == NULL) {
_cpl_fitsdataset_delete(dataset);
_cpl_fitsheader_delete(hdr0);
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
}
status = _cpl_fitsdataset_set_header(dataset, _hdr);
_cpl_fitsheader_delete(_hdr);
_hdr = NULL;
if (status) {
_cpl_fitsdataset_delete(dataset);
_cpl_fitsheader_delete(hdr0);
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
}
}
/*
* If the primary header is not empty, create the new dataset header by
* joining the primary and the auxiliary headers.
*/
if ((hdr0 != NULL) && (_cpl_fitsheader_get_size(hdr0) > 0)) {
cpl_fitsheader *_hdr = _cpl_fitsdataset_get_header(dataset);
_cpl_fitsheader_join(_hdr, hdr0);
_cpl_fitsheader_sort(_hdr, _cpl_fitscard_compare_dicb);
}
_cpl_fitsheader_delete(hdr0);
hdr0 = NULL;
/*
* Construct and assign the new dataset id
*/
const cxchar *_name = _cpl_fitsdataset_get_name(dataset);
cx_string *_id = 0;
switch (flags) {
case CPL_MULTIFRAME_ID_SET: {
_id = cx_string_create(id);
break;
}
case CPL_MULTIFRAME_ID_PREFIX: {
_id = cx_string_create(id);
cx_string_append(_id, _name);
break;
}
case CPL_MULTIFRAME_ID_JOIN: {
const cxchar *_prefix = _cpl_fitsdataset_get_name(_dataset);
if ((_prefix == NULL) || (strlen(_prefix) == 0)) {
_cpl_fitsdataset_delete(dataset);
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
_id = cx_string_create(_prefix);
if (_name && (*_name != '\0')) {
cx_string_append(_id, id);
cx_string_append(_id, _name);
}
break;
}
default: {
/* This point should never be reached */
_cpl_fitsdataset_delete(dataset);
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_UNSUPPORTED_MODE);
break;
}
}
_cpl_fitsdataset_delete(_dataset);
_dataset = NULL;
// FIXME: The following call does not handle the dataset version and level.
// Instead the dataset should be searched for the id, and if
// it is already present, the dataset version should be set
// to one more than the maximum version which is already present.
status = _cpl_fitsdataset_set_id(dataset, cx_string_get(_id), 0, 0);
cx_string_delete(_id);
_id = NULL;
if (status) {
_cpl_fitsdataset_delete(dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
}
/*
* Register the dataset, and the associated fitsfile handle if it has not
* been registered before.
*/
cx_deque_push_back(self->_m_datasets, dataset);
if (it == cx_map_end(self->_m_files)) {
cx_map_insert(self->_m_files, cx_strdup(filename), fp);
}
return CPL_ERROR_NONE;
}
inline static cpl_fitsdataset *
_cpl_multiframe_merge(fitsfile *fp,
cxsize position,
const cpl_fitsheader *hdr,
const cpl_regex *filter1,
const cpl_regex *filter2)
{
cx_assert(fp != NULL);
cx_assert(hdr != NULL);
// FIXME: Should it be allowed to append a primary dataset?
cx_assert(position > 0);
cxint status = 0;
cpl_fitsheader *hdr0 = NULL;
if (filter1) {
hdr0 = _cpl_fitsheader_create_from_filter(hdr, filter1);
}
else {
hdr0 = _cpl_fitsheader_duplicate(hdr);
}
if (hdr0 == NULL) {
cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
return NULL;
}
cpl_fitsdataset *dataset = _cpl_fitsdataset_new(fp, position);
if (dataset == NULL) {
_cpl_fitsheader_delete(hdr0);
cpl_error_set_(CPL_ERROR_DATA_NOT_FOUND);
return NULL;
}
/*
* If a filter for the secondary header is given apply it and create a
* new, filtered header.
*/
if (filter2) {
const cpl_fitsheader *hdr1 = _cpl_fitsdataset_get_header_const(dataset);
cpl_fitsheader *_hdr =
_cpl_fitsheader_create_from_filter(hdr1, filter2);
if (_hdr == NULL) {
_cpl_fitsdataset_delete(dataset);
_cpl_fitsheader_delete(hdr0);
cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
return NULL;
}
status = _cpl_fitsdataset_set_header(dataset, _hdr);
_cpl_fitsheader_delete(_hdr);
_hdr = NULL;
if (status) {
_cpl_fitsdataset_delete(dataset);
_cpl_fitsheader_delete(hdr0);
cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
return NULL;
}
}
/*
* If the primary header is not empty, create the new dataset header by
* joining the primary and the auxiliary headers.
*/
if ((hdr0 != NULL) && (_cpl_fitsheader_get_size(hdr0) > 0)) {
cpl_fitsheader *_hdr = _cpl_fitsdataset_get_header(dataset);
_cpl_fitsheader_join(_hdr, hdr0);
_cpl_fitsheader_sort(_hdr, _cpl_fitscard_compare_dicb);
}
_cpl_fitsheader_delete(hdr0);
return dataset;
}
inline static cpl_error_code
_cpl_multiframe_append_datagroup(cpl_multiframe *self,
const cxchar *id,
const cxchar *filename,
cxsize nsets,
cpl_size *positions,
const cpl_regex **filter1,
const cpl_regex **filter2,
const cxchar **links,
cxint flags)
{
cx_assert(self != NULL);
cx_assert(id != NULL);
cx_assert(filename != NULL);
cx_assert(positions != NULL);
cx_assert(nsets > 1);
cx_assert((flags == CPL_MULTIFRAME_ID_PREFIX) ||
(flags == CPL_MULTIFRAME_ID_JOIN));
cx_assert((flags != CPL_MULTIFRAME_ID_PREFIX) || (*id != '\0'));
cxsize is;
for (is = 0; is < nsets; ++is) {
if (positions[is] < 1) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
}
cxint status = 0;
cx_map_iterator it = cx_map_find(self->_m_files, filename);
fitsfile *fp = NULL;
if (it != cx_map_end(self->_m_files)) {
fp = cx_map_get_value(self->_m_files, it);
}
else {
fits_open_diskfile(&fp, filename, READONLY, &status);
if (status) {
return cpl_error_set_(CPL_ERROR_ASSIGNING_STREAM);
}
}
cpl_fitsdataset *_dataset = _cpl_fitsdataset_new(fp, 0);
if (_dataset == NULL) {
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_DATA_NOT_FOUND);
}
const cxchar *name0 = _cpl_fitsdataset_get_name(_dataset);
if ((flags == CPL_MULTIFRAME_ID_JOIN) &&
((name0 == NULL) || (*name0 == '\0'))) {
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
const cpl_fitsheader *hdr0 = _cpl_fitsdataset_get_header_const(_dataset);
for (is = 0; is < nsets; ++is) {
const cpl_regex *_filter1 = (filter1 != NULL) ? filter1[is] : NULL;
const cpl_regex *_filter2 = (filter2 != NULL) ? filter2[is] : NULL;
cpl_fitsdataset *dataset =
_cpl_multiframe_merge(fp, positions[is], hdr0, _filter1, _filter2);
if (dataset == NULL) {
cpl_error_code _status = cpl_error_get_code();
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
cpl_error_set_where(cpl_func);
return _status;
}
/*
* Create and set the new dataset id
*/
cxchar *name = cx_strdup(_cpl_fitsdataset_get_name(dataset));
cx_string *_id;
if (flags == CPL_MULTIFRAME_ID_JOIN) {
_id = cx_string_create(name0);
if (name && (*name != '\0')) {
cx_string_append(_id, id);
cx_string_append(_id, name);
}
}
else {
_id = cx_string_create(id);
cx_string_append(_id, name);
}
// FIXME: The following call does not handle the dataset version and
// level. Instead the dataset should be searched for the id, and if it
// is already present, the dataset version should be setto one more than
// the maximum version which is already present.
status = _cpl_fitsdataset_set_id(dataset, cx_string_get(_id), 0, 0);
cx_string_delete(_id);
_id = NULL;
if (status) {
cx_free(name);
_cpl_fitsdataset_delete(dataset);
_cpl_fitsdataset_delete(_dataset);
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
}
/*
* Update dataset links
*/
if (links) {
cpl_fitsheader *hdr = _cpl_fitsdataset_get_header(dataset);
const cxchar **_links = links;
const cxchar *lnkname;
while ((lnkname = *_links)) {
cpl_fitscard *card = _cpl_fitsheader_find(hdr, lnkname);
if (card) {
cx_string *value = _cpl_fitscard_get_value(card);
cx_string *comment = _cpl_fitscard_get_comment(card);
cxsize first = cx_string_find_first_not_of(value, "'");
cxsize last = cx_string_find_last_not_of(value, "' ");
cx_string *_value =
cx_string_substr(value, first, last - first + 1);
cx_string_delete(value);
value = cx_string_create("'");
if (flags == CPL_MULTIFRAME_ID_JOIN) {
cx_string_append(value, name0);
if (name && (*name != '\0')) {
cx_string_append(value, id);
cx_string_append(value, cx_string_get(_value));
}
}
else {
cx_string_append(value, id);
cx_string_append(value, cx_string_get(_value));
}
cx_string_append(value, "'");
_cpl_fitscard_set_value(card, cx_string_get(value),
cx_string_get(comment));
cx_string_delete(_value);
cx_string_delete(comment);
cx_string_delete(value);
}
++_links;
}
}
cx_free(name);
/*
* Register the dataset.
*/
cx_deque_push_back(self->_m_datasets, dataset);
}
_cpl_fitsdataset_delete(_dataset);
/*
* Register the datagroup's fitsfile handle if it has not been registered
* before.
*/
if (it == cx_map_end(self->_m_files)) {
cx_map_insert(self->_m_files, cx_strdup(filename), fp);
}
return CPL_ERROR_NONE;
}
/**
* @brief
* Create a new multi-frame container object.
*
* @param head The first dataset of the multi-frame object
* @param id Unique dataset identifier.
* @param filter Filter to be applied to the dataset properties on merge.
*
* @return
* The function returns a newly allocated multi-frame object on success, or
* @c NULL otherwise.
*
* The function allocates the memory for a multi-frame container and adds the
* frame @em head as the head, i.e. the first and emtpy dataset of the
* multi-frame object. A unique dataset identifier @em id may be given. The
* identifier @em id may be the empty string, in which case it is ignored when
* writing the multi-frame object to a file. Furthermore a regular expression
* filter object may be given which will be applied to each of the properties
* of the dataset @em head. Only those properties of @em head, which pass the
* filter @em filter will be propagated to the created multi-frame container.
*/
cpl_multiframe *
cpl_multiframe_new(const cpl_frame *head, const char *id, cpl_regex *filter)
{
if ((head == NULL) || (id == NULL)) {
cpl_error_set_(CPL_ERROR_NULL_INPUT);
return NULL;
}
cpl_multiframe *self = cx_malloc(sizeof *self);
self->_m_files = cx_map_new(_cpl_multiframe_key_compare, cx_free, NULL);
self->_m_datasets = cx_deque_new();
const cxchar *filename = cpl_frame_get_filename(head);
if (filename == NULL) {
cpl_multiframe_delete(self);
return NULL;
}
cxint status = 0;
fitsfile *fp;
fits_open_diskfile(&fp, filename, READONLY, &status);
if (status) {
cpl_multiframe_delete(self);
return NULL;
}
cpl_fitsdataset *dataset = _cpl_fitsdataset_new(fp, 0);
if (dataset == NULL) {
fits_close_file(fp, &status);
cpl_multiframe_delete(self);
return NULL;
}
/*
* Remove keywords which should not appear in an empty HDU
*/
const cpl_fitsheader *hdr = _cpl_fitsdataset_get_header_const(dataset);
const cxchar *ignore_key_pattern = "(^BSCALE =|^BZERO =|^BUNIT ="
"|^BLANK =|^DATAMIN =|^DATAMAX =)";
cpl_regex *ignore_keys =
cpl_regex_new(ignore_key_pattern, TRUE,
CPL_REGEX_EXTENDED | CPL_REGEX_NOSUBS);
cpl_fitsheader *_hdr = _cpl_fitsheader_create_from_filter(hdr, ignore_keys);
cpl_regex_delete(ignore_keys);
if (_hdr == NULL) {
_cpl_fitsdataset_delete(dataset);
fits_close_file(fp, &status);
cpl_multiframe_delete(self);
return NULL;
}
/*
* Apply given filter to the pre-processed header
*/
if (filter) {
cpl_fitsheader *hdr0 = _cpl_fitsheader_create_from_filter(_hdr, filter);
if (hdr0 == NULL) {
_cpl_fitsdataset_delete(dataset);
fits_close_file(fp, &status);
cpl_multiframe_delete(self);
return NULL;
}
_cpl_fitsheader_delete(_hdr);
_hdr = hdr0;
}
_cpl_fitsdataset_delete(dataset);
/*
* Create a new empty dataset
*/
cpl_fitsemptyunit *empty = _cpl_fitsemptyunit_new();
dataset = _cpl_fitsdataset_create(_hdr, (cpl_fitsdataunit *)empty);
// status = _cpl_fitsdataset_set_header(dataset, _hdr);
_cpl_fitsemptyunit_delete(empty);
empty = NULL;
_cpl_fitsheader_delete(_hdr);
_hdr = NULL;
// if (status) {
if (!dataset) {
fits_close_file(fp, &status);
cpl_multiframe_delete(self);
return NULL;
}
status = _cpl_fitsdataset_set_id(dataset, id, 0, 0);
if (status) {
_cpl_fitsdataset_delete(dataset);
fits_close_file(fp, &status);
cpl_multiframe_delete(self);
return NULL;
}
/*
* Register dataset
*/
cx_deque_push_back(self->_m_datasets, dataset);
cx_map_insert(self->_m_files, cx_strdup(cpl_frame_get_filename(head)), fp);
return self;
}
/**
* @brief
* Destroys a multi-frame container object
*
* @param self The multi-frame container.
*
* @return
* Nothing.
*
* The function deallocates the multi-frame container @em self.
*/
void
cpl_multiframe_delete(cpl_multiframe *self)
{
if (self != NULL) {
if (self->_m_datasets != NULL) {
cx_deque_destroy(self->_m_datasets,
(cx_free_func)_cpl_fitsdataset_delete);
}
if (self->_m_files) {
cxint status = 0;
cx_map_iterator it = cx_map_begin(self->_m_files);
while (it != cx_map_end(self->_m_files)) {
fits_close_file(cx_map_get_value(self->_m_files, it), &status);
status = 0;
it = cx_map_next(self->_m_files, it);
}
cx_map_delete(self->_m_files);
}
cx_free(self);
}
return;
}
/**
* @brief
* Get the size of a multi-frame container object.
*
* @param self The multi-frame object.
*
* @return
* The function returns the current number of datasets referenced by
* the multi-frame container.
*
* The function returns the number of dataset entries stored in the
* multi-frame @em self.
*/
cpl_size
cpl_multiframe_get_size(const cpl_multiframe *self)
{
if (self == NULL) {
cpl_error_set_(CPL_ERROR_NULL_INPUT);
return 0L;
}
return (cpl_size)cx_deque_size(self->_m_datasets);
}
/**
* @brief
* Get the unique dataset identifier of a dataset in a multi-frame container
* given by its position.
*
* @param self The multi-frame object.
* @param position Position of the dataset in the multi-frame container.
*
* @return
* The function returns the unique identifier of the dataset on success.
* On error the function returns @c NULL and sets an appropriate error code.
*
* @error
* <table class="ec" align="center">
* <tr>
* <td class="ecl">CPL_ERROR_NULL_INPUT</td>
* <td class="ecr">
* The given multi-frame container pointer is a @c NULL pointer.
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_ILLEGAL_INPUT</td>
* <td class="ecr">
* The given dataset positions is invalid (out of bounds).
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_DATA_NOT_FOUND</td>
* <td class="ecr">
* The multi-frame container itself is empty.
* </td>
* </tr>
* </table>
* @enderror
*
* The function retrieves the unique identifier of the dataset found at
* position @em position in the multi-frame container @em self.
*/
const char *
cpl_multiframe_dataset_get_id(const cpl_multiframe *self, cpl_size position)
{
cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
cxsize size = cx_deque_size(self->_m_datasets);
cpl_ensure(size > 0, CPL_ERROR_DATA_NOT_FOUND, NULL);
cpl_ensure((position >= 0) && ((cxsize)position < size),
CPL_ERROR_ILLEGAL_INPUT, NULL);
const cpl_fitsdataset *dataset = cx_deque_get(self->_m_datasets, position);
const cxchar *name = _cpl_fitsdataset_get_name(dataset);
cx_assert(name != NULL);
return name;
/*
const cpl_fitsheader *header = _cpl_fitsdataset_get_header_const(dataset);
if (_cpl_fitsheader_get_size(header) == 0) {
cpl_error_set_message_(CPL_ERROR_DATA_NOT_FOUND,
"Empty header encountered");
return NULL;
}
const cpl_fitscard *card = _cpl_fitsheader_find_const(header, "EXTNAME");
if (card == NULL) {
cpl_error_set_message_(CPL_ERROR_DATA_NOT_FOUND,
"Keyword `EXTNAME` not found");
return NULL;
}
return cx_string_get(_cpl_fitscard_get_value(card));
*/
}
/**
* @brief
* Get the position of a dataset in a multi-frame container
* given its unique identifier.
*
* @param self The multi-frame object.
* @param id Unique id of the dataset in the multi-frame container.
*
* @return
* The function returns the position of the dataset on success. If an
* error occurs the function returns @c 0 and sets an appropriate error
* code.
*
* @error
* <table class="ec" align="center">
* <tr>
* <td class="ecl">CPL_ERROR_NULL_INPUT</td>
* <td class="ecr">
* The given multi-frame container pointer is a @c NULL pointer.
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_DATA_NOT_FOUND</td>
* <td class="ecr">
* Either the multi-frame container is empty, or the given dataset
* id cannot be found.
* </td>
* </tr>
* </table>
* @enderror
*
* Given the unique identifier @em id of the dataset, the function locates
* the dataset in the multi-frame container @em self and returns its position.
*/
cpl_size
cpl_multiframe_dataset_get_position(const cpl_multiframe *self, const char *id)
{
cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, 0);
cxsize size = cx_deque_size(self->_m_datasets);
cpl_ensure(size > 0, CPL_ERROR_DATA_NOT_FOUND, 0);
/*
* For each dataset in the containser check whether the dataset header
* contain the identifier record (EXTNAME). Empty headers, or headers
* without an identifier record are not an error, so if that happens
* just continue with the next dataset until the end of the container
* is reached.
*/
cxsize pos = 0;
cx_deque_const_iterator it = cx_deque_begin(self->_m_datasets);
while (it != cx_deque_end(self->_m_datasets)) {
const cpl_fitsdataset *dataset = cx_deque_get(self->_m_datasets, it);
const cxchar *name = _cpl_fitsdataset_get_name(dataset);
cx_assert(name != NULL);
if (strncmp(name, id, strlen(name)) == 0) {
break;
}
/*
const cpl_fitsheader *header =
_cpl_fitsdataset_get_header_const(dataset);
if (_cpl_fitsheader_get_size(header) != 0) {
const cpl_fitscard *card =
_cpl_fitsheader_find_const(header, "EXTNAME");
if (card != NULL) {
cx_string *value = _cpl_fitscard_get_value(card);
if (strncmp(cx_string_get(value), id, cx_string_size(value)) ==
0) {
break;
}
}
}
*/
++pos;
it = cx_deque_next(self->_m_datasets, it);
}
if (pos >= size) {
cpl_error_set_message_(CPL_ERROR_DATA_NOT_FOUND,
"Dataset identifier `%s` not found", id);
return 0;
}
return pos;
}
/**
* @brief
* Update the metadata of a dataset in a multi-frame container given by
* its position.
*
* @param self The multi-frame object.
* @param position Position of the dataset in the multi-frame container.
* @param properties The list of properties to add or modify.
*
* @return The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* @error
* <table class="ec" align="center">
* <tr>
* <td class="ecl">CPL_ERROR_NULL_INPUT</td>
* <td class="ecr">
* The given multi-frame container pointer or the property list is
* a @c NULL pointer.
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_ILLEGAL_INPUT</td>
* <td class="ecr">
* The requested data set position is out of range
* list is a @c NULL pointer, or the property list
* contains entries which cannot be parsed into
* a valid FITS record.
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_INVALID_TYPE</td>
* <td class="ecr">
* The data type of a property to be modified in the data set
* cannot be determined.
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_TYPE_MISMATCH</td>
* <td class="ecr">
* The data type of a property in the data set does not match the
* data type it has in the input property list.
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_ILLEGAL_OUTPUT</td>
* <td class="ecr">
* The metadata of the data set could not be updated with the
* properties in the property list.
* </td>
* </tr>
* </table>
* @enderror
*
* The function updates the header of the dataset found at position
* @em position in the multi-frame container @em self with the properties
* of the property list @em properties. If a property is present in
* @em properties but not in the header of the data set it is appended
* to the data set header. If a property is already present in the
* data set header its value and comment will be replaced by the value
* and comment the property has in @em properties, provided that the
* both properties have the same data type.
*/
cpl_error_code
cpl_multiframe_dataset_properties_update(cpl_multiframe *self,
cpl_size position,
const cpl_propertylist *properties)
{
cpl_ensure_code((self != NULL) && (properties != NULL),
CPL_ERROR_NULL_INPUT);
cxsize size = cx_deque_size(self->_m_datasets);
cpl_ensure_code((position >= 0) && ((cxsize)position < size),
CPL_ERROR_ILLEGAL_INPUT);
if (!cpl_propertylist_is_empty(properties)) {
cpl_fitsdataset *dataset = cx_deque_get(self->_m_datasets, position);
cpl_fitsheader *header = _cpl_fitsdataset_get_header(dataset);
cpl_size n_properties = cpl_propertylist_get_size(properties);
cpl_size i_properties;
for (i_properties = 0; i_properties < n_properties; ++i_properties) {
const cpl_property *property =
cpl_propertylist_get_const(properties, i_properties);
const char *keyname = cpl_property_get_name(property);
/*
* Convert the property value to its string representation
*/
cx_string *value = cx_string_new();
cxint status = _cpl_fits_parse_property_value(value, property);
if (status) {
cx_string_delete(value);
return cpl_error_set_message(
cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Cannot parse value of property `%s`", keyname);
}
cpl_fitscard *card = _cpl_fitsheader_find(header, keyname);
const char *comment = cpl_property_get_comment(property);
status = 0;
if (card != NULL) {
/*
* Verify that the value type of the target card to update
* matches the value type of the property to be written to
* the card. This validation is permissive in the sense that
* different integer types are considered as equal, and the
* same applies to different precision floating-point types
*/
cxint is_type =
_cpl_fitscard_is_type(card,
cpl_property_get_type(property));
if (is_type == -1) {
cpl_error_code _status = cpl_error_set_message(
cpl_func, CPL_ERROR_INVALID_TYPE,
"Cannot determine the value type of card `%s`",
keyname);
cx_string_delete(value);
return _status;
}
if (is_type == 0) {
cpl_error_code _status = cpl_error_set_message(
cpl_func, CPL_ERROR_TYPE_MISMATCH,
"Cannot update card `%s`, value type of card does "
"not match property type",
keyname);
cx_string_delete(value);
return _status;
}
else {
status = _cpl_fitscard_set_value(card, cx_string_get(value),
comment);
}
}
else {
cx_string *_keyname = cx_string_new();
status = _cpl_fits_parse_keyname(_keyname, keyname);
if (status) {
cx_string_delete(_keyname);
cx_string_delete(value);
return cpl_error_set_message(
cpl_func, CPL_ERROR_ILLEGAL_INPUT,
"Cannot parse name of property `%s`", keyname);
}
cx_string *record = cx_string_new();
status = _cpl_fits_format_card(record, cx_string_get(_keyname),
cx_string_get(value), comment);
if (!status) {
card = _cpl_fitscard_new(cx_string_get(record));
status = _cpl_fitsheader_append(header, card);
}
cx_string_delete(record);
cx_string_delete(_keyname);
}
cx_string_delete(value);
if (status) {
return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
"Cannot update dataset header");
}
}
}
return CPL_ERROR_NONE;
}
/*
* @brief
* Update header entries of a dataset in a multi-frame container given by
* its dataset id.
*
* @param self The multi-frame object.
* @param id Unique id of the dataset in the multi-frame container.
* @param properties The list of properties to add or modify.
*
* @return The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* @error
* <table class="ec" align="center">
* <tr>
* <td class="ecl">CPL_ERROR_NULL_INPUT</td>
* <td class="ecr">
* Any of the given multi-frame container pointer, the dataset id, or the
* property list is a @c NULL pointer.
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_DATA_NOT_FOUND</td>
* <td class="ecr">
* A dataset with the given identifier was not found in the multi-frame
* container.
* </td>
* </tr>
* </table>
* @enderror
*
* The function searches the multi-frame container @em self for a dataset with
* the identifier @em id and updates the dataset header with the properties in
* @em properties. If a property is already present in the header of the
* dataset its value and comment are replaced by the value and the comment of
* the corresponding property in @em properties. If the property is not present
* in the header of the dataset it is appended to the dataset header.
*
* In addition to the error codes listed previously, the function may also return
* the error codes listed for cpl_multiframe_dataset_properties_update().
*
* @see cpl_multiframe_dataset_properties_update()
*/
cpl_error_code
cpl_multiframe_dataset_properties_update_id(cpl_multiframe *self,
const char *id,
const cpl_propertylist *properties)
{
cpl_ensure_code((self != NULL) && (id != NULL) && (properties != NULL),
CPL_ERROR_NULL_INPUT);
cpl_error_code status = CPL_ERROR_NONE;
if (!cpl_propertylist_is_empty(properties)) {
cpl_error_reset();
cpl_size position = cpl_multiframe_dataset_get_position(self, id);
if (cpl_error_get_code() == CPL_ERROR_DATA_NOT_FOUND) {
return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
"dataset `%s` not found", id);
}
status = cpl_multiframe_dataset_properties_update(self, position,
properties);
}
return status;
}
/**
* @brief
* Remove header entries of a dataset in a multi-frame container, given
* its position.
*
* @param self The multi-frame object.
* @param position Position of the dataset in the multi-frame container.
* @param properties The list of properties to remove.
*
* @return The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* @error
* <table class="ec" align="center">
* <tr>
* <td class="ecl">CPL_ERROR_NULL_INPUT</td>
* <td class="ecr">
* The given multi-frame container pointer, the property list or both
* are a @c NULL pointer.
* </td>
* </tr>
* <tr>
* <td class="ecl">CPL_ERROR_ILLEGAL_INPUT</td>
* <td class="ecr">
* The given position of a dataset in the multi-frame container is out
* of bounds.
* </td>
* </tr>
* </table>
* @enderror
*
* The function searches the header of the dataset at position @em position
* in the multi-frame container @em self for each property in @em properties.
* If a property is present in the header of the given dataset it is removed.
* It is not an error if a property which is present in @em properties is is
* not found in the header of the dataset. It is also not an error if
* @em properties is empty. Note that only the name of the properties in
* @em properties is taken into account when removing properties. Any mismatch
* in the property type or the property value is ignored.
*/
cpl_error_code
cpl_multiframe_dataset_properties_remove(cpl_multiframe *self,
cpl_size position,
const cpl_propertylist *properties)
{
cpl_ensure_code((self != NULL) && (properties != NULL),
CPL_ERROR_NULL_INPUT);
cxsize size = cx_deque_size(self->_m_datasets);
cpl_ensure_code((position >= 0) && ((cxsize)position < size),
CPL_ERROR_ILLEGAL_INPUT);
if (!cpl_propertylist_is_empty(properties)) {
cpl_fitsdataset *dataset = cx_deque_get(self->_m_datasets, position);
cpl_fitsheader *header = _cpl_fitsdataset_get_header(dataset);
cpl_size n_properties = cpl_propertylist_get_size(properties);
cpl_size i_properties;
for (i_properties = 0; i_properties < n_properties; ++i_properties) {
const cpl_property *property =
cpl_propertylist_get_const(properties, i_properties);
const char *keyname = cpl_property_get_name(property);
cxsize _position = _cpl_fitsheader_get_position(header, keyname);
if (_position < _cpl_fitsheader_get_size(header)) {
cpl_fitscard *card = _cpl_fitsheader_remove(header, _position);
_cpl_fitscard_delete(card);
}
}
}
return CPL_ERROR_NONE;
}
/**
* @brief
* Adds a dataset reference given by position to a multi-frame container
* object.
*
* @param self The multi-frame object.
* @param id Unique dataset identifier.
* @param frame The source data frame from which the dataset is taken.
* @param position Position of the source dataset in the source data frame.
* @param filter1 Property filter to apply to the primary header of the
* source dataset.
* @param filter2 Property filter to apply to the extension header of the
* source dataset.
* @param flags Flag controlling the creation of the dataset's target id.
*
* @return
* The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* The function adds a new dataset entry to the multi-frame @em self. The
* dataset to add is the taken from position @em position of the source data
* frame @em frame. Before the selected dataset is added to the multi-frame
* @em self, the dataset's primary and supplementary properties are merged.
* If the filter arguments are given, i.e. @em filter1 and/or @em filter2
* are non @c NULL, they are applied to the primary and the supplementary
* properties before both are merged.
*
* The creation of the dataset's target id is controlled by the argument
* @em flags. It can be set to one of the values defined by the enumeration
* cpl_multiframe_id_mode. If @em flags is set to @c CPL_MULTIFRAME_ID_SET,
* the argument @em id is used as dataset identifier. If @em flags is set to
* @c CPL_MULTIFRAME_ID_PREFIX then @em id is used as prefix for the
* dataset's original name (extension name). If the dataset to be appended
* does not have a name, @em id is used as the full dataset identifier. If
* @em flags is set to @c CPL_MULTIFRAME_ID_JOIN, the dataset's identifier
* is created by concatenating the dataset name found in the primary
* properties, and the dataset name taken from the supplementary properties,
* using @em id as separator string. If no dataset name is found in the
* supplementary properties, only the dataset name found in the primary
* properties is used as identifier and the given separator is not appended.
* Note that for this last method it is an error if there is no dataset name
* present in the primary properties of the source dataset.
*
* The argument @em id may be the empty string for the methods
* @c CPL_MULTIFRAME_ID_SET and @c CPL_MULTIFRAME_ID_JOIN. For the method
* @c CPL_MULTIFRAME_ID_PREFIX this is an error.
*/
cpl_error_code
cpl_multiframe_append_dataset_from_position(cpl_multiframe *self,
const char *id,
const cpl_frame *frame,
cpl_size position,
const cpl_regex *filter1,
const cpl_regex *filter2,
unsigned int flags)
{
if ((self == NULL) || (id == NULL) || (frame == NULL)) {
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
}
if (position < 0) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
if ((flags != CPL_MULTIFRAME_ID_SET) &&
(flags != CPL_MULTIFRAME_ID_PREFIX) &&
(flags != CPL_MULTIFRAME_ID_JOIN)) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
if ((flags == CPL_MULTIFRAME_ID_PREFIX) && (*id == '\0')) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
const cxchar *filename = cpl_frame_get_filename(frame);
if (filename == NULL) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
cxint status = _cpl_multiframe_append_dataset(self, id, filename, position,
filter1, filter2, flags);
if (status) {
cpl_error_set_where(cpl_func);
return status;
}
return CPL_ERROR_NONE;
}
/**
* @brief
* Adds a dataset reference given by name to a multi-frame container
* object.
*
* @param self The multi-frame object.
* @param id Unique dataset identifier.
* @param frame The source data frame from which the dataset is taken.
* @param name Name of the source dataset in the source data frame.
* @param filter1 Property filter to apply to the primary header of the
* source dataset.
* @param filter2 Property filter to apply to the extension header of the
* source dataset.
* @param flags Flag controlling the creation of the dataset's target id.
*
* @return
* The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* The function adds a new dataset entry to the multi-frame @em self. The
* dataset to add is looked up in the source data frame @em frame using its
* name @em name. It is an error if no dataset with the given name
* @em name is found. Before the selected dataset is added to the
* multi-frame
* @em self, the dataset's primary and supplementary properties are merged.
* If the filter arguments are given, i.e. @em filter1 and/or @em filter2
* are non @c NULL, they are applied to the primary and the supplementary
* properties before both are merged.
*
* The creation of the dataset's target id is controlled by the argument
* @em flags. It can be set to one of the values defined by the enumeration
* cpl_multiframe_id_mode. If @em flags is set to @c CPL_MULTIFRAME_ID_SET,
* the argument @em id is used as dataset identifier. If @em flags is set to
* @c CPL_MULTIFRAME_ID_PREFIX then @em id is used as prefix for the
* dataset's original name (extension name). If the dataset to be appended
* does not have a name, @em id is used as the full dataset identifier. If
* @em flags is set to @c CPL_MULTIFRAME_ID_JOIN, the dataset's identifier
* is created by concatenating the dataset name found in the primary
* properties, and the dataset name taken from the supplementary properties,
* using @em id as separator string. If no dataset name is found in the
* supplementary properties, only the dataset name found in the primary
* properties is used as identifier and the given separator is not appended.
* Note that for this last method it is an error if there is no dataset name
* present in the primary properties of the source dataset.
*
* The argument @em id may be the empty string for the methods
* @c CPL_MULTIFRAME_ID_SET and @c CPL_MULTIFRAME_ID_JOIN. For the method
* @c CPL_MULTIFRAME_ID_PREFIX this is an error.
*/
cpl_error_code
cpl_multiframe_append_dataset(cpl_multiframe *self,
const char *id,
const cpl_frame *frame,
const char *name,
const cpl_regex *filter1,
const cpl_regex *filter2,
unsigned int flags)
{
if ((self == NULL) || (id == NULL) || (frame == NULL) || (name == NULL)) {
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
}
if ((flags != CPL_MULTIFRAME_ID_SET) &&
(flags != CPL_MULTIFRAME_ID_PREFIX) &&
(flags != CPL_MULTIFRAME_ID_JOIN)) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
if ((flags == CPL_MULTIFRAME_ID_PREFIX) && (*id == '\0')) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
const cxchar *filename = cpl_frame_get_filename(frame);
if (filename == NULL) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
cxsize position = 0;
if (strlen(name) > 0) {
cxssize _position = _cpl_fits_find_extension(filename, name, 0);
if (_position < 0) {
return cpl_error_set_(CPL_ERROR_DATA_NOT_FOUND);
}
position = _position;
}
cxint status = _cpl_multiframe_append_dataset(self, id, filename, position,
filter1, filter2, flags);
if (status) {
cpl_error_set_where(cpl_func);
return status;
}
return CPL_ERROR_NONE;
}
/**
* @brief
* Adds a group of dataset references given by position to a multi-frame
* container object.
*
* @param self The multi-frame object.
* @param id Unique dataset identifier.
* @param frame The source data frame from which the datasets are
* taken.
* @param nsets The number of datasets to be merged.
* @param positions Positions of the source datasets in the source data
* frame.
* @param filter1 Property filters to apply to the primary header of each
* source dataset.
* @param filter2 Property filters to apply to the extension header of
* each source dataset.
* @param properties Property names to be updated.
* @param flags Flag controlling the creation of the dataset's target
* id.
*
* @return
* The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* The function adds @em nsets new dataset entry to the multi-frame @em
* self. The datasets to add are taken from the source data frame @em frame
* and are specified by the first @em nsets positions passed through the
* array
* @em positions. Before each selected dataset is added to the multi-frame
* @em self, the dataset's primary and supplementary properties are merged.
* If the filter arguments are given, i.e. the respective entries in
* @em filter1 and/or @em filter2 are non @c NULL, they are applied to the
* primary and the supplementary properties before both are merged. The
* arrays
* @em filter1 and @em filter2 must be given, and they must have @em nsets
* elements. The array elements, i.e. an individual filter may be set to
* @c NULL if no filter should be applied.
*
* The creation of the dataset's target id is controlled by the argument
* @em flags. It can be only set to @c CPL_MULTIFRAME_ID_PREFIX or
* @c CPL_MULTIFRAME_ID_JOIN. If @em flags is set to @c
* CPL_MULTIFRAME_ID_PREFIX then @em id is used as prefix for the current
* dataset's original name (extension name). If the dataset to be appended
* does not have a name,
* @em id is used as the dataset identifier. If @em flags is set to
* @c CPL_MULTIFRAME_ID_JOIN, the dataset's identifier is created by
* concatenating the dataset name found in the primary properties, and the
* dataset name taken from the supplementary properties, using @em id as
* separator string. If no dataset name is found in the supplementary
* properties, only the dataset name found in the primary properties is used
* as identifier and the given separator is not appended. Note that for this
* last method it is an error if there is no dataset name present in the
* primary properties of the source dataset.
*
* The argument @em id may be the empty string for the method
* @c CPL_MULTIFRAME_ID_JOIN. For the method @c CPL_MULTIFRAME_ID_PREFIX
* this is an error.
*
* If @em properties is given it has to be a @c NULL terminated array of
* property names. For each specified property name their value is changed
* according to the naming scheme selected by @em flags, i.e. the value is
* either prefixed by @em id, or it is set to the concatenation of the
* source dataset name found in its primary properties, @em id, and its
* original value. This can be used to correctly change properties used to
* reference one of the other datasets in the given group through their
* value. If a given property is not found, it is ignored.
*/
cpl_error_code
cpl_multiframe_append_datagroup_from_position(cpl_multiframe *self,
const char *id,
const cpl_frame *frame,
cpl_size nsets,
cpl_size *positions,
const cpl_regex **filter1,
const cpl_regex **filter2,
const char **properties,
unsigned int flags)
{
if ((self == NULL) || (id == NULL) || (frame == NULL)) {
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
}
if (positions == NULL) {
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
}
if (nsets < 2) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
if ((flags != CPL_MULTIFRAME_ID_PREFIX) &&
(flags != CPL_MULTIFRAME_ID_JOIN)) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
if ((flags == CPL_MULTIFRAME_ID_PREFIX) && (*id == '\0')) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
const cxchar *filename = cpl_frame_get_filename(frame);
if (filename == NULL) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
cxint status =
_cpl_multiframe_append_datagroup(self, id, filename, nsets, positions,
filter1, filter2, properties, flags);
if (status) {
cpl_error_set_where(cpl_func);
return status;
}
return CPL_ERROR_NONE;
}
/**
* @brief
* Adds a group of dataset references given by name to a multi-frame
* container object.
*
* @param self The multi-frame object.
* @param id Unique dataset identifier.
* @param frame The source data frame from which the datasets are
* taken.
* @param nsets The number of datasets to be merged.
* @param names The names of the source datasets in the source data
* frame.
* @param filter1 Property filters to apply to the primary header of each
* source dataset.
* @param filter2 Property filters to apply to the extension header of
* each source dataset.
* @param properties Property names to be updated.
* @param flags Flag controlling the creation of the dataset's target
* id.
*
* @return
* The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* The function is equivalent to
* cpl_multiframe_append_datagroup_from_position(), but the source datasets
* to be added are looked up in the source data frame
* @em frame using their names given in the array @em names.
*/
cpl_error_code
cpl_multiframe_append_datagroup(cpl_multiframe *self,
const char *id,
const cpl_frame *frame,
cpl_size nsets,
const char **names,
const cpl_regex **filter1,
const cpl_regex **filter2,
const char **properties,
unsigned int flags)
{
if ((self == NULL) || (id == NULL) || (frame == NULL)) {
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
}
if (names == NULL) {
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
}
if (nsets < 2) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
if ((flags != CPL_MULTIFRAME_ID_PREFIX) &&
(flags != CPL_MULTIFRAME_ID_JOIN)) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
if ((flags == CPL_MULTIFRAME_ID_PREFIX) && (*id == '\0')) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
const cxchar *filename = cpl_frame_get_filename(frame);
if (filename == NULL) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
cxssize is;
cpl_size *positions = cx_calloc(nsets, sizeof(cpl_size));
for (is = 0; is < nsets; ++is) {
cxint _position = cpl_fits_find_extension(filename, names[is]);
if (_position > 0) {
positions[is] = _position;
}
else {
cx_free(positions);
return cpl_error_set_(CPL_ERROR_DATA_NOT_FOUND);
}
}
cxint status =
_cpl_multiframe_append_datagroup(self, id, filename, nsets, positions,
filter1, filter2, properties, flags);
cx_free(positions);
positions = NULL;
if (status) {
cpl_error_set_where(cpl_func);
return status;
}
return CPL_ERROR_NONE;
}
/**
* @brief
* Add a placeholder to a multi-frame container.
*
* @param self The multi-frame object.
* @param id Unique dataset identifier.
*
* @return
* The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* The function adds an empty dataset, a placeholder to a multi-frame
* container. An empty dataset is special since it is not attached to an
* underlying data file. When the multi-frame object is written to a file,
* an empty dataset appears as a named, but otherwise empty unit.
*/
cpl_error_code
cpl_multiframe_add_empty(cpl_multiframe *self, const char *id)
{
if ((self == NULL) || (id == NULL) || (*id == '\0')) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_INPUT);
}
cpl_fitsheader *hdr = _cpl_fitsheader_new();
cpl_fitsemptyunit *data = _cpl_fitsemptyunit_new();
if ((hdr == NULL) || (data == NULL)) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
}
cpl_fitsdataset *dataset =
_cpl_fitsdataset_create(hdr, (cpl_fitsdataunit *)data);
_cpl_fitsheader_delete(hdr);
_cpl_fitsemptyunit_delete(data);
if (dataset == NULL) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
}
// FIXME: The following call does not handle the dataset version and
// level. Instead the dataset should be searched for the id, and if
// it is already present, the dataset version should be set
// to one more than the maximum version which is already present.
cxint status = _cpl_fitsdataset_set_id(dataset, id, 0, 0);
if (status) {
return cpl_error_set_(CPL_ERROR_ILLEGAL_OUTPUT);
}
/*
* Register the empty dataset.
*/
cx_deque_push_back(self->_m_datasets, dataset);
return CPL_ERROR_NONE;
}
/**
* @brief
* Write a multi-frame container to a file.
*
* @param self The multi-frame container object.
* @param filename Name of the file to which the multi-frame object is
* written.
*
* @return
* The function returns @c CPL_ERROR_NONE on success, and an appropriate
* error code otherwise.
*
* The function writes the multi-frame object @em self to the file @em
* filename. A multi-frame object contains only the properties and the
* references to the data units, which may be located in different files.
* Only when this function is called the all referenced datasets are copied
* and written to the output file.
*/
cpl_error_code
cpl_multiframe_write(cpl_multiframe *self, const char *filename)
{
if ((self == NULL) || (filename == NULL)) {
return cpl_error_set_(CPL_ERROR_NULL_INPUT);
}
cxint status = 0;
fitsfile *fp = NULL;
fits_create_file(&fp, filename, &status);
if (status == FILE_NOT_CREATED) {
cx_string *_filename = cx_string_create("!");
cx_string_append(_filename, filename);
status = 0;
fits_create_file(&fp, cx_string_get(_filename), &status);
cx_string_delete(_filename);
_filename = NULL;
}
if (status) {
return cpl_error_set_(CPL_ERROR_FILE_NOT_CREATED);
}
cxsize sz = cx_deque_size(self->_m_datasets);
if (sz == 0) {
fits_close_file(fp, &status);
return cpl_error_set_(CPL_ERROR_DATA_NOT_FOUND);
}
/*
* Write datasets to the output file
*/
cpl_fitsdataset *dataset = cx_deque_get(self->_m_datasets, 0);
_cpl_fitsheader_sort(_cpl_fitsdataset_get_header(dataset),
_cpl_fitscard_compare_dicb);
_cpl_fitsdataset_write(dataset, fp, TRUE, FALSE);
cxsize i;
for (i = 1; i < sz; ++i) {
dataset = cx_deque_get(self->_m_datasets, i);
_cpl_fitsheader_sort(_cpl_fitsdataset_get_header(dataset),
_cpl_fitscard_compare_dicb);
_cpl_fitsdataset_write(dataset, fp, FALSE, FALSE);
}
/*
* Update file creation timestamp
*/
int type;
fits_movabs_hdu(fp, 1, &type, &status);
fits_write_date(fp, &status);
status = 0;
fits_close_file(fp, &status);
return CPL_ERROR_NONE;
}
/**@}*/
|