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
|
/* $Id: vmimgextraction.c,v 1.6 2013-08-07 15:39:39 cgarcia Exp $
*
* This file is part of the VIMOS Pipeline
* Copyright (C) 2002-2004 European Southern Observatory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* $Author: cgarcia $
* $Date: 2013-08-07 15:39:39 $
* $Revision: 1.6 $
* $Name: not supported by cvs2svn $
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <assert.h>
#include <pilmemory.h>
#include <piltask.h>
#include <piltranslator.h>
#include <pilmessages.h>
#include <cpl_msg.h>
#include <pilstrutils.h>
#include <pilutils.h>
#include "vmimage.h"
#include "vmtable.h"
#include "vmgalaxytable.h"
#include "vmastrometrictable.h"
#include "vmstarmatchtable.h"
#include "vmstartable.h"
#include "vmsextractor.h"
#include "vmimgutils.h"
#include "vmimgextraction.h"
#define SEXTRACTOR_ARGC 20
#define MIN_DEVIATION 1.e-6
/*
* Mandatory SExtractor output columns. These columns define the VIMOS
* galaxy table datatype.
*/
static SextParameter galaxy_table_columns[] = {
{"NUMBER", SEXT_COLUMN_INT},
{"MAG_ISOCOR", SEXT_COLUMN_FLOAT},
{"MAGERR_ISOCOR", SEXT_COLUMN_FLOAT},
{"MAG_APER", SEXT_COLUMN_FLOAT},
{"MAGERR_APER", SEXT_COLUMN_FLOAT},
{"MAG_AUTO", SEXT_COLUMN_FLOAT},
{"MAGERR_AUTO", SEXT_COLUMN_FLOAT},
{"MAG_BEST", SEXT_COLUMN_FLOAT},
{"MAGERR_BEST", SEXT_COLUMN_FLOAT},
{"X_IMAGE", SEXT_COLUMN_FLOAT},
{"Y_IMAGE", SEXT_COLUMN_FLOAT},
{"X_WORLD", SEXT_COLUMN_FLOAT},
{"Y_WORLD", SEXT_COLUMN_FLOAT},
{"ISOAREA_WORLD", SEXT_COLUMN_FLOAT},
{"A_IMAGE", SEXT_COLUMN_FLOAT},
{"B_IMAGE", SEXT_COLUMN_FLOAT},
{"A_WORLD", SEXT_COLUMN_FLOAT},
{"B_WORLD", SEXT_COLUMN_FLOAT},
{"FWHM_IMAGE", SEXT_COLUMN_FLOAT},
{"FWHM_WORLD", SEXT_COLUMN_FLOAT},
{"THETA_IMAGE", SEXT_COLUMN_FLOAT},
{"ERRTHETA_IMAGE", SEXT_COLUMN_FLOAT},
{"ELLIPTICITY", SEXT_COLUMN_FLOAT},
{"CLASS_STAR", SEXT_COLUMN_FLOAT},
{"FLAGS", SEXT_COLUMN_INT},
{0, SEXT_COLUMN_UNDEF}
};
/* The following Sex columns are not used in DRS, thus they are NOT */
/* part of sex output BG */
/* {"ISO0", SEXT_COLUMN_INT}, */
/* {"ISO1", SEXT_COLUMN_INT}, */
/* {"ISO2", SEXT_COLUMN_INT}, */
/* {"ISO3", SEXT_COLUMN_INT}, */
/* {"ISO4", SEXT_COLUMN_INT}, */
/* {"ISO5", SEXT_COLUMN_INT}, */
/* {"ISO6", SEXT_COLUMN_INT}, */
/* {"ISO7", SEXT_COLUMN_INT}, */
/* {"KRON_RADIUS", SEXT_COLUMN_FLOAT}, */
/* {"BACKGROUND", SEXT_COLUMN_FLOAT}, */
/* {"THRESHOLD", SEXT_COLUMN_FLOAT}, */
/* {"MU_THRESHOLD", SEXT_COLUMN_FLOAT}, */
/* {"FLUX_MAX", SEXT_COLUMN_FLOAT}, */
/* {"MU_MAX", SEXT_COLUMN_FLOAT}, */
/* {"ISOAREA_IMAGE", SEXT_COLUMN_FLOAT}, */
/* {"A_IMAGE", SEXT_COLUMN_FLOAT}, */
/* {"B_IMAGE", SEXT_COLUMN_FLOAT}, */
/* {"THETA_IMAGE", SEXT_COLUMN_FLOAT}, */
/* {"THETA_WORLD", SEXT_COLUMN_FLOAT}, */
/* {"ELONGATION", SEXT_COLUMN_FLOAT}, */
/**
* @name VmImObjectDetection
*
* @doc
* The modules provides functions for detecting objects in an image,
* operations on source lists including correlation with catalog data.
*/
/**@{*/
/*
* @memo
* Append world coordinate system information to a keyword list.
*
* @return The function returns #EXIT_SUCCESS# if no error occurred,
* otherwise #EXIT_FAILURE# is returned.
*
* @param list Target keyword list.
* @param wcs WCS information.
*
* @doc
* The function appends the world coordinate system information found
* in \textbf{wcs} to the list of keywords specified by \textbf{list}.
* Using the WCS information from \textbf{wcs} the function appends the
* keywords
* \begin{itemize}
* \item #CRVALi#,
* \item #CRPIXi#, and
* \item #CTYPEi#
* \end{itemize}
* for i = 1, 2, the
* CD matrix, i.e. the keywords #CDi_j# for i, j = 1, 2, and the keywords
* specifying the equinox and the reference frame #EQUINOX# and #RADECSYS#.
*
* @author R. Palsa
*/
inline static int
writeWcsInfo(VimosDescriptor **list, struct WorldCoor *wcs)
{
register int i, j;
VimosDescriptor *dsc = NULL;
assert(list != 0 && wcs != 0);
/*
* Save the table type descriptor and remove it temporarily from the
* list. It will be restored later.
*/
if (vimosDscCopy(&dsc, *list, pilTrnGetKeyword("Table"), NULL) ==
EXIT_FAILURE) {
return EXIT_FAILURE;
}
else {
vimosDscErase(list, pilTrnGetKeyword("Table"));
}
if (writeDoubleDescriptor(list, pilTrnGetKeyword("Crval", 1), wcs->xref,
"HH MM SS.SS, RA at ref pixel (deg)") == VM_FALSE)
return EXIT_FAILURE;
if (writeDoubleDescriptor(list, pilTrnGetKeyword("Crval", 2), wcs->yref,
"DD MM SS, DEC at ref pixel (deg)") == VM_FALSE)
return EXIT_FAILURE;
if (writeDoubleDescriptor(list, pilTrnGetKeyword("Crpix", 1), wcs->xrefpix,
"Ref pixel in X") == VM_FALSE)
return EXIT_FAILURE;
if (writeDoubleDescriptor(list, pilTrnGetKeyword("Crpix", 2), wcs->yrefpix,
"Ref pixel in Y") == VM_FALSE)
return EXIT_FAILURE;
if (writeStringDescriptor(list, pilTrnGetKeyword("Ctype", 1), "RA---TAN",
"Ref pixel in X") == VM_FALSE)
return EXIT_FAILURE;
if (writeStringDescriptor(list, pilTrnGetKeyword("Ctype", 2), "DEC--TAN",
"Ref pixel in Y") == VM_FALSE)
return EXIT_FAILURE;
/*
* The 4 matrix elements of the CD matrix. Stored as (1, 1), (1, 2),
* (2, 1) and (2, 2) in wcs->cd.
*/
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
if (writeDoubleDescriptor(list, pilTrnGetKeyword("CD", i + 1, j + 1),
wcs->cd[2 * i + j], "Translation matrix "
"element") == VM_FALSE)
return EXIT_FAILURE;
}
}
if (writeDoubleDescriptor(list, pilTrnGetKeyword("Equinox"), wcs->equinox,
"Standard FK5") == VM_FALSE)
return EXIT_FAILURE;
if (writeStringDescriptor(list, pilTrnGetKeyword("Radecsys"), wcs->radecsys,
"FK5") == VM_FALSE)
return EXIT_FAILURE;
/*
* Restore the table type descriptor
*/
if (vimosDscCopy(list, dsc, pilTrnGetKeyword("Table"), NULL) ==
EXIT_FAILURE) {
return EXIT_FAILURE;
}
else {
deleteDescriptor(dsc);
}
return EXIT_SUCCESS;
}
/*
* @memo
* Sort a table.
*
* @return The function returns a pointer to the sorted table if no error
* occurred, otherwise a #NULL# pointer is returned.
*
* @param table Table object.
* @param name Name of the reference column.
*
* @doc
* The function looks for the column named \textbf{name} in the input table
* \textbf{table} and uses this column as a reference for sorting the
* table. The reference column must be of type \textbf{double} or
* \textbf{float} and its values are converted to type float for generating
* the index table, i.e you will loose precision (this will be fixed in
* future).
*
* @author R. Palsa
*/
inline static VimosTable *sortTable(VimosTable *table, const char *name)
{
const char *fctid = "sortTable";
int sz;
struct bucket_t {
size_t size;
unsigned char *data;
} bucket;
VimosColumn *column;
assert(table != 0);
assert(name != 0);
/*
* Check for the reference column.
*/
if (!(column = findColInTab(table, name))) {
cpl_msg_debug(fctid, "Table column '%s' is missing!", name);
return 0;
}
/*
* Nothing to do if the table contains less than 2 columns
*/
if ((sz = (colGetSize(column))) > 1) {
register int i;
int *indexTable;
float *buffer;
void *values;
VimosColumn *c;
/*
* Check if all columns have the same size as the reference column.
*/
c = table->cols;
while (c) {
if (colGetSize(c) != sz) {
cpl_msg_debug(fctid, "Column sizes do not match!");
return 0;
}
c = c->next;
}
/*
* Generate the index table
*/
/* FIXME: Is the extra element really needed?
*/
if ((buffer = pil_calloc(sz + 1, sizeof(float))) == 0)
return 0;
switch (column->colType) {
case VM_FLOAT:
values = colGetFloatData(column);
for (i = 0; i < sz; i++)
buffer[i] = (float) *((float *)values + i);
break;
case VM_DOUBLE:
cpl_msg_debug(fctid, "Converting double to float. Possible loss of "
"precision!");
values = colGetDoubleData(column);
for (i = 0; i < sz; i++)
buffer[i] = (float) *((double *)values + i);
break;
default:
cpl_msg_debug(fctid, "Type of column '%s' is not supported!",
name);
pil_free(buffer);
return 0;
}
/*
* Allocate working buffers
*/
if ((indexTable = pil_calloc(sz, sizeof(int))) == 0)
return 0;
Indexx(sz, buffer, indexTable);
pil_free(buffer);
/*
* Rearrange the table. There is some memory needed for temporary
* storage of the column data. This storage place is just a block of
* bytes which is accessed through the appropriate handle. Its initial
* size is calculated for a double column assuming that doubles are
* the largest type, but for being on the safe side the actual size
* is checked and the byte buffer is enlarged if necessary.
*/
bucket.size = sz * sizeof(double);
bucket.data = (unsigned char *)pil_calloc(bucket.size, sizeof(unsigned char));
if (!bucket.data) {
pil_free(indexTable);
return 0;
}
column = table->cols;
while(column) {
size_t bytes;
switch (column->colType) {
case VM_INT: {
int *handle, *columnData;
if ((bytes = sz * sizeof(int)) > bucket.size) {
if (!pil_realloc(bucket.data, bytes)) {
pil_free(bucket.data);
pil_free(indexTable);
return 0;
}
else
bucket.size = bytes;
}
columnData = colGetIntData(column);
memcpy(bucket.data, columnData, bytes);
handle = (int *)bucket.data;
for (i = 0; i < sz; i++){
columnData[i] = *(handle + indexTable[i]);
}
}
break;
case VM_FLOAT: {
float *handle, *columnData;
if ((bytes = sz * sizeof(float)) > bucket.size) {
if (!pil_realloc(bucket.data, bytes)) {
pil_free(bucket.data);
pil_free(indexTable);
return 0;
}
else
bucket.size = bytes;
}
columnData = colGetFloatData(column);
memcpy(bucket.data, columnData, bytes);
handle = (float *)bucket.data;
for (i = 0; i < sz; i++)
columnData[i] = *(handle + indexTable[i]);
}
break;
case VM_DOUBLE: {
double *handle, *columnData;
if ((bytes = sz * sizeof(double)) > bucket.size) {
if (!pil_realloc(bucket.data, bytes)) {
pil_free(bucket.data);
pil_free(indexTable);
return 0;
}
else
bucket.size = bytes;
}
columnData = colGetDoubleData(column);
memcpy(bucket.data, columnData, bytes);
handle = (double *)bucket.data;
for (i = 0; i < sz; i++)
columnData[i] = *(handle + indexTable[i]);
}
break;
case VM_STRING: {
char **handle, **columnData;
if ((bytes = sz * sizeof(char *)) > bucket.size) {
if (!pil_realloc(bucket.data, bytes)) {
pil_free(bucket.data);
pil_free(indexTable);
return 0;
}
else
bucket.size = bytes;
}
columnData = colGetStringData(column);
memcpy(bucket.data, columnData, bytes);
handle = (char **)bucket.data;
for (i = 0; i < sz; i++)
columnData[i] = *(handle + indexTable[i]);
}
break;
default:
cpl_msg_debug(fctid, "Invalid column type!");
pil_free(bucket.data);
pil_free(indexTable);
return 0;
break;
}
column = column->next;
}
pil_free(bucket.data);
pil_free(indexTable);
}
return table;
}
/*
* @memo
* Prepare an astrometric catalog for processing.
*
* @return The function returns a pointer to the updated catalog if no error
* occurred, otherwise a #NULL# pointer is returned.
*
* @param catalog Astrometric catalog.
* @param filter Filter name.
*
* @doc
* The function prepares the astrometric catalog \textbf{catalog} for
* the object identification task, i.e. the column containing the
* magnitudes for the band corresponding to the filter \textbf{filter},
* is copied to a new column with the name 'MAG'. Finally the catalog
* is sorted with respect to this column.
*
* @author R. Palsa
*/
inline static VimosTable *setupAstrometricTable(VimosTable *catalog,
const char *filter)
{
const char *fctid = "setupAstrometricTable";
char name[6] = "MAG_";
VimosColumn *column;
assert(catalog != 0 && filter != 0);
/*
* Verify the type of the input table. Just rely on the table tag here.
*/
if (strncmp(catalog->name, VM_AST, strlen(VM_AST))) {
cpl_msg_debug(fctid, "Invalid type of input catalog!");
return 0;
}
/*
* Get the wavelength band designation from the filter name.
* This seems to be pretty unreliable.
*/
switch (*filter) {
case 'U':
strcat(name, "U");
break;
case 'B':
strcat(name, "B");
break;
case 'V':
strcat(name, "V");
break;
case 'R':
strcat(name, "R");
break;
case 'I':
strcat(name, "I");
break;
case 'z':
case 'Z':
strcat(name, "z");
break;
default:
cpl_msg_debug(fctid, "Invalid filter name!");
return 0;
break;
}
/*
* Copy the appropriate column and change its name and append it to
* the table.
*/
if (!(column = tblCopyColumn(catalog, name)))
return 0;
else {
colSetName(column, "MAG");
tblAppendColumn(catalog, column);
}
/*
* Sort the catalog with respect to magnitude.
*/
if (!sortTable(catalog, "MAG")) {
cpl_msg_debug(fctid, "Cannot sort astrometric catalog by magnitude!");
return 0;
}
return catalog;
}
/**
* @memo
* Convert a SExtractor output table into a galaxy table.
*
* @return The function returns the updated table object if no error
* occurred, otherwise the return value is NULL;
*
* @param table SExtractor output catalog table object.
* @param image Source detection image used for the catalog creation.
*
* @doc
* The function adds the galaxy table tag to the input source list
* generated by SExtractor and also all keywords required to make the
* input table a valid galaxy table are added to the table header.
* Additionally required unit conversions are applied.
*
* The function also applies the sky to CCD transformation matrix to the
* coordinates of the detected objects and corrects for temperature
* effects.
*
* @author R. Palsa
*/
VimosTable *VmImBuildGalaxyTable(VimosTable *table, VimosImage *image)
{
const char fctid[] = "VmImBuildGalaxyTable";
const char *labels[] = { /* Columns which need conversions */
"A_WORLD",
"B_WORLD",
"FWHM_WORLD",
"ISOAREA_WORLD",
"MAG_ISOCOR",
"MAG_APER",
"MAG_AUTO",
"MAG_BEST",
"X_IMAGE",
"Y_IMAGE"
};
char comment[COMMENT_LENGTH];
const size_t nc = sizeof labels / sizeof(const char *);
int i, sz;
int quadrant;
double exposureTime, magnitudeCorrection, airmass;
VimosColumn *columns[nc];
/*
* Get exposure time from the input observation and compute
* the magnitude correction to account for the observation
* duration.
*/
if (readDoubleDescriptor(image->descs, pilTrnGetKeyword("ExposureTime"),
&exposureTime, comment) != VM_TRUE) {
cpl_msg_error(fctid, "Cannot get exposure time!");
return 0;
}
else
magnitudeCorrection = 2.5 * log10(exposureTime);
/*
* Check if the input table has the properties of a galaxy table,
* i.e. if all required columns and keywords a present.
*/
for (i = 0; (size_t)i < nc; i++)
if (!(columns[i] = findColInTab(table, labels[i]))) {
cpl_msg_error(fctid, "Table column '%s' is missing!", labels[i]);
return 0;
}
/*
* Check that the columns all have the same size. Again this is needed
* since a generic table may be passed.
*/
sz = colGetSize(columns[0]);
for (i = 1; (size_t)i < nc; i++) {
if (colGetSize(columns[i]) != sz) {
cpl_msg_error(fctid, "Column sizes do not match!");
return 0;
}
}
/*
* Apply the conversions
*/
for (i = 0; i < sz; i++) {
/* FIXME:
* It is not obvious what is converted into what for the first 4
* columns. Check and add some explanation.
*/
*(colGetDoubleData(columns[0]) + i) *= 3600.;
*(colGetDoubleData(columns[1]) + i) *= 3600.;
*(colGetDoubleData(columns[2]) + i) *= 3600.;
*(colGetDoubleData(columns[3]) + i) *= 3600. * 3600.;
*(colGetDoubleData(columns[4]) + i) += magnitudeCorrection;
*(colGetDoubleData(columns[5]) + i) += magnitudeCorrection;
*(colGetDoubleData(columns[6]) + i) += magnitudeCorrection;
*(colGetDoubleData(columns[7]) + i) += magnitudeCorrection;
/*
* Shift image pixels by 1. SExtractor pixel coordinates
* start at (0,0), while the FITS standard requires to
* start at (1,1). See Greisen & Calabretta, A&A, preprint
*/
*(colGetDoubleData(columns[8]) + i) += 1.;
*(colGetDoubleData(columns[9]) + i) += 1.;
}
/*
* Add necessary keywords to the table.
*/
if (readIntDescriptor(image->descs, pilTrnGetKeyword("Quadrant"),
&quadrant, comment) != VM_TRUE)
return 0;
if (vimosDscCopy(&(table->descs), image->descs,
".*-OBS$", 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("Instrument"), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
"^ESO OBS (DID|ID|PROG ID)", 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("INS.DID"), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("InstrumentMode"), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("FilterId", quadrant), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("FilterName", quadrant), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("DET.DID"), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("Adu2Electron", 1), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("ReadNoise", 1), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("Electron2Adu", 1), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("WINi.BINX", 1), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("WINi.BINY", 1), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("SeqWindowSizeX", 1), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("SeqWindowSizeY", 1), 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
"^ESO DET READ (CLOCK|SPEED|MODE)", 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
"^ESO OCS (CON QUAD|DID)", 0)) {
return 0;
}
if (vimosDscCopy(&(table->descs), image->descs,
pilTrnGetKeyword("MagZero"), 0)) {
return 0;
}
if (VmComputeAirmass(image, &airmass) == EXIT_FAILURE)
return 0;
else {
if (writeDoubleDescriptor(&(table->descs), pilTrnGetKeyword("AirMass"),
airmass, "Averaged Airmass") != VM_TRUE)
return 0;
}
/*
* Finally the input table is now dressed up as a galaxy table and
* deserves its tag.
*/
strcpy(table->name, VM_GAL);
if (writeStringDescriptor(&(table->descs), pilTrnGetKeyword("Table"),
VM_GAL, "Type of table") != VM_TRUE)
return 0;
return table;
}
/**
* @memo
* Create a star table from a galaxy table.
*
* @return The function returns a pointer to the created star table if no
* error occured, otherwise a NULL pointer is returned.
*
* @param table Galaxy table.
* @param starIndex Stellarity index threshold.
* @param magLimit Magnitude lower limit.
*
* @doc
* The function creates a star table from a source list \textbf{table}
* having the properties of a galaxy table, i.e. the columns
* \begin{itemize}
* \item #NUMBER#,
* \item #X_IMAGE#,
* \item #Y_IMAGE#,
* \item #X_WORLD# and
* \item #Y_WORLD#
* \end{itemize}
* must be present. All objects in the input source list having a
* stellarity index greater than \textbf{starIndex}, which may have
* values ranging from 0 to 1, and which are brighter than
* \textbf{magLimit} are considered as stars.
*
* A preselection of the detected objects is done on the basis
* of the extraction attributes (source list column #FLAGS#). Only
* 'clean' source detections are considered, i.e. detections have
* no flags set (#FLAGS# = 0).
*
* The output star table is created and the selected entries from
* \textbf{table} corresponding to stars are copied.
*
* @see VmImBuildGalaxyTable, VmImDetectObjects
*
* @author P. Sartoretti, R. Palsa
*/
VimosTable *VmImBuildStarTable(VimosTable *table, float starIndex,
float magLimit)
{
const char fctid[] = "VmImBuildStarTable";
const char *labels[] = { /* Columns needed from the galaxy table */
"NUMBER",
"X_IMAGE",
"Y_IMAGE",
"X_WORLD",
"Y_WORLD",
"FLAGS",
"CLASS_STAR",
"MAG_BEST"
};
const size_t nc = sizeof labels / sizeof(const char *);
size_t i, objectCount, starCount;
int selection;
int *flags;
int *selectedEntry;
int *star;
double *class;
double *ximage, *yimage;
double *xworld, *yworld;
double *magBest, *magnitude;
VimosColumn *columns[nc];
VimosTable *starTable;
/*
* Validate input
*/
assert(table != 0);
if((starIndex < 0.) || (starIndex > 1.)) {
cpl_msg_error(fctid, "Stellarity index is out of range!");
return 0;
}
/*
* Check if the input table has the properties of a galaxy table,
* i.e. if all required columns and keywords a present.
*/
for (i = 0; i < nc; i++)
if (!(columns[i] = findColInTab(table, labels[i]))) {
cpl_msg_error(fctid, "Table column '%s' is missing!", labels[i]);
return 0;
}
cpl_msg_debug(fctid, "Stellarity index threshold: %.3f", starIndex);
cpl_msg_debug(fctid, "Magnitude limit: %.3f", magLimit);
/*
* Find all bright stars in the input table and record their
* sequence number
*/
objectCount = colGetSize(columns[0]);
selectedEntry = (int *)pil_calloc(objectCount, sizeof(int));
/*
* Check the selection criteria and count the stars.
*
* Column 5: FLAGS
* Column 6: CLASS_STAR
* Column 7: MAG_BEST
*/
flags = colGetIntData(columns[5]);
class = colGetDoubleData(columns[6]);
magBest = colGetDoubleData(columns[7]);
for(i = 0, starCount = 0; i < objectCount; i++) {
if (flags[i] == 0 && class[i] > starIndex && magBest[i] < magLimit) {
selectedEntry[starCount] = i;
starCount++;
}
}
/*
* Beware of empty table
*/
if (starCount <= 0) {
cpl_msg_warning(fctid, "No stars found for current settings!");
starTable = newStarTableEmpty();
pil_free(selectedEntry);
return 0;
}
else {
cpl_msg_info(fctid, "%zd stars have been selected.", starCount);
starTable = newStarTable(starCount);
}
/*
* Setup the output table and its columns
*/
if (!starTable) {
cpl_msg_error(fctid, "Cannot create star table!");
pil_free(selectedEntry);
return 0;
}
else {
char *hint = pil_strdup(pilTrnGetKeyword("Table"));
vimosDscCopy(&(starTable->descs), table->descs,
".*-OBS$", hint);
vimosDscCopy(&(starTable->descs), table->descs,
pilTrnGetKeyword("Instrument"), hint);
vimosDscCopy(&(starTable->descs), table->descs,
"^ESO (OBS|INS|DET|OCS)", hint);
vimosDscCopy(&(starTable->descs), table->descs,
"^ESO PRO (MAG ZERO|AIRMASS)", hint);
pil_free(hint);
}
if (starCount > 0) {
/*
* Copy NUMBER, X_IMAGE, Y_IMAGE, X_WORLD, Y_WORLD and MAG of the
* selected stars from the galaxy table to the output star table.
*/
star = tblGetIntData(starTable, "NUMBER");
ximage = tblGetDoubleData(starTable, "X_IMAGE");
yimage = tblGetDoubleData(starTable, "Y_IMAGE");
xworld = tblGetDoubleData(starTable, "X_WORLD");
yworld = tblGetDoubleData(starTable, "Y_WORLD");
magnitude = tblGetDoubleData(starTable, "MAG");
for (i = 0; i < starCount; i++) {
selection = selectedEntry[i];
star[i] = *(colGetIntData(columns[0]) + selection);
ximage[i] = *(colGetDoubleData(columns[1]) + selection);
yimage[i] = *(colGetDoubleData(columns[2]) + selection);
xworld[i] = *(colGetDoubleData(columns[3]) + selection);
yworld[i] = *(colGetDoubleData(columns[4]) + selection);
magnitude[i] = *(colGetDoubleData(columns[7]) + selection);
}
}
pil_free(selectedEntry);
return starTable;
}
/**
* @memo
* Build a star match table from a list of stars and a catalog.
*
* @return The function returns a pointer to the created star match table
* if no error occured, otherwise a NULL pointer is returned.
*
* @param image Image with WCS information.
* @param starTable List of detected stars.
* @param astTable Astrometric catalog data.
* @param minStars Minimum number of stars to be found.
* @param searchRadius Search radius in arcsec.
* @param iMagTolerance Magnitude tolerance for the initial selection
* of matching stars.
* @param fMagTolerance Magnitude tolerance for the final selection of
* matching stars.
* @param sigmaClip
*
* @doc
* The function identifies the detected stellar objects provided by
* the source list \textbf{starTable} with stars listed in a astrometric
* catalog \textbf{astTable}. The detected objects are identified using
* \textbf{VmImSearchMatches()}. For all identified objects, the
* information from the source list and the astrometric catalog are
* merged into the result star match table. For a detailed information
* on the remaining parameters see the documentation of
* \textbf{VmImSearchMatches()}.
*
* @see VmImSearchMatches
*
* @author P. Montegriffo, P. Sartoretti, B. Garilli, R. Palsa
*/
VimosTable *VmImBuildStarMatchTable(VimosImage *image, VimosTable *starTable,
VimosTable *astTable, int minStars,
double searchRadius, double iMagTolerance,
double fMagTolerance, float sigmaClip)
{
const char fctid[] = "VmImBuildStarMatchTable";
register int i;
char comment[COMMENT_LENGTH];
char filter[FLEN_VALUE + 1];
char **star_name;
int quadrant, matchCount;
int *indexTable;
int *star;
double *ximage, *yimage;
double *xworld, *yworld;
double *ra, *dec;
double *magnitude;
struct WorldCoor *wcs;
VimosTable *stmcTable;
/*
* Validate input
*/
assert(image != 0);
assert(starTable != 0 && astTable != 0);
/*
if (starTable->cols == 0 || starTable->cols->len == 0) {
cpl_msg_error(fctid, "Empty input source list!");
return 0;
}
*/
if (astTable->cols == 0 || astTable->cols->len == 0) {
cpl_msg_error(fctid, "Empty astrometric catalog!");
return 0;
}
/*
* Retrieve the filter name used for the observation from the
* input image.
*/
if (readIntDescriptor(image->descs, pilTrnGetKeyword("Quadrant"), &quadrant,
comment) == VM_FALSE)
return 0;
if (readStringDescriptor(image->descs,
pilTrnGetKeyword("FilterName", quadrant), filter,
comment) == VM_FALSE)
return 0;
/*
* Get world coordinate system from image header
*/
if (!(wcs = rdimage(image->descs))) {
cpl_msg_error(fctid, "World coordinate system not found in input image");
return 0;
}
/*
* Setup the astrometric catalog for the used filter, sort it by
* magnitude and check that enough astrometric stars are available.
*/
if (!setupAstrometricTable(astTable, filter)) {
cpl_msg_error(fctid, "Astrometric table setup failed!");
return 0;
}
if (astTable->cols->len < minStars) {
cpl_msg_error(fctid, "Too few entries in astrometric catalog for filter %s!",
filter);
return 0;
}
/*
* Sort the input source list by magnitude
*/
if ((starTable->cols) && starTable->cols->len > 0) {
if (!sortTable(starTable, "MAG")) {
cpl_msg_error(fctid, "Cannot sort source list by magnitude!");
return 0;
}
/*
* Project astrometric stars into the image pixel coordinate system
*/
cpl_msg_debug(fctid, "Projecting astrometric stars into image plane!");
wcstopix(astTable->cols->len, astTable, wcs);
/*
* Convert serch radius from arcsec to pixel.
*/
searchRadius /= fabs(3600. * wcs->cdelt[0]);
/*
* Identify sources from the source list with catalog entries.
*/
cpl_msg_debug(fctid, "Searching for matching stars.");
indexTable = VmSearchMatches(starTable, astTable, searchRadius,
iMagTolerance, fMagTolerance, sigmaClip,
minStars, &matchCount);
if (!indexTable) {
char *hint = pil_strdup(pilTrnGetKeyword("Table"));
cpl_msg_warning(fctid, "Search for matching stars failed!");
stmcTable = newStarMatchTableEmpty();
vimosDscCopy(&stmcTable->descs, starTable->descs,
".*-OBS$", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
pilTrnGetKeyword("Instrument"), hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO (OBS|INS|DET|OCS)", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO PRO (MAG ZERO|AIRMASS)", hint);
pil_free(hint);
return stmcTable;
}
else
cpl_msg_info(fctid, "%d matching stars found.", matchCount);
/*
* Project stars onto sky. World coordinates are needed for the
* StarMatch table.
*/
cpl_msg_debug(fctid, "Projecting stars onto sky!");
pixtowcs(starTable->cols->len, starTable, wcs);
/*
* Build the StarMatch table.
*/
if (!(stmcTable = newStarMatchTable(matchCount))) {
pil_free(indexTable);
return 0;
}
}
else {
char *hint = pil_strdup(pilTrnGetKeyword("Table"));
stmcTable = newStarMatchTableEmpty();
vimosDscCopy(&stmcTable->descs, starTable->descs,
".*-OBS$", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
pilTrnGetKeyword("Instrument"), hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO (OBS|INS|DET|OCS)", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO PRO (MAG ZERO|AIRMASS)", hint);
pil_free(hint);
return stmcTable;
}
/*
* Setup the star match table keyword list, i.e. copy the keywords
* from the star table and insert the world coordinate system keywords.
*/
if (writeWcsInfo(&stmcTable->descs, wcs) == EXIT_FAILURE) {
cpl_msg_debug(fctid, "Appending WCS keywords failed!");
pil_free(indexTable);
deleteTable(stmcTable);
return 0;
}
else {
char *hint = pil_strdup(pilTrnGetKeyword("Table"));
vimosDscCopy(&stmcTable->descs, starTable->descs,
".*-OBS$", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
pilTrnGetKeyword("Instrument"), hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO (OBS|INS|DET|OCS)", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO PRO (MAG ZERO|AIRMASS)", hint);
pil_free(hint);
}
/*
* Copy the data for the matching entries from the star and astrometric
* table.
*/
star = tblGetIntData(starTable, "NUMBER");
star_name = tblGetStringData(astTable, "ID");
ximage = tblGetDoubleData(starTable, "X_IMAGE");
yimage = tblGetDoubleData(starTable, "Y_IMAGE");
xworld = tblGetDoubleData(starTable, "X_WORLD");
yworld = tblGetDoubleData(starTable, "Y_WORLD");
magnitude = tblGetDoubleData(starTable, "MAG");
ra = tblGetDoubleData(astTable, "RA");
dec = tblGetDoubleData(astTable, "DEC");
for (i = 0; i < matchCount; i++) {
int j, k;
j = indexTable[2 * i];
k = indexTable[2 * i + 1];
tblSetIntValue(stmcTable, "NUMBER", i, star[j]);
tblSetStringValue(stmcTable, "ID", i, star_name[k]);
tblSetDoubleValue(stmcTable, "X_IMAGE", i, ximage[j]);
tblSetDoubleValue(stmcTable, "Y_IMAGE", i, yimage[j]);
tblSetDoubleValue(stmcTable, "X_WORLD", i, xworld[j]);
tblSetDoubleValue(stmcTable, "Y_WORLD", i, yworld[j]);
tblSetDoubleValue(stmcTable, "MAG", i, magnitude[j]);
tblSetDoubleValue(stmcTable, "RA", i, ra[k]);
tblSetDoubleValue(stmcTable, "DEC", i, dec[k]);
}
/*
* Extra magnutude columns from the astrometric table. Not all of
* them might be present!
*/
if (!findColInTab(astTable, "MAG_U"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_U"));
else {
double *m = tblGetDoubleData(astTable, "MAG_U");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_U", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_B"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_B"));
else {
double *m = tblGetDoubleData(astTable, "MAG_B");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_B", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_V"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_V"));
else {
double *m = tblGetDoubleData(astTable, "MAG_V");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_V", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_R"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_R"));
else {
double *m = tblGetDoubleData(astTable, "MAG_R");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_R", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_I"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_I"));
else {
double *m = tblGetDoubleData(astTable, "MAG_I");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_I", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_z"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_z"));
else {
double *m = tblGetDoubleData(astTable, "MAG_z");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_z", i, m[indexTable[2 * i + 1]]);
}
/*
* Cleanup
*/
pil_free(indexTable);
return stmcTable;
}
/*PDB - New version of VmImBuildStarMatchTable that has a fix for vmskyccd*/
VimosTable *VmImBuildStarMatchTable_skyccd(VimosImage *image, VimosTable *starTable,
VimosTable *astTable, int minStars,
double searchRadius, double iMagTolerance,
double fMagTolerance, float sigmaClip)
{
const char fctid[] = "VmImBuildStarMatchTable";
register int i;
char comment[COMMENT_LENGTH];
char filter[FLEN_VALUE + 1];
char **star_name;
int quadrant, matchCount;
int *indexTable;
int *star;
double *ximage, *yimage;
double *xworld, *yworld;
double *ra, *dec;
double *magnitude;
struct WorldCoor *wcs;
VimosTable *stmcTable;
/*
* Validate input
*/
assert(image != 0);
assert(starTable != 0 && astTable != 0);
/*
if (starTable->cols == 0 || starTable->cols->len == 0) {
cpl_msg_error(fctid, "Empty input source list!");
return 0;
}
*/
if (astTable->cols == 0 || astTable->cols->len == 0) {
cpl_msg_error(fctid, "Empty astrometric catalog!");
return 0;
}
/*
* Retrieve the filter name used for the observation from the
* input image.
*/
if (readIntDescriptor(image->descs, pilTrnGetKeyword("Quadrant"), &quadrant,
comment) == VM_FALSE)
return 0;
if (readStringDescriptor(image->descs,
pilTrnGetKeyword("FilterName", quadrant), filter,
comment) == VM_FALSE)
return 0;
/*
* Get world coordinate system from image header
*/
if (!(wcs = rdimage(image->descs))) {
cpl_msg_error(fctid, "World coordinate system not found in input image");
return 0;
}
/*
* Setup the astrometric catalog for the used filter, sort it by
* magnitude and check that enough astrometric stars are available.
*/
if (!setupAstrometricTable(astTable, filter)) {
cpl_msg_error(fctid, "Astrometric table setup failed!");
return 0;
}
if (astTable->cols->len < minStars) {
cpl_msg_error(fctid, "Too few entries in astrometric catalog for filter %s!",
filter);
return 0;
}
/*
* Sort the input source list by magnitude
*/
if ((starTable->cols) && starTable->cols->len > 0) {
if (!sortTable(starTable, "MAG")) {
cpl_msg_error(fctid, "Cannot sort source list by magnitude!");
return 0;
}
/*
* Project astrometric stars into the image pixel coordinate system
*/
cpl_msg_debug(fctid, "Projecting astrometric stars into image plane!");
wcstopix(astTable->cols->len, astTable, wcs);
/*
* Convert serch radius from arcsec to pixel.
*/
searchRadius /= fabs(3600. * wcs->cdelt[0]);
/*
* Identify sources from the source list with catalog entries.
*/
cpl_msg_debug(fctid, "Searching for matching stars.");
indexTable = VmSearchMatches(starTable, astTable, searchRadius,
iMagTolerance, fMagTolerance, sigmaClip,
minStars, &matchCount);
if (!indexTable) {
char *hint = pil_strdup(pilTrnGetKeyword("Table"));
cpl_msg_warning(fctid, "Search for matching stars failed!");
stmcTable = newStarMatchTableEmpty();
vimosDscCopy(&stmcTable->descs, starTable->descs,
".*-OBS$", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
pilTrnGetKeyword("Instrument"), hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO (OBS|INS|DET|OCS)", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO PRO (MAG ZERO|AIRMASS)", hint);
pil_free(hint);
return stmcTable;
}
else
cpl_msg_info(fctid, "%d matching stars found.", matchCount);
/*
* Project stars onto sky. World coordinates are needed for the
* StarMatch table.
*/
cpl_msg_debug(fctid, "Projecting stars onto sky!");
pixtowcs(starTable->cols->len, starTable, wcs);
/*
* Build the StarMatch table.
*/
if (!(stmcTable = newStarMatchTable(matchCount))) {
pil_free(indexTable);
return 0;
}
}
else {
char *hint = pil_strdup(pilTrnGetKeyword("Table"));
stmcTable = newStarMatchTableEmpty();
vimosDscCopy(&stmcTable->descs, starTable->descs,
".*-OBS$", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
pilTrnGetKeyword("Instrument"), hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO (OBS|INS|DET|OCS)", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO PRO (MAG ZERO|AIRMASS)", hint);
pil_free(hint);
return stmcTable;
}
/*
* Setup the star match table keyword list, i.e. copy the keywords
* from the star table and insert the world coordinate system keywords.
*/
if (writeWcsInfo(&stmcTable->descs, wcs) == EXIT_FAILURE) {
cpl_msg_debug(fctid, "Appending WCS keywords failed!");
pil_free(indexTable);
deleteTable(stmcTable);
return 0;
}
else {
char *hint = pil_strdup(pilTrnGetKeyword("Table"));
vimosDscCopy(&stmcTable->descs, starTable->descs,
".*-OBS$", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
pilTrnGetKeyword("Instrument"), hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO (OBS|INS|DET|OCS)", hint);
vimosDscCopy(&stmcTable->descs, starTable->descs,
"^ESO PRO (MAG ZERO|AIRMASS)", hint);
pil_free(hint);
}
/*
* Copy the data for the matching entries from the star and astrometric
* table.
*/
star = tblGetIntData(starTable, "NUMBER");
star_name = tblGetStringData(astTable, "ID");
/*PDB table is astTable in 2 lines below, this is specific to the
VmImBuildStarMatchTable_skyccd version of the function and
is necessary for vmskyccd to work properly*/
ximage = tblGetDoubleData(astTable, "X_IMAGE");
yimage = tblGetDoubleData(astTable, "Y_IMAGE");
xworld = tblGetDoubleData(starTable, "X_WORLD");
yworld = tblGetDoubleData(starTable, "Y_WORLD");
magnitude = tblGetDoubleData(starTable, "MAG");
ra = tblGetDoubleData(astTable, "RA");
dec = tblGetDoubleData(astTable, "DEC");
for (i = 0; i < matchCount; i++) {
int j, k;
j = indexTable[2 * i];
k = indexTable[2 * i + 1];
tblSetIntValue(stmcTable, "NUMBER", i, star[j]);
tblSetStringValue(stmcTable, "ID", i, star_name[k]);
/*PDB index is [k] in 2 lines below, this is specific to the
VmImBuildStarMatchTable_skyccd version of the function and
is necessary for vmskyccd to work properly*/
tblSetDoubleValue(stmcTable, "X_IMAGE", i, ximage[k]);
tblSetDoubleValue(stmcTable, "Y_IMAGE", i, yimage[k]);
tblSetDoubleValue(stmcTable, "X_WORLD", i, xworld[j]);
tblSetDoubleValue(stmcTable, "Y_WORLD", i, yworld[j]);
tblSetDoubleValue(stmcTable, "MAG", i, magnitude[j]);
tblSetDoubleValue(stmcTable, "RA", i, ra[k]);
tblSetDoubleValue(stmcTable, "DEC", i, dec[k]);
}
/*
* Extra magnutude columns from the astrometric table. Not all of
* them might be present!
*/
if (!findColInTab(astTable, "MAG_U"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_U"));
else {
double *m = tblGetDoubleData(astTable, "MAG_U");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_U", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_B"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_B"));
else {
double *m = tblGetDoubleData(astTable, "MAG_B");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_B", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_V"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_V"));
else {
double *m = tblGetDoubleData(astTable, "MAG_V");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_V", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_R"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_R"));
else {
double *m = tblGetDoubleData(astTable, "MAG_R");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_R", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_I"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_I"));
else {
double *m = tblGetDoubleData(astTable, "MAG_I");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_I", i, m[indexTable[2 * i + 1]]);
}
if (!findColInTab(astTable, "MAG_z"))
deleteColumn(tblRemoveColumn(stmcTable, "MAG_z"));
else {
double *m = tblGetDoubleData(astTable, "MAG_z");
for (i = 0; i < matchCount; i++)
tblSetDoubleValue(stmcTable, "MAG_z", i, m[indexTable[2 * i + 1]]);
}
/*
* Cleanup
*/
pil_free(indexTable);
return stmcTable;
}
/**
* @memo
* Detect objects in an imaging observation.
*
* @return The function returns a pointer to the generated source list if
* no error occurred, othewise the return value is NULL.
*
* @param image Image which should be searched for objects.
* @param badPixels Bad pixel map. (This is not supported. Set it to 0.)
*
* @doc
*
* @author R. Palsa
*/
VimosTable *VmImDetectObjects(VimosImage *image, VimosImage *badpixels)
{
const char fctid[] = "VmImDetectObjects";
char *configName, *parameterName, *imageName;
char *networkName, *filterName, *catalogName;
char *assocName = 0;
char *checkName = 0;
char *flagName = 0;
char *weightName = 0;
char *argv[SEXTRACTOR_ARGC + 1];
char cwd[PATHNAME_MAX + 1];
int argc;
time_t timeout = sextGetExecutionTimeLimit();
FILE *configFile, *parameterFile;
VimosTable *galTable;
/*
* Prepare the SExtractor setup files. The input setup is taken
* from the recipe database. The output is defined here to be
* exactly what is needed for building a galaxy table. The setup
* for the galaxy/star classifier (neuronal network setup) as well
* the convolution mask setup are just read from the files given
* in the database.
*/
/*
* Get the current working directory.
*/
if (!getcwd(cwd, PATHNAME_MAX)) {
cpl_msg_error(fctid, "Cannot determine current working directory!");
return 0;
}
/*
* Create and write the SExtractor setup file. The function tempnam
* is used to create a name for a temporary file. The name has to
* be passed to SExtractor so it has to be kept.
*/
if (!(configName = tempnam(cwd, "sext"))) {
cpl_msg_error(fctid, "Cannot create unique name for temporary file!");
return 0;
}
else {
if (!(configFile = fopen(configName, "w"))) {
cpl_msg_error(fctid, "Cannot create temporary setup file!");
pil_free(configName);
return 0;
}
}
if (sextSaveConfiguration(configFile, image) == EXIT_FAILURE) {
cpl_msg_error(fctid, "Cannot dump SExtractor setup!");
fclose(configFile);
remove(configName);
pil_free(configName);
return 0;
}
fclose(configFile);
/*
* Create and write the SExtractor output configuration file. The
* function tempnam is used to create a name for a temporary file.
* The name has to be passed to SExtractor so it has to be kept.
*/
if (!(parameterName = tempnam(cwd, "sext"))) {
cpl_msg_error(fctid, "Cannot create unique name for temporary file!");
remove(configName);
pil_free(configName);
return 0;
}
else {
if (!(parameterFile = fopen(parameterName, "w"))) {
cpl_msg_error(fctid, "Cannot create temporary setup file!");
remove(configName);
pil_free(configName);
pil_free(parameterName);
return 0;
}
}
if (sextSaveParameters(parameterFile, galaxy_table_columns) ==
EXIT_FAILURE) {
cpl_msg_error(fctid, "Cannot write SExtractor parameter file!");
fclose(parameterFile);
remove(configName);
remove(parameterName);
pil_free(configName);
pil_free(parameterName);
return 0;
}
fclose(parameterFile);
/*
* Get the fully expanded path to the neuronal network and convolution
* mask setup files. Their existance and permissions are checked.
*/
if (!(networkName = pil_strdup(sextGetStarNnwName()))) {
cpl_msg_error(fctid, "Cannot retrieve SExtractor neuronal network "
"setup file!");
remove(configName);
remove(parameterName);
pil_free(configName);
pil_free(parameterName);
return 0;
}
else
if (access(networkName, F_OK | R_OK)) {
cpl_msg_error(fctid, "Cannot access SExtractor neuronal network "
"setup file %s!", networkName);
remove(configName);
remove(parameterName);
pil_free(configName);
pil_free(parameterName);
pil_free(networkName);
return 0;
}
if (!(filterName = pil_strdup(sextGetFilterName()))) {
cpl_msg_error(fctid, "Cannot retrieve SExtractor filter setup file!");
remove(configName);
remove(parameterName);
pil_free(configName);
pil_free(parameterName);
pil_free(networkName);
return 0;
}
else
if (access(filterName, F_OK | R_OK)) {
cpl_msg_error(fctid, "Cannot access SExtractor neuronal network "
"setup file %s!", filterName);
remove(configName);
remove(parameterName);
pil_free(configName);
pil_free(parameterName);
pil_free(networkName);
pil_free(filterName);
return 0;
}
/* FIXME:
* A better solution for generating the output catalog name and the
* input image name might be to use one of the file name header keywords
* in the image together with the appropriate suffix. Also, it
* should be forced that image and catalog file are generated in the
* current working directory. (RP)
*/
/*
* Generate the output catalog name
*/
if (!(catalogName = tempnam(cwd, "sext"))) {
cpl_msg_error(fctid, "Cannot create unique name for temporary file!");
remove(configName);
remove(parameterName);
pil_free(configName);
pil_free(parameterName);
pil_free(networkName);
pil_free(filterName);
return 0;
}
/*
* Save input images to local disk files which can be used by
* SExtractor. Also the CDELT keywords are removed here, which
* confuse SExtractor if the CD matrix is present too.
*/
if (findDescriptor(image->descs, pilTrnGetKeyword("CD", 1, 1))) {
if (findDescriptor(image->descs, pilTrnGetKeyword("Cdelt", 1)))
removeDescriptor(&image->descs, pilTrnGetKeyword("Cdelt", 1));
if (findDescriptor(image->descs, pilTrnGetKeyword("Cdelt", 2)))
removeDescriptor(&image->descs, pilTrnGetKeyword("Cdelt", 2));
}
if (!(imageName = tempnam(cwd, "sext"))) {
cpl_msg_error(fctid, "Cannot create unique name for temporary file!");
remove(configName);
remove(parameterName);
pil_free(configName);
pil_free(parameterName);
pil_free(networkName);
pil_free(filterName);
pil_free(catalogName);
return 0;
}
else {
if (!createFitsImage(imageName, image, "UNKNOWN")) {
cpl_msg_error(fctid, "Cannot create temporary SExtractor input image "
"file!");
remove(configName);
remove(parameterName);
pil_free(configName);
pil_free(parameterName);
pil_free(networkName);
pil_free(filterName);
pil_free(imageName);
pil_free(catalogName);
return 0;
}
}
/*
* Build the SExtractor command line and execute it.
*/
argv[0] = pil_strdup(sextGetSextractorPath());
argv[1] = imageName;
argv[2] = "-c";
argv[3] = configName;
argv[4] = "-PARAMETERS_NAME";
argv[5] = parameterName;
argv[6] = "-CATALOG_NAME";
argv[7] = catalogName;
argc = 7;
if (filterName) {
argv[++argc] = "-FILTER_NAME";
argv[++argc] = filterName;
}
if (networkName) {
argv[++argc] = "-STARNNW_NAME";
argv[++argc] = networkName;
}
/*
* Note that the following assignements refer to an static string
* of the functions on the right side. Therefore the assignement
* should be considered valid only within the enclosing
* if-statement.
*/
if ((assocName = (char *)sextGetAssocName())) {
argv[++argc] = "-ASSOC_NAME";
argv[++argc] = assocName;
}
if ((checkName = (char *)sextGetCheckImageName())) {
argv[++argc] = "-CHECKIMAGE_NAME";
argv[++argc] = checkName;
}
if ((flagName = (char *)sextGetFlagImageName())) {
argv[++argc] = "-FLAG_IMAGE";
argv[++argc] = flagName;
}
if ((weightName = (char *)sextGetWeightImageName())) {
argv[++argc] = "-WEIGHT_IMAGE";
argv[++argc] = weightName;
}
argv[++argc] = 0;
if (pilTaskExecWait(argc, (const char *const *)argv, timeout)) {
cpl_msg_error(fctid, "Running SExtractor failed!");
remove(configName);
remove(parameterName);
remove(imageName);
pil_free(configName);
pil_free(parameterName);
pil_free(networkName);
pil_free(filterName);
pil_free(imageName);
pil_free(catalogName);
return 0;
}
remove(configName);
remove(parameterName);
remove(imageName);
pil_free(configName);
pil_free(parameterName);
pil_free(networkName);
pil_free(filterName);
pil_free(imageName);
/*
* Check the validity of SExtractor output source list and build
* a galaxy table from it.
*/
if (!(galTable = sextConvertCatalog(catalogName, galaxy_table_columns))) {
cpl_msg_error(fctid, "SExtractor output catalog conversion failed!");
remove(catalogName);
pil_free(catalogName);
return 0;
}
else {
remove(catalogName);
pil_free(catalogName);
}
if (!(galTable = VmImBuildGalaxyTable(galTable, image))) {
cpl_msg_error(fctid, "Building Galaxy table from SExtractor output catalog "
"failed!");
deleteTable(galTable);
return 0;
}
return galTable;
}
/*
* The function below is basically a copy of searchmatch(). Modifications
* have been applied to the prototype in the sense that the size of the
* columns are taken from the input tables instead of being passed, and
* the order of the arguments has changed. The function body has been
* cleaned up with respect to comments and input only!
*/
/**
* @memo
* Search for matching objects.
*
* @return The function returns the index table for the matching objects if
* no error occurred, otherwise a #NULL# pointer is returned.
*
* @param sourceList Table containing detected objects.
* @param catalog Reference catalog.
* @param radius Search radius in pixel.
* @param magTolerance1 Magnitude tolerance for initial selection of
* matching objects.
* @param magTolerance2 Magnitude tolerance for final selection of
* matching objects.
* @param sigmaClip Sigma clipping threshold.
* @param minStars Required minimum number of matching pairs.
* @param matches Number of matching pairs found.
*
* @doc
* The function identifies the sources listed in the source list
* \textbf{sourceList}. A detected source from \textbf{sourceList} is
* identified by first looking for catalog objects listed in
* \textbf{catalog} within \textbf{radius} pixel around the position of
* the detected objects and, additionally, discriminating on the difference
* in brightness of the detected objects and the catalog sources, using
* \textbf{magTolerance1} as an upper limit. During this first selection
* at least \textbf{minStars} must be found, or the function returns an
* error. In a second step a selection among the surviving objects is
* performed applying a sigma clipping method to the positional offsets
* from the mean offset of the catalog sources from the detected positions.
* The sensitivity of the sigma clipping algorithm is controlled by the
* sigma clipping factor \textbf{sigmaClip}. Again the discrimination on
* the difference in brightness is applied, this time using
* \textbf{magTolerance2} as upper limit. If \textbf{magTolerance1}
* or \textbf{magTolerance2} are less than zero, the corresponding
* magnitude discrimination is ignored.
*
* The final number of matching pairs is passed back to the caller through
* the argument \textbf{matches}.
*
* @author P. Montegriffo, P. Sartoretti, R. Palsa
*/
int *VmSearchMatches(VimosTable *sourceList, VimosTable *catalog,
double radius, double magTolerance1, double magTolerance2,
float sigmaClip, int minStars, int *matches)
{
const char *fctid = "VmSearchMatches";
int i, j, jbest;
int sz, n_ele1, n_ele2;
int nmatch0;
int nrej;
int nmatch = 0;
int *imatch = 0; /* returning index table for matching stars */
int *imatch0 = 0; /* temporary index table for matching stars */
int *flag = 0;
double tol2, dx, dy, dxy, dxys;
double dmag, dmsum, dmave;
double dxsum, dysum, dxave, dyave;
double dxdev, dydev;
VimosColumn *aXimaCol, *aYimaCol, *aXwldCol;
VimosColumn *aYwldCol, *aMagCol, *agoffCol;
VimosColumn *oXimaCol, *oYimaCol, *oMagCol;
*matches = 0;
tol2 = pow(radius, 2.);
cpl_msg_debug(fctid, "Tolerance = %f pixel", radius);
/*
* Get the columns from the input source list
*/
if (!(oXimaCol = findColInTab(sourceList, "X_IMAGE"))) {
cpl_msg_error(fctid, "Column 'X_IMAGE' not found in source list!");
return NULL;
}
if (!(oYimaCol = findColInTab(sourceList, "Y_IMAGE"))) {
cpl_msg_error(fctid, "Column 'Y_IMAGE' not found in source list!");
return NULL;
}
if (!(oMagCol = findColInTab(sourceList, "MAG"))) {
cpl_msg_error(fctid, "Column 'MAG' not found in source list!");
return NULL;
}
/*
* Get columns from the reference catalog
*/
if (!(aXimaCol = findColInTab(catalog, "X_IMAGE"))) {
cpl_msg_error(fctid, "Column 'X_IMAGE' not found in reference catalog!");
return NULL;
}
if (!(aYimaCol = findColInTab(catalog, "Y_IMAGE"))) {
cpl_msg_error(fctid, "Column 'Y_IMAGE' not found in reference catalog!");
return NULL;
}
if (!(aXwldCol = findColInTab(catalog, "RA"))) {
cpl_msg_error(fctid, "Column 'RA' not found in reference catalog!");
return NULL;
}
if (!(aYwldCol = findColInTab(catalog, "DEC"))) {
cpl_msg_error(fctid, "Column 'DEC' not found in reference catalog!");
return NULL;
}
if (!(aMagCol = findColInTab(catalog, "MAG"))) {
cpl_msg_error(fctid, "Column 'MAG' not found in reference catalog!");
return NULL;
}
if (!(agoffCol = findColInTab(catalog, "GOFF"))) {
cpl_msg_error(fctid, "Column 'GOFF' not found in reference catalog!");
return NULL;
}
/*
* Get the number of rows in the input tables.
*/
/* FIXME: This assumes that all columns have the same size. This should
* be checked!
*/
n_ele1 = colGetSize(oXimaCol);
n_ele2 = colGetSize(aXimaCol);
/*
* Allocate working arrays for the index table and the selection flag
*/
sz = n_ele1 < n_ele2 ? n_ele1 : n_ele2;
imatch0 = (int *)pil_calloc((size_t)(2 * sz), sizeof(int));
if (!imatch0) {
cpl_msg_error(fctid, "Not enough memory!");
return NULL;
}
if ((flag = (int *)pil_calloc((size_t)n_ele2, sizeof(int))) == 0) {
cpl_msg_error(fctid, "Not enough memory!");
pil_free(imatch0);
return NULL;
}
/*
* Object idendification starts here
*/
nmatch0 = 0;
dxsum = 0.0;
dysum = 0.0;
dmsum = 0.0;
for (i = 0; i < n_ele1; i++) {
dxys = -1.0;
jbest = -1;
for (j = 0; j < n_ele2; j++) {
/*
* flags is used to discard problematic object, objects close
* to the border for instance.
*/
if ((!(agoffCol->colValue->iArray[j])) && !(*(flag+j))) {
dx = aXimaCol->colValue->dArray[j] - oXimaCol->colValue->dArray[i];
dy = aYimaCol->colValue->dArray[j] - oYimaCol->colValue->dArray[i];
dxy = pow(dx, 2.) + pow(dy, 2.);
dmag = fabs((aMagCol->colValue->dArray[j] -
oMagCol->colValue->dArray[i]));
if (magTolerance1 > 0.0) {
if (dxy < tol2 && dmag <= magTolerance1) {
if (dxys < 0.0) {
dxys = dxy;
jbest = j;
}
else
if (dxy < dxys) {
dxys = dxy;
jbest = j;
}
}
}
else {
if (dxy < tol2) {
if (dxys < 0.0) {
dxys = dxy;
jbest = j;
}
else
if (dxy < dxys) {
dxys = dxy;
jbest = j;
}
}
}
}
}
if (jbest > -1) {
dx = aXimaCol->colValue->dArray[jbest] - oXimaCol->colValue->dArray[i];
dy = aYimaCol->colValue->dArray[jbest] - oYimaCol->colValue->dArray[i];
dxsum += dx;
dysum += dy;
dmsum += aMagCol->colValue->dArray[jbest] - oMagCol->colValue->dArray[i];
*(imatch0+(2 * nmatch0)) = i;
*(imatch0+(2 * nmatch0 +1)) = jbest;
*(flag+jbest) = 1;
nmatch0++;
}
}
pil_free(flag);
if (nmatch0 < (minStars < 2 ? minStars : 2)) {
cpl_msg_error(fctid, "Insufficient number of matches found [%d]", nmatch0);
pil_free(imatch0);
return NULL;
}
cpl_msg_debug(fctid, "Found %d matches", nmatch0);
if (nmatch0 > 1) {
dxave = dxsum / nmatch0;
dyave = dysum / nmatch0;
dmave = dmsum / nmatch0;
/*
* Get the standard deviations of dx and dy from the mean
*/
dxsum = 0.0;
dysum = 0.0;
dxdev = 0.0;
dydev = 0.0;
for (i = 0; i < nmatch0; i++) {
dx = (aXimaCol->colValue->dArray[imatch0[2 * i + 1]]) -
(oXimaCol->colValue->dArray[imatch0[2 * i]]) - dxave;
dy = (aYimaCol->colValue->dArray[imatch0[2 * i + 1]]) -
(oYimaCol->colValue->dArray[imatch0[2 * i]]) - dyave;
dxsum += dx;
dysum += dy;
dxdev += dx*dx;
dydev += dy*dy;
}
dxdev = sqrt((dxdev - pow(dxsum, 2.) / nmatch0) / (nmatch0 - 1));
dydev = sqrt((dydev - pow(dysum, 2.) / nmatch0) / (nmatch0 - 1));
/** FIXME:
* (Paola) patch here: apply sigma clipping only when dxdev and dydev
* are significant. If not we play with "numeric noise" and
* many sources are excluded. This happened in "fitCO"
* where many artificial sources are excluded due only to noise.
*/
if(dxdev < MIN_DEVIATION)
dxdev = MIN_DEVIATION;
if(dydev < MIN_DEVIATION )
dydev = MIN_DEVIATION;
cpl_msg_debug(fctid, "Applying 2-sigma rejection: dxdev=%g; dydev=%g",
dxdev, dydev);
/*
* Find best matches applying a Sigma clipping
*/
if (!(imatch = (int *)pil_calloc((size_t)(2 * nmatch0), sizeof(int)))) {
cpl_msg_error(fctid, "Not enough memory!");
pil_free(imatch0);
return NULL;
}
nmatch = 0;
nrej = 0;
for (i = 0; i < nmatch0; i++) {
dx = (aXimaCol->colValue->dArray[imatch0[2 * i + 1]]) -
(oXimaCol->colValue->dArray[imatch0[2 * i]]) - dxave;
dy = (aYimaCol->colValue->dArray[imatch0[2 * i + 1]]) -
(oYimaCol->colValue->dArray[imatch0[2 * i]]) - dyave;
dmag = (aMagCol->colValue->dArray[imatch0[2 * i + 1]]) -
(oMagCol->colValue->dArray[imatch0[2 * i]]) - dmave;
if (magTolerance2 > 0.0) {
if (fabs(dx) <= sigmaClip * dxdev && fabs(dy) <= sigmaClip * dydev &&
fabs(dmag) <= magTolerance2 ) {
*(imatch+(2 * nmatch)) = imatch0[2 * i];
*(imatch+(2 * nmatch +1)) = imatch0[2 * i + 1];
nmatch++;
}
else
nrej++;
}
else {
if (fabs(dx) <= sigmaClip * dxdev && fabs(dy) <= sigmaClip * dydev) {
*(imatch+(2 * nmatch)) = imatch0[2 * i];
*(imatch+(2 * nmatch +1)) = imatch0[2 * i + 1];
nmatch++;
}
else
nrej++;
}
}
if(nrej > 0)
cpl_msg_debug(fctid, "Rejected %d pair(s)", nrej);
pil_free (imatch0);
*matches = nmatch;
}
else {
if (!(imatch = (int *)pil_calloc((size_t)(2 * nmatch0), sizeof(int)))) {
cpl_msg_error(fctid, "Not enough memory!");
pil_free(imatch0);
return NULL;
}
*(imatch) = imatch0[0];
*(imatch+1) = imatch0[1];
*matches = 1;
}
return imatch;
}
/**@}*/
|