aboutsummaryrefslogtreecommitdiff
path: root/pkg/obsolete/t_oimstat.x
blob: 417d1eed0c3d908ea4062be89010708d726a40df (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<mach.h>
include	<imhdr.h>
include "oimstat.h"


# T_OIMSTATISTICS -- Compute and print the statistics of images.

procedure t_oimstatistics ()

pointer	fieldstr			# Pointer to fields string
real	lower				# Lower limit of data value window
real	upper				# Upper limit of data value window
real	binwidth			# Width of histogram bin in sigma
int	format				# Format the output

int	nfields, nbins
int	minmax, npix, mean, median, mode, stddev, skew, kurtosis
pointer	sp, fields, image, v
pointer	im, list, ist, buf, hgm
real	hwidth, hmin, hmax

bool	clgetb()
int	ist_fields(), ist_isfield, imtgetim(), ist_ihist(), btoi()
pointer	imtopenp(), imgnlr()
real	clgetr()
pointer	immap()

begin
	call smark (sp)
	call salloc (fieldstr, SZ_LINE, TY_CHAR)
	call salloc (fields, NFIELDS, TY_INT)
	call salloc (ist, LEN_IMSTAT, TY_STRUCT)
	call salloc (image, SZ_FNAME, TY_CHAR)
	call salloc (v, IM_MAXDIM, TY_LONG)

	# Open the list of input images, the fields and the data value limits.
	list = imtopenp ("images")
	call clgstr ("fields", Memc[fieldstr], SZ_LINE)
	lower = clgetr ("lower")
	upper = clgetr ("upper")
	binwidth = clgetr ("binwidth")
	format = btoi (clgetb ("format"))

	# Get the selected fields.
	nfields = ist_fields (Memc[fieldstr], Memi[fields], NFIELDS)
	if (nfields <= 0) {
	    call imtclose (list)
	    call sfree (sp)
	    return
	}

	# Set the computation switches.
	npix = ist_isfield (IS_FNPIX, Memi[fields], nfields)
	mean = ist_isfield (IS_FMEAN, Memi[fields], nfields)
	median = ist_isfield (IS_FMEDIAN, Memi[fields], nfields)
	mode = ist_isfield (IS_FMODE, Memi[fields], nfields)
	stddev = ist_isfield (IS_FSTDDEV, Memi[fields], nfields)
	skew = ist_isfield (IS_FSKEW, Memi[fields], nfields)
	kurtosis = ist_isfield (IS_FKURTOSIS, Memi[fields], nfields)
	if (ist_isfield (IS_FMIN, Memi[fields], nfields) == YES)
	    minmax = YES
	else if (ist_isfield (IS_FMAX, Memi[fields], nfields) == YES)
	    minmax = YES
	else if (median == YES || mode == YES)
	    minmax = YES
	else
	    minmax = NO

	# Print a header banner for the selected fields.
	if (format == YES)
	    call ist_pheader (Memi[fields], nfields)

	# Loop through the input images.
	while (imtgetim (list, Memc[image], SZ_FNAME) != EOF) {

	    im = immap (Memc[image], READ_ONLY, 0)
	    call ist_initialize (ist, lower, upper)

	    # Accumulate the central moment statistics.
	    call amovkl (long(1), Meml[v], IM_MAXDIM)
	    if (kurtosis == YES) {
	    	while (imgnlr (im, buf, Meml[v]) != EOF)
		    call ist_accumulate4 (ist, Memr[buf], int (IM_LEN(im, 1)),
		        lower, upper, minmax)
	    } else if (skew == YES) {
	    	while (imgnlr (im, buf, Meml[v]) != EOF)
		    call ist_accumulate3 (ist, Memr[buf], int (IM_LEN (im, 1)),
		        lower, upper, minmax)
	    } else if (stddev == YES || median == YES || mode == YES) {
	    	while (imgnlr (im, buf, Meml[v]) != EOF)
		    call ist_accumulate2 (ist, Memr[buf], int (IM_LEN(im,1)),
		        lower, upper, minmax)
	    } else if (mean == YES) {
	    	while (imgnlr (im, buf, Meml[v]) != EOF)
		    call ist_accumulate1 (ist, Memr[buf], int (IM_LEN(im,1)),
		        lower, upper, minmax)
	    } else if (npix == YES) {
	    	while (imgnlr (im, buf, Meml[v]) != EOF)
		    call ist_accumulate0 (ist, Memr[buf], int (IM_LEN(im,1)),
		        lower, upper, minmax)
	    } else if (minmax == YES) {
	    	while (imgnlr (im, buf, Meml[v]) != EOF)
		    call ist_accumulate0 (ist, Memr[buf], int (IM_LEN(im,1)),
		        lower, upper, YES)
	    }

	    # Compute the central moment statistics.
	    call ist_stats (ist, skew, kurtosis)

	    # Accumulate the histogram.
	    hgm = NULL
	    if ((median == YES || mode == YES) && ist_ihist (ist, binwidth,
	        hgm, nbins, hwidth, hmin, hmax) == YES) {
		call aclri (Memi[hgm], nbins)
		call amovkl (long(1), Meml[v], IM_MAXDIM)
		while (imgnlr (im, buf, Meml[v]) != EOF)
		    call ahgmr (Memr[buf], int(IM_LEN(im,1)), Memi[hgm], nbins,
		        hmin, hmax)
		if (median == YES)
		    call ist_hmedian (ist, Memi[hgm], nbins, hwidth, hmin,
			hmax)
		if (mode == YES)
		    call ist_hmode (ist, Memi[hgm], nbins, hwidth, hmin, hmax)
	    }

	    # Print the statistics.
	    if (format == YES)
	        call ist_print (Memc[image], ist, Memi[fields], nfields)
	    else
	        call ist_fprint (Memc[image], ist, Memi[fields], nfields)
		
	    if (hgm != NULL)
		call mfree (hgm, TY_INT)
	    call imunmap (im)
	}

	call imtclose (list)
	call sfree (sp)
end


# IST_FIELDS -- Procedure to decode the fields string into a list of the
# fields to be computed and printed.

int procedure ist_fields (fieldstr, fields, max_nfields)

char	fieldstr[ARB]		# string containing the list of fields
int	fields[ARB]		# fields array
int	max_nfields		# maximum number of fields

int	nfields, flist, field
pointer	sp, fname
int	fntopnb(), fntgfnb(), strdic()

begin
	nfields = 0

	call smark (sp)
	call salloc (fname, SZ_FNAME, TY_CHAR)

	flist = fntopnb (fieldstr, NO)
	while (fntgfnb (flist, Memc[fname], SZ_FNAME) != EOF && 
	    (nfields < max_nfields)) {
	    field = strdic (Memc[fname], Memc[fname], SZ_FNAME, IS_FIELDS)
	    if (field == 0)
		next
	    nfields = nfields + 1
	    fields[nfields] = field
	}
	call fntclsb (flist)

	call sfree (sp)

	return (nfields)
end


# IST_ISFIELD -- Procedure to determine whether a specified field is one
# of the selected fields or not.

int procedure ist_isfield (field, fields, nfields)

int	field		# field to be tested
int	fields[ARB]	# array of selected fields
int	nfields		# number of fields

int	i, isfield

begin
	isfield = NO
	do i = 1, nfields {
	    if (field != fields[i])
		next
	    isfield = YES
	    break
	}

	return (isfield)
end


# IST_INITIALIZE -- Initialize the sum array to zero.

procedure ist_initialize (ist, lower, upper)

pointer	ist		# pointer to the statistics structure
real	lower		# lower datalimit
real	upper		# upperlimit

begin
	if (IS_INDEFR(lower))
	    IS_LO(ist) = -MAX_REAL
	else
	    IS_LO(ist) = lower
	if (IS_INDEFR(upper))
	    IS_HI(ist) = MAX_REAL
	else
	    IS_HI(ist) = upper

	IS_NPIX(ist) = 0
	IS_SUMX(ist) = 0.0d0
	IS_SUMX2(ist) = 0.0d0
	IS_SUMX3(ist) = 0.0d0
	IS_SUMX4(ist) = 0.0d0

	IS_MIN(ist) = MAX_REAL
	IS_MAX(ist) = -MAX_REAL
	IS_MEAN(ist) = INDEFR
	IS_MEDIAN(ist) = INDEFR
	IS_MODE(ist) = INDEFR
	IS_STDDEV(ist) = INDEFR
	IS_SKEW(ist) = INDEFR
	IS_KURTOSIS(ist) = INDEFR
end


# IST_ACCUMULATE4 -- Accumulate sums up to the fourth power of the data for
# data values between lower and upper.

procedure ist_accumulate4 (is, x, npts, lower, upper, minmax)

pointer	is		# pointer to the statistics structure
real	x[ARB]		# the data array
int	npts		# the number of data points
real	lower		# lower data boundary
real	upper		# upper data boundary
int	minmax		# compute the minimum and maximum

int	i, npix
real	lo, hi, xmin, xmax
double	xx, xx2, sumx, sumx2, sumx3, sumx4

begin
	lo = IS_LO(is)
	hi = IS_HI(is)
	npix = IS_NPIX(is)
	sumx = 0.0
	sumx2 = 0.0
	sumx3 = 0.0
	sumx4 = 0.0
	xmin = IS_MIN(is)
	xmax = IS_MAX(is)

	if (IS_INDEFR(lower) && IS_INDEFR(upper)) {
	    npix = npix + npts
	    if (minmax == YES) {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < xmin)
			xmin = xx
		    if (xx > xmax)
			xmax = xx
		    xx2 = xx * xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx2
		    sumx3 = sumx3 + xx2 * xx
		    sumx4 = sumx4 + xx2 * xx2
		}
	    } else {
		do i = 1, npts {
		    xx = x[i]
		    xx2 = xx * xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx2
		    sumx3 = sumx3 + xx2 * xx
		    sumx4 = sumx4 + xx2 * xx2
		}
	    }
	} else {
	    if (minmax == YES) {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
			next
		    if (xx < xmin)
			xmin = xx
		    if (xx > xmax)
			xmax = xx
		    npix = npix + 1
		    xx2 = xx * xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx2
		    sumx3 = sumx3 + xx2 * xx
		    sumx4 = sumx4 + xx2 * xx2
		}
	    } else {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
			next
		    npix = npix + 1
		    xx2 = xx * xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx2
		    sumx3 = sumx3 + xx2 * xx
		    sumx4 = sumx4 + xx2 * xx2
		}
	    }
	}

	IS_NPIX(is) = npix
	IS_SUMX(is) = IS_SUMX(is) + sumx
	IS_SUMX2(is) = IS_SUMX2(is) + sumx2
	IS_SUMX3(is) = IS_SUMX3(is) + sumx3
	IS_SUMX4(is) = IS_SUMX4(is) + sumx4
	IS_MIN(is) = xmin
	IS_MAX(is) = xmax
end


# IST_ACCUMULATE3 -- Accumulate sums up to the third power of the data for
# data values between lower and upper.

procedure ist_accumulate3 (is, x, npts, lower, upper, minmax)

pointer	is		# pointer to the statistics structure
real	x[ARB]		# the data array
int	npts		# the number of data points
real	lower		# lower data boundary
real	upper		# upper data boundary
int	minmax		# compute the minimum and maximum

int	i, npix
real	lo, hi, xmin, xmax
double 	xx, xx2, sumx, sumx2, sumx3

begin
	lo = IS_LO(is)
	hi = IS_HI(is)
	npix = IS_NPIX(is)
	sumx = 0.0
	sumx2 = 0.0
	sumx3 = 0.0
	xmin = IS_MIN(is)
	xmax = IS_MAX(is)

	if (IS_INDEFR(lower) && IS_INDEFR(upper)) {
	    npix = npix + npts
	    if (minmax == YES) {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < xmin)
			xmin = xx
		    if (xx > xmax)
			xmax = xx
		    xx2 = xx * xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx2
		    sumx3 = sumx3 + xx2 * xx
		}
	    } else {
		do i = 1, npts {
		    xx = x[i]
		    xx2 = xx * xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx2
		    sumx3 = sumx3 + xx2 * xx
		}
	    }
	} else {
	    if (minmax == YES) {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
			next
		    if (xx < xmin)
			xmin = xx
		    if (xx > xmax)
			xmax = xx
		    npix = npix + 1
		    xx2 = xx * xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx2
		    sumx3 = sumx3 + xx2 * xx
		}
	    } else {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
			next
		    npix = npix + 1
		    xx2 = xx * xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx2
		    sumx3 = sumx3 + xx2 * xx
		}
	    }
	}

	IS_NPIX(is) = npix
	IS_SUMX(is) = IS_SUMX(is) + sumx
	IS_SUMX2(is) = IS_SUMX2(is) + sumx2
	IS_SUMX3(is) = IS_SUMX3(is) + sumx3
	IS_MIN(is) = xmin
	IS_MAX(is) = xmax
end


# IST_ACCUMULATE2 -- Accumulate sums up to the second power of the data for
# data values between lower and upper.

procedure ist_accumulate2 (is, x, npts, lower, upper, minmax)

pointer	is		# pointer to the statistics structure
real	x[ARB]		# the data array
int	npts		# the number of data points
real	lower		# lower data boundary
real	upper		# upper data boundary
int	minmax		# compute the minimum and maximum

int	i, npix
real	lo, hi, xmin, xmax
double	xx, sumx, sumx2

begin
	lo = IS_LO(is)
	hi = IS_HI(is)
	npix = IS_NPIX(is)
	sumx = 0.0
	sumx2 = 0.0
	xmin = IS_MIN(is)
	xmax = IS_MAX(is)

	if (IS_INDEFR(lower) && IS_INDEFR(upper)) {
	    npix = npix + npts
	    if (minmax == YES) {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < xmin)
			xmin = xx
		    if (xx > xmax)
			xmax = xx
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx * xx
		}
	    } else {
		do i = 1, npts {
		    xx = x[i]
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx * xx
		}
	    }
	} else {
	    if (minmax == YES) {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
			next
		    if (xx < xmin)
			xmin = xx
		    if (xx > xmax)
			xmax = xx
		    npix = npix + 1
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx * xx
		}
	    } else {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
			next
		    npix = npix + 1
		    sumx = sumx + xx
		    sumx2 = sumx2 + xx * xx
		}
	    }
	}

	IS_NPIX(is) = npix
	IS_SUMX(is) = IS_SUMX(is) + sumx
	IS_SUMX2(is) = IS_SUMX2(is) + sumx2
	IS_MIN(is) = xmin
	IS_MAX(is) = xmax
end


# IST_ACCUMULATE1 -- Accumulate sums up to the first power of the data for
# data values between lower and upper.

procedure ist_accumulate1 (is, x, npts, lower, upper, minmax)

pointer	is		# pointer to the statistics structure
real	x[ARB]		# the data array
int	npts		# the number of data points
real	lower		# lower data boundary
real	upper		# upper data boundary
int	minmax		# compute the minimum and maximum

int	i, npix
real	lo, hi, xx, xmin, xmax
double	sumx

begin
	lo = IS_LO(is)
	hi = IS_HI(is)
	npix = IS_NPIX(is)
	sumx = 0.0
	xmin = IS_MIN(is)
	xmax = IS_MAX(is)

	if (IS_INDEFR(lower) && IS_INDEFR(upper)) {
	    npix = npix + npts
	    if (minmax == YES) {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < xmin)
			xmin = xx
		    if (xx > xmax)
			xmax = xx
		    sumx = sumx + xx
		}
	    } else {
		do i = 1, npts
		    sumx = sumx + x[i]
	    }
	} else {
	    if (minmax == YES) {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
			next
		    npix = npix + 1
		    if (xx < xmin)
			xmin = xx
		    if (xx > xmax)
			xmax = xx
		    sumx = sumx + xx
		}
	    } else {
		do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
			next
		    npix = npix + 1
		    sumx = sumx + xx
		}
	    }
	}

	IS_NPIX(is) = npix
	IS_SUMX(is) = IS_SUMX(is) + sumx
	IS_MIN(is) = xmin
	IS_MAX(is) = xmax
end


# IST_ACCUMULATE0 -- Accumulate sums up to the 0th power of the data for
# data values between lower and upper.

procedure ist_accumulate0 (is, x, npts, lower, upper, minmax)

pointer	is		# pointer to the statistics structure
real	x[ARB]		# the data array
int	npts		# the number of data points
real	lower		# lower data boundary
real	upper		# upper data boundary
int	minmax		# compute the minimum and maximum

int	i, npix
real	lo, hi, xx, xmin, xmax

begin
	lo = IS_LO(is)
	hi = IS_HI(is)
	npix = IS_NPIX(is)
	xmin = IS_MIN(is)
	xmax = IS_MAX(is)

	if (IS_INDEFR(lower) && IS_INDEFR(upper)) {
	    npix = npix + npts
	    if (minmax == YES) {
	        do i = 1, npts {
		    xx = x[i]
		    if (xx < xmin)
		        xmin = xx
		    if (xx > xmax)
		        xmax = xx
	        }
	    }
	} else {
	    if (minmax == YES) {
	        do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
		        next
		    npix = npix + 1
		    if (xx < xmin)
		        xmin = xx
		    if (xx > xmax)
		        xmax = xx
	        }
	    } else {
	        do i = 1, npts {
		    xx = x[i]
		    if (xx < lo || xx > hi)
		        next
		    npix = npix + 1
		}
	    }
	}

	IS_NPIX(is) = npix
	IS_MIN(is) = xmin
	IS_MAX(is) = xmax
end


# IST_STATS -- Procedure to compute the first four central moments of the
# distribution.

procedure ist_stats (ist, bskew, bkurtosis)

pointer	ist			# statistics structure
int	bskew			# skew switch
int	bkurtosis		# kurtosis switch

double	mean, var, stdev
bool	fp_equalr()

begin
	if (fp_equalr (IS_MIN(ist), MAX_REAL))
	    IS_MIN(ist) = INDEFR
	if (fp_equalr (IS_MAX(ist), -MAX_REAL))
	    IS_MAX(ist) = INDEFR

	if (IS_NPIX(ist) <= 0)
	    return
	mean = IS_SUMX(ist) / IS_NPIX(ist)
	IS_MEAN(ist) = mean

	if (IS_NPIX(ist) < 2)
	    return
	var = (IS_SUMX2(ist) - IS_SUMX(ist) * mean) /
	    (IS_NPIX(ist) - 1)
	if (var <= 0.0) {
	    IS_STDDEV(ist) = 0.0
	    return
	} else {
	    stdev = sqrt (var)
	    IS_STDDEV(ist) = stdev
	}

	if (bskew == YES)
	    IS_SKEW(ist) = (IS_SUMX3(ist) - 3.0d0 * IS_MEAN(ist) *
	        IS_SUMX2(ist) + 3.0d0 * mean * mean *
		IS_SUMX(ist) - IS_NPIX(ist) * mean ** 3) /
		IS_NPIX(ist) / stdev / stdev / stdev
	    
	if (bkurtosis == YES)
	    IS_KURTOSIS(ist) = (IS_SUMX4(ist) - 4.0d0 * mean *
	        IS_SUMX3(ist) + 6.0d0 * mean * mean *
	        IS_SUMX2(ist) - 4.0 * mean ** 3 * IS_SUMX(ist) +
	        IS_NPIX(ist) * mean ** 4) / IS_NPIX(ist) /
	        stdev / stdev / stdev / stdev - 3.0d0
end


# IST_IHIST -- Procedure to initilaize the histogram of the image pixels.

int procedure ist_ihist (ist, binwidth, hgm, nbins, hwidth, hmin, hmax)

pointer	ist		# pointer to the statistics structure
real	binwidth	# histogram bin width in sigma
pointer	hgm		# pointer to the histogram
int	nbins		# number of bins
real	hwidth		# histogram resolution
real	hmin		# minimum histogram value
real	hmax		# maximum histogram value

begin
	nbins = 0
	if (binwidth <= 0.0)
	    return (NO)
	hwidth = binwidth * IS_STDDEV(ist)
	if (hwidth <= 0.0)
	    return (NO)
	nbins = (IS_MAX(ist) - IS_MIN(ist)) / hwidth + 1
	if (nbins < 3)
	    return (NO)

	hmin = IS_MIN(ist)
	hmax = IS_MAX(ist)
	call malloc (hgm, nbins, TY_INT)
	return (YES)
end


# IST_HMEDIAN -- Procedure to compute the median of the values.

procedure ist_hmedian (ist, hgm, nbins, hwidth, hmin, hmax)

pointer	ist		# pointer to the statistics strucuture
int	hgm[ARB]	# histogram of the pixels
int	nbins		# number of bins in the histogram
real	hwidth		# resolution of the histogram
real	hmin		# minimum histogram value
real	hmax		# maximum histogram value

int	i, lo, hi
pointer	sp, ihgm
real	h1, hdiff, hnorm
bool	fp_equalr()

begin
	call smark (sp)
	call salloc (ihgm, nbins, TY_REAL)

	# Integrate the histogram and normalize.
	Memr[ihgm] = hgm[1]
	do i = 2, nbins
	    Memr[ihgm+i-1] = hgm[i] + Memr[ihgm+i-2]
	hnorm = Memr[ihgm+nbins-1]
	call adivkr (Memr[ihgm], hnorm, Memr[ihgm], nbins)

	# Initialize the low and high bin numbers.
	lo = 0
	hi = 1

	# Search for the point which divides the integral in half.
	do i = 1, nbins {
	    if (Memr[ihgm+i-1] > 0.5)
		break
	    lo = i
	}
	hi = lo + 1
	#call eprintf (
	    #"hmin=%g hmax=%g hw=%g nbins=%d lo=%d ih(lo)=%g hi=%d ih(hi)=%g\n")
	    #call pargr (hmin)
	    #call pargr (hmax)
	    #call pargr (hwidth)
	    #call pargi (nbins)
	    #call pargi (lo)
	    #call pargr (Memr[ihgm+lo-1])
	    #call pargi (hi)
	    #call pargr (Memr[ihgm+hi-1])

	# Approximate the histogram.
	h1 = hmin + lo * hwidth
	if (lo == 0)
	    hdiff = Memr[ihgm+hi-1]
	else
	    hdiff = Memr[ihgm+hi-1] - Memr[ihgm+lo-1]
	if (fp_equalr (hdiff, 0.0))
	    IS_MEDIAN(ist) = h1
	else if (lo == 0)
	    IS_MEDIAN(ist) = h1 + 0.5 / hdiff * hwidth
	else
	    IS_MEDIAN(ist) = h1 + (0.5 - Memr[ihgm+lo-1]) / hdiff * hwidth
	#call eprintf ("hlo=%g hhi=%g h1=%g hdiff=%g median=%g\n")
	    #call pargr (hmin)
	    #call pargr (hmin + (nbins - 1) * hwidth)
	    #call pargr (h1)
	    #call pargr (hdiff)
	    #call pargr (IS_MEDIAN(ist))

	call sfree (sp)
end


# IST_HMODE -- Procedure to compute the mode.

procedure ist_hmode (ist, hgm, nbins, hwidth, hmin, hmax)

pointer	ist		# pointer to the statistics strucuture
int	hgm[ARB]	# histogram of the pixels
int	nbins		# number of bins in the histogram
real	hwidth		# resolution of the histogram
real	hmin		# minimum histogram value
real	hmax		# maximum histogram value

int	i, bpeak
real	hpeak, dh1, dh2, denom
bool	fp_equalr()

begin
	# If there is a single bin return the midpoint of that bin.
	if (nbins == 1) {
	    IS_MODE(ist) = hmin + 0.5 * hwidth
	    return
	}

	# If there are two bins return the midpoint of the greater bin.
	if (nbins == 2) {
	    if (hgm[1] > hgm[2])
	        IS_MODE(ist) = hmin + 0.5 * hwidth
	    else if (hgm[2] > hgm[1])
	        IS_MODE(ist) = hmin + 1.5 * hwidth
	    else
	        IS_MODE(ist) = hmin + hwidth
	    return
	}

	# Find the bin containing the histogram maximum.
	hpeak = hgm[1]
	bpeak = 1
	do i = 2, nbins {
	    if (hgm[i] > hpeak) {
		hpeak = hgm[i]
		bpeak = i
	    }
	}

	# If the maximum is in the first bin return the midpoint of the bin.
	if (bpeak == 1) {
	    IS_MODE(ist) = hmin + 0.5 * hwidth
	    return
	}

	# If the maximum is in the last bin return the midpoint of the bin.
	if (bpeak == nbins) {
	    IS_MODE(ist) = hmin + (nbins - 0.5) * hwidth
	    return
	}

	# Compute the lower limit of bpeak.
	bpeak = bpeak - 1

	# Do a parabolic interpolation to find the peak.
	dh1 = hgm[bpeak+1] - hgm[bpeak]
	dh2 = hgm[bpeak+1] - hgm[bpeak+2]
	denom = dh1 + dh2
	if (fp_equalr (denom, 0.0)) {
	    IS_MODE(ist) = hmin + (bpeak + 0.5) * hwidth
	} else {
	    IS_MODE(ist) = bpeak + 1 + 0.5 * (dh1 - dh2) / denom
	    IS_MODE(ist) = hmin + (IS_MODE(ist) - 0.5) * hwidth
	}


	dh1 = hgm[bpeak] * (hmin + (bpeak - 0.5) * hwidth) +
	    hgm[bpeak+1] * (hmin + (bpeak + 0.5) * hwidth) +
	    hgm[bpeak+2] * (hmin + (bpeak + 1.5) * hwidth)
	dh2 = hgm[bpeak] + hgm[bpeak+1] + hgm[bpeak+2]
end


# IST_PHEADER -- Print the banner fields.

procedure ist_pheader (fields, nfields)

int	fields[ARB]		# fields to be printed
int	nfields			# number of fields

int	i

begin
	call printf ("#")
	do i = 1, nfields {
	    switch (fields[i]) {
	    case IS_FIMAGE:
	        call printf (IS_FSTRING)
		    call pargstr (IS_KIMAGE)
	    case IS_FNPIX:
	        call printf (IS_FCOLUMN)
		    call pargstr (IS_KNPIX)
	    case IS_FMIN:
		call printf (IS_FCOLUMN)
		    call pargstr (IS_KMIN)
	    case IS_FMAX:
		call printf (IS_FCOLUMN)
		    call pargstr (IS_KMAX)
	    case IS_FMEAN:
		call printf (IS_FCOLUMN)
		    call pargstr (IS_KMEAN)
	    case IS_FMEDIAN:
		call printf (IS_FCOLUMN)
		    call pargstr (IS_KMEDIAN)
	    case IS_FMODE:
		call printf (IS_FCOLUMN)
		    call pargstr (IS_KMODE)
	    case IS_FSTDDEV:
		call printf (IS_FCOLUMN)
		    call pargstr (IS_KSTDDEV)
	    case IS_FSKEW:
		call printf (IS_FCOLUMN)
		    call pargstr (IS_KSKEW)
	    case IS_FKURTOSIS:
		call printf (IS_FCOLUMN)
		    call pargstr (IS_KKURTOSIS)
	    }
	}

	call printf ("\n")
	call flush (STDOUT)
end


# IST_PRINT -- Print the fields

procedure ist_print (image, ist, fields, nfields)

char	image[ARB]		# image name
pointer	ist			# pointer to the statistics structure
int	fields[ARB]		# fields to be printed
int	nfields			# number of fields

int	i

begin
	call printf (" ")
	do i = 1, nfields {
	    switch (fields[i]) {
	    case IS_FIMAGE:
	        call printf (IS_FSTRING)
		    call pargstr (image)
	    case IS_FNPIX:
	        call printf (IS_FINTEGER)
		    call pargi (IS_NPIX(ist))
	    case IS_FMIN:
		call printf (IS_FREAL)
		    call pargr (IS_MIN(ist))
	    case IS_FMAX:
		call printf (IS_FREAL)
		    call pargr (IS_MAX(ist))
	    case IS_FMEAN:
		call printf (IS_FREAL)
		    call pargr (IS_MEAN(ist))
	    case IS_FMEDIAN:
		call printf (IS_FREAL)
		    call pargr (IS_MEDIAN(ist))
	    case IS_FMODE:
		call printf (IS_FREAL)
		    call pargr (IS_MODE(ist))
	    case IS_FSTDDEV:
		call printf (IS_FREAL)
		    call pargr (IS_STDDEV(ist))
	    case IS_FSKEW:
		call printf (IS_FREAL)
		    call pargr (IS_SKEW(ist))
	    case IS_FKURTOSIS:
		call printf (IS_FREAL)
		    call pargr (IS_KURTOSIS(ist))
	    }
	}

	call printf ("\n")
	call flush (STDOUT)
end


# IST_FPRINT -- Print the fields using a free format.

procedure ist_fprint (image, ist, fields, nfields)

char	image[ARB]		# image name
pointer	ist			# pointer to the statistics structure
int	fields[ARB]		# fields to be printed
int	nfields			# number of fields

int	i

begin
	do i = 1, nfields {
	    switch (fields[i]) {
	    case IS_FIMAGE:
	        call printf ("%s")
		    call pargstr (image)
	    case IS_FNPIX:
	        call printf ("%d")
		    call pargi (IS_NPIX(ist))
	    case IS_FMIN:
		call printf ("%g")
		    call pargr (IS_MIN(ist))
	    case IS_FMAX:
		call printf ("%g")
		    call pargr (IS_MAX(ist))
	    case IS_FMEAN:
		call printf ("%g")
		    call pargr (IS_MEAN(ist))
	    case IS_FMEDIAN:
		call printf ("%g")
		    call pargr (IS_MEDIAN(ist))
	    case IS_FMODE:
		call printf ("%g")
		    call pargr (IS_MODE(ist))
	    case IS_FSTDDEV:
		call printf ("%g")
		    call pargr (IS_STDDEV(ist))
	    case IS_FSKEW:
		call printf ("%g")
		    call pargr (IS_SKEW(ist))
	    case IS_FKURTOSIS:
		call printf ("%g")
		    call pargr (IS_KURTOSIS(ist))
	    }
	    if (i < nfields)
		call printf ("  ")
	}

	call printf ("\n")
	call flush (STDOUT)
end