aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/daophot/psf/dpspstars.x
blob: 69b14933fa79fc1f80d15f9e7568c8932b61d322 (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
include <mach.h>
include "../lib/daophotdef.h"
include "../lib/apseldef.h"
include "../lib/psfdef.h"

define	NCOLUMN	5

# DP_GPSTARS -- Select the psf stars.

procedure dp_gpstars (dao, im, tp_in, tp_out, text_file, maxnpsf, gd, mgd, id,
	mkstars, interactive, use_cmdfile)

pointer	dao			# pointer to the daophot structure
pointer	im			# pointer to the input image
int	tp_in			# the input file descriptor
int	tp_out			# the output file descriptor
bool	text_file		# text or table file
int	maxnpsf			# the maximum number of psf stars
pointer	gd			# pointer to the graphics stream
pointer	mgd			# pointer to the plot metacode file
pointer	id			# pointer to the image display stream
bool	mkstars			# mark the accepted and deleted stars
bool	interactive		# interactive mode
bool	use_cmdfile		# cursor command file mode

int	ier, npsf
pointer	apsel, sp, ocolpoint, index
real	radius
int	dp_pfstars(), dp_ipfstars()

begin
	# Get some daophot pointers.
	apsel = DP_APSEL(dao)

	# Define the radius for determining proximity.
	radius = DP_PSFRAD(dao) + DP_FITRAD(dao) + 2.0

	# Allocate some working memory.
	call smark (sp)
	call salloc (ocolpoint, NCOLUMN, TY_POINTER)
	call salloc (index, DP_APNUM(apsel), TY_INT)

	# Initialize the output file.
	if (text_file) {
	    call seek (tp_in, BOF)
	    call dp_apheader (tp_in, tp_out)
	    call dp_xpselpars (tp_out, DP_INIMAGE(dao), maxnpsf, DP_SCALE(dao),
	        DP_SPSFRAD(dao), DP_SFITRAD(dao))
	    call dp_xpbanner (tp_out)
	} else {
	    call dp_tpdefcol (tp_out, Memi[ocolpoint])
	    call tbhcal (tp_in, tp_out)
	    call dp_tpselpars (tp_out, DP_INIMAGE(dao), maxnpsf, DP_SCALE(dao),
	        DP_SPSFRAD(dao), DP_SFITRAD(dao))
	}

	# Change all the INDEFS to a large negative number.
	call dp_chindef (Memr[DP_APMAG(apsel)], Memr[DP_APMAG(apsel)],
	    DP_APNUM(apsel), -MAX_REAL)

	# Sort the stars.
	call quick (Memr[DP_APMAG(apsel)], DP_APNUM(apsel), Memi[index], ier)
	call dp_irectify (Memi[DP_APID(apsel)], Memi[index], DP_APNUM(apsel)) 
	call dp_rectify (Memr[DP_APXCEN(apsel)], Memi[index], DP_APNUM(apsel)) 
	call dp_rectify (Memr[DP_APYCEN(apsel)], Memi[index], DP_APNUM(apsel)) 
	call dp_rectify (Memr[DP_APMSKY(apsel)], Memi[index], DP_APNUM(apsel)) 

	# Select the stars and write them to the output file.
	if (use_cmdfile)
	    npsf = dp_ipfstars (dao, im, maxnpsf, -MAX_REAL, radius, gd,
		mgd, id, mkstars, false, false)
	else if (interactive)
	    npsf = dp_ipfstars (dao, im, maxnpsf, -MAX_REAL, radius, gd,
		mgd, id, mkstars, true, true)
	else
	    npsf = dp_pfstars (dao, im, Memi[DP_APID(apsel)],
	        Memr[DP_APXCEN(apsel)], Memr[DP_APYCEN(apsel)],
		Memr[DP_APMAG(apsel)], Memr[DP_APMSKY(apsel)],
		DP_APNUM(apsel), maxnpsf, -MAX_REAL, radius, mgd)

	if (DP_VERBOSE(dao) == YES) {
	    call printf ("\nTotal of %d PSF stars selected\n")
		call pargi (npsf)
	}

	# Write out the stars.
	call dp_wout (dao, im, Memr[DP_APXCEN(apsel)], Memr[DP_APYCEN(apsel)],
	    Memr[DP_APXCEN(apsel)], Memr[DP_APYCEN(apsel)], npsf)
	call dp_wpstars (tp_out, Memi[ocolpoint], text_file,
	    Memi[DP_APID(apsel)], Memr[DP_APXCEN(apsel)],
	    Memr[DP_APYCEN(apsel)], Memr[DP_APMAG(apsel)],
	    Memr[DP_APMSKY(apsel)], npsf)

	# Free memory.
	call sfree (sp)
end


# DP_CHINDEF -- Change all the INDEFS to a legal number which is a lower limit.

procedure dp_chindef (a, b, npix, lolimit)

real	a[ARB]			# the input array.
real	b[ARB]			# the output array.
int	npix			# number of points
real	lolimit			# the lower limit

int	i

begin
	do i = 1, npix {
	    if (IS_INDEFR(a[i]))
		b[i] = lolimit
	    else
		b[i] = a[i]
	}
end


# DP_PFSTARS -- Select the psf stars.

int procedure dp_pfstars (dao, im, ids, xcen, ycen, mag, sky, nstar, maxnpsf,
	lolimit, radius, mgd) 

pointer	dao			# pointer to the daophot structure
pointer	im			# pointer to the input image
int	ids[ARB]		# array of star ids
real	xcen[ARB]		# array of x coordinates
real	ycen[ARB]		# array of y coordinates
real	mag[ARB]		# array of magnitudes
real	sky[ARB]		# array of sky values
int	nstar			# the number of stars
int	maxnpsf			# the maximum number of psf stars
real	lolimit			# lower data limit
real	radius			# minimum separation
pointer	mgd			# pointer to the plot metacode file

bool	omit
int	istar, jstar, npsf
real	radsq, dy2, dr2
int	dp_addstar()

begin
	# Initialize the list.
	call dp_pseti (dao, PNUM, 0)

	# Set the radius.
	radsq = radius * radius

	# Get the first star.
	if ((mag[1] > lolimit) && (dp_addstar (dao, im, xcen[1], ycen[1],
	    INDEFR, ids[1], NULL, mgd, false)) == OK) {
	    npsf = 1
	} else
	    npsf = 0

	# Loop over the candidate psf stars.
	do istar = 2, nstar {

	    # Test for the maximum number of psf stars.
	    if (npsf >= maxnpsf)
		break

	    # Test that the candidate psf stars are not saturated and
	    # are sufficiently far from the edge of the frame.

	    if (mag[istar] <= lolimit)
		next

	    # Text that there are no brighter stars with a distance squared
	    # of radsq.
	    omit = false
	    do jstar = 1, istar - 1 {
		dy2 = abs (ycen[jstar] - ycen[istar])
		if (dy2 >= radius)
		    next
		dr2 = (xcen[jstar] - xcen[istar]) ** 2 + dy2 ** 2
		if (dr2 >= radsq)
		    next
		omit = true
		break
	    }

	    if (omit)
		next

	    if (dp_addstar (dao, im, xcen[istar], ycen[istar], INDEFR,
	        ids[istar], NULL, mgd, false) == ERR)
		next
	    npsf = npsf + 1
	}

	return (npsf)
end