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
|
/*********************************************************************
Arithmetic - Do arithmetic operations on images.
Arithmetic is part of GNU Astronomy Utilities (Gnuastro) package.
Original author:
Mohammad Akhlaghi <mohammad@akhlaghi.org>
Contributing author(s):
Copyright (C) 2015-2025 Free Software Foundation, Inc.
Gnuastro 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 3 of the License, or (at your
option) any later version.
Gnuastro 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 Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include <config.h>
#include <math.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <string.h>
#include <stdlib.h>
#include <gnuastro/wcs.h>
#include <gnuastro/fits.h>
#include <gnuastro/array.h>
#include <gnuastro/binary.h>
#include <gnuastro/threads.h>
#include <gnuastro/pointer.h>
#include <gnuastro/dimension.h>
#include <gnuastro/statistics.h>
#include <gnuastro/arithmetic.h>
#include <gnuastro/interpolate.h>
#include <gnuastro-internal/checkset.h>
#include "main.h"
#include "operands.h"
#include "arithmetic.h"
/***************************************************************/
/************* Internal functions *************/
/***************************************************************/
#define SET_NUM_OP(CTYPE) { \
CTYPE a=*(CTYPE *)(numpop->array); \
gal_data_free(numpop); \
if(a>0) return a; }
static size_t
pop_number_of_operands(struct arithmeticparams *p, int op,
char *token_string, gal_data_t **params)
{
char *cstring="first";
size_t c, numparams=0;
gal_data_t *tmp, *numpop;
/* See if this operator needs any parameters. If so, pop them. */
switch(op)
{
case GAL_ARITHMETIC_OP_STITCH:
case GAL_ARITHMETIC_OP_QUANTILE:
numparams=1;
break;
case GAL_ARITHMETIC_OP_MADCLIP_STD:
case GAL_ARITHMETIC_OP_SIGCLIP_STD:
case GAL_ARITHMETIC_OP_MADCLIP_MAD:
case GAL_ARITHMETIC_OP_SIGCLIP_MAD:
case GAL_ARITHMETIC_OP_SIGCLIP_ALL:
case GAL_ARITHMETIC_OP_MADCLIP_ALL:
case GAL_ARITHMETIC_OP_MADCLIP_MEAN:
case GAL_ARITHMETIC_OP_SIGCLIP_MEAN:
case GAL_ARITHMETIC_OP_MADCLIP_MEDIAN:
case GAL_ARITHMETIC_OP_SIGCLIP_MEDIAN:
case GAL_ARITHMETIC_OP_MADCLIP_MASKFILLED:
case GAL_ARITHMETIC_OP_SIGCLIP_MASKFILLED:
numparams=2;
break;
}
/* If any parameters should be read, read them. */
*params=NULL;
for(c=0;c<numparams;++c)
{
/* If it only has one element, save it as floating point and add it
to the list. */
tmp=operands_pop(p, token_string);
if(tmp->size>1)
error(EXIT_FAILURE, 0, "the %s popped operand of the '%s' "
"operator must be a single number", cstring, token_string);
tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_FLOAT32);
gal_list_data_add(params, tmp);
/* A small sanity check (none of the parameters for sigma-clipping,
or quantile estimation can be negative). */
if( ((float *)(tmp->array))[0]<=0.0 )
error(EXIT_FAILURE, 0, "the %s popped operand of the '%s' "
"operator cannot be zero or negative", cstring, token_string);
/* Increment the counter string. */
cstring=c?"third":"second";
}
/* Check if it is a number. */
numpop=operands_pop(p, token_string);
if(numpop->size>1)
error(EXIT_FAILURE, 0, "the %s popped operand of the '%s' "
"operator (number of input datasets) must be a number, not an "
"array", cstring, token_string);
/* Check its type and return the value. */
switch(numpop->type)
{
/* For the integer types, if they are unsigned, then just pass their
value, if they are signed, you have to make sure they are zero or
positive. */
case GAL_TYPE_UINT8: SET_NUM_OP(uint8_t); break;
case GAL_TYPE_INT8: SET_NUM_OP(int8_t); break;
case GAL_TYPE_UINT16: SET_NUM_OP(uint16_t); break;
case GAL_TYPE_INT16: SET_NUM_OP(int16_t); break;
case GAL_TYPE_UINT32: SET_NUM_OP(uint32_t); break;
case GAL_TYPE_INT32: SET_NUM_OP(int32_t); break;
case GAL_TYPE_UINT64: SET_NUM_OP(uint64_t); break;
case GAL_TYPE_INT64: SET_NUM_OP(int64_t); break;
/* Floating point numbers are not acceptable in this context. */
case GAL_TYPE_FLOAT32:
case GAL_TYPE_FLOAT64:
error(EXIT_FAILURE, 0, "the %s popped operand of the '%s' "
"operator (number of input datasets) must be an integer type",
cstring, token_string);
default:
error(EXIT_FAILURE, 0, "%s: type code %d not recognized",
__func__, numpop->type);
}
/* If control reaches here, then the number must have been a negative
value, so print an error. */
error(EXIT_FAILURE, 0, "the %s popped operand of the '%s' operator "
"cannot be zero or a negative number", cstring,
token_string);
return 0;
}
/**********************************************************************/
/**************** Filtering operators *****************/
/**********************************************************************/
#define ARITHMETIC_FILTER_DIM 10
struct arithmetic_filter_p
{
int operator; /* The type of filtering. */
size_t *fsize; /* Filter size. */
size_t *hpfsize; /* Positive Half-filter size. */
size_t *hnfsize; /* Negative Half-filter size. */
float sclip_multip; /* Sigma multiple in sigma-clipping. */
float sclip_param; /* Termination critera in sigma-cliping. */
gal_data_t *input; /* Input dataset. */
gal_data_t *out; /* Output dataset. */
};
/* Main filtering work function. */
static void *
arithmetic_filter(void *in_prm)
{
struct gal_threads_params *tprm=(struct gal_threads_params *)in_prm;
struct arithmetic_filter_p *afp=(struct arithmetic_filter_p *)tprm->params;
gal_data_t *input=afp->input;
size_t sind=-1;
int clipflags=0;
size_t ind, index, one=1;
gal_data_t *sigclip, *result=NULL;
size_t *hpfsize=afp->hpfsize, *hnfsize=afp->hnfsize;
size_t *tsize, *dsize=input->dsize, *fsize=afp->fsize;
size_t i, j, coord[ARITHMETIC_FILTER_DIM], ndim=input->ndim;
size_t start[ARITHMETIC_FILTER_DIM], end[ARITHMETIC_FILTER_DIM];
gal_data_t *tile=gal_data_alloc(NULL, input->type, ndim, afp->fsize,
NULL, 0, -1, 1, NULL, NULL, NULL);
/* Prepare the tile. */
free(tile->array);
tsize=tile->dsize;
tile->block=input;
/* Go over all the pixels that were assigned to this thread. */
for(i=0; tprm->indexs[i] != GAL_BLANK_SIZE_T; ++i)
{
/* For easy reading, put the index in 'ind'. */
ind=tprm->indexs[i];
/* Get the coordinate of the pixel. */
gal_dimension_index_to_coord(ind, ndim, dsize, coord);
/* See which dimensions need trimming. */
tile->size=1;
for(j=0;j<ndim;++j)
{
/* Estimate the coordinate of the filter's starting point. Note
that we are dealing with size_t (unsigned int) type here, so
there are no negatives. A negative result will produce an
extremely large number, so instead of checking for negative,
we can just see if the result of a subtraction is less than
the width of the input. */
if( (coord[j] - hnfsize[j] > dsize[j])
|| (coord[j] + hpfsize[j] >= dsize[j]) )
{
start[j] = ( (coord[j] - hnfsize[j] > dsize[j])
? 0 : coord[j] - hnfsize[j] );
end[j] = ( (coord[j] + hpfsize[j] >= dsize[j])
? dsize[j]
: coord[j] + hpfsize[j] + 1);
tsize[j] = end[j] - start[j];
}
else /* NOT on the edge (given requested filter width). */
{
tsize[j] = fsize[j];
start[j] = coord[j] - hnfsize[j];
}
tile->size *= tsize[j];
}
/* For a test.
printf("coord: %zu, %zu\n", coord[1]+1, coord[0]+1);
printf("\tstart: %zu, %zu\n", start[1]+1, start[0]+1);
printf("\ttsize: %zu, %zu\n", tsize[1], tsize[0]);
*/
/* Set the tile's starting pointer. */
index=gal_dimension_coord_to_index(ndim, dsize, start);
tile->array=gal_pointer_increment(input->array, index, input->type);
/* Do the necessary calculation. */
switch(afp->operator)
{
case ARITHMETIC_OP_FILTER_MINIMUM:
result=gal_statistics_minimum(tile);
break;
case ARITHMETIC_OP_FILTER_MAXIMUM:
result=gal_statistics_maximum(tile);
break;
case ARITHMETIC_OP_FILTER_MEDIAN:
result=gal_statistics_median(tile, 0);
break;
case ARITHMETIC_OP_FILTER_MEAN:
result=gal_statistics_mean(tile);
break;
case ARITHMETIC_OP_FILTER_SIGCLIP_STD:
case ARITHMETIC_OP_FILTER_MADCLIP_STD:
case ARITHMETIC_OP_FILTER_SIGCLIP_MAD:
case ARITHMETIC_OP_FILTER_MADCLIP_MAD:
case ARITHMETIC_OP_FILTER_SIGCLIP_MEAN:
case ARITHMETIC_OP_FILTER_MADCLIP_MEAN:
case ARITHMETIC_OP_FILTER_SIGCLIP_MEDIAN:
case ARITHMETIC_OP_FILTER_MADCLIP_MEDIAN:
/* The mean needs to be explicitly requested. */
if( afp->operator == ARITHMETIC_OP_FILTER_SIGCLIP_MEAN
|| afp->operator == ARITHMETIC_OP_FILTER_MADCLIP_MEAN )
clipflags = GAL_STATISTICS_CLIP_OUTCOL_OPTIONAL_MEAN;
/* The STD must be explicitly requested for MAD-clipping. */
if( afp->operator == ARITHMETIC_OP_FILTER_MADCLIP_STD )
clipflags = GAL_STATISTICS_CLIP_OUTCOL_OPTIONAL_STD;
/* The MAD must be explicitly requested for sigma-clipping. */
if( afp->operator == ARITHMETIC_OP_FILTER_SIGCLIP_MAD )
clipflags = GAL_STATISTICS_CLIP_OUTCOL_OPTIONAL_STD;
/* Do the main operation. */
if( afp->operator==ARITHMETIC_OP_FILTER_SIGCLIP_STD
|| afp->operator==ARITHMETIC_OP_FILTER_SIGCLIP_MAD
|| afp->operator==ARITHMETIC_OP_FILTER_SIGCLIP_MEAN
|| afp->operator==ARITHMETIC_OP_FILTER_SIGCLIP_MEDIAN )
sigclip=gal_statistics_clip_sigma(tile, afp->sclip_multip,
afp->sclip_param, clipflags,
0, 1);
else
sigclip=gal_statistics_clip_mad(tile, afp->sclip_multip,
afp->sclip_param, clipflags,
0, 1);
/* Set the required index. */
switch(afp->operator)
{
case ARITHMETIC_OP_FILTER_SIGCLIP_STD:
case ARITHMETIC_OP_FILTER_MADCLIP_STD:
sind = GAL_STATISTICS_CLIP_OUTCOL_STD; break;
case ARITHMETIC_OP_FILTER_SIGCLIP_MAD:
case ARITHMETIC_OP_FILTER_MADCLIP_MAD:
sind = GAL_STATISTICS_CLIP_OUTCOL_MAD; break;
case ARITHMETIC_OP_FILTER_SIGCLIP_MEAN:
case ARITHMETIC_OP_FILTER_MADCLIP_MEAN:
sind = GAL_STATISTICS_CLIP_OUTCOL_MEAN; break;
case ARITHMETIC_OP_FILTER_SIGCLIP_MEDIAN:
case ARITHMETIC_OP_FILTER_MADCLIP_MEDIAN:
sind = GAL_STATISTICS_CLIP_OUTCOL_MEDIAN; break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at "
"%s to fix the problem. The 'afp->operator' value "
"%d is not recognized as sigma-clipped median or "
"mean", __func__, PACKAGE_BUGREPORT, afp->operator);
}
/* Allocate the output and write the value into it. */
result=gal_data_alloc(NULL, GAL_TYPE_FLOAT32, 1, &one, NULL,
0, -1, 1, NULL, NULL, NULL);
((float *)(result->array))[0]=((float *)(sigclip->array))[sind];
/* Clean up. */
gal_data_free(sigclip);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s "
"to fix the problem. 'afp->operator' code %d is not "
"recognized", __func__, PACKAGE_BUGREPORT,
afp->operator);
}
/* Make sure the output array type and result's type are the
same. */
if(result->type!=afp->out->type)
result=gal_data_copy_to_new_type_free(result, afp->out->type);
/* Copy the result into the output array. */
memcpy(gal_pointer_increment(afp->out->array, ind, afp->out->type),
result->array, gal_type_sizeof(afp->out->type));
/* Clean up for this pixel. */
gal_data_free(result);
}
/* Clean up for this thread. */
tile->array=NULL;
tile->block=NULL;
gal_data_free(tile);
/* Wait for all the other threads to finish, then return. */
if(tprm->b) pthread_barrier_wait(tprm->b);
return NULL;
}
static void
wrapper_for_filter(struct arithmeticparams *p, char *token, int operator)
{
int type=GAL_TYPE_INVALID;
size_t i=0, ndim, nparams, one=1;
struct arithmetic_filter_p afp={0};
size_t fsize[ARITHMETIC_FILTER_DIM];
gal_data_t *tmp, *tmp2, *zero, *comp, *params_list=NULL;
size_t hnfsize[ARITHMETIC_FILTER_DIM], hpfsize[ARITHMETIC_FILTER_DIM];
int isclip=( operator==ARITHMETIC_OP_FILTER_SIGCLIP_STD
|| operator==ARITHMETIC_OP_FILTER_MADCLIP_STD
|| operator==ARITHMETIC_OP_FILTER_SIGCLIP_MAD
|| operator==ARITHMETIC_OP_FILTER_MADCLIP_MAD
|| operator==ARITHMETIC_OP_FILTER_SIGCLIP_MEAN
|| operator==ARITHMETIC_OP_FILTER_MADCLIP_MEAN
|| operator==ARITHMETIC_OP_FILTER_SIGCLIP_MEDIAN
|| operator==ARITHMETIC_OP_FILTER_MADCLIP_MEDIAN );
/* Get the input's number of dimensions. */
afp.input=operands_pop(p, token);
afp.operator=operator;
ndim=afp.input->ndim;
afp.hnfsize=hnfsize;
afp.hpfsize=hpfsize;
afp.fsize=fsize;
/* A small sanity check. */
if(ndim>ARITHMETIC_FILTER_DIM)
error(EXIT_FAILURE, 0, "%s: currently only datasets with less than "
"%d dimensions are acceptable. The input has %zu dimensions",
__func__, ARITHMETIC_FILTER_DIM, ndim);
/* A zero value for checking the value of input widths. */
zero=gal_data_alloc(NULL, GAL_TYPE_INT32, 1, &one, NULL, 1, -1, 1, NULL,
NULL, NULL);
/* Based on the first popped operand's dimensions and the operator, of
pop the necessary number of operands. */
nparams = ndim + (isclip ? 2 : 0 );
for(i=0;i<nparams;++i)
{
/* Add this to the list of parameters. */
gal_list_data_add(¶ms_list, operands_pop(p, token));
/* Make sure it only has a single element. */
if(params_list->size!=1)
error(EXIT_FAILURE, 0, "the parameters given to the filtering "
"operators can only be numbers. Operand number %zu after "
"the main input has %zu elements, so its an array", i,
params_list->size);
}
/* If this is a sigma-clipping filter, the top two operands are the
sigma-clipping parameters. */
if(isclip)
{
/* Read the sigma-clipping multiple (first element in the list). */
tmp=gal_list_data_pop(¶ms_list);
tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_FLOAT32);
afp.sclip_multip=*(float *)(tmp->array);
gal_data_free(tmp);
/* Read the sigma-clipping termination parameter. */
tmp=gal_list_data_pop(¶ms_list);
tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_FLOAT32);
afp.sclip_param=*(float *)(tmp->array);
gal_data_free(tmp);
}
/* If the input only has one element, it is most probably and error (the
user has confused the parameters with the input dataset). So report a
warning filtering makes no sense, so don't waste time, just add the
input onto the stack. */
if(afp.input->size==1)
{
/* Inform the user that this is suspicious. */
if(p->cp.quiet==0)
error(EXIT_SUCCESS, 0, "WARNING: the first popped operand to the "
"filtering operators has a single element! This is most "
"probably a typo in the order of operands! Recall that the "
"filtering operators need the main input image/cube as the "
"first popped operand");
/* Set the input as the output. */
afp.out=afp.input;
}
else
{
/* Allocate an array for the size of the filter and fill it in. The
values must be written in the inverse order since the user gives
dimensions with the FITS standard. */
i=ndim-1;
for(tmp=params_list; tmp!=NULL; tmp=tmp->next)
{
/* Make sure the user has given an integer type. */
if(tmp->type==GAL_TYPE_FLOAT32 || tmp->type==GAL_TYPE_FLOAT64)
error(EXIT_FAILURE, 0, "lengths of filter along dimensions "
"must be integer values, not floats. The given length "
"along dimension %zu is a float", ndim-i);
/* Make sure it isn't negative. */
comp=gal_arithmetic(GAL_ARITHMETIC_OP_GT, 1, 0, tmp, zero);
if( *(uint8_t *)(comp->array) == 0 )
error(EXIT_FAILURE, 0, "lengths of filter along dimensions "
"must be positive. The given length in dimension %zu"
"is either zero or negative", ndim-i);
gal_data_free(comp);
/* Convert the input into size_t and put it into the array that
keeps the filter size. */
tmp2=gal_data_copy_to_new_type(tmp, GAL_TYPE_SIZE_T);
fsize[ i ] = *(size_t *)(tmp2->array);
gal_data_free(tmp2);
/* If the width is larger than the input's size, change the width
to the input's size. */
if( fsize[i] > afp.input->dsize[i] )
error(EXIT_FAILURE, 0, "%s: the filter size along dimension "
"%zu (%zu) is greater than the input's length in that "
"dimension (%zu)", __func__, i, fsize[i],
afp.input->dsize[i]);
/* Go onto the previous dimension. */
--i;
}
/* Set the half filter sizes. Note that when the size is an odd
number, the number of pixels before and after the actual pixel are
equal, but for an even number, we will look into one element more
when looking before than the ones after. */
for(i=0;i<ndim;++i)
{
if( fsize[i]%2 )
hnfsize[i]=hpfsize[i]=fsize[i]/2;
else
{ hnfsize[i]=fsize[i]/2; hpfsize[i]=fsize[i]/2-1; }
}
/* For a test.
printf("fsize: %zu, %zu\n", fsize[0], fsize[1]);
printf("hnfsize: %zu, %zu\n", hnfsize[0], hnfsize[1]);
printf("hpfsize: %zu, %zu\n", hpfsize[0], hpfsize[1]);
*/
/* Set the type of the output dataset. */
switch(operator)
{
case ARITHMETIC_OP_FILTER_MEDIAN:
case ARITHMETIC_OP_FILTER_MINIMUM:
case ARITHMETIC_OP_FILTER_MAXIMUM:
case ARITHMETIC_OP_FILTER_SIGCLIP_MEDIAN:
case ARITHMETIC_OP_FILTER_MADCLIP_MEDIAN:
type=afp.input->type;
break;
case ARITHMETIC_OP_FILTER_MEAN:
case ARITHMETIC_OP_FILTER_SIGCLIP_STD:
case ARITHMETIC_OP_FILTER_MADCLIP_STD:
case ARITHMETIC_OP_FILTER_SIGCLIP_MAD:
case ARITHMETIC_OP_FILTER_MADCLIP_MAD:
case ARITHMETIC_OP_FILTER_SIGCLIP_MEAN:
case ARITHMETIC_OP_FILTER_MADCLIP_MEAN:
type=GAL_TYPE_FLOAT64;
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! please contact us at %s "
"to fix the problem. The 'operator' code %d is not "
"recognized", __func__, PACKAGE_BUGREPORT, operator);
}
/* Allocate the output dataset. Note that filtering doesn't change
the units of the dataset. */
afp.out=gal_data_alloc(NULL, type, ndim, afp.input->dsize,
afp.input->wcs, 0, afp.input->minmapsize,
afp.input->quietmmap, NULL, afp.input->unit,
NULL);
/* Spin off threads for each pixel. */
gal_threads_spin_off(arithmetic_filter, &afp, afp.input->size,
p->cp.numthreads, p->cp.minmapsize,
p->cp.quietmmap);
}
/* Add the output to the top of the stack. */
operands_add(p, NULL, afp.out);
/* Clean up and add the output on top of the stack. */
gal_data_free(zero);
gal_list_data_free(params_list);
if(afp.input!=afp.out) gal_data_free(afp.input); /* Single-element. */
}
/***************************************************************/
/************* Other functions *************/
/***************************************************************/
static int
arithmetic_binary_sanity_checks(gal_data_t *in, gal_data_t *conn,
char *operator)
{
int conn_int;
/* Do proper sanity checks on 'conn'. */
if(conn->size!=1)
error(EXIT_FAILURE, 0, "the first popped operand to '%s' must be a "
"single number. However, it has %zu elements", operator,
conn->size);
if(conn->type==GAL_TYPE_FLOAT32 || conn->type==GAL_TYPE_FLOAT64)
error(EXIT_FAILURE, 0, "the first popped operand to '%s' is the "
"connectivity (a value between 1 and the number of dimensions) "
"therefore, it must NOT be a floating point", operator);
/* Convert the connectivity value to a 32-bit integer and read it in and
make sure it is not larger than the number of dimensions. */
conn=gal_data_copy_to_new_type_free(conn, GAL_TYPE_INT32);
conn_int = *((int32_t *)(conn->array));
if(conn_int>in->ndim)
error(EXIT_FAILURE, 0, "the first popped operand of '%s' (%d) is "
"larger than the number of dimensions in the second-popped "
"operand (%zu)", operator, conn_int, in->ndim);
/* Make sure the array has an unsigned 8-bit type. */
if(in->type!=GAL_TYPE_UINT8)
error(EXIT_FAILURE, 0, "the second popped operand of '%s' has a type "
"of %s. However, it must be a binary dataset (only being equal "
"to zero is checked). You can use the 'uint8' operator for type "
"conversion, alternatively, if all your values are positive "
"and floating point, you can use '0 gt', if you want non-blank "
"values you can use 'isblank not' and many other operators that "
"produce a binary output", operator, gal_type_name(in->type, 1));
/* Clean up and return the integer value of 'conn'. */
gal_data_free(conn);
return conn_int;
}
static void
arithmetic_erode_dilate(struct arithmeticparams *p, char *token, int op)
{
int conn_int;
/* Pop the two necessary operands. */
gal_data_t *conn = operands_pop(p, token);
gal_data_t *in = operands_pop(p, token);
/* Do the sanity checks. */
conn_int=arithmetic_binary_sanity_checks(in, conn, token);
/* Do the operation. */
switch(op)
{
case ARITHMETIC_OP_ERODE: gal_binary_erode(in, 1, conn_int, 1);
break;
case ARITHMETIC_OP_DILATE: gal_binary_dilate(in, 1, conn_int, 1);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. The operator code %d not recognized",
__func__, PACKAGE_BUGREPORT, op);
}
/* Push the result onto the stack. */
operands_add(p, NULL, in);
}
static void
arithmetic_number_neighbors(struct arithmeticparams *p, char *token, int op)
{
int conn_int;
/* Pop the two necessary operands. */
gal_data_t *conn = operands_pop(p, token);
gal_data_t *in = operands_pop(p, token);
/* Do the sanity checks and do the job. */
conn_int=arithmetic_binary_sanity_checks(in, conn, token);
in=gal_binary_number_neighbors(in, conn_int, 1);
/* Push the result onto the stack. */
operands_add(p, NULL, in);
}
static void
arithmetic_connected_components(struct arithmeticparams *p, char *token)
{
int conn_int;
gal_data_t *out=NULL;
/* Pop the two necessary operands. */
gal_data_t *conn = operands_pop(p, token);
gal_data_t *in = operands_pop(p, token);
/* Basic sanity checks. */
conn_int=arithmetic_binary_sanity_checks(in, conn, token);
/* Do the connected components labeling. */
gal_binary_connected_components(in, &out, conn_int);
/* Push the result onto the stack. */
operands_add(p, NULL, out);
/* Clean up ('conn' was freed in the sanity check). */
gal_data_free(in);
}
static void
arithmetic_fill_holes(struct arithmeticparams *p, char *token)
{
int conn_int;
/* Pop the two necessary operands. */
gal_data_t *conn = operands_pop(p, token);
gal_data_t *in = operands_pop(p, token);
/* Basic sanity checks. */
conn_int=arithmetic_binary_sanity_checks(in, conn, token);
/* Fill the holes. */
gal_binary_holes_fill(in, conn_int, -1);
/* Push the result onto the stack. */
operands_add(p, NULL, in);
}
static void
arithmetic_invert(struct arithmeticparams *p, char *token)
{
gal_data_t *in = operands_pop(p, token);
uint8_t *u8 = in->array, *u8f = u8 + in->size;
uint8_t *u16 = in->array, *u16f = u16 + in->size;
uint8_t *u32 = in->array, *u32f = u32 + in->size;
uint8_t *u64 = in->array, *u64f = u64 + in->size;
/* Do the inversion based on type. */
switch(in->type)
{
case GAL_TYPE_UINT8: do *u8=UINT8_MAX-*u8; while(++u8<u8f); break;
case GAL_TYPE_UINT16: do *u16=UINT16_MAX-*u16;while(++u16<u16f); break;
case GAL_TYPE_UINT32: do *u32=UINT32_MAX-*u32;while(++u32<u32f); break;
case GAL_TYPE_UINT64: do *u64=UINT64_MAX-*u64;while(++u64<u64f); break;
default:
error(EXIT_FAILURE, 0, "'invert' operand has %s type. 'invert' can "
"only take unsigned integer types.\n\nYou can use any of the "
"'uint8', 'uint16', 'uint32', or 'uint64' operators to chage "
"the type before calling 'invert'",
gal_type_name(in->type, 1));
}
/* Push the result onto the stack. */
operands_add(p, NULL, in);
}
#define INTERPOLATE_REGION(TYPE,OP,FUNC) { \
TYPE mm, b, *a=in->array, *m=minmax->array; \
FUNC(in->type, &mm); \
gal_blank_write(&b, in->type); \
for(i=0;i<minmax->size;++i) m[i]=mm; \
for(i=0;i<in->size;++i) \
{ \
if( l[i]>0 ) \
GAL_DIMENSION_NEIGHBOR_OP(i, in->ndim, in->dsize, ocon[0], \
dinc, \
{ \
if( in->type==GAL_TYPE_FLOAT32 \
|| in->type==GAL_TYPE_FLOAT64 ) \
{ if( a[nind] OP m[l[i]] ) m[l[i]]=a[nind]; } \
else \
{ if( a[nind]!=b && a[nind] OP m[l[i]] ) \
m[l[i]]=a[nind]; } \
}); \
} \
for(i=0;i<in->size;++i) if( l[i]>0 ) { a[i]=m[l[i]]; } \
}
#define INTERPOLATE_REGION_OP(TYPE) { \
switch(operator) \
{ \
case ARITHMETIC_OP_INTERPOLATE_MINOFREGION: \
INTERPOLATE_REGION(TYPE,<,gal_type_max); break; \
case ARITHMETIC_OP_INTERPOLATE_MAXOFREGION: \
INTERPOLATE_REGION(TYPE,>,gal_type_min); break; \
default: \
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to " \
"fix the problem. The operator code %d isn't recognized", \
__func__, PACKAGE_BUGREPORT, operator); \
} \
}
static void
arithmetic_interpolate_region(struct arithmeticparams *p,
int operator, char *token)
{
/* Pop the dataset to interpolate. */
int32_t *l;
gal_data_t *minmax;
gal_data_t *lab=NULL, *flag;
size_t i, *icon, *ocon, *dinc, numlabs;
/* First pop the number of nearby neighbors.*/
gal_data_t *outconn = operands_pop(p, token);
gal_data_t *inconn = operands_pop(p, token);
/* Then pop the actual dataset to interpolate. */
gal_data_t *in = operands_pop(p, token);
/* Do proper sanity checks on 'con'. */
if(inconn->size!=1 || outconn->size!=1)
error(EXIT_FAILURE, 0, "the first and second popped operands to the "
"'interpolate-*ofregion' operators must be a single number. "
"However, it has %zu elements", inconn->size);
if( inconn->type==GAL_TYPE_FLOAT32
|| inconn->type==GAL_TYPE_FLOAT64
|| outconn->type==GAL_TYPE_FLOAT32
|| outconn->type==GAL_TYPE_FLOAT64 )
error(EXIT_FAILURE, 0, "the first and second popped operands to "
"'interpolate-*ofregion' operators are the connectivity to "
"define connected regions. The first popped is used to the "
"define the connectivity of the outer neighbors and the "
"second popped operand is used to define the region. They "
"should both be a counter (a positive integer) with a maximum "
"of the number of dimensions of the input). They cannot NOT "
"be a floating point. If its already an integer, but in a "
"floating point container, you can use the 'int32' operator "
"to convert it to a 32-bit integer");
/* Convert connectivity to an integer type and make sure its not larger
than dimensions of the input. */
inconn=gal_data_copy_to_new_type_free(inconn, GAL_TYPE_SIZE_T);
outconn=gal_data_copy_to_new_type_free(outconn, GAL_TYPE_SIZE_T);
icon=inconn->array;
ocon=outconn->array;
if(icon[0]>in->ndim || ocon[0]>in->ndim)
error(EXIT_FAILURE, 0, "the first and second popped operands to "
"'interpolate-*ofregion' operators must not be larger than "
"the number of dimensions of the input. The connectivity is "
"used to define connected blank regions. For example in a "
"2D dataset, a connectivity of 1 corresponds to 4-connected "
"neighbors and connectivity 2 corresponds to 8-connected "
"neighbors");
/* First make sure the input has blank values. If it doesn't, don't waste
time, just return it. */
if( gal_blank_present(in, 1)==0 )
{ operands_add(p, NULL, in); return; }
/* Build a binary image with the blank regions masked and label them,
then free the flagged array. */
flag=gal_blank_flag(in);
numlabs=gal_binary_connected_components(flag, &lab, icon[0]);
gal_data_free(flag);
/* Allocate array to keep maximum values for each region. Just note that
because we count the regions from 1, but the indexs from 0, we'll
allocate one extra point. */
++numlabs;
minmax=gal_data_alloc(NULL, in->type, 1, &numlabs, NULL, 0, in->minmapsize,
in->quietmmap, NULL, NULL, NULL);
/* Parse the dataset elements for NaNs. */
l=lab->array;
dinc=gal_dimension_increment(in->ndim, in->dsize);
switch(in->type)
{
case GAL_TYPE_UINT8: INTERPOLATE_REGION_OP(uint8_t); break;
case GAL_TYPE_INT8: INTERPOLATE_REGION_OP(int8_t); break;
case GAL_TYPE_UINT16: INTERPOLATE_REGION_OP(uint16_t); break;
case GAL_TYPE_INT16: INTERPOLATE_REGION_OP(int16_t); break;
case GAL_TYPE_UINT32: INTERPOLATE_REGION_OP(uint32_t); break;
case GAL_TYPE_INT32: INTERPOLATE_REGION_OP(int32_t); break;
case GAL_TYPE_FLOAT32: INTERPOLATE_REGION_OP(float); break;
case GAL_TYPE_FLOAT64: INTERPOLATE_REGION_OP(double); break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. The code %d is not a recognized data "
"type", __func__, PACKAGE_BUGREPORT, in->type);
}
/* For tests.
gal_fits_img_write(lab, "test-out.fits", NULL, NULL);
gal_fits_img_write(in, "test-out.fits", NULL, NULL);
printf("\n...%s...\n", __func__); exit(0);
*/
/* Clean up. */
free(dinc);
gal_data_free(lab);
gal_data_free(minmax);
gal_data_free(inconn);
/* Push the interpolated dataset onto the stack. */
operands_add(p, NULL, in);
}
static void
arithmetic_interpolate(struct arithmeticparams *p, int operator, char *token)
{
gal_data_t *interpolated;
int num_int, interpop=GAL_ARITHMETIC_OP_INVALID;
/* First pop the number of nearby neighbors. */
gal_data_t *num = operands_pop(p, token);
/* Then pop the actual dataset to interpolate. */
gal_data_t *in = operands_pop(p, token);
/* Do proper sanity checks on 'num'. */
if(num->size!=1)
error(EXIT_FAILURE, 0, "the first popped operand to "
"'interpolate-XXXXXngb' operators must be a single number. "
"However, it has %zu elements", num->size);
if(num->type==GAL_TYPE_FLOAT32 || num->type==GAL_TYPE_FLOAT64)
error(EXIT_FAILURE, 0, "the first popped operand to "
"'interpolate-XXXXXngb' operators is the number of nearby "
"neighbors (a counter, an integer). It must NOT be a floating "
"point.\n\n"
"If its already an integer, but in a floating point container, "
"you can use the 'int32' operator to convert it to a 32-bit "
"integer for example");
/* Convert the given number to a 32-bit integer and read it in. */
num=gal_data_copy_to_new_type_free(num, GAL_TYPE_INT32);
num_int = *((int32_t *)(num->array));
/* Set the interpolation operator. */
switch(operator)
{
case ARITHMETIC_OP_INTERPOLATE_MINNGB:
interpop=GAL_INTERPOLATE_NEIGHBORS_FUNC_MIN;
break;
case ARITHMETIC_OP_INTERPOLATE_MAXNGB:
interpop=GAL_INTERPOLATE_NEIGHBORS_FUNC_MAX;
break;
case ARITHMETIC_OP_INTERPOLATE_MEANNGB:
interpop=GAL_INTERPOLATE_NEIGHBORS_FUNC_MEAN;
break;
case ARITHMETIC_OP_INTERPOLATE_MEDIANNGB:
interpop=GAL_INTERPOLATE_NEIGHBORS_FUNC_MEDIAN;
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. The operator code %d isn't recognized",
__func__, PACKAGE_BUGREPORT, operator);
}
/* Call the interpolation function. */
interpolated=gal_interpolate_neighbors(in, NULL, p->cp.interpmetric,
num_int, p->cp.numthreads,
1, 0, interpop);
/* Clean up and push the interpolated array onto the stack. */
gal_data_free(in);
gal_data_free(num);
operands_add(p, NULL, interpolated);
}
static void
arithmetic_collapse_single_value(gal_data_t *input, char *counter,
char *opstr, char *description,
int checkint)
{
if( input->ndim!=1 || input->size!=1)
error(EXIT_FAILURE, 0, "%s popped operand of 'collapse-%s*' "
"operators (%s) must be a single number (single-element, "
"one-dimensional dataset). But it has %zu dimension(s) and "
"%zu element(s).", counter, opstr, description, input->ndim,
input->size);
if(checkint)
if(input->type==GAL_TYPE_FLOAT32 || input->type==GAL_TYPE_FLOAT64)
error(EXIT_FAILURE, 0, "%s popped operand of 'collapse-%s*' "
"operators (%s) must have an integer type, but it has a "
"floating point type ('%s')", counter, opstr, description,
gal_type_name(input->type,1));
}
static void
arithmetic_collapse(struct arithmeticparams *p, char *token, int operator)
{
long dim;
float p1=NAN, p2=NAN;
int qmm=p->cp.quietmmap;
gal_data_t *dimension=NULL, *input=NULL;
size_t nt=p->cp.numthreads, mms=p->cp.minmapsize;
gal_data_t *collapsed=NULL, *param1=NULL, *param2=NULL;
/* First popped operand is the dimension. */
dimension = operands_pop(p, token);
/* If we are on any of the sigma-clipping operators, we need to pop two
more operands. */
if( operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_MAD
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_STD
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_MEAN
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_MEDIAN
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_NUMBER
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_MAD
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_STD
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_MEAN
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_MEDIAN
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_NUMBER
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MAD
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_STD
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MEAN
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MEDIAN
|| operator==ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_NUMBER
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MAD
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_STD
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MEAN
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MEDIAN
|| operator==ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_NUMBER )
{
param2=operands_pop(p, token); /* Termination criteria. */
param1=operands_pop(p, token); /* Multiple of sigma. */
}
/* Final popped operand is the desired input dataset. */
input = operands_pop(p, token);
/* Sanity checks. */
arithmetic_collapse_single_value(dimension, "first", "",
"dimension to collapse", 1);
dimension=gal_data_copy_to_new_type_free(dimension, GAL_TYPE_LONG);
dim=((long *)(dimension->array))[0];
if(dim<=0)
error(EXIT_FAILURE, 0, "first popped operand of 'collapse-*' "
"operators (dimension to collapse) must be positive (larger "
"than zero), it is %ld", dim);
if(dim > input->ndim)
error(EXIT_FAILURE, 0, "input dataset to '%s' has %zu dimension(s), "
"but you have asked to collapse along dimension %ld", token,
input->ndim, dim);
if(param1)
{
arithmetic_collapse_single_value(param1, "third", "sigclip-",
"sigma-clip's multiple of sigma",
0);
arithmetic_collapse_single_value(param2, "second", "sigclip-",
"sigma-clip's termination param",
0);
param1=gal_data_copy_to_new_type_free(param1, GAL_TYPE_FLOAT32);
param2=gal_data_copy_to_new_type_free(param2, GAL_TYPE_FLOAT32);
p1=((float *)(param1->array))[0];
p2=((float *)(param2->array))[0];
if(p1<=0 || p2<=0)
error(EXIT_FAILURE, 0, "third and second popped operands of "
"'collapse-sigclip-*' operators (sigma multiple and "
"termination criteria) must be positive (larger than "
"zero), but they are %g and %g", p1, p2);
}
/* If a WCS structure has been read, we'll need to pass it to
'gal_dimension_collapse', so it modifies it respectively. */
if(p->wcs_collapsed==0)
{
p->wcs_collapsed=1;
input->wcs=p->refdata.wcs;
}
/* Run the relevant library function. */
switch(operator)
{
case ARITHMETIC_OP_COLLAPSE_SUM:
collapsed=gal_dimension_collapse_sum(input, input->ndim-dim, NULL);
break;
case ARITHMETIC_OP_COLLAPSE_MEAN:
collapsed=gal_dimension_collapse_mean(input, input->ndim-dim, NULL);
break;
case ARITHMETIC_OP_COLLAPSE_NUMBER:
collapsed=gal_dimension_collapse_number(input, input->ndim-dim);
break;
case ARITHMETIC_OP_COLLAPSE_MIN:
collapsed=gal_dimension_collapse_minmax(input, input->ndim-dim, 0);
break;
case ARITHMETIC_OP_COLLAPSE_MAX:
collapsed=gal_dimension_collapse_minmax(input, input->ndim-dim, 1);
break;
case ARITHMETIC_OP_COLLAPSE_MEDIAN:
collapsed=gal_dimension_collapse_median(input, input->ndim-dim,
nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_MAD:
collapsed=gal_dimension_collapse_mclip_std(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MAD:
collapsed=gal_dimension_collapse_mclip_fill_std(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_STD:
collapsed=gal_dimension_collapse_mclip_std(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_STD:
collapsed=gal_dimension_collapse_mclip_fill_std(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_MEAN:
collapsed=gal_dimension_collapse_mclip_mean(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MEAN:
collapsed=gal_dimension_collapse_mclip_fill_mean(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_MEDIAN:
collapsed=gal_dimension_collapse_mclip_median(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MEDIAN:
collapsed=gal_dimension_collapse_mclip_fill_median(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_NUMBER:
collapsed=gal_dimension_collapse_mclip_number(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_NUMBER:
collapsed=gal_dimension_collapse_mclip_fill_number(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_MAD:
collapsed=gal_dimension_collapse_sclip_std(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MAD:
collapsed=gal_dimension_collapse_sclip_fill_std(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_STD:
collapsed=gal_dimension_collapse_sclip_std(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_STD:
collapsed=gal_dimension_collapse_sclip_fill_std(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_MEAN:
collapsed=gal_dimension_collapse_sclip_mean(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MEAN:
collapsed=gal_dimension_collapse_sclip_fill_mean(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_MEDIAN:
collapsed=gal_dimension_collapse_sclip_median(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MEDIAN:
collapsed=gal_dimension_collapse_sclip_fill_median(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_NUMBER:
collapsed=gal_dimension_collapse_sclip_number(input, input->ndim-dim,
p1, p2, nt, mms, qmm);
break;
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_NUMBER:
collapsed=gal_dimension_collapse_sclip_fill_number(input,
input->ndim-dim, p1, p2, nt, mms, qmm);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s to "
"fix the problem. The operator code %d is not recognized",
__func__, PACKAGE_BUGREPORT, operator);
}
/* If a WCS structure existed, a modified WCS is now present in
'collapsed->wcs'. So we'll let the freeing of 'input' free the old
'p->refdata.wcs' structure and we'll put the new one there, then we'll
set 'collapsed->wcs' to 'NULL', so the new one isn't freed. */
if(collapsed->wcs)
{
p->refdata.wcs = collapsed->wcs;
collapsed->wcs = NULL;
}
/* We'll also need to correct the size of the reference dataset if it
hasn't been corrected yet. We'll use 'memcpy' to write the new 'dsize'
values into the old ones. The dimensions have decreased, so we won't
be writing outside of allocated space that 'p->refdata.dsize' points
to. */
if( p->refdata.ndim != collapsed->ndim )
{
p->refdata.ndim -= 1;
memcpy( p->refdata.dsize, collapsed->dsize,
p->refdata.ndim * (sizeof *p->refdata.dsize) );
}
/* Clean up and add the collapsed dataset to the top of the operands. */
gal_data_free(input);
gal_data_free(dimension);
operands_add(p, NULL, collapsed);
}
void
arithmetic_tofile(struct arithmeticparams *p, char *token, int freeflag)
{
/* Pop the top dataset. */
gal_data_t *popped = operands_pop(p, token);
char *filename = &token[ freeflag
? OPERATOR_PREFIX_LENGTH_TOFILEFREE
: OPERATOR_PREFIX_LENGTH_TOFILE ];
/* Make sure that if the file exists, it is deleted. */
gal_checkset_writable_remove(filename, NULL, p->cp.keep,
p->cp.dontdelete);
/* Save it to a file. */
popped->wcs=p->refdata.wcs;
if(popped->ndim==1 && p->onedasimage==0)
gal_table_write(popped, NULL, NULL, p->cp.tableformat, filename,
"ARITHMETIC", 0, 0);
else gal_fits_img_write(popped, filename, NULL, 0);
if(!p->cp.quiet)
printf(" - Write: %s\n", filename);
/* Reset the WCS to NULL and put it back on the stack. */
popped->wcs=NULL;
if(freeflag) gal_data_free(popped);
else operands_add(p, NULL, popped);
}
void
arithmetic_add_dimension(struct arithmeticparams *p, char *token,
int operator)
{
size_t one=1;
gal_data_t *ref=NULL, *out=NULL;
size_t i, j, num, dsize[3], nbytes=0;
gal_data_t *tmp = operands_pop(p, token);
/* Make sure the first operand is a number. */
if(tmp->size!=1)
error(EXIT_FAILURE, 0, "first popped operand to '%s' must be a "
"number (specifying how many datasets to use)", token);
/* Put the first-popped value into 'num'. */
tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_SIZE_T);
num=*(size_t *)(tmp->array);
gal_data_free(tmp);
/* Pop all the datasets and put them in a list. */
for(i=0;i<num;++i)
{
/* Pop the operand. */
tmp=operands_pop(p, token);
/* Things that differ from the first dataset and the rest. */
if(out) /* Not the first. */
{
/* All entries have to have the same data type. */
if(tmp->type!=out->type)
error(EXIT_FAILURE, 0, "the operands to '%s' must have "
"the same data type (the first popped operand has a "
"'%s' type, but the popped operand number %zu has "
"type '%s')", token, gal_type_name(out->type, 1), i+1,
gal_type_name(tmp->type, 1));
/* All entries should also have the same dimension. */
if( gal_dimension_is_different(ref, tmp) )
error(EXIT_FAILURE, 0, "the operands to '%s' must have the "
"same size in all dimensions", token);
}
else /* First popped operand. */
{
/* First popped operand, do necessary basic checks here. */
switch(operator)
{
case ARITHMETIC_OP_ADD_DIMENSION_SLOW:
dsize[0]=num;
dsize[1]=tmp->dsize[0];
dsize[2]=tmp->dsize[1];
nbytes=gal_type_sizeof(tmp->type)*tmp->size;
if(tmp->ndim!=2)
error(EXIT_FAILURE, 0, "currently only 2-dimensional "
"datasets are acceptable for '%s', please get in "
"touch with us at %s so we add functionality for "
"different dimensions", token, PACKAGE_BUGREPORT);
break;
case ARITHMETIC_OP_ADD_DIMENSION_FAST:
dsize[1]=num;
dsize[0]=tmp->dsize[0];
nbytes=gal_type_sizeof(tmp->type);
if(tmp->ndim!=1)
error(EXIT_FAILURE, 0, "currently only 1-dimensional "
"datasets are acceptable for '%s', please get in "
"touch with us at %s so we add functionality for "
"different dimensions", token, PACKAGE_BUGREPORT);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at "
"%s to address the problem. The operator code '%d' "
"isn't recognized", __func__, PACKAGE_BUGREPORT,
operator);
}
/* Allocate the size-reference dataset to simplify checking of
the sizes for each new dataset. This is done because this
dataset that is actually being popped will be freed right
after its values have been written into the output. We don't
care about any data from this dataset, and to avoid wasting
memory, we will simply allocate a one-element dataset. But
we'll many set its sizes to the size of the popped dataset. */
ref=gal_data_alloc(NULL, tmp->type, 1, &one, NULL, 0, -1, 1,
NULL, NULL, NULL);
free(ref->dsize);
ref->ndim=tmp->ndim;
ref->dsize=gal_pointer_allocate(GAL_TYPE_SIZE_T, ref->ndim, 0,
__func__, "ref->dsize");
for(j=0;j<ref->ndim;++j) ref->dsize[j]=tmp->dsize[j];
/* Allocate the output dataset. */
out = gal_data_alloc(NULL, tmp->type, tmp->ndim+1, dsize, NULL,
0, p->cp.minmapsize, p->cp.quietmmap, NULL,
NULL, NULL);
}
/* Copy the dataset into the higher-dimensional output. */
switch(operator)
{
case ARITHMETIC_OP_ADD_DIMENSION_SLOW:
memcpy(gal_pointer_increment(out->array, (num-1-i)*tmp->size,
tmp->type),
tmp->array, nbytes);
break;
case ARITHMETIC_OP_ADD_DIMENSION_FAST:
for(j=0;j<tmp->size;++j)
memcpy(gal_pointer_increment(out->array, j*num+num-i-1,
out->type),
gal_pointer_increment(tmp->array, j, out->type),
nbytes);
break;
}
/* Clean up. */
gal_data_free(tmp);
}
/* Put the higher-dimensional output on the operands stack. */
operands_add(p, NULL, out);
/* Clean up. */
gal_data_free(ref);
}
void
arithmetic_repeat(struct arithmeticparams *p, char *token, int operator)
{
size_t i, num;
gal_data_t *ttmp, *tmp = operands_pop(p, token);
/* Make sure the first operand is a number. */
if(tmp->size!=1)
error(EXIT_FAILURE, 0, "first popped operand to '%s' must be a "
"number (specifying how many datasets to use)", token);
/* Put the first-popped value into 'num'. */
tmp=gal_data_copy_to_new_type_free(tmp, GAL_TYPE_SIZE_T);
num=*(size_t *)(tmp->array);
gal_data_free(tmp);
/* Pop the dataset to be copied. */
tmp=operands_pop(p, token);
/* Add it to the stack for the desired number of times. */
for(i=0; i<num; ++i)
{
ttmp=gal_data_copy(tmp);
operands_add(p, NULL, ttmp);
}
/* Clean up. */
gal_data_free(tmp);
}
/***************************************************************/
/************* Reverse Polish algorithm *************/
/***************************************************************/
static int
arithmetic_set_operator(char *string, size_t *num_operands, int *inlib)
{
/* Use the library's main function for its own operators. */
int op = gal_arithmetic_set_operator(string, num_operands);
/* Mark if this operator is in the library or not. */
*inlib = op==GAL_ARITHMETIC_OP_INVALID ? 0 : 1;
/* If its not a library operator, check if its an internal operator. */
if(op==GAL_ARITHMETIC_OP_INVALID)
{
/* See if its a custom operator to the Arithmetic program. */
if (!strcmp(string, "filter-mean"))
{ op=ARITHMETIC_OP_FILTER_MEAN; *num_operands=0; }
else if (!strcmp(string, "filter-median"))
{ op=ARITHMETIC_OP_FILTER_MEDIAN; *num_operands=0; }
else if (!strcmp(string, "filter-minimum"))
{ op=ARITHMETIC_OP_FILTER_MINIMUM; *num_operands=0; }
else if (!strcmp(string, "filter-maximum"))
{ op=ARITHMETIC_OP_FILTER_MAXIMUM; *num_operands=0; }
else if (!strcmp(string, "filter-sigclip-std"))
{ op=ARITHMETIC_OP_FILTER_SIGCLIP_STD; *num_operands=0; }
else if (!strcmp(string, "filter-sigclip-mad"))
{ op=ARITHMETIC_OP_FILTER_SIGCLIP_MAD; *num_operands=0; }
else if (!strcmp(string, "filter-madclip-std"))
{ op=ARITHMETIC_OP_FILTER_MADCLIP_STD; *num_operands=0; }
else if (!strcmp(string, "filter-madclip-mad"))
{ op=ARITHMETIC_OP_FILTER_MADCLIP_MAD; *num_operands=0; }
else if (!strcmp(string, "filter-sigclip-mean"))
{ op=ARITHMETIC_OP_FILTER_SIGCLIP_MEAN; *num_operands=0; }
else if (!strcmp(string, "filter-madclip-mean"))
{ op=ARITHMETIC_OP_FILTER_MADCLIP_MEAN; *num_operands=0; }
else if (!strcmp(string, "filter-sigclip-median"))
{ op=ARITHMETIC_OP_FILTER_SIGCLIP_MEDIAN; *num_operands=0; }
else if (!strcmp(string, "filter-madclip-median"))
{ op=ARITHMETIC_OP_FILTER_MADCLIP_MEDIAN; *num_operands=0; }
else if (!strcmp(string, "erode"))
{ op=ARITHMETIC_OP_ERODE; *num_operands=0; }
else if (!strcmp(string, "dilate"))
{ op=ARITHMETIC_OP_DILATE; *num_operands=0; }
else if (!strcmp(string, "number-neighbors"))
{ op=ARITHMETIC_OP_NUMBER_NEIGHBORS; *num_operands=0; }
else if (!strcmp(string, "connected-components"))
{ op=ARITHMETIC_OP_CONNECTED_COMPONENTS; *num_operands=0; }
else if (!strcmp(string, "fill-holes"))
{ op=ARITHMETIC_OP_FILL_HOLES; *num_operands=0; }
else if (!strcmp(string, "invert"))
{ op=ARITHMETIC_OP_INVERT; *num_operands=0; }
else if (!strcmp(string, "interpolate-minngb"))
{ op=ARITHMETIC_OP_INTERPOLATE_MINNGB; *num_operands=0; }
else if (!strcmp(string, "interpolate-maxngb"))
{ op=ARITHMETIC_OP_INTERPOLATE_MAXNGB; *num_operands=0; }
else if (!strcmp(string, "interpolate-meanngb"))
{ op=ARITHMETIC_OP_INTERPOLATE_MEANNGB; *num_operands=0; }
else if (!strcmp(string, "interpolate-medianngb"))
{ op=ARITHMETIC_OP_INTERPOLATE_MEDIANNGB; *num_operands=0; }
else if (!strcmp(string, "interpolate-minofregion"))
{ op=ARITHMETIC_OP_INTERPOLATE_MINOFREGION; *num_operands=0; }
else if (!strcmp(string, "interpolate-maxofregion"))
{ op=ARITHMETIC_OP_INTERPOLATE_MAXOFREGION; *num_operands=0; }
else if (!strcmp(string, "collapse-sum"))
{ op=ARITHMETIC_OP_COLLAPSE_SUM; *num_operands=0; }
else if (!strcmp(string, "collapse-min"))
{ op=ARITHMETIC_OP_COLLAPSE_MIN; *num_operands=0; }
else if (!strcmp(string, "collapse-max"))
{ op=ARITHMETIC_OP_COLLAPSE_MAX; *num_operands=0; }
else if (!strcmp(string, "collapse-mean"))
{ op=ARITHMETIC_OP_COLLAPSE_MEAN; *num_operands=0; }
else if (!strcmp(string, "collapse-number"))
{ op=ARITHMETIC_OP_COLLAPSE_NUMBER; *num_operands=0; }
else if (!strcmp(string, "collapse-median"))
{ op=ARITHMETIC_OP_COLLAPSE_MEDIAN; *num_operands=0; }
else if (!strcmp(string, "collapse-madclip-mad"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_MAD; *num_operands=0; }
else if (!strcmp(string, "collapse-madclip-std"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_STD; *num_operands=0; }
else if (!strcmp(string, "collapse-madclip-mean"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_MEAN; *num_operands=0; }
else if (!strcmp(string, "collapse-madclip-median"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_MEDIAN;*num_operands=0;}
else if (!strcmp(string, "collapse-madclip-number"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_NUMBER;*num_operands=0;}
else if (!strcmp(string, "collapse-sigclip-mad"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_MAD; *num_operands=0; }
else if (!strcmp(string, "collapse-sigclip-std"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_STD; *num_operands=0; }
else if (!strcmp(string, "collapse-sigclip-mean"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_MEAN; *num_operands=0; }
else if (!strcmp(string, "collapse-sigclip-median"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_MEDIAN;*num_operands=0;}
else if (!strcmp(string, "collapse-sigclip-number"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_NUMBER;*num_operands=0;}
else if (!strcmp(string, "collapse-madclip-fill-mad"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MAD; *num_operands=0; }
else if (!strcmp(string, "collapse-madclip-fill-std"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_STD; *num_operands=0; }
else if (!strcmp(string, "collapse-madclip-fill-mean"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MEAN; *num_operands=0; }
else if (!strcmp(string, "collapse-madclip-fill-median"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MEDIAN;*num_operands=0;}
else if (!strcmp(string, "collapse-madclip-fill-number"))
{ op=ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_NUMBER;*num_operands=0;}
else if (!strcmp(string, "collapse-sigclip-fill-mad"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MAD; *num_operands=0; }
else if (!strcmp(string, "collapse-sigclip-fill-std"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_STD; *num_operands=0; }
else if (!strcmp(string, "collapse-sigclip-fill-mean"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MEAN; *num_operands=0; }
else if (!strcmp(string, "collapse-sigclip-fill-median"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MEDIAN;*num_operands=0;}
else if (!strcmp(string, "collapse-sigclip-fill-number"))
{ op=ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_NUMBER;*num_operands=0;}
else if (!strcmp(string, "add-dimension-slow"))
{ op=ARITHMETIC_OP_ADD_DIMENSION_SLOW; *num_operands=0; }
else if (!strcmp(string, "add-dimension-fast"))
{ op=ARITHMETIC_OP_ADD_DIMENSION_FAST; *num_operands=0; }
else if (!strcmp(string, "repeat"))
{ op=ARITHMETIC_OP_REPEAT; *num_operands=0; }
else
error(EXIT_FAILURE, 0, "the argument '%s' could not be "
"interpretted as a file name, named dataset, number, "
"or operator", string);
}
/* Return the operator code. */
return op;
}
static gal_data_t *
arithmetic_prepare_meta(gal_data_t *d1, gal_data_t *d2, gal_data_t *d3)
{
size_t i;
gal_data_t *out;
/* A small sanity check (these operators only need a single
operator). */
if(d2 || d3)
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at '%s' to "
"find and fix the problem. Meta operators should only "
"take a single operand, but for some reason, this "
"hasn't occurred here", __func__, PACKAGE_BUGREPORT);
/* Allocate the output dataset. */
out=gal_data_alloc_empty(d1->ndim, d1->minmapsize, d1->quietmmap);
/* Fill the output dataset and return. */
for(i=0;i<d1->ndim;++i) out->dsize[i]=d1->dsize[i];
out->size=d1->size;
return out;
}
static void
arithmetic_operator_run(struct arithmeticparams *p, int operator,
char *operator_string, size_t num_operands,
int inlib)
{
size_t i;
unsigned int numop;
int flags = GAL_ARITHMETIC_FLAGS_BASIC;
gal_data_t *d1=NULL, *d2=NULL, *d3=NULL, *d4=NULL, *d5=NULL;
/* Set the operating-mode flags if necessary. */
if(p->cp.quiet) flags |= GAL_ARITHMETIC_FLAG_QUIET;
if(p->envseed) flags |= GAL_ARITHMETIC_FLAG_ENVSEED;
/* If this operator is in the library, we should pop everything here. */
if(inlib)
{
/* Pop the necessary number of operators. Note that the
operators are poped from a linked list (which is
last-in-first-out). So for the operators which need a
specific order, the first poped operand is actally the
last (right most, in in-fix notation) input operand.*/
switch(num_operands)
{
case 0:
break;
case 1:
d1=operands_pop(p, operator_string);
break;
case 2:
d2=operands_pop(p, operator_string);
d1=operands_pop(p, operator_string);
break;
case 3:
d3=operands_pop(p, operator_string);
d2=operands_pop(p, operator_string);
d1=operands_pop(p, operator_string);
break;
case 4:
d4=operands_pop(p, operator_string);
d3=operands_pop(p, operator_string);
d2=operands_pop(p, operator_string);
d1=operands_pop(p, operator_string);
break;
case 5:
d5=operands_pop(p, operator_string);
d4=operands_pop(p, operator_string);
d3=operands_pop(p, operator_string);
d2=operands_pop(p, operator_string);
d1=operands_pop(p, operator_string);
break;
case -1:
/* This case is when the number of operands is itself an
operand. So except for operators that have high-level
parameters (like sigma-clipping), the first popped operand
must be an integer number, we will use that to construct a
linked list of any number of operands within the single 'd1'
pointer. */
numop=pop_number_of_operands(p, operator, operator_string, &d2);
for(i=0;i<numop;++i)
gal_list_data_add(&d1, operands_pop(p, operator_string));
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at %s "
"to fix the problem. '%zu' is not recognized as an "
"operand counter (with '%s')", __func__,
PACKAGE_BUGREPORT, num_operands, operator_string);
}
/* Operators with special attention. */
switch(operator)
{
/* Meta operators (where only the information about the pixels is
relevant, not the pixel values themselves, like the 'index'
operator). For these (that only accept one operand), we should
add the first popped operand back on the stack. */
case GAL_ARITHMETIC_OP_INDEX:
case GAL_ARITHMETIC_OP_COUNTER:
operands_add(p, NULL, d1);
d1=arithmetic_prepare_meta(d1, d2, d3);
break;
/* Operators that also modify the reference WCS. */
case GAL_ARITHMETIC_OP_TRIM:
d1->wcs=gal_wcs_copy(p->refdata.wcs);
break;
/* Operators that need/modify the WCS. */
case GAL_ARITHMETIC_OP_POOLMAX:
case GAL_ARITHMETIC_OP_POOLMIN:
case GAL_ARITHMETIC_OP_POOLSUM:
case GAL_ARITHMETIC_OP_POOLMEAN:
case GAL_ARITHMETIC_OP_POOLMEDIAN:
/* Print warning in case of existing WCS. */
if(p->refdata.wcs)
{
if(p->cp.quiet==0)
error(EXIT_SUCCESS, 0, "WARNING: the WCS is not currently "
"supported for the pooling operators (the output "
"will not contain WCS). If it is necessary in your "
"usage, please get in touch with us at '%s'. You "
"can suppress this warning with the '--quiet' option",
PACKAGE_BUGREPORT);
gal_wcs_free(p->refdata.wcs);
p->refdata.wcs=NULL;
}
/* In case you want to pass the WCS to the library function ...
d1->wcs=gal_wcs_copy(p->refdata.wcs);
... uncomment the line above and comment the whole
'if(p->refdata.wcs)' check above (so the wcs is not freed).*/
break;
}
/* Run the arithmetic operation. Note that 'gal_arithmetic'
is a variable argument function (like printf). So the
number of arguments it uses depend on the operator. So
when the operator doesn't need three operands, the extra
arguments will be ignored. */
operands_add(p, NULL, gal_arithmetic(operator, p->cp.numthreads,
flags, d1, d2, d3, d4, d5));
/* Operators with special attention afterwards. */
switch(operator)
{
case GAL_ARITHMETIC_OP_TRIM:
gal_wcs_free(p->refdata.wcs);
p->refdata.wcs=gal_wcs_copy(p->operands->data->wcs);
break;
}
}
/* No need to call the arithmetic library, call the proper wrappers
directly. */
else
{
switch(operator)
{
case ARITHMETIC_OP_FILTER_MEAN:
case ARITHMETIC_OP_FILTER_MEDIAN:
case ARITHMETIC_OP_FILTER_MINIMUM:
case ARITHMETIC_OP_FILTER_MAXIMUM:
case ARITHMETIC_OP_FILTER_SIGCLIP_STD:
case ARITHMETIC_OP_FILTER_MADCLIP_STD:
case ARITHMETIC_OP_FILTER_SIGCLIP_MAD:
case ARITHMETIC_OP_FILTER_MADCLIP_MAD:
case ARITHMETIC_OP_FILTER_SIGCLIP_MEAN:
case ARITHMETIC_OP_FILTER_MADCLIP_MEAN:
case ARITHMETIC_OP_FILTER_SIGCLIP_MEDIAN:
case ARITHMETIC_OP_FILTER_MADCLIP_MEDIAN:
wrapper_for_filter(p, operator_string, operator);
break;
case ARITHMETIC_OP_ERODE:
case ARITHMETIC_OP_DILATE:
arithmetic_erode_dilate(p, operator_string, operator);
break;
case ARITHMETIC_OP_NUMBER_NEIGHBORS:
arithmetic_number_neighbors(p, operator_string, operator);
break;
case ARITHMETIC_OP_CONNECTED_COMPONENTS:
arithmetic_connected_components(p, operator_string);
break;
case ARITHMETIC_OP_FILL_HOLES:
arithmetic_fill_holes(p, operator_string);
break;
case ARITHMETIC_OP_INVERT:
arithmetic_invert(p, operator_string);
break;
case ARITHMETIC_OP_INTERPOLATE_MINNGB:
case ARITHMETIC_OP_INTERPOLATE_MAXNGB:
case ARITHMETIC_OP_INTERPOLATE_MEANNGB:
case ARITHMETIC_OP_INTERPOLATE_MEDIANNGB:
arithmetic_interpolate(p, operator, operator_string);
break;
case ARITHMETIC_OP_INTERPOLATE_MINOFREGION:
case ARITHMETIC_OP_INTERPOLATE_MAXOFREGION:
arithmetic_interpolate_region(p, operator, operator_string);
break;
case ARITHMETIC_OP_COLLAPSE_SUM:
case ARITHMETIC_OP_COLLAPSE_MIN:
case ARITHMETIC_OP_COLLAPSE_MAX:
case ARITHMETIC_OP_COLLAPSE_MEAN:
case ARITHMETIC_OP_COLLAPSE_MEDIAN:
case ARITHMETIC_OP_COLLAPSE_NUMBER:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_MAD:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_MAD:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_STD:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_STD:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_MEAN:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_MEAN:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_MEDIAN:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_MEDIAN:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_NUMBER:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_NUMBER:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MAD:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MAD:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_STD:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_STD:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MEAN:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MEAN:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_MEDIAN:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_MEDIAN:
case ARITHMETIC_OP_COLLAPSE_MADCLIP_FILL_NUMBER:
case ARITHMETIC_OP_COLLAPSE_SIGCLIP_FILL_NUMBER:
arithmetic_collapse(p, operator_string, operator);
break;
case ARITHMETIC_OP_ADD_DIMENSION_SLOW:
case ARITHMETIC_OP_ADD_DIMENSION_FAST:
arithmetic_add_dimension(p, operator_string, operator);
break;
case ARITHMETIC_OP_REPEAT:
arithmetic_repeat(p, operator_string, operator);
break;
default:
error(EXIT_FAILURE, 0, "%s: a bug! please contact us at "
"%s to fix the problem. The code %d is not "
"recognized for 'op'", __func__, PACKAGE_BUGREPORT,
operator);
}
}
}
/* Used by the 'set-' operator. */
static int
arithmetic_set_name_used_later(void *in, char *name)
{
struct gal_arithmetic_set_params *p=(struct gal_arithmetic_set_params *)in;
gal_list_str_t *token, *tokens = (gal_list_str_t *)(p->tokens);
size_t counter=0;
/* If the name exists after the current token, then return 1. */
for(token=tokens;token!=NULL;token=token->next)
if( counter++ > p->tokencounter && !strcmp(token->v, name) )
return 1;
/* If we get to this point, it means that the name doesn't exist. */
return 0;
}
static void
arithmetic_final_read_file(struct arithmeticparams *p,
struct operand *operand)
{
gal_data_t *out=NULL;
char *hdu, *filename;
/* Read the desired image and report it if necessary. */
hdu=operand->hdu;
filename=operand->filename;
if( gal_fits_file_recognized(filename) )
{
/* Read the data, note that the WCS has already been set. */
out=gal_array_read_one_ch(filename, hdu, NULL,
p->cp.minmapsize,
p->cp.quietmmap, "--hdu");
out->ndim=gal_dimension_remove_extra(out->ndim, out->dsize,
NULL);
if(!p->cp.quiet) printf(" - %s (hdu %s) is read.\n", filename, hdu);
}
else
error(EXIT_FAILURE, 0, "%s: a bug! please contact us at %s to fix "
"the problem. While 'operands->data' is NULL, the filename "
"('%s') is not recognized as a FITS file", __func__,
PACKAGE_BUGREPORT, filename);
/* Keep the read dataset (note that we don't need to free 'filename' or
'hdu' since their pointers are simply copied into the operand, they
are not copied.*/
operand->data=out;
}
/* Extract all the datasets of the remaining operands. */
static gal_data_t *
arithmetic_final_data(struct arithmeticparams *p)
{
struct operand *op;
gal_data_t *out=NULL;
/* Go over the operands, extract their dataset and add it to the list. */
for(op=p->operands; op!=NULL; op=op->next)
{
gal_list_data_add(&out, op->data);
out->wcs=gal_wcs_copy(p->refdata.wcs);
}
/* Reverse the list so it goes in the same order as the operands, then
return it. */
gal_list_data_reverse(&out);
return out;
}
/* Add any metadata in case the user wanted to do so. We are treating each
metadata entry separately because the user may not want to specify them
all for all outputs and the looping is not computationally expensive.*/
static void
arithmetic_out_metadata(struct arithmeticparams *p, gal_data_t *data)
{
gal_data_t *d;
gal_list_str_t *s;
/* Loop over 'metaname' and add each item to the output dataset(s). */
d=data;
for(s=p->metaname; s!=NULL && d!=NULL; s=s->next, d=d->next)
{
if(d->name) free(d->name);
gal_checkset_allocate_copy(s->v, &d->name);
}
/* Similar to 'metaname' but for meta units. */
d=data;
for(s=p->metaunit; s!=NULL && d!=NULL; s=s->next, d=d->next)
{
if(d->unit) free(d->unit);
gal_checkset_allocate_copy(s->v, &d->unit);
}
/* Similar to 'metacomment' but for meta comments. */
d=data;
for(s=p->metacomment; s!=NULL && d!=NULL; s=s->next, d=d->next)
{
if(d->comment) free(d->comment);
gal_checkset_allocate_copy(s->v, &d->comment);
}
}
/* This function implements the reverse polish algorithm as explained
in the Wikipedia page.
NOTE that in ui.c, the input linked list of tokens was ordered to
have the same order as what the user provided. */
void
reversepolish(struct arithmeticparams *p)
{
char *printnum=NULL;
struct operand *otmp;
size_t num_operands=0;
gal_list_str_t *token;
gal_data_t *tmp, *data, *col;
struct gal_options_common_params *cp=&p->cp;
int inlib, operator=GAL_ARITHMETIC_OP_INVALID;
/* Prepare the processing: */
p->popcounter=0;
p->operands=NULL;
p->setprm.params=p;
p->setprm.tokencounter=0;
p->setprm.tokens=p->tokens;
p->setprm.pop=operands_pop_wrapper_set;
p->setprm.used_later=arithmetic_set_name_used_later;
/* Go over each input token and do the work. */
for(token=p->tokens;token!=NULL;token=token->next)
{
/* The 'tofile-' operator's string can end in a '.fits', similar to a
FITS file input file. So, it needs to be checked before checking
for a filename. If we have a name or number, then add it to the
operands linked list. Otherwise, pull out two members and do the
specified operation on them. */
operator=GAL_ARITHMETIC_OP_INVALID;
if( !strncmp(OPERATOR_PREFIX_TOFILE, token->v,
OPERATOR_PREFIX_LENGTH_TOFILE) )
arithmetic_tofile(p, token->v, 0);
else if( !strncmp(OPERATOR_PREFIX_TOFILEFREE, token->v,
OPERATOR_PREFIX_LENGTH_TOFILEFREE) )
arithmetic_tofile(p, token->v, 1);
else if( !strncmp(token->v, GAL_ARITHMETIC_SET_PREFIX,
GAL_ARITHMETIC_SET_PREFIX_LENGTH) )
gal_arithmetic_set_name(&p->setprm, token->v);
else if( (col=gal_arithmetic_load_col(token->v, cp->searchin,
cp->ignorecase,
cp->minmapsize,
cp->quietmmap))!=NULL )
operands_add(p, NULL, col);
else if( gal_array_file_recognized(token->v)
|| gal_arithmetic_set_is_name(p->setprm.named, token->v) )
operands_add(p, token->v, NULL);
else if( (data=gal_data_copy_string_to_number(token->v)) )
{
/* The 'minmapsize' and 'quietmmap' parameters should be passed
onto the library within numbers also (since they are the
only things that go in the library sometimes). */
data->quietmmap=p->cp.quietmmap;
data->minmapsize=p->cp.minmapsize;
operands_add(p, NULL, data);
}
/* Last option is an operator: the program will abort if the token
isn't an operator. */
else
{
operator=arithmetic_set_operator(token->v, &num_operands,
&inlib);
arithmetic_operator_run(p, operator, token->v, num_operands,
inlib);
}
/* Increment the token counter. */
++p->setprm.tokencounter;
}
/* If there aren't any more operands (a variable has been set but not
used), then there is nothing to create. */
if(p->operands==NULL)
error(EXIT_FAILURE, 0, "no operands on the stack to write (as output)");
/* If there is more than one node in the operands stack then the user has
given too many operands which is an error. */
if(p->writeall==0 && p->operands->next!=NULL)
error(EXIT_FAILURE, 0, "too many operands");
/* If the final operand has a filename, but its 'data' element is NULL,
then the file hasn't actually be read yet. In this case, we need to
read the contents of the file and put the resulting dataset into the
operands 'data' element. This can happen for example if no operators
are called and there is only one filename as an argument (which can
happen in scripts).*/
for(otmp=p->operands; otmp!=NULL; otmp=otmp->next)
if(otmp->data==NULL && otmp->filename)
arithmetic_final_read_file(p, otmp);
/* If the final data structure has more than one element, write it as a
FITS file. Otherwise, if the user didn't call '--output', print it in
the standard output. */
data=arithmetic_final_data(p);
if(data->next==NULL && data->size==1 && data->ndim==1
&& p->outnamerequested==0)
{
/* Make the string to print the number. */
printnum=gal_type_to_string(data->array, data->type, 0);
printf("%s\n", printnum);
/* Clean up (don't set it to 'NULL', this is used to specify if a
file was created in the end or not). */
free(printnum);
}
else
{
/* If the user has requested a name, units or comments for the FITS
file, insert them into the dataset here. */
arithmetic_out_metadata(p, data);
/* Put a copy of the WCS structure from the reference image, it will
be freed while freeing 'data'. But start the file with the 0-th
HDU keywords.*/
if(data->ndim==1 && p->onedasimage==0)
{
/* The keywords HDU should only be created when a FITS file is to
be made; not when the output should go to a text file or the
standard output. */
if( p->onedonstdout==0
&& gal_fits_name_is_fits(p->cp.output)==0 )
gal_fits_key_write(p->cp.ckeys, p->cp.output, "0", "NONE",
1, 1);
gal_table_write(data, NULL, NULL, p->cp.tableformat,
p->onedonstdout ? NULL : p->cp.output,
"ARITHMETIC", 0, 0);
}
else
{
if(p->append==0) /* 0-HDU metadata only when not appending. */
gal_fits_key_write(p->cp.ckeys, p->cp.output, "0",
"NONE", 1, 1);
for(tmp=data; tmp!=NULL; tmp=tmp->next)
gal_fits_img_write(tmp, p->cp.output, NULL, 0);
}
/* Let the user know that the job is done. */
if(!p->cp.quiet)
printf(" - Write (final): %s\n", p->cp.output);
}
/* Clean up, note that above, we copied the pointer to 'refdata->wcs'
into 'data', so it is freed when freeing 'data'. */
gal_data_free(data);
free(p->refdata.dsize);
gal_list_data_free(p->setprm.named);
/* Clean up. Note that the tokens were taken from the command-line
arguments, so the string within each token linked list must not be
freed. */
gal_list_str_free(p->tokens, 0);
free(p->operands);
}
/***************************************************************/
/************* Top-level function *************/
/***************************************************************/
void
arithmetic(struct arithmeticparams *p)
{
/* Parse the arguments */
reversepolish(p);
}
|