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
|
/* File getpix.c
* March 17, 2021
* By Jessica Mink, Harvard-Smithsonian Center for Astrophysics)
* Send bug reports to jmink@cfa.harvard.edu
Copyright (C) 1996-2021
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 MAXFILES 2000
static int maxnfile = MAXFILES;
#define MAXNPIX 100;
static int maxnpix = MAXNPIX;
static void usage();
static int PrintPix();
static void procpix();
static char *RevMsg = "GETPIX WCSTools 3.9.7, 26 April 2022, Jessica Mink (jmink@cfa.harvard.edu)";
static int verbose = 0; /* verbose/debugging flag */
static int version = 0; /* If 1, print only program name and version */
static int nline = 10; /* Number of pixels displayed per line */
static int ndec = -1; /* Format in which to print pixels */
static int pixlabel = 0; /* If 1, label pixels in output */
static int gtcheck = 0; /* If 1, list pixels greater than gtval */
static int ltcheck = 0; /* If 1, list pixels less than ltval */
static int nopunct=0; /* If 1, print output with no punctuation */
static int printrange = 0; /* If 1, print range of values, not values */
static int printmean = 0; /* If 1, print mean of values, not values */
static int printname = 0; /* If 1, print file name */
static int bycol = 0; /* If 1, display column as line */
static double gtval = 0.0;
static double ltval = 0.0;
static double ra0 = -99.0; /* Initial center RA in degrees */
static double dec0 = -99.0; /* Initial center Dec in degrees */
static double rad0 = 0.0; /* Search box radius */
static double dra0 = 0.0; /* Search box width */
static double ddec0 = 0.0; /* Search box height */
static double eqcoor = 2000.0; /* Equinox of search center */
static int syscoor = 0; /* Input search coordinate system */
static int identifier = 0; /* If 1, identifier precedes x y in file */
static int printtab = 0; /* If 1, separate number with tabs */
static int procinit = 1; /* If 1, start mean and limits */
static char *extensions; /* Extension number(s) or name to read */
static char *extension; /* Extension number or name to read */
static char pformat[8]; /* If not null, format for pixels without leading % */
int
main (ac, av)
int ac;
char **av;
{
char *str, *str1;
char listfile[256];
char **fn;
char filename[256];
int ifile, nbytes;
FILE *flist;
char *xrange; /* Horizontal(x) range string */
char *yrange; /* Vertical(y) range string */
char *rstr;
char *dstr = NULL;
char rastr[32], decstr[32];
char *cstr;
int systemp;
int i, j;
int nch;
int npix = 0;
int nfile;
int ixnum, iynum;
int ixrange, iyrange;
char linebuff[1024];
char listname[256];
char *fext, *fcomma;
char *namext;
char *extroot = NULL;
char *extroot0;
char *lastroot = NULL;
char *line;
char xstr[16], ystr[16], temp[64];
int ix, iy;
int *xpix, *ypix;
int iline, nlines;
FILE *fd;
int *xp1, *yp1;
double x, y;
int nfext = 0;
int nrmax=10;
struct Range *erange = NULL;
nfile = 0;
fn = (char **)calloc (maxnfile, sizeof(char *));
xpix = (int *)calloc (maxnpix, sizeof (int));
ypix = (int *)calloc (maxnpix, sizeof (int));
pformat[0] = (char) 0;
/* 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();
}
xrange = NULL;
yrange = NULL;
/* crack arguments */
for (av++; --ac > 0; av++) {
str = *av;
ixnum = isnum (str);
ixrange = isrange (str);
if (ac > 0) {
str1 = *(av+1);
iynum = isnum (str1);
iyrange = isrange (str1);
}
else {
str1 = NULL;
iynum = 0;
iyrange = 0;
}
/* Command */
if (str[0] == '-') {
char c;
while ((c = *++str))
switch (c) {
case 'v': /* more verbosity */
verbose++;
break;
case 'd': /* Display this number of decimal places */
if (ac < 2)
usage();
ndec = atoi (*++av);
ac--;
break;
case 'e': /* Print range of values */
printrange=1;
break;
case 'g': /* Keep pixels greater than this */
if (ac < 2)
usage();
gtval = atof (*++av);
gtcheck++;
ac--;
break;
case 'h': /* Print file name above pixel values */
printname++;
break;
case 'i': /* Identifier precedes x y in file */
identifier++;
break;
case 'l': /* Keep pixels less than this */
if (ac < 2)
usage();
ltval = atof (*++av);
ltcheck++;
ac--;
break;
case 'm': /* Print mean, sigma of values */
printmean = 1;
break;
case 'n': /* Number of pixels per line */
if (ac < 2)
usage();
nline = atoi (*++av);
ac--;
break;
case 'o': /* Output pixel format without leading % */
if (ac < 2)
usage();
strncpy (pformat, *++av, 8);
ac--;
break;
case 'p': /* label pixels */
pixlabel++;
break;
case 'r': /* Box radius in arcseconds */
if (ac < 2)
usage ();
av++;
if ((dstr = strchr (*av, ',')) != NULL) {
*dstr = (char) 0;
dstr++;
}
if (strchr (*av,':'))
rad0 = 3600.0 * str2dec (*av);
else
rad0 = atof (*av);
if (dstr != NULL) {
dra0 = rad0;
rad0 = 0.0;
if (strchr (dstr, ':'))
ddec0 = 3600.0 * str2dec (dstr);
else
ddec0 = atof (dstr);
if (ddec0 <= 0.0)
ddec0 = dra0;
/* rad0 = sqrt (dra0*dra0 + ddec0*ddec0); */
}
ac--;
break;
case 's': /* Print x y value without punctuation */
nopunct++;
break;
case 't': /* Separate pixels with tabs */
printtab++;
break;
case 'y': /* Display by column instead of line */
bycol++;
break;
case 'x': /* FITS extension to read */
if (ac < 2)
usage();
if (isnum (*(av+1)) || isrange (*(av+1))) {
extroot = NULL;
extensions = *++av;
ac--;
}
else {
extroot = *++av;
lastroot = extroot + strlen (extroot);
extensions = extroot;
while (extensions < lastroot) {
if (isrange (extensions))
break;
else
extensions++;
}
if (extensions == lastroot) {
extensions = calloc (16, 1);
if (strlen (extroot) > 0)
strcpy (extensions, "1-1000");
else
strcpy (extensions, "0-1000");
}
else {
extroot0 = extroot;
extroot = calloc (16, 1);
strncpy (extroot, extroot0, extensions-extroot0);
}
ac--;
}
if (isrange (extensions)) {
erange = RangeInit (extensions, nrmax);
nfext = rgetn (erange);
if (verbose)
fprintf (stderr, "Searching extensions %s\n", extensions);
}
else {
extension = extensions;
if (extension)
nfext = 1;
else
nfext = 0;
if (verbose)
fprintf (stderr, "Searching extension %s\n", extension);
}
break;
default:
usage();
break;
}
}
/* Set search RA, Dec, and equinox if colon in argument */
else if (ixnum == 3 && iynum == 3) {
if (ac < 2) {
usage ();
}
else {
strcpy (rstr, str);
ac--;
av++;
strcpy (dstr, str1);
ra0 = str2ra (rstr);
dec0 = str2dec (dstr);
ac--;
if (ac < 1) {
syscoor = WCS_J2000;
eqcoor = 2000.0;
}
else if ((syscoor = wcscsys (*(av+1))) >= 0)
eqcoor = wcsceq (*++av);
else {
syscoor = WCS_J2000;
eqcoor = 2000.0;
}
}
}
/* Search coordinates in degrees if coordinate system specified */
else if (ixnum == 2 && iynum == 2) {
rstr = str;
dstr = str1;
ac--;
av++;
av++;
if (ac > 0 && (systemp = wcscsys (*av)) > 0) {
ra0 = atof (rstr);
dec0 = atof (dstr);
cstr = *av++;
syscoor = systemp;
eqcoor = wcsceq (cstr);
}
/* Fractional coordinate pair if no coordinate system */
else {
if (npix+1 > maxnpix) {
maxnpix = 2 * maxnpix;
xp1 = calloc (maxnpix, sizeof (int));
yp1 = calloc (maxnpix, sizeof (int));
for (i = 0; i < maxnpix; i++) {
xp1[i] = xpix[i];
yp1[i] = ypix[i];
}
free (xpix);
free (ypix);
xpix = xp1;
ypix = yp1;
}
x = atof (rstr);
if (x > 0.0)
ix = (int) (x + 0.5);
else if (x < 0.0)
ix = (int) (x - 0.5);
else
ix = 0;
y = atof (dstr);
if (y > 0.0)
iy = (int) (y + 0.5);
else if (y < 0.0)
iy = (int) (y - 0.5);
else
iy = 0;
xpix[npix] = ix;
ypix[npix] = iy;
npix++;
}
}
/* Ranges of x and y pixels to print (only one pair allowed) */
else if (ixrange && iynum || ixnum && iyrange || ixrange && iyrange) {
xrange = str;
yrange = str1;
ac--;
av++;
}
/* Two zeroes indicates that the entire image should be printed */
else if (!strcmp (str, "0") && !strcmp (str, "0")) {
xrange = str;
yrange = str1;
ac--;
av++;
}
/* Coordinate pairs for pixels to print */
else if (ixnum == 1 && iynum == 1) {
if (npix+1 > maxnpix) {
maxnpix = 2 * maxnpix;
xp1 = calloc (maxnpix, sizeof (int));
yp1 = calloc (maxnpix, sizeof (int));
for (i = 0; i < maxnpix; i++) {
xp1[i] = xpix[i];
yp1[i] = ypix[i];
}
free (xpix);
free (ypix);
xpix = xp1;
ypix = yp1;
}
ix = atoi (str);
iy = atoi (str1);
if (ix == 0 || iy == 0) {
xrange = str1;
yrange = str;
}
else {
xpix[npix] = ix;
ypix[npix] = iy;
npix++;
}
av++;
ac--;
}
/* File containing a list of files or image coordinates */
else if (str[0] == '@') {
strcpy (listname, str+1);
if (isimlist (listname)) {
strcpy (listfile, listname);
listname[0] = (char) 0;
}
else {
nlines = getfilelines (listname);
fd = fopen (listname, "r");
if (fd == NULL) {
fprintf (stderr, "GETPIX: Cannot read file %s\n", listname);
nlines = 0;
}
for (iline = 0; iline < nlines; iline++) {
if (!fgets (linebuff, 1023, fd))
break;
line = linebuff;
if (line[0] == '#')
continue;
if (identifier)
sscanf (line,"%s %s %s", temp, xstr, ystr);
else
sscanf (line,"%s %s", xstr, ystr);
if (npix+1 > maxnpix) {
maxnpix = 2 * maxnpix;
xp1 = calloc (maxnpix, sizeof (int));
yp1 = calloc (maxnpix, sizeof (int));
for (i = 0; i < maxnpix; i++) {
xp1[i] = xpix[i];
yp1[i] = ypix[i];
}
free (xpix);
free (ypix);
xpix = xp1;
ypix = yp1;
}
xpix[npix] = atoi (xstr);
ypix[npix] = atoi (ystr);
npix++;
}
}
ac--;
}
/* Image file name */
else if (isfits (str) || isiraf (str)) {
if (nfile >= maxnfile) {
maxnfile = maxnfile * 2;
nbytes = maxnfile * sizeof (char *);
fn = (char **) realloc ((void *)fn, nbytes);
}
fn[nfile] = str;
nfile++;
}
}
if ((xrange && yrange) || npix > 0) {
/* Process files already read from the command line */
if (nfile) {
for (ifile = 0; ifile < nfile; ifile++) {
strcpy (filename, fn[ifile]);
if (nfext > 1) {
rstart (erange);
extension = calloc (1, 8);
for (i = 0; i < nfext; i++) {
j = rgeti4 (erange);
sprintf (extension, "%d", j);
nch = strlen (filename) + 2 + strlen (extension);
if (extroot)
nch = nch + strlen (extroot);
namext = (char *) calloc (1, nch);
strcpy (namext, filename);
strcat (namext, ",");
if (extroot)
strcat (namext,extroot);
strcat (namext, extension);
if (PrintPix (namext, xrange, yrange, npix, xpix, ypix)) {
if (namext != NULL) {
free (namext);
namext = NULL;
}
break;
}
if (namext != NULL) {
free (namext);
namext = NULL;
}
}
if (extension != NULL) {
free (extension);
extension = NULL;
}
/* if (erange != NULL)
free (erange); */
}
else
PrintPix (filename, xrange, yrange, npix, xpix, ypix);
}
}
/* Process files from listfile one at a time */
else if (isimlist (listfile)) {
nfile = getfilelines (listfile);
if ((flist = fopen (listfile, "r")) == NULL) {
fprintf (stderr,"GETPIX: Image list file %s cannot be read\n",
listfile);
usage ();
}
for (ifile = 0; ifile < nfile; ifile++) {
first_token (flist, 254, filename);
if (nfext > 1) {
rstart (erange);
extension = calloc (1, 8);
for (i = 0; i < nfext; i++) {
j = rgeti4 (erange);
sprintf (extension, "%d", j);
nch = strlen (filename) + 2 + strlen (extension);
if (extroot)
nch = nch + strlen (extroot);
namext = (char *) calloc (1, nch);
strcpy (namext, filename);
strcat (namext, ",");
if (extroot)
strcat (namext,extroot);
strcat (namext, extension);
if (PrintPix (namext, xrange, yrange, npix, xpix, ypix)) {
if (namext != NULL) {
free (namext);
namext = NULL;
}
break;
}
if (namext != NULL) {
free (namext);
namext = NULL;
}
}
if (extension != NULL) {
free (extension);
extension = NULL;
}
}
else
PrintPix (filename, xrange, yrange, npix, xpix, ypix);
}
fclose (flist);
}
}
free (xpix);
free (ypix);
free (fn);
return (0);
}
static void
usage ()
{
fprintf (stderr,"%s\n",RevMsg);
if (version)
exit (-1);
fprintf (stderr,"Print FITS or IRAF pixel values\n");
fprintf(stderr,"Usage: getpix [-vp][-n num][-g val][-l val] file.fits x_range y_range\n");
fprintf(stderr," or getpix [-vp][-n num][-g val][-l val] file.fits x1 y1 x2 y2 ... xn yn\n");
fprintf(stderr," or getpix [-vp][-n num][-g val][-l val] file.fits @file\n");
fprintf(stderr," file: File with x y coordinates as first two tokens on lines\n");
fprintf(stderr," -d: Number of decimal places in displayed pixel values\n");
fprintf(stderr," -e: Print range of pixel values in specified image region\n");
fprintf(stderr," -f name: Write specified region to a FITS file\n");
fprintf(stderr," -g num: keep pixels with values greater than this\n");
fprintf(stderr," -h: print file name on line above pixel values\n");
fprintf(stderr," -i: Ignore first token per line of coordinate file\n");
fprintf(stderr," -l num: keep pixels with values less than this\n");
fprintf(stderr," -m: Print mean of pixel values in specified image region\n");
fprintf(stderr," -n num: number of pixel values printed per line\n");
fprintf(stderr," -o format: Output format in C style without leading %%\n");
fprintf(stderr," -p: label pixels\n");
fprintf(stderr," -r num: radius (<0=box) to extract in degrees/arcsec\n");
fprintf(stderr," -s: print x y value with no punctuation\n");
fprintf(stderr," -t: separate columns in table with tabs not spaces \n");
fprintf(stderr," -v: verbose\n");
fprintf(stderr," -x [range]: Read header for these extensions (no arg=all)\n");
fprintf(stderr," -y: display by column instead of by row\n");
exit (1);
}
static int
PrintPix (name, xrange, yrange, npix, xpix, ypix)
char *name;
char *xrange; /* Horizontal(x) range string */
char *yrange; /* Vertical(y) range string */
int npix; /* Number of coordinate pairs */
int *xpix, *ypix; /* Vectors of x,y coordinate pairs */
{
char *header; /* FITS image header */
char pform[8]; /* Pixel display format */
char testval[32]; /* Test value string for column header formatting */
int lhead; /* Maximum number of bytes in FITS header */
int nbhead; /* Actual number of bytes in FITS header */
char *irafheader; /* IRAF image header */
char *image; /* FITS or IRAF image */
double bzero; /* Zero point for pixel scaling */
double bscale; /* Scale factor for pixel scaling */
int iraffile;
int lf;
int intout = 0; /* Display output pixel values as integer if =1 */
double dpix, dsum, dmean, dmin, dmax, dnpix;
char *c;
int *yi, *xi;
int bitpix, xdim, ydim, i, nx, ny, ix, iy, x, y, x1, y1, pixperline, ndig;
int ipix = 0;
char pixname[255];
char cform[8], rform[8];
struct Range *crange; /* Column (nominally x) range structure */
struct Range *rrange; /* Row (nominally y) range structure */
/* Open IRAF image if .imh extension is present */
if (isiraf (name)) {
iraffile = 1;
if ((irafheader = irafrhead (name, &lhead)) != NULL) {
header = iraf2fits (name, irafheader, lhead, &nbhead);
free (irafheader);
if (header == NULL) {
fprintf (stderr, "Cannot translate IRAF header %s/n",name);
return (1);
}
if ((image = irafrimage (header)) == NULL) {
hgetm (header,"PIXFIL", 255, pixname);
fprintf (stderr, "Cannot read IRAF pixel file %s\n", pixname);
free (irafheader);
free (header);
return (1);
}
}
else {
fprintf (stderr, "Cannot read IRAF file %s\n", name);
return (1);
}
}
/* Open FITS file if .imh extension is not present */
else {
iraffile = 0;
if ((header = fitsrhead (name, &lhead, &nbhead)) != NULL) {
if ((image = fitsrimage (name, nbhead, header)) == NULL) {
fprintf (stderr, "Cannot read FITS image %s\n", name);
free (header);
return (1);
}
}
else {
fprintf (stderr, "Cannot read FITS file %s\n", name);
return (1);
}
}
if (printname) {
if (ltcheck & gtcheck)
fprintf (stderr, "%s: %f < pixel values < %f\n", name, gtval, ltval);
else if (ltcheck)
fprintf (stderr, "%s: pixel values < %f\n", name, ltval);
else if (gtcheck)
fprintf (stderr, "%s: pixel values > %f\n", name, gtval);
else
fprintf (stderr,"%s:\n", name);
}
/* Get size of image and scaling factors */
bitpix = 32;
hgeti4 (header,"BITPIX",&bitpix);
xdim = 1;
hgeti4 (header,"NAXIS1",&xdim);
ydim = 1;
hgeti4 (header,"NAXIS2",&ydim);
bzero = 0.0;
hgetr8 (header,"BZERO",&bzero);
bscale = 1.0;
hgetr8 (header,"BSCALE",&bscale);
if (verbose) {
fprintf (stderr,"%s\n",RevMsg);
if (npix > 0)
fprintf (stderr,"Print pixels of ");
else if (!strcmp (xrange, "0") && !strcmp (yrange, "0"))
fprintf (stderr,"Print x 1-%d columns, y 1-%d rows of ", xdim, ydim);
else if (!strcmp (xrange, "0"))
fprintf (stderr,"Print x 1-%d columns, y %s rows of ", xdim, yrange);
else if (bycol && !strcmp (yrange, "0"))
fprintf (stderr,"Print y 1-%d columns, x %s rows of ", ydim, xrange);
else if (!strcmp (yrange, "0"))
fprintf (stderr,"Print x %s columns, y 1-%d rows of ", xrange, ydim);
else if (bycol)
fprintf (stderr,"Print y %s columns , x %s rows of ", yrange, xrange);
else
fprintf (stderr,"Print x %s rows, y %s columns of ", xrange, yrange);
if (iraffile)
fprintf (stderr,"IRAF image file %s\n", name);
else
fprintf (stderr,"FITS image file %s\n", name);
if (ltcheck & gtcheck)
fprintf (stderr, "%f < pixel values < %f\n", gtval, ltval);
else if (ltcheck)
fprintf (stderr, "pixel values < %f\n", ltval);
else if (gtcheck)
fprintf (stderr, "pixel values > %f\n", gtval);
}
if (verbose && !pixlabel)
pixperline = 1;
else
pixperline = 0;
/* Set up pixel access and display */
/* Set initial values */
dsum = 0.0;
dnpix = 0.0;
dmin = 0.0;
dmax = 0.0;
procinit = 1;
/* Set format if not already set */
for (i = 0; i < 8; i++)
pform[i] = (char) 0;
if (pformat[0] != (char) 0) {
sprintf (pform, "%%%s", pformat);
lf = strlen (pformat);
if (pformat[lf-1] == 'd')
intout = 1;
}
else if (ndec == 0) {
sprintf (pform, "%%d");
intout = 1;
}
else if (ndec > 0) {
sprintf (pform, "%%.%df", ndec);
intout = 0;
}
else if (bitpix > 0) {
sprintf (pform, "%%d");
intout = 1;
}
else {
sprintf (pform, "%%.2f");
intout = 0;
}
/* Print values at specified coordinates in an image */
if (npix > 0) {
/* Loop through rows starting with the last one */
for (i = 0; i < npix; i++) {
dpix = getpix1(image,bitpix,xdim,ydim,bzero,bscale,xpix[i],ypix[i]);
if (gtcheck || ltcheck) {
if ((gtcheck && dpix > gtval) ||
(ltcheck && dpix < ltval)) {
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
if (intout) {
if (dpix > 0)
ipix = (int) (dpix + 0.5);
else if (dpix < 0)
ipix = (int) (dpix - 0.5);
else
ipix = 0;
}
if (nopunct)
printf ("%d %d \n", xpix[i], ypix[i]);
else
printf ("[%d,%d] = \n", xpix[i], ypix[i]);
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
printf ("\n");
}
continue;
}
else
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
if (printrange || printmean)
continue;
if (intout) {
if (dpix > 0)
ipix = (int) (dpix + 0.5);
else if (dpix < 0)
ipix = (int) (dpix - 0.5);
else
ipix = 0;
}
if (pixperline) {
printf ("%s[%d,%d] = ",name,xpix[i],ypix[i]);
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
printf ("\n");
}
else {
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
if ((i+1) % nline == 0)
printf ("\n");
else if (printtab)
printf ("\t");
else
printf (" ");
}
}
if (!pixperline && !ltcheck && !gtcheck)
printf ("\n");
}
/* Print entire image */
else if (!strcmp (xrange, "0") && !strcmp (yrange, "0")) {
if (printmean || printrange) {
nx = xdim;
ny = ydim;
for (y = 0; y < ny; y++) {
for (x = 0; x < nx; x++) {
dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
if (!gtcheck && !ltcheck)
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
else if (gtcheck && ltcheck) {
if (dpix > gtval && dpix < ltval)
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
}
else if ((gtcheck && dpix > gtval) ||
(ltcheck && dpix < ltval))
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
}
}
}
else if (gtcheck || ltcheck) {
nx = xdim;
ny = ydim;
for (y = 0; y < ny; y++) {
y1 = y + 1;
for (x = 0; x < nx; x++) {
x1 = x + 1;
dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
if (gtcheck && ltcheck) {
if (dpix > gtval && dpix < ltval) {
if (nopunct)
printf ("%d %d %f\n", x1, y1, dpix);
else
printf ("[%d,%d] = %f\n", x1, y1, dpix);
}
}
else if ((gtcheck && dpix > gtval) ||
(ltcheck && dpix < ltval)) {
if (nopunct)
printf ("%d %d %f\n", x1, y1, dpix);
else
printf ("[%d,%d] = %f\n", x1, y1, dpix);
}
}
}
}
else
printf ("GETPIX will not print this %d x %d image; use ranges\n",
xdim, ydim);
}
/* Print entire columns */
else if (!strcmp (yrange, "0")) {
/* Make list of x coordinates */
crange = RangeInit (xrange, xdim);
nx = rgetn (crange);
xi = (int *) calloc (nx, sizeof (int));
for (i = 0; i < nx; i++) {
xi[i] = rgeti4 (crange);
}
ny = ydim;
for (y = 0; y < ny; y++) {
y1 = y + 1;
for (ix = 0; ix < nx; ix++) {
x = xi[ix];
x1 = x + 1;
dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
if (gtcheck || ltcheck) {
if ((gtcheck && dpix > gtval) ||
(ltcheck && dpix < ltval)) {
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
if (nopunct)
printf ("%d %d %f\n", x1, y1, dpix);
else
printf ("[%d,%d] = %f\n", x1, y1, dpix);
}
continue;
}
else
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
if (intout) {
if (dpix > 0)
ipix = (int) (dpix + 0.5);
else if (dpix < 0)
ipix = (int) (dpix - 0.5);
else
ipix = 0;
}
if (pixperline) {
printf ("%s[%d,%d] = ",name,x,y);
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
printf ("\n");
}
else {
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
if ((y1) % nline == 0)
printf ("\n");
else if (printtab)
printf ("\t");
else
printf (" ");
}
}
if (y % nline != 0 && !gtcheck && !ltcheck)
printf ("\n");
if (nx > 1 && !gtcheck && !ltcheck)
printf ("\n");
}
free (crange);
}
/* Print entire rows */
else if (!strcmp (xrange, "0")) {
/* Make list of y coordinates */
rrange = RangeInit (yrange, ydim);
ny = rgetn (rrange);
yi = (int *) calloc (ny, sizeof (int));
for (i = 0; i < ny; i++) {
yi[i] = rgeti4 (rrange);
}
nx = xdim;
for (x = 0; x < nx; x++) {
for (iy = 0; iy < ny; iy++) {
y = yi[iy];
dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
if (gtcheck || ltcheck) {
if ((gtcheck && dpix > gtval) ||
(ltcheck && dpix < ltval)) {
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
if (nopunct)
printf ("%d %d %f\n", x+1, y+1, dpix);
else
printf ("[%d,%d] = %f\n", x+1, y+1, dpix);
}
continue;
}
else
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
if (intout) {
if (dpix > 0)
ipix = (int) (dpix + 0.5);
else if (dpix < 0)
ipix = (int) (dpix - 0.5);
else
ipix = 0;
}
if (pixperline) {
printf ("%s[%d,%d] = ",name,x,y);
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
printf ("\n");
}
else {
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
if ((x+1) % nline == 0)
printf ("\n");
else if (printtab)
printf ("\t");
else
printf (" ");
}
}
if (x % nline != 0 && !gtcheck && !ltcheck)
printf ("\n");
if (ny > 1 && !gtcheck && !ltcheck)
printf ("\n");
}
free (rrange);
}
/* Print a region of a two-dimensional image sideways */
else if (bycol) {
/* Make list of x coordinates */
rrange = RangeInit (xrange, xdim);
nx = rgetn (rrange);
xi = (int *) calloc (nx, sizeof (int));
for (i = 0; i < nx; i++) {
xi[i] = rgeti4 (rrange);
}
/* Make list of x coordinates */
crange = RangeInit (yrange, xdim);
ny = rgetn (crange);
yi = (int *) calloc (ny, sizeof (int));
for (i = 0; i < ny; i++) {
yi[i] = rgeti4 (crange);
}
/* Set pixel label formats */
if (pixlabel) {
/* Format for row pixel labels */
x = xi[nx-1];
sprintf (testval, "%d", x);
ndig = strlen (testval);
if (printtab) {
sprintf (rform, "%%%dd:\t", ndig);
}
else{
sprintf (rform, "%%%dd: ", ndig);
}
/* Format for column pixel labels */
sprintf (testval, pform, 1.12345678);
ndig = strlen (testval);
y = yi[ny-1];
sprintf (testval, "%d", y);
ndig = ndig + strlen (testval);
if (printtab) {
sprintf (cform, "%%%dd\t", ndig);
}
else{
sprintf (cform, "%%%dd ", ndig);
}
/* Label column pixels */
printf ("Coord");
for (iy = 0; iy < ny; iy++) {
y = yi[iy];
printf (cform, y);
}
printf ("\n");
}
/* Loop through rows */
for (ix = 0; ix < nx; ix++) {
x1 = xi[ix];
x = x1 - 1;
if (pixlabel) {
printf (rform, x1);
}
/* Loop through columns */
for (iy = 0; iy < ny; iy++) {
y1 = yi[iy];
y = y1 - 1;
dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
if (gtcheck || ltcheck) {
if ((gtcheck && dpix > gtval) ||
(ltcheck && dpix < ltval)) {
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
if (nopunct) {
printf ("%d %d %f\n", x1, y1, dpix);
}
else {
printf ("[%d,%d] = %f\n", x1, y1, dpix);
}
}
continue;
}
else {
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
}
if (printrange || printmean) {
continue;
}
if (intout) {
if (dpix > 0)
ipix = (int) (dpix + 0.5);
else if (dpix < 0)
ipix = (int) (dpix - 0.5);
else
ipix = 0;
}
if (pixperline) {
printf ("%s[%d,%d] = ", name, x1, y1);
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
printf ("\n");
}
else {
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
if ((iy+1) % nline == 0)
printf ("\n");
else if (printtab)
printf ("\t");
else
printf (" ");
}
}
if (!pixperline && !ltcheck && !gtcheck) {
if (!printrange && !printmean && iy % nline != 0)
printf ("\n");
}
}
free (rrange);
free (crange);
}
/* Print a region of a two-dimensional image */
else {
/* Make list of x coordinates */
crange = RangeInit (xrange, xdim);
nx = rgetn (crange);
xi = (int *) calloc (nx, sizeof (int));
for (i = 0; i < nx; i++) {
xi[i] = rgeti4 (crange);
}
/* Make list of y coordinates */
rrange = RangeInit (yrange, ydim);
ny = rgetn (rrange);
yi = (int *) calloc (ny, sizeof (int));
for (i = 0; i < ny; i++) {
yi[i] = rgeti4 (rrange);
}
/* Set pixel label formats */
if (pixlabel) {
/* Format for row pixel labels */
x = xi[nx-1];
sprintf (testval, "%d", x);
ndig = strlen (testval);
sprintf (rform, "%%%dd:", ndig);
if (printtab) {
sprintf (rform, "%%%dd:\t", ndig);
}
else{
sprintf (rform, "%%%dd: ", ndig);
}
/* Format for column pixel labels */
sprintf (testval, pform, 1.12345678);
ndig = strlen (testval);
y = yi[ny-1];
sprintf (testval, "%d", y);
ndig = ndig + strlen (testval);
if (printtab) {
sprintf (cform, "%%%dd\t", ndig);
}
else{
sprintf (cform, "%%%dd ", ndig);
}
/* Label column pixels */
printf ("Coord");
for (ix = 0; ix < nx; ix++) {
x = xi[ix];
printf (cform, x);
}
printf ("\n");
}
if (pixperline)
iy = -1;
else
iy = ny;
/* Loop through rows starting with the last one */
for (i = 0; i < ny; i++) {
if (pixperline)
iy++;
else
iy--;
y1 = yi[iy];
y = y1 - 1;
if (pixlabel) {
printf (rform, y1);
}
/* Loop through columns */
for (ix = 0; ix < nx; ix++) {
x1 = xi[ix];
x = x1 - 1;
dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
if (gtcheck || ltcheck) {
if ((gtcheck && dpix > gtval) ||
(ltcheck && dpix < ltval)) {
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
if (nopunct)
printf ("%d %d %f\n", x1, y1, dpix);
else
printf ("[%d,%d] = %f\n", x1, y1, dpix);
}
continue;
}
else {
procpix (&dsum, &dnpix, &dmin, &dmax, dpix);
}
if (printrange || printmean)
continue;
if (intout) {
if (dpix > 0)
ipix = (int) (dpix + 0.5);
else if (dpix < 0)
ipix = (int) (dpix - 0.5);
else
ipix = 0;
}
if (pixperline) {
printf ("%s[%d,%d] = ", name, x1, y1);
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
printf ("\n");
}
else {
if (intout)
printf (pform, ipix);
else
printf (pform, dpix);
if ((ix+1) % nline == 0)
printf ("\n");
else if (printtab)
printf ("\t");
else
printf (" ");
}
}
if (!pixperline && !ltcheck && !gtcheck) {
if (!printrange && !printmean && ix % nline != 0)
printf ("\n");
}
}
free (rrange);
free (crange);
}
if (printmean) {
dmean = dsum / dnpix;
if (verbose)
printf ("Mean= %.4f ", dmean);
else
printf ("%.4f", dmean);
}
if (printrange) {
if (printmean)
printf (" ");
if (verbose)
printf ("Range = %.4f - %.4f ", dmin, dmax);
else
printf ("%.4f %.4f", dmin, dmax);
}
if (printmean || printrange) {
if (verbose)
printf ("for %d pixels\n", (int) dnpix);
else
printf ("\n");
}
free (header);
free (image);
return (0);
}
static void
procpix (dsum, dnpix, dmin, dmax, dpix)
double *dsum; /* Sum of pixel values */
double *dnpix; /* Number of pixels so far */
double *dmin; /* Minimum pixel value */
double *dmax; /* Maximum pixel value */
double dpix; /* Current pixel value */
{
if (procinit) {
*dsum = dpix;
*dnpix = 1.0;
*dmin = dpix;
*dmax = dpix;
}
else {
*dsum = *dsum + dpix;
*dnpix = *dnpix + 1.0;
if (dpix < *dmin)
*dmin = dpix;
else if (dpix > *dmax)
*dmax = dpix;
}
procinit = 0;
}
/* Dec 6 1996 New program
*
* Feb 21 1997 Check pointers against NULL explicitly for Linux
* Dec 15 1997 Add capability of reading and writing IRAF 2.11 images
*
* May 27 1998 Include fitsio.h instead of fitshead.h
* Jul 24 1998 Make irafheader char instead of int
* Aug 6 1998 Change fitsio.h to fitsfile.h
* Oct 14 1998 Use isiraf() to determine file type
* Nov 30 1998 Add version and help commands for consistency
*
* Feb 12 1999 Initialize dxisn to 1 so it works for 1-D images
* Apr 29 1999 Add BZERO and BSCALE
* Jun 29 1999 Fix typo in BSCALE setting
* Jul 2 1999 Use ranges instead of individual pixels
* Oct 15 1999 Fix format statement
* Oct 22 1999 Drop unused variables after lint
* Dec 9 1999 Add -g -l limits
* Dec 13 1999 Fix bug so that -g and -l limits can be ANDed
*
* Mar 23 2000 Use hgetm() to get the IRAF pixel file name, not hgets()
*
* Jan 30 2001 Fix format specification in help message
*
* Jun 3 2002 Add -s option to print x y value with no punctuation
* Oct 30 2002 Add code to count lines when printing a region
*
* Feb 20 2003 Add option to enter multiple pixel (x,y) as well as ranges
* Mar 26 2003 Fix pixel counter bug in individual pixel printing
* Sep 17 2003 Fix bug which broke use of 0 as substitute for 1-naxisn range
*
* Apr 26 2004 Fix handling of 0 0 for entire image
* Aug 30 2004 Fix declarations
* Sep 21 2004 Fix bug which used x instead of ix for number of elements printed
*
* Jul 29 2005 Add mean and range computation
*
* Jun 21 2006 Clean up code
*
* Jan 10 2007 Declare RevMsg static, not const
* Dec 20 2007 Add option to read x y coordinates from a file
* Dec 21 2007 Fix bug reallocating coordinate list when current size exceeded
*
* Aug 14 2009 If coordinates are floating point round to appropriate pixel
*
* Sep 21 2010 Add option -t to separate numbers by tabs
* Sep 21 2010 Fix bug in computing means and limits
*
* Feb 22 2012 Print descriptors for mean and limits only in verbose mode
* Feb 22 2012 Fix bug to avoid printing all pixels if printing mean and/or limits
*
* Jan 10 2014 Get same pixels from multiple files
* Jan 10 2014 Add command line option @listfile as list of files
*
* Jun 9 2016 Fix isnum() tests for added coloned times and dashed dates
*
* May 12 2020 Reverse column and row assignment in range interpretation (Steve Willner)
* May 12 2020 Change range of values to -e and add -d for decimal places
* May 12 2020 Add -y to print columns as rows
* May 14 2020 Clean up distinctions between image x and y and output columns and rows
* May 14 2020 Add -o [format] to allow any legal C format for pixel display
*
* Feb 10 2021 Add -x to read specified extension(s) from every input file
* Mar 17 2021 Fix bug when reading filename(s) from command line
*/
|