File: sumpix.c

package info (click to toggle)
wcstools 3.9.5-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,116 kB
  • sloc: ansic: 104,402; sh: 511; makefile: 239; lisp: 86; sed: 1
file content (842 lines) | stat: -rw-r--r-- 20,004 bytes parent folder | download
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
/* File sumpix.c
 * June 9, 2016
 * By Jessica Mink Harvard-Smithsonian Center for Astrophysics)
 * Send bug reports to jmink@cfa.harvard.edu

   Copyright (C) 1999-2016
   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;

static void usage();
static void SumPix();
extern double PhotPix();

static char *RevMsg = "SUMPIX WCSTools 3.9.5, 30 March 2017, 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 complim = 0;	/* If 1, compute limiting values over range */
static int compsum = 0;	/* If 1, compute sum over range */
static int compmean = 0;/* If 1, compute mean over range */
static int compmed = 0;/* If 1, compute median over range */
static int compvar = 0;	/* If 1, compute variance over range */
static int compstd = 0;	/* If 1, compute standard deviation over range */
static int compphot = 0; /* If 1, compute photometry in circular aperture */
static int printindex = 0; /* If 1, print row and/or column */
static int printhead = 0; /* If 1, print column headers for output values */
static int printtab = 0; /* If 1, tab-separate values */
static char *xstr;	/* X coordinate of center of aperture */
static char *ystr;	/* Y coordinate of center of aperture */
static double rad;	/* Radius of aperture in pixels */
static int printfile = 0;/* If 1, print file name at start of line */
static int ndec = -1;	/* Number of decimal places in outout */

int
main (ac, av)
int ac;
char **av;
{
    char *str;
    char *listfile = NULL;
    char **fn;
    char filename[256];
    int ifile, nbytes;
    FILE *flist;

    char *rrange;	/* Row range string */
    char *crange;	/* Column range string */
    char *zrange;

    int nfile = 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();
	}

    crange = NULL;
    rrange = NULL;
    zrange = (char *)calloc (4, sizeof(char));
    strcpy (zrange, "0");
    fn = (char **)calloc (maxnfile, sizeof(char *));

    /* crack arguments */
    for (av++; --ac > 0; av++) {
	str = *av;
	if (str[0] == '-') {
	    char c;
	    while ((c = *++str))
	    switch (c) {

		case 'c':	/* Compute circular aperture */
		    if (ac < 4)
			usage();
		    xstr = *++av;
		    ac--;
		    ystr = *++av;
		    ac--;
		    rad = atof (*++av);
		    ac--;
		    compstd++;
		    break;

		case 'd':	/* Compute standard deviation */
		    compstd++;
		    break;

		case 'e':	/* Compute median */
		    compmed++;
		    break;

		case 'h':	/* Print column headers for output values */
		    printhead++;
		    break;

		case 'i':	/* Print index */
		    printindex++;
		    break;

		case 'l':	/* Compute limits */
		    complim++;
		    break;

		case 'm':	/* Compute mean */
		    compmean++;
		    break;

		case 'n': /* Number of decimal places in output */
		    if (ac < 2)
			usage();
		    ndec = (int) (atof (*++av));
		    ac--;
		    break;

		case 'p':	/* Print filename at start of line */
		    printfile++;
		    break;

		case 'r':	/* Compute variance */
		    compvar++;
		    break;

		case 's':	/* Compute sum */
		    compsum++;
		    break;

		case 't':	/* Tab-separate values */
		    printtab++;
		    printhead++;
		    break;

		case 'v':	/* more verbosity */
		    verbose++;
		    break;

		default:
		    usage();
		    break;
		}
	    }

	/* Read range in x or y */
        else if (isnum (*av) == 1 || isrange (*av)) {
	    if (crange == NULL)
		crange = *av;
	    else
		rrange = *av;
	    }

	/* File with list of image files */
	else if (*av[0] == '@') {
	    listfile = *av + 1;
	    }

        /* Image file */
        else if (isfits (*av) || isiraf (*av)) {
	    if (nfile >= maxnfile) {
		maxnfile = maxnfile * 2;
		nbytes = maxnfile * sizeof (char *);
		fn = (char **) realloc ((void *)fn, nbytes);
		}
	    fn[nfile] = *av;
	    nfile++;
	    }
        }

    if (crange == NULL)
	crange = zrange;
    if (rrange == NULL)
	rrange = zrange;
    if (!compmean && !compvar && !compstd)
	compsum++;

    /* Process files from listfile one at a time */
    if (listfile && isimlist (listfile)) {
	nfile = getfilelines (listfile);
	if ((flist = fopen (listfile, "r")) == NULL) {
	    fprintf (stderr,"SUMPIX: Image list file %s cannot be read\n",
		     listfile);
	    usage ();
	    }
	for (ifile = 0; ifile < nfile; ifile++) {
	    first_token (flist, 254, filename);
	    SumPix (filename, crange, rrange);
	    }
	fclose (flist);
	}

    /* Process files already read from the command line */
    else if (fn) {
	for (ifile = 0; ifile < nfile; ifile++) {
	    SumPix (fn[ifile], crange, rrange);
	    }
	}

    return (0);
}

static void
usage ()
{
    fprintf (stderr,"%s\n",RevMsg);
    if (version)
	exit (-1);
    fprintf (stderr,"Sum row, column, or region of a FITS or IRAF image\n");
    fprintf(stderr,"Usage: sumpix [-dmrsv][-n num] x_range  y_range file.fit ...\n");
    fprintf(stderr,"  -c x y r: Compute total counts in circle r from (x,y)\n");
    fprintf(stderr,"  -d: Compute and print standard deviation\n");
    fprintf(stderr,"  -e: Compute and print median\n");
    fprintf(stderr,"  -h: Print column headers for output values\n");
    fprintf(stderr,"  -i: Print row or column number if other is 0\n");
    fprintf(stderr,"  -l: Compute and print min and max values\n");
    fprintf(stderr,"  -m: Compute and print mean\n");
    fprintf(stderr,"  -n: Number of decimal places in output\n");
    fprintf(stderr,"  -p: Print file name at start of line\n");
    fprintf(stderr,"  -r: Compute and print variance (sum of squares)\n");
    fprintf(stderr,"  -s: Compute and print sum (default)\n");
    fprintf(stderr,"  -t: Separate output values with tabs and add column headers\n");
    fprintf(stderr,"  -v: Verbose\n");
    fprintf(stderr,"  a range of 0 implies the full dimension\n");
    fprintf(stderr,"  an absence of ranges uses the entire image\n");
    exit (1);
}


static void
SumPix (name, crange, rrange)

char *name;	/* FITS or IRAF .imh file name */
char *crange;	/* Column range string */
char *rrange;	/* Row range string */

{
    char *header;	/* FITS image header */
    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 */
    double counts;
    double wt;
    double medpix();
    int iraffile;
    double dpix, sum;
    double ra, dec, xp, yp;
    int bitpix,xdim,ydim;
    int nx, ny, ix, iy, x, y;
    int offscl;
    int np;
    double dnp, mean, variance, std, sumsq, dmin, dmax;
    double xmax, ymax, median, medpix();
    char pixname[256];
    char numform[8];
    char numforme[8];
    struct Range *xrange = NULL;    /* X range structure */
    struct Range *yrange = NULL;    /* Y range structure */
    struct WorldCoor *wcs, *GetWCSFITS();

    /* 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;
		}
	    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;
		}
	    }
	else {
	    fprintf (stderr, "Cannot read IRAF file %s\n", name);
	    return;
	    }
	}

    /* 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;
		}
	    }
	else {
	    fprintf (stderr, "Cannot read FITS file %s\n", name);
	    return;
	    }
	}
    if (verbose) {
	fprintf (stderr,"%s\n",RevMsg);
	if (!strcmp (crange, "0"))
	    fprintf (stderr,"Sum rows %s in ", rrange);
	else if (!strcmp (rrange, "0"))
	    fprintf (stderr,"Sum columns %s in ", crange);
	else
	    fprintf (stderr,"Sum rows %s, columns %s in ", rrange, crange);
	if (iraffile)
	    fprintf (stderr,"IRAF image file %s\n", name);
	else
	    fprintf (stderr,"FITS image file %s\n", name);
	}

    if (ndec > -1) {
	sprintf (numform, "%%.%df", ndec);
	sprintf (numforme, "%%.%df", ndec);
	}
    else {
	sprintf (numform, "%%.2f");
	sprintf (numforme, "%%.2f");
	}

/* Get information about the image */
    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 (printfile)
	printf ("%s ", name);

    /* Compute counts in circular area */
    if (compphot) {
	if (strchr (xstr, ':') != NULL || strchr (ystr, ':') != NULL) {
	    wcs = GetWCSFITS (name, header, verbose);
	    ra = str2ra (xstr);
	    dec = str2dec (ystr);
	    wcs2pix (wcs, ra, dec, &xp, &yp, &offscl);
	    if (verbose)
		printf ("%s %s (%.3f,%.3f) %.1f = ", xstr, ystr, xp, yp, rad);
	    }
	else {
	    xp = atof (xstr);
	    yp = atof (ystr);
	    if (verbose)
		printf ("(%.3f,%.3f) %.1f = ", xp, yp, rad);
	    }
	counts = PhotPix (image, header, xp, yp, rad, &wt);
	printf ("%f", counts);
	if (verbose)
	    printf (" (%f pixels)", wt);
	printf ("\n");
	}

    /* Sum entire image */
    else if (!strcmp (crange, "0") && !strcmp (rrange, "0")) {
	nx = xdim;
	ny = ydim;
	if (compsum || complim) {
	    sum = 0.0;
	    sumsq = 0.0;
	    np = 0;
	    dmin = 1000000000000000.0;
	    dmax = -10000000000000000.0;
	    for (x = 0; x < nx; x++) {
		for (y = 0; y < ny; y++) {
     		    dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
		    sum = sum + dpix;
		    sumsq = sumsq + (dpix * dpix);
		    if (dpix < dmin)
			dmin = dpix;
		    if (dpix > dmax)
			dmax = dpix;
		    np++;
		    }
		}
	    }
	dnp = (double) np;
	if (compsum)
	    printf (numform, sum);
	mean = sum / dnp;
	if (compmean)
	    printf (numform, mean);
	variance = sumsq;
	if (compvar)
	    printf (numform, variance);
	if (compstd) {
	    std = sqrt ((sumsq / dnp) - (mean * mean));
	    printf (numforme, std);
	    }
	if (complim) {
	    printf (" ");
	    printf (numform, dmin);
	    printf ("- ");
	    printf (numform, dmax);
	    }
	printf ("\n");
	}

    /* Sum entire columns */
    else if (!strcmp (rrange, "0")) {
	xrange = RangeInit (crange, xdim);
	nx = rgetn (xrange);
	ny = ydim;
	xmax = xrange->valmax;
	if (printhead) {
	    if (printindex) {
		printf ("i");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compmed) {
		printf ("median");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compsum) {
		printf ("sum");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compmean) {
		printf ("mean");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compvar) {
		printf ("var");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compstd) {
		printf ("stdev");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (complim) {
		printf ("min");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		printf ("max");
		}
	    printf ("\n");
	    if (printtab) {
		if (printindex) 
		    printf ("-----	");
		if (compmed)
		    printf ("------	");
		if (compsum)
		    printf ("---	");
		if (compmean)
		    printf ("----	");
		if (compvar)
		    printf ("---	");
	    	if (compstd)
		    printf ("-----	");
		if (complim)
		    printf ("---	---");
		printf ("\n");
		}
	    }
	for (ix = 0; ix < nx; ix++) {
	    x = rgeti4 (xrange) - 1;
	    sum = 0.0;
	    sumsq = 0.0;
	    dmin = 1000000000000000.0;
	    dmax = -10000000000000000.0;
	    np = 0;
	    for (y = 0; y < ny; y++) {
        	dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
		sum = sum + dpix;
		sumsq = sumsq + (dpix * dpix);
		if (dpix < dmin)
		    dmin = dpix;
		if (dpix > dmax)
		    dmax = dpix;
		np++;
		}
	    dnp = (double) np;
	    if (printindex) {
		if (xmax < 10.0)
		    printf ("%d", ix);
		else if (xmax < 100.0)
		    printf ("%2d", ix);
		else if (xmax < 1000.0)
		    printf ("%3d", ix);
		else if (xmax < 10000.0)
		    printf ("%4d", ix);
		else
		    printf ("%5d", ix);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compmed) {
		median = (double) medpix (image, 0.0, x, 1, nx, ny, nx, 1, bitpix);
		printf (numform, median);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compsum) {
		printf (numform, sum);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    mean = sum / dnp;
	    if (compmean) {
		printf (numform, mean);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    variance = sumsq;
	    if (compvar) {
		printf (numform, variance);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compstd) {
		std = sqrt ((sumsq / dnp) - (mean * mean));
		printf (numforme, std);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (complim) {
		printf (numform, dmin);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		printf (numform, dmax);
		}
	    printf ("\n");
	    }
	free (xrange);
	}

    /* Sum entire rows */
    else if (!strcmp (crange, "0")) {
	yrange = RangeInit (rrange, xdim);
	ny = rgetn (yrange);
	nx = xdim;
	ymax = yrange->valmax;
	if (printhead) {
	    if (printindex) {
		printf ("i");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compmed) {
		printf ("median");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compsum) {
		printf ("sum");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compmean) {
		printf ("mean");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compvar) {
		printf ("var");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compstd) {
		printf ("stdev");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (complim) {
		printf ("min");
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		printf ("max");
		}
	    printf ("\n");
	    if (printtab) {
		if (printindex)
		    printf ("-----	");
		if (compmed)
		    printf ("------	");
		if (compsum)
		    printf ("---	");
		if (compmean)
		    printf ("----	");
		if (compvar)
		    printf ("---	");
		if (compstd)
		    printf ("-----	");
		if (complim)
		    printf ("---	---");
		printf ("\n");
		}
	    }
	    for (iy = 0; iy < ny; iy++) {
	    y = rgeti4 (yrange) - 1;
	    if (printindex) {
		if (ymax < 10.0)
		    printf ("%d", iy);
		else if (ymax < 100.0)
		    printf ("%2d", iy);
		else if (ymax < 1000.0)
		    printf ("%3d", iy);
		else if (ymax < 10000.0)
		    printf ("%4d", iy);
		else
		    printf ("%5d", iy);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compmed) {
		median = (double) medpix (image, 0.0, 1, y, nx, ny, nx, 1, bitpix);
		printf (numform, median);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    sum = 0.0;
	    sumsq = 0.0;
	    dmin = 1000000000000000.0;
	    dmax = -10000000000000000.0;
	    np = 0;
	    for (x = 0; x < nx; x++) {
        	dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
		sum = sum + dpix;
		sumsq = sumsq + (dpix * dpix);
		if (dpix < dmin)
		    dmin = dpix;
		if (dpix > dmax)
		    dmax = dpix;
		np++;
		}
	    dnp = (double) np;
	    if (compsum) {
		printf (numform, sum);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    mean = sum / dnp;
	    if (compmean) {
		printf (numform, mean);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    variance = sumsq;
	    if (compvar) {
		printf (numform, variance);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (compstd) {
		std = sqrt ((sumsq / dnp) - (mean * mean));
		printf (numforme, std);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		}
	    if (complim) {
		printf (" ");
		printf (numform, dmin);
		if (printtab)
		    printf ("\t");
		else
		    printf (" ");
		printf (numform, dmax);
		}
	    printf ("\n");
	    }
	free (yrange);
	}

    /* Sum a region of a two-dimensional image */
    else {
	yrange = RangeInit (rrange, ydim);
	ny = rgetn (yrange);
	sum = 0.0;
	sumsq = 0.0;
	np = 0;
	dmin = 1000000000000000.0;
	dmax = -10000000000000000.0;
	for (iy = 0; iy < ny; iy++) {
	    y = rgeti4 (yrange) - 1;
	    xrange = RangeInit (crange, xdim);
	    nx = rgetn (xrange);
	    for (ix = 0; ix < nx; ix++) {
		x = rgeti4 (xrange) - 1;
        	dpix = getpix (image,bitpix,xdim,ydim,bzero,bscale,x,y);
		sum = sum + dpix;
		sumsq = sumsq + (dpix * dpix);
		if (dpix < dmin)
		    dmin = dpix;
		if (dpix > dmax)
		    dmax = dpix;
		np++;
		if (verbose)
		    printf ("%s[%d,%d] = %f\n",name,x,y,dpix);
		}
	    }
	dnp = (double) np;
	if (compsum)
	    printf ("%f ", sum);
	mean = sum / dnp;
	if (compmean)
	    printf ("%f ", mean);
	variance = sumsq;
	if (compvar)
	    printf ("%f ", variance);
	if (compstd) {
	    std = sqrt ((sumsq / dnp) - (mean * mean));
	    printf ("%f", std);
	    }
	if (complim) {
	    printf (" ");
	    printf (numform, dmin);
	    printf ("- ");
	    printf (numform, dmax);
	    }
	printf ("\n");
	if (xrange != NULL)
	    free (xrange);
	free (yrange);
	}

    free (header);
    free (image);
    return;
}
/* Jul  2 1999	New program
 * Jul  6 1999	Fix bug with x computation in patch adding section
 * Oct 22 1999	Drop unused variables after lint
 * Oct 29 1999	Add option to compute and printf mean, rms, std, and/or sum
 * Dec 10 1999	Add option -n to set number of decimal places in output
 * Dec 14 1999	Change rms to variance
 *
 * Mar 23 2000	Use hgetm() to get the IRAF pixel file name, not hgets()
 *
 * Dec  6 2002	Add option to sum over entire image if both ranges are 0
 * Dec  6 2002	Add -l option to printf range of values
 *
 * Jun 21 2006	Clean up code
 *
 * Jan 10 2007	Declare RevMsg static, not const
 * Jan 10 2007	Include wcs.h
 * Jan 11 2007	Add PhotPix to compute circular aperture photometry
 *
 * Feb  1 2012	Improve argument parsing so order doesn't matter
 * May 16 2012	Add options i to print index and e to compute medians
 * May 16 2012	Add option t to separate values by tabs
 * May 16 2012	Add option h to print headings
 *
 * Jan 10 2014	Add list file with @ as command line option
 *
 * Jun  9 2016	Fix isnum() tests for added coloned times and dashed dates
 */