aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/imutil/src/t_imstat.x
blob: 9641a83e85ef16271fdc90298223b9c316a7ba29 (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
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<mach.h>
include	<imhdr.h>
include <imset.h>
include "imstat.h"


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

procedure t_imstatistics ()

real	lower, upper, binwidth, lsigma, usigma, low, up, hwidth, hmin, hmax
pointer	sp, fieldstr, fields, image, ist, v
pointer	im, buf, hgm
int	i, list, nclip, format, nfields, nbins, npix, cache, old_size

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

begin
	call smark (sp)
	call salloc (fieldstr, SZ_LINE, TY_CHAR)
	call salloc (fields, IST_NFIELDS, TY_INT)
	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")
	nclip = clgeti ("nclip")
	lsigma = clgetr ("lsigma")
	usigma = clgetr ("usigma")
	binwidth = clgetr ("binwidth")
	format = btoi (clgetb ("format"))
	cache = btoi (clgetb ("cache"))

	# Allocate space for statistics structure
	call ist_allocate (ist)

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

        # Set the processing switches
        call ist_switches (ist, Memi[fields], nfields, nclip)

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

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

	    # Open the image.
	    iferr (im = immap (Memc[image], READ_ONLY, 0)) {
		call printf ("Error reading image %s ...\n")
		    call pargstr (Memc[image])
		next
	    }

	    if (cache == YES)
		call ist_cache1 (cache, im, old_size)
		
	    # Accumulate the central moment statistics.
	    low = lower
	    up = upper
	    do i = 0, nclip {

	        call ist_initialize (ist, low, up)
	        call amovkl (long(1), Meml[v], IM_MAXDIM)

	        if (IST_SKURTOSIS(IST_SW(ist)) == YES) {
	    	    while (imgnlr (im, buf, Meml[v]) != EOF)
		        call ist_accumulate4 (ist, Memr[buf],
			    int (IM_LEN(im, 1)), low, up,
			    IST_SMINMAX(IST_SW(ist)))
	    	} else if (IST_SSKEW(IST_SW(ist)) == YES) {
	    	    while (imgnlr (im, buf, Meml[v]) != EOF)
		        call ist_accumulate3 (ist, Memr[buf],
			    int (IM_LEN (im, 1)), low, up,
			    IST_SMINMAX(IST_SW(ist)))
	        } else if (IST_SSTDDEV(IST_SW(ist)) == YES ||
		    IST_SMEDIAN(IST_SW(ist)) == YES ||
		    IST_SMODE(IST_SW(ist)) == YES) {
	    	    while (imgnlr (im, buf, Meml[v]) != EOF)
		        call ist_accumulate2 (ist, Memr[buf],
			    int (IM_LEN(im,1)), low, up,
			    IST_SMINMAX(IST_SW(ist)))
	        } else if (IST_SMEAN(IST_SW(ist)) == YES) {
	    	    while (imgnlr (im, buf, Meml[v]) != EOF)
		        call ist_accumulate1 (ist, Memr[buf],
			    int (IM_LEN(im,1)), low, up,
			    IST_SMINMAX(IST_SW(ist)))
	        } else if (IST_SNPIX(IST_SW(ist)) == YES) {
	    	    while (imgnlr (im, buf, Meml[v]) != EOF)
		        call ist_accumulate0 (ist, Memr[buf],
			    int (IM_LEN(im,1)), low, up,
			    IST_SMINMAX(IST_SW(ist)))
	        } else if (IST_SMINMAX(IST_SW(ist)) == YES) {
	    	    while (imgnlr (im, buf, Meml[v]) != EOF)
		        call ist_accumulate0 (ist, Memr[buf],
			    int (IM_LEN(im,1)), low, up, YES)
	        }


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

                # Compute new limits and iterate.
                if (i < nclip) {
                    if (IS_INDEFR(lsigma) || IS_INDEFR(IST_MEAN(ist)) ||
		        IS_INDEFR(IST_STDDEV(ist)))
                        low = -MAX_REAL
                    else if (lsigma > 0.0)
                        low = IST_MEAN(ist) - lsigma * IST_STDDEV(ist)
                    else
                        low = -MAX_REAL
                    if (IS_INDEFR(usigma) || IS_INDEFR(IST_MEAN(ist)) ||
		        IS_INDEFR(IST_STDDEV(ist)))
                        up = MAX_REAL
                    else if (usigma > 0.0)
                        up = IST_MEAN(ist) + usigma * IST_STDDEV(ist)
                    else
                        up = MAX_REAL
		    if (!IS_INDEFR(lower))
		        low = max (low, lower)
		    if (!IS_INDEFR(upper))
		        up = min (up, upper)
                    if (i > 0) {
                        if (IST_NPIX(ist) == npix)
                            break
                    }
                    npix = IST_NPIX(ist)
                }

	    }

	    # Accumulate the histogram.
	    hgm = NULL
	    if ((IST_SMEDIAN(IST_SW(ist)) == YES || IST_SMODE(IST_SW(ist)) ==
	        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 (IST_SMEDIAN(IST_SW(ist)) == YES)
		    call ist_hmedian (ist, Memi[hgm], nbins, hwidth, hmin,
			hmax)
		if (IST_SMODE(IST_SW(ist)) == YES)
		    call ist_hmode (ist, Memi[hgm], nbins, hwidth, hmin, hmax)
	    }
	    if (hgm != NULL)
		call mfree (hgm, TY_INT)

	    # 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)
		
	    call imunmap (im)
	    if (cache == YES)
		call fixmem (old_size)
	}

	call ist_free (ist)
	call imtclose (list)
	call sfree (sp)
end


# IST_ALLOCATE -- Allocate space for the statistics structure.

procedure ist_allocate (ist)

pointer	ist		#O the statistics descriptor

begin
    	call calloc (ist, LEN_IMSTAT, TY_STRUCT)
	call malloc (IST_SW(ist), LEN_NSWITCHES, TY_INT)
end


# IST_FREE -- Free the statistics structure.

procedure ist_free (ist)

pointer	ist		#O the statistics descriptor

begin
	call mfree (IST_SW(ist), TY_INT)
	call mfree (ist, TY_STRUCT)
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]           #I string containing the list of fields
int     fields[ARB]             #O fields array
int     max_nfields             #I 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, IST_FIELDS)
            if (field == 0)
                next
            nfields = nfields + 1
            fields[nfields] = field
        }
        call fntclsb (flist)

        call sfree (sp)

        return (nfields)
end


# IST_SWITCHES -- Set the processing switches.

procedure ist_switches (ist, fields, nfields, nclip)

pointer	ist			#I the statistics pointer
int     fields[ARB]             #I fields array
int     nfields                 #I maximum number of fields
int	nclip			#I the number of clipping iterations

pointer	sw
int	ist_isfield()

begin
	# Initialize.
	sw = IST_SW(ist)
	call amovki (NO, Memi[sw], LEN_NSWITCHES)

        # Set the computation switches.
        IST_SNPIX(sw) = ist_isfield (IST_FNPIX, fields, nfields)
        IST_SMEAN(sw) = ist_isfield (IST_FMEAN, fields, nfields)
        IST_SMEDIAN(sw) = ist_isfield (IST_FMEDIAN, fields, nfields)
        IST_SMODE(sw) = ist_isfield (IST_FMODE, fields, nfields)
        if (nclip > 0)
            IST_SSTDDEV(sw) = YES
	else
            IST_SSTDDEV(sw) = ist_isfield (IST_FSTDDEV, fields, nfields)
        IST_SSKEW(sw) = ist_isfield (IST_FSKEW, fields, nfields)
        IST_SKURTOSIS(sw) = ist_isfield (IST_FKURTOSIS, fields, nfields)

	# Adjust the computation switches.
        if (ist_isfield (IST_FMIN, fields, nfields) == YES)
            IST_SMINMAX(sw) = YES
        else if (ist_isfield (IST_FMAX, fields, nfields) == YES)
            IST_SMINMAX(sw) = YES
        else if (IST_SMEDIAN(sw) == YES || IST_SMODE(sw) == YES)
            IST_SMINMAX(sw) = YES
        else
            IST_SMINMAX(sw) = NO
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 IST_FIMAGE:
                call printf (IST_FSTRING)
                    call pargstr (IST_KIMAGE)
            case IST_FNPIX:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KNPIX)
            case IST_FMIN:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KMIN)
            case IST_FMAX:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KMAX)
            case IST_FMEAN:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KMEAN)
            case IST_FMEDIAN:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KMEDIAN)
            case IST_FMODE:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KMODE)
            case IST_FSTDDEV:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KSTDDEV)
            case IST_FSKEW:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KSKEW)
            case IST_FKURTOSIS:
                call printf (IST_FCOLUMN)
                    call pargstr (IST_KKURTOSIS)
            }
        }

        call printf ("\n")
        call flush (STDOUT)
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           #I field to be tested
int     fields[ARB]     #I array of selected fields
int     nfields         #I 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 statistics computation.

procedure ist_initialize (ist, lower, upper)

pointer ist             #I pointer to the statistics structure
real    lower           #I lower good data limit
real    upper           #I upper good data limit

begin
        if (IS_INDEFR(lower))
            IST_LO(ist) = -MAX_REAL
        else
            IST_LO(ist) = lower
        if (IS_INDEFR(upper))
            IST_HI(ist) = MAX_REAL
        else
            IST_HI(ist) = upper

        IST_NPIX(ist) = 0
        IST_SUMX(ist) = 0.0d0
        IST_SUMX2(ist) = 0.0d0
        IST_SUMX3(ist) = 0.0d0
        IST_SUMX4(ist) = 0.0d0

        IST_MIN(ist) = MAX_REAL
        IST_MAX(ist) = -MAX_REAL
        IST_MEAN(ist) = INDEFR
        IST_MEDIAN(ist) = INDEFR
        IST_MODE(ist) = INDEFR
        IST_STDDEV(ist) = INDEFR
        IST_SKEW(ist) = INDEFR
        IST_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 (ist, x, npts, lower, upper, minmax)

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

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

begin
        lo = IST_LO(ist)
        hi = IST_HI(ist)
        npix = IST_NPIX(ist)
        sumx = 0.0
        sumx2 = 0.0
        sumx3 = 0.0
        sumx4 = 0.0
        xmin = IST_MIN(ist)
        xmax = IST_MAX(ist)

        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
                }
            }
        }

        IST_NPIX(ist) = npix
        IST_SUMX(ist) = IST_SUMX(ist) + sumx
        IST_SUMX2(ist) = IST_SUMX2(ist) + sumx2
        IST_SUMX3(ist) = IST_SUMX3(ist) + sumx3
        IST_SUMX4(ist) = IST_SUMX4(ist) + sumx4
        IST_MIN(ist) = xmin
        IST_MAX(ist) = xmax
end


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

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

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

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

begin
        lo = IST_LO(ist)
        hi = IST_HI(ist)
        npix = IST_NPIX(ist)
        sumx = 0.0
        sumx2 = 0.0
        sumx3 = 0.0
        xmin = IST_MIN(ist)
        xmax = IST_MAX(ist)

        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
                }
            }
        }

        IST_NPIX(ist) = npix
        IST_SUMX(ist) = IST_SUMX(ist) + sumx
        IST_SUMX2(ist) = IST_SUMX2(ist) + sumx2
        IST_SUMX3(ist) = IST_SUMX3(ist) + sumx3
        IST_MIN(ist) = xmin
        IST_MAX(ist) = xmax
end


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

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

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

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

begin
        lo = IST_LO(ist)
        hi = IST_HI(ist)
        npix = IST_NPIX(ist)
        sumx = 0.0
        sumx2 = 0.0
        xmin = IST_MIN(ist)
        xmax = IST_MAX(ist)

        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
                }
            }
        }

        IST_NPIX(ist) = npix
        IST_SUMX(ist) = IST_SUMX(ist) + sumx
        IST_SUMX2(ist) = IST_SUMX2(ist) + sumx2
        IST_MIN(ist) = xmin
        IST_MAX(ist) = xmax
end


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

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

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

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

begin
        lo = IST_LO(ist)
        hi = IST_HI(ist)
        npix = IST_NPIX(ist)
        sumx = 0.0
        xmin = IST_MIN(ist)
        xmax = IST_MAX(ist)

        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
                }
            }
        }

        IST_NPIX(ist) = npix
        IST_SUMX(ist) = IST_SUMX(ist) + sumx
        IST_MIN(ist) = xmin
        IST_MAX(ist) = xmax
end


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

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

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

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

begin
        lo = IST_LO(ist)
        hi = IST_HI(ist)
        npix = IST_NPIX(ist)
        xmin = IST_MIN(ist)
        xmax = IST_MAX(ist)

        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
                }
            }
        }

        IST_NPIX(ist) = npix
        IST_MIN(ist) = xmin
        IST_MAX(ist) = xmax
end


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

procedure ist_stats (ist)

pointer ist                     #I statistics structure

double  mean, var, stdev
pointer	sw
bool    fp_equalr()

begin
	sw = IST_SW(ist)

	# Compute the basic statistics regardless of the switches.
        if (fp_equalr (IST_MIN(ist), MAX_REAL))
            IST_MIN(ist) = INDEFR
        if (fp_equalr (IST_MAX(ist), -MAX_REAL))
            IST_MAX(ist) = INDEFR
        if (IST_NPIX(ist) <= 0)
            return

        mean = IST_SUMX(ist) / IST_NPIX(ist)
        IST_MEAN(ist) = mean
        if (IST_NPIX(ist) < 2)
            return

        var = (IST_SUMX2(ist) - IST_SUMX(ist) * mean) /
            (IST_NPIX(ist) - 1)
        if (var <= 0.0) {
            IST_STDDEV(ist) = 0.0
            return
        } else {
            stdev = sqrt (var)
            IST_STDDEV(ist) = stdev
        }

	# Compute higher order moments if the switches are set.
        if (IST_SSKEW(sw)== YES)
            IST_SKEW(ist) = (IST_SUMX3(ist) - 3.0d0 * IST_MEAN(ist) *
                IST_SUMX2(ist) + 3.0d0 * mean * mean *
                IST_SUMX(ist) - IST_NPIX(ist) * mean ** 3) /
                IST_NPIX(ist) / stdev / stdev / stdev

        if (IST_SKURTOSIS(sw) == YES)
            IST_KURTOSIS(ist) = (IST_SUMX4(ist) - 4.0d0 * mean *
                IST_SUMX3(ist) + 6.0d0 * mean * mean *
                IST_SUMX2(ist) - 4.0 * mean ** 3 * IST_SUMX(ist) +
                IST_NPIX(ist) * mean ** 4) / IST_NPIX(ist) /
                stdev / stdev / stdev / stdev - 3.0d0
end



# IST_IHIST -- Initilaize the histogram of the image pixels.

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

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

begin
        nbins = 0
        if (binwidth <= 0.0)
            return (NO)

        hwidth = binwidth * IST_STDDEV(ist)
        if (hwidth <= 0.0)
            return (NO)

        nbins = (IST_MAX(ist) - IST_MIN(ist)) / hwidth + 1
        if (nbins < 3)
            return (NO)

        hmin = IST_MIN(ist)
        hmax = IST_MAX(ist)

        call malloc (hgm, nbins, TY_INT)

        return (YES)
end


# IST_HMEDIAN -- Estimate the median from the histogram.

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

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

real    h1, hdiff, hnorm
pointer sp, ihgm
int     i, lo, hi

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

        # Approximate the median.
        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))
            IST_MEDIAN(ist) = h1
        else if (lo == 0)
            IST_MEDIAN(ist) = h1 + 0.5 / hdiff * hwidth
        else
            IST_MEDIAN(ist) = h1 + (0.5 - Memr[ihgm+lo-1]) / hdiff * hwidth

        call sfree (sp)
end


# IST_HMODE -- Procedure to compute the mode.

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

pointer ist             #I pointer to the statistics strucuture
int     hgm[ARB]        #I histogram of the pixels
int     nbins           #I number of bins in the histogram
real    hwidth          #I resolution of the histogram
real    hmin            #I minimum histogram value
real    hmax            #I 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) {
            IST_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])
                IST_MODE(ist) = hmin + 0.5 * hwidth
            else if (hgm[2] > hgm[1])
                IST_MODE(ist) = hmin + 1.5 * hwidth
            else
                IST_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) {
            IST_MODE(ist) = hmin + 0.5 * hwidth
            return
        }

        # If the maximum is in the last bin return the midpoint of the bin.
        if (bpeak == nbins) {
            IST_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)) {
            IST_MODE(ist) = hmin + (bpeak + 0.5) * hwidth
        } else {
            IST_MODE(ist) = bpeak + 1 + 0.5 * (dh1 - dh2) / denom
            IST_MODE(ist) = hmin + (IST_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_PRINT -- Print the fields using builtin format strings.

procedure ist_print (image, mask, ist, fields, nfields)

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

int     i

begin
        call printf (" ")
        do i = 1, nfields {
            switch (fields[i]) {
            case IST_FIMAGE:
                call printf (IST_FSTRING)
                    call pargstr (image)
            case IST_FNPIX:
                call printf (IST_FINTEGER)
                    call pargi (IST_NPIX(ist))
            case IST_FMIN:
                call printf (IST_FREAL)
                    call pargr (IST_MIN(ist))
            case IST_FMAX:
                call printf (IST_FREAL)
                    call pargr (IST_MAX(ist))
            case IST_FMEAN:
                call printf (IST_FREAL)
                    call pargr (IST_MEAN(ist))
            case IST_FMEDIAN:
                call printf (IST_FREAL)
                    call pargr (IST_MEDIAN(ist))
            case IST_FMODE:
                call printf (IST_FREAL)
                    call pargr (IST_MODE(ist))
            case IST_FSTDDEV:
                call printf (IST_FREAL)
                    call pargr (IST_STDDEV(ist))
            case IST_FSKEW:
                call printf (IST_FREAL)
                    call pargr (IST_SKEW(ist))
            case IST_FKURTOSIS:
                call printf (IST_FREAL)
                    call pargr (IST_KURTOSIS(ist))
            }
        }

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


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

procedure ist_fprint (image, mask, ist, fields, nfields)

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

int     i

begin
        do i = 1, nfields {
            switch (fields[i]) {
            case IST_FIMAGE:
                call printf ("%s")
                    call pargstr (image)
            case IST_FNPIX:
                call printf ("%d")
                    call pargi (IST_NPIX(ist))
            case IST_FMIN:
                call printf ("%g")
                    call pargr (IST_MIN(ist))
            case IST_FMAX:
                call printf ("%g")
                    call pargr (IST_MAX(ist))
            case IST_FMEAN:
                call printf ("%g")
                    call pargr (IST_MEAN(ist))
            case IST_FMEDIAN:
                call printf ("%g")
                    call pargr (IST_MEDIAN(ist))
            case IST_FMODE:
                call printf ("%g")
                    call pargr (IST_MODE(ist))
            case IST_FSTDDEV:
                call printf ("%g")
                    call pargr (IST_STDDEV(ist))
            case IST_FSKEW:
                call printf ("%g")
                    call pargr (IST_SKEW(ist))
            case IST_FKURTOSIS:
                call printf ("%g")
                    call pargr (IST_KURTOSIS(ist))
            }
            if (i < nfields)
                call printf ("  ")
        }

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


define	MEMFUDGE	1.05

# IST_CACHE1 -- Cache 1 image in memory using the image i/o buffer sizes.

procedure ist_cache1 (cache, im, old_size)

int	cache			#I cache the image pixels in the imio buffer
pointer	im			#I the image descriptor
int	old_size		#O the old working set size

int	i, req_size, buf_size
int	sizeof(), ist_memstat()

begin
	req_size = MEMFUDGE * IM_LEN(im,1) * sizeof (IM_PIXTYPE(im))
	do i = 2, IM_NDIM(im)
	    req_size = req_size * IM_LEN(im,i)
	if (ist_memstat (cache, req_size, old_size) == YES) 
	    call ist_pcache (im, INDEFI, buf_size)
end


# IST_MEMSTAT -- Figure out if there is  enough memory to cache the image
# pixels. If it is necessary to request more memory and the memory is
# avalilable return YES otherwise return NO.

int procedure ist_memstat (cache, req_size, old_size)

int	cache			#I cache memory ?
int	req_size		#I the requested working set size in chars 
int	old_size		#O the original working set size in chars 

int	cur_size, max_size
int	begmem()

begin
        # Find the default working set size.
        cur_size = begmem (0, old_size, max_size)

	# If cacheing is disabled return NO regardless of the working set size.
	if (cache == NO)
	    return (NO)

	# If the requested working set size is less than the current working
	# set size return YES.
	if (req_size <= cur_size)
	    return (YES)

	# Reset the current working set size.
	cur_size = begmem (req_size, old_size, max_size)
	if (req_size <= cur_size) {
	    return (YES)
	} else {
	    return (NO)
	}
end


# IST_PCACHE -- Cache the image pixels im memory by resetting the  default image
# buffer size. If req_size is INDEF the size of the image is used to determine
# the size of the image i/o buffers.

procedure ist_pcache (im, req_size, buf_size)

pointer im                      #I the input image point
int     req_size                #I the requested working set size in chars
int	buf_size		#O the new image buffer size

int     i, def_size, new_imbufsize
int     sizeof(), imstati()

begin
	# Find the default buffer size.
	def_size = imstati (im, IM_BUFSIZE)

        # Compute the new required image i/o buffer size in chars.
        if (IS_INDEFI(req_size)) {
            new_imbufsize = IM_LEN(im,1) * sizeof (IM_PIXTYPE(im))
	    do i = 2, IM_NDIM(im)
		new_imbufsize = new_imbufsize * IM_LEN(im,i)
        } else {
            new_imbufsize = req_size
        }

	# If the default image i/o buffer size is already bigger than
	# the requested size do nothing.
	if (def_size >= new_imbufsize) {
	    buf_size = def_size
	    return
	}

        # Reset the image i/o buffer.
        call imseti (im, IM_BUFSIZE, new_imbufsize)
        call imseti (im, IM_BUFFRAC, 0)
	buf_size = new_imbufsize
end