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
|
/*
* Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
*/
#include <math.h>
#include <funtools.h>
#include <NaN.h>
#include <filter.h>
#include <swap.h>
#include <word.h>
#include <xalloc.h>
#define MAXROW 8192
static int maxrow=MAXROW;
/* max number of intervals we will process in one pass through table data */
#define MAXINTV 1024
/* type of data we process */
#define SRC 0
#define BKG 1
/* this must match number of types above */
#define NTYPE 2
/* how corrections (time and exposure) are applied to source/bkgd */
#define NONE 0
#define SCORR 1
#define BCORR 2
#define BOTH 3
/* types of background */
#define BKG_VAL 1
#define BKG_ALL 2
#define BKG_EACH 3
/* types of image -> exposure pixel conversion algorithms */
#define EXP_RATIO 1
#define EXP_WCS 2
#define ARCSEC_PER_DEG 3600.0
#define ARCSEC_PER_DEGSQ (ARCSEC_PER_DEG*ARCSEC_PER_DEG)
extern char *optarg;
extern int optind;
#ifdef ANSI_FUNC
static void
usage (char *fname)
#else
static void usage(fname)
char *fname;
#endif
{
fprintf(stderr,
"usage: %s <switches> sname [sreg] [bname breg|breg|cnts]\n",
fname);
fprintf(stderr, "optional switches:\n");
fprintf(stderr, " -e \"source_exposure[;bkgd_exposure]\" # exp matches data\n");
fprintf(stderr, " -w \"source_exposure[;bkgd_exposure]\" # WCS method\n");
fprintf(stderr, "\t\t# source (bkgd) FITS exposure image\n");
fprintf(stderr, " -t \"source_timecorr[;bkgd_timecorr]\"\n");
fprintf(stderr, "\t\t# source (bkgd) time correction value or header parameter name\n");
fprintf(stderr, " -g\t\t# output using nice g format\n");
fprintf(stderr, " -G\t\t# output using %%.14g format (maximum precision)\n");
fprintf(stderr, " -i \"[column;]int1;int2...\" # column-based intervals\n");
fprintf(stderr, " -m\t\t# match individual source and bkgd regions\n");
fprintf(stderr, " -p\t\t# output in pixels, even if wcs is present\n");
fprintf(stderr, " -r\t\t# output inner/outer radii (and angles) for annuli (and pandas)\n");
fprintf(stderr, " -s\t\t# output summed values\n");
fprintf(stderr, " -v \"scol[;bcol]\" # src and bkgd value columns for tables\n");
fprintf(stderr, " -T\t\t# output in starbase/rdb table format\n");
fprintf(stderr, " -z\t\t# include regions with zero area in output\n");
fprintf(stderr, "\n(version: %s)\n", FUN_VERSION);
exit(1);
}
#ifdef ANSI_FUNC
static double
DConvert (char *buf, int type, int n)
#else
static double
DConvert(buf, type, n)
char *buf;
int type;
int n;
#endif
{
int ival;
double dval=0.0;
switch(type){
case 'X':
if( n == 16 )
dval = (double)*(unsigned short *)buf;
else if( n == 32 )
dval = (double)*(unsigned int *)buf;
else
dval = (double)*(unsigned char *)buf;
break;
case 'B':
dval = (double)*(unsigned char *)buf;
break;
case 'I':
dval = (double)*(short *)buf;
break;
case 'U':
dval = (double)*(unsigned short *)buf;
break;
case 'J':
dval = (double)*(int *)buf;
break;
case 'K':
#if HAVE_LONG_LONG == 0
gerror(stderr,
"64-bit data support not built (long long not available)\n");
#endif
break;
case 'V':
dval = (double)*(unsigned int *)buf;
break;
case 'E':
dval = (double)*(float *)buf;
break;
case 'D':
dval = *(double *)buf;
break;
case 'L':
ival = (int)*(unsigned char *)buf;
if( !ival || (ival == 'F') || (ival == 'f') )
dval = 0.0;
else
dval = 1.0;
break;
default:
dval = 0.0;
break;
}
return(dval);
}
#ifdef ANSI_FUNC
int
main (int argc, char **argv)
#else
int
main(argc, argv)
int argc;
char **argv;
#endif
{
int c;
int i, j, k, v;
int args;
int bkarea=0;
int bktype=BKG_VAL;
int dosum=0;
int dog=0;
int domatch=0;
int doradang=0;
int dobkgderr=1;
int doexp=0;
int dotim=0;
int dopixels=0;
int dozero=0;
int evimage=0;
int cold=' ';
int nintv=1;
int endian[NTYPE], type[NTYPE], rawsize[NTYPE], rowsize[NTYPE];
int x0[NTYPE], x1[NTYPE], y0[NTYPE], y1[NTYPE];
int dim1[4], dim2[4], block[4];
int *area[NTYPE];
int *savearea[NTYPE];
int nmask[NTYPE]={0,0};
int nreg[NTYPE]={0,0};
int valtypes[NTYPE]={0,0};
int valoffsets[NTYPE]={0,0};
int valns[NTYPE]={0,0};
int exptrans[NTYPE]={EXP_RATIO,EXP_RATIO};
double bkexp=0.0;
double dpp[NTYPE]={-1.0,-1.0};
double timecorr[NTYPE]={1.0,1.0};
double *cnts[MAXINTV][NTYPE];
double *savecnts[MAXINTV][NTYPE];
double *bncnts[MAXINTV];
double *bnerr[MAXINTV];
double *bscnts[MAXINTV];
double *bserr[MAXINTV];
double *exp[NTYPE];
double *saveexp[NTYPE];
double dppnorm=1.0;
double bkval=0.0;
char mode[SZ_LINE];
char tbuf[SZ_LINE];
char tbuf2[SZ_LINE];
char *intvs[MAXINTV];
char *s, *t, *u;
char *ebuf;
char *eptr;
char *fmt=NULL;
char *expstr=NULL;
char *intvstr=NULL;
char *valstr=NULL;
char *timestr=NULL;
char *radang=NULL;
char *cradang=NULL;
char *tradang=NULL;
char *region[NTYPE]={NULL,NULL};
char *filtstr[NTYPE]={NULL,NULL};
char *bincols[NTYPE]={NULL,NULL};
char *valname[2]={NULL,NULL};
char *name[4]={NULL,NULL,NULL, NULL};
Fun fun[4]={NULL,NULL,NULL,NULL};
FITSHead header[NTYPE]={NULL,NULL};
FilterMask masks[NTYPE]={NULL,NULL};
Filter filter[NTYPE]={NULL,NULL};
struct WorldCoor *wcs[4]={NULL,NULL,NULL,NULL};
/* clear array elelemts to mark them as unallocated */
memset(cnts, 0, MAXINTV*NTYPE*sizeof(double *));
memset(savecnts, 0, MAXINTV*NTYPE*sizeof(double *));
memset(bncnts, 0, NTYPE*sizeof(double *));
memset(bnerr, 0, NTYPE*sizeof(double *));
memset(bscnts, 0, NTYPE*sizeof(double *));
memset(bserr, 0, NTYPE*sizeof(double *));
memset(exp, 0, NTYPE*sizeof(double *));
memset(saveexp, 0, NTYPE*sizeof(double *));
memset(area, 0, NTYPE*sizeof(int *));
memset(savearea, 0, NTYPE*sizeof(int *));
memset(intvs, 0, MAXINTV*sizeof(char *));
/* exit on gio errors */
if( !getenv("GERROR") )
setgerror(2);
/* we want the args in the same order in which they arrived, and
gnu getopt sometimes changes things without this */
putenv("POSIXLY_CORRECT=true");
/* process switch arguments */
while ((c = getopt(argc, argv, "e:gGhi:mprst:v:w:zEIT")) != -1){
switch(c){
case 'e':
expstr = optarg;
break;
case 'g':
dog = 1;
break;
case 'G':
dog = 2;
break;
case 'h':
usage(argv[0]);
break;
case 'i':
intvstr = optarg;
break;
case 'm':
domatch = 1;
break;
case 'p':
dopixels = 1;
break;
case 'r':
doradang = 1;
break;
case 's':
dosum = 1;
break;
case 't':
timestr = optarg;
break;
case 'v':
valstr = optarg;
break;
case 'w':
putenv("FUNCNTS_EXPTRANS=wcs");
expstr = optarg;
break;
case 'z':
dozero = 1;
break;
case 'E':
evimage = 0;
break;
case 'I':
evimage = 1;
break;
case 'T':
cold = '\t';
break;
}
}
/* get maxrow,if user-specified */
if( (s=getenv("FUN_MAXROW")) != NULL )
maxrow = atoi(s);
/* check for required arguments */
args = argc - optind;
if( args < 1 ) usage(argv[0]);
/* arg 1: source file name */
name[SRC] = xstrdup(argv[optind+0]);
/* arg 2: source region */
if( (args == 1) || !*(region[SRC] = argv[optind+1]) )
region[SRC] = "field()";
/* arg 3: background region */
if( args >= 3 ){
if( args == 3 ){
name[BKG] = NULL;
region[BKG] = argv[optind+2];
}
else{
name[BKG] = xstrdup(argv[optind+2]);
region[BKG] = argv[optind+3];
}
/* check for constant numeric value -- background counts */
bkval = strtod(region[BKG], &s);
/* if we did not get a valid numeric constant, its a region */
if( region[BKG] && *region[BKG] && (s == region[BKG]) ){
bktype = BKG_ALL;
bkval = 0.0;
}
else{
/* can't have background file and background value */
if( args == 4 ) usage(argv[0]);
bktype = BKG_VAL;
region[BKG] = NULL;
}
}
/* get value column names, if necessary */
if( valstr ){
int ip=0;
int i=0;
newdtable(";");
for(i=0; i<NTYPE; i++){
if( word(valstr, tbuf, &ip) && *tbuf ){
if( strcasecmp(tbuf, "$none") )
valname[i] = xstrdup(tbuf);
else
valname[i] = xstrdup("-");
}
}
freedtable();
}
/* open the source FITS file */
if( !(fun[SRC] = FunOpen(name[SRC], "r", NULL)) )
gerror(stderr, "can't FunOpen source file (or find extension): %s\n",
name[SRC]);
/* get required information from funtools structure */
FunInfoGet(fun[SRC], FUN_ENDIAN, &endian[SRC],
FUN_TYPE, &type[SRC], FUN_HEADER, &header[SRC],
FUN_SECT_X0, &x0[SRC], FUN_SECT_X1, &x1[SRC],
FUN_SECT_Y0, &y0[SRC], FUN_SECT_Y1, &y1[SRC],
FUN_SECT_DIM1, &dim1[SRC], FUN_SECT_DIM2, &dim2[SRC],
FUN_SECT_BLOCK, &block[SRC], FUN_WCS, &wcs[SRC],
FUN_RAWSIZE, &rawsize[SRC], FUN_BINCOLS, &bincols[SRC],
FUN_ROWSIZE, &rowsize[SRC],
0);
/* set dim2 to 1 for a 1D image */
if( dim2[SRC] == 0 ) dim2[SRC] = 1;
/* make up filter mode string using source bincols */
strcpy(mode, "type=image");
/* add the binning key */
if( bincols[SRC] ){
strcat(mode, ",");
strcat(mode, bincols[SRC]);
}
/* open the source region */
filter[SRC] = FilterOpen(header[SRC], region[SRC], mode);
if( filter[SRC] && (filter[SRC] != NOFILTER) ){
/* retrieve region mask segments */
nmask[SRC] = FilterImage(filter[SRC],
x0[SRC], x1[SRC], y0[SRC], y1[SRC],
block[SRC], &masks[SRC], &nreg[SRC]);
/* filtstr[SRC] = xstrdup(FilterString(filter[SRC])); */
/* for display, we have to add # comment chars after each \n */
t = FilterString(filter[SRC]);
filtstr[SRC] = calloc(strlen(t)*3, sizeof(char));
for(u=filtstr[SRC]; *t; t++){
*u++ = *t;
if( *t == '\n' ){
*u++ = '#';
*u++ = ' ';
}
}
filtstr[SRC] = realloc(filtstr[SRC], strlen(filtstr[SRC])+1);
}
if( filter[SRC] ) FilterClose(filter[SRC]);
/* make sure we have something to do */
if( nreg[SRC] == 0 )
gerror(stderr, "no valid regions included in '%s'\n", region[SRC]);
/* gather up intervals, if necessary (must be done before memory alloc) */
if( intvstr ){
int ip=0;
int ntok=0;
newdtable(";");
nintv = 0;
*tbuf2 = '\0';
while( word(intvstr, tbuf, &ip) && *tbuf ){
/* check for default column as first token */
if( !ntok && !strpbrk(tbuf, "=><&|") ){
strcpy(tbuf2, tbuf);
strcat(tbuf2, "=");
}
else{
if( nintv >= MAXINTV ){
gwarning(stderr, "Too many intervals; ignoring: %s\n", tbuf);
continue;
}
intvs[nintv] = (char *)calloc(SZ_LINE, sizeof(char));
if( *tbuf2 ) strcat(intvs[nintv], tbuf2);
strcat(intvs[nintv], tbuf);
nintv++;
}
ntok++;
}
freedtable();
}
/* if a value name was specified, make sure its a calid column */
if( valname[SRC] && (*valname[SRC] != '-') ){
if( !FunColumnLookup(fun[SRC], valname[SRC], 0, NULL,
&valtypes[SRC], NULL,
&valoffsets[SRC], NULL, NULL) ){
gerror(stderr, "value column does not exist: %s\n", valname[SRC]);
}
}
/* allocate space for results */
for(i=0; i<nintv; i++){
cnts[i][SRC] = (double *)xcalloc(nreg[SRC]+1, sizeof(double));
savecnts[i][SRC] = (double *)xcalloc(nreg[SRC]+1, sizeof(double));
bncnts[i] = (double *)xcalloc(nreg[SRC]+1, sizeof(double));
bnerr[i] = (double *)xcalloc(nreg[SRC]+1, sizeof(double));
bscnts[i] = (double *)xcalloc(nreg[SRC]+1, sizeof(double));
bserr[i] = (double *)xcalloc(nreg[SRC]+1, sizeof(double));
}
area[SRC] = (int *)xcalloc(nreg[SRC]+1, sizeof(int));
exp[SRC] = (double *)xcalloc(nreg[SRC]+1, sizeof(double));
savearea[SRC] = (int *)xcalloc(nreg[SRC]+1, sizeof(int));
saveexp[SRC] = (double *)xcalloc(nreg[SRC]+1, sizeof(double));
/* get radii and angle string, if needed */
if( doradang ){
if( (radang = FilterRadAng()) )
radang = xstrdup(radang);
}
/* process background region, if necessary */
if( region[BKG] ){
/* open the background FITS file */
if( name[BKG] ){
if( !(fun[BKG] = FunOpen(name[BKG], "r", NULL)) )
gerror(stderr,
"can't FunOpen background file (or find extension): %s\n",
name[BKG]);
/* get required information from funtools structure */
FunInfoGet(fun[BKG], FUN_ENDIAN, &endian[BKG],
FUN_TYPE, &type[BKG], FUN_HEADER, &header[BKG],
FUN_SECT_X0, &x0[BKG], FUN_SECT_X1, &x1[BKG],
FUN_SECT_Y0, &y0[BKG], FUN_SECT_Y1, &y1[BKG],
FUN_SECT_DIM1, &dim1[BKG], FUN_SECT_DIM2, &dim2[BKG],
FUN_SECT_BLOCK, &block[BKG], FUN_WCS, &wcs[BKG],
FUN_RAWSIZE, &rawsize[BKG], FUN_BINCOLS, &bincols[BKG],
FUN_ROWSIZE, &rowsize[BKG],
0);
/* set dim2 to 1 for a 1D image */
if( dim2[BKG] == 0 ) dim2[BKG] = 1;
/* make up filter mode string using back bincols */
strcpy(mode, "type=image");
/* add the binning key */
if( bincols[BKG] ){
strcat(mode, ",");
strcat(mode, bincols[BKG]);
}
}
/* use values from the source */
else{
fun[BKG] = fun[SRC];
endian[BKG] = endian[SRC];
type[BKG] = type[SRC]; header[BKG] = header[SRC];
x0[BKG] = x0[SRC]; x1[BKG] = x1[SRC];
y0[BKG] = y0[SRC]; y1[BKG] = y1[SRC];
dim1[BKG] = dim1[SRC]; dim2[BKG] = dim2[SRC];
block[BKG] = block[SRC];
rawsize[BKG] = rawsize[SRC];
rowsize[BKG] = rowsize[SRC];
bincols[BKG] = bincols[SRC];
}
/* open the background region */
filter[BKG] = FilterOpen(header[BKG], region[BKG], mode);
if( filter[BKG] && (filter[BKG] != NOFILTER) ){
/* retrieve region mask segments */
nmask[BKG] = FilterImage(filter[BKG],
x0[BKG], x1[BKG], y0[BKG], y1[BKG],
block[BKG], &masks[BKG], &nreg[BKG]);
/* see if we want to, and can, match source and bkgd regions */
if( domatch && (nreg[SRC] == nreg[BKG]) )
bktype = BKG_EACH;
/* filtstr[BKG] = xstrdup(FilterString(filter[BKG])); */
/* for display, we have to add # comment chars after each \n */
t = FilterString(filter[BKG]);
filtstr[BKG] = calloc(strlen(t)*3, sizeof(char));
for(u=filtstr[BKG]; *t; t++){
*u++ = *t;
if( *t == '\n' ){
*u++ = '#';
*u++ = ' ';
}
}
filtstr[BKG] = realloc(filtstr[BKG], strlen(filtstr[BKG])+1);
}
if( filter[BKG] ) FilterClose(filter[BKG]);
/* if a value name was specified, make sure its a calid column */
if( valname[BKG] ){
if( *valname[BKG] != '-' ){
if( !FunColumnLookup(fun[BKG], valname[BKG], 0, NULL,
&valtypes[BKG], NULL,
&valoffsets[BKG], NULL, NULL) ){
gerror(stderr, "value column does not exist: %s\n", valname[BKG]);
}
}
}
else{
if( valname[SRC] && (*valname[SRC] != '-') ){
valname[BKG] = xstrdup(valname[SRC]);
valtypes[BKG] = valtypes[SRC];
valoffsets[BKG] = valoffsets[SRC];
}
}
/* allocate space for results */
for(i=0; i<nintv; i++){
cnts[i][BKG] = (double *)xcalloc(nreg[BKG]+1, sizeof(double));
savecnts[i][BKG] = (double *)xcalloc(nreg[BKG]+1, sizeof(double));
}
area[BKG] = (int *)xcalloc(nreg[BKG]+1, sizeof(int));
exp[BKG] = (double *)xcalloc(nreg[BKG]+1, sizeof(double));
savearea[BKG] = (int *)xcalloc(nreg[BKG]+1, sizeof(int));
saveexp[BKG] = (double *)xcalloc(nreg[BKG]+1, sizeof(double));
}
/* look for degrees/pixel in source and background files -- we will
use these to normalize background area, if both are present */
if( name[BKG] ){
for(i=0; i<2; i++){
if( wcs[i] && iswcs(wcs[i]) ){
if( !wcs[i]->coorflip )
dpp[i] = ABS(wcs[i]->cdelt[0]) * block[i];
else
dpp[i] = ABS(wcs[i]->cdelt[1]) * block[i];
}
}
/* note that BOTH must be present or we do not do this normalization */
if( (dpp[SRC] > 0.0) && (dpp[BKG] > 0.0) )
dppnorm = (dpp[SRC]/dpp[BKG]) * (dpp[SRC]/dpp[BKG]);
}
/* get degrees/pixel for source; not used in norm, but users want to know */
else{
if( wcs[SRC] && iswcs(wcs[SRC]) ){
if( !wcs[SRC]->coorflip )
dpp[SRC] = ABS(wcs[SRC]->cdelt[0]) * block[SRC];
else
dpp[SRC] = ABS(wcs[SRC]->cdelt[1]) * block[SRC];
}
}
/* open exposure file(s), if necessary */
if( expstr ){
int ip=0;
int i=0;
int got=0;
newdtable(";");
for(i=0; i<NTYPE; i++){
if( word(expstr, tbuf, &ip) && *tbuf ){
name[NTYPE+i] = xstrdup(tbuf);
if( !(fun[NTYPE+i] = FunOpen(name[NTYPE+i], "r", NULL)) )
gerror(stderr, "can't FunOpen exp file: %s\n", name[NTYPE+i]);
FunInfoGet(fun[NTYPE+i],
FUN_SECT_DIM1, &dim1[NTYPE+i],
FUN_SECT_DIM2, &dim2[NTYPE+i],
FUN_SECT_BLOCK, &block[NTYPE+i], FUN_WCS, &wcs[NTYPE+i], 0);
/* set dim2 to 1 for a 1D image */
if( dim2[NTYPE+i] == 0 ) dim2[NTYPE+i] = 1;
doexp |= (i+1);
got++;
}
}
freedtable();
/* if we have wcs for the data and exposure, and the user asks for it,
use wcs for conversion */
if( (s=getenv("FUNCNTS_EXPTRANS")) && !strcasecmp(s, "wcs") ){
for(i=0; i<got; i++){
if( wcs[i] && wcs[NTYPE+i] )
exptrans[i] = EXP_WCS;
else
gerror(stderr,
"no WCS present for WCS-based exposure", name[NTYPE+i]);
}
}
}
/* if we have source exposure file but no background exposure file,
and if background data comes from source file , then use the source
exposure file for bkgd exposure as well */
if( (doexp == SCORR) && !name[BKG] && (bktype != BKG_VAL) ){
doexp |= BCORR;
fun[3] = fun[2];
dim1[3] = dim1[2];
dim2[3] = dim2[2];
block[3] = block[2];
}
/* get time correction values */
if( timestr ){
int ip=0;
int i=0;
int got;
double dval;
char *t;
newdtable(";");
for(i=0; i<2; i++){
if( word(timestr, tbuf, &ip) && *tbuf ){
dval = strtod(tbuf, &t);
if( t != tbuf ){
timecorr[i] = dval;
}
else{
timecorr[i] = FunParamGetd(fun[i], tbuf, 0, 1.0, &got);
if( !got ){
/* try pure upper case */
cluc(tbuf);
timecorr[i] = FunParamGetd(fun[i], tbuf, 0, 1.0, &got);
if( !got ){
gerror(stderr, "can't find time correction parameter: %s\n",
tbuf);
}
}
}
dotim |= (i+1);
}
}
freedtable();
}
/* if we have source time correction but no background time correction,
and if background data comes from source file , then use the source
correction for bkgd as well */
if( (dotim == SCORR) && !name[BKG] && (bktype != BKG_VAL) ){
dotim |= BCORR;
timecorr[BKG] = timecorr[SRC];
}
/* process separate source/background files */
if( name[BKG] ){
for(k=0; k<NTYPE; k++){
/* process counts in all regions */
switch(type[k]){
case FUN_IMAGE:
case FUN_ARRAY:
{
int y, lasty;
double *dbuf=NULL;
/* allocate a row buffer */
dbuf = xmalloc(dim1[k] * sizeof(double));
/* seed with impossible value so we load first line */
lasty = -1;
for(i=0; i<nmask[k]; i++){
y = masks[k][i].y;
if( y != lasty ){
if( !FunImageRowGet(fun[k], dbuf, y, y, "bitpix=-64") )
gerror(stderr, "can't FunImageRowGet: %d %s\n", y, name[k]);
lasty = y;
}
area[k][masks[k][i].region-1] +=
masks[k][i].xstop - masks[k][i].xstart + 1;
for(j=masks[k][i].xstart-1; j<=masks[k][i].xstop-1; j++){
if( !isnand(dbuf[j]) ){
cnts[0][k][masks[k][i].region-1] += dbuf[j];
}
}
}
/* free temp buffers */
if( dbuf ) xfree(dbuf);
}
break;
case FUN_TABLE:
case FUN_EVENTS:
if( evimage ){
int *iptr;
int *ibuf=NULL;
/* read data as ints */
if( !(ibuf = FunImageGet(fun[k], NULL, "bitpix=32")) )
gerror(stderr, "can't FunImageGet: %s\n", name[k]);
/* get source counts */
for(i=0; i<nmask[k]; i++){
iptr = &(ibuf[(masks[k][i].y-1)*dim1[k]]);
for(j=masks[k][i].xstart-1; j<=masks[k][i].xstop-1; j++){
cnts[0][k][masks[k][i].region-1] += iptr[j];
}
}
/* free up space */
if( ibuf ) xfree(ibuf);
}
else{
int *rbuf;
int got;
char *rawbuf;
Filter efilter[MAXINTV];
/* make sure we will have a valid image section in which to filter */
if( (x1[k]-x0[k] <= 0) || (y1[k]-y0[k] <= 0) )
gerror(stderr,
"invalid or zero image dimensions(s) for table (%s)\n",
bincols[k] ? bincols[k] : "invalid bincols?");
/* make up the mode string */
snprintf(mode, SZ_LINE,
"type=events,convert=%s,evsect=\"%d %d %d %d %d\"",
(endian[k] == is_bigendian()) ? "false" : "true",
x0[k], x1[k], y0[k], y1[k], block[k]);
/* add columns */
if( bincols[k] ){
strcat(mode, ",");
strcat(mode, bincols[k]);
}
/* open filters for all intervals */
for(v=0; v<nintv; v++){
strcpy(tbuf, region[k]);
if( intvs[v] ){
strcat(tbuf, "&&(");
strcat(tbuf, intvs[v]);
strcat(tbuf, ")");
}
efilter[v] = FilterOpen(header[k], tbuf, mode);
}
/* allocate region value buffer */
rbuf = xmalloc(maxrow*sizeof(int));
/* extract events */
while( (ebuf = FunTableRowGet(fun[k], NULL, maxrow, NULL, &got)) ){
/* get pointer to raw buffer */
FunInfoGet(fun[k], FUN_RAWBUF, &rawbuf, 0);
/* process all intervals in one pass through this data */
for(v=0; v<nintv; v++){
/* get events which pass the region filter */
if( efilter[v] && (efilter[v] != NOFILTER) &&
FilterEvents(efilter[v], rawbuf, rawsize[k], got, rbuf)){
/* loop through events, process those which are in a region */
for(i=0; i<got; i++){
eptr = (char *)(ebuf+(rowsize[k]*i)+valoffsets[k]);
if( rbuf[i] > 0 ){
if( valname[k] && (*valname[k] != '-') )
cnts[v][k][rbuf[i]-1] +=
DConvert(eptr, valtypes[k], valns[k]);
else
cnts[v][k][rbuf[i]-1] += 1;
}
}
}
}
/* free for next read */
if( ebuf ) xfree(ebuf);
}
/* Done with region values */
if( rbuf ) xfree(rbuf);
/* close filters */
for(v=0; v<nintv; v++){
if( efilter[v] && (efilter[v] != NOFILTER) ){
FilterClose(efilter[v]);
}
}
}
/* get area */
for(i=0; i<nmask[k]; i++){
area[k][masks[k][i].region-1] +=
masks[k][i].xstop - masks[k][i].xstart + 1;
}
break;
}
}
}
/* same file for source and background:
this code is optimized so that we only traverse the file once
*/
else{
/* process counts in all regions */
switch(type[SRC]){
case FUN_IMAGE:
case FUN_ARRAY:
{
int state;
int y, lasty;
double *dbuf=NULL;
/* allocate a row buffer */
dbuf = xmalloc(dim1[SRC] * sizeof(double));
/* seed with impossible value so we load first line */
lasty = -1;
for(i=0, j=0; i<nmask[SRC] || j<nmask[BKG]; ){
if( (i<nmask[SRC]) && (j<nmask[BKG]) ) state = 3;
else if( i<nmask[SRC] ) state = 1;
else if( j<nmask[BKG] ) state = 2;
else break;
switch(state){
case 1:
sline:
y = masks[SRC][i].y;
if( y != lasty ){
if( !FunImageRowGet(fun[SRC], dbuf, y, y, "bitpix=-64") )
gerror(stderr, "can't FunImageRowGet: %d %s\n", y, name[SRC]);
lasty = y;
}
area[SRC][masks[SRC][i].region-1] +=
masks[SRC][i].xstop - masks[SRC][i].xstart + 1;
for(k=masks[SRC][i].xstart-1; k<=masks[SRC][i].xstop-1; k++){
if( !isnand(dbuf[k]) ){
cnts[0][SRC][masks[SRC][i].region-1] += dbuf[k];
}
}
i++;
break;
case 2:
bline:
y = masks[BKG][j].y;
if( y != lasty ){
if( !FunImageRowGet(fun[SRC], dbuf, y, y, "bitpix=-64") )
gerror(stderr, "can't FunImageRowGet: %d %s\n", y, name[SRC]);
lasty = y;
}
area[BKG][masks[BKG][j].region-1] +=
masks[BKG][j].xstop - masks[BKG][j].xstart + 1;
for(k=masks[BKG][j].xstart-1; k<=masks[BKG][j].xstop-1; k++){
if( !isnand(dbuf[k]) ){
cnts[0][BKG][masks[BKG][j].region-1] += dbuf[k];
}
}
j++;
break;
case 3:
if( masks[SRC][i].y <= masks[BKG][j].y ){
goto sline;
}
goto bline;
}
}
if( dbuf ) xfree(dbuf);
}
break;
case FUN_TABLE:
case FUN_EVENTS:
if( evimage ){
int *iptr;
int *ibuf=NULL;
/* read data as ints */
if( !(ibuf = FunImageGet(fun[SRC], NULL, "bitpix=32")) )
gerror(stderr, "can't FunImageGet: %s\n", name[SRC]);
/* get source counts */
for(i=0; i<nmask[SRC]; i++){
iptr = &(ibuf[(masks[SRC][i].y-1)*dim1[SRC]]);
for(j=masks[SRC][i].xstart-1; j<=masks[SRC][i].xstop-1; j++){
cnts[0][SRC][masks[SRC][i].region-1] += iptr[j];
}
}
/* get background counts and area, if necessary */
if( bktype != BKG_VAL ){
for(i=0; i<nmask[BKG]; i++){
iptr = &(ibuf[(masks[BKG][i].y-1)*dim1[SRC]]);
for(j=masks[BKG][i].xstart-1; j<=masks[BKG][i].xstop-1; j++){
cnts[0][BKG][masks[BKG][i].region-1] += iptr[j];
}
}
}
/* free up space */
if( ibuf ) xfree(ibuf);
}
else{
int *rbuf;
int got;
char *rawbuf;
Filter efilter[MAXINTV][NTYPE];
/* make sure we will have a valid image section in which to filter */
if( (x1[SRC]-x0[SRC] <= 0) || (y1[SRC]-y0[SRC] <= 0) )
gerror(stderr,
"invalid or zero image dimensions(s) for table (%s)\n",
bincols[SRC] ? bincols[SRC] : "invalid bincols?");
/* open new filters to filter events through regions */
snprintf(mode, SZ_LINE,
"type=events,convert=%s,evsect=\"%d %d %d %d %d\"",
(endian[SRC] == is_bigendian()) ? "false" : "true",
x0[SRC], x1[SRC], y0[SRC], y1[SRC], block[SRC]);
/* add columns */
if( bincols[SRC] ){
strcat(mode, ",");
strcat(mode, bincols[SRC]);
}
/* open filters for all intervals */
for(v=0; v<nintv; v++){
for(i=0; i<NTYPE; i++){
if( region[i] ){
strcpy(tbuf, region[i]);
if( intvs[v] ){
strcat(tbuf, "&&(");
strcat(tbuf, intvs[v]);
strcat(tbuf, ")");
}
efilter[v][i] = FilterOpen(header[i], tbuf, mode);
}
else{
efilter[v][i] = NULL;
}
}
}
/* allocate region value buffer */
rbuf = xmalloc(maxrow*sizeof(int));
/* extract and filter events */
while( (ebuf = FunTableRowGet(fun[SRC], NULL, maxrow, NULL, &got)) ){
/* get pointer to raw buffer */
FunInfoGet(fun[SRC], FUN_RAWBUF, &rawbuf, 0);
/* process all intervals in one pass through this data */
for(v=0; v<nintv; v++){
/* get events which pass the region filter */
for(i=0; i<NTYPE; i++){
if( efilter[v][i] && (efilter[v][i] != NOFILTER) &&
FilterEvents(efilter[v][i], rawbuf, rawsize[i], got, rbuf)){
/* count events which are in a region */
for(j=0; j<got; j++){
eptr = (char *)(ebuf+(rowsize[i]*j)+valoffsets[i]);
if( rbuf[j] > 0 ){
if( valname[i] && (*valname[i] != '-') )
cnts[v][i][rbuf[j]-1] +=
DConvert(eptr, valtypes[i], valns[i]);
else
cnts[v][i][rbuf[j]-1] += 1;
}
}
}
}
}
/* free for next read */
if( ebuf ) xfree(ebuf);
}
/* done with region values */
if( rbuf ) xfree(rbuf);
/* close filters */
for(v=0; v<nintv; v++)
for(i=0; i<NTYPE; i++){
if( efilter[v][i] && (efilter[v][i] != NOFILTER) ){
FilterClose(efilter[v][i]);
}
}
}
/* get source area */
for(i=0; i<nmask[SRC]; i++){
area[SRC][masks[SRC][i].region-1] +=
masks[SRC][i].xstop - masks[SRC][i].xstart + 1;
}
/* get background area, if necessary */
if( bktype != BKG_VAL ){
for(i=0; i<nmask[BKG]; i++){
area[BKG][masks[BKG][i].region-1] +=
masks[BKG][i].xstop - masks[BKG][i].xstart + 1;
}
}
break;
}
}
/* accumulate averge exposure for each region */
if( doexp ){
for(k=0; k<2; k++){
if( fun[NTYPE+k] ){
int ex, ey, lastey;
double d1, d2;
double *exbuf=NULL;
char *tname="unknown";
exbuf = xmalloc(dim1[NTYPE+k] * sizeof(double));
if( name[NTYPE+k] )
tname = name[NTYPE+k];
else if( (k>0) && name[NTYPE+k-1] )
tname = name[NTYPE+k-1];
switch(exptrans[k]){
case EXP_RATIO:
lastey = -1;
d1 = ((double)dim1[NTYPE+k]/(double)dim1[k]);
d2 = ((double)dim2[NTYPE+k]/(double)dim2[k]);
for(i=0; i<nmask[k]; i++){
ey = ((masks[k][i].y - 1) * d2) + 1;
if( ey != lastey ){
if( !FunImageRowGet(fun[NTYPE+k], exbuf, ey, ey, "bitpix=-64") )
gerror(stderr, "can't FunImageRowGet (exp file): %d %s\n",
ey, tname);
lastey = ey;
}
for(j=masks[k][i].xstart; j<=masks[k][i].xstop; j++){
ex = (j-1) * d1 + 1;
if( ex < 1 ) ex = 1;
if( ex > dim1[NTYPE+k] ) ex = dim1[NTYPE+k];
exp[k][masks[k][i].region-1] += exbuf[ex-1];
}
}
break;
case EXP_WCS:
lastey = -1;
for(i=0; i<nmask[k]; i++){
for(j=masks[k][i].xstart; j<=masks[k][i].xstop; j++){
double dval1, dval2;
double dex, dey;
int offscl;
/* convert data image pixels to ra/dec using wcs */
pix2wcs(wcs[k], (double)j,(double)masks[k][i].y, &dval1,&dval2);
/* convert ra/dec to exp image pixels using wcs */
wcs2pix(wcs[NTYPE+k], dval1, dval2, &dex, &dey, &offscl);
ex = (int)(dex+0.5); ey = (int)(dey+0.5);
if( ex < 1 ) ex = 1;
if( ex > dim1[NTYPE+k] ) ex = dim1[NTYPE+k];
if( ey < 1 ) ey = 1;
if( ey > dim2[NTYPE+k] ) ey = dim2[NTYPE+k];
if( ey != lastey ){
if( !FunImageRowGet(fun[NTYPE+k], exbuf, ey, ey, "bitpix=-64") )
gerror(stderr, "can't FunImageRowGet (exp file): %d %s\n",
ey, tname);
lastey = ey;
}
exp[k][masks[k][i].region-1] += exbuf[ex-1];
}
}
break;
default:
gerror(stderr, "unknown exposure conversion type");
}
if( exbuf ) xfree(exbuf);
}
/* since we already checked for source exposure, we must be missing
bkgd exposure. This is OK, unless we have a bkgd file. In that case,
we need to warn the user and make an assumption about errors */
else if( (k==1) && name[BKG] ){
gwarning(stderr,
"No exposure file was specified for the background file.\nWe therefore assume that the bkgd already has been corrected for\nexposure and that the error associated with each bkgd pixel is 0.\n");
dobkgderr = 0;
}
}
}
/* reset the interval index for next stage of proessing*/
v = 0;
/* come back here if we are processing multiple intervals */
intvagain:
/* for data-based background, check validity of background area */
if( bktype != BKG_VAL ){
bkval = 0.0;
bkarea = 0;
bkexp = 0.0;
for(i=0; i<nreg[BKG]; i++){
bkval += cnts[v][BKG][i];
bkarea += area[BKG][i];
bkexp += exp[BKG][i];
}
/* if background area is 0, that is bad */
if( bkarea == 0 ){
gerror(stdout, "background has zero area\n");
}
}
/* display source header information */
fprintf(stdout, "# source\n");
fprintf(stdout, "# data_file:\t\t%s\n", name[SRC]);
if( intvs[v] )
fprintf(stdout, "# interval:\t\t%s\n", intvs[v]);
if( valname[SRC] && (*valname[SRC] != '-') )
fprintf(stdout, "# value column:\t%s\n", valname[SRC]);
if( dpp[SRC] > 0.0 )
fprintf(stdout, "# arcsec/pixel:\t%g\n", dpp[SRC]*ARCSEC_PER_DEG);
if( doexp & SCORR )
fprintf(stdout, "# exp_correction:\t%s\n", name[NTYPE+SRC]);
if( dotim & SCORR )
fprintf(stdout, "# time_correction:\t%g\n", timecorr[SRC]);
/* display bkgd header information */
fprintf(stdout, "# background\n");
if( name[BKG] ){
fprintf(stdout, "# data_file:\t\t%s\n", name[BKG]);
if( intvs[v] )
fprintf(stdout, "# interval:\t\t%s\n", intvs[v]);
if( valname[BKG] && (*valname[BKG] != '-') )
fprintf(stdout, "# value column:\t%s\n", valname[BKG]);
if( dpp[BKG] > 0.0 )
fprintf(stdout, "# arcsec/pixel:\t%g\n", dpp[BKG]*ARCSEC_PER_DEG);
if( doexp & BCORR )
fprintf(stdout, "# exp_correction:\t%s\n", name[NTYPE+BKG]);
if( dotim & BCORR )
fprintf(stdout, "# time_correction:\t%g\n", timecorr[BKG]);
if( dppnorm != 1.0 )
fprintf(stdout, "# wcs area norm factor:\t%g/%g (source/bkgd))\n",
dpp[SRC],dpp[BKG]);
}
else if( bktype != BKG_VAL ){
fprintf(stdout, "# data_file:\t\t%s\n", name[SRC]);
if( valname[SRC] && valname[BKG] && strcmp(valname[SRC], valname[BKG]) &&
(*valname[BKG] != '-') )
fprintf(stdout, "# value_column:\t%s\n", valname[BKG]);
}
else
fprintf(stdout, "# constant_value:\t%.6f\n", bkval);
/* dislay table information */
if( (dpp[SRC] > 0.0) && !dopixels )
s = "arcsec";
else
s = "pixel";
fprintf(stdout, "# column units\n");
fprintf(stdout, "# area:\t\t%s**2\n", s);
fprintf(stdout, "# surf_bri:\t\tcnts/%s**2%s%s\n",
s, (dotim & SCORR)? "/sec" : "", (doexp & SCORR)? "/expval" : "");
fprintf(stdout, "# surf_err:\t\tcnts/%s**2%s%s\n",
s, (dotim & SCORR)? "/sec" : "", (doexp & SCORR)? "/expval" : "");
if( doradang ){
fprintf(stdout, "# radii:\t\t%ss\n", s);
fprintf(stdout, "# angles:\t\tdegrees\n");
}
/* come back here if we also are outputting summed results */
sumagain:
/* if we need to display sums, so the sum now, but save unsummed values,
because we will have to display those as well */
switch(dosum){
case 1:
memcpy(savecnts[v][SRC], cnts[v][SRC], (nreg[SRC]+1)*sizeof(double));
memcpy(savearea[SRC], area[SRC], (nreg[SRC]+1)*sizeof(int));
for(i=1; i<nreg[SRC]; i++){
cnts[v][SRC][i] += cnts[v][SRC][i-1];
area[SRC][i] += area[SRC][i-1];
}
if( bktype != BKG_VAL ){
memcpy(savecnts[v][BKG], cnts[v][BKG],
(nreg[BKG]+1)*sizeof(double));
memcpy(savearea[BKG], area[BKG], (nreg[BKG]+1)*sizeof(int));
for(i=1; i<nreg[BKG]; i++){
cnts[v][BKG][i] += cnts[v][BKG][i-1];
area[BKG][i] += area[BKG][i-1];
}
}
break;
case 2:
memcpy(cnts[v][SRC], savecnts[v][SRC], (nreg[SRC]+1)*sizeof(double));
memcpy(area[SRC], savearea[SRC], (nreg[SRC]+1)*sizeof(int));
if( bktype != BKG_VAL ){
memcpy(cnts[v][BKG], savecnts[v][BKG],
(nreg[BKG]+1)*sizeof(double));
memcpy(area[BKG], savearea[BKG], (nreg[BKG]+1)*sizeof(int));
}
break;
default:
break;
}
/* process the background */
switch(bktype){
/* constant background is counts/pixel */
case BKG_VAL:
for(i=0; i<nreg[SRC]; i++){
bncnts[v][i] = (bkval * area[SRC][i] * timecorr[SRC]);
bscnts[v][i] = cnts[v][SRC][i] - bncnts[v][i];
bserr[v][i] = sqrt(cnts[v][SRC][i]);
bnerr[v][i] = 0.0;
}
break;
case BKG_ALL:
switch( dosum ){
case 1:
bkval = cnts[v][BKG][nreg[BKG]-1];
bkarea = area[BKG][nreg[BKG]-1];
bkexp = exp[BKG][nreg[BKG]-1];
break;
case 2:
default:
bkval = 0.0;
bkarea = 0;
bkexp = 0.0;
/* get total background and background area */
for(i=0; i<nreg[BKG]; i++){
bkval += cnts[v][BKG][i];
bkarea += area[BKG][i];
bkexp += exp[BKG][i];
}
break;
}
{
double tempnorm=0.0;
/* subtract entire normalized background from each source region */
for(i=0; i<nreg[SRC]; i++){
/* get area and exposure normalization */
switch(doexp){
case NONE:
tempnorm = (double)area[SRC][i] / bkarea;
break;
case SCORR:
tempnorm = exp[SRC][i] / bkarea;
break;
case BCORR:
tempnorm = (double)area[SRC][i] / bkexp;
break;
case BOTH:
tempnorm = exp[SRC][i] / bkexp;
break;
}
/* add time normalization */
tempnorm *= (timecorr[SRC]/timecorr[BKG]);
/* add normalization due to different pixels sizes in src and bkg */
tempnorm *= dppnorm;
bncnts[v][i] = (bkval * tempnorm);
bscnts[v][i] = cnts[v][SRC][i] - bncnts[v][i];
bserr[v][i] = sqrt(cnts[v][SRC][i] + (tempnorm*tempnorm*bkval));
if( dobkgderr )
bnerr[v][i] = sqrt(bkval) * tempnorm;
else
bnerr[v][i] = 0.0;
}
}
break;
case BKG_EACH:
{
double tempnorm=0.0;
/* subtract each background from each source region */
for(i=0; i<nreg[SRC]; i++){
/* get area and exposure normalization */
switch(doexp){
case NONE:
tempnorm = (double)area[SRC][i] / (double)area[BKG][i];
break;
case SCORR:
tempnorm = exp[SRC][i] / (double)area[BKG][i];
break;
case BCORR:
tempnorm = (double)area[SRC][i] / (double)exp[BKG][i];
break;
case BOTH:
tempnorm = exp[SRC][i] / (double)exp[BKG][i];
break;
}
/* add time normalization */
tempnorm *= (timecorr[SRC]/timecorr[BKG]);
/* add normalization due to different pixels sizes in src and bkg */
tempnorm *= dppnorm;
bncnts[v][i] = (cnts[v][BKG][i] * tempnorm);
bscnts[v][i] = cnts[v][SRC][i] - bncnts[v][i];
bserr[v][i] = sqrt(cnts[v][SRC][i] +
(tempnorm * tempnorm * cnts[v][BKG][i]));
if( dobkgderr )
bnerr[v][i] = sqrt(cnts[v][BKG][i]) * tempnorm;
else
bnerr[v][i] = 0.0;
}
}
break;
}
/* display results */
/* display the main output table */
switch( dosum ){
case 1:
fprintf(stdout, "\n");
fprintf(stdout, "# summed background-subtracted results\n");
fprintf(stdout, "upto%c net_counts%c error", cold, cold);
break;
case 2:
default:
if( cold == '\t' ) fprintf(stdout, "\f");
fprintf(stdout, "\n");
fprintf(stdout, "# background-subtracted results\n");
fprintf(stdout, " reg%c net_counts%c error", cold, cold);
break;
}
fprintf(stdout, "%c background%c berror", cold, cold);
fprintf(stdout, "%c area%c surf_bri%c surf_err", cold, cold, cold);
if( doradang )
fprintf(stdout, "%c radius1%c radius2%c angle1%c angle2",
cold, cold, cold, cold);
fprintf(stdout, "\n");
fprintf(stdout, "----%c------------%c---------", cold, cold);
fprintf(stdout, "%c------------%c---------", cold, cold);
fprintf(stdout, "%c---------%c---------%c---------", cold, cold, cold);
if( doradang )
fprintf(stdout, "%c---------%c---------%c---------%c---------",
cold, cold, cold, cold);
fprintf(stdout, "\n");
if( radang )
newdtable(",");
cradang = radang;
for(i=0; i<nreg[SRC]; i++){
/* get next line from radii/angle string */
if( cradang ){
tradang = (char *)strchr(cradang, '\n');
if( tradang )
*tradang = '\0';
}
if( area[SRC][i] ){
double cntsperarea;
double errperarea;
double areasq;
if( doexp & SCORR ){
cntsperarea = bscnts[v][i]/(exp[SRC][i]*timecorr[SRC]);
errperarea = bserr[v][i]/(exp[SRC][i]*timecorr[SRC]);
areasq = area[SRC][i];
}
else{
cntsperarea = bscnts[v][i]/(area[SRC][i]*timecorr[SRC]);
errperarea = bserr[v][i]/(area[SRC][i]*timecorr[SRC]);
areasq = area[SRC][i];
}
/* if we know how to convert to cnts/pix**2 to cnts/arcsec**2, do it */
if( !dopixels && (dpp[SRC] > 0.0) ){
cntsperarea = (cntsperarea / (dpp[SRC]*dpp[SRC])) / ARCSEC_PER_DEGSQ;
errperarea = (errperarea / (dpp[SRC]*dpp[SRC])) / ARCSEC_PER_DEGSQ;
areasq = (areasq * (dpp[SRC]*dpp[SRC])) * ARCSEC_PER_DEGSQ;
}
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%4d%c%12.3f%c%9.3f%c%12.3f%c%9.3f%c%9.2f%c%9.3f%c%9.3f";
break;
case 1:
fmt = "%4d%c%12.3g%c%9.3g%c%12.3g%c%9.3g%c%9.2g%c%9.3g%c%9.3g";
break;
case 2:
fmt = "%4d%c%.14g%c%.14g%c%.14g%c%.14g%c%.14g%c%.14g%c%.14g";
break;
}
fprintf(stdout, fmt,
i+1, cold,
bscnts[v][i], cold, bserr[v][i], cold,
bncnts[v][i], cold, bnerr[v][i], cold,
areasq, cold, cntsperarea, cold, errperarea);
/* display values from this line of radii/angles */
if( doradang && cradang ){
int ip=0;
double dval;
for(j=0; j<4; j++){
if( word(cradang, tbuf, &ip) && strcmp(tbuf, "NA") ){
dval = strtod(tbuf, NULL);
if( (j<2) && !dopixels && (dpp[SRC]>0.0) )
fprintf(stdout, "%c%9.3f", cold, (dval*dpp[SRC]*ARCSEC_PER_DEG));
else
fprintf(stdout, "%c%9.3f", cold, dval);
}
else{
fprintf(stdout, "%c%9.9s", cold, "NA");
}
}
}
/* new-line at end */
fprintf(stdout, "\n");
}
/* might have to display zero area pixels */
else if( dozero ){
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%4d%c%12.3f%c%9.3f%c%12.3f%c%9.3f%c%9.2f%c%9.3f%c%9.3f";
break;
case 1:
fmt = "%4d%c%12.3g%c%9.3g%c%12.3g%c%9.3g%c%9.2g%c%9.3g%c%9.3g";
break;
case 2:
fmt = "%4d%c%.14g%c%.14g%c%.14g%c%.14g%c%.14g%c%.14g%c%.14g";
break;
}
fprintf(stdout, fmt, i+1, cold, 0.0, cold, 0.0, cold, 0.0, cold, 0.0,
cold, 0.0, cold, 0.0, cold, 0.0);
/* add the correct radii and angle info, to make plotting easier */
if( doradang && cradang ){
int ip=0;
double dval;
for(j=0; j<4; j++){
if( word(cradang, tbuf, &ip) && strcmp(tbuf, "NA") ){
dval = strtod(tbuf, NULL);
if( (j<2) && !dopixels && (dpp[SRC]>0.0) )
fprintf(stdout, "%c%9.3f", cold, (dval*dpp[SRC]*ARCSEC_PER_DEG));
else
fprintf(stdout, "%c%9.3f", cold, dval);
}
else{
fprintf(stdout, "%c%9.9s", cold, "NA");
}
}
}
/* new-line at end */
fprintf(stdout, "\n");
}
/* bump to next line of radii/angles */
if( tradang ){
cradang = tradang+1;
/* put back the cr in case we pass through again */
*tradang = '\n';
}
}
if( radang )
freedtable();
fprintf(stdout, "\n");
fflush(stdout);
/* if we just summed, go back and display unsummed values */
switch(dosum){
case 1:
dosum = 2;
goto sumagain;
/* set flag back to 1 in case we have multiple intervals to process */
case 2:
dosum = 1;
break;
default:
break;
}
/* display raw source counts */
if( dosum ){
int tarea=0;
double tcnts=0;
if( cold == '\t' ) fprintf(stdout, "\f");
fprintf(stdout, "\n");
/* display source info */
if( filtstr[SRC] ){
fprintf(stdout, "# source_region(s):\n");
fprintf(stdout, "# %s\n\n", filtstr[SRC]);
}
fprintf(stdout, "# summed_source_data\n");
fprintf(stdout,
" reg%c counts%c pixels%c sumcnts%c sumpix\n",
cold, cold, cold, cold);
fprintf(stdout,
"----%c------------%c---------%c------------%c---------\n",
cold, cold, cold, cold);
for(i=0; i<nreg[SRC]; i++){
tcnts += cnts[v][SRC][i];
tarea += area[SRC][i];
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%4d%c%12.3f%c%9d%c%12.3f%c%9d\n";
break;
case 1:
fmt = "%4d%c%12.3g%c%9d%c%12.3g%c%9d\n";
break;
case 2:
fmt = "%4d%c%.14g%c%9d%c%.14g%c%9d\n";
break;
}
fprintf(stdout, fmt,
i+1, cold,
cnts[v][SRC][i], cold, area[SRC][i], cold,
tcnts, cold, tarea);
}
} else{
if( cold == '\t' ) fprintf(stdout, "\f");
fprintf(stdout, "\n");
/* display source info */
if( filtstr[SRC] ){
fprintf(stdout, "# source_region(s):\n");
fprintf(stdout, "# %s\n\n", filtstr[SRC]);
}
fprintf(stdout, "# source_data\n");
fprintf(stdout, " reg%c counts%c pixels", cold, cold);
if( doexp & SCORR )
fprintf(stdout, "%c avg_exp", cold);
fprintf(stdout, "\n");
fprintf(stdout, "----%c------------%c---------", cold, cold);
if( doexp & SCORR )
fprintf(stdout, "%c---------", cold);
fprintf(stdout, "\n");
for(i=0; i<nreg[SRC]; i++){
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%4d%c%12.3f%c%9d";
break;
case 1:
fmt = "%4d%c%12.3g%c%9d";
break;
case 2:
fmt = "%4d%c%.14g%c%9d";
break;
}
fprintf(stdout, fmt, i+1, cold, cnts[v][SRC][i], cold, area[SRC][i]);
if( doexp & SCORR ){
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%c%9.3f";
break;
case 1:
fmt = "%c%9.3g";
break;
case 2:
fmt = "%c%.14g";
break;
}
if( area[SRC][i] > 0 )
fprintf(stdout, fmt, cold, exp[SRC][i]/area[SRC][i]);
else
fprintf(stdout, fmt, cold, 0.0);
}
fprintf(stdout, "\n");
}
}
fprintf(stdout, "\n");
fflush(stdout);
/* display raw background info */
switch(bktype){
case BKG_VAL:
break;
case BKG_ALL:
if( cold == '\t' ) fprintf(stdout, "\f");
fprintf(stdout, "\n");
if( filtstr[BKG] ){
fprintf(stdout, "# background_region(s)\n");
fprintf(stdout, "# %s\n\n", filtstr[BKG]);
}
fprintf(stdout, "# background_data\n");
fprintf(stdout, " reg%c counts%c pixels", cold, cold);
if( doexp & BCORR )
fprintf(stdout, "%c avg_exp", cold);
fprintf(stdout, "\n");
fprintf(stdout, "----%c------------%c---------", cold, cold);
if( doexp & BCORR )
fprintf(stdout, "%c---------", cold);
fprintf(stdout, "\n");
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%s%c%12.3f%c%9d";
break;
case 1:
fmt = "%s%c%12.3g%c%9d";
break;
case 2:
fmt = "%s%c%.14g%c%9d";
break;
}
fprintf(stdout, fmt, "all ", cold, bkval, cold, bkarea);
if( doexp & BCORR ){
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%c%9.3f";
break;
case 1:
fmt = "%c%9.3g";
break;
case 2:
fmt = "%c%.14g";
break;
}
fprintf(stdout, fmt, cold, bkexp/bkarea);
}
fprintf(stdout, "\n");
break;
case BKG_EACH:
if( dosum ){
int tarea=0;
double tcnts=0;
if( cold == '\t' ) fprintf(stdout, "\f");
fprintf(stdout, "\n");
if( filtstr[BKG] ){
fprintf(stdout, "# background_region(s)\n");
fprintf(stdout, "# %s\n\n", filtstr[BKG]);
}
fprintf(stdout, "# summed_background_data\n");
fprintf(stdout,
" reg%c counts%c pixels%c sumcnts%c sumpix\n",
cold, cold, cold, cold);
fprintf(stdout,
"----%c------------%c---------%c------------%c---------\n",
cold, cold, cold, cold);
for(i=0; i<nreg[BKG]; i++){
tcnts += cnts[v][BKG][i];
tarea += area[BKG][i];
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%4d%c%12.3f%c%9d%c%12.3f%c%9d\n";
break;
case 1:
fmt = "%4d%c%12.3g%c%9d%c%12.3g%c%9d\n";
break;
case 2:
fmt = "%4d%c%.14g%c%9d%c%.14g%c%9d\n";
break;
}
fprintf(stdout, fmt, i+1, cold,
cnts[v][BKG][i], cold, area[BKG][i], cold,
tcnts, cold, tarea);
}
} else{
if( cold == '\t' ) fprintf(stdout, "\f");
fprintf(stdout, "\n");
if( filtstr[BKG] ){
fprintf(stdout, "# background_region(s)\n");
fprintf(stdout, "# %s\n\n", filtstr[BKG]);
}
fprintf(stdout, "# background_data\n");
fprintf(stdout, " reg%c counts%c pixels", cold, cold);
if( doexp & BCORR )
fprintf(stdout, "%c avg_exp", cold);
fprintf(stdout, "\n");
fprintf(stdout, "----%c------------%c---------", cold, cold);
if( doexp & BCORR )
fprintf(stdout, "%c---------", cold);
fprintf(stdout, "\n");
for(i=0; i<nreg[BKG]; i++){
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%4d%c%12.3f%c%9d";
break;
case 1:
fmt = "%4d%c%12.3g%c%9d";
break;
case 2:
fmt = "%4d%c%.14g%c%9d";
break;
}
fprintf(stdout, fmt, i+1, cold,
cnts[v][BKG][i], cold, area[BKG][i]);
if( doexp & BCORR ){
/* get correctly precisioned format statement */
switch(dog){
case 0:
fmt = "%c%9.3f";
break;
case 1:
fmt = "%c%9.3g";
break;
case 2:
fmt = "%c%.14g";
break;
}
if( area[BKG][i] > 0 )
fprintf(stdout, fmt, cold, exp[BKG][i]/area[BKG][i]);
else
fprintf(stdout, fmt, cold, 0.0);
}
fprintf(stdout, "\n");
}
}
break;
}
fprintf(stdout, "\n");
fflush(stdout);
/* process the next interval, if necessary */
if( ++v < nintv ){
fprintf(stdout, "\014\n");
goto intvagain;
}
/* cleanup */
for(i=0; i<nintv; i++){
if( bncnts[i] ) xfree(bncnts[i]);
if( bnerr[i] ) xfree(bnerr[i]);
if( bscnts[i] ) xfree(bscnts[i]);
if( bserr[i] ) xfree(bserr[i]);
for(j=0; j<2; j++){
if( cnts[i][j] ) xfree(cnts[i][j]);
if( savecnts[i][j] ) xfree(savecnts[i][j]);
}
if( intvs[i] ) xfree(intvs[i]);
}
for(i=0; i<2; i++){
if( masks[i] ) xfree(masks[i]);
if( area[i] ) xfree(area[i]);
if( exp[i] ) xfree(exp[i]);
if( savearea[i] ) xfree(savearea[i]);
if( saveexp[i] ) xfree(saveexp[i]);
if( filtstr[i] ) xfree(filtstr[i]);
if( valname[i] ) xfree(valname[i]);
}
if( radang ) xfree(radang);
for(i=0; i<4; i++){
if( name[i] ){
xfree(name[i]);
if( fun[i] ) FunClose(fun[i]);
}
}
return(0);
}
|