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
|
/* 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 2 special cases that represent unsigned integers */
if (longbitpix == USHORT_IMG)
longbitpix = SHORT_IMG;
else if (longbitpix == ULONG_IMG)
longbitpix = LONG_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)
{
sprintf(message,
"Illegal value for BITPIX keyword: %d", bitpix);
ffpmsg(message);
return(*status = BAD_BITPIX);
}
if (naxis < 0 || naxis > 999)
{
sprintf(message,
"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)
{
sprintf(message,
"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 * (abs(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 */
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);
}
/* 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 */
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) {
*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;
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 (fficol)");
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++)
{
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
{
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);
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 sprintf is platform dependent ( %lld, %ld, %I64d ) */
sprintf(tfm,"%.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, anynull;
long tfields, repeat, width, 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;
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 columns to or from IMAGE HDUs (ffcpcl)");
return(*status = NOT_TABLE);
}
if ( (infptr->Fptr)->hdutype == BINARY_TBL && (outfptr->Fptr)->hdutype == 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);
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 ((infptr->Fptr)->hdutype == ASCII_TBL && (outfptr->Fptr)->hdutype == 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);
}
/* 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 ((infptr->Fptr)->hdutype == ASCII_TBL && (outfptr->Fptr)->hdutype == 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;
}
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 && (infptr->Fptr)->hdutype == 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.;
}
else /* 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);
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 /* 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);
}
else
{
free(dvalues);
}
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;
unsigned char *buffer;
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);
}
/* 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);
}
/* copy the rows, 1 at a time */
jj = outnaxis2 + 1;
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);
free(buffer);
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;
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);
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 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);
}
|