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
|
/* File getcol.c
* January 22, 2020
* By Jessica Mink, Harvard-Smithsonian Center for Astrophysics
* Send bug reports to jmink@cfa.harvard.edu
Copyright (C) 1999-2020
Smithsonian Astrophysical Observatory, Cambridge, MA USA
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
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <math.h>
#include "libwcs/wcs.h"
#include "libwcs/fitsfile.h"
#include "libwcs/wcscat.h"
#define MAX_LTOK 256
#define MAX_NTOK 1024
#define MAXFILES 2000
#define MAXLINES 500000
static char *RevMsg = "GETCOL WCSTools 3.9.7, 26 April 2022, Jessica Mink (jmink@cfa.harvard.edu)";
static void usage();
static int ListFile();
static char *iscolop();
static int iscol();
static double median();
static double stdev();
static int maxnfile = MAXFILES;
static int maxq = MAXFILES;
static double badval = 0.0; /* Value to ignore */
static int isbadval = 0; /* 1 if badval is set */
static int maxlfn = 0;
static int maxncond = 100;
static int maxnop = 100;
static int listpath = 0; /* 1 to list pathnames */
static int verbose = 0; /* Verbose/debugging flag */
static int sumcol = 0; /* True to sum column values */
static int ameancol = 0; /* True for absolute mean of column values */
static int meancol = 0; /* True to compute mean of column values */
static int adevcol = 0; /* True for stdev of absolute mean of column values */
static int devcol = 0; /* True for stdev of mean of column values */
static int amedcol = 0; /* True for absolute median of column values */
static int medcol = 0; /* True to compute median of column values */
static int countcol = 0; /* True to count entries in columns */
static int rangecol = 0; /* True to print range of column values */
static int version = 0; /* If 1, print only program name and version */
static int nread = 0; /* Number of lines to read (0=all) */
static int nskip = 0; /* Number of lines to skip */
static int tabout = 0; /* If 1, separate output fields with tabs */
static int linout = 0; /* If 1, put each output field on a new line */
static int counttok = 0; /* If 1, print number of columns on line */
static int printhead = 0; /* If 1, print Starbase tab table header */
static int intcompare();
static int ncond=0; /* Number of keyword conditions to check */
static int condand=1; /* If 1, AND comparisons, else OR */
static char **cond; /* Conditions to check */
static char **ccond; /* Condition characters */
static int nop=0; /* Number of keyword values to operate on */
static char **cop; /* First column to operate on */
static int *lc; /* Length of column 1 */
static char **op; /* Operator and second column to operate on */
static int napp=0; /* Number of lines to append */
static int ndec=-1; /* Number of decimal places in f.p. output */
static int printcol = 1; /* Flag to print extracted data */
static char *cwhite;
static int *frstchar; /* First character of column to use (1-n) */
static int *lastchar; /* Last character of column to use (1-n) */
static int qmeancol = 0; /* If 1, print mean of columns added in quadrature */
/* 0 = no mean of quadruature */
static char **format;
int
main (ac, av)
int ac;
char **av;
{
char *str;
char *temp;
char **fn;
int nfile = 0;
int ifile;
int nbytes, lfn;
char *filename;
char *ranges = NULL;
char *lfile = NULL;
char *lranges = NULL;
char *cdot, *ccol;
char *opi;
int icol;
int match, newbytes;
int nrbytes = 0;
int maxnop0;
int i;
char **op1; /* Operator */
char **cop1; /* Operation character */
int *lc1; /* Length of column 1 */
char sop1[16], sop2[16];
cwhite = NULL;
fn = (char **)calloc (maxnfile, sizeof(char *));
if (ac == 1)
usage ();
cond = (char **)calloc (maxncond, sizeof(char *));
ccond = (char **)calloc (maxncond, sizeof(char *));
op = (char **)calloc (maxnop, sizeof(char *));
lc = (int *)calloc (maxnop, sizeof(int *));
cop = (char **)calloc (maxnop, sizeof(char));
frstchar = (int *) calloc (MAX_NTOK, sizeof (int));
lastchar = (int *) calloc (MAX_NTOK, sizeof (int));
format = (char **)calloc (MAX_NTOK, sizeof (char *));
/* Check for help or version command first */
str = *(av+1);
if (!str || !strcmp (str, "help") || !strcmp (str, "-help"))
usage ();
if (!strcmp (str, "version") || !strcmp (str, "-version")) {
version = 1;
usage ();
}
/* crack arguments */
for (av++; --ac > 0; av++) {
/* Set column number */
if (iscol (*av)) {
cdot = strchr (*av,'.');
if (cdot) {
ccol = strchr (*av, ':');
if (ccol) {
*cdot = (char) 0;
icol = atoi (*av);
*ccol = (char) 0;
if (icol > 0 && icol < MAX_NTOK) {
if (strlen (cdot+1) > 0)
frstchar[icol] = atoi (cdot+1);
else
frstchar[icol] = 1;
if (strlen (ccol+1) > 0)
lastchar[icol] = atoi (ccol+1);
else
lastchar[icol] = 0;
}
}
else
match = 1;
}
if (ranges) {
newbytes = strlen(ranges) + strlen(*av) + 2;
newbytes = ((newbytes / 16) + 1) * 16;
if (newbytes > nrbytes) {
temp = ranges;
ranges = (char *)calloc (newbytes, 1);
strcpy (ranges, temp);
nrbytes = newbytes;
free (temp);
}
strcat (ranges, ",");
strcat (ranges, *av);
}
else {
nrbytes = strlen(*av) + 2;
nrbytes = ((nrbytes / 16) + 1) * 16;
ranges = (char *) calloc (nrbytes, 1);
strcpy (ranges, *av);
}
icol = atoi (*av);
}
/* Set range and make a list of column numbers from it */
else if (isrange (*av)) {
if (ranges) {
newbytes = strlen(ranges) + strlen(*av) + 2;
newbytes = ((newbytes / 16) + 1) * 16;
if (newbytes > nrbytes) {
temp = ranges;
ranges = (char *) calloc (newbytes, 1);
strcpy (ranges, temp);
nrbytes = newbytes;
free (temp);
}
strcat (ranges, ",");
strcat (ranges, *av);
}
else {
ranges = (char *) calloc (strlen(*av) + 2, 1);
if (strchr (*av,'.'))
match = 1;
strcpy (ranges, *av);
}
}
/* Negative numbers aren't ranges, but positive ones are */
else if (isnum (*av) == 1) {
if (ranges) {
newbytes = strlen(ranges) + strlen(*av) + 2;
newbytes = ((newbytes / 16) + 1) * 16;
if (newbytes > nrbytes) {
temp = ranges;
ranges = (char *) calloc (newbytes, 1);
strcpy (ranges, temp);
nrbytes = newbytes;
free (temp);
}
strcat (ranges, ",");
strcat (ranges, *av);
}
else {
ranges = (char *) calloc (strlen(*av) + 2, 1);
if (strchr (*av,'.'))
match = 1;
strcpy (ranges, *av);
}
}
/* Read format string */
else if (*(str = *av) == '%') {
if (icol > 0) {
format[icol] = (char *) calloc (1+strlen(*av), 1);
strcpy (format[icol], *av);
}
}
/* Read and save file name */
else if (isfile (*av)) {
if (nfile >= maxnfile) {
maxnfile = maxnfile * 2;
nbytes = maxnfile * sizeof (char *);
fn = (char **) realloc ((void *)fn, nbytes);
}
fn[nfile] = *av;
if (listpath || (filename = strrchr (fn[nfile],'/')) == NULL)
filename = fn[nfile];
else
filename = filename + 1;
lfn = strlen (filename);
if (lfn > maxlfn)
maxlfn = lfn;
nfile++;
}
/* Condition */
else if (strchr (*av, '=') != NULL || strchr (*av, '#') != NULL ||
strchr (*av, '>') != NULL || strchr (*av, '<') != NULL ) {
if (ncond >= maxncond) {
maxncond = maxncond * 2;
cond = (char **)realloc((void *)cond, maxncond*sizeof(void *));
ccond = (char **)realloc((void *)ccond, maxncond*sizeof(void *));
}
cond[ncond] = *av;
ccond[ncond] = strchr (*av, '=');
if (ccond[ncond] == NULL)
ccond[ncond] = strchr (*av, '#');
if (ccond[ncond] == NULL)
ccond[ncond] = strchr (*av, '>');
if (ccond[ncond] == NULL)
ccond[ncond] = strchr (*av, '<');
ncond++;
}
/* Otherwise, read command */
else if (*(str = *av) == '-') {
char c;
while ((c = *++str))
switch (c) {
case 'a': /* Sum values in each numeric column */
sumcol++;
break;
case 'b': /* bar-separated input */
cwhite = (char *) calloc (4,1);
strcpy (cwhite, "bar");
break;
case 'c': /* Count entries in each column */
countcol++;
break;
case 'd': /* Number of decimal places in f.p. output */
if (ac < 2)
usage ();
ndec = atoi (*++av);
ac--;
break;
case 'e': /* Compute median of each numeric column */
medcol++;
break;
case 'f': /* Range of values in column */
rangecol++;
break;
case 'g': /* Compute absolute median of each numeric column */
amedcol++;
break;
case 'h': /* Print Starbase tab table header */
printhead++;
break;
case 'i': /* tab-separated input */
cwhite = (char *) calloc (4,1);
strcpy (cwhite, "tab");
break;
case 'j': /* Compute absolute mean of each numeric column */
ameancol++;
break;
case 'k': /* Count columns on first line */
counttok++;
break;
case 'l': /* Number of lines to append */
if (ac < 2)
usage ();
napp = atoi (*++av);
ac--;
break;
case 'm': /* Compute mean of each numeric column */
meancol++;
break;
case 'n': /* Number of lines to read */
if (ac < 2)
usage ();
nread = atoi (*++av);
ac--;
break;
case 'o': /* OR conditions insted of ANDing them */
condand = 0;
break;
case 'p': /* Print sum or mean only */
printcol = 0;
break;
case 'q': /* Compute mean in quadrature of selected numeric columns */
qmeancol = 1;
break;
case 'r': /* Range of lines to read */
if (ac < 2)
usage ();
if (*(av+1)[0] == '@') {
lfile = *++av + 1;
ac--;
}
else if (isrange (*(av+1)) || isnum (*(av+1)) == 1) {
lranges = (char *) calloc (strlen(*av) + 1, 1);
strcpy (lranges, *++av);
ac--;
}
break;
case 's': /* Number of lines to skip */
if (ac < 2)
usage ();
nskip = atoi (*++av);
ac--;
break;
case 't': /* Tab-separated output */
tabout++;
break;
case 'u': /* Line-separated output */
linout++;
break;
case 'v': /* More verbosity */
verbose++;
break;
case 'w': /* Print file pathnames */
listpath++;
break;
case 'x': /* Value to ignore */
if (ac < 2)
usage ();
badval = atof (*++av);
isbadval++;
ac--;
break;
case 'y': /* Compute standard deviation of means of selected columns */
devcol++;
break;
case 'z': /* Compute standard deviation of absolute means of selected columns */
adevcol++;
break;
default:
usage ();
break;
}
}
/* Operation */
else if ((opi = iscolop (*av)) != NULL) {
if (nop >= maxnop) {
maxnop0 = maxnop;
maxnop = maxnop0 * 2;
op1 = (char **)calloc (maxnop, sizeof (char *));
cop1 = (char **)calloc (maxnop, sizeof (char));
lc1 = (int *) calloc (maxnop, sizeof (int));
for (i = 0; i < maxnop0; i++) {
op1[i] = op[i];
cop1[i] = cop[i];
lc1[i] = lc[i];
}
free (op);
free (cop);
free (lc);
op = op1;
cop = cop1;
lc = lc1;
op1 = NULL;
cop1 = NULL;
lc1 = NULL;
}
op[nop] = opi;
cop[nop] = *av;
lc[nop] = (int) (op[nop] - cop[nop]);
for (i = 0; i < 16; i++)
sop1[i] = (char) 0;
for (i = 0; i < 16; i++)
sop2[i] = (char) 0;
strncpy (sop1, cop[nop], lc[nop]);
strcpy (sop2, op[nop]+1);
if (isnum (sop1) == 1 && isnum (sop2) == 1) {
if (verbose)
printf (" Column %s %c %s\n", sop1, opi[0], sop2);
nop++;
}
else
printf (" Constants %s, %s, or operation %c illegal\n",sop1,sop2,opi[0]);
}
/* File to read */
else
usage();
}
if (nfile <= 0) {
fprintf (stderr, "GETCOL: no files specified\n");
exit (1);
}
for (ifile = 0; ifile < nfile; ifile++)
(void)ListFile (fn[ifile], ranges, lranges, lfile);
free (lranges);
free (ranges);
return (0);
}
static void
usage ()
{
fprintf (stderr,"%s\n", RevMsg);
if (version)
exit (-1);
fprintf (stderr,"Extract specified columns from an ASCII table file\n");
fprintf (stderr,"Usage: [-abcefghijkmopqtv][-d num][-l num][-n num][-r lines][-s num] filename [col] [cond] ...\n");
fprintf(stderr," col: Number range (n1-n2,n3-n4...) or col.c1:c2\n");
fprintf(stderr," col: Combination of columns n1[+_*/asmd][n2 or constant with .]\n");
fprintf(stderr," cond: Condition for which to list [n1][=#><][n2 or constant with .]\n");
fprintf(stderr," %%f.dx: C output format for last column specified\n");
fprintf(stderr," -a: Sum selected numeric column(s)\n");
fprintf(stderr," -b: Input columns are delimited by vertical bars\n");
fprintf(stderr," -c: Add count of number of lines in each column at end\n");
fprintf(stderr," -d num: Number of decimal places in f.p. output\n");
fprintf(stderr," -e: Median values of selected numeric column(s)\n");
fprintf(stderr," -f: Print range of values in selected column(s)\n");
fprintf(stderr," -g: Median absolute values of selected column(s)\n");
fprintf(stderr," -h: Print Starbase tab table header and # comments\n");
fprintf(stderr," -i: Input is tab-separate table file\n");
fprintf(stderr," -j: Print means of absolute values of selected column(s)\n");
fprintf(stderr," -k: Print number of columns on first line\n");
fprintf(stderr," -l num: Number of lines to add to each line\n");
fprintf(stderr," -m: Print means of selected numeric column(s)\n");
fprintf(stderr," -n num: Number of lines to read (default %d)\n", MAXLINES);
fprintf(stderr," -o: OR conditions insted of ANDing them\n");
fprintf(stderr," -p: Print only sum or mean, not individual values\n");
fprintf(stderr," -q: Compute mean of selected columns added in quadrature\n");
fprintf(stderr," -r: Range or @file of lines to read, if not all\n");
fprintf(stderr," -s: Number of lines to skip\n");
fprintf(stderr," -t: Starbase tab table output\n");
fprintf(stderr," -u: Each field on a new line\n");
fprintf(stderr," -v: Verbose\n");
fprintf(stderr," -w: Print file pathnames\n");
fprintf(stderr," -x num: Set value to ignore\n");
fprintf(stderr," -y: Print stdev of means of selected numeric column(s)\n");
fprintf(stderr," -z: Print stdev of means of absolute values of selected column(s)\n");
exit (1);
}
static int
ListFile (filename, ranges, lranges, lfile)
char *filename; /* File name */
char *ranges; /* String with range of column numbers to list */
char *lranges; /* String with range of lines to list */
char *lfile; /* Name of file with lines to list */
{
int i, j, il, ir, nbytes, ncol;
char line[4096];
char *nextline;
char *lastchar;
FILE *fd = NULL;
FILE *lfd = NULL;
struct Tokens tokens; /* Token structure */
struct Range *range = NULL;
struct Range *lrange;
int *iline;
int nline = 0;
int idnum;
int iln;
int nfdef = 9;
double *sum;
double *asum = NULL;
double *colmin, *colmax, **med;
double **amed = NULL;
double *qmed = NULL;
double *qmed1 = NULL;
double qsum = 0.0;
double qsum1;
int ncstr; /* Argument isnum() value */
int ncstr0; /* Original argument isnum() value */
int ncval; /* Column isnum() value */
int ncval0; /* Column isnum() before any conversion */
int *nsum;
int *nent;
int *hms; /* Flag for hh:mm:ss or dd:mm:ss format */
int *limset; /* Flag for range initialization */
int nlmax;
double dtok, dnum;
int nfind = 0;
int ntok, nt, ltok,iop, ndtok, ndnum, nd;
int *inum;
int icond, itok;
char tcond, *cstr, *cval, top;
char colstr[16];
char numstr[32], numstr1[32];
double dcond, dval, dmean;
int pass;
int nchar, k;
int iapp;
int jcond, jval;
char token[MAX_LTOK];
char token1[MAX_LTOK];
int lform;
char *iform;
char cform;
int nq, nqsum = 0;
nent = NULL;
sum = NULL;
inum = NULL;
nsum = NULL;
colmin = NULL;
colmax = NULL;
lrange = NULL;
iline = NULL;
hms = NULL;
med = NULL;
limset = NULL;
if (verbose)
printf ("\n%s\n", RevMsg);
/* if (listpath)
printf ("%s ", filename); */
if (nread < 1)
nread = MAXLINES;
/* Make list of line numbers to read from list or range on command line */
if (lranges != NULL) {
lrange = RangeInit (lranges, nfdef);
nline = rgetn (lrange);
nbytes = nline * sizeof (int);
if (!(iline = (int *) calloc (nline+1, sizeof(int))) ) {
fprintf (stderr, "Could not calloc %d bytes for iline\n", nbytes);
return (-1);
}
for (i = 0; i < nline; i++)
iline[i] = rgeti4 (lrange);
qsort (iline, nline, sizeof(int), intcompare);
}
/* Make list of line numbers to read from file specified on command line */
if (lfile != NULL) {
if (!(lfd = fopen (lfile, "r")))
return (-1);
nlmax = 99;
nline = 0;
nbytes = nlmax * sizeof(int);
if (!(iline = (int *) calloc (nlmax, sizeof(int))) ) {
fprintf (stderr, "Could not calloc %d bytes for iline\n", nbytes);
fclose (lfd);
return (-1);
}
il = 0;
nextline = line;
for (ir = 0; ir < nread; ir++) {
if (fgets (nextline, 1023, lfd) == NULL)
break;
/* Skip lines with comments */
if (nextline[0] == '#') {
if (!printhead) {
while (line[0] == '#') {
if (fgets (nextline, 1023, lfd) == NULL)
break;
}
}
else
continue;
}
/* Drop linefeeds */
lastchar = nextline + strlen(nextline) - 1;
if (*lastchar < 32)
*lastchar = (char) 0;
/* Add lines with escaped linefeeds */
lastchar = nextline + strlen(nextline) - 1;
if (*lastchar == (char) 92) {
nextline = lastchar;
continue;
}
else
nextline = line;
ntok = setoken (&tokens, line, cwhite);
nt = 0;
il++;
if (il > nlmax) {
nlmax = nlmax + 100;
nbytes = nlmax * sizeof(int);
if (!(iline = (int *) realloc ((void *) iline, nbytes))) {
fprintf (stderr, "Could not realloc %d bytes for iline\n",
nbytes);
fclose (lfd);
return (-1);
}
}
for (i = 0; i < ntok; i++) {
if (getoken (&tokens, i+1, token, MAX_LTOK)) {
iline[il] = atoi (token);
if (iline[il] > 0) {
nline++;
break;
}
}
}
}
fclose (lfd);
qsort (iline, nline, sizeof(int), intcompare);
}
/* Open input file */
if (!strcmp (filename, "stdin"))
fd = stdin;
else if (!(fd = fopen (filename, "r"))) {
if (verbose)
fprintf (stderr, "*** Cannot read file %s\n", filename);
return (-1);
}
/* Skip lines into input file */
if (nskip > 0) {
for (i = 0; i < nskip; i++) {
if (fgets (line, 1023, fd) == NULL)
break;
if (!printhead) {
while (line[0] == '#') {
if (fgets (line, 1023, fd) == NULL)
break;
}
}
}
}
/* Print entire selected lines */
if (ranges == NULL && nop == 0) {
iln = 0;
il = 0;
iapp = 0;
nextline = line;
for (ir = 0; ir < nread; ir++) {
if (fgets (nextline, 1023, fd) == NULL)
break;
/* Skip lines with comments */
if (nextline[0] == '#') {
if (!printhead) {
while (nextline[0] == '#') {
if (fgets (nextline, 1023, lfd) == NULL)
break;
}
}
else
continue;
}
/* Add lines with escaped linefeeds */
lastchar = nextline + strlen(nextline) - 1;
if (*lastchar == (char) 92) {
nextline = lastchar;
continue;
}
else if (iapp++ < napp) {
*lastchar = ' ';
nextline = lastchar + 1;
continue;
}
else {
iapp = 0;
nextline = line;
}
il++;
/* Skip if line is not on list, if there is one */
if (iline != NULL) {
if (il+1 < iline[iln])
continue;
else if (il+1 > iline[nline-1])
break;
else
iln++;
}
/* Drop control character at end of string */
lastchar = line + strlen(line) - 1;
if (*lastchar < 32)
*lastchar = (char) 0;
/* Drop second control character at end of string */
lastchar = line + strlen(line) - 1;
if (*lastchar < 32)
*lastchar = (char) 0;
/* Echo line if it is a comment */
if (line[0] == '#' && printhead) {
printf ("%s\n", line);
continue;
}
/* Check conditions */
ntok = setoken (&tokens, line, cwhite);
if (counttok) {
printf ("%d", ntok-1);
if (verbose)
printf (" columns in %s", filename);
else
printf ("\n");
return (0);
}
pass = 0;
if (ncond > 0) {
for (icond = 0; icond < ncond; icond++) {
if (condand)
pass = 0;
/* Extract test value from comparison string */
tcond = *ccond[icond];
cstr = ccond[icond]+1;
ncstr0 = isnum (cstr);
/* If test value is sexigesimal, convert it to decimal */
if (ncstr0 == 3) {
dnum = str2dec (cstr);
num2str (numstr, dnum, 0, 7);
cstr = numstr;
}
/* If test value is integer, use value for that column */
else if (ncstr0 == 1) {
itok = atoi (cstr);
getoken (&tokens, itok, token1, MAX_LTOK);
cstr = token1;
}
strfix (cstr, 0, 1);
ncstr = isnum (cstr);
/* Read comparison value from header */
itok = atoi (cond[icond]);
getoken (&tokens, itok, token, MAX_LTOK);
cval = token;
ncval0 = isnum (cval);
/* If column value is sexigesimal, convert it to decimal */
if (ncval0 == 3) {
dnum = str2dec (cval);
num2str (numstr1, dnum, 0, 7);
cval = numstr1;
}
strfix (cval, 0, 1);
ncval = isnum (cval);
/* Compare floating point numbers */
if (ncstr == 2 && ncval) {
dcond = atof (cstr);
dval = atof (cval);
if (tcond == '=' && dval == dcond)
pass = 1;
if (tcond == '#' && dval != dcond)
pass = 1;
if (tcond == '>' && dval > dcond)
pass = 1;
if (tcond == '<' && dval < dcond)
pass = 1;
}
/* Compare integers */
else if (ncstr == 1 && ncval) {
jcond = (int) atof (cstr);
jval = (int) atof (cval);
if (tcond == '=' && jval == jcond)
pass = 1;
if (tcond == '#' && jval != jcond)
pass = 1;
if (tcond == '>' && jval > jcond)
pass = 1;
if (tcond == '<' && jval < jcond)
pass = 1;
}
/* Check number type */
else if (cstr[0] == 'n' && ncval0) {
ncstr = atoi (cstr+1);
if (ncval0 == ncstr)
pass = 1;
}
/* Compare strings (only equal or not equal */
else {
if (tcond == '=' && !strcmp (cstr, cval))
pass = 1;
if (tcond == '#' && strcmp (cstr, cval))
pass = 1;
}
if (condand && !pass)
break;
}
if (pass) {
printf ("%s", line);
if (listpath)
printf ("%s", filename);
printf ("\n");
}
}
else {
printf ("%s", line);
if (listpath)
printf ("%s", filename);
printf ("\n");
}
}
}
/* Find columns specified by number */
else {
if (ranges || nop > 0) {
if (ranges) {
range = RangeInit (ranges, nfdef);
nfind = rgetn (range);
}
else
nfind = 0;
ncol = nfind + nop;
nbytes = ncol * sizeof (double);
if (!(sum = (double *) calloc (ncol, sizeof (double))) ) {
fprintf (stderr, "Could not calloc %d bytes for sum\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(asum = (double *) calloc (ncol, sizeof (double))) ) {
fprintf (stderr, "Could not calloc %d bytes for asum\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(colmin = (double *) calloc (ncol, sizeof (double))) ) {
fprintf (stderr, "Could not calloc %d bytes for colmin\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(colmax = (double *) calloc (ncol, sizeof (double))) ) {
fprintf (stderr, "Could not calloc %d bytes for colmax\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(med = (double **) calloc (ncol, sizeof (double *))) ) {
fprintf (stderr, "Could not calloc %d bytes for med\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(amed = (double **) calloc (ncol, sizeof (double *))) ) {
fprintf (stderr, "Could not calloc %d bytes for amed\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
else {
nbytes = maxq * sizeof (double);
if (!(qmed = calloc (maxq, sizeof(double)))) {
fprintf (stderr, "Could not calloc %d bytes for qmed\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
for (i = 0; i < ncol; i++) {
if (!(med[i] = calloc (nread, sizeof(double)))) {
fprintf (stderr, "Could not calloc %d bytes for med%d\n",
nbytes, i);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(amed[i] = calloc (nread, sizeof(double)))) {
fprintf (stderr, "Could not calloc %d bytes for amed%d\n",
nbytes, i);
if (fd != stdin) fclose (fd);
return (-1);
}
}
}
nbytes = ncol * sizeof (int);
if (!(nsum = (int *) calloc (ncol, sizeof(int))) ) {
fprintf (stderr, "Could not calloc %d bytes for nsum\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(hms = (int *) calloc (ncol, sizeof(int))) ) {
fprintf (stderr, "Could not calloc %d bytes for hms\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(limset = (int *) calloc (ncol, sizeof(int))) ) {
fprintf (stderr, "Could not calloc %d bytes for limset\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(nent = (int *) calloc (ncol, sizeof(int))) ) {
fprintf (stderr, "Could not calloc %d bytes for nent\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
if (!(inum = (int *) calloc (ncol, sizeof(int))) ) {
fprintf (stderr, "Could not calloc %d bytes for inum\n", nbytes);
if (fd != stdin) fclose (fd);
return (-1);
}
else {
for (i = 0; i < nfind; i++)
inum[i] = rgeti4 (range);
}
}
iln = 0;
iapp = 0;
nextline = line;
for (il = 0; il < nread; il++) {
if (fgets (nextline, 1023, fd) == NULL)
break;
/* Ignore line if it is a comment and printhead flag is not set */
if (line[0] == '#' && !printhead) {
while (line[0] == '#') {
if (fgets (nextline, 1023, fd) == NULL)
break;
}
}
/* Clear control character at end of string */
lastchar = line + strlen(line) - 1;
if (*lastchar < 32)
*lastchar = (char) 0;
/* Clear second control character at end of string */
lastchar = line + strlen(line) - 1;
if (*lastchar < 32)
*lastchar = (char) 0;
/* Add lines with escaped linefeeds */
lastchar = nextline + strlen(nextline) - 1;
if (*lastchar == (char) 92) {
nextline = lastchar;
continue;
}
else if (iapp++ < napp) {
*++lastchar = ' ';
nextline = lastchar + 1;
continue;
}
else
nextline = line;
iapp = 0;
/* Skip if line is not on list, if there is one */
if (iline != NULL) {
if (il+1 < iline[iln])
continue;
else if (il > iline[nline-1])
break;
else {
iln++;
if (iln > nline)
break;
}
}
/* Echo line if it is a comment */
if (line[0] == '#') {
if (printhead)
printf ("%s\n", line);
continue;
}
ntok = setoken (&tokens, line, cwhite);
if (counttok) {
printf ("%d", ntok);
if (verbose)
printf (" columns in %s", filename);
else
printf ("\n");
return (0);
}
/* Check conditions */
pass = 0;
if (ncond > 0) {
for (icond = 0; icond < ncond; icond++) {
if (condand)
pass = 0;
/* Extract test value from comparison string */
tcond = *ccond[icond];
cstr = ccond[icond]+1;
ncstr0 = isnum (cstr);
/* Convert sexigesimal test value to decimal */
if (ncstr0 == 3) {
dnum = str2dec (cstr);
num2str (numstr, dnum, 0, 7);
cstr = numstr;
}
/* Get column value if integer test value */
else if (ncstr0 == 1 && atoi (cstr) != 0) {
itok = atoi (cstr);
getoken (&tokens, itok, token1, MAX_LTOK);
cstr = token1;
ncstr = isnum (cstr);
/* Convert sexigesimal column value to decimal */
if (ncstr == 3) {
dnum = str2dec (cstr);
num2str (numstr, dnum, 0, 7);
cstr = numstr;
}
}
strfix (cstr, 0, 1);
ncstr = isnum (cstr);
/* Read value to check against from line */
itok = atoi (cond[icond]);
getoken (&tokens, itok, token, MAX_LTOK);
cval = token;
ncval0 = isnum (cval);
/* Convert sexigesimal column value to decimal */
if (ncval0 == 3) {
dnum = str2dec (cval);
num2str (numstr, dnum, 0, 7);
cval = numstr;
}
strfix (cval, 0, 1);
ncval = isnum (cval);
/* Compare floating point numbers */
if (ncstr == 2 && ncval > 0 && ncval < 3) {
dcond = atof (cstr);
dval = atof (cval);
if (tcond == '=' && dval == dcond)
pass = 1;
if (tcond == '#' && dval != dcond)
pass = 1;
if (tcond == '>' && dval > dcond)
pass = 1;
if (tcond == '<' && dval < dcond)
pass = 1;
}
/* Compare integers */
else if (ncstr == 1 && ncval > 0 && ncval < 3) {
jcond = atoi (cstr);
jval = atoi (cval);
if (tcond == '=' && jval == jcond)
pass = 1;
if (tcond == '#' && jval != jcond)
pass = 1;
if (tcond == '>' && jval > jcond)
pass = 1;
if (tcond == '<' && jval < jcond)
pass = 1;
}
/* Check number type */
else if (cstr[0] == 'n' && ncval0) {
ncstr = atoi (cstr+1);
if (ncval0 == ncstr)
pass = 1;
}
/* Compare strings (only equal or not equal */
else {
if (tcond == '=' && !strcmp (cstr, cval))
pass = 1;
if (tcond == '#' && strcmp (cstr, cval))
pass = 1;
}
if (condand && !pass)
break;
}
if (!pass)
continue;
}
nt = 0;
if (il == 0 && printhead && tabout) {
/* Print conditions in header */
for (icond = 0; icond < ncond; icond++) {
if (verbose) {
if (condand || icond == 0)
printf ("%s\n",cond[icond]);
else
printf (" or %s\n",cond[icond]);
}
else if (tabout) {
if (condand || icond == 0)
printf ("condition %s\n", cond[icond]);
else
printf ("condition or %s\n", cond[icond]);
}
}
for (i = 0; i < nfind; i++) {
if (getoken (&tokens, inum[i], token, MAX_LTOK)) {
ltok = strlen (token);
printf ("%03d", inum[i]);
for (j = 3; j < ltok; j++)
printf (" ");
}
else
printf ("%03d", inum[i]);
printf (" ");
}
printf ("\n");
for (i = 0; i < nfind; i++) {
if (getoken (&tokens, inum[i], token, MAX_LTOK)) {
ltok = strlen (token);
for (j = 0; j < ltok; j++)
printf ("-");
}
else
printf ("---");
printf (" ");
}
printf ("\n");
}
/* Print requested columns */
qsum1 = 0;
nq = 0;
for (i = 0; i < nfind; i++) {
if (getoken (&tokens, inum[i], token, MAX_LTOK)) {
/* Get substring of column, if requested */
if (frstchar[i] > 0) {
ltok = strlen (token);
if (lastchar[i] > 0)
nchar = lastchar[i] - frstchar[i] + 1;
else
nchar = ltok - frstchar[i] + 1;
k = frstchar[i];
for (j = 0; j < nchar; j++)
token[j] = token[k++];
for (j = nchar; j < ltok; j++);
token[j] = (char) 0;
}
strfix (token, 0, 1);
if (isnum (token) > 0 && isnum (token) < 3) {
dval = atof (token);
hms[i] = 0;
idnum = 1;
}
else if (isnum (token) == 3) {
dval = str2dec (token);
hms[i] = 1;
idnum = 1;
}
else {
dval = 0.0;
idnum = 0;
}
if (idnum) {
if (!isbadval || (isbadval && dval != badval)) {
qsum1 = qsum1 + dval * dval;
nq++;
sum[i] = sum[i] + dval;
asum[i] = asum[i] + fabs (dval);
if (!limset[i]) {
colmin[i] = dval;
colmax[i] = dval;
limset[i]++;
}
else if (dval < colmin[i])
colmin[i] = dval;
else if (dval > colmax[i])
colmax[i] = dval;
med[i][nsum[i]] = dval;
amed[i][il] = fabs (dval);
nsum[i]++;
}
}
if (printcol) {
if (i > 0) {
if (tabout)
printf (" ");
else if (linout)
printf ("\n");
else
printf (" ");
}
iform = format[inum[i]];
if (inum[i] > tokens.ntok || inum[i] < 1)
printf ("___");
else if (iform) {
lform = strlen (iform);
cform = iform[lform-1];
if (cform == 'f' || cform == 'g')
printf (iform, atof(token));
else if (cform == 'd')
printf (iform, atoi(token));
else
printf (iform, token);
}
else if (ndec > -1 && isnum (token) == 2) {
num2str (numstr, atof (token), 0, ndec);
printf ("%s", numstr);
}
else
printf ("%s", token);
}
nt++;
nent[i]++;
}
if (nq > 1) {
if (nqsum >= maxq) {
maxq = maxq * 2;
nbytes = maxq * sizeof (double);
qmed1 = (double *) realloc ((void *)qmed, nbytes);
qmed = qmed1;
qmed1 = NULL;
}
qmed[nqsum] = sqrt (qsum1);
nqsum++;
qsum = qsum + sqrt (qsum1);
}
}
/* Print columns being operated on */
for (iop = 0; iop < nop; iop++) {
if (i > 0 || nfind > 0) {
if (printcol) {
if (tabout)
printf (" ");
else if (linout)
printf ("\n");
else
printf (" ");
}
}
/* Extract first value from input line */
for (j = 0; j< 16; j++)
colstr[j] = (char) 0;
strncpy (colstr, cop[iop], lc[iop]);
if (isnum (colstr) == 2) {
dnum = atof (colstr);
ndnum = numdec (colstr);
}
else {
itok = atoi (colstr);
if (getoken (&tokens, itok, token, MAX_LTOK)) {
dnum = atof (token);
ndnum = numdec (token);
}
else {
printf ("___");
continue;
}
}
/* Extract operator from input line */
top = op[iop][0];
/* Extract second value from input line and operate on it */
itok = atoi (op[iop]+1);
if (getoken (&tokens, itok, token, MAX_LTOK)) {
dtok = atof (token);
ndtok = numdec (token);
nd = ndtok;
if (ndec > -1)
nd = ndec;
if (ndnum > nd)
nd = ndnum;
if (top == '+' || top == 'a') {
num2str (numstr, dnum+dtok, 0, nd);
dval = dnum + dtok;
}
else if (top == '_' || top == 's') {
num2str (numstr, dnum-dtok, 0, nd);
dval = dnum - dtok;
}
else if (top == '*' || top == 'm') {
num2str (numstr, dnum*dtok, 0, nd);
dval = dnum * dtok;
}
else if (top == '/' || top == 'd') {
num2str (numstr, dnum/dtok, 0, nd);
dval = dnum / dtok;
}
else
strcpy (numstr,"___");
if (printcol)
printf ("%s", numstr);
if ((!isbadval || (isbadval && dval != badval)) &&
strcmp (numstr,"___")) {
qsum1 = qsum1 + dval * dval;
nq++;
sum[i] = sum[i] + dval;
asum[i] = asum[i] + fabs (dval);
if (!limset[i]) {
colmin[i] = dval;
colmax[i] = dval;
limset[i]++;
}
else if (dval < colmin[i])
colmin[i] = dval;
else if (dval > colmax[i])
colmax[i] = dval;
med[i][nsum[i]] = dval;
amed[i][il] = fabs (dval);
nsum[i]++;
}
}
nt++;
if (nq > 1) {
qmed[nqsum] = sqrt (qsum1);
nqsum++;
qsum = qsum + sqrt (qsum1);
}
}
if (nt > 0 && printcol) {
if (listpath) {
if (tabout)
printf (" ");
else if (linout)
printf ("\n");
else
printf (" ");
printf ("%s", filename);
}
printf ("\n");
}
}
}
/* Print sums of values in numeric columns */
ncol = nfind + nop;
if (sumcol) {
for (i = 0; i < ncol; i++) {
if (nsum[i] > 0) {
if (i < ncol-1)
printf ("%f ", sum[i]);
else
printf ("%f", sum[i]);
}
else if (i < ncol-1)
printf ("___ ");
else
printf ("___");
}
if (ncol > 0)
printf ("\n");
}
/* Print means of absolute values in numeric columns */
if (ameancol) {
for (i = 0; i < ncol; i++) {
if (nsum[i] > 0) {
dval = asum[i] / (double)nsum[i];
if (hms[i]) {
if (ndec > -1)
dec2str (numstr, 32, dval, ndec);
else
dec2str (numstr, 32, dval, 3);
}
else if (ndec > -1)
num2str (numstr, dval, 0, ndec);
else {
sprintf (numstr, "%f", dval);
strfix (numstr, 0, 1);
}
if (i < ncol-1)
printf ("%s ", numstr);
else
printf ("%s", numstr);
}
else if (i < ncol-1)
printf ("___ ");
else
printf ("___");
}
if (countcol) {
for (i = 0; i < ncol; i++) {
if (nent[i] > 0)
printf (" %d", nent[i]);
}
}
if (lranges != NULL)
printf (" %s", lranges);
if (ncol > 0)
printf ("\n");
}
/* Standard deviation of mean absolute value */
if (adevcol) {
for (i = 0; i < ncol; i++) {
if (nsum[i] > 0) {
dmean = asum[i] / (double)nsum[i];
dval = stdev (amed[i], nsum[i], dmean);
if (hms[i]) {
if (ndec > -1)
dec2str (numstr, 32, dval, ndec);
else
dec2str (numstr, 32, dval, 3);
}
else if (ndec > -1)
num2str (numstr, dval, 0, ndec);
else {
sprintf (numstr, "%f", dval);
strfix (numstr, 0, 1);
}
if (i < ncol-1)
printf ("%s ", numstr);
else
printf ("%s", numstr);
}
else if (i < ncol-1)
printf ("___ ");
else
printf ("___");
}
if (countcol) {
for (i = 0; i < ncol; i++) {
if (nent[i] > 0)
printf (" %d", nent[i]);
}
}
if (lranges != NULL)
printf (" %s", lranges);
if (ncol > 0)
printf ("\n");
}
/* Print means of values in numeric columns */
if (meancol) {
for (i = 0; i < ncol; i++) {
if (nsum[i] > 0) {
dval = sum[i] / (double)nsum[i];
if (hms[i]) {
if (ndec > -1)
dec2str (numstr, 32, dval, ndec);
else
dec2str (numstr, 32, dval, 3);
}
else if (ndec > -1)
num2str (numstr, dval, 0, ndec);
else {
sprintf (numstr, "%f", dval);
strfix (numstr, 0, 1);
}
if (i < ncol-1)
printf ("%s ", numstr);
else
printf ("%s ", numstr);
}
else if (i < ncol-1)
printf ("___ ");
else
printf ("___");
}
if (countcol) {
for (i = 0; i < ncol; i++) {
if (nent[i] > 0)
printf (" %d", nent[i]);
}
}
if (lranges != NULL)
printf (" %s", lranges);
if (ncol > 0)
printf ("\n");
}
/* Standard deviation of mean values */
if (devcol) {
for (i = 0; i < ncol; i++) {
if (nsum[i] > 0) {
dmean = sum[i] / (double)nsum[i];
dval = stdev (med[i], nsum[i], dmean);
if (hms[i]) {
if (ndec > -1)
dec2str (numstr, 32, dval, ndec);
else
dec2str (numstr, 32, dval, 3);
}
else if (ndec > -1)
num2str (numstr, dval, 0, ndec);
else {
sprintf (numstr, "%f", dval);
strfix (numstr, 0, 1);
}
if (i < ncol-1)
printf ("%s ", numstr);
else
printf ("%s", numstr);
}
else if (i < ncol-1)
printf ("___ ");
else
printf ("___");
}
if (countcol) {
for (i = 0; i < ncol; i++) {
if (nent[i] > 0)
printf (" %d", nent[i]);
}
}
if (lranges != NULL)
printf (" %s", lranges);
if (ncol > 0)
printf ("\n");
}
/* Print medians of absolute values in numeric columns */
if (amedcol) {
for (i = 0; i < ncol; i++) {
if (nsum[i] > 0) {
dval = median (amed[i], nsum[i]);
if (hms[i]) {
if (ndec > -1)
dec2str (numstr, 32, dval, ndec);
else
dec2str (numstr, 32, dval, 3);
}
else if (ndec > -1)
num2str (numstr, dval, 0, ndec);
else {
sprintf (numstr, "%f", dval);
strfix (numstr, 0, 1);
}
if (i < ncol-1)
printf ("%s ", numstr);
else
printf ("%s", numstr);
}
else if (i < ncol-1)
printf ("___ ");
else
printf ("___");
}
if (countcol) {
for (i = 0; i < ncol; i++) {
if (nent[i] > 0)
printf (" %d", nent[i]);
}
}
if (lranges != NULL)
printf (" %s", lranges);
if (ncol > 0)
printf ("\n");
}
/* Print medians of values in numeric columns */
if (medcol) {
for (i = 0; i < ncol; i++) {
if (nsum[i] > 0) {
dval = median (med[i], nsum[i]);
if (hms[i]) {
if (ndec > -1)
dec2str (numstr, 32, dval, ndec);
else
dec2str (numstr, 32, dval, 3);
}
else if (ndec > -1)
num2str (numstr, dval, 0, ndec);
else {
sprintf (numstr, "%f", dval);
strfix (numstr, 0, 1);
}
if (i < ncol-1)
printf ("%s ", numstr);
else
printf ("%s", numstr);
}
else if (i < ncol-1)
printf ("___ ");
else
printf ("___");
}
if (countcol) {
for (i = 0; i < ncol; i++) {
if (nent[i] > 0)
printf (" %d", nent[i]);
}
}
if (lranges != NULL)
printf (" %s", lranges);
if (ncol > 0)
printf ("\n");
}
/* Print ranges of values in numeric columns */
if (rangecol) {
for (i = 0; i < ncol; i++) {
if (hms[i]) {
if (ndec > -1)
dec2str (numstr, 32, colmin[i], ndec);
else
dec2str (numstr, 32, colmin[i], 3);
}
else if (ndec > -1)
num2str (numstr, colmin[i], 0, ndec);
else
sprintf (numstr, "%f", colmin[i]);
strfix (numstr, 0, 1);
printf ("%s-", numstr);
if (hms[i]) {
if (ndec > -1)
dec2str (numstr, 32, colmax[i], ndec);
else
dec2str (numstr, 32, colmax[i], 3);
}
else if (ndec > -1)
num2str (numstr, colmax[i], 0, ndec);
else
sprintf (numstr, "%f", colmax[i]);
strfix (numstr, 0, 1);
if (i < ncol-1)
printf ("%s ", numstr);
else
printf ("%s", numstr);
}
if (lranges != NULL)
printf (" %s", lranges);
if (ncol > 0)
printf ("\n");
}
/* Print count for each column */
if (countcol && !meancol && !qmeancol && !ameancol && !medcol) {
for (i = 0; i < ncol; i++) {
if (nent[i] > 0) {
if (i < ncol-1)
printf ("%d ", nent[i]);
else
printf ("%d", nent[i]);
}
}
if (ncol > 0)
printf ("\n");
}
/* Print mean of all numeric columns added in quadrature */
if (qmeancol) {
if (nqsum > 0) {
dval = qsum / (double)nqsum;
sprintf (numstr, "%f", dval);
strfix (numstr, 0, 1);
printf ("%s", numstr);
dval = median (qmed, nqsum);
sprintf (numstr, "%f", dval);
strfix (numstr, 0, 1);
printf (" %s\n", numstr);
}
if (countcol) {
for (i = 0; i < ncol; i++) {
if (nent[i] > 0)
printf (" %d", nent[i]);
}
}
}
/* Free memory used for search results */
if (inum) free ((char *)inum);
if (nsum) free ((char *)nsum);
if (nent) free ((char *)nent);
if (sum) free ((char *)sum);
if (fd != stdin) fclose (fd);
return (ncol);
}
static int
intcompare (i, j)
int *i, *j;
{
if (*i > *j)
return (1);
if (*i < *j)
return (-1);
return (0);
}
static char *
iscolop (string)
char *string;
{
char *c;
/* Check for presence of operation */
if ((c = strchr (string, '+')) != NULL ||
(c = strchr (string, '_')) != NULL ||
(c = strchr (string, '*')) != NULL ||
(c = strchr (string, '/')) != NULL ||
(c = strchr (string, 'a')) != NULL ||
(c = strchr (string, 's')) != NULL ||
(c = strchr (string, 'm')) != NULL ||
(c = strchr (string, 'd')) != NULL) {
/* Check to see if string is a file */
if (access(string,0) && strcmp (string, "stdin"))
return (c);
else
return (NULL);
}
else
return (NULL);
}
static double
median (x, n)
int n;
double *x;
{
int rhs, lhs;
int NComp();
if (n <= 0)
return (0.0);
else if (n == 1)
return (x[0]);
qsort (x, n, sizeof (double), NComp);
lhs = (n - 1) / 2;
rhs = n / 2;
if (lhs == rhs)
return (x[lhs]);
else
return ((x[lhs] + x[rhs]) / 2.0);
}
static double
stdev (x, n, dmean)
double *x;
int n;
double dmean;
{
double diff, sqdiff;
double sumsq = 0.0;
double dn = (double) n;
int i;
if (n < 2) {
return (0.0);
}
for (i = 0; i < n; i++) {
diff = (x[i] - dmean);
sqdiff = diff * diff;
sumsq = sumsq + sqdiff;
}
return (sqrt (sumsq / dn));
}
static int
iscol (string)
char *string; /* Character string */
{
int lstr, i, nd;
char cstr;
/* Return 0 if string is NULL */
if (string == NULL)
return (0);
lstr = strlen (string);
nd = 0;
/* Remove trailing spaces */
while (string[lstr-1] == ' ')
lstr--;
/* Column strings contain 0123456789 and . or : for subranges */
for (i = 0; i < lstr; i++) {
cstr = string[i];
if (cstr == '\n')
break;
/* Ignore leading spaces */
if (cstr == ' ' && nd == 0)
continue;
if ((cstr < 48 || cstr > 57) &&
cstr != ':' && cstr != '.' )
return (0);
else if (cstr >= 47 && cstr <= 57)
nd++;
}
if (nd > 0)
return (1);
else
return (0);
}
int
NComp (pd1, pd2)
void *pd1, *pd2;
{
double d1 = *((double *)pd1);
double d2 = *((double *)pd2);
if (d1 > d2)
return (1);
else if (d1 < d2)
return (-1);
else
return (0);
}
/* Nov 2 1999 New program
* Nov 3 1999 Add option to read from stdin as input filename
* Dec 1 1999 Add options to print counts, means, and sums of columns
* Dec 14 1999 Add option for tab-separated output
*
* Jan 7 2000 Add option to list range of lines or filed list of lines
* Jan 26 2000 Add documentation of entry count and tab output
* Jan 26 2000 Add option to print tab table header
* Feb 11 2000 Fix reallocation of range variables
* Mar 20 2000 Add conditional line printing
* Apr 4 2000 Add option to operate on keyword values
* Apr 5 2000 Simply echo lines starting with #
* Jul 21 2000 Link lines with escaped linefeeds at end to next line
* Aug 8 2000 Add -l option to append lines without backslashes
* Oct 23 2000 Declare ListFile(); fix column arithmetic command line options
* Nov 20 2000 Clean up code using lint
* Dec 11 2000 Include fitshead.h for string search
*
* Jan 17 2001 Add -d option to set number of output decimal places
* Jan 17 2001 Add a, s, m, d for add, subtract, multiply, divide const or col
* Mar 19 2001 Drop type declarations from intcompare argument list
* Jun 18 2001 Add maximum length of returned string to getoken()
* Jul 17 2001 Check operations for stdin as well as file
* Oct 10 2001 Add sum, mean, sigma for hh:mm:ss and dd:mm:ss entries
* Oct 10 2001 Add -p option to print only sum, mean, sigma, not entries
* Oct 11 2001 Add -f option to print range of values in selected columns
* Oct 16 2001 Add -e option to compute medians
* Oct 16 2001 Ignore non-numeric values for sums, means, and medians
* Nov 13 2001 Add option to specifiy characters of column to use
* Dec 28 2001 Clear second control character at the end of a line (CR/LF)
*
* Apr 9 2002 NComp() cannot be static as it is passed to qsort()
* Apr 10 2002 Add option to print means and medians of absolute values
* Apr 11 2002 Add option to print mean of selected columns added in quadrature
* Apr 12 2002 Add option to print median of selected columns added in quadrature
* Apr 12 2002 Fix bug in computing median of filtered file
* Apr 12 2002 Add -x option to set ignorable value
* Jun 19 2002 Fix bug that could read files as letter operations
* Jul 18 2002 Read multiple files; add option to print pathname
* Jul 19 2002 Ignore commented lines completely if -z
*
* Jun 10 2003 Print default number of lines in command list
* Dec 9 2003 Allow operation on column if only argument
* Dec 10 2003 Fix number of decimal places in output
* Dec 15 2003 Fix column operation option
* Dec 17 2003 Drop undocumented -z option; print # comments if -h is set
*
* Apr 15 2004 Add % output format option
* Apr 15 2004 Avoid removing trailing zeroes from exponents
* Jul 14 2004 Fix bug in online help message
* Aug 30 2004
* Nov 10 2004 Add -column to include rest of line to newline
*
* Jul 15 2005 Drop unused variables
* Jul 15 2005 Use pointer to tokens in calls to getoken()
* Dec 9 2005 Add line count to same line as mean or median
* Dec 9 2005 Use decimal place setting for means, medians, and ranges, too
* Dec 13 2005 If only scanning a range of lines, print the range
* Dec 15 2005 Clean up line range code
*
* Jan 18 2006 Count tokens if no columns specified, too
* Jun 21 2006 Increase maximum number of tokens from 256 to 1024
* Jun 21 2006 Increase maximum line length from 1024 to 4096
* Jun 21 2006 Clean up code
* Jun 29 2006 Rename strclean() strfix() and move to hget.c
*
* Jan 10 2007 Declare revmsg static, not const
* Jan 10 2007 Include wcs.h
* Feb 6 2006 Fix bug setting conditions and improve usage()
* Feb 7 2007 Fix bug setting up column arithmetic
*
* Jul 1 2008 Fix bug so columns can be compared if both are integers
* Sep 25 2009 Fix error message about illegal operation
*
* Apr 18 2011 Fix allocation bug for median of too many entries
*
* Aug 12 2013 Subtract one from returned token count for accuracy
*
* Nov 19 2015 Add test <col>=n[0][1][2][3][4] to check for numericness
*
* Dec 10 2015 Fix tests
*
* Jun 9 2016 Fix isnum() tests for added coloned times and dashed dates
* Aug 10 2016 Add -u argument to print each field on a new line
*
* Mar 30 2017 Increase default maximum number of lines to process to 500000
*
* Jul 25 2019 Fix bug so stats on floating point numbers are computed
* Jul 25 2019 Add standard deviations of means of selected columns
*
* Jan 22 2020 Change path option to append path at end of each line
*/
|