aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/apphot/fitpsf/apsfmoments.x
blob: ddae00f2e77850e2669c20016a8b507864b6294d (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
include <math.h>
include "../lib/fitpsf.h"

define	NPARAMETERS	7

# APSFMOMENTS -- Procedure to compute the 0, 1st and second moments of an 
# image and estimate the x,y center, the ellipticity and the position angle.

int procedure apsfmoments (ctrpix, nx, ny, lthreshold, uthreshold, positive,
	par, perr, npar)

real	ctrpix[nx, ny]		# object to be centered
int	nx, ny			# dimensions of subarray
real	lthreshold		# lower threshold for moment computation
real	uthreshold		# upper threshold for moment computation
int	positive		# emission feature
real	par[ARB]		# parameters
real	perr[ARB]		# errors in parameters
int	npar			# number of parameters

int	i, j
real	temp, sumi, sumxi, sumyi, sumx2i, sumy2i, sumxyi, r2, varx, vary, varxy
bool	fp_equalr()

begin
	# Initialize the sums.
	sumi = 0.0
	sumxi = 0.0
	sumyi = 0.0
	sumxyi = 0.0
	sumx2i = 0.0
	sumy2i = 0.0

	# Accumulate the moments.
	if (positive == YES) {
	    do j = 1, ny {
	        do i = 1, nx {
		    if (ctrpix[i,j] > uthreshold)
			next
		    temp = ctrpix[i,j] - lthreshold
		    if (temp <= 0.0)
		        next
		    sumi = sumi + temp
		    sumxi = sumxi + i * temp
		    sumyi = sumyi + j * temp
		    sumxyi = sumxyi + i * j * temp
		    sumx2i = sumx2i + i * i * temp
		    sumy2i = sumy2i + j * j * temp
	        }
	    }
	} else {
	    do j = 1, ny {
	        do i = 1, nx {
		    if (ctrpix[i,j] < uthreshold)
			next
		    temp = lthreshold - ctrpix[i,j]
		    if (temp <= 0.0)
		        next
		    sumi = sumi + temp
		    sumxi = sumxi + i * temp
		    sumyi = sumyi + j * temp
		    sumxyi = sumxyi + i * j * temp
		    sumx2i = sumx2i + i * i * temp
		    sumy2i = sumy2i + j * j * temp
	        }
	    }
	}

	# Compute the parameters.
	if (fp_equalr (sumi, 0.0)) {
	    par[1] = 0.0
	    par[2] = (1 + nx) / 2.0
	    par[3] = (1 + ny) / 2.0
	    par[4] = 0.0
	    par[5] = 0.0
	    par[6] = 0.0
	    par[7] = lthreshold
	} else {
	    par[1] = sumi
	    par[2] = sumxi / sumi
	    par[3] = sumyi / sumi
	    varx = max (0.0, sumx2i / sumi - par[2] ** 2)
	    vary = max (0.0, sumy2i / sumi - par[3] ** 2)
	    r2 = varx + vary
	    if (r2 <= 0.0) {
		par[4] = 0.0
		par[5] = 0.0
		par[6] = 0.0
	    } else {
	        par[4] = sqrt (r2)
	        varxy = sumxyi / sumi - par[2] * par[3]
	        par[5] = sqrt ((varx - vary) ** 2 + 4.0 * varxy ** 2) / r2
		par[6] = (0.5 * r2 * (1.0 + par[5]) - vary)
		if (fp_equalr (par[6], 0.0))
		    par[6] = 90.0
		else
		    par[6] = RADTODEG (atan (varxy / par[6]))
	    }
	    par[7] = lthreshold
	}

	# Compute the errors.
	npar = NPARAMETERS
	call amovkr (0.0, perr, NPARAMETERS)
	return (AP_OK)
end