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
|
/* This file, editcol.c, contains the set of FITSIO routines that */
/* insert or delete rows or columns in a table or resize an image */
/* The FITSIO software was written by William Pence at the High Energy */
/* Astrophysic Science Archive Research Center (HEASARC) at the NASA */
/* Goddard Space Flight Center. */
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "fitsio2.h"
/*--------------------------------------------------------------------------*/
int ffrsim(fitsfile *fptr, /* I - FITS file pointer */
int bitpix, /* I - bits per pixel */
int naxis, /* I - number of axes in the array */
long *naxes, /* I - size of each axis */
int *status) /* IO - error status */
/*
resize an existing primary array or IMAGE extension.
*/
{
LONGLONG tnaxes[99];
int ii;
if (*status > 0)
return(*status);
for (ii = 0; (ii < naxis) && (ii < 99); ii++)
tnaxes[ii] = naxes[ii];
ffrsimll(fptr, bitpix, naxis, tnaxes, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffrsimll(fitsfile *fptr, /* I - FITS file pointer */
int bitpix, /* I - bits per pixel */
int naxis, /* I - number of axes in the array */
LONGLONG *naxes, /* I - size of each axis */
int *status) /* IO - error status */
/*
resize an existing primary array or IMAGE extension.
*/
{
int ii, simple, obitpix, onaxis, extend, nmodify;
long nblocks, longval;
long pcount, gcount, longbitpix;
LONGLONG onaxes[99], newsize, oldsize;
char comment[FLEN_COMMENT], keyname[FLEN_KEYWORD], message[FLEN_ERRMSG];
if (*status > 0)
return(*status);
/* reset position to the correct HDU if necessary */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
/* get current image size parameters */
if (ffghprll(fptr, 99, &simple, &obitpix, &onaxis, onaxes, &pcount,
&gcount, &extend, status) > 0)
return(*status);
longbitpix = bitpix;
/* test for the 4 special cases that represent unsigned integers
or signed bytes */
if (longbitpix == USHORT_IMG)
longbitpix = SHORT_IMG;
else if (longbitpix == ULONG_IMG)
longbitpix = LONG_IMG;
else if (longbitpix == SBYTE_IMG)
longbitpix = BYTE_IMG;
else if (longbitpix == ULONGLONG_IMG)
longbitpix = LONGLONG_IMG;
/* test that the new values are legal */
if (longbitpix != BYTE_IMG && longbitpix != SHORT_IMG &&
longbitpix != LONG_IMG && longbitpix != LONGLONG_IMG &&
longbitpix != FLOAT_IMG && longbitpix != DOUBLE_IMG)
{
snprintf(message, FLEN_ERRMSG,
"Illegal value for BITPIX keyword: %d", bitpix);
ffpmsg(message);
return(*status = BAD_BITPIX);
}
if (naxis < 0 || naxis > 999)
{
snprintf(message, FLEN_ERRMSG,
"Illegal value for NAXIS keyword: %d", naxis);
ffpmsg(message);
return(*status = BAD_NAXIS);
}
if (naxis == 0)
newsize = 0;
else
newsize = 1;
for (ii = 0; ii < naxis; ii++)
{
if (naxes[ii] < 0)
{
snprintf(message, FLEN_ERRMSG,
"Illegal value for NAXIS%d keyword: %.0f", ii + 1, (double) (naxes[ii]));
ffpmsg(message);
return(*status = BAD_NAXES);
}
newsize *= naxes[ii]; /* compute new image size, in pixels */
}
/* compute size of old image, in bytes */
if (onaxis == 0)
oldsize = 0;
else
{
oldsize = 1;
for (ii = 0; ii < onaxis; ii++)
oldsize *= onaxes[ii];
oldsize = (oldsize + pcount) * gcount * (abs(obitpix) / 8);
}
oldsize = (oldsize + 2879) / 2880; /* old size, in blocks */
newsize = (newsize + pcount) * gcount * (labs(longbitpix) / 8);
newsize = (newsize + 2879) / 2880; /* new size, in blocks */
if (newsize > oldsize) /* have to insert new blocks for image */
{
nblocks = (long) (newsize - oldsize);
if (ffiblk(fptr, nblocks, 1, status) > 0)
return(*status);
}
else if (oldsize > newsize) /* have to delete blocks from image */
{
nblocks = (long) (oldsize - newsize);
if (ffdblk(fptr, nblocks, status) > 0)
return(*status);
}
/* now update the header keywords */
strcpy(comment,"&"); /* special value to leave comments unchanged */
if (longbitpix != obitpix)
{ /* update BITPIX value */
ffmkyj(fptr, "BITPIX", longbitpix, comment, status);
}
if (naxis != onaxis)
{ /* update NAXIS value */
longval = naxis;
ffmkyj(fptr, "NAXIS", longval, comment, status);
}
/* modify the existing NAXISn keywords */
nmodify = minvalue(naxis, onaxis);
for (ii = 0; ii < nmodify; ii++)
{
ffkeyn("NAXIS", ii+1, keyname, status);
ffmkyj(fptr, keyname, naxes[ii], comment, status);
}
if (naxis > onaxis) /* insert additional NAXISn keywords */
{
strcpy(comment,"length of data axis");
for (ii = onaxis; ii < naxis; ii++)
{
ffkeyn("NAXIS", ii+1, keyname, status);
ffikyj(fptr, keyname, naxes[ii], comment, status);
}
}
else if (onaxis > naxis) /* delete old NAXISn keywords */
{
for (ii = naxis; ii < onaxis; ii++)
{
ffkeyn("NAXIS", ii+1, keyname, status);
ffdkey(fptr, keyname, status);
}
}
/* Update the BSCALE and BZERO keywords, if an unsigned integer image
or a signed byte image. */
if (bitpix == USHORT_IMG)
{
strcpy(comment, "offset data range to that of unsigned short");
ffukyg(fptr, "BZERO", 32768., 0, comment, status);
strcpy(comment, "default scaling factor");
ffukyg(fptr, "BSCALE", 1.0, 0, comment, status);
}
else if (bitpix == ULONG_IMG)
{
strcpy(comment, "offset data range to that of unsigned long");
ffukyg(fptr, "BZERO", 2147483648., 0, comment, status);
strcpy(comment, "default scaling factor");
ffukyg(fptr, "BSCALE", 1.0, 0, comment, status);
}
else if (bitpix == ULONGLONG_IMG)
{
strcpy(comment, "offset data range to that of unsigned long long");
ffukyg(fptr, "BZERO", 9223372036854775808., 0, comment, status);
strcpy(comment, "default scaling factor");
ffukyg(fptr, "BSCALE", 1.0, 0, comment, status);
}
else if (bitpix == SBYTE_IMG)
{
strcpy(comment, "offset data range to that of signed byte");
ffukyg(fptr, "BZERO", -128., 0, comment, status);
strcpy(comment, "default scaling factor");
ffukyg(fptr, "BSCALE", 1.0, 0, comment, status);
}
/* re-read the header, to make sure structures are updated */
ffrdef(fptr, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffirow(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG firstrow, /* I - insert space AFTER this row */
/* 0 = insert space at beginning of table */
LONGLONG nrows, /* I - number of rows to insert */
int *status) /* IO - error status */
/*
insert NROWS blank rows immediated after row firstrow (1 = first row).
Set firstrow = 0 to insert space at the beginning of the table.
*/
{
int tstatus;
LONGLONG naxis1, naxis2;
LONGLONG datasize, firstbyte, nshift, nbytes;
LONGLONG freespace;
long nblock;
if (*status > 0)
return(*status);
/* reset position to the correct HDU if necessary */
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only add rows to TABLE or BINTABLE extension (ffirow)");
return(*status = NOT_TABLE);
}
if (nrows < 0 )
return(*status = NEG_BYTES);
else if (nrows == 0)
return(*status); /* no op, so just return */
/* get the current size of the table */
/* use internal structure since NAXIS2 keyword may not be up to date */
naxis1 = (fptr->Fptr)->rowlength;
naxis2 = (fptr->Fptr)->numrows;
if (firstrow > naxis2)
{
ffpmsg(
"Insert position greater than the number of rows in the table (ffirow)");
return(*status = BAD_ROW_NUM);
}
else if (firstrow < 0)
{
ffpmsg("Insert position is less than 0 (ffirow)");
return(*status = BAD_ROW_NUM);
}
/* current data size */
datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize;
freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize;
nshift = naxis1 * nrows; /* no. of bytes to add to table */
if ( (freespace - nshift) < 0) /* not enough existing space? */
{
nblock = (long) ((nshift - freespace + 2879) / 2880); /* number of blocks */
ffiblk(fptr, nblock, 1, status); /* insert the blocks */
}
firstbyte = naxis1 * firstrow; /* relative insert position */
nbytes = datasize - firstbyte; /* no. of bytes to shift down */
firstbyte += ((fptr->Fptr)->datastart); /* absolute insert position */
if (nshift > 0) { /* nshift may be zero if naxis1 == naxis2 == 0 */
ffshft(fptr, firstbyte, nbytes, nshift, status); /* shift rows and heap */
}
/* update the heap starting address */
(fptr->Fptr)->heapstart += nshift;
/* update the THEAP keyword if it exists */
tstatus = 0;
ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus);
/* update the NAXIS2 keyword */
ffmkyj(fptr, "NAXIS2", naxis2 + nrows, "&", status);
((fptr->Fptr)->numrows) += nrows;
((fptr->Fptr)->origrows) += nrows;
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdrow(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG firstrow, /* I - first row to delete (1 = first) */
LONGLONG nrows, /* I - number of rows to delete */
int *status) /* IO - error status */
/*
delete NROWS rows from table starting with firstrow (1 = first row of table).
*/
{
int tstatus;
LONGLONG naxis1, naxis2;
LONGLONG datasize, firstbyte, nbytes, nshift;
LONGLONG freespace;
long nblock;
char comm[FLEN_COMMENT];
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only delete rows in TABLE or BINTABLE extension (ffdrow)");
return(*status = NOT_TABLE);
}
if (nrows < 0 )
return(*status = NEG_BYTES);
else if (nrows == 0)
return(*status); /* no op, so just return */
ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); /* get the current */
/* ffgkyj(fptr, "NAXIS2", &naxis2, comm, status);*/ /* size of the table */
/* the NAXIS2 keyword may not be up to date, so use the structure value */
naxis2 = (fptr->Fptr)->numrows;
if (firstrow > naxis2)
{
ffpmsg(
"Delete position greater than the number of rows in the table (ffdrow)");
return(*status = BAD_ROW_NUM);
}
else if (firstrow < 1)
{
ffpmsg("Delete position is less than 1 (ffdrow)");
return(*status = BAD_ROW_NUM);
}
else if (firstrow + nrows - 1 > naxis2)
{
ffpmsg("No. of rows to delete exceeds size of table (ffdrow)");
return(*status = BAD_ROW_NUM);
}
nshift = naxis1 * nrows; /* no. of bytes to delete from table */
/* cur size of data */
datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize;
firstbyte = naxis1 * (firstrow + nrows - 1); /* relative del pos */
nbytes = datasize - firstbyte; /* no. of bytes to shift up */
firstbyte += ((fptr->Fptr)->datastart); /* absolute delete position */
ffshft(fptr, firstbyte, nbytes, nshift * (-1), status); /* shift data */
freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize;
nblock = (long) ((nshift + freespace) / 2880); /* number of blocks */
/* delete integral number blocks */
if (nblock > 0)
ffdblk(fptr, nblock, status);
/* update the heap starting address */
(fptr->Fptr)->heapstart -= nshift;
/* update the THEAP keyword if it exists */
tstatus = 0;
ffmkyj(fptr, "THEAP", (long)(fptr->Fptr)->heapstart, "&", &tstatus);
/* update the NAXIS2 keyword */
ffmkyj(fptr, "NAXIS2", naxis2 - nrows, "&", status);
((fptr->Fptr)->numrows) -= nrows;
((fptr->Fptr)->origrows) -= nrows;
/* Update the heap data, if any. This will remove any orphaned data */
/* that was only pointed to by the rows that have been deleted */
ffcmph(fptr, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdrrg(fitsfile *fptr, /* I - FITS file pointer to table */
char *ranges, /* I - ranges of rows to delete (1 = first) */
int *status) /* IO - error status */
/*
delete the ranges of rows from the table (1 = first row of table).
The 'ranges' parameter typically looks like:
'10-20, 30 - 40, 55' or '50-'
and gives a list of rows or row ranges separated by commas.
*/
{
char *cptr;
int nranges, nranges2, ii;
long *minrow, *maxrow, nrows, *rowarray, jj, kk;
LONGLONG naxis2;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only delete rows in TABLE or BINTABLE extension (ffdrrg)");
return(*status = NOT_TABLE);
}
/* the NAXIS2 keyword may not be up to date, so use the structure value */
naxis2 = (fptr->Fptr)->numrows;
/* find how many ranges were specified ( = no. of commas in string + 1) */
cptr = ranges;
for (nranges = 1; (cptr = strchr(cptr, ',')); nranges++)
cptr++;
minrow = calloc(nranges, sizeof(long));
maxrow = calloc(nranges, sizeof(long));
if (!minrow || !maxrow) {
*status = MEMORY_ALLOCATION;
ffpmsg("failed to allocate memory for row ranges (ffdrrg)");
if (maxrow) free(maxrow);
if (minrow) free(minrow);
return(*status);
}
/* parse range list into array of range min and max values */
ffrwrg(ranges, naxis2, nranges, &nranges2, minrow, maxrow, status);
if (*status > 0 || nranges2 == 0) {
free(maxrow);
free(minrow);
return(*status);
}
/* determine total number or rows to delete */
nrows = 0;
for (ii = 0; ii < nranges2; ii++) {
nrows = nrows + maxrow[ii] - minrow[ii] + 1;
}
rowarray = calloc(nrows, sizeof(long));
if (!rowarray) {
free(maxrow);
free(minrow);
*status = MEMORY_ALLOCATION;
ffpmsg("failed to allocate memory for row array (ffdrrg)");
return(*status);
}
for (kk = 0, ii = 0; ii < nranges2; ii++) {
for (jj = minrow[ii]; jj <= maxrow[ii]; jj++) {
rowarray[kk] = jj;
kk++;
}
}
/* delete the rows */
ffdrws(fptr, rowarray, nrows, status);
free(rowarray);
free(maxrow);
free(minrow);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdrws(fitsfile *fptr, /* I - FITS file pointer */
long *rownum, /* I - list of rows to delete (1 = first) */
long nrows, /* I - number of rows to delete */
int *status) /* IO - error status */
/*
delete the list of rows from the table (1 = first row of table).
*/
{
LONGLONG naxis1, naxis2, insertpos, nextrowpos;
long ii, nextrow;
char comm[FLEN_COMMENT];
unsigned char *buffer;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
/* rescan header if data structure is undefined */
if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only delete rows in TABLE or BINTABLE extension (ffdrws)");
return(*status = NOT_TABLE);
}
if (nrows < 0 )
return(*status = NEG_BYTES);
else if (nrows == 0)
return(*status); /* no op, so just return */
ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); /* row width */
ffgkyjj(fptr, "NAXIS2", &naxis2, comm, status); /* number of rows */
/* check that input row list is in ascending order */
for (ii = 1; ii < nrows; ii++)
{
if (rownum[ii - 1] >= rownum[ii])
{
ffpmsg("row numbers are not in increasing order (ffdrws)");
return(*status = BAD_ROW_NUM);
}
}
if (rownum[0] < 1)
{
ffpmsg("first row to delete is less than 1 (ffdrws)");
return(*status = BAD_ROW_NUM);
}
else if (rownum[nrows - 1] > naxis2)
{
ffpmsg("last row to delete exceeds size of table (ffdrws)");
return(*status = BAD_ROW_NUM);
}
buffer = (unsigned char *) malloc( (size_t) naxis1); /* buffer for one row */
if (!buffer)
{
ffpmsg("malloc failed (ffdrws)");
return(*status = MEMORY_ALLOCATION);
}
/* byte location to start of first row to delete, and the next row */
insertpos = (fptr->Fptr)->datastart + ((rownum[0] - 1) * naxis1);
nextrowpos = insertpos + naxis1;
nextrow = rownum[0] + 1;
/* work through the list of rows to delete */
for (ii = 1; ii < nrows; nextrow++, nextrowpos += naxis1)
{
if (nextrow < rownum[ii])
{ /* keep this row, so copy it to the new position */
ffmbyt(fptr, nextrowpos, REPORT_EOF, status);
ffgbyt(fptr, naxis1, buffer, status); /* read the bytes */
ffmbyt(fptr, insertpos, IGNORE_EOF, status);
ffpbyt(fptr, naxis1, buffer, status); /* write the bytes */
if (*status > 0)
{
ffpmsg("error while copying good rows in table (ffdrws)");
free(buffer);
return(*status);
}
insertpos += naxis1;
}
else
{ /* skip over this row since it is in the list */
ii++;
}
}
/* finished with all the rows to delete; copy remaining rows */
while(nextrow <= naxis2)
{
ffmbyt(fptr, nextrowpos, REPORT_EOF, status);
ffgbyt(fptr, naxis1, buffer, status); /* read the bytes */
ffmbyt(fptr, insertpos, IGNORE_EOF, status);
ffpbyt(fptr, naxis1, buffer, status); /* write the bytes */
if (*status > 0)
{
ffpmsg("failed to copy remaining rows in table (ffdrws)");
free(buffer);
return(*status);
}
insertpos += naxis1;
nextrowpos += naxis1;
nextrow++;
}
free(buffer);
/* now delete the empty rows at the end of the table */
ffdrow(fptr, naxis2 - nrows + 1, nrows, status);
/* Update the heap data, if any. This will remove any orphaned data */
/* that was only pointed to by the rows that have been deleted */
ffcmph(fptr, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdrwsll(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG *rownum, /* I - list of rows to delete (1 = first) */
LONGLONG nrows, /* I - number of rows to delete */
int *status) /* IO - error status */
/*
delete the list of rows from the table (1 = first row of table).
*/
{
LONGLONG insertpos, nextrowpos;
LONGLONG naxis1, naxis2, ii, nextrow;
char comm[FLEN_COMMENT];
unsigned char *buffer;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
/* rescan header if data structure is undefined */
if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only delete rows in TABLE or BINTABLE extension (ffdrws)");
return(*status = NOT_TABLE);
}
if (nrows < 0 )
return(*status = NEG_BYTES);
else if (nrows == 0)
return(*status); /* no op, so just return */
ffgkyjj(fptr, "NAXIS1", &naxis1, comm, status); /* row width */
ffgkyjj(fptr, "NAXIS2", &naxis2, comm, status); /* number of rows */
/* check that input row list is in ascending order */
for (ii = 1; ii < nrows; ii++)
{
if (rownum[ii - 1] >= rownum[ii])
{
ffpmsg("row numbers are not in increasing order (ffdrws)");
return(*status = BAD_ROW_NUM);
}
}
if (rownum[0] < 1)
{
ffpmsg("first row to delete is less than 1 (ffdrws)");
return(*status = BAD_ROW_NUM);
}
else if (rownum[nrows - 1] > naxis2)
{
ffpmsg("last row to delete exceeds size of table (ffdrws)");
return(*status = BAD_ROW_NUM);
}
buffer = (unsigned char *) malloc( (size_t) naxis1); /* buffer for one row */
if (!buffer)
{
ffpmsg("malloc failed (ffdrwsll)");
return(*status = MEMORY_ALLOCATION);
}
/* byte location to start of first row to delete, and the next row */
insertpos = (fptr->Fptr)->datastart + ((rownum[0] - 1) * naxis1);
nextrowpos = insertpos + naxis1;
nextrow = rownum[0] + 1;
/* work through the list of rows to delete */
for (ii = 1; ii < nrows; nextrow++, nextrowpos += naxis1)
{
if (nextrow < rownum[ii])
{ /* keep this row, so copy it to the new position */
ffmbyt(fptr, nextrowpos, REPORT_EOF, status);
ffgbyt(fptr, naxis1, buffer, status); /* read the bytes */
ffmbyt(fptr, insertpos, IGNORE_EOF, status);
ffpbyt(fptr, naxis1, buffer, status); /* write the bytes */
if (*status > 0)
{
ffpmsg("error while copying good rows in table (ffdrws)");
free(buffer);
return(*status);
}
insertpos += naxis1;
}
else
{ /* skip over this row since it is in the list */
ii++;
}
}
/* finished with all the rows to delete; copy remaining rows */
while(nextrow <= naxis2)
{
ffmbyt(fptr, nextrowpos, REPORT_EOF, status);
ffgbyt(fptr, naxis1, buffer, status); /* read the bytes */
ffmbyt(fptr, insertpos, IGNORE_EOF, status);
ffpbyt(fptr, naxis1, buffer, status); /* write the bytes */
if (*status > 0)
{
ffpmsg("failed to copy remaining rows in table (ffdrws)");
free(buffer);
return(*status);
}
insertpos += naxis1;
nextrowpos += naxis1;
nextrow++;
}
free(buffer);
/* now delete the empty rows at the end of the table */
ffdrow(fptr, naxis2 - nrows + 1, nrows, status);
/* Update the heap data, if any. This will remove any orphaned data */
/* that was only pointed to by the rows that have been deleted */
ffcmph(fptr, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffrwrg(
char *rowlist, /* I - list of rows and row ranges */
LONGLONG maxrows, /* I - number of rows in the table */
int maxranges, /* I - max number of ranges to be returned */
int *numranges, /* O - number ranges returned */
long *minrow, /* O - first row in each range */
long *maxrow, /* O - last row in each range */
int *status) /* IO - status value */
{
/*
parse the input list of row ranges, returning the number of ranges,
and the min and max row value in each range.
The only characters allowed in the input rowlist are
decimal digits, minus sign, and comma (and non-significant spaces)
Example:
list = "10-20, 30-35,50"
would return numranges = 3, minrow[] = {10, 30, 50}, maxrow[] = {20, 35, 50}
error is returned if min value of range is > max value of range or if the
ranges are not monotonically increasing.
*/
char *next;
long minval, maxval;
if (*status > 0)
return(*status);
if (maxrows <= 0 ) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Input maximum range value is <= 0 (fits_parse_ranges)");
return(*status);
}
next = rowlist;
*numranges = 0;
while (*next == ' ')next++; /* skip spaces */
while (*next != '\0') {
/* find min value of next range; *next must be '-' or a digit */
if (*next == '-') {
minval = 1; /* implied minrow value = 1 */
} else if ( isdigit((int) *next) ) {
minval = strtol(next, &next, 10);
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
while (*next == ' ')next++; /* skip spaces */
/* find max value of next range; *next must be '-', or ',' */
if (*next == '-') {
next++;
while (*next == ' ')next++; /* skip spaces */
if ( isdigit((int) *next) ) {
maxval = strtol(next, &next, 10);
} else if (*next == ',' || *next == '\0') {
maxval = (long) maxrows; /* implied max value */
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
} else if (*next == ',' || *next == '\0') {
maxval = minval; /* only a single integer in this range */
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
if (*numranges + 1 > maxranges) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Overflowed maximum number of ranges (fits_parse_ranges)");
return(*status);
}
if (minval < 1 ) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list: row number < 1");
ffpmsg(rowlist);
return(*status);
}
if (maxval < minval) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list: min > max");
ffpmsg(rowlist);
return(*status);
}
if (*numranges > 0) {
if (minval <= maxrow[(*numranges) - 1]) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list. Range minimum is");
ffpmsg(" less than or equal to previous range maximum");
ffpmsg(rowlist);
return(*status);
}
}
if (minval <= maxrows) { /* ignore range if greater than maxrows */
if (maxval > maxrows)
maxval = (long) maxrows;
minrow[*numranges] = minval;
maxrow[*numranges] = maxval;
(*numranges)++;
}
while (*next == ' ')next++; /* skip spaces */
if (*next == ',') {
next++;
while (*next == ' ')next++; /* skip more spaces */
}
}
if (*numranges == 0) { /* a null string was entered */
minrow[0] = 1;
maxrow[0] = (long) maxrows;
*numranges = 1;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffrwrgll(
char *rowlist, /* I - list of rows and row ranges */
LONGLONG maxrows, /* I - number of rows in the list */
int maxranges, /* I - max number of ranges to be returned */
int *numranges, /* O - number ranges returned */
LONGLONG *minrow, /* O - first row in each range */
LONGLONG *maxrow, /* O - last row in each range */
int *status) /* IO - status value */
{
/*
parse the input list of row ranges, returning the number of ranges,
and the min and max row value in each range.
The only characters allowed in the input rowlist are
decimal digits, minus sign, and comma (and non-significant spaces)
Example:
list = "10-20, 30-35,50"
would return numranges = 3, minrow[] = {10, 30, 50}, maxrow[] = {20, 35, 50}
error is returned if min value of range is > max value of range or if the
ranges are not monotonically increasing.
*/
char *next;
LONGLONG minval, maxval;
double dvalue;
if (*status > 0)
return(*status);
if (maxrows <= 0 ) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Input maximum range value is <= 0 (fits_parse_ranges)");
return(*status);
}
next = rowlist;
*numranges = 0;
while (*next == ' ')next++; /* skip spaces */
while (*next != '\0') {
/* find min value of next range; *next must be '-' or a digit */
if (*next == '-') {
minval = 1; /* implied minrow value = 1 */
} else if ( isdigit((int) *next) ) {
/* read as a double, because the string to LONGLONG function */
/* is platform dependent (strtoll, strtol, _atoI64) */
dvalue = strtod(next, &next);
minval = (LONGLONG) (dvalue + 0.1);
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
while (*next == ' ')next++; /* skip spaces */
/* find max value of next range; *next must be '-', or ',' */
if (*next == '-') {
next++;
while (*next == ' ')next++; /* skip spaces */
if ( isdigit((int) *next) ) {
/* read as a double, because the string to LONGLONG function */
/* is platform dependent (strtoll, strtol, _atoI64) */
dvalue = strtod(next, &next);
maxval = (LONGLONG) (dvalue + 0.1);
} else if (*next == ',' || *next == '\0') {
maxval = maxrows; /* implied max value */
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
} else if (*next == ',' || *next == '\0') {
maxval = minval; /* only a single integer in this range */
} else {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list:");
ffpmsg(rowlist);
return(*status);
}
if (*numranges + 1 > maxranges) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Overflowed maximum number of ranges (fits_parse_ranges)");
return(*status);
}
if (minval < 1 ) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list: row number < 1");
ffpmsg(rowlist);
return(*status);
}
if (maxval < minval) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list: min > max");
ffpmsg(rowlist);
return(*status);
}
if (*numranges > 0) {
if (minval <= maxrow[(*numranges) - 1]) {
*status = RANGE_PARSE_ERROR;
ffpmsg("Syntax error in this row range list. Range minimum is");
ffpmsg(" less than or equal to previous range maximum");
ffpmsg(rowlist);
return(*status);
}
}
if (minval <= maxrows) { /* ignore range if greater than maxrows */
if (maxval > maxrows)
maxval = maxrows;
minrow[*numranges] = minval;
maxrow[*numranges] = maxval;
(*numranges)++;
}
while (*next == ' ')next++; /* skip spaces */
if (*next == ',') {
next++;
while (*next == ' ')next++; /* skip more spaces */
}
}
if (*numranges == 0) { /* a null string was entered */
minrow[0] = 1;
maxrow[0] = maxrows;
*numranges = 1;
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fficol(fitsfile *fptr, /* I - FITS file pointer */
int numcol, /* I - position for new col. (1 = 1st) */
char *ttype, /* I - name of column (TTYPE keyword) */
char *tform, /* I - format of column (TFORM keyword) */
int *status) /* IO - error status */
/*
Insert a new column into an existing table at position numcol. If
numcol is greater than the number of existing columns in the table
then the new column will be appended as the last column in the table.
*/
{
char *name, *format;
name = ttype;
format = tform;
fficls(fptr, numcol, 1, &name, &format, status);
return(*status);
}
/*--------------------------------------------------------------------------*/
int fficls(fitsfile *fptr, /* I - FITS file pointer */
int fstcol, /* I - position for first new col. (1 = 1st) */
int ncols, /* I - number of columns to insert */
char **ttype, /* I - array of column names(TTYPE keywords) */
char **tform, /* I - array of formats of column (TFORM) */
int *status) /* IO - error status */
/*
Insert 1 or more new columns into an existing table at position numcol. If
fstcol is greater than the number of existing columns in the table
then the new column will be appended as the last column in the table.
*/
{
int colnum, datacode, decims, tfields, tstatus, ii;
LONGLONG datasize, firstbyte, nbytes, nadd, naxis1, naxis2, freespace;
LONGLONG tbcol, firstcol, delbyte;
long nblock, width, repeat;
char tfm[FLEN_VALUE], keyname[FLEN_KEYWORD], comm[FLEN_COMMENT], *cptr;
char card[FLEN_CARD];
tcolumn *colptr;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg("Can only add columns to TABLE or BINTABLE extension (fficls)");
return(*status = NOT_TABLE);
}
/* is the column number valid? */
tfields = (fptr->Fptr)->tfield;
if (fstcol < 1 )
return(*status = BAD_COL_NUM);
else if (fstcol > tfields)
colnum = tfields + 1; /* append as last column */
else
colnum = fstcol;
/* parse the tform value and calc number of bytes to add to each row */
delbyte = 0;
for (ii = 0; ii < ncols; ii++)
{
if (strlen(tform[ii]) > FLEN_VALUE-1)
{
ffpmsg("Column format string too long (fficls)");
return (*status=BAD_TFORM);
}
strcpy(tfm, tform[ii]);
ffupch(tfm); /* make sure format is in upper case */
if ((fptr->Fptr)->hdutype == ASCII_TBL)
{
ffasfm(tfm, &datacode, &width, &decims, status);
delbyte += width + 1; /* add one space between the columns */
}
else
{
ffbnfm(tfm, &datacode, &repeat, &width, status);
if (datacode < 0) { /* variable length array column */
if (strchr(tfm, 'Q'))
delbyte += 16;
else
delbyte += 8;
} else if (datacode == 1) /* bit column; round up */
delbyte += (repeat + 7) / 8; /* to multiple of 8 bits */
else if (datacode == 16) /* ASCII string column */
delbyte += repeat;
else /* numerical data type */
delbyte += (datacode / 10) * repeat;
}
}
if (*status > 0)
return(*status);
/* get the current size of the table */
/* use internal structure since NAXIS2 keyword may not be up to date */
naxis1 = (fptr->Fptr)->rowlength;
naxis2 = (fptr->Fptr)->numrows;
/* current size of data */
datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize;
freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize;
nadd = delbyte * naxis2; /* no. of bytes to add to table */
if ( (freespace - nadd) < 0) /* not enough existing space? */
{
nblock = (long) ((nadd - freespace + 2879) / 2880); /* number of blocks */
if (ffiblk(fptr, nblock, 1, status) > 0) /* insert the blocks */
return(*status);
}
/* shift heap down (if it exists) */
if ((fptr->Fptr)->heapsize > 0)
{
nbytes = (fptr->Fptr)->heapsize; /* no. of bytes to shift down */
/* absolute heap pos */
firstbyte = (fptr->Fptr)->datastart + (fptr->Fptr)->heapstart;
if (ffshft(fptr, firstbyte, nbytes, nadd, status) > 0) /* move heap */
return(*status);
}
/* update the heap starting address */
(fptr->Fptr)->heapstart += nadd;
/* update the THEAP keyword if it exists */
tstatus = 0;
ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus);
/* calculate byte position in the row where to insert the new column */
if (colnum > tfields)
firstcol = naxis1;
else
{
colptr = (fptr->Fptr)->tableptr;
colptr += (colnum - 1);
firstcol = colptr->tbcol;
}
/* insert delbyte bytes in every row, at byte position firstcol */
ffcins(fptr, naxis1, naxis2, delbyte, firstcol, status);
if ((fptr->Fptr)->hdutype == ASCII_TBL)
{
/* adjust the TBCOL values of the existing columns */
for(ii = 0; ii < tfields; ii++)
{
ffkeyn("TBCOL", ii + 1, keyname, status);
ffgkyjj(fptr, keyname, &tbcol, comm, status);
if (tbcol > firstcol)
{
tbcol += delbyte;
ffmkyj(fptr, keyname, tbcol, "&", status);
}
}
}
/* update the mandatory keywords */
ffmkyj(fptr, "TFIELDS", tfields + ncols, "&", status);
ffmkyj(fptr, "NAXIS1", naxis1 + delbyte, "&", status);
/* increment the index value on any existing column keywords */
if(colnum <= tfields)
ffkshf(fptr, colnum, tfields, ncols, status);
/* add the required keywords for the new columns */
for (ii = 0; ii < ncols; ii++, colnum++)
{
strcpy(comm, "label for field");
ffkeyn("TTYPE", colnum, keyname, status);
ffpkys(fptr, keyname, ttype[ii], comm, status);
strcpy(comm, "format of field");
strcpy(tfm, tform[ii]);
ffupch(tfm); /* make sure format is in upper case */
ffkeyn("TFORM", colnum, keyname, status);
if (abs(datacode) == TSBYTE)
{
/* Replace the 'S' with an 'B' in the TFORMn code */
cptr = tfm;
while (*cptr != 'S')
cptr++;
*cptr = 'B';
ffpkys(fptr, keyname, tfm, comm, status);
/* write the TZEROn and TSCALn keywords */
ffkeyn("TZERO", colnum, keyname, status);
strcpy(comm, "offset for signed bytes");
ffpkyg(fptr, keyname, -128., 0, comm, status);
ffkeyn("TSCAL", colnum, keyname, status);
strcpy(comm, "data are not scaled");
ffpkyg(fptr, keyname, 1., 0, comm, status);
}
else if (abs(datacode) == TUSHORT)
{
/* Replace the 'U' with an 'I' in the TFORMn code */
cptr = tfm;
while (*cptr != 'U')
cptr++;
*cptr = 'I';
ffpkys(fptr, keyname, tfm, comm, status);
/* write the TZEROn and TSCALn keywords */
ffkeyn("TZERO", colnum, keyname, status);
strcpy(comm, "offset for unsigned integers");
ffpkyg(fptr, keyname, 32768., 0, comm, status);
ffkeyn("TSCAL", colnum, keyname, status);
strcpy(comm, "data are not scaled");
ffpkyg(fptr, keyname, 1., 0, comm, status);
}
else if (abs(datacode) == TULONG)
{
/* Replace the 'V' with an 'J' in the TFORMn code */
cptr = tfm;
while (*cptr != 'V')
cptr++;
*cptr = 'J';
ffpkys(fptr, keyname, tfm, comm, status);
/* write the TZEROn and TSCALn keywords */
ffkeyn("TZERO", colnum, keyname, status);
strcpy(comm, "offset for unsigned integers");
ffpkyg(fptr, keyname, 2147483648., 0, comm, status);
ffkeyn("TSCAL", colnum, keyname, status);
strcpy(comm, "data are not scaled");
ffpkyg(fptr, keyname, 1., 0, comm, status);
}
else if (abs(datacode) == TULONGLONG)
{
/* Replace the 'W' with an 'K' in the TFORMn code */
cptr = tfm;
while (*cptr != 'W')
cptr++;
*cptr = 'K';
ffpkys(fptr, keyname, tfm, comm, status);
/* write the TZEROn and TSCALn keywords */
ffkeyn("TZERO", colnum, card, status);
strcat(card, " "); /* make sure name is >= 8 chars long */
*(card+8) = '\0';
strcat(card, "= 9223372036854775808 / offset for unsigned integers");
fits_write_record(fptr, card, status);
ffkeyn("TSCAL", colnum, keyname, status);
strcpy(comm, "data are not scaled");
ffpkyg(fptr, keyname, 1., 0, comm, status);
}
else
{
ffpkys(fptr, keyname, tfm, comm, status);
}
if ((fptr->Fptr)->hdutype == ASCII_TBL) /* write the TBCOL keyword */
{
if (colnum == tfields + 1)
tbcol = firstcol + 2; /* allow space between preceding col */
else
tbcol = firstcol + 1;
strcpy(comm, "beginning column of field");
ffkeyn("TBCOL", colnum, keyname, status);
ffpkyj(fptr, keyname, tbcol, comm, status);
/* increment the column starting position for the next column */
ffasfm(tfm, &datacode, &width, &decims, status);
firstcol += width + 1; /* add one space between the columns */
}
}
ffrdef(fptr, status); /* initialize the new table structure */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffmvec(fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - position of col to be modified */
LONGLONG newveclen, /* I - new vector length of column (TFORM) */
int *status) /* IO - error status */
/*
Modify the vector length of a column in a binary table, larger or smaller.
E.g., change a column from TFORMn = '1E' to '20E'.
*/
{
int datacode, tfields, tstatus;
LONGLONG datasize, size, firstbyte, nbytes, nadd, ndelete;
LONGLONG naxis1, naxis2, firstcol, freespace;
LONGLONG width, delbyte, repeat;
long nblock;
char tfm[FLEN_VALUE], keyname[FLEN_KEYWORD], tcode[2];
tcolumn *colptr;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype != BINARY_TBL)
{
ffpmsg(
"Can only change vector length of a column in BINTABLE extension (ffmvec)");
return(*status = NOT_TABLE);
}
/* is the column number valid? */
tfields = (fptr->Fptr)->tfield;
if (colnum < 1 || colnum > tfields)
return(*status = BAD_COL_NUM);
/* look up the current vector length and element width */
colptr = (fptr->Fptr)->tableptr;
colptr += (colnum - 1);
datacode = colptr->tdatatype; /* datatype of the column */
repeat = colptr->trepeat; /* field repeat count */
width = colptr->twidth; /* width of a single element in chars */
if (datacode < 0)
{
ffpmsg(
"Can't modify vector length of variable length column (ffmvec)");
return(*status = BAD_TFORM);
}
if (repeat == newveclen)
return(*status); /* column already has the desired vector length */
if (datacode == TSTRING)
width = 1; /* width was equal to width of unit string */
naxis1 = (fptr->Fptr)->rowlength; /* current width of the table */
naxis2 = (fptr->Fptr)->numrows;
delbyte = (newveclen - repeat) * width; /* no. of bytes to insert */
if (datacode == TBIT) /* BIT column is a special case */
delbyte = ((newveclen + 7) / 8) - ((repeat + 7) / 8);
if (delbyte > 0) /* insert space for more elements */
{
/* current size of data */
datasize = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize;
freespace = ( ( (datasize + 2879) / 2880) * 2880) - datasize;
nadd = (LONGLONG)delbyte * naxis2; /* no. of bytes to add to table */
if ( (freespace - nadd) < 0) /* not enough existing space? */
{
nblock = (long) ((nadd - freespace + 2879) / 2880); /* number of blocks */
if (ffiblk(fptr, nblock, 1, status) > 0) /* insert the blocks */
return(*status);
}
/* shift heap down (if it exists) */
if ((fptr->Fptr)->heapsize > 0)
{
nbytes = (fptr->Fptr)->heapsize; /* no. of bytes to shift down */
/* absolute heap pos */
firstbyte = (fptr->Fptr)->datastart + (fptr->Fptr)->heapstart;
if (ffshft(fptr, firstbyte, nbytes, nadd, status) > 0) /* move heap */
return(*status);
}
/* update the heap starting address */
(fptr->Fptr)->heapstart += nadd;
/* update the THEAP keyword if it exists */
tstatus = 0;
ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus);
/* Must reset colptr before using it again. (fptr->Fptr)->tableptr
may have been reallocated down in ffbinit via the call to ffiblk above.*/
colptr = (fptr->Fptr)->tableptr;
colptr += (colnum - 1);
firstcol = colptr->tbcol + (repeat * width); /* insert position */
/* insert delbyte bytes in every row, at byte position firstcol */
ffcins(fptr, naxis1, naxis2, delbyte, firstcol, status);
}
else if (delbyte < 0)
{
/* current size of table */
size = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize;
freespace = ((size + 2879) / 2880) * 2880 - size - ((LONGLONG)delbyte * naxis2);
nblock = (long) (freespace / 2880); /* number of empty blocks to delete */
firstcol = colptr->tbcol + (newveclen * width); /* delete position */
/* delete elements from the vector */
ffcdel(fptr, naxis1, naxis2, -delbyte, firstcol, status);
/* abs heap pos */
firstbyte = (fptr->Fptr)->datastart + (fptr->Fptr)->heapstart;
ndelete = (LONGLONG)delbyte * naxis2; /* size of shift (negative) */
/* shift heap up (if it exists) */
if ((fptr->Fptr)->heapsize > 0)
{
nbytes = (fptr->Fptr)->heapsize; /* no. of bytes to shift up */
if (ffshft(fptr, firstbyte, nbytes, ndelete, status) > 0)
return(*status);
}
/* delete the empty blocks at the end of the HDU */
if (nblock > 0)
ffdblk(fptr, nblock, status);
/* update the heap starting address */
(fptr->Fptr)->heapstart += ndelete; /* ndelete is negative */
/* update the THEAP keyword if it exists */
tstatus = 0;
ffmkyj(fptr, "THEAP", (fptr->Fptr)->heapstart, "&", &tstatus);
}
/* construct the new TFORM keyword for the column */
if (datacode == TBIT)
strcpy(tcode,"X");
else if (datacode == TBYTE)
strcpy(tcode,"B");
else if (datacode == TLOGICAL)
strcpy(tcode,"L");
else if (datacode == TSTRING)
strcpy(tcode,"A");
else if (datacode == TSHORT)
strcpy(tcode,"I");
else if (datacode == TLONG)
strcpy(tcode,"J");
else if (datacode == TLONGLONG)
strcpy(tcode,"K");
else if (datacode == TFLOAT)
strcpy(tcode,"E");
else if (datacode == TDOUBLE)
strcpy(tcode,"D");
else if (datacode == TCOMPLEX)
strcpy(tcode,"C");
else if (datacode == TDBLCOMPLEX)
strcpy(tcode,"M");
/* write as a double value because the LONGLONG conversion */
/* character in snprintf is platform dependent ( %lld, %ld, %I64d ) */
snprintf(tfm,FLEN_VALUE,"%.0f%s",(double) newveclen, tcode);
ffkeyn("TFORM", colnum, keyname, status); /* Keyword name */
ffmkys(fptr, keyname, tfm, "&", status); /* modify TFORM keyword */
ffmkyj(fptr, "NAXIS1", naxis1 + delbyte, "&", status); /* modify NAXIS1 */
ffrdef(fptr, status); /* reinitialize the new table structure */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffcpcl(fitsfile *infptr, /* I - FITS file pointer to input file */
fitsfile *outfptr, /* I - FITS file pointer to output file */
int incol, /* I - number of input column */
int outcol, /* I - number for output column */
int create_col, /* I - create new col if TRUE, else overwrite */
int *status) /* IO - error status */
/*
copy a column from infptr and insert it in the outfptr table.
*/
{
int tstatus, colnum, typecode, otypecode, etypecode, anynull;
int inHduType, outHduType;
long tfields, repeat, orepeat, width, owidth, nrows, outrows;
long inloop, outloop, maxloop, ndone, ntodo, npixels;
long firstrow, firstelem, ii;
char keyname[FLEN_KEYWORD], ttype[FLEN_VALUE], tform[FLEN_VALUE];
char ttype_comm[FLEN_COMMENT],tform_comm[FLEN_COMMENT];
char *lvalues = 0, nullflag, **strarray = 0;
char nulstr[] = {'\5', '\0'}; /* unique null string value */
double dnull = 0.l, *dvalues = 0;
float fnull = 0., *fvalues = 0;
long long int *jjvalues = 0;
unsigned long long int *ujjvalues = 0;
if (*status > 0)
return(*status);
if (infptr->HDUposition != (infptr->Fptr)->curhdu)
{
ffmahd(infptr, (infptr->HDUposition) + 1, NULL, status);
}
else if ((infptr->Fptr)->datastart == DATA_UNDEFINED)
ffrdef(infptr, status); /* rescan header */
inHduType = (infptr->Fptr)->hdutype;
if (outfptr->HDUposition != (outfptr->Fptr)->curhdu)
{
ffmahd(outfptr, (outfptr->HDUposition) + 1, NULL, status);
}
else if ((outfptr->Fptr)->datastart == DATA_UNDEFINED)
ffrdef(outfptr, status); /* rescan header */
outHduType = (outfptr->Fptr)->hdutype;
if (*status > 0)
return(*status);
if (inHduType == IMAGE_HDU || outHduType == IMAGE_HDU)
{
ffpmsg
("Can not copy columns to or from IMAGE HDUs (ffcpcl)");
return(*status = NOT_TABLE);
}
if ( inHduType == BINARY_TBL && outHduType == ASCII_TBL)
{
ffpmsg
("Copying from Binary table to ASCII table is not supported (ffcpcl)");
return(*status = NOT_BTABLE);
}
/* get the datatype and vector repeat length of the column */
ffgtcl(infptr, incol, &typecode, &repeat, &width, status);
/* ... and equivalent type code */
ffeqty(infptr, incol, &etypecode, 0, 0, status);
if (typecode < 0)
{
ffpmsg("Variable-length columns are not supported (ffcpcl)");
return(*status = BAD_TFORM);
}
if (create_col) /* insert new column in output table? */
{
tstatus = 0;
ffkeyn("TTYPE", incol, keyname, &tstatus);
ffgkys(infptr, keyname, ttype, ttype_comm, &tstatus);
ffkeyn("TFORM", incol, keyname, &tstatus);
if (ffgkys(infptr, keyname, tform, tform_comm, &tstatus) )
{
ffpmsg
("Could not find TTYPE and TFORM keywords in input table (ffcpcl)");
return(*status = NO_TFORM);
}
if (inHduType == ASCII_TBL && outHduType == BINARY_TBL)
{
/* convert from ASCII table to BINARY table format string */
if (typecode == TSTRING)
ffnkey(width, "A", tform, status);
else if (typecode == TLONG)
strcpy(tform, "1J");
else if (typecode == TSHORT)
strcpy(tform, "1I");
else if (typecode == TFLOAT)
strcpy(tform,"1E");
else if (typecode == TDOUBLE)
strcpy(tform,"1D");
}
if (ffgkyj(outfptr, "TFIELDS", &tfields, 0, &tstatus))
{
ffpmsg
("Could not read TFIELDS keyword in output table (ffcpcl)");
return(*status = NO_TFIELDS);
}
colnum = minvalue((int) tfields + 1, outcol); /* output col. number */
/* create the empty column */
if (fficol(outfptr, colnum, ttype, tform, status) > 0)
{
ffpmsg
("Could not append new column to output file (ffcpcl)");
return(*status);
}
if ((infptr->Fptr == outfptr->Fptr)
&& (infptr->HDUposition == outfptr->HDUposition)
&& (colnum <= incol)) {
incol++; /* the input column has been shifted over */
}
/* copy the comment strings from the input file for TTYPE and TFORM */
tstatus = 0;
ffkeyn("TTYPE", colnum, keyname, &tstatus);
ffmcom(outfptr, keyname, ttype_comm, &tstatus);
ffkeyn("TFORM", colnum, keyname, &tstatus);
ffmcom(outfptr, keyname, tform_comm, &tstatus);
/* copy other column-related keywords if they exist */
ffcpky(infptr, outfptr, incol, colnum, "TUNIT", status);
ffcpky(infptr, outfptr, incol, colnum, "TSCAL", status);
ffcpky(infptr, outfptr, incol, colnum, "TZERO", status);
ffcpky(infptr, outfptr, incol, colnum, "TDISP", status);
ffcpky(infptr, outfptr, incol, colnum, "TLMIN", status);
ffcpky(infptr, outfptr, incol, colnum, "TLMAX", status);
ffcpky(infptr, outfptr, incol, colnum, "TDIM", status);
/* WCS keywords */
ffcpky(infptr, outfptr, incol, colnum, "TCTYP", status);
ffcpky(infptr, outfptr, incol, colnum, "TCUNI", status);
ffcpky(infptr, outfptr, incol, colnum, "TCRVL", status);
ffcpky(infptr, outfptr, incol, colnum, "TCRPX", status);
ffcpky(infptr, outfptr, incol, colnum, "TCDLT", status);
ffcpky(infptr, outfptr, incol, colnum, "TCROT", status);
if (inHduType == ASCII_TBL && outHduType == BINARY_TBL)
{
/* binary tables only have TNULLn keyword for integer columns */
if (typecode == TLONG || typecode == TSHORT)
{
/* check if null string is defined; replace with integer */
ffkeyn("TNULL", incol, keyname, &tstatus);
if (ffgkys(infptr, keyname, ttype, 0, &tstatus) <= 0)
{
ffkeyn("TNULL", colnum, keyname, &tstatus);
if (typecode == TLONG)
ffpkyj(outfptr, keyname, -9999999L, "Null value", status);
else
ffpkyj(outfptr, keyname, -32768L, "Null value", status);
}
}
}
else
{
ffcpky(infptr, outfptr, incol, colnum, "TNULL", status);
}
/* rescan header to recognize the new keywords */
if (ffrdef(outfptr, status) )
return(*status);
}
else
{
colnum = outcol;
/* get the datatype and vector repeat length of the output column */
ffgtcl(outfptr, outcol, &otypecode, &orepeat, &owidth, status);
if (orepeat != repeat) {
ffpmsg("Input and output vector columns must have same length (ffcpcl)");
return(*status = BAD_TFORM);
}
}
ffgkyj(infptr, "NAXIS2", &nrows, 0, status); /* no. of input rows */
ffgkyj(outfptr, "NAXIS2", &outrows, 0, status); /* no. of output rows */
nrows = minvalue(nrows, outrows);
if (typecode == TBIT)
repeat = (repeat + 7) / 8; /* convert from bits to bytes */
else if (typecode == TSTRING && inHduType == BINARY_TBL)
repeat = repeat / width; /* convert from chars to unit strings */
/* get optimum number of rows to copy at one time */
ffgrsz(infptr, &inloop, status);
ffgrsz(outfptr, &outloop, status);
/* adjust optimum number, since 2 tables are open at once */
maxloop = minvalue(inloop, outloop); /* smallest of the 2 tables */
maxloop = maxvalue(1, maxloop / 2); /* at least 1 row */
maxloop = minvalue(maxloop, nrows); /* max = nrows to be copied */
maxloop *= repeat; /* mult by no of elements in a row */
/* allocate memory for arrays */
if (typecode == TLOGICAL)
{
lvalues = (char *) calloc(maxloop, sizeof(char) );
if (!lvalues)
{
ffpmsg
("malloc failed to get memory for logicals (ffcpcl)");
return(*status = ARRAY_TOO_BIG);
}
}
else if (typecode == TSTRING)
{
/* allocate array of pointers */
strarray = (char **) calloc(maxloop, sizeof(strarray));
/* allocate space for each string */
for (ii = 0; ii < maxloop; ii++)
strarray[ii] = (char *) calloc(width+1, sizeof(char));
}
else if (typecode == TCOMPLEX)
{
fvalues = (float *) calloc(maxloop * 2, sizeof(float) );
if (!fvalues)
{
ffpmsg
("malloc failed to get memory for complex (ffcpcl)");
return(*status = ARRAY_TOO_BIG);
}
fnull = 0.;
}
else if (typecode == TDBLCOMPLEX)
{
dvalues = (double *) calloc(maxloop * 2, sizeof(double) );
if (!dvalues)
{
ffpmsg
("malloc failed to get memory for dbl complex (ffcpcl)");
return(*status = ARRAY_TOO_BIG);
}
dnull = 0.;
}
/* These are unsigned long-long ints that are not rescaled to floating point numbers */
else if (typecode == TLONGLONG && etypecode == TULONGLONG) {
ujjvalues = (unsigned long long int *) calloc(maxloop, sizeof(unsigned long long int) );
if (!ujjvalues)
{
ffpmsg
("malloc failed to get memory for unsigned long long int (ffcpcl)");
return(*status = ARRAY_TOO_BIG);
}
}
/* These are long-long ints that are not rescaled to floating point numbers */
else if (typecode == TLONGLONG && etypecode != TDOUBLE) {
jjvalues = (long long int *) calloc(maxloop, sizeof(long long int) );
if (!jjvalues)
{
ffpmsg
("malloc failed to get memory for long long int (ffcpcl)");
return(*status = ARRAY_TOO_BIG);
}
}
else /* other numerical datatype; read them all as doubles */
{
dvalues = (double *) calloc(maxloop, sizeof(double) );
if (!dvalues)
{
ffpmsg
("malloc failed to get memory for doubles (ffcpcl)");
return(*status = ARRAY_TOO_BIG);
}
dnull = -9.99991999E31; /* use an unlikely value for nulls */
}
npixels = nrows * repeat; /* total no. of pixels to copy */
ntodo = minvalue(npixels, maxloop); /* no. to copy per iteration */
ndone = 0; /* total no. of pixels that have been copied */
while (ntodo) /* iterate through the table */
{
firstrow = ndone / repeat + 1;
firstelem = ndone - ((firstrow - 1) * repeat) + 1;
/* read from input table */
if (typecode == TLOGICAL)
ffgcl(infptr, incol, firstrow, firstelem, ntodo,
lvalues, status);
else if (typecode == TSTRING)
ffgcvs(infptr, incol, firstrow, firstelem, ntodo,
nulstr, strarray, &anynull, status);
else if (typecode == TCOMPLEX)
ffgcvc(infptr, incol, firstrow, firstelem, ntodo, fnull,
fvalues, &anynull, status);
else if (typecode == TDBLCOMPLEX)
ffgcvm(infptr, incol, firstrow, firstelem, ntodo, dnull,
dvalues, &anynull, status);
/* Neither TULONGLONG nor TLONGLONG does null checking. Whatever
null value is in input table is transferred to output table
without checking. Since the TNULL value was copied, this
should preserve null values */
else if (typecode == TLONGLONG && etypecode == TULONGLONG)
ffgcvujj(infptr, incol, firstrow, firstelem, ntodo, /*nulval*/ 0,
ujjvalues, &anynull, status);
else if (typecode == TLONGLONG && etypecode != TDOUBLE)
ffgcvjj(infptr, incol, firstrow, firstelem, ntodo, /*nulval*/ 0,
jjvalues, &anynull, status);
else /* all numerical types */
ffgcvd(infptr, incol, firstrow, firstelem, ntodo, dnull,
dvalues, &anynull, status);
if (*status > 0)
{
ffpmsg("Error reading input copy of column (ffcpcl)");
break;
}
/* write to output table */
if (typecode == TLOGICAL)
{
nullflag = 2;
ffpcnl(outfptr, colnum, firstrow, firstelem, ntodo,
lvalues, nullflag, status);
}
else if (typecode == TSTRING)
{
if (anynull)
ffpcns(outfptr, colnum, firstrow, firstelem, ntodo,
strarray, nulstr, status);
else
ffpcls(outfptr, colnum, firstrow, firstelem, ntodo,
strarray, status);
}
else if (typecode == TCOMPLEX)
{ /* doesn't support writing nulls */
ffpclc(outfptr, colnum, firstrow, firstelem, ntodo,
fvalues, status);
}
else if (typecode == TDBLCOMPLEX)
{ /* doesn't support writing nulls */
ffpclm(outfptr, colnum, firstrow, firstelem, ntodo,
dvalues, status);
}
else if (typecode == TLONGLONG && etypecode == TULONGLONG)
{ /* No null checking because we did none to read */
ffpclujj(outfptr, colnum, firstrow, firstelem, ntodo,
ujjvalues, status);
}
else if (typecode == TLONGLONG && etypecode != TDOUBLE)
{ /* No null checking because we did none to read */
ffpcljj(outfptr, colnum, firstrow, firstelem, ntodo,
jjvalues, status);
}
else /* all other numerical types */
{
if (anynull)
ffpcnd(outfptr, colnum, firstrow, firstelem, ntodo,
dvalues, dnull, status);
else
ffpcld(outfptr, colnum, firstrow, firstelem, ntodo,
dvalues, status);
}
if (*status > 0)
{
ffpmsg("Error writing output copy of column (ffcpcl)");
break;
}
npixels -= ntodo;
ndone += ntodo;
ntodo = minvalue(npixels, maxloop);
}
/* free the previously allocated memory */
if (typecode == TLOGICAL)
{
free(lvalues);
}
else if (typecode == TSTRING)
{
for (ii = 0; ii < maxloop; ii++)
free(strarray[ii]);
free(strarray);
}
if (ujjvalues) free(ujjvalues);
if (jjvalues) free(jjvalues);
if (dvalues) free(dvalues);
if (fvalues) free(fvalues);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffccls(fitsfile *infptr, /* I - FITS file pointer to input file */
fitsfile *outfptr, /* I - FITS file pointer to output file */
int incol, /* I - number of first input column */
int outcol, /* I - number for first output column */
int ncols, /* I - number of columns to copy from input to output */
int create_col, /* I - create new col if TRUE, else overwrite */
int *status) /* IO - error status */
/*
copy multiple columns from infptr and insert them in the outfptr
table. Optimized for multiple-column case since it only expands the
output file once using fits_insert_cols() instead of calling
fits_insert_col() multiple times.
*/
{
int tstatus, colnum, typecode, otypecode;
int inHduType, outHduType;
long tfields, repeat, orepeat, width, owidth;
char keyname[FLEN_KEYWORD], ttype[FLEN_VALUE], tform[FLEN_VALUE];
char ttype_comm[FLEN_COMMENT],tform_comm[FLEN_COMMENT];
int typecodes[1000];
char *ttypes[1000], *tforms[1000], keyarr[1001][FLEN_CARD];
int ikey = 0;
int icol, incol1, outcol1;
if (*status > 0)
return(*status);
/* Do not allow more than internal array limit to be copied */
if (ncols > 1000) return (*status = ARRAY_TOO_BIG);
if (infptr->HDUposition != (infptr->Fptr)->curhdu)
{
ffmahd(infptr, (infptr->HDUposition) + 1, NULL, status);
}
else if ((infptr->Fptr)->datastart == DATA_UNDEFINED)
ffrdef(infptr, status); /* rescan header */
inHduType = (infptr->Fptr)->hdutype;
if (outfptr->HDUposition != (outfptr->Fptr)->curhdu)
{
ffmahd(outfptr, (outfptr->HDUposition) + 1, NULL, status);
}
else if ((outfptr->Fptr)->datastart == DATA_UNDEFINED)
ffrdef(outfptr, status); /* rescan header */
outHduType = (outfptr->Fptr)->hdutype;
if (*status > 0)
return(*status);
if (inHduType == IMAGE_HDU || outHduType == IMAGE_HDU)
{
ffpmsg
("Can not copy columns to or from IMAGE HDUs (ffccls)");
return(*status = NOT_TABLE);
}
if ( (inHduType == BINARY_TBL && outHduType == ASCII_TBL) ||
(inHduType == ASCII_TBL && outHduType == BINARY_TBL) )
{
ffpmsg
("Copying between Binary and ASCII tables is not supported (ffccls)");
return(*status = NOT_BTABLE);
}
/* Do not allow copying multiple columns in the same HDU because the
permutations of possible overlapping copies is mind-bending */
if ((infptr->Fptr == outfptr->Fptr)
&& (infptr->HDUposition == outfptr->HDUposition))
{
ffpmsg
("Copying multiple columns in same HDU is not supported (ffccls)");
return(*status = NOT_BTABLE);
}
/* Retrieve the number of columns in output file */
tstatus=0;
if (ffgkyj(outfptr, "TFIELDS", &tfields, 0, &tstatus))
{
ffpmsg
("Could not read TFIELDS keyword in output table (ffccls)");
return(*status = NO_TFIELDS);
}
colnum = minvalue((int) tfields + 1, outcol); /* output col. number */
/* Collect data about input column (type, repeat, etc) */
for (incol1 = incol, outcol1 = colnum, icol = 0;
icol < ncols;
icol++, incol1++, outcol1++)
{
ffgtcl(infptr, incol1, &typecode, &repeat, &width, status);
if (typecode < 0)
{
ffpmsg("Variable-length columns are not supported (ffccls)");
return(*status = BAD_TFORM);
}
typecodes[icol] = typecode;
tstatus = 0;
ffkeyn("TTYPE", incol1, keyname, &tstatus);
ffgkys(infptr, keyname, ttype, ttype_comm, &tstatus);
ffkeyn("TFORM", incol1, keyname, &tstatus);
if (ffgkys(infptr, keyname, tform, tform_comm, &tstatus) )
{
ffpmsg
("Could not find TTYPE and TFORM keywords in input table (ffccls)");
return(*status = NO_TFORM);
}
/* If creating columns, we need to save these values */
if ( create_col ) {
tforms[icol] = keyarr[ikey++];
ttypes[icol] = keyarr[ikey++];
strcpy(tforms[icol], tform);
strcpy(ttypes[icol], ttype);
} else {
/* If not creating columns, then check the datatype and vector
repeat length of the output column */
ffgtcl(outfptr, outcol1, &otypecode, &orepeat, &owidth, status);
if (orepeat != repeat) {
ffpmsg("Input and output vector columns must have same length (ffccls)");
return(*status = BAD_TFORM);
}
}
}
/* Insert columns into output file and copy all meta-data
keywords, if requested */
if (create_col)
{
/* create the empty columns */
if (fficls(outfptr, colnum, ncols, ttypes, tforms, status) > 0)
{
ffpmsg
("Could not append new columns to output file (ffccls)");
return(*status);
}
/* Copy meta-data strings from input column to output */
for (incol1 = incol, outcol1 = colnum, icol = 0;
icol < ncols;
icol++, incol1++, outcol1++)
{
/* copy the comment strings from the input file for TTYPE and TFORM */
ffkeyn("TTYPE", incol1, keyname, status);
ffgkys(infptr, keyname, ttype, ttype_comm, status);
ffkeyn("TTYPE", outcol1, keyname, status);
ffmcom(outfptr, keyname, ttype_comm, status);
ffkeyn("TFORM", incol1, keyname, status);
ffgkys(infptr, keyname, tform, tform_comm, status);
ffkeyn("TFORM", outcol1, keyname, status);
ffmcom(outfptr, keyname, tform_comm, status);
/* copy other column-related keywords if they exist */
ffcpky(infptr, outfptr, incol1, outcol1, "TUNIT", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TSCAL", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TZERO", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TDISP", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TLMIN", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TLMAX", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TDIM", status);
/* WCS keywords */
ffcpky(infptr, outfptr, incol1, outcol1, "TCTYP", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TCUNI", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TCRVL", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TCRPX", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TCDLT", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TCROT", status);
ffcpky(infptr, outfptr, incol1, outcol1, "TNULL", status);
}
/* rescan header to recognize the new keywords */
if (ffrdef(outfptr, status) )
return(*status);
}
/* Copy columns using standard ffcpcl(); do this in a loop because
the I/O-intensive column expanding is done */
for (incol1 = incol, outcol1 = colnum, icol = 0;
icol < ncols;
icol++, incol1++, outcol1++)
{
ffcpcl(infptr, outfptr, incol1, outcol1, 0, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffcprw(fitsfile *infptr, /* I - FITS file pointer to input file */
fitsfile *outfptr, /* I - FITS file pointer to output file */
LONGLONG firstrow, /* I - number of first row to copy (1 based) */
LONGLONG nrows, /* I - number of rows to copy */
int *status) /* IO - error status */
/*
copy consecutive set of rows from infptr and append it in the outfptr table.
*/
{
LONGLONG innaxis1, innaxis2, outnaxis1, outnaxis2, ii, jj, icol;
LONGLONG iVarCol, inPos, outPos, nVarBytes, nVarAllocBytes = 0;
unsigned char *buffer, *varColBuff=0;
int nInVarCols=0, nOutVarCols=0, varColDiff=0;
int *inVarCols=0, *outVarCols=0;
long nNewBlocks;
LONGLONG hrepeat=0, hoffset=0;
tcolumn *colptr=0;
if (*status > 0)
return(*status);
if (infptr->HDUposition != (infptr->Fptr)->curhdu)
{
ffmahd(infptr, (infptr->HDUposition) + 1, NULL, status);
}
else if ((infptr->Fptr)->datastart == DATA_UNDEFINED)
ffrdef(infptr, status); /* rescan header */
if (outfptr->HDUposition != (outfptr->Fptr)->curhdu)
{
ffmahd(outfptr, (outfptr->HDUposition) + 1, NULL, status);
}
else if ((outfptr->Fptr)->datastart == DATA_UNDEFINED)
ffrdef(outfptr, status); /* rescan header */
if (*status > 0)
return(*status);
if ((infptr->Fptr)->hdutype == IMAGE_HDU || (outfptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg
("Can not copy rows to or from IMAGE HDUs (ffcprw)");
return(*status = NOT_TABLE);
}
if ( ((infptr->Fptr)->hdutype == BINARY_TBL && (outfptr->Fptr)->hdutype == ASCII_TBL) ||
((infptr->Fptr)->hdutype == ASCII_TBL && (outfptr->Fptr)->hdutype == BINARY_TBL) )
{
ffpmsg
("Copying rows between Binary and ASCII tables is not supported (ffcprw)");
return(*status = NOT_BTABLE);
}
ffgkyjj(infptr, "NAXIS1", &innaxis1, 0, status); /* width of input rows */
ffgkyjj(infptr, "NAXIS2", &innaxis2, 0, status); /* no. of input rows */
ffgkyjj(outfptr, "NAXIS1", &outnaxis1, 0, status); /* width of output rows */
ffgkyjj(outfptr, "NAXIS2", &outnaxis2, 0, status); /* no. of output rows */
if (*status > 0)
return(*status);
if (outnaxis1 != innaxis1) {
ffpmsg
("Input and output tables do not have same width (ffcprw)");
return(*status = BAD_ROW_WIDTH);
}
if (firstrow + nrows - 1 > innaxis2) {
ffpmsg
("Not enough rows in input table to copy (ffcprw)");
return(*status = BAD_ROW_NUM);
}
if ((infptr->Fptr)->tfield != (outfptr->Fptr)->tfield)
{
ffpmsg
("Input and output tables do not have same number of columns (ffcprw)");
return(*status = BAD_COL_NUM);
}
/* allocate buffer to hold 1 row of data */
buffer = malloc( (size_t) innaxis1);
if (!buffer) {
ffpmsg
("Unable to allocate memory (ffcprw)");
return(*status = MEMORY_ALLOCATION);
}
inVarCols = malloc(infptr->Fptr->tfield*sizeof(int));
outVarCols = malloc(outfptr->Fptr->tfield*sizeof(int));
fffvcl(infptr, &nInVarCols, inVarCols, status);
fffvcl(outfptr, &nOutVarCols, outVarCols, status);
if (nInVarCols != nOutVarCols)
varColDiff=1;
else
{
for (ii=0; ii<nInVarCols; ++ii)
{
if (inVarCols[ii] != outVarCols[ii])
{
varColDiff=1;
break;
}
}
}
if (varColDiff)
{
ffpmsg("Input and output tables have different variable columns (ffcprw)");
*status = BAD_COL_NUM;
goto CLEANUP_RETURN;
}
jj = outnaxis2 + 1;
if (nInVarCols)
{
ffirow(outfptr, outnaxis2, nrows, status);
for (ii = firstrow; ii < firstrow + nrows; ii++)
{
fits_read_tblbytes (infptr, ii, 1, innaxis1, buffer, status);
fits_write_tblbytes(outfptr, jj, 1, innaxis1, buffer, status);
/* Now make corrections for variable length columns */
iVarCol=0;
colptr = (infptr->Fptr)->tableptr;
for (icol=0; icol<(infptr->Fptr)->tfield; ++icol)
{
if (iVarCol < nInVarCols && inVarCols[iVarCol] == icol+1)
{
/* Copy from a variable length column */
ffgdesll(infptr, icol+1, ii, &hrepeat, &hoffset, status);
/* If this is a bit column, hrepeat will be number of
bits, not bytes. If it is a string column, hrepeat
is the number of bytes, twidth is the max col width
and can be ignored.*/
if (colptr->tdatatype == -TBIT)
{
nVarBytes = (hrepeat+7)/8;
}
else if (colptr->tdatatype == -TSTRING)
{
nVarBytes = hrepeat;
}
else
{
nVarBytes = hrepeat*colptr->twidth*sizeof(char);
}
inPos = (infptr->Fptr)->datastart + (infptr->Fptr)->heapstart
+ hoffset;
outPos = (outfptr->Fptr)->datastart + (outfptr->Fptr)->heapstart
+ (outfptr->Fptr)->heapsize;
ffmbyt(infptr, inPos, REPORT_EOF, status);
/* If this is not the last HDU in the file, then check if */
/* extending the heap would overwrite the following header. */
/* If so, then have to insert more blocks. */
if ( !((outfptr->Fptr)->lasthdu) )
{
if (outPos+nVarBytes >
(outfptr->Fptr)->headstart[(outfptr->Fptr)->curhdu+1])
{
nNewBlocks = (long)(((outPos+nVarBytes - 1 -
(outfptr->Fptr)->headstart[(outfptr->Fptr)->
curhdu+1]) / 2880) + 1);
if (ffiblk(outfptr, nNewBlocks, 1, status) > 0)
{
ffpmsg("Failed to extend the size of the variable length heap (ffcprw)");
goto CLEANUP_RETURN;
}
}
}
if (nVarBytes)
{
if (nVarBytes > nVarAllocBytes)
{
/* Grow the copy buffer to accomodate the new maximum size.
Note it is safe to call realloc() with null input pointer,
which is equivalent to malloc(). */
unsigned char *varColBuff1 = (unsigned char *) realloc(varColBuff, nVarBytes);
if (! varColBuff1)
{
*status = MEMORY_ALLOCATION;
ffpmsg("failed to allocate memory for variable column copy (ffcprw)");
goto CLEANUP_RETURN;
}
/* Record the new state */
varColBuff = varColBuff1;
nVarAllocBytes = nVarBytes;
}
/* Copy date from input to output */
ffgbyt(infptr, nVarBytes, varColBuff, status);
ffmbyt(outfptr, outPos, IGNORE_EOF, status);
ffpbyt(outfptr, nVarBytes, varColBuff, status);
}
ffpdes(outfptr, icol+1, jj, hrepeat, (outfptr->Fptr)->heapsize, status);
(outfptr->Fptr)->heapsize += nVarBytes;
++iVarCol;
}
++colptr;
}
++jj;
}
}
else
{
/* copy the rows, 1 at a time */
for (ii = firstrow; ii < firstrow + nrows; ii++) {
fits_read_tblbytes (infptr, ii, 1, innaxis1, buffer, status);
fits_write_tblbytes(outfptr, jj, 1, innaxis1, buffer, status);
jj++;
}
}
outnaxis2 += nrows;
fits_update_key(outfptr, TLONGLONG, "NAXIS2", &outnaxis2, 0, status);
CLEANUP_RETURN:
free(buffer);
free(inVarCols);
free(outVarCols);
if (varColBuff) free(varColBuff);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffcpsr(fitsfile *infptr, /* I - FITS file pointer to input file */
fitsfile *outfptr, /* I - FITS file pointer to output file */
LONGLONG firstrow, /* I - number of first row to copy (1 based) */
LONGLONG nrows, /* I - number of rows to copy */
char *row_status, /* I - quality list of rows to keep (1) or not keep (0) */
int *status) /* IO - error status */
/*
copy consecutive set of rows from infptr and append it in the outfptr table.
*/
{
LONGLONG innaxis1, innaxis2, outnaxis1, outnaxis2, ii, jj, i0, icol;
LONGLONG iVarCol, inPos, outPos, nVarBytes, nVarAllocBytes = 0;
unsigned char *buffer, *varColBuff=0;
int nInVarCols=0, nOutVarCols=0, varColDiff=0;
int *inVarCols=0, *outVarCols=0;
long nNewBlocks;
LONGLONG hrepeat=0, hoffset=0;
tcolumn *colptr=0;
LONGLONG n_good_rows = nrows;
if (*status > 0)
return(*status);
if (infptr->HDUposition != (infptr->Fptr)->curhdu)
{
ffmahd(infptr, (infptr->HDUposition) + 1, NULL, status);
}
else if ((infptr->Fptr)->datastart == DATA_UNDEFINED)
ffrdef(infptr, status); /* rescan header */
if (outfptr->HDUposition != (outfptr->Fptr)->curhdu)
{
ffmahd(outfptr, (outfptr->HDUposition) + 1, NULL, status);
}
else if ((outfptr->Fptr)->datastart == DATA_UNDEFINED)
ffrdef(outfptr, status); /* rescan header */
if (*status > 0)
return(*status);
if ((infptr->Fptr)->hdutype == IMAGE_HDU || (outfptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg
("Can not copy rows to or from IMAGE HDUs (ffcprw)");
return(*status = NOT_TABLE);
}
if ( ((infptr->Fptr)->hdutype == BINARY_TBL && (outfptr->Fptr)->hdutype == ASCII_TBL) ||
((infptr->Fptr)->hdutype == ASCII_TBL && (outfptr->Fptr)->hdutype == BINARY_TBL) )
{
ffpmsg
("Copying rows between Binary and ASCII tables is not supported (ffcprw)");
return(*status = NOT_BTABLE);
}
ffgkyjj(infptr, "NAXIS1", &innaxis1, 0, status); /* width of input rows */
ffgkyjj(infptr, "NAXIS2", &innaxis2, 0, status); /* no. of input rows */
ffgkyjj(outfptr, "NAXIS1", &outnaxis1, 0, status); /* width of output rows */
ffgkyjj(outfptr, "NAXIS2", &outnaxis2, 0, status); /* no. of output rows */
if (*status > 0)
return(*status);
if (outnaxis1 != innaxis1) {
ffpmsg
("Input and output tables do not have same width (ffcprw)");
return(*status = BAD_ROW_WIDTH);
}
if (firstrow + nrows - 1 > innaxis2) {
ffpmsg
("Not enough rows in input table to copy (ffcprw)");
return(*status = BAD_ROW_NUM);
}
if ((infptr->Fptr)->tfield != (outfptr->Fptr)->tfield)
{
ffpmsg
("Input and output tables do not have same number of columns (ffcprw)");
return(*status = BAD_COL_NUM);
}
/* allocate buffer to hold 1 row of data */
buffer = malloc( (size_t) innaxis1);
if (!buffer) {
ffpmsg
("Unable to allocate memory (ffcprw)");
return(*status = MEMORY_ALLOCATION);
}
inVarCols = malloc(infptr->Fptr->tfield*sizeof(int));
outVarCols = malloc(outfptr->Fptr->tfield*sizeof(int));
fffvcl(infptr, &nInVarCols, inVarCols, status);
fffvcl(outfptr, &nOutVarCols, outVarCols, status);
if (nInVarCols != nOutVarCols)
varColDiff=1;
else
{
for (ii=0; ii<nInVarCols; ++ii)
{
if (inVarCols[ii] != outVarCols[ii])
{
varColDiff=1;
break;
}
}
}
if (varColDiff)
{
ffpmsg("Input and output tables have different variable columns (ffcprw)");
*status = BAD_COL_NUM;
goto CLEANUP_RETURN;
}
jj = outnaxis2 + 1;
if (nInVarCols)
{
if (row_status) {
for (n_good_rows = 0, ii = 0; ii < nrows; ii++) {
if (row_status[ii]) n_good_rows++;
}
}
ffirow(outfptr, outnaxis2, n_good_rows, status);
for (ii = firstrow, i0 = 0; i0 < nrows; i0++, ii++)
{
/* Ignore rows with row_status[] == 0 */
if (row_status && !row_status[i0]) continue;
fits_read_tblbytes (infptr, ii, 1, innaxis1, buffer, status);
fits_write_tblbytes(outfptr, jj, 1, innaxis1, buffer, status);
/* Now make corrections for variable length columns */
iVarCol=0;
colptr = (infptr->Fptr)->tableptr;
for (icol=0; icol<(infptr->Fptr)->tfield; ++icol)
{
if (iVarCol < nInVarCols && inVarCols[iVarCol] == icol+1)
{
/* Copy from a variable length column */
ffgdesll(infptr, icol+1, ii, &hrepeat, &hoffset, status);
/* If this is a bit column, hrepeat will be number of
bits, not bytes. If it is a string column, hrepeat
is the number of bytes, twidth is the max col width
and can be ignored.*/
if (colptr->tdatatype == -TBIT)
{
nVarBytes = (hrepeat+7)/8;
}
else if (colptr->tdatatype == -TSTRING)
{
nVarBytes = hrepeat;
}
else
{
nVarBytes = hrepeat*colptr->twidth*sizeof(char);
}
inPos = (infptr->Fptr)->datastart + (infptr->Fptr)->heapstart
+ hoffset;
outPos = (outfptr->Fptr)->datastart + (outfptr->Fptr)->heapstart
+ (outfptr->Fptr)->heapsize;
ffmbyt(infptr, inPos, REPORT_EOF, status);
/* If this is not the last HDU in the file, then check if */
/* extending the heap would overwrite the following header. */
/* If so, then have to insert more blocks. */
if ( !((outfptr->Fptr)->lasthdu) )
{
if (outPos+nVarBytes >
(outfptr->Fptr)->headstart[(outfptr->Fptr)->curhdu+1])
{
nNewBlocks = (long)(((outPos+nVarBytes - 1 -
(outfptr->Fptr)->headstart[(outfptr->Fptr)->
curhdu+1]) / 2880) + 1);
if (ffiblk(outfptr, nNewBlocks, 1, status) > 0)
{
ffpmsg("Failed to extend the size of the variable length heap (ffcprw)");
goto CLEANUP_RETURN;
}
}
}
if (nVarBytes)
{
if (nVarBytes > nVarAllocBytes)
{
/* Grow the copy buffer to accomodate the new maximum size.
Note it is safe to call realloc() with null input pointer,
which is equivalent to malloc(). */
unsigned char *varColBuff1 = (unsigned char *) realloc(varColBuff, nVarBytes);
if (! varColBuff1)
{
*status = MEMORY_ALLOCATION;
ffpmsg("failed to allocate memory for variable column copy (ffcprw)");
goto CLEANUP_RETURN;
}
/* Record the new state */
varColBuff = varColBuff1;
nVarAllocBytes = nVarBytes;
}
/* Copy date from input to output */
ffgbyt(infptr, nVarBytes, varColBuff, status);
ffmbyt(outfptr, outPos, IGNORE_EOF, status);
ffpbyt(outfptr, nVarBytes, varColBuff, status);
}
ffpdes(outfptr, icol+1, jj, hrepeat, (outfptr->Fptr)->heapsize, status);
(outfptr->Fptr)->heapsize += nVarBytes;
++iVarCol;
}
++colptr;
}
++jj;
}
}
else
{
/* copy the rows, 1 at a time */
n_good_rows = 0;
for (ii = firstrow, i0 = 0; i0 < nrows; i0++, ii++)
{
/* Ignore rows with row_status[] == 0 */
if (row_status && !row_status[i0]) continue;
fits_read_tblbytes (infptr, ii, 1, innaxis1, buffer, status);
fits_write_tblbytes(outfptr, jj, 1, innaxis1, buffer, status);
n_good_rows ++;
jj++;
}
}
outnaxis2 += n_good_rows;
fits_update_key(outfptr, TLONGLONG, "NAXIS2", &outnaxis2, 0, status);
CLEANUP_RETURN:
free(buffer);
free(inVarCols);
free(outVarCols);
if (varColBuff) free(varColBuff);
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffcpky(fitsfile *infptr, /* I - FITS file pointer to input file */
fitsfile *outfptr, /* I - FITS file pointer to output file */
int incol, /* I - input index number */
int outcol, /* I - output index number */
char *rootname, /* I - root name of the keyword to be copied */
int *status) /* IO - error status */
/*
copy an indexed keyword from infptr to outfptr.
*/
{
int tstatus = 0;
char keyname[FLEN_KEYWORD];
char value[FLEN_VALUE], comment[FLEN_COMMENT], card[FLEN_CARD];
ffkeyn(rootname, incol, keyname, &tstatus);
if (ffgkey(infptr, keyname, value, comment, &tstatus) <= 0)
{
ffkeyn(rootname, outcol, keyname, &tstatus);
ffmkky(keyname, value, comment, card, status);
ffprec(outfptr, card, status);
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffdcol(fitsfile *fptr, /* I - FITS file pointer */
int colnum, /* I - column to delete (1 = 1st) */
int *status) /* IO - error status */
/*
Delete a column from a table.
*/
{
int ii, tstatus;
LONGLONG firstbyte, size, ndelete, nbytes, naxis1, naxis2, firstcol, delbyte, freespace;
LONGLONG tbcol;
long nblock, nspace;
char keyname[FLEN_KEYWORD], comm[FLEN_COMMENT];
tcolumn *colptr, *nextcol;
if (*status > 0)
return(*status);
if (fptr->HDUposition != (fptr->Fptr)->curhdu)
{
ffmahd(fptr, (fptr->HDUposition) + 1, NULL, status);
}
/* rescan header if data structure is undefined */
else if ((fptr->Fptr)->datastart == DATA_UNDEFINED)
if ( ffrdef(fptr, status) > 0)
return(*status);
if ((fptr->Fptr)->hdutype == IMAGE_HDU)
{
ffpmsg
("Can only delete column from TABLE or BINTABLE extension (ffdcol)");
return(*status = NOT_TABLE);
}
if (colnum < 1 || colnum > (fptr->Fptr)->tfield )
return(*status = BAD_COL_NUM);
colptr = (fptr->Fptr)->tableptr;
colptr += (colnum - 1);
firstcol = colptr->tbcol; /* starting byte position of the column */
/* use column width to determine how many bytes to delete in each row */
if ((fptr->Fptr)->hdutype == ASCII_TBL)
{
delbyte = colptr->twidth; /* width of ASCII column */
if (colnum < (fptr->Fptr)->tfield) /* check for space between next column */
{
nextcol = colptr + 1;
nspace = (long) ((nextcol->tbcol) - (colptr->tbcol) - delbyte);
if (nspace > 0)
delbyte++;
}
else if (colnum > 1) /* check for space between last 2 columns */
{
nextcol = colptr - 1;
nspace = (long) ((colptr->tbcol) - (nextcol->tbcol) - (nextcol->twidth));
if (nspace > 0)
{
delbyte++;
firstcol--; /* delete the leading space */
}
}
}
else /* a binary table */
{
if (colnum < (fptr->Fptr)->tfield)
{
nextcol = colptr + 1;
delbyte = (nextcol->tbcol) - (colptr->tbcol);
}
else
{
delbyte = ((fptr->Fptr)->rowlength) - (colptr->tbcol);
}
}
naxis1 = (fptr->Fptr)->rowlength; /* current width of the table */
naxis2 = (fptr->Fptr)->numrows;
/* current size of table */
size = (fptr->Fptr)->heapstart + (fptr->Fptr)->heapsize;
freespace = ((LONGLONG)delbyte * naxis2) + ((size + 2879) / 2880) * 2880 - size;
nblock = (long) (freespace / 2880); /* number of empty blocks to delete */
ffcdel(fptr, naxis1, naxis2, delbyte, firstcol, status); /* delete col */
/* absolute heap position */
firstbyte = (fptr->Fptr)->datastart + (fptr->Fptr)->heapstart;
ndelete = (LONGLONG)delbyte * naxis2; /* size of shift */
/* shift heap up (if it exists) */
if ((fptr->Fptr)->heapsize > 0)
{
nbytes = (fptr->Fptr)->heapsize; /* no. of bytes to shift up */
if (ffshft(fptr, firstbyte, nbytes, -ndelete, status) > 0) /* mv heap */
return(*status);
}
/* delete the empty blocks at the end of the HDU */
if (nblock > 0)
ffdblk(fptr, nblock, status);
/* update the heap starting address */
(fptr->Fptr)->heapstart -= ndelete;
/* update the THEAP keyword if it exists */
tstatus = 0;
ffmkyj(fptr, "THEAP", (long)(fptr->Fptr)->heapstart, "&", &tstatus);
if ((fptr->Fptr)->hdutype == ASCII_TBL)
{
/* adjust the TBCOL values of the remaining columns */
for (ii = 1; ii <= (fptr->Fptr)->tfield; ii++)
{
ffkeyn("TBCOL", ii, keyname, status);
ffgkyjj(fptr, keyname, &tbcol, comm, status);
if (tbcol > firstcol)
{
tbcol = tbcol - delbyte;
ffmkyj(fptr, keyname, tbcol, "&", status);
}
}
}
/* update the mandatory keywords */
ffmkyj(fptr, "TFIELDS", ((fptr->Fptr)->tfield) - 1, "&", status);
ffmkyj(fptr, "NAXIS1", naxis1 - delbyte, "&", status);
/*
delete the index keywords starting with 'T' associated with the
deleted column and subtract 1 from index of all higher keywords
*/
ffkshf(fptr, colnum, (fptr->Fptr)->tfield, -1, status);
ffrdef(fptr, status); /* initialize the new table structure */
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffcins(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG naxis1, /* I - width of the table, in bytes */
LONGLONG naxis2, /* I - number of rows in the table */
LONGLONG ninsert, /* I - number of bytes to insert in each row */
LONGLONG bytepos, /* I - rel. position in row to insert bytes */
int *status) /* IO - error status */
/*
Insert 'ninsert' bytes into each row of the table at position 'bytepos'.
*/
{
unsigned char buffer[10000], cfill;
LONGLONG newlen, fbyte, nbytes, irow, nseg, ii;
if (*status > 0)
return(*status);
if (naxis2 == 0)
return(*status); /* just return if there are 0 rows in the table */
/* select appropriate fill value */
if ((fptr->Fptr)->hdutype == ASCII_TBL)
cfill = 32; /* ASCII tables use blank fill */
else
cfill = 0; /* primary array and binary tables use zero fill */
newlen = naxis1 + ninsert;
if (newlen <= 10000)
{
/*******************************************************************
CASE #1: optimal case where whole new row fits in the work buffer
*******************************************************************/
for (ii = 0; ii < ninsert; ii++)
buffer[ii] = cfill; /* initialize buffer with fill value */
/* first move the trailing bytes (if any) in the last row */
fbyte = bytepos + 1;
nbytes = naxis1 - bytepos;
/* If the last row hasn't yet been accessed in full, it's possible
that logfilesize hasn't been updated to account for it (by way
of an ffldrc call). This could cause ffgtbb to return with an
EOF error. To prevent this, we must increase logfilesize here.
*/
if ((fptr->Fptr)->logfilesize < (fptr->Fptr)->datastart +
(fptr->Fptr)->heapstart)
{
(fptr->Fptr)->logfilesize = (((fptr->Fptr)->datastart +
(fptr->Fptr)->heapstart + 2879)/2880)*2880;
}
ffgtbb(fptr, naxis2, fbyte, nbytes, &buffer[ninsert], status);
(fptr->Fptr)->rowlength = newlen; /* new row length */
/* write the row (with leading fill bytes) in the new place */
nbytes += ninsert;
ffptbb(fptr, naxis2, fbyte, nbytes, buffer, status);
(fptr->Fptr)->rowlength = naxis1; /* reset to orig. value */
/* now move the rest of the rows */
for (irow = naxis2 - 1; irow > 0; irow--)
{
/* read the row to be shifted (work backwards thru the table) */
ffgtbb(fptr, irow, fbyte, naxis1, &buffer[ninsert], status);
(fptr->Fptr)->rowlength = newlen; /* new row length */
/* write the row (with the leading fill bytes) in the new place */
ffptbb(fptr, irow, fbyte, newlen, buffer, status);
(fptr->Fptr)->rowlength = naxis1; /* reset to orig value */
}
}
else
{
/*****************************************************************
CASE #2: whole row doesn't fit in work buffer; move row in pieces
******************************************************************
first copy the data, then go back and write fill into the new column
start by copying the trailing bytes (if any) in the last row. */
nbytes = naxis1 - bytepos;
nseg = (nbytes + 9999) / 10000;
fbyte = (nseg - 1) * 10000 + bytepos + 1;
nbytes = naxis1 - fbyte + 1;
for (ii = 0; ii < nseg; ii++)
{
ffgtbb(fptr, naxis2, fbyte, nbytes, buffer, status);
(fptr->Fptr)->rowlength = newlen; /* new row length */
ffptbb(fptr, naxis2, fbyte + ninsert, nbytes, buffer, status);
(fptr->Fptr)->rowlength = naxis1; /* reset to orig value */
fbyte -= 10000;
nbytes = 10000;
}
/* now move the rest of the rows */
nseg = (naxis1 + 9999) / 10000;
for (irow = naxis2 - 1; irow > 0; irow--)
{
fbyte = (nseg - 1) * 10000 + bytepos + 1;
nbytes = naxis1 - (nseg - 1) * 10000;
for (ii = 0; ii < nseg; ii++)
{
/* read the row to be shifted (work backwards thru the table) */
ffgtbb(fptr, irow, fbyte, nbytes, buffer, status);
(fptr->Fptr)->rowlength = newlen; /* new row length */
/* write the row in the new place */
ffptbb(fptr, irow, fbyte + ninsert, nbytes, buffer, status);
(fptr->Fptr)->rowlength = naxis1; /* reset to orig value */
fbyte -= 10000;
nbytes = 10000;
}
}
/* now write the fill values into the new column */
nbytes = minvalue(ninsert, 10000);
memset(buffer, cfill, (size_t) nbytes); /* initialize with fill value */
nseg = (ninsert + 9999) / 10000;
(fptr->Fptr)->rowlength = newlen; /* new row length */
for (irow = 1; irow <= naxis2; irow++)
{
fbyte = bytepos + 1;
nbytes = ninsert - ((nseg - 1) * 10000);
for (ii = 0; ii < nseg; ii++)
{
ffptbb(fptr, irow, fbyte, nbytes, buffer, status);
fbyte += nbytes;
nbytes = 10000;
}
}
(fptr->Fptr)->rowlength = naxis1; /* reset to orig value */
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffcdel(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG naxis1, /* I - width of the table, in bytes */
LONGLONG naxis2, /* I - number of rows in the table */
LONGLONG ndelete, /* I - number of bytes to delete in each row */
LONGLONG bytepos, /* I - rel. position in row to delete bytes */
int *status) /* IO - error status */
/*
delete 'ndelete' bytes from each row of the table at position 'bytepos'. */
{
unsigned char buffer[10000];
LONGLONG i1, i2, ii, irow, nseg;
LONGLONG newlen, remain, nbytes;
if (*status > 0)
return(*status);
if (naxis2 == 0)
return(*status); /* just return if there are 0 rows in the table */
newlen = naxis1 - ndelete;
if (newlen <= 10000)
{
/*******************************************************************
CASE #1: optimal case where whole new row fits in the work buffer
*******************************************************************/
i1 = bytepos + 1;
i2 = i1 + ndelete;
for (irow = 1; irow < naxis2; irow++)
{
ffgtbb(fptr, irow, i2, newlen, buffer, status); /* read row */
(fptr->Fptr)->rowlength = newlen; /* new row length */
ffptbb(fptr, irow, i1, newlen, buffer, status); /* write row */
(fptr->Fptr)->rowlength = naxis1; /* reset to orig value */
}
/* now do the last row */
remain = naxis1 - (bytepos + ndelete);
if (remain > 0)
{
ffgtbb(fptr, naxis2, i2, remain, buffer, status); /* read row */
(fptr->Fptr)->rowlength = newlen; /* new row length */
ffptbb(fptr, naxis2, i1, remain, buffer, status); /* write row */
(fptr->Fptr)->rowlength = naxis1; /* reset to orig value */
}
}
else
{
/*****************************************************************
CASE #2: whole row doesn't fit in work buffer; move row in pieces
******************************************************************/
nseg = (newlen + 9999) / 10000;
for (irow = 1; irow < naxis2; irow++)
{
i1 = bytepos + 1;
i2 = i1 + ndelete;
nbytes = newlen - (nseg - 1) * 10000;
for (ii = 0; ii < nseg; ii++)
{
ffgtbb(fptr, irow, i2, nbytes, buffer, status); /* read bytes */
(fptr->Fptr)->rowlength = newlen; /* new row length */
ffptbb(fptr, irow, i1, nbytes, buffer, status); /* rewrite bytes */
(fptr->Fptr)->rowlength = naxis1; /* reset to orig value */
i1 += nbytes;
i2 += nbytes;
nbytes = 10000;
}
}
/* now do the last row */
remain = naxis1 - (bytepos + ndelete);
if (remain > 0)
{
nseg = (remain + 9999) / 10000;
i1 = bytepos + 1;
i2 = i1 + ndelete;
nbytes = remain - (nseg - 1) * 10000;
for (ii = 0; ii < nseg; ii++)
{
ffgtbb(fptr, naxis2, i2, nbytes, buffer, status);
(fptr->Fptr)->rowlength = newlen; /* new row length */
ffptbb(fptr, naxis2, i1, nbytes, buffer, status); /* write row */
(fptr->Fptr)->rowlength = naxis1; /* reset to orig value */
i1 += nbytes;
i2 += nbytes;
nbytes = 10000;
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffkshf(fitsfile *fptr, /* I - FITS file pointer */
int colmin, /* I - starting col. to be incremented; 1 = 1st */
int colmax, /* I - last column to be incremented */
int incre, /* I - shift index number by this amount */
int *status) /* IO - error status */
/*
shift the index value on any existing column keywords
This routine will modify the name of any keyword that begins with 'T'
and has an index number in the range COLMIN - COLMAX, inclusive.
if incre is positive, then the index values will be incremented.
if incre is negative, then the kewords with index = COLMIN
will be deleted and the index of higher numbered keywords will
be decremented.
*/
{
int nkeys, nmore, nrec, tstatus, i1;
long ivalue;
char rec[FLEN_CARD], q[FLEN_KEYWORD], newkey[FLEN_KEYWORD];
ffghsp(fptr, &nkeys, &nmore, status); /* get number of keywords */
/* go thru header starting with the 9th keyword looking for 'TxxxxNNN' */
for (nrec = 9; nrec <= nkeys; nrec++)
{
ffgrec(fptr, nrec, rec, status);
if (rec[0] == 'T')
{
i1 = 0;
strncpy(q, &rec[1], 4);
if (!strncmp(q, "BCOL", 4) || !strncmp(q, "FORM", 4) ||
!strncmp(q, "TYPE", 4) || !strncmp(q, "SCAL", 4) ||
!strncmp(q, "UNIT", 4) || !strncmp(q, "NULL", 4) ||
!strncmp(q, "ZERO", 4) || !strncmp(q, "DISP", 4) ||
!strncmp(q, "LMIN", 4) || !strncmp(q, "LMAX", 4) ||
!strncmp(q, "DMIN", 4) || !strncmp(q, "DMAX", 4) ||
!strncmp(q, "CTYP", 4) || !strncmp(q, "CRPX", 4) ||
!strncmp(q, "CRVL", 4) || !strncmp(q, "CDLT", 4) ||
!strncmp(q, "CROT", 4) || !strncmp(q, "CUNI", 4) )
i1 = 5;
else if (!strncmp(rec, "TDIM", 4) )
i1 = 4;
if (i1)
{
/* try reading the index number suffix */
q[0] = '\0';
strncat(q, &rec[i1], 8 - i1);
tstatus = 0;
ffc2ii(q, &ivalue, &tstatus);
if (tstatus == 0 && ivalue >= colmin && ivalue <= colmax)
{
if (incre <= 0 && ivalue == colmin)
{
ffdrec(fptr, nrec, status); /* delete keyword */
nkeys = nkeys - 1;
nrec = nrec - 1;
}
else
{
ivalue = ivalue + incre;
q[0] = '\0';
strncat(q, rec, i1);
ffkeyn(q, ivalue, newkey, status);
/* NOTE: because of null termination, it is not
equivalent to use strcpy() for the same calls */
strncpy(rec, " ", 8); /* erase old keyword name */
i1 = strlen(newkey);
strncpy(rec, newkey, i1); /* overwrite new keyword name */
ffmrec(fptr, nrec, rec, status); /* modify the record */
}
}
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int fffvcl(fitsfile *fptr, /* I - FITS file pointer */
int *nvarcols, /* O - Number of variable length columns found */
int *colnums, /* O - 1-based variable column positions */
int *status) /* IO - error status */
{
/*
Internal function to identify which columns in a binary table are variable length.
The colnums array will be filled with nvarcols elements - the 1-based numbers
of all variable length columns in the table. This ASSUMES calling function
has passed in a colnums array large enough to hold these (colnums==NULL also
allowed).
*/
int tfields=0,icol;
tcolumn *colptr=0;
*nvarcols = 0;
if (*status > 0)
return(*status);
if ((fptr->Fptr)->hdutype != BINARY_TBL)
{
ffpmsg("Var-length column search can only be performed on Binary tables (fffvcl)");
return(*status = NOT_BTABLE);
}
if ((fptr->Fptr)->tableptr)
{
colptr = (fptr->Fptr)->tableptr;
tfields = (fptr->Fptr)->tfield;
for (icol=0; icol<tfields; ++icol, ++colptr)
{
/* Condition for variable length column: negative tdatatype */
if (colptr->tdatatype < 0)
{
if (colnums) colnums[*nvarcols] = icol + 1;
*nvarcols += 1;
}
}
}
return(*status);
}
/*--------------------------------------------------------------------------*/
int ffshft(fitsfile *fptr, /* I - FITS file pointer */
LONGLONG firstbyte, /* I - position of first byte in block to shift */
LONGLONG nbytes, /* I - size of block of bytes to shift */
LONGLONG nshift, /* I - size of shift in bytes (+ or -) */
int *status) /* IO - error status */
/*
Shift block of bytes by nshift bytes (positive or negative).
A positive nshift value moves the block down further in the file, while a
negative value shifts the block towards the beginning of the file.
*/
{
#define shftbuffsize 100000
long ntomov;
LONGLONG ptr, ntodo;
char buffer[shftbuffsize];
if (*status > 0)
return(*status);
ntodo = nbytes; /* total number of bytes to shift */
if (nshift > 0)
/* start at the end of the block and work backwards */
ptr = firstbyte + nbytes;
else
/* start at the beginning of the block working forwards */
ptr = firstbyte;
while (ntodo)
{
/* number of bytes to move at one time */
ntomov = (long) (minvalue(ntodo, shftbuffsize));
if (nshift > 0) /* if moving block down ... */
ptr -= ntomov;
/* move to position and read the bytes to be moved */
ffmbyt(fptr, ptr, REPORT_EOF, status);
ffgbyt(fptr, ntomov, buffer, status);
/* move by shift amount and write the bytes */
ffmbyt(fptr, ptr + nshift, IGNORE_EOF, status);
if (ffpbyt(fptr, ntomov, buffer, status) > 0)
{
ffpmsg("Error while shifting block (ffshft)");
return(*status);
}
ntodo -= ntomov;
if (nshift < 0) /* if moving block up ... */
ptr += ntomov;
}
/* now overwrite the old data with fill */
if ((fptr->Fptr)->hdutype == ASCII_TBL)
memset(buffer, 32, shftbuffsize); /* fill ASCII tables with spaces */
else
memset(buffer, 0, shftbuffsize); /* fill other HDUs with zeros */
if (nshift < 0)
{
ntodo = -nshift;
/* point to the end of the shifted block */
ptr = firstbyte + nbytes + nshift;
}
else
{
ntodo = nshift;
/* point to original beginning of the block */
ptr = firstbyte;
}
ffmbyt(fptr, ptr, REPORT_EOF, status);
while (ntodo)
{
ntomov = (long) (minvalue(ntodo, shftbuffsize));
ffpbyt(fptr, ntomov, buffer, status);
ntodo -= ntomov;
}
return(*status);
}
|