aboutsummaryrefslogtreecommitdiff
path: root/noao/obsutil/src/starfocus/stfprofile.x
blob: d26c085d395b357636ce1a261155b720359c6727 (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
include	<imhdr.h>
include	<mach.h>
include	<math.h>
include	<math/iminterp.h>
include	<math/nlfit.h>
include	"starfocus.h"


# STF_FIND      -- Find the object and return the data raster and object center.
# STF_BKGD      -- Compute the background.
# STF_PROFILE   -- Compute enclosed flux profile, derivative, and moments.
# STF_NORM      -- Renormalized enclosed flux profile
# STF_WIDTHS    -- Set widths.
# STF_I2R       -- Radius from sample index.
# STF_R2I       -- Sample index from radius.
# STF_R2N       -- Number of subsamples from radius.
# STF_MODEL     -- Return model values.
# STF_DFWHM     -- Direct FWHM from profile.
# STF_FWHMS     -- Measure FWHM vs level.
# STF_RADIUS    -- Measure the radius at the specified level.
# STF_FIT       -- Fit model.
# STF_GAUSS1    -- Gaussian function used in NLFIT.
# STF_GAUSS2    -- Gaussian function and derivatives used in NLFIT.
# STF_MOFFAT1   -- Moffat function used in NLFIT.
# STF_MOFFAT2   -- Moffat function and derivatives used in NLFIT.


# STF_FIND -- Find the object and return the data raster and object center.
# Centering uses centroid of marginal distributions of data above the mean.

procedure stf_find (sf, sfd, im)

pointer	sf			#I Starfocus pointer
pointer	sfd			#I Object pointer
pointer	im			#I Image pointer

long	lseed
int	i, j, k, x1, x2, y1, y2, nx, ny, npts
real	radius, buffer, width, xc, yc, xlast, ylast, r1, r2
real	mean, sum, sum1, sum2, sum3, asumr(), urand()
pointer	data, ptr, imgs2r()
errchk	imgs2r

begin
	radius = max (3., SFD_RADIUS(sfd))
	buffer = SF_SBUF(sf)
	width = SF_SWIDTH(sf)

	xc = SFD_X(sfd)
	yc = SFD_Y(sfd)
	r1 = radius + buffer + width
	r2 = radius

	# Iterate on the center finding.
	do k = 1, 3 {

	    # Extract region around current center.
	    xlast = xc
	    ylast = yc

	    x1 = max (1-NBNDRYPIX, nint (xc - r2))
	    x2 = min (IM_LEN(im,1)+NBNDRYPIX, nint (xc + r2))
	    nx = x2 - x1 + 1
	    y1 = max (1-NBNDRYPIX, nint (yc - r2))
	    y2 = min (IM_LEN(im,2)+NBNDRYPIX, nint (yc + r2))
	    ny = y2 - y1 + 1
	    npts = nx * ny
	    data = imgs2r (im, x1, x2, y1, y2)

	    # Find center of gravity of marginal distributions above mean.
	    npts = nx * ny
	    sum = asumr (Memr[data], npts)
	    mean = sum / nx
	    sum1 = 0.
	    sum2 = 0.

	    do i = x1, x2 {
		ptr = data + i - x1
		sum3 = 0.
		do j = y1, y2 {
		    sum3 = sum3 + Memr[ptr]
		    ptr = ptr + nx
		}
		sum3 = sum3 - mean
		if (sum3 > 0.) {
		    sum1 = sum1 + i * sum3
		    sum2 = sum2 + sum3
		}
	    }
	    if (sum2 <= 0)
		call error (1, "Centering failed to converge")
	    xc = sum1 / sum2
	    if (xlast - xc > 0.2 * nx)
		xc = xlast - 0.2 * nx
	    if (xc - xlast > 0.2 * nx)
		xc = xlast + 0.2 * nx

	    ptr = data
	    mean = sum / ny
	    sum1 = 0.
	    sum2 = 0.
	    do j = y1, y2 {
		sum3 = 0.
		do i = x1, x2 {
		    sum3 = sum3 + Memr[ptr]
		    ptr = ptr + 1
		}
		sum3 = sum3 - mean
		if (sum3 > 0.) {
		    sum1 = sum1 + j * sum3
		    sum2 = sum2 + sum3
		}
	    }
	    if (sum2 <= 0)
		call error (1, "Centering failed to converge")
	    yc = sum1 / sum2
	    if (ylast - yc > 0.2 * ny)
		yc = ylast - 0.2 * ny
	    if (yc - ylast > 0.2 * ny)
		yc = ylast + 0.2 * ny

	    if (nint(xc) == nint(xlast) && nint(yc) == nint(ylast))
		break
	}

	# Get a new centered raster if necessary.
	if (nint(xc) != nint(xlast) || nint(yc) != nint(ylast) || r2 < r1) {
	    x1 = max (1-NBNDRYPIX, nint (xc - r1))
	    x2 = min (IM_LEN(im,1)+NBNDRYPIX, nint (xc + r1))
	    nx = x2 - x1 + 1
	    y1 = max (1-NBNDRYPIX, nint (yc - r1))
	    y2 = min (IM_LEN(im,2)+NBNDRYPIX, nint (yc + r1))
	    ny = y2 - y1 + 1
	    npts = nx * ny
	    data = imgs2r (im, x1, x2, y1, y2)
	}

	# Add a dither for integer data.  The random numbers are always
	# the same to provide reproducibility.

	i = IM_PIXTYPE(im)
	if (i == TY_SHORT || i == TY_INT || i == TY_LONG) {
	    lseed = 1
	    do i = 0, npts-1
		Memr[data+i] = Memr[data+i] + urand(lseed) - 0.5
	}

	SFD_DATA(sfd) = data
	SFD_X1(sfd) = x1
	SFD_X2(sfd) = x2
	SFD_Y1(sfd) = y1
	SFD_Y2(sfd) = y2
	SFD_X(sfd) = xc
	SFD_Y(sfd) = yc
end


# STF_BKGD -- Compute the background.
# A mode is estimated from the minimum slope in the sorted background pixels
# with a bin width of 5%.

procedure stf_bkgd (sf, sfd)

pointer	sf			#I Parameter structure
pointer	sfd			#I Star structure

int	i, j, x1, x2, y1, y2, xc, yc, nx, ny, npts, ns, nsat
real	sat, bkgd, miso
real	r, r1, r2, r3, dx, dy, dz
pointer	sp, data, bdata, ptr

begin
	data = SFD_DATA(sfd)
	x1 = SFD_X1(sfd)
	x2 = SFD_X2(sfd)
	y1 = SFD_Y1(sfd)
	y2 = SFD_Y2(sfd)
	xc = SFD_X(sfd)
	yc = SFD_Y(sfd)

	nx = x2 - x1 + 1
	ny = y2 - y1 + 1
	npts = nx * ny

	ns = 0
	nsat = 0
	r1 = SFD_RADIUS(sfd) ** 2
	r2 = (SFD_RADIUS(sfd) + SF_SBUF(sf)) ** 2
	r3 = (SFD_RADIUS(sfd) + SF_SBUF(sf) + SF_SWIDTH(sf)) ** 2
	sat = SF_SAT(sf)
	if (IS_INDEF(sat))
	    sat = MAX_REAL

	call smark (sp)
	call salloc (bdata, npts, TY_REAL)

	ptr = data
	do j = y1, y2 {
	    dy = (yc - j) ** 2
	    do i = x1, x2 {
		dx = (xc - i) ** 2
		r = dx + dy
		if (r <= r1) {
		    if (Memr[ptr] >= sat)
			nsat = nsat + 1
		} else if (r >= r2 && r <= r3) {
		    Memr[bdata+ns] = Memr[ptr]
		    ns = ns + 1
		}
		ptr = ptr + 1
	    }
	}

	if (ns > 9) {
	    call asrtr (Memr[bdata], Memr[bdata], ns)
	    r = Memr[bdata+ns-1] - Memr[bdata]
	    bkgd = Memr[bdata] + r / 2
	    miso = r / 2

	    j = 1 + 0.50 * ns
	    do i = 0, ns - j {
		dz = Memr[bdata+i+j-1] - Memr[bdata+i]
		if (dz < r) {
		    r = dz
		    bkgd = Memr[bdata+i] + dz / 2
		    miso = dz / 2
		}
	    }
	} else {
	    bkgd = 0.
	    miso = 0.
	}

	SFD_BKGD1(sfd) = bkgd
	SFD_BKGD(sfd) = bkgd
	SFD_MISO(sfd) = miso
	SFD_NSAT(sfd) = nsat

	call sfree (sp)
end


# STF_PROFILE -- Compute enclosed flux profile, derivative, direct FWHM, and
# profile moments..
# 1.  The flux profile is normalized at the maximum value.
# 2.  The radial profile is computed from the numerical derivative of the
#     enclose flux profile.

procedure stf_profile (sf, sfd)

pointer	sf			#I Parameter structure
pointer	sfd			#I Star structure

int	np
real	radius, xc, yc

int	i, j, k, l, m, ns, nx, ny, x1, x2, y1, y2
real	bkgd, miso, sigma, peak
real	r, r1, r2, r3, dx, dy, dx1, dx2, dy1, dy2, dz, xx, yy, xy, ds, da
pointer	sp, data, profile, ptr, asi, msi, gs
int	stf_r2n()
real	asieval(), msieval(), gseval(), stf_i2r(), stf_r2i()
errchk	asiinit, asifit, msiinit, msifit, gsrestore

real	gsdata[24]
data	gsdata/ 1., 4., 4., 1., 0., 0.6726812, 1., 2., 1.630641, 0.088787,
		0.00389378, -0.001457133, 0.3932125, -0.1267456, -0.004864541,
		0.00249941, 0.03078612, 0.02731274, -4.875850E-4, 2.307464E-4,
		-0.002134843, 0.007603908, -0.002552385, -8.010564E-4/

begin
	data = SFD_DATA(sfd)
	x1 = SFD_X1(sfd)
	x2 = SFD_X2(sfd)
	y1 = SFD_Y1(sfd)
	y2 = SFD_Y2(sfd)
	xc = SFD_X(sfd)
	yc = SFD_Y(sfd)
	bkgd = SFD_BKGD(sfd)
	miso = SFD_MISO(sfd)
	radius = SFD_RADIUS(sfd)
	np = SFD_NP(sfd)

	nx = x2 - x1 + 1
	ny = y2 - y1 + 1

	# Use an image interpolator fit to the data.
	call msiinit (msi, II_BISPLINE3)
	call msifit (msi, Memr[data], nx, ny, nx)

	# To avoid trying to interpolate outside the center of the
	# edge pixels, a requirement of the interpolator functions,
	# we reset the data limits.
	x1 = x1 + 1
	x2 = x2 - 1
	y1 = y1 + 1
	y2 = y2 - 1

	# Compute the enclosed flux profile, its derivative, and moments.
	call smark (sp)
	call salloc (profile, np, TY_REAL)
	call aclrr (Memr[profile], np)

	xx = 0.
	yy = 0.
	xy = 0.
	do j = y1, y2 {
	    ptr = data + (j-y1+1)*nx + 1
	    dy = j - yc
	    do i = x1, x2 {
		dx = i - xc

		# Set the subpixel sampling which may be a function of radius.
		r = sqrt (dx * dx + dy * dy)
		ns = stf_r2n (r)
		ds = 1. / ns
		da = ds * ds
		dz = 0.5 + 0.5 * ds

		# Sum the interpolator values over the subpixels and compute
		# an offset to give the correct total for the pixel.

		r2 = 0.
		dy1 = dy - dz
		do l = 1, ns {
		    dy1 = dy1 + ds
		    dy2 = dy1 * dy1
		    dx1 = dx - dz
		    do k = 1, ns {
			dx1 = dx1 + ds
			dx2 = dx1 * dx1
			r1 = msieval (msi, dx1+xc-x1+2, dy1+yc-y1+2)
			r2 = r2 + r1
		    }
		}

		r1 = Memr[ptr] - bkgd
		ptr = ptr + 1
		r2 = r1 - r2 * da

		# Accumulate the enclosed flux over the sub pixels.
		dy1 = dy - dz
		do l = 1, ns {
		    dy1 = dy1 + ds
		    dy2 = dy1 * dy1
		    dx1 = dx - dz
		    do k = 1, ns {
			dx1 = dx1 + ds
			dx2 = dx1 * dx1
			r = max (0., sqrt (dx2 + dy2) - ds / 2)
			if (r < radius) {
			    r1 = da * (msieval (msi, dx1+xc-x1+2, dy1+yc-y1+2) +
				r2)

			    # Use approximation for fractions of a subpixel.
			    for (m=stf_r2i(r)+1; m<=np; m=m+1) {
				r3 = (stf_i2r (real(m)) - r) / ds
				if (r3 >= 1.)
				    break
				Memr[profile+m-1] = Memr[profile+m-1] + r3 * r1
			    }

			    # The subpixel is completely within these radii.
			    for (; m<=np; m=m+1)
				Memr[profile+m-1] = Memr[profile+m-1] + r1

			    # Accumulate the moments above an isophote.
			    if (r1 > miso) {
				xx = xx + dx2 * r1
				yy = yy + dy2 * r1
				xy = xy + dx1 * dy1 * r1
			    }
			}
		    }
		}
	    }
	}

	call msifree (msi)

	# Compute the ellipticity and position angle from the moments.
	r = (xx + yy)
	if (r > 0.) {
	    r1 = (xx - yy) / r
	    r2 = 2 * xy / r
	    SFD_E(sfd) = sqrt (r1**2 + r2**2)
	    SFD_PA(sfd) = RADTODEG (atan2 (r2, r1) / 2.)
	} else {
	    SFD_E(sfd) = 0.
	    SFD_PA(sfd) = 0.
	}

	# The magnitude and profile normalization is from the max enclosed flux.
	call alimr (Memr[profile], np, r, SFD_M(sfd))
	if (SFD_M(sfd) <= 0.)
	    call error (1, "Invalid flux profile")
	call adivkr (Memr[profile], SFD_M(sfd), Memr[profile], np)

	# Fit interpolator to the enclosed flux profile.
	call asiinit (asi, II_SPLINE3)
	call asifit (asi, Memr[profile], np)
	SFD_ASI1(sfd) = asi

	# Estimate a gaussian sigma (actually sqrt(2)*sigma) and if it is
	# it is small subtract the gaussian so that the image interpolator
	# can more accurately estimate subpixel values.

	#call stf_radius (sf, sfd, SF_LEVEL(sf), r)
	#sigma = r / sqrt (log (1/(1-SF_LEVEL(sf))))
	call stf_radius (sf, sfd, 0.8, r)
	r = r / SF_SCALE(sf)
	sigma = 2 * r * sqrt (log(2.) / log (1/(1-0.8)))
	if (sigma < 5.) {
	    if (sigma <= 2.) {
		call gsrestore (gs, gsdata)
		dx = xc - nint (xc)
		dy = yc - nint (yc)
		r = sqrt (dx * dx + dy * dy)
		dx = 1.
		ds = abs (sigma - gseval (gs, r, dx))
		for (da = 1.; da <= 2.; da = da + .01) {
		    dz = abs (sigma - gseval (gs, r, da))
		    if (dz < ds) {
			ds = dz
			dx = da
		    }
		}
		sigma = dx
		call gsfree (gs)
	    }

	    sigma = sigma / (2 * sqrt (log(2.)))
	    sigma = sigma * sigma

	    # Compute the peak that gives the correct central pixel value.
	    i = nint (xc)
	    j = nint (yc)
	    dx = i - xc
	    dy = j - yc
	    r = sqrt (dx * dx + dy * dy)
	    ns = stf_r2n (r)
	    ds = 1. / ns
	    da = ds * ds
	    dz = 0.5 + 0.5 * ds

	    r1 = 0.
	    dy1 = dy - dz
	    do l = 1, ns {
		dy1 = dy1 + ds
		dy2 = dy1 * dy1
		dx1 = dx - dz
		do k = 1, ns {
		    dx1 = dx1 + ds
		    dx2 = dx1 * dx1
		    r2 = (dx2 + dy2) / sigma
		    if (r2 < 25.)
			r1 = r1 + exp (-r2)
		}
	    }
	    ptr = data + (j - y1 + 1) * nx + (i - x1 + 1)
	    peak = (Memr[ptr] - bkgd) / (r1 * da)

	    # Subtract the gaussian from the data.
	    do j = y1, y2 {
		ptr = data + (j - y1 + 1) * nx + 1
		dy = j - yc
		do i = x1, x2 {
		    dx = i - xc
		    r = sqrt (dx * dx + dy * dy)
		    ns = stf_r2n (r)
		    ds = 1. / ns
		    da = ds * ds
		    dz = 0.5 + 0.5 * ds

		    r1 = 0.
		    dy1 = dy - dz
		    do l = 1, ns {
			dy1 = dy1 + ds
			dy2 = dy1 * dy1
			dx1 = dx - dz
			do k = 1, ns {
			    dx1 = dx1 + ds
			    dx2 = dx1 * dx1
			    r2 = (dx2 + dy2) / sigma
			    if (r2 < 25.)
				r1 = r1 + peak * exp (-r2)
			}
		    }
		    Memr[ptr] = Memr[ptr] - r1 * da
		    ptr = ptr + 1
		}
	    }

	    # Fit the image interpolator to the residual data.
	    call msiinit (msi, II_BISPLINE3)
	    call msifit (msi, Memr[data], nx, ny, nx)

	    # Recompute the enclosed flux profile and moments
	    # using the gaussian plus image interpolator fit to the residuals.

	    call aclrr (Memr[profile], np)

	    xx = 0.
	    yy = 0.
	    xy = 0.
	    do j = y1, y2 {
		ptr = data + (j - y1 + 1) * nx + 1
		dy = j - yc
		do i = x1, x2 {
		    dx = i - xc
		    r = sqrt (dx * dx + dy * dy)
		    ns = stf_r2n (r)
		    ds = 1. / ns
		    da = ds * ds
		    dz = 0.5 + 0.5 * ds

		    # Compute interpolator correction.
		    r2 = 0.
		    dy1 = dy - dz
		    do l = 1, ns {
			dy1 = dy1 + ds
			dx1 = dx - dz
			do k = 1, ns {
			    dx1 = dx1 + ds
			    r1 = msieval (msi, dx1+xc-x1+2, dy1+yc-y1+2)
			    r2 = r2 + r1
			}
		    }

		    r1 = Memr[ptr] - bkgd
		    ptr = ptr + 1
		    r2 = r1 - r2 * da

		    # Accumulate the enclosed flux and moments.
		    dy1 = dy - dz
		    do l = 1, ns {
			dy1 = dy1 + ds
			dy2 = dy1 * dy1
			dx1 = dx - dz
			do k = 1, ns {
			    dx1 = dx1 + ds
			    dx2 = dx1 * dx1
			    r3 = (dx2 + dy2) / sigma
			    if (r3 < 25.)
				r3 = peak * exp (-r3)
			    else
				r3 = 0.
			    r = max (0., sqrt (dx2 + dy2) - ds / 2)
			    if (r < radius) {
				r1 = msieval (msi, dx1+xc-x1+2, dy1+yc-y1+2)
				r1 = da * (r1 + r2 + r3)

				for (m=stf_r2i(r)+1; m<=np; m=m+1) {
				    r3 = (stf_i2r (real(m)) - r) / ds
				    if (r3 >= 1.)
					break
				    Memr[profile+m-1] = Memr[profile+m-1] +
					r3 * r1
				}
				for (; m<=np; m=m+1)
				    Memr[profile+m-1] = Memr[profile+m-1] + r1

				if (r1 > miso) {
				    xx = xx + dx2 * r1
				    yy = yy + dy2 * r1
				    xy = xy + dx1 * dy1 * r1
				}
			    }
			}
		    }
		}
	    }

	    call msifree (msi)

	    # Recompute the moments, magnitude, normalized flux, and interp.
	    r = (xx + yy)
	    if (r > 0.) {
		r1 = (xx - yy) / r
		r2 = 2 * xy / r
		SFD_E(sfd) = sqrt (r1**2 + r2**2)
		SFD_PA(sfd) = RADTODEG (atan2 (r2, r1) / 2.)
	    } else {
		SFD_E(sfd) = 0.
		SFD_PA(sfd) = 0.
	    }

	    call alimr (Memr[profile], np, r, SFD_M(sfd))
	    if (SFD_M(sfd) <= 0.)
		call error (1, "Invalid flux profile")
	    call adivkr (Memr[profile], SFD_M(sfd), Memr[profile], np)

	    call asifit (asi, Memr[profile], np)
	    SFD_ASI1(sfd) = asi
	}

	# Compute derivative of enclosed flux profile and fit an image
	# interpolator.

	dx = 0.25
	Memr[profile] = 0.
	ns = 0
	do i = 1, np {
	    r = stf_i2r (real(i))
	    r2 = stf_r2i (r + dx)
	    if (r2 > np) {
		k = i
		break
	    }
	    r1 = stf_r2i (r - dx)
	    if (r1 < 1) {
		if (i > 1) {
		    dy = asieval (asi, real(i)) / r**2
		    Memr[profile] = (ns * Memr[profile] + dy) / (ns + 1)
		    ns = ns + 1
		}
		j = i
	    } else {
		dy = (asieval (asi, r2) - asieval (asi, r1)) /
		    (4 * r * dx)
		Memr[profile+i-1] = dy
	    }
	}
	do i = 2, j
	    Memr[profile+i-1] = (Memr[profile+j] - Memr[profile]) / j *
		(i - 1) + Memr[profile]
	do i = k, np
	    Memr[profile+i-1] = Memr[profile+k-2]

	call adivkr (Memr[profile], SF_SCALE(sf)**2, Memr[profile], np)
	call alimr (Memr[profile], np, SFD_YP1(sfd), SFD_YP2(sfd))
	call asiinit (asi, II_SPLINE3)
	call asifit (asi, Memr[profile], np)
	SFD_ASI2(sfd) = asi
	#SF_XP1(sf) = j+1
	SF_XP1(sf) = 1
	SF_XP2(sf) = k-1

	call sfree (sp)
end


# STF_NORM -- Renormalize the enclosed flux profile.

procedure stf_norm (sf, sfd, x, y)

pointer	sf			#I Parameter structure
pointer	sfd			#I Star structure
real	x			#I Radius
real	y			#I Flux

int	npmax, np
pointer	asi

int	i, j, k
real	r, r1, r2, dx, dy
pointer	sp, profile
real	asieval(), stf_i2r(), stf_r2i()
errchk	asifit

begin
	npmax = SFD_NPMAX(sfd)
	np = SFD_NP(sfd)
	asi = SFD_ASI1(sfd)

	call smark (sp)
	call salloc (profile, npmax, TY_REAL)

	# Renormalize the enclosed flux profile.
	if (IS_INDEF(x) || x <= 0.) {
	    dy = SFD_BKGD(sfd) - SFD_BKGD1(sfd)
	    SFD_BKGD(sfd) = SFD_BKGD(sfd) - dy
	    do i = 1, npmax
		Memr[profile+i-1] = asieval (asi, real(i)) +
		    dy * stf_i2r(real(i)) ** 2
	    call alimr (Memr[profile], np, r1, r2)
	    call adivkr (Memr[profile], r2, Memr[profile], npmax)
	} else if (IS_INDEF(y)) {
	    r = max (1., min (real(np), stf_r2i (x)))
	    r2 = asieval (asi, r)
	    if (r2 <= 0.)
		return
	    do i = 1, npmax
		Memr[profile+i-1] = asieval (asi, real(i))
	    call adivkr (Memr[profile], r2, Memr[profile], npmax)
	} else {
	    r = max (1., min (real(np), stf_r2i (x)))
	    r1 = asieval (asi, r)
	    dy = (y - r1) / x ** 2
	    SFD_BKGD(sfd) = SFD_BKGD(sfd) - dy
	    do i = 1, npmax
		Memr[profile+i-1] = asieval (asi, real(i)) +
		    dy * stf_i2r(real(i)) ** 2
	}

	call asifit (asi, Memr[profile], npmax)
	SFD_ASI1(sfd) = asi

	# Compute derivative of enclosed flux profile and fit an image
	# interpolator.

	dx = 0.25
	do i = 1, npmax {
	    r = stf_i2r (real(i))
	    r2 = stf_r2i (r + dx)
	    if (r2 > np) {
		k = i
		break
	    }
	    r1 = stf_r2i (r - dx)
	    if (r1 < 1) {
		if (i > 1) {
		    dy = asieval (asi, real(i)) / r**2
		    Memr[profile] = dy
		}
		j = i
	    } else {
		dy = (asieval (asi, r2) - asieval (asi, r1)) /
		    (4 * r * dx)
		Memr[profile+i-1] = dy
	    }
	}
	do i = 2, j
	    Memr[profile+i-1] = (Memr[profile+j] - Memr[profile]) / j *
		(i - 1) + Memr[profile]
	do i = k, npmax
	    Memr[profile+i-1] = Memr[profile+k-2]

	call adivkr (Memr[profile], SF_SCALE(sf)**2, Memr[profile], np)
	call alimr (Memr[profile], np, SFD_YP1(sfd), SFD_YP2(sfd))
	asi = SFD_ASI2(sfd)
	call asifit (asi, Memr[profile], np)
	SFD_ASI2(sfd) = asi
	#SF_XP1(sf) = min (j+1, np)
	SF_XP1(sf) = 1
	SF_XP2(sf) = min (k-1, np)

	call sfree (sp)
end


# STF_WIDTHS -- Set the widhts.

procedure stf_widths (sf, sfd)

pointer	sf			#I Main data structure
pointer	sfd			#I Star data structure

errchk	stf_radius, stf_dfwhm, stf_fit

begin
	call stf_radius (sf, sfd, SF_LEVEL(sf), SFD_R(sfd))
	call stf_dfwhm (sf, sfd)
	call stf_fit (sf, sfd)

	switch (SF_WCODE(sf)) {
	case 1:
	    SFD_W(sfd) = SFD_R(sfd)
	case 2:
	    SFD_W(sfd) = SFD_DFWHM(sfd)
	case 3:
	    SFD_W(sfd) = SFD_GFWHM(sfd)
	case 4:
	    SFD_W(sfd) = SFD_MFWHM(sfd)
	}
end


# STF_I2R -- Compute radius from sample index.

real procedure stf_i2r (i)

real	i			#I Index
real	r			#O Radius

begin
	if (i < 20)
	    r = 0.05 * i
	else if (i < 30)
	    r = 0.1 * i - 1
	else if (i < 40)
	    r = 0.2 * i - 4
	else if (i < 50)
	    r = 0.5 * i - 16
	else
	    r = i - 41
	return (r)
end


# STF_R2I -- Compute sample index from radius.

real procedure stf_r2i (r)

real	r			#I Radius
real	i			#O Index

begin
	if (r < 1)
	    i = 20 * r
	else if (r < 2)
	    i = 10 * (r + 1)
	else if (r < 4)
	    i = 5 * (r + 4)
	else if (r < 9)
	    i = 2 * (r + 16)
	else
	    i = r + 41
	return (i)
end


# STF_R2N -- Compute number of subsamples from radius.

int procedure stf_r2n (r)

real	r			#I Radius
int	n			#O Number of subsamples

begin
	if (r < 1)
	    n = 20
	else if (r < 2)
	    n = 10
	else if (r < 4)
	    n = 5
	else if (r < 9)
	    n = 2
	else
	    n = 1
	return (n)
end


# STF_MODEL -- Return model value.

procedure stf_model (sf, sfd, r, profile, flux)

pointer	sf			#I Main data structure
pointer	sfd			#I Star data structure
real	r			#I Radius at level
real	profile			#I Profile value
real	flux			#I Enclosed flux value

real	x, x1, x2, r1, r2, dr

begin
	dr = 0.25 * SF_SCALE(sf)
	r1 = r - dr
	r2 = r + dr
	if (r1 < 0.) {
	    r1 = dr
	    r2 = r1 + dr
	}

	switch (SF_WCODE(sf)) {
	case 3:
	    x = r**2 / (2. * SFD_SIGMA(sfd)**2)
	    if (x < 20.)
		flux = 1 - exp (-x)
	    else
		flux = 0.

	    x1 = r1**2 / (2. * SFD_SIGMA(sfd)**2)
	    x2 = r2**2 / (2. * SFD_SIGMA(sfd)**2)
	    if (x2 < 20.) {
		x1 = 1 - exp (-x1)
		x2 = 1 - exp (-x2)
	    } else {
		x1 = 1.
		x2 = 1.
	    }
	    if (r <= dr) {
		x1 = x1 / dr ** 2
		x2 = x2 / (4 * dr ** 2)
		profile = (x2 - x1) / dr * r + x1
	    } else {
		profile = (x2 - x1) / (4 * r * dr)
	    }
	default:
	    x = 1 + (r / SFD_ALPHA(sfd)) ** 2
	    flux = 1 - x ** (1 - SFD_BETA(sfd))

	    x1 = 1 + (r1 / SFD_ALPHA(sfd)) ** 2
	    x2 = 1 + (r2 / SFD_ALPHA(sfd)) ** 2
	    x1 = 1 - x1 ** (1 - SFD_BETA(sfd))
	    x2 = 1 - x2 ** (1 - SFD_BETA(sfd))
	    if (r <= dr) {
		x1 = x1 / dr ** 2
		x2 = x2 / (4 * dr ** 2)
		profile = (x2 - x1) / dr * r + x1
	    } else {
		profile = (x2 - x1) / (4 * r * dr)
	    }
	}
end


# STF_DFWHM -- Direct FWHM from profile.

procedure stf_dfwhm (sf, sfd)

pointer	sf			#I Main data structure
pointer	sfd			#I Star data structure

int	np
real	r, rpeak, profile, peak, asieval(), stf_i2r()
pointer	asi

begin
	asi = SFD_ASI2(sfd)
	np = SFD_NP(sfd)

	rpeak = 1.
	peak = 0.
	for (r=1.; r <= np; r = r + 0.01) {
	    profile = asieval (asi, r)
	    if (profile > peak) {
		rpeak = r
		peak = profile
	    }
	}
	
	peak = peak / 2.
	for (r=rpeak; r <= np && asieval (asi, r) > peak; r = r + 0.01)
	    ;

	SFD_DFWHM(sfd) = 2 * stf_i2r (r) * SF_SCALE(sf)
end


# STF_FWHMS -- Measure FWHM vs level.

procedure stf_fwhms (sf, sfd)

pointer	sf			#I Main data structure
pointer	sfd			#I Star data structure

int	i
real	level, r

begin
	do i = 1, 19 {
	    level = i * 0.05
	    call stf_radius (sf, sfd, level, r)
	    switch (SF_WCODE(sf)) {
	    case 3:
		SFD_FWHM(sfd,i) = 2 * r * sqrt (log (2.) / log (1/(1-level)))
	    default:
		r = r / sqrt ((1.-level)**(1./(1.-SFD_BETA(sfd))) - 1.)
		SFD_FWHM(sfd,i) = 2 * r * sqrt (2.**(1./SFD_BETA(sfd))-1.)
	    }
	}
end


# STF_RADIUS -- Measure the radius at the specified level.

procedure stf_radius (sf, sfd, level, r)

pointer	sf			#I Main data structure
pointer	sfd			#I Star data structure
real	level			#I Level to measure
real	r			#O Radius

int	np
pointer	asi
real	f, fmax, rmax, asieval(), stf_i2r()

begin
	np = SFD_NP(sfd)
	asi = SFD_ASI1(sfd)

	for (r=1; r <= np && asieval (asi, r) < level; r = r + 0.01)
	    ;
	if (r > np) {
	    fmax = 0.
	    rmax = 0.
	    for (r=1; r <= np; r = r + 0.01) {
		f = asieval (asi, r)
		if (f > fmax) {
		    fmax = f
		    rmax = r
		}
	    }
	    r = rmax
	}
	r = stf_i2r (r) * SF_SCALE(sf)
end


# STF_FIT -- Fit models to enclosed flux.

procedure stf_fit (sf, sfd)

pointer	sf			#I Main data structure
pointer	sfd			#I Star data structure

int	i, j, n, np, pfit[2]
real	beta, z, params[3]
pointer	asi, nl
pointer	sp, x, y, w

int	locpr()
real	asieval(), stf_i2r()
extern	stf_gauss1(), stf_gauss2(), stf_moffat1(), stf_moffat2()
errchk	nlinitr, nlfitr

data	pfit/2,3/

begin
	np = SFD_NP(sfd)
	asi = SFD_ASI1(sfd)

	call smark (sp)
	call salloc (x, np, TY_REAL)
	call salloc (y, np, TY_REAL)
	call salloc (w, np, TY_REAL)

	n = 0
	j = 0
	do i = 1, np {
	    z = 1. - max (0., asieval (asi, real(i)))
	    if (n > np/3 && z < 0.5)
		break
	    if ((n < np/3 && z > 0.01) || z > 0.5)
		j = n

	    Memr[x+n] = stf_i2r (real(i)) * SF_SCALE(sf)
	    Memr[y+n] = z
	    Memr[w+n] = 1.
	    n = n + 1
	}

	# Gaussian.
	np = 1
	params[2] = Memr[x+j] / sqrt (2. * log (1./min(0.99,Memr[y+j])))
	params[1] = 1
	call nlinitr (nl, locpr (stf_gauss1), locpr (stf_gauss2),
	    params, params, 2, pfit, np, .001, 100)
	call nlfitr (nl, Memr[x], Memr[y], Memr[w], n, 1, WTS_USER, i)
	if (i != SINGULAR && i != NO_DEG_FREEDOM) {
	    call nlpgetr (nl, params, i)
	    if (params[2] < 0.)
		params[2] = Memr[x+j] / sqrt (2. * log (1./min(0.99,Memr[y+j])))
	}
	SFD_SIGMA(sfd) = params[2]
	SFD_GFWHM(sfd) = 2 * SFD_SIGMA(sfd) * sqrt (2. * log (2.))

	# Moffat.
	if (SF_BETA(sf) < 1.1) {
	    call nlfreer (nl)
	    call sfree (sp)
	    call error (1, "Cannot measure FWHM - Moffat beta too small")
	}

	beta = SF_BETA(sf)
	if (IS_INDEFR(beta)) {
	    beta = 2.5
	    np = 2
	} else {
	    np = 1
	}
	params[3] = 1 - beta
	params[2] = Memr[x+j] / sqrt (min(0.99,Memr[y+j])**(1./params[3]) - 1.)
	params[1] = 1
	call nlinitr (nl, locpr (stf_moffat1), locpr (stf_moffat2),
	    params, params, 3, pfit, np, .001, 100)
	call nlfitr (nl, Memr[x], Memr[y], Memr[w], n, 1, WTS_USER, i)
	if (i != SINGULAR && i != NO_DEG_FREEDOM) {
	    call nlpgetr (nl, params, i)
	    if (params[2] < 0.) {
		params[3] = 1. - beta
		params[2] = Memr[x+j] /
		    sqrt (min(0.99,Memr[y+j])**(1./params[3]) - 1.)
	    }
	}
	SFD_ALPHA(sfd) = params[2]
	SFD_BETA(sfd) = 1 - params[3]
	SFD_MFWHM(sfd) = 2 * SFD_ALPHA(sfd) * sqrt (2.**(1./SFD_BETA(sfd))-1.)

	call nlfreer (nl)
	call sfree (sp)
end


# STF_GAUSS1 -- Gaussian function used in NLFIT.  The parameters are the
# amplitude and sigma and the input variable is the radius.

procedure stf_gauss1 (x, nvars, p, np, z)

real	x[nvars]		#I Input variables
int	nvars			#I Number of variables
real	p[np]			#I Parameter vector
int	np			#I Number of parameters
real	z			#O Function return

real	r2

begin
	r2 = x[1]**2 / (2 * p[2]**2)
	if (abs (r2) > 20.)
	    z = 0.
	else
	    z = p[1] * exp (-r2)
end


# STF_GAUSS2 -- Gaussian function and derivatives used in NLFIT.  The parameters
# are the amplitude and sigma and the input variable is the radius.

procedure stf_gauss2 (x, nvars, p, dp, np, z, der)

real	x[nvars]		#I Input variables
int	nvars			#I Number of variables
real	p[np]			#I Parameter vector
real	dp[np]			#I Dummy array of parameters increments
int	np			#I Number of parameters
real	z			#O Function return
real	der[np]			#O Derivatives

real	r2

begin
	r2 = x[1]**2 / (2 * p[2]**2)
	if (abs (r2) > 20.) {
	    z = 0.
	    der[1] = 0.
	    der[2] = 0.
	} else {
	    der[1] = exp (-r2)
	    z = p[1] * der[1]
	    der[2] = z * 2 * r2 / p[2]
	}
end


# STF_MOFFAT1 -- Moffat function used in NLFIT.  The parameters are the
# amplitude, alpha squared, and beta and the input variable is the radius.

procedure stf_moffat1 (x, nvars, p, np, z)

real	x[nvars]		#I Input variables
int	nvars			#I Number of variables
real	p[np]			#I Parameter vector
int	np			#I Number of parameters
real	z			#O Function return

real	y

begin
	y = 1 + (x[1] / p[2]) ** 2
	if (abs (y) > 20.)
	    z = 0.
	else
	    z = p[1] * y ** p[3]
end


# STF_MOFFAT2 -- Moffat function and derivatives used in NLFIT.  The
# parameters are the amplitude, alpha squared, and beta and the input
# variable is the radius.

procedure stf_moffat2 (x, nvars, p, dp, np, z, der)

real	x[nvars]		#I Input variables
int	nvars			#I Number of variables
real	p[np]			#I Parameter vector
real	dp[np]			#I Dummy array of parameters increments
int	np			#I Number of parameters
real	z			#O Function return
real	der[np]			#O Derivatives

real	y

begin
	y = 1 + (x[1] / p[2]) ** 2
	if (abs (y) > 20.) {
	    z = 0.
	    der[1] = 0.
	    der[2] = 0.
	    der[3] = 0.
	} else {
	    der[1] = y ** p[3]
	    z = p[1] * der[1]
	    der[2] = -2 * z / y * p[3] / p[2] * (x[1] / p[2]) ** 2
	    der[3] = z * log (y)
	}
end